blob: eb4c5d120c649faabb6f7c9522353f4145a55228 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001" Test various aspects of the Vim9 script language.
2
Bram Moolenaar673660a2020-01-26 16:50:05 +01003source check.vim
Bram Moolenaar101f4812020-06-16 23:18:51 +02004source term_util.vim
Bram Moolenaarad39c092020-02-26 18:23:43 +01005source view_util.vim
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006source vim9.vim
Bram Moolenaare3d46852020-08-29 13:39:17 +02007source shared.vim
Bram Moolenaar37294bd2021-03-10 13:40:08 +01008source screendump.vim
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010def Test_range_only()
11 new
12 setline(1, ['blah', 'Blah'])
13 :/Blah/
14 assert_equal(2, getcurpos()[1])
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020015 bwipe!
16
17 # without range commands use current line
18 new
19 setline(1, ['one', 'two', 'three'])
20 :2
21 print
22 assert_equal('two', Screenline(&lines))
23 :3
24 list
25 assert_equal('three$', Screenline(&lines))
Bram Moolenaarb8554302021-02-15 21:30:30 +010026
27 # missing command does not print the line
28 var lines =<< trim END
29 vim9script
30 :1|
31 assert_equal('three$', Screenline(&lines))
32 :|
33 assert_equal('three$', Screenline(&lines))
34 END
35 CheckScriptSuccess(lines)
36
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020037 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +010038
39 # won't generate anything
40 if false
41 :123
42 endif
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020043enddef
44
Bram Moolenaara6e67e42020-05-15 23:36:40 +020045let g:alist = [7]
46let g:astring = 'text'
Bram Moolenaarf0b9f432020-07-17 23:03:17 +020047let g:anumber = 123
Bram Moolenaar6e587dc2020-02-06 13:15:52 +010048
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020049def Test_delfunction()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020050 # Check function is defined in script namespace
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020051 CheckScriptSuccess([
52 'vim9script',
53 'func CheckMe()',
54 ' return 123',
55 'endfunc',
56 'assert_equal(123, s:CheckMe())',
57 ])
58
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020059 # Check function in script namespace cannot be deleted
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020060 CheckScriptFailure([
61 'vim9script',
62 'func DeleteMe1()',
63 'endfunc',
64 'delfunction DeleteMe1',
65 ], 'E1084:')
66 CheckScriptFailure([
67 'vim9script',
68 'func DeleteMe2()',
69 'endfunc',
70 'def DoThat()',
71 ' delfunction DeleteMe2',
72 'enddef',
73 'DoThat()',
74 ], 'E1084:')
75 CheckScriptFailure([
76 'vim9script',
77 'def DeleteMe3()',
78 'enddef',
79 'delfunction DeleteMe3',
80 ], 'E1084:')
81 CheckScriptFailure([
82 'vim9script',
83 'def DeleteMe4()',
84 'enddef',
85 'def DoThat()',
86 ' delfunction DeleteMe4',
87 'enddef',
88 'DoThat()',
89 ], 'E1084:')
Bram Moolenaar925e9fd2020-07-25 15:41:11 +020090
91 # Check that global :def function can be replaced and deleted
Bram Moolenaarcfcd0112020-09-27 15:19:27 +020092 var lines =<< trim END
Bram Moolenaar925e9fd2020-07-25 15:41:11 +020093 vim9script
94 def g:Global(): string
95 return "yes"
96 enddef
97 assert_equal("yes", g:Global())
98 def! g:Global(): string
99 return "no"
100 enddef
101 assert_equal("no", g:Global())
102 delfunc g:Global
103 assert_false(exists('*g:Global'))
104 END
105 CheckScriptSuccess(lines)
106
107 # Check that global function can be replaced by a :def function and deleted
108 lines =<< trim END
109 vim9script
110 func g:Global()
111 return "yes"
112 endfunc
113 assert_equal("yes", g:Global())
114 def! g:Global(): string
115 return "no"
116 enddef
117 assert_equal("no", g:Global())
118 delfunc g:Global
119 assert_false(exists('*g:Global'))
120 END
121 CheckScriptSuccess(lines)
122
123 # Check that global :def function can be replaced by a function and deleted
124 lines =<< trim END
125 vim9script
126 def g:Global(): string
127 return "yes"
128 enddef
129 assert_equal("yes", g:Global())
130 func! g:Global()
131 return "no"
132 endfunc
133 assert_equal("no", g:Global())
134 delfunc g:Global
135 assert_false(exists('*g:Global'))
136 END
137 CheckScriptSuccess(lines)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200138enddef
139
Bram Moolenaar08052222020-09-14 17:04:31 +0200140def Test_wrong_type()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200141 CheckDefFailure(['var name: list<nothing>'], 'E1010:')
142 CheckDefFailure(['var name: list<list<nothing>>'], 'E1010:')
143 CheckDefFailure(['var name: dict<nothing>'], 'E1010:')
144 CheckDefFailure(['var name: dict<dict<nothing>>'], 'E1010:')
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100145
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200146 CheckDefFailure(['var name: dict<number'], 'E1009:')
147 CheckDefFailure(['var name: dict<list<number>'], 'E1009:')
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100148
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200149 CheckDefFailure(['var name: ally'], 'E1010:')
150 CheckDefFailure(['var name: bram'], 'E1010:')
151 CheckDefFailure(['var name: cathy'], 'E1010:')
152 CheckDefFailure(['var name: dom'], 'E1010:')
153 CheckDefFailure(['var name: freddy'], 'E1010:')
154 CheckDefFailure(['var name: john'], 'E1010:')
155 CheckDefFailure(['var name: larry'], 'E1010:')
156 CheckDefFailure(['var name: ned'], 'E1010:')
157 CheckDefFailure(['var name: pam'], 'E1010:')
158 CheckDefFailure(['var name: sam'], 'E1010:')
159 CheckDefFailure(['var name: vim'], 'E1010:')
Bram Moolenaara0a9f432020-04-28 21:29:34 +0200160
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200161 CheckDefFailure(['var Ref: number', 'Ref()'], 'E1085:')
162 CheckDefFailure(['var Ref: string', 'var res = Ref()'], 'E1085:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200163enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaar10c65862020-10-08 21:16:42 +0200165def Test_script_wrong_type()
166 var lines =<< trim END
167 vim9script
168 var s:dict: dict<string>
169 s:dict['a'] = ['x']
170 END
171 CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got list<string>', 3)
172enddef
173
Bram Moolenaar08052222020-09-14 17:04:31 +0200174def Test_const()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200175 CheckDefFailure(['final name = 234', 'name = 99'], 'E1018:')
176 CheckDefFailure(['final one = 234', 'var one = 99'], 'E1017:')
177 CheckDefFailure(['final list = [1, 2]', 'var list = [3, 4]'], 'E1017:')
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200178 CheckDefFailure(['final two'], 'E1125:')
179 CheckDefFailure(['final &option'], 'E996:')
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200180
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200181 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200182 final list = [1, 2, 3]
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200183 list[0] = 4
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200184 list->assert_equal([4, 2, 3])
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200185 const other = [5, 6, 7]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200186 other->assert_equal([5, 6, 7])
Bram Moolenaar71abe482020-09-14 22:28:30 +0200187
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200188 var varlist = [7, 8]
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200189 const constlist = [1, varlist, 3]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200190 varlist[0] = 77
191 # TODO: does not work yet
192 # constlist[1][1] = 88
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200193 var cl = constlist[1]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200194 cl[1] = 88
195 constlist->assert_equal([1, [77, 88], 3])
196
Bram Moolenaare0de1712020-12-02 17:36:54 +0100197 var vardict = {five: 5, six: 6}
198 const constdict = {one: 1, two: vardict, three: 3}
Bram Moolenaar71abe482020-09-14 22:28:30 +0200199 vardict['five'] = 55
200 # TODO: does not work yet
201 # constdict['two']['six'] = 66
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200202 var cd = constdict['two']
Bram Moolenaar71abe482020-09-14 22:28:30 +0200203 cd['six'] = 66
Bram Moolenaare0de1712020-12-02 17:36:54 +0100204 constdict->assert_equal({one: 1, two: {five: 55, six: 66}, three: 3})
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200205 END
206 CheckDefAndScriptSuccess(lines)
Bram Moolenaar08052222020-09-14 17:04:31 +0200207enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200209def Test_const_bang()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200210 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200211 const var = 234
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200212 var = 99
213 END
214 CheckDefExecFailure(lines, 'E1018:', 2)
215 CheckScriptFailure(['vim9script'] + lines, 'E46:', 3)
216
217 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200218 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200219 ll[0] = 99
220 END
221 CheckDefExecFailure(lines, 'E1119:', 2)
222 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
223
224 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200225 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200226 ll[3] = 99
227 END
228 CheckDefExecFailure(lines, 'E1118:', 2)
229 CheckScriptFailure(['vim9script'] + lines, 'E684:', 3)
230
231 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100232 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200233 dd["one"] = 99
234 END
235 CheckDefExecFailure(lines, 'E1121:', 2)
236 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
237
238 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100239 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200240 dd["three"] = 99
241 END
242 CheckDefExecFailure(lines, 'E1120:')
243 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
244enddef
245
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200246def Test_range_no_colon()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200247 CheckDefFailure(['%s/a/b/'], 'E1050:')
248 CheckDefFailure(['+ s/a/b/'], 'E1050:')
249 CheckDefFailure(['- s/a/b/'], 'E1050:')
250 CheckDefFailure(['. s/a/b/'], 'E1050:')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200251enddef
252
253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254def Test_block()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200255 var outer = 1
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 {
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200257 var inner = 2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258 assert_equal(1, outer)
259 assert_equal(2, inner)
260 }
261 assert_equal(1, outer)
Bram Moolenaar3f1e9f02021-02-27 22:36:43 +0100262
263 {|echo 'yes'|}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264enddef
265
Bram Moolenaar08052222020-09-14 17:04:31 +0200266def Test_block_failure()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200267 CheckDefFailure(['{', 'var inner = 1', '}', 'echo inner'], 'E1001:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200268 CheckDefFailure(['}'], 'E1025:')
269 CheckDefFailure(['{', 'echo 1'], 'E1026:')
270enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200272def Test_block_local_vars()
273 var lines =<< trim END
274 vim9script
Bram Moolenaared234f22020-10-15 20:42:20 +0200275 v:testing = 1
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200276 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200277 var text = ['hello']
278 def SayHello(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200279 return text
280 enddef
281 def SetText(v: string)
Bram Moolenaared234f22020-10-15 20:42:20 +0200282 text = [v]
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200283 enddef
284 endif
285
286 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200287 var text = ['again']
288 def SayAgain(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200289 return text
290 enddef
291 endif
Bram Moolenaared234f22020-10-15 20:42:20 +0200292
293 # test that the "text" variables are not cleaned up
294 test_garbagecollect_now()
295
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200296 defcompile
297
Bram Moolenaared234f22020-10-15 20:42:20 +0200298 assert_equal(['hello'], SayHello())
299 assert_equal(['again'], SayAgain())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200300
301 SetText('foobar')
Bram Moolenaared234f22020-10-15 20:42:20 +0200302 assert_equal(['foobar'], SayHello())
303
304 call writefile(['ok'], 'Xdidit')
305 qall!
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200306 END
Bram Moolenaared234f22020-10-15 20:42:20 +0200307
308 # need to execute this with a separate Vim instance to avoid the current
309 # context gets garbage collected.
310 writefile(lines, 'Xscript')
311 RunVim([], [], '-S Xscript')
312 assert_equal(['ok'], readfile('Xdidit'))
313
314 delete('Xscript')
315 delete('Xdidit')
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200316enddef
317
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200318def Test_block_local_vars_with_func()
319 var lines =<< trim END
320 vim9script
321 if true
322 var foo = 'foo'
323 if true
324 var bar = 'bar'
325 def Func(): list<string>
326 return [foo, bar]
327 enddef
328 endif
329 endif
330 # function is compiled here, after blocks have finished, can still access
331 # "foo" and "bar"
332 assert_equal(['foo', 'bar'], Func())
333 END
334 CheckScriptSuccess(lines)
335enddef
336
Bram Moolenaard032f342020-07-18 18:13:02 +0200337func g:NoSuchFunc()
338 echo 'none'
339endfunc
340
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100341def Test_try_catch_throw()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200342 var l = []
Bram Moolenaar7a092242020-04-16 22:10:49 +0200343 try # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344 add(l, '1')
345 throw 'wrong'
346 add(l, '2')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200347 catch # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100348 add(l, v:exception)
Bram Moolenaar7a092242020-04-16 22:10:49 +0200349 finally # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100350 add(l, '3')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200351 endtry # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100352 assert_equal(['1', 'wrong', '3'], l)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200353
Bram Moolenaare8593122020-07-18 15:17:02 +0200354 l = []
355 try
356 try
357 add(l, '1')
358 throw 'wrong'
359 add(l, '2')
360 catch /right/
361 add(l, v:exception)
362 endtry
363 catch /wrong/
364 add(l, 'caught')
Bram Moolenaar373863e2020-09-26 17:20:53 +0200365 fina
Bram Moolenaare8593122020-07-18 15:17:02 +0200366 add(l, 'finally')
367 endtry
368 assert_equal(['1', 'caught', 'finally'], l)
369
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200370 var n: number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200371 try
372 n = l[3]
373 catch /E684:/
374 n = 99
375 endtry
376 assert_equal(99, n)
377
Bram Moolenaar69f70502021-01-01 16:10:46 +0100378 var done = 'no'
379 if 0
380 try | catch | endtry
381 else
382 done = 'yes'
383 endif
384 assert_equal('yes', done)
385
386 done = 'no'
387 if 1
388 done = 'yes'
389 else
390 try | catch | endtry
391 done = 'never'
392 endif
393 assert_equal('yes', done)
394
395 if 1
396 else
397 try | catch /pat/ | endtry
398 try | catch /pat/
399 endtry
400 try
401 catch /pat/ | endtry
402 try
403 catch /pat/
404 endtry
405 endif
406
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200407 try
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200408 # string slice returns a string, not a number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200409 n = g:astring[3]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200410 catch /E1012:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200411 n = 77
412 endtry
413 assert_equal(77, n)
414
415 try
416 n = l[g:astring]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200417 catch /E1012:/
Bram Moolenaar56acb092020-08-16 14:48:19 +0200418 n = 88
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200419 endtry
Bram Moolenaar56acb092020-08-16 14:48:19 +0200420 assert_equal(88, n)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200421
422 try
423 n = s:does_not_exist
424 catch /E121:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200425 n = 111
426 endtry
427 assert_equal(111, n)
428
429 try
430 n = g:does_not_exist
431 catch /E121:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200432 n = 121
433 endtry
434 assert_equal(121, n)
435
Bram Moolenaare0de1712020-12-02 17:36:54 +0100436 var d = {one: 1}
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200437 try
438 n = d[g:astring]
439 catch /E716:/
440 n = 222
441 endtry
442 assert_equal(222, n)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200443
444 try
445 n = -g:astring
446 catch /E39:/
447 n = 233
448 endtry
449 assert_equal(233, n)
450
451 try
452 n = +g:astring
453 catch /E1030:/
454 n = 244
455 endtry
456 assert_equal(244, n)
457
458 try
459 n = +g:alist
460 catch /E745:/
461 n = 255
462 endtry
463 assert_equal(255, n)
464
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200465 var nd: dict<any>
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200466 try
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100467 nd = {[g:alist]: 1}
468 catch /E1105:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200469 n = 266
470 endtry
471 assert_equal(266, n)
472
473 try
474 [n] = [1, 2, 3]
475 catch /E1093:/
476 n = 277
477 endtry
478 assert_equal(277, n)
479
Bram Moolenaare8593122020-07-18 15:17:02 +0200480 try
481 &ts = g:astring
Bram Moolenaar5e654232020-09-16 15:22:00 +0200482 catch /E1012:/
Bram Moolenaare8593122020-07-18 15:17:02 +0200483 n = 288
484 endtry
485 assert_equal(288, n)
486
487 try
488 &backspace = 'asdf'
489 catch /E474:/
490 n = 299
491 endtry
492 assert_equal(299, n)
493
494 l = [1]
495 try
496 l[3] = 3
497 catch /E684:/
498 n = 300
499 endtry
500 assert_equal(300, n)
501
502 try
Bram Moolenaare8593122020-07-18 15:17:02 +0200503 unlet g:does_not_exist
504 catch /E108:/
505 n = 322
506 endtry
507 assert_equal(322, n)
508
509 try
Bram Moolenaar2bede172020-11-19 18:53:18 +0100510 d = {text: 1, [g:astring]: 2}
Bram Moolenaare8593122020-07-18 15:17:02 +0200511 catch /E721:/
512 n = 333
513 endtry
514 assert_equal(333, n)
515
516 try
517 l = DeletedFunc()
518 catch /E933:/
519 n = 344
520 endtry
521 assert_equal(344, n)
Bram Moolenaard032f342020-07-18 18:13:02 +0200522
523 try
524 echo len(v:true)
525 catch /E701:/
526 n = 355
527 endtry
528 assert_equal(355, n)
529
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200530 var P = function('g:NoSuchFunc')
Bram Moolenaard032f342020-07-18 18:13:02 +0200531 delfunc g:NoSuchFunc
532 try
533 echo P()
534 catch /E117:/
535 n = 366
536 endtry
537 assert_equal(366, n)
538
539 try
540 echo g:NoSuchFunc()
541 catch /E117:/
542 n = 377
543 endtry
544 assert_equal(377, n)
545
546 try
547 echo g:alist + 4
548 catch /E745:/
549 n = 388
550 endtry
551 assert_equal(388, n)
552
553 try
554 echo 4 + g:alist
555 catch /E745:/
556 n = 399
557 endtry
558 assert_equal(399, n)
559
560 try
561 echo g:alist.member
562 catch /E715:/
563 n = 400
564 endtry
565 assert_equal(400, n)
566
567 try
568 echo d.member
569 catch /E716:/
570 n = 411
571 endtry
572 assert_equal(411, n)
Bram Moolenaard9d77892021-02-12 21:32:47 +0100573
574 var counter = 0
575 for i in range(4)
576 try
577 eval [][0]
578 catch
579 endtry
580 counter += 1
581 endfor
582 assert_equal(4, counter)
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100583
584 # return in finally after empty catch
585 def ReturnInFinally(): number
586 try
587 finally
588 return 4
589 endtry
590 return 2
591 enddef
592 assert_equal(4, ReturnInFinally())
Bram Moolenaar8ac681a2021-06-15 20:06:34 +0200593
594 var lines =<< trim END
595 vim9script
596 try
597 acos('0.5')
598 ->setline(1)
599 catch
600 g:caught = v:exception
601 endtry
602 END
603 CheckScriptSuccess(lines)
604 assert_match('E808: Number or Float required', g:caught)
605 unlet g:caught
Bram Moolenaar41978282021-07-04 14:47:30 +0200606
607 # missing catch and/or finally
608 lines =<< trim END
609 vim9script
610 try
611 echo 'something'
612 endtry
613 END
614 CheckScriptFailure(lines, 'E1032:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615enddef
616
Bram Moolenaar3f987b52021-06-30 12:02:24 +0200617def Test_try_in_catch()
618 var lines =<< trim END
619 vim9script
620 var seq = []
621 def DoIt()
622 try
623 seq->add('throw 1')
624 eval [][0]
625 seq->add('notreached')
626 catch
627 seq->add('catch')
628 try
629 seq->add('throw 2')
630 eval [][0]
631 seq->add('notreached')
632 catch /nothing/
633 seq->add('notreached')
634 endtry
635 seq->add('done')
636 endtry
637 enddef
638 DoIt()
639 assert_equal(['throw 1', 'catch', 'throw 2', 'done'], seq)
640 END
641enddef
642
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200643def Test_error_in_catch()
644 var lines =<< trim END
645 try
646 eval [][0]
647 catch /E684:/
648 eval [][0]
649 endtry
650 END
651 CheckDefExecFailure(lines, 'E684:', 4)
652enddef
653
Bram Moolenaar2e34c342021-03-14 12:13:33 +0100654" :while at the very start of a function that :continue jumps to
655def TryContinueFunc()
656 while g:Count < 2
657 g:sequence ..= 't'
658 try
659 echoerr 'Test'
660 catch
661 g:Count += 1
662 g:sequence ..= 'c'
663 continue
664 endtry
665 g:sequence ..= 'e'
666 g:Count += 1
667 endwhile
668enddef
669
670def Test_continue_in_try_in_while()
671 g:Count = 0
672 g:sequence = ''
673 TryContinueFunc()
674 assert_equal('tctc', g:sequence)
675 unlet g:Count
676 unlet g:sequence
677enddef
678
Bram Moolenaar9cb577a2021-02-22 22:45:10 +0100679def Test_nocatch_return_in_try()
680 # return in try block returns normally
681 def ReturnInTry(): string
682 try
683 return '"some message"'
684 catch
685 endtry
686 return 'not reached'
687 enddef
688 exe 'echoerr ' .. ReturnInTry()
689enddef
690
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100691def Test_cnext_works_in_catch()
692 var lines =<< trim END
693 vim9script
Bram Moolenaarc3235272021-07-10 19:42:03 +0200694 au BufEnter * eval 1 + 2
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100695 writefile(['text'], 'Xfile1')
696 writefile(['text'], 'Xfile2')
697 var items = [
698 {lnum: 1, filename: 'Xfile1', valid: true},
699 {lnum: 1, filename: 'Xfile2', valid: true}
700 ]
701 setqflist([], ' ', {items: items})
702 cwindow
703
704 def CnextOrCfirst()
705 # if cnext fails, cfirst is used
706 try
707 cnext
708 catch
709 cfirst
710 endtry
711 enddef
712
713 CnextOrCfirst()
714 CnextOrCfirst()
715 writefile([getqflist({idx: 0}).idx], 'Xresult')
716 qall
717 END
718 writefile(lines, 'XCatchCnext')
719 RunVim([], [], '--clean -S XCatchCnext')
720 assert_equal(['1'], readfile('Xresult'))
721
722 delete('Xfile1')
723 delete('Xfile2')
724 delete('XCatchCnext')
725 delete('Xresult')
726enddef
727
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100728def Test_throw_skipped()
729 if 0
730 throw dontgethere
731 endif
732enddef
733
Bram Moolenaar8f81b222021-01-14 21:47:06 +0100734def Test_nocatch_throw_silenced()
735 var lines =<< trim END
736 vim9script
737 def Func()
738 throw 'error'
739 enddef
740 silent! Func()
741 END
742 writefile(lines, 'XthrowSilenced')
743 source XthrowSilenced
744 delete('XthrowSilenced')
745enddef
746
Bram Moolenaare8593122020-07-18 15:17:02 +0200747def DeletedFunc(): list<any>
748 return ['delete me']
749enddef
750defcompile
751delfunc DeletedFunc
752
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100753def ThrowFromDef()
Bram Moolenaara72cfb82020-04-23 17:07:30 +0200754 throw "getout" # comment
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100755enddef
756
757func CatchInFunc()
758 try
759 call ThrowFromDef()
760 catch
761 let g:thrown_func = v:exception
762 endtry
763endfunc
764
765def CatchInDef()
766 try
767 ThrowFromDef()
768 catch
769 g:thrown_def = v:exception
770 endtry
771enddef
772
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100773def ReturnFinally(): string
774 try
775 return 'intry'
Bram Moolenaar373863e2020-09-26 17:20:53 +0200776 finall
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100777 g:in_finally = 'finally'
778 endtry
779 return 'end'
780enddef
781
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100782def Test_try_catch_nested()
783 CatchInFunc()
784 assert_equal('getout', g:thrown_func)
785
786 CatchInDef()
787 assert_equal('getout', g:thrown_def)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100788
789 assert_equal('intry', ReturnFinally())
790 assert_equal('finally', g:in_finally)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200791
792 var l = []
793 try
794 l->add('1')
795 throw 'bad'
796 l->add('x')
797 catch /bad/
798 l->add('2')
799 try
800 l->add('3')
801 throw 'one'
802 l->add('x')
803 catch /one/
804 l->add('4')
805 try
806 l->add('5')
807 throw 'more'
808 l->add('x')
809 catch /more/
810 l->add('6')
811 endtry
812 endtry
813 endtry
814 assert_equal(['1', '2', '3', '4', '5', '6'], l)
Bram Moolenaar834193a2021-06-30 20:39:15 +0200815
816 l = []
817 try
818 try
819 l->add('1')
820 throw 'foo'
821 l->add('x')
822 catch
823 l->add('2')
824 throw 'bar'
825 l->add('x')
826 finally
827 l->add('3')
828 endtry
829 l->add('x')
830 catch /bar/
831 l->add('4')
832 endtry
833 assert_equal(['1', '2', '3', '4'], l)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100834enddef
835
Bram Moolenaar9939f572020-09-16 22:29:52 +0200836def TryOne(): number
837 try
838 return 0
839 catch
840 endtry
841 return 0
842enddef
843
844def TryTwo(n: number): string
845 try
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200846 var x = {}
Bram Moolenaar9939f572020-09-16 22:29:52 +0200847 catch
848 endtry
849 return 'text'
850enddef
851
852def Test_try_catch_twice()
853 assert_equal('text', TryOne()->TryTwo())
854enddef
855
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100856def Test_try_catch_match()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200857 var seq = 'a'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100858 try
859 throw 'something'
860 catch /nothing/
861 seq ..= 'x'
862 catch /some/
863 seq ..= 'b'
864 catch /asdf/
865 seq ..= 'x'
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200866 catch ?a\?sdf?
867 seq ..= 'y'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100868 finally
869 seq ..= 'c'
870 endtry
871 assert_equal('abc', seq)
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100872enddef
873
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200874def Test_try_catch_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200875 CheckDefFailure(['catch'], 'E603:')
876 CheckDefFailure(['try', 'echo 0', 'catch', 'catch'], 'E1033:')
877 CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
878 CheckDefFailure(['finally'], 'E606:')
879 CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:')
880 CheckDefFailure(['endtry'], 'E602:')
881 CheckDefFailure(['while 1', 'endtry'], 'E170:')
882 CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:')
Bram Moolenaar13106602020-10-04 16:06:05 +0200883 CheckDefFailure(['if 1', 'endtry'], 'E171:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200884 CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:')
Bram Moolenaar585fea72020-04-02 22:33:21 +0200885
Bram Moolenaare4984292020-12-13 14:19:25 +0100886 CheckDefFailure(['throw'], 'E1143:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200887 CheckDefFailure(['throw xxx'], 'E1001:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200888enddef
889
Bram Moolenaar7c5b3c0362021-02-14 22:40:57 +0100890def Try_catch_skipped()
891 var l = []
892 try
893 finally
894 endtry
895
896 if 1
897 else
898 try
899 endtry
900 endif
901enddef
902
903" The skipped try/endtry was updating the wrong instruction.
904def Test_try_catch_skipped()
905 var instr = execute('disassemble Try_catch_skipped')
906 assert_match("NEWLIST size 0\n", instr)
907enddef
908
909
910
Bram Moolenaar006ad482020-06-30 20:55:15 +0200911def Test_throw_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200912 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200913 var lines =<< trim END
Bram Moolenaar006ad482020-06-30 20:55:15 +0200914 vim9script
915 try
916 throw 'one'
917 .. 'two'
918 catch
919 assert_equal('onetwo', v:exception)
920 endtry
921 END
922 CheckScriptSuccess(lines)
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200923
924 lines =<< trim END
925 vim9script
Bram Moolenaar352134b2020-10-17 22:04:08 +0200926 @r = ''
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200927 def Func()
928 throw @r
929 enddef
930 var result = ''
931 try
932 Func()
933 catch /E1129:/
934 result = 'caught'
935 endtry
936 assert_equal('caught', result)
937 END
938 CheckScriptSuccess(lines)
Bram Moolenaar006ad482020-06-30 20:55:15 +0200939enddef
940
Bram Moolenaared677f52020-08-12 16:38:10 +0200941def Test_error_in_nested_function()
Bram Moolenaar03dfde22021-02-14 13:17:22 +0100942 # an error in a nested :function aborts executing in the calling :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200943 var lines =<< trim END
Bram Moolenaared677f52020-08-12 16:38:10 +0200944 vim9script
945 def Func()
946 Error()
947 g:test_var = 1
948 enddef
949 func Error() abort
950 eval [][0]
951 endfunc
952 Func()
953 END
954 g:test_var = 0
955 CheckScriptFailure(lines, 'E684:')
956 assert_equal(0, g:test_var)
957enddef
958
Bram Moolenaar227c58a2021-04-28 20:40:44 +0200959def Test_abort_after_error()
960 var lines =<< trim END
961 vim9script
962 while true
963 echo notfound
964 endwhile
965 g:gotthere = true
966 END
967 g:gotthere = false
968 CheckScriptFailure(lines, 'E121:')
969 assert_false(g:gotthere)
970 unlet g:gotthere
971enddef
972
Bram Moolenaar37c83712020-06-30 21:18:36 +0200973def Test_cexpr_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200974 # only checks line continuation
Bram Moolenaar37c83712020-06-30 21:18:36 +0200975 set errorformat=File\ %f\ line\ %l
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200976 var lines =<< trim END
Bram Moolenaar37c83712020-06-30 21:18:36 +0200977 vim9script
978 cexpr 'File'
979 .. ' someFile' ..
980 ' line 19'
981 assert_equal(19, getqflist()[0].lnum)
982 END
983 CheckScriptSuccess(lines)
984 set errorformat&
985enddef
986
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200987def Test_statusline_syntax()
988 # legacy syntax is used for 'statusline'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200989 var lines =<< trim END
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200990 vim9script
991 func g:Status()
992 return '%{"x" is# "x"}'
993 endfunc
994 set laststatus=2 statusline=%!Status()
995 redrawstatus
996 set laststatus statusline=
997 END
998 CheckScriptSuccess(lines)
999enddef
1000
Bram Moolenaarb2097502020-07-19 17:17:02 +02001001def Test_list_vimscript()
1002 # checks line continuation and comments
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001003 var lines =<< trim END
Bram Moolenaarb2097502020-07-19 17:17:02 +02001004 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001005 var mylist = [
Bram Moolenaarb2097502020-07-19 17:17:02 +02001006 'one',
1007 # comment
1008 'two', # empty line follows
1009
1010 'three',
1011 ]
1012 assert_equal(['one', 'two', 'three'], mylist)
1013 END
1014 CheckScriptSuccess(lines)
Bram Moolenaar66250c92020-08-20 15:02:42 +02001015
1016 # check all lines from heredoc are kept
1017 lines =<< trim END
1018 # comment 1
1019 two
1020 # comment 3
1021
1022 five
1023 # comment 6
1024 END
1025 assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines)
Bram Moolenaar4bce26b2021-01-22 22:06:56 +01001026
1027 lines =<< trim END
1028 [{
1029 a: 0}]->string()->assert_equal("[{'a': 0}]")
1030 END
1031 CheckDefAndScriptSuccess(lines)
Bram Moolenaarb2097502020-07-19 17:17:02 +02001032enddef
1033
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001034if has('channel')
1035 let someJob = test_null_job()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001036
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001037 def FuncWithError()
1038 echomsg g:someJob
1039 enddef
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001040
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001041 func Test_convert_emsg_to_exception()
1042 try
1043 call FuncWithError()
1044 catch
1045 call assert_match('Vim:E908:', v:exception)
1046 endtry
1047 endfunc
1048endif
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050let s:export_script_lines =<< trim END
1051 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001052 var name: string = 'bob'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 def Concat(arg: string): string
1054 return name .. arg
1055 enddef
Bram Moolenaar227a69d2020-05-15 18:17:28 +02001056 g:result = Concat('bie')
1057 g:localname = name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058
1059 export const CONST = 1234
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001060 export var exported = 9876
1061 export var exp_name = 'John'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001062 export def Exported(): string
1063 return 'Exported'
1064 enddef
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001065 export final theList = [1]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066END
1067
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001068def Undo_export_script_lines()
1069 unlet g:result
1070 unlet g:localname
1071enddef
1072
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001073def Test_vim9_import_export()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001074 var import_script_lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 vim9script
1076 import {exported, Exported} from './Xexport.vim'
1077 g:imported = exported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001078 exported += 3
1079 g:imported_added = exported
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 g:imported_func = Exported()
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001081
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001082 def GetExported(): string
Bram Moolenaare0de1712020-12-02 17:36:54 +01001083 var local_dict = {ref: Exported}
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001084 return local_dict.ref()
1085 enddef
1086 g:funcref_result = GetExported()
1087
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001088 import {exp_name} from './Xexport.vim'
1089 g:imported_name = exp_name
1090 exp_name ..= ' Doe'
1091 g:imported_name_appended = exp_name
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001092 g:imported_later = exported
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001093
1094 import theList from './Xexport.vim'
1095 theList->add(2)
1096 assert_equal([1, 2], theList)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 END
1098
1099 writefile(import_script_lines, 'Ximport.vim')
1100 writefile(s:export_script_lines, 'Xexport.vim')
1101
1102 source Ximport.vim
1103
1104 assert_equal('bobbie', g:result)
1105 assert_equal('bob', g:localname)
1106 assert_equal(9876, g:imported)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001107 assert_equal(9879, g:imported_added)
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001108 assert_equal(9879, g:imported_later)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 assert_equal('Exported', g:imported_func)
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001110 assert_equal('Exported', g:funcref_result)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001111 assert_equal('John', g:imported_name)
1112 assert_equal('John Doe', g:imported_name_appended)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113 assert_false(exists('g:name'))
1114
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001115 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 unlet g:imported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001117 unlet g:imported_added
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001118 unlet g:imported_later
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 unlet g:imported_func
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001120 unlet g:imported_name g:imported_name_appended
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 delete('Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001122
Bram Moolenaar1c991142020-07-04 13:15:31 +02001123 # similar, with line breaks
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001124 var import_line_break_script_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001125 vim9script
1126 import {
1127 exported,
1128 Exported,
1129 }
1130 from
1131 './Xexport.vim'
1132 g:imported = exported
1133 exported += 5
1134 g:imported_added = exported
1135 g:imported_func = Exported()
1136 END
1137 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
1138 source Ximport_lbr.vim
1139
1140 assert_equal(9876, g:imported)
1141 assert_equal(9881, g:imported_added)
1142 assert_equal('Exported', g:imported_func)
1143
1144 # exported script not sourced again
1145 assert_false(exists('g:result'))
1146 unlet g:imported
1147 unlet g:imported_added
1148 unlet g:imported_func
1149 delete('Ximport_lbr.vim')
1150
1151 # import inside :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001152 var import_in_def_lines =<< trim END
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001153 vim9script
1154 def ImportInDef()
1155 import exported from './Xexport.vim'
1156 g:imported = exported
1157 exported += 7
1158 g:imported_added = exported
1159 enddef
1160 ImportInDef()
1161 END
1162 writefile(import_in_def_lines, 'Ximport2.vim')
1163 source Ximport2.vim
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001164 # TODO: this should be 9879
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001165 assert_equal(9876, g:imported)
1166 assert_equal(9883, g:imported_added)
1167 unlet g:imported
1168 unlet g:imported_added
1169 delete('Ximport2.vim')
1170
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001171 var import_star_as_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001172 vim9script
1173 import * as Export from './Xexport.vim'
1174 def UseExport()
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001175 g:imported_def = Export.exported
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001176 enddef
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001177 g:imported_script = Export.exported
1178 assert_equal(1, exists('Export.exported'))
1179 assert_equal(0, exists('Export.notexported'))
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001180 UseExport()
1181 END
1182 writefile(import_star_as_lines, 'Ximport.vim')
1183 source Ximport.vim
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001184 assert_equal(9883, g:imported_def)
1185 assert_equal(9883, g:imported_script)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001186
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001187 var import_star_as_lines_no_dot =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001188 vim9script
1189 import * as Export from './Xexport.vim'
1190 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001191 var dummy = 1
1192 var imported = Export + dummy
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001193 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001194 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001195 END
1196 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001197 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001198
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001199 var import_star_as_lines_dot_space =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001200 vim9script
1201 import * as Export from './Xexport.vim'
1202 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001203 var imported = Export . exported
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001204 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001205 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001206 END
1207 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001208 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001209
Bram Moolenaara6294952020-12-27 13:39:50 +01001210 var import_star_as_duplicated =<< trim END
1211 vim9script
1212 import * as Export from './Xexport.vim'
1213 var some = 'other'
1214 import * as Export from './Xexport.vim'
1215 defcompile
1216 END
1217 writefile(import_star_as_duplicated, 'Ximport.vim')
1218 assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim')
1219
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001220 var import_star_as_lines_script_no_dot =<< trim END
1221 vim9script
1222 import * as Export from './Xexport.vim'
1223 g:imported_script = Export exported
1224 END
1225 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
1226 assert_fails('source Ximport.vim', 'E1029:')
1227
1228 var import_star_as_lines_script_space_after_dot =<< trim END
1229 vim9script
1230 import * as Export from './Xexport.vim'
1231 g:imported_script = Export. exported
1232 END
1233 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
1234 assert_fails('source Ximport.vim', 'E1074:')
1235
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001236 var import_star_as_lines_missing_name =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001237 vim9script
1238 import * as Export from './Xexport.vim'
1239 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001240 var imported = Export.
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001241 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001242 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001243 END
1244 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001245 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001246
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001247 var import_star_as_lbr_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001248 vim9script
1249 import *
1250 as Export
1251 from
1252 './Xexport.vim'
1253 def UseExport()
1254 g:imported = Export.exported
1255 enddef
1256 UseExport()
1257 END
1258 writefile(import_star_as_lbr_lines, 'Ximport.vim')
1259 source Ximport.vim
1260 assert_equal(9883, g:imported)
1261
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001262 var import_star_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001263 vim9script
1264 import * from './Xexport.vim'
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001265 END
1266 writefile(import_star_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001267 assert_fails('source Ximport.vim', 'E1045:', '', 2, 'Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001268
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001269 # try to import something that exists but is not exported
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001270 var import_not_exported_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001271 vim9script
1272 import name from './Xexport.vim'
1273 END
1274 writefile(import_not_exported_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001275 assert_fails('source Ximport.vim', 'E1049:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001276
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001277 # try to import something that is already defined
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001278 var import_already_defined =<< trim END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001279 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001280 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001281 import exported from './Xexport.vim'
1282 END
1283 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001284 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001285
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001286 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001287 import_already_defined =<< trim END
1288 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001289 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001290 import * as exported from './Xexport.vim'
1291 END
1292 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001293 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001294
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001295 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001296 import_already_defined =<< trim END
1297 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001298 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001299 import {exported} from './Xexport.vim'
1300 END
1301 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001302 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001303
Bram Moolenaar918a4242020-12-06 14:37:08 +01001304 # try changing an imported const
1305 var import_assign_to_const =<< trim END
1306 vim9script
1307 import CONST from './Xexport.vim'
1308 def Assign()
1309 CONST = 987
1310 enddef
1311 defcompile
1312 END
1313 writefile(import_assign_to_const, 'Ximport.vim')
1314 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1315
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001316 # try changing an imported final
1317 var import_assign_to_final =<< trim END
1318 vim9script
1319 import theList from './Xexport.vim'
1320 def Assign()
1321 theList = [2]
1322 enddef
1323 defcompile
1324 END
1325 writefile(import_assign_to_final, 'Ximport.vim')
1326 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1327
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001328 # import a very long name, requires making a copy
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001329 var import_long_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001330 vim9script
1331 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
1332 END
1333 writefile(import_long_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001334 assert_fails('source Ximport.vim', 'E1048:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001335
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001336 var import_no_from_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001337 vim9script
1338 import name './Xexport.vim'
1339 END
1340 writefile(import_no_from_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001341 assert_fails('source Ximport.vim', 'E1070:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001342
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001343 var import_invalid_string_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001344 vim9script
1345 import name from Xexport.vim
1346 END
1347 writefile(import_invalid_string_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001348 assert_fails('source Ximport.vim', 'E1071:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001349
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001350 var import_wrong_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001351 vim9script
1352 import name from './XnoExport.vim'
1353 END
1354 writefile(import_wrong_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001355 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001356
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001357 var import_missing_comma_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001358 vim9script
1359 import {exported name} from './Xexport.vim'
1360 END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001361 writefile(import_missing_comma_lines, 'Ximport3.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001362 assert_fails('source Ximport3.vim', 'E1046:', '', 2, 'Ximport3.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001363
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001364 delete('Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001365 delete('Ximport3.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 delete('Xexport.vim')
1367
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001368 # Check that in a Vim9 script 'cpo' is set to the Vim default.
Bram Moolenaar3e191692021-03-17 17:46:00 +01001369 # Flags added or removed are also applied to the restored value.
1370 set cpo=abcd
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001371 var lines =<< trim END
Bram Moolenaar750802b2020-02-23 18:08:33 +01001372 vim9script
1373 g:cpo_in_vim9script = &cpo
Bram Moolenaar3e191692021-03-17 17:46:00 +01001374 set cpo+=f
1375 set cpo-=c
1376 g:cpo_after_vim9script = &cpo
Bram Moolenaar750802b2020-02-23 18:08:33 +01001377 END
1378 writefile(lines, 'Xvim9_script')
1379 source Xvim9_script
Bram Moolenaar3e191692021-03-17 17:46:00 +01001380 assert_equal('fabd', &cpo)
Bram Moolenaar750802b2020-02-23 18:08:33 +01001381 set cpo&vim
1382 assert_equal(&cpo, g:cpo_in_vim9script)
Bram Moolenaar3e191692021-03-17 17:46:00 +01001383 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
1384 assert_equal(newcpo, g:cpo_after_vim9script)
1385
Bram Moolenaar750802b2020-02-23 18:08:33 +01001386 delete('Xvim9_script')
1387enddef
1388
Bram Moolenaar0a842842021-02-27 22:41:19 +01001389def Test_import_as()
1390 var export_lines =<< trim END
1391 vim9script
1392 export var one = 1
1393 export var yes = 'yes'
Bram Moolenaarc967d572021-07-08 21:38:50 +02001394 export var slist: list<string>
Bram Moolenaar0a842842021-02-27 22:41:19 +01001395 END
1396 writefile(export_lines, 'XexportAs')
1397
1398 var import_lines =<< trim END
1399 vim9script
Bram Moolenaar6c7cc342021-04-17 16:38:50 +02001400 var one = 'notused'
1401 var yes = 777
Bram Moolenaar0a842842021-02-27 22:41:19 +01001402 import one as thatOne from './XexportAs'
1403 assert_equal(1, thatOne)
1404 import yes as yesYes from './XexportAs'
1405 assert_equal('yes', yesYes)
1406 END
1407 CheckScriptSuccess(import_lines)
1408
1409 import_lines =<< trim END
1410 vim9script
1411 import {one as thatOne, yes as yesYes} from './XexportAs'
1412 assert_equal(1, thatOne)
1413 assert_equal('yes', yesYes)
1414 assert_fails('echo one', 'E121:')
1415 assert_fails('echo yes', 'E121:')
1416 END
1417 CheckScriptSuccess(import_lines)
1418
Bram Moolenaarc967d572021-07-08 21:38:50 +02001419 import_lines =<< trim END
1420 vim9script
1421 import {slist as impSlist} from './XexportAs'
1422 impSlist->add(123)
1423 END
1424 CheckScriptFailure(import_lines, 'E1012: Type mismatch; expected string but got number')
1425
Bram Moolenaar0a842842021-02-27 22:41:19 +01001426 delete('XexportAs')
1427enddef
1428
Bram Moolenaar803af682020-08-05 16:20:03 +02001429func g:Trigger()
1430 source Ximport.vim
1431 return "echo 'yes'\<CR>"
1432endfunc
1433
1434def Test_import_export_expr_map()
1435 # check that :import and :export work when buffer is locked
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001436 var export_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001437 vim9script
1438 export def That(): string
1439 return 'yes'
1440 enddef
1441 END
1442 writefile(export_lines, 'Xexport_that.vim')
1443
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001444 var import_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001445 vim9script
1446 import That from './Xexport_that.vim'
1447 assert_equal('yes', That())
1448 END
1449 writefile(import_lines, 'Ximport.vim')
1450
1451 nnoremap <expr> trigger g:Trigger()
1452 feedkeys('trigger', "xt")
1453
Bram Moolenaar730b2482020-08-09 13:02:10 +02001454 delete('Xexport_that.vim')
Bram Moolenaar803af682020-08-05 16:20:03 +02001455 delete('Ximport.vim')
1456 nunmap trigger
1457enddef
1458
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001459def Test_import_in_filetype()
1460 # check that :import works when the buffer is locked
1461 mkdir('ftplugin', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001462 var export_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001463 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001464 export var That = 'yes'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001465 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001466 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001467
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001468 var import_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001469 vim9script
Bram Moolenaar730b2482020-08-09 13:02:10 +02001470 import That from './Xexport_ft.vim'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001471 assert_equal('yes', That)
1472 g:did_load_mytpe = 1
1473 END
1474 writefile(import_lines, 'ftplugin/qf.vim')
1475
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001476 var save_rtp = &rtp
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001477 &rtp = getcwd() .. ',' .. &rtp
1478
1479 filetype plugin on
1480 copen
1481 assert_equal(1, g:did_load_mytpe)
1482
1483 quit!
Bram Moolenaar730b2482020-08-09 13:02:10 +02001484 delete('Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001485 delete('ftplugin', 'rf')
1486 &rtp = save_rtp
1487enddef
1488
Bram Moolenaarefa94442020-08-08 22:16:00 +02001489def Test_use_import_in_mapping()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001490 var lines =<< trim END
Bram Moolenaarefa94442020-08-08 22:16:00 +02001491 vim9script
1492 export def Funcx()
1493 g:result = 42
1494 enddef
1495 END
1496 writefile(lines, 'XsomeExport.vim')
1497 lines =<< trim END
1498 vim9script
1499 import Funcx from './XsomeExport.vim'
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001500 nnoremap <F3> :call <sid>Funcx()<cr>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001501 END
1502 writefile(lines, 'Xmapscript.vim')
1503
1504 source Xmapscript.vim
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001505 feedkeys("\<F3>", "xt")
Bram Moolenaarefa94442020-08-08 22:16:00 +02001506 assert_equal(42, g:result)
1507
1508 unlet g:result
1509 delete('XsomeExport.vim')
1510 delete('Xmapscript.vim')
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001511 nunmap <F3>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001512enddef
1513
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01001514def Test_vim9script_mix()
1515 var lines =<< trim END
1516 if has(g:feature)
1517 " legacy script
1518 let g:legacy = 1
1519 finish
1520 endif
1521 vim9script
1522 g:legacy = 0
1523 END
1524 g:feature = 'eval'
1525 g:legacy = -1
1526 CheckScriptSuccess(lines)
1527 assert_equal(1, g:legacy)
1528
1529 g:feature = 'noteval'
1530 g:legacy = -1
1531 CheckScriptSuccess(lines)
1532 assert_equal(0, g:legacy)
1533enddef
1534
Bram Moolenaar750802b2020-02-23 18:08:33 +01001535def Test_vim9script_fails()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1537 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001538 CheckScriptFailure(['export var some = 123'], 'E1042:')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001539 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001540 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
Bram Moolenaar750802b2020-02-23 18:08:33 +01001541 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
1542
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001543 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001544 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1545
Bram Moolenaare2e40752020-09-04 21:18:46 +02001546 assert_fails('vim9script', 'E1038:')
1547 assert_fails('export something', 'E1043:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548enddef
1549
Bram Moolenaarf7d267e2020-06-17 12:04:54 +02001550func Test_import_fails_without_script()
Bram Moolenaar101f4812020-06-16 23:18:51 +02001551 CheckRunVimInTerminal
1552
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001553 " call indirectly to avoid compilation error for missing functions
Bram Moolenaarc620c052020-07-08 15:16:19 +02001554 call Run_Test_import_fails_on_command_line()
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001555endfunc
1556
Bram Moolenaarc620c052020-07-08 15:16:19 +02001557def Run_Test_import_fails_on_command_line()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001558 var export =<< trim END
Bram Moolenaar101f4812020-06-16 23:18:51 +02001559 vim9script
1560 export def Foo(): number
1561 return 0
1562 enddef
1563 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001564 writefile(export, 'XexportCmd.vim')
Bram Moolenaar101f4812020-06-16 23:18:51 +02001565
Bram Moolenaare0de1712020-12-02 17:36:54 +01001566 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001567 rows: 6, wait_for_ruler: 0})
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001568 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaar101f4812020-06-16 23:18:51 +02001569
Bram Moolenaar730b2482020-08-09 13:02:10 +02001570 delete('XexportCmd.vim')
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001571 StopVimInTerminal(buf)
1572enddef
Bram Moolenaar101f4812020-06-16 23:18:51 +02001573
Bram Moolenaar2b327002020-12-26 15:39:31 +01001574def Test_vim9script_reload_noclear()
1575 var lines =<< trim END
Bram Moolenaara6294952020-12-27 13:39:50 +01001576 vim9script
1577 export var exported = 'thexport'
1578 END
1579 writefile(lines, 'XExportReload')
1580 lines =<< trim END
Bram Moolenaar2b327002020-12-26 15:39:31 +01001581 vim9script noclear
1582 g:loadCount += 1
1583 var s:reloaded = 'init'
Bram Moolenaara6294952020-12-27 13:39:50 +01001584 import exported from './XExportReload'
Bram Moolenaar2b327002020-12-26 15:39:31 +01001585
1586 def Again(): string
1587 return 'again'
1588 enddef
1589
1590 if exists('s:loaded') | finish | endif
1591 var s:loaded = true
1592
1593 var s:notReloaded = 'yes'
1594 s:reloaded = 'first'
1595 def g:Values(): list<string>
Bram Moolenaara6294952020-12-27 13:39:50 +01001596 return [s:reloaded, s:notReloaded, Again(), Once(), exported]
Bram Moolenaar2b327002020-12-26 15:39:31 +01001597 enddef
1598
1599 def Once(): string
1600 return 'once'
1601 enddef
1602 END
1603 writefile(lines, 'XReloaded')
1604 g:loadCount = 0
1605 source XReloaded
1606 assert_equal(1, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001607 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001608 source XReloaded
1609 assert_equal(2, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001610 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001611 source XReloaded
1612 assert_equal(3, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001613 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001614
Bram Moolenaar48e11c12021-01-11 18:47:00 +01001615 delete('XReloaded')
Bram Moolenaara6294952020-12-27 13:39:50 +01001616 delete('XExportReload')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001617 delfunc g:Values
Bram Moolenaar2b327002020-12-26 15:39:31 +01001618 unlet g:loadCount
Bram Moolenaar577dc932021-06-27 15:35:40 +02001619
1620 lines =<< trim END
1621 vim9script
1622 def Inner()
1623 enddef
1624 END
1625 lines->writefile('XreloadScript.vim')
1626 source XreloadScript.vim
1627
1628 lines =<< trim END
1629 vim9script
1630 def Outer()
1631 def Inner()
1632 enddef
1633 enddef
1634 defcompile
1635 END
1636 lines->writefile('XreloadScript.vim')
1637 source XreloadScript.vim
1638
1639 delete('XreloadScript.vim')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001640enddef
1641
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001642def Test_vim9script_reload_import()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001643 var lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 vim9script
1645 const var = ''
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001646 var valone = 1234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 def MyFunc(arg: string)
1648 valone = 5678
1649 enddef
1650 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001651 var morelines =<< trim END
1652 var valtwo = 222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653 export def GetValtwo(): number
1654 return valtwo
1655 enddef
1656 END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001657 writefile(lines + morelines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 source Xreload.vim
1659 source Xreload.vim
1660 source Xreload.vim
1661
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001662 var testlines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 vim9script
1664 def TheFunc()
1665 import GetValtwo from './Xreload.vim'
1666 assert_equal(222, GetValtwo())
1667 enddef
1668 TheFunc()
1669 END
1670 writefile(testlines, 'Ximport.vim')
1671 source Ximport.vim
1672
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001673 # Test that when not using "morelines" GetValtwo() and valtwo are still
1674 # defined, because import doesn't reload a script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675 writefile(lines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 source Ximport.vim
1677
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001678 # cannot declare a var twice
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 lines =<< trim END
1680 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001681 var valone = 1234
1682 var valone = 5678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683 END
1684 writefile(lines, 'Xreload.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001685 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686
1687 delete('Xreload.vim')
1688 delete('Ximport.vim')
1689enddef
1690
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001691" if a script is reloaded with a script-local variable that changed its type, a
1692" compiled function using that variable must fail.
1693def Test_script_reload_change_type()
1694 var lines =<< trim END
1695 vim9script noclear
1696 var str = 'string'
1697 def g:GetStr(): string
1698 return str .. 'xxx'
1699 enddef
1700 END
1701 writefile(lines, 'Xreload.vim')
1702 source Xreload.vim
1703 echo g:GetStr()
1704
1705 lines =<< trim END
1706 vim9script noclear
1707 var str = 1234
1708 END
1709 writefile(lines, 'Xreload.vim')
1710 source Xreload.vim
1711 assert_fails('echo g:GetStr()', 'E1150:')
1712
1713 delfunc g:GetStr
1714 delete('Xreload.vim')
1715enddef
1716
Bram Moolenaarc970e422021-03-17 15:03:04 +01001717" Define CallFunc so that the test can be compiled
1718command CallFunc echo 'nop'
1719
1720def Test_script_reload_from_function()
1721 var lines =<< trim END
1722 vim9script
1723
1724 if exists('g:loaded')
1725 finish
1726 endif
1727 g:loaded = 1
1728 delcommand CallFunc
1729 command CallFunc Func()
1730 def Func()
Bram Moolenaara4c81be2021-03-17 15:23:16 +01001731 so XreloadFunc.vim
Bram Moolenaarc970e422021-03-17 15:03:04 +01001732 g:didTheFunc = 1
1733 enddef
1734 END
1735 writefile(lines, 'XreloadFunc.vim')
1736 source XreloadFunc.vim
1737 CallFunc
1738 assert_equal(1, g:didTheFunc)
1739
1740 delete('XreloadFunc.vim')
1741 delcommand CallFunc
1742 unlet g:loaded
1743 unlet g:didTheFunc
1744enddef
1745
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001746def Test_script_var_shadows_function()
1747 var lines =<< trim END
1748 vim9script
1749 def Func(): number
1750 return 123
1751 enddef
1752 var Func = 1
1753 END
1754 CheckScriptFailure(lines, 'E1041:', 5)
1755enddef
1756
Bram Moolenaarc3235272021-07-10 19:42:03 +02001757def Test_script_var_shadows_command()
1758 var lines =<< trim END
1759 var undo = 1
1760 undo = 2
1761 assert_equal(2, undo)
1762 END
1763 CheckDefAndScriptSuccess(lines)
1764
1765 lines =<< trim END
1766 var undo = 1
1767 undo
1768 END
1769 CheckDefAndScriptFailure(lines, 'E1207:', 2)
1770enddef
1771
Bram Moolenaar95006e32020-08-29 17:47:08 +02001772def s:RetSome(): string
1773 return 'some'
1774enddef
1775
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001776" Not exported function that is referenced needs to be accessed by the
1777" script-local name.
1778def Test_vim9script_funcref()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001779 var sortlines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001780 vim9script
1781 def Compare(i1: number, i2: number): number
Bram Moolenaarbed36b92020-07-07 23:31:36 +02001782 return i2 - i1
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001783 enddef
1784
1785 export def FastSort(): list<number>
1786 return range(5)->sort(Compare)
1787 enddef
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001788
1789 export def GetString(arg: string): string
1790 return arg
1791 enddef
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001792 END
1793 writefile(sortlines, 'Xsort.vim')
1794
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001795 var lines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001796 vim9script
1797 import FastSort from './Xsort.vim'
1798 def Test()
1799 g:result = FastSort()
1800 enddef
1801 Test()
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001802
1803 # using a function imported with "as"
1804 import * as anAlias from './Xsort.vim'
1805 assert_equal('yes', anAlias.GetString('yes'))
1806
1807 # using the function from a compiled function
1808 def TestMore(): string
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001809 var s = s:anAlias.GetString('foo')
1810 return s .. anAlias.GetString('bar')
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001811 enddef
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001812 assert_equal('foobar', TestMore())
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001813
1814 # error when using a function that isn't exported
1815 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001816 END
1817 writefile(lines, 'Xscript.vim')
1818
1819 source Xscript.vim
1820 assert_equal([4, 3, 2, 1, 0], g:result)
1821
1822 unlet g:result
1823 delete('Xsort.vim')
1824 delete('Xscript.vim')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001825
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001826 var Funcref = function('s:RetSome')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001827 assert_equal('some', Funcref())
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001828enddef
1829
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001830" Check that when searching for "FilterFunc" it finds the import in the
1831" script where FastFilter() is called from, both as a string and as a direct
1832" function reference.
Bram Moolenaarc620c052020-07-08 15:16:19 +02001833def Test_vim9script_funcref_other_script()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001834 var filterLines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001835 vim9script
1836 export def FilterFunc(idx: number, val: number): bool
1837 return idx % 2 == 1
1838 enddef
1839 export def FastFilter(): list<number>
1840 return range(10)->filter('FilterFunc')
1841 enddef
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001842 export def FastFilterDirect(): list<number>
1843 return range(10)->filter(FilterFunc)
1844 enddef
Bram Moolenaarc620c052020-07-08 15:16:19 +02001845 END
1846 writefile(filterLines, 'Xfilter.vim')
1847
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001848 var lines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001849 vim9script
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001850 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim'
Bram Moolenaarc620c052020-07-08 15:16:19 +02001851 def Test()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001852 var x: list<number> = FastFilter()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001853 enddef
1854 Test()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001855 def TestDirect()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001856 var x: list<number> = FastFilterDirect()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001857 enddef
1858 TestDirect()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001859 END
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001860 CheckScriptSuccess(lines)
Bram Moolenaarc620c052020-07-08 15:16:19 +02001861 delete('Xfilter.vim')
Bram Moolenaarc620c052020-07-08 15:16:19 +02001862enddef
1863
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001864def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001865 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001866 vim9script
1867 def FuncYes(): string
1868 return 'yes'
1869 enddef
1870 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001871 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001872 def FuncNo(): string
1873 return 'no'
1874 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001875 def g:DoCheck(no_exists: bool)
1876 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001877 assert_equal('no', FuncNo())
1878 enddef
1879 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001880 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001881 def g:DoCheck(no_exists: bool)
1882 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02001883 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001884 enddef
1885 END
1886
1887 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001888 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001889 source Xreloaded.vim
1890 g:DoCheck(true)
1891
1892 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001893 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001894 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001895 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001896
1897 # FuncNo() is back
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001898 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001899 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001900 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001901
1902 delete('Xreloaded.vim')
1903enddef
1904
Bram Moolenaar89483d42020-05-10 15:24:44 +02001905def Test_vim9script_reload_delvar()
1906 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001907 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02001908 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001909 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001910 END
1911 writefile(lines, 'XreloadVar.vim')
1912 source XreloadVar.vim
1913
1914 # now write the script using the same variable locally - works
1915 lines =<< trim END
1916 vim9script
1917 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001918 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001919 enddef
1920 END
1921 writefile(lines, 'XreloadVar.vim')
1922 source XreloadVar.vim
1923
1924 delete('XreloadVar.vim')
1925enddef
1926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927def Test_import_absolute()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001928 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02001929 'vim9script',
1930 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
1931 'def UseExported()',
1932 ' g:imported_abs = exported',
1933 ' exported = 8888',
1934 ' g:imported_after = exported',
1935 'enddef',
1936 'UseExported()',
1937 'g:import_disassembled = execute("disass UseExported")',
1938 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 writefile(import_lines, 'Ximport_abs.vim')
1940 writefile(s:export_script_lines, 'Xexport_abs.vim')
1941
1942 source Ximport_abs.vim
1943
1944 assert_equal(9876, g:imported_abs)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001945 assert_equal(8888, g:imported_after)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001946 assert_match('<SNR>\d\+_UseExported\_s*' ..
1947 'g:imported_abs = exported\_s*' ..
1948 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1949 '1 STOREG g:imported_abs\_s*' ..
1950 'exported = 8888\_s*' ..
1951 '2 PUSHNR 8888\_s*' ..
1952 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1953 'g:imported_after = exported\_s*' ..
1954 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1955 '5 STOREG g:imported_after',
Bram Moolenaare6085c52020-04-12 20:19:16 +02001956 g:import_disassembled)
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001957
1958 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 unlet g:imported_abs
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001960 unlet g:import_disassembled
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961
1962 delete('Ximport_abs.vim')
1963 delete('Xexport_abs.vim')
1964enddef
1965
1966def Test_import_rtp()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001967 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02001968 'vim9script',
1969 'import exported from "Xexport_rtp.vim"',
1970 'g:imported_rtp = exported',
1971 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 writefile(import_lines, 'Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02001973 mkdir('import', 'p')
1974 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001976 var save_rtp = &rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 &rtp = getcwd()
1978 source Ximport_rtp.vim
1979 &rtp = save_rtp
1980
1981 assert_equal(9876, g:imported_rtp)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001983 Undo_export_script_lines()
1984 unlet g:imported_rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 delete('Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02001986 delete('import', 'rf')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987enddef
1988
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001989def Test_import_compile_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001990 var export_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001991 'vim9script',
1992 'export def ExpFunc(): string',
1993 ' return notDefined',
1994 'enddef',
1995 ]
1996 writefile(export_lines, 'Xexported.vim')
1997
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001998 var import_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001999 'vim9script',
2000 'import ExpFunc from "./Xexported.vim"',
2001 'def ImpFunc()',
2002 ' echo ExpFunc()',
2003 'enddef',
2004 'defcompile',
2005 ]
2006 writefile(import_lines, 'Ximport.vim')
2007
2008 try
2009 source Ximport.vim
2010 catch /E1001/
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002011 # Error should be fore the Xexported.vim file.
Bram Moolenaar77072282020-09-16 17:55:40 +02002012 assert_match('E1001: Variable not found: notDefined', v:exception)
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002013 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
2014 endtry
2015
2016 delete('Xexported.vim')
2017 delete('Ximport.vim')
2018enddef
2019
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002020def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002021 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002022 'vim9script',
2023 'def Func()',
2024 ' eval [][0]',
2025 'enddef',
2026 'Func()',
2027 ]
2028 writefile(lines, 'Xtestscript.vim')
2029
2030 for count in range(3)
2031 try
2032 source Xtestscript.vim
2033 catch /E684/
2034 # function name should contain <SNR> every time
2035 assert_match('E684: list index out of range', v:exception)
2036 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
2037 endtry
2038 endfor
2039
2040 delete('Xtestscript.vim')
2041enddef
2042
Bram Moolenaareef21022020-08-01 22:16:43 +02002043def Test_func_overrules_import_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002044 var export_lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002045 vim9script
2046 export def Func()
2047 echo 'imported'
2048 enddef
2049 END
2050 writefile(export_lines, 'XexportedFunc.vim')
2051
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002052 var lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002053 vim9script
2054 import Func from './XexportedFunc.vim'
2055 def Func()
2056 echo 'local to function'
2057 enddef
2058 END
2059 CheckScriptFailure(lines, 'E1073:')
2060
2061 lines =<< trim END
2062 vim9script
2063 import Func from './XexportedFunc.vim'
2064 def Outer()
2065 def Func()
2066 echo 'local to function'
2067 enddef
2068 enddef
2069 defcompile
2070 END
2071 CheckScriptFailure(lines, 'E1073:')
2072
2073 delete('XexportedFunc.vim')
2074enddef
2075
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002076def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002077 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002078 vim9script
2079 def Func()
2080 echo 'one'
2081 enddef
2082 def Func()
2083 echo 'two'
2084 enddef
2085 END
2086 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002087
2088 lines =<< trim END
2089 vim9script
2090 def Foo(): string
2091 return 'foo'
2092 enddef
2093 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002094 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002095 enddef
2096 defcompile
2097 END
2098 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002099enddef
2100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002102 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002103 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 l->remove(0)
2105 l->add(5)
2106 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002107 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108enddef
2109
Bram Moolenaarae616492020-07-28 20:07:27 +02002110def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002111 CheckDefExecFailure(['a = 1'], 'E1100:')
2112 CheckDefExecFailure(['c = 1'], 'E1100:')
2113 CheckDefExecFailure(['i = 1'], 'E1100:')
2114 CheckDefExecFailure(['t = 1'], 'E1100:')
2115 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002116
Bram Moolenaarae616492020-07-28 20:07:27 +02002117 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
2118 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002119 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
2120 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002121 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
2122 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01002123 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
2124 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002125 CheckScriptFailure(['vim9script', 't'], 'E1100:')
2126 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
2127 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002128enddef
2129
Bram Moolenaar158906c2020-02-06 20:39:45 +01002130def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002131 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01002132 if what == 1
2133 res = "one"
2134 elseif what == 2
2135 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002136 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01002137 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002138 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01002139 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002140enddef
2141
Bram Moolenaar158906c2020-02-06 20:39:45 +01002142def Test_if_elseif_else()
2143 assert_equal('one', IfElse(1))
2144 assert_equal('two', IfElse(2))
2145 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002146enddef
2147
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002148def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002149 CheckDefFailure(['elseif true'], 'E582:')
2150 CheckDefFailure(['else'], 'E581:')
2151 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01002152 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002153 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002154
2155 var lines =<< trim END
2156 var s = ''
2157 if s = ''
2158 endif
2159 END
2160 CheckDefFailure(lines, 'E488:')
2161
2162 lines =<< trim END
2163 var s = ''
2164 if s == ''
2165 elseif s = ''
2166 endif
2167 END
2168 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002169enddef
2170
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002171let g:bool_true = v:true
2172let g:bool_false = v:false
2173
2174def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002175 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002176 if true ? true : false
2177 res = true
2178 endif
2179 assert_equal(true, res)
2180
Bram Moolenaar585fea72020-04-02 22:33:21 +02002181 g:glob = 2
2182 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02002183 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002184 endif
2185 assert_equal(2, g:glob)
2186 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02002187 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002188 endif
2189 assert_equal(3, g:glob)
2190
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002191 res = false
2192 if g:bool_true ? true : false
2193 res = true
2194 endif
2195 assert_equal(true, res)
2196
2197 res = false
2198 if true ? g:bool_true : false
2199 res = true
2200 endif
2201 assert_equal(true, res)
2202
2203 res = false
2204 if true ? true : g:bool_false
2205 res = true
2206 endif
2207 assert_equal(true, res)
2208
2209 res = false
2210 if true ? false : true
2211 res = true
2212 endif
2213 assert_equal(false, res)
2214
2215 res = false
2216 if false ? false : true
2217 res = true
2218 endif
2219 assert_equal(true, res)
2220
2221 res = false
2222 if false ? true : false
2223 res = true
2224 endif
2225 assert_equal(false, res)
2226
2227 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002228 if has('xyz') ? true : false
2229 res = true
2230 endif
2231 assert_equal(false, res)
2232
2233 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002234 if true && true
2235 res = true
2236 endif
2237 assert_equal(true, res)
2238
2239 res = false
2240 if true && false
2241 res = true
2242 endif
2243 assert_equal(false, res)
2244
2245 res = false
2246 if g:bool_true && false
2247 res = true
2248 endif
2249 assert_equal(false, res)
2250
2251 res = false
2252 if true && g:bool_false
2253 res = true
2254 endif
2255 assert_equal(false, res)
2256
2257 res = false
2258 if false && false
2259 res = true
2260 endif
2261 assert_equal(false, res)
2262
2263 res = false
2264 if true || false
2265 res = true
2266 endif
2267 assert_equal(true, res)
2268
2269 res = false
2270 if g:bool_true || false
2271 res = true
2272 endif
2273 assert_equal(true, res)
2274
2275 res = false
2276 if true || g:bool_false
2277 res = true
2278 endif
2279 assert_equal(true, res)
2280
2281 res = false
2282 if false || false
2283 res = true
2284 endif
2285 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02002286
2287 # with constant "false" expression may be invalid so long as the syntax is OK
Bram Moolenaarc3235272021-07-10 19:42:03 +02002288 if false | eval 1 + 2 | endif
Bram Moolenaar3988f642020-08-27 22:43:03 +02002289 if false | eval burp + 234 | endif
2290 if false | echo burp 234 'asd' | endif
2291 if false
2292 burp
2293 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002294enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002295
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002296def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002297 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
2298 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
2299 CheckDefFailure(["if has('aaa'"], 'E110:')
2300 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002301enddef
2302
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002303def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002304 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002305 if i % 2
2306 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002307 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002308 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002309 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002310 endif
2311 x += 1
2312 else
2313 x += 1000
2314 endif
2315 return x
2316enddef
2317
2318def Test_nested_if()
2319 assert_equal(1, RunNested(1))
2320 assert_equal(1000, RunNested(2))
2321enddef
2322
Bram Moolenaarad39c092020-02-26 18:23:43 +01002323def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01002324 # missing argument is ignored
2325 execute
2326 execute # comment
2327
Bram Moolenaarad39c092020-02-26 18:23:43 +01002328 new
2329 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002330 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002331 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002332
Bram Moolenaard2c61702020-09-06 15:58:36 +02002333 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002334 assert_equal('execute-string', getline(1))
2335
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002336 var cmd1 = 'setline(1,'
2337 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002338 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01002339 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002340
Bram Moolenaard2c61702020-09-06 15:58:36 +02002341 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002342 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002343
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002344 var cmd_first = 'call '
2345 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002346 execute cmd_first .. cmd_last
2347 assert_equal('execute-var-var', getline(1))
2348 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02002349
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002350 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002351 execute 'echomsg' (n ? '"true"' : '"no"')
2352 assert_match('^true$', Screenline(&lines))
2353
Bram Moolenaare0de1712020-12-02 17:36:54 +01002354 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002355 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
2356
Bram Moolenaard2c61702020-09-06 15:58:36 +02002357 CheckDefFailure(['execute xxx'], 'E1001:', 1)
2358 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
2359 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002360enddef
2361
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002362def Test_execute_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002363 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002364 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002365 vim9script
2366 execute 'g:someVar'
2367 .. ' = ' ..
2368 '28'
2369 assert_equal(28, g:someVar)
2370 unlet g:someVar
2371 END
2372 CheckScriptSuccess(lines)
2373enddef
2374
Bram Moolenaarad39c092020-02-26 18:23:43 +01002375def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002376 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02002377 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002378 assert_match('^something$', Screenline(&lines))
2379
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002380 echo "some" # comment
2381 echon "thing"
2382 assert_match('^something$', Screenline(&lines))
2383
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002384 var str1 = 'some'
2385 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002386 echo str1 str2
2387 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002388
Bram Moolenaard2c61702020-09-06 15:58:36 +02002389 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01002390enddef
2391
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002392def Test_echomsg_cmd()
2393 echomsg 'some' 'more' # comment
2394 assert_match('^some more$', Screenline(&lines))
2395 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002396 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002397 assert_match('^some more$', Screenline(&lines))
2398
Bram Moolenaard2c61702020-09-06 15:58:36 +02002399 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002400enddef
2401
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002402def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002403 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002404 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002405 vim9script
2406 echomsg 'here'
2407 .. ' is ' ..
2408 'a message'
2409 assert_match('^here is a message$', Screenline(&lines))
2410 END
2411 CheckScriptSuccess(lines)
2412enddef
2413
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002414def Test_echoerr_cmd()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002415 try
2416 echoerr 'something' 'wrong' # comment
2417 catch
2418 assert_match('something wrong', v:exception)
2419 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002420enddef
2421
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002422def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002423 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002424 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002425 vim9script
2426 try
2427 echoerr 'this'
2428 .. ' is ' ..
2429 'wrong'
2430 catch
2431 assert_match('this is wrong', v:exception)
2432 endtry
2433 END
2434 CheckScriptSuccess(lines)
2435enddef
2436
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002437def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002438 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002439 vim9script
2440 new
2441 for var in range(0, 3)
2442 append(line('$'), var)
2443 endfor
2444 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2445 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01002446
2447 var result = ''
2448 for i in [1, 2, 3]
2449 var loop = ' loop ' .. i
2450 result ..= loop
2451 endfor
2452 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002453 END
2454 writefile(lines, 'Xvim9for.vim')
2455 source Xvim9for.vim
2456 delete('Xvim9for.vim')
2457enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002459def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002460 var lines =<< trim END
2461 var result = ''
2462 for cnt in range(7)
2463 if cnt == 4
2464 break
2465 endif
2466 if cnt == 2
2467 continue
2468 endif
2469 result ..= cnt .. '_'
2470 endfor
2471 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02002472
Bram Moolenaarf2253962021-04-13 20:53:13 +02002473 var concat = ''
2474 for str in eval('["one", "two"]')
2475 concat ..= str
2476 endfor
2477 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002478
Bram Moolenaarf2253962021-04-13 20:53:13 +02002479 var total = 0
2480 for nr in
2481 [1, 2, 3]
2482 total += nr
2483 endfor
2484 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002485
Bram Moolenaarf2253962021-04-13 20:53:13 +02002486 total = 0
2487 for nr
2488 in [1, 2, 3]
2489 total += nr
2490 endfor
2491 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002492
Bram Moolenaarf2253962021-04-13 20:53:13 +02002493 total = 0
2494 for nr
2495 in
2496 [1, 2, 3]
2497 total += nr
2498 endfor
2499 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01002500
Bram Moolenaara3589a02021-04-14 13:30:46 +02002501 # with type
2502 total = 0
2503 for n: number in [1, 2, 3]
2504 total += n
2505 endfor
2506 assert_equal(6, total)
2507
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002508 var chars = ''
2509 for s: string in 'foobar'
2510 chars ..= s
2511 endfor
2512 assert_equal('foobar', chars)
2513
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02002514 chars = ''
2515 for x: string in {a: 'a', b: 'b'}->values()
2516 chars ..= x
2517 endfor
2518 assert_equal('ab', chars)
2519
Bram Moolenaara3589a02021-04-14 13:30:46 +02002520 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02002521 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02002522 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2523 res ..= n .. s
2524 endfor
2525 assert_equal('1a2b', res)
2526
Bram Moolenaar444d8782021-06-26 12:40:56 +02002527 # unpack with one var
2528 var reslist = []
2529 for [x] in [['aaa'], ['bbb']]
2530 reslist->add(x)
2531 endfor
2532 assert_equal(['aaa', 'bbb'], reslist)
2533
Bram Moolenaara3589a02021-04-14 13:30:46 +02002534 # loop over string
2535 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02002536 for c in 'aéc̀d'
2537 res ..= c .. '-'
2538 endfor
2539 assert_equal('a-é-c̀-d-', res)
2540
2541 res = ''
2542 for c in ''
2543 res ..= c .. '-'
2544 endfor
2545 assert_equal('', res)
2546
2547 res = ''
2548 for c in test_null_string()
2549 res ..= c .. '-'
2550 endfor
2551 assert_equal('', res)
2552
2553 var foo: list<dict<any>> = [
2554 {a: 'Cat'}
2555 ]
2556 for dd in foo
2557 dd.counter = 12
2558 endfor
2559 assert_equal([{a: 'Cat', counter: 12}], foo)
2560 END
2561 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002562enddef
2563
2564def Test_for_loop_fails()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002565 CheckDefAndScriptFailure2(['for '], 'E1097:', 'E690:')
2566 CheckDefAndScriptFailure2(['for x'], 'E1097:', 'E690:')
2567 CheckDefAndScriptFailure2(['for x in'], 'E1097:', 'E15:')
2568 CheckDefAndScriptFailure(['for # in range(5)'], 'E690:')
2569 CheckDefAndScriptFailure(['for i In range(5)'], 'E690:')
2570 CheckDefAndScriptFailure2(['var x = 5', 'for x in range(5)', 'endfor'], 'E1017:', 'E1041:')
Bram Moolenaard4ab8072021-07-08 19:22:12 +02002571 CheckScriptFailure(['vim9script', 'var x = 5', 'for x in range(5)', '# comment', 'endfor'], 'E1041:', 3)
Bram Moolenaar822ba242020-05-24 23:00:18 +02002572 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002573 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02002574 CheckDefFailure(['for i in xxx'], 'E1001:')
2575 CheckDefFailure(['endfor'], 'E588:')
2576 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002577
2578 # wrong type detected at compile time
2579 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2580
2581 # wrong type detected at runtime
2582 g:adict = {a: 1}
2583 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2584 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002585
2586 var lines =<< trim END
2587 var d: list<dict<any>> = [{a: 0}]
2588 for e in d
2589 e = {a: 0, b: ''}
2590 endfor
2591 END
2592 CheckDefAndScriptFailure2(lines, 'E1018:', 'E46:', 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002593
2594 lines =<< trim END
2595 for nr: number in ['foo']
2596 endfor
2597 END
2598 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaar404557e2021-07-05 21:41:48 +02002599
2600 lines =<< trim END
2601 for n : number in [1, 2]
2602 echo n
2603 endfor
2604 END
2605 CheckDefAndScriptFailure(lines, 'E1059:', 1)
Bram Moolenaarefc5db52021-07-07 21:21:30 +02002606
2607 lines =<< trim END
2608 var d: dict<number> = {a: 1, b: 2}
2609 for [k: job, v: job] in d->items()
2610 echo k v
2611 endfor
2612 END
2613 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected job but got string', 2)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002614enddef
2615
Bram Moolenaarea870692020-12-02 14:24:30 +01002616def Test_for_loop_script_var()
2617 # cannot use s:var in a :def function
2618 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1101:')
2619
2620 # can use s:var in Vim9 script, with or without s:
2621 var lines =<< trim END
2622 vim9script
2623 var total = 0
2624 for s:var in [1, 2, 3]
2625 total += s:var
2626 endfor
2627 assert_equal(6, total)
2628
2629 total = 0
2630 for var in [1, 2, 3]
2631 total += var
2632 endfor
2633 assert_equal(6, total)
2634 END
2635enddef
2636
Bram Moolenaar792f7862020-11-23 08:31:18 +01002637def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01002638 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002639 var result = []
2640 for [v1, v2] in [[1, 2], [3, 4]]
2641 result->add(v1)
2642 result->add(v2)
2643 endfor
2644 assert_equal([1, 2, 3, 4], result)
2645
2646 result = []
2647 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2648 result->add(v1)
2649 result->add(v2)
2650 result->add(v3)
2651 endfor
2652 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
2653
2654 result = []
2655 for [&ts, &sw] in [[1, 2], [3, 4]]
2656 result->add(&ts)
2657 result->add(&sw)
2658 endfor
2659 assert_equal([1, 2, 3, 4], result)
2660
2661 var slist: list<string>
2662 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
2663 slist->add($LOOPVAR)
2664 slist->add(@r)
2665 slist->add(v:errmsg)
2666 endfor
2667 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
2668
2669 slist = []
2670 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
2671 slist->add(g:globalvar)
2672 slist->add(b:bufvar)
2673 slist->add(w:winvar)
2674 slist->add(t:tabvar)
2675 endfor
2676 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01002677 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaarb777da92021-05-22 21:40:39 +02002678
2679 var res = []
2680 for [_, n, _] in [[1, 2, 3], [4, 5, 6]]
2681 res->add(n)
2682 endfor
2683 assert_equal([2, 5], res)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002684 END
2685 CheckDefAndScriptSuccess(lines)
2686
2687 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01002688 for [v1, v2] in [[1, 2, 3], [3, 4]]
2689 echo v1 v2
2690 endfor
2691 END
2692 CheckDefExecFailure(lines, 'E710:', 1)
2693
2694 lines =<< trim END
2695 for [v1, v2] in [[1], [3, 4]]
2696 echo v1 v2
2697 endfor
2698 END
2699 CheckDefExecFailure(lines, 'E711:', 1)
2700
2701 lines =<< trim END
2702 for [v1, v1] in [[1, 2], [3, 4]]
2703 echo v1
2704 endfor
2705 END
2706 CheckDefExecFailure(lines, 'E1017:', 1)
2707enddef
2708
Bram Moolenaarc150c092021-02-13 15:02:46 +01002709def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002710 var lines =<< trim END
2711 var looped = 0
2712 var cleanup = 0
2713 for i in range(3)
2714 looped += 1
2715 try
2716 eval [][0]
2717 catch
2718 continue
2719 finally
2720 cleanup += 1
2721 endtry
2722 endfor
2723 assert_equal(3, looped)
2724 assert_equal(3, cleanup)
2725 END
2726 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002727enddef
2728
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002729def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002730 var result = ''
2731 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002732 while cnt < 555
2733 if cnt == 3
2734 break
2735 endif
2736 cnt += 1
2737 if cnt == 2
2738 continue
2739 endif
2740 result ..= cnt .. '_'
2741 endwhile
2742 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002743
2744 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002745 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002746 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002747enddef
2748
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002749def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02002750 CheckDefFailure(['while xxx'], 'E1001:')
2751 CheckDefFailure(['endwhile'], 'E588:')
2752 CheckDefFailure(['continue'], 'E586:')
2753 CheckDefFailure(['if true', 'continue'], 'E586:')
2754 CheckDefFailure(['break'], 'E587:')
2755 CheckDefFailure(['if true', 'break'], 'E587:')
2756 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002757
2758 var lines =<< trim END
2759 var s = ''
2760 while s = ''
2761 endwhile
2762 END
2763 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002764enddef
2765
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002766def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002767 var caught = false
2768 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002769 try
2770 while 1
2771 x += 1
2772 if x == 100
2773 feedkeys("\<C-C>", 'Lt')
2774 endif
2775 endwhile
2776 catch
2777 caught = true
2778 assert_equal(100, x)
2779 endtry
2780 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02002781 # consume the CTRL-C
2782 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002783enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01002784
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002785def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002786 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002787 'one',
2788 'two',
2789 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002790 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002791 assert_equal(['one', 'two', 'three'], mylist)
2792
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002793 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01002794 ['one']: 1,
2795 ['two']: 2,
2796 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002797 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002798 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01002799 assert_equal({one: 1, two: 2, three: 3}, mydict)
2800 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002801 one: 1, # comment
2802 two: # comment
2803 2, # comment
2804 three: 3 # comment
2805 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002806 assert_equal({one: 1, two: 2, three: 3}, mydict)
2807 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002808 one: 1,
2809 two:
2810 2,
2811 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002812 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002813 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002814
2815 assert_equal(
2816 ['one', 'two', 'three'],
2817 split('one two three')
2818 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002819enddef
2820
Bram Moolenaar7a092242020-04-16 22:10:49 +02002821def Test_vim9_comment()
2822 CheckScriptSuccess([
2823 'vim9script',
2824 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002825 '#something',
2826 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002827 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002828
2829 split Xfile
2830 CheckScriptSuccess([
2831 'vim9script',
2832 'edit #something',
2833 ])
2834 CheckScriptSuccess([
2835 'vim9script',
2836 'edit #{something',
2837 ])
2838 close
2839
Bram Moolenaar7a092242020-04-16 22:10:49 +02002840 CheckScriptFailure([
2841 'vim9script',
2842 ':# something',
2843 ], 'E488:')
2844 CheckScriptFailure([
2845 '# something',
2846 ], 'E488:')
2847 CheckScriptFailure([
2848 ':# something',
2849 ], 'E488:')
2850
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002851 { # block start
2852 } # block end
2853 CheckDefFailure([
2854 '{# comment',
2855 ], 'E488:')
2856 CheckDefFailure([
2857 '{',
2858 '}# comment',
2859 ], 'E488:')
2860
2861 echo "yes" # comment
2862 CheckDefFailure([
2863 'echo "yes"# comment',
2864 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002865 CheckScriptSuccess([
2866 'vim9script',
2867 'echo "yes" # something',
2868 ])
2869 CheckScriptFailure([
2870 'vim9script',
2871 'echo "yes"# something',
2872 ], 'E121:')
2873 CheckScriptFailure([
2874 'vim9script',
2875 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002876 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002877 CheckScriptFailure([
2878 'echo "yes" # something',
2879 ], 'E121:')
2880
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002881 exe "echo" # comment
2882 CheckDefFailure([
2883 'exe "echo"# comment',
2884 ], 'E488:')
2885 CheckScriptSuccess([
2886 'vim9script',
2887 'exe "echo" # something',
2888 ])
2889 CheckScriptFailure([
2890 'vim9script',
2891 'exe "echo"# something',
2892 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002893 CheckScriptFailure([
2894 'vim9script',
2895 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002896 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002897 CheckScriptFailure([
2898 'exe "echo" # something',
2899 ], 'E121:')
2900
Bram Moolenaar7a092242020-04-16 22:10:49 +02002901 CheckDefFailure([
2902 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002903 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002904 'catch',
2905 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002906 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002907 CheckScriptFailure([
2908 'vim9script',
2909 'try# comment',
2910 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002911 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002912 CheckDefFailure([
2913 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002914 ' throw#comment',
2915 'catch',
2916 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002917 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002918 CheckDefFailure([
2919 'try',
2920 ' throw "yes"#comment',
2921 'catch',
2922 'endtry',
2923 ], 'E488:')
2924 CheckDefFailure([
2925 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002926 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002927 'catch# comment',
2928 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002929 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002930 CheckScriptFailure([
2931 'vim9script',
2932 'try',
2933 ' echo "yes"',
2934 'catch# comment',
2935 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002936 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002937 CheckDefFailure([
2938 'try',
2939 ' echo "yes"',
2940 'catch /pat/# comment',
2941 'endtry',
2942 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002943 CheckDefFailure([
2944 'try',
2945 'echo "yes"',
2946 'catch',
2947 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002948 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002949 CheckScriptFailure([
2950 'vim9script',
2951 'try',
2952 ' echo "yes"',
2953 'catch',
2954 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002955 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002956
2957 CheckScriptSuccess([
2958 'vim9script',
2959 'hi # comment',
2960 ])
2961 CheckScriptFailure([
2962 'vim9script',
2963 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002964 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002965 CheckScriptSuccess([
2966 'vim9script',
2967 'hi Search # comment',
2968 ])
2969 CheckScriptFailure([
2970 'vim9script',
2971 'hi Search# comment',
2972 ], 'E416:')
2973 CheckScriptSuccess([
2974 'vim9script',
2975 'hi link This Search # comment',
2976 ])
2977 CheckScriptFailure([
2978 'vim9script',
2979 'hi link This That# comment',
2980 ], 'E413:')
2981 CheckScriptSuccess([
2982 'vim9script',
2983 'hi clear This # comment',
2984 'hi clear # comment',
2985 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002986 # not tested, because it doesn't give an error but a warning:
2987 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02002988 CheckScriptFailure([
2989 'vim9script',
2990 'hi clear# comment',
2991 ], 'E416:')
2992
2993 CheckScriptSuccess([
2994 'vim9script',
2995 'hi Group term=bold',
2996 'match Group /todo/ # comment',
2997 ])
2998 CheckScriptFailure([
2999 'vim9script',
3000 'hi Group term=bold',
3001 'match Group /todo/# comment',
3002 ], 'E488:')
3003 CheckScriptSuccess([
3004 'vim9script',
3005 'match # comment',
3006 ])
3007 CheckScriptFailure([
3008 'vim9script',
3009 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003010 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003011 CheckScriptSuccess([
3012 'vim9script',
3013 'match none # comment',
3014 ])
3015 CheckScriptFailure([
3016 'vim9script',
3017 'match none# comment',
3018 ], 'E475:')
3019
3020 CheckScriptSuccess([
3021 'vim9script',
3022 'menutrans clear # comment',
3023 ])
3024 CheckScriptFailure([
3025 'vim9script',
3026 'menutrans clear# comment text',
3027 ], 'E474:')
3028
3029 CheckScriptSuccess([
3030 'vim9script',
3031 'syntax clear # comment',
3032 ])
3033 CheckScriptFailure([
3034 'vim9script',
3035 'syntax clear# comment text',
3036 ], 'E28:')
3037 CheckScriptSuccess([
3038 'vim9script',
3039 'syntax keyword Word some',
3040 'syntax clear Word # comment',
3041 ])
3042 CheckScriptFailure([
3043 'vim9script',
3044 'syntax keyword Word some',
3045 'syntax clear Word# comment text',
3046 ], 'E28:')
3047
3048 CheckScriptSuccess([
3049 'vim9script',
3050 'syntax list # comment',
3051 ])
3052 CheckScriptFailure([
3053 'vim9script',
3054 'syntax list# comment text',
3055 ], 'E28:')
3056
3057 CheckScriptSuccess([
3058 'vim9script',
3059 'syntax match Word /pat/ oneline # comment',
3060 ])
3061 CheckScriptFailure([
3062 'vim9script',
3063 'syntax match Word /pat/ oneline# comment',
3064 ], 'E475:')
3065
3066 CheckScriptSuccess([
3067 'vim9script',
3068 'syntax keyword Word word # comm[ent',
3069 ])
3070 CheckScriptFailure([
3071 'vim9script',
3072 'syntax keyword Word word# comm[ent',
3073 ], 'E789:')
3074
3075 CheckScriptSuccess([
3076 'vim9script',
3077 'syntax match Word /pat/ # comment',
3078 ])
3079 CheckScriptFailure([
3080 'vim9script',
3081 'syntax match Word /pat/# comment',
3082 ], 'E402:')
3083
3084 CheckScriptSuccess([
3085 'vim9script',
3086 'syntax match Word /pat/ contains=Something # comment',
3087 ])
3088 CheckScriptFailure([
3089 'vim9script',
3090 'syntax match Word /pat/ contains=Something# comment',
3091 ], 'E475:')
3092 CheckScriptFailure([
3093 'vim9script',
3094 'syntax match Word /pat/ contains= # comment',
3095 ], 'E406:')
3096 CheckScriptFailure([
3097 'vim9script',
3098 'syntax match Word /pat/ contains=# comment',
3099 ], 'E475:')
3100
3101 CheckScriptSuccess([
3102 'vim9script',
3103 'syntax region Word start=/pat/ end=/pat/ # comment',
3104 ])
3105 CheckScriptFailure([
3106 'vim9script',
3107 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02003108 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003109
3110 CheckScriptSuccess([
3111 'vim9script',
3112 'syntax sync # comment',
3113 ])
3114 CheckScriptFailure([
3115 'vim9script',
3116 'syntax sync# comment',
3117 ], 'E404:')
3118 CheckScriptSuccess([
3119 'vim9script',
3120 'syntax sync ccomment # comment',
3121 ])
3122 CheckScriptFailure([
3123 'vim9script',
3124 'syntax sync ccomment# comment',
3125 ], 'E404:')
3126
3127 CheckScriptSuccess([
3128 'vim9script',
3129 'syntax cluster Some contains=Word # comment',
3130 ])
3131 CheckScriptFailure([
3132 'vim9script',
3133 'syntax cluster Some contains=Word# comment',
3134 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003135
3136 CheckScriptSuccess([
3137 'vim9script',
3138 'command Echo echo # comment',
3139 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003140 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003141 ])
3142 CheckScriptFailure([
3143 'vim9script',
3144 'command Echo echo# comment',
3145 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003146 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003147 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003148
3149 var curdir = getcwd()
3150 CheckScriptSuccess([
3151 'command Echo cd " comment',
3152 'Echo',
3153 'delcommand Echo',
3154 ])
3155 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01003156 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003157 'command Echo cd # comment',
3158 'Echo',
3159 'delcommand Echo',
3160 ])
3161 CheckScriptFailure([
3162 'vim9script',
3163 'command Echo cd " comment',
3164 'Echo',
3165 ], 'E344:')
3166 delcommand Echo
3167 chdir(curdir)
3168
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003169 CheckScriptFailure([
3170 'vim9script',
3171 'command Echo# comment',
3172 ], 'E182:')
3173 CheckScriptFailure([
3174 'vim9script',
3175 'command Echo echo',
3176 'command Echo# comment',
3177 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003178 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003179
3180 CheckScriptSuccess([
3181 'vim9script',
3182 'function # comment',
3183 ])
3184 CheckScriptFailure([
3185 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02003186 'function " comment',
3187 ], 'E129:')
3188 CheckScriptFailure([
3189 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003190 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003191 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003192 CheckScriptSuccess([
3193 'vim9script',
3194 'function CheckScriptSuccess # comment',
3195 ])
3196 CheckScriptFailure([
3197 'vim9script',
3198 'function CheckScriptSuccess# comment',
3199 ], 'E488:')
3200
3201 CheckScriptSuccess([
3202 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003203 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003204 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003205 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003206 ])
3207 CheckScriptFailure([
3208 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003209 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003210 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003211 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003212 ], 'E488:')
3213
3214 CheckScriptSuccess([
3215 'vim9script',
3216 'call execute("ls") # comment',
3217 ])
3218 CheckScriptFailure([
3219 'vim9script',
3220 'call execute("ls")# comment',
3221 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02003222
3223 CheckScriptFailure([
3224 'def Test() " comment',
3225 'enddef',
3226 ], 'E488:')
3227 CheckScriptFailure([
3228 'vim9script',
3229 'def Test() " comment',
3230 'enddef',
3231 ], 'E488:')
3232
3233 CheckScriptSuccess([
3234 'func Test() " comment',
3235 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003236 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02003237 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02003238 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02003239 'vim9script',
3240 'func Test() " comment',
3241 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02003242 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02003243
3244 CheckScriptSuccess([
3245 'def Test() # comment',
3246 'enddef',
3247 ])
3248 CheckScriptFailure([
3249 'func Test() # comment',
3250 'endfunc',
3251 ], 'E488:')
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003252
3253 var lines =<< trim END
3254 vim9script
3255 syn region Text
3256 \ start='foo'
3257 #\ comment
3258 \ end='bar'
Bram Moolenaar5072b472021-06-03 21:56:10 +02003259 syn region Text start='foo'
3260 #\ comment
3261 \ end='bar'
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003262 END
3263 CheckScriptSuccess(lines)
3264
3265 lines =<< trim END
3266 vim9script
3267 syn region Text
3268 \ start='foo'
3269 "\ comment
3270 \ end='bar'
3271 END
3272 CheckScriptFailure(lines, 'E399:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003273enddef
3274
3275def Test_vim9_comment_gui()
3276 CheckCanRunGui
3277
3278 CheckScriptFailure([
3279 'vim9script',
3280 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003281 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003282 CheckScriptFailure([
3283 'vim9script',
3284 'gui -f#comment'
3285 ], 'E499:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003286enddef
3287
Bram Moolenaara26b9702020-04-18 19:53:28 +02003288def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02003289 au TabEnter *.vim g:entered = 1
3290 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02003291
3292 edit test.vim
3293 doautocmd TabEnter #comment
3294 assert_equal(1, g:entered)
3295
3296 doautocmd TabEnter f.x
3297 assert_equal(2, g:entered)
3298
3299 g:entered = 0
3300 doautocmd TabEnter f.x #comment
3301 assert_equal(2, g:entered)
3302
3303 assert_fails('doautocmd Syntax#comment', 'E216:')
3304
3305 au! TabEnter
3306 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003307
3308 CheckScriptSuccess([
3309 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02003310 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003311 'b:var = 456',
3312 'w:var = 777',
3313 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003314 'unlet g:var w:var # something',
3315 ])
3316
3317 CheckScriptFailure([
3318 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003319 'let var = 123',
3320 ], 'E1126: Cannot use :let in Vim9 script')
3321
3322 CheckScriptFailure([
3323 'vim9script',
3324 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003325 ], 'E1016: Cannot declare a global variable:')
3326
3327 CheckScriptFailure([
3328 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003329 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003330 ], 'E1016: Cannot declare a buffer variable:')
3331
3332 CheckScriptFailure([
3333 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003334 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003335 ], 'E1016: Cannot declare a window variable:')
3336
3337 CheckScriptFailure([
3338 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003339 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003340 ], 'E1016: Cannot declare a tab variable:')
3341
3342 CheckScriptFailure([
3343 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003344 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003345 ], 'E1016: Cannot declare a v: variable:')
3346
3347 CheckScriptFailure([
3348 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003349 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003350 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02003351
3352 CheckScriptFailure([
3353 'vim9script',
3354 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003355 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003356 ], 'E108:')
3357
3358 CheckScriptFailure([
3359 'let g:var = 123',
3360 'unlet g:var # something',
3361 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003362
3363 CheckScriptSuccess([
3364 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003365 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003366 ' echo "yes"',
3367 'elseif 2 #comment',
3368 ' echo "no"',
3369 'endif',
3370 ])
3371
3372 CheckScriptFailure([
3373 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003374 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003375 ' echo "yes"',
3376 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003377 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003378
3379 CheckScriptFailure([
3380 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003381 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003382 ' echo "yes"',
3383 'elseif 2#comment',
3384 ' echo "no"',
3385 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003386 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003387
3388 CheckScriptSuccess([
3389 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003390 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003391 ])
3392
3393 CheckScriptFailure([
3394 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003395 'var v = 1# comment6',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003396 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003397
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003398 CheckScriptSuccess([
3399 'vim9script',
3400 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003401 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003402 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003403 'dsearch /pat/ #comment',
3404 'bwipe!',
3405 ])
3406
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003407 CheckScriptFailure([
3408 'vim9script',
3409 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003410 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003411 ':$',
3412 'dsearch /pat/#comment',
3413 'bwipe!',
3414 ], 'E488:')
3415
3416 CheckScriptFailure([
3417 'vim9script',
3418 'func! SomeFunc()',
3419 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02003420enddef
3421
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003422def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003423 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003424 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02003425 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003426 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02003427 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003428 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02003429 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003430 END
3431 writefile(lines, 'Xfinished')
3432 source Xfinished
3433 assert_equal('two', g:res)
3434
3435 unlet g:res
3436 delete('Xfinished')
3437enddef
3438
Bram Moolenaara5d00772020-05-14 23:20:55 +02003439def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003440 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02003441 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02003442 def GetValue(): string
3443 return theVal
3444 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003445 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02003446 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02003447 theVal = 'else'
3448 g:laterVal = GetValue()
3449 END
3450 writefile(lines, 'Xforward')
3451 source Xforward
3452 assert_equal('something', g:initVal)
3453 assert_equal('else', g:laterVal)
3454
3455 unlet g:initVal
3456 unlet g:laterVal
3457 delete('Xforward')
3458enddef
3459
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003460def Test_source_vim9_from_legacy()
Bram Moolenaara6294952020-12-27 13:39:50 +01003461 var vim9_lines =<< trim END
3462 vim9script
3463 var local = 'local'
3464 g:global = 'global'
3465 export var exported = 'exported'
3466 export def GetText(): string
3467 return 'text'
3468 enddef
3469 END
3470 writefile(vim9_lines, 'Xvim9_script.vim')
3471
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003472 var legacy_lines =<< trim END
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003473 source Xvim9_script.vim
3474
3475 call assert_false(exists('local'))
3476 call assert_false(exists('exported'))
3477 call assert_false(exists('s:exported'))
3478 call assert_equal('global', global)
3479 call assert_equal('global', g:global)
3480
3481 " imported variable becomes script-local
3482 import exported from './Xvim9_script.vim'
3483 call assert_equal('exported', s:exported)
3484 call assert_false(exists('exported'))
3485
3486 " imported function becomes script-local
3487 import GetText from './Xvim9_script.vim'
3488 call assert_equal('text', s:GetText())
3489 call assert_false(exists('*GetText'))
3490 END
3491 writefile(legacy_lines, 'Xlegacy_script.vim')
3492
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003493 source Xlegacy_script.vim
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003494 assert_equal('global', g:global)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003495 unlet g:global
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003496
3497 delete('Xlegacy_script.vim')
3498 delete('Xvim9_script.vim')
3499enddef
Bram Moolenaara5d00772020-05-14 23:20:55 +02003500
Bram Moolenaare535db82021-03-31 21:07:24 +02003501def Test_declare_script_in_func()
3502 var lines =<< trim END
3503 vim9script
3504 func Declare()
3505 let s:local = 123
3506 endfunc
3507 Declare()
3508 assert_equal(123, local)
3509
3510 var error: string
3511 try
3512 local = 'asdf'
3513 catch
3514 error = v:exception
3515 endtry
3516 assert_match('E1012: Type mismatch; expected number but got string', error)
3517
3518 lockvar local
3519 try
3520 local = 999
3521 catch
3522 error = v:exception
3523 endtry
3524 assert_match('E741: Value is locked: local', error)
3525 END
3526 CheckScriptSuccess(lines)
3527enddef
3528
3529
Bram Moolenaar7d699702020-08-14 20:52:28 +02003530func Test_vim9script_not_global()
3531 " check that items defined in Vim9 script are script-local, not global
3532 let vim9lines =<< trim END
3533 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003534 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02003535 func TheFunc()
3536 echo 'local'
3537 endfunc
3538 def DefFunc()
3539 echo 'local'
3540 enddef
3541 END
3542 call writefile(vim9lines, 'Xvim9script.vim')
3543 source Xvim9script.vim
3544 try
3545 echo g:var
3546 assert_report('did not fail')
3547 catch /E121:/
3548 " caught
3549 endtry
3550 try
3551 call TheFunc()
3552 assert_report('did not fail')
3553 catch /E117:/
3554 " caught
3555 endtry
3556 try
3557 call DefFunc()
3558 assert_report('did not fail')
3559 catch /E117:/
3560 " caught
3561 endtry
3562
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003563 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02003564endfunc
3565
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02003566def Test_vim9_copen()
3567 # this was giving an error for setting w:quickfix_title
3568 copen
3569 quit
3570enddef
3571
Bram Moolenaar03290b82020-12-19 16:30:44 +01003572" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003573def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003574 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003575 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01003576 def some#gettest(): string
3577 return 'test'
3578 enddef
3579 g:some#name = 'name'
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003580 g:some#dict = {key: 'value'}
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003581
3582 def some#varargs(a1: string, ...l: list<string>): string
3583 return a1 .. l[0] .. l[1]
3584 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01003585 END
3586
3587 mkdir('Xdir/autoload', 'p')
3588 writefile(lines, 'Xdir/autoload/some.vim')
3589 var save_rtp = &rtp
3590 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3591
3592 assert_equal('test', g:some#gettest())
3593 assert_equal('name', g:some#name)
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003594 assert_equal('value', g:some#dict.key)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003595 g:some#other = 'other'
3596 assert_equal('other', g:some#other)
3597
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003598 assert_equal('abc', some#varargs('a', 'b', 'c'))
3599
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003600 # upper case script name works
3601 lines =<< trim END
3602 vim9script
3603 def Other#getOther(): string
3604 return 'other'
3605 enddef
3606 END
3607 writefile(lines, 'Xdir/autoload/Other.vim')
3608 assert_equal('other', g:Other#getOther())
3609
Bram Moolenaar03290b82020-12-19 16:30:44 +01003610 delete('Xdir', 'rf')
3611 &rtp = save_rtp
3612enddef
3613
3614" test using a vim9script that is auto-loaded from an autocmd
3615def Test_vim9_aucmd_autoload()
3616 var lines =<< trim END
3617 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003618 def foo#test()
3619 echomsg getreg('"')
3620 enddef
3621 END
3622
3623 mkdir('Xdir/autoload', 'p')
3624 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003625 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003626 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3627 augroup test
3628 autocmd TextYankPost * call foo#test()
3629 augroup END
3630
3631 normal Y
3632
3633 augroup test
3634 autocmd!
3635 augroup END
3636 delete('Xdir', 'rf')
3637 &rtp = save_rtp
3638enddef
3639
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003640" This was causing a crash because suppress_errthrow wasn't reset.
3641def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003642 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003643 vim9script
3644 def crash#func()
3645 try
3646 for x in List()
3647 endfor
3648 catch
3649 endtry
3650 g:ok = true
3651 enddef
3652 fu List()
3653 invalid
3654 endfu
3655 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01003656 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003657 catch /wontmatch/
3658 endtry
3659 END
3660 call mkdir('Xruntime/autoload', 'p')
3661 call writefile(lines, 'Xruntime/autoload/crash.vim')
3662
3663 # run in a separate Vim to avoid the side effects of assert_fails()
3664 lines =<< trim END
3665 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
3666 call crash#func()
3667 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003668 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003669 END
3670 writefile(lines, 'Xscript')
3671 RunVim([], [], '-S Xscript')
3672 assert_equal(['ok'], readfile('Xdidit'))
3673
3674 delete('Xdidit')
3675 delete('Xscript')
3676 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01003677
3678 lines =<< trim END
3679 vim9script
3680 var foo#bar = 'asdf'
3681 END
3682 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003683enddef
3684
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003685def Test_script_var_in_autocmd()
3686 # using a script variable from an autocommand, defined in a :def function in a
3687 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003688 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003689 let s:counter = 1
3690 def s:Func()
3691 au! CursorHold
3692 au CursorHold * s:counter += 1
3693 enddef
3694 call s:Func()
3695 doau CursorHold
3696 call assert_equal(2, s:counter)
3697 au! CursorHold
3698 END
3699 CheckScriptSuccess(lines)
3700enddef
3701
Bram Moolenaar3896a102020-08-09 14:33:55 +02003702def Test_cmdline_win()
3703 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
3704 # the command line window.
3705 mkdir('rtp/syntax', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003706 var export_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02003707 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003708 export var That = 'yes'
Bram Moolenaar3896a102020-08-09 14:33:55 +02003709 END
3710 writefile(export_lines, 'rtp/syntax/Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003711 var import_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02003712 vim9script
3713 import That from './Xexport.vim'
3714 END
3715 writefile(import_lines, 'rtp/syntax/vim.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003716 var save_rtp = &rtp
Bram Moolenaar3896a102020-08-09 14:33:55 +02003717 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
3718 syntax on
3719 augroup CmdWin
3720 autocmd CmdwinEnter * g:got_there = 'yes'
3721 augroup END
3722 # this will open and also close the cmdline window
3723 feedkeys('q:', 'xt')
3724 assert_equal('yes', g:got_there)
3725
3726 augroup CmdWin
3727 au!
3728 augroup END
3729 &rtp = save_rtp
3730 delete('rtp', 'rf')
3731enddef
3732
Bram Moolenaare3d46852020-08-29 13:39:17 +02003733def Test_invalid_sid()
3734 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003735
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003736 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02003737 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02003738 endif
3739 delete('Xdidit')
3740enddef
3741
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003742def Test_restoring_cpo()
3743 writefile(['vim9script', 'set nocp'], 'Xsourced')
3744 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
3745 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
3746 assert_equal(['done'], readfile('Xdone'))
3747 endif
3748 delete('Xsourced')
3749 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01003750 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01003751
3752 writefile(['vim9script'], 'XanotherScript')
3753 set cpo=aABceFsMny>
3754 edit XanotherScript
3755 so %
3756 assert_equal('aABceFsMny>', &cpo)
3757 :1del
3758 w
3759 so %
3760 assert_equal('aABceFsMny>', &cpo)
3761
3762 delete('XanotherScript')
3763 set cpo&vim
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003764enddef
3765
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003766" Use :function so we can use Check commands
3767func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003768 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003769 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003770
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003771 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003772 vim9script
3773 def script#func()
3774 enddef
3775 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003776 call mkdir('Xdir/autoload', 'p')
3777 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003778
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003779 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003780 vim9script
3781 set cpo+=M
3782 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02003783 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003784 setline(1, 'some text')
3785 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003786 call writefile(lines, 'XTest_redraw_cpo')
3787 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
3788 call term_sendkeys(buf, "V:")
3789 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003790
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003791 " clean up
3792 call term_sendkeys(buf, "\<Esc>u")
3793 call StopVimInTerminal(buf)
3794 call delete('XTest_redraw_cpo')
3795 call delete('Xdir', 'rf')
3796endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003797
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003798
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003799def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003800 var lines =<< trim END
3801 var name: any
3802 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003803 END
3804 CheckDefAndScriptSuccess(lines)
3805enddef
3806
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003807func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02003808 CheckRunVimInTerminal
3809
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003810 " call indirectly to avoid compilation error for missing functions
3811 call Run_Test_define_func_at_command_line()
3812endfunc
3813
3814def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003815 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003816 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003817 func CheckAndQuit()
3818 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
3819 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
3820 endfunc
3821 END
3822 writefile([''], 'Xdidcmd')
3823 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01003824 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003825 # define Afunc() on the command line
3826 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
3827 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003828 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003829
3830 call StopVimInTerminal(buf)
3831 delete('XcallFunc')
3832 delete('Xdidcmd')
3833enddef
3834
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003835def Test_script_var_scope()
3836 var lines =<< trim END
3837 vim9script
3838 if true
3839 if true
3840 var one = 'one'
3841 echo one
3842 endif
3843 echo one
3844 endif
3845 END
3846 CheckScriptFailure(lines, 'E121:', 7)
3847
3848 lines =<< trim END
3849 vim9script
3850 if true
3851 if false
3852 var one = 'one'
3853 echo one
3854 else
3855 var one = 'one'
3856 echo one
3857 endif
3858 echo one
3859 endif
3860 END
3861 CheckScriptFailure(lines, 'E121:', 10)
3862
3863 lines =<< trim END
3864 vim9script
3865 while true
3866 var one = 'one'
3867 echo one
3868 break
3869 endwhile
3870 echo one
3871 END
3872 CheckScriptFailure(lines, 'E121:', 7)
3873
3874 lines =<< trim END
3875 vim9script
3876 for i in range(1)
3877 var one = 'one'
3878 echo one
3879 endfor
3880 echo one
3881 END
3882 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003883
3884 lines =<< trim END
3885 vim9script
3886 {
3887 var one = 'one'
3888 assert_equal('one', one)
3889 }
3890 assert_false(exists('one'))
3891 assert_false(exists('s:one'))
3892 END
3893 CheckScriptSuccess(lines)
3894
3895 lines =<< trim END
3896 vim9script
3897 {
3898 var one = 'one'
3899 echo one
3900 }
3901 echo one
3902 END
3903 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003904enddef
3905
Bram Moolenaar352134b2020-10-17 22:04:08 +02003906def Test_catch_exception_in_callback()
3907 var lines =<< trim END
3908 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02003909 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02003910 try
3911 var x: string
3912 var y: string
3913 # this error should be caught with CHECKLEN
3914 [x, y] = ['']
3915 catch
3916 g:caught = 'yes'
3917 endtry
3918 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01003919 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02003920 feedkeys("\r", 'xt')
3921 END
3922 CheckScriptSuccess(lines)
3923
3924 unlet g:caught
3925enddef
3926
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003927def Test_no_unknown_error_after_error()
3928 if !has('unix') || !has('job')
3929 throw 'Skipped: not unix of missing +job feature'
3930 endif
3931 var lines =<< trim END
3932 vim9script
3933 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02003934 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003935 eval [][0]
3936 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02003937 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003938 sleep 1m
3939 source += l
3940 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01003941 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01003942 while job_status(myjob) == 'run'
3943 sleep 10m
3944 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01003945 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02003946 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003947 END
3948 writefile(lines, 'Xdef')
3949 assert_fails('so Xdef', ['E684:', 'E1012:'])
3950 delete('Xdef')
3951enddef
3952
Bram Moolenaar4324d872020-12-01 20:12:24 +01003953def InvokeNormal()
3954 exe "norm! :m+1\r"
3955enddef
3956
3957def Test_invoke_normal_in_visual_mode()
3958 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
3959 new
3960 setline(1, ['aaa', 'bbb'])
3961 feedkeys("V\<F3>", 'xt')
3962 assert_equal(['bbb', 'aaa'], getline(1, 2))
3963 xunmap <F3>
3964enddef
3965
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003966def Test_white_space_after_command()
3967 var lines =<< trim END
3968 exit_cb: Func})
3969 END
3970 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003971
3972 lines =<< trim END
3973 e#
3974 END
3975 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003976enddef
3977
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003978def Test_script_var_gone_when_sourced_twice()
3979 var lines =<< trim END
3980 vim9script
3981 if exists('g:guard')
3982 finish
3983 endif
3984 g:guard = 1
3985 var name = 'thename'
3986 def g:GetName(): string
3987 return name
3988 enddef
3989 def g:SetName(arg: string)
3990 name = arg
3991 enddef
3992 END
3993 writefile(lines, 'XscriptTwice.vim')
3994 so XscriptTwice.vim
3995 assert_equal('thename', g:GetName())
3996 g:SetName('newname')
3997 assert_equal('newname', g:GetName())
3998 so XscriptTwice.vim
3999 assert_fails('call g:GetName()', 'E1149:')
4000 assert_fails('call g:SetName("x")', 'E1149:')
4001
4002 delfunc g:GetName
4003 delfunc g:SetName
4004 delete('XscriptTwice.vim')
4005 unlet g:guard
4006enddef
4007
4008def Test_import_gone_when_sourced_twice()
4009 var exportlines =<< trim END
4010 vim9script
4011 if exists('g:guard')
4012 finish
4013 endif
4014 g:guard = 1
4015 export var name = 'someName'
4016 END
4017 writefile(exportlines, 'XexportScript.vim')
4018
4019 var lines =<< trim END
4020 vim9script
4021 import name from './XexportScript.vim'
4022 def g:GetName(): string
4023 return name
4024 enddef
4025 END
4026 writefile(lines, 'XscriptImport.vim')
4027 so XscriptImport.vim
4028 assert_equal('someName', g:GetName())
4029
4030 so XexportScript.vim
4031 assert_fails('call g:GetName()', 'E1149:')
4032
4033 delfunc g:GetName
4034 delete('XexportScript.vim')
4035 delete('XscriptImport.vim')
4036 unlet g:guard
4037enddef
4038
Bram Moolenaar10b94212021-02-19 21:42:57 +01004039def Test_unsupported_commands()
4040 var lines =<< trim END
4041 ka
4042 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004043 CheckDefFailure(lines, 'E476:')
4044 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaar10b94212021-02-19 21:42:57 +01004045
4046 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01004047 :1ka
4048 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004049 CheckDefFailure(lines, 'E476:')
4050 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaarada1d872021-02-20 08:16:51 +01004051
4052 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01004053 t
4054 END
4055 CheckDefFailure(lines, 'E1100:')
4056 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4057
4058 lines =<< trim END
4059 x
4060 END
4061 CheckDefFailure(lines, 'E1100:')
4062 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4063
4064 lines =<< trim END
4065 xit
4066 END
4067 CheckDefFailure(lines, 'E1100:')
4068 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4069enddef
4070
Bram Moolenaarc70fe462021-04-17 17:59:19 +02004071def Test_mapping_line_number()
4072 var lines =<< trim END
4073 vim9script
4074 def g:FuncA()
4075 # Some comment
4076 FuncB(0)
4077 enddef
4078 # Some comment
4079 def FuncB(
4080 # Some comment
4081 n: number
4082 )
4083 exe 'nno '
4084 # Some comment
4085 .. '<F3> a'
4086 .. 'b'
4087 .. 'c'
4088 enddef
4089 END
4090 CheckScriptSuccess(lines)
4091 var res = execute('verbose nmap <F3>')
4092 assert_match('No mapping found', res)
4093
4094 g:FuncA()
4095 res = execute('verbose nmap <F3>')
4096 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
4097
4098 nunmap <F3>
4099 delfunc g:FuncA
4100enddef
4101
Bram Moolenaardeb108b2021-07-08 17:35:36 +02004102def Test_option_set()
4103 # legacy script allows for white space
4104 var lines =<< trim END
4105 set foldlevel =11
4106 call assert_equal(11, &foldlevel)
4107 END
4108 CheckScriptSuccess(lines)
4109
4110 set foldlevel
4111 set foldlevel=12
4112 assert_equal(12, &foldlevel)
4113 set foldlevel+=2
4114 assert_equal(14, &foldlevel)
4115 set foldlevel-=3
4116 assert_equal(11, &foldlevel)
4117
4118 lines =<< trim END
4119 set foldlevel =1
4120 END
4121 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: =1')
4122
4123 lines =<< trim END
4124 set foldlevel +=1
4125 END
4126 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: +=1')
4127
4128 lines =<< trim END
4129 set foldlevel ^=1
4130 END
4131 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: ^=1')
4132
4133 lines =<< trim END
4134 set foldlevel -=1
4135 END
4136 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: -=1')
4137
4138 set foldlevel&
4139enddef
4140
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004141def Test_option_modifier()
Bram Moolenaar1594f312021-07-08 16:40:13 +02004142 # legacy script allows for white space
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004143 var lines =<< trim END
4144 set hlsearch & hlsearch !
4145 call assert_equal(1, &hlsearch)
4146 END
4147 CheckScriptSuccess(lines)
4148
Bram Moolenaar1594f312021-07-08 16:40:13 +02004149 set hlsearch
4150 set hlsearch!
4151 assert_equal(false, &hlsearch)
4152
4153 set hlsearch
4154 set hlsearch&
4155 assert_equal(false, &hlsearch)
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004156
4157 lines =<< trim END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004158 set hlsearch &
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004159 END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004160 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: &')
4161
4162 lines =<< trim END
4163 set hlsearch !
4164 END
4165 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: !')
4166
4167 set hlsearch&
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004168enddef
4169
Bram Moolenaard9162552021-07-11 15:26:13 +02004170def ProfiledFunc()
4171 var n = 3
4172 echo [[1, 2], [3, 4]]->filter((_, l) => l[0] == n)
4173enddef
4174
4175" Execute this near the end, profiling doesn't stop until Vim exists.
4176" This only tests that it works, not the profiling output.
4177def Test_xx_profile_with_lambda()
4178 profile start Xprofile.log
4179 profile func ProfiledFunc
4180 ProfiledFunc()
4181enddef
4182
Bram Moolenaar585fea72020-04-02 22:33:21 +02004183" Keep this last, it messes up highlighting.
4184def Test_substitute_cmd()
4185 new
4186 setline(1, 'something')
4187 :substitute(some(other(
4188 assert_equal('otherthing', getline(1))
4189 bwipe!
4190
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004191 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004192 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02004193 vim9script
4194 new
4195 setline(1, 'something')
4196 :substitute(some(other(
4197 assert_equal('otherthing', getline(1))
4198 bwipe!
4199 END
4200 writefile(lines, 'Xvim9lines')
4201 source Xvim9lines
4202
4203 delete('Xvim9lines')
4204enddef
4205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker