blob: c9ceccf0d8cf8cd3f5f184bbc4b7daeaab9c4f4b [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
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00001514 CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use foo itself'])
Bram Moolenaar6853c382021-09-07 22:12:19 +02001515 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 Moolenaarf8a79fc2021-12-14 12:06:16 +00001539
1540 lines =<< trim END
1541 vim9script
1542 def TheFunc()
1543 echo 'the func'
1544 enddef
1545 export var Ref = TheFunc
1546 END
1547 writefile([], 'Xthat.vim')
1548 lines =<< trim END
1549 import * as That from './Xthat.vim'
1550 That()
1551 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00001552 CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use That itself'])
Bram Moolenaarf8a79fc2021-12-14 12:06:16 +00001553 delete('Xthat.vim')
Bram Moolenaar6853c382021-09-07 22:12:19 +02001554enddef
1555
Bram Moolenaar0a842842021-02-27 22:41:19 +01001556def Test_import_as()
1557 var export_lines =<< trim END
1558 vim9script
1559 export var one = 1
1560 export var yes = 'yes'
Bram Moolenaarc967d572021-07-08 21:38:50 +02001561 export var slist: list<string>
Bram Moolenaar0a842842021-02-27 22:41:19 +01001562 END
1563 writefile(export_lines, 'XexportAs')
1564
1565 var import_lines =<< trim END
1566 vim9script
Bram Moolenaar6c7cc342021-04-17 16:38:50 +02001567 var one = 'notused'
1568 var yes = 777
Bram Moolenaar0a842842021-02-27 22:41:19 +01001569 import one as thatOne from './XexportAs'
1570 assert_equal(1, thatOne)
1571 import yes as yesYes from './XexportAs'
1572 assert_equal('yes', yesYes)
1573 END
1574 CheckScriptSuccess(import_lines)
1575
1576 import_lines =<< trim END
1577 vim9script
1578 import {one as thatOne, yes as yesYes} from './XexportAs'
1579 assert_equal(1, thatOne)
1580 assert_equal('yes', yesYes)
1581 assert_fails('echo one', 'E121:')
1582 assert_fails('echo yes', 'E121:')
1583 END
1584 CheckScriptSuccess(import_lines)
1585
Bram Moolenaarc967d572021-07-08 21:38:50 +02001586 import_lines =<< trim END
1587 vim9script
1588 import {slist as impSlist} from './XexportAs'
1589 impSlist->add(123)
1590 END
1591 CheckScriptFailure(import_lines, 'E1012: Type mismatch; expected string but got number')
1592
Bram Moolenaar0a842842021-02-27 22:41:19 +01001593 delete('XexportAs')
1594enddef
1595
Bram Moolenaar803af682020-08-05 16:20:03 +02001596func g:Trigger()
1597 source Ximport.vim
1598 return "echo 'yes'\<CR>"
1599endfunc
1600
1601def Test_import_export_expr_map()
1602 # check that :import and :export work when buffer is locked
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001603 var export_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001604 vim9script
1605 export def That(): string
1606 return 'yes'
1607 enddef
1608 END
1609 writefile(export_lines, 'Xexport_that.vim')
1610
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001611 var import_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001612 vim9script
1613 import That from './Xexport_that.vim'
1614 assert_equal('yes', That())
1615 END
1616 writefile(import_lines, 'Ximport.vim')
1617
1618 nnoremap <expr> trigger g:Trigger()
1619 feedkeys('trigger', "xt")
1620
Bram Moolenaar730b2482020-08-09 13:02:10 +02001621 delete('Xexport_that.vim')
Bram Moolenaar803af682020-08-05 16:20:03 +02001622 delete('Ximport.vim')
1623 nunmap trigger
1624enddef
1625
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001626def Test_import_in_filetype()
1627 # check that :import works when the buffer is locked
1628 mkdir('ftplugin', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001629 var export_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001630 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001631 export var That = 'yes'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001632 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001633 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001634
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001635 var import_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001636 vim9script
Bram Moolenaar730b2482020-08-09 13:02:10 +02001637 import That from './Xexport_ft.vim'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001638 assert_equal('yes', That)
1639 g:did_load_mytpe = 1
1640 END
1641 writefile(import_lines, 'ftplugin/qf.vim')
1642
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001643 var save_rtp = &rtp
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001644 &rtp = getcwd() .. ',' .. &rtp
1645
1646 filetype plugin on
1647 copen
1648 assert_equal(1, g:did_load_mytpe)
1649
1650 quit!
Bram Moolenaar730b2482020-08-09 13:02:10 +02001651 delete('Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001652 delete('ftplugin', 'rf')
1653 &rtp = save_rtp
1654enddef
1655
Bram Moolenaarefa94442020-08-08 22:16:00 +02001656def Test_use_import_in_mapping()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001657 var lines =<< trim END
Bram Moolenaarefa94442020-08-08 22:16:00 +02001658 vim9script
1659 export def Funcx()
1660 g:result = 42
1661 enddef
1662 END
1663 writefile(lines, 'XsomeExport.vim')
1664 lines =<< trim END
1665 vim9script
1666 import Funcx from './XsomeExport.vim'
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001667 nnoremap <F3> :call <sid>Funcx()<cr>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001668 END
1669 writefile(lines, 'Xmapscript.vim')
1670
1671 source Xmapscript.vim
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001672 feedkeys("\<F3>", "xt")
Bram Moolenaarefa94442020-08-08 22:16:00 +02001673 assert_equal(42, g:result)
1674
1675 unlet g:result
1676 delete('XsomeExport.vim')
1677 delete('Xmapscript.vim')
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001678 nunmap <F3>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001679enddef
1680
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01001681def Test_vim9script_mix()
1682 var lines =<< trim END
1683 if has(g:feature)
1684 " legacy script
1685 let g:legacy = 1
1686 finish
1687 endif
1688 vim9script
1689 g:legacy = 0
1690 END
1691 g:feature = 'eval'
1692 g:legacy = -1
1693 CheckScriptSuccess(lines)
1694 assert_equal(1, g:legacy)
1695
1696 g:feature = 'noteval'
1697 g:legacy = -1
1698 CheckScriptSuccess(lines)
1699 assert_equal(0, g:legacy)
1700enddef
1701
Bram Moolenaar750802b2020-02-23 18:08:33 +01001702def Test_vim9script_fails()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1704 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001705 CheckScriptFailure(['export var some = 123'], 'E1042:')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001706 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001707 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
Bram Moolenaar750802b2020-02-23 18:08:33 +01001708 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
1709
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001710 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001711 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1712
Bram Moolenaare2e40752020-09-04 21:18:46 +02001713 assert_fails('vim9script', 'E1038:')
1714 assert_fails('export something', 'E1043:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715enddef
1716
Bram Moolenaarf7d267e2020-06-17 12:04:54 +02001717func Test_import_fails_without_script()
Bram Moolenaar101f4812020-06-16 23:18:51 +02001718 CheckRunVimInTerminal
1719
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001720 " call indirectly to avoid compilation error for missing functions
Bram Moolenaarc620c052020-07-08 15:16:19 +02001721 call Run_Test_import_fails_on_command_line()
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001722endfunc
1723
Bram Moolenaarc620c052020-07-08 15:16:19 +02001724def Run_Test_import_fails_on_command_line()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001725 var export =<< trim END
Bram Moolenaar101f4812020-06-16 23:18:51 +02001726 vim9script
1727 export def Foo(): number
1728 return 0
1729 enddef
1730 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001731 writefile(export, 'XexportCmd.vim')
Bram Moolenaar101f4812020-06-16 23:18:51 +02001732
Bram Moolenaare0de1712020-12-02 17:36:54 +01001733 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001734 rows: 6, wait_for_ruler: 0})
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001735 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaar101f4812020-06-16 23:18:51 +02001736
Bram Moolenaar730b2482020-08-09 13:02:10 +02001737 delete('XexportCmd.vim')
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001738 StopVimInTerminal(buf)
1739enddef
Bram Moolenaar101f4812020-06-16 23:18:51 +02001740
Bram Moolenaar2b327002020-12-26 15:39:31 +01001741def Test_vim9script_reload_noclear()
1742 var lines =<< trim END
Bram Moolenaara6294952020-12-27 13:39:50 +01001743 vim9script
1744 export var exported = 'thexport'
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001745
1746 export def TheFunc(x = 0)
1747 enddef
Bram Moolenaara6294952020-12-27 13:39:50 +01001748 END
1749 writefile(lines, 'XExportReload')
1750 lines =<< trim END
Bram Moolenaar2b327002020-12-26 15:39:31 +01001751 vim9script noclear
1752 g:loadCount += 1
1753 var s:reloaded = 'init'
Bram Moolenaara6294952020-12-27 13:39:50 +01001754 import exported from './XExportReload'
Bram Moolenaar2b327002020-12-26 15:39:31 +01001755
1756 def Again(): string
1757 return 'again'
1758 enddef
1759
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001760 import TheFunc from './XExportReload'
1761 TheFunc()
1762
Bram Moolenaar2b327002020-12-26 15:39:31 +01001763 if exists('s:loaded') | finish | endif
1764 var s:loaded = true
1765
1766 var s:notReloaded = 'yes'
1767 s:reloaded = 'first'
1768 def g:Values(): list<string>
Bram Moolenaara6294952020-12-27 13:39:50 +01001769 return [s:reloaded, s:notReloaded, Again(), Once(), exported]
Bram Moolenaar2b327002020-12-26 15:39:31 +01001770 enddef
1771
1772 def Once(): string
1773 return 'once'
1774 enddef
1775 END
1776 writefile(lines, 'XReloaded')
1777 g:loadCount = 0
1778 source XReloaded
1779 assert_equal(1, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001780 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001781 source XReloaded
1782 assert_equal(2, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001783 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001784 source XReloaded
1785 assert_equal(3, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001786 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001787
Bram Moolenaar48e11c12021-01-11 18:47:00 +01001788 delete('XReloaded')
Bram Moolenaara6294952020-12-27 13:39:50 +01001789 delete('XExportReload')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001790 delfunc g:Values
Bram Moolenaar2b327002020-12-26 15:39:31 +01001791 unlet g:loadCount
Bram Moolenaar577dc932021-06-27 15:35:40 +02001792
1793 lines =<< trim END
1794 vim9script
1795 def Inner()
1796 enddef
1797 END
1798 lines->writefile('XreloadScript.vim')
1799 source XreloadScript.vim
1800
1801 lines =<< trim END
1802 vim9script
1803 def Outer()
1804 def Inner()
1805 enddef
1806 enddef
1807 defcompile
1808 END
1809 lines->writefile('XreloadScript.vim')
1810 source XreloadScript.vim
1811
1812 delete('XreloadScript.vim')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001813enddef
1814
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001815def Test_vim9script_reload_import()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001816 var lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 vim9script
1818 const var = ''
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001819 var valone = 1234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 def MyFunc(arg: string)
1821 valone = 5678
1822 enddef
1823 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001824 var morelines =<< trim END
1825 var valtwo = 222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 export def GetValtwo(): number
1827 return valtwo
1828 enddef
1829 END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001830 writefile(lines + morelines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 source Xreload.vim
1832 source Xreload.vim
1833 source Xreload.vim
1834
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001835 # cannot declare a var twice
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 lines =<< trim END
1837 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001838 var valone = 1234
1839 var valone = 5678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 END
1841 writefile(lines, 'Xreload.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001842 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843
1844 delete('Xreload.vim')
1845 delete('Ximport.vim')
1846enddef
1847
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001848" if a script is reloaded with a script-local variable that changed its type, a
1849" compiled function using that variable must fail.
1850def Test_script_reload_change_type()
1851 var lines =<< trim END
1852 vim9script noclear
1853 var str = 'string'
1854 def g:GetStr(): string
1855 return str .. 'xxx'
1856 enddef
1857 END
1858 writefile(lines, 'Xreload.vim')
1859 source Xreload.vim
1860 echo g:GetStr()
1861
1862 lines =<< trim END
1863 vim9script noclear
1864 var str = 1234
1865 END
1866 writefile(lines, 'Xreload.vim')
1867 source Xreload.vim
1868 assert_fails('echo g:GetStr()', 'E1150:')
1869
1870 delfunc g:GetStr
1871 delete('Xreload.vim')
1872enddef
1873
Bram Moolenaarc970e422021-03-17 15:03:04 +01001874" Define CallFunc so that the test can be compiled
1875command CallFunc echo 'nop'
1876
1877def Test_script_reload_from_function()
1878 var lines =<< trim END
1879 vim9script
1880
1881 if exists('g:loaded')
1882 finish
1883 endif
1884 g:loaded = 1
1885 delcommand CallFunc
1886 command CallFunc Func()
1887 def Func()
Bram Moolenaara4c81be2021-03-17 15:23:16 +01001888 so XreloadFunc.vim
Bram Moolenaarc970e422021-03-17 15:03:04 +01001889 g:didTheFunc = 1
1890 enddef
1891 END
1892 writefile(lines, 'XreloadFunc.vim')
1893 source XreloadFunc.vim
1894 CallFunc
1895 assert_equal(1, g:didTheFunc)
1896
1897 delete('XreloadFunc.vim')
1898 delcommand CallFunc
1899 unlet g:loaded
1900 unlet g:didTheFunc
1901enddef
1902
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001903def Test_script_var_shadows_function()
1904 var lines =<< trim END
1905 vim9script
1906 def Func(): number
1907 return 123
1908 enddef
1909 var Func = 1
1910 END
1911 CheckScriptFailure(lines, 'E1041:', 5)
1912enddef
1913
Bram Moolenaar052ff292021-12-11 13:54:46 +00001914def Test_function_shadows_script_var()
1915 var lines =<< trim END
1916 vim9script
1917 var Func = 1
1918 def Func(): number
1919 return 123
1920 enddef
1921 END
1922 CheckScriptFailure(lines, 'E1041:', 3)
1923enddef
1924
Bram Moolenaarc3235272021-07-10 19:42:03 +02001925def Test_script_var_shadows_command()
1926 var lines =<< trim END
1927 var undo = 1
1928 undo = 2
1929 assert_equal(2, undo)
1930 END
1931 CheckDefAndScriptSuccess(lines)
1932
1933 lines =<< trim END
1934 var undo = 1
1935 undo
1936 END
1937 CheckDefAndScriptFailure(lines, 'E1207:', 2)
1938enddef
1939
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001940def Test_vim9script_call_wrong_type()
1941 var lines =<< trim END
1942 vim9script
1943 var Time = 'localtime'
1944 Time()
1945 END
1946 CheckScriptFailure(lines, 'E1085:')
1947enddef
1948
Bram Moolenaar95006e32020-08-29 17:47:08 +02001949def s:RetSome(): string
1950 return 'some'
1951enddef
1952
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001953" Not exported function that is referenced needs to be accessed by the
1954" script-local name.
1955def Test_vim9script_funcref()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001956 var sortlines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001957 vim9script
1958 def Compare(i1: number, i2: number): number
Bram Moolenaarbed36b92020-07-07 23:31:36 +02001959 return i2 - i1
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001960 enddef
1961
1962 export def FastSort(): list<number>
1963 return range(5)->sort(Compare)
1964 enddef
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001965
1966 export def GetString(arg: string): string
1967 return arg
1968 enddef
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001969 END
1970 writefile(sortlines, 'Xsort.vim')
1971
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001972 var lines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001973 vim9script
1974 import FastSort from './Xsort.vim'
1975 def Test()
1976 g:result = FastSort()
1977 enddef
1978 Test()
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001979
1980 # using a function imported with "as"
1981 import * as anAlias from './Xsort.vim'
1982 assert_equal('yes', anAlias.GetString('yes'))
1983
1984 # using the function from a compiled function
1985 def TestMore(): string
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001986 var s = s:anAlias.GetString('foo')
1987 return s .. anAlias.GetString('bar')
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001988 enddef
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001989 assert_equal('foobar', TestMore())
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001990
1991 # error when using a function that isn't exported
1992 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001993 END
1994 writefile(lines, 'Xscript.vim')
1995
1996 source Xscript.vim
1997 assert_equal([4, 3, 2, 1, 0], g:result)
1998
1999 unlet g:result
2000 delete('Xsort.vim')
2001 delete('Xscript.vim')
Bram Moolenaar95006e32020-08-29 17:47:08 +02002002
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002003 var Funcref = function('s:RetSome')
Bram Moolenaar95006e32020-08-29 17:47:08 +02002004 assert_equal('some', Funcref())
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002005enddef
2006
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002007" Check that when searching for "FilterFunc" it finds the import in the
2008" script where FastFilter() is called from, both as a string and as a direct
2009" function reference.
Bram Moolenaarc620c052020-07-08 15:16:19 +02002010def Test_vim9script_funcref_other_script()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002011 var filterLines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02002012 vim9script
2013 export def FilterFunc(idx: number, val: number): bool
2014 return idx % 2 == 1
2015 enddef
2016 export def FastFilter(): list<number>
2017 return range(10)->filter('FilterFunc')
2018 enddef
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002019 export def FastFilterDirect(): list<number>
2020 return range(10)->filter(FilterFunc)
2021 enddef
Bram Moolenaarc620c052020-07-08 15:16:19 +02002022 END
2023 writefile(filterLines, 'Xfilter.vim')
2024
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002025 var lines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02002026 vim9script
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002027 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim'
Bram Moolenaarc620c052020-07-08 15:16:19 +02002028 def Test()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002029 var x: list<number> = FastFilter()
Bram Moolenaarc620c052020-07-08 15:16:19 +02002030 enddef
2031 Test()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002032 def TestDirect()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002033 var x: list<number> = FastFilterDirect()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002034 enddef
2035 TestDirect()
Bram Moolenaarc620c052020-07-08 15:16:19 +02002036 END
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002037 CheckScriptSuccess(lines)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002038 delete('Xfilter.vim')
Bram Moolenaarc620c052020-07-08 15:16:19 +02002039enddef
2040
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002041def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002042 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002043 vim9script
2044 def FuncYes(): string
2045 return 'yes'
2046 enddef
2047 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002048 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002049 def FuncNo(): string
2050 return 'no'
2051 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002052 def g:DoCheck(no_exists: bool)
2053 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002054 assert_equal('no', FuncNo())
2055 enddef
2056 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002057 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002058 def g:DoCheck(no_exists: bool)
2059 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02002060 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002061 enddef
2062 END
2063
2064 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002065 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002066 source Xreloaded.vim
2067 g:DoCheck(true)
2068
2069 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002070 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002071 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01002072 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002073
2074 # FuncNo() is back
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002075 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002076 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01002077 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002078
2079 delete('Xreloaded.vim')
2080enddef
2081
Bram Moolenaar89483d42020-05-10 15:24:44 +02002082def Test_vim9script_reload_delvar()
2083 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002084 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02002085 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002086 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02002087 END
2088 writefile(lines, 'XreloadVar.vim')
2089 source XreloadVar.vim
2090
2091 # now write the script using the same variable locally - works
2092 lines =<< trim END
2093 vim9script
2094 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002095 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02002096 enddef
2097 END
2098 writefile(lines, 'XreloadVar.vim')
2099 source XreloadVar.vim
2100
2101 delete('XreloadVar.vim')
2102enddef
2103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104def Test_import_absolute()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002105 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02002106 'vim9script',
2107 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
2108 'def UseExported()',
2109 ' g:imported_abs = exported',
2110 ' exported = 8888',
2111 ' g:imported_after = exported',
2112 'enddef',
2113 'UseExported()',
2114 'g:import_disassembled = execute("disass UseExported")',
2115 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 writefile(import_lines, 'Ximport_abs.vim')
2117 writefile(s:export_script_lines, 'Xexport_abs.vim')
2118
2119 source Ximport_abs.vim
2120
2121 assert_equal(9876, g:imported_abs)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002122 assert_equal(8888, g:imported_after)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002123 assert_match('<SNR>\d\+_UseExported\_s*' ..
2124 'g:imported_abs = exported\_s*' ..
2125 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
2126 '1 STOREG g:imported_abs\_s*' ..
2127 'exported = 8888\_s*' ..
2128 '2 PUSHNR 8888\_s*' ..
2129 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
2130 'g:imported_after = exported\_s*' ..
2131 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
2132 '5 STOREG g:imported_after',
Bram Moolenaare6085c52020-04-12 20:19:16 +02002133 g:import_disassembled)
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002134
2135 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 unlet g:imported_abs
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002137 unlet g:import_disassembled
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138
2139 delete('Ximport_abs.vim')
2140 delete('Xexport_abs.vim')
2141enddef
2142
2143def Test_import_rtp()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002144 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02002145 'vim9script',
2146 'import exported from "Xexport_rtp.vim"',
2147 'g:imported_rtp = exported',
2148 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149 writefile(import_lines, 'Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002150 mkdir('import', 'p')
2151 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002152
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002153 var save_rtp = &rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 &rtp = getcwd()
2155 source Ximport_rtp.vim
2156 &rtp = save_rtp
2157
2158 assert_equal(9876, g:imported_rtp)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002160 Undo_export_script_lines()
2161 unlet g:imported_rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 delete('Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002163 delete('import', 'rf')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164enddef
2165
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002166def Test_import_compile_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002167 var export_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002168 'vim9script',
2169 'export def ExpFunc(): string',
2170 ' return notDefined',
2171 'enddef',
2172 ]
2173 writefile(export_lines, 'Xexported.vim')
2174
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002175 var import_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002176 'vim9script',
2177 'import ExpFunc from "./Xexported.vim"',
2178 'def ImpFunc()',
2179 ' echo ExpFunc()',
2180 'enddef',
2181 'defcompile',
2182 ]
2183 writefile(import_lines, 'Ximport.vim')
2184
2185 try
2186 source Ximport.vim
2187 catch /E1001/
Dominique Pelle923dce22021-11-21 11:36:04 +00002188 # Error should be before the Xexported.vim file.
Bram Moolenaar77072282020-09-16 17:55:40 +02002189 assert_match('E1001: Variable not found: notDefined', v:exception)
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002190 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
2191 endtry
2192
2193 delete('Xexported.vim')
2194 delete('Ximport.vim')
2195enddef
2196
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002197def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002198 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002199 'vim9script',
2200 'def Func()',
2201 ' eval [][0]',
2202 'enddef',
2203 'Func()',
2204 ]
2205 writefile(lines, 'Xtestscript.vim')
2206
2207 for count in range(3)
2208 try
2209 source Xtestscript.vim
2210 catch /E684/
2211 # function name should contain <SNR> every time
2212 assert_match('E684: list index out of range', v:exception)
2213 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
2214 endtry
2215 endfor
2216
2217 delete('Xtestscript.vim')
2218enddef
2219
Bram Moolenaareef21022020-08-01 22:16:43 +02002220def Test_func_overrules_import_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002221 var export_lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002222 vim9script
2223 export def Func()
2224 echo 'imported'
2225 enddef
2226 END
2227 writefile(export_lines, 'XexportedFunc.vim')
2228
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002229 var lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002230 vim9script
2231 import Func from './XexportedFunc.vim'
2232 def Func()
2233 echo 'local to function'
2234 enddef
2235 END
Bram Moolenaar052ff292021-12-11 13:54:46 +00002236 CheckScriptFailure(lines, 'E1041:')
Bram Moolenaareef21022020-08-01 22:16:43 +02002237
2238 lines =<< trim END
2239 vim9script
2240 import Func from './XexportedFunc.vim'
2241 def Outer()
2242 def Func()
2243 echo 'local to function'
2244 enddef
2245 enddef
2246 defcompile
2247 END
2248 CheckScriptFailure(lines, 'E1073:')
2249
2250 delete('XexportedFunc.vim')
2251enddef
2252
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002253def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002254 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002255 vim9script
2256 def Func()
2257 echo 'one'
2258 enddef
2259 def Func()
2260 echo 'two'
2261 enddef
2262 END
2263 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002264
2265 lines =<< trim END
2266 vim9script
2267 def Foo(): string
2268 return 'foo'
Bram Moolenaar052ff292021-12-11 13:54:46 +00002269 enddef
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002270 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002271 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002272 enddef
2273 defcompile
2274 END
2275 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002276enddef
2277
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002279 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002280 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 l->remove(0)
2282 l->add(5)
2283 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002284 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285enddef
2286
Bram Moolenaarae616492020-07-28 20:07:27 +02002287def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002288 CheckDefExecFailure(['a = 1'], 'E1100:')
2289 CheckDefExecFailure(['c = 1'], 'E1100:')
2290 CheckDefExecFailure(['i = 1'], 'E1100:')
2291 CheckDefExecFailure(['t = 1'], 'E1100:')
2292 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002293
Bram Moolenaarae616492020-07-28 20:07:27 +02002294 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
2295 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002296 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
2297 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002298 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
2299 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01002300 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
2301 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002302 CheckScriptFailure(['vim9script', 't'], 'E1100:')
2303 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
2304 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002305enddef
2306
Bram Moolenaar158906c2020-02-06 20:39:45 +01002307def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002308 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01002309 if what == 1
2310 res = "one"
2311 elseif what == 2
2312 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002313 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01002314 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002315 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01002316 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002317enddef
2318
Bram Moolenaar158906c2020-02-06 20:39:45 +01002319def Test_if_elseif_else()
2320 assert_equal('one', IfElse(1))
2321 assert_equal('two', IfElse(2))
2322 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002323enddef
2324
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002325def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002326 CheckDefFailure(['elseif true'], 'E582:')
2327 CheckDefFailure(['else'], 'E581:')
2328 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01002329 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002330 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002331
2332 var lines =<< trim END
2333 var s = ''
2334 if s = ''
2335 endif
2336 END
2337 CheckDefFailure(lines, 'E488:')
2338
2339 lines =<< trim END
2340 var s = ''
2341 if s == ''
2342 elseif s = ''
2343 endif
2344 END
2345 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002346enddef
2347
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002348let g:bool_true = v:true
2349let g:bool_false = v:false
2350
2351def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002352 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002353 if true ? true : false
2354 res = true
2355 endif
2356 assert_equal(true, res)
2357
Bram Moolenaar585fea72020-04-02 22:33:21 +02002358 g:glob = 2
2359 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02002360 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002361 endif
2362 assert_equal(2, g:glob)
2363 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02002364 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002365 endif
2366 assert_equal(3, g:glob)
2367
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002368 res = false
2369 if g:bool_true ? true : false
2370 res = true
2371 endif
2372 assert_equal(true, res)
2373
2374 res = false
2375 if true ? g:bool_true : false
2376 res = true
2377 endif
2378 assert_equal(true, res)
2379
2380 res = false
2381 if true ? true : g:bool_false
2382 res = true
2383 endif
2384 assert_equal(true, res)
2385
2386 res = false
2387 if true ? false : true
2388 res = true
2389 endif
2390 assert_equal(false, res)
2391
2392 res = false
2393 if false ? false : true
2394 res = true
2395 endif
2396 assert_equal(true, res)
2397
2398 res = false
2399 if false ? true : false
2400 res = true
2401 endif
2402 assert_equal(false, res)
2403
2404 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002405 if has('xyz') ? true : false
2406 res = true
2407 endif
2408 assert_equal(false, res)
2409
2410 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002411 if true && true
2412 res = true
2413 endif
2414 assert_equal(true, res)
2415
2416 res = false
2417 if true && false
2418 res = true
2419 endif
2420 assert_equal(false, res)
2421
2422 res = false
2423 if g:bool_true && false
2424 res = true
2425 endif
2426 assert_equal(false, res)
2427
2428 res = false
2429 if true && g:bool_false
2430 res = true
2431 endif
2432 assert_equal(false, res)
2433
2434 res = false
2435 if false && false
2436 res = true
2437 endif
2438 assert_equal(false, res)
2439
2440 res = false
2441 if true || false
2442 res = true
2443 endif
2444 assert_equal(true, res)
2445
2446 res = false
2447 if g:bool_true || false
2448 res = true
2449 endif
2450 assert_equal(true, res)
2451
2452 res = false
2453 if true || g:bool_false
2454 res = true
2455 endif
2456 assert_equal(true, res)
2457
2458 res = false
2459 if false || false
2460 res = true
2461 endif
2462 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02002463
2464 # with constant "false" expression may be invalid so long as the syntax is OK
Bram Moolenaarc3235272021-07-10 19:42:03 +02002465 if false | eval 1 + 2 | endif
Bram Moolenaar3988f642020-08-27 22:43:03 +02002466 if false | eval burp + 234 | endif
2467 if false | echo burp 234 'asd' | endif
2468 if false
2469 burp
2470 endif
Bram Moolenaare525bdd2021-08-07 18:12:40 +02002471
2472 # expression with line breaks skipped
2473 if false
2474 ('aaa'
2475 .. 'bbb'
2476 .. 'ccc'
2477 )->setline(1)
2478 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002479enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002480
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002481def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002482 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
2483 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
2484 CheckDefFailure(["if has('aaa'"], 'E110:')
2485 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002486enddef
2487
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002488def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002489 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002490 if i % 2
2491 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002492 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002493 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002494 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002495 endif
2496 x += 1
2497 else
2498 x += 1000
2499 endif
2500 return x
2501enddef
2502
2503def Test_nested_if()
2504 assert_equal(1, RunNested(1))
2505 assert_equal(1000, RunNested(2))
2506enddef
2507
Bram Moolenaarad39c092020-02-26 18:23:43 +01002508def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01002509 # missing argument is ignored
2510 execute
2511 execute # comment
2512
Bram Moolenaarad39c092020-02-26 18:23:43 +01002513 new
2514 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002515 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002516 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002517
Bram Moolenaard2c61702020-09-06 15:58:36 +02002518 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002519 assert_equal('execute-string', getline(1))
2520
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002521 var cmd1 = 'setline(1,'
2522 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002523 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01002524 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002525
Bram Moolenaard2c61702020-09-06 15:58:36 +02002526 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002527 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002528
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002529 var cmd_first = 'call '
2530 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002531 execute cmd_first .. cmd_last
2532 assert_equal('execute-var-var', getline(1))
2533 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02002534
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002535 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002536 execute 'echomsg' (n ? '"true"' : '"no"')
2537 assert_match('^true$', Screenline(&lines))
2538
Bram Moolenaare0de1712020-12-02 17:36:54 +01002539 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002540 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
2541
Bram Moolenaard2c61702020-09-06 15:58:36 +02002542 CheckDefFailure(['execute xxx'], 'E1001:', 1)
2543 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
2544 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002545enddef
2546
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002547def Test_execute_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002548 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002549 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002550 vim9script
2551 execute 'g:someVar'
2552 .. ' = ' ..
2553 '28'
2554 assert_equal(28, g:someVar)
2555 unlet g:someVar
2556 END
2557 CheckScriptSuccess(lines)
2558enddef
2559
Bram Moolenaarad39c092020-02-26 18:23:43 +01002560def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002561 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02002562 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002563 assert_match('^something$', Screenline(&lines))
2564
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002565 echo "some" # comment
2566 echon "thing"
2567 assert_match('^something$', Screenline(&lines))
2568
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002569 var str1 = 'some'
2570 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002571 echo str1 str2
2572 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002573
Bram Moolenaard2c61702020-09-06 15:58:36 +02002574 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01002575enddef
2576
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002577def Test_echomsg_cmd()
2578 echomsg 'some' 'more' # comment
2579 assert_match('^some more$', Screenline(&lines))
2580 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002581 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002582 assert_match('^some more$', Screenline(&lines))
2583
Bram Moolenaard2c61702020-09-06 15:58:36 +02002584 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002585enddef
2586
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002587def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002588 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002589 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002590 vim9script
2591 echomsg 'here'
2592 .. ' is ' ..
2593 'a message'
2594 assert_match('^here is a message$', Screenline(&lines))
2595 END
2596 CheckScriptSuccess(lines)
2597enddef
2598
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002599def Test_echoerr_cmd()
Bram Moolenaar7de62622021-08-07 15:05:47 +02002600 var local = 'local'
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002601 try
Bram Moolenaar7de62622021-08-07 15:05:47 +02002602 echoerr 'something' local 'wrong' # comment
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002603 catch
Bram Moolenaar7de62622021-08-07 15:05:47 +02002604 assert_match('something local wrong', v:exception)
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002605 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002606enddef
2607
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002608def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002609 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002610 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002611 vim9script
2612 try
2613 echoerr 'this'
2614 .. ' is ' ..
2615 'wrong'
2616 catch
2617 assert_match('this is wrong', v:exception)
2618 endtry
2619 END
2620 CheckScriptSuccess(lines)
2621enddef
2622
Bram Moolenaar7de62622021-08-07 15:05:47 +02002623def Test_echoconsole_cmd()
2624 var local = 'local'
2625 echoconsole 'something' local # comment
2626 # output goes anywhere
2627enddef
2628
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002629def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002630 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002631 vim9script
2632 new
2633 for var in range(0, 3)
2634 append(line('$'), var)
2635 endfor
2636 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2637 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01002638
2639 var result = ''
2640 for i in [1, 2, 3]
2641 var loop = ' loop ' .. i
2642 result ..= loop
2643 endfor
2644 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002645 END
2646 writefile(lines, 'Xvim9for.vim')
2647 source Xvim9for.vim
2648 delete('Xvim9for.vim')
2649enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650
rbtnnbebf0692021-08-21 17:26:50 +02002651def Test_for_skipped_block()
2652 # test skipped blocks at outside of function
2653 var lines =<< trim END
2654 var result = []
2655 if true
2656 for n in [1, 2]
2657 result += [n]
2658 endfor
2659 else
2660 for n in [3, 4]
2661 result += [n]
2662 endfor
2663 endif
2664 assert_equal([1, 2], result)
2665
2666 result = []
2667 if false
2668 for n in [1, 2]
2669 result += [n]
2670 endfor
2671 else
2672 for n in [3, 4]
2673 result += [n]
2674 endfor
2675 endif
2676 assert_equal([3, 4], result)
2677 END
2678 CheckDefAndScriptSuccess(lines)
2679
2680 # test skipped blocks at inside of function
2681 lines =<< trim END
2682 def DefTrue()
2683 var result = []
2684 if true
2685 for n in [1, 2]
2686 result += [n]
2687 endfor
2688 else
2689 for n in [3, 4]
2690 result += [n]
2691 endfor
2692 endif
2693 assert_equal([1, 2], result)
2694 enddef
2695 DefTrue()
2696
2697 def DefFalse()
2698 var result = []
2699 if false
2700 for n in [1, 2]
2701 result += [n]
2702 endfor
2703 else
2704 for n in [3, 4]
2705 result += [n]
2706 endfor
2707 endif
2708 assert_equal([3, 4], result)
2709 enddef
2710 DefFalse()
2711 END
2712 CheckDefAndScriptSuccess(lines)
2713enddef
2714
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002715def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002716 var lines =<< trim END
2717 var result = ''
2718 for cnt in range(7)
2719 if cnt == 4
2720 break
2721 endif
2722 if cnt == 2
2723 continue
2724 endif
2725 result ..= cnt .. '_'
2726 endfor
2727 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02002728
Bram Moolenaarf2253962021-04-13 20:53:13 +02002729 var concat = ''
2730 for str in eval('["one", "two"]')
2731 concat ..= str
2732 endfor
2733 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002734
Bram Moolenaarf2253962021-04-13 20:53:13 +02002735 var total = 0
2736 for nr in
2737 [1, 2, 3]
2738 total += nr
2739 endfor
2740 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002741
Bram Moolenaarf2253962021-04-13 20:53:13 +02002742 total = 0
2743 for nr
2744 in [1, 2, 3]
2745 total += nr
2746 endfor
2747 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002748
Bram Moolenaarf2253962021-04-13 20:53:13 +02002749 total = 0
2750 for nr
2751 in
2752 [1, 2, 3]
2753 total += nr
2754 endfor
2755 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01002756
Bram Moolenaara3589a02021-04-14 13:30:46 +02002757 # with type
2758 total = 0
2759 for n: number in [1, 2, 3]
2760 total += n
2761 endfor
2762 assert_equal(6, total)
2763
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002764 var chars = ''
2765 for s: string in 'foobar'
2766 chars ..= s
2767 endfor
2768 assert_equal('foobar', chars)
2769
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02002770 chars = ''
2771 for x: string in {a: 'a', b: 'b'}->values()
2772 chars ..= x
2773 endfor
2774 assert_equal('ab', chars)
2775
Bram Moolenaara3589a02021-04-14 13:30:46 +02002776 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02002777 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02002778 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2779 res ..= n .. s
2780 endfor
2781 assert_equal('1a2b', res)
2782
Bram Moolenaar444d8782021-06-26 12:40:56 +02002783 # unpack with one var
2784 var reslist = []
2785 for [x] in [['aaa'], ['bbb']]
2786 reslist->add(x)
2787 endfor
2788 assert_equal(['aaa', 'bbb'], reslist)
2789
Bram Moolenaara3589a02021-04-14 13:30:46 +02002790 # loop over string
2791 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02002792 for c in 'aéc̀d'
2793 res ..= c .. '-'
2794 endfor
2795 assert_equal('a-é-c̀-d-', res)
2796
2797 res = ''
2798 for c in ''
2799 res ..= c .. '-'
2800 endfor
2801 assert_equal('', res)
2802
2803 res = ''
2804 for c in test_null_string()
2805 res ..= c .. '-'
2806 endfor
2807 assert_equal('', res)
2808
2809 var foo: list<dict<any>> = [
2810 {a: 'Cat'}
2811 ]
2812 for dd in foo
2813 dd.counter = 12
2814 endfor
2815 assert_equal([{a: 'Cat', counter: 12}], foo)
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002816
2817 reslist = []
2818 for _ in range(3)
2819 reslist->add('x')
2820 endfor
2821 assert_equal(['x', 'x', 'x'], reslist)
Bram Moolenaarf2253962021-04-13 20:53:13 +02002822 END
2823 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002824enddef
2825
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02002826def Test_for_loop_with_closure()
2827 var lines =<< trim END
2828 var flist: list<func>
2829 for i in range(5)
2830 var inloop = i
2831 flist[i] = () => inloop
2832 endfor
2833 for i in range(5)
2834 assert_equal(4, flist[i]())
2835 endfor
2836 END
2837 CheckDefAndScriptSuccess(lines)
2838
2839 lines =<< trim END
2840 var flist: list<func>
2841 for i in range(5)
2842 var inloop = i
2843 flist[i] = () => {
2844 return inloop
2845 }
2846 endfor
2847 for i in range(5)
2848 assert_equal(4, flist[i]())
2849 endfor
2850 END
2851 CheckDefAndScriptSuccess(lines)
2852enddef
2853
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002854def Test_for_loop_fails()
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002855 CheckDefAndScriptFailure(['for '], ['E1097:', 'E690:'])
2856 CheckDefAndScriptFailure(['for x'], ['E1097:', 'E690:'])
2857 CheckDefAndScriptFailure(['for x in'], ['E1097:', 'E15:'])
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002858 CheckDefAndScriptFailure(['for # in range(5)'], 'E690:')
2859 CheckDefAndScriptFailure(['for i In range(5)'], 'E690:')
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002860 CheckDefAndScriptFailure(['var x = 5', 'for x in range(5)', 'endfor'], ['E1017:', 'E1041:'])
Bram Moolenaard4ab8072021-07-08 19:22:12 +02002861 CheckScriptFailure(['vim9script', 'var x = 5', 'for x in range(5)', '# comment', 'endfor'], 'E1041:', 3)
Bram Moolenaar822ba242020-05-24 23:00:18 +02002862 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002863 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02002864 CheckDefFailure(['for i in xxx'], 'E1001:')
2865 CheckDefFailure(['endfor'], 'E588:')
2866 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002867
2868 # wrong type detected at compile time
2869 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2870
2871 # wrong type detected at runtime
2872 g:adict = {a: 1}
2873 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2874 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002875
2876 var lines =<< trim END
2877 var d: list<dict<any>> = [{a: 0}]
2878 for e in d
2879 e = {a: 0, b: ''}
2880 endfor
2881 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002882 CheckDefAndScriptFailure(lines, ['E1018:', 'E46:'], 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002883
2884 lines =<< trim END
2885 for nr: number in ['foo']
2886 endfor
2887 END
2888 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaar404557e2021-07-05 21:41:48 +02002889
2890 lines =<< trim END
2891 for n : number in [1, 2]
2892 echo n
2893 endfor
2894 END
2895 CheckDefAndScriptFailure(lines, 'E1059:', 1)
Bram Moolenaarefc5db52021-07-07 21:21:30 +02002896
2897 lines =<< trim END
2898 var d: dict<number> = {a: 1, b: 2}
2899 for [k: job, v: job] in d->items()
2900 echo k v
2901 endfor
2902 END
2903 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected job but got string', 2)
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002904
2905 lines =<< trim END
2906 var i = 0
2907 for i in [1, 2, 3]
2908 echo i
2909 endfor
2910 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002911 CheckDefExecAndScriptFailure(lines, ['E1017:', 'E1041:'])
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002912
2913 lines =<< trim END
2914 var l = [0]
2915 for l[0] in [1, 2, 3]
2916 echo l[0]
2917 endfor
2918 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002919 CheckDefExecAndScriptFailure(lines, ['E461:', 'E1017:'])
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002920
2921 lines =<< trim END
2922 var d = {x: 0}
2923 for d.x in [1, 2, 3]
2924 echo d.x
2925 endfor
2926 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002927 CheckDefExecAndScriptFailure(lines, ['E461:', 'E1017:'])
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002928enddef
2929
Bram Moolenaarea870692020-12-02 14:24:30 +01002930def Test_for_loop_script_var()
2931 # cannot use s:var in a :def function
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002932 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E461:')
Bram Moolenaarea870692020-12-02 14:24:30 +01002933
2934 # can use s:var in Vim9 script, with or without s:
2935 var lines =<< trim END
2936 vim9script
2937 var total = 0
2938 for s:var in [1, 2, 3]
2939 total += s:var
2940 endfor
2941 assert_equal(6, total)
2942
2943 total = 0
2944 for var in [1, 2, 3]
2945 total += var
2946 endfor
2947 assert_equal(6, total)
2948 END
2949enddef
2950
Bram Moolenaar792f7862020-11-23 08:31:18 +01002951def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01002952 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002953 var result = []
2954 for [v1, v2] in [[1, 2], [3, 4]]
2955 result->add(v1)
2956 result->add(v2)
2957 endfor
2958 assert_equal([1, 2, 3, 4], result)
2959
2960 result = []
2961 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2962 result->add(v1)
2963 result->add(v2)
2964 result->add(v3)
2965 endfor
2966 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
2967
2968 result = []
2969 for [&ts, &sw] in [[1, 2], [3, 4]]
2970 result->add(&ts)
2971 result->add(&sw)
2972 endfor
2973 assert_equal([1, 2, 3, 4], result)
2974
2975 var slist: list<string>
2976 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
2977 slist->add($LOOPVAR)
2978 slist->add(@r)
2979 slist->add(v:errmsg)
2980 endfor
2981 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
2982
2983 slist = []
2984 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
2985 slist->add(g:globalvar)
2986 slist->add(b:bufvar)
2987 slist->add(w:winvar)
2988 slist->add(t:tabvar)
2989 endfor
2990 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01002991 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaarb777da92021-05-22 21:40:39 +02002992
2993 var res = []
2994 for [_, n, _] in [[1, 2, 3], [4, 5, 6]]
2995 res->add(n)
2996 endfor
2997 assert_equal([2, 5], res)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002998 END
2999 CheckDefAndScriptSuccess(lines)
3000
3001 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01003002 for [v1, v2] in [[1, 2, 3], [3, 4]]
3003 echo v1 v2
3004 endfor
3005 END
3006 CheckDefExecFailure(lines, 'E710:', 1)
3007
3008 lines =<< trim END
3009 for [v1, v2] in [[1], [3, 4]]
3010 echo v1 v2
3011 endfor
3012 END
3013 CheckDefExecFailure(lines, 'E711:', 1)
3014
3015 lines =<< trim END
3016 for [v1, v1] in [[1, 2], [3, 4]]
3017 echo v1
3018 endfor
3019 END
3020 CheckDefExecFailure(lines, 'E1017:', 1)
3021enddef
3022
Bram Moolenaarc150c092021-02-13 15:02:46 +01003023def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02003024 var lines =<< trim END
3025 var looped = 0
3026 var cleanup = 0
3027 for i in range(3)
3028 looped += 1
3029 try
3030 eval [][0]
3031 catch
3032 continue
3033 finally
3034 cleanup += 1
3035 endtry
3036 endfor
3037 assert_equal(3, looped)
3038 assert_equal(3, cleanup)
3039 END
3040 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003041enddef
3042
rbtnnd895b1d2021-08-20 20:54:25 +02003043def Test_while_skipped_block()
3044 # test skipped blocks at outside of function
3045 var lines =<< trim END
3046 var result = []
3047 var n = 0
3048 if true
3049 n = 1
3050 while n < 3
3051 result += [n]
3052 n += 1
3053 endwhile
3054 else
3055 n = 3
3056 while n < 5
3057 result += [n]
3058 n += 1
3059 endwhile
3060 endif
3061 assert_equal([1, 2], result)
3062
3063 result = []
3064 if false
3065 n = 1
3066 while n < 3
3067 result += [n]
3068 n += 1
3069 endwhile
3070 else
3071 n = 3
3072 while n < 5
3073 result += [n]
3074 n += 1
3075 endwhile
3076 endif
3077 assert_equal([3, 4], result)
3078 END
3079 CheckDefAndScriptSuccess(lines)
3080
3081 # test skipped blocks at inside of function
3082 lines =<< trim END
3083 def DefTrue()
3084 var result = []
3085 var n = 0
3086 if true
3087 n = 1
3088 while n < 3
3089 result += [n]
3090 n += 1
3091 endwhile
3092 else
3093 n = 3
3094 while n < 5
3095 result += [n]
3096 n += 1
3097 endwhile
3098 endif
3099 assert_equal([1, 2], result)
3100 enddef
3101 DefTrue()
3102
3103 def DefFalse()
3104 var result = []
3105 var n = 0
3106 if false
3107 n = 1
3108 while n < 3
3109 result += [n]
3110 n += 1
3111 endwhile
3112 else
3113 n = 3
3114 while n < 5
3115 result += [n]
3116 n += 1
3117 endwhile
3118 endif
3119 assert_equal([3, 4], result)
3120 enddef
3121 DefFalse()
3122 END
3123 CheckDefAndScriptSuccess(lines)
3124enddef
3125
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003126def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003127 var result = ''
3128 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003129 while cnt < 555
3130 if cnt == 3
3131 break
3132 endif
3133 cnt += 1
3134 if cnt == 2
3135 continue
3136 endif
3137 result ..= cnt .. '_'
3138 endwhile
3139 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01003140
3141 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01003142 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01003143 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003144enddef
3145
Bram Moolenaar7a53f292021-11-22 18:31:02 +00003146def Test_while_loop_in_script()
3147 var lines =<< trim END
3148 vim9script
3149 var result = ''
3150 var cnt = 0
3151 while cnt < 3
3152 var s = 'v' .. cnt
3153 result ..= s
3154 cnt += 1
3155 endwhile
3156 assert_equal('v0v1v2', result)
3157 END
3158 CheckScriptSuccess(lines)
3159enddef
3160
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003161def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02003162 CheckDefFailure(['while xxx'], 'E1001:')
3163 CheckDefFailure(['endwhile'], 'E588:')
3164 CheckDefFailure(['continue'], 'E586:')
3165 CheckDefFailure(['if true', 'continue'], 'E586:')
3166 CheckDefFailure(['break'], 'E587:')
3167 CheckDefFailure(['if true', 'break'], 'E587:')
3168 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01003169
3170 var lines =<< trim END
3171 var s = ''
3172 while s = ''
3173 endwhile
3174 END
3175 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003176enddef
3177
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003178def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003179 var caught = false
3180 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003181 try
3182 while 1
3183 x += 1
3184 if x == 100
3185 feedkeys("\<C-C>", 'Lt')
3186 endif
3187 endwhile
3188 catch
3189 caught = true
3190 assert_equal(100, x)
3191 endtry
3192 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003193 # consume the CTRL-C
3194 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003195enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01003196
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003197def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003198 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003199 'one',
3200 'two',
3201 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003202 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003203 assert_equal(['one', 'two', 'three'], mylist)
3204
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003205 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003206 ['one']: 1,
3207 ['two']: 2,
3208 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003209 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003210 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01003211 assert_equal({one: 1, two: 2, three: 3}, mydict)
3212 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003213 one: 1, # comment
3214 two: # comment
3215 2, # comment
3216 three: 3 # comment
3217 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003218 assert_equal({one: 1, two: 2, three: 3}, mydict)
3219 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003220 one: 1,
3221 two:
3222 2,
3223 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003224 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003225 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02003226
3227 assert_equal(
3228 ['one', 'two', 'three'],
3229 split('one two three')
3230 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003231enddef
3232
Bram Moolenaar7a092242020-04-16 22:10:49 +02003233def Test_vim9_comment()
3234 CheckScriptSuccess([
3235 'vim9script',
3236 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01003237 '#something',
3238 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003239 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01003240
3241 split Xfile
3242 CheckScriptSuccess([
3243 'vim9script',
3244 'edit #something',
3245 ])
3246 CheckScriptSuccess([
3247 'vim9script',
3248 'edit #{something',
3249 ])
3250 close
3251
Bram Moolenaar7a092242020-04-16 22:10:49 +02003252 CheckScriptFailure([
3253 'vim9script',
3254 ':# something',
3255 ], 'E488:')
3256 CheckScriptFailure([
3257 '# something',
3258 ], 'E488:')
3259 CheckScriptFailure([
3260 ':# something',
3261 ], 'E488:')
3262
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003263 { # block start
3264 } # block end
3265 CheckDefFailure([
3266 '{# comment',
3267 ], 'E488:')
3268 CheckDefFailure([
3269 '{',
3270 '}# comment',
3271 ], 'E488:')
3272
3273 echo "yes" # comment
3274 CheckDefFailure([
3275 'echo "yes"# comment',
3276 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003277 CheckScriptSuccess([
3278 'vim9script',
3279 'echo "yes" # something',
3280 ])
3281 CheckScriptFailure([
3282 'vim9script',
3283 'echo "yes"# something',
3284 ], 'E121:')
3285 CheckScriptFailure([
3286 'vim9script',
3287 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003288 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003289 CheckScriptFailure([
3290 'echo "yes" # something',
3291 ], 'E121:')
3292
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003293 exe "echo" # comment
3294 CheckDefFailure([
3295 'exe "echo"# comment',
3296 ], 'E488:')
3297 CheckScriptSuccess([
3298 'vim9script',
3299 'exe "echo" # something',
3300 ])
3301 CheckScriptFailure([
3302 'vim9script',
3303 'exe "echo"# something',
3304 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003305 CheckScriptFailure([
3306 'vim9script',
3307 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003308 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003309 CheckScriptFailure([
3310 'exe "echo" # something',
3311 ], 'E121:')
3312
Bram Moolenaar7a092242020-04-16 22:10:49 +02003313 CheckDefFailure([
3314 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003315 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003316 'catch',
3317 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003318 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003319 CheckScriptFailure([
3320 'vim9script',
3321 'try# comment',
3322 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003323 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003324 CheckDefFailure([
3325 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003326 ' throw#comment',
3327 'catch',
3328 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003329 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003330 CheckDefFailure([
3331 'try',
3332 ' throw "yes"#comment',
3333 'catch',
3334 'endtry',
3335 ], 'E488:')
3336 CheckDefFailure([
3337 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003338 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003339 'catch# comment',
3340 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003341 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003342 CheckScriptFailure([
3343 'vim9script',
3344 'try',
3345 ' echo "yes"',
3346 'catch# comment',
3347 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003348 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003349 CheckDefFailure([
3350 'try',
3351 ' echo "yes"',
3352 'catch /pat/# comment',
3353 'endtry',
3354 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003355 CheckDefFailure([
3356 'try',
3357 'echo "yes"',
3358 'catch',
3359 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003360 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003361 CheckScriptFailure([
3362 'vim9script',
3363 'try',
3364 ' echo "yes"',
3365 'catch',
3366 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003367 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003368
3369 CheckScriptSuccess([
3370 'vim9script',
3371 'hi # comment',
3372 ])
3373 CheckScriptFailure([
3374 'vim9script',
3375 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003376 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003377 CheckScriptSuccess([
3378 'vim9script',
3379 'hi Search # comment',
3380 ])
3381 CheckScriptFailure([
3382 'vim9script',
3383 'hi Search# comment',
3384 ], 'E416:')
3385 CheckScriptSuccess([
3386 'vim9script',
3387 'hi link This Search # comment',
3388 ])
3389 CheckScriptFailure([
3390 'vim9script',
3391 'hi link This That# comment',
3392 ], 'E413:')
3393 CheckScriptSuccess([
3394 'vim9script',
3395 'hi clear This # comment',
3396 'hi clear # comment',
3397 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003398 # not tested, because it doesn't give an error but a warning:
3399 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02003400 CheckScriptFailure([
3401 'vim9script',
3402 'hi clear# comment',
3403 ], 'E416:')
3404
3405 CheckScriptSuccess([
3406 'vim9script',
3407 'hi Group term=bold',
3408 'match Group /todo/ # comment',
3409 ])
3410 CheckScriptFailure([
3411 'vim9script',
3412 'hi Group term=bold',
3413 'match Group /todo/# comment',
3414 ], 'E488:')
3415 CheckScriptSuccess([
3416 'vim9script',
3417 'match # comment',
3418 ])
3419 CheckScriptFailure([
3420 'vim9script',
3421 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003422 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003423 CheckScriptSuccess([
3424 'vim9script',
3425 'match none # comment',
3426 ])
3427 CheckScriptFailure([
3428 'vim9script',
3429 'match none# comment',
3430 ], 'E475:')
3431
3432 CheckScriptSuccess([
3433 'vim9script',
3434 'menutrans clear # comment',
3435 ])
3436 CheckScriptFailure([
3437 'vim9script',
3438 'menutrans clear# comment text',
3439 ], 'E474:')
3440
3441 CheckScriptSuccess([
3442 'vim9script',
3443 'syntax clear # comment',
3444 ])
3445 CheckScriptFailure([
3446 'vim9script',
3447 'syntax clear# comment text',
3448 ], 'E28:')
3449 CheckScriptSuccess([
3450 'vim9script',
3451 'syntax keyword Word some',
3452 'syntax clear Word # comment',
3453 ])
3454 CheckScriptFailure([
3455 'vim9script',
3456 'syntax keyword Word some',
3457 'syntax clear Word# comment text',
3458 ], 'E28:')
3459
3460 CheckScriptSuccess([
3461 'vim9script',
3462 'syntax list # comment',
3463 ])
3464 CheckScriptFailure([
3465 'vim9script',
3466 'syntax list# comment text',
3467 ], 'E28:')
3468
3469 CheckScriptSuccess([
3470 'vim9script',
3471 'syntax match Word /pat/ oneline # comment',
3472 ])
3473 CheckScriptFailure([
3474 'vim9script',
3475 'syntax match Word /pat/ oneline# comment',
3476 ], 'E475:')
3477
3478 CheckScriptSuccess([
3479 'vim9script',
3480 'syntax keyword Word word # comm[ent',
3481 ])
3482 CheckScriptFailure([
3483 'vim9script',
3484 'syntax keyword Word word# comm[ent',
3485 ], 'E789:')
3486
3487 CheckScriptSuccess([
3488 'vim9script',
3489 'syntax match Word /pat/ # comment',
3490 ])
3491 CheckScriptFailure([
3492 'vim9script',
3493 'syntax match Word /pat/# comment',
3494 ], 'E402:')
3495
3496 CheckScriptSuccess([
3497 'vim9script',
3498 'syntax match Word /pat/ contains=Something # comment',
3499 ])
3500 CheckScriptFailure([
3501 'vim9script',
3502 'syntax match Word /pat/ contains=Something# comment',
3503 ], 'E475:')
3504 CheckScriptFailure([
3505 'vim9script',
3506 'syntax match Word /pat/ contains= # comment',
3507 ], 'E406:')
3508 CheckScriptFailure([
3509 'vim9script',
3510 'syntax match Word /pat/ contains=# comment',
3511 ], 'E475:')
3512
3513 CheckScriptSuccess([
3514 'vim9script',
3515 'syntax region Word start=/pat/ end=/pat/ # comment',
3516 ])
3517 CheckScriptFailure([
3518 'vim9script',
3519 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02003520 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003521
3522 CheckScriptSuccess([
3523 'vim9script',
3524 'syntax sync # comment',
3525 ])
3526 CheckScriptFailure([
3527 'vim9script',
3528 'syntax sync# comment',
3529 ], 'E404:')
3530 CheckScriptSuccess([
3531 'vim9script',
3532 'syntax sync ccomment # comment',
3533 ])
3534 CheckScriptFailure([
3535 'vim9script',
3536 'syntax sync ccomment# comment',
3537 ], 'E404:')
3538
3539 CheckScriptSuccess([
3540 'vim9script',
3541 'syntax cluster Some contains=Word # comment',
3542 ])
3543 CheckScriptFailure([
3544 'vim9script',
3545 'syntax cluster Some contains=Word# comment',
3546 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003547
3548 CheckScriptSuccess([
3549 'vim9script',
3550 'command Echo echo # comment',
3551 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003552 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003553 ])
3554 CheckScriptFailure([
3555 'vim9script',
3556 'command Echo echo# comment',
3557 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003558 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003559 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003560
3561 var curdir = getcwd()
3562 CheckScriptSuccess([
3563 'command Echo cd " comment',
3564 'Echo',
3565 'delcommand Echo',
3566 ])
3567 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01003568 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003569 'command Echo cd # comment',
3570 'Echo',
3571 'delcommand Echo',
3572 ])
3573 CheckScriptFailure([
3574 'vim9script',
3575 'command Echo cd " comment',
3576 'Echo',
3577 ], 'E344:')
3578 delcommand Echo
3579 chdir(curdir)
3580
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003581 CheckScriptFailure([
3582 'vim9script',
3583 'command Echo# comment',
3584 ], 'E182:')
3585 CheckScriptFailure([
3586 'vim9script',
3587 'command Echo echo',
3588 'command Echo# comment',
3589 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003590 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003591
3592 CheckScriptSuccess([
3593 'vim9script',
3594 'function # comment',
3595 ])
3596 CheckScriptFailure([
3597 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02003598 'function " comment',
3599 ], 'E129:')
3600 CheckScriptFailure([
3601 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003602 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003603 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003604 CheckScriptSuccess([
3605 'vim9script',
3606 'function CheckScriptSuccess # comment',
3607 ])
3608 CheckScriptFailure([
3609 'vim9script',
3610 'function CheckScriptSuccess# comment',
3611 ], 'E488:')
3612
3613 CheckScriptSuccess([
3614 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003615 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003616 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003617 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003618 ])
3619 CheckScriptFailure([
3620 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003621 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003622 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003623 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003624 ], 'E488:')
3625
3626 CheckScriptSuccess([
3627 'vim9script',
3628 'call execute("ls") # comment',
3629 ])
3630 CheckScriptFailure([
3631 'vim9script',
3632 'call execute("ls")# comment',
3633 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02003634
3635 CheckScriptFailure([
3636 'def Test() " comment',
3637 'enddef',
3638 ], 'E488:')
3639 CheckScriptFailure([
3640 'vim9script',
3641 'def Test() " comment',
3642 'enddef',
3643 ], 'E488:')
3644
3645 CheckScriptSuccess([
3646 'func Test() " comment',
3647 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003648 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02003649 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02003650 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02003651 'vim9script',
3652 'func Test() " comment',
3653 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02003654 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02003655
3656 CheckScriptSuccess([
3657 'def Test() # comment',
3658 'enddef',
3659 ])
3660 CheckScriptFailure([
3661 'func Test() # comment',
3662 'endfunc',
3663 ], 'E488:')
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003664
3665 var lines =<< trim END
3666 vim9script
3667 syn region Text
3668 \ start='foo'
3669 #\ comment
3670 \ end='bar'
Bram Moolenaar5072b472021-06-03 21:56:10 +02003671 syn region Text start='foo'
3672 #\ comment
3673 \ end='bar'
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003674 END
3675 CheckScriptSuccess(lines)
3676
3677 lines =<< trim END
3678 vim9script
3679 syn region Text
3680 \ start='foo'
3681 "\ comment
3682 \ end='bar'
3683 END
3684 CheckScriptFailure(lines, 'E399:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003685enddef
3686
3687def Test_vim9_comment_gui()
3688 CheckCanRunGui
3689
3690 CheckScriptFailure([
3691 'vim9script',
3692 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003693 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003694 CheckScriptFailure([
3695 'vim9script',
3696 'gui -f#comment'
Bram Moolenaarb8bd2e62021-08-21 17:13:14 +02003697 ], 'E194:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003698enddef
3699
Bram Moolenaara26b9702020-04-18 19:53:28 +02003700def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02003701 au TabEnter *.vim g:entered = 1
3702 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02003703
3704 edit test.vim
3705 doautocmd TabEnter #comment
3706 assert_equal(1, g:entered)
3707
3708 doautocmd TabEnter f.x
3709 assert_equal(2, g:entered)
3710
3711 g:entered = 0
3712 doautocmd TabEnter f.x #comment
3713 assert_equal(2, g:entered)
3714
3715 assert_fails('doautocmd Syntax#comment', 'E216:')
3716
3717 au! TabEnter
3718 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003719
3720 CheckScriptSuccess([
3721 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02003722 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003723 'b:var = 456',
3724 'w:var = 777',
3725 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003726 'unlet g:var w:var # something',
3727 ])
3728
3729 CheckScriptFailure([
3730 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003731 'let var = 123',
3732 ], 'E1126: Cannot use :let in Vim9 script')
3733
3734 CheckScriptFailure([
3735 'vim9script',
3736 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003737 ], 'E1016: Cannot declare a global variable:')
3738
3739 CheckScriptFailure([
3740 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003741 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003742 ], 'E1016: Cannot declare a buffer variable:')
3743
3744 CheckScriptFailure([
3745 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003746 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003747 ], 'E1016: Cannot declare a window variable:')
3748
3749 CheckScriptFailure([
3750 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003751 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003752 ], 'E1016: Cannot declare a tab variable:')
3753
3754 CheckScriptFailure([
3755 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003756 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003757 ], 'E1016: Cannot declare a v: variable:')
3758
3759 CheckScriptFailure([
3760 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003761 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003762 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02003763
3764 CheckScriptFailure([
3765 'vim9script',
3766 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003767 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003768 ], 'E108:')
3769
3770 CheckScriptFailure([
3771 'let g:var = 123',
3772 'unlet g:var # something',
3773 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003774
3775 CheckScriptSuccess([
3776 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003777 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003778 ' echo "yes"',
3779 'elseif 2 #comment',
3780 ' echo "no"',
3781 'endif',
3782 ])
3783
3784 CheckScriptFailure([
3785 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003786 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003787 ' echo "yes"',
3788 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003789 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003790
3791 CheckScriptFailure([
3792 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003793 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003794 ' echo "yes"',
3795 'elseif 2#comment',
3796 ' echo "no"',
3797 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003798 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003799
3800 CheckScriptSuccess([
3801 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003802 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003803 ])
3804
3805 CheckScriptFailure([
3806 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003807 'var v = 1# comment6',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003808 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003809
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003810 CheckScriptSuccess([
3811 'vim9script',
3812 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003813 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003814 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003815 'dsearch /pat/ #comment',
3816 'bwipe!',
3817 ])
3818
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003819 CheckScriptFailure([
3820 'vim9script',
3821 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003822 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003823 ':$',
3824 'dsearch /pat/#comment',
3825 'bwipe!',
3826 ], 'E488:')
3827
3828 CheckScriptFailure([
3829 'vim9script',
3830 'func! SomeFunc()',
3831 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02003832enddef
3833
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003834def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003835 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003836 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02003837 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003838 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02003839 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003840 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02003841 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003842 END
3843 writefile(lines, 'Xfinished')
3844 source Xfinished
3845 assert_equal('two', g:res)
3846
3847 unlet g:res
3848 delete('Xfinished')
3849enddef
3850
Bram Moolenaara5d00772020-05-14 23:20:55 +02003851def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003852 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02003853 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02003854 def GetValue(): string
3855 return theVal
3856 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003857 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02003858 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02003859 theVal = 'else'
3860 g:laterVal = GetValue()
3861 END
3862 writefile(lines, 'Xforward')
3863 source Xforward
3864 assert_equal('something', g:initVal)
3865 assert_equal('else', g:laterVal)
3866
3867 unlet g:initVal
3868 unlet g:laterVal
3869 delete('Xforward')
3870enddef
3871
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003872def Test_source_vim9_from_legacy()
Bram Moolenaara6294952020-12-27 13:39:50 +01003873 var vim9_lines =<< trim END
3874 vim9script
3875 var local = 'local'
3876 g:global = 'global'
3877 export var exported = 'exported'
3878 export def GetText(): string
3879 return 'text'
3880 enddef
3881 END
3882 writefile(vim9_lines, 'Xvim9_script.vim')
3883
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003884 var legacy_lines =<< trim END
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003885 source Xvim9_script.vim
3886
3887 call assert_false(exists('local'))
3888 call assert_false(exists('exported'))
3889 call assert_false(exists('s:exported'))
3890 call assert_equal('global', global)
3891 call assert_equal('global', g:global)
3892
3893 " imported variable becomes script-local
3894 import exported from './Xvim9_script.vim'
3895 call assert_equal('exported', s:exported)
3896 call assert_false(exists('exported'))
3897
3898 " imported function becomes script-local
3899 import GetText from './Xvim9_script.vim'
3900 call assert_equal('text', s:GetText())
3901 call assert_false(exists('*GetText'))
3902 END
3903 writefile(legacy_lines, 'Xlegacy_script.vim')
3904
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003905 source Xlegacy_script.vim
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003906 assert_equal('global', g:global)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003907 unlet g:global
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003908
3909 delete('Xlegacy_script.vim')
3910 delete('Xvim9_script.vim')
3911enddef
Bram Moolenaara5d00772020-05-14 23:20:55 +02003912
Bram Moolenaare535db82021-03-31 21:07:24 +02003913def Test_declare_script_in_func()
3914 var lines =<< trim END
3915 vim9script
3916 func Declare()
3917 let s:local = 123
3918 endfunc
3919 Declare()
3920 assert_equal(123, local)
3921
3922 var error: string
3923 try
3924 local = 'asdf'
3925 catch
3926 error = v:exception
3927 endtry
3928 assert_match('E1012: Type mismatch; expected number but got string', error)
3929
3930 lockvar local
3931 try
3932 local = 999
3933 catch
3934 error = v:exception
3935 endtry
3936 assert_match('E741: Value is locked: local', error)
3937 END
3938 CheckScriptSuccess(lines)
3939enddef
3940
3941
Bram Moolenaar7d699702020-08-14 20:52:28 +02003942func Test_vim9script_not_global()
3943 " check that items defined in Vim9 script are script-local, not global
3944 let vim9lines =<< trim END
3945 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003946 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02003947 func TheFunc()
3948 echo 'local'
3949 endfunc
3950 def DefFunc()
3951 echo 'local'
3952 enddef
3953 END
3954 call writefile(vim9lines, 'Xvim9script.vim')
3955 source Xvim9script.vim
3956 try
3957 echo g:var
3958 assert_report('did not fail')
3959 catch /E121:/
3960 " caught
3961 endtry
3962 try
3963 call TheFunc()
3964 assert_report('did not fail')
3965 catch /E117:/
3966 " caught
3967 endtry
3968 try
3969 call DefFunc()
3970 assert_report('did not fail')
3971 catch /E117:/
3972 " caught
3973 endtry
3974
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003975 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02003976endfunc
3977
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02003978def Test_vim9_copen()
3979 # this was giving an error for setting w:quickfix_title
3980 copen
3981 quit
3982enddef
3983
Bram Moolenaar03290b82020-12-19 16:30:44 +01003984" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003985def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003986 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003987 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01003988 def some#gettest(): string
3989 return 'test'
3990 enddef
3991 g:some#name = 'name'
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003992 g:some#dict = {key: 'value'}
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003993
3994 def some#varargs(a1: string, ...l: list<string>): string
3995 return a1 .. l[0] .. l[1]
3996 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01003997 END
3998
3999 mkdir('Xdir/autoload', 'p')
4000 writefile(lines, 'Xdir/autoload/some.vim')
4001 var save_rtp = &rtp
4002 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4003
4004 assert_equal('test', g:some#gettest())
4005 assert_equal('name', g:some#name)
Bram Moolenaar227c58a2021-04-28 20:40:44 +02004006 assert_equal('value', g:some#dict.key)
Bram Moolenaar03290b82020-12-19 16:30:44 +01004007 g:some#other = 'other'
4008 assert_equal('other', g:some#other)
4009
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01004010 assert_equal('abc', some#varargs('a', 'b', 'c'))
4011
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004012 # upper case script name works
4013 lines =<< trim END
4014 vim9script
4015 def Other#getOther(): string
4016 return 'other'
4017 enddef
4018 END
4019 writefile(lines, 'Xdir/autoload/Other.vim')
4020 assert_equal('other', g:Other#getOther())
4021
Bram Moolenaar03290b82020-12-19 16:30:44 +01004022 delete('Xdir', 'rf')
4023 &rtp = save_rtp
4024enddef
4025
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00004026" test disassembling an auto-loaded function starting with "debug"
4027def Test_vim9_autoload_disass()
4028 mkdir('Xdir/autoload', 'p')
4029 var save_rtp = &rtp
4030 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4031
4032 var lines =<< trim END
4033 vim9script
4034 def debugit#test(): string
4035 return 'debug'
4036 enddef
4037 END
4038 writefile(lines, 'Xdir/autoload/debugit.vim')
4039
4040 lines =<< trim END
4041 vim9script
4042 def profileit#test(): string
4043 return 'profile'
4044 enddef
4045 END
4046 writefile(lines, 'Xdir/autoload/profileit.vim')
4047
4048 lines =<< trim END
4049 vim9script
4050 assert_equal('debug', debugit#test())
4051 disass debugit#test
4052 assert_equal('profile', profileit#test())
4053 disass profileit#test
4054 END
4055 CheckScriptSuccess(lines)
4056
4057 delete('Xdir', 'rf')
4058 &rtp = save_rtp
4059enddef
4060
Bram Moolenaar03290b82020-12-19 16:30:44 +01004061" test using a vim9script that is auto-loaded from an autocmd
4062def Test_vim9_aucmd_autoload()
4063 var lines =<< trim END
4064 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004065 def foo#test()
4066 echomsg getreg('"')
4067 enddef
4068 END
4069
4070 mkdir('Xdir/autoload', 'p')
4071 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004072 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004073 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4074 augroup test
4075 autocmd TextYankPost * call foo#test()
4076 augroup END
4077
4078 normal Y
4079
4080 augroup test
4081 autocmd!
4082 augroup END
4083 delete('Xdir', 'rf')
4084 &rtp = save_rtp
4085enddef
4086
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004087" This was causing a crash because suppress_errthrow wasn't reset.
4088def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004089 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004090 vim9script
4091 def crash#func()
4092 try
4093 for x in List()
4094 endfor
4095 catch
4096 endtry
4097 g:ok = true
4098 enddef
4099 fu List()
4100 invalid
4101 endfu
4102 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01004103 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004104 catch /wontmatch/
4105 endtry
4106 END
4107 call mkdir('Xruntime/autoload', 'p')
4108 call writefile(lines, 'Xruntime/autoload/crash.vim')
4109
4110 # run in a separate Vim to avoid the side effects of assert_fails()
4111 lines =<< trim END
4112 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
4113 call crash#func()
4114 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004115 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004116 END
4117 writefile(lines, 'Xscript')
4118 RunVim([], [], '-S Xscript')
4119 assert_equal(['ok'], readfile('Xdidit'))
4120
4121 delete('Xdidit')
4122 delete('Xscript')
4123 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01004124
4125 lines =<< trim END
4126 vim9script
4127 var foo#bar = 'asdf'
4128 END
4129 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004130enddef
4131
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02004132def Test_script_var_in_autocmd()
4133 # using a script variable from an autocommand, defined in a :def function in a
4134 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004135 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02004136 let s:counter = 1
4137 def s:Func()
4138 au! CursorHold
4139 au CursorHold * s:counter += 1
4140 enddef
4141 call s:Func()
4142 doau CursorHold
4143 call assert_equal(2, s:counter)
4144 au! CursorHold
4145 END
4146 CheckScriptSuccess(lines)
4147enddef
4148
Bram Moolenaarb5841b92021-07-15 18:09:53 +02004149def Test_error_in_autoload_script()
4150 var save_rtp = &rtp
4151 var dir = getcwd() .. '/Xruntime'
4152 &rtp = dir
4153 mkdir(dir .. '/autoload', 'p')
4154
4155 var lines =<< trim END
4156 vim9script noclear
4157 def script#autoloaded()
4158 enddef
4159 def Broken()
4160 var x: any = ''
4161 eval x != 0
4162 enddef
4163 Broken()
4164 END
4165 writefile(lines, dir .. '/autoload/script.vim')
4166
4167 lines =<< trim END
4168 vim9script
4169 def CallAutoloaded()
4170 script#autoloaded()
4171 enddef
4172
4173 function Legacy()
4174 try
4175 call s:CallAutoloaded()
4176 catch
4177 call assert_match('E1030: Using a String as a Number', v:exception)
4178 endtry
4179 endfunction
4180
4181 Legacy()
4182 END
4183 CheckScriptSuccess(lines)
4184
4185 &rtp = save_rtp
4186 delete(dir, 'rf')
4187enddef
4188
Bram Moolenaar3896a102020-08-09 14:33:55 +02004189def Test_cmdline_win()
4190 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
4191 # the command line window.
4192 mkdir('rtp/syntax', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004193 var export_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02004194 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004195 export var That = 'yes'
Bram Moolenaar3896a102020-08-09 14:33:55 +02004196 END
4197 writefile(export_lines, 'rtp/syntax/Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004198 var import_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02004199 vim9script
4200 import That from './Xexport.vim'
4201 END
4202 writefile(import_lines, 'rtp/syntax/vim.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004203 var save_rtp = &rtp
Bram Moolenaar3896a102020-08-09 14:33:55 +02004204 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
4205 syntax on
4206 augroup CmdWin
4207 autocmd CmdwinEnter * g:got_there = 'yes'
4208 augroup END
4209 # this will open and also close the cmdline window
4210 feedkeys('q:', 'xt')
4211 assert_equal('yes', g:got_there)
4212
4213 augroup CmdWin
4214 au!
4215 augroup END
4216 &rtp = save_rtp
4217 delete('rtp', 'rf')
4218enddef
4219
Bram Moolenaare3d46852020-08-29 13:39:17 +02004220def Test_invalid_sid()
4221 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02004222
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004223 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02004224 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02004225 endif
4226 delete('Xdidit')
4227enddef
4228
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004229def Test_restoring_cpo()
4230 writefile(['vim9script', 'set nocp'], 'Xsourced')
4231 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
4232 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
4233 assert_equal(['done'], readfile('Xdone'))
4234 endif
4235 delete('Xsourced')
4236 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01004237 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01004238
4239 writefile(['vim9script'], 'XanotherScript')
4240 set cpo=aABceFsMny>
4241 edit XanotherScript
4242 so %
4243 assert_equal('aABceFsMny>', &cpo)
4244 :1del
4245 w
4246 so %
4247 assert_equal('aABceFsMny>', &cpo)
4248
4249 delete('XanotherScript')
4250 set cpo&vim
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004251enddef
4252
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004253" Use :function so we can use Check commands
4254func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004255 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004256 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004257
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004258 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004259 vim9script
4260 def script#func()
4261 enddef
4262 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004263 call mkdir('Xdir/autoload', 'p')
4264 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004265
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004266 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004267 vim9script
4268 set cpo+=M
4269 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02004270 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004271 setline(1, 'some text')
4272 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004273 call writefile(lines, 'XTest_redraw_cpo')
4274 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
4275 call term_sendkeys(buf, "V:")
4276 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004277
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004278 " clean up
4279 call term_sendkeys(buf, "\<Esc>u")
4280 call StopVimInTerminal(buf)
4281 call delete('XTest_redraw_cpo')
4282 call delete('Xdir', 'rf')
4283endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004284
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004285
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02004286def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004287 var lines =<< trim END
4288 var name: any
4289 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02004290 END
4291 CheckDefAndScriptSuccess(lines)
4292enddef
4293
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02004294func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02004295 CheckRunVimInTerminal
4296
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02004297 " call indirectly to avoid compilation error for missing functions
4298 call Run_Test_define_func_at_command_line()
4299endfunc
4300
4301def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004302 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004303 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004304 func CheckAndQuit()
4305 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
4306 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
4307 endfunc
4308 END
4309 writefile([''], 'Xdidcmd')
4310 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01004311 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004312 # define Afunc() on the command line
4313 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
4314 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004315 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004316
4317 call StopVimInTerminal(buf)
4318 delete('XcallFunc')
4319 delete('Xdidcmd')
4320enddef
4321
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004322def Test_script_var_scope()
4323 var lines =<< trim END
4324 vim9script
4325 if true
4326 if true
4327 var one = 'one'
4328 echo one
4329 endif
4330 echo one
4331 endif
4332 END
4333 CheckScriptFailure(lines, 'E121:', 7)
4334
4335 lines =<< trim END
4336 vim9script
4337 if true
4338 if false
4339 var one = 'one'
4340 echo one
4341 else
4342 var one = 'one'
4343 echo one
4344 endif
4345 echo one
4346 endif
4347 END
4348 CheckScriptFailure(lines, 'E121:', 10)
4349
4350 lines =<< trim END
4351 vim9script
4352 while true
4353 var one = 'one'
4354 echo one
4355 break
4356 endwhile
4357 echo one
4358 END
4359 CheckScriptFailure(lines, 'E121:', 7)
4360
4361 lines =<< trim END
4362 vim9script
4363 for i in range(1)
4364 var one = 'one'
4365 echo one
4366 endfor
4367 echo one
4368 END
4369 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02004370
4371 lines =<< trim END
4372 vim9script
4373 {
4374 var one = 'one'
4375 assert_equal('one', one)
4376 }
4377 assert_false(exists('one'))
4378 assert_false(exists('s:one'))
4379 END
4380 CheckScriptSuccess(lines)
4381
4382 lines =<< trim END
4383 vim9script
4384 {
4385 var one = 'one'
4386 echo one
4387 }
4388 echo one
4389 END
4390 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004391enddef
4392
Bram Moolenaar352134b2020-10-17 22:04:08 +02004393def Test_catch_exception_in_callback()
4394 var lines =<< trim END
4395 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02004396 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02004397 try
4398 var x: string
4399 var y: string
4400 # this error should be caught with CHECKLEN
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00004401 var sl = ['']
4402 [x, y] = sl
Bram Moolenaar352134b2020-10-17 22:04:08 +02004403 catch
4404 g:caught = 'yes'
4405 endtry
4406 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004407 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02004408 feedkeys("\r", 'xt')
4409 END
4410 CheckScriptSuccess(lines)
4411
4412 unlet g:caught
4413enddef
4414
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004415def Test_no_unknown_error_after_error()
4416 if !has('unix') || !has('job')
4417 throw 'Skipped: not unix of missing +job feature'
4418 endif
Bram Moolenaarb16ff292021-09-26 20:14:39 +01004419 # FIXME: this check should not be needed
4420 if has('win32')
4421 throw 'Skipped: does not work on MS-Windows'
4422 endif
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004423 var lines =<< trim END
4424 vim9script
4425 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02004426 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004427 eval [][0]
4428 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02004429 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004430 sleep 1m
4431 source += l
4432 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004433 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01004434 while job_status(myjob) == 'run'
4435 sleep 10m
4436 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01004437 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02004438 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004439 END
4440 writefile(lines, 'Xdef')
4441 assert_fails('so Xdef', ['E684:', 'E1012:'])
4442 delete('Xdef')
4443enddef
4444
Bram Moolenaar4324d872020-12-01 20:12:24 +01004445def InvokeNormal()
4446 exe "norm! :m+1\r"
4447enddef
4448
4449def Test_invoke_normal_in_visual_mode()
4450 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
4451 new
4452 setline(1, ['aaa', 'bbb'])
4453 feedkeys("V\<F3>", 'xt')
4454 assert_equal(['bbb', 'aaa'], getline(1, 2))
4455 xunmap <F3>
4456enddef
4457
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004458def Test_white_space_after_command()
4459 var lines =<< trim END
4460 exit_cb: Func})
4461 END
4462 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01004463
4464 lines =<< trim END
4465 e#
4466 END
4467 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004468enddef
4469
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004470def Test_script_var_gone_when_sourced_twice()
4471 var lines =<< trim END
4472 vim9script
4473 if exists('g:guard')
4474 finish
4475 endif
4476 g:guard = 1
4477 var name = 'thename'
4478 def g:GetName(): string
4479 return name
4480 enddef
4481 def g:SetName(arg: string)
4482 name = arg
4483 enddef
4484 END
4485 writefile(lines, 'XscriptTwice.vim')
4486 so XscriptTwice.vim
4487 assert_equal('thename', g:GetName())
4488 g:SetName('newname')
4489 assert_equal('newname', g:GetName())
4490 so XscriptTwice.vim
4491 assert_fails('call g:GetName()', 'E1149:')
4492 assert_fails('call g:SetName("x")', 'E1149:')
4493
4494 delfunc g:GetName
4495 delfunc g:SetName
4496 delete('XscriptTwice.vim')
4497 unlet g:guard
4498enddef
4499
4500def Test_import_gone_when_sourced_twice()
4501 var exportlines =<< trim END
4502 vim9script
4503 if exists('g:guard')
4504 finish
4505 endif
4506 g:guard = 1
4507 export var name = 'someName'
4508 END
4509 writefile(exportlines, 'XexportScript.vim')
4510
4511 var lines =<< trim END
4512 vim9script
4513 import name from './XexportScript.vim'
4514 def g:GetName(): string
4515 return name
4516 enddef
4517 END
4518 writefile(lines, 'XscriptImport.vim')
4519 so XscriptImport.vim
4520 assert_equal('someName', g:GetName())
4521
4522 so XexportScript.vim
4523 assert_fails('call g:GetName()', 'E1149:')
4524
4525 delfunc g:GetName
4526 delete('XexportScript.vim')
4527 delete('XscriptImport.vim')
4528 unlet g:guard
4529enddef
4530
Bram Moolenaar10b94212021-02-19 21:42:57 +01004531def Test_unsupported_commands()
4532 var lines =<< trim END
4533 ka
4534 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004535 CheckDefFailure(lines, 'E476:')
4536 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaar10b94212021-02-19 21:42:57 +01004537
4538 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01004539 :1ka
4540 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004541 CheckDefFailure(lines, 'E476:')
4542 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaarada1d872021-02-20 08:16:51 +01004543
4544 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01004545 t
4546 END
4547 CheckDefFailure(lines, 'E1100:')
4548 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4549
4550 lines =<< trim END
4551 x
4552 END
4553 CheckDefFailure(lines, 'E1100:')
4554 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4555
4556 lines =<< trim END
4557 xit
4558 END
4559 CheckDefFailure(lines, 'E1100:')
4560 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4561enddef
4562
Bram Moolenaarc70fe462021-04-17 17:59:19 +02004563def Test_mapping_line_number()
4564 var lines =<< trim END
4565 vim9script
4566 def g:FuncA()
4567 # Some comment
4568 FuncB(0)
4569 enddef
4570 # Some comment
4571 def FuncB(
4572 # Some comment
4573 n: number
4574 )
4575 exe 'nno '
4576 # Some comment
4577 .. '<F3> a'
4578 .. 'b'
4579 .. 'c'
4580 enddef
4581 END
4582 CheckScriptSuccess(lines)
4583 var res = execute('verbose nmap <F3>')
4584 assert_match('No mapping found', res)
4585
4586 g:FuncA()
4587 res = execute('verbose nmap <F3>')
4588 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
4589
4590 nunmap <F3>
4591 delfunc g:FuncA
4592enddef
4593
Bram Moolenaardeb108b2021-07-08 17:35:36 +02004594def Test_option_set()
4595 # legacy script allows for white space
4596 var lines =<< trim END
4597 set foldlevel =11
4598 call assert_equal(11, &foldlevel)
4599 END
4600 CheckScriptSuccess(lines)
4601
4602 set foldlevel
4603 set foldlevel=12
4604 assert_equal(12, &foldlevel)
4605 set foldlevel+=2
4606 assert_equal(14, &foldlevel)
4607 set foldlevel-=3
4608 assert_equal(11, &foldlevel)
4609
4610 lines =<< trim END
4611 set foldlevel =1
4612 END
4613 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: =1')
4614
4615 lines =<< trim END
4616 set foldlevel +=1
4617 END
4618 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: +=1')
4619
4620 lines =<< trim END
4621 set foldlevel ^=1
4622 END
4623 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: ^=1')
4624
4625 lines =<< trim END
4626 set foldlevel -=1
4627 END
4628 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: -=1')
4629
4630 set foldlevel&
4631enddef
4632
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004633def Test_option_modifier()
Bram Moolenaar1594f312021-07-08 16:40:13 +02004634 # legacy script allows for white space
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004635 var lines =<< trim END
4636 set hlsearch & hlsearch !
4637 call assert_equal(1, &hlsearch)
4638 END
4639 CheckScriptSuccess(lines)
4640
Bram Moolenaar1594f312021-07-08 16:40:13 +02004641 set hlsearch
4642 set hlsearch!
4643 assert_equal(false, &hlsearch)
4644
4645 set hlsearch
4646 set hlsearch&
4647 assert_equal(false, &hlsearch)
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004648
4649 lines =<< trim END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004650 set hlsearch &
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004651 END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004652 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: &')
4653
4654 lines =<< trim END
4655 set hlsearch !
4656 END
4657 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: !')
4658
4659 set hlsearch&
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004660enddef
4661
Bram Moolenaarc03fe662021-07-11 16:52:45 +02004662" This must be called last, it may cause following :def functions to fail
4663def Test_xxx_echoerr_line_number()
4664 var lines =<< trim END
4665 echoerr 'some'
4666 .. ' error'
4667 .. ' continued'
4668 END
4669 CheckDefExecAndScriptFailure(lines, 'some error continued', 1)
4670enddef
4671
Bram Moolenaar9537e372021-12-10 21:05:53 +00004672func Test_debug_with_lambda()
Bram Moolenaar9fffef92021-12-10 16:55:58 +00004673 CheckRunVimInTerminal
4674
Bram Moolenaar9537e372021-12-10 21:05:53 +00004675 " call indirectly to avoid compilation error for missing functions
4676 call Run_Test_debug_with_lambda()
4677endfunc
4678
4679def Run_Test_debug_with_lambda()
Bram Moolenaar9fffef92021-12-10 16:55:58 +00004680 var lines =<< trim END
4681 vim9script
4682 def Func()
4683 var n = 0
4684 echo [0]->filter((_, v) => v == n)
4685 enddef
4686 breakadd func Func
4687 Func()
4688 END
4689 writefile(lines, 'XdebugFunc')
4690 var buf = RunVimInTerminal('-S XdebugFunc', {rows: 6, wait_for_ruler: 0})
4691 WaitForAssert(() => assert_match('^>', term_getline(buf, 6)))
4692
4693 term_sendkeys(buf, "cont\<CR>")
4694 WaitForAssert(() => assert_match('\[0\]', term_getline(buf, 5)))
4695
4696 StopVimInTerminal(buf)
4697 delete('XdebugFunc')
4698enddef
4699
Bram Moolenaar648594e2021-07-11 17:55:01 +02004700def ProfiledWithLambda()
Bram Moolenaard9162552021-07-11 15:26:13 +02004701 var n = 3
4702 echo [[1, 2], [3, 4]]->filter((_, l) => l[0] == n)
4703enddef
4704
Bram Moolenaar648594e2021-07-11 17:55:01 +02004705def ProfiledNested()
4706 var x = 0
4707 def Nested(): any
4708 return x
4709 enddef
4710 Nested()
4711enddef
4712
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004713def ProfiledNestedProfiled()
4714 var x = 0
4715 def Nested(): any
4716 return x
4717 enddef
4718 Nested()
4719enddef
4720
Dominique Pelle923dce22021-11-21 11:36:04 +00004721" Execute this near the end, profiling doesn't stop until Vim exits.
Bram Moolenaard9162552021-07-11 15:26:13 +02004722" This only tests that it works, not the profiling output.
4723def Test_xx_profile_with_lambda()
Bram Moolenaar4ece1522021-07-11 16:31:51 +02004724 CheckFeature profile
4725
Bram Moolenaard9162552021-07-11 15:26:13 +02004726 profile start Xprofile.log
Bram Moolenaar648594e2021-07-11 17:55:01 +02004727 profile func ProfiledWithLambda
4728 ProfiledWithLambda()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004729
Bram Moolenaar648594e2021-07-11 17:55:01 +02004730 profile func ProfiledNested
4731 ProfiledNested()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004732
4733 # Also profile the nested function. Use a different function, although the
4734 # contents is the same, to make sure it was not already compiled.
4735 profile func *
4736 ProfiledNestedProfiled()
4737
4738 profdel func *
4739 profile pause
Bram Moolenaard9162552021-07-11 15:26:13 +02004740enddef
4741
Bram Moolenaar585fea72020-04-02 22:33:21 +02004742" Keep this last, it messes up highlighting.
4743def Test_substitute_cmd()
4744 new
4745 setline(1, 'something')
4746 :substitute(some(other(
4747 assert_equal('otherthing', getline(1))
4748 bwipe!
4749
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004750 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004751 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02004752 vim9script
4753 new
4754 setline(1, 'something')
4755 :substitute(some(other(
4756 assert_equal('otherthing', getline(1))
4757 bwipe!
4758 END
4759 writefile(lines, 'Xvim9lines')
4760 source Xvim9lines
4761
4762 delete('Xvim9lines')
4763enddef
4764
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker