blob: 8c7f85f167d681056e941e2ec94c87ae42798873 [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 Moolenaarcfe435d2020-04-25 20:02:55 +02005source vim9.vim
Bram Moolenaar37294bd2021-03-10 13:40:08 +01006source screendump.vim
Bram Moolenaard8448622022-01-07 21:39:52 +00007source shared.vim
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008
Bram Moolenaarb79ee0c2022-01-01 12:17:00 +00009def Test_vim9script_feature()
10 # example from the help, here the feature is always present
11 var lines =<< trim END
12 " old style comment
13 if !has('vim9script')
14 " legacy commands would go here
15 finish
16 endif
17 vim9script
18 # Vim9 script commands go here
19 g:didit = true
20 END
21 CheckScriptSuccess(lines)
22 assert_equal(true, g:didit)
23 unlet g:didit
24enddef
25
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020026def Test_range_only()
27 new
28 setline(1, ['blah', 'Blah'])
29 :/Blah/
30 assert_equal(2, getcurpos()[1])
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020031 bwipe!
32
33 # without range commands use current line
34 new
35 setline(1, ['one', 'two', 'three'])
36 :2
37 print
38 assert_equal('two', Screenline(&lines))
39 :3
40 list
41 assert_equal('three$', Screenline(&lines))
Bram Moolenaarb8554302021-02-15 21:30:30 +010042
43 # missing command does not print the line
44 var lines =<< trim END
45 vim9script
46 :1|
47 assert_equal('three$', Screenline(&lines))
48 :|
49 assert_equal('three$', Screenline(&lines))
50 END
51 CheckScriptSuccess(lines)
52
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020053 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +010054
55 # won't generate anything
56 if false
57 :123
58 endif
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020059enddef
60
Bram Moolenaara6e67e42020-05-15 23:36:40 +020061let g:alist = [7]
62let g:astring = 'text'
Bram Moolenaarf0b9f432020-07-17 23:03:17 +020063let g:anumber = 123
Bram Moolenaar6e587dc2020-02-06 13:15:52 +010064
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020065def Test_delfunction()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020066 # Check function is defined in script namespace
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020067 CheckScriptSuccess([
68 'vim9script',
69 'func CheckMe()',
70 ' return 123',
71 'endfunc',
72 'assert_equal(123, s:CheckMe())',
73 ])
74
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020075 # Check function in script namespace cannot be deleted
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020076 CheckScriptFailure([
77 'vim9script',
78 'func DeleteMe1()',
79 'endfunc',
80 'delfunction DeleteMe1',
81 ], 'E1084:')
82 CheckScriptFailure([
83 'vim9script',
84 'func DeleteMe2()',
85 'endfunc',
86 'def DoThat()',
87 ' delfunction DeleteMe2',
88 'enddef',
89 'DoThat()',
90 ], 'E1084:')
91 CheckScriptFailure([
92 'vim9script',
93 'def DeleteMe3()',
94 'enddef',
95 'delfunction DeleteMe3',
96 ], 'E1084:')
97 CheckScriptFailure([
98 'vim9script',
99 'def DeleteMe4()',
100 'enddef',
101 'def DoThat()',
102 ' delfunction DeleteMe4',
103 'enddef',
104 'DoThat()',
105 ], 'E1084:')
Bram Moolenaar925e9fd2020-07-25 15:41:11 +0200106
107 # Check that global :def function can be replaced and deleted
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200108 var lines =<< trim END
Bram Moolenaar925e9fd2020-07-25 15:41:11 +0200109 vim9script
110 def g:Global(): string
111 return "yes"
112 enddef
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 function can be replaced by a :def function and deleted
124 lines =<< trim END
125 vim9script
126 func g:Global()
127 return "yes"
128 endfunc
129 assert_equal("yes", g:Global())
130 def! g:Global(): string
131 return "no"
132 enddef
133 assert_equal("no", g:Global())
134 delfunc g:Global
135 assert_false(exists('*g:Global'))
136 END
137 CheckScriptSuccess(lines)
138
139 # Check that global :def function can be replaced by a function and deleted
140 lines =<< trim END
141 vim9script
142 def g:Global(): string
143 return "yes"
144 enddef
145 assert_equal("yes", g:Global())
146 func! g:Global()
147 return "no"
148 endfunc
149 assert_equal("no", g:Global())
150 delfunc g:Global
151 assert_false(exists('*g:Global'))
152 END
153 CheckScriptSuccess(lines)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200154enddef
155
Bram Moolenaar08052222020-09-14 17:04:31 +0200156def Test_wrong_type()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200157 CheckDefFailure(['var name: list<nothing>'], 'E1010:')
158 CheckDefFailure(['var name: list<list<nothing>>'], 'E1010:')
159 CheckDefFailure(['var name: dict<nothing>'], 'E1010:')
160 CheckDefFailure(['var name: dict<dict<nothing>>'], 'E1010:')
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100161
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200162 CheckDefFailure(['var name: dict<number'], 'E1009:')
163 CheckDefFailure(['var name: dict<list<number>'], 'E1009:')
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100164
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200165 CheckDefFailure(['var name: ally'], 'E1010:')
166 CheckDefFailure(['var name: bram'], 'E1010:')
167 CheckDefFailure(['var name: cathy'], 'E1010:')
168 CheckDefFailure(['var name: dom'], 'E1010:')
169 CheckDefFailure(['var name: freddy'], 'E1010:')
170 CheckDefFailure(['var name: john'], 'E1010:')
171 CheckDefFailure(['var name: larry'], 'E1010:')
172 CheckDefFailure(['var name: ned'], 'E1010:')
173 CheckDefFailure(['var name: pam'], 'E1010:')
174 CheckDefFailure(['var name: sam'], 'E1010:')
175 CheckDefFailure(['var name: vim'], 'E1010:')
Bram Moolenaara0a9f432020-04-28 21:29:34 +0200176
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200177 CheckDefFailure(['var Ref: number', 'Ref()'], 'E1085:')
178 CheckDefFailure(['var Ref: string', 'var res = Ref()'], 'E1085:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200179enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100180
Bram Moolenaar10c65862020-10-08 21:16:42 +0200181def Test_script_wrong_type()
182 var lines =<< trim END
183 vim9script
184 var s:dict: dict<string>
185 s:dict['a'] = ['x']
186 END
187 CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got list<string>', 3)
188enddef
189
Bram Moolenaar08052222020-09-14 17:04:31 +0200190def Test_const()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200191 CheckDefFailure(['final name = 234', 'name = 99'], 'E1018:')
192 CheckDefFailure(['final one = 234', 'var one = 99'], 'E1017:')
193 CheckDefFailure(['final list = [1, 2]', 'var list = [3, 4]'], 'E1017:')
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200194 CheckDefFailure(['final two'], 'E1125:')
195 CheckDefFailure(['final &option'], 'E996:')
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200196
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200197 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200198 final list = [1, 2, 3]
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200199 list[0] = 4
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200200 list->assert_equal([4, 2, 3])
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200201 const other = [5, 6, 7]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200202 other->assert_equal([5, 6, 7])
Bram Moolenaar71abe482020-09-14 22:28:30 +0200203
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200204 var varlist = [7, 8]
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200205 const constlist = [1, varlist, 3]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200206 varlist[0] = 77
Bram Moolenaar23e2e112021-08-03 21:16:18 +0200207 constlist[1][1] = 88
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200208 var cl = constlist[1]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200209 cl[1] = 88
210 constlist->assert_equal([1, [77, 88], 3])
211
Bram Moolenaare0de1712020-12-02 17:36:54 +0100212 var vardict = {five: 5, six: 6}
213 const constdict = {one: 1, two: vardict, three: 3}
Bram Moolenaar71abe482020-09-14 22:28:30 +0200214 vardict['five'] = 55
Bram Moolenaar23e2e112021-08-03 21:16:18 +0200215 constdict['two']['six'] = 66
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200216 var cd = constdict['two']
Bram Moolenaar71abe482020-09-14 22:28:30 +0200217 cd['six'] = 66
Bram Moolenaare0de1712020-12-02 17:36:54 +0100218 constdict->assert_equal({one: 1, two: {five: 55, six: 66}, three: 3})
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200219 END
220 CheckDefAndScriptSuccess(lines)
Bram Moolenaar08052222020-09-14 17:04:31 +0200221enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200223def Test_const_bang()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200224 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200225 const var = 234
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200226 var = 99
227 END
228 CheckDefExecFailure(lines, 'E1018:', 2)
229 CheckScriptFailure(['vim9script'] + lines, 'E46:', 3)
230
231 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200232 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200233 ll[0] = 99
234 END
235 CheckDefExecFailure(lines, 'E1119:', 2)
236 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
237
238 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200239 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200240 ll[3] = 99
241 END
242 CheckDefExecFailure(lines, 'E1118:', 2)
243 CheckScriptFailure(['vim9script'] + lines, 'E684:', 3)
244
245 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100246 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200247 dd["one"] = 99
248 END
249 CheckDefExecFailure(lines, 'E1121:', 2)
250 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
251
252 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100253 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200254 dd["three"] = 99
255 END
256 CheckDefExecFailure(lines, 'E1120:')
257 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
258enddef
259
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200260def Test_range_no_colon()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200261 CheckDefFailure(['%s/a/b/'], 'E1050:')
262 CheckDefFailure(['+ s/a/b/'], 'E1050:')
263 CheckDefFailure(['- s/a/b/'], 'E1050:')
264 CheckDefFailure(['. s/a/b/'], 'E1050:')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200265enddef
266
267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268def Test_block()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200269 var outer = 1
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100270 {
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200271 var inner = 2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272 assert_equal(1, outer)
273 assert_equal(2, inner)
274 }
275 assert_equal(1, outer)
Bram Moolenaar3f1e9f02021-02-27 22:36:43 +0100276
277 {|echo 'yes'|}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278enddef
279
Bram Moolenaar08052222020-09-14 17:04:31 +0200280def Test_block_failure()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200281 CheckDefFailure(['{', 'var inner = 1', '}', 'echo inner'], 'E1001:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200282 CheckDefFailure(['}'], 'E1025:')
283 CheckDefFailure(['{', 'echo 1'], 'E1026:')
284enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200286def Test_block_local_vars()
287 var lines =<< trim END
288 vim9script
Bram Moolenaared234f22020-10-15 20:42:20 +0200289 v:testing = 1
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200290 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200291 var text = ['hello']
292 def SayHello(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200293 return text
294 enddef
295 def SetText(v: string)
Bram Moolenaared234f22020-10-15 20:42:20 +0200296 text = [v]
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200297 enddef
298 endif
299
300 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200301 var text = ['again']
302 def SayAgain(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200303 return text
304 enddef
305 endif
Bram Moolenaared234f22020-10-15 20:42:20 +0200306
307 # test that the "text" variables are not cleaned up
308 test_garbagecollect_now()
309
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200310 defcompile
311
Bram Moolenaared234f22020-10-15 20:42:20 +0200312 assert_equal(['hello'], SayHello())
313 assert_equal(['again'], SayAgain())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200314
315 SetText('foobar')
Bram Moolenaared234f22020-10-15 20:42:20 +0200316 assert_equal(['foobar'], SayHello())
317
318 call writefile(['ok'], 'Xdidit')
319 qall!
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200320 END
Bram Moolenaared234f22020-10-15 20:42:20 +0200321
322 # need to execute this with a separate Vim instance to avoid the current
323 # context gets garbage collected.
324 writefile(lines, 'Xscript')
325 RunVim([], [], '-S Xscript')
326 assert_equal(['ok'], readfile('Xdidit'))
327
328 delete('Xscript')
329 delete('Xdidit')
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200330enddef
331
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200332def Test_block_local_vars_with_func()
333 var lines =<< trim END
334 vim9script
335 if true
336 var foo = 'foo'
337 if true
338 var bar = 'bar'
339 def Func(): list<string>
340 return [foo, bar]
341 enddef
342 endif
343 endif
344 # function is compiled here, after blocks have finished, can still access
345 # "foo" and "bar"
346 assert_equal(['foo', 'bar'], Func())
347 END
348 CheckScriptSuccess(lines)
349enddef
350
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200351" legacy func for command that's defined later
352func InvokeSomeCommand()
353 SomeCommand
354endfunc
355
356def Test_autocommand_block()
357 com SomeCommand {
358 g:someVar = 'some'
359 }
360 InvokeSomeCommand()
361 assert_equal('some', g:someVar)
362
363 delcommand SomeCommand
364 unlet g:someVar
365enddef
366
367def Test_command_block()
368 au BufNew *.xml {
369 g:otherVar = 'other'
370 }
371 split other.xml
372 assert_equal('other', g:otherVar)
373
374 bwipe!
375 au! BufNew *.xml
376 unlet g:otherVar
377enddef
378
Bram Moolenaard032f342020-07-18 18:13:02 +0200379func g:NoSuchFunc()
380 echo 'none'
381endfunc
382
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100383def Test_try_catch_throw()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200384 var l = []
Bram Moolenaar7a092242020-04-16 22:10:49 +0200385 try # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100386 add(l, '1')
387 throw 'wrong'
388 add(l, '2')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200389 catch # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390 add(l, v:exception)
Bram Moolenaar7a092242020-04-16 22:10:49 +0200391 finally # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100392 add(l, '3')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200393 endtry # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100394 assert_equal(['1', 'wrong', '3'], l)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200395
Bram Moolenaare8593122020-07-18 15:17:02 +0200396 l = []
397 try
398 try
399 add(l, '1')
400 throw 'wrong'
401 add(l, '2')
402 catch /right/
403 add(l, v:exception)
404 endtry
405 catch /wrong/
406 add(l, 'caught')
Bram Moolenaar373863e2020-09-26 17:20:53 +0200407 fina
Bram Moolenaare8593122020-07-18 15:17:02 +0200408 add(l, 'finally')
409 endtry
410 assert_equal(['1', 'caught', 'finally'], l)
411
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200412 var n: number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200413 try
414 n = l[3]
415 catch /E684:/
416 n = 99
417 endtry
418 assert_equal(99, n)
419
Bram Moolenaar69f70502021-01-01 16:10:46 +0100420 var done = 'no'
421 if 0
422 try | catch | endtry
423 else
424 done = 'yes'
425 endif
426 assert_equal('yes', done)
427
428 done = 'no'
429 if 1
430 done = 'yes'
431 else
432 try | catch | endtry
433 done = 'never'
434 endif
435 assert_equal('yes', done)
436
437 if 1
438 else
439 try | catch /pat/ | endtry
440 try | catch /pat/
441 endtry
442 try
443 catch /pat/ | endtry
444 try
445 catch /pat/
446 endtry
447 endif
448
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200449 try
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200450 # string slice returns a string, not a number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200451 n = g:astring[3]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200452 catch /E1012:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200453 n = 77
454 endtry
455 assert_equal(77, n)
456
457 try
458 n = l[g:astring]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200459 catch /E1012:/
Bram Moolenaar56acb092020-08-16 14:48:19 +0200460 n = 88
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200461 endtry
Bram Moolenaar56acb092020-08-16 14:48:19 +0200462 assert_equal(88, n)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200463
464 try
465 n = s:does_not_exist
466 catch /E121:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200467 n = 111
468 endtry
469 assert_equal(111, n)
470
471 try
472 n = g:does_not_exist
473 catch /E121:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200474 n = 121
475 endtry
476 assert_equal(121, n)
477
Bram Moolenaare0de1712020-12-02 17:36:54 +0100478 var d = {one: 1}
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200479 try
480 n = d[g:astring]
481 catch /E716:/
482 n = 222
483 endtry
484 assert_equal(222, n)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200485
486 try
487 n = -g:astring
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +0200488 catch /E1012:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200489 n = 233
490 endtry
491 assert_equal(233, n)
492
493 try
494 n = +g:astring
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +0200495 catch /E1012:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200496 n = 244
497 endtry
498 assert_equal(244, n)
499
500 try
501 n = +g:alist
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +0200502 catch /E1012:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200503 n = 255
504 endtry
505 assert_equal(255, n)
506
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200507 var nd: dict<any>
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200508 try
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100509 nd = {[g:alist]: 1}
510 catch /E1105:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200511 n = 266
512 endtry
513 assert_equal(266, n)
514
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +0000515 l = [1, 2, 3]
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200516 try
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +0000517 [n] = l
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200518 catch /E1093:/
519 n = 277
520 endtry
521 assert_equal(277, n)
522
Bram Moolenaare8593122020-07-18 15:17:02 +0200523 try
524 &ts = g:astring
Bram Moolenaar5e654232020-09-16 15:22:00 +0200525 catch /E1012:/
Bram Moolenaare8593122020-07-18 15:17:02 +0200526 n = 288
527 endtry
528 assert_equal(288, n)
529
530 try
531 &backspace = 'asdf'
532 catch /E474:/
533 n = 299
534 endtry
535 assert_equal(299, n)
536
537 l = [1]
538 try
539 l[3] = 3
540 catch /E684:/
541 n = 300
542 endtry
543 assert_equal(300, n)
544
545 try
Bram Moolenaare8593122020-07-18 15:17:02 +0200546 unlet g:does_not_exist
547 catch /E108:/
548 n = 322
549 endtry
550 assert_equal(322, n)
551
552 try
Bram Moolenaar2bede172020-11-19 18:53:18 +0100553 d = {text: 1, [g:astring]: 2}
Bram Moolenaare8593122020-07-18 15:17:02 +0200554 catch /E721:/
555 n = 333
556 endtry
557 assert_equal(333, n)
558
559 try
560 l = DeletedFunc()
561 catch /E933:/
562 n = 344
563 endtry
564 assert_equal(344, n)
Bram Moolenaard032f342020-07-18 18:13:02 +0200565
566 try
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200567 echo range(1, 2, 0)
568 catch /E726:/
Bram Moolenaard032f342020-07-18 18:13:02 +0200569 n = 355
570 endtry
571 assert_equal(355, n)
572
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200573 var P = function('g:NoSuchFunc')
Bram Moolenaard032f342020-07-18 18:13:02 +0200574 delfunc g:NoSuchFunc
575 try
576 echo P()
577 catch /E117:/
578 n = 366
579 endtry
580 assert_equal(366, n)
581
582 try
583 echo g:NoSuchFunc()
584 catch /E117:/
585 n = 377
586 endtry
587 assert_equal(377, n)
588
589 try
590 echo g:alist + 4
591 catch /E745:/
592 n = 388
593 endtry
594 assert_equal(388, n)
595
596 try
597 echo 4 + g:alist
598 catch /E745:/
599 n = 399
600 endtry
601 assert_equal(399, n)
602
603 try
604 echo g:alist.member
605 catch /E715:/
606 n = 400
607 endtry
608 assert_equal(400, n)
609
610 try
611 echo d.member
612 catch /E716:/
613 n = 411
614 endtry
615 assert_equal(411, n)
Bram Moolenaard9d77892021-02-12 21:32:47 +0100616
617 var counter = 0
618 for i in range(4)
619 try
620 eval [][0]
621 catch
622 endtry
623 counter += 1
624 endfor
625 assert_equal(4, counter)
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100626
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +0200627 # no requirement for spaces before |
628 try|echo 0|catch|endtry
629
Bram Moolenaar003312b2021-12-20 10:55:35 +0000630 # return in try with finally
631 def ReturnInTry(): number
632 var ret = 4
633 try
634 return ret
635 catch /this/
636 return -1
637 catch /that/
638 return -1
639 finally
640 # changing ret has no effect
641 ret = 7
642 endtry
643 return -2
644 enddef
645 assert_equal(4, ReturnInTry())
646
647 # return in catch with finally
648 def ReturnInCatch(): number
649 var ret = 5
650 try
651 throw 'getout'
652 return -1
653 catch /getout/
654 # ret is evaluated here
655 return ret
656 finally
657 # changing ret later has no effect
658 ret = -3
659 endtry
660 return -2
661 enddef
662 assert_equal(5, ReturnInCatch())
663
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100664 # return in finally after empty catch
665 def ReturnInFinally(): number
666 try
667 finally
Bram Moolenaar003312b2021-12-20 10:55:35 +0000668 return 6
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100669 endtry
Bram Moolenaar003312b2021-12-20 10:55:35 +0000670 return -1
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100671 enddef
Bram Moolenaar003312b2021-12-20 10:55:35 +0000672 assert_equal(6, ReturnInFinally())
Bram Moolenaar8ac681a2021-06-15 20:06:34 +0200673
674 var lines =<< trim END
675 vim9script
676 try
677 acos('0.5')
678 ->setline(1)
679 catch
680 g:caught = v:exception
681 endtry
682 END
683 CheckScriptSuccess(lines)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200684 assert_match('E1219: Float or Number required for argument 1', g:caught)
Bram Moolenaar8ac681a2021-06-15 20:06:34 +0200685 unlet g:caught
Bram Moolenaar41978282021-07-04 14:47:30 +0200686
687 # missing catch and/or finally
688 lines =<< trim END
689 vim9script
690 try
691 echo 'something'
692 endtry
693 END
694 CheckScriptFailure(lines, 'E1032:')
rbtnn84934992021-08-07 13:26:53 +0200695
696 # skipping try-finally-endtry when try-finally-endtry is used in another block
697 lines =<< trim END
698 if v:true
699 try
700 finally
701 endtry
702 else
703 try
704 finally
705 endtry
706 endif
707 END
708 CheckDefAndScriptSuccess(lines)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709enddef
710
Bram Moolenaar3f987b52021-06-30 12:02:24 +0200711def Test_try_in_catch()
712 var lines =<< trim END
713 vim9script
714 var seq = []
715 def DoIt()
716 try
717 seq->add('throw 1')
718 eval [][0]
719 seq->add('notreached')
720 catch
721 seq->add('catch')
722 try
723 seq->add('throw 2')
724 eval [][0]
725 seq->add('notreached')
726 catch /nothing/
727 seq->add('notreached')
728 endtry
729 seq->add('done')
730 endtry
731 enddef
732 DoIt()
733 assert_equal(['throw 1', 'catch', 'throw 2', 'done'], seq)
734 END
735enddef
736
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200737def Test_error_in_catch()
738 var lines =<< trim END
739 try
740 eval [][0]
741 catch /E684:/
742 eval [][0]
743 endtry
744 END
745 CheckDefExecFailure(lines, 'E684:', 4)
746enddef
747
Bram Moolenaar2e34c342021-03-14 12:13:33 +0100748" :while at the very start of a function that :continue jumps to
749def TryContinueFunc()
750 while g:Count < 2
751 g:sequence ..= 't'
752 try
753 echoerr 'Test'
754 catch
755 g:Count += 1
756 g:sequence ..= 'c'
757 continue
758 endtry
759 g:sequence ..= 'e'
760 g:Count += 1
761 endwhile
762enddef
763
764def Test_continue_in_try_in_while()
765 g:Count = 0
766 g:sequence = ''
767 TryContinueFunc()
768 assert_equal('tctc', g:sequence)
769 unlet g:Count
770 unlet g:sequence
771enddef
772
Bram Moolenaar9cb577a2021-02-22 22:45:10 +0100773def Test_nocatch_return_in_try()
774 # return in try block returns normally
775 def ReturnInTry(): string
776 try
777 return '"some message"'
778 catch
779 endtry
780 return 'not reached'
781 enddef
782 exe 'echoerr ' .. ReturnInTry()
783enddef
784
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100785def Test_cnext_works_in_catch()
786 var lines =<< trim END
787 vim9script
Bram Moolenaarc3235272021-07-10 19:42:03 +0200788 au BufEnter * eval 1 + 2
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100789 writefile(['text'], 'Xfile1')
790 writefile(['text'], 'Xfile2')
791 var items = [
792 {lnum: 1, filename: 'Xfile1', valid: true},
793 {lnum: 1, filename: 'Xfile2', valid: true}
794 ]
795 setqflist([], ' ', {items: items})
796 cwindow
797
798 def CnextOrCfirst()
799 # if cnext fails, cfirst is used
800 try
801 cnext
802 catch
803 cfirst
804 endtry
805 enddef
806
807 CnextOrCfirst()
808 CnextOrCfirst()
809 writefile([getqflist({idx: 0}).idx], 'Xresult')
810 qall
811 END
812 writefile(lines, 'XCatchCnext')
813 RunVim([], [], '--clean -S XCatchCnext')
814 assert_equal(['1'], readfile('Xresult'))
815
816 delete('Xfile1')
817 delete('Xfile2')
818 delete('XCatchCnext')
819 delete('Xresult')
820enddef
821
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100822def Test_throw_skipped()
823 if 0
824 throw dontgethere
825 endif
826enddef
827
Bram Moolenaar8f81b222021-01-14 21:47:06 +0100828def Test_nocatch_throw_silenced()
829 var lines =<< trim END
830 vim9script
831 def Func()
832 throw 'error'
833 enddef
834 silent! Func()
835 END
836 writefile(lines, 'XthrowSilenced')
837 source XthrowSilenced
838 delete('XthrowSilenced')
839enddef
840
Bram Moolenaare8593122020-07-18 15:17:02 +0200841def DeletedFunc(): list<any>
842 return ['delete me']
843enddef
844defcompile
845delfunc DeletedFunc
846
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100847def ThrowFromDef()
Bram Moolenaara72cfb82020-04-23 17:07:30 +0200848 throw "getout" # comment
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100849enddef
850
851func CatchInFunc()
852 try
853 call ThrowFromDef()
854 catch
855 let g:thrown_func = v:exception
856 endtry
857endfunc
858
859def CatchInDef()
860 try
861 ThrowFromDef()
862 catch
863 g:thrown_def = v:exception
864 endtry
865enddef
866
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100867def ReturnFinally(): string
868 try
869 return 'intry'
Bram Moolenaar373863e2020-09-26 17:20:53 +0200870 finall
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100871 g:in_finally = 'finally'
872 endtry
873 return 'end'
874enddef
875
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100876def Test_try_catch_nested()
877 CatchInFunc()
878 assert_equal('getout', g:thrown_func)
879
880 CatchInDef()
881 assert_equal('getout', g:thrown_def)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100882
883 assert_equal('intry', ReturnFinally())
884 assert_equal('finally', g:in_finally)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200885
886 var l = []
887 try
888 l->add('1')
889 throw 'bad'
890 l->add('x')
891 catch /bad/
892 l->add('2')
893 try
894 l->add('3')
895 throw 'one'
896 l->add('x')
897 catch /one/
898 l->add('4')
899 try
900 l->add('5')
901 throw 'more'
902 l->add('x')
903 catch /more/
904 l->add('6')
905 endtry
906 endtry
907 endtry
908 assert_equal(['1', '2', '3', '4', '5', '6'], l)
Bram Moolenaar834193a2021-06-30 20:39:15 +0200909
910 l = []
911 try
912 try
913 l->add('1')
914 throw 'foo'
915 l->add('x')
916 catch
917 l->add('2')
918 throw 'bar'
919 l->add('x')
920 finally
921 l->add('3')
922 endtry
923 l->add('x')
924 catch /bar/
925 l->add('4')
926 endtry
927 assert_equal(['1', '2', '3', '4'], l)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100928enddef
929
Bram Moolenaar9939f572020-09-16 22:29:52 +0200930def TryOne(): number
931 try
932 return 0
933 catch
934 endtry
935 return 0
936enddef
937
938def TryTwo(n: number): string
939 try
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200940 var x = {}
Bram Moolenaar9939f572020-09-16 22:29:52 +0200941 catch
942 endtry
943 return 'text'
944enddef
945
946def Test_try_catch_twice()
947 assert_equal('text', TryOne()->TryTwo())
948enddef
949
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100950def Test_try_catch_match()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200951 var seq = 'a'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100952 try
953 throw 'something'
954 catch /nothing/
955 seq ..= 'x'
956 catch /some/
957 seq ..= 'b'
958 catch /asdf/
959 seq ..= 'x'
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200960 catch ?a\?sdf?
961 seq ..= 'y'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100962 finally
963 seq ..= 'c'
964 endtry
965 assert_equal('abc', seq)
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100966enddef
967
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200968def Test_try_catch_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200969 CheckDefFailure(['catch'], 'E603:')
970 CheckDefFailure(['try', 'echo 0', 'catch', 'catch'], 'E1033:')
971 CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
972 CheckDefFailure(['finally'], 'E606:')
973 CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:')
974 CheckDefFailure(['endtry'], 'E602:')
975 CheckDefFailure(['while 1', 'endtry'], 'E170:')
976 CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:')
Bram Moolenaar13106602020-10-04 16:06:05 +0200977 CheckDefFailure(['if 1', 'endtry'], 'E171:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200978 CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:')
Bram Moolenaar585fea72020-04-02 22:33:21 +0200979
Bram Moolenaare4984292020-12-13 14:19:25 +0100980 CheckDefFailure(['throw'], 'E1143:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200981 CheckDefFailure(['throw xxx'], 'E1001:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200982enddef
983
Bram Moolenaar7c5b3c0362021-02-14 22:40:57 +0100984def Try_catch_skipped()
985 var l = []
986 try
987 finally
988 endtry
989
990 if 1
991 else
992 try
993 endtry
994 endif
995enddef
996
997" The skipped try/endtry was updating the wrong instruction.
998def Test_try_catch_skipped()
999 var instr = execute('disassemble Try_catch_skipped')
1000 assert_match("NEWLIST size 0\n", instr)
1001enddef
1002
1003
1004
Bram Moolenaar006ad482020-06-30 20:55:15 +02001005def Test_throw_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001006 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001007 var lines =<< trim END
Bram Moolenaar006ad482020-06-30 20:55:15 +02001008 vim9script
1009 try
1010 throw 'one'
1011 .. 'two'
1012 catch
1013 assert_equal('onetwo', v:exception)
1014 endtry
1015 END
1016 CheckScriptSuccess(lines)
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001017
1018 lines =<< trim END
1019 vim9script
Bram Moolenaar352134b2020-10-17 22:04:08 +02001020 @r = ''
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001021 def Func()
1022 throw @r
1023 enddef
1024 var result = ''
1025 try
1026 Func()
1027 catch /E1129:/
1028 result = 'caught'
1029 endtry
1030 assert_equal('caught', result)
1031 END
1032 CheckScriptSuccess(lines)
Bram Moolenaar006ad482020-06-30 20:55:15 +02001033enddef
1034
Bram Moolenaared677f52020-08-12 16:38:10 +02001035def Test_error_in_nested_function()
Bram Moolenaar03dfde22021-02-14 13:17:22 +01001036 # an error in a nested :function aborts executing in the calling :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001037 var lines =<< trim END
Bram Moolenaared677f52020-08-12 16:38:10 +02001038 vim9script
1039 def Func()
1040 Error()
1041 g:test_var = 1
1042 enddef
1043 func Error() abort
1044 eval [][0]
1045 endfunc
1046 Func()
1047 END
1048 g:test_var = 0
1049 CheckScriptFailure(lines, 'E684:')
1050 assert_equal(0, g:test_var)
1051enddef
1052
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001053def Test_abort_after_error()
1054 var lines =<< trim END
1055 vim9script
1056 while true
1057 echo notfound
1058 endwhile
1059 g:gotthere = true
1060 END
1061 g:gotthere = false
1062 CheckScriptFailure(lines, 'E121:')
1063 assert_false(g:gotthere)
1064 unlet g:gotthere
1065enddef
1066
Bram Moolenaar37c83712020-06-30 21:18:36 +02001067def Test_cexpr_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001068 # only checks line continuation
Bram Moolenaar37c83712020-06-30 21:18:36 +02001069 set errorformat=File\ %f\ line\ %l
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001070 var lines =<< trim END
Bram Moolenaar37c83712020-06-30 21:18:36 +02001071 vim9script
1072 cexpr 'File'
1073 .. ' someFile' ..
1074 ' line 19'
1075 assert_equal(19, getqflist()[0].lnum)
1076 END
1077 CheckScriptSuccess(lines)
1078 set errorformat&
1079enddef
1080
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +02001081def Test_statusline_syntax()
1082 # legacy syntax is used for 'statusline'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001083 var lines =<< trim END
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +02001084 vim9script
1085 func g:Status()
1086 return '%{"x" is# "x"}'
1087 endfunc
1088 set laststatus=2 statusline=%!Status()
1089 redrawstatus
1090 set laststatus statusline=
1091 END
1092 CheckScriptSuccess(lines)
1093enddef
1094
Bram Moolenaarb2097502020-07-19 17:17:02 +02001095def Test_list_vimscript()
1096 # checks line continuation and comments
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001097 var lines =<< trim END
Bram Moolenaarb2097502020-07-19 17:17:02 +02001098 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001099 var mylist = [
Bram Moolenaarb2097502020-07-19 17:17:02 +02001100 'one',
1101 # comment
1102 'two', # empty line follows
1103
1104 'three',
1105 ]
1106 assert_equal(['one', 'two', 'three'], mylist)
1107 END
1108 CheckScriptSuccess(lines)
Bram Moolenaar66250c92020-08-20 15:02:42 +02001109
1110 # check all lines from heredoc are kept
1111 lines =<< trim END
1112 # comment 1
1113 two
1114 # comment 3
1115
1116 five
1117 # comment 6
1118 END
1119 assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines)
Bram Moolenaar4bce26b2021-01-22 22:06:56 +01001120
1121 lines =<< trim END
1122 [{
1123 a: 0}]->string()->assert_equal("[{'a': 0}]")
1124 END
1125 CheckDefAndScriptSuccess(lines)
Bram Moolenaarb2097502020-07-19 17:17:02 +02001126enddef
1127
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001128if has('channel')
1129 let someJob = test_null_job()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001130
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001131 def FuncWithError()
1132 echomsg g:someJob
1133 enddef
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001134
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001135 func Test_convert_emsg_to_exception()
1136 try
1137 call FuncWithError()
1138 catch
1139 call assert_match('Vim:E908:', v:exception)
1140 endtry
1141 endfunc
1142endif
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001143
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01001144def Test_vim9script_mix()
1145 var lines =<< trim END
1146 if has(g:feature)
1147 " legacy script
1148 let g:legacy = 1
1149 finish
1150 endif
1151 vim9script
1152 g:legacy = 0
1153 END
1154 g:feature = 'eval'
1155 g:legacy = -1
1156 CheckScriptSuccess(lines)
1157 assert_equal(1, g:legacy)
1158
1159 g:feature = 'noteval'
1160 g:legacy = -1
1161 CheckScriptSuccess(lines)
1162 assert_equal(0, g:legacy)
1163enddef
1164
Bram Moolenaar750802b2020-02-23 18:08:33 +01001165def Test_vim9script_fails()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1167 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
Bram Moolenaar750802b2020-02-23 18:08:33 +01001168
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001169 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001170 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1171
Bram Moolenaare2e40752020-09-04 21:18:46 +02001172 assert_fails('vim9script', 'E1038:')
Bram Moolenaarc970e422021-03-17 15:03:04 +01001173enddef
1174
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001175def Test_script_var_shadows_function()
1176 var lines =<< trim END
1177 vim9script
1178 def Func(): number
1179 return 123
1180 enddef
1181 var Func = 1
1182 END
1183 CheckScriptFailure(lines, 'E1041:', 5)
1184enddef
1185
Bram Moolenaar052ff292021-12-11 13:54:46 +00001186def Test_function_shadows_script_var()
1187 var lines =<< trim END
1188 vim9script
1189 var Func = 1
1190 def Func(): number
1191 return 123
1192 enddef
1193 END
1194 CheckScriptFailure(lines, 'E1041:', 3)
1195enddef
1196
Bram Moolenaarc3235272021-07-10 19:42:03 +02001197def Test_script_var_shadows_command()
1198 var lines =<< trim END
1199 var undo = 1
1200 undo = 2
1201 assert_equal(2, undo)
1202 END
1203 CheckDefAndScriptSuccess(lines)
1204
1205 lines =<< trim END
1206 var undo = 1
1207 undo
1208 END
1209 CheckDefAndScriptFailure(lines, 'E1207:', 2)
1210enddef
1211
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001212def Test_vim9script_call_wrong_type()
1213 var lines =<< trim END
1214 vim9script
1215 var Time = 'localtime'
1216 Time()
1217 END
1218 CheckScriptFailure(lines, 'E1085:')
1219enddef
1220
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001221def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001222 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001223 vim9script
1224 def FuncYes(): string
1225 return 'yes'
1226 enddef
1227 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001228 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001229 def FuncNo(): string
1230 return 'no'
1231 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001232 def g:DoCheck(no_exists: bool)
1233 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001234 assert_equal('no', FuncNo())
1235 enddef
1236 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001237 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001238 def g:DoCheck(no_exists: bool)
1239 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02001240 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001241 enddef
1242 END
1243
1244 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001245 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001246 source Xreloaded.vim
1247 g:DoCheck(true)
1248
1249 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001250 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001251 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001252 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001253
1254 # FuncNo() is back
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001255 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001256 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001257 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001258
1259 delete('Xreloaded.vim')
1260enddef
1261
Bram Moolenaar89483d42020-05-10 15:24:44 +02001262def Test_vim9script_reload_delvar()
1263 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001264 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02001265 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001266 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001267 END
1268 writefile(lines, 'XreloadVar.vim')
1269 source XreloadVar.vim
1270
1271 # now write the script using the same variable locally - works
1272 lines =<< trim END
1273 vim9script
1274 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001275 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001276 enddef
1277 END
1278 writefile(lines, 'XreloadVar.vim')
1279 source XreloadVar.vim
1280
1281 delete('XreloadVar.vim')
1282enddef
1283
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001284def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001285 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001286 'vim9script',
1287 'def Func()',
1288 ' eval [][0]',
1289 'enddef',
1290 'Func()',
1291 ]
1292 writefile(lines, 'Xtestscript.vim')
1293
1294 for count in range(3)
1295 try
1296 source Xtestscript.vim
1297 catch /E684/
1298 # function name should contain <SNR> every time
1299 assert_match('E684: list index out of range', v:exception)
1300 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
1301 endtry
1302 endfor
1303
1304 delete('Xtestscript.vim')
1305enddef
1306
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02001307def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001308 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02001309 vim9script
1310 def Func()
1311 echo 'one'
1312 enddef
1313 def Func()
1314 echo 'two'
1315 enddef
1316 END
1317 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02001318
1319 lines =<< trim END
1320 vim9script
1321 def Foo(): string
1322 return 'foo'
Bram Moolenaar052ff292021-12-11 13:54:46 +00001323 enddef
Bram Moolenaarfa211f32020-08-07 22:00:26 +02001324 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001325 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02001326 enddef
1327 defcompile
1328 END
1329 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02001330enddef
1331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001333 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001334 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 l->remove(0)
1336 l->add(5)
1337 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001338 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339enddef
1340
Bram Moolenaarae616492020-07-28 20:07:27 +02001341def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001342 CheckDefExecFailure(['a = 1'], 'E1100:')
1343 CheckDefExecFailure(['c = 1'], 'E1100:')
1344 CheckDefExecFailure(['i = 1'], 'E1100:')
1345 CheckDefExecFailure(['t = 1'], 'E1100:')
1346 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02001347
Bram Moolenaarae616492020-07-28 20:07:27 +02001348 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
1349 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02001350 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
1351 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02001352 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
1353 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01001354 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
1355 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02001356 CheckScriptFailure(['vim9script', 't'], 'E1100:')
1357 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
1358 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02001359enddef
1360
Bram Moolenaar158906c2020-02-06 20:39:45 +01001361def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001362 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01001363 if what == 1
1364 res = "one"
1365 elseif what == 2
1366 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01001367 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01001368 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01001369 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01001370 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01001371enddef
1372
Bram Moolenaar158906c2020-02-06 20:39:45 +01001373def Test_if_elseif_else()
1374 assert_equal('one', IfElse(1))
1375 assert_equal('two', IfElse(2))
1376 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001377enddef
1378
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02001379def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001380 CheckDefFailure(['elseif true'], 'E582:')
1381 CheckDefFailure(['else'], 'E581:')
1382 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01001383 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001384 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01001385
1386 var lines =<< trim END
1387 var s = ''
1388 if s = ''
1389 endif
1390 END
1391 CheckDefFailure(lines, 'E488:')
1392
1393 lines =<< trim END
1394 var s = ''
1395 if s == ''
1396 elseif s = ''
1397 endif
1398 END
1399 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02001400enddef
1401
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01001402let g:bool_true = v:true
1403let g:bool_false = v:false
1404
1405def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001406 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01001407 if true ? true : false
1408 res = true
1409 endif
1410 assert_equal(true, res)
1411
Bram Moolenaar585fea72020-04-02 22:33:21 +02001412 g:glob = 2
1413 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02001414 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02001415 endif
1416 assert_equal(2, g:glob)
1417 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02001418 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02001419 endif
1420 assert_equal(3, g:glob)
1421
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01001422 res = false
1423 if g:bool_true ? true : false
1424 res = true
1425 endif
1426 assert_equal(true, res)
1427
1428 res = false
1429 if true ? g:bool_true : false
1430 res = true
1431 endif
1432 assert_equal(true, res)
1433
1434 res = false
1435 if true ? true : g:bool_false
1436 res = true
1437 endif
1438 assert_equal(true, res)
1439
1440 res = false
1441 if true ? false : true
1442 res = true
1443 endif
1444 assert_equal(false, res)
1445
1446 res = false
1447 if false ? false : true
1448 res = true
1449 endif
1450 assert_equal(true, res)
1451
1452 res = false
1453 if false ? true : false
1454 res = true
1455 endif
1456 assert_equal(false, res)
1457
1458 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02001459 if has('xyz') ? true : false
1460 res = true
1461 endif
1462 assert_equal(false, res)
1463
1464 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01001465 if true && true
1466 res = true
1467 endif
1468 assert_equal(true, res)
1469
1470 res = false
1471 if true && false
1472 res = true
1473 endif
1474 assert_equal(false, res)
1475
1476 res = false
1477 if g:bool_true && false
1478 res = true
1479 endif
1480 assert_equal(false, res)
1481
1482 res = false
1483 if true && g:bool_false
1484 res = true
1485 endif
1486 assert_equal(false, res)
1487
1488 res = false
1489 if false && false
1490 res = true
1491 endif
1492 assert_equal(false, res)
1493
1494 res = false
1495 if true || false
1496 res = true
1497 endif
1498 assert_equal(true, res)
1499
1500 res = false
1501 if g:bool_true || false
1502 res = true
1503 endif
1504 assert_equal(true, res)
1505
1506 res = false
1507 if true || g:bool_false
1508 res = true
1509 endif
1510 assert_equal(true, res)
1511
1512 res = false
1513 if false || false
1514 res = true
1515 endif
1516 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02001517
1518 # with constant "false" expression may be invalid so long as the syntax is OK
Bram Moolenaarc3235272021-07-10 19:42:03 +02001519 if false | eval 1 + 2 | endif
Bram Moolenaar3988f642020-08-27 22:43:03 +02001520 if false | eval burp + 234 | endif
1521 if false | echo burp 234 'asd' | endif
1522 if false
1523 burp
1524 endif
Bram Moolenaare525bdd2021-08-07 18:12:40 +02001525
1526 # expression with line breaks skipped
1527 if false
1528 ('aaa'
1529 .. 'bbb'
1530 .. 'ccc'
1531 )->setline(1)
1532 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02001533enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01001534
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02001535def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001536 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
1537 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
1538 CheckDefFailure(["if has('aaa'"], 'E110:')
1539 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01001540enddef
1541
Bram Moolenaar72abcf42020-06-18 18:26:24 +02001542def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001543 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02001544 if i % 2
1545 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001546 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02001547 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001548 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02001549 endif
1550 x += 1
1551 else
1552 x += 1000
1553 endif
1554 return x
1555enddef
1556
1557def Test_nested_if()
1558 assert_equal(1, RunNested(1))
1559 assert_equal(1000, RunNested(2))
1560enddef
1561
Bram Moolenaarad39c092020-02-26 18:23:43 +01001562def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01001563 # missing argument is ignored
1564 execute
1565 execute # comment
1566
Bram Moolenaarad39c092020-02-26 18:23:43 +01001567 new
1568 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001569 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01001570 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02001571
Bram Moolenaard2c61702020-09-06 15:58:36 +02001572 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02001573 assert_equal('execute-string', getline(1))
1574
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001575 var cmd1 = 'setline(1,'
1576 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02001577 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01001578 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02001579
Bram Moolenaard2c61702020-09-06 15:58:36 +02001580 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01001581 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02001582
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001583 var cmd_first = 'call '
1584 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01001585 execute cmd_first .. cmd_last
1586 assert_equal('execute-var-var', getline(1))
1587 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02001588
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001589 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02001590 execute 'echomsg' (n ? '"true"' : '"no"')
1591 assert_match('^true$', Screenline(&lines))
1592
Bram Moolenaare0de1712020-12-02 17:36:54 +01001593 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001594 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
1595
Bram Moolenaard2c61702020-09-06 15:58:36 +02001596 CheckDefFailure(['execute xxx'], 'E1001:', 1)
1597 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
1598 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001599enddef
1600
Bram Moolenaar47e880d2020-06-30 22:02:02 +02001601def Test_execute_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001602 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001603 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02001604 vim9script
1605 execute 'g:someVar'
1606 .. ' = ' ..
1607 '28'
1608 assert_equal(28, g:someVar)
1609 unlet g:someVar
1610 END
1611 CheckScriptSuccess(lines)
1612enddef
1613
Bram Moolenaarad39c092020-02-26 18:23:43 +01001614def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02001615 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02001616 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01001617 assert_match('^something$', Screenline(&lines))
1618
Bram Moolenaara72cfb82020-04-23 17:07:30 +02001619 echo "some" # comment
1620 echon "thing"
1621 assert_match('^something$', Screenline(&lines))
1622
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001623 var str1 = 'some'
1624 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01001625 echo str1 str2
1626 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02001627
Bram Moolenaard2c61702020-09-06 15:58:36 +02001628 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01001629enddef
1630
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001631def Test_echomsg_cmd()
1632 echomsg 'some' 'more' # comment
1633 assert_match('^some more$', Screenline(&lines))
1634 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001635 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001636 assert_match('^some more$', Screenline(&lines))
1637
Bram Moolenaard2c61702020-09-06 15:58:36 +02001638 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001639enddef
1640
Bram Moolenaar47e880d2020-06-30 22:02:02 +02001641def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001642 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001643 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02001644 vim9script
1645 echomsg 'here'
1646 .. ' is ' ..
1647 'a message'
1648 assert_match('^here is a message$', Screenline(&lines))
1649 END
1650 CheckScriptSuccess(lines)
1651enddef
1652
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001653def Test_echoerr_cmd()
Bram Moolenaar7de62622021-08-07 15:05:47 +02001654 var local = 'local'
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001655 try
Bram Moolenaar7de62622021-08-07 15:05:47 +02001656 echoerr 'something' local 'wrong' # comment
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001657 catch
Bram Moolenaar7de62622021-08-07 15:05:47 +02001658 assert_match('something local wrong', v:exception)
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001659 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001660enddef
1661
Bram Moolenaar47e880d2020-06-30 22:02:02 +02001662def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001663 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001664 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02001665 vim9script
1666 try
1667 echoerr 'this'
1668 .. ' is ' ..
1669 'wrong'
1670 catch
1671 assert_match('this is wrong', v:exception)
1672 endtry
1673 END
1674 CheckScriptSuccess(lines)
1675enddef
1676
Bram Moolenaar7de62622021-08-07 15:05:47 +02001677def Test_echoconsole_cmd()
1678 var local = 'local'
1679 echoconsole 'something' local # comment
1680 # output goes anywhere
1681enddef
1682
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001683def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001684 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001685 vim9script
1686 new
1687 for var in range(0, 3)
1688 append(line('$'), var)
1689 endfor
1690 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
1691 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01001692
1693 var result = ''
1694 for i in [1, 2, 3]
1695 var loop = ' loop ' .. i
1696 result ..= loop
1697 endfor
1698 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001699 END
1700 writefile(lines, 'Xvim9for.vim')
1701 source Xvim9for.vim
1702 delete('Xvim9for.vim')
1703enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704
rbtnnbebf0692021-08-21 17:26:50 +02001705def Test_for_skipped_block()
1706 # test skipped blocks at outside of function
1707 var lines =<< trim END
1708 var result = []
1709 if true
1710 for n in [1, 2]
1711 result += [n]
1712 endfor
1713 else
1714 for n in [3, 4]
1715 result += [n]
1716 endfor
1717 endif
1718 assert_equal([1, 2], result)
1719
1720 result = []
1721 if false
1722 for n in [1, 2]
1723 result += [n]
1724 endfor
1725 else
1726 for n in [3, 4]
1727 result += [n]
1728 endfor
1729 endif
1730 assert_equal([3, 4], result)
1731 END
1732 CheckDefAndScriptSuccess(lines)
1733
1734 # test skipped blocks at inside of function
1735 lines =<< trim END
1736 def DefTrue()
1737 var result = []
1738 if true
1739 for n in [1, 2]
1740 result += [n]
1741 endfor
1742 else
1743 for n in [3, 4]
1744 result += [n]
1745 endfor
1746 endif
1747 assert_equal([1, 2], result)
1748 enddef
1749 DefTrue()
1750
1751 def DefFalse()
1752 var result = []
1753 if false
1754 for n in [1, 2]
1755 result += [n]
1756 endfor
1757 else
1758 for n in [3, 4]
1759 result += [n]
1760 endfor
1761 endif
1762 assert_equal([3, 4], result)
1763 enddef
1764 DefFalse()
1765 END
1766 CheckDefAndScriptSuccess(lines)
1767enddef
1768
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02001769def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02001770 var lines =<< trim END
1771 var result = ''
1772 for cnt in range(7)
1773 if cnt == 4
1774 break
1775 endif
1776 if cnt == 2
1777 continue
1778 endif
1779 result ..= cnt .. '_'
1780 endfor
1781 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02001782
Bram Moolenaarf2253962021-04-13 20:53:13 +02001783 var concat = ''
1784 for str in eval('["one", "two"]')
1785 concat ..= str
1786 endfor
1787 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01001788
Bram Moolenaarf2253962021-04-13 20:53:13 +02001789 var total = 0
1790 for nr in
1791 [1, 2, 3]
1792 total += nr
1793 endfor
1794 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01001795
Bram Moolenaarf2253962021-04-13 20:53:13 +02001796 total = 0
1797 for nr
1798 in [1, 2, 3]
1799 total += nr
1800 endfor
1801 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01001802
Bram Moolenaarf2253962021-04-13 20:53:13 +02001803 total = 0
1804 for nr
1805 in
1806 [1, 2, 3]
1807 total += nr
1808 endfor
1809 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01001810
Bram Moolenaara3589a02021-04-14 13:30:46 +02001811 # with type
1812 total = 0
1813 for n: number in [1, 2, 3]
1814 total += n
1815 endfor
1816 assert_equal(6, total)
1817
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02001818 var chars = ''
1819 for s: string in 'foobar'
1820 chars ..= s
1821 endfor
1822 assert_equal('foobar', chars)
1823
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02001824 chars = ''
1825 for x: string in {a: 'a', b: 'b'}->values()
1826 chars ..= x
1827 endfor
1828 assert_equal('ab', chars)
1829
Bram Moolenaara3589a02021-04-14 13:30:46 +02001830 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02001831 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02001832 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
1833 res ..= n .. s
1834 endfor
1835 assert_equal('1a2b', res)
1836
Bram Moolenaar444d8782021-06-26 12:40:56 +02001837 # unpack with one var
1838 var reslist = []
1839 for [x] in [['aaa'], ['bbb']]
1840 reslist->add(x)
1841 endfor
1842 assert_equal(['aaa', 'bbb'], reslist)
1843
Bram Moolenaara3589a02021-04-14 13:30:46 +02001844 # loop over string
1845 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02001846 for c in 'aéc̀d'
1847 res ..= c .. '-'
1848 endfor
1849 assert_equal('a-é-c̀-d-', res)
1850
1851 res = ''
1852 for c in ''
1853 res ..= c .. '-'
1854 endfor
1855 assert_equal('', res)
1856
1857 res = ''
1858 for c in test_null_string()
1859 res ..= c .. '-'
1860 endfor
1861 assert_equal('', res)
1862
1863 var foo: list<dict<any>> = [
1864 {a: 'Cat'}
1865 ]
1866 for dd in foo
1867 dd.counter = 12
1868 endfor
1869 assert_equal([{a: 'Cat', counter: 12}], foo)
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001870
1871 reslist = []
1872 for _ in range(3)
1873 reslist->add('x')
1874 endfor
1875 assert_equal(['x', 'x', 'x'], reslist)
Bram Moolenaarf2253962021-04-13 20:53:13 +02001876 END
1877 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02001878enddef
1879
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001880def Test_for_loop_with_closure()
1881 var lines =<< trim END
1882 var flist: list<func>
1883 for i in range(5)
1884 var inloop = i
1885 flist[i] = () => inloop
1886 endfor
1887 for i in range(5)
1888 assert_equal(4, flist[i]())
1889 endfor
1890 END
1891 CheckDefAndScriptSuccess(lines)
1892
1893 lines =<< trim END
1894 var flist: list<func>
1895 for i in range(5)
1896 var inloop = i
1897 flist[i] = () => {
1898 return inloop
1899 }
1900 endfor
1901 for i in range(5)
1902 assert_equal(4, flist[i]())
1903 endfor
1904 END
1905 CheckDefAndScriptSuccess(lines)
1906enddef
1907
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02001908def Test_for_loop_fails()
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00001909 CheckDefAndScriptFailure(['for '], ['E1097:', 'E690:'])
1910 CheckDefAndScriptFailure(['for x'], ['E1097:', 'E690:'])
1911 CheckDefAndScriptFailure(['for x in'], ['E1097:', 'E15:'])
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001912 CheckDefAndScriptFailure(['for # in range(5)'], 'E690:')
1913 CheckDefAndScriptFailure(['for i In range(5)'], 'E690:')
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00001914 CheckDefAndScriptFailure(['var x = 5', 'for x in range(5)', 'endfor'], ['E1017:', 'E1041:'])
Bram Moolenaard4ab8072021-07-08 19:22:12 +02001915 CheckScriptFailure(['vim9script', 'var x = 5', 'for x in range(5)', '# comment', 'endfor'], 'E1041:', 3)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001916 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01001917 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02001918 CheckDefFailure(['for i in xxx'], 'E1001:')
1919 CheckDefFailure(['endfor'], 'E588:')
1920 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001921
1922 # wrong type detected at compile time
1923 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
1924
1925 # wrong type detected at runtime
1926 g:adict = {a: 1}
1927 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
1928 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001929
1930 var lines =<< trim END
1931 var d: list<dict<any>> = [{a: 0}]
1932 for e in d
1933 e = {a: 0, b: ''}
1934 endfor
1935 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00001936 CheckDefAndScriptFailure(lines, ['E1018:', 'E46:'], 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02001937
1938 lines =<< trim END
1939 for nr: number in ['foo']
1940 endfor
1941 END
1942 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaar404557e2021-07-05 21:41:48 +02001943
1944 lines =<< trim END
1945 for n : number in [1, 2]
1946 echo n
1947 endfor
1948 END
1949 CheckDefAndScriptFailure(lines, 'E1059:', 1)
Bram Moolenaarefc5db52021-07-07 21:21:30 +02001950
1951 lines =<< trim END
1952 var d: dict<number> = {a: 1, b: 2}
1953 for [k: job, v: job] in d->items()
1954 echo k v
1955 endfor
1956 END
1957 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected job but got string', 2)
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001958
1959 lines =<< trim END
1960 var i = 0
1961 for i in [1, 2, 3]
1962 echo i
1963 endfor
1964 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00001965 CheckDefExecAndScriptFailure(lines, ['E1017:', 'E1041:'])
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001966
1967 lines =<< trim END
1968 var l = [0]
1969 for l[0] in [1, 2, 3]
1970 echo l[0]
1971 endfor
1972 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00001973 CheckDefExecAndScriptFailure(lines, ['E461:', 'E1017:'])
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001974
1975 lines =<< trim END
1976 var d = {x: 0}
1977 for d.x in [1, 2, 3]
1978 echo d.x
1979 endfor
1980 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00001981 CheckDefExecAndScriptFailure(lines, ['E461:', 'E1017:'])
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02001982enddef
1983
Bram Moolenaarea870692020-12-02 14:24:30 +01001984def Test_for_loop_script_var()
1985 # cannot use s:var in a :def function
Bram Moolenaara99fb232021-12-20 12:25:03 +00001986 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1254:')
Bram Moolenaarea870692020-12-02 14:24:30 +01001987
1988 # can use s:var in Vim9 script, with or without s:
1989 var lines =<< trim END
1990 vim9script
1991 var total = 0
1992 for s:var in [1, 2, 3]
1993 total += s:var
1994 endfor
1995 assert_equal(6, total)
1996
1997 total = 0
1998 for var in [1, 2, 3]
1999 total += var
2000 endfor
2001 assert_equal(6, total)
2002 END
2003enddef
2004
Bram Moolenaar792f7862020-11-23 08:31:18 +01002005def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01002006 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002007 var result = []
2008 for [v1, v2] in [[1, 2], [3, 4]]
2009 result->add(v1)
2010 result->add(v2)
2011 endfor
2012 assert_equal([1, 2, 3, 4], result)
2013
2014 result = []
2015 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2016 result->add(v1)
2017 result->add(v2)
2018 result->add(v3)
2019 endfor
2020 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
2021
2022 result = []
2023 for [&ts, &sw] in [[1, 2], [3, 4]]
2024 result->add(&ts)
2025 result->add(&sw)
2026 endfor
2027 assert_equal([1, 2, 3, 4], result)
2028
2029 var slist: list<string>
2030 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
2031 slist->add($LOOPVAR)
2032 slist->add(@r)
2033 slist->add(v:errmsg)
2034 endfor
2035 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
2036
2037 slist = []
2038 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
2039 slist->add(g:globalvar)
2040 slist->add(b:bufvar)
2041 slist->add(w:winvar)
2042 slist->add(t:tabvar)
2043 endfor
2044 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01002045 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaarb777da92021-05-22 21:40:39 +02002046
2047 var res = []
2048 for [_, n, _] in [[1, 2, 3], [4, 5, 6]]
2049 res->add(n)
2050 endfor
2051 assert_equal([2, 5], res)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002052 END
2053 CheckDefAndScriptSuccess(lines)
2054
2055 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01002056 for [v1, v2] in [[1, 2, 3], [3, 4]]
2057 echo v1 v2
2058 endfor
2059 END
2060 CheckDefExecFailure(lines, 'E710:', 1)
2061
2062 lines =<< trim END
2063 for [v1, v2] in [[1], [3, 4]]
2064 echo v1 v2
2065 endfor
2066 END
2067 CheckDefExecFailure(lines, 'E711:', 1)
2068
2069 lines =<< trim END
2070 for [v1, v1] in [[1, 2], [3, 4]]
2071 echo v1
2072 endfor
2073 END
2074 CheckDefExecFailure(lines, 'E1017:', 1)
2075enddef
2076
Bram Moolenaarc150c092021-02-13 15:02:46 +01002077def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002078 var lines =<< trim END
2079 var looped = 0
2080 var cleanup = 0
2081 for i in range(3)
2082 looped += 1
2083 try
2084 eval [][0]
2085 catch
2086 continue
2087 finally
2088 cleanup += 1
2089 endtry
2090 endfor
2091 assert_equal(3, looped)
2092 assert_equal(3, cleanup)
2093 END
2094 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002095enddef
2096
rbtnnd895b1d2021-08-20 20:54:25 +02002097def Test_while_skipped_block()
2098 # test skipped blocks at outside of function
2099 var lines =<< trim END
2100 var result = []
2101 var n = 0
2102 if true
2103 n = 1
2104 while n < 3
2105 result += [n]
2106 n += 1
2107 endwhile
2108 else
2109 n = 3
2110 while n < 5
2111 result += [n]
2112 n += 1
2113 endwhile
2114 endif
2115 assert_equal([1, 2], result)
2116
2117 result = []
2118 if false
2119 n = 1
2120 while n < 3
2121 result += [n]
2122 n += 1
2123 endwhile
2124 else
2125 n = 3
2126 while n < 5
2127 result += [n]
2128 n += 1
2129 endwhile
2130 endif
2131 assert_equal([3, 4], result)
2132 END
2133 CheckDefAndScriptSuccess(lines)
2134
2135 # test skipped blocks at inside of function
2136 lines =<< trim END
2137 def DefTrue()
2138 var result = []
2139 var n = 0
2140 if true
2141 n = 1
2142 while n < 3
2143 result += [n]
2144 n += 1
2145 endwhile
2146 else
2147 n = 3
2148 while n < 5
2149 result += [n]
2150 n += 1
2151 endwhile
2152 endif
2153 assert_equal([1, 2], result)
2154 enddef
2155 DefTrue()
2156
2157 def DefFalse()
2158 var result = []
2159 var n = 0
2160 if false
2161 n = 1
2162 while n < 3
2163 result += [n]
2164 n += 1
2165 endwhile
2166 else
2167 n = 3
2168 while n < 5
2169 result += [n]
2170 n += 1
2171 endwhile
2172 endif
2173 assert_equal([3, 4], result)
2174 enddef
2175 DefFalse()
2176 END
2177 CheckDefAndScriptSuccess(lines)
2178enddef
2179
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002180def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002181 var result = ''
2182 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002183 while cnt < 555
2184 if cnt == 3
2185 break
2186 endif
2187 cnt += 1
2188 if cnt == 2
2189 continue
2190 endif
2191 result ..= cnt .. '_'
2192 endwhile
2193 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002194
2195 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002196 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002197 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002198enddef
2199
Bram Moolenaar7a53f292021-11-22 18:31:02 +00002200def Test_while_loop_in_script()
2201 var lines =<< trim END
2202 vim9script
2203 var result = ''
2204 var cnt = 0
2205 while cnt < 3
2206 var s = 'v' .. cnt
2207 result ..= s
2208 cnt += 1
2209 endwhile
2210 assert_equal('v0v1v2', result)
2211 END
2212 CheckScriptSuccess(lines)
2213enddef
2214
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002215def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02002216 CheckDefFailure(['while xxx'], 'E1001:')
2217 CheckDefFailure(['endwhile'], 'E588:')
2218 CheckDefFailure(['continue'], 'E586:')
2219 CheckDefFailure(['if true', 'continue'], 'E586:')
2220 CheckDefFailure(['break'], 'E587:')
2221 CheckDefFailure(['if true', 'break'], 'E587:')
2222 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002223
2224 var lines =<< trim END
2225 var s = ''
2226 while s = ''
2227 endwhile
2228 END
2229 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002230enddef
2231
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002232def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002233 var caught = false
2234 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002235 try
2236 while 1
2237 x += 1
2238 if x == 100
2239 feedkeys("\<C-C>", 'Lt')
2240 endif
2241 endwhile
2242 catch
2243 caught = true
2244 assert_equal(100, x)
2245 endtry
2246 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02002247 # consume the CTRL-C
2248 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002249enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01002250
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002251def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002252 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002253 'one',
2254 'two',
2255 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002256 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002257 assert_equal(['one', 'two', 'three'], mylist)
2258
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002259 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01002260 ['one']: 1,
2261 ['two']: 2,
2262 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002263 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002264 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01002265 assert_equal({one: 1, two: 2, three: 3}, mydict)
2266 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002267 one: 1, # comment
2268 two: # comment
2269 2, # comment
2270 three: 3 # comment
2271 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002272 assert_equal({one: 1, two: 2, three: 3}, mydict)
2273 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002274 one: 1,
2275 two:
2276 2,
2277 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002278 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002279 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002280
2281 assert_equal(
2282 ['one', 'two', 'three'],
2283 split('one two three')
2284 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002285enddef
2286
Bram Moolenaar7a092242020-04-16 22:10:49 +02002287def Test_vim9_comment()
2288 CheckScriptSuccess([
2289 'vim9script',
2290 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002291 '#something',
2292 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002293 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002294
2295 split Xfile
2296 CheckScriptSuccess([
2297 'vim9script',
2298 'edit #something',
2299 ])
2300 CheckScriptSuccess([
2301 'vim9script',
2302 'edit #{something',
2303 ])
2304 close
2305
Bram Moolenaar7a092242020-04-16 22:10:49 +02002306 CheckScriptFailure([
2307 'vim9script',
2308 ':# something',
2309 ], 'E488:')
2310 CheckScriptFailure([
2311 '# something',
2312 ], 'E488:')
2313 CheckScriptFailure([
2314 ':# something',
2315 ], 'E488:')
2316
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002317 { # block start
2318 } # block end
2319 CheckDefFailure([
2320 '{# comment',
2321 ], 'E488:')
2322 CheckDefFailure([
2323 '{',
2324 '}# comment',
2325 ], 'E488:')
2326
2327 echo "yes" # comment
2328 CheckDefFailure([
2329 'echo "yes"# comment',
2330 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002331 CheckScriptSuccess([
2332 'vim9script',
2333 'echo "yes" # something',
2334 ])
2335 CheckScriptFailure([
2336 'vim9script',
2337 'echo "yes"# something',
2338 ], 'E121:')
2339 CheckScriptFailure([
2340 'vim9script',
2341 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002342 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002343 CheckScriptFailure([
2344 'echo "yes" # something',
2345 ], 'E121:')
2346
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002347 exe "echo" # comment
2348 CheckDefFailure([
2349 'exe "echo"# comment',
2350 ], 'E488:')
2351 CheckScriptSuccess([
2352 'vim9script',
2353 'exe "echo" # something',
2354 ])
2355 CheckScriptFailure([
2356 'vim9script',
2357 'exe "echo"# something',
2358 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002359 CheckScriptFailure([
2360 'vim9script',
2361 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002362 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002363 CheckScriptFailure([
2364 'exe "echo" # something',
2365 ], 'E121:')
2366
Bram Moolenaar7a092242020-04-16 22:10:49 +02002367 CheckDefFailure([
2368 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002369 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002370 'catch',
2371 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002372 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002373 CheckScriptFailure([
2374 'vim9script',
2375 'try# comment',
2376 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002377 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002378 CheckDefFailure([
2379 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002380 ' throw#comment',
2381 'catch',
2382 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002383 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002384 CheckDefFailure([
2385 'try',
2386 ' throw "yes"#comment',
2387 'catch',
2388 'endtry',
2389 ], 'E488:')
2390 CheckDefFailure([
2391 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002392 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002393 'catch# comment',
2394 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002395 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002396 CheckScriptFailure([
2397 'vim9script',
2398 'try',
2399 ' echo "yes"',
2400 'catch# comment',
2401 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002402 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002403 CheckDefFailure([
2404 'try',
2405 ' echo "yes"',
2406 'catch /pat/# comment',
2407 'endtry',
2408 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002409 CheckDefFailure([
2410 'try',
2411 'echo "yes"',
2412 'catch',
2413 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002414 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002415 CheckScriptFailure([
2416 'vim9script',
2417 'try',
2418 ' echo "yes"',
2419 'catch',
2420 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002421 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002422
2423 CheckScriptSuccess([
2424 'vim9script',
2425 'hi # comment',
2426 ])
2427 CheckScriptFailure([
2428 'vim9script',
2429 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002430 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002431 CheckScriptSuccess([
2432 'vim9script',
2433 'hi Search # comment',
2434 ])
2435 CheckScriptFailure([
2436 'vim9script',
2437 'hi Search# comment',
2438 ], 'E416:')
2439 CheckScriptSuccess([
2440 'vim9script',
2441 'hi link This Search # comment',
2442 ])
2443 CheckScriptFailure([
2444 'vim9script',
2445 'hi link This That# comment',
2446 ], 'E413:')
2447 CheckScriptSuccess([
2448 'vim9script',
2449 'hi clear This # comment',
2450 'hi clear # comment',
2451 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002452 # not tested, because it doesn't give an error but a warning:
2453 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02002454 CheckScriptFailure([
2455 'vim9script',
2456 'hi clear# comment',
2457 ], 'E416:')
2458
2459 CheckScriptSuccess([
2460 'vim9script',
2461 'hi Group term=bold',
2462 'match Group /todo/ # comment',
2463 ])
2464 CheckScriptFailure([
2465 'vim9script',
2466 'hi Group term=bold',
2467 'match Group /todo/# comment',
2468 ], 'E488:')
2469 CheckScriptSuccess([
2470 'vim9script',
2471 'match # comment',
2472 ])
2473 CheckScriptFailure([
2474 'vim9script',
2475 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002476 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002477 CheckScriptSuccess([
2478 'vim9script',
2479 'match none # comment',
2480 ])
2481 CheckScriptFailure([
2482 'vim9script',
2483 'match none# comment',
2484 ], 'E475:')
2485
2486 CheckScriptSuccess([
2487 'vim9script',
2488 'menutrans clear # comment',
2489 ])
2490 CheckScriptFailure([
2491 'vim9script',
2492 'menutrans clear# comment text',
2493 ], 'E474:')
2494
2495 CheckScriptSuccess([
2496 'vim9script',
2497 'syntax clear # comment',
2498 ])
2499 CheckScriptFailure([
2500 'vim9script',
2501 'syntax clear# comment text',
2502 ], 'E28:')
2503 CheckScriptSuccess([
2504 'vim9script',
2505 'syntax keyword Word some',
2506 'syntax clear Word # comment',
2507 ])
2508 CheckScriptFailure([
2509 'vim9script',
2510 'syntax keyword Word some',
2511 'syntax clear Word# comment text',
2512 ], 'E28:')
2513
2514 CheckScriptSuccess([
2515 'vim9script',
2516 'syntax list # comment',
2517 ])
2518 CheckScriptFailure([
2519 'vim9script',
2520 'syntax list# comment text',
2521 ], 'E28:')
2522
2523 CheckScriptSuccess([
2524 'vim9script',
2525 'syntax match Word /pat/ oneline # comment',
2526 ])
2527 CheckScriptFailure([
2528 'vim9script',
2529 'syntax match Word /pat/ oneline# comment',
2530 ], 'E475:')
2531
2532 CheckScriptSuccess([
2533 'vim9script',
2534 'syntax keyword Word word # comm[ent',
2535 ])
2536 CheckScriptFailure([
2537 'vim9script',
2538 'syntax keyword Word word# comm[ent',
2539 ], 'E789:')
2540
2541 CheckScriptSuccess([
2542 'vim9script',
2543 'syntax match Word /pat/ # comment',
2544 ])
2545 CheckScriptFailure([
2546 'vim9script',
2547 'syntax match Word /pat/# comment',
2548 ], 'E402:')
2549
2550 CheckScriptSuccess([
2551 'vim9script',
2552 'syntax match Word /pat/ contains=Something # comment',
2553 ])
2554 CheckScriptFailure([
2555 'vim9script',
2556 'syntax match Word /pat/ contains=Something# comment',
2557 ], 'E475:')
2558 CheckScriptFailure([
2559 'vim9script',
2560 'syntax match Word /pat/ contains= # comment',
2561 ], 'E406:')
2562 CheckScriptFailure([
2563 'vim9script',
2564 'syntax match Word /pat/ contains=# comment',
2565 ], 'E475:')
2566
2567 CheckScriptSuccess([
2568 'vim9script',
2569 'syntax region Word start=/pat/ end=/pat/ # comment',
2570 ])
2571 CheckScriptFailure([
2572 'vim9script',
2573 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02002574 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002575
2576 CheckScriptSuccess([
2577 'vim9script',
2578 'syntax sync # comment',
2579 ])
2580 CheckScriptFailure([
2581 'vim9script',
2582 'syntax sync# comment',
2583 ], 'E404:')
2584 CheckScriptSuccess([
2585 'vim9script',
2586 'syntax sync ccomment # comment',
2587 ])
2588 CheckScriptFailure([
2589 'vim9script',
2590 'syntax sync ccomment# comment',
2591 ], 'E404:')
2592
2593 CheckScriptSuccess([
2594 'vim9script',
2595 'syntax cluster Some contains=Word # comment',
2596 ])
2597 CheckScriptFailure([
2598 'vim9script',
2599 'syntax cluster Some contains=Word# comment',
2600 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002601
2602 CheckScriptSuccess([
2603 'vim9script',
2604 'command Echo echo # comment',
2605 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002606 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002607 ])
2608 CheckScriptFailure([
2609 'vim9script',
2610 'command Echo echo# comment',
2611 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002612 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002613 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01002614
2615 var curdir = getcwd()
2616 CheckScriptSuccess([
2617 'command Echo cd " comment',
2618 'Echo',
2619 'delcommand Echo',
2620 ])
2621 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01002622 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01002623 'command Echo cd # comment',
2624 'Echo',
2625 'delcommand Echo',
2626 ])
2627 CheckScriptFailure([
2628 'vim9script',
2629 'command Echo cd " comment',
2630 'Echo',
2631 ], 'E344:')
2632 delcommand Echo
2633 chdir(curdir)
2634
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002635 CheckScriptFailure([
2636 'vim9script',
2637 'command Echo# comment',
2638 ], 'E182:')
2639 CheckScriptFailure([
2640 'vim9script',
2641 'command Echo echo',
2642 'command Echo# comment',
2643 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002644 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002645
2646 CheckScriptSuccess([
2647 'vim9script',
2648 'function # comment',
2649 ])
2650 CheckScriptFailure([
2651 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02002652 'function " comment',
2653 ], 'E129:')
2654 CheckScriptFailure([
2655 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002656 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002657 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002658 CheckScriptSuccess([
2659 'vim9script',
2660 'function CheckScriptSuccess # comment',
2661 ])
2662 CheckScriptFailure([
2663 'vim9script',
2664 'function CheckScriptSuccess# comment',
2665 ], 'E488:')
2666
2667 CheckScriptSuccess([
2668 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002669 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002670 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002671 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002672 ])
2673 CheckScriptFailure([
2674 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002675 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002676 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002677 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002678 ], 'E488:')
2679
2680 CheckScriptSuccess([
2681 'vim9script',
2682 'call execute("ls") # comment',
2683 ])
2684 CheckScriptFailure([
2685 'vim9script',
2686 'call execute("ls")# comment',
2687 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02002688
2689 CheckScriptFailure([
2690 'def Test() " comment',
2691 'enddef',
2692 ], 'E488:')
2693 CheckScriptFailure([
2694 'vim9script',
2695 'def Test() " comment',
2696 'enddef',
2697 ], 'E488:')
2698
2699 CheckScriptSuccess([
2700 'func Test() " comment',
2701 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002702 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02002703 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02002704 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02002705 'vim9script',
2706 'func Test() " comment',
2707 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02002708 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02002709
2710 CheckScriptSuccess([
2711 'def Test() # comment',
2712 'enddef',
2713 ])
2714 CheckScriptFailure([
2715 'func Test() # comment',
2716 'endfunc',
2717 ], 'E488:')
Bram Moolenaar0f37e352021-06-02 15:28:15 +02002718
2719 var lines =<< trim END
2720 vim9script
2721 syn region Text
2722 \ start='foo'
2723 #\ comment
2724 \ end='bar'
Bram Moolenaar5072b472021-06-03 21:56:10 +02002725 syn region Text start='foo'
2726 #\ comment
2727 \ end='bar'
Bram Moolenaar0f37e352021-06-02 15:28:15 +02002728 END
2729 CheckScriptSuccess(lines)
2730
2731 lines =<< trim END
2732 vim9script
2733 syn region Text
2734 \ start='foo'
2735 "\ comment
2736 \ end='bar'
2737 END
2738 CheckScriptFailure(lines, 'E399:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002739enddef
2740
2741def Test_vim9_comment_gui()
2742 CheckCanRunGui
2743
2744 CheckScriptFailure([
2745 'vim9script',
2746 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002747 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002748 CheckScriptFailure([
2749 'vim9script',
2750 'gui -f#comment'
Bram Moolenaarb8bd2e62021-08-21 17:13:14 +02002751 ], 'E194:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002752enddef
2753
Bram Moolenaara26b9702020-04-18 19:53:28 +02002754def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02002755 au TabEnter *.vim g:entered = 1
2756 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02002757
2758 edit test.vim
2759 doautocmd TabEnter #comment
2760 assert_equal(1, g:entered)
2761
2762 doautocmd TabEnter f.x
2763 assert_equal(2, g:entered)
2764
2765 g:entered = 0
2766 doautocmd TabEnter f.x #comment
2767 assert_equal(2, g:entered)
2768
2769 assert_fails('doautocmd Syntax#comment', 'E216:')
2770
2771 au! TabEnter
2772 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002773
2774 CheckScriptSuccess([
2775 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02002776 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002777 'b:var = 456',
2778 'w:var = 777',
2779 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002780 'unlet g:var w:var # something',
2781 ])
2782
2783 CheckScriptFailure([
2784 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002785 'let var = 123',
2786 ], 'E1126: Cannot use :let in Vim9 script')
2787
2788 CheckScriptFailure([
2789 'vim9script',
2790 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002791 ], 'E1016: Cannot declare a global variable:')
2792
2793 CheckScriptFailure([
2794 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002795 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002796 ], 'E1016: Cannot declare a buffer variable:')
2797
2798 CheckScriptFailure([
2799 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002800 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002801 ], 'E1016: Cannot declare a window variable:')
2802
2803 CheckScriptFailure([
2804 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002805 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002806 ], 'E1016: Cannot declare a tab variable:')
2807
2808 CheckScriptFailure([
2809 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002810 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002811 ], 'E1016: Cannot declare a v: variable:')
2812
2813 CheckScriptFailure([
2814 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002815 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002816 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02002817
2818 CheckScriptFailure([
2819 'vim9script',
2820 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02002821 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002822 ], 'E108:')
2823
2824 CheckScriptFailure([
2825 'let g:var = 123',
2826 'unlet g:var # something',
2827 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002828
2829 CheckScriptSuccess([
2830 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02002831 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002832 ' echo "yes"',
2833 'elseif 2 #comment',
2834 ' echo "no"',
2835 'endif',
2836 ])
2837
2838 CheckScriptFailure([
2839 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02002840 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002841 ' echo "yes"',
2842 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002843 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002844
2845 CheckScriptFailure([
2846 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02002847 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002848 ' echo "yes"',
2849 'elseif 2#comment',
2850 ' echo "no"',
2851 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002852 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002853
2854 CheckScriptSuccess([
2855 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002856 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002857 ])
2858
2859 CheckScriptFailure([
2860 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002861 'var v = 1# comment6',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002862 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002863
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002864 CheckScriptSuccess([
2865 'vim9script',
2866 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02002867 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002868 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002869 'dsearch /pat/ #comment',
2870 'bwipe!',
2871 ])
2872
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002873 CheckScriptFailure([
2874 'vim9script',
2875 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02002876 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002877 ':$',
2878 'dsearch /pat/#comment',
2879 'bwipe!',
2880 ], 'E488:')
2881
2882 CheckScriptFailure([
2883 'vim9script',
2884 'func! SomeFunc()',
2885 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02002886enddef
2887
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02002888def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002889 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02002890 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02002891 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02002892 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02002893 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02002894 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02002895 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02002896 END
2897 writefile(lines, 'Xfinished')
2898 source Xfinished
2899 assert_equal('two', g:res)
2900
2901 unlet g:res
2902 delete('Xfinished')
2903enddef
2904
Bram Moolenaara5d00772020-05-14 23:20:55 +02002905def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002906 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02002907 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02002908 def GetValue(): string
2909 return theVal
2910 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002911 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02002912 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02002913 theVal = 'else'
2914 g:laterVal = GetValue()
2915 END
2916 writefile(lines, 'Xforward')
2917 source Xforward
2918 assert_equal('something', g:initVal)
2919 assert_equal('else', g:laterVal)
2920
2921 unlet g:initVal
2922 unlet g:laterVal
2923 delete('Xforward')
2924enddef
2925
Bram Moolenaare535db82021-03-31 21:07:24 +02002926def Test_declare_script_in_func()
2927 var lines =<< trim END
2928 vim9script
2929 func Declare()
2930 let s:local = 123
2931 endfunc
2932 Declare()
2933 assert_equal(123, local)
2934
2935 var error: string
2936 try
2937 local = 'asdf'
2938 catch
2939 error = v:exception
2940 endtry
2941 assert_match('E1012: Type mismatch; expected number but got string', error)
2942
2943 lockvar local
2944 try
2945 local = 999
2946 catch
2947 error = v:exception
2948 endtry
2949 assert_match('E741: Value is locked: local', error)
2950 END
2951 CheckScriptSuccess(lines)
2952enddef
2953
2954
Bram Moolenaar7d699702020-08-14 20:52:28 +02002955func Test_vim9script_not_global()
2956 " check that items defined in Vim9 script are script-local, not global
2957 let vim9lines =<< trim END
2958 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002959 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02002960 func TheFunc()
2961 echo 'local'
2962 endfunc
2963 def DefFunc()
2964 echo 'local'
2965 enddef
2966 END
2967 call writefile(vim9lines, 'Xvim9script.vim')
2968 source Xvim9script.vim
2969 try
2970 echo g:var
2971 assert_report('did not fail')
2972 catch /E121:/
2973 " caught
2974 endtry
2975 try
2976 call TheFunc()
2977 assert_report('did not fail')
2978 catch /E117:/
2979 " caught
2980 endtry
2981 try
2982 call DefFunc()
2983 assert_report('did not fail')
2984 catch /E117:/
2985 " caught
2986 endtry
2987
Bram Moolenaar25859dd2020-08-30 12:54:53 +02002988 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02002989endfunc
2990
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02002991def Test_vim9_copen()
2992 # this was giving an error for setting w:quickfix_title
2993 copen
2994 quit
2995enddef
2996
Bram Moolenaar03290b82020-12-19 16:30:44 +01002997" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002998def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002999 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003000 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01003001 def some#gettest(): string
3002 return 'test'
3003 enddef
3004 g:some#name = 'name'
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003005 g:some#dict = {key: 'value'}
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003006
3007 def some#varargs(a1: string, ...l: list<string>): string
3008 return a1 .. l[0] .. l[1]
3009 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01003010 END
3011
3012 mkdir('Xdir/autoload', 'p')
3013 writefile(lines, 'Xdir/autoload/some.vim')
3014 var save_rtp = &rtp
3015 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3016
3017 assert_equal('test', g:some#gettest())
3018 assert_equal('name', g:some#name)
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003019 assert_equal('value', g:some#dict.key)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003020 g:some#other = 'other'
3021 assert_equal('other', g:some#other)
3022
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003023 assert_equal('abc', some#varargs('a', 'b', 'c'))
3024
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003025 # upper case script name works
3026 lines =<< trim END
3027 vim9script
3028 def Other#getOther(): string
3029 return 'other'
3030 enddef
3031 END
3032 writefile(lines, 'Xdir/autoload/Other.vim')
3033 assert_equal('other', g:Other#getOther())
3034
Bram Moolenaar03290b82020-12-19 16:30:44 +01003035 delete('Xdir', 'rf')
3036 &rtp = save_rtp
3037enddef
3038
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00003039" test disassembling an auto-loaded function starting with "debug"
3040def Test_vim9_autoload_disass()
3041 mkdir('Xdir/autoload', 'p')
3042 var save_rtp = &rtp
3043 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3044
3045 var lines =<< trim END
3046 vim9script
3047 def debugit#test(): string
3048 return 'debug'
3049 enddef
3050 END
3051 writefile(lines, 'Xdir/autoload/debugit.vim')
3052
3053 lines =<< trim END
3054 vim9script
3055 def profileit#test(): string
3056 return 'profile'
3057 enddef
3058 END
3059 writefile(lines, 'Xdir/autoload/profileit.vim')
3060
3061 lines =<< trim END
3062 vim9script
3063 assert_equal('debug', debugit#test())
3064 disass debugit#test
3065 assert_equal('profile', profileit#test())
3066 disass profileit#test
3067 END
3068 CheckScriptSuccess(lines)
3069
3070 delete('Xdir', 'rf')
3071 &rtp = save_rtp
3072enddef
3073
Bram Moolenaar03290b82020-12-19 16:30:44 +01003074" test using a vim9script that is auto-loaded from an autocmd
3075def Test_vim9_aucmd_autoload()
3076 var lines =<< trim END
3077 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003078 def foo#test()
3079 echomsg getreg('"')
3080 enddef
3081 END
3082
3083 mkdir('Xdir/autoload', 'p')
3084 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003085 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003086 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3087 augroup test
3088 autocmd TextYankPost * call foo#test()
3089 augroup END
3090
3091 normal Y
3092
3093 augroup test
3094 autocmd!
3095 augroup END
3096 delete('Xdir', 'rf')
3097 &rtp = save_rtp
3098enddef
3099
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003100" This was causing a crash because suppress_errthrow wasn't reset.
3101def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003102 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003103 vim9script
3104 def crash#func()
3105 try
3106 for x in List()
3107 endfor
3108 catch
3109 endtry
3110 g:ok = true
3111 enddef
3112 fu List()
3113 invalid
3114 endfu
3115 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01003116 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003117 catch /wontmatch/
3118 endtry
3119 END
3120 call mkdir('Xruntime/autoload', 'p')
3121 call writefile(lines, 'Xruntime/autoload/crash.vim')
3122
3123 # run in a separate Vim to avoid the side effects of assert_fails()
3124 lines =<< trim END
3125 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
3126 call crash#func()
3127 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003128 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003129 END
3130 writefile(lines, 'Xscript')
3131 RunVim([], [], '-S Xscript')
3132 assert_equal(['ok'], readfile('Xdidit'))
3133
3134 delete('Xdidit')
3135 delete('Xscript')
3136 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01003137
3138 lines =<< trim END
3139 vim9script
3140 var foo#bar = 'asdf'
3141 END
3142 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003143enddef
3144
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003145def Test_script_var_in_autocmd()
3146 # using a script variable from an autocommand, defined in a :def function in a
3147 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003148 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003149 let s:counter = 1
3150 def s:Func()
3151 au! CursorHold
3152 au CursorHold * s:counter += 1
3153 enddef
3154 call s:Func()
3155 doau CursorHold
3156 call assert_equal(2, s:counter)
3157 au! CursorHold
3158 END
3159 CheckScriptSuccess(lines)
3160enddef
3161
Bram Moolenaarb5841b92021-07-15 18:09:53 +02003162def Test_error_in_autoload_script()
3163 var save_rtp = &rtp
3164 var dir = getcwd() .. '/Xruntime'
3165 &rtp = dir
3166 mkdir(dir .. '/autoload', 'p')
3167
3168 var lines =<< trim END
3169 vim9script noclear
3170 def script#autoloaded()
3171 enddef
3172 def Broken()
3173 var x: any = ''
3174 eval x != 0
3175 enddef
3176 Broken()
3177 END
3178 writefile(lines, dir .. '/autoload/script.vim')
3179
3180 lines =<< trim END
3181 vim9script
3182 def CallAutoloaded()
3183 script#autoloaded()
3184 enddef
3185
3186 function Legacy()
3187 try
3188 call s:CallAutoloaded()
3189 catch
3190 call assert_match('E1030: Using a String as a Number', v:exception)
3191 endtry
3192 endfunction
3193
3194 Legacy()
3195 END
3196 CheckScriptSuccess(lines)
3197
3198 &rtp = save_rtp
3199 delete(dir, 'rf')
3200enddef
3201
Bram Moolenaare3d46852020-08-29 13:39:17 +02003202def Test_invalid_sid()
3203 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003204
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003205 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02003206 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02003207 endif
3208 delete('Xdidit')
3209enddef
3210
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003211def Test_restoring_cpo()
3212 writefile(['vim9script', 'set nocp'], 'Xsourced')
3213 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
3214 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
3215 assert_equal(['done'], readfile('Xdone'))
3216 endif
3217 delete('Xsourced')
3218 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01003219 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01003220
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00003221 writefile(['vim9script', 'g:cpoval = &cpo'], 'XanotherScript')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01003222 set cpo=aABceFsMny>
3223 edit XanotherScript
3224 so %
3225 assert_equal('aABceFsMny>', &cpo)
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00003226 assert_equal('aABceFs', g:cpoval)
Bram Moolenaar0123cc12021-02-07 17:17:58 +01003227 :1del
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00003228 setline(1, 'let g:cpoval = &cpo')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01003229 w
3230 so %
3231 assert_equal('aABceFsMny>', &cpo)
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00003232 assert_equal('aABceFsMny>', g:cpoval)
Bram Moolenaar0123cc12021-02-07 17:17:58 +01003233
3234 delete('XanotherScript')
3235 set cpo&vim
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00003236 unlet g:cpoval
3237
3238 if has('unix')
3239 # 'cpo' is not restored in main vimrc
3240 var save_HOME = $HOME
3241 $HOME = getcwd() .. '/Xhome'
3242 mkdir('Xhome')
3243 var lines =<< trim END
3244 vim9script
3245 writefile(['before: ' .. &cpo], 'Xresult')
3246 set cpo+=M
3247 writefile(['after: ' .. &cpo], 'Xresult', 'a')
3248 END
3249 writefile(lines, 'Xhome/.vimrc')
3250
3251 lines =<< trim END
3252 call writefile(['later: ' .. &cpo], 'Xresult', 'a')
3253 END
3254 writefile(lines, 'Xlegacy')
3255
3256 lines =<< trim END
3257 vim9script
3258 call writefile(['vim9: ' .. &cpo], 'Xresult', 'a')
3259 qa
3260 END
3261 writefile(lines, 'Xvim9')
3262
3263 var cmd = GetVimCommand() .. " -S Xlegacy -S Xvim9"
3264 cmd = substitute(cmd, '-u NONE', '', '')
3265 exe "silent !" .. cmd
3266
3267 assert_equal([
3268 'before: aABceFs',
3269 'after: aABceFsM',
3270 'later: aABceFsM',
3271 'vim9: aABceFs'], readfile('Xresult'))
3272
3273 $HOME = save_HOME
3274 delete('Xhome', 'rf')
3275 delete('Xlegacy')
3276 delete('Xvim9')
3277 delete('Xresult')
3278 endif
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003279enddef
3280
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003281" Use :function so we can use Check commands
3282func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003283 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003284 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003285
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003286 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003287 vim9script
3288 def script#func()
3289 enddef
3290 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003291 call mkdir('Xdir/autoload', 'p')
3292 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003293
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003294 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003295 vim9script
3296 set cpo+=M
3297 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02003298 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003299 setline(1, 'some text')
3300 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003301 call writefile(lines, 'XTest_redraw_cpo')
3302 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
3303 call term_sendkeys(buf, "V:")
3304 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003305
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003306 " clean up
3307 call term_sendkeys(buf, "\<Esc>u")
3308 call StopVimInTerminal(buf)
3309 call delete('XTest_redraw_cpo')
3310 call delete('Xdir', 'rf')
3311endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003312
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003313
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003314def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003315 var lines =<< trim END
3316 var name: any
3317 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003318 END
3319 CheckDefAndScriptSuccess(lines)
3320enddef
3321
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003322func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02003323 CheckRunVimInTerminal
3324
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003325 " call indirectly to avoid compilation error for missing functions
3326 call Run_Test_define_func_at_command_line()
3327endfunc
3328
3329def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003330 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003331 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003332 func CheckAndQuit()
3333 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
3334 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
3335 endfunc
3336 END
3337 writefile([''], 'Xdidcmd')
3338 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01003339 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003340 # define Afunc() on the command line
3341 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
3342 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003343 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003344
3345 call StopVimInTerminal(buf)
3346 delete('XcallFunc')
3347 delete('Xdidcmd')
3348enddef
3349
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003350def Test_script_var_scope()
3351 var lines =<< trim END
3352 vim9script
3353 if true
3354 if true
3355 var one = 'one'
3356 echo one
3357 endif
3358 echo one
3359 endif
3360 END
3361 CheckScriptFailure(lines, 'E121:', 7)
3362
3363 lines =<< trim END
3364 vim9script
3365 if true
3366 if false
3367 var one = 'one'
3368 echo one
3369 else
3370 var one = 'one'
3371 echo one
3372 endif
3373 echo one
3374 endif
3375 END
3376 CheckScriptFailure(lines, 'E121:', 10)
3377
3378 lines =<< trim END
3379 vim9script
3380 while true
3381 var one = 'one'
3382 echo one
3383 break
3384 endwhile
3385 echo one
3386 END
3387 CheckScriptFailure(lines, 'E121:', 7)
3388
3389 lines =<< trim END
3390 vim9script
3391 for i in range(1)
3392 var one = 'one'
3393 echo one
3394 endfor
3395 echo one
3396 END
3397 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003398
3399 lines =<< trim END
3400 vim9script
3401 {
3402 var one = 'one'
3403 assert_equal('one', one)
3404 }
3405 assert_false(exists('one'))
3406 assert_false(exists('s:one'))
3407 END
3408 CheckScriptSuccess(lines)
3409
3410 lines =<< trim END
3411 vim9script
3412 {
3413 var one = 'one'
3414 echo one
3415 }
3416 echo one
3417 END
3418 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003419enddef
3420
Bram Moolenaar352134b2020-10-17 22:04:08 +02003421def Test_catch_exception_in_callback()
3422 var lines =<< trim END
3423 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02003424 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02003425 try
3426 var x: string
3427 var y: string
3428 # this error should be caught with CHECKLEN
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00003429 var sl = ['']
3430 [x, y] = sl
Bram Moolenaar352134b2020-10-17 22:04:08 +02003431 catch
3432 g:caught = 'yes'
3433 endtry
3434 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01003435 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02003436 feedkeys("\r", 'xt')
3437 END
3438 CheckScriptSuccess(lines)
3439
3440 unlet g:caught
3441enddef
3442
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003443def Test_no_unknown_error_after_error()
3444 if !has('unix') || !has('job')
3445 throw 'Skipped: not unix of missing +job feature'
3446 endif
Bram Moolenaarb16ff292021-09-26 20:14:39 +01003447 # FIXME: this check should not be needed
3448 if has('win32')
3449 throw 'Skipped: does not work on MS-Windows'
3450 endif
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003451 var lines =<< trim END
3452 vim9script
3453 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02003454 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003455 eval [][0]
3456 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02003457 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003458 sleep 1m
3459 source += l
3460 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01003461 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01003462 while job_status(myjob) == 'run'
3463 sleep 10m
3464 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01003465 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02003466 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003467 END
3468 writefile(lines, 'Xdef')
3469 assert_fails('so Xdef', ['E684:', 'E1012:'])
3470 delete('Xdef')
3471enddef
3472
Bram Moolenaar4324d872020-12-01 20:12:24 +01003473def InvokeNormal()
3474 exe "norm! :m+1\r"
3475enddef
3476
3477def Test_invoke_normal_in_visual_mode()
3478 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
3479 new
3480 setline(1, ['aaa', 'bbb'])
3481 feedkeys("V\<F3>", 'xt')
3482 assert_equal(['bbb', 'aaa'], getline(1, 2))
3483 xunmap <F3>
3484enddef
3485
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003486def Test_white_space_after_command()
3487 var lines =<< trim END
3488 exit_cb: Func})
3489 END
3490 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003491
3492 lines =<< trim END
3493 e#
3494 END
3495 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003496enddef
3497
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003498def Test_script_var_gone_when_sourced_twice()
3499 var lines =<< trim END
3500 vim9script
3501 if exists('g:guard')
3502 finish
3503 endif
3504 g:guard = 1
3505 var name = 'thename'
3506 def g:GetName(): string
3507 return name
3508 enddef
3509 def g:SetName(arg: string)
3510 name = arg
3511 enddef
3512 END
3513 writefile(lines, 'XscriptTwice.vim')
3514 so XscriptTwice.vim
3515 assert_equal('thename', g:GetName())
3516 g:SetName('newname')
3517 assert_equal('newname', g:GetName())
3518 so XscriptTwice.vim
3519 assert_fails('call g:GetName()', 'E1149:')
3520 assert_fails('call g:SetName("x")', 'E1149:')
3521
3522 delfunc g:GetName
3523 delfunc g:SetName
3524 delete('XscriptTwice.vim')
3525 unlet g:guard
3526enddef
3527
Bram Moolenaar10b94212021-02-19 21:42:57 +01003528def Test_unsupported_commands()
3529 var lines =<< trim END
3530 ka
3531 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003532 CheckDefFailure(lines, 'E476:')
3533 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaar10b94212021-02-19 21:42:57 +01003534
3535 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01003536 :1ka
3537 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003538 CheckDefFailure(lines, 'E476:')
3539 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaarada1d872021-02-20 08:16:51 +01003540
3541 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01003542 t
3543 END
3544 CheckDefFailure(lines, 'E1100:')
3545 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
3546
3547 lines =<< trim END
3548 x
3549 END
3550 CheckDefFailure(lines, 'E1100:')
3551 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
3552
3553 lines =<< trim END
3554 xit
3555 END
3556 CheckDefFailure(lines, 'E1100:')
3557 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
3558enddef
3559
Bram Moolenaarc70fe462021-04-17 17:59:19 +02003560def Test_mapping_line_number()
3561 var lines =<< trim END
3562 vim9script
3563 def g:FuncA()
3564 # Some comment
3565 FuncB(0)
3566 enddef
3567 # Some comment
3568 def FuncB(
3569 # Some comment
3570 n: number
3571 )
3572 exe 'nno '
3573 # Some comment
3574 .. '<F3> a'
3575 .. 'b'
3576 .. 'c'
3577 enddef
3578 END
3579 CheckScriptSuccess(lines)
3580 var res = execute('verbose nmap <F3>')
3581 assert_match('No mapping found', res)
3582
3583 g:FuncA()
3584 res = execute('verbose nmap <F3>')
3585 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
3586
3587 nunmap <F3>
3588 delfunc g:FuncA
3589enddef
3590
Bram Moolenaardeb108b2021-07-08 17:35:36 +02003591def Test_option_set()
3592 # legacy script allows for white space
3593 var lines =<< trim END
3594 set foldlevel =11
3595 call assert_equal(11, &foldlevel)
3596 END
3597 CheckScriptSuccess(lines)
3598
3599 set foldlevel
3600 set foldlevel=12
3601 assert_equal(12, &foldlevel)
3602 set foldlevel+=2
3603 assert_equal(14, &foldlevel)
3604 set foldlevel-=3
3605 assert_equal(11, &foldlevel)
3606
3607 lines =<< trim END
3608 set foldlevel =1
3609 END
3610 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: =1')
3611
3612 lines =<< trim END
3613 set foldlevel +=1
3614 END
3615 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: +=1')
3616
3617 lines =<< trim END
3618 set foldlevel ^=1
3619 END
3620 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: ^=1')
3621
3622 lines =<< trim END
3623 set foldlevel -=1
3624 END
3625 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: -=1')
3626
3627 set foldlevel&
3628enddef
3629
Bram Moolenaar208f0b42021-06-20 12:40:08 +02003630def Test_option_modifier()
Bram Moolenaar1594f312021-07-08 16:40:13 +02003631 # legacy script allows for white space
Bram Moolenaar208f0b42021-06-20 12:40:08 +02003632 var lines =<< trim END
3633 set hlsearch & hlsearch !
3634 call assert_equal(1, &hlsearch)
3635 END
3636 CheckScriptSuccess(lines)
3637
Bram Moolenaar1594f312021-07-08 16:40:13 +02003638 set hlsearch
3639 set hlsearch!
3640 assert_equal(false, &hlsearch)
3641
3642 set hlsearch
3643 set hlsearch&
3644 assert_equal(false, &hlsearch)
Bram Moolenaar208f0b42021-06-20 12:40:08 +02003645
3646 lines =<< trim END
Bram Moolenaar1594f312021-07-08 16:40:13 +02003647 set hlsearch &
Bram Moolenaar208f0b42021-06-20 12:40:08 +02003648 END
Bram Moolenaar1594f312021-07-08 16:40:13 +02003649 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: &')
3650
3651 lines =<< trim END
3652 set hlsearch !
3653 END
3654 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: !')
3655
3656 set hlsearch&
Bram Moolenaar208f0b42021-06-20 12:40:08 +02003657enddef
3658
Bram Moolenaarc03fe662021-07-11 16:52:45 +02003659" This must be called last, it may cause following :def functions to fail
3660def Test_xxx_echoerr_line_number()
3661 var lines =<< trim END
3662 echoerr 'some'
3663 .. ' error'
3664 .. ' continued'
3665 END
3666 CheckDefExecAndScriptFailure(lines, 'some error continued', 1)
3667enddef
3668
Bram Moolenaar9537e372021-12-10 21:05:53 +00003669func Test_debug_with_lambda()
Bram Moolenaar9fffef92021-12-10 16:55:58 +00003670 CheckRunVimInTerminal
3671
Bram Moolenaar9537e372021-12-10 21:05:53 +00003672 " call indirectly to avoid compilation error for missing functions
3673 call Run_Test_debug_with_lambda()
3674endfunc
3675
3676def Run_Test_debug_with_lambda()
Bram Moolenaar9fffef92021-12-10 16:55:58 +00003677 var lines =<< trim END
3678 vim9script
3679 def Func()
3680 var n = 0
3681 echo [0]->filter((_, v) => v == n)
3682 enddef
3683 breakadd func Func
3684 Func()
3685 END
3686 writefile(lines, 'XdebugFunc')
3687 var buf = RunVimInTerminal('-S XdebugFunc', {rows: 6, wait_for_ruler: 0})
3688 WaitForAssert(() => assert_match('^>', term_getline(buf, 6)))
3689
3690 term_sendkeys(buf, "cont\<CR>")
3691 WaitForAssert(() => assert_match('\[0\]', term_getline(buf, 5)))
3692
3693 StopVimInTerminal(buf)
3694 delete('XdebugFunc')
3695enddef
3696
Bram Moolenaar310091d2021-12-23 21:14:37 +00003697func Test_debug_running_out_of_lines()
3698 CheckRunVimInTerminal
3699
3700 " call indirectly to avoid compilation error for missing functions
3701 call Run_Test_debug_running_out_of_lines()
3702endfunc
3703
3704def Run_Test_debug_running_out_of_lines()
3705 var lines =<< trim END
3706 vim9script
3707 def Crash()
3708 #
3709 #
3710 #
3711 #
3712 #
3713 #
3714 #
3715 if true
3716 #
3717 endif
3718 enddef
3719 breakadd func Crash
3720 Crash()
3721 END
3722 writefile(lines, 'XdebugFunc')
3723 var buf = RunVimInTerminal('-S XdebugFunc', {rows: 6, wait_for_ruler: 0})
3724 WaitForAssert(() => assert_match('^>', term_getline(buf, 6)))
3725
3726 term_sendkeys(buf, "next\<CR>")
3727 TermWait(buf)
3728 WaitForAssert(() => assert_match('^>', term_getline(buf, 6)))
3729
3730 term_sendkeys(buf, "cont\<CR>")
3731 TermWait(buf)
3732
3733 StopVimInTerminal(buf)
3734 delete('XdebugFunc')
3735enddef
3736
Bram Moolenaar648594e2021-07-11 17:55:01 +02003737def ProfiledWithLambda()
Bram Moolenaard9162552021-07-11 15:26:13 +02003738 var n = 3
3739 echo [[1, 2], [3, 4]]->filter((_, l) => l[0] == n)
3740enddef
3741
Bram Moolenaar648594e2021-07-11 17:55:01 +02003742def ProfiledNested()
3743 var x = 0
3744 def Nested(): any
3745 return x
3746 enddef
3747 Nested()
3748enddef
3749
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02003750def ProfiledNestedProfiled()
3751 var x = 0
3752 def Nested(): any
3753 return x
3754 enddef
3755 Nested()
3756enddef
3757
Dominique Pelle923dce22021-11-21 11:36:04 +00003758" Execute this near the end, profiling doesn't stop until Vim exits.
Bram Moolenaard9162552021-07-11 15:26:13 +02003759" This only tests that it works, not the profiling output.
3760def Test_xx_profile_with_lambda()
Bram Moolenaar4ece1522021-07-11 16:31:51 +02003761 CheckFeature profile
3762
Bram Moolenaard9162552021-07-11 15:26:13 +02003763 profile start Xprofile.log
Bram Moolenaar648594e2021-07-11 17:55:01 +02003764 profile func ProfiledWithLambda
3765 ProfiledWithLambda()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02003766
Bram Moolenaar648594e2021-07-11 17:55:01 +02003767 profile func ProfiledNested
3768 ProfiledNested()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02003769
3770 # Also profile the nested function. Use a different function, although the
3771 # contents is the same, to make sure it was not already compiled.
3772 profile func *
3773 ProfiledNestedProfiled()
3774
3775 profdel func *
3776 profile pause
Bram Moolenaard9162552021-07-11 15:26:13 +02003777enddef
3778
Bram Moolenaar585fea72020-04-02 22:33:21 +02003779" Keep this last, it messes up highlighting.
3780def Test_substitute_cmd()
3781 new
3782 setline(1, 'something')
3783 :substitute(some(other(
3784 assert_equal('otherthing', getline(1))
3785 bwipe!
3786
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003787 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003788 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02003789 vim9script
3790 new
3791 setline(1, 'something')
3792 :substitute(some(other(
3793 assert_equal('otherthing', getline(1))
3794 bwipe!
3795 END
3796 writefile(lines, 'Xvim9lines')
3797 source Xvim9lines
3798
3799 delete('Xvim9lines')
3800enddef
3801
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker