blob: b789b00921efb4c33feae1e6d6dc31e9a08ac8cc [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001" Test various aspects of the Vim9 script language.
2
Bram Moolenaar673660a2020-01-26 16:50:05 +01003source check.vim
Bram Moolenaar101f4812020-06-16 23:18:51 +02004source term_util.vim
Bram Moolenaarad39c092020-02-26 18:23:43 +01005source view_util.vim
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006source vim9.vim
Bram Moolenaare3d46852020-08-29 13:39:17 +02007source shared.vim
Bram Moolenaar37294bd2021-03-10 13:40:08 +01008source screendump.vim
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010def Test_range_only()
11 new
12 setline(1, ['blah', 'Blah'])
13 :/Blah/
14 assert_equal(2, getcurpos()[1])
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020015 bwipe!
16
17 # without range commands use current line
18 new
19 setline(1, ['one', 'two', 'three'])
20 :2
21 print
22 assert_equal('two', Screenline(&lines))
23 :3
24 list
25 assert_equal('three$', Screenline(&lines))
Bram Moolenaarb8554302021-02-15 21:30:30 +010026
27 # missing command does not print the line
28 var lines =<< trim END
29 vim9script
30 :1|
31 assert_equal('three$', Screenline(&lines))
32 :|
33 assert_equal('three$', Screenline(&lines))
34 END
35 CheckScriptSuccess(lines)
36
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020037 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +010038
39 # won't generate anything
40 if false
41 :123
42 endif
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020043enddef
44
Bram Moolenaara6e67e42020-05-15 23:36:40 +020045let g:alist = [7]
46let g:astring = 'text'
Bram Moolenaarf0b9f432020-07-17 23:03:17 +020047let g:anumber = 123
Bram Moolenaar6e587dc2020-02-06 13:15:52 +010048
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020049def Test_delfunction()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020050 # Check function is defined in script namespace
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020051 CheckScriptSuccess([
52 'vim9script',
53 'func CheckMe()',
54 ' return 123',
55 'endfunc',
56 'assert_equal(123, s:CheckMe())',
57 ])
58
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020059 # Check function in script namespace cannot be deleted
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020060 CheckScriptFailure([
61 'vim9script',
62 'func DeleteMe1()',
63 'endfunc',
64 'delfunction DeleteMe1',
65 ], 'E1084:')
66 CheckScriptFailure([
67 'vim9script',
68 'func DeleteMe2()',
69 'endfunc',
70 'def DoThat()',
71 ' delfunction DeleteMe2',
72 'enddef',
73 'DoThat()',
74 ], 'E1084:')
75 CheckScriptFailure([
76 'vim9script',
77 'def DeleteMe3()',
78 'enddef',
79 'delfunction DeleteMe3',
80 ], 'E1084:')
81 CheckScriptFailure([
82 'vim9script',
83 'def DeleteMe4()',
84 'enddef',
85 'def DoThat()',
86 ' delfunction DeleteMe4',
87 'enddef',
88 'DoThat()',
89 ], 'E1084:')
Bram Moolenaar925e9fd2020-07-25 15:41:11 +020090
91 # Check that global :def function can be replaced and deleted
Bram Moolenaarcfcd0112020-09-27 15:19:27 +020092 var lines =<< trim END
Bram Moolenaar925e9fd2020-07-25 15:41:11 +020093 vim9script
94 def g:Global(): string
95 return "yes"
96 enddef
97 assert_equal("yes", g:Global())
98 def! g:Global(): string
99 return "no"
100 enddef
101 assert_equal("no", g:Global())
102 delfunc g:Global
103 assert_false(exists('*g:Global'))
104 END
105 CheckScriptSuccess(lines)
106
107 # Check that global function can be replaced by a :def function and deleted
108 lines =<< trim END
109 vim9script
110 func g:Global()
111 return "yes"
112 endfunc
113 assert_equal("yes", g:Global())
114 def! g:Global(): string
115 return "no"
116 enddef
117 assert_equal("no", g:Global())
118 delfunc g:Global
119 assert_false(exists('*g:Global'))
120 END
121 CheckScriptSuccess(lines)
122
123 # Check that global :def function can be replaced by a function and deleted
124 lines =<< trim END
125 vim9script
126 def g:Global(): string
127 return "yes"
128 enddef
129 assert_equal("yes", g:Global())
130 func! g:Global()
131 return "no"
132 endfunc
133 assert_equal("no", g:Global())
134 delfunc g:Global
135 assert_false(exists('*g:Global'))
136 END
137 CheckScriptSuccess(lines)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200138enddef
139
Bram Moolenaar08052222020-09-14 17:04:31 +0200140def Test_wrong_type()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200141 CheckDefFailure(['var name: list<nothing>'], 'E1010:')
142 CheckDefFailure(['var name: list<list<nothing>>'], 'E1010:')
143 CheckDefFailure(['var name: dict<nothing>'], 'E1010:')
144 CheckDefFailure(['var name: dict<dict<nothing>>'], 'E1010:')
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100145
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200146 CheckDefFailure(['var name: dict<number'], 'E1009:')
147 CheckDefFailure(['var name: dict<list<number>'], 'E1009:')
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100148
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200149 CheckDefFailure(['var name: ally'], 'E1010:')
150 CheckDefFailure(['var name: bram'], 'E1010:')
151 CheckDefFailure(['var name: cathy'], 'E1010:')
152 CheckDefFailure(['var name: dom'], 'E1010:')
153 CheckDefFailure(['var name: freddy'], 'E1010:')
154 CheckDefFailure(['var name: john'], 'E1010:')
155 CheckDefFailure(['var name: larry'], 'E1010:')
156 CheckDefFailure(['var name: ned'], 'E1010:')
157 CheckDefFailure(['var name: pam'], 'E1010:')
158 CheckDefFailure(['var name: sam'], 'E1010:')
159 CheckDefFailure(['var name: vim'], 'E1010:')
Bram Moolenaara0a9f432020-04-28 21:29:34 +0200160
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200161 CheckDefFailure(['var Ref: number', 'Ref()'], 'E1085:')
162 CheckDefFailure(['var Ref: string', 'var res = Ref()'], 'E1085:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200163enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaar10c65862020-10-08 21:16:42 +0200165def Test_script_wrong_type()
166 var lines =<< trim END
167 vim9script
168 var s:dict: dict<string>
169 s:dict['a'] = ['x']
170 END
171 CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got list<string>', 3)
172enddef
173
Bram Moolenaar08052222020-09-14 17:04:31 +0200174def Test_const()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200175 CheckDefFailure(['final name = 234', 'name = 99'], 'E1018:')
176 CheckDefFailure(['final one = 234', 'var one = 99'], 'E1017:')
177 CheckDefFailure(['final list = [1, 2]', 'var list = [3, 4]'], 'E1017:')
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200178 CheckDefFailure(['final two'], 'E1125:')
179 CheckDefFailure(['final &option'], 'E996:')
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200180
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200181 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200182 final list = [1, 2, 3]
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200183 list[0] = 4
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200184 list->assert_equal([4, 2, 3])
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200185 const other = [5, 6, 7]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200186 other->assert_equal([5, 6, 7])
Bram Moolenaar71abe482020-09-14 22:28:30 +0200187
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200188 var varlist = [7, 8]
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200189 const constlist = [1, varlist, 3]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200190 varlist[0] = 77
Bram Moolenaar23e2e112021-08-03 21:16:18 +0200191 constlist[1][1] = 88
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200192 var cl = constlist[1]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200193 cl[1] = 88
194 constlist->assert_equal([1, [77, 88], 3])
195
Bram Moolenaare0de1712020-12-02 17:36:54 +0100196 var vardict = {five: 5, six: 6}
197 const constdict = {one: 1, two: vardict, three: 3}
Bram Moolenaar71abe482020-09-14 22:28:30 +0200198 vardict['five'] = 55
Bram Moolenaar23e2e112021-08-03 21:16:18 +0200199 constdict['two']['six'] = 66
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200200 var cd = constdict['two']
Bram Moolenaar71abe482020-09-14 22:28:30 +0200201 cd['six'] = 66
Bram Moolenaare0de1712020-12-02 17:36:54 +0100202 constdict->assert_equal({one: 1, two: {five: 55, six: 66}, three: 3})
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200203 END
204 CheckDefAndScriptSuccess(lines)
Bram Moolenaar08052222020-09-14 17:04:31 +0200205enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200207def Test_const_bang()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200208 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200209 const var = 234
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200210 var = 99
211 END
212 CheckDefExecFailure(lines, 'E1018:', 2)
213 CheckScriptFailure(['vim9script'] + lines, 'E46:', 3)
214
215 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200216 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200217 ll[0] = 99
218 END
219 CheckDefExecFailure(lines, 'E1119:', 2)
220 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
221
222 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200223 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200224 ll[3] = 99
225 END
226 CheckDefExecFailure(lines, 'E1118:', 2)
227 CheckScriptFailure(['vim9script'] + lines, 'E684:', 3)
228
229 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100230 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200231 dd["one"] = 99
232 END
233 CheckDefExecFailure(lines, 'E1121:', 2)
234 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
235
236 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100237 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200238 dd["three"] = 99
239 END
240 CheckDefExecFailure(lines, 'E1120:')
241 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
242enddef
243
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200244def Test_range_no_colon()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200245 CheckDefFailure(['%s/a/b/'], 'E1050:')
246 CheckDefFailure(['+ s/a/b/'], 'E1050:')
247 CheckDefFailure(['- s/a/b/'], 'E1050:')
248 CheckDefFailure(['. s/a/b/'], 'E1050:')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200249enddef
250
251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252def Test_block()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200253 var outer = 1
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254 {
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200255 var inner = 2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 assert_equal(1, outer)
257 assert_equal(2, inner)
258 }
259 assert_equal(1, outer)
Bram Moolenaar3f1e9f02021-02-27 22:36:43 +0100260
261 {|echo 'yes'|}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262enddef
263
Bram Moolenaar08052222020-09-14 17:04:31 +0200264def Test_block_failure()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200265 CheckDefFailure(['{', 'var inner = 1', '}', 'echo inner'], 'E1001:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200266 CheckDefFailure(['}'], 'E1025:')
267 CheckDefFailure(['{', 'echo 1'], 'E1026:')
268enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200270def Test_block_local_vars()
271 var lines =<< trim END
272 vim9script
Bram Moolenaared234f22020-10-15 20:42:20 +0200273 v:testing = 1
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200274 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200275 var text = ['hello']
276 def SayHello(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200277 return text
278 enddef
279 def SetText(v: string)
Bram Moolenaared234f22020-10-15 20:42:20 +0200280 text = [v]
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200281 enddef
282 endif
283
284 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200285 var text = ['again']
286 def SayAgain(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200287 return text
288 enddef
289 endif
Bram Moolenaared234f22020-10-15 20:42:20 +0200290
291 # test that the "text" variables are not cleaned up
292 test_garbagecollect_now()
293
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200294 defcompile
295
Bram Moolenaared234f22020-10-15 20:42:20 +0200296 assert_equal(['hello'], SayHello())
297 assert_equal(['again'], SayAgain())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200298
299 SetText('foobar')
Bram Moolenaared234f22020-10-15 20:42:20 +0200300 assert_equal(['foobar'], SayHello())
301
302 call writefile(['ok'], 'Xdidit')
303 qall!
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200304 END
Bram Moolenaared234f22020-10-15 20:42:20 +0200305
306 # need to execute this with a separate Vim instance to avoid the current
307 # context gets garbage collected.
308 writefile(lines, 'Xscript')
309 RunVim([], [], '-S Xscript')
310 assert_equal(['ok'], readfile('Xdidit'))
311
312 delete('Xscript')
313 delete('Xdidit')
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200314enddef
315
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200316def Test_block_local_vars_with_func()
317 var lines =<< trim END
318 vim9script
319 if true
320 var foo = 'foo'
321 if true
322 var bar = 'bar'
323 def Func(): list<string>
324 return [foo, bar]
325 enddef
326 endif
327 endif
328 # function is compiled here, after blocks have finished, can still access
329 # "foo" and "bar"
330 assert_equal(['foo', 'bar'], Func())
331 END
332 CheckScriptSuccess(lines)
333enddef
334
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200335" legacy func for command that's defined later
336func InvokeSomeCommand()
337 SomeCommand
338endfunc
339
340def Test_autocommand_block()
341 com SomeCommand {
342 g:someVar = 'some'
343 }
344 InvokeSomeCommand()
345 assert_equal('some', g:someVar)
346
347 delcommand SomeCommand
348 unlet g:someVar
349enddef
350
351def Test_command_block()
352 au BufNew *.xml {
353 g:otherVar = 'other'
354 }
355 split other.xml
356 assert_equal('other', g:otherVar)
357
358 bwipe!
359 au! BufNew *.xml
360 unlet g:otherVar
361enddef
362
Bram Moolenaard032f342020-07-18 18:13:02 +0200363func g:NoSuchFunc()
364 echo 'none'
365endfunc
366
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100367def Test_try_catch_throw()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200368 var l = []
Bram Moolenaar7a092242020-04-16 22:10:49 +0200369 try # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370 add(l, '1')
371 throw 'wrong'
372 add(l, '2')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200373 catch # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100374 add(l, v:exception)
Bram Moolenaar7a092242020-04-16 22:10:49 +0200375 finally # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100376 add(l, '3')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200377 endtry # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378 assert_equal(['1', 'wrong', '3'], l)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200379
Bram Moolenaare8593122020-07-18 15:17:02 +0200380 l = []
381 try
382 try
383 add(l, '1')
384 throw 'wrong'
385 add(l, '2')
386 catch /right/
387 add(l, v:exception)
388 endtry
389 catch /wrong/
390 add(l, 'caught')
Bram Moolenaar373863e2020-09-26 17:20:53 +0200391 fina
Bram Moolenaare8593122020-07-18 15:17:02 +0200392 add(l, 'finally')
393 endtry
394 assert_equal(['1', 'caught', 'finally'], l)
395
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200396 var n: number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200397 try
398 n = l[3]
399 catch /E684:/
400 n = 99
401 endtry
402 assert_equal(99, n)
403
Bram Moolenaar69f70502021-01-01 16:10:46 +0100404 var done = 'no'
405 if 0
406 try | catch | endtry
407 else
408 done = 'yes'
409 endif
410 assert_equal('yes', done)
411
412 done = 'no'
413 if 1
414 done = 'yes'
415 else
416 try | catch | endtry
417 done = 'never'
418 endif
419 assert_equal('yes', done)
420
421 if 1
422 else
423 try | catch /pat/ | endtry
424 try | catch /pat/
425 endtry
426 try
427 catch /pat/ | endtry
428 try
429 catch /pat/
430 endtry
431 endif
432
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200433 try
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200434 # string slice returns a string, not a number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200435 n = g:astring[3]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200436 catch /E1012:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200437 n = 77
438 endtry
439 assert_equal(77, n)
440
441 try
442 n = l[g:astring]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200443 catch /E1012:/
Bram Moolenaar56acb092020-08-16 14:48:19 +0200444 n = 88
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200445 endtry
Bram Moolenaar56acb092020-08-16 14:48:19 +0200446 assert_equal(88, n)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200447
448 try
449 n = s:does_not_exist
450 catch /E121:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200451 n = 111
452 endtry
453 assert_equal(111, n)
454
455 try
456 n = g:does_not_exist
457 catch /E121:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200458 n = 121
459 endtry
460 assert_equal(121, n)
461
Bram Moolenaare0de1712020-12-02 17:36:54 +0100462 var d = {one: 1}
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200463 try
464 n = d[g:astring]
465 catch /E716:/
466 n = 222
467 endtry
468 assert_equal(222, n)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200469
470 try
471 n = -g:astring
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +0200472 catch /E1012:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200473 n = 233
474 endtry
475 assert_equal(233, n)
476
477 try
478 n = +g:astring
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +0200479 catch /E1012:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200480 n = 244
481 endtry
482 assert_equal(244, n)
483
484 try
485 n = +g:alist
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +0200486 catch /E1012:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200487 n = 255
488 endtry
489 assert_equal(255, n)
490
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200491 var nd: dict<any>
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200492 try
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100493 nd = {[g:alist]: 1}
494 catch /E1105:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200495 n = 266
496 endtry
497 assert_equal(266, n)
498
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +0000499 l = [1, 2, 3]
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200500 try
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +0000501 [n] = l
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200502 catch /E1093:/
503 n = 277
504 endtry
505 assert_equal(277, n)
506
Bram Moolenaare8593122020-07-18 15:17:02 +0200507 try
508 &ts = g:astring
Bram Moolenaar5e654232020-09-16 15:22:00 +0200509 catch /E1012:/
Bram Moolenaare8593122020-07-18 15:17:02 +0200510 n = 288
511 endtry
512 assert_equal(288, n)
513
514 try
515 &backspace = 'asdf'
516 catch /E474:/
517 n = 299
518 endtry
519 assert_equal(299, n)
520
521 l = [1]
522 try
523 l[3] = 3
524 catch /E684:/
525 n = 300
526 endtry
527 assert_equal(300, n)
528
529 try
Bram Moolenaare8593122020-07-18 15:17:02 +0200530 unlet g:does_not_exist
531 catch /E108:/
532 n = 322
533 endtry
534 assert_equal(322, n)
535
536 try
Bram Moolenaar2bede172020-11-19 18:53:18 +0100537 d = {text: 1, [g:astring]: 2}
Bram Moolenaare8593122020-07-18 15:17:02 +0200538 catch /E721:/
539 n = 333
540 endtry
541 assert_equal(333, n)
542
543 try
544 l = DeletedFunc()
545 catch /E933:/
546 n = 344
547 endtry
548 assert_equal(344, n)
Bram Moolenaard032f342020-07-18 18:13:02 +0200549
550 try
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200551 echo range(1, 2, 0)
552 catch /E726:/
Bram Moolenaard032f342020-07-18 18:13:02 +0200553 n = 355
554 endtry
555 assert_equal(355, n)
556
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200557 var P = function('g:NoSuchFunc')
Bram Moolenaard032f342020-07-18 18:13:02 +0200558 delfunc g:NoSuchFunc
559 try
560 echo P()
561 catch /E117:/
562 n = 366
563 endtry
564 assert_equal(366, n)
565
566 try
567 echo g:NoSuchFunc()
568 catch /E117:/
569 n = 377
570 endtry
571 assert_equal(377, n)
572
573 try
574 echo g:alist + 4
575 catch /E745:/
576 n = 388
577 endtry
578 assert_equal(388, n)
579
580 try
581 echo 4 + g:alist
582 catch /E745:/
583 n = 399
584 endtry
585 assert_equal(399, n)
586
587 try
588 echo g:alist.member
589 catch /E715:/
590 n = 400
591 endtry
592 assert_equal(400, n)
593
594 try
595 echo d.member
596 catch /E716:/
597 n = 411
598 endtry
599 assert_equal(411, n)
Bram Moolenaard9d77892021-02-12 21:32:47 +0100600
601 var counter = 0
602 for i in range(4)
603 try
604 eval [][0]
605 catch
606 endtry
607 counter += 1
608 endfor
609 assert_equal(4, counter)
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100610
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +0200611 # no requirement for spaces before |
612 try|echo 0|catch|endtry
613
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100614 # return in finally after empty catch
615 def ReturnInFinally(): number
616 try
617 finally
618 return 4
619 endtry
620 return 2
621 enddef
622 assert_equal(4, ReturnInFinally())
Bram Moolenaar8ac681a2021-06-15 20:06:34 +0200623
624 var lines =<< trim END
625 vim9script
626 try
627 acos('0.5')
628 ->setline(1)
629 catch
630 g:caught = v:exception
631 endtry
632 END
633 CheckScriptSuccess(lines)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200634 assert_match('E1219: Float or Number required for argument 1', g:caught)
Bram Moolenaar8ac681a2021-06-15 20:06:34 +0200635 unlet g:caught
Bram Moolenaar41978282021-07-04 14:47:30 +0200636
637 # missing catch and/or finally
638 lines =<< trim END
639 vim9script
640 try
641 echo 'something'
642 endtry
643 END
644 CheckScriptFailure(lines, 'E1032:')
rbtnn84934992021-08-07 13:26:53 +0200645
646 # skipping try-finally-endtry when try-finally-endtry is used in another block
647 lines =<< trim END
648 if v:true
649 try
650 finally
651 endtry
652 else
653 try
654 finally
655 endtry
656 endif
657 END
658 CheckDefAndScriptSuccess(lines)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100659enddef
660
Bram Moolenaar3f987b52021-06-30 12:02:24 +0200661def Test_try_in_catch()
662 var lines =<< trim END
663 vim9script
664 var seq = []
665 def DoIt()
666 try
667 seq->add('throw 1')
668 eval [][0]
669 seq->add('notreached')
670 catch
671 seq->add('catch')
672 try
673 seq->add('throw 2')
674 eval [][0]
675 seq->add('notreached')
676 catch /nothing/
677 seq->add('notreached')
678 endtry
679 seq->add('done')
680 endtry
681 enddef
682 DoIt()
683 assert_equal(['throw 1', 'catch', 'throw 2', 'done'], seq)
684 END
685enddef
686
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200687def Test_error_in_catch()
688 var lines =<< trim END
689 try
690 eval [][0]
691 catch /E684:/
692 eval [][0]
693 endtry
694 END
695 CheckDefExecFailure(lines, 'E684:', 4)
696enddef
697
Bram Moolenaar2e34c342021-03-14 12:13:33 +0100698" :while at the very start of a function that :continue jumps to
699def TryContinueFunc()
700 while g:Count < 2
701 g:sequence ..= 't'
702 try
703 echoerr 'Test'
704 catch
705 g:Count += 1
706 g:sequence ..= 'c'
707 continue
708 endtry
709 g:sequence ..= 'e'
710 g:Count += 1
711 endwhile
712enddef
713
714def Test_continue_in_try_in_while()
715 g:Count = 0
716 g:sequence = ''
717 TryContinueFunc()
718 assert_equal('tctc', g:sequence)
719 unlet g:Count
720 unlet g:sequence
721enddef
722
Bram Moolenaar9cb577a2021-02-22 22:45:10 +0100723def Test_nocatch_return_in_try()
724 # return in try block returns normally
725 def ReturnInTry(): string
726 try
727 return '"some message"'
728 catch
729 endtry
730 return 'not reached'
731 enddef
732 exe 'echoerr ' .. ReturnInTry()
733enddef
734
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100735def Test_cnext_works_in_catch()
736 var lines =<< trim END
737 vim9script
Bram Moolenaarc3235272021-07-10 19:42:03 +0200738 au BufEnter * eval 1 + 2
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100739 writefile(['text'], 'Xfile1')
740 writefile(['text'], 'Xfile2')
741 var items = [
742 {lnum: 1, filename: 'Xfile1', valid: true},
743 {lnum: 1, filename: 'Xfile2', valid: true}
744 ]
745 setqflist([], ' ', {items: items})
746 cwindow
747
748 def CnextOrCfirst()
749 # if cnext fails, cfirst is used
750 try
751 cnext
752 catch
753 cfirst
754 endtry
755 enddef
756
757 CnextOrCfirst()
758 CnextOrCfirst()
759 writefile([getqflist({idx: 0}).idx], 'Xresult')
760 qall
761 END
762 writefile(lines, 'XCatchCnext')
763 RunVim([], [], '--clean -S XCatchCnext')
764 assert_equal(['1'], readfile('Xresult'))
765
766 delete('Xfile1')
767 delete('Xfile2')
768 delete('XCatchCnext')
769 delete('Xresult')
770enddef
771
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100772def Test_throw_skipped()
773 if 0
774 throw dontgethere
775 endif
776enddef
777
Bram Moolenaar8f81b222021-01-14 21:47:06 +0100778def Test_nocatch_throw_silenced()
779 var lines =<< trim END
780 vim9script
781 def Func()
782 throw 'error'
783 enddef
784 silent! Func()
785 END
786 writefile(lines, 'XthrowSilenced')
787 source XthrowSilenced
788 delete('XthrowSilenced')
789enddef
790
Bram Moolenaare8593122020-07-18 15:17:02 +0200791def DeletedFunc(): list<any>
792 return ['delete me']
793enddef
794defcompile
795delfunc DeletedFunc
796
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100797def ThrowFromDef()
Bram Moolenaara72cfb82020-04-23 17:07:30 +0200798 throw "getout" # comment
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100799enddef
800
801func CatchInFunc()
802 try
803 call ThrowFromDef()
804 catch
805 let g:thrown_func = v:exception
806 endtry
807endfunc
808
809def CatchInDef()
810 try
811 ThrowFromDef()
812 catch
813 g:thrown_def = v:exception
814 endtry
815enddef
816
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100817def ReturnFinally(): string
818 try
819 return 'intry'
Bram Moolenaar373863e2020-09-26 17:20:53 +0200820 finall
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100821 g:in_finally = 'finally'
822 endtry
823 return 'end'
824enddef
825
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100826def Test_try_catch_nested()
827 CatchInFunc()
828 assert_equal('getout', g:thrown_func)
829
830 CatchInDef()
831 assert_equal('getout', g:thrown_def)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100832
833 assert_equal('intry', ReturnFinally())
834 assert_equal('finally', g:in_finally)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200835
836 var l = []
837 try
838 l->add('1')
839 throw 'bad'
840 l->add('x')
841 catch /bad/
842 l->add('2')
843 try
844 l->add('3')
845 throw 'one'
846 l->add('x')
847 catch /one/
848 l->add('4')
849 try
850 l->add('5')
851 throw 'more'
852 l->add('x')
853 catch /more/
854 l->add('6')
855 endtry
856 endtry
857 endtry
858 assert_equal(['1', '2', '3', '4', '5', '6'], l)
Bram Moolenaar834193a2021-06-30 20:39:15 +0200859
860 l = []
861 try
862 try
863 l->add('1')
864 throw 'foo'
865 l->add('x')
866 catch
867 l->add('2')
868 throw 'bar'
869 l->add('x')
870 finally
871 l->add('3')
872 endtry
873 l->add('x')
874 catch /bar/
875 l->add('4')
876 endtry
877 assert_equal(['1', '2', '3', '4'], l)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100878enddef
879
Bram Moolenaar9939f572020-09-16 22:29:52 +0200880def TryOne(): number
881 try
882 return 0
883 catch
884 endtry
885 return 0
886enddef
887
888def TryTwo(n: number): string
889 try
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200890 var x = {}
Bram Moolenaar9939f572020-09-16 22:29:52 +0200891 catch
892 endtry
893 return 'text'
894enddef
895
896def Test_try_catch_twice()
897 assert_equal('text', TryOne()->TryTwo())
898enddef
899
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100900def Test_try_catch_match()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200901 var seq = 'a'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100902 try
903 throw 'something'
904 catch /nothing/
905 seq ..= 'x'
906 catch /some/
907 seq ..= 'b'
908 catch /asdf/
909 seq ..= 'x'
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200910 catch ?a\?sdf?
911 seq ..= 'y'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100912 finally
913 seq ..= 'c'
914 endtry
915 assert_equal('abc', seq)
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100916enddef
917
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200918def Test_try_catch_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200919 CheckDefFailure(['catch'], 'E603:')
920 CheckDefFailure(['try', 'echo 0', 'catch', 'catch'], 'E1033:')
921 CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
922 CheckDefFailure(['finally'], 'E606:')
923 CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:')
924 CheckDefFailure(['endtry'], 'E602:')
925 CheckDefFailure(['while 1', 'endtry'], 'E170:')
926 CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:')
Bram Moolenaar13106602020-10-04 16:06:05 +0200927 CheckDefFailure(['if 1', 'endtry'], 'E171:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200928 CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:')
Bram Moolenaar585fea72020-04-02 22:33:21 +0200929
Bram Moolenaare4984292020-12-13 14:19:25 +0100930 CheckDefFailure(['throw'], 'E1143:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200931 CheckDefFailure(['throw xxx'], 'E1001:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200932enddef
933
Bram Moolenaar7c5b3c0362021-02-14 22:40:57 +0100934def Try_catch_skipped()
935 var l = []
936 try
937 finally
938 endtry
939
940 if 1
941 else
942 try
943 endtry
944 endif
945enddef
946
947" The skipped try/endtry was updating the wrong instruction.
948def Test_try_catch_skipped()
949 var instr = execute('disassemble Try_catch_skipped')
950 assert_match("NEWLIST size 0\n", instr)
951enddef
952
953
954
Bram Moolenaar006ad482020-06-30 20:55:15 +0200955def Test_throw_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200956 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200957 var lines =<< trim END
Bram Moolenaar006ad482020-06-30 20:55:15 +0200958 vim9script
959 try
960 throw 'one'
961 .. 'two'
962 catch
963 assert_equal('onetwo', v:exception)
964 endtry
965 END
966 CheckScriptSuccess(lines)
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200967
968 lines =<< trim END
969 vim9script
Bram Moolenaar352134b2020-10-17 22:04:08 +0200970 @r = ''
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200971 def Func()
972 throw @r
973 enddef
974 var result = ''
975 try
976 Func()
977 catch /E1129:/
978 result = 'caught'
979 endtry
980 assert_equal('caught', result)
981 END
982 CheckScriptSuccess(lines)
Bram Moolenaar006ad482020-06-30 20:55:15 +0200983enddef
984
Bram Moolenaared677f52020-08-12 16:38:10 +0200985def Test_error_in_nested_function()
Bram Moolenaar03dfde22021-02-14 13:17:22 +0100986 # an error in a nested :function aborts executing in the calling :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200987 var lines =<< trim END
Bram Moolenaared677f52020-08-12 16:38:10 +0200988 vim9script
989 def Func()
990 Error()
991 g:test_var = 1
992 enddef
993 func Error() abort
994 eval [][0]
995 endfunc
996 Func()
997 END
998 g:test_var = 0
999 CheckScriptFailure(lines, 'E684:')
1000 assert_equal(0, g:test_var)
1001enddef
1002
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001003def Test_abort_after_error()
1004 var lines =<< trim END
1005 vim9script
1006 while true
1007 echo notfound
1008 endwhile
1009 g:gotthere = true
1010 END
1011 g:gotthere = false
1012 CheckScriptFailure(lines, 'E121:')
1013 assert_false(g:gotthere)
1014 unlet g:gotthere
1015enddef
1016
Bram Moolenaar37c83712020-06-30 21:18:36 +02001017def Test_cexpr_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001018 # only checks line continuation
Bram Moolenaar37c83712020-06-30 21:18:36 +02001019 set errorformat=File\ %f\ line\ %l
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001020 var lines =<< trim END
Bram Moolenaar37c83712020-06-30 21:18:36 +02001021 vim9script
1022 cexpr 'File'
1023 .. ' someFile' ..
1024 ' line 19'
1025 assert_equal(19, getqflist()[0].lnum)
1026 END
1027 CheckScriptSuccess(lines)
1028 set errorformat&
1029enddef
1030
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +02001031def Test_statusline_syntax()
1032 # legacy syntax is used for 'statusline'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001033 var lines =<< trim END
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +02001034 vim9script
1035 func g:Status()
1036 return '%{"x" is# "x"}'
1037 endfunc
1038 set laststatus=2 statusline=%!Status()
1039 redrawstatus
1040 set laststatus statusline=
1041 END
1042 CheckScriptSuccess(lines)
1043enddef
1044
Bram Moolenaarb2097502020-07-19 17:17:02 +02001045def Test_list_vimscript()
1046 # checks line continuation and comments
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001047 var lines =<< trim END
Bram Moolenaarb2097502020-07-19 17:17:02 +02001048 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001049 var mylist = [
Bram Moolenaarb2097502020-07-19 17:17:02 +02001050 'one',
1051 # comment
1052 'two', # empty line follows
1053
1054 'three',
1055 ]
1056 assert_equal(['one', 'two', 'three'], mylist)
1057 END
1058 CheckScriptSuccess(lines)
Bram Moolenaar66250c92020-08-20 15:02:42 +02001059
1060 # check all lines from heredoc are kept
1061 lines =<< trim END
1062 # comment 1
1063 two
1064 # comment 3
1065
1066 five
1067 # comment 6
1068 END
1069 assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines)
Bram Moolenaar4bce26b2021-01-22 22:06:56 +01001070
1071 lines =<< trim END
1072 [{
1073 a: 0}]->string()->assert_equal("[{'a': 0}]")
1074 END
1075 CheckDefAndScriptSuccess(lines)
Bram Moolenaarb2097502020-07-19 17:17:02 +02001076enddef
1077
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001078if has('channel')
1079 let someJob = test_null_job()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001080
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001081 def FuncWithError()
1082 echomsg g:someJob
1083 enddef
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001084
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001085 func Test_convert_emsg_to_exception()
1086 try
1087 call FuncWithError()
1088 catch
1089 call assert_match('Vim:E908:', v:exception)
1090 endtry
1091 endfunc
1092endif
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094let s:export_script_lines =<< trim END
1095 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001096 var name: string = 'bob'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 def Concat(arg: string): string
1098 return name .. arg
1099 enddef
Bram Moolenaar227a69d2020-05-15 18:17:28 +02001100 g:result = Concat('bie')
1101 g:localname = name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001102
1103 export const CONST = 1234
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001104 export var exported = 9876
1105 export var exp_name = 'John'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 export def Exported(): string
1107 return 'Exported'
1108 enddef
Bram Moolenaar24e93162021-07-18 20:40:33 +02001109 export def ExportedValue(): number
1110 return exported
1111 enddef
1112 export def ExportedInc()
1113 exported += 5
1114 enddef
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001115 export final theList = [1]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116END
1117
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001118def Undo_export_script_lines()
1119 unlet g:result
1120 unlet g:localname
1121enddef
1122
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001123def Test_vim9_import_export()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001124 var import_script_lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 vim9script
Bram Moolenaar24e93162021-07-18 20:40:33 +02001126 import {exported, Exported, ExportedValue} from './Xexport.vim'
1127 g:exported1 = exported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001128 exported += 3
Bram Moolenaar24e93162021-07-18 20:40:33 +02001129 g:exported2 = exported
1130 g:exported3 = ExportedValue()
1131
1132 import ExportedInc from './Xexport.vim'
1133 ExportedInc()
1134 g:exported_i1 = exported
1135 g:exported_i2 = ExportedValue()
1136
1137 exported = 11
1138 g:exported_s1 = exported
1139 g:exported_s2 = ExportedValue()
1140
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 g:imported_func = Exported()
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001142
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001143 def GetExported(): string
Bram Moolenaare0de1712020-12-02 17:36:54 +01001144 var local_dict = {ref: Exported}
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001145 return local_dict.ref()
1146 enddef
1147 g:funcref_result = GetExported()
1148
Bram Moolenaar4db572e2021-07-18 18:21:38 +02001149 var dir = './'
1150 var ext = ".vim"
1151 import {exp_name} from dir .. 'Xexport' .. ext
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001152 g:imported_name = exp_name
1153 exp_name ..= ' Doe'
1154 g:imported_name_appended = exp_name
Bram Moolenaar24e93162021-07-18 20:40:33 +02001155 g:exported_later = exported
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001156
1157 import theList from './Xexport.vim'
1158 theList->add(2)
1159 assert_equal([1, 2], theList)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001160 END
1161
1162 writefile(import_script_lines, 'Ximport.vim')
1163 writefile(s:export_script_lines, 'Xexport.vim')
1164
1165 source Ximport.vim
1166
1167 assert_equal('bobbie', g:result)
1168 assert_equal('bob', g:localname)
Bram Moolenaar24e93162021-07-18 20:40:33 +02001169 assert_equal(9876, g:exported1)
1170 assert_equal(9879, g:exported2)
1171 assert_equal(9879, g:exported3)
1172
1173 assert_equal(9884, g:exported_i1)
1174 assert_equal(9884, g:exported_i2)
1175
1176 assert_equal(11, g:exported_s1)
1177 assert_equal(11, g:exported_s2)
1178 assert_equal(11, g:exported_later)
1179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 assert_equal('Exported', g:imported_func)
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001181 assert_equal('Exported', g:funcref_result)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001182 assert_equal('John', g:imported_name)
1183 assert_equal('John Doe', g:imported_name_appended)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184 assert_false(exists('g:name'))
1185
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001186 Undo_export_script_lines()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001187 unlet g:exported1
1188 unlet g:exported2
1189 unlet g:exported3
1190 unlet g:exported_i1
1191 unlet g:exported_i2
1192 unlet g:exported_later
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193 unlet g:imported_func
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001194 unlet g:imported_name g:imported_name_appended
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195 delete('Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001196
Bram Moolenaar1c991142020-07-04 13:15:31 +02001197 # similar, with line breaks
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001198 var import_line_break_script_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001199 vim9script
1200 import {
1201 exported,
1202 Exported,
1203 }
1204 from
1205 './Xexport.vim'
Bram Moolenaar24e93162021-07-18 20:40:33 +02001206 g:exported = exported
1207 exported += 7
1208 g:exported_added = exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001209 g:imported_func = Exported()
1210 END
1211 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
1212 source Ximport_lbr.vim
1213
Bram Moolenaar24e93162021-07-18 20:40:33 +02001214 assert_equal(11, g:exported)
1215 assert_equal(18, g:exported_added)
Bram Moolenaar1c991142020-07-04 13:15:31 +02001216 assert_equal('Exported', g:imported_func)
1217
1218 # exported script not sourced again
1219 assert_false(exists('g:result'))
Bram Moolenaar24e93162021-07-18 20:40:33 +02001220 unlet g:exported
1221 unlet g:exported_added
Bram Moolenaar1c991142020-07-04 13:15:31 +02001222 unlet g:imported_func
1223 delete('Ximport_lbr.vim')
1224
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001225 var import_star_as_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001226 vim9script
1227 import * as Export from './Xexport.vim'
1228 def UseExport()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001229 g:exported_def = Export.exported
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001230 enddef
Bram Moolenaar24e93162021-07-18 20:40:33 +02001231 g:exported_script = Export.exported
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001232 assert_equal(1, exists('Export.exported'))
1233 assert_equal(0, exists('Export.notexported'))
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001234 UseExport()
1235 END
1236 writefile(import_star_as_lines, 'Ximport.vim')
1237 source Ximport.vim
Bram Moolenaar24e93162021-07-18 20:40:33 +02001238
1239 assert_equal(18, g:exported_def)
1240 assert_equal(18, g:exported_script)
1241 unlet g:exported_def
1242 unlet g:exported_script
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001243
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001244 var import_star_as_lines_no_dot =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001245 vim9script
1246 import * as Export from './Xexport.vim'
1247 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001248 var dummy = 1
1249 var imported = Export + dummy
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001250 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001251 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001252 END
1253 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001254 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001255
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001256 var import_star_as_lines_dot_space =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001257 vim9script
1258 import * as Export from './Xexport.vim'
1259 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001260 var imported = Export . exported
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001261 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001262 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001263 END
1264 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001265 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001266
Bram Moolenaar921ba522021-07-29 22:25:05 +02001267 var import_func_duplicated =<< trim END
1268 vim9script
1269 import ExportedInc from './Xexport.vim'
1270 import ExportedInc from './Xexport.vim'
1271
1272 ExportedInc()
1273 END
1274 writefile(import_func_duplicated, 'Ximport.vim')
1275 assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
1276
Bram Moolenaara6294952020-12-27 13:39:50 +01001277 var import_star_as_duplicated =<< trim END
1278 vim9script
1279 import * as Export from './Xexport.vim'
1280 var some = 'other'
1281 import * as Export from './Xexport.vim'
1282 defcompile
1283 END
1284 writefile(import_star_as_duplicated, 'Ximport.vim')
1285 assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim')
1286
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001287 var import_star_as_lines_script_no_dot =<< trim END
1288 vim9script
1289 import * as Export from './Xexport.vim'
1290 g:imported_script = Export exported
1291 END
1292 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
1293 assert_fails('source Ximport.vim', 'E1029:')
1294
1295 var import_star_as_lines_script_space_after_dot =<< trim END
1296 vim9script
1297 import * as Export from './Xexport.vim'
1298 g:imported_script = Export. exported
1299 END
1300 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
1301 assert_fails('source Ximport.vim', 'E1074:')
1302
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001303 var import_star_as_lines_missing_name =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001304 vim9script
1305 import * as Export from './Xexport.vim'
1306 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001307 var imported = Export.
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001308 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001309 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001310 END
1311 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001312 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001313
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001314 var import_star_as_lbr_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001315 vim9script
1316 import *
1317 as Export
1318 from
1319 './Xexport.vim'
1320 def UseExport()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001321 g:exported = Export.exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001322 enddef
1323 UseExport()
1324 END
1325 writefile(import_star_as_lbr_lines, 'Ximport.vim')
1326 source Ximport.vim
Bram Moolenaar24e93162021-07-18 20:40:33 +02001327 assert_equal(18, g:exported)
1328 unlet g:exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001329
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001330 var import_star_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001331 vim9script
1332 import * from './Xexport.vim'
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001333 END
1334 writefile(import_star_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001335 assert_fails('source Ximport.vim', 'E1045:', '', 2, 'Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001336
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001337 # try to import something that exists but is not exported
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001338 var import_not_exported_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001339 vim9script
1340 import name from './Xexport.vim'
1341 END
1342 writefile(import_not_exported_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001343 assert_fails('source Ximport.vim', 'E1049:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001344
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001345 # try to import something that is already defined
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001346 var import_already_defined =<< trim END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001347 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001348 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001349 import exported from './Xexport.vim'
1350 END
1351 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001352 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001353
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001354 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001355 import_already_defined =<< trim END
1356 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001357 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001358 import * as exported from './Xexport.vim'
1359 END
1360 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001361 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001362
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001363 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001364 import_already_defined =<< trim END
1365 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001366 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001367 import {exported} from './Xexport.vim'
1368 END
1369 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001370 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001371
Bram Moolenaar918a4242020-12-06 14:37:08 +01001372 # try changing an imported const
1373 var import_assign_to_const =<< trim END
1374 vim9script
1375 import CONST from './Xexport.vim'
1376 def Assign()
1377 CONST = 987
1378 enddef
1379 defcompile
1380 END
1381 writefile(import_assign_to_const, 'Ximport.vim')
1382 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1383
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001384 # try changing an imported final
1385 var import_assign_to_final =<< trim END
1386 vim9script
1387 import theList from './Xexport.vim'
1388 def Assign()
1389 theList = [2]
1390 enddef
1391 defcompile
1392 END
1393 writefile(import_assign_to_final, 'Ximport.vim')
1394 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1395
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001396 # import a very long name, requires making a copy
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001397 var import_long_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001398 vim9script
1399 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
1400 END
1401 writefile(import_long_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001402 assert_fails('source Ximport.vim', 'E1048:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001403
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001404 var import_no_from_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001405 vim9script
1406 import name './Xexport.vim'
1407 END
1408 writefile(import_no_from_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001409 assert_fails('source Ximport.vim', 'E1070:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001410
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001411 var import_invalid_string_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001412 vim9script
1413 import name from Xexport.vim
1414 END
1415 writefile(import_invalid_string_lines, 'Ximport.vim')
Bram Moolenaar4db572e2021-07-18 18:21:38 +02001416 assert_fails('source Ximport.vim', 'E121:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001417
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001418 var import_wrong_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001419 vim9script
1420 import name from './XnoExport.vim'
1421 END
1422 writefile(import_wrong_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001423 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001424
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001425 var import_missing_comma_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001426 vim9script
1427 import {exported name} from './Xexport.vim'
1428 END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001429 writefile(import_missing_comma_lines, 'Ximport3.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001430 assert_fails('source Ximport3.vim', 'E1046:', '', 2, 'Ximport3.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001431
Bram Moolenaar60579352021-07-19 21:45:07 +02001432 var import_redefining_lines =<< trim END
1433 vim9script
1434 import exported from './Xexport.vim'
1435 var exported = 5
1436 END
1437 writefile(import_redefining_lines, 'Ximport.vim')
1438 assert_fails('source Ximport.vim', 'E1213: Redefining imported item "exported"', '', 3)
1439
1440 var import_assign_wrong_type_lines =<< trim END
1441 vim9script
1442 import exported from './Xexport.vim'
1443 exported = 'xxx'
1444 END
1445 writefile(import_assign_wrong_type_lines, 'Ximport.vim')
1446 assert_fails('source Ximport.vim', 'E1012: Type mismatch; expected number but got string', '', 3)
1447
1448 var import_assign_const_lines =<< trim END
1449 vim9script
1450 import CONST from './Xexport.vim'
1451 CONST = 4321
1452 END
1453 writefile(import_assign_const_lines, 'Ximport.vim')
1454 assert_fails('source Ximport.vim', 'E741: Value is locked: CONST', '', 3)
1455
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001456 delete('Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001457 delete('Ximport3.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 delete('Xexport.vim')
1459
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001460 # Check that in a Vim9 script 'cpo' is set to the Vim default.
Bram Moolenaar3e191692021-03-17 17:46:00 +01001461 # Flags added or removed are also applied to the restored value.
1462 set cpo=abcd
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001463 var lines =<< trim END
Bram Moolenaar750802b2020-02-23 18:08:33 +01001464 vim9script
1465 g:cpo_in_vim9script = &cpo
Bram Moolenaar3e191692021-03-17 17:46:00 +01001466 set cpo+=f
1467 set cpo-=c
1468 g:cpo_after_vim9script = &cpo
Bram Moolenaar750802b2020-02-23 18:08:33 +01001469 END
1470 writefile(lines, 'Xvim9_script')
1471 source Xvim9_script
Bram Moolenaar3e191692021-03-17 17:46:00 +01001472 assert_equal('fabd', &cpo)
Bram Moolenaar750802b2020-02-23 18:08:33 +01001473 set cpo&vim
1474 assert_equal(&cpo, g:cpo_in_vim9script)
Bram Moolenaar3e191692021-03-17 17:46:00 +01001475 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
1476 assert_equal(newcpo, g:cpo_after_vim9script)
1477
Bram Moolenaar750802b2020-02-23 18:08:33 +01001478 delete('Xvim9_script')
1479enddef
1480
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001481def Test_import_funcref()
1482 var lines =<< trim END
1483 vim9script
1484 export def F(): number
1485 return 42
1486 enddef
1487 export const G = F
1488 END
1489 writefile(lines, 'Xlib.vim')
1490
1491 lines =<< trim END
1492 vim9script
1493 import {G} from './Xlib.vim'
1494 const Foo = G()
1495 assert_equal(42, Foo)
1496
1497 def DoTest()
1498 const Goo = G()
Bram Moolenaar06ca48a2021-10-23 10:25:21 +01001499 assert_equal(42, Goo)
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001500 enddef
1501 DoTest()
1502 END
1503 CheckScriptSuccess(lines)
1504
1505 delete('Xlib.vim')
1506enddef
1507
Bram Moolenaar6853c382021-09-07 22:12:19 +02001508def Test_import_star_fails()
1509 writefile([], 'Xfoo.vim')
1510 var lines =<< trim END
Bram Moolenaaraf2d5d22021-09-07 22:35:34 +02001511 import * as foo from './Xfoo.vim'
Bram Moolenaar6853c382021-09-07 22:12:19 +02001512 foo = 'bar'
1513 END
1514 CheckDefAndScriptFailure2(lines, 'E1094:', 'E1236: Cannot use foo itself')
1515 lines =<< trim END
1516 vim9script
Bram Moolenaaraf2d5d22021-09-07 22:35:34 +02001517 import * as foo from './Xfoo.vim'
Bram Moolenaar6853c382021-09-07 22:12:19 +02001518 var that = foo
1519 END
1520 CheckScriptFailure(lines, 'E1029: Expected ''.''')
Bram Moolenaara9e3d562021-09-08 12:31:35 +02001521
1522 lines =<< trim END
1523 vim9script
1524 import * as 9foo from './Xfoo.vim'
1525 END
1526 CheckScriptFailure(lines, 'E1047:')
1527 lines =<< trim END
1528 vim9script
1529 import * as the#foo from './Xfoo.vim'
1530 END
1531 CheckScriptFailure(lines, 'E1047:')
1532 lines =<< trim END
1533 vim9script
1534 import * as g:foo from './Xfoo.vim'
1535 END
1536 CheckScriptFailure(lines, 'E1047:')
1537
Bram Moolenaaraf2d5d22021-09-07 22:35:34 +02001538 delete('Xfoo.vim')
Bram Moolenaar6853c382021-09-07 22:12:19 +02001539enddef
1540
Bram Moolenaar0a842842021-02-27 22:41:19 +01001541def Test_import_as()
1542 var export_lines =<< trim END
1543 vim9script
1544 export var one = 1
1545 export var yes = 'yes'
Bram Moolenaarc967d572021-07-08 21:38:50 +02001546 export var slist: list<string>
Bram Moolenaar0a842842021-02-27 22:41:19 +01001547 END
1548 writefile(export_lines, 'XexportAs')
1549
1550 var import_lines =<< trim END
1551 vim9script
Bram Moolenaar6c7cc342021-04-17 16:38:50 +02001552 var one = 'notused'
1553 var yes = 777
Bram Moolenaar0a842842021-02-27 22:41:19 +01001554 import one as thatOne from './XexportAs'
1555 assert_equal(1, thatOne)
1556 import yes as yesYes from './XexportAs'
1557 assert_equal('yes', yesYes)
1558 END
1559 CheckScriptSuccess(import_lines)
1560
1561 import_lines =<< trim END
1562 vim9script
1563 import {one as thatOne, yes as yesYes} from './XexportAs'
1564 assert_equal(1, thatOne)
1565 assert_equal('yes', yesYes)
1566 assert_fails('echo one', 'E121:')
1567 assert_fails('echo yes', 'E121:')
1568 END
1569 CheckScriptSuccess(import_lines)
1570
Bram Moolenaarc967d572021-07-08 21:38:50 +02001571 import_lines =<< trim END
1572 vim9script
1573 import {slist as impSlist} from './XexportAs'
1574 impSlist->add(123)
1575 END
1576 CheckScriptFailure(import_lines, 'E1012: Type mismatch; expected string but got number')
1577
Bram Moolenaar0a842842021-02-27 22:41:19 +01001578 delete('XexportAs')
1579enddef
1580
Bram Moolenaar803af682020-08-05 16:20:03 +02001581func g:Trigger()
1582 source Ximport.vim
1583 return "echo 'yes'\<CR>"
1584endfunc
1585
1586def Test_import_export_expr_map()
1587 # check that :import and :export work when buffer is locked
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001588 var export_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001589 vim9script
1590 export def That(): string
1591 return 'yes'
1592 enddef
1593 END
1594 writefile(export_lines, 'Xexport_that.vim')
1595
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001596 var import_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001597 vim9script
1598 import That from './Xexport_that.vim'
1599 assert_equal('yes', That())
1600 END
1601 writefile(import_lines, 'Ximport.vim')
1602
1603 nnoremap <expr> trigger g:Trigger()
1604 feedkeys('trigger', "xt")
1605
Bram Moolenaar730b2482020-08-09 13:02:10 +02001606 delete('Xexport_that.vim')
Bram Moolenaar803af682020-08-05 16:20:03 +02001607 delete('Ximport.vim')
1608 nunmap trigger
1609enddef
1610
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001611def Test_import_in_filetype()
1612 # check that :import works when the buffer is locked
1613 mkdir('ftplugin', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001614 var export_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001615 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001616 export var That = 'yes'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001617 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001618 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001619
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001620 var import_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001621 vim9script
Bram Moolenaar730b2482020-08-09 13:02:10 +02001622 import That from './Xexport_ft.vim'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001623 assert_equal('yes', That)
1624 g:did_load_mytpe = 1
1625 END
1626 writefile(import_lines, 'ftplugin/qf.vim')
1627
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001628 var save_rtp = &rtp
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001629 &rtp = getcwd() .. ',' .. &rtp
1630
1631 filetype plugin on
1632 copen
1633 assert_equal(1, g:did_load_mytpe)
1634
1635 quit!
Bram Moolenaar730b2482020-08-09 13:02:10 +02001636 delete('Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001637 delete('ftplugin', 'rf')
1638 &rtp = save_rtp
1639enddef
1640
Bram Moolenaarefa94442020-08-08 22:16:00 +02001641def Test_use_import_in_mapping()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001642 var lines =<< trim END
Bram Moolenaarefa94442020-08-08 22:16:00 +02001643 vim9script
1644 export def Funcx()
1645 g:result = 42
1646 enddef
1647 END
1648 writefile(lines, 'XsomeExport.vim')
1649 lines =<< trim END
1650 vim9script
1651 import Funcx from './XsomeExport.vim'
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001652 nnoremap <F3> :call <sid>Funcx()<cr>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001653 END
1654 writefile(lines, 'Xmapscript.vim')
1655
1656 source Xmapscript.vim
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001657 feedkeys("\<F3>", "xt")
Bram Moolenaarefa94442020-08-08 22:16:00 +02001658 assert_equal(42, g:result)
1659
1660 unlet g:result
1661 delete('XsomeExport.vim')
1662 delete('Xmapscript.vim')
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001663 nunmap <F3>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001664enddef
1665
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01001666def Test_vim9script_mix()
1667 var lines =<< trim END
1668 if has(g:feature)
1669 " legacy script
1670 let g:legacy = 1
1671 finish
1672 endif
1673 vim9script
1674 g:legacy = 0
1675 END
1676 g:feature = 'eval'
1677 g:legacy = -1
1678 CheckScriptSuccess(lines)
1679 assert_equal(1, g:legacy)
1680
1681 g:feature = 'noteval'
1682 g:legacy = -1
1683 CheckScriptSuccess(lines)
1684 assert_equal(0, g:legacy)
1685enddef
1686
Bram Moolenaar750802b2020-02-23 18:08:33 +01001687def Test_vim9script_fails()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1689 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001690 CheckScriptFailure(['export var some = 123'], 'E1042:')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001691 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001692 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
Bram Moolenaar750802b2020-02-23 18:08:33 +01001693 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
1694
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001695 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001696 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1697
Bram Moolenaare2e40752020-09-04 21:18:46 +02001698 assert_fails('vim9script', 'E1038:')
1699 assert_fails('export something', 'E1043:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700enddef
1701
Bram Moolenaarf7d267e2020-06-17 12:04:54 +02001702func Test_import_fails_without_script()
Bram Moolenaar101f4812020-06-16 23:18:51 +02001703 CheckRunVimInTerminal
1704
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001705 " call indirectly to avoid compilation error for missing functions
Bram Moolenaarc620c052020-07-08 15:16:19 +02001706 call Run_Test_import_fails_on_command_line()
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001707endfunc
1708
Bram Moolenaarc620c052020-07-08 15:16:19 +02001709def Run_Test_import_fails_on_command_line()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001710 var export =<< trim END
Bram Moolenaar101f4812020-06-16 23:18:51 +02001711 vim9script
1712 export def Foo(): number
1713 return 0
1714 enddef
1715 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001716 writefile(export, 'XexportCmd.vim')
Bram Moolenaar101f4812020-06-16 23:18:51 +02001717
Bram Moolenaare0de1712020-12-02 17:36:54 +01001718 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001719 rows: 6, wait_for_ruler: 0})
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001720 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaar101f4812020-06-16 23:18:51 +02001721
Bram Moolenaar730b2482020-08-09 13:02:10 +02001722 delete('XexportCmd.vim')
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001723 StopVimInTerminal(buf)
1724enddef
Bram Moolenaar101f4812020-06-16 23:18:51 +02001725
Bram Moolenaar2b327002020-12-26 15:39:31 +01001726def Test_vim9script_reload_noclear()
1727 var lines =<< trim END
Bram Moolenaara6294952020-12-27 13:39:50 +01001728 vim9script
1729 export var exported = 'thexport'
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001730
1731 export def TheFunc(x = 0)
1732 enddef
Bram Moolenaara6294952020-12-27 13:39:50 +01001733 END
1734 writefile(lines, 'XExportReload')
1735 lines =<< trim END
Bram Moolenaar2b327002020-12-26 15:39:31 +01001736 vim9script noclear
1737 g:loadCount += 1
1738 var s:reloaded = 'init'
Bram Moolenaara6294952020-12-27 13:39:50 +01001739 import exported from './XExportReload'
Bram Moolenaar2b327002020-12-26 15:39:31 +01001740
1741 def Again(): string
1742 return 'again'
1743 enddef
1744
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001745 import TheFunc from './XExportReload'
1746 TheFunc()
1747
Bram Moolenaar2b327002020-12-26 15:39:31 +01001748 if exists('s:loaded') | finish | endif
1749 var s:loaded = true
1750
1751 var s:notReloaded = 'yes'
1752 s:reloaded = 'first'
1753 def g:Values(): list<string>
Bram Moolenaara6294952020-12-27 13:39:50 +01001754 return [s:reloaded, s:notReloaded, Again(), Once(), exported]
Bram Moolenaar2b327002020-12-26 15:39:31 +01001755 enddef
1756
1757 def Once(): string
1758 return 'once'
1759 enddef
1760 END
1761 writefile(lines, 'XReloaded')
1762 g:loadCount = 0
1763 source XReloaded
1764 assert_equal(1, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001765 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001766 source XReloaded
1767 assert_equal(2, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001768 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001769 source XReloaded
1770 assert_equal(3, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001771 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001772
Bram Moolenaar48e11c12021-01-11 18:47:00 +01001773 delete('XReloaded')
Bram Moolenaara6294952020-12-27 13:39:50 +01001774 delete('XExportReload')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001775 delfunc g:Values
Bram Moolenaar2b327002020-12-26 15:39:31 +01001776 unlet g:loadCount
Bram Moolenaar577dc932021-06-27 15:35:40 +02001777
1778 lines =<< trim END
1779 vim9script
1780 def Inner()
1781 enddef
1782 END
1783 lines->writefile('XreloadScript.vim')
1784 source XreloadScript.vim
1785
1786 lines =<< trim END
1787 vim9script
1788 def Outer()
1789 def Inner()
1790 enddef
1791 enddef
1792 defcompile
1793 END
1794 lines->writefile('XreloadScript.vim')
1795 source XreloadScript.vim
1796
1797 delete('XreloadScript.vim')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001798enddef
1799
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001800def Test_vim9script_reload_import()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001801 var lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 vim9script
1803 const var = ''
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001804 var valone = 1234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 def MyFunc(arg: string)
1806 valone = 5678
1807 enddef
1808 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001809 var morelines =<< trim END
1810 var valtwo = 222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 export def GetValtwo(): number
1812 return valtwo
1813 enddef
1814 END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001815 writefile(lines + morelines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816 source Xreload.vim
1817 source Xreload.vim
1818 source Xreload.vim
1819
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001820 # cannot declare a var twice
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 lines =<< trim END
1822 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001823 var valone = 1234
1824 var valone = 5678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 END
1826 writefile(lines, 'Xreload.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001827 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828
1829 delete('Xreload.vim')
1830 delete('Ximport.vim')
1831enddef
1832
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001833" if a script is reloaded with a script-local variable that changed its type, a
1834" compiled function using that variable must fail.
1835def Test_script_reload_change_type()
1836 var lines =<< trim END
1837 vim9script noclear
1838 var str = 'string'
1839 def g:GetStr(): string
1840 return str .. 'xxx'
1841 enddef
1842 END
1843 writefile(lines, 'Xreload.vim')
1844 source Xreload.vim
1845 echo g:GetStr()
1846
1847 lines =<< trim END
1848 vim9script noclear
1849 var str = 1234
1850 END
1851 writefile(lines, 'Xreload.vim')
1852 source Xreload.vim
1853 assert_fails('echo g:GetStr()', 'E1150:')
1854
1855 delfunc g:GetStr
1856 delete('Xreload.vim')
1857enddef
1858
Bram Moolenaarc970e422021-03-17 15:03:04 +01001859" Define CallFunc so that the test can be compiled
1860command CallFunc echo 'nop'
1861
1862def Test_script_reload_from_function()
1863 var lines =<< trim END
1864 vim9script
1865
1866 if exists('g:loaded')
1867 finish
1868 endif
1869 g:loaded = 1
1870 delcommand CallFunc
1871 command CallFunc Func()
1872 def Func()
Bram Moolenaara4c81be2021-03-17 15:23:16 +01001873 so XreloadFunc.vim
Bram Moolenaarc970e422021-03-17 15:03:04 +01001874 g:didTheFunc = 1
1875 enddef
1876 END
1877 writefile(lines, 'XreloadFunc.vim')
1878 source XreloadFunc.vim
1879 CallFunc
1880 assert_equal(1, g:didTheFunc)
1881
1882 delete('XreloadFunc.vim')
1883 delcommand CallFunc
1884 unlet g:loaded
1885 unlet g:didTheFunc
1886enddef
1887
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001888def Test_script_var_shadows_function()
1889 var lines =<< trim END
1890 vim9script
1891 def Func(): number
1892 return 123
1893 enddef
1894 var Func = 1
1895 END
1896 CheckScriptFailure(lines, 'E1041:', 5)
1897enddef
1898
Bram Moolenaar052ff292021-12-11 13:54:46 +00001899def Test_function_shadows_script_var()
1900 var lines =<< trim END
1901 vim9script
1902 var Func = 1
1903 def Func(): number
1904 return 123
1905 enddef
1906 END
1907 CheckScriptFailure(lines, 'E1041:', 3)
1908enddef
1909
Bram Moolenaarc3235272021-07-10 19:42:03 +02001910def Test_script_var_shadows_command()
1911 var lines =<< trim END
1912 var undo = 1
1913 undo = 2
1914 assert_equal(2, undo)
1915 END
1916 CheckDefAndScriptSuccess(lines)
1917
1918 lines =<< trim END
1919 var undo = 1
1920 undo
1921 END
1922 CheckDefAndScriptFailure(lines, 'E1207:', 2)
1923enddef
1924
Bram Moolenaar95006e32020-08-29 17:47:08 +02001925def s:RetSome(): string
1926 return 'some'
1927enddef
1928
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001929" Not exported function that is referenced needs to be accessed by the
1930" script-local name.
1931def Test_vim9script_funcref()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001932 var sortlines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001933 vim9script
1934 def Compare(i1: number, i2: number): number
Bram Moolenaarbed36b92020-07-07 23:31:36 +02001935 return i2 - i1
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001936 enddef
1937
1938 export def FastSort(): list<number>
1939 return range(5)->sort(Compare)
1940 enddef
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001941
1942 export def GetString(arg: string): string
1943 return arg
1944 enddef
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001945 END
1946 writefile(sortlines, 'Xsort.vim')
1947
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001948 var lines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001949 vim9script
1950 import FastSort from './Xsort.vim'
1951 def Test()
1952 g:result = FastSort()
1953 enddef
1954 Test()
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001955
1956 # using a function imported with "as"
1957 import * as anAlias from './Xsort.vim'
1958 assert_equal('yes', anAlias.GetString('yes'))
1959
1960 # using the function from a compiled function
1961 def TestMore(): string
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001962 var s = s:anAlias.GetString('foo')
1963 return s .. anAlias.GetString('bar')
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001964 enddef
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001965 assert_equal('foobar', TestMore())
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001966
1967 # error when using a function that isn't exported
1968 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001969 END
1970 writefile(lines, 'Xscript.vim')
1971
1972 source Xscript.vim
1973 assert_equal([4, 3, 2, 1, 0], g:result)
1974
1975 unlet g:result
1976 delete('Xsort.vim')
1977 delete('Xscript.vim')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001978
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001979 var Funcref = function('s:RetSome')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001980 assert_equal('some', Funcref())
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001981enddef
1982
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001983" Check that when searching for "FilterFunc" it finds the import in the
1984" script where FastFilter() is called from, both as a string and as a direct
1985" function reference.
Bram Moolenaarc620c052020-07-08 15:16:19 +02001986def Test_vim9script_funcref_other_script()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001987 var filterLines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001988 vim9script
1989 export def FilterFunc(idx: number, val: number): bool
1990 return idx % 2 == 1
1991 enddef
1992 export def FastFilter(): list<number>
1993 return range(10)->filter('FilterFunc')
1994 enddef
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001995 export def FastFilterDirect(): list<number>
1996 return range(10)->filter(FilterFunc)
1997 enddef
Bram Moolenaarc620c052020-07-08 15:16:19 +02001998 END
1999 writefile(filterLines, 'Xfilter.vim')
2000
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002001 var lines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02002002 vim9script
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002003 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim'
Bram Moolenaarc620c052020-07-08 15:16:19 +02002004 def Test()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002005 var x: list<number> = FastFilter()
Bram Moolenaarc620c052020-07-08 15:16:19 +02002006 enddef
2007 Test()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002008 def TestDirect()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002009 var x: list<number> = FastFilterDirect()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002010 enddef
2011 TestDirect()
Bram Moolenaarc620c052020-07-08 15:16:19 +02002012 END
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002013 CheckScriptSuccess(lines)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002014 delete('Xfilter.vim')
Bram Moolenaarc620c052020-07-08 15:16:19 +02002015enddef
2016
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002017def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002018 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002019 vim9script
2020 def FuncYes(): string
2021 return 'yes'
2022 enddef
2023 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002024 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002025 def FuncNo(): string
2026 return 'no'
2027 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002028 def g:DoCheck(no_exists: bool)
2029 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002030 assert_equal('no', FuncNo())
2031 enddef
2032 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002033 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002034 def g:DoCheck(no_exists: bool)
2035 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02002036 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002037 enddef
2038 END
2039
2040 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002041 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002042 source Xreloaded.vim
2043 g:DoCheck(true)
2044
2045 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002046 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002047 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01002048 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002049
2050 # FuncNo() is back
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002051 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002052 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01002053 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002054
2055 delete('Xreloaded.vim')
2056enddef
2057
Bram Moolenaar89483d42020-05-10 15:24:44 +02002058def Test_vim9script_reload_delvar()
2059 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002060 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02002061 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002062 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02002063 END
2064 writefile(lines, 'XreloadVar.vim')
2065 source XreloadVar.vim
2066
2067 # now write the script using the same variable locally - works
2068 lines =<< trim END
2069 vim9script
2070 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002071 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02002072 enddef
2073 END
2074 writefile(lines, 'XreloadVar.vim')
2075 source XreloadVar.vim
2076
2077 delete('XreloadVar.vim')
2078enddef
2079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080def Test_import_absolute()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002081 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02002082 'vim9script',
2083 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
2084 'def UseExported()',
2085 ' g:imported_abs = exported',
2086 ' exported = 8888',
2087 ' g:imported_after = exported',
2088 'enddef',
2089 'UseExported()',
2090 'g:import_disassembled = execute("disass UseExported")',
2091 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092 writefile(import_lines, 'Ximport_abs.vim')
2093 writefile(s:export_script_lines, 'Xexport_abs.vim')
2094
2095 source Ximport_abs.vim
2096
2097 assert_equal(9876, g:imported_abs)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002098 assert_equal(8888, g:imported_after)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002099 assert_match('<SNR>\d\+_UseExported\_s*' ..
2100 'g:imported_abs = exported\_s*' ..
2101 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
2102 '1 STOREG g:imported_abs\_s*' ..
2103 'exported = 8888\_s*' ..
2104 '2 PUSHNR 8888\_s*' ..
2105 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
2106 'g:imported_after = exported\_s*' ..
2107 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
2108 '5 STOREG g:imported_after',
Bram Moolenaare6085c52020-04-12 20:19:16 +02002109 g:import_disassembled)
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002110
2111 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112 unlet g:imported_abs
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002113 unlet g:import_disassembled
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114
2115 delete('Ximport_abs.vim')
2116 delete('Xexport_abs.vim')
2117enddef
2118
2119def Test_import_rtp()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002120 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02002121 'vim9script',
2122 'import exported from "Xexport_rtp.vim"',
2123 'g:imported_rtp = exported',
2124 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 writefile(import_lines, 'Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002126 mkdir('import', 'p')
2127 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002129 var save_rtp = &rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002130 &rtp = getcwd()
2131 source Ximport_rtp.vim
2132 &rtp = save_rtp
2133
2134 assert_equal(9876, g:imported_rtp)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002136 Undo_export_script_lines()
2137 unlet g:imported_rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 delete('Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002139 delete('import', 'rf')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140enddef
2141
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002142def Test_import_compile_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002143 var export_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002144 'vim9script',
2145 'export def ExpFunc(): string',
2146 ' return notDefined',
2147 'enddef',
2148 ]
2149 writefile(export_lines, 'Xexported.vim')
2150
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002151 var import_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002152 'vim9script',
2153 'import ExpFunc from "./Xexported.vim"',
2154 'def ImpFunc()',
2155 ' echo ExpFunc()',
2156 'enddef',
2157 'defcompile',
2158 ]
2159 writefile(import_lines, 'Ximport.vim')
2160
2161 try
2162 source Ximport.vim
2163 catch /E1001/
Dominique Pelle923dce22021-11-21 11:36:04 +00002164 # Error should be before the Xexported.vim file.
Bram Moolenaar77072282020-09-16 17:55:40 +02002165 assert_match('E1001: Variable not found: notDefined', v:exception)
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002166 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
2167 endtry
2168
2169 delete('Xexported.vim')
2170 delete('Ximport.vim')
2171enddef
2172
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002173def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002174 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002175 'vim9script',
2176 'def Func()',
2177 ' eval [][0]',
2178 'enddef',
2179 'Func()',
2180 ]
2181 writefile(lines, 'Xtestscript.vim')
2182
2183 for count in range(3)
2184 try
2185 source Xtestscript.vim
2186 catch /E684/
2187 # function name should contain <SNR> every time
2188 assert_match('E684: list index out of range', v:exception)
2189 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
2190 endtry
2191 endfor
2192
2193 delete('Xtestscript.vim')
2194enddef
2195
Bram Moolenaareef21022020-08-01 22:16:43 +02002196def Test_func_overrules_import_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002197 var export_lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002198 vim9script
2199 export def Func()
2200 echo 'imported'
2201 enddef
2202 END
2203 writefile(export_lines, 'XexportedFunc.vim')
2204
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002205 var lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002206 vim9script
2207 import Func from './XexportedFunc.vim'
2208 def Func()
2209 echo 'local to function'
2210 enddef
2211 END
Bram Moolenaar052ff292021-12-11 13:54:46 +00002212 CheckScriptFailure(lines, 'E1041:')
Bram Moolenaareef21022020-08-01 22:16:43 +02002213
2214 lines =<< trim END
2215 vim9script
2216 import Func from './XexportedFunc.vim'
2217 def Outer()
2218 def Func()
2219 echo 'local to function'
2220 enddef
2221 enddef
2222 defcompile
2223 END
2224 CheckScriptFailure(lines, 'E1073:')
2225
2226 delete('XexportedFunc.vim')
2227enddef
2228
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002229def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002230 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002231 vim9script
2232 def Func()
2233 echo 'one'
2234 enddef
2235 def Func()
2236 echo 'two'
2237 enddef
2238 END
2239 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002240
2241 lines =<< trim END
2242 vim9script
2243 def Foo(): string
2244 return 'foo'
Bram Moolenaar052ff292021-12-11 13:54:46 +00002245 enddef
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002246 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002247 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002248 enddef
2249 defcompile
2250 END
2251 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002252enddef
2253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002255 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002256 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 l->remove(0)
2258 l->add(5)
2259 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002260 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261enddef
2262
Bram Moolenaarae616492020-07-28 20:07:27 +02002263def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002264 CheckDefExecFailure(['a = 1'], 'E1100:')
2265 CheckDefExecFailure(['c = 1'], 'E1100:')
2266 CheckDefExecFailure(['i = 1'], 'E1100:')
2267 CheckDefExecFailure(['t = 1'], 'E1100:')
2268 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002269
Bram Moolenaarae616492020-07-28 20:07:27 +02002270 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
2271 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002272 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
2273 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002274 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
2275 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01002276 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
2277 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002278 CheckScriptFailure(['vim9script', 't'], 'E1100:')
2279 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
2280 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002281enddef
2282
Bram Moolenaar158906c2020-02-06 20:39:45 +01002283def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002284 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01002285 if what == 1
2286 res = "one"
2287 elseif what == 2
2288 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002289 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01002290 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002291 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01002292 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002293enddef
2294
Bram Moolenaar158906c2020-02-06 20:39:45 +01002295def Test_if_elseif_else()
2296 assert_equal('one', IfElse(1))
2297 assert_equal('two', IfElse(2))
2298 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002299enddef
2300
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002301def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002302 CheckDefFailure(['elseif true'], 'E582:')
2303 CheckDefFailure(['else'], 'E581:')
2304 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01002305 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002306 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002307
2308 var lines =<< trim END
2309 var s = ''
2310 if s = ''
2311 endif
2312 END
2313 CheckDefFailure(lines, 'E488:')
2314
2315 lines =<< trim END
2316 var s = ''
2317 if s == ''
2318 elseif s = ''
2319 endif
2320 END
2321 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002322enddef
2323
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002324let g:bool_true = v:true
2325let g:bool_false = v:false
2326
2327def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002328 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002329 if true ? true : false
2330 res = true
2331 endif
2332 assert_equal(true, res)
2333
Bram Moolenaar585fea72020-04-02 22:33:21 +02002334 g:glob = 2
2335 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02002336 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002337 endif
2338 assert_equal(2, g:glob)
2339 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02002340 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002341 endif
2342 assert_equal(3, g:glob)
2343
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002344 res = false
2345 if g:bool_true ? true : false
2346 res = true
2347 endif
2348 assert_equal(true, res)
2349
2350 res = false
2351 if true ? g:bool_true : false
2352 res = true
2353 endif
2354 assert_equal(true, res)
2355
2356 res = false
2357 if true ? true : g:bool_false
2358 res = true
2359 endif
2360 assert_equal(true, res)
2361
2362 res = false
2363 if true ? false : true
2364 res = true
2365 endif
2366 assert_equal(false, res)
2367
2368 res = false
2369 if false ? false : true
2370 res = true
2371 endif
2372 assert_equal(true, res)
2373
2374 res = false
2375 if false ? true : false
2376 res = true
2377 endif
2378 assert_equal(false, res)
2379
2380 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002381 if has('xyz') ? true : false
2382 res = true
2383 endif
2384 assert_equal(false, res)
2385
2386 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002387 if true && true
2388 res = true
2389 endif
2390 assert_equal(true, res)
2391
2392 res = false
2393 if true && false
2394 res = true
2395 endif
2396 assert_equal(false, res)
2397
2398 res = false
2399 if g:bool_true && false
2400 res = true
2401 endif
2402 assert_equal(false, res)
2403
2404 res = false
2405 if true && g:bool_false
2406 res = true
2407 endif
2408 assert_equal(false, res)
2409
2410 res = false
2411 if false && false
2412 res = true
2413 endif
2414 assert_equal(false, res)
2415
2416 res = false
2417 if true || false
2418 res = true
2419 endif
2420 assert_equal(true, res)
2421
2422 res = false
2423 if g:bool_true || false
2424 res = true
2425 endif
2426 assert_equal(true, res)
2427
2428 res = false
2429 if true || g:bool_false
2430 res = true
2431 endif
2432 assert_equal(true, res)
2433
2434 res = false
2435 if false || false
2436 res = true
2437 endif
2438 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02002439
2440 # with constant "false" expression may be invalid so long as the syntax is OK
Bram Moolenaarc3235272021-07-10 19:42:03 +02002441 if false | eval 1 + 2 | endif
Bram Moolenaar3988f642020-08-27 22:43:03 +02002442 if false | eval burp + 234 | endif
2443 if false | echo burp 234 'asd' | endif
2444 if false
2445 burp
2446 endif
Bram Moolenaare525bdd2021-08-07 18:12:40 +02002447
2448 # expression with line breaks skipped
2449 if false
2450 ('aaa'
2451 .. 'bbb'
2452 .. 'ccc'
2453 )->setline(1)
2454 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002455enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002456
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002457def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002458 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
2459 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
2460 CheckDefFailure(["if has('aaa'"], 'E110:')
2461 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002462enddef
2463
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002464def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002465 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002466 if i % 2
2467 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002468 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002469 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002470 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002471 endif
2472 x += 1
2473 else
2474 x += 1000
2475 endif
2476 return x
2477enddef
2478
2479def Test_nested_if()
2480 assert_equal(1, RunNested(1))
2481 assert_equal(1000, RunNested(2))
2482enddef
2483
Bram Moolenaarad39c092020-02-26 18:23:43 +01002484def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01002485 # missing argument is ignored
2486 execute
2487 execute # comment
2488
Bram Moolenaarad39c092020-02-26 18:23:43 +01002489 new
2490 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002491 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002492 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002493
Bram Moolenaard2c61702020-09-06 15:58:36 +02002494 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002495 assert_equal('execute-string', getline(1))
2496
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002497 var cmd1 = 'setline(1,'
2498 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002499 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01002500 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002501
Bram Moolenaard2c61702020-09-06 15:58:36 +02002502 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002503 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002504
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002505 var cmd_first = 'call '
2506 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002507 execute cmd_first .. cmd_last
2508 assert_equal('execute-var-var', getline(1))
2509 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02002510
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002511 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002512 execute 'echomsg' (n ? '"true"' : '"no"')
2513 assert_match('^true$', Screenline(&lines))
2514
Bram Moolenaare0de1712020-12-02 17:36:54 +01002515 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002516 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
2517
Bram Moolenaard2c61702020-09-06 15:58:36 +02002518 CheckDefFailure(['execute xxx'], 'E1001:', 1)
2519 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
2520 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002521enddef
2522
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002523def Test_execute_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002524 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002525 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002526 vim9script
2527 execute 'g:someVar'
2528 .. ' = ' ..
2529 '28'
2530 assert_equal(28, g:someVar)
2531 unlet g:someVar
2532 END
2533 CheckScriptSuccess(lines)
2534enddef
2535
Bram Moolenaarad39c092020-02-26 18:23:43 +01002536def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002537 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02002538 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002539 assert_match('^something$', Screenline(&lines))
2540
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002541 echo "some" # comment
2542 echon "thing"
2543 assert_match('^something$', Screenline(&lines))
2544
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002545 var str1 = 'some'
2546 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002547 echo str1 str2
2548 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002549
Bram Moolenaard2c61702020-09-06 15:58:36 +02002550 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01002551enddef
2552
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002553def Test_echomsg_cmd()
2554 echomsg 'some' 'more' # comment
2555 assert_match('^some more$', Screenline(&lines))
2556 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002557 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002558 assert_match('^some more$', Screenline(&lines))
2559
Bram Moolenaard2c61702020-09-06 15:58:36 +02002560 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002561enddef
2562
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002563def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002564 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002565 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002566 vim9script
2567 echomsg 'here'
2568 .. ' is ' ..
2569 'a message'
2570 assert_match('^here is a message$', Screenline(&lines))
2571 END
2572 CheckScriptSuccess(lines)
2573enddef
2574
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002575def Test_echoerr_cmd()
Bram Moolenaar7de62622021-08-07 15:05:47 +02002576 var local = 'local'
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002577 try
Bram Moolenaar7de62622021-08-07 15:05:47 +02002578 echoerr 'something' local 'wrong' # comment
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002579 catch
Bram Moolenaar7de62622021-08-07 15:05:47 +02002580 assert_match('something local wrong', v:exception)
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002581 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002582enddef
2583
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002584def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002585 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002586 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002587 vim9script
2588 try
2589 echoerr 'this'
2590 .. ' is ' ..
2591 'wrong'
2592 catch
2593 assert_match('this is wrong', v:exception)
2594 endtry
2595 END
2596 CheckScriptSuccess(lines)
2597enddef
2598
Bram Moolenaar7de62622021-08-07 15:05:47 +02002599def Test_echoconsole_cmd()
2600 var local = 'local'
2601 echoconsole 'something' local # comment
2602 # output goes anywhere
2603enddef
2604
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002605def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002606 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002607 vim9script
2608 new
2609 for var in range(0, 3)
2610 append(line('$'), var)
2611 endfor
2612 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2613 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01002614
2615 var result = ''
2616 for i in [1, 2, 3]
2617 var loop = ' loop ' .. i
2618 result ..= loop
2619 endfor
2620 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002621 END
2622 writefile(lines, 'Xvim9for.vim')
2623 source Xvim9for.vim
2624 delete('Xvim9for.vim')
2625enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626
rbtnnbebf0692021-08-21 17:26:50 +02002627def Test_for_skipped_block()
2628 # test skipped blocks at outside of function
2629 var lines =<< trim END
2630 var result = []
2631 if true
2632 for n in [1, 2]
2633 result += [n]
2634 endfor
2635 else
2636 for n in [3, 4]
2637 result += [n]
2638 endfor
2639 endif
2640 assert_equal([1, 2], result)
2641
2642 result = []
2643 if false
2644 for n in [1, 2]
2645 result += [n]
2646 endfor
2647 else
2648 for n in [3, 4]
2649 result += [n]
2650 endfor
2651 endif
2652 assert_equal([3, 4], result)
2653 END
2654 CheckDefAndScriptSuccess(lines)
2655
2656 # test skipped blocks at inside of function
2657 lines =<< trim END
2658 def DefTrue()
2659 var result = []
2660 if true
2661 for n in [1, 2]
2662 result += [n]
2663 endfor
2664 else
2665 for n in [3, 4]
2666 result += [n]
2667 endfor
2668 endif
2669 assert_equal([1, 2], result)
2670 enddef
2671 DefTrue()
2672
2673 def DefFalse()
2674 var result = []
2675 if false
2676 for n in [1, 2]
2677 result += [n]
2678 endfor
2679 else
2680 for n in [3, 4]
2681 result += [n]
2682 endfor
2683 endif
2684 assert_equal([3, 4], result)
2685 enddef
2686 DefFalse()
2687 END
2688 CheckDefAndScriptSuccess(lines)
2689enddef
2690
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002691def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002692 var lines =<< trim END
2693 var result = ''
2694 for cnt in range(7)
2695 if cnt == 4
2696 break
2697 endif
2698 if cnt == 2
2699 continue
2700 endif
2701 result ..= cnt .. '_'
2702 endfor
2703 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02002704
Bram Moolenaarf2253962021-04-13 20:53:13 +02002705 var concat = ''
2706 for str in eval('["one", "two"]')
2707 concat ..= str
2708 endfor
2709 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002710
Bram Moolenaarf2253962021-04-13 20:53:13 +02002711 var total = 0
2712 for nr in
2713 [1, 2, 3]
2714 total += nr
2715 endfor
2716 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002717
Bram Moolenaarf2253962021-04-13 20:53:13 +02002718 total = 0
2719 for nr
2720 in [1, 2, 3]
2721 total += nr
2722 endfor
2723 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002724
Bram Moolenaarf2253962021-04-13 20:53:13 +02002725 total = 0
2726 for nr
2727 in
2728 [1, 2, 3]
2729 total += nr
2730 endfor
2731 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01002732
Bram Moolenaara3589a02021-04-14 13:30:46 +02002733 # with type
2734 total = 0
2735 for n: number in [1, 2, 3]
2736 total += n
2737 endfor
2738 assert_equal(6, total)
2739
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002740 var chars = ''
2741 for s: string in 'foobar'
2742 chars ..= s
2743 endfor
2744 assert_equal('foobar', chars)
2745
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02002746 chars = ''
2747 for x: string in {a: 'a', b: 'b'}->values()
2748 chars ..= x
2749 endfor
2750 assert_equal('ab', chars)
2751
Bram Moolenaara3589a02021-04-14 13:30:46 +02002752 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02002753 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02002754 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2755 res ..= n .. s
2756 endfor
2757 assert_equal('1a2b', res)
2758
Bram Moolenaar444d8782021-06-26 12:40:56 +02002759 # unpack with one var
2760 var reslist = []
2761 for [x] in [['aaa'], ['bbb']]
2762 reslist->add(x)
2763 endfor
2764 assert_equal(['aaa', 'bbb'], reslist)
2765
Bram Moolenaara3589a02021-04-14 13:30:46 +02002766 # loop over string
2767 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02002768 for c in 'aéc̀d'
2769 res ..= c .. '-'
2770 endfor
2771 assert_equal('a-é-c̀-d-', res)
2772
2773 res = ''
2774 for c in ''
2775 res ..= c .. '-'
2776 endfor
2777 assert_equal('', res)
2778
2779 res = ''
2780 for c in test_null_string()
2781 res ..= c .. '-'
2782 endfor
2783 assert_equal('', res)
2784
2785 var foo: list<dict<any>> = [
2786 {a: 'Cat'}
2787 ]
2788 for dd in foo
2789 dd.counter = 12
2790 endfor
2791 assert_equal([{a: 'Cat', counter: 12}], foo)
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002792
2793 reslist = []
2794 for _ in range(3)
2795 reslist->add('x')
2796 endfor
2797 assert_equal(['x', 'x', 'x'], reslist)
Bram Moolenaarf2253962021-04-13 20:53:13 +02002798 END
2799 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002800enddef
2801
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02002802def Test_for_loop_with_closure()
2803 var lines =<< trim END
2804 var flist: list<func>
2805 for i in range(5)
2806 var inloop = i
2807 flist[i] = () => inloop
2808 endfor
2809 for i in range(5)
2810 assert_equal(4, flist[i]())
2811 endfor
2812 END
2813 CheckDefAndScriptSuccess(lines)
2814
2815 lines =<< trim END
2816 var flist: list<func>
2817 for i in range(5)
2818 var inloop = i
2819 flist[i] = () => {
2820 return inloop
2821 }
2822 endfor
2823 for i in range(5)
2824 assert_equal(4, flist[i]())
2825 endfor
2826 END
2827 CheckDefAndScriptSuccess(lines)
2828enddef
2829
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002830def Test_for_loop_fails()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002831 CheckDefAndScriptFailure2(['for '], 'E1097:', 'E690:')
2832 CheckDefAndScriptFailure2(['for x'], 'E1097:', 'E690:')
2833 CheckDefAndScriptFailure2(['for x in'], 'E1097:', 'E15:')
2834 CheckDefAndScriptFailure(['for # in range(5)'], 'E690:')
2835 CheckDefAndScriptFailure(['for i In range(5)'], 'E690:')
2836 CheckDefAndScriptFailure2(['var x = 5', 'for x in range(5)', 'endfor'], 'E1017:', 'E1041:')
Bram Moolenaard4ab8072021-07-08 19:22:12 +02002837 CheckScriptFailure(['vim9script', 'var x = 5', 'for x in range(5)', '# comment', 'endfor'], 'E1041:', 3)
Bram Moolenaar822ba242020-05-24 23:00:18 +02002838 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002839 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02002840 CheckDefFailure(['for i in xxx'], 'E1001:')
2841 CheckDefFailure(['endfor'], 'E588:')
2842 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002843
2844 # wrong type detected at compile time
2845 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2846
2847 # wrong type detected at runtime
2848 g:adict = {a: 1}
2849 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2850 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002851
2852 var lines =<< trim END
2853 var d: list<dict<any>> = [{a: 0}]
2854 for e in d
2855 e = {a: 0, b: ''}
2856 endfor
2857 END
2858 CheckDefAndScriptFailure2(lines, 'E1018:', 'E46:', 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002859
2860 lines =<< trim END
2861 for nr: number in ['foo']
2862 endfor
2863 END
2864 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaar404557e2021-07-05 21:41:48 +02002865
2866 lines =<< trim END
2867 for n : number in [1, 2]
2868 echo n
2869 endfor
2870 END
2871 CheckDefAndScriptFailure(lines, 'E1059:', 1)
Bram Moolenaarefc5db52021-07-07 21:21:30 +02002872
2873 lines =<< trim END
2874 var d: dict<number> = {a: 1, b: 2}
2875 for [k: job, v: job] in d->items()
2876 echo k v
2877 endfor
2878 END
2879 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected job but got string', 2)
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002880
2881 lines =<< trim END
2882 var i = 0
2883 for i in [1, 2, 3]
2884 echo i
2885 endfor
2886 END
2887 CheckDefExecAndScriptFailure2(lines, 'E1017:', 'E1041:')
2888
2889 lines =<< trim END
2890 var l = [0]
2891 for l[0] in [1, 2, 3]
2892 echo l[0]
2893 endfor
2894 END
2895 CheckDefExecAndScriptFailure2(lines, 'E461:', 'E1017:')
2896
2897 lines =<< trim END
2898 var d = {x: 0}
2899 for d.x in [1, 2, 3]
2900 echo d.x
2901 endfor
2902 END
2903 CheckDefExecAndScriptFailure2(lines, 'E461:', 'E1017:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002904enddef
2905
Bram Moolenaarea870692020-12-02 14:24:30 +01002906def Test_for_loop_script_var()
2907 # cannot use s:var in a :def function
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002908 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E461:')
Bram Moolenaarea870692020-12-02 14:24:30 +01002909
2910 # can use s:var in Vim9 script, with or without s:
2911 var lines =<< trim END
2912 vim9script
2913 var total = 0
2914 for s:var in [1, 2, 3]
2915 total += s:var
2916 endfor
2917 assert_equal(6, total)
2918
2919 total = 0
2920 for var in [1, 2, 3]
2921 total += var
2922 endfor
2923 assert_equal(6, total)
2924 END
2925enddef
2926
Bram Moolenaar792f7862020-11-23 08:31:18 +01002927def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01002928 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002929 var result = []
2930 for [v1, v2] in [[1, 2], [3, 4]]
2931 result->add(v1)
2932 result->add(v2)
2933 endfor
2934 assert_equal([1, 2, 3, 4], result)
2935
2936 result = []
2937 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2938 result->add(v1)
2939 result->add(v2)
2940 result->add(v3)
2941 endfor
2942 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
2943
2944 result = []
2945 for [&ts, &sw] in [[1, 2], [3, 4]]
2946 result->add(&ts)
2947 result->add(&sw)
2948 endfor
2949 assert_equal([1, 2, 3, 4], result)
2950
2951 var slist: list<string>
2952 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
2953 slist->add($LOOPVAR)
2954 slist->add(@r)
2955 slist->add(v:errmsg)
2956 endfor
2957 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
2958
2959 slist = []
2960 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
2961 slist->add(g:globalvar)
2962 slist->add(b:bufvar)
2963 slist->add(w:winvar)
2964 slist->add(t:tabvar)
2965 endfor
2966 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01002967 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaarb777da92021-05-22 21:40:39 +02002968
2969 var res = []
2970 for [_, n, _] in [[1, 2, 3], [4, 5, 6]]
2971 res->add(n)
2972 endfor
2973 assert_equal([2, 5], res)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002974 END
2975 CheckDefAndScriptSuccess(lines)
2976
2977 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01002978 for [v1, v2] in [[1, 2, 3], [3, 4]]
2979 echo v1 v2
2980 endfor
2981 END
2982 CheckDefExecFailure(lines, 'E710:', 1)
2983
2984 lines =<< trim END
2985 for [v1, v2] in [[1], [3, 4]]
2986 echo v1 v2
2987 endfor
2988 END
2989 CheckDefExecFailure(lines, 'E711:', 1)
2990
2991 lines =<< trim END
2992 for [v1, v1] in [[1, 2], [3, 4]]
2993 echo v1
2994 endfor
2995 END
2996 CheckDefExecFailure(lines, 'E1017:', 1)
2997enddef
2998
Bram Moolenaarc150c092021-02-13 15:02:46 +01002999def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02003000 var lines =<< trim END
3001 var looped = 0
3002 var cleanup = 0
3003 for i in range(3)
3004 looped += 1
3005 try
3006 eval [][0]
3007 catch
3008 continue
3009 finally
3010 cleanup += 1
3011 endtry
3012 endfor
3013 assert_equal(3, looped)
3014 assert_equal(3, cleanup)
3015 END
3016 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003017enddef
3018
rbtnnd895b1d2021-08-20 20:54:25 +02003019def Test_while_skipped_block()
3020 # test skipped blocks at outside of function
3021 var lines =<< trim END
3022 var result = []
3023 var n = 0
3024 if true
3025 n = 1
3026 while n < 3
3027 result += [n]
3028 n += 1
3029 endwhile
3030 else
3031 n = 3
3032 while n < 5
3033 result += [n]
3034 n += 1
3035 endwhile
3036 endif
3037 assert_equal([1, 2], result)
3038
3039 result = []
3040 if false
3041 n = 1
3042 while n < 3
3043 result += [n]
3044 n += 1
3045 endwhile
3046 else
3047 n = 3
3048 while n < 5
3049 result += [n]
3050 n += 1
3051 endwhile
3052 endif
3053 assert_equal([3, 4], result)
3054 END
3055 CheckDefAndScriptSuccess(lines)
3056
3057 # test skipped blocks at inside of function
3058 lines =<< trim END
3059 def DefTrue()
3060 var result = []
3061 var n = 0
3062 if true
3063 n = 1
3064 while n < 3
3065 result += [n]
3066 n += 1
3067 endwhile
3068 else
3069 n = 3
3070 while n < 5
3071 result += [n]
3072 n += 1
3073 endwhile
3074 endif
3075 assert_equal([1, 2], result)
3076 enddef
3077 DefTrue()
3078
3079 def DefFalse()
3080 var result = []
3081 var n = 0
3082 if false
3083 n = 1
3084 while n < 3
3085 result += [n]
3086 n += 1
3087 endwhile
3088 else
3089 n = 3
3090 while n < 5
3091 result += [n]
3092 n += 1
3093 endwhile
3094 endif
3095 assert_equal([3, 4], result)
3096 enddef
3097 DefFalse()
3098 END
3099 CheckDefAndScriptSuccess(lines)
3100enddef
3101
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003102def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003103 var result = ''
3104 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003105 while cnt < 555
3106 if cnt == 3
3107 break
3108 endif
3109 cnt += 1
3110 if cnt == 2
3111 continue
3112 endif
3113 result ..= cnt .. '_'
3114 endwhile
3115 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01003116
3117 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01003118 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01003119 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003120enddef
3121
Bram Moolenaar7a53f292021-11-22 18:31:02 +00003122def Test_while_loop_in_script()
3123 var lines =<< trim END
3124 vim9script
3125 var result = ''
3126 var cnt = 0
3127 while cnt < 3
3128 var s = 'v' .. cnt
3129 result ..= s
3130 cnt += 1
3131 endwhile
3132 assert_equal('v0v1v2', result)
3133 END
3134 CheckScriptSuccess(lines)
3135enddef
3136
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003137def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02003138 CheckDefFailure(['while xxx'], 'E1001:')
3139 CheckDefFailure(['endwhile'], 'E588:')
3140 CheckDefFailure(['continue'], 'E586:')
3141 CheckDefFailure(['if true', 'continue'], 'E586:')
3142 CheckDefFailure(['break'], 'E587:')
3143 CheckDefFailure(['if true', 'break'], 'E587:')
3144 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01003145
3146 var lines =<< trim END
3147 var s = ''
3148 while s = ''
3149 endwhile
3150 END
3151 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003152enddef
3153
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003154def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003155 var caught = false
3156 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003157 try
3158 while 1
3159 x += 1
3160 if x == 100
3161 feedkeys("\<C-C>", 'Lt')
3162 endif
3163 endwhile
3164 catch
3165 caught = true
3166 assert_equal(100, x)
3167 endtry
3168 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003169 # consume the CTRL-C
3170 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003171enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01003172
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003173def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003174 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003175 'one',
3176 'two',
3177 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003178 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003179 assert_equal(['one', 'two', 'three'], mylist)
3180
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003181 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003182 ['one']: 1,
3183 ['two']: 2,
3184 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003185 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003186 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01003187 assert_equal({one: 1, two: 2, three: 3}, mydict)
3188 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003189 one: 1, # comment
3190 two: # comment
3191 2, # comment
3192 three: 3 # comment
3193 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003194 assert_equal({one: 1, two: 2, three: 3}, mydict)
3195 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003196 one: 1,
3197 two:
3198 2,
3199 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003200 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003201 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02003202
3203 assert_equal(
3204 ['one', 'two', 'three'],
3205 split('one two three')
3206 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003207enddef
3208
Bram Moolenaar7a092242020-04-16 22:10:49 +02003209def Test_vim9_comment()
3210 CheckScriptSuccess([
3211 'vim9script',
3212 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01003213 '#something',
3214 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003215 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01003216
3217 split Xfile
3218 CheckScriptSuccess([
3219 'vim9script',
3220 'edit #something',
3221 ])
3222 CheckScriptSuccess([
3223 'vim9script',
3224 'edit #{something',
3225 ])
3226 close
3227
Bram Moolenaar7a092242020-04-16 22:10:49 +02003228 CheckScriptFailure([
3229 'vim9script',
3230 ':# something',
3231 ], 'E488:')
3232 CheckScriptFailure([
3233 '# something',
3234 ], 'E488:')
3235 CheckScriptFailure([
3236 ':# something',
3237 ], 'E488:')
3238
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003239 { # block start
3240 } # block end
3241 CheckDefFailure([
3242 '{# comment',
3243 ], 'E488:')
3244 CheckDefFailure([
3245 '{',
3246 '}# comment',
3247 ], 'E488:')
3248
3249 echo "yes" # comment
3250 CheckDefFailure([
3251 'echo "yes"# comment',
3252 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003253 CheckScriptSuccess([
3254 'vim9script',
3255 'echo "yes" # something',
3256 ])
3257 CheckScriptFailure([
3258 'vim9script',
3259 'echo "yes"# something',
3260 ], 'E121:')
3261 CheckScriptFailure([
3262 'vim9script',
3263 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003264 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003265 CheckScriptFailure([
3266 'echo "yes" # something',
3267 ], 'E121:')
3268
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003269 exe "echo" # comment
3270 CheckDefFailure([
3271 'exe "echo"# comment',
3272 ], 'E488:')
3273 CheckScriptSuccess([
3274 'vim9script',
3275 'exe "echo" # something',
3276 ])
3277 CheckScriptFailure([
3278 'vim9script',
3279 'exe "echo"# something',
3280 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003281 CheckScriptFailure([
3282 'vim9script',
3283 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003284 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003285 CheckScriptFailure([
3286 'exe "echo" # something',
3287 ], 'E121:')
3288
Bram Moolenaar7a092242020-04-16 22:10:49 +02003289 CheckDefFailure([
3290 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003291 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003292 'catch',
3293 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003294 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003295 CheckScriptFailure([
3296 'vim9script',
3297 'try# comment',
3298 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003299 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003300 CheckDefFailure([
3301 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003302 ' throw#comment',
3303 'catch',
3304 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003305 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003306 CheckDefFailure([
3307 'try',
3308 ' throw "yes"#comment',
3309 'catch',
3310 'endtry',
3311 ], 'E488:')
3312 CheckDefFailure([
3313 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003314 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003315 'catch# comment',
3316 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003317 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003318 CheckScriptFailure([
3319 'vim9script',
3320 'try',
3321 ' echo "yes"',
3322 'catch# comment',
3323 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003324 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003325 CheckDefFailure([
3326 'try',
3327 ' echo "yes"',
3328 'catch /pat/# comment',
3329 'endtry',
3330 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003331 CheckDefFailure([
3332 'try',
3333 'echo "yes"',
3334 'catch',
3335 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003336 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003337 CheckScriptFailure([
3338 'vim9script',
3339 'try',
3340 ' echo "yes"',
3341 'catch',
3342 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003343 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003344
3345 CheckScriptSuccess([
3346 'vim9script',
3347 'hi # comment',
3348 ])
3349 CheckScriptFailure([
3350 'vim9script',
3351 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003352 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003353 CheckScriptSuccess([
3354 'vim9script',
3355 'hi Search # comment',
3356 ])
3357 CheckScriptFailure([
3358 'vim9script',
3359 'hi Search# comment',
3360 ], 'E416:')
3361 CheckScriptSuccess([
3362 'vim9script',
3363 'hi link This Search # comment',
3364 ])
3365 CheckScriptFailure([
3366 'vim9script',
3367 'hi link This That# comment',
3368 ], 'E413:')
3369 CheckScriptSuccess([
3370 'vim9script',
3371 'hi clear This # comment',
3372 'hi clear # comment',
3373 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003374 # not tested, because it doesn't give an error but a warning:
3375 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02003376 CheckScriptFailure([
3377 'vim9script',
3378 'hi clear# comment',
3379 ], 'E416:')
3380
3381 CheckScriptSuccess([
3382 'vim9script',
3383 'hi Group term=bold',
3384 'match Group /todo/ # comment',
3385 ])
3386 CheckScriptFailure([
3387 'vim9script',
3388 'hi Group term=bold',
3389 'match Group /todo/# comment',
3390 ], 'E488:')
3391 CheckScriptSuccess([
3392 'vim9script',
3393 'match # comment',
3394 ])
3395 CheckScriptFailure([
3396 'vim9script',
3397 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003398 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003399 CheckScriptSuccess([
3400 'vim9script',
3401 'match none # comment',
3402 ])
3403 CheckScriptFailure([
3404 'vim9script',
3405 'match none# comment',
3406 ], 'E475:')
3407
3408 CheckScriptSuccess([
3409 'vim9script',
3410 'menutrans clear # comment',
3411 ])
3412 CheckScriptFailure([
3413 'vim9script',
3414 'menutrans clear# comment text',
3415 ], 'E474:')
3416
3417 CheckScriptSuccess([
3418 'vim9script',
3419 'syntax clear # comment',
3420 ])
3421 CheckScriptFailure([
3422 'vim9script',
3423 'syntax clear# comment text',
3424 ], 'E28:')
3425 CheckScriptSuccess([
3426 'vim9script',
3427 'syntax keyword Word some',
3428 'syntax clear Word # comment',
3429 ])
3430 CheckScriptFailure([
3431 'vim9script',
3432 'syntax keyword Word some',
3433 'syntax clear Word# comment text',
3434 ], 'E28:')
3435
3436 CheckScriptSuccess([
3437 'vim9script',
3438 'syntax list # comment',
3439 ])
3440 CheckScriptFailure([
3441 'vim9script',
3442 'syntax list# comment text',
3443 ], 'E28:')
3444
3445 CheckScriptSuccess([
3446 'vim9script',
3447 'syntax match Word /pat/ oneline # comment',
3448 ])
3449 CheckScriptFailure([
3450 'vim9script',
3451 'syntax match Word /pat/ oneline# comment',
3452 ], 'E475:')
3453
3454 CheckScriptSuccess([
3455 'vim9script',
3456 'syntax keyword Word word # comm[ent',
3457 ])
3458 CheckScriptFailure([
3459 'vim9script',
3460 'syntax keyword Word word# comm[ent',
3461 ], 'E789:')
3462
3463 CheckScriptSuccess([
3464 'vim9script',
3465 'syntax match Word /pat/ # comment',
3466 ])
3467 CheckScriptFailure([
3468 'vim9script',
3469 'syntax match Word /pat/# comment',
3470 ], 'E402:')
3471
3472 CheckScriptSuccess([
3473 'vim9script',
3474 'syntax match Word /pat/ contains=Something # comment',
3475 ])
3476 CheckScriptFailure([
3477 'vim9script',
3478 'syntax match Word /pat/ contains=Something# comment',
3479 ], 'E475:')
3480 CheckScriptFailure([
3481 'vim9script',
3482 'syntax match Word /pat/ contains= # comment',
3483 ], 'E406:')
3484 CheckScriptFailure([
3485 'vim9script',
3486 'syntax match Word /pat/ contains=# comment',
3487 ], 'E475:')
3488
3489 CheckScriptSuccess([
3490 'vim9script',
3491 'syntax region Word start=/pat/ end=/pat/ # comment',
3492 ])
3493 CheckScriptFailure([
3494 'vim9script',
3495 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02003496 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003497
3498 CheckScriptSuccess([
3499 'vim9script',
3500 'syntax sync # comment',
3501 ])
3502 CheckScriptFailure([
3503 'vim9script',
3504 'syntax sync# comment',
3505 ], 'E404:')
3506 CheckScriptSuccess([
3507 'vim9script',
3508 'syntax sync ccomment # comment',
3509 ])
3510 CheckScriptFailure([
3511 'vim9script',
3512 'syntax sync ccomment# comment',
3513 ], 'E404:')
3514
3515 CheckScriptSuccess([
3516 'vim9script',
3517 'syntax cluster Some contains=Word # comment',
3518 ])
3519 CheckScriptFailure([
3520 'vim9script',
3521 'syntax cluster Some contains=Word# comment',
3522 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003523
3524 CheckScriptSuccess([
3525 'vim9script',
3526 'command Echo echo # comment',
3527 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003528 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003529 ])
3530 CheckScriptFailure([
3531 'vim9script',
3532 'command Echo echo# comment',
3533 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003534 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003535 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003536
3537 var curdir = getcwd()
3538 CheckScriptSuccess([
3539 'command Echo cd " comment',
3540 'Echo',
3541 'delcommand Echo',
3542 ])
3543 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01003544 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003545 'command Echo cd # comment',
3546 'Echo',
3547 'delcommand Echo',
3548 ])
3549 CheckScriptFailure([
3550 'vim9script',
3551 'command Echo cd " comment',
3552 'Echo',
3553 ], 'E344:')
3554 delcommand Echo
3555 chdir(curdir)
3556
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003557 CheckScriptFailure([
3558 'vim9script',
3559 'command Echo# comment',
3560 ], 'E182:')
3561 CheckScriptFailure([
3562 'vim9script',
3563 'command Echo echo',
3564 'command Echo# comment',
3565 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003566 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003567
3568 CheckScriptSuccess([
3569 'vim9script',
3570 'function # comment',
3571 ])
3572 CheckScriptFailure([
3573 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02003574 'function " comment',
3575 ], 'E129:')
3576 CheckScriptFailure([
3577 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003578 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003579 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003580 CheckScriptSuccess([
3581 'vim9script',
3582 'function CheckScriptSuccess # comment',
3583 ])
3584 CheckScriptFailure([
3585 'vim9script',
3586 'function CheckScriptSuccess# comment',
3587 ], 'E488:')
3588
3589 CheckScriptSuccess([
3590 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003591 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003592 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003593 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003594 ])
3595 CheckScriptFailure([
3596 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003597 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003598 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003599 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003600 ], 'E488:')
3601
3602 CheckScriptSuccess([
3603 'vim9script',
3604 'call execute("ls") # comment',
3605 ])
3606 CheckScriptFailure([
3607 'vim9script',
3608 'call execute("ls")# comment',
3609 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02003610
3611 CheckScriptFailure([
3612 'def Test() " comment',
3613 'enddef',
3614 ], 'E488:')
3615 CheckScriptFailure([
3616 'vim9script',
3617 'def Test() " comment',
3618 'enddef',
3619 ], 'E488:')
3620
3621 CheckScriptSuccess([
3622 'func Test() " comment',
3623 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003624 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02003625 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02003626 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02003627 'vim9script',
3628 'func Test() " comment',
3629 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02003630 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02003631
3632 CheckScriptSuccess([
3633 'def Test() # comment',
3634 'enddef',
3635 ])
3636 CheckScriptFailure([
3637 'func Test() # comment',
3638 'endfunc',
3639 ], 'E488:')
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003640
3641 var lines =<< trim END
3642 vim9script
3643 syn region Text
3644 \ start='foo'
3645 #\ comment
3646 \ end='bar'
Bram Moolenaar5072b472021-06-03 21:56:10 +02003647 syn region Text start='foo'
3648 #\ comment
3649 \ end='bar'
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003650 END
3651 CheckScriptSuccess(lines)
3652
3653 lines =<< trim END
3654 vim9script
3655 syn region Text
3656 \ start='foo'
3657 "\ comment
3658 \ end='bar'
3659 END
3660 CheckScriptFailure(lines, 'E399:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003661enddef
3662
3663def Test_vim9_comment_gui()
3664 CheckCanRunGui
3665
3666 CheckScriptFailure([
3667 'vim9script',
3668 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003669 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003670 CheckScriptFailure([
3671 'vim9script',
3672 'gui -f#comment'
Bram Moolenaarb8bd2e62021-08-21 17:13:14 +02003673 ], 'E194:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003674enddef
3675
Bram Moolenaara26b9702020-04-18 19:53:28 +02003676def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02003677 au TabEnter *.vim g:entered = 1
3678 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02003679
3680 edit test.vim
3681 doautocmd TabEnter #comment
3682 assert_equal(1, g:entered)
3683
3684 doautocmd TabEnter f.x
3685 assert_equal(2, g:entered)
3686
3687 g:entered = 0
3688 doautocmd TabEnter f.x #comment
3689 assert_equal(2, g:entered)
3690
3691 assert_fails('doautocmd Syntax#comment', 'E216:')
3692
3693 au! TabEnter
3694 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003695
3696 CheckScriptSuccess([
3697 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02003698 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003699 'b:var = 456',
3700 'w:var = 777',
3701 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003702 'unlet g:var w:var # something',
3703 ])
3704
3705 CheckScriptFailure([
3706 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003707 'let var = 123',
3708 ], 'E1126: Cannot use :let in Vim9 script')
3709
3710 CheckScriptFailure([
3711 'vim9script',
3712 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003713 ], 'E1016: Cannot declare a global variable:')
3714
3715 CheckScriptFailure([
3716 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003717 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003718 ], 'E1016: Cannot declare a buffer variable:')
3719
3720 CheckScriptFailure([
3721 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003722 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003723 ], 'E1016: Cannot declare a window variable:')
3724
3725 CheckScriptFailure([
3726 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003727 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003728 ], 'E1016: Cannot declare a tab variable:')
3729
3730 CheckScriptFailure([
3731 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003732 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003733 ], 'E1016: Cannot declare a v: variable:')
3734
3735 CheckScriptFailure([
3736 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003737 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003738 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02003739
3740 CheckScriptFailure([
3741 'vim9script',
3742 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003743 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003744 ], 'E108:')
3745
3746 CheckScriptFailure([
3747 'let g:var = 123',
3748 'unlet g:var # something',
3749 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003750
3751 CheckScriptSuccess([
3752 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003753 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003754 ' echo "yes"',
3755 'elseif 2 #comment',
3756 ' echo "no"',
3757 'endif',
3758 ])
3759
3760 CheckScriptFailure([
3761 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003762 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003763 ' echo "yes"',
3764 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003765 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003766
3767 CheckScriptFailure([
3768 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003769 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003770 ' echo "yes"',
3771 'elseif 2#comment',
3772 ' echo "no"',
3773 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003774 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003775
3776 CheckScriptSuccess([
3777 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003778 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003779 ])
3780
3781 CheckScriptFailure([
3782 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003783 'var v = 1# comment6',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003784 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003785
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003786 CheckScriptSuccess([
3787 'vim9script',
3788 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003789 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003790 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003791 'dsearch /pat/ #comment',
3792 'bwipe!',
3793 ])
3794
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003795 CheckScriptFailure([
3796 'vim9script',
3797 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003798 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003799 ':$',
3800 'dsearch /pat/#comment',
3801 'bwipe!',
3802 ], 'E488:')
3803
3804 CheckScriptFailure([
3805 'vim9script',
3806 'func! SomeFunc()',
3807 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02003808enddef
3809
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003810def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003811 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003812 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02003813 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003814 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02003815 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003816 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02003817 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003818 END
3819 writefile(lines, 'Xfinished')
3820 source Xfinished
3821 assert_equal('two', g:res)
3822
3823 unlet g:res
3824 delete('Xfinished')
3825enddef
3826
Bram Moolenaara5d00772020-05-14 23:20:55 +02003827def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003828 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02003829 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02003830 def GetValue(): string
3831 return theVal
3832 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003833 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02003834 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02003835 theVal = 'else'
3836 g:laterVal = GetValue()
3837 END
3838 writefile(lines, 'Xforward')
3839 source Xforward
3840 assert_equal('something', g:initVal)
3841 assert_equal('else', g:laterVal)
3842
3843 unlet g:initVal
3844 unlet g:laterVal
3845 delete('Xforward')
3846enddef
3847
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003848def Test_source_vim9_from_legacy()
Bram Moolenaara6294952020-12-27 13:39:50 +01003849 var vim9_lines =<< trim END
3850 vim9script
3851 var local = 'local'
3852 g:global = 'global'
3853 export var exported = 'exported'
3854 export def GetText(): string
3855 return 'text'
3856 enddef
3857 END
3858 writefile(vim9_lines, 'Xvim9_script.vim')
3859
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003860 var legacy_lines =<< trim END
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003861 source Xvim9_script.vim
3862
3863 call assert_false(exists('local'))
3864 call assert_false(exists('exported'))
3865 call assert_false(exists('s:exported'))
3866 call assert_equal('global', global)
3867 call assert_equal('global', g:global)
3868
3869 " imported variable becomes script-local
3870 import exported from './Xvim9_script.vim'
3871 call assert_equal('exported', s:exported)
3872 call assert_false(exists('exported'))
3873
3874 " imported function becomes script-local
3875 import GetText from './Xvim9_script.vim'
3876 call assert_equal('text', s:GetText())
3877 call assert_false(exists('*GetText'))
3878 END
3879 writefile(legacy_lines, 'Xlegacy_script.vim')
3880
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003881 source Xlegacy_script.vim
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003882 assert_equal('global', g:global)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003883 unlet g:global
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003884
3885 delete('Xlegacy_script.vim')
3886 delete('Xvim9_script.vim')
3887enddef
Bram Moolenaara5d00772020-05-14 23:20:55 +02003888
Bram Moolenaare535db82021-03-31 21:07:24 +02003889def Test_declare_script_in_func()
3890 var lines =<< trim END
3891 vim9script
3892 func Declare()
3893 let s:local = 123
3894 endfunc
3895 Declare()
3896 assert_equal(123, local)
3897
3898 var error: string
3899 try
3900 local = 'asdf'
3901 catch
3902 error = v:exception
3903 endtry
3904 assert_match('E1012: Type mismatch; expected number but got string', error)
3905
3906 lockvar local
3907 try
3908 local = 999
3909 catch
3910 error = v:exception
3911 endtry
3912 assert_match('E741: Value is locked: local', error)
3913 END
3914 CheckScriptSuccess(lines)
3915enddef
3916
3917
Bram Moolenaar7d699702020-08-14 20:52:28 +02003918func Test_vim9script_not_global()
3919 " check that items defined in Vim9 script are script-local, not global
3920 let vim9lines =<< trim END
3921 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003922 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02003923 func TheFunc()
3924 echo 'local'
3925 endfunc
3926 def DefFunc()
3927 echo 'local'
3928 enddef
3929 END
3930 call writefile(vim9lines, 'Xvim9script.vim')
3931 source Xvim9script.vim
3932 try
3933 echo g:var
3934 assert_report('did not fail')
3935 catch /E121:/
3936 " caught
3937 endtry
3938 try
3939 call TheFunc()
3940 assert_report('did not fail')
3941 catch /E117:/
3942 " caught
3943 endtry
3944 try
3945 call DefFunc()
3946 assert_report('did not fail')
3947 catch /E117:/
3948 " caught
3949 endtry
3950
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003951 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02003952endfunc
3953
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02003954def Test_vim9_copen()
3955 # this was giving an error for setting w:quickfix_title
3956 copen
3957 quit
3958enddef
3959
Bram Moolenaar03290b82020-12-19 16:30:44 +01003960" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003961def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003962 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003963 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01003964 def some#gettest(): string
3965 return 'test'
3966 enddef
3967 g:some#name = 'name'
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003968 g:some#dict = {key: 'value'}
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003969
3970 def some#varargs(a1: string, ...l: list<string>): string
3971 return a1 .. l[0] .. l[1]
3972 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01003973 END
3974
3975 mkdir('Xdir/autoload', 'p')
3976 writefile(lines, 'Xdir/autoload/some.vim')
3977 var save_rtp = &rtp
3978 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3979
3980 assert_equal('test', g:some#gettest())
3981 assert_equal('name', g:some#name)
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003982 assert_equal('value', g:some#dict.key)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003983 g:some#other = 'other'
3984 assert_equal('other', g:some#other)
3985
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003986 assert_equal('abc', some#varargs('a', 'b', 'c'))
3987
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003988 # upper case script name works
3989 lines =<< trim END
3990 vim9script
3991 def Other#getOther(): string
3992 return 'other'
3993 enddef
3994 END
3995 writefile(lines, 'Xdir/autoload/Other.vim')
3996 assert_equal('other', g:Other#getOther())
3997
Bram Moolenaar03290b82020-12-19 16:30:44 +01003998 delete('Xdir', 'rf')
3999 &rtp = save_rtp
4000enddef
4001
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00004002" test disassembling an auto-loaded function starting with "debug"
4003def Test_vim9_autoload_disass()
4004 mkdir('Xdir/autoload', 'p')
4005 var save_rtp = &rtp
4006 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4007
4008 var lines =<< trim END
4009 vim9script
4010 def debugit#test(): string
4011 return 'debug'
4012 enddef
4013 END
4014 writefile(lines, 'Xdir/autoload/debugit.vim')
4015
4016 lines =<< trim END
4017 vim9script
4018 def profileit#test(): string
4019 return 'profile'
4020 enddef
4021 END
4022 writefile(lines, 'Xdir/autoload/profileit.vim')
4023
4024 lines =<< trim END
4025 vim9script
4026 assert_equal('debug', debugit#test())
4027 disass debugit#test
4028 assert_equal('profile', profileit#test())
4029 disass profileit#test
4030 END
4031 CheckScriptSuccess(lines)
4032
4033 delete('Xdir', 'rf')
4034 &rtp = save_rtp
4035enddef
4036
Bram Moolenaar03290b82020-12-19 16:30:44 +01004037" test using a vim9script that is auto-loaded from an autocmd
4038def Test_vim9_aucmd_autoload()
4039 var lines =<< trim END
4040 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004041 def foo#test()
4042 echomsg getreg('"')
4043 enddef
4044 END
4045
4046 mkdir('Xdir/autoload', 'p')
4047 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004048 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004049 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4050 augroup test
4051 autocmd TextYankPost * call foo#test()
4052 augroup END
4053
4054 normal Y
4055
4056 augroup test
4057 autocmd!
4058 augroup END
4059 delete('Xdir', 'rf')
4060 &rtp = save_rtp
4061enddef
4062
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004063" This was causing a crash because suppress_errthrow wasn't reset.
4064def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004065 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004066 vim9script
4067 def crash#func()
4068 try
4069 for x in List()
4070 endfor
4071 catch
4072 endtry
4073 g:ok = true
4074 enddef
4075 fu List()
4076 invalid
4077 endfu
4078 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01004079 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004080 catch /wontmatch/
4081 endtry
4082 END
4083 call mkdir('Xruntime/autoload', 'p')
4084 call writefile(lines, 'Xruntime/autoload/crash.vim')
4085
4086 # run in a separate Vim to avoid the side effects of assert_fails()
4087 lines =<< trim END
4088 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
4089 call crash#func()
4090 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004091 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004092 END
4093 writefile(lines, 'Xscript')
4094 RunVim([], [], '-S Xscript')
4095 assert_equal(['ok'], readfile('Xdidit'))
4096
4097 delete('Xdidit')
4098 delete('Xscript')
4099 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01004100
4101 lines =<< trim END
4102 vim9script
4103 var foo#bar = 'asdf'
4104 END
4105 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004106enddef
4107
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02004108def Test_script_var_in_autocmd()
4109 # using a script variable from an autocommand, defined in a :def function in a
4110 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004111 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02004112 let s:counter = 1
4113 def s:Func()
4114 au! CursorHold
4115 au CursorHold * s:counter += 1
4116 enddef
4117 call s:Func()
4118 doau CursorHold
4119 call assert_equal(2, s:counter)
4120 au! CursorHold
4121 END
4122 CheckScriptSuccess(lines)
4123enddef
4124
Bram Moolenaarb5841b92021-07-15 18:09:53 +02004125def Test_error_in_autoload_script()
4126 var save_rtp = &rtp
4127 var dir = getcwd() .. '/Xruntime'
4128 &rtp = dir
4129 mkdir(dir .. '/autoload', 'p')
4130
4131 var lines =<< trim END
4132 vim9script noclear
4133 def script#autoloaded()
4134 enddef
4135 def Broken()
4136 var x: any = ''
4137 eval x != 0
4138 enddef
4139 Broken()
4140 END
4141 writefile(lines, dir .. '/autoload/script.vim')
4142
4143 lines =<< trim END
4144 vim9script
4145 def CallAutoloaded()
4146 script#autoloaded()
4147 enddef
4148
4149 function Legacy()
4150 try
4151 call s:CallAutoloaded()
4152 catch
4153 call assert_match('E1030: Using a String as a Number', v:exception)
4154 endtry
4155 endfunction
4156
4157 Legacy()
4158 END
4159 CheckScriptSuccess(lines)
4160
4161 &rtp = save_rtp
4162 delete(dir, 'rf')
4163enddef
4164
Bram Moolenaar3896a102020-08-09 14:33:55 +02004165def Test_cmdline_win()
4166 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
4167 # the command line window.
4168 mkdir('rtp/syntax', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004169 var export_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02004170 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004171 export var That = 'yes'
Bram Moolenaar3896a102020-08-09 14:33:55 +02004172 END
4173 writefile(export_lines, 'rtp/syntax/Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004174 var import_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02004175 vim9script
4176 import That from './Xexport.vim'
4177 END
4178 writefile(import_lines, 'rtp/syntax/vim.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004179 var save_rtp = &rtp
Bram Moolenaar3896a102020-08-09 14:33:55 +02004180 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
4181 syntax on
4182 augroup CmdWin
4183 autocmd CmdwinEnter * g:got_there = 'yes'
4184 augroup END
4185 # this will open and also close the cmdline window
4186 feedkeys('q:', 'xt')
4187 assert_equal('yes', g:got_there)
4188
4189 augroup CmdWin
4190 au!
4191 augroup END
4192 &rtp = save_rtp
4193 delete('rtp', 'rf')
4194enddef
4195
Bram Moolenaare3d46852020-08-29 13:39:17 +02004196def Test_invalid_sid()
4197 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02004198
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004199 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02004200 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02004201 endif
4202 delete('Xdidit')
4203enddef
4204
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004205def Test_restoring_cpo()
4206 writefile(['vim9script', 'set nocp'], 'Xsourced')
4207 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
4208 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
4209 assert_equal(['done'], readfile('Xdone'))
4210 endif
4211 delete('Xsourced')
4212 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01004213 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01004214
4215 writefile(['vim9script'], 'XanotherScript')
4216 set cpo=aABceFsMny>
4217 edit XanotherScript
4218 so %
4219 assert_equal('aABceFsMny>', &cpo)
4220 :1del
4221 w
4222 so %
4223 assert_equal('aABceFsMny>', &cpo)
4224
4225 delete('XanotherScript')
4226 set cpo&vim
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004227enddef
4228
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004229" Use :function so we can use Check commands
4230func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004231 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004232 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004233
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004234 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004235 vim9script
4236 def script#func()
4237 enddef
4238 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004239 call mkdir('Xdir/autoload', 'p')
4240 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004241
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004242 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004243 vim9script
4244 set cpo+=M
4245 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02004246 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004247 setline(1, 'some text')
4248 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004249 call writefile(lines, 'XTest_redraw_cpo')
4250 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
4251 call term_sendkeys(buf, "V:")
4252 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004253
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004254 " clean up
4255 call term_sendkeys(buf, "\<Esc>u")
4256 call StopVimInTerminal(buf)
4257 call delete('XTest_redraw_cpo')
4258 call delete('Xdir', 'rf')
4259endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004260
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004261
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02004262def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004263 var lines =<< trim END
4264 var name: any
4265 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02004266 END
4267 CheckDefAndScriptSuccess(lines)
4268enddef
4269
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02004270func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02004271 CheckRunVimInTerminal
4272
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02004273 " call indirectly to avoid compilation error for missing functions
4274 call Run_Test_define_func_at_command_line()
4275endfunc
4276
4277def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004278 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004279 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004280 func CheckAndQuit()
4281 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
4282 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
4283 endfunc
4284 END
4285 writefile([''], 'Xdidcmd')
4286 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01004287 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004288 # define Afunc() on the command line
4289 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
4290 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004291 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004292
4293 call StopVimInTerminal(buf)
4294 delete('XcallFunc')
4295 delete('Xdidcmd')
4296enddef
4297
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004298def Test_script_var_scope()
4299 var lines =<< trim END
4300 vim9script
4301 if true
4302 if true
4303 var one = 'one'
4304 echo one
4305 endif
4306 echo one
4307 endif
4308 END
4309 CheckScriptFailure(lines, 'E121:', 7)
4310
4311 lines =<< trim END
4312 vim9script
4313 if true
4314 if false
4315 var one = 'one'
4316 echo one
4317 else
4318 var one = 'one'
4319 echo one
4320 endif
4321 echo one
4322 endif
4323 END
4324 CheckScriptFailure(lines, 'E121:', 10)
4325
4326 lines =<< trim END
4327 vim9script
4328 while true
4329 var one = 'one'
4330 echo one
4331 break
4332 endwhile
4333 echo one
4334 END
4335 CheckScriptFailure(lines, 'E121:', 7)
4336
4337 lines =<< trim END
4338 vim9script
4339 for i in range(1)
4340 var one = 'one'
4341 echo one
4342 endfor
4343 echo one
4344 END
4345 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02004346
4347 lines =<< trim END
4348 vim9script
4349 {
4350 var one = 'one'
4351 assert_equal('one', one)
4352 }
4353 assert_false(exists('one'))
4354 assert_false(exists('s:one'))
4355 END
4356 CheckScriptSuccess(lines)
4357
4358 lines =<< trim END
4359 vim9script
4360 {
4361 var one = 'one'
4362 echo one
4363 }
4364 echo one
4365 END
4366 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004367enddef
4368
Bram Moolenaar352134b2020-10-17 22:04:08 +02004369def Test_catch_exception_in_callback()
4370 var lines =<< trim END
4371 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02004372 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02004373 try
4374 var x: string
4375 var y: string
4376 # this error should be caught with CHECKLEN
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00004377 var sl = ['']
4378 [x, y] = sl
Bram Moolenaar352134b2020-10-17 22:04:08 +02004379 catch
4380 g:caught = 'yes'
4381 endtry
4382 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004383 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02004384 feedkeys("\r", 'xt')
4385 END
4386 CheckScriptSuccess(lines)
4387
4388 unlet g:caught
4389enddef
4390
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004391def Test_no_unknown_error_after_error()
4392 if !has('unix') || !has('job')
4393 throw 'Skipped: not unix of missing +job feature'
4394 endif
Bram Moolenaarb16ff292021-09-26 20:14:39 +01004395 # FIXME: this check should not be needed
4396 if has('win32')
4397 throw 'Skipped: does not work on MS-Windows'
4398 endif
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004399 var lines =<< trim END
4400 vim9script
4401 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02004402 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004403 eval [][0]
4404 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02004405 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004406 sleep 1m
4407 source += l
4408 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004409 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01004410 while job_status(myjob) == 'run'
4411 sleep 10m
4412 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01004413 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02004414 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004415 END
4416 writefile(lines, 'Xdef')
4417 assert_fails('so Xdef', ['E684:', 'E1012:'])
4418 delete('Xdef')
4419enddef
4420
Bram Moolenaar4324d872020-12-01 20:12:24 +01004421def InvokeNormal()
4422 exe "norm! :m+1\r"
4423enddef
4424
4425def Test_invoke_normal_in_visual_mode()
4426 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
4427 new
4428 setline(1, ['aaa', 'bbb'])
4429 feedkeys("V\<F3>", 'xt')
4430 assert_equal(['bbb', 'aaa'], getline(1, 2))
4431 xunmap <F3>
4432enddef
4433
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004434def Test_white_space_after_command()
4435 var lines =<< trim END
4436 exit_cb: Func})
4437 END
4438 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01004439
4440 lines =<< trim END
4441 e#
4442 END
4443 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004444enddef
4445
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004446def Test_script_var_gone_when_sourced_twice()
4447 var lines =<< trim END
4448 vim9script
4449 if exists('g:guard')
4450 finish
4451 endif
4452 g:guard = 1
4453 var name = 'thename'
4454 def g:GetName(): string
4455 return name
4456 enddef
4457 def g:SetName(arg: string)
4458 name = arg
4459 enddef
4460 END
4461 writefile(lines, 'XscriptTwice.vim')
4462 so XscriptTwice.vim
4463 assert_equal('thename', g:GetName())
4464 g:SetName('newname')
4465 assert_equal('newname', g:GetName())
4466 so XscriptTwice.vim
4467 assert_fails('call g:GetName()', 'E1149:')
4468 assert_fails('call g:SetName("x")', 'E1149:')
4469
4470 delfunc g:GetName
4471 delfunc g:SetName
4472 delete('XscriptTwice.vim')
4473 unlet g:guard
4474enddef
4475
4476def Test_import_gone_when_sourced_twice()
4477 var exportlines =<< trim END
4478 vim9script
4479 if exists('g:guard')
4480 finish
4481 endif
4482 g:guard = 1
4483 export var name = 'someName'
4484 END
4485 writefile(exportlines, 'XexportScript.vim')
4486
4487 var lines =<< trim END
4488 vim9script
4489 import name from './XexportScript.vim'
4490 def g:GetName(): string
4491 return name
4492 enddef
4493 END
4494 writefile(lines, 'XscriptImport.vim')
4495 so XscriptImport.vim
4496 assert_equal('someName', g:GetName())
4497
4498 so XexportScript.vim
4499 assert_fails('call g:GetName()', 'E1149:')
4500
4501 delfunc g:GetName
4502 delete('XexportScript.vim')
4503 delete('XscriptImport.vim')
4504 unlet g:guard
4505enddef
4506
Bram Moolenaar10b94212021-02-19 21:42:57 +01004507def Test_unsupported_commands()
4508 var lines =<< trim END
4509 ka
4510 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004511 CheckDefFailure(lines, 'E476:')
4512 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaar10b94212021-02-19 21:42:57 +01004513
4514 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01004515 :1ka
4516 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004517 CheckDefFailure(lines, 'E476:')
4518 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaarada1d872021-02-20 08:16:51 +01004519
4520 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01004521 t
4522 END
4523 CheckDefFailure(lines, 'E1100:')
4524 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4525
4526 lines =<< trim END
4527 x
4528 END
4529 CheckDefFailure(lines, 'E1100:')
4530 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4531
4532 lines =<< trim END
4533 xit
4534 END
4535 CheckDefFailure(lines, 'E1100:')
4536 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4537enddef
4538
Bram Moolenaarc70fe462021-04-17 17:59:19 +02004539def Test_mapping_line_number()
4540 var lines =<< trim END
4541 vim9script
4542 def g:FuncA()
4543 # Some comment
4544 FuncB(0)
4545 enddef
4546 # Some comment
4547 def FuncB(
4548 # Some comment
4549 n: number
4550 )
4551 exe 'nno '
4552 # Some comment
4553 .. '<F3> a'
4554 .. 'b'
4555 .. 'c'
4556 enddef
4557 END
4558 CheckScriptSuccess(lines)
4559 var res = execute('verbose nmap <F3>')
4560 assert_match('No mapping found', res)
4561
4562 g:FuncA()
4563 res = execute('verbose nmap <F3>')
4564 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
4565
4566 nunmap <F3>
4567 delfunc g:FuncA
4568enddef
4569
Bram Moolenaardeb108b2021-07-08 17:35:36 +02004570def Test_option_set()
4571 # legacy script allows for white space
4572 var lines =<< trim END
4573 set foldlevel =11
4574 call assert_equal(11, &foldlevel)
4575 END
4576 CheckScriptSuccess(lines)
4577
4578 set foldlevel
4579 set foldlevel=12
4580 assert_equal(12, &foldlevel)
4581 set foldlevel+=2
4582 assert_equal(14, &foldlevel)
4583 set foldlevel-=3
4584 assert_equal(11, &foldlevel)
4585
4586 lines =<< trim END
4587 set foldlevel =1
4588 END
4589 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: =1')
4590
4591 lines =<< trim END
4592 set foldlevel +=1
4593 END
4594 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: +=1')
4595
4596 lines =<< trim END
4597 set foldlevel ^=1
4598 END
4599 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: ^=1')
4600
4601 lines =<< trim END
4602 set foldlevel -=1
4603 END
4604 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: -=1')
4605
4606 set foldlevel&
4607enddef
4608
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004609def Test_option_modifier()
Bram Moolenaar1594f312021-07-08 16:40:13 +02004610 # legacy script allows for white space
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004611 var lines =<< trim END
4612 set hlsearch & hlsearch !
4613 call assert_equal(1, &hlsearch)
4614 END
4615 CheckScriptSuccess(lines)
4616
Bram Moolenaar1594f312021-07-08 16:40:13 +02004617 set hlsearch
4618 set hlsearch!
4619 assert_equal(false, &hlsearch)
4620
4621 set hlsearch
4622 set hlsearch&
4623 assert_equal(false, &hlsearch)
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004624
4625 lines =<< trim END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004626 set hlsearch &
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004627 END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004628 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: &')
4629
4630 lines =<< trim END
4631 set hlsearch !
4632 END
4633 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: !')
4634
4635 set hlsearch&
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004636enddef
4637
Bram Moolenaarc03fe662021-07-11 16:52:45 +02004638" This must be called last, it may cause following :def functions to fail
4639def Test_xxx_echoerr_line_number()
4640 var lines =<< trim END
4641 echoerr 'some'
4642 .. ' error'
4643 .. ' continued'
4644 END
4645 CheckDefExecAndScriptFailure(lines, 'some error continued', 1)
4646enddef
4647
Bram Moolenaar9537e372021-12-10 21:05:53 +00004648func Test_debug_with_lambda()
Bram Moolenaar9fffef92021-12-10 16:55:58 +00004649 CheckRunVimInTerminal
4650
Bram Moolenaar9537e372021-12-10 21:05:53 +00004651 " call indirectly to avoid compilation error for missing functions
4652 call Run_Test_debug_with_lambda()
4653endfunc
4654
4655def Run_Test_debug_with_lambda()
Bram Moolenaar9fffef92021-12-10 16:55:58 +00004656 var lines =<< trim END
4657 vim9script
4658 def Func()
4659 var n = 0
4660 echo [0]->filter((_, v) => v == n)
4661 enddef
4662 breakadd func Func
4663 Func()
4664 END
4665 writefile(lines, 'XdebugFunc')
4666 var buf = RunVimInTerminal('-S XdebugFunc', {rows: 6, wait_for_ruler: 0})
4667 WaitForAssert(() => assert_match('^>', term_getline(buf, 6)))
4668
4669 term_sendkeys(buf, "cont\<CR>")
4670 WaitForAssert(() => assert_match('\[0\]', term_getline(buf, 5)))
4671
4672 StopVimInTerminal(buf)
4673 delete('XdebugFunc')
4674enddef
4675
Bram Moolenaar648594e2021-07-11 17:55:01 +02004676def ProfiledWithLambda()
Bram Moolenaard9162552021-07-11 15:26:13 +02004677 var n = 3
4678 echo [[1, 2], [3, 4]]->filter((_, l) => l[0] == n)
4679enddef
4680
Bram Moolenaar648594e2021-07-11 17:55:01 +02004681def ProfiledNested()
4682 var x = 0
4683 def Nested(): any
4684 return x
4685 enddef
4686 Nested()
4687enddef
4688
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004689def ProfiledNestedProfiled()
4690 var x = 0
4691 def Nested(): any
4692 return x
4693 enddef
4694 Nested()
4695enddef
4696
Dominique Pelle923dce22021-11-21 11:36:04 +00004697" Execute this near the end, profiling doesn't stop until Vim exits.
Bram Moolenaard9162552021-07-11 15:26:13 +02004698" This only tests that it works, not the profiling output.
4699def Test_xx_profile_with_lambda()
Bram Moolenaar4ece1522021-07-11 16:31:51 +02004700 CheckFeature profile
4701
Bram Moolenaard9162552021-07-11 15:26:13 +02004702 profile start Xprofile.log
Bram Moolenaar648594e2021-07-11 17:55:01 +02004703 profile func ProfiledWithLambda
4704 ProfiledWithLambda()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004705
Bram Moolenaar648594e2021-07-11 17:55:01 +02004706 profile func ProfiledNested
4707 ProfiledNested()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004708
4709 # Also profile the nested function. Use a different function, although the
4710 # contents is the same, to make sure it was not already compiled.
4711 profile func *
4712 ProfiledNestedProfiled()
4713
4714 profdel func *
4715 profile pause
Bram Moolenaard9162552021-07-11 15:26:13 +02004716enddef
4717
Bram Moolenaar585fea72020-04-02 22:33:21 +02004718" Keep this last, it messes up highlighting.
4719def Test_substitute_cmd()
4720 new
4721 setline(1, 'something')
4722 :substitute(some(other(
4723 assert_equal('otherthing', getline(1))
4724 bwipe!
4725
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004726 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004727 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02004728 vim9script
4729 new
4730 setline(1, 'something')
4731 :substitute(some(other(
4732 assert_equal('otherthing', getline(1))
4733 bwipe!
4734 END
4735 writefile(lines, 'Xvim9lines')
4736 source Xvim9lines
4737
4738 delete('Xvim9lines')
4739enddef
4740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker