blob: 4b05e9cf1023a38a79b22ff9a5969129eb8525ca [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001" Test various aspects of the Vim9 script language.
2
Bram Moolenaar673660a2020-01-26 16:50:05 +01003source check.vim
Bram Moolenaar101f4812020-06-16 23:18:51 +02004source term_util.vim
Bram Moolenaarad39c092020-02-26 18:23:43 +01005source view_util.vim
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006source vim9.vim
Bram Moolenaare3d46852020-08-29 13:39:17 +02007source shared.vim
Bram Moolenaar37294bd2021-03-10 13:40:08 +01008source screendump.vim
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010def Test_range_only()
11 new
12 setline(1, ['blah', 'Blah'])
13 :/Blah/
14 assert_equal(2, getcurpos()[1])
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020015 bwipe!
16
17 # without range commands use current line
18 new
19 setline(1, ['one', 'two', 'three'])
20 :2
21 print
22 assert_equal('two', Screenline(&lines))
23 :3
24 list
25 assert_equal('three$', Screenline(&lines))
Bram Moolenaarb8554302021-02-15 21:30:30 +010026
27 # missing command does not print the line
28 var lines =<< trim END
29 vim9script
30 :1|
31 assert_equal('three$', Screenline(&lines))
32 :|
33 assert_equal('three$', Screenline(&lines))
34 END
35 CheckScriptSuccess(lines)
36
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020037 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +010038
39 # won't generate anything
40 if false
41 :123
42 endif
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020043enddef
44
Bram Moolenaara6e67e42020-05-15 23:36:40 +020045let g:alist = [7]
46let g:astring = 'text'
Bram Moolenaarf0b9f432020-07-17 23:03:17 +020047let g:anumber = 123
Bram Moolenaar6e587dc2020-02-06 13:15:52 +010048
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020049def Test_delfunction()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020050 # Check function is defined in script namespace
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020051 CheckScriptSuccess([
52 'vim9script',
53 'func CheckMe()',
54 ' return 123',
55 'endfunc',
56 'assert_equal(123, s:CheckMe())',
57 ])
58
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020059 # Check function in script namespace cannot be deleted
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020060 CheckScriptFailure([
61 'vim9script',
62 'func DeleteMe1()',
63 'endfunc',
64 'delfunction DeleteMe1',
65 ], 'E1084:')
66 CheckScriptFailure([
67 'vim9script',
68 'func DeleteMe2()',
69 'endfunc',
70 'def DoThat()',
71 ' delfunction DeleteMe2',
72 'enddef',
73 'DoThat()',
74 ], 'E1084:')
75 CheckScriptFailure([
76 'vim9script',
77 'def DeleteMe3()',
78 'enddef',
79 'delfunction DeleteMe3',
80 ], 'E1084:')
81 CheckScriptFailure([
82 'vim9script',
83 'def DeleteMe4()',
84 'enddef',
85 'def DoThat()',
86 ' delfunction DeleteMe4',
87 'enddef',
88 'DoThat()',
89 ], 'E1084:')
Bram Moolenaar925e9fd2020-07-25 15:41:11 +020090
91 # Check that global :def function can be replaced and deleted
Bram Moolenaarcfcd0112020-09-27 15:19:27 +020092 var lines =<< trim END
Bram Moolenaar925e9fd2020-07-25 15:41:11 +020093 vim9script
94 def g:Global(): string
95 return "yes"
96 enddef
97 assert_equal("yes", g:Global())
98 def! g:Global(): string
99 return "no"
100 enddef
101 assert_equal("no", g:Global())
102 delfunc g:Global
103 assert_false(exists('*g:Global'))
104 END
105 CheckScriptSuccess(lines)
106
107 # Check that global function can be replaced by a :def function and deleted
108 lines =<< trim END
109 vim9script
110 func g:Global()
111 return "yes"
112 endfunc
113 assert_equal("yes", g:Global())
114 def! g:Global(): string
115 return "no"
116 enddef
117 assert_equal("no", g:Global())
118 delfunc g:Global
119 assert_false(exists('*g:Global'))
120 END
121 CheckScriptSuccess(lines)
122
123 # Check that global :def function can be replaced by a function and deleted
124 lines =<< trim END
125 vim9script
126 def g:Global(): string
127 return "yes"
128 enddef
129 assert_equal("yes", g:Global())
130 func! g:Global()
131 return "no"
132 endfunc
133 assert_equal("no", g:Global())
134 delfunc g:Global
135 assert_false(exists('*g:Global'))
136 END
137 CheckScriptSuccess(lines)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200138enddef
139
Bram Moolenaar08052222020-09-14 17:04:31 +0200140def Test_wrong_type()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200141 CheckDefFailure(['var name: list<nothing>'], 'E1010:')
142 CheckDefFailure(['var name: list<list<nothing>>'], 'E1010:')
143 CheckDefFailure(['var name: dict<nothing>'], 'E1010:')
144 CheckDefFailure(['var name: dict<dict<nothing>>'], 'E1010:')
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100145
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200146 CheckDefFailure(['var name: dict<number'], 'E1009:')
147 CheckDefFailure(['var name: dict<list<number>'], 'E1009:')
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100148
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200149 CheckDefFailure(['var name: ally'], 'E1010:')
150 CheckDefFailure(['var name: bram'], 'E1010:')
151 CheckDefFailure(['var name: cathy'], 'E1010:')
152 CheckDefFailure(['var name: dom'], 'E1010:')
153 CheckDefFailure(['var name: freddy'], 'E1010:')
154 CheckDefFailure(['var name: john'], 'E1010:')
155 CheckDefFailure(['var name: larry'], 'E1010:')
156 CheckDefFailure(['var name: ned'], 'E1010:')
157 CheckDefFailure(['var name: pam'], 'E1010:')
158 CheckDefFailure(['var name: sam'], 'E1010:')
159 CheckDefFailure(['var name: vim'], 'E1010:')
Bram Moolenaara0a9f432020-04-28 21:29:34 +0200160
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200161 CheckDefFailure(['var Ref: number', 'Ref()'], 'E1085:')
162 CheckDefFailure(['var Ref: string', 'var res = Ref()'], 'E1085:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200163enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaar10c65862020-10-08 21:16:42 +0200165def Test_script_wrong_type()
166 var lines =<< trim END
167 vim9script
168 var s:dict: dict<string>
169 s:dict['a'] = ['x']
170 END
171 CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got list<string>', 3)
172enddef
173
Bram Moolenaar08052222020-09-14 17:04:31 +0200174def Test_const()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200175 CheckDefFailure(['final name = 234', 'name = 99'], 'E1018:')
176 CheckDefFailure(['final one = 234', 'var one = 99'], 'E1017:')
177 CheckDefFailure(['final list = [1, 2]', 'var list = [3, 4]'], 'E1017:')
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200178 CheckDefFailure(['final two'], 'E1125:')
179 CheckDefFailure(['final &option'], 'E996:')
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200180
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200181 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200182 final list = [1, 2, 3]
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200183 list[0] = 4
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200184 list->assert_equal([4, 2, 3])
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200185 const other = [5, 6, 7]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200186 other->assert_equal([5, 6, 7])
Bram Moolenaar71abe482020-09-14 22:28:30 +0200187
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200188 var varlist = [7, 8]
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200189 const constlist = [1, varlist, 3]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200190 varlist[0] = 77
Bram Moolenaar23e2e112021-08-03 21:16:18 +0200191 constlist[1][1] = 88
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200192 var cl = constlist[1]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200193 cl[1] = 88
194 constlist->assert_equal([1, [77, 88], 3])
195
Bram Moolenaare0de1712020-12-02 17:36:54 +0100196 var vardict = {five: 5, six: 6}
197 const constdict = {one: 1, two: vardict, three: 3}
Bram Moolenaar71abe482020-09-14 22:28:30 +0200198 vardict['five'] = 55
Bram Moolenaar23e2e112021-08-03 21:16:18 +0200199 constdict['two']['six'] = 66
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200200 var cd = constdict['two']
Bram Moolenaar71abe482020-09-14 22:28:30 +0200201 cd['six'] = 66
Bram Moolenaare0de1712020-12-02 17:36:54 +0100202 constdict->assert_equal({one: 1, two: {five: 55, six: 66}, three: 3})
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200203 END
204 CheckDefAndScriptSuccess(lines)
Bram Moolenaar08052222020-09-14 17:04:31 +0200205enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200207def Test_const_bang()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200208 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200209 const var = 234
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200210 var = 99
211 END
212 CheckDefExecFailure(lines, 'E1018:', 2)
213 CheckScriptFailure(['vim9script'] + lines, 'E46:', 3)
214
215 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200216 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200217 ll[0] = 99
218 END
219 CheckDefExecFailure(lines, 'E1119:', 2)
220 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
221
222 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200223 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200224 ll[3] = 99
225 END
226 CheckDefExecFailure(lines, 'E1118:', 2)
227 CheckScriptFailure(['vim9script'] + lines, 'E684:', 3)
228
229 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100230 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200231 dd["one"] = 99
232 END
233 CheckDefExecFailure(lines, 'E1121:', 2)
234 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
235
236 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100237 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200238 dd["three"] = 99
239 END
240 CheckDefExecFailure(lines, 'E1120:')
241 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
242enddef
243
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200244def Test_range_no_colon()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200245 CheckDefFailure(['%s/a/b/'], 'E1050:')
246 CheckDefFailure(['+ s/a/b/'], 'E1050:')
247 CheckDefFailure(['- s/a/b/'], 'E1050:')
248 CheckDefFailure(['. s/a/b/'], 'E1050:')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200249enddef
250
251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252def Test_block()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200253 var outer = 1
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254 {
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200255 var inner = 2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 assert_equal(1, outer)
257 assert_equal(2, inner)
258 }
259 assert_equal(1, outer)
Bram Moolenaar3f1e9f02021-02-27 22:36:43 +0100260
261 {|echo 'yes'|}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262enddef
263
Bram Moolenaar08052222020-09-14 17:04:31 +0200264def Test_block_failure()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200265 CheckDefFailure(['{', 'var inner = 1', '}', 'echo inner'], 'E1001:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200266 CheckDefFailure(['}'], 'E1025:')
267 CheckDefFailure(['{', 'echo 1'], 'E1026:')
268enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200270def Test_block_local_vars()
271 var lines =<< trim END
272 vim9script
Bram Moolenaared234f22020-10-15 20:42:20 +0200273 v:testing = 1
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200274 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200275 var text = ['hello']
276 def SayHello(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200277 return text
278 enddef
279 def SetText(v: string)
Bram Moolenaared234f22020-10-15 20:42:20 +0200280 text = [v]
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200281 enddef
282 endif
283
284 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200285 var text = ['again']
286 def SayAgain(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200287 return text
288 enddef
289 endif
Bram Moolenaared234f22020-10-15 20:42:20 +0200290
291 # test that the "text" variables are not cleaned up
292 test_garbagecollect_now()
293
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200294 defcompile
295
Bram Moolenaared234f22020-10-15 20:42:20 +0200296 assert_equal(['hello'], SayHello())
297 assert_equal(['again'], SayAgain())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200298
299 SetText('foobar')
Bram Moolenaared234f22020-10-15 20:42:20 +0200300 assert_equal(['foobar'], SayHello())
301
302 call writefile(['ok'], 'Xdidit')
303 qall!
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200304 END
Bram Moolenaared234f22020-10-15 20:42:20 +0200305
306 # need to execute this with a separate Vim instance to avoid the current
307 # context gets garbage collected.
308 writefile(lines, 'Xscript')
309 RunVim([], [], '-S Xscript')
310 assert_equal(['ok'], readfile('Xdidit'))
311
312 delete('Xscript')
313 delete('Xdidit')
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200314enddef
315
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200316def Test_block_local_vars_with_func()
317 var lines =<< trim END
318 vim9script
319 if true
320 var foo = 'foo'
321 if true
322 var bar = 'bar'
323 def Func(): list<string>
324 return [foo, bar]
325 enddef
326 endif
327 endif
328 # function is compiled here, after blocks have finished, can still access
329 # "foo" and "bar"
330 assert_equal(['foo', 'bar'], Func())
331 END
332 CheckScriptSuccess(lines)
333enddef
334
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200335" legacy func for command that's defined later
336func InvokeSomeCommand()
337 SomeCommand
338endfunc
339
340def Test_autocommand_block()
341 com SomeCommand {
342 g:someVar = 'some'
343 }
344 InvokeSomeCommand()
345 assert_equal('some', g:someVar)
346
347 delcommand SomeCommand
348 unlet g:someVar
349enddef
350
351def Test_command_block()
352 au BufNew *.xml {
353 g:otherVar = 'other'
354 }
355 split other.xml
356 assert_equal('other', g:otherVar)
357
358 bwipe!
359 au! BufNew *.xml
360 unlet g:otherVar
361enddef
362
Bram Moolenaard032f342020-07-18 18:13:02 +0200363func g:NoSuchFunc()
364 echo 'none'
365endfunc
366
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100367def Test_try_catch_throw()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200368 var l = []
Bram Moolenaar7a092242020-04-16 22:10:49 +0200369 try # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370 add(l, '1')
371 throw 'wrong'
372 add(l, '2')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200373 catch # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100374 add(l, v:exception)
Bram Moolenaar7a092242020-04-16 22:10:49 +0200375 finally # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100376 add(l, '3')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200377 endtry # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378 assert_equal(['1', 'wrong', '3'], l)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200379
Bram Moolenaare8593122020-07-18 15:17:02 +0200380 l = []
381 try
382 try
383 add(l, '1')
384 throw 'wrong'
385 add(l, '2')
386 catch /right/
387 add(l, v:exception)
388 endtry
389 catch /wrong/
390 add(l, 'caught')
Bram Moolenaar373863e2020-09-26 17:20:53 +0200391 fina
Bram Moolenaare8593122020-07-18 15:17:02 +0200392 add(l, 'finally')
393 endtry
394 assert_equal(['1', 'caught', 'finally'], l)
395
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200396 var n: number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200397 try
398 n = l[3]
399 catch /E684:/
400 n = 99
401 endtry
402 assert_equal(99, n)
403
Bram Moolenaar69f70502021-01-01 16:10:46 +0100404 var done = 'no'
405 if 0
406 try | catch | endtry
407 else
408 done = 'yes'
409 endif
410 assert_equal('yes', done)
411
412 done = 'no'
413 if 1
414 done = 'yes'
415 else
416 try | catch | endtry
417 done = 'never'
418 endif
419 assert_equal('yes', done)
420
421 if 1
422 else
423 try | catch /pat/ | endtry
424 try | catch /pat/
425 endtry
426 try
427 catch /pat/ | endtry
428 try
429 catch /pat/
430 endtry
431 endif
432
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200433 try
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200434 # string slice returns a string, not a number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200435 n = g:astring[3]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200436 catch /E1012:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200437 n = 77
438 endtry
439 assert_equal(77, n)
440
441 try
442 n = l[g:astring]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200443 catch /E1012:/
Bram Moolenaar56acb092020-08-16 14:48:19 +0200444 n = 88
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200445 endtry
Bram Moolenaar56acb092020-08-16 14:48:19 +0200446 assert_equal(88, n)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200447
448 try
449 n = s:does_not_exist
450 catch /E121:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200451 n = 111
452 endtry
453 assert_equal(111, n)
454
455 try
456 n = g:does_not_exist
457 catch /E121:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200458 n = 121
459 endtry
460 assert_equal(121, n)
461
Bram Moolenaare0de1712020-12-02 17:36:54 +0100462 var d = {one: 1}
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200463 try
464 n = d[g:astring]
465 catch /E716:/
466 n = 222
467 endtry
468 assert_equal(222, n)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200469
470 try
471 n = -g:astring
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +0200472 catch /E1012:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200473 n = 233
474 endtry
475 assert_equal(233, n)
476
477 try
478 n = +g:astring
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +0200479 catch /E1012:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200480 n = 244
481 endtry
482 assert_equal(244, n)
483
484 try
485 n = +g:alist
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +0200486 catch /E1012:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200487 n = 255
488 endtry
489 assert_equal(255, n)
490
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200491 var nd: dict<any>
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200492 try
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100493 nd = {[g:alist]: 1}
494 catch /E1105:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200495 n = 266
496 endtry
497 assert_equal(266, n)
498
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +0000499 l = [1, 2, 3]
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200500 try
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +0000501 [n] = l
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200502 catch /E1093:/
503 n = 277
504 endtry
505 assert_equal(277, n)
506
Bram Moolenaare8593122020-07-18 15:17:02 +0200507 try
508 &ts = g:astring
Bram Moolenaar5e654232020-09-16 15:22:00 +0200509 catch /E1012:/
Bram Moolenaare8593122020-07-18 15:17:02 +0200510 n = 288
511 endtry
512 assert_equal(288, n)
513
514 try
515 &backspace = 'asdf'
516 catch /E474:/
517 n = 299
518 endtry
519 assert_equal(299, n)
520
521 l = [1]
522 try
523 l[3] = 3
524 catch /E684:/
525 n = 300
526 endtry
527 assert_equal(300, n)
528
529 try
Bram Moolenaare8593122020-07-18 15:17:02 +0200530 unlet g:does_not_exist
531 catch /E108:/
532 n = 322
533 endtry
534 assert_equal(322, n)
535
536 try
Bram Moolenaar2bede172020-11-19 18:53:18 +0100537 d = {text: 1, [g:astring]: 2}
Bram Moolenaare8593122020-07-18 15:17:02 +0200538 catch /E721:/
539 n = 333
540 endtry
541 assert_equal(333, n)
542
543 try
544 l = DeletedFunc()
545 catch /E933:/
546 n = 344
547 endtry
548 assert_equal(344, n)
Bram Moolenaard032f342020-07-18 18:13:02 +0200549
550 try
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200551 echo range(1, 2, 0)
552 catch /E726:/
Bram Moolenaard032f342020-07-18 18:13:02 +0200553 n = 355
554 endtry
555 assert_equal(355, n)
556
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200557 var P = function('g:NoSuchFunc')
Bram Moolenaard032f342020-07-18 18:13:02 +0200558 delfunc g:NoSuchFunc
559 try
560 echo P()
561 catch /E117:/
562 n = 366
563 endtry
564 assert_equal(366, n)
565
566 try
567 echo g:NoSuchFunc()
568 catch /E117:/
569 n = 377
570 endtry
571 assert_equal(377, n)
572
573 try
574 echo g:alist + 4
575 catch /E745:/
576 n = 388
577 endtry
578 assert_equal(388, n)
579
580 try
581 echo 4 + g:alist
582 catch /E745:/
583 n = 399
584 endtry
585 assert_equal(399, n)
586
587 try
588 echo g:alist.member
589 catch /E715:/
590 n = 400
591 endtry
592 assert_equal(400, n)
593
594 try
595 echo d.member
596 catch /E716:/
597 n = 411
598 endtry
599 assert_equal(411, n)
Bram Moolenaard9d77892021-02-12 21:32:47 +0100600
601 var counter = 0
602 for i in range(4)
603 try
604 eval [][0]
605 catch
606 endtry
607 counter += 1
608 endfor
609 assert_equal(4, counter)
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100610
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +0200611 # no requirement for spaces before |
612 try|echo 0|catch|endtry
613
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100614 # return in finally after empty catch
615 def ReturnInFinally(): number
616 try
617 finally
618 return 4
619 endtry
620 return 2
621 enddef
622 assert_equal(4, ReturnInFinally())
Bram Moolenaar8ac681a2021-06-15 20:06:34 +0200623
624 var lines =<< trim END
625 vim9script
626 try
627 acos('0.5')
628 ->setline(1)
629 catch
630 g:caught = v:exception
631 endtry
632 END
633 CheckScriptSuccess(lines)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200634 assert_match('E1219: Float or Number required for argument 1', g:caught)
Bram Moolenaar8ac681a2021-06-15 20:06:34 +0200635 unlet g:caught
Bram Moolenaar41978282021-07-04 14:47:30 +0200636
637 # missing catch and/or finally
638 lines =<< trim END
639 vim9script
640 try
641 echo 'something'
642 endtry
643 END
644 CheckScriptFailure(lines, 'E1032:')
rbtnn84934992021-08-07 13:26:53 +0200645
646 # skipping try-finally-endtry when try-finally-endtry is used in another block
647 lines =<< trim END
648 if v:true
649 try
650 finally
651 endtry
652 else
653 try
654 finally
655 endtry
656 endif
657 END
658 CheckDefAndScriptSuccess(lines)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100659enddef
660
Bram Moolenaar3f987b52021-06-30 12:02:24 +0200661def Test_try_in_catch()
662 var lines =<< trim END
663 vim9script
664 var seq = []
665 def DoIt()
666 try
667 seq->add('throw 1')
668 eval [][0]
669 seq->add('notreached')
670 catch
671 seq->add('catch')
672 try
673 seq->add('throw 2')
674 eval [][0]
675 seq->add('notreached')
676 catch /nothing/
677 seq->add('notreached')
678 endtry
679 seq->add('done')
680 endtry
681 enddef
682 DoIt()
683 assert_equal(['throw 1', 'catch', 'throw 2', 'done'], seq)
684 END
685enddef
686
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200687def Test_error_in_catch()
688 var lines =<< trim END
689 try
690 eval [][0]
691 catch /E684:/
692 eval [][0]
693 endtry
694 END
695 CheckDefExecFailure(lines, 'E684:', 4)
696enddef
697
Bram Moolenaar2e34c342021-03-14 12:13:33 +0100698" :while at the very start of a function that :continue jumps to
699def TryContinueFunc()
700 while g:Count < 2
701 g:sequence ..= 't'
702 try
703 echoerr 'Test'
704 catch
705 g:Count += 1
706 g:sequence ..= 'c'
707 continue
708 endtry
709 g:sequence ..= 'e'
710 g:Count += 1
711 endwhile
712enddef
713
714def Test_continue_in_try_in_while()
715 g:Count = 0
716 g:sequence = ''
717 TryContinueFunc()
718 assert_equal('tctc', g:sequence)
719 unlet g:Count
720 unlet g:sequence
721enddef
722
Bram Moolenaar9cb577a2021-02-22 22:45:10 +0100723def Test_nocatch_return_in_try()
724 # return in try block returns normally
725 def ReturnInTry(): string
726 try
727 return '"some message"'
728 catch
729 endtry
730 return 'not reached'
731 enddef
732 exe 'echoerr ' .. ReturnInTry()
733enddef
734
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100735def Test_cnext_works_in_catch()
736 var lines =<< trim END
737 vim9script
Bram Moolenaarc3235272021-07-10 19:42:03 +0200738 au BufEnter * eval 1 + 2
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100739 writefile(['text'], 'Xfile1')
740 writefile(['text'], 'Xfile2')
741 var items = [
742 {lnum: 1, filename: 'Xfile1', valid: true},
743 {lnum: 1, filename: 'Xfile2', valid: true}
744 ]
745 setqflist([], ' ', {items: items})
746 cwindow
747
748 def CnextOrCfirst()
749 # if cnext fails, cfirst is used
750 try
751 cnext
752 catch
753 cfirst
754 endtry
755 enddef
756
757 CnextOrCfirst()
758 CnextOrCfirst()
759 writefile([getqflist({idx: 0}).idx], 'Xresult')
760 qall
761 END
762 writefile(lines, 'XCatchCnext')
763 RunVim([], [], '--clean -S XCatchCnext')
764 assert_equal(['1'], readfile('Xresult'))
765
766 delete('Xfile1')
767 delete('Xfile2')
768 delete('XCatchCnext')
769 delete('Xresult')
770enddef
771
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100772def Test_throw_skipped()
773 if 0
774 throw dontgethere
775 endif
776enddef
777
Bram Moolenaar8f81b222021-01-14 21:47:06 +0100778def Test_nocatch_throw_silenced()
779 var lines =<< trim END
780 vim9script
781 def Func()
782 throw 'error'
783 enddef
784 silent! Func()
785 END
786 writefile(lines, 'XthrowSilenced')
787 source XthrowSilenced
788 delete('XthrowSilenced')
789enddef
790
Bram Moolenaare8593122020-07-18 15:17:02 +0200791def DeletedFunc(): list<any>
792 return ['delete me']
793enddef
794defcompile
795delfunc DeletedFunc
796
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100797def ThrowFromDef()
Bram Moolenaara72cfb82020-04-23 17:07:30 +0200798 throw "getout" # comment
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100799enddef
800
801func CatchInFunc()
802 try
803 call ThrowFromDef()
804 catch
805 let g:thrown_func = v:exception
806 endtry
807endfunc
808
809def CatchInDef()
810 try
811 ThrowFromDef()
812 catch
813 g:thrown_def = v:exception
814 endtry
815enddef
816
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100817def ReturnFinally(): string
818 try
819 return 'intry'
Bram Moolenaar373863e2020-09-26 17:20:53 +0200820 finall
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100821 g:in_finally = 'finally'
822 endtry
823 return 'end'
824enddef
825
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100826def Test_try_catch_nested()
827 CatchInFunc()
828 assert_equal('getout', g:thrown_func)
829
830 CatchInDef()
831 assert_equal('getout', g:thrown_def)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100832
833 assert_equal('intry', ReturnFinally())
834 assert_equal('finally', g:in_finally)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200835
836 var l = []
837 try
838 l->add('1')
839 throw 'bad'
840 l->add('x')
841 catch /bad/
842 l->add('2')
843 try
844 l->add('3')
845 throw 'one'
846 l->add('x')
847 catch /one/
848 l->add('4')
849 try
850 l->add('5')
851 throw 'more'
852 l->add('x')
853 catch /more/
854 l->add('6')
855 endtry
856 endtry
857 endtry
858 assert_equal(['1', '2', '3', '4', '5', '6'], l)
Bram Moolenaar834193a2021-06-30 20:39:15 +0200859
860 l = []
861 try
862 try
863 l->add('1')
864 throw 'foo'
865 l->add('x')
866 catch
867 l->add('2')
868 throw 'bar'
869 l->add('x')
870 finally
871 l->add('3')
872 endtry
873 l->add('x')
874 catch /bar/
875 l->add('4')
876 endtry
877 assert_equal(['1', '2', '3', '4'], l)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100878enddef
879
Bram Moolenaar9939f572020-09-16 22:29:52 +0200880def TryOne(): number
881 try
882 return 0
883 catch
884 endtry
885 return 0
886enddef
887
888def TryTwo(n: number): string
889 try
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200890 var x = {}
Bram Moolenaar9939f572020-09-16 22:29:52 +0200891 catch
892 endtry
893 return 'text'
894enddef
895
896def Test_try_catch_twice()
897 assert_equal('text', TryOne()->TryTwo())
898enddef
899
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100900def Test_try_catch_match()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200901 var seq = 'a'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100902 try
903 throw 'something'
904 catch /nothing/
905 seq ..= 'x'
906 catch /some/
907 seq ..= 'b'
908 catch /asdf/
909 seq ..= 'x'
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200910 catch ?a\?sdf?
911 seq ..= 'y'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100912 finally
913 seq ..= 'c'
914 endtry
915 assert_equal('abc', seq)
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100916enddef
917
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200918def Test_try_catch_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200919 CheckDefFailure(['catch'], 'E603:')
920 CheckDefFailure(['try', 'echo 0', 'catch', 'catch'], 'E1033:')
921 CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
922 CheckDefFailure(['finally'], 'E606:')
923 CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:')
924 CheckDefFailure(['endtry'], 'E602:')
925 CheckDefFailure(['while 1', 'endtry'], 'E170:')
926 CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:')
Bram Moolenaar13106602020-10-04 16:06:05 +0200927 CheckDefFailure(['if 1', 'endtry'], 'E171:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200928 CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:')
Bram Moolenaar585fea72020-04-02 22:33:21 +0200929
Bram Moolenaare4984292020-12-13 14:19:25 +0100930 CheckDefFailure(['throw'], 'E1143:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200931 CheckDefFailure(['throw xxx'], 'E1001:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200932enddef
933
Bram Moolenaar7c5b3c0362021-02-14 22:40:57 +0100934def Try_catch_skipped()
935 var l = []
936 try
937 finally
938 endtry
939
940 if 1
941 else
942 try
943 endtry
944 endif
945enddef
946
947" The skipped try/endtry was updating the wrong instruction.
948def Test_try_catch_skipped()
949 var instr = execute('disassemble Try_catch_skipped')
950 assert_match("NEWLIST size 0\n", instr)
951enddef
952
953
954
Bram Moolenaar006ad482020-06-30 20:55:15 +0200955def Test_throw_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200956 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200957 var lines =<< trim END
Bram Moolenaar006ad482020-06-30 20:55:15 +0200958 vim9script
959 try
960 throw 'one'
961 .. 'two'
962 catch
963 assert_equal('onetwo', v:exception)
964 endtry
965 END
966 CheckScriptSuccess(lines)
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200967
968 lines =<< trim END
969 vim9script
Bram Moolenaar352134b2020-10-17 22:04:08 +0200970 @r = ''
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200971 def Func()
972 throw @r
973 enddef
974 var result = ''
975 try
976 Func()
977 catch /E1129:/
978 result = 'caught'
979 endtry
980 assert_equal('caught', result)
981 END
982 CheckScriptSuccess(lines)
Bram Moolenaar006ad482020-06-30 20:55:15 +0200983enddef
984
Bram Moolenaared677f52020-08-12 16:38:10 +0200985def Test_error_in_nested_function()
Bram Moolenaar03dfde22021-02-14 13:17:22 +0100986 # an error in a nested :function aborts executing in the calling :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200987 var lines =<< trim END
Bram Moolenaared677f52020-08-12 16:38:10 +0200988 vim9script
989 def Func()
990 Error()
991 g:test_var = 1
992 enddef
993 func Error() abort
994 eval [][0]
995 endfunc
996 Func()
997 END
998 g:test_var = 0
999 CheckScriptFailure(lines, 'E684:')
1000 assert_equal(0, g:test_var)
1001enddef
1002
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001003def Test_abort_after_error()
1004 var lines =<< trim END
1005 vim9script
1006 while true
1007 echo notfound
1008 endwhile
1009 g:gotthere = true
1010 END
1011 g:gotthere = false
1012 CheckScriptFailure(lines, 'E121:')
1013 assert_false(g:gotthere)
1014 unlet g:gotthere
1015enddef
1016
Bram Moolenaar37c83712020-06-30 21:18:36 +02001017def Test_cexpr_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001018 # only checks line continuation
Bram Moolenaar37c83712020-06-30 21:18:36 +02001019 set errorformat=File\ %f\ line\ %l
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001020 var lines =<< trim END
Bram Moolenaar37c83712020-06-30 21:18:36 +02001021 vim9script
1022 cexpr 'File'
1023 .. ' someFile' ..
1024 ' line 19'
1025 assert_equal(19, getqflist()[0].lnum)
1026 END
1027 CheckScriptSuccess(lines)
1028 set errorformat&
1029enddef
1030
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +02001031def Test_statusline_syntax()
1032 # legacy syntax is used for 'statusline'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001033 var lines =<< trim END
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +02001034 vim9script
1035 func g:Status()
1036 return '%{"x" is# "x"}'
1037 endfunc
1038 set laststatus=2 statusline=%!Status()
1039 redrawstatus
1040 set laststatus statusline=
1041 END
1042 CheckScriptSuccess(lines)
1043enddef
1044
Bram Moolenaarb2097502020-07-19 17:17:02 +02001045def Test_list_vimscript()
1046 # checks line continuation and comments
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001047 var lines =<< trim END
Bram Moolenaarb2097502020-07-19 17:17:02 +02001048 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001049 var mylist = [
Bram Moolenaarb2097502020-07-19 17:17:02 +02001050 'one',
1051 # comment
1052 'two', # empty line follows
1053
1054 'three',
1055 ]
1056 assert_equal(['one', 'two', 'three'], mylist)
1057 END
1058 CheckScriptSuccess(lines)
Bram Moolenaar66250c92020-08-20 15:02:42 +02001059
1060 # check all lines from heredoc are kept
1061 lines =<< trim END
1062 # comment 1
1063 two
1064 # comment 3
1065
1066 five
1067 # comment 6
1068 END
1069 assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines)
Bram Moolenaar4bce26b2021-01-22 22:06:56 +01001070
1071 lines =<< trim END
1072 [{
1073 a: 0}]->string()->assert_equal("[{'a': 0}]")
1074 END
1075 CheckDefAndScriptSuccess(lines)
Bram Moolenaarb2097502020-07-19 17:17:02 +02001076enddef
1077
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001078if has('channel')
1079 let someJob = test_null_job()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001080
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001081 def FuncWithError()
1082 echomsg g:someJob
1083 enddef
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001084
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001085 func Test_convert_emsg_to_exception()
1086 try
1087 call FuncWithError()
1088 catch
1089 call assert_match('Vim:E908:', v:exception)
1090 endtry
1091 endfunc
1092endif
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094let s:export_script_lines =<< trim END
1095 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001096 var name: string = 'bob'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 def Concat(arg: string): string
1098 return name .. arg
1099 enddef
Bram Moolenaar227a69d2020-05-15 18:17:28 +02001100 g:result = Concat('bie')
1101 g:localname = name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001102
1103 export const CONST = 1234
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001104 export var exported = 9876
1105 export var exp_name = 'John'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 export def Exported(): string
1107 return 'Exported'
1108 enddef
Bram Moolenaar24e93162021-07-18 20:40:33 +02001109 export def ExportedValue(): number
1110 return exported
1111 enddef
1112 export def ExportedInc()
1113 exported += 5
1114 enddef
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001115 export final theList = [1]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116END
1117
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001118def Undo_export_script_lines()
1119 unlet g:result
1120 unlet g:localname
1121enddef
1122
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001123def Test_vim9_import_export()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001124 var import_script_lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 vim9script
Bram Moolenaar24e93162021-07-18 20:40:33 +02001126 import {exported, Exported, ExportedValue} from './Xexport.vim'
1127 g:exported1 = exported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001128 exported += 3
Bram Moolenaar24e93162021-07-18 20:40:33 +02001129 g:exported2 = exported
1130 g:exported3 = ExportedValue()
1131
1132 import ExportedInc from './Xexport.vim'
1133 ExportedInc()
1134 g:exported_i1 = exported
1135 g:exported_i2 = ExportedValue()
1136
1137 exported = 11
1138 g:exported_s1 = exported
1139 g:exported_s2 = ExportedValue()
1140
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 g:imported_func = Exported()
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001142
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001143 def GetExported(): string
Bram Moolenaare0de1712020-12-02 17:36:54 +01001144 var local_dict = {ref: Exported}
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001145 return local_dict.ref()
1146 enddef
1147 g:funcref_result = GetExported()
1148
Bram Moolenaar4db572e2021-07-18 18:21:38 +02001149 var dir = './'
1150 var ext = ".vim"
1151 import {exp_name} from dir .. 'Xexport' .. ext
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001152 g:imported_name = exp_name
1153 exp_name ..= ' Doe'
1154 g:imported_name_appended = exp_name
Bram Moolenaar24e93162021-07-18 20:40:33 +02001155 g:exported_later = exported
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001156
1157 import theList from './Xexport.vim'
1158 theList->add(2)
1159 assert_equal([1, 2], theList)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001160 END
1161
1162 writefile(import_script_lines, 'Ximport.vim')
1163 writefile(s:export_script_lines, 'Xexport.vim')
1164
1165 source Ximport.vim
1166
1167 assert_equal('bobbie', g:result)
1168 assert_equal('bob', g:localname)
Bram Moolenaar24e93162021-07-18 20:40:33 +02001169 assert_equal(9876, g:exported1)
1170 assert_equal(9879, g:exported2)
1171 assert_equal(9879, g:exported3)
1172
1173 assert_equal(9884, g:exported_i1)
1174 assert_equal(9884, g:exported_i2)
1175
1176 assert_equal(11, g:exported_s1)
1177 assert_equal(11, g:exported_s2)
1178 assert_equal(11, g:exported_later)
1179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 assert_equal('Exported', g:imported_func)
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001181 assert_equal('Exported', g:funcref_result)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001182 assert_equal('John', g:imported_name)
1183 assert_equal('John Doe', g:imported_name_appended)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184 assert_false(exists('g:name'))
1185
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001186 Undo_export_script_lines()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001187 unlet g:exported1
1188 unlet g:exported2
1189 unlet g:exported3
1190 unlet g:exported_i1
1191 unlet g:exported_i2
1192 unlet g:exported_later
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193 unlet g:imported_func
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001194 unlet g:imported_name g:imported_name_appended
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195 delete('Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001196
Bram Moolenaar1c991142020-07-04 13:15:31 +02001197 # similar, with line breaks
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001198 var import_line_break_script_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001199 vim9script
1200 import {
1201 exported,
1202 Exported,
1203 }
1204 from
1205 './Xexport.vim'
Bram Moolenaar24e93162021-07-18 20:40:33 +02001206 g:exported = exported
1207 exported += 7
1208 g:exported_added = exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001209 g:imported_func = Exported()
1210 END
1211 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
1212 source Ximport_lbr.vim
1213
Bram Moolenaar24e93162021-07-18 20:40:33 +02001214 assert_equal(11, g:exported)
1215 assert_equal(18, g:exported_added)
Bram Moolenaar1c991142020-07-04 13:15:31 +02001216 assert_equal('Exported', g:imported_func)
1217
1218 # exported script not sourced again
1219 assert_false(exists('g:result'))
Bram Moolenaar24e93162021-07-18 20:40:33 +02001220 unlet g:exported
1221 unlet g:exported_added
Bram Moolenaar1c991142020-07-04 13:15:31 +02001222 unlet g:imported_func
1223 delete('Ximport_lbr.vim')
1224
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001225 var import_star_as_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001226 vim9script
1227 import * as Export from './Xexport.vim'
1228 def UseExport()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001229 g:exported_def = Export.exported
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001230 enddef
Bram Moolenaar24e93162021-07-18 20:40:33 +02001231 g:exported_script = Export.exported
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001232 assert_equal(1, exists('Export.exported'))
1233 assert_equal(0, exists('Export.notexported'))
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001234 UseExport()
1235 END
1236 writefile(import_star_as_lines, 'Ximport.vim')
1237 source Ximport.vim
Bram Moolenaar24e93162021-07-18 20:40:33 +02001238
1239 assert_equal(18, g:exported_def)
1240 assert_equal(18, g:exported_script)
1241 unlet g:exported_def
1242 unlet g:exported_script
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001243
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001244 var import_star_as_lines_no_dot =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001245 vim9script
1246 import * as Export from './Xexport.vim'
1247 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001248 var dummy = 1
1249 var imported = Export + dummy
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001250 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001251 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001252 END
1253 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001254 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001255
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001256 var import_star_as_lines_dot_space =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001257 vim9script
1258 import * as Export from './Xexport.vim'
1259 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001260 var imported = Export . exported
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001261 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001262 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001263 END
1264 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001265 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001266
Bram Moolenaar921ba522021-07-29 22:25:05 +02001267 var import_func_duplicated =<< trim END
1268 vim9script
1269 import ExportedInc from './Xexport.vim'
1270 import ExportedInc from './Xexport.vim'
1271
1272 ExportedInc()
1273 END
1274 writefile(import_func_duplicated, 'Ximport.vim')
1275 assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
1276
Bram Moolenaara6294952020-12-27 13:39:50 +01001277 var import_star_as_duplicated =<< trim END
1278 vim9script
1279 import * as Export from './Xexport.vim'
1280 var some = 'other'
1281 import * as Export from './Xexport.vim'
1282 defcompile
1283 END
1284 writefile(import_star_as_duplicated, 'Ximport.vim')
1285 assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim')
1286
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001287 var import_star_as_lines_script_no_dot =<< trim END
1288 vim9script
1289 import * as Export from './Xexport.vim'
1290 g:imported_script = Export exported
1291 END
1292 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
1293 assert_fails('source Ximport.vim', 'E1029:')
1294
1295 var import_star_as_lines_script_space_after_dot =<< trim END
1296 vim9script
1297 import * as Export from './Xexport.vim'
1298 g:imported_script = Export. exported
1299 END
1300 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
1301 assert_fails('source Ximport.vim', 'E1074:')
1302
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001303 var import_star_as_lines_missing_name =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001304 vim9script
1305 import * as Export from './Xexport.vim'
1306 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001307 var imported = Export.
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001308 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001309 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001310 END
1311 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001312 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001313
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001314 var import_star_as_lbr_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001315 vim9script
1316 import *
1317 as Export
1318 from
1319 './Xexport.vim'
1320 def UseExport()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001321 g:exported = Export.exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001322 enddef
1323 UseExport()
1324 END
1325 writefile(import_star_as_lbr_lines, 'Ximport.vim')
1326 source Ximport.vim
Bram Moolenaar24e93162021-07-18 20:40:33 +02001327 assert_equal(18, g:exported)
1328 unlet g:exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001329
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001330 var import_star_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001331 vim9script
1332 import * from './Xexport.vim'
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001333 END
1334 writefile(import_star_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001335 assert_fails('source Ximport.vim', 'E1045:', '', 2, 'Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001336
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001337 # try to import something that exists but is not exported
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001338 var import_not_exported_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001339 vim9script
1340 import name from './Xexport.vim'
1341 END
1342 writefile(import_not_exported_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001343 assert_fails('source Ximport.vim', 'E1049:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001344
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001345 # try to import something that is already defined
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001346 var import_already_defined =<< trim END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001347 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001348 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001349 import exported from './Xexport.vim'
1350 END
1351 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001352 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001353
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001354 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001355 import_already_defined =<< trim END
1356 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001357 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001358 import * as exported from './Xexport.vim'
1359 END
1360 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001361 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001362
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001363 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001364 import_already_defined =<< trim END
1365 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001366 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001367 import {exported} from './Xexport.vim'
1368 END
1369 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001370 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001371
Bram Moolenaar918a4242020-12-06 14:37:08 +01001372 # try changing an imported const
1373 var import_assign_to_const =<< trim END
1374 vim9script
1375 import CONST from './Xexport.vim'
1376 def Assign()
1377 CONST = 987
1378 enddef
1379 defcompile
1380 END
1381 writefile(import_assign_to_const, 'Ximport.vim')
1382 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1383
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001384 # try changing an imported final
1385 var import_assign_to_final =<< trim END
1386 vim9script
1387 import theList from './Xexport.vim'
1388 def Assign()
1389 theList = [2]
1390 enddef
1391 defcompile
1392 END
1393 writefile(import_assign_to_final, 'Ximport.vim')
1394 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1395
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001396 # import a very long name, requires making a copy
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001397 var import_long_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001398 vim9script
1399 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
1400 END
1401 writefile(import_long_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001402 assert_fails('source Ximport.vim', 'E1048:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001403
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001404 var import_no_from_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001405 vim9script
1406 import name './Xexport.vim'
1407 END
1408 writefile(import_no_from_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001409 assert_fails('source Ximport.vim', 'E1070:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001410
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001411 var import_invalid_string_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001412 vim9script
1413 import name from Xexport.vim
1414 END
1415 writefile(import_invalid_string_lines, 'Ximport.vim')
Bram Moolenaar4db572e2021-07-18 18:21:38 +02001416 assert_fails('source Ximport.vim', 'E121:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001417
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001418 var import_wrong_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001419 vim9script
1420 import name from './XnoExport.vim'
1421 END
1422 writefile(import_wrong_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001423 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001424
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001425 var import_missing_comma_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001426 vim9script
1427 import {exported name} from './Xexport.vim'
1428 END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001429 writefile(import_missing_comma_lines, 'Ximport3.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001430 assert_fails('source Ximport3.vim', 'E1046:', '', 2, 'Ximport3.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001431
Bram Moolenaar60579352021-07-19 21:45:07 +02001432 var import_redefining_lines =<< trim END
1433 vim9script
1434 import exported from './Xexport.vim'
1435 var exported = 5
1436 END
1437 writefile(import_redefining_lines, 'Ximport.vim')
1438 assert_fails('source Ximport.vim', 'E1213: Redefining imported item "exported"', '', 3)
1439
1440 var import_assign_wrong_type_lines =<< trim END
1441 vim9script
1442 import exported from './Xexport.vim'
1443 exported = 'xxx'
1444 END
1445 writefile(import_assign_wrong_type_lines, 'Ximport.vim')
1446 assert_fails('source Ximport.vim', 'E1012: Type mismatch; expected number but got string', '', 3)
1447
1448 var import_assign_const_lines =<< trim END
1449 vim9script
1450 import CONST from './Xexport.vim'
1451 CONST = 4321
1452 END
1453 writefile(import_assign_const_lines, 'Ximport.vim')
1454 assert_fails('source Ximport.vim', 'E741: Value is locked: CONST', '', 3)
1455
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001456 delete('Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001457 delete('Ximport3.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 delete('Xexport.vim')
1459
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001460 # Check that in a Vim9 script 'cpo' is set to the Vim default.
Bram Moolenaar3e191692021-03-17 17:46:00 +01001461 # Flags added or removed are also applied to the restored value.
1462 set cpo=abcd
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001463 var lines =<< trim END
Bram Moolenaar750802b2020-02-23 18:08:33 +01001464 vim9script
1465 g:cpo_in_vim9script = &cpo
Bram Moolenaar3e191692021-03-17 17:46:00 +01001466 set cpo+=f
1467 set cpo-=c
1468 g:cpo_after_vim9script = &cpo
Bram Moolenaar750802b2020-02-23 18:08:33 +01001469 END
1470 writefile(lines, 'Xvim9_script')
1471 source Xvim9_script
Bram Moolenaar3e191692021-03-17 17:46:00 +01001472 assert_equal('fabd', &cpo)
Bram Moolenaar750802b2020-02-23 18:08:33 +01001473 set cpo&vim
1474 assert_equal(&cpo, g:cpo_in_vim9script)
Bram Moolenaar3e191692021-03-17 17:46:00 +01001475 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
1476 assert_equal(newcpo, g:cpo_after_vim9script)
1477
Bram Moolenaar750802b2020-02-23 18:08:33 +01001478 delete('Xvim9_script')
1479enddef
1480
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001481def Test_import_funcref()
1482 var lines =<< trim END
1483 vim9script
1484 export def F(): number
1485 return 42
1486 enddef
1487 export const G = F
1488 END
1489 writefile(lines, 'Xlib.vim')
1490
1491 lines =<< trim END
1492 vim9script
1493 import {G} from './Xlib.vim'
1494 const Foo = G()
1495 assert_equal(42, Foo)
1496
1497 def DoTest()
1498 const Goo = G()
Bram Moolenaar06ca48a2021-10-23 10:25:21 +01001499 assert_equal(42, Goo)
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001500 enddef
1501 DoTest()
1502 END
1503 CheckScriptSuccess(lines)
1504
1505 delete('Xlib.vim')
1506enddef
1507
Bram Moolenaar6853c382021-09-07 22:12:19 +02001508def Test_import_star_fails()
1509 writefile([], 'Xfoo.vim')
1510 var lines =<< trim END
Bram Moolenaaraf2d5d22021-09-07 22:35:34 +02001511 import * as foo from './Xfoo.vim'
Bram Moolenaar6853c382021-09-07 22:12:19 +02001512 foo = 'bar'
1513 END
1514 CheckDefAndScriptFailure2(lines, 'E1094:', 'E1236: Cannot use foo itself')
1515 lines =<< trim END
1516 vim9script
Bram Moolenaaraf2d5d22021-09-07 22:35:34 +02001517 import * as foo from './Xfoo.vim'
Bram Moolenaar6853c382021-09-07 22:12:19 +02001518 var that = foo
1519 END
1520 CheckScriptFailure(lines, 'E1029: Expected ''.''')
Bram Moolenaara9e3d562021-09-08 12:31:35 +02001521
1522 lines =<< trim END
1523 vim9script
1524 import * as 9foo from './Xfoo.vim'
1525 END
1526 CheckScriptFailure(lines, 'E1047:')
1527 lines =<< trim END
1528 vim9script
1529 import * as the#foo from './Xfoo.vim'
1530 END
1531 CheckScriptFailure(lines, 'E1047:')
1532 lines =<< trim END
1533 vim9script
1534 import * as g:foo from './Xfoo.vim'
1535 END
1536 CheckScriptFailure(lines, 'E1047:')
1537
Bram Moolenaaraf2d5d22021-09-07 22:35:34 +02001538 delete('Xfoo.vim')
Bram Moolenaar6853c382021-09-07 22:12:19 +02001539enddef
1540
Bram Moolenaar0a842842021-02-27 22:41:19 +01001541def Test_import_as()
1542 var export_lines =<< trim END
1543 vim9script
1544 export var one = 1
1545 export var yes = 'yes'
Bram Moolenaarc967d572021-07-08 21:38:50 +02001546 export var slist: list<string>
Bram Moolenaar0a842842021-02-27 22:41:19 +01001547 END
1548 writefile(export_lines, 'XexportAs')
1549
1550 var import_lines =<< trim END
1551 vim9script
Bram Moolenaar6c7cc342021-04-17 16:38:50 +02001552 var one = 'notused'
1553 var yes = 777
Bram Moolenaar0a842842021-02-27 22:41:19 +01001554 import one as thatOne from './XexportAs'
1555 assert_equal(1, thatOne)
1556 import yes as yesYes from './XexportAs'
1557 assert_equal('yes', yesYes)
1558 END
1559 CheckScriptSuccess(import_lines)
1560
1561 import_lines =<< trim END
1562 vim9script
1563 import {one as thatOne, yes as yesYes} from './XexportAs'
1564 assert_equal(1, thatOne)
1565 assert_equal('yes', yesYes)
1566 assert_fails('echo one', 'E121:')
1567 assert_fails('echo yes', 'E121:')
1568 END
1569 CheckScriptSuccess(import_lines)
1570
Bram Moolenaarc967d572021-07-08 21:38:50 +02001571 import_lines =<< trim END
1572 vim9script
1573 import {slist as impSlist} from './XexportAs'
1574 impSlist->add(123)
1575 END
1576 CheckScriptFailure(import_lines, 'E1012: Type mismatch; expected string but got number')
1577
Bram Moolenaar0a842842021-02-27 22:41:19 +01001578 delete('XexportAs')
1579enddef
1580
Bram Moolenaar803af682020-08-05 16:20:03 +02001581func g:Trigger()
1582 source Ximport.vim
1583 return "echo 'yes'\<CR>"
1584endfunc
1585
1586def Test_import_export_expr_map()
1587 # check that :import and :export work when buffer is locked
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001588 var export_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001589 vim9script
1590 export def That(): string
1591 return 'yes'
1592 enddef
1593 END
1594 writefile(export_lines, 'Xexport_that.vim')
1595
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001596 var import_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001597 vim9script
1598 import That from './Xexport_that.vim'
1599 assert_equal('yes', That())
1600 END
1601 writefile(import_lines, 'Ximport.vim')
1602
1603 nnoremap <expr> trigger g:Trigger()
1604 feedkeys('trigger', "xt")
1605
Bram Moolenaar730b2482020-08-09 13:02:10 +02001606 delete('Xexport_that.vim')
Bram Moolenaar803af682020-08-05 16:20:03 +02001607 delete('Ximport.vim')
1608 nunmap trigger
1609enddef
1610
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001611def Test_import_in_filetype()
1612 # check that :import works when the buffer is locked
1613 mkdir('ftplugin', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001614 var export_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001615 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001616 export var That = 'yes'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001617 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001618 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001619
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001620 var import_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001621 vim9script
Bram Moolenaar730b2482020-08-09 13:02:10 +02001622 import That from './Xexport_ft.vim'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001623 assert_equal('yes', That)
1624 g:did_load_mytpe = 1
1625 END
1626 writefile(import_lines, 'ftplugin/qf.vim')
1627
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001628 var save_rtp = &rtp
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001629 &rtp = getcwd() .. ',' .. &rtp
1630
1631 filetype plugin on
1632 copen
1633 assert_equal(1, g:did_load_mytpe)
1634
1635 quit!
Bram Moolenaar730b2482020-08-09 13:02:10 +02001636 delete('Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001637 delete('ftplugin', 'rf')
1638 &rtp = save_rtp
1639enddef
1640
Bram Moolenaarefa94442020-08-08 22:16:00 +02001641def Test_use_import_in_mapping()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001642 var lines =<< trim END
Bram Moolenaarefa94442020-08-08 22:16:00 +02001643 vim9script
1644 export def Funcx()
1645 g:result = 42
1646 enddef
1647 END
1648 writefile(lines, 'XsomeExport.vim')
1649 lines =<< trim END
1650 vim9script
1651 import Funcx from './XsomeExport.vim'
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001652 nnoremap <F3> :call <sid>Funcx()<cr>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001653 END
1654 writefile(lines, 'Xmapscript.vim')
1655
1656 source Xmapscript.vim
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001657 feedkeys("\<F3>", "xt")
Bram Moolenaarefa94442020-08-08 22:16:00 +02001658 assert_equal(42, g:result)
1659
1660 unlet g:result
1661 delete('XsomeExport.vim')
1662 delete('Xmapscript.vim')
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001663 nunmap <F3>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001664enddef
1665
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01001666def Test_vim9script_mix()
1667 var lines =<< trim END
1668 if has(g:feature)
1669 " legacy script
1670 let g:legacy = 1
1671 finish
1672 endif
1673 vim9script
1674 g:legacy = 0
1675 END
1676 g:feature = 'eval'
1677 g:legacy = -1
1678 CheckScriptSuccess(lines)
1679 assert_equal(1, g:legacy)
1680
1681 g:feature = 'noteval'
1682 g:legacy = -1
1683 CheckScriptSuccess(lines)
1684 assert_equal(0, g:legacy)
1685enddef
1686
Bram Moolenaar750802b2020-02-23 18:08:33 +01001687def Test_vim9script_fails()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1689 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001690 CheckScriptFailure(['export var some = 123'], 'E1042:')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001691 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001692 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
Bram Moolenaar750802b2020-02-23 18:08:33 +01001693 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
1694
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001695 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001696 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1697
Bram Moolenaare2e40752020-09-04 21:18:46 +02001698 assert_fails('vim9script', 'E1038:')
1699 assert_fails('export something', 'E1043:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700enddef
1701
Bram Moolenaarf7d267e2020-06-17 12:04:54 +02001702func Test_import_fails_without_script()
Bram Moolenaar101f4812020-06-16 23:18:51 +02001703 CheckRunVimInTerminal
1704
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001705 " call indirectly to avoid compilation error for missing functions
Bram Moolenaarc620c052020-07-08 15:16:19 +02001706 call Run_Test_import_fails_on_command_line()
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001707endfunc
1708
Bram Moolenaarc620c052020-07-08 15:16:19 +02001709def Run_Test_import_fails_on_command_line()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001710 var export =<< trim END
Bram Moolenaar101f4812020-06-16 23:18:51 +02001711 vim9script
1712 export def Foo(): number
1713 return 0
1714 enddef
1715 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001716 writefile(export, 'XexportCmd.vim')
Bram Moolenaar101f4812020-06-16 23:18:51 +02001717
Bram Moolenaare0de1712020-12-02 17:36:54 +01001718 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001719 rows: 6, wait_for_ruler: 0})
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001720 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaar101f4812020-06-16 23:18:51 +02001721
Bram Moolenaar730b2482020-08-09 13:02:10 +02001722 delete('XexportCmd.vim')
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001723 StopVimInTerminal(buf)
1724enddef
Bram Moolenaar101f4812020-06-16 23:18:51 +02001725
Bram Moolenaar2b327002020-12-26 15:39:31 +01001726def Test_vim9script_reload_noclear()
1727 var lines =<< trim END
Bram Moolenaara6294952020-12-27 13:39:50 +01001728 vim9script
1729 export var exported = 'thexport'
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001730
1731 export def TheFunc(x = 0)
1732 enddef
Bram Moolenaara6294952020-12-27 13:39:50 +01001733 END
1734 writefile(lines, 'XExportReload')
1735 lines =<< trim END
Bram Moolenaar2b327002020-12-26 15:39:31 +01001736 vim9script noclear
1737 g:loadCount += 1
1738 var s:reloaded = 'init'
Bram Moolenaara6294952020-12-27 13:39:50 +01001739 import exported from './XExportReload'
Bram Moolenaar2b327002020-12-26 15:39:31 +01001740
1741 def Again(): string
1742 return 'again'
1743 enddef
1744
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001745 import TheFunc from './XExportReload'
1746 TheFunc()
1747
Bram Moolenaar2b327002020-12-26 15:39:31 +01001748 if exists('s:loaded') | finish | endif
1749 var s:loaded = true
1750
1751 var s:notReloaded = 'yes'
1752 s:reloaded = 'first'
1753 def g:Values(): list<string>
Bram Moolenaara6294952020-12-27 13:39:50 +01001754 return [s:reloaded, s:notReloaded, Again(), Once(), exported]
Bram Moolenaar2b327002020-12-26 15:39:31 +01001755 enddef
1756
1757 def Once(): string
1758 return 'once'
1759 enddef
1760 END
1761 writefile(lines, 'XReloaded')
1762 g:loadCount = 0
1763 source XReloaded
1764 assert_equal(1, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001765 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001766 source XReloaded
1767 assert_equal(2, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001768 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001769 source XReloaded
1770 assert_equal(3, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001771 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001772
Bram Moolenaar48e11c12021-01-11 18:47:00 +01001773 delete('XReloaded')
Bram Moolenaara6294952020-12-27 13:39:50 +01001774 delete('XExportReload')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001775 delfunc g:Values
Bram Moolenaar2b327002020-12-26 15:39:31 +01001776 unlet g:loadCount
Bram Moolenaar577dc932021-06-27 15:35:40 +02001777
1778 lines =<< trim END
1779 vim9script
1780 def Inner()
1781 enddef
1782 END
1783 lines->writefile('XreloadScript.vim')
1784 source XreloadScript.vim
1785
1786 lines =<< trim END
1787 vim9script
1788 def Outer()
1789 def Inner()
1790 enddef
1791 enddef
1792 defcompile
1793 END
1794 lines->writefile('XreloadScript.vim')
1795 source XreloadScript.vim
1796
1797 delete('XreloadScript.vim')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001798enddef
1799
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001800def Test_vim9script_reload_import()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001801 var lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 vim9script
1803 const var = ''
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001804 var valone = 1234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 def MyFunc(arg: string)
1806 valone = 5678
1807 enddef
1808 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001809 var morelines =<< trim END
1810 var valtwo = 222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 export def GetValtwo(): number
1812 return valtwo
1813 enddef
1814 END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001815 writefile(lines + morelines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816 source Xreload.vim
1817 source Xreload.vim
1818 source Xreload.vim
1819
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001820 # cannot declare a var twice
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 lines =<< trim END
1822 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001823 var valone = 1234
1824 var valone = 5678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 END
1826 writefile(lines, 'Xreload.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001827 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828
1829 delete('Xreload.vim')
1830 delete('Ximport.vim')
1831enddef
1832
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001833" if a script is reloaded with a script-local variable that changed its type, a
1834" compiled function using that variable must fail.
1835def Test_script_reload_change_type()
1836 var lines =<< trim END
1837 vim9script noclear
1838 var str = 'string'
1839 def g:GetStr(): string
1840 return str .. 'xxx'
1841 enddef
1842 END
1843 writefile(lines, 'Xreload.vim')
1844 source Xreload.vim
1845 echo g:GetStr()
1846
1847 lines =<< trim END
1848 vim9script noclear
1849 var str = 1234
1850 END
1851 writefile(lines, 'Xreload.vim')
1852 source Xreload.vim
1853 assert_fails('echo g:GetStr()', 'E1150:')
1854
1855 delfunc g:GetStr
1856 delete('Xreload.vim')
1857enddef
1858
Bram Moolenaarc970e422021-03-17 15:03:04 +01001859" Define CallFunc so that the test can be compiled
1860command CallFunc echo 'nop'
1861
1862def Test_script_reload_from_function()
1863 var lines =<< trim END
1864 vim9script
1865
1866 if exists('g:loaded')
1867 finish
1868 endif
1869 g:loaded = 1
1870 delcommand CallFunc
1871 command CallFunc Func()
1872 def Func()
Bram Moolenaara4c81be2021-03-17 15:23:16 +01001873 so XreloadFunc.vim
Bram Moolenaarc970e422021-03-17 15:03:04 +01001874 g:didTheFunc = 1
1875 enddef
1876 END
1877 writefile(lines, 'XreloadFunc.vim')
1878 source XreloadFunc.vim
1879 CallFunc
1880 assert_equal(1, g:didTheFunc)
1881
1882 delete('XreloadFunc.vim')
1883 delcommand CallFunc
1884 unlet g:loaded
1885 unlet g:didTheFunc
1886enddef
1887
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001888def Test_script_var_shadows_function()
1889 var lines =<< trim END
1890 vim9script
1891 def Func(): number
1892 return 123
1893 enddef
1894 var Func = 1
1895 END
1896 CheckScriptFailure(lines, 'E1041:', 5)
1897enddef
1898
Bram Moolenaar052ff292021-12-11 13:54:46 +00001899def Test_function_shadows_script_var()
1900 var lines =<< trim END
1901 vim9script
1902 var Func = 1
1903 def Func(): number
1904 return 123
1905 enddef
1906 END
1907 CheckScriptFailure(lines, 'E1041:', 3)
1908enddef
1909
Bram Moolenaarc3235272021-07-10 19:42:03 +02001910def Test_script_var_shadows_command()
1911 var lines =<< trim END
1912 var undo = 1
1913 undo = 2
1914 assert_equal(2, undo)
1915 END
1916 CheckDefAndScriptSuccess(lines)
1917
1918 lines =<< trim END
1919 var undo = 1
1920 undo
1921 END
1922 CheckDefAndScriptFailure(lines, 'E1207:', 2)
1923enddef
1924
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001925def Test_vim9script_call_wrong_type()
1926 var lines =<< trim END
1927 vim9script
1928 var Time = 'localtime'
1929 Time()
1930 END
1931 CheckScriptFailure(lines, 'E1085:')
1932enddef
1933
Bram Moolenaar95006e32020-08-29 17:47:08 +02001934def s:RetSome(): string
1935 return 'some'
1936enddef
1937
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001938" Not exported function that is referenced needs to be accessed by the
1939" script-local name.
1940def Test_vim9script_funcref()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001941 var sortlines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001942 vim9script
1943 def Compare(i1: number, i2: number): number
Bram Moolenaarbed36b92020-07-07 23:31:36 +02001944 return i2 - i1
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001945 enddef
1946
1947 export def FastSort(): list<number>
1948 return range(5)->sort(Compare)
1949 enddef
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001950
1951 export def GetString(arg: string): string
1952 return arg
1953 enddef
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001954 END
1955 writefile(sortlines, 'Xsort.vim')
1956
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001957 var lines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001958 vim9script
1959 import FastSort from './Xsort.vim'
1960 def Test()
1961 g:result = FastSort()
1962 enddef
1963 Test()
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001964
1965 # using a function imported with "as"
1966 import * as anAlias from './Xsort.vim'
1967 assert_equal('yes', anAlias.GetString('yes'))
1968
1969 # using the function from a compiled function
1970 def TestMore(): string
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001971 var s = s:anAlias.GetString('foo')
1972 return s .. anAlias.GetString('bar')
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001973 enddef
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001974 assert_equal('foobar', TestMore())
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001975
1976 # error when using a function that isn't exported
1977 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001978 END
1979 writefile(lines, 'Xscript.vim')
1980
1981 source Xscript.vim
1982 assert_equal([4, 3, 2, 1, 0], g:result)
1983
1984 unlet g:result
1985 delete('Xsort.vim')
1986 delete('Xscript.vim')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001987
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001988 var Funcref = function('s:RetSome')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001989 assert_equal('some', Funcref())
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001990enddef
1991
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001992" Check that when searching for "FilterFunc" it finds the import in the
1993" script where FastFilter() is called from, both as a string and as a direct
1994" function reference.
Bram Moolenaarc620c052020-07-08 15:16:19 +02001995def Test_vim9script_funcref_other_script()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001996 var filterLines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001997 vim9script
1998 export def FilterFunc(idx: number, val: number): bool
1999 return idx % 2 == 1
2000 enddef
2001 export def FastFilter(): list<number>
2002 return range(10)->filter('FilterFunc')
2003 enddef
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002004 export def FastFilterDirect(): list<number>
2005 return range(10)->filter(FilterFunc)
2006 enddef
Bram Moolenaarc620c052020-07-08 15:16:19 +02002007 END
2008 writefile(filterLines, 'Xfilter.vim')
2009
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002010 var lines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02002011 vim9script
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002012 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim'
Bram Moolenaarc620c052020-07-08 15:16:19 +02002013 def Test()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002014 var x: list<number> = FastFilter()
Bram Moolenaarc620c052020-07-08 15:16:19 +02002015 enddef
2016 Test()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002017 def TestDirect()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002018 var x: list<number> = FastFilterDirect()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002019 enddef
2020 TestDirect()
Bram Moolenaarc620c052020-07-08 15:16:19 +02002021 END
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002022 CheckScriptSuccess(lines)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002023 delete('Xfilter.vim')
Bram Moolenaarc620c052020-07-08 15:16:19 +02002024enddef
2025
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002026def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002027 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002028 vim9script
2029 def FuncYes(): string
2030 return 'yes'
2031 enddef
2032 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002033 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002034 def FuncNo(): string
2035 return 'no'
2036 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002037 def g:DoCheck(no_exists: bool)
2038 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002039 assert_equal('no', FuncNo())
2040 enddef
2041 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002042 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002043 def g:DoCheck(no_exists: bool)
2044 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02002045 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002046 enddef
2047 END
2048
2049 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002050 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002051 source Xreloaded.vim
2052 g:DoCheck(true)
2053
2054 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002055 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002056 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01002057 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002058
2059 # FuncNo() is back
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002060 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002061 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01002062 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002063
2064 delete('Xreloaded.vim')
2065enddef
2066
Bram Moolenaar89483d42020-05-10 15:24:44 +02002067def Test_vim9script_reload_delvar()
2068 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002069 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02002070 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002071 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02002072 END
2073 writefile(lines, 'XreloadVar.vim')
2074 source XreloadVar.vim
2075
2076 # now write the script using the same variable locally - works
2077 lines =<< trim END
2078 vim9script
2079 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002080 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02002081 enddef
2082 END
2083 writefile(lines, 'XreloadVar.vim')
2084 source XreloadVar.vim
2085
2086 delete('XreloadVar.vim')
2087enddef
2088
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089def Test_import_absolute()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002090 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02002091 'vim9script',
2092 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
2093 'def UseExported()',
2094 ' g:imported_abs = exported',
2095 ' exported = 8888',
2096 ' g:imported_after = exported',
2097 'enddef',
2098 'UseExported()',
2099 'g:import_disassembled = execute("disass UseExported")',
2100 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101 writefile(import_lines, 'Ximport_abs.vim')
2102 writefile(s:export_script_lines, 'Xexport_abs.vim')
2103
2104 source Ximport_abs.vim
2105
2106 assert_equal(9876, g:imported_abs)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002107 assert_equal(8888, g:imported_after)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002108 assert_match('<SNR>\d\+_UseExported\_s*' ..
2109 'g:imported_abs = exported\_s*' ..
2110 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
2111 '1 STOREG g:imported_abs\_s*' ..
2112 'exported = 8888\_s*' ..
2113 '2 PUSHNR 8888\_s*' ..
2114 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
2115 'g:imported_after = exported\_s*' ..
2116 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
2117 '5 STOREG g:imported_after',
Bram Moolenaare6085c52020-04-12 20:19:16 +02002118 g:import_disassembled)
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002119
2120 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121 unlet g:imported_abs
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002122 unlet g:import_disassembled
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123
2124 delete('Ximport_abs.vim')
2125 delete('Xexport_abs.vim')
2126enddef
2127
2128def Test_import_rtp()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002129 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02002130 'vim9script',
2131 'import exported from "Xexport_rtp.vim"',
2132 'g:imported_rtp = exported',
2133 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 writefile(import_lines, 'Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002135 mkdir('import', 'p')
2136 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002138 var save_rtp = &rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002139 &rtp = getcwd()
2140 source Ximport_rtp.vim
2141 &rtp = save_rtp
2142
2143 assert_equal(9876, g:imported_rtp)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002145 Undo_export_script_lines()
2146 unlet g:imported_rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147 delete('Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002148 delete('import', 'rf')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149enddef
2150
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002151def Test_import_compile_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002152 var export_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002153 'vim9script',
2154 'export def ExpFunc(): string',
2155 ' return notDefined',
2156 'enddef',
2157 ]
2158 writefile(export_lines, 'Xexported.vim')
2159
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002160 var import_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002161 'vim9script',
2162 'import ExpFunc from "./Xexported.vim"',
2163 'def ImpFunc()',
2164 ' echo ExpFunc()',
2165 'enddef',
2166 'defcompile',
2167 ]
2168 writefile(import_lines, 'Ximport.vim')
2169
2170 try
2171 source Ximport.vim
2172 catch /E1001/
Dominique Pelle923dce22021-11-21 11:36:04 +00002173 # Error should be before the Xexported.vim file.
Bram Moolenaar77072282020-09-16 17:55:40 +02002174 assert_match('E1001: Variable not found: notDefined', v:exception)
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002175 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
2176 endtry
2177
2178 delete('Xexported.vim')
2179 delete('Ximport.vim')
2180enddef
2181
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002182def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002183 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002184 'vim9script',
2185 'def Func()',
2186 ' eval [][0]',
2187 'enddef',
2188 'Func()',
2189 ]
2190 writefile(lines, 'Xtestscript.vim')
2191
2192 for count in range(3)
2193 try
2194 source Xtestscript.vim
2195 catch /E684/
2196 # function name should contain <SNR> every time
2197 assert_match('E684: list index out of range', v:exception)
2198 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
2199 endtry
2200 endfor
2201
2202 delete('Xtestscript.vim')
2203enddef
2204
Bram Moolenaareef21022020-08-01 22:16:43 +02002205def Test_func_overrules_import_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002206 var export_lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002207 vim9script
2208 export def Func()
2209 echo 'imported'
2210 enddef
2211 END
2212 writefile(export_lines, 'XexportedFunc.vim')
2213
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002214 var lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002215 vim9script
2216 import Func from './XexportedFunc.vim'
2217 def Func()
2218 echo 'local to function'
2219 enddef
2220 END
Bram Moolenaar052ff292021-12-11 13:54:46 +00002221 CheckScriptFailure(lines, 'E1041:')
Bram Moolenaareef21022020-08-01 22:16:43 +02002222
2223 lines =<< trim END
2224 vim9script
2225 import Func from './XexportedFunc.vim'
2226 def Outer()
2227 def Func()
2228 echo 'local to function'
2229 enddef
2230 enddef
2231 defcompile
2232 END
2233 CheckScriptFailure(lines, 'E1073:')
2234
2235 delete('XexportedFunc.vim')
2236enddef
2237
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002238def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002239 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002240 vim9script
2241 def Func()
2242 echo 'one'
2243 enddef
2244 def Func()
2245 echo 'two'
2246 enddef
2247 END
2248 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002249
2250 lines =<< trim END
2251 vim9script
2252 def Foo(): string
2253 return 'foo'
Bram Moolenaar052ff292021-12-11 13:54:46 +00002254 enddef
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002255 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002256 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002257 enddef
2258 defcompile
2259 END
2260 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002261enddef
2262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002264 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002265 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 l->remove(0)
2267 l->add(5)
2268 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002269 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270enddef
2271
Bram Moolenaarae616492020-07-28 20:07:27 +02002272def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002273 CheckDefExecFailure(['a = 1'], 'E1100:')
2274 CheckDefExecFailure(['c = 1'], 'E1100:')
2275 CheckDefExecFailure(['i = 1'], 'E1100:')
2276 CheckDefExecFailure(['t = 1'], 'E1100:')
2277 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002278
Bram Moolenaarae616492020-07-28 20:07:27 +02002279 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
2280 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002281 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
2282 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002283 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
2284 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01002285 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
2286 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002287 CheckScriptFailure(['vim9script', 't'], 'E1100:')
2288 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
2289 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002290enddef
2291
Bram Moolenaar158906c2020-02-06 20:39:45 +01002292def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002293 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01002294 if what == 1
2295 res = "one"
2296 elseif what == 2
2297 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002298 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01002299 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002300 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01002301 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002302enddef
2303
Bram Moolenaar158906c2020-02-06 20:39:45 +01002304def Test_if_elseif_else()
2305 assert_equal('one', IfElse(1))
2306 assert_equal('two', IfElse(2))
2307 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002308enddef
2309
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002310def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002311 CheckDefFailure(['elseif true'], 'E582:')
2312 CheckDefFailure(['else'], 'E581:')
2313 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01002314 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002315 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002316
2317 var lines =<< trim END
2318 var s = ''
2319 if s = ''
2320 endif
2321 END
2322 CheckDefFailure(lines, 'E488:')
2323
2324 lines =<< trim END
2325 var s = ''
2326 if s == ''
2327 elseif s = ''
2328 endif
2329 END
2330 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002331enddef
2332
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002333let g:bool_true = v:true
2334let g:bool_false = v:false
2335
2336def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002337 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002338 if true ? true : false
2339 res = true
2340 endif
2341 assert_equal(true, res)
2342
Bram Moolenaar585fea72020-04-02 22:33:21 +02002343 g:glob = 2
2344 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02002345 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002346 endif
2347 assert_equal(2, g:glob)
2348 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02002349 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002350 endif
2351 assert_equal(3, g:glob)
2352
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002353 res = false
2354 if g:bool_true ? true : false
2355 res = true
2356 endif
2357 assert_equal(true, res)
2358
2359 res = false
2360 if true ? g:bool_true : false
2361 res = true
2362 endif
2363 assert_equal(true, res)
2364
2365 res = false
2366 if true ? true : g:bool_false
2367 res = true
2368 endif
2369 assert_equal(true, res)
2370
2371 res = false
2372 if true ? false : true
2373 res = true
2374 endif
2375 assert_equal(false, res)
2376
2377 res = false
2378 if false ? false : true
2379 res = true
2380 endif
2381 assert_equal(true, res)
2382
2383 res = false
2384 if false ? true : false
2385 res = true
2386 endif
2387 assert_equal(false, res)
2388
2389 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002390 if has('xyz') ? true : false
2391 res = true
2392 endif
2393 assert_equal(false, res)
2394
2395 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002396 if true && true
2397 res = true
2398 endif
2399 assert_equal(true, res)
2400
2401 res = false
2402 if true && false
2403 res = true
2404 endif
2405 assert_equal(false, res)
2406
2407 res = false
2408 if g:bool_true && false
2409 res = true
2410 endif
2411 assert_equal(false, res)
2412
2413 res = false
2414 if true && g:bool_false
2415 res = true
2416 endif
2417 assert_equal(false, res)
2418
2419 res = false
2420 if false && false
2421 res = true
2422 endif
2423 assert_equal(false, res)
2424
2425 res = false
2426 if true || false
2427 res = true
2428 endif
2429 assert_equal(true, res)
2430
2431 res = false
2432 if g:bool_true || false
2433 res = true
2434 endif
2435 assert_equal(true, res)
2436
2437 res = false
2438 if true || g:bool_false
2439 res = true
2440 endif
2441 assert_equal(true, res)
2442
2443 res = false
2444 if false || false
2445 res = true
2446 endif
2447 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02002448
2449 # with constant "false" expression may be invalid so long as the syntax is OK
Bram Moolenaarc3235272021-07-10 19:42:03 +02002450 if false | eval 1 + 2 | endif
Bram Moolenaar3988f642020-08-27 22:43:03 +02002451 if false | eval burp + 234 | endif
2452 if false | echo burp 234 'asd' | endif
2453 if false
2454 burp
2455 endif
Bram Moolenaare525bdd2021-08-07 18:12:40 +02002456
2457 # expression with line breaks skipped
2458 if false
2459 ('aaa'
2460 .. 'bbb'
2461 .. 'ccc'
2462 )->setline(1)
2463 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002464enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002465
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002466def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002467 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
2468 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
2469 CheckDefFailure(["if has('aaa'"], 'E110:')
2470 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002471enddef
2472
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002473def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002474 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002475 if i % 2
2476 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002477 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002478 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002479 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002480 endif
2481 x += 1
2482 else
2483 x += 1000
2484 endif
2485 return x
2486enddef
2487
2488def Test_nested_if()
2489 assert_equal(1, RunNested(1))
2490 assert_equal(1000, RunNested(2))
2491enddef
2492
Bram Moolenaarad39c092020-02-26 18:23:43 +01002493def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01002494 # missing argument is ignored
2495 execute
2496 execute # comment
2497
Bram Moolenaarad39c092020-02-26 18:23:43 +01002498 new
2499 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002500 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002501 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002502
Bram Moolenaard2c61702020-09-06 15:58:36 +02002503 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002504 assert_equal('execute-string', getline(1))
2505
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002506 var cmd1 = 'setline(1,'
2507 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002508 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01002509 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002510
Bram Moolenaard2c61702020-09-06 15:58:36 +02002511 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002512 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002513
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002514 var cmd_first = 'call '
2515 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002516 execute cmd_first .. cmd_last
2517 assert_equal('execute-var-var', getline(1))
2518 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02002519
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002520 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002521 execute 'echomsg' (n ? '"true"' : '"no"')
2522 assert_match('^true$', Screenline(&lines))
2523
Bram Moolenaare0de1712020-12-02 17:36:54 +01002524 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002525 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
2526
Bram Moolenaard2c61702020-09-06 15:58:36 +02002527 CheckDefFailure(['execute xxx'], 'E1001:', 1)
2528 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
2529 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002530enddef
2531
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002532def Test_execute_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002533 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002534 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002535 vim9script
2536 execute 'g:someVar'
2537 .. ' = ' ..
2538 '28'
2539 assert_equal(28, g:someVar)
2540 unlet g:someVar
2541 END
2542 CheckScriptSuccess(lines)
2543enddef
2544
Bram Moolenaarad39c092020-02-26 18:23:43 +01002545def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002546 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02002547 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002548 assert_match('^something$', Screenline(&lines))
2549
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002550 echo "some" # comment
2551 echon "thing"
2552 assert_match('^something$', Screenline(&lines))
2553
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002554 var str1 = 'some'
2555 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002556 echo str1 str2
2557 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002558
Bram Moolenaard2c61702020-09-06 15:58:36 +02002559 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01002560enddef
2561
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002562def Test_echomsg_cmd()
2563 echomsg 'some' 'more' # comment
2564 assert_match('^some more$', Screenline(&lines))
2565 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002566 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002567 assert_match('^some more$', Screenline(&lines))
2568
Bram Moolenaard2c61702020-09-06 15:58:36 +02002569 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002570enddef
2571
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002572def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002573 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002574 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002575 vim9script
2576 echomsg 'here'
2577 .. ' is ' ..
2578 'a message'
2579 assert_match('^here is a message$', Screenline(&lines))
2580 END
2581 CheckScriptSuccess(lines)
2582enddef
2583
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002584def Test_echoerr_cmd()
Bram Moolenaar7de62622021-08-07 15:05:47 +02002585 var local = 'local'
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002586 try
Bram Moolenaar7de62622021-08-07 15:05:47 +02002587 echoerr 'something' local 'wrong' # comment
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002588 catch
Bram Moolenaar7de62622021-08-07 15:05:47 +02002589 assert_match('something local wrong', v:exception)
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002590 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002591enddef
2592
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002593def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002594 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002595 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002596 vim9script
2597 try
2598 echoerr 'this'
2599 .. ' is ' ..
2600 'wrong'
2601 catch
2602 assert_match('this is wrong', v:exception)
2603 endtry
2604 END
2605 CheckScriptSuccess(lines)
2606enddef
2607
Bram Moolenaar7de62622021-08-07 15:05:47 +02002608def Test_echoconsole_cmd()
2609 var local = 'local'
2610 echoconsole 'something' local # comment
2611 # output goes anywhere
2612enddef
2613
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002614def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002615 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002616 vim9script
2617 new
2618 for var in range(0, 3)
2619 append(line('$'), var)
2620 endfor
2621 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2622 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01002623
2624 var result = ''
2625 for i in [1, 2, 3]
2626 var loop = ' loop ' .. i
2627 result ..= loop
2628 endfor
2629 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002630 END
2631 writefile(lines, 'Xvim9for.vim')
2632 source Xvim9for.vim
2633 delete('Xvim9for.vim')
2634enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635
rbtnnbebf0692021-08-21 17:26:50 +02002636def Test_for_skipped_block()
2637 # test skipped blocks at outside of function
2638 var lines =<< trim END
2639 var result = []
2640 if true
2641 for n in [1, 2]
2642 result += [n]
2643 endfor
2644 else
2645 for n in [3, 4]
2646 result += [n]
2647 endfor
2648 endif
2649 assert_equal([1, 2], result)
2650
2651 result = []
2652 if false
2653 for n in [1, 2]
2654 result += [n]
2655 endfor
2656 else
2657 for n in [3, 4]
2658 result += [n]
2659 endfor
2660 endif
2661 assert_equal([3, 4], result)
2662 END
2663 CheckDefAndScriptSuccess(lines)
2664
2665 # test skipped blocks at inside of function
2666 lines =<< trim END
2667 def DefTrue()
2668 var result = []
2669 if true
2670 for n in [1, 2]
2671 result += [n]
2672 endfor
2673 else
2674 for n in [3, 4]
2675 result += [n]
2676 endfor
2677 endif
2678 assert_equal([1, 2], result)
2679 enddef
2680 DefTrue()
2681
2682 def DefFalse()
2683 var result = []
2684 if false
2685 for n in [1, 2]
2686 result += [n]
2687 endfor
2688 else
2689 for n in [3, 4]
2690 result += [n]
2691 endfor
2692 endif
2693 assert_equal([3, 4], result)
2694 enddef
2695 DefFalse()
2696 END
2697 CheckDefAndScriptSuccess(lines)
2698enddef
2699
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002700def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002701 var lines =<< trim END
2702 var result = ''
2703 for cnt in range(7)
2704 if cnt == 4
2705 break
2706 endif
2707 if cnt == 2
2708 continue
2709 endif
2710 result ..= cnt .. '_'
2711 endfor
2712 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02002713
Bram Moolenaarf2253962021-04-13 20:53:13 +02002714 var concat = ''
2715 for str in eval('["one", "two"]')
2716 concat ..= str
2717 endfor
2718 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002719
Bram Moolenaarf2253962021-04-13 20:53:13 +02002720 var total = 0
2721 for nr in
2722 [1, 2, 3]
2723 total += nr
2724 endfor
2725 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002726
Bram Moolenaarf2253962021-04-13 20:53:13 +02002727 total = 0
2728 for nr
2729 in [1, 2, 3]
2730 total += nr
2731 endfor
2732 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002733
Bram Moolenaarf2253962021-04-13 20:53:13 +02002734 total = 0
2735 for nr
2736 in
2737 [1, 2, 3]
2738 total += nr
2739 endfor
2740 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01002741
Bram Moolenaara3589a02021-04-14 13:30:46 +02002742 # with type
2743 total = 0
2744 for n: number in [1, 2, 3]
2745 total += n
2746 endfor
2747 assert_equal(6, total)
2748
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002749 var chars = ''
2750 for s: string in 'foobar'
2751 chars ..= s
2752 endfor
2753 assert_equal('foobar', chars)
2754
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02002755 chars = ''
2756 for x: string in {a: 'a', b: 'b'}->values()
2757 chars ..= x
2758 endfor
2759 assert_equal('ab', chars)
2760
Bram Moolenaara3589a02021-04-14 13:30:46 +02002761 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02002762 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02002763 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2764 res ..= n .. s
2765 endfor
2766 assert_equal('1a2b', res)
2767
Bram Moolenaar444d8782021-06-26 12:40:56 +02002768 # unpack with one var
2769 var reslist = []
2770 for [x] in [['aaa'], ['bbb']]
2771 reslist->add(x)
2772 endfor
2773 assert_equal(['aaa', 'bbb'], reslist)
2774
Bram Moolenaara3589a02021-04-14 13:30:46 +02002775 # loop over string
2776 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02002777 for c in 'aéc̀d'
2778 res ..= c .. '-'
2779 endfor
2780 assert_equal('a-é-c̀-d-', res)
2781
2782 res = ''
2783 for c in ''
2784 res ..= c .. '-'
2785 endfor
2786 assert_equal('', res)
2787
2788 res = ''
2789 for c in test_null_string()
2790 res ..= c .. '-'
2791 endfor
2792 assert_equal('', res)
2793
2794 var foo: list<dict<any>> = [
2795 {a: 'Cat'}
2796 ]
2797 for dd in foo
2798 dd.counter = 12
2799 endfor
2800 assert_equal([{a: 'Cat', counter: 12}], foo)
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002801
2802 reslist = []
2803 for _ in range(3)
2804 reslist->add('x')
2805 endfor
2806 assert_equal(['x', 'x', 'x'], reslist)
Bram Moolenaarf2253962021-04-13 20:53:13 +02002807 END
2808 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002809enddef
2810
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02002811def Test_for_loop_with_closure()
2812 var lines =<< trim END
2813 var flist: list<func>
2814 for i in range(5)
2815 var inloop = i
2816 flist[i] = () => inloop
2817 endfor
2818 for i in range(5)
2819 assert_equal(4, flist[i]())
2820 endfor
2821 END
2822 CheckDefAndScriptSuccess(lines)
2823
2824 lines =<< trim END
2825 var flist: list<func>
2826 for i in range(5)
2827 var inloop = i
2828 flist[i] = () => {
2829 return inloop
2830 }
2831 endfor
2832 for i in range(5)
2833 assert_equal(4, flist[i]())
2834 endfor
2835 END
2836 CheckDefAndScriptSuccess(lines)
2837enddef
2838
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002839def Test_for_loop_fails()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002840 CheckDefAndScriptFailure2(['for '], 'E1097:', 'E690:')
2841 CheckDefAndScriptFailure2(['for x'], 'E1097:', 'E690:')
2842 CheckDefAndScriptFailure2(['for x in'], 'E1097:', 'E15:')
2843 CheckDefAndScriptFailure(['for # in range(5)'], 'E690:')
2844 CheckDefAndScriptFailure(['for i In range(5)'], 'E690:')
2845 CheckDefAndScriptFailure2(['var x = 5', 'for x in range(5)', 'endfor'], 'E1017:', 'E1041:')
Bram Moolenaard4ab8072021-07-08 19:22:12 +02002846 CheckScriptFailure(['vim9script', 'var x = 5', 'for x in range(5)', '# comment', 'endfor'], 'E1041:', 3)
Bram Moolenaar822ba242020-05-24 23:00:18 +02002847 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002848 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02002849 CheckDefFailure(['for i in xxx'], 'E1001:')
2850 CheckDefFailure(['endfor'], 'E588:')
2851 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002852
2853 # wrong type detected at compile time
2854 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2855
2856 # wrong type detected at runtime
2857 g:adict = {a: 1}
2858 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2859 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002860
2861 var lines =<< trim END
2862 var d: list<dict<any>> = [{a: 0}]
2863 for e in d
2864 e = {a: 0, b: ''}
2865 endfor
2866 END
2867 CheckDefAndScriptFailure2(lines, 'E1018:', 'E46:', 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002868
2869 lines =<< trim END
2870 for nr: number in ['foo']
2871 endfor
2872 END
2873 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaar404557e2021-07-05 21:41:48 +02002874
2875 lines =<< trim END
2876 for n : number in [1, 2]
2877 echo n
2878 endfor
2879 END
2880 CheckDefAndScriptFailure(lines, 'E1059:', 1)
Bram Moolenaarefc5db52021-07-07 21:21:30 +02002881
2882 lines =<< trim END
2883 var d: dict<number> = {a: 1, b: 2}
2884 for [k: job, v: job] in d->items()
2885 echo k v
2886 endfor
2887 END
2888 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected job but got string', 2)
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002889
2890 lines =<< trim END
2891 var i = 0
2892 for i in [1, 2, 3]
2893 echo i
2894 endfor
2895 END
2896 CheckDefExecAndScriptFailure2(lines, 'E1017:', 'E1041:')
2897
2898 lines =<< trim END
2899 var l = [0]
2900 for l[0] in [1, 2, 3]
2901 echo l[0]
2902 endfor
2903 END
2904 CheckDefExecAndScriptFailure2(lines, 'E461:', 'E1017:')
2905
2906 lines =<< trim END
2907 var d = {x: 0}
2908 for d.x in [1, 2, 3]
2909 echo d.x
2910 endfor
2911 END
2912 CheckDefExecAndScriptFailure2(lines, 'E461:', 'E1017:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002913enddef
2914
Bram Moolenaarea870692020-12-02 14:24:30 +01002915def Test_for_loop_script_var()
2916 # cannot use s:var in a :def function
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002917 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E461:')
Bram Moolenaarea870692020-12-02 14:24:30 +01002918
2919 # can use s:var in Vim9 script, with or without s:
2920 var lines =<< trim END
2921 vim9script
2922 var total = 0
2923 for s:var in [1, 2, 3]
2924 total += s:var
2925 endfor
2926 assert_equal(6, total)
2927
2928 total = 0
2929 for var in [1, 2, 3]
2930 total += var
2931 endfor
2932 assert_equal(6, total)
2933 END
2934enddef
2935
Bram Moolenaar792f7862020-11-23 08:31:18 +01002936def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01002937 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002938 var result = []
2939 for [v1, v2] in [[1, 2], [3, 4]]
2940 result->add(v1)
2941 result->add(v2)
2942 endfor
2943 assert_equal([1, 2, 3, 4], result)
2944
2945 result = []
2946 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2947 result->add(v1)
2948 result->add(v2)
2949 result->add(v3)
2950 endfor
2951 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
2952
2953 result = []
2954 for [&ts, &sw] in [[1, 2], [3, 4]]
2955 result->add(&ts)
2956 result->add(&sw)
2957 endfor
2958 assert_equal([1, 2, 3, 4], result)
2959
2960 var slist: list<string>
2961 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
2962 slist->add($LOOPVAR)
2963 slist->add(@r)
2964 slist->add(v:errmsg)
2965 endfor
2966 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
2967
2968 slist = []
2969 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
2970 slist->add(g:globalvar)
2971 slist->add(b:bufvar)
2972 slist->add(w:winvar)
2973 slist->add(t:tabvar)
2974 endfor
2975 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01002976 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaarb777da92021-05-22 21:40:39 +02002977
2978 var res = []
2979 for [_, n, _] in [[1, 2, 3], [4, 5, 6]]
2980 res->add(n)
2981 endfor
2982 assert_equal([2, 5], res)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002983 END
2984 CheckDefAndScriptSuccess(lines)
2985
2986 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01002987 for [v1, v2] in [[1, 2, 3], [3, 4]]
2988 echo v1 v2
2989 endfor
2990 END
2991 CheckDefExecFailure(lines, 'E710:', 1)
2992
2993 lines =<< trim END
2994 for [v1, v2] in [[1], [3, 4]]
2995 echo v1 v2
2996 endfor
2997 END
2998 CheckDefExecFailure(lines, 'E711:', 1)
2999
3000 lines =<< trim END
3001 for [v1, v1] in [[1, 2], [3, 4]]
3002 echo v1
3003 endfor
3004 END
3005 CheckDefExecFailure(lines, 'E1017:', 1)
3006enddef
3007
Bram Moolenaarc150c092021-02-13 15:02:46 +01003008def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02003009 var lines =<< trim END
3010 var looped = 0
3011 var cleanup = 0
3012 for i in range(3)
3013 looped += 1
3014 try
3015 eval [][0]
3016 catch
3017 continue
3018 finally
3019 cleanup += 1
3020 endtry
3021 endfor
3022 assert_equal(3, looped)
3023 assert_equal(3, cleanup)
3024 END
3025 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003026enddef
3027
rbtnnd895b1d2021-08-20 20:54:25 +02003028def Test_while_skipped_block()
3029 # test skipped blocks at outside of function
3030 var lines =<< trim END
3031 var result = []
3032 var n = 0
3033 if true
3034 n = 1
3035 while n < 3
3036 result += [n]
3037 n += 1
3038 endwhile
3039 else
3040 n = 3
3041 while n < 5
3042 result += [n]
3043 n += 1
3044 endwhile
3045 endif
3046 assert_equal([1, 2], result)
3047
3048 result = []
3049 if false
3050 n = 1
3051 while n < 3
3052 result += [n]
3053 n += 1
3054 endwhile
3055 else
3056 n = 3
3057 while n < 5
3058 result += [n]
3059 n += 1
3060 endwhile
3061 endif
3062 assert_equal([3, 4], result)
3063 END
3064 CheckDefAndScriptSuccess(lines)
3065
3066 # test skipped blocks at inside of function
3067 lines =<< trim END
3068 def DefTrue()
3069 var result = []
3070 var n = 0
3071 if true
3072 n = 1
3073 while n < 3
3074 result += [n]
3075 n += 1
3076 endwhile
3077 else
3078 n = 3
3079 while n < 5
3080 result += [n]
3081 n += 1
3082 endwhile
3083 endif
3084 assert_equal([1, 2], result)
3085 enddef
3086 DefTrue()
3087
3088 def DefFalse()
3089 var result = []
3090 var n = 0
3091 if false
3092 n = 1
3093 while n < 3
3094 result += [n]
3095 n += 1
3096 endwhile
3097 else
3098 n = 3
3099 while n < 5
3100 result += [n]
3101 n += 1
3102 endwhile
3103 endif
3104 assert_equal([3, 4], result)
3105 enddef
3106 DefFalse()
3107 END
3108 CheckDefAndScriptSuccess(lines)
3109enddef
3110
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003111def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003112 var result = ''
3113 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003114 while cnt < 555
3115 if cnt == 3
3116 break
3117 endif
3118 cnt += 1
3119 if cnt == 2
3120 continue
3121 endif
3122 result ..= cnt .. '_'
3123 endwhile
3124 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01003125
3126 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01003127 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01003128 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003129enddef
3130
Bram Moolenaar7a53f292021-11-22 18:31:02 +00003131def Test_while_loop_in_script()
3132 var lines =<< trim END
3133 vim9script
3134 var result = ''
3135 var cnt = 0
3136 while cnt < 3
3137 var s = 'v' .. cnt
3138 result ..= s
3139 cnt += 1
3140 endwhile
3141 assert_equal('v0v1v2', result)
3142 END
3143 CheckScriptSuccess(lines)
3144enddef
3145
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003146def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02003147 CheckDefFailure(['while xxx'], 'E1001:')
3148 CheckDefFailure(['endwhile'], 'E588:')
3149 CheckDefFailure(['continue'], 'E586:')
3150 CheckDefFailure(['if true', 'continue'], 'E586:')
3151 CheckDefFailure(['break'], 'E587:')
3152 CheckDefFailure(['if true', 'break'], 'E587:')
3153 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01003154
3155 var lines =<< trim END
3156 var s = ''
3157 while s = ''
3158 endwhile
3159 END
3160 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003161enddef
3162
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003163def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003164 var caught = false
3165 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003166 try
3167 while 1
3168 x += 1
3169 if x == 100
3170 feedkeys("\<C-C>", 'Lt')
3171 endif
3172 endwhile
3173 catch
3174 caught = true
3175 assert_equal(100, x)
3176 endtry
3177 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003178 # consume the CTRL-C
3179 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003180enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01003181
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003182def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003183 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003184 'one',
3185 'two',
3186 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003187 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003188 assert_equal(['one', 'two', 'three'], mylist)
3189
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003190 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003191 ['one']: 1,
3192 ['two']: 2,
3193 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003194 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003195 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01003196 assert_equal({one: 1, two: 2, three: 3}, mydict)
3197 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003198 one: 1, # comment
3199 two: # comment
3200 2, # comment
3201 three: 3 # comment
3202 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003203 assert_equal({one: 1, two: 2, three: 3}, mydict)
3204 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003205 one: 1,
3206 two:
3207 2,
3208 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003209 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003210 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02003211
3212 assert_equal(
3213 ['one', 'two', 'three'],
3214 split('one two three')
3215 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003216enddef
3217
Bram Moolenaar7a092242020-04-16 22:10:49 +02003218def Test_vim9_comment()
3219 CheckScriptSuccess([
3220 'vim9script',
3221 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01003222 '#something',
3223 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003224 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01003225
3226 split Xfile
3227 CheckScriptSuccess([
3228 'vim9script',
3229 'edit #something',
3230 ])
3231 CheckScriptSuccess([
3232 'vim9script',
3233 'edit #{something',
3234 ])
3235 close
3236
Bram Moolenaar7a092242020-04-16 22:10:49 +02003237 CheckScriptFailure([
3238 'vim9script',
3239 ':# something',
3240 ], 'E488:')
3241 CheckScriptFailure([
3242 '# something',
3243 ], 'E488:')
3244 CheckScriptFailure([
3245 ':# something',
3246 ], 'E488:')
3247
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003248 { # block start
3249 } # block end
3250 CheckDefFailure([
3251 '{# comment',
3252 ], 'E488:')
3253 CheckDefFailure([
3254 '{',
3255 '}# comment',
3256 ], 'E488:')
3257
3258 echo "yes" # comment
3259 CheckDefFailure([
3260 'echo "yes"# comment',
3261 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003262 CheckScriptSuccess([
3263 'vim9script',
3264 'echo "yes" # something',
3265 ])
3266 CheckScriptFailure([
3267 'vim9script',
3268 'echo "yes"# something',
3269 ], 'E121:')
3270 CheckScriptFailure([
3271 'vim9script',
3272 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003273 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003274 CheckScriptFailure([
3275 'echo "yes" # something',
3276 ], 'E121:')
3277
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003278 exe "echo" # comment
3279 CheckDefFailure([
3280 'exe "echo"# comment',
3281 ], 'E488:')
3282 CheckScriptSuccess([
3283 'vim9script',
3284 'exe "echo" # something',
3285 ])
3286 CheckScriptFailure([
3287 'vim9script',
3288 'exe "echo"# something',
3289 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003290 CheckScriptFailure([
3291 'vim9script',
3292 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003293 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003294 CheckScriptFailure([
3295 'exe "echo" # something',
3296 ], 'E121:')
3297
Bram Moolenaar7a092242020-04-16 22:10:49 +02003298 CheckDefFailure([
3299 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003300 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003301 'catch',
3302 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003303 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003304 CheckScriptFailure([
3305 'vim9script',
3306 'try# comment',
3307 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003308 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003309 CheckDefFailure([
3310 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003311 ' throw#comment',
3312 'catch',
3313 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003314 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003315 CheckDefFailure([
3316 'try',
3317 ' throw "yes"#comment',
3318 'catch',
3319 'endtry',
3320 ], 'E488:')
3321 CheckDefFailure([
3322 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003323 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003324 'catch# comment',
3325 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003326 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003327 CheckScriptFailure([
3328 'vim9script',
3329 'try',
3330 ' echo "yes"',
3331 'catch# comment',
3332 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003333 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003334 CheckDefFailure([
3335 'try',
3336 ' echo "yes"',
3337 'catch /pat/# comment',
3338 'endtry',
3339 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003340 CheckDefFailure([
3341 'try',
3342 'echo "yes"',
3343 'catch',
3344 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003345 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003346 CheckScriptFailure([
3347 'vim9script',
3348 'try',
3349 ' echo "yes"',
3350 'catch',
3351 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003352 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003353
3354 CheckScriptSuccess([
3355 'vim9script',
3356 'hi # comment',
3357 ])
3358 CheckScriptFailure([
3359 'vim9script',
3360 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003361 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003362 CheckScriptSuccess([
3363 'vim9script',
3364 'hi Search # comment',
3365 ])
3366 CheckScriptFailure([
3367 'vim9script',
3368 'hi Search# comment',
3369 ], 'E416:')
3370 CheckScriptSuccess([
3371 'vim9script',
3372 'hi link This Search # comment',
3373 ])
3374 CheckScriptFailure([
3375 'vim9script',
3376 'hi link This That# comment',
3377 ], 'E413:')
3378 CheckScriptSuccess([
3379 'vim9script',
3380 'hi clear This # comment',
3381 'hi clear # comment',
3382 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003383 # not tested, because it doesn't give an error but a warning:
3384 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02003385 CheckScriptFailure([
3386 'vim9script',
3387 'hi clear# comment',
3388 ], 'E416:')
3389
3390 CheckScriptSuccess([
3391 'vim9script',
3392 'hi Group term=bold',
3393 'match Group /todo/ # comment',
3394 ])
3395 CheckScriptFailure([
3396 'vim9script',
3397 'hi Group term=bold',
3398 'match Group /todo/# comment',
3399 ], 'E488:')
3400 CheckScriptSuccess([
3401 'vim9script',
3402 'match # comment',
3403 ])
3404 CheckScriptFailure([
3405 'vim9script',
3406 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003407 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003408 CheckScriptSuccess([
3409 'vim9script',
3410 'match none # comment',
3411 ])
3412 CheckScriptFailure([
3413 'vim9script',
3414 'match none# comment',
3415 ], 'E475:')
3416
3417 CheckScriptSuccess([
3418 'vim9script',
3419 'menutrans clear # comment',
3420 ])
3421 CheckScriptFailure([
3422 'vim9script',
3423 'menutrans clear# comment text',
3424 ], 'E474:')
3425
3426 CheckScriptSuccess([
3427 'vim9script',
3428 'syntax clear # comment',
3429 ])
3430 CheckScriptFailure([
3431 'vim9script',
3432 'syntax clear# comment text',
3433 ], 'E28:')
3434 CheckScriptSuccess([
3435 'vim9script',
3436 'syntax keyword Word some',
3437 'syntax clear Word # comment',
3438 ])
3439 CheckScriptFailure([
3440 'vim9script',
3441 'syntax keyword Word some',
3442 'syntax clear Word# comment text',
3443 ], 'E28:')
3444
3445 CheckScriptSuccess([
3446 'vim9script',
3447 'syntax list # comment',
3448 ])
3449 CheckScriptFailure([
3450 'vim9script',
3451 'syntax list# comment text',
3452 ], 'E28:')
3453
3454 CheckScriptSuccess([
3455 'vim9script',
3456 'syntax match Word /pat/ oneline # comment',
3457 ])
3458 CheckScriptFailure([
3459 'vim9script',
3460 'syntax match Word /pat/ oneline# comment',
3461 ], 'E475:')
3462
3463 CheckScriptSuccess([
3464 'vim9script',
3465 'syntax keyword Word word # comm[ent',
3466 ])
3467 CheckScriptFailure([
3468 'vim9script',
3469 'syntax keyword Word word# comm[ent',
3470 ], 'E789:')
3471
3472 CheckScriptSuccess([
3473 'vim9script',
3474 'syntax match Word /pat/ # comment',
3475 ])
3476 CheckScriptFailure([
3477 'vim9script',
3478 'syntax match Word /pat/# comment',
3479 ], 'E402:')
3480
3481 CheckScriptSuccess([
3482 'vim9script',
3483 'syntax match Word /pat/ contains=Something # comment',
3484 ])
3485 CheckScriptFailure([
3486 'vim9script',
3487 'syntax match Word /pat/ contains=Something# comment',
3488 ], 'E475:')
3489 CheckScriptFailure([
3490 'vim9script',
3491 'syntax match Word /pat/ contains= # comment',
3492 ], 'E406:')
3493 CheckScriptFailure([
3494 'vim9script',
3495 'syntax match Word /pat/ contains=# comment',
3496 ], 'E475:')
3497
3498 CheckScriptSuccess([
3499 'vim9script',
3500 'syntax region Word start=/pat/ end=/pat/ # comment',
3501 ])
3502 CheckScriptFailure([
3503 'vim9script',
3504 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02003505 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003506
3507 CheckScriptSuccess([
3508 'vim9script',
3509 'syntax sync # comment',
3510 ])
3511 CheckScriptFailure([
3512 'vim9script',
3513 'syntax sync# comment',
3514 ], 'E404:')
3515 CheckScriptSuccess([
3516 'vim9script',
3517 'syntax sync ccomment # comment',
3518 ])
3519 CheckScriptFailure([
3520 'vim9script',
3521 'syntax sync ccomment# comment',
3522 ], 'E404:')
3523
3524 CheckScriptSuccess([
3525 'vim9script',
3526 'syntax cluster Some contains=Word # comment',
3527 ])
3528 CheckScriptFailure([
3529 'vim9script',
3530 'syntax cluster Some contains=Word# comment',
3531 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003532
3533 CheckScriptSuccess([
3534 'vim9script',
3535 'command Echo echo # comment',
3536 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003537 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003538 ])
3539 CheckScriptFailure([
3540 'vim9script',
3541 'command Echo echo# comment',
3542 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003543 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003544 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003545
3546 var curdir = getcwd()
3547 CheckScriptSuccess([
3548 'command Echo cd " comment',
3549 'Echo',
3550 'delcommand Echo',
3551 ])
3552 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01003553 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003554 'command Echo cd # comment',
3555 'Echo',
3556 'delcommand Echo',
3557 ])
3558 CheckScriptFailure([
3559 'vim9script',
3560 'command Echo cd " comment',
3561 'Echo',
3562 ], 'E344:')
3563 delcommand Echo
3564 chdir(curdir)
3565
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003566 CheckScriptFailure([
3567 'vim9script',
3568 'command Echo# comment',
3569 ], 'E182:')
3570 CheckScriptFailure([
3571 'vim9script',
3572 'command Echo echo',
3573 'command Echo# comment',
3574 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003575 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003576
3577 CheckScriptSuccess([
3578 'vim9script',
3579 'function # comment',
3580 ])
3581 CheckScriptFailure([
3582 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02003583 'function " comment',
3584 ], 'E129:')
3585 CheckScriptFailure([
3586 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003587 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003588 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003589 CheckScriptSuccess([
3590 'vim9script',
3591 'function CheckScriptSuccess # comment',
3592 ])
3593 CheckScriptFailure([
3594 'vim9script',
3595 'function CheckScriptSuccess# comment',
3596 ], 'E488:')
3597
3598 CheckScriptSuccess([
3599 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003600 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003601 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003602 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003603 ])
3604 CheckScriptFailure([
3605 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003606 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003607 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003608 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003609 ], 'E488:')
3610
3611 CheckScriptSuccess([
3612 'vim9script',
3613 'call execute("ls") # comment',
3614 ])
3615 CheckScriptFailure([
3616 'vim9script',
3617 'call execute("ls")# comment',
3618 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02003619
3620 CheckScriptFailure([
3621 'def Test() " comment',
3622 'enddef',
3623 ], 'E488:')
3624 CheckScriptFailure([
3625 'vim9script',
3626 'def Test() " comment',
3627 'enddef',
3628 ], 'E488:')
3629
3630 CheckScriptSuccess([
3631 'func Test() " comment',
3632 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003633 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02003634 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02003635 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02003636 'vim9script',
3637 'func Test() " comment',
3638 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02003639 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02003640
3641 CheckScriptSuccess([
3642 'def Test() # comment',
3643 'enddef',
3644 ])
3645 CheckScriptFailure([
3646 'func Test() # comment',
3647 'endfunc',
3648 ], 'E488:')
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003649
3650 var lines =<< trim END
3651 vim9script
3652 syn region Text
3653 \ start='foo'
3654 #\ comment
3655 \ end='bar'
Bram Moolenaar5072b472021-06-03 21:56:10 +02003656 syn region Text start='foo'
3657 #\ comment
3658 \ end='bar'
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003659 END
3660 CheckScriptSuccess(lines)
3661
3662 lines =<< trim END
3663 vim9script
3664 syn region Text
3665 \ start='foo'
3666 "\ comment
3667 \ end='bar'
3668 END
3669 CheckScriptFailure(lines, 'E399:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003670enddef
3671
3672def Test_vim9_comment_gui()
3673 CheckCanRunGui
3674
3675 CheckScriptFailure([
3676 'vim9script',
3677 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003678 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003679 CheckScriptFailure([
3680 'vim9script',
3681 'gui -f#comment'
Bram Moolenaarb8bd2e62021-08-21 17:13:14 +02003682 ], 'E194:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003683enddef
3684
Bram Moolenaara26b9702020-04-18 19:53:28 +02003685def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02003686 au TabEnter *.vim g:entered = 1
3687 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02003688
3689 edit test.vim
3690 doautocmd TabEnter #comment
3691 assert_equal(1, g:entered)
3692
3693 doautocmd TabEnter f.x
3694 assert_equal(2, g:entered)
3695
3696 g:entered = 0
3697 doautocmd TabEnter f.x #comment
3698 assert_equal(2, g:entered)
3699
3700 assert_fails('doautocmd Syntax#comment', 'E216:')
3701
3702 au! TabEnter
3703 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003704
3705 CheckScriptSuccess([
3706 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02003707 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003708 'b:var = 456',
3709 'w:var = 777',
3710 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003711 'unlet g:var w:var # something',
3712 ])
3713
3714 CheckScriptFailure([
3715 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003716 'let var = 123',
3717 ], 'E1126: Cannot use :let in Vim9 script')
3718
3719 CheckScriptFailure([
3720 'vim9script',
3721 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003722 ], 'E1016: Cannot declare a global variable:')
3723
3724 CheckScriptFailure([
3725 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003726 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003727 ], 'E1016: Cannot declare a buffer variable:')
3728
3729 CheckScriptFailure([
3730 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003731 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003732 ], 'E1016: Cannot declare a window variable:')
3733
3734 CheckScriptFailure([
3735 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003736 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003737 ], 'E1016: Cannot declare a tab variable:')
3738
3739 CheckScriptFailure([
3740 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003741 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003742 ], 'E1016: Cannot declare a v: variable:')
3743
3744 CheckScriptFailure([
3745 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003746 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003747 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02003748
3749 CheckScriptFailure([
3750 'vim9script',
3751 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003752 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003753 ], 'E108:')
3754
3755 CheckScriptFailure([
3756 'let g:var = 123',
3757 'unlet g:var # something',
3758 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003759
3760 CheckScriptSuccess([
3761 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003762 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003763 ' echo "yes"',
3764 'elseif 2 #comment',
3765 ' echo "no"',
3766 'endif',
3767 ])
3768
3769 CheckScriptFailure([
3770 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003771 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003772 ' echo "yes"',
3773 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003774 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003775
3776 CheckScriptFailure([
3777 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003778 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003779 ' echo "yes"',
3780 'elseif 2#comment',
3781 ' echo "no"',
3782 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003783 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003784
3785 CheckScriptSuccess([
3786 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003787 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003788 ])
3789
3790 CheckScriptFailure([
3791 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003792 'var v = 1# comment6',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003793 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003794
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003795 CheckScriptSuccess([
3796 'vim9script',
3797 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003798 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003799 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003800 'dsearch /pat/ #comment',
3801 'bwipe!',
3802 ])
3803
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003804 CheckScriptFailure([
3805 'vim9script',
3806 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003807 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003808 ':$',
3809 'dsearch /pat/#comment',
3810 'bwipe!',
3811 ], 'E488:')
3812
3813 CheckScriptFailure([
3814 'vim9script',
3815 'func! SomeFunc()',
3816 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02003817enddef
3818
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003819def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003820 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003821 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02003822 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003823 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02003824 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003825 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02003826 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003827 END
3828 writefile(lines, 'Xfinished')
3829 source Xfinished
3830 assert_equal('two', g:res)
3831
3832 unlet g:res
3833 delete('Xfinished')
3834enddef
3835
Bram Moolenaara5d00772020-05-14 23:20:55 +02003836def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003837 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02003838 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02003839 def GetValue(): string
3840 return theVal
3841 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003842 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02003843 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02003844 theVal = 'else'
3845 g:laterVal = GetValue()
3846 END
3847 writefile(lines, 'Xforward')
3848 source Xforward
3849 assert_equal('something', g:initVal)
3850 assert_equal('else', g:laterVal)
3851
3852 unlet g:initVal
3853 unlet g:laterVal
3854 delete('Xforward')
3855enddef
3856
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003857def Test_source_vim9_from_legacy()
Bram Moolenaara6294952020-12-27 13:39:50 +01003858 var vim9_lines =<< trim END
3859 vim9script
3860 var local = 'local'
3861 g:global = 'global'
3862 export var exported = 'exported'
3863 export def GetText(): string
3864 return 'text'
3865 enddef
3866 END
3867 writefile(vim9_lines, 'Xvim9_script.vim')
3868
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003869 var legacy_lines =<< trim END
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003870 source Xvim9_script.vim
3871
3872 call assert_false(exists('local'))
3873 call assert_false(exists('exported'))
3874 call assert_false(exists('s:exported'))
3875 call assert_equal('global', global)
3876 call assert_equal('global', g:global)
3877
3878 " imported variable becomes script-local
3879 import exported from './Xvim9_script.vim'
3880 call assert_equal('exported', s:exported)
3881 call assert_false(exists('exported'))
3882
3883 " imported function becomes script-local
3884 import GetText from './Xvim9_script.vim'
3885 call assert_equal('text', s:GetText())
3886 call assert_false(exists('*GetText'))
3887 END
3888 writefile(legacy_lines, 'Xlegacy_script.vim')
3889
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003890 source Xlegacy_script.vim
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003891 assert_equal('global', g:global)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003892 unlet g:global
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003893
3894 delete('Xlegacy_script.vim')
3895 delete('Xvim9_script.vim')
3896enddef
Bram Moolenaara5d00772020-05-14 23:20:55 +02003897
Bram Moolenaare535db82021-03-31 21:07:24 +02003898def Test_declare_script_in_func()
3899 var lines =<< trim END
3900 vim9script
3901 func Declare()
3902 let s:local = 123
3903 endfunc
3904 Declare()
3905 assert_equal(123, local)
3906
3907 var error: string
3908 try
3909 local = 'asdf'
3910 catch
3911 error = v:exception
3912 endtry
3913 assert_match('E1012: Type mismatch; expected number but got string', error)
3914
3915 lockvar local
3916 try
3917 local = 999
3918 catch
3919 error = v:exception
3920 endtry
3921 assert_match('E741: Value is locked: local', error)
3922 END
3923 CheckScriptSuccess(lines)
3924enddef
3925
3926
Bram Moolenaar7d699702020-08-14 20:52:28 +02003927func Test_vim9script_not_global()
3928 " check that items defined in Vim9 script are script-local, not global
3929 let vim9lines =<< trim END
3930 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003931 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02003932 func TheFunc()
3933 echo 'local'
3934 endfunc
3935 def DefFunc()
3936 echo 'local'
3937 enddef
3938 END
3939 call writefile(vim9lines, 'Xvim9script.vim')
3940 source Xvim9script.vim
3941 try
3942 echo g:var
3943 assert_report('did not fail')
3944 catch /E121:/
3945 " caught
3946 endtry
3947 try
3948 call TheFunc()
3949 assert_report('did not fail')
3950 catch /E117:/
3951 " caught
3952 endtry
3953 try
3954 call DefFunc()
3955 assert_report('did not fail')
3956 catch /E117:/
3957 " caught
3958 endtry
3959
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003960 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02003961endfunc
3962
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02003963def Test_vim9_copen()
3964 # this was giving an error for setting w:quickfix_title
3965 copen
3966 quit
3967enddef
3968
Bram Moolenaar03290b82020-12-19 16:30:44 +01003969" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003970def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003971 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003972 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01003973 def some#gettest(): string
3974 return 'test'
3975 enddef
3976 g:some#name = 'name'
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003977 g:some#dict = {key: 'value'}
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003978
3979 def some#varargs(a1: string, ...l: list<string>): string
3980 return a1 .. l[0] .. l[1]
3981 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01003982 END
3983
3984 mkdir('Xdir/autoload', 'p')
3985 writefile(lines, 'Xdir/autoload/some.vim')
3986 var save_rtp = &rtp
3987 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3988
3989 assert_equal('test', g:some#gettest())
3990 assert_equal('name', g:some#name)
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003991 assert_equal('value', g:some#dict.key)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003992 g:some#other = 'other'
3993 assert_equal('other', g:some#other)
3994
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003995 assert_equal('abc', some#varargs('a', 'b', 'c'))
3996
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003997 # upper case script name works
3998 lines =<< trim END
3999 vim9script
4000 def Other#getOther(): string
4001 return 'other'
4002 enddef
4003 END
4004 writefile(lines, 'Xdir/autoload/Other.vim')
4005 assert_equal('other', g:Other#getOther())
4006
Bram Moolenaar03290b82020-12-19 16:30:44 +01004007 delete('Xdir', 'rf')
4008 &rtp = save_rtp
4009enddef
4010
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00004011" test disassembling an auto-loaded function starting with "debug"
4012def Test_vim9_autoload_disass()
4013 mkdir('Xdir/autoload', 'p')
4014 var save_rtp = &rtp
4015 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4016
4017 var lines =<< trim END
4018 vim9script
4019 def debugit#test(): string
4020 return 'debug'
4021 enddef
4022 END
4023 writefile(lines, 'Xdir/autoload/debugit.vim')
4024
4025 lines =<< trim END
4026 vim9script
4027 def profileit#test(): string
4028 return 'profile'
4029 enddef
4030 END
4031 writefile(lines, 'Xdir/autoload/profileit.vim')
4032
4033 lines =<< trim END
4034 vim9script
4035 assert_equal('debug', debugit#test())
4036 disass debugit#test
4037 assert_equal('profile', profileit#test())
4038 disass profileit#test
4039 END
4040 CheckScriptSuccess(lines)
4041
4042 delete('Xdir', 'rf')
4043 &rtp = save_rtp
4044enddef
4045
Bram Moolenaar03290b82020-12-19 16:30:44 +01004046" test using a vim9script that is auto-loaded from an autocmd
4047def Test_vim9_aucmd_autoload()
4048 var lines =<< trim END
4049 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004050 def foo#test()
4051 echomsg getreg('"')
4052 enddef
4053 END
4054
4055 mkdir('Xdir/autoload', 'p')
4056 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004057 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004058 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4059 augroup test
4060 autocmd TextYankPost * call foo#test()
4061 augroup END
4062
4063 normal Y
4064
4065 augroup test
4066 autocmd!
4067 augroup END
4068 delete('Xdir', 'rf')
4069 &rtp = save_rtp
4070enddef
4071
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004072" This was causing a crash because suppress_errthrow wasn't reset.
4073def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004074 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004075 vim9script
4076 def crash#func()
4077 try
4078 for x in List()
4079 endfor
4080 catch
4081 endtry
4082 g:ok = true
4083 enddef
4084 fu List()
4085 invalid
4086 endfu
4087 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01004088 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004089 catch /wontmatch/
4090 endtry
4091 END
4092 call mkdir('Xruntime/autoload', 'p')
4093 call writefile(lines, 'Xruntime/autoload/crash.vim')
4094
4095 # run in a separate Vim to avoid the side effects of assert_fails()
4096 lines =<< trim END
4097 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
4098 call crash#func()
4099 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004100 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004101 END
4102 writefile(lines, 'Xscript')
4103 RunVim([], [], '-S Xscript')
4104 assert_equal(['ok'], readfile('Xdidit'))
4105
4106 delete('Xdidit')
4107 delete('Xscript')
4108 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01004109
4110 lines =<< trim END
4111 vim9script
4112 var foo#bar = 'asdf'
4113 END
4114 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004115enddef
4116
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02004117def Test_script_var_in_autocmd()
4118 # using a script variable from an autocommand, defined in a :def function in a
4119 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004120 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02004121 let s:counter = 1
4122 def s:Func()
4123 au! CursorHold
4124 au CursorHold * s:counter += 1
4125 enddef
4126 call s:Func()
4127 doau CursorHold
4128 call assert_equal(2, s:counter)
4129 au! CursorHold
4130 END
4131 CheckScriptSuccess(lines)
4132enddef
4133
Bram Moolenaarb5841b92021-07-15 18:09:53 +02004134def Test_error_in_autoload_script()
4135 var save_rtp = &rtp
4136 var dir = getcwd() .. '/Xruntime'
4137 &rtp = dir
4138 mkdir(dir .. '/autoload', 'p')
4139
4140 var lines =<< trim END
4141 vim9script noclear
4142 def script#autoloaded()
4143 enddef
4144 def Broken()
4145 var x: any = ''
4146 eval x != 0
4147 enddef
4148 Broken()
4149 END
4150 writefile(lines, dir .. '/autoload/script.vim')
4151
4152 lines =<< trim END
4153 vim9script
4154 def CallAutoloaded()
4155 script#autoloaded()
4156 enddef
4157
4158 function Legacy()
4159 try
4160 call s:CallAutoloaded()
4161 catch
4162 call assert_match('E1030: Using a String as a Number', v:exception)
4163 endtry
4164 endfunction
4165
4166 Legacy()
4167 END
4168 CheckScriptSuccess(lines)
4169
4170 &rtp = save_rtp
4171 delete(dir, 'rf')
4172enddef
4173
Bram Moolenaar3896a102020-08-09 14:33:55 +02004174def Test_cmdline_win()
4175 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
4176 # the command line window.
4177 mkdir('rtp/syntax', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004178 var export_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02004179 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004180 export var That = 'yes'
Bram Moolenaar3896a102020-08-09 14:33:55 +02004181 END
4182 writefile(export_lines, 'rtp/syntax/Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004183 var import_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02004184 vim9script
4185 import That from './Xexport.vim'
4186 END
4187 writefile(import_lines, 'rtp/syntax/vim.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004188 var save_rtp = &rtp
Bram Moolenaar3896a102020-08-09 14:33:55 +02004189 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
4190 syntax on
4191 augroup CmdWin
4192 autocmd CmdwinEnter * g:got_there = 'yes'
4193 augroup END
4194 # this will open and also close the cmdline window
4195 feedkeys('q:', 'xt')
4196 assert_equal('yes', g:got_there)
4197
4198 augroup CmdWin
4199 au!
4200 augroup END
4201 &rtp = save_rtp
4202 delete('rtp', 'rf')
4203enddef
4204
Bram Moolenaare3d46852020-08-29 13:39:17 +02004205def Test_invalid_sid()
4206 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02004207
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004208 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02004209 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02004210 endif
4211 delete('Xdidit')
4212enddef
4213
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004214def Test_restoring_cpo()
4215 writefile(['vim9script', 'set nocp'], 'Xsourced')
4216 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
4217 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
4218 assert_equal(['done'], readfile('Xdone'))
4219 endif
4220 delete('Xsourced')
4221 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01004222 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01004223
4224 writefile(['vim9script'], 'XanotherScript')
4225 set cpo=aABceFsMny>
4226 edit XanotherScript
4227 so %
4228 assert_equal('aABceFsMny>', &cpo)
4229 :1del
4230 w
4231 so %
4232 assert_equal('aABceFsMny>', &cpo)
4233
4234 delete('XanotherScript')
4235 set cpo&vim
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004236enddef
4237
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004238" Use :function so we can use Check commands
4239func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004240 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004241 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004242
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004243 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004244 vim9script
4245 def script#func()
4246 enddef
4247 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004248 call mkdir('Xdir/autoload', 'p')
4249 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004250
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004251 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004252 vim9script
4253 set cpo+=M
4254 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02004255 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004256 setline(1, 'some text')
4257 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004258 call writefile(lines, 'XTest_redraw_cpo')
4259 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
4260 call term_sendkeys(buf, "V:")
4261 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004262
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004263 " clean up
4264 call term_sendkeys(buf, "\<Esc>u")
4265 call StopVimInTerminal(buf)
4266 call delete('XTest_redraw_cpo')
4267 call delete('Xdir', 'rf')
4268endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004269
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004270
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02004271def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004272 var lines =<< trim END
4273 var name: any
4274 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02004275 END
4276 CheckDefAndScriptSuccess(lines)
4277enddef
4278
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02004279func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02004280 CheckRunVimInTerminal
4281
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02004282 " call indirectly to avoid compilation error for missing functions
4283 call Run_Test_define_func_at_command_line()
4284endfunc
4285
4286def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004287 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004288 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004289 func CheckAndQuit()
4290 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
4291 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
4292 endfunc
4293 END
4294 writefile([''], 'Xdidcmd')
4295 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01004296 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004297 # define Afunc() on the command line
4298 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
4299 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004300 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004301
4302 call StopVimInTerminal(buf)
4303 delete('XcallFunc')
4304 delete('Xdidcmd')
4305enddef
4306
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004307def Test_script_var_scope()
4308 var lines =<< trim END
4309 vim9script
4310 if true
4311 if true
4312 var one = 'one'
4313 echo one
4314 endif
4315 echo one
4316 endif
4317 END
4318 CheckScriptFailure(lines, 'E121:', 7)
4319
4320 lines =<< trim END
4321 vim9script
4322 if true
4323 if false
4324 var one = 'one'
4325 echo one
4326 else
4327 var one = 'one'
4328 echo one
4329 endif
4330 echo one
4331 endif
4332 END
4333 CheckScriptFailure(lines, 'E121:', 10)
4334
4335 lines =<< trim END
4336 vim9script
4337 while true
4338 var one = 'one'
4339 echo one
4340 break
4341 endwhile
4342 echo one
4343 END
4344 CheckScriptFailure(lines, 'E121:', 7)
4345
4346 lines =<< trim END
4347 vim9script
4348 for i in range(1)
4349 var one = 'one'
4350 echo one
4351 endfor
4352 echo one
4353 END
4354 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02004355
4356 lines =<< trim END
4357 vim9script
4358 {
4359 var one = 'one'
4360 assert_equal('one', one)
4361 }
4362 assert_false(exists('one'))
4363 assert_false(exists('s:one'))
4364 END
4365 CheckScriptSuccess(lines)
4366
4367 lines =<< trim END
4368 vim9script
4369 {
4370 var one = 'one'
4371 echo one
4372 }
4373 echo one
4374 END
4375 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004376enddef
4377
Bram Moolenaar352134b2020-10-17 22:04:08 +02004378def Test_catch_exception_in_callback()
4379 var lines =<< trim END
4380 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02004381 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02004382 try
4383 var x: string
4384 var y: string
4385 # this error should be caught with CHECKLEN
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00004386 var sl = ['']
4387 [x, y] = sl
Bram Moolenaar352134b2020-10-17 22:04:08 +02004388 catch
4389 g:caught = 'yes'
4390 endtry
4391 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004392 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02004393 feedkeys("\r", 'xt')
4394 END
4395 CheckScriptSuccess(lines)
4396
4397 unlet g:caught
4398enddef
4399
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004400def Test_no_unknown_error_after_error()
4401 if !has('unix') || !has('job')
4402 throw 'Skipped: not unix of missing +job feature'
4403 endif
Bram Moolenaarb16ff292021-09-26 20:14:39 +01004404 # FIXME: this check should not be needed
4405 if has('win32')
4406 throw 'Skipped: does not work on MS-Windows'
4407 endif
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004408 var lines =<< trim END
4409 vim9script
4410 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02004411 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004412 eval [][0]
4413 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02004414 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004415 sleep 1m
4416 source += l
4417 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004418 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01004419 while job_status(myjob) == 'run'
4420 sleep 10m
4421 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01004422 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02004423 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004424 END
4425 writefile(lines, 'Xdef')
4426 assert_fails('so Xdef', ['E684:', 'E1012:'])
4427 delete('Xdef')
4428enddef
4429
Bram Moolenaar4324d872020-12-01 20:12:24 +01004430def InvokeNormal()
4431 exe "norm! :m+1\r"
4432enddef
4433
4434def Test_invoke_normal_in_visual_mode()
4435 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
4436 new
4437 setline(1, ['aaa', 'bbb'])
4438 feedkeys("V\<F3>", 'xt')
4439 assert_equal(['bbb', 'aaa'], getline(1, 2))
4440 xunmap <F3>
4441enddef
4442
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004443def Test_white_space_after_command()
4444 var lines =<< trim END
4445 exit_cb: Func})
4446 END
4447 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01004448
4449 lines =<< trim END
4450 e#
4451 END
4452 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004453enddef
4454
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004455def Test_script_var_gone_when_sourced_twice()
4456 var lines =<< trim END
4457 vim9script
4458 if exists('g:guard')
4459 finish
4460 endif
4461 g:guard = 1
4462 var name = 'thename'
4463 def g:GetName(): string
4464 return name
4465 enddef
4466 def g:SetName(arg: string)
4467 name = arg
4468 enddef
4469 END
4470 writefile(lines, 'XscriptTwice.vim')
4471 so XscriptTwice.vim
4472 assert_equal('thename', g:GetName())
4473 g:SetName('newname')
4474 assert_equal('newname', g:GetName())
4475 so XscriptTwice.vim
4476 assert_fails('call g:GetName()', 'E1149:')
4477 assert_fails('call g:SetName("x")', 'E1149:')
4478
4479 delfunc g:GetName
4480 delfunc g:SetName
4481 delete('XscriptTwice.vim')
4482 unlet g:guard
4483enddef
4484
4485def Test_import_gone_when_sourced_twice()
4486 var exportlines =<< trim END
4487 vim9script
4488 if exists('g:guard')
4489 finish
4490 endif
4491 g:guard = 1
4492 export var name = 'someName'
4493 END
4494 writefile(exportlines, 'XexportScript.vim')
4495
4496 var lines =<< trim END
4497 vim9script
4498 import name from './XexportScript.vim'
4499 def g:GetName(): string
4500 return name
4501 enddef
4502 END
4503 writefile(lines, 'XscriptImport.vim')
4504 so XscriptImport.vim
4505 assert_equal('someName', g:GetName())
4506
4507 so XexportScript.vim
4508 assert_fails('call g:GetName()', 'E1149:')
4509
4510 delfunc g:GetName
4511 delete('XexportScript.vim')
4512 delete('XscriptImport.vim')
4513 unlet g:guard
4514enddef
4515
Bram Moolenaar10b94212021-02-19 21:42:57 +01004516def Test_unsupported_commands()
4517 var lines =<< trim END
4518 ka
4519 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004520 CheckDefFailure(lines, 'E476:')
4521 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaar10b94212021-02-19 21:42:57 +01004522
4523 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01004524 :1ka
4525 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004526 CheckDefFailure(lines, 'E476:')
4527 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaarada1d872021-02-20 08:16:51 +01004528
4529 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01004530 t
4531 END
4532 CheckDefFailure(lines, 'E1100:')
4533 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4534
4535 lines =<< trim END
4536 x
4537 END
4538 CheckDefFailure(lines, 'E1100:')
4539 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4540
4541 lines =<< trim END
4542 xit
4543 END
4544 CheckDefFailure(lines, 'E1100:')
4545 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4546enddef
4547
Bram Moolenaarc70fe462021-04-17 17:59:19 +02004548def Test_mapping_line_number()
4549 var lines =<< trim END
4550 vim9script
4551 def g:FuncA()
4552 # Some comment
4553 FuncB(0)
4554 enddef
4555 # Some comment
4556 def FuncB(
4557 # Some comment
4558 n: number
4559 )
4560 exe 'nno '
4561 # Some comment
4562 .. '<F3> a'
4563 .. 'b'
4564 .. 'c'
4565 enddef
4566 END
4567 CheckScriptSuccess(lines)
4568 var res = execute('verbose nmap <F3>')
4569 assert_match('No mapping found', res)
4570
4571 g:FuncA()
4572 res = execute('verbose nmap <F3>')
4573 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
4574
4575 nunmap <F3>
4576 delfunc g:FuncA
4577enddef
4578
Bram Moolenaardeb108b2021-07-08 17:35:36 +02004579def Test_option_set()
4580 # legacy script allows for white space
4581 var lines =<< trim END
4582 set foldlevel =11
4583 call assert_equal(11, &foldlevel)
4584 END
4585 CheckScriptSuccess(lines)
4586
4587 set foldlevel
4588 set foldlevel=12
4589 assert_equal(12, &foldlevel)
4590 set foldlevel+=2
4591 assert_equal(14, &foldlevel)
4592 set foldlevel-=3
4593 assert_equal(11, &foldlevel)
4594
4595 lines =<< trim END
4596 set foldlevel =1
4597 END
4598 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: =1')
4599
4600 lines =<< trim END
4601 set foldlevel +=1
4602 END
4603 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: +=1')
4604
4605 lines =<< trim END
4606 set foldlevel ^=1
4607 END
4608 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: ^=1')
4609
4610 lines =<< trim END
4611 set foldlevel -=1
4612 END
4613 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: -=1')
4614
4615 set foldlevel&
4616enddef
4617
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004618def Test_option_modifier()
Bram Moolenaar1594f312021-07-08 16:40:13 +02004619 # legacy script allows for white space
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004620 var lines =<< trim END
4621 set hlsearch & hlsearch !
4622 call assert_equal(1, &hlsearch)
4623 END
4624 CheckScriptSuccess(lines)
4625
Bram Moolenaar1594f312021-07-08 16:40:13 +02004626 set hlsearch
4627 set hlsearch!
4628 assert_equal(false, &hlsearch)
4629
4630 set hlsearch
4631 set hlsearch&
4632 assert_equal(false, &hlsearch)
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004633
4634 lines =<< trim END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004635 set hlsearch &
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004636 END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004637 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: &')
4638
4639 lines =<< trim END
4640 set hlsearch !
4641 END
4642 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: !')
4643
4644 set hlsearch&
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004645enddef
4646
Bram Moolenaarc03fe662021-07-11 16:52:45 +02004647" This must be called last, it may cause following :def functions to fail
4648def Test_xxx_echoerr_line_number()
4649 var lines =<< trim END
4650 echoerr 'some'
4651 .. ' error'
4652 .. ' continued'
4653 END
4654 CheckDefExecAndScriptFailure(lines, 'some error continued', 1)
4655enddef
4656
Bram Moolenaar9537e372021-12-10 21:05:53 +00004657func Test_debug_with_lambda()
Bram Moolenaar9fffef92021-12-10 16:55:58 +00004658 CheckRunVimInTerminal
4659
Bram Moolenaar9537e372021-12-10 21:05:53 +00004660 " call indirectly to avoid compilation error for missing functions
4661 call Run_Test_debug_with_lambda()
4662endfunc
4663
4664def Run_Test_debug_with_lambda()
Bram Moolenaar9fffef92021-12-10 16:55:58 +00004665 var lines =<< trim END
4666 vim9script
4667 def Func()
4668 var n = 0
4669 echo [0]->filter((_, v) => v == n)
4670 enddef
4671 breakadd func Func
4672 Func()
4673 END
4674 writefile(lines, 'XdebugFunc')
4675 var buf = RunVimInTerminal('-S XdebugFunc', {rows: 6, wait_for_ruler: 0})
4676 WaitForAssert(() => assert_match('^>', term_getline(buf, 6)))
4677
4678 term_sendkeys(buf, "cont\<CR>")
4679 WaitForAssert(() => assert_match('\[0\]', term_getline(buf, 5)))
4680
4681 StopVimInTerminal(buf)
4682 delete('XdebugFunc')
4683enddef
4684
Bram Moolenaar648594e2021-07-11 17:55:01 +02004685def ProfiledWithLambda()
Bram Moolenaard9162552021-07-11 15:26:13 +02004686 var n = 3
4687 echo [[1, 2], [3, 4]]->filter((_, l) => l[0] == n)
4688enddef
4689
Bram Moolenaar648594e2021-07-11 17:55:01 +02004690def ProfiledNested()
4691 var x = 0
4692 def Nested(): any
4693 return x
4694 enddef
4695 Nested()
4696enddef
4697
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004698def ProfiledNestedProfiled()
4699 var x = 0
4700 def Nested(): any
4701 return x
4702 enddef
4703 Nested()
4704enddef
4705
Dominique Pelle923dce22021-11-21 11:36:04 +00004706" Execute this near the end, profiling doesn't stop until Vim exits.
Bram Moolenaard9162552021-07-11 15:26:13 +02004707" This only tests that it works, not the profiling output.
4708def Test_xx_profile_with_lambda()
Bram Moolenaar4ece1522021-07-11 16:31:51 +02004709 CheckFeature profile
4710
Bram Moolenaard9162552021-07-11 15:26:13 +02004711 profile start Xprofile.log
Bram Moolenaar648594e2021-07-11 17:55:01 +02004712 profile func ProfiledWithLambda
4713 ProfiledWithLambda()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004714
Bram Moolenaar648594e2021-07-11 17:55:01 +02004715 profile func ProfiledNested
4716 ProfiledNested()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004717
4718 # Also profile the nested function. Use a different function, although the
4719 # contents is the same, to make sure it was not already compiled.
4720 profile func *
4721 ProfiledNestedProfiled()
4722
4723 profdel func *
4724 profile pause
Bram Moolenaard9162552021-07-11 15:26:13 +02004725enddef
4726
Bram Moolenaar585fea72020-04-02 22:33:21 +02004727" Keep this last, it messes up highlighting.
4728def Test_substitute_cmd()
4729 new
4730 setline(1, 'something')
4731 :substitute(some(other(
4732 assert_equal('otherthing', getline(1))
4733 bwipe!
4734
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004735 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004736 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02004737 vim9script
4738 new
4739 setline(1, 'something')
4740 :substitute(some(other(
4741 assert_equal('otherthing', getline(1))
4742 bwipe!
4743 END
4744 writefile(lines, 'Xvim9lines')
4745 source Xvim9lines
4746
4747 delete('Xvim9lines')
4748enddef
4749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker