blob: fe985de35c3ca4edf8604227a7e297a26423b194 [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 Moolenaar003312b2021-12-20 10:55:35 +0000614 # return in try with finally
615 def ReturnInTry(): number
616 var ret = 4
617 try
618 return ret
619 catch /this/
620 return -1
621 catch /that/
622 return -1
623 finally
624 # changing ret has no effect
625 ret = 7
626 endtry
627 return -2
628 enddef
629 assert_equal(4, ReturnInTry())
630
631 # return in catch with finally
632 def ReturnInCatch(): number
633 var ret = 5
634 try
635 throw 'getout'
636 return -1
637 catch /getout/
638 # ret is evaluated here
639 return ret
640 finally
641 # changing ret later has no effect
642 ret = -3
643 endtry
644 return -2
645 enddef
646 assert_equal(5, ReturnInCatch())
647
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100648 # return in finally after empty catch
649 def ReturnInFinally(): number
650 try
651 finally
Bram Moolenaar003312b2021-12-20 10:55:35 +0000652 return 6
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100653 endtry
Bram Moolenaar003312b2021-12-20 10:55:35 +0000654 return -1
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100655 enddef
Bram Moolenaar003312b2021-12-20 10:55:35 +0000656 assert_equal(6, ReturnInFinally())
Bram Moolenaar8ac681a2021-06-15 20:06:34 +0200657
658 var lines =<< trim END
659 vim9script
660 try
661 acos('0.5')
662 ->setline(1)
663 catch
664 g:caught = v:exception
665 endtry
666 END
667 CheckScriptSuccess(lines)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200668 assert_match('E1219: Float or Number required for argument 1', g:caught)
Bram Moolenaar8ac681a2021-06-15 20:06:34 +0200669 unlet g:caught
Bram Moolenaar41978282021-07-04 14:47:30 +0200670
671 # missing catch and/or finally
672 lines =<< trim END
673 vim9script
674 try
675 echo 'something'
676 endtry
677 END
678 CheckScriptFailure(lines, 'E1032:')
rbtnn84934992021-08-07 13:26:53 +0200679
680 # skipping try-finally-endtry when try-finally-endtry is used in another block
681 lines =<< trim END
682 if v:true
683 try
684 finally
685 endtry
686 else
687 try
688 finally
689 endtry
690 endif
691 END
692 CheckDefAndScriptSuccess(lines)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693enddef
694
Bram Moolenaar3f987b52021-06-30 12:02:24 +0200695def Test_try_in_catch()
696 var lines =<< trim END
697 vim9script
698 var seq = []
699 def DoIt()
700 try
701 seq->add('throw 1')
702 eval [][0]
703 seq->add('notreached')
704 catch
705 seq->add('catch')
706 try
707 seq->add('throw 2')
708 eval [][0]
709 seq->add('notreached')
710 catch /nothing/
711 seq->add('notreached')
712 endtry
713 seq->add('done')
714 endtry
715 enddef
716 DoIt()
717 assert_equal(['throw 1', 'catch', 'throw 2', 'done'], seq)
718 END
719enddef
720
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200721def Test_error_in_catch()
722 var lines =<< trim END
723 try
724 eval [][0]
725 catch /E684:/
726 eval [][0]
727 endtry
728 END
729 CheckDefExecFailure(lines, 'E684:', 4)
730enddef
731
Bram Moolenaar2e34c342021-03-14 12:13:33 +0100732" :while at the very start of a function that :continue jumps to
733def TryContinueFunc()
734 while g:Count < 2
735 g:sequence ..= 't'
736 try
737 echoerr 'Test'
738 catch
739 g:Count += 1
740 g:sequence ..= 'c'
741 continue
742 endtry
743 g:sequence ..= 'e'
744 g:Count += 1
745 endwhile
746enddef
747
748def Test_continue_in_try_in_while()
749 g:Count = 0
750 g:sequence = ''
751 TryContinueFunc()
752 assert_equal('tctc', g:sequence)
753 unlet g:Count
754 unlet g:sequence
755enddef
756
Bram Moolenaar9cb577a2021-02-22 22:45:10 +0100757def Test_nocatch_return_in_try()
758 # return in try block returns normally
759 def ReturnInTry(): string
760 try
761 return '"some message"'
762 catch
763 endtry
764 return 'not reached'
765 enddef
766 exe 'echoerr ' .. ReturnInTry()
767enddef
768
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100769def Test_cnext_works_in_catch()
770 var lines =<< trim END
771 vim9script
Bram Moolenaarc3235272021-07-10 19:42:03 +0200772 au BufEnter * eval 1 + 2
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100773 writefile(['text'], 'Xfile1')
774 writefile(['text'], 'Xfile2')
775 var items = [
776 {lnum: 1, filename: 'Xfile1', valid: true},
777 {lnum: 1, filename: 'Xfile2', valid: true}
778 ]
779 setqflist([], ' ', {items: items})
780 cwindow
781
782 def CnextOrCfirst()
783 # if cnext fails, cfirst is used
784 try
785 cnext
786 catch
787 cfirst
788 endtry
789 enddef
790
791 CnextOrCfirst()
792 CnextOrCfirst()
793 writefile([getqflist({idx: 0}).idx], 'Xresult')
794 qall
795 END
796 writefile(lines, 'XCatchCnext')
797 RunVim([], [], '--clean -S XCatchCnext')
798 assert_equal(['1'], readfile('Xresult'))
799
800 delete('Xfile1')
801 delete('Xfile2')
802 delete('XCatchCnext')
803 delete('Xresult')
804enddef
805
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100806def Test_throw_skipped()
807 if 0
808 throw dontgethere
809 endif
810enddef
811
Bram Moolenaar8f81b222021-01-14 21:47:06 +0100812def Test_nocatch_throw_silenced()
813 var lines =<< trim END
814 vim9script
815 def Func()
816 throw 'error'
817 enddef
818 silent! Func()
819 END
820 writefile(lines, 'XthrowSilenced')
821 source XthrowSilenced
822 delete('XthrowSilenced')
823enddef
824
Bram Moolenaare8593122020-07-18 15:17:02 +0200825def DeletedFunc(): list<any>
826 return ['delete me']
827enddef
828defcompile
829delfunc DeletedFunc
830
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100831def ThrowFromDef()
Bram Moolenaara72cfb82020-04-23 17:07:30 +0200832 throw "getout" # comment
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100833enddef
834
835func CatchInFunc()
836 try
837 call ThrowFromDef()
838 catch
839 let g:thrown_func = v:exception
840 endtry
841endfunc
842
843def CatchInDef()
844 try
845 ThrowFromDef()
846 catch
847 g:thrown_def = v:exception
848 endtry
849enddef
850
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100851def ReturnFinally(): string
852 try
853 return 'intry'
Bram Moolenaar373863e2020-09-26 17:20:53 +0200854 finall
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100855 g:in_finally = 'finally'
856 endtry
857 return 'end'
858enddef
859
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100860def Test_try_catch_nested()
861 CatchInFunc()
862 assert_equal('getout', g:thrown_func)
863
864 CatchInDef()
865 assert_equal('getout', g:thrown_def)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100866
867 assert_equal('intry', ReturnFinally())
868 assert_equal('finally', g:in_finally)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200869
870 var l = []
871 try
872 l->add('1')
873 throw 'bad'
874 l->add('x')
875 catch /bad/
876 l->add('2')
877 try
878 l->add('3')
879 throw 'one'
880 l->add('x')
881 catch /one/
882 l->add('4')
883 try
884 l->add('5')
885 throw 'more'
886 l->add('x')
887 catch /more/
888 l->add('6')
889 endtry
890 endtry
891 endtry
892 assert_equal(['1', '2', '3', '4', '5', '6'], l)
Bram Moolenaar834193a2021-06-30 20:39:15 +0200893
894 l = []
895 try
896 try
897 l->add('1')
898 throw 'foo'
899 l->add('x')
900 catch
901 l->add('2')
902 throw 'bar'
903 l->add('x')
904 finally
905 l->add('3')
906 endtry
907 l->add('x')
908 catch /bar/
909 l->add('4')
910 endtry
911 assert_equal(['1', '2', '3', '4'], l)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100912enddef
913
Bram Moolenaar9939f572020-09-16 22:29:52 +0200914def TryOne(): number
915 try
916 return 0
917 catch
918 endtry
919 return 0
920enddef
921
922def TryTwo(n: number): string
923 try
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200924 var x = {}
Bram Moolenaar9939f572020-09-16 22:29:52 +0200925 catch
926 endtry
927 return 'text'
928enddef
929
930def Test_try_catch_twice()
931 assert_equal('text', TryOne()->TryTwo())
932enddef
933
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100934def Test_try_catch_match()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200935 var seq = 'a'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100936 try
937 throw 'something'
938 catch /nothing/
939 seq ..= 'x'
940 catch /some/
941 seq ..= 'b'
942 catch /asdf/
943 seq ..= 'x'
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200944 catch ?a\?sdf?
945 seq ..= 'y'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100946 finally
947 seq ..= 'c'
948 endtry
949 assert_equal('abc', seq)
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100950enddef
951
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200952def Test_try_catch_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200953 CheckDefFailure(['catch'], 'E603:')
954 CheckDefFailure(['try', 'echo 0', 'catch', 'catch'], 'E1033:')
955 CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
956 CheckDefFailure(['finally'], 'E606:')
957 CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:')
958 CheckDefFailure(['endtry'], 'E602:')
959 CheckDefFailure(['while 1', 'endtry'], 'E170:')
960 CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:')
Bram Moolenaar13106602020-10-04 16:06:05 +0200961 CheckDefFailure(['if 1', 'endtry'], 'E171:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200962 CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:')
Bram Moolenaar585fea72020-04-02 22:33:21 +0200963
Bram Moolenaare4984292020-12-13 14:19:25 +0100964 CheckDefFailure(['throw'], 'E1143:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200965 CheckDefFailure(['throw xxx'], 'E1001:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200966enddef
967
Bram Moolenaar7c5b3c0362021-02-14 22:40:57 +0100968def Try_catch_skipped()
969 var l = []
970 try
971 finally
972 endtry
973
974 if 1
975 else
976 try
977 endtry
978 endif
979enddef
980
981" The skipped try/endtry was updating the wrong instruction.
982def Test_try_catch_skipped()
983 var instr = execute('disassemble Try_catch_skipped')
984 assert_match("NEWLIST size 0\n", instr)
985enddef
986
987
988
Bram Moolenaar006ad482020-06-30 20:55:15 +0200989def Test_throw_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200990 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200991 var lines =<< trim END
Bram Moolenaar006ad482020-06-30 20:55:15 +0200992 vim9script
993 try
994 throw 'one'
995 .. 'two'
996 catch
997 assert_equal('onetwo', v:exception)
998 endtry
999 END
1000 CheckScriptSuccess(lines)
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001001
1002 lines =<< trim END
1003 vim9script
Bram Moolenaar352134b2020-10-17 22:04:08 +02001004 @r = ''
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001005 def Func()
1006 throw @r
1007 enddef
1008 var result = ''
1009 try
1010 Func()
1011 catch /E1129:/
1012 result = 'caught'
1013 endtry
1014 assert_equal('caught', result)
1015 END
1016 CheckScriptSuccess(lines)
Bram Moolenaar006ad482020-06-30 20:55:15 +02001017enddef
1018
Bram Moolenaared677f52020-08-12 16:38:10 +02001019def Test_error_in_nested_function()
Bram Moolenaar03dfde22021-02-14 13:17:22 +01001020 # an error in a nested :function aborts executing in the calling :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001021 var lines =<< trim END
Bram Moolenaared677f52020-08-12 16:38:10 +02001022 vim9script
1023 def Func()
1024 Error()
1025 g:test_var = 1
1026 enddef
1027 func Error() abort
1028 eval [][0]
1029 endfunc
1030 Func()
1031 END
1032 g:test_var = 0
1033 CheckScriptFailure(lines, 'E684:')
1034 assert_equal(0, g:test_var)
1035enddef
1036
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001037def Test_abort_after_error()
1038 var lines =<< trim END
1039 vim9script
1040 while true
1041 echo notfound
1042 endwhile
1043 g:gotthere = true
1044 END
1045 g:gotthere = false
1046 CheckScriptFailure(lines, 'E121:')
1047 assert_false(g:gotthere)
1048 unlet g:gotthere
1049enddef
1050
Bram Moolenaar37c83712020-06-30 21:18:36 +02001051def Test_cexpr_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001052 # only checks line continuation
Bram Moolenaar37c83712020-06-30 21:18:36 +02001053 set errorformat=File\ %f\ line\ %l
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001054 var lines =<< trim END
Bram Moolenaar37c83712020-06-30 21:18:36 +02001055 vim9script
1056 cexpr 'File'
1057 .. ' someFile' ..
1058 ' line 19'
1059 assert_equal(19, getqflist()[0].lnum)
1060 END
1061 CheckScriptSuccess(lines)
1062 set errorformat&
1063enddef
1064
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +02001065def Test_statusline_syntax()
1066 # legacy syntax is used for 'statusline'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001067 var lines =<< trim END
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +02001068 vim9script
1069 func g:Status()
1070 return '%{"x" is# "x"}'
1071 endfunc
1072 set laststatus=2 statusline=%!Status()
1073 redrawstatus
1074 set laststatus statusline=
1075 END
1076 CheckScriptSuccess(lines)
1077enddef
1078
Bram Moolenaarb2097502020-07-19 17:17:02 +02001079def Test_list_vimscript()
1080 # checks line continuation and comments
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001081 var lines =<< trim END
Bram Moolenaarb2097502020-07-19 17:17:02 +02001082 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001083 var mylist = [
Bram Moolenaarb2097502020-07-19 17:17:02 +02001084 'one',
1085 # comment
1086 'two', # empty line follows
1087
1088 'three',
1089 ]
1090 assert_equal(['one', 'two', 'three'], mylist)
1091 END
1092 CheckScriptSuccess(lines)
Bram Moolenaar66250c92020-08-20 15:02:42 +02001093
1094 # check all lines from heredoc are kept
1095 lines =<< trim END
1096 # comment 1
1097 two
1098 # comment 3
1099
1100 five
1101 # comment 6
1102 END
1103 assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines)
Bram Moolenaar4bce26b2021-01-22 22:06:56 +01001104
1105 lines =<< trim END
1106 [{
1107 a: 0}]->string()->assert_equal("[{'a': 0}]")
1108 END
1109 CheckDefAndScriptSuccess(lines)
Bram Moolenaarb2097502020-07-19 17:17:02 +02001110enddef
1111
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001112if has('channel')
1113 let someJob = test_null_job()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001114
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001115 def FuncWithError()
1116 echomsg g:someJob
1117 enddef
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001118
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001119 func Test_convert_emsg_to_exception()
1120 try
1121 call FuncWithError()
1122 catch
1123 call assert_match('Vim:E908:', v:exception)
1124 endtry
1125 endfunc
1126endif
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128let s:export_script_lines =<< trim END
1129 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001130 var name: string = 'bob'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131 def Concat(arg: string): string
1132 return name .. arg
1133 enddef
Bram Moolenaar227a69d2020-05-15 18:17:28 +02001134 g:result = Concat('bie')
1135 g:localname = name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001136
1137 export const CONST = 1234
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001138 export var exported = 9876
1139 export var exp_name = 'John'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 export def Exported(): string
1141 return 'Exported'
1142 enddef
Bram Moolenaar24e93162021-07-18 20:40:33 +02001143 export def ExportedValue(): number
1144 return exported
1145 enddef
1146 export def ExportedInc()
1147 exported += 5
1148 enddef
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001149 export final theList = [1]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150END
1151
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001152def Undo_export_script_lines()
1153 unlet g:result
1154 unlet g:localname
1155enddef
1156
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001157def Test_vim9_import_export()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001158 var import_script_lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001159 vim9script
Bram Moolenaar24e93162021-07-18 20:40:33 +02001160 import {exported, Exported, ExportedValue} from './Xexport.vim'
1161 g:exported1 = exported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001162 exported += 3
Bram Moolenaar24e93162021-07-18 20:40:33 +02001163 g:exported2 = exported
1164 g:exported3 = ExportedValue()
1165
1166 import ExportedInc from './Xexport.vim'
1167 ExportedInc()
1168 g:exported_i1 = exported
1169 g:exported_i2 = ExportedValue()
1170
1171 exported = 11
1172 g:exported_s1 = exported
1173 g:exported_s2 = ExportedValue()
1174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 g:imported_func = Exported()
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001176
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001177 def GetExported(): string
Bram Moolenaare0de1712020-12-02 17:36:54 +01001178 var local_dict = {ref: Exported}
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001179 return local_dict.ref()
1180 enddef
1181 g:funcref_result = GetExported()
1182
Bram Moolenaar4db572e2021-07-18 18:21:38 +02001183 var dir = './'
1184 var ext = ".vim"
1185 import {exp_name} from dir .. 'Xexport' .. ext
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001186 g:imported_name = exp_name
1187 exp_name ..= ' Doe'
1188 g:imported_name_appended = exp_name
Bram Moolenaar24e93162021-07-18 20:40:33 +02001189 g:exported_later = exported
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001190
1191 import theList from './Xexport.vim'
1192 theList->add(2)
1193 assert_equal([1, 2], theList)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 END
1195
1196 writefile(import_script_lines, 'Ximport.vim')
1197 writefile(s:export_script_lines, 'Xexport.vim')
1198
1199 source Ximport.vim
1200
1201 assert_equal('bobbie', g:result)
1202 assert_equal('bob', g:localname)
Bram Moolenaar24e93162021-07-18 20:40:33 +02001203 assert_equal(9876, g:exported1)
1204 assert_equal(9879, g:exported2)
1205 assert_equal(9879, g:exported3)
1206
1207 assert_equal(9884, g:exported_i1)
1208 assert_equal(9884, g:exported_i2)
1209
1210 assert_equal(11, g:exported_s1)
1211 assert_equal(11, g:exported_s2)
1212 assert_equal(11, g:exported_later)
1213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 assert_equal('Exported', g:imported_func)
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001215 assert_equal('Exported', g:funcref_result)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001216 assert_equal('John', g:imported_name)
1217 assert_equal('John Doe', g:imported_name_appended)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 assert_false(exists('g:name'))
1219
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001220 Undo_export_script_lines()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001221 unlet g:exported1
1222 unlet g:exported2
1223 unlet g:exported3
1224 unlet g:exported_i1
1225 unlet g:exported_i2
1226 unlet g:exported_later
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227 unlet g:imported_func
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001228 unlet g:imported_name g:imported_name_appended
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 delete('Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001230
Bram Moolenaar1c991142020-07-04 13:15:31 +02001231 # similar, with line breaks
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001232 var import_line_break_script_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001233 vim9script
1234 import {
1235 exported,
1236 Exported,
1237 }
1238 from
1239 './Xexport.vim'
Bram Moolenaar24e93162021-07-18 20:40:33 +02001240 g:exported = exported
1241 exported += 7
1242 g:exported_added = exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001243 g:imported_func = Exported()
1244 END
1245 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
1246 source Ximport_lbr.vim
1247
Bram Moolenaar24e93162021-07-18 20:40:33 +02001248 assert_equal(11, g:exported)
1249 assert_equal(18, g:exported_added)
Bram Moolenaar1c991142020-07-04 13:15:31 +02001250 assert_equal('Exported', g:imported_func)
1251
1252 # exported script not sourced again
1253 assert_false(exists('g:result'))
Bram Moolenaar24e93162021-07-18 20:40:33 +02001254 unlet g:exported
1255 unlet g:exported_added
Bram Moolenaar1c991142020-07-04 13:15:31 +02001256 unlet g:imported_func
1257 delete('Ximport_lbr.vim')
1258
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001259 var import_star_as_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001260 vim9script
1261 import * as Export from './Xexport.vim'
1262 def UseExport()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001263 g:exported_def = Export.exported
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001264 enddef
Bram Moolenaar24e93162021-07-18 20:40:33 +02001265 g:exported_script = Export.exported
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001266 assert_equal(1, exists('Export.exported'))
1267 assert_equal(0, exists('Export.notexported'))
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001268 UseExport()
1269 END
1270 writefile(import_star_as_lines, 'Ximport.vim')
1271 source Ximport.vim
Bram Moolenaar24e93162021-07-18 20:40:33 +02001272
1273 assert_equal(18, g:exported_def)
1274 assert_equal(18, g:exported_script)
1275 unlet g:exported_def
1276 unlet g:exported_script
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001277
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001278 var import_star_as_lines_no_dot =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001279 vim9script
1280 import * as Export from './Xexport.vim'
1281 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001282 var dummy = 1
1283 var imported = Export + dummy
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001284 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001285 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001286 END
1287 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001288 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001289
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001290 var import_star_as_lines_dot_space =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001291 vim9script
1292 import * as Export from './Xexport.vim'
1293 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001294 var imported = Export . exported
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001295 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001296 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001297 END
1298 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001299 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001300
Bram Moolenaar921ba522021-07-29 22:25:05 +02001301 var import_func_duplicated =<< trim END
1302 vim9script
1303 import ExportedInc from './Xexport.vim'
1304 import ExportedInc from './Xexport.vim'
1305
1306 ExportedInc()
1307 END
1308 writefile(import_func_duplicated, 'Ximport.vim')
1309 assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
1310
Bram Moolenaara6294952020-12-27 13:39:50 +01001311 var import_star_as_duplicated =<< trim END
1312 vim9script
1313 import * as Export from './Xexport.vim'
1314 var some = 'other'
1315 import * as Export from './Xexport.vim'
1316 defcompile
1317 END
1318 writefile(import_star_as_duplicated, 'Ximport.vim')
1319 assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim')
1320
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001321 var import_star_as_lines_script_no_dot =<< trim END
1322 vim9script
1323 import * as Export from './Xexport.vim'
1324 g:imported_script = Export exported
1325 END
1326 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
1327 assert_fails('source Ximport.vim', 'E1029:')
1328
1329 var import_star_as_lines_script_space_after_dot =<< trim END
1330 vim9script
1331 import * as Export from './Xexport.vim'
1332 g:imported_script = Export. exported
1333 END
1334 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
1335 assert_fails('source Ximport.vim', 'E1074:')
1336
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001337 var import_star_as_lines_missing_name =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001338 vim9script
1339 import * as Export from './Xexport.vim'
1340 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001341 var imported = Export.
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001342 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001343 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001344 END
1345 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001346 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001347
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001348 var import_star_as_lbr_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001349 vim9script
1350 import *
1351 as Export
1352 from
1353 './Xexport.vim'
1354 def UseExport()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001355 g:exported = Export.exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001356 enddef
1357 UseExport()
1358 END
1359 writefile(import_star_as_lbr_lines, 'Ximport.vim')
1360 source Ximport.vim
Bram Moolenaar24e93162021-07-18 20:40:33 +02001361 assert_equal(18, g:exported)
1362 unlet g:exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001363
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001364 var import_star_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001365 vim9script
1366 import * from './Xexport.vim'
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001367 END
1368 writefile(import_star_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001369 assert_fails('source Ximport.vim', 'E1045:', '', 2, 'Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001370
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001371 # try to import something that exists but is not exported
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001372 var import_not_exported_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001373 vim9script
1374 import name from './Xexport.vim'
1375 END
1376 writefile(import_not_exported_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001377 assert_fails('source Ximport.vim', 'E1049:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001378
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001379 # try to import something that is already defined
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001380 var import_already_defined =<< trim END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001381 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001382 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001383 import exported from './Xexport.vim'
1384 END
1385 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001386 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001387
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001388 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001389 import_already_defined =<< trim END
1390 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001391 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001392 import * as exported from './Xexport.vim'
1393 END
1394 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001395 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001396
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001397 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001398 import_already_defined =<< trim END
1399 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001400 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001401 import {exported} from './Xexport.vim'
1402 END
1403 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001404 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001405
Bram Moolenaar918a4242020-12-06 14:37:08 +01001406 # try changing an imported const
1407 var import_assign_to_const =<< trim END
1408 vim9script
1409 import CONST from './Xexport.vim'
1410 def Assign()
1411 CONST = 987
1412 enddef
1413 defcompile
1414 END
1415 writefile(import_assign_to_const, 'Ximport.vim')
1416 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1417
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001418 # try changing an imported final
1419 var import_assign_to_final =<< trim END
1420 vim9script
1421 import theList from './Xexport.vim'
1422 def Assign()
1423 theList = [2]
1424 enddef
1425 defcompile
1426 END
1427 writefile(import_assign_to_final, 'Ximport.vim')
1428 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1429
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001430 # import a very long name, requires making a copy
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001431 var import_long_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001432 vim9script
1433 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
1434 END
1435 writefile(import_long_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001436 assert_fails('source Ximport.vim', 'E1048:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001437
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001438 var import_no_from_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001439 vim9script
1440 import name './Xexport.vim'
1441 END
1442 writefile(import_no_from_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001443 assert_fails('source Ximport.vim', 'E1070:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001444
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001445 var import_invalid_string_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001446 vim9script
1447 import name from Xexport.vim
1448 END
1449 writefile(import_invalid_string_lines, 'Ximport.vim')
Bram Moolenaar4db572e2021-07-18 18:21:38 +02001450 assert_fails('source Ximport.vim', 'E121:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001451
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001452 var import_wrong_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001453 vim9script
1454 import name from './XnoExport.vim'
1455 END
1456 writefile(import_wrong_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001457 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001458
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001459 var import_missing_comma_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001460 vim9script
1461 import {exported name} from './Xexport.vim'
1462 END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001463 writefile(import_missing_comma_lines, 'Ximport3.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001464 assert_fails('source Ximport3.vim', 'E1046:', '', 2, 'Ximport3.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001465
Bram Moolenaar60579352021-07-19 21:45:07 +02001466 var import_redefining_lines =<< trim END
1467 vim9script
1468 import exported from './Xexport.vim'
1469 var exported = 5
1470 END
1471 writefile(import_redefining_lines, 'Ximport.vim')
1472 assert_fails('source Ximport.vim', 'E1213: Redefining imported item "exported"', '', 3)
1473
1474 var import_assign_wrong_type_lines =<< trim END
1475 vim9script
1476 import exported from './Xexport.vim'
1477 exported = 'xxx'
1478 END
1479 writefile(import_assign_wrong_type_lines, 'Ximport.vim')
1480 assert_fails('source Ximport.vim', 'E1012: Type mismatch; expected number but got string', '', 3)
1481
1482 var import_assign_const_lines =<< trim END
1483 vim9script
1484 import CONST from './Xexport.vim'
1485 CONST = 4321
1486 END
1487 writefile(import_assign_const_lines, 'Ximport.vim')
1488 assert_fails('source Ximport.vim', 'E741: Value is locked: CONST', '', 3)
1489
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001490 delete('Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001491 delete('Ximport3.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 delete('Xexport.vim')
1493
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001494 # Check that in a Vim9 script 'cpo' is set to the Vim default.
Bram Moolenaar3e191692021-03-17 17:46:00 +01001495 # Flags added or removed are also applied to the restored value.
1496 set cpo=abcd
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001497 var lines =<< trim END
Bram Moolenaar750802b2020-02-23 18:08:33 +01001498 vim9script
1499 g:cpo_in_vim9script = &cpo
Bram Moolenaar3e191692021-03-17 17:46:00 +01001500 set cpo+=f
1501 set cpo-=c
1502 g:cpo_after_vim9script = &cpo
Bram Moolenaar750802b2020-02-23 18:08:33 +01001503 END
1504 writefile(lines, 'Xvim9_script')
1505 source Xvim9_script
Bram Moolenaar3e191692021-03-17 17:46:00 +01001506 assert_equal('fabd', &cpo)
Bram Moolenaar750802b2020-02-23 18:08:33 +01001507 set cpo&vim
1508 assert_equal(&cpo, g:cpo_in_vim9script)
Bram Moolenaar3e191692021-03-17 17:46:00 +01001509 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
1510 assert_equal(newcpo, g:cpo_after_vim9script)
1511
Bram Moolenaar750802b2020-02-23 18:08:33 +01001512 delete('Xvim9_script')
1513enddef
1514
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001515def Test_import_funcref()
1516 var lines =<< trim END
1517 vim9script
1518 export def F(): number
1519 return 42
1520 enddef
1521 export const G = F
1522 END
1523 writefile(lines, 'Xlib.vim')
1524
1525 lines =<< trim END
1526 vim9script
1527 import {G} from './Xlib.vim'
1528 const Foo = G()
1529 assert_equal(42, Foo)
1530
1531 def DoTest()
1532 const Goo = G()
Bram Moolenaar06ca48a2021-10-23 10:25:21 +01001533 assert_equal(42, Goo)
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001534 enddef
1535 DoTest()
1536 END
1537 CheckScriptSuccess(lines)
1538
1539 delete('Xlib.vim')
1540enddef
1541
Bram Moolenaar6853c382021-09-07 22:12:19 +02001542def Test_import_star_fails()
1543 writefile([], 'Xfoo.vim')
1544 var lines =<< trim END
Bram Moolenaaraf2d5d22021-09-07 22:35:34 +02001545 import * as foo from './Xfoo.vim'
Bram Moolenaar6853c382021-09-07 22:12:19 +02001546 foo = 'bar'
1547 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00001548 CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use foo itself'])
Bram Moolenaar6853c382021-09-07 22:12:19 +02001549 lines =<< trim END
1550 vim9script
Bram Moolenaaraf2d5d22021-09-07 22:35:34 +02001551 import * as foo from './Xfoo.vim'
Bram Moolenaar6853c382021-09-07 22:12:19 +02001552 var that = foo
1553 END
1554 CheckScriptFailure(lines, 'E1029: Expected ''.''')
Bram Moolenaara9e3d562021-09-08 12:31:35 +02001555
1556 lines =<< trim END
1557 vim9script
1558 import * as 9foo from './Xfoo.vim'
1559 END
1560 CheckScriptFailure(lines, 'E1047:')
1561 lines =<< trim END
1562 vim9script
1563 import * as the#foo from './Xfoo.vim'
1564 END
1565 CheckScriptFailure(lines, 'E1047:')
1566 lines =<< trim END
1567 vim9script
1568 import * as g:foo from './Xfoo.vim'
1569 END
1570 CheckScriptFailure(lines, 'E1047:')
1571
Bram Moolenaaraf2d5d22021-09-07 22:35:34 +02001572 delete('Xfoo.vim')
Bram Moolenaarf8a79fc2021-12-14 12:06:16 +00001573
1574 lines =<< trim END
1575 vim9script
1576 def TheFunc()
1577 echo 'the func'
1578 enddef
1579 export var Ref = TheFunc
1580 END
1581 writefile([], 'Xthat.vim')
1582 lines =<< trim END
1583 import * as That from './Xthat.vim'
1584 That()
1585 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00001586 CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use That itself'])
Bram Moolenaarf8a79fc2021-12-14 12:06:16 +00001587 delete('Xthat.vim')
Bram Moolenaar6853c382021-09-07 22:12:19 +02001588enddef
1589
Bram Moolenaar0a842842021-02-27 22:41:19 +01001590def Test_import_as()
1591 var export_lines =<< trim END
1592 vim9script
1593 export var one = 1
1594 export var yes = 'yes'
Bram Moolenaarc967d572021-07-08 21:38:50 +02001595 export var slist: list<string>
Bram Moolenaar0a842842021-02-27 22:41:19 +01001596 END
1597 writefile(export_lines, 'XexportAs')
1598
1599 var import_lines =<< trim END
1600 vim9script
Bram Moolenaar6c7cc342021-04-17 16:38:50 +02001601 var one = 'notused'
1602 var yes = 777
Bram Moolenaar0a842842021-02-27 22:41:19 +01001603 import one as thatOne from './XexportAs'
1604 assert_equal(1, thatOne)
1605 import yes as yesYes from './XexportAs'
1606 assert_equal('yes', yesYes)
1607 END
1608 CheckScriptSuccess(import_lines)
1609
1610 import_lines =<< trim END
1611 vim9script
1612 import {one as thatOne, yes as yesYes} from './XexportAs'
1613 assert_equal(1, thatOne)
1614 assert_equal('yes', yesYes)
1615 assert_fails('echo one', 'E121:')
1616 assert_fails('echo yes', 'E121:')
1617 END
1618 CheckScriptSuccess(import_lines)
1619
Bram Moolenaarc967d572021-07-08 21:38:50 +02001620 import_lines =<< trim END
1621 vim9script
1622 import {slist as impSlist} from './XexportAs'
1623 impSlist->add(123)
1624 END
1625 CheckScriptFailure(import_lines, 'E1012: Type mismatch; expected string but got number')
1626
Bram Moolenaar0a842842021-02-27 22:41:19 +01001627 delete('XexportAs')
1628enddef
1629
Bram Moolenaar803af682020-08-05 16:20:03 +02001630func g:Trigger()
1631 source Ximport.vim
1632 return "echo 'yes'\<CR>"
1633endfunc
1634
1635def Test_import_export_expr_map()
1636 # check that :import and :export work when buffer is locked
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001637 var export_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001638 vim9script
1639 export def That(): string
1640 return 'yes'
1641 enddef
1642 END
1643 writefile(export_lines, 'Xexport_that.vim')
1644
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001645 var import_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001646 vim9script
1647 import That from './Xexport_that.vim'
1648 assert_equal('yes', That())
1649 END
1650 writefile(import_lines, 'Ximport.vim')
1651
1652 nnoremap <expr> trigger g:Trigger()
1653 feedkeys('trigger', "xt")
1654
Bram Moolenaar730b2482020-08-09 13:02:10 +02001655 delete('Xexport_that.vim')
Bram Moolenaar803af682020-08-05 16:20:03 +02001656 delete('Ximport.vim')
1657 nunmap trigger
1658enddef
1659
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001660def Test_import_in_filetype()
1661 # check that :import works when the buffer is locked
1662 mkdir('ftplugin', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001663 var export_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001664 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001665 export var That = 'yes'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001666 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001667 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001668
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001669 var import_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001670 vim9script
Bram Moolenaar730b2482020-08-09 13:02:10 +02001671 import That from './Xexport_ft.vim'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001672 assert_equal('yes', That)
1673 g:did_load_mytpe = 1
1674 END
1675 writefile(import_lines, 'ftplugin/qf.vim')
1676
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001677 var save_rtp = &rtp
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001678 &rtp = getcwd() .. ',' .. &rtp
1679
1680 filetype plugin on
1681 copen
1682 assert_equal(1, g:did_load_mytpe)
1683
1684 quit!
Bram Moolenaar730b2482020-08-09 13:02:10 +02001685 delete('Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001686 delete('ftplugin', 'rf')
1687 &rtp = save_rtp
1688enddef
1689
Bram Moolenaarefa94442020-08-08 22:16:00 +02001690def Test_use_import_in_mapping()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001691 var lines =<< trim END
Bram Moolenaarefa94442020-08-08 22:16:00 +02001692 vim9script
1693 export def Funcx()
1694 g:result = 42
1695 enddef
1696 END
1697 writefile(lines, 'XsomeExport.vim')
1698 lines =<< trim END
1699 vim9script
1700 import Funcx from './XsomeExport.vim'
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001701 nnoremap <F3> :call <sid>Funcx()<cr>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001702 END
1703 writefile(lines, 'Xmapscript.vim')
1704
1705 source Xmapscript.vim
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001706 feedkeys("\<F3>", "xt")
Bram Moolenaarefa94442020-08-08 22:16:00 +02001707 assert_equal(42, g:result)
1708
1709 unlet g:result
1710 delete('XsomeExport.vim')
1711 delete('Xmapscript.vim')
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001712 nunmap <F3>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001713enddef
1714
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01001715def Test_vim9script_mix()
1716 var lines =<< trim END
1717 if has(g:feature)
1718 " legacy script
1719 let g:legacy = 1
1720 finish
1721 endif
1722 vim9script
1723 g:legacy = 0
1724 END
1725 g:feature = 'eval'
1726 g:legacy = -1
1727 CheckScriptSuccess(lines)
1728 assert_equal(1, g:legacy)
1729
1730 g:feature = 'noteval'
1731 g:legacy = -1
1732 CheckScriptSuccess(lines)
1733 assert_equal(0, g:legacy)
1734enddef
1735
Bram Moolenaar750802b2020-02-23 18:08:33 +01001736def Test_vim9script_fails()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1738 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001739 CheckScriptFailure(['export var some = 123'], 'E1042:')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001740 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001741 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
Bram Moolenaar750802b2020-02-23 18:08:33 +01001742 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
1743
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001744 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001745 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1746
Bram Moolenaare2e40752020-09-04 21:18:46 +02001747 assert_fails('vim9script', 'E1038:')
1748 assert_fails('export something', 'E1043:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001749enddef
1750
Bram Moolenaarf7d267e2020-06-17 12:04:54 +02001751func Test_import_fails_without_script()
Bram Moolenaar101f4812020-06-16 23:18:51 +02001752 CheckRunVimInTerminal
1753
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001754 " call indirectly to avoid compilation error for missing functions
Bram Moolenaarc620c052020-07-08 15:16:19 +02001755 call Run_Test_import_fails_on_command_line()
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001756endfunc
1757
Bram Moolenaarc620c052020-07-08 15:16:19 +02001758def Run_Test_import_fails_on_command_line()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001759 var export =<< trim END
Bram Moolenaar101f4812020-06-16 23:18:51 +02001760 vim9script
1761 export def Foo(): number
1762 return 0
1763 enddef
1764 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001765 writefile(export, 'XexportCmd.vim')
Bram Moolenaar101f4812020-06-16 23:18:51 +02001766
Bram Moolenaare0de1712020-12-02 17:36:54 +01001767 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001768 rows: 6, wait_for_ruler: 0})
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001769 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaar101f4812020-06-16 23:18:51 +02001770
Bram Moolenaar730b2482020-08-09 13:02:10 +02001771 delete('XexportCmd.vim')
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001772 StopVimInTerminal(buf)
1773enddef
Bram Moolenaar101f4812020-06-16 23:18:51 +02001774
Bram Moolenaar2b327002020-12-26 15:39:31 +01001775def Test_vim9script_reload_noclear()
1776 var lines =<< trim END
Bram Moolenaara6294952020-12-27 13:39:50 +01001777 vim9script
1778 export var exported = 'thexport'
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001779
1780 export def TheFunc(x = 0)
1781 enddef
Bram Moolenaara6294952020-12-27 13:39:50 +01001782 END
1783 writefile(lines, 'XExportReload')
1784 lines =<< trim END
Bram Moolenaar2b327002020-12-26 15:39:31 +01001785 vim9script noclear
1786 g:loadCount += 1
1787 var s:reloaded = 'init'
Bram Moolenaara6294952020-12-27 13:39:50 +01001788 import exported from './XExportReload'
Bram Moolenaar2b327002020-12-26 15:39:31 +01001789
1790 def Again(): string
1791 return 'again'
1792 enddef
1793
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001794 import TheFunc from './XExportReload'
1795 TheFunc()
1796
Bram Moolenaar2b327002020-12-26 15:39:31 +01001797 if exists('s:loaded') | finish | endif
1798 var s:loaded = true
1799
1800 var s:notReloaded = 'yes'
1801 s:reloaded = 'first'
1802 def g:Values(): list<string>
Bram Moolenaara6294952020-12-27 13:39:50 +01001803 return [s:reloaded, s:notReloaded, Again(), Once(), exported]
Bram Moolenaar2b327002020-12-26 15:39:31 +01001804 enddef
1805
1806 def Once(): string
1807 return 'once'
1808 enddef
1809 END
1810 writefile(lines, 'XReloaded')
1811 g:loadCount = 0
1812 source XReloaded
1813 assert_equal(1, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001814 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001815 source XReloaded
1816 assert_equal(2, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001817 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001818 source XReloaded
1819 assert_equal(3, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001820 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001821
Bram Moolenaar48e11c12021-01-11 18:47:00 +01001822 delete('XReloaded')
Bram Moolenaara6294952020-12-27 13:39:50 +01001823 delete('XExportReload')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001824 delfunc g:Values
Bram Moolenaar2b327002020-12-26 15:39:31 +01001825 unlet g:loadCount
Bram Moolenaar577dc932021-06-27 15:35:40 +02001826
1827 lines =<< trim END
1828 vim9script
1829 def Inner()
1830 enddef
1831 END
1832 lines->writefile('XreloadScript.vim')
1833 source XreloadScript.vim
1834
1835 lines =<< trim END
1836 vim9script
1837 def Outer()
1838 def Inner()
1839 enddef
1840 enddef
1841 defcompile
1842 END
1843 lines->writefile('XreloadScript.vim')
1844 source XreloadScript.vim
1845
1846 delete('XreloadScript.vim')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001847enddef
1848
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001849def Test_vim9script_reload_import()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001850 var lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 vim9script
1852 const var = ''
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001853 var valone = 1234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 def MyFunc(arg: string)
1855 valone = 5678
1856 enddef
1857 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001858 var morelines =<< trim END
1859 var valtwo = 222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 export def GetValtwo(): number
1861 return valtwo
1862 enddef
1863 END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001864 writefile(lines + morelines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 source Xreload.vim
1866 source Xreload.vim
1867 source Xreload.vim
1868
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001869 # cannot declare a var twice
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 lines =<< trim END
1871 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001872 var valone = 1234
1873 var valone = 5678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 END
1875 writefile(lines, 'Xreload.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001876 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877
1878 delete('Xreload.vim')
1879 delete('Ximport.vim')
1880enddef
1881
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001882" if a script is reloaded with a script-local variable that changed its type, a
1883" compiled function using that variable must fail.
1884def Test_script_reload_change_type()
1885 var lines =<< trim END
1886 vim9script noclear
1887 var str = 'string'
1888 def g:GetStr(): string
1889 return str .. 'xxx'
1890 enddef
1891 END
1892 writefile(lines, 'Xreload.vim')
1893 source Xreload.vim
1894 echo g:GetStr()
1895
1896 lines =<< trim END
1897 vim9script noclear
1898 var str = 1234
1899 END
1900 writefile(lines, 'Xreload.vim')
1901 source Xreload.vim
1902 assert_fails('echo g:GetStr()', 'E1150:')
1903
1904 delfunc g:GetStr
1905 delete('Xreload.vim')
1906enddef
1907
Bram Moolenaarc970e422021-03-17 15:03:04 +01001908" Define CallFunc so that the test can be compiled
1909command CallFunc echo 'nop'
1910
1911def Test_script_reload_from_function()
1912 var lines =<< trim END
1913 vim9script
1914
1915 if exists('g:loaded')
1916 finish
1917 endif
1918 g:loaded = 1
1919 delcommand CallFunc
1920 command CallFunc Func()
1921 def Func()
Bram Moolenaara4c81be2021-03-17 15:23:16 +01001922 so XreloadFunc.vim
Bram Moolenaarc970e422021-03-17 15:03:04 +01001923 g:didTheFunc = 1
1924 enddef
1925 END
1926 writefile(lines, 'XreloadFunc.vim')
1927 source XreloadFunc.vim
1928 CallFunc
1929 assert_equal(1, g:didTheFunc)
1930
1931 delete('XreloadFunc.vim')
1932 delcommand CallFunc
1933 unlet g:loaded
1934 unlet g:didTheFunc
1935enddef
1936
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001937def Test_script_var_shadows_function()
1938 var lines =<< trim END
1939 vim9script
1940 def Func(): number
1941 return 123
1942 enddef
1943 var Func = 1
1944 END
1945 CheckScriptFailure(lines, 'E1041:', 5)
1946enddef
1947
Bram Moolenaar052ff292021-12-11 13:54:46 +00001948def Test_function_shadows_script_var()
1949 var lines =<< trim END
1950 vim9script
1951 var Func = 1
1952 def Func(): number
1953 return 123
1954 enddef
1955 END
1956 CheckScriptFailure(lines, 'E1041:', 3)
1957enddef
1958
Bram Moolenaarc3235272021-07-10 19:42:03 +02001959def Test_script_var_shadows_command()
1960 var lines =<< trim END
1961 var undo = 1
1962 undo = 2
1963 assert_equal(2, undo)
1964 END
1965 CheckDefAndScriptSuccess(lines)
1966
1967 lines =<< trim END
1968 var undo = 1
1969 undo
1970 END
1971 CheckDefAndScriptFailure(lines, 'E1207:', 2)
1972enddef
1973
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001974def Test_vim9script_call_wrong_type()
1975 var lines =<< trim END
1976 vim9script
1977 var Time = 'localtime'
1978 Time()
1979 END
1980 CheckScriptFailure(lines, 'E1085:')
1981enddef
1982
Bram Moolenaar95006e32020-08-29 17:47:08 +02001983def s:RetSome(): string
1984 return 'some'
1985enddef
1986
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001987" Not exported function that is referenced needs to be accessed by the
1988" script-local name.
1989def Test_vim9script_funcref()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001990 var sortlines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001991 vim9script
1992 def Compare(i1: number, i2: number): number
Bram Moolenaarbed36b92020-07-07 23:31:36 +02001993 return i2 - i1
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001994 enddef
1995
1996 export def FastSort(): list<number>
1997 return range(5)->sort(Compare)
1998 enddef
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001999
2000 export def GetString(arg: string): string
2001 return arg
2002 enddef
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002003 END
2004 writefile(sortlines, 'Xsort.vim')
2005
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002006 var lines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002007 vim9script
2008 import FastSort from './Xsort.vim'
2009 def Test()
2010 g:result = FastSort()
2011 enddef
2012 Test()
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002013
2014 # using a function imported with "as"
2015 import * as anAlias from './Xsort.vim'
2016 assert_equal('yes', anAlias.GetString('yes'))
2017
2018 # using the function from a compiled function
2019 def TestMore(): string
Bram Moolenaarca51cc02021-04-01 21:38:53 +02002020 var s = s:anAlias.GetString('foo')
2021 return s .. anAlias.GetString('bar')
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002022 enddef
Bram Moolenaarca51cc02021-04-01 21:38:53 +02002023 assert_equal('foobar', TestMore())
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002024
2025 # error when using a function that isn't exported
2026 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002027 END
2028 writefile(lines, 'Xscript.vim')
2029
2030 source Xscript.vim
2031 assert_equal([4, 3, 2, 1, 0], g:result)
2032
2033 unlet g:result
2034 delete('Xsort.vim')
2035 delete('Xscript.vim')
Bram Moolenaar95006e32020-08-29 17:47:08 +02002036
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002037 var Funcref = function('s:RetSome')
Bram Moolenaar95006e32020-08-29 17:47:08 +02002038 assert_equal('some', Funcref())
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002039enddef
2040
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002041" Check that when searching for "FilterFunc" it finds the import in the
2042" script where FastFilter() is called from, both as a string and as a direct
2043" function reference.
Bram Moolenaarc620c052020-07-08 15:16:19 +02002044def Test_vim9script_funcref_other_script()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002045 var filterLines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02002046 vim9script
2047 export def FilterFunc(idx: number, val: number): bool
2048 return idx % 2 == 1
2049 enddef
2050 export def FastFilter(): list<number>
2051 return range(10)->filter('FilterFunc')
2052 enddef
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002053 export def FastFilterDirect(): list<number>
2054 return range(10)->filter(FilterFunc)
2055 enddef
Bram Moolenaarc620c052020-07-08 15:16:19 +02002056 END
2057 writefile(filterLines, 'Xfilter.vim')
2058
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002059 var lines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02002060 vim9script
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002061 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim'
Bram Moolenaarc620c052020-07-08 15:16:19 +02002062 def Test()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002063 var x: list<number> = FastFilter()
Bram Moolenaarc620c052020-07-08 15:16:19 +02002064 enddef
2065 Test()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002066 def TestDirect()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002067 var x: list<number> = FastFilterDirect()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002068 enddef
2069 TestDirect()
Bram Moolenaarc620c052020-07-08 15:16:19 +02002070 END
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002071 CheckScriptSuccess(lines)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002072 delete('Xfilter.vim')
Bram Moolenaarc620c052020-07-08 15:16:19 +02002073enddef
2074
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002075def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002076 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002077 vim9script
2078 def FuncYes(): string
2079 return 'yes'
2080 enddef
2081 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002082 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002083 def FuncNo(): string
2084 return 'no'
2085 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002086 def g:DoCheck(no_exists: bool)
2087 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002088 assert_equal('no', FuncNo())
2089 enddef
2090 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002091 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002092 def g:DoCheck(no_exists: bool)
2093 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02002094 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002095 enddef
2096 END
2097
2098 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002099 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002100 source Xreloaded.vim
2101 g:DoCheck(true)
2102
2103 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002104 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002105 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01002106 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002107
2108 # FuncNo() is back
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002109 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002110 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01002111 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002112
2113 delete('Xreloaded.vim')
2114enddef
2115
Bram Moolenaar89483d42020-05-10 15:24:44 +02002116def Test_vim9script_reload_delvar()
2117 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002118 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02002119 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002120 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02002121 END
2122 writefile(lines, 'XreloadVar.vim')
2123 source XreloadVar.vim
2124
2125 # now write the script using the same variable locally - works
2126 lines =<< trim END
2127 vim9script
2128 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002129 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02002130 enddef
2131 END
2132 writefile(lines, 'XreloadVar.vim')
2133 source XreloadVar.vim
2134
2135 delete('XreloadVar.vim')
2136enddef
2137
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138def Test_import_absolute()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002139 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02002140 'vim9script',
2141 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
2142 'def UseExported()',
2143 ' g:imported_abs = exported',
2144 ' exported = 8888',
2145 ' g:imported_after = exported',
2146 'enddef',
2147 'UseExported()',
2148 'g:import_disassembled = execute("disass UseExported")',
2149 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150 writefile(import_lines, 'Ximport_abs.vim')
2151 writefile(s:export_script_lines, 'Xexport_abs.vim')
2152
2153 source Ximport_abs.vim
2154
2155 assert_equal(9876, g:imported_abs)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002156 assert_equal(8888, g:imported_after)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002157 assert_match('<SNR>\d\+_UseExported\_s*' ..
2158 'g:imported_abs = exported\_s*' ..
2159 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
2160 '1 STOREG g:imported_abs\_s*' ..
2161 'exported = 8888\_s*' ..
2162 '2 PUSHNR 8888\_s*' ..
2163 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
2164 'g:imported_after = exported\_s*' ..
2165 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
2166 '5 STOREG g:imported_after',
Bram Moolenaare6085c52020-04-12 20:19:16 +02002167 g:import_disassembled)
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002168
2169 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170 unlet g:imported_abs
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002171 unlet g:import_disassembled
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172
2173 delete('Ximport_abs.vim')
2174 delete('Xexport_abs.vim')
2175enddef
2176
2177def Test_import_rtp()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002178 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02002179 'vim9script',
2180 'import exported from "Xexport_rtp.vim"',
2181 'g:imported_rtp = exported',
2182 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002183 writefile(import_lines, 'Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002184 mkdir('import', 'p')
2185 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002187 var save_rtp = &rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002188 &rtp = getcwd()
2189 source Ximport_rtp.vim
2190 &rtp = save_rtp
2191
2192 assert_equal(9876, g:imported_rtp)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002194 Undo_export_script_lines()
2195 unlet g:imported_rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 delete('Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002197 delete('import', 'rf')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198enddef
2199
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002200def Test_import_compile_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002201 var export_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002202 'vim9script',
2203 'export def ExpFunc(): string',
2204 ' return notDefined',
2205 'enddef',
2206 ]
2207 writefile(export_lines, 'Xexported.vim')
2208
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002209 var import_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002210 'vim9script',
2211 'import ExpFunc from "./Xexported.vim"',
2212 'def ImpFunc()',
2213 ' echo ExpFunc()',
2214 'enddef',
2215 'defcompile',
2216 ]
2217 writefile(import_lines, 'Ximport.vim')
2218
2219 try
2220 source Ximport.vim
2221 catch /E1001/
Dominique Pelle923dce22021-11-21 11:36:04 +00002222 # Error should be before the Xexported.vim file.
Bram Moolenaar77072282020-09-16 17:55:40 +02002223 assert_match('E1001: Variable not found: notDefined', v:exception)
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002224 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
2225 endtry
2226
2227 delete('Xexported.vim')
2228 delete('Ximport.vim')
2229enddef
2230
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002231def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002232 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002233 'vim9script',
2234 'def Func()',
2235 ' eval [][0]',
2236 'enddef',
2237 'Func()',
2238 ]
2239 writefile(lines, 'Xtestscript.vim')
2240
2241 for count in range(3)
2242 try
2243 source Xtestscript.vim
2244 catch /E684/
2245 # function name should contain <SNR> every time
2246 assert_match('E684: list index out of range', v:exception)
2247 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
2248 endtry
2249 endfor
2250
2251 delete('Xtestscript.vim')
2252enddef
2253
Bram Moolenaareef21022020-08-01 22:16:43 +02002254def Test_func_overrules_import_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002255 var export_lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002256 vim9script
2257 export def Func()
2258 echo 'imported'
2259 enddef
2260 END
2261 writefile(export_lines, 'XexportedFunc.vim')
2262
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002263 var lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002264 vim9script
2265 import Func from './XexportedFunc.vim'
2266 def Func()
2267 echo 'local to function'
2268 enddef
2269 END
Bram Moolenaar052ff292021-12-11 13:54:46 +00002270 CheckScriptFailure(lines, 'E1041:')
Bram Moolenaareef21022020-08-01 22:16:43 +02002271
2272 lines =<< trim END
2273 vim9script
2274 import Func from './XexportedFunc.vim'
2275 def Outer()
2276 def Func()
2277 echo 'local to function'
2278 enddef
2279 enddef
2280 defcompile
2281 END
2282 CheckScriptFailure(lines, 'E1073:')
2283
2284 delete('XexportedFunc.vim')
2285enddef
2286
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002287def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002288 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002289 vim9script
2290 def Func()
2291 echo 'one'
2292 enddef
2293 def Func()
2294 echo 'two'
2295 enddef
2296 END
2297 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002298
2299 lines =<< trim END
2300 vim9script
2301 def Foo(): string
2302 return 'foo'
Bram Moolenaar052ff292021-12-11 13:54:46 +00002303 enddef
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002304 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002305 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002306 enddef
2307 defcompile
2308 END
2309 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002310enddef
2311
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002313 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002314 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 l->remove(0)
2316 l->add(5)
2317 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002318 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319enddef
2320
Bram Moolenaarae616492020-07-28 20:07:27 +02002321def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002322 CheckDefExecFailure(['a = 1'], 'E1100:')
2323 CheckDefExecFailure(['c = 1'], 'E1100:')
2324 CheckDefExecFailure(['i = 1'], 'E1100:')
2325 CheckDefExecFailure(['t = 1'], 'E1100:')
2326 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002327
Bram Moolenaarae616492020-07-28 20:07:27 +02002328 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
2329 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002330 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
2331 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002332 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
2333 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01002334 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
2335 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002336 CheckScriptFailure(['vim9script', 't'], 'E1100:')
2337 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
2338 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002339enddef
2340
Bram Moolenaar158906c2020-02-06 20:39:45 +01002341def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002342 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01002343 if what == 1
2344 res = "one"
2345 elseif what == 2
2346 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002347 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01002348 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002349 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01002350 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002351enddef
2352
Bram Moolenaar158906c2020-02-06 20:39:45 +01002353def Test_if_elseif_else()
2354 assert_equal('one', IfElse(1))
2355 assert_equal('two', IfElse(2))
2356 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002357enddef
2358
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002359def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002360 CheckDefFailure(['elseif true'], 'E582:')
2361 CheckDefFailure(['else'], 'E581:')
2362 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01002363 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002364 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002365
2366 var lines =<< trim END
2367 var s = ''
2368 if s = ''
2369 endif
2370 END
2371 CheckDefFailure(lines, 'E488:')
2372
2373 lines =<< trim END
2374 var s = ''
2375 if s == ''
2376 elseif s = ''
2377 endif
2378 END
2379 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002380enddef
2381
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002382let g:bool_true = v:true
2383let g:bool_false = v:false
2384
2385def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002386 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002387 if true ? true : false
2388 res = true
2389 endif
2390 assert_equal(true, res)
2391
Bram Moolenaar585fea72020-04-02 22:33:21 +02002392 g:glob = 2
2393 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02002394 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002395 endif
2396 assert_equal(2, g:glob)
2397 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02002398 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002399 endif
2400 assert_equal(3, g:glob)
2401
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002402 res = false
2403 if g:bool_true ? true : false
2404 res = true
2405 endif
2406 assert_equal(true, res)
2407
2408 res = false
2409 if true ? g:bool_true : false
2410 res = true
2411 endif
2412 assert_equal(true, res)
2413
2414 res = false
2415 if true ? true : g:bool_false
2416 res = true
2417 endif
2418 assert_equal(true, res)
2419
2420 res = false
2421 if true ? false : true
2422 res = true
2423 endif
2424 assert_equal(false, res)
2425
2426 res = false
2427 if false ? false : true
2428 res = true
2429 endif
2430 assert_equal(true, res)
2431
2432 res = false
2433 if false ? true : false
2434 res = true
2435 endif
2436 assert_equal(false, res)
2437
2438 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002439 if has('xyz') ? true : false
2440 res = true
2441 endif
2442 assert_equal(false, res)
2443
2444 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002445 if true && true
2446 res = true
2447 endif
2448 assert_equal(true, res)
2449
2450 res = false
2451 if true && false
2452 res = true
2453 endif
2454 assert_equal(false, res)
2455
2456 res = false
2457 if g:bool_true && false
2458 res = true
2459 endif
2460 assert_equal(false, res)
2461
2462 res = false
2463 if true && g:bool_false
2464 res = true
2465 endif
2466 assert_equal(false, res)
2467
2468 res = false
2469 if false && false
2470 res = true
2471 endif
2472 assert_equal(false, res)
2473
2474 res = false
2475 if true || false
2476 res = true
2477 endif
2478 assert_equal(true, res)
2479
2480 res = false
2481 if g:bool_true || false
2482 res = true
2483 endif
2484 assert_equal(true, res)
2485
2486 res = false
2487 if true || g:bool_false
2488 res = true
2489 endif
2490 assert_equal(true, res)
2491
2492 res = false
2493 if false || false
2494 res = true
2495 endif
2496 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02002497
2498 # with constant "false" expression may be invalid so long as the syntax is OK
Bram Moolenaarc3235272021-07-10 19:42:03 +02002499 if false | eval 1 + 2 | endif
Bram Moolenaar3988f642020-08-27 22:43:03 +02002500 if false | eval burp + 234 | endif
2501 if false | echo burp 234 'asd' | endif
2502 if false
2503 burp
2504 endif
Bram Moolenaare525bdd2021-08-07 18:12:40 +02002505
2506 # expression with line breaks skipped
2507 if false
2508 ('aaa'
2509 .. 'bbb'
2510 .. 'ccc'
2511 )->setline(1)
2512 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002513enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002514
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002515def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002516 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
2517 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
2518 CheckDefFailure(["if has('aaa'"], 'E110:')
2519 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002520enddef
2521
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002522def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002523 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002524 if i % 2
2525 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002526 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002527 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002528 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002529 endif
2530 x += 1
2531 else
2532 x += 1000
2533 endif
2534 return x
2535enddef
2536
2537def Test_nested_if()
2538 assert_equal(1, RunNested(1))
2539 assert_equal(1000, RunNested(2))
2540enddef
2541
Bram Moolenaarad39c092020-02-26 18:23:43 +01002542def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01002543 # missing argument is ignored
2544 execute
2545 execute # comment
2546
Bram Moolenaarad39c092020-02-26 18:23:43 +01002547 new
2548 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002549 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002550 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002551
Bram Moolenaard2c61702020-09-06 15:58:36 +02002552 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002553 assert_equal('execute-string', getline(1))
2554
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002555 var cmd1 = 'setline(1,'
2556 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002557 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01002558 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002559
Bram Moolenaard2c61702020-09-06 15:58:36 +02002560 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002561 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002562
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002563 var cmd_first = 'call '
2564 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002565 execute cmd_first .. cmd_last
2566 assert_equal('execute-var-var', getline(1))
2567 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02002568
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002569 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002570 execute 'echomsg' (n ? '"true"' : '"no"')
2571 assert_match('^true$', Screenline(&lines))
2572
Bram Moolenaare0de1712020-12-02 17:36:54 +01002573 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002574 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
2575
Bram Moolenaard2c61702020-09-06 15:58:36 +02002576 CheckDefFailure(['execute xxx'], 'E1001:', 1)
2577 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
2578 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002579enddef
2580
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002581def Test_execute_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002582 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002583 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002584 vim9script
2585 execute 'g:someVar'
2586 .. ' = ' ..
2587 '28'
2588 assert_equal(28, g:someVar)
2589 unlet g:someVar
2590 END
2591 CheckScriptSuccess(lines)
2592enddef
2593
Bram Moolenaarad39c092020-02-26 18:23:43 +01002594def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002595 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02002596 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002597 assert_match('^something$', Screenline(&lines))
2598
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002599 echo "some" # comment
2600 echon "thing"
2601 assert_match('^something$', Screenline(&lines))
2602
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002603 var str1 = 'some'
2604 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002605 echo str1 str2
2606 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002607
Bram Moolenaard2c61702020-09-06 15:58:36 +02002608 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01002609enddef
2610
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002611def Test_echomsg_cmd()
2612 echomsg 'some' 'more' # comment
2613 assert_match('^some more$', Screenline(&lines))
2614 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002615 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002616 assert_match('^some more$', Screenline(&lines))
2617
Bram Moolenaard2c61702020-09-06 15:58:36 +02002618 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002619enddef
2620
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002621def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002622 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002623 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002624 vim9script
2625 echomsg 'here'
2626 .. ' is ' ..
2627 'a message'
2628 assert_match('^here is a message$', Screenline(&lines))
2629 END
2630 CheckScriptSuccess(lines)
2631enddef
2632
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002633def Test_echoerr_cmd()
Bram Moolenaar7de62622021-08-07 15:05:47 +02002634 var local = 'local'
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002635 try
Bram Moolenaar7de62622021-08-07 15:05:47 +02002636 echoerr 'something' local 'wrong' # comment
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002637 catch
Bram Moolenaar7de62622021-08-07 15:05:47 +02002638 assert_match('something local wrong', v:exception)
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002639 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002640enddef
2641
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002642def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002643 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002644 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002645 vim9script
2646 try
2647 echoerr 'this'
2648 .. ' is ' ..
2649 'wrong'
2650 catch
2651 assert_match('this is wrong', v:exception)
2652 endtry
2653 END
2654 CheckScriptSuccess(lines)
2655enddef
2656
Bram Moolenaar7de62622021-08-07 15:05:47 +02002657def Test_echoconsole_cmd()
2658 var local = 'local'
2659 echoconsole 'something' local # comment
2660 # output goes anywhere
2661enddef
2662
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002663def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002664 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002665 vim9script
2666 new
2667 for var in range(0, 3)
2668 append(line('$'), var)
2669 endfor
2670 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2671 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01002672
2673 var result = ''
2674 for i in [1, 2, 3]
2675 var loop = ' loop ' .. i
2676 result ..= loop
2677 endfor
2678 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002679 END
2680 writefile(lines, 'Xvim9for.vim')
2681 source Xvim9for.vim
2682 delete('Xvim9for.vim')
2683enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684
rbtnnbebf0692021-08-21 17:26:50 +02002685def Test_for_skipped_block()
2686 # test skipped blocks at outside of function
2687 var lines =<< trim END
2688 var result = []
2689 if true
2690 for n in [1, 2]
2691 result += [n]
2692 endfor
2693 else
2694 for n in [3, 4]
2695 result += [n]
2696 endfor
2697 endif
2698 assert_equal([1, 2], result)
2699
2700 result = []
2701 if false
2702 for n in [1, 2]
2703 result += [n]
2704 endfor
2705 else
2706 for n in [3, 4]
2707 result += [n]
2708 endfor
2709 endif
2710 assert_equal([3, 4], result)
2711 END
2712 CheckDefAndScriptSuccess(lines)
2713
2714 # test skipped blocks at inside of function
2715 lines =<< trim END
2716 def DefTrue()
2717 var result = []
2718 if true
2719 for n in [1, 2]
2720 result += [n]
2721 endfor
2722 else
2723 for n in [3, 4]
2724 result += [n]
2725 endfor
2726 endif
2727 assert_equal([1, 2], result)
2728 enddef
2729 DefTrue()
2730
2731 def DefFalse()
2732 var result = []
2733 if false
2734 for n in [1, 2]
2735 result += [n]
2736 endfor
2737 else
2738 for n in [3, 4]
2739 result += [n]
2740 endfor
2741 endif
2742 assert_equal([3, 4], result)
2743 enddef
2744 DefFalse()
2745 END
2746 CheckDefAndScriptSuccess(lines)
2747enddef
2748
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002749def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002750 var lines =<< trim END
2751 var result = ''
2752 for cnt in range(7)
2753 if cnt == 4
2754 break
2755 endif
2756 if cnt == 2
2757 continue
2758 endif
2759 result ..= cnt .. '_'
2760 endfor
2761 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02002762
Bram Moolenaarf2253962021-04-13 20:53:13 +02002763 var concat = ''
2764 for str in eval('["one", "two"]')
2765 concat ..= str
2766 endfor
2767 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002768
Bram Moolenaarf2253962021-04-13 20:53:13 +02002769 var total = 0
2770 for nr in
2771 [1, 2, 3]
2772 total += nr
2773 endfor
2774 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002775
Bram Moolenaarf2253962021-04-13 20:53:13 +02002776 total = 0
2777 for nr
2778 in [1, 2, 3]
2779 total += nr
2780 endfor
2781 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002782
Bram Moolenaarf2253962021-04-13 20:53:13 +02002783 total = 0
2784 for nr
2785 in
2786 [1, 2, 3]
2787 total += nr
2788 endfor
2789 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01002790
Bram Moolenaara3589a02021-04-14 13:30:46 +02002791 # with type
2792 total = 0
2793 for n: number in [1, 2, 3]
2794 total += n
2795 endfor
2796 assert_equal(6, total)
2797
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002798 var chars = ''
2799 for s: string in 'foobar'
2800 chars ..= s
2801 endfor
2802 assert_equal('foobar', chars)
2803
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02002804 chars = ''
2805 for x: string in {a: 'a', b: 'b'}->values()
2806 chars ..= x
2807 endfor
2808 assert_equal('ab', chars)
2809
Bram Moolenaara3589a02021-04-14 13:30:46 +02002810 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02002811 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02002812 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2813 res ..= n .. s
2814 endfor
2815 assert_equal('1a2b', res)
2816
Bram Moolenaar444d8782021-06-26 12:40:56 +02002817 # unpack with one var
2818 var reslist = []
2819 for [x] in [['aaa'], ['bbb']]
2820 reslist->add(x)
2821 endfor
2822 assert_equal(['aaa', 'bbb'], reslist)
2823
Bram Moolenaara3589a02021-04-14 13:30:46 +02002824 # loop over string
2825 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02002826 for c in 'aéc̀d'
2827 res ..= c .. '-'
2828 endfor
2829 assert_equal('a-é-c̀-d-', res)
2830
2831 res = ''
2832 for c in ''
2833 res ..= c .. '-'
2834 endfor
2835 assert_equal('', res)
2836
2837 res = ''
2838 for c in test_null_string()
2839 res ..= c .. '-'
2840 endfor
2841 assert_equal('', res)
2842
2843 var foo: list<dict<any>> = [
2844 {a: 'Cat'}
2845 ]
2846 for dd in foo
2847 dd.counter = 12
2848 endfor
2849 assert_equal([{a: 'Cat', counter: 12}], foo)
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002850
2851 reslist = []
2852 for _ in range(3)
2853 reslist->add('x')
2854 endfor
2855 assert_equal(['x', 'x', 'x'], reslist)
Bram Moolenaarf2253962021-04-13 20:53:13 +02002856 END
2857 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002858enddef
2859
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02002860def Test_for_loop_with_closure()
2861 var lines =<< trim END
2862 var flist: list<func>
2863 for i in range(5)
2864 var inloop = i
2865 flist[i] = () => inloop
2866 endfor
2867 for i in range(5)
2868 assert_equal(4, flist[i]())
2869 endfor
2870 END
2871 CheckDefAndScriptSuccess(lines)
2872
2873 lines =<< trim END
2874 var flist: list<func>
2875 for i in range(5)
2876 var inloop = i
2877 flist[i] = () => {
2878 return inloop
2879 }
2880 endfor
2881 for i in range(5)
2882 assert_equal(4, flist[i]())
2883 endfor
2884 END
2885 CheckDefAndScriptSuccess(lines)
2886enddef
2887
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002888def Test_for_loop_fails()
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002889 CheckDefAndScriptFailure(['for '], ['E1097:', 'E690:'])
2890 CheckDefAndScriptFailure(['for x'], ['E1097:', 'E690:'])
2891 CheckDefAndScriptFailure(['for x in'], ['E1097:', 'E15:'])
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002892 CheckDefAndScriptFailure(['for # in range(5)'], 'E690:')
2893 CheckDefAndScriptFailure(['for i In range(5)'], 'E690:')
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002894 CheckDefAndScriptFailure(['var x = 5', 'for x in range(5)', 'endfor'], ['E1017:', 'E1041:'])
Bram Moolenaard4ab8072021-07-08 19:22:12 +02002895 CheckScriptFailure(['vim9script', 'var x = 5', 'for x in range(5)', '# comment', 'endfor'], 'E1041:', 3)
Bram Moolenaar822ba242020-05-24 23:00:18 +02002896 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002897 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02002898 CheckDefFailure(['for i in xxx'], 'E1001:')
2899 CheckDefFailure(['endfor'], 'E588:')
2900 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002901
2902 # wrong type detected at compile time
2903 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2904
2905 # wrong type detected at runtime
2906 g:adict = {a: 1}
2907 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2908 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002909
2910 var lines =<< trim END
2911 var d: list<dict<any>> = [{a: 0}]
2912 for e in d
2913 e = {a: 0, b: ''}
2914 endfor
2915 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002916 CheckDefAndScriptFailure(lines, ['E1018:', 'E46:'], 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002917
2918 lines =<< trim END
2919 for nr: number in ['foo']
2920 endfor
2921 END
2922 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaar404557e2021-07-05 21:41:48 +02002923
2924 lines =<< trim END
2925 for n : number in [1, 2]
2926 echo n
2927 endfor
2928 END
2929 CheckDefAndScriptFailure(lines, 'E1059:', 1)
Bram Moolenaarefc5db52021-07-07 21:21:30 +02002930
2931 lines =<< trim END
2932 var d: dict<number> = {a: 1, b: 2}
2933 for [k: job, v: job] in d->items()
2934 echo k v
2935 endfor
2936 END
2937 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected job but got string', 2)
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002938
2939 lines =<< trim END
2940 var i = 0
2941 for i in [1, 2, 3]
2942 echo i
2943 endfor
2944 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002945 CheckDefExecAndScriptFailure(lines, ['E1017:', 'E1041:'])
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002946
2947 lines =<< trim END
2948 var l = [0]
2949 for l[0] in [1, 2, 3]
2950 echo l[0]
2951 endfor
2952 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002953 CheckDefExecAndScriptFailure(lines, ['E461:', 'E1017:'])
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002954
2955 lines =<< trim END
2956 var d = {x: 0}
2957 for d.x in [1, 2, 3]
2958 echo d.x
2959 endfor
2960 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002961 CheckDefExecAndScriptFailure(lines, ['E461:', 'E1017:'])
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002962enddef
2963
Bram Moolenaarea870692020-12-02 14:24:30 +01002964def Test_for_loop_script_var()
2965 # cannot use s:var in a :def function
Bram Moolenaara99fb232021-12-20 12:25:03 +00002966 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1254:')
Bram Moolenaarea870692020-12-02 14:24:30 +01002967
2968 # can use s:var in Vim9 script, with or without s:
2969 var lines =<< trim END
2970 vim9script
2971 var total = 0
2972 for s:var in [1, 2, 3]
2973 total += s:var
2974 endfor
2975 assert_equal(6, total)
2976
2977 total = 0
2978 for var in [1, 2, 3]
2979 total += var
2980 endfor
2981 assert_equal(6, total)
2982 END
2983enddef
2984
Bram Moolenaar792f7862020-11-23 08:31:18 +01002985def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01002986 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002987 var result = []
2988 for [v1, v2] in [[1, 2], [3, 4]]
2989 result->add(v1)
2990 result->add(v2)
2991 endfor
2992 assert_equal([1, 2, 3, 4], result)
2993
2994 result = []
2995 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2996 result->add(v1)
2997 result->add(v2)
2998 result->add(v3)
2999 endfor
3000 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
3001
3002 result = []
3003 for [&ts, &sw] in [[1, 2], [3, 4]]
3004 result->add(&ts)
3005 result->add(&sw)
3006 endfor
3007 assert_equal([1, 2, 3, 4], result)
3008
3009 var slist: list<string>
3010 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
3011 slist->add($LOOPVAR)
3012 slist->add(@r)
3013 slist->add(v:errmsg)
3014 endfor
3015 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
3016
3017 slist = []
3018 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
3019 slist->add(g:globalvar)
3020 slist->add(b:bufvar)
3021 slist->add(w:winvar)
3022 slist->add(t:tabvar)
3023 endfor
3024 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01003025 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaarb777da92021-05-22 21:40:39 +02003026
3027 var res = []
3028 for [_, n, _] in [[1, 2, 3], [4, 5, 6]]
3029 res->add(n)
3030 endfor
3031 assert_equal([2, 5], res)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01003032 END
3033 CheckDefAndScriptSuccess(lines)
3034
3035 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01003036 for [v1, v2] in [[1, 2, 3], [3, 4]]
3037 echo v1 v2
3038 endfor
3039 END
3040 CheckDefExecFailure(lines, 'E710:', 1)
3041
3042 lines =<< trim END
3043 for [v1, v2] in [[1], [3, 4]]
3044 echo v1 v2
3045 endfor
3046 END
3047 CheckDefExecFailure(lines, 'E711:', 1)
3048
3049 lines =<< trim END
3050 for [v1, v1] in [[1, 2], [3, 4]]
3051 echo v1
3052 endfor
3053 END
3054 CheckDefExecFailure(lines, 'E1017:', 1)
3055enddef
3056
Bram Moolenaarc150c092021-02-13 15:02:46 +01003057def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02003058 var lines =<< trim END
3059 var looped = 0
3060 var cleanup = 0
3061 for i in range(3)
3062 looped += 1
3063 try
3064 eval [][0]
3065 catch
3066 continue
3067 finally
3068 cleanup += 1
3069 endtry
3070 endfor
3071 assert_equal(3, looped)
3072 assert_equal(3, cleanup)
3073 END
3074 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003075enddef
3076
rbtnnd895b1d2021-08-20 20:54:25 +02003077def Test_while_skipped_block()
3078 # test skipped blocks at outside of function
3079 var lines =<< trim END
3080 var result = []
3081 var n = 0
3082 if true
3083 n = 1
3084 while n < 3
3085 result += [n]
3086 n += 1
3087 endwhile
3088 else
3089 n = 3
3090 while n < 5
3091 result += [n]
3092 n += 1
3093 endwhile
3094 endif
3095 assert_equal([1, 2], result)
3096
3097 result = []
3098 if false
3099 n = 1
3100 while n < 3
3101 result += [n]
3102 n += 1
3103 endwhile
3104 else
3105 n = 3
3106 while n < 5
3107 result += [n]
3108 n += 1
3109 endwhile
3110 endif
3111 assert_equal([3, 4], result)
3112 END
3113 CheckDefAndScriptSuccess(lines)
3114
3115 # test skipped blocks at inside of function
3116 lines =<< trim END
3117 def DefTrue()
3118 var result = []
3119 var n = 0
3120 if true
3121 n = 1
3122 while n < 3
3123 result += [n]
3124 n += 1
3125 endwhile
3126 else
3127 n = 3
3128 while n < 5
3129 result += [n]
3130 n += 1
3131 endwhile
3132 endif
3133 assert_equal([1, 2], result)
3134 enddef
3135 DefTrue()
3136
3137 def DefFalse()
3138 var result = []
3139 var n = 0
3140 if false
3141 n = 1
3142 while n < 3
3143 result += [n]
3144 n += 1
3145 endwhile
3146 else
3147 n = 3
3148 while n < 5
3149 result += [n]
3150 n += 1
3151 endwhile
3152 endif
3153 assert_equal([3, 4], result)
3154 enddef
3155 DefFalse()
3156 END
3157 CheckDefAndScriptSuccess(lines)
3158enddef
3159
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003160def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003161 var result = ''
3162 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003163 while cnt < 555
3164 if cnt == 3
3165 break
3166 endif
3167 cnt += 1
3168 if cnt == 2
3169 continue
3170 endif
3171 result ..= cnt .. '_'
3172 endwhile
3173 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01003174
3175 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01003176 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01003177 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003178enddef
3179
Bram Moolenaar7a53f292021-11-22 18:31:02 +00003180def Test_while_loop_in_script()
3181 var lines =<< trim END
3182 vim9script
3183 var result = ''
3184 var cnt = 0
3185 while cnt < 3
3186 var s = 'v' .. cnt
3187 result ..= s
3188 cnt += 1
3189 endwhile
3190 assert_equal('v0v1v2', result)
3191 END
3192 CheckScriptSuccess(lines)
3193enddef
3194
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003195def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02003196 CheckDefFailure(['while xxx'], 'E1001:')
3197 CheckDefFailure(['endwhile'], 'E588:')
3198 CheckDefFailure(['continue'], 'E586:')
3199 CheckDefFailure(['if true', 'continue'], 'E586:')
3200 CheckDefFailure(['break'], 'E587:')
3201 CheckDefFailure(['if true', 'break'], 'E587:')
3202 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01003203
3204 var lines =<< trim END
3205 var s = ''
3206 while s = ''
3207 endwhile
3208 END
3209 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003210enddef
3211
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003212def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003213 var caught = false
3214 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003215 try
3216 while 1
3217 x += 1
3218 if x == 100
3219 feedkeys("\<C-C>", 'Lt')
3220 endif
3221 endwhile
3222 catch
3223 caught = true
3224 assert_equal(100, x)
3225 endtry
3226 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003227 # consume the CTRL-C
3228 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003229enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01003230
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003231def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003232 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003233 'one',
3234 'two',
3235 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003236 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003237 assert_equal(['one', 'two', 'three'], mylist)
3238
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003239 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003240 ['one']: 1,
3241 ['two']: 2,
3242 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003243 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003244 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01003245 assert_equal({one: 1, two: 2, three: 3}, mydict)
3246 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003247 one: 1, # comment
3248 two: # comment
3249 2, # comment
3250 three: 3 # comment
3251 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003252 assert_equal({one: 1, two: 2, three: 3}, mydict)
3253 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003254 one: 1,
3255 two:
3256 2,
3257 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003258 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003259 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02003260
3261 assert_equal(
3262 ['one', 'two', 'three'],
3263 split('one two three')
3264 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003265enddef
3266
Bram Moolenaar7a092242020-04-16 22:10:49 +02003267def Test_vim9_comment()
3268 CheckScriptSuccess([
3269 'vim9script',
3270 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01003271 '#something',
3272 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003273 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01003274
3275 split Xfile
3276 CheckScriptSuccess([
3277 'vim9script',
3278 'edit #something',
3279 ])
3280 CheckScriptSuccess([
3281 'vim9script',
3282 'edit #{something',
3283 ])
3284 close
3285
Bram Moolenaar7a092242020-04-16 22:10:49 +02003286 CheckScriptFailure([
3287 'vim9script',
3288 ':# something',
3289 ], 'E488:')
3290 CheckScriptFailure([
3291 '# something',
3292 ], 'E488:')
3293 CheckScriptFailure([
3294 ':# something',
3295 ], 'E488:')
3296
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003297 { # block start
3298 } # block end
3299 CheckDefFailure([
3300 '{# comment',
3301 ], 'E488:')
3302 CheckDefFailure([
3303 '{',
3304 '}# comment',
3305 ], 'E488:')
3306
3307 echo "yes" # comment
3308 CheckDefFailure([
3309 'echo "yes"# comment',
3310 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003311 CheckScriptSuccess([
3312 'vim9script',
3313 'echo "yes" # something',
3314 ])
3315 CheckScriptFailure([
3316 'vim9script',
3317 'echo "yes"# something',
3318 ], 'E121:')
3319 CheckScriptFailure([
3320 'vim9script',
3321 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003322 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003323 CheckScriptFailure([
3324 'echo "yes" # something',
3325 ], 'E121:')
3326
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003327 exe "echo" # comment
3328 CheckDefFailure([
3329 'exe "echo"# comment',
3330 ], 'E488:')
3331 CheckScriptSuccess([
3332 'vim9script',
3333 'exe "echo" # something',
3334 ])
3335 CheckScriptFailure([
3336 'vim9script',
3337 'exe "echo"# something',
3338 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003339 CheckScriptFailure([
3340 'vim9script',
3341 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003342 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003343 CheckScriptFailure([
3344 'exe "echo" # something',
3345 ], 'E121:')
3346
Bram Moolenaar7a092242020-04-16 22:10:49 +02003347 CheckDefFailure([
3348 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003349 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003350 'catch',
3351 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003352 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003353 CheckScriptFailure([
3354 'vim9script',
3355 'try# comment',
3356 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003357 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003358 CheckDefFailure([
3359 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003360 ' throw#comment',
3361 'catch',
3362 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003363 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003364 CheckDefFailure([
3365 'try',
3366 ' throw "yes"#comment',
3367 'catch',
3368 'endtry',
3369 ], 'E488:')
3370 CheckDefFailure([
3371 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003372 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003373 'catch# comment',
3374 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003375 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003376 CheckScriptFailure([
3377 'vim9script',
3378 'try',
3379 ' echo "yes"',
3380 'catch# comment',
3381 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003382 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003383 CheckDefFailure([
3384 'try',
3385 ' echo "yes"',
3386 'catch /pat/# comment',
3387 'endtry',
3388 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003389 CheckDefFailure([
3390 'try',
3391 'echo "yes"',
3392 'catch',
3393 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003394 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003395 CheckScriptFailure([
3396 'vim9script',
3397 'try',
3398 ' echo "yes"',
3399 'catch',
3400 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003401 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003402
3403 CheckScriptSuccess([
3404 'vim9script',
3405 'hi # comment',
3406 ])
3407 CheckScriptFailure([
3408 'vim9script',
3409 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003410 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003411 CheckScriptSuccess([
3412 'vim9script',
3413 'hi Search # comment',
3414 ])
3415 CheckScriptFailure([
3416 'vim9script',
3417 'hi Search# comment',
3418 ], 'E416:')
3419 CheckScriptSuccess([
3420 'vim9script',
3421 'hi link This Search # comment',
3422 ])
3423 CheckScriptFailure([
3424 'vim9script',
3425 'hi link This That# comment',
3426 ], 'E413:')
3427 CheckScriptSuccess([
3428 'vim9script',
3429 'hi clear This # comment',
3430 'hi clear # comment',
3431 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003432 # not tested, because it doesn't give an error but a warning:
3433 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02003434 CheckScriptFailure([
3435 'vim9script',
3436 'hi clear# comment',
3437 ], 'E416:')
3438
3439 CheckScriptSuccess([
3440 'vim9script',
3441 'hi Group term=bold',
3442 'match Group /todo/ # comment',
3443 ])
3444 CheckScriptFailure([
3445 'vim9script',
3446 'hi Group term=bold',
3447 'match Group /todo/# comment',
3448 ], 'E488:')
3449 CheckScriptSuccess([
3450 'vim9script',
3451 'match # comment',
3452 ])
3453 CheckScriptFailure([
3454 'vim9script',
3455 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003456 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003457 CheckScriptSuccess([
3458 'vim9script',
3459 'match none # comment',
3460 ])
3461 CheckScriptFailure([
3462 'vim9script',
3463 'match none# comment',
3464 ], 'E475:')
3465
3466 CheckScriptSuccess([
3467 'vim9script',
3468 'menutrans clear # comment',
3469 ])
3470 CheckScriptFailure([
3471 'vim9script',
3472 'menutrans clear# comment text',
3473 ], 'E474:')
3474
3475 CheckScriptSuccess([
3476 'vim9script',
3477 'syntax clear # comment',
3478 ])
3479 CheckScriptFailure([
3480 'vim9script',
3481 'syntax clear# comment text',
3482 ], 'E28:')
3483 CheckScriptSuccess([
3484 'vim9script',
3485 'syntax keyword Word some',
3486 'syntax clear Word # comment',
3487 ])
3488 CheckScriptFailure([
3489 'vim9script',
3490 'syntax keyword Word some',
3491 'syntax clear Word# comment text',
3492 ], 'E28:')
3493
3494 CheckScriptSuccess([
3495 'vim9script',
3496 'syntax list # comment',
3497 ])
3498 CheckScriptFailure([
3499 'vim9script',
3500 'syntax list# comment text',
3501 ], 'E28:')
3502
3503 CheckScriptSuccess([
3504 'vim9script',
3505 'syntax match Word /pat/ oneline # comment',
3506 ])
3507 CheckScriptFailure([
3508 'vim9script',
3509 'syntax match Word /pat/ oneline# comment',
3510 ], 'E475:')
3511
3512 CheckScriptSuccess([
3513 'vim9script',
3514 'syntax keyword Word word # comm[ent',
3515 ])
3516 CheckScriptFailure([
3517 'vim9script',
3518 'syntax keyword Word word# comm[ent',
3519 ], 'E789:')
3520
3521 CheckScriptSuccess([
3522 'vim9script',
3523 'syntax match Word /pat/ # comment',
3524 ])
3525 CheckScriptFailure([
3526 'vim9script',
3527 'syntax match Word /pat/# comment',
3528 ], 'E402:')
3529
3530 CheckScriptSuccess([
3531 'vim9script',
3532 'syntax match Word /pat/ contains=Something # comment',
3533 ])
3534 CheckScriptFailure([
3535 'vim9script',
3536 'syntax match Word /pat/ contains=Something# comment',
3537 ], 'E475:')
3538 CheckScriptFailure([
3539 'vim9script',
3540 'syntax match Word /pat/ contains= # comment',
3541 ], 'E406:')
3542 CheckScriptFailure([
3543 'vim9script',
3544 'syntax match Word /pat/ contains=# comment',
3545 ], 'E475:')
3546
3547 CheckScriptSuccess([
3548 'vim9script',
3549 'syntax region Word start=/pat/ end=/pat/ # comment',
3550 ])
3551 CheckScriptFailure([
3552 'vim9script',
3553 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02003554 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003555
3556 CheckScriptSuccess([
3557 'vim9script',
3558 'syntax sync # comment',
3559 ])
3560 CheckScriptFailure([
3561 'vim9script',
3562 'syntax sync# comment',
3563 ], 'E404:')
3564 CheckScriptSuccess([
3565 'vim9script',
3566 'syntax sync ccomment # comment',
3567 ])
3568 CheckScriptFailure([
3569 'vim9script',
3570 'syntax sync ccomment# comment',
3571 ], 'E404:')
3572
3573 CheckScriptSuccess([
3574 'vim9script',
3575 'syntax cluster Some contains=Word # comment',
3576 ])
3577 CheckScriptFailure([
3578 'vim9script',
3579 'syntax cluster Some contains=Word# comment',
3580 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003581
3582 CheckScriptSuccess([
3583 'vim9script',
3584 'command Echo echo # comment',
3585 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003586 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003587 ])
3588 CheckScriptFailure([
3589 'vim9script',
3590 'command Echo echo# comment',
3591 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003592 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003593 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003594
3595 var curdir = getcwd()
3596 CheckScriptSuccess([
3597 'command Echo cd " comment',
3598 'Echo',
3599 'delcommand Echo',
3600 ])
3601 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01003602 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003603 'command Echo cd # comment',
3604 'Echo',
3605 'delcommand Echo',
3606 ])
3607 CheckScriptFailure([
3608 'vim9script',
3609 'command Echo cd " comment',
3610 'Echo',
3611 ], 'E344:')
3612 delcommand Echo
3613 chdir(curdir)
3614
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003615 CheckScriptFailure([
3616 'vim9script',
3617 'command Echo# comment',
3618 ], 'E182:')
3619 CheckScriptFailure([
3620 'vim9script',
3621 'command Echo echo',
3622 'command Echo# comment',
3623 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003624 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003625
3626 CheckScriptSuccess([
3627 'vim9script',
3628 'function # comment',
3629 ])
3630 CheckScriptFailure([
3631 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02003632 'function " comment',
3633 ], 'E129:')
3634 CheckScriptFailure([
3635 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003636 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003637 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003638 CheckScriptSuccess([
3639 'vim9script',
3640 'function CheckScriptSuccess # comment',
3641 ])
3642 CheckScriptFailure([
3643 'vim9script',
3644 'function CheckScriptSuccess# comment',
3645 ], 'E488:')
3646
3647 CheckScriptSuccess([
3648 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003649 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003650 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003651 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003652 ])
3653 CheckScriptFailure([
3654 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003655 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003656 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003657 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003658 ], 'E488:')
3659
3660 CheckScriptSuccess([
3661 'vim9script',
3662 'call execute("ls") # comment',
3663 ])
3664 CheckScriptFailure([
3665 'vim9script',
3666 'call execute("ls")# comment',
3667 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02003668
3669 CheckScriptFailure([
3670 'def Test() " comment',
3671 'enddef',
3672 ], 'E488:')
3673 CheckScriptFailure([
3674 'vim9script',
3675 'def Test() " comment',
3676 'enddef',
3677 ], 'E488:')
3678
3679 CheckScriptSuccess([
3680 'func Test() " comment',
3681 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003682 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02003683 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02003684 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02003685 'vim9script',
3686 'func Test() " comment',
3687 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02003688 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02003689
3690 CheckScriptSuccess([
3691 'def Test() # comment',
3692 'enddef',
3693 ])
3694 CheckScriptFailure([
3695 'func Test() # comment',
3696 'endfunc',
3697 ], 'E488:')
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003698
3699 var lines =<< trim END
3700 vim9script
3701 syn region Text
3702 \ start='foo'
3703 #\ comment
3704 \ end='bar'
Bram Moolenaar5072b472021-06-03 21:56:10 +02003705 syn region Text start='foo'
3706 #\ comment
3707 \ end='bar'
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003708 END
3709 CheckScriptSuccess(lines)
3710
3711 lines =<< trim END
3712 vim9script
3713 syn region Text
3714 \ start='foo'
3715 "\ comment
3716 \ end='bar'
3717 END
3718 CheckScriptFailure(lines, 'E399:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003719enddef
3720
3721def Test_vim9_comment_gui()
3722 CheckCanRunGui
3723
3724 CheckScriptFailure([
3725 'vim9script',
3726 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003727 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003728 CheckScriptFailure([
3729 'vim9script',
3730 'gui -f#comment'
Bram Moolenaarb8bd2e62021-08-21 17:13:14 +02003731 ], 'E194:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003732enddef
3733
Bram Moolenaara26b9702020-04-18 19:53:28 +02003734def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02003735 au TabEnter *.vim g:entered = 1
3736 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02003737
3738 edit test.vim
3739 doautocmd TabEnter #comment
3740 assert_equal(1, g:entered)
3741
3742 doautocmd TabEnter f.x
3743 assert_equal(2, g:entered)
3744
3745 g:entered = 0
3746 doautocmd TabEnter f.x #comment
3747 assert_equal(2, g:entered)
3748
3749 assert_fails('doautocmd Syntax#comment', 'E216:')
3750
3751 au! TabEnter
3752 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003753
3754 CheckScriptSuccess([
3755 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02003756 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003757 'b:var = 456',
3758 'w:var = 777',
3759 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003760 'unlet g:var w:var # something',
3761 ])
3762
3763 CheckScriptFailure([
3764 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003765 'let var = 123',
3766 ], 'E1126: Cannot use :let in Vim9 script')
3767
3768 CheckScriptFailure([
3769 'vim9script',
3770 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003771 ], 'E1016: Cannot declare a global variable:')
3772
3773 CheckScriptFailure([
3774 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003775 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003776 ], 'E1016: Cannot declare a buffer variable:')
3777
3778 CheckScriptFailure([
3779 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003780 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003781 ], 'E1016: Cannot declare a window variable:')
3782
3783 CheckScriptFailure([
3784 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003785 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003786 ], 'E1016: Cannot declare a tab variable:')
3787
3788 CheckScriptFailure([
3789 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003790 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003791 ], 'E1016: Cannot declare a v: variable:')
3792
3793 CheckScriptFailure([
3794 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003795 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003796 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02003797
3798 CheckScriptFailure([
3799 'vim9script',
3800 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003801 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003802 ], 'E108:')
3803
3804 CheckScriptFailure([
3805 'let g:var = 123',
3806 'unlet g:var # something',
3807 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003808
3809 CheckScriptSuccess([
3810 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003811 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003812 ' echo "yes"',
3813 'elseif 2 #comment',
3814 ' echo "no"',
3815 'endif',
3816 ])
3817
3818 CheckScriptFailure([
3819 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003820 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003821 ' echo "yes"',
3822 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003823 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003824
3825 CheckScriptFailure([
3826 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003827 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003828 ' echo "yes"',
3829 'elseif 2#comment',
3830 ' echo "no"',
3831 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003832 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003833
3834 CheckScriptSuccess([
3835 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003836 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003837 ])
3838
3839 CheckScriptFailure([
3840 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003841 'var v = 1# comment6',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003842 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003843
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003844 CheckScriptSuccess([
3845 'vim9script',
3846 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003847 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003848 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003849 'dsearch /pat/ #comment',
3850 'bwipe!',
3851 ])
3852
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003853 CheckScriptFailure([
3854 'vim9script',
3855 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003856 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003857 ':$',
3858 'dsearch /pat/#comment',
3859 'bwipe!',
3860 ], 'E488:')
3861
3862 CheckScriptFailure([
3863 'vim9script',
3864 'func! SomeFunc()',
3865 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02003866enddef
3867
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003868def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003869 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003870 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02003871 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003872 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02003873 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003874 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02003875 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003876 END
3877 writefile(lines, 'Xfinished')
3878 source Xfinished
3879 assert_equal('two', g:res)
3880
3881 unlet g:res
3882 delete('Xfinished')
3883enddef
3884
Bram Moolenaara5d00772020-05-14 23:20:55 +02003885def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003886 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02003887 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02003888 def GetValue(): string
3889 return theVal
3890 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003891 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02003892 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02003893 theVal = 'else'
3894 g:laterVal = GetValue()
3895 END
3896 writefile(lines, 'Xforward')
3897 source Xforward
3898 assert_equal('something', g:initVal)
3899 assert_equal('else', g:laterVal)
3900
3901 unlet g:initVal
3902 unlet g:laterVal
3903 delete('Xforward')
3904enddef
3905
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003906def Test_source_vim9_from_legacy()
Bram Moolenaara6294952020-12-27 13:39:50 +01003907 var vim9_lines =<< trim END
3908 vim9script
3909 var local = 'local'
3910 g:global = 'global'
3911 export var exported = 'exported'
3912 export def GetText(): string
3913 return 'text'
3914 enddef
3915 END
3916 writefile(vim9_lines, 'Xvim9_script.vim')
3917
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003918 var legacy_lines =<< trim END
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003919 source Xvim9_script.vim
3920
3921 call assert_false(exists('local'))
3922 call assert_false(exists('exported'))
3923 call assert_false(exists('s:exported'))
3924 call assert_equal('global', global)
3925 call assert_equal('global', g:global)
3926
3927 " imported variable becomes script-local
3928 import exported from './Xvim9_script.vim'
3929 call assert_equal('exported', s:exported)
3930 call assert_false(exists('exported'))
3931
3932 " imported function becomes script-local
3933 import GetText from './Xvim9_script.vim'
3934 call assert_equal('text', s:GetText())
3935 call assert_false(exists('*GetText'))
3936 END
3937 writefile(legacy_lines, 'Xlegacy_script.vim')
3938
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003939 source Xlegacy_script.vim
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003940 assert_equal('global', g:global)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003941 unlet g:global
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003942
3943 delete('Xlegacy_script.vim')
3944 delete('Xvim9_script.vim')
3945enddef
Bram Moolenaara5d00772020-05-14 23:20:55 +02003946
Bram Moolenaare535db82021-03-31 21:07:24 +02003947def Test_declare_script_in_func()
3948 var lines =<< trim END
3949 vim9script
3950 func Declare()
3951 let s:local = 123
3952 endfunc
3953 Declare()
3954 assert_equal(123, local)
3955
3956 var error: string
3957 try
3958 local = 'asdf'
3959 catch
3960 error = v:exception
3961 endtry
3962 assert_match('E1012: Type mismatch; expected number but got string', error)
3963
3964 lockvar local
3965 try
3966 local = 999
3967 catch
3968 error = v:exception
3969 endtry
3970 assert_match('E741: Value is locked: local', error)
3971 END
3972 CheckScriptSuccess(lines)
3973enddef
3974
3975
Bram Moolenaar7d699702020-08-14 20:52:28 +02003976func Test_vim9script_not_global()
3977 " check that items defined in Vim9 script are script-local, not global
3978 let vim9lines =<< trim END
3979 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003980 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02003981 func TheFunc()
3982 echo 'local'
3983 endfunc
3984 def DefFunc()
3985 echo 'local'
3986 enddef
3987 END
3988 call writefile(vim9lines, 'Xvim9script.vim')
3989 source Xvim9script.vim
3990 try
3991 echo g:var
3992 assert_report('did not fail')
3993 catch /E121:/
3994 " caught
3995 endtry
3996 try
3997 call TheFunc()
3998 assert_report('did not fail')
3999 catch /E117:/
4000 " caught
4001 endtry
4002 try
4003 call DefFunc()
4004 assert_report('did not fail')
4005 catch /E117:/
4006 " caught
4007 endtry
4008
Bram Moolenaar25859dd2020-08-30 12:54:53 +02004009 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02004010endfunc
4011
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02004012def Test_vim9_copen()
4013 # this was giving an error for setting w:quickfix_title
4014 copen
4015 quit
4016enddef
4017
Bram Moolenaar03290b82020-12-19 16:30:44 +01004018" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004019def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004020 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004021 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01004022 def some#gettest(): string
4023 return 'test'
4024 enddef
4025 g:some#name = 'name'
Bram Moolenaar227c58a2021-04-28 20:40:44 +02004026 g:some#dict = {key: 'value'}
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01004027
4028 def some#varargs(a1: string, ...l: list<string>): string
4029 return a1 .. l[0] .. l[1]
4030 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01004031 END
4032
4033 mkdir('Xdir/autoload', 'p')
4034 writefile(lines, 'Xdir/autoload/some.vim')
4035 var save_rtp = &rtp
4036 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4037
4038 assert_equal('test', g:some#gettest())
4039 assert_equal('name', g:some#name)
Bram Moolenaar227c58a2021-04-28 20:40:44 +02004040 assert_equal('value', g:some#dict.key)
Bram Moolenaar03290b82020-12-19 16:30:44 +01004041 g:some#other = 'other'
4042 assert_equal('other', g:some#other)
4043
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01004044 assert_equal('abc', some#varargs('a', 'b', 'c'))
4045
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004046 # upper case script name works
4047 lines =<< trim END
4048 vim9script
4049 def Other#getOther(): string
4050 return 'other'
4051 enddef
4052 END
4053 writefile(lines, 'Xdir/autoload/Other.vim')
4054 assert_equal('other', g:Other#getOther())
4055
Bram Moolenaar03290b82020-12-19 16:30:44 +01004056 delete('Xdir', 'rf')
4057 &rtp = save_rtp
4058enddef
4059
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00004060" test disassembling an auto-loaded function starting with "debug"
4061def Test_vim9_autoload_disass()
4062 mkdir('Xdir/autoload', 'p')
4063 var save_rtp = &rtp
4064 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4065
4066 var lines =<< trim END
4067 vim9script
4068 def debugit#test(): string
4069 return 'debug'
4070 enddef
4071 END
4072 writefile(lines, 'Xdir/autoload/debugit.vim')
4073
4074 lines =<< trim END
4075 vim9script
4076 def profileit#test(): string
4077 return 'profile'
4078 enddef
4079 END
4080 writefile(lines, 'Xdir/autoload/profileit.vim')
4081
4082 lines =<< trim END
4083 vim9script
4084 assert_equal('debug', debugit#test())
4085 disass debugit#test
4086 assert_equal('profile', profileit#test())
4087 disass profileit#test
4088 END
4089 CheckScriptSuccess(lines)
4090
4091 delete('Xdir', 'rf')
4092 &rtp = save_rtp
4093enddef
4094
Bram Moolenaar03290b82020-12-19 16:30:44 +01004095" test using a vim9script that is auto-loaded from an autocmd
4096def Test_vim9_aucmd_autoload()
4097 var lines =<< trim END
4098 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004099 def foo#test()
4100 echomsg getreg('"')
4101 enddef
4102 END
4103
4104 mkdir('Xdir/autoload', 'p')
4105 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004106 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004107 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4108 augroup test
4109 autocmd TextYankPost * call foo#test()
4110 augroup END
4111
4112 normal Y
4113
4114 augroup test
4115 autocmd!
4116 augroup END
4117 delete('Xdir', 'rf')
4118 &rtp = save_rtp
4119enddef
4120
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004121" This was causing a crash because suppress_errthrow wasn't reset.
4122def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004123 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004124 vim9script
4125 def crash#func()
4126 try
4127 for x in List()
4128 endfor
4129 catch
4130 endtry
4131 g:ok = true
4132 enddef
4133 fu List()
4134 invalid
4135 endfu
4136 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01004137 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004138 catch /wontmatch/
4139 endtry
4140 END
4141 call mkdir('Xruntime/autoload', 'p')
4142 call writefile(lines, 'Xruntime/autoload/crash.vim')
4143
4144 # run in a separate Vim to avoid the side effects of assert_fails()
4145 lines =<< trim END
4146 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
4147 call crash#func()
4148 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004149 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004150 END
4151 writefile(lines, 'Xscript')
4152 RunVim([], [], '-S Xscript')
4153 assert_equal(['ok'], readfile('Xdidit'))
4154
4155 delete('Xdidit')
4156 delete('Xscript')
4157 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01004158
4159 lines =<< trim END
4160 vim9script
4161 var foo#bar = 'asdf'
4162 END
4163 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004164enddef
4165
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02004166def Test_script_var_in_autocmd()
4167 # using a script variable from an autocommand, defined in a :def function in a
4168 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004169 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02004170 let s:counter = 1
4171 def s:Func()
4172 au! CursorHold
4173 au CursorHold * s:counter += 1
4174 enddef
4175 call s:Func()
4176 doau CursorHold
4177 call assert_equal(2, s:counter)
4178 au! CursorHold
4179 END
4180 CheckScriptSuccess(lines)
4181enddef
4182
Bram Moolenaarb5841b92021-07-15 18:09:53 +02004183def Test_error_in_autoload_script()
4184 var save_rtp = &rtp
4185 var dir = getcwd() .. '/Xruntime'
4186 &rtp = dir
4187 mkdir(dir .. '/autoload', 'p')
4188
4189 var lines =<< trim END
4190 vim9script noclear
4191 def script#autoloaded()
4192 enddef
4193 def Broken()
4194 var x: any = ''
4195 eval x != 0
4196 enddef
4197 Broken()
4198 END
4199 writefile(lines, dir .. '/autoload/script.vim')
4200
4201 lines =<< trim END
4202 vim9script
4203 def CallAutoloaded()
4204 script#autoloaded()
4205 enddef
4206
4207 function Legacy()
4208 try
4209 call s:CallAutoloaded()
4210 catch
4211 call assert_match('E1030: Using a String as a Number', v:exception)
4212 endtry
4213 endfunction
4214
4215 Legacy()
4216 END
4217 CheckScriptSuccess(lines)
4218
4219 &rtp = save_rtp
4220 delete(dir, 'rf')
4221enddef
4222
Bram Moolenaar3896a102020-08-09 14:33:55 +02004223def Test_cmdline_win()
4224 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
4225 # the command line window.
4226 mkdir('rtp/syntax', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004227 var export_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02004228 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004229 export var That = 'yes'
Bram Moolenaar3896a102020-08-09 14:33:55 +02004230 END
4231 writefile(export_lines, 'rtp/syntax/Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004232 var import_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02004233 vim9script
4234 import That from './Xexport.vim'
4235 END
4236 writefile(import_lines, 'rtp/syntax/vim.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004237 var save_rtp = &rtp
Bram Moolenaar3896a102020-08-09 14:33:55 +02004238 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
4239 syntax on
4240 augroup CmdWin
4241 autocmd CmdwinEnter * g:got_there = 'yes'
4242 augroup END
4243 # this will open and also close the cmdline window
4244 feedkeys('q:', 'xt')
4245 assert_equal('yes', g:got_there)
4246
4247 augroup CmdWin
4248 au!
4249 augroup END
4250 &rtp = save_rtp
4251 delete('rtp', 'rf')
4252enddef
4253
Bram Moolenaare3d46852020-08-29 13:39:17 +02004254def Test_invalid_sid()
4255 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02004256
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004257 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02004258 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02004259 endif
4260 delete('Xdidit')
4261enddef
4262
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004263def Test_restoring_cpo()
4264 writefile(['vim9script', 'set nocp'], 'Xsourced')
4265 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
4266 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
4267 assert_equal(['done'], readfile('Xdone'))
4268 endif
4269 delete('Xsourced')
4270 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01004271 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01004272
4273 writefile(['vim9script'], 'XanotherScript')
4274 set cpo=aABceFsMny>
4275 edit XanotherScript
4276 so %
4277 assert_equal('aABceFsMny>', &cpo)
4278 :1del
4279 w
4280 so %
4281 assert_equal('aABceFsMny>', &cpo)
4282
4283 delete('XanotherScript')
4284 set cpo&vim
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004285enddef
4286
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004287" Use :function so we can use Check commands
4288func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004289 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004290 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004291
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004292 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004293 vim9script
4294 def script#func()
4295 enddef
4296 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004297 call mkdir('Xdir/autoload', 'p')
4298 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004299
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004300 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004301 vim9script
4302 set cpo+=M
4303 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02004304 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004305 setline(1, 'some text')
4306 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004307 call writefile(lines, 'XTest_redraw_cpo')
4308 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
4309 call term_sendkeys(buf, "V:")
4310 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004311
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004312 " clean up
4313 call term_sendkeys(buf, "\<Esc>u")
4314 call StopVimInTerminal(buf)
4315 call delete('XTest_redraw_cpo')
4316 call delete('Xdir', 'rf')
4317endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004318
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004319
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02004320def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004321 var lines =<< trim END
4322 var name: any
4323 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02004324 END
4325 CheckDefAndScriptSuccess(lines)
4326enddef
4327
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02004328func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02004329 CheckRunVimInTerminal
4330
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02004331 " call indirectly to avoid compilation error for missing functions
4332 call Run_Test_define_func_at_command_line()
4333endfunc
4334
4335def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004336 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004337 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004338 func CheckAndQuit()
4339 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
4340 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
4341 endfunc
4342 END
4343 writefile([''], 'Xdidcmd')
4344 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01004345 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004346 # define Afunc() on the command line
4347 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
4348 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004349 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004350
4351 call StopVimInTerminal(buf)
4352 delete('XcallFunc')
4353 delete('Xdidcmd')
4354enddef
4355
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004356def Test_script_var_scope()
4357 var lines =<< trim END
4358 vim9script
4359 if true
4360 if true
4361 var one = 'one'
4362 echo one
4363 endif
4364 echo one
4365 endif
4366 END
4367 CheckScriptFailure(lines, 'E121:', 7)
4368
4369 lines =<< trim END
4370 vim9script
4371 if true
4372 if false
4373 var one = 'one'
4374 echo one
4375 else
4376 var one = 'one'
4377 echo one
4378 endif
4379 echo one
4380 endif
4381 END
4382 CheckScriptFailure(lines, 'E121:', 10)
4383
4384 lines =<< trim END
4385 vim9script
4386 while true
4387 var one = 'one'
4388 echo one
4389 break
4390 endwhile
4391 echo one
4392 END
4393 CheckScriptFailure(lines, 'E121:', 7)
4394
4395 lines =<< trim END
4396 vim9script
4397 for i in range(1)
4398 var one = 'one'
4399 echo one
4400 endfor
4401 echo one
4402 END
4403 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02004404
4405 lines =<< trim END
4406 vim9script
4407 {
4408 var one = 'one'
4409 assert_equal('one', one)
4410 }
4411 assert_false(exists('one'))
4412 assert_false(exists('s:one'))
4413 END
4414 CheckScriptSuccess(lines)
4415
4416 lines =<< trim END
4417 vim9script
4418 {
4419 var one = 'one'
4420 echo one
4421 }
4422 echo one
4423 END
4424 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004425enddef
4426
Bram Moolenaar352134b2020-10-17 22:04:08 +02004427def Test_catch_exception_in_callback()
4428 var lines =<< trim END
4429 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02004430 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02004431 try
4432 var x: string
4433 var y: string
4434 # this error should be caught with CHECKLEN
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00004435 var sl = ['']
4436 [x, y] = sl
Bram Moolenaar352134b2020-10-17 22:04:08 +02004437 catch
4438 g:caught = 'yes'
4439 endtry
4440 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004441 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02004442 feedkeys("\r", 'xt')
4443 END
4444 CheckScriptSuccess(lines)
4445
4446 unlet g:caught
4447enddef
4448
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004449def Test_no_unknown_error_after_error()
4450 if !has('unix') || !has('job')
4451 throw 'Skipped: not unix of missing +job feature'
4452 endif
Bram Moolenaarb16ff292021-09-26 20:14:39 +01004453 # FIXME: this check should not be needed
4454 if has('win32')
4455 throw 'Skipped: does not work on MS-Windows'
4456 endif
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004457 var lines =<< trim END
4458 vim9script
4459 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02004460 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004461 eval [][0]
4462 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02004463 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004464 sleep 1m
4465 source += l
4466 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004467 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01004468 while job_status(myjob) == 'run'
4469 sleep 10m
4470 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01004471 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02004472 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004473 END
4474 writefile(lines, 'Xdef')
4475 assert_fails('so Xdef', ['E684:', 'E1012:'])
4476 delete('Xdef')
4477enddef
4478
Bram Moolenaar4324d872020-12-01 20:12:24 +01004479def InvokeNormal()
4480 exe "norm! :m+1\r"
4481enddef
4482
4483def Test_invoke_normal_in_visual_mode()
4484 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
4485 new
4486 setline(1, ['aaa', 'bbb'])
4487 feedkeys("V\<F3>", 'xt')
4488 assert_equal(['bbb', 'aaa'], getline(1, 2))
4489 xunmap <F3>
4490enddef
4491
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004492def Test_white_space_after_command()
4493 var lines =<< trim END
4494 exit_cb: Func})
4495 END
4496 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01004497
4498 lines =<< trim END
4499 e#
4500 END
4501 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004502enddef
4503
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004504def Test_script_var_gone_when_sourced_twice()
4505 var lines =<< trim END
4506 vim9script
4507 if exists('g:guard')
4508 finish
4509 endif
4510 g:guard = 1
4511 var name = 'thename'
4512 def g:GetName(): string
4513 return name
4514 enddef
4515 def g:SetName(arg: string)
4516 name = arg
4517 enddef
4518 END
4519 writefile(lines, 'XscriptTwice.vim')
4520 so XscriptTwice.vim
4521 assert_equal('thename', g:GetName())
4522 g:SetName('newname')
4523 assert_equal('newname', g:GetName())
4524 so XscriptTwice.vim
4525 assert_fails('call g:GetName()', 'E1149:')
4526 assert_fails('call g:SetName("x")', 'E1149:')
4527
4528 delfunc g:GetName
4529 delfunc g:SetName
4530 delete('XscriptTwice.vim')
4531 unlet g:guard
4532enddef
4533
4534def Test_import_gone_when_sourced_twice()
4535 var exportlines =<< trim END
4536 vim9script
4537 if exists('g:guard')
4538 finish
4539 endif
4540 g:guard = 1
4541 export var name = 'someName'
4542 END
4543 writefile(exportlines, 'XexportScript.vim')
4544
4545 var lines =<< trim END
4546 vim9script
4547 import name from './XexportScript.vim'
4548 def g:GetName(): string
4549 return name
4550 enddef
4551 END
4552 writefile(lines, 'XscriptImport.vim')
4553 so XscriptImport.vim
4554 assert_equal('someName', g:GetName())
4555
4556 so XexportScript.vim
4557 assert_fails('call g:GetName()', 'E1149:')
4558
4559 delfunc g:GetName
4560 delete('XexportScript.vim')
4561 delete('XscriptImport.vim')
4562 unlet g:guard
4563enddef
4564
Bram Moolenaar10b94212021-02-19 21:42:57 +01004565def Test_unsupported_commands()
4566 var lines =<< trim END
4567 ka
4568 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004569 CheckDefFailure(lines, 'E476:')
4570 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaar10b94212021-02-19 21:42:57 +01004571
4572 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01004573 :1ka
4574 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004575 CheckDefFailure(lines, 'E476:')
4576 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaarada1d872021-02-20 08:16:51 +01004577
4578 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01004579 t
4580 END
4581 CheckDefFailure(lines, 'E1100:')
4582 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4583
4584 lines =<< trim END
4585 x
4586 END
4587 CheckDefFailure(lines, 'E1100:')
4588 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4589
4590 lines =<< trim END
4591 xit
4592 END
4593 CheckDefFailure(lines, 'E1100:')
4594 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4595enddef
4596
Bram Moolenaarc70fe462021-04-17 17:59:19 +02004597def Test_mapping_line_number()
4598 var lines =<< trim END
4599 vim9script
4600 def g:FuncA()
4601 # Some comment
4602 FuncB(0)
4603 enddef
4604 # Some comment
4605 def FuncB(
4606 # Some comment
4607 n: number
4608 )
4609 exe 'nno '
4610 # Some comment
4611 .. '<F3> a'
4612 .. 'b'
4613 .. 'c'
4614 enddef
4615 END
4616 CheckScriptSuccess(lines)
4617 var res = execute('verbose nmap <F3>')
4618 assert_match('No mapping found', res)
4619
4620 g:FuncA()
4621 res = execute('verbose nmap <F3>')
4622 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
4623
4624 nunmap <F3>
4625 delfunc g:FuncA
4626enddef
4627
Bram Moolenaardeb108b2021-07-08 17:35:36 +02004628def Test_option_set()
4629 # legacy script allows for white space
4630 var lines =<< trim END
4631 set foldlevel =11
4632 call assert_equal(11, &foldlevel)
4633 END
4634 CheckScriptSuccess(lines)
4635
4636 set foldlevel
4637 set foldlevel=12
4638 assert_equal(12, &foldlevel)
4639 set foldlevel+=2
4640 assert_equal(14, &foldlevel)
4641 set foldlevel-=3
4642 assert_equal(11, &foldlevel)
4643
4644 lines =<< trim END
4645 set foldlevel =1
4646 END
4647 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: =1')
4648
4649 lines =<< trim END
4650 set foldlevel +=1
4651 END
4652 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: +=1')
4653
4654 lines =<< trim END
4655 set foldlevel ^=1
4656 END
4657 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: ^=1')
4658
4659 lines =<< trim END
4660 set foldlevel -=1
4661 END
4662 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: -=1')
4663
4664 set foldlevel&
4665enddef
4666
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004667def Test_option_modifier()
Bram Moolenaar1594f312021-07-08 16:40:13 +02004668 # legacy script allows for white space
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004669 var lines =<< trim END
4670 set hlsearch & hlsearch !
4671 call assert_equal(1, &hlsearch)
4672 END
4673 CheckScriptSuccess(lines)
4674
Bram Moolenaar1594f312021-07-08 16:40:13 +02004675 set hlsearch
4676 set hlsearch!
4677 assert_equal(false, &hlsearch)
4678
4679 set hlsearch
4680 set hlsearch&
4681 assert_equal(false, &hlsearch)
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004682
4683 lines =<< trim END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004684 set hlsearch &
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004685 END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004686 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: &')
4687
4688 lines =<< trim END
4689 set hlsearch !
4690 END
4691 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: !')
4692
4693 set hlsearch&
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004694enddef
4695
Bram Moolenaarc03fe662021-07-11 16:52:45 +02004696" This must be called last, it may cause following :def functions to fail
4697def Test_xxx_echoerr_line_number()
4698 var lines =<< trim END
4699 echoerr 'some'
4700 .. ' error'
4701 .. ' continued'
4702 END
4703 CheckDefExecAndScriptFailure(lines, 'some error continued', 1)
4704enddef
4705
Bram Moolenaar9537e372021-12-10 21:05:53 +00004706func Test_debug_with_lambda()
Bram Moolenaar9fffef92021-12-10 16:55:58 +00004707 CheckRunVimInTerminal
4708
Bram Moolenaar9537e372021-12-10 21:05:53 +00004709 " call indirectly to avoid compilation error for missing functions
4710 call Run_Test_debug_with_lambda()
4711endfunc
4712
4713def Run_Test_debug_with_lambda()
Bram Moolenaar9fffef92021-12-10 16:55:58 +00004714 var lines =<< trim END
4715 vim9script
4716 def Func()
4717 var n = 0
4718 echo [0]->filter((_, v) => v == n)
4719 enddef
4720 breakadd func Func
4721 Func()
4722 END
4723 writefile(lines, 'XdebugFunc')
4724 var buf = RunVimInTerminal('-S XdebugFunc', {rows: 6, wait_for_ruler: 0})
4725 WaitForAssert(() => assert_match('^>', term_getline(buf, 6)))
4726
4727 term_sendkeys(buf, "cont\<CR>")
4728 WaitForAssert(() => assert_match('\[0\]', term_getline(buf, 5)))
4729
4730 StopVimInTerminal(buf)
4731 delete('XdebugFunc')
4732enddef
4733
Bram Moolenaar648594e2021-07-11 17:55:01 +02004734def ProfiledWithLambda()
Bram Moolenaard9162552021-07-11 15:26:13 +02004735 var n = 3
4736 echo [[1, 2], [3, 4]]->filter((_, l) => l[0] == n)
4737enddef
4738
Bram Moolenaar648594e2021-07-11 17:55:01 +02004739def ProfiledNested()
4740 var x = 0
4741 def Nested(): any
4742 return x
4743 enddef
4744 Nested()
4745enddef
4746
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004747def ProfiledNestedProfiled()
4748 var x = 0
4749 def Nested(): any
4750 return x
4751 enddef
4752 Nested()
4753enddef
4754
Dominique Pelle923dce22021-11-21 11:36:04 +00004755" Execute this near the end, profiling doesn't stop until Vim exits.
Bram Moolenaard9162552021-07-11 15:26:13 +02004756" This only tests that it works, not the profiling output.
4757def Test_xx_profile_with_lambda()
Bram Moolenaar4ece1522021-07-11 16:31:51 +02004758 CheckFeature profile
4759
Bram Moolenaard9162552021-07-11 15:26:13 +02004760 profile start Xprofile.log
Bram Moolenaar648594e2021-07-11 17:55:01 +02004761 profile func ProfiledWithLambda
4762 ProfiledWithLambda()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004763
Bram Moolenaar648594e2021-07-11 17:55:01 +02004764 profile func ProfiledNested
4765 ProfiledNested()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004766
4767 # Also profile the nested function. Use a different function, although the
4768 # contents is the same, to make sure it was not already compiled.
4769 profile func *
4770 ProfiledNestedProfiled()
4771
4772 profdel func *
4773 profile pause
Bram Moolenaard9162552021-07-11 15:26:13 +02004774enddef
4775
Bram Moolenaar585fea72020-04-02 22:33:21 +02004776" Keep this last, it messes up highlighting.
4777def Test_substitute_cmd()
4778 new
4779 setline(1, 'something')
4780 :substitute(some(other(
4781 assert_equal('otherthing', getline(1))
4782 bwipe!
4783
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004784 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004785 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02004786 vim9script
4787 new
4788 setline(1, 'something')
4789 :substitute(some(other(
4790 assert_equal('otherthing', getline(1))
4791 bwipe!
4792 END
4793 writefile(lines, 'Xvim9lines')
4794 source Xvim9lines
4795
4796 delete('Xvim9lines')
4797enddef
4798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker