blob: 58cf0202224bd26d52b5e0b7b6276eb0c27a6908 [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 Moolenaarc3235272021-07-10 19:42:03 +02001899def Test_script_var_shadows_command()
1900 var lines =<< trim END
1901 var undo = 1
1902 undo = 2
1903 assert_equal(2, undo)
1904 END
1905 CheckDefAndScriptSuccess(lines)
1906
1907 lines =<< trim END
1908 var undo = 1
1909 undo
1910 END
1911 CheckDefAndScriptFailure(lines, 'E1207:', 2)
1912enddef
1913
Bram Moolenaar95006e32020-08-29 17:47:08 +02001914def s:RetSome(): string
1915 return 'some'
1916enddef
1917
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001918" Not exported function that is referenced needs to be accessed by the
1919" script-local name.
1920def Test_vim9script_funcref()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001921 var sortlines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001922 vim9script
1923 def Compare(i1: number, i2: number): number
Bram Moolenaarbed36b92020-07-07 23:31:36 +02001924 return i2 - i1
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001925 enddef
1926
1927 export def FastSort(): list<number>
1928 return range(5)->sort(Compare)
1929 enddef
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001930
1931 export def GetString(arg: string): string
1932 return arg
1933 enddef
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001934 END
1935 writefile(sortlines, 'Xsort.vim')
1936
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001937 var lines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001938 vim9script
1939 import FastSort from './Xsort.vim'
1940 def Test()
1941 g:result = FastSort()
1942 enddef
1943 Test()
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001944
1945 # using a function imported with "as"
1946 import * as anAlias from './Xsort.vim'
1947 assert_equal('yes', anAlias.GetString('yes'))
1948
1949 # using the function from a compiled function
1950 def TestMore(): string
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001951 var s = s:anAlias.GetString('foo')
1952 return s .. anAlias.GetString('bar')
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001953 enddef
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001954 assert_equal('foobar', TestMore())
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001955
1956 # error when using a function that isn't exported
1957 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001958 END
1959 writefile(lines, 'Xscript.vim')
1960
1961 source Xscript.vim
1962 assert_equal([4, 3, 2, 1, 0], g:result)
1963
1964 unlet g:result
1965 delete('Xsort.vim')
1966 delete('Xscript.vim')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001967
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001968 var Funcref = function('s:RetSome')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001969 assert_equal('some', Funcref())
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001970enddef
1971
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001972" Check that when searching for "FilterFunc" it finds the import in the
1973" script where FastFilter() is called from, both as a string and as a direct
1974" function reference.
Bram Moolenaarc620c052020-07-08 15:16:19 +02001975def Test_vim9script_funcref_other_script()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001976 var filterLines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001977 vim9script
1978 export def FilterFunc(idx: number, val: number): bool
1979 return idx % 2 == 1
1980 enddef
1981 export def FastFilter(): list<number>
1982 return range(10)->filter('FilterFunc')
1983 enddef
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001984 export def FastFilterDirect(): list<number>
1985 return range(10)->filter(FilterFunc)
1986 enddef
Bram Moolenaarc620c052020-07-08 15:16:19 +02001987 END
1988 writefile(filterLines, 'Xfilter.vim')
1989
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001990 var lines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001991 vim9script
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001992 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim'
Bram Moolenaarc620c052020-07-08 15:16:19 +02001993 def Test()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001994 var x: list<number> = FastFilter()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001995 enddef
1996 Test()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001997 def TestDirect()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001998 var x: list<number> = FastFilterDirect()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001999 enddef
2000 TestDirect()
Bram Moolenaarc620c052020-07-08 15:16:19 +02002001 END
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002002 CheckScriptSuccess(lines)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002003 delete('Xfilter.vim')
Bram Moolenaarc620c052020-07-08 15:16:19 +02002004enddef
2005
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002006def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002007 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002008 vim9script
2009 def FuncYes(): string
2010 return 'yes'
2011 enddef
2012 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002013 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002014 def FuncNo(): string
2015 return 'no'
2016 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002017 def g:DoCheck(no_exists: bool)
2018 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002019 assert_equal('no', FuncNo())
2020 enddef
2021 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002022 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002023 def g:DoCheck(no_exists: bool)
2024 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02002025 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002026 enddef
2027 END
2028
2029 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002030 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002031 source Xreloaded.vim
2032 g:DoCheck(true)
2033
2034 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002035 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002036 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01002037 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002038
2039 # FuncNo() is back
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002040 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002041 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01002042 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002043
2044 delete('Xreloaded.vim')
2045enddef
2046
Bram Moolenaar89483d42020-05-10 15:24:44 +02002047def Test_vim9script_reload_delvar()
2048 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002049 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02002050 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002051 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02002052 END
2053 writefile(lines, 'XreloadVar.vim')
2054 source XreloadVar.vim
2055
2056 # now write the script using the same variable locally - works
2057 lines =<< trim END
2058 vim9script
2059 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002060 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02002061 enddef
2062 END
2063 writefile(lines, 'XreloadVar.vim')
2064 source XreloadVar.vim
2065
2066 delete('XreloadVar.vim')
2067enddef
2068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069def Test_import_absolute()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002070 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02002071 'vim9script',
2072 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
2073 'def UseExported()',
2074 ' g:imported_abs = exported',
2075 ' exported = 8888',
2076 ' g:imported_after = exported',
2077 'enddef',
2078 'UseExported()',
2079 'g:import_disassembled = execute("disass UseExported")',
2080 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081 writefile(import_lines, 'Ximport_abs.vim')
2082 writefile(s:export_script_lines, 'Xexport_abs.vim')
2083
2084 source Ximport_abs.vim
2085
2086 assert_equal(9876, g:imported_abs)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002087 assert_equal(8888, g:imported_after)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002088 assert_match('<SNR>\d\+_UseExported\_s*' ..
2089 'g:imported_abs = exported\_s*' ..
2090 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
2091 '1 STOREG g:imported_abs\_s*' ..
2092 'exported = 8888\_s*' ..
2093 '2 PUSHNR 8888\_s*' ..
2094 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
2095 'g:imported_after = exported\_s*' ..
2096 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
2097 '5 STOREG g:imported_after',
Bram Moolenaare6085c52020-04-12 20:19:16 +02002098 g:import_disassembled)
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002099
2100 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101 unlet g:imported_abs
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002102 unlet g:import_disassembled
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103
2104 delete('Ximport_abs.vim')
2105 delete('Xexport_abs.vim')
2106enddef
2107
2108def Test_import_rtp()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002109 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02002110 'vim9script',
2111 'import exported from "Xexport_rtp.vim"',
2112 'g:imported_rtp = exported',
2113 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 writefile(import_lines, 'Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002115 mkdir('import', 'p')
2116 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002118 var save_rtp = &rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 &rtp = getcwd()
2120 source Ximport_rtp.vim
2121 &rtp = save_rtp
2122
2123 assert_equal(9876, g:imported_rtp)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002125 Undo_export_script_lines()
2126 unlet g:imported_rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127 delete('Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002128 delete('import', 'rf')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129enddef
2130
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002131def Test_import_compile_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002132 var export_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002133 'vim9script',
2134 'export def ExpFunc(): string',
2135 ' return notDefined',
2136 'enddef',
2137 ]
2138 writefile(export_lines, 'Xexported.vim')
2139
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002140 var import_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002141 'vim9script',
2142 'import ExpFunc from "./Xexported.vim"',
2143 'def ImpFunc()',
2144 ' echo ExpFunc()',
2145 'enddef',
2146 'defcompile',
2147 ]
2148 writefile(import_lines, 'Ximport.vim')
2149
2150 try
2151 source Ximport.vim
2152 catch /E1001/
Dominique Pelle923dce22021-11-21 11:36:04 +00002153 # Error should be before the Xexported.vim file.
Bram Moolenaar77072282020-09-16 17:55:40 +02002154 assert_match('E1001: Variable not found: notDefined', v:exception)
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002155 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
2156 endtry
2157
2158 delete('Xexported.vim')
2159 delete('Ximport.vim')
2160enddef
2161
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002162def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002163 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002164 'vim9script',
2165 'def Func()',
2166 ' eval [][0]',
2167 'enddef',
2168 'Func()',
2169 ]
2170 writefile(lines, 'Xtestscript.vim')
2171
2172 for count in range(3)
2173 try
2174 source Xtestscript.vim
2175 catch /E684/
2176 # function name should contain <SNR> every time
2177 assert_match('E684: list index out of range', v:exception)
2178 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
2179 endtry
2180 endfor
2181
2182 delete('Xtestscript.vim')
2183enddef
2184
Bram Moolenaareef21022020-08-01 22:16:43 +02002185def Test_func_overrules_import_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002186 var export_lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002187 vim9script
2188 export def Func()
2189 echo 'imported'
2190 enddef
2191 END
2192 writefile(export_lines, 'XexportedFunc.vim')
2193
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002194 var lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002195 vim9script
2196 import Func from './XexportedFunc.vim'
2197 def Func()
2198 echo 'local to function'
2199 enddef
2200 END
2201 CheckScriptFailure(lines, 'E1073:')
2202
2203 lines =<< trim END
2204 vim9script
2205 import Func from './XexportedFunc.vim'
2206 def Outer()
2207 def Func()
2208 echo 'local to function'
2209 enddef
2210 enddef
2211 defcompile
2212 END
2213 CheckScriptFailure(lines, 'E1073:')
2214
2215 delete('XexportedFunc.vim')
2216enddef
2217
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002218def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002219 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002220 vim9script
2221 def Func()
2222 echo 'one'
2223 enddef
2224 def Func()
2225 echo 'two'
2226 enddef
2227 END
2228 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002229
2230 lines =<< trim END
2231 vim9script
2232 def Foo(): string
2233 return 'foo'
2234 enddef
2235 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002236 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002237 enddef
2238 defcompile
2239 END
2240 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002241enddef
2242
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002244 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002245 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 l->remove(0)
2247 l->add(5)
2248 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002249 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250enddef
2251
Bram Moolenaarae616492020-07-28 20:07:27 +02002252def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002253 CheckDefExecFailure(['a = 1'], 'E1100:')
2254 CheckDefExecFailure(['c = 1'], 'E1100:')
2255 CheckDefExecFailure(['i = 1'], 'E1100:')
2256 CheckDefExecFailure(['t = 1'], 'E1100:')
2257 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002258
Bram Moolenaarae616492020-07-28 20:07:27 +02002259 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
2260 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002261 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
2262 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002263 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
2264 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01002265 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
2266 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002267 CheckScriptFailure(['vim9script', 't'], 'E1100:')
2268 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
2269 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002270enddef
2271
Bram Moolenaar158906c2020-02-06 20:39:45 +01002272def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002273 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01002274 if what == 1
2275 res = "one"
2276 elseif what == 2
2277 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002278 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01002279 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002280 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01002281 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002282enddef
2283
Bram Moolenaar158906c2020-02-06 20:39:45 +01002284def Test_if_elseif_else()
2285 assert_equal('one', IfElse(1))
2286 assert_equal('two', IfElse(2))
2287 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002288enddef
2289
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002290def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002291 CheckDefFailure(['elseif true'], 'E582:')
2292 CheckDefFailure(['else'], 'E581:')
2293 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01002294 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002295 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002296
2297 var lines =<< trim END
2298 var s = ''
2299 if s = ''
2300 endif
2301 END
2302 CheckDefFailure(lines, 'E488:')
2303
2304 lines =<< trim END
2305 var s = ''
2306 if s == ''
2307 elseif s = ''
2308 endif
2309 END
2310 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002311enddef
2312
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002313let g:bool_true = v:true
2314let g:bool_false = v:false
2315
2316def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002317 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002318 if true ? true : false
2319 res = true
2320 endif
2321 assert_equal(true, res)
2322
Bram Moolenaar585fea72020-04-02 22:33:21 +02002323 g:glob = 2
2324 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02002325 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002326 endif
2327 assert_equal(2, g:glob)
2328 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02002329 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002330 endif
2331 assert_equal(3, g:glob)
2332
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002333 res = false
2334 if g:bool_true ? true : false
2335 res = true
2336 endif
2337 assert_equal(true, res)
2338
2339 res = false
2340 if true ? g:bool_true : false
2341 res = true
2342 endif
2343 assert_equal(true, res)
2344
2345 res = false
2346 if true ? true : g:bool_false
2347 res = true
2348 endif
2349 assert_equal(true, res)
2350
2351 res = false
2352 if true ? false : true
2353 res = true
2354 endif
2355 assert_equal(false, res)
2356
2357 res = false
2358 if false ? false : true
2359 res = true
2360 endif
2361 assert_equal(true, res)
2362
2363 res = false
2364 if false ? true : false
2365 res = true
2366 endif
2367 assert_equal(false, res)
2368
2369 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002370 if has('xyz') ? true : false
2371 res = true
2372 endif
2373 assert_equal(false, res)
2374
2375 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002376 if true && true
2377 res = true
2378 endif
2379 assert_equal(true, res)
2380
2381 res = false
2382 if true && false
2383 res = true
2384 endif
2385 assert_equal(false, res)
2386
2387 res = false
2388 if g:bool_true && false
2389 res = true
2390 endif
2391 assert_equal(false, res)
2392
2393 res = false
2394 if true && g:bool_false
2395 res = true
2396 endif
2397 assert_equal(false, res)
2398
2399 res = false
2400 if false && false
2401 res = true
2402 endif
2403 assert_equal(false, res)
2404
2405 res = false
2406 if true || false
2407 res = true
2408 endif
2409 assert_equal(true, res)
2410
2411 res = false
2412 if g:bool_true || false
2413 res = true
2414 endif
2415 assert_equal(true, res)
2416
2417 res = false
2418 if true || g:bool_false
2419 res = true
2420 endif
2421 assert_equal(true, res)
2422
2423 res = false
2424 if false || false
2425 res = true
2426 endif
2427 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02002428
2429 # with constant "false" expression may be invalid so long as the syntax is OK
Bram Moolenaarc3235272021-07-10 19:42:03 +02002430 if false | eval 1 + 2 | endif
Bram Moolenaar3988f642020-08-27 22:43:03 +02002431 if false | eval burp + 234 | endif
2432 if false | echo burp 234 'asd' | endif
2433 if false
2434 burp
2435 endif
Bram Moolenaare525bdd2021-08-07 18:12:40 +02002436
2437 # expression with line breaks skipped
2438 if false
2439 ('aaa'
2440 .. 'bbb'
2441 .. 'ccc'
2442 )->setline(1)
2443 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002444enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002445
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002446def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002447 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
2448 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
2449 CheckDefFailure(["if has('aaa'"], 'E110:')
2450 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002451enddef
2452
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002453def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002454 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002455 if i % 2
2456 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002457 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002458 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002459 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002460 endif
2461 x += 1
2462 else
2463 x += 1000
2464 endif
2465 return x
2466enddef
2467
2468def Test_nested_if()
2469 assert_equal(1, RunNested(1))
2470 assert_equal(1000, RunNested(2))
2471enddef
2472
Bram Moolenaarad39c092020-02-26 18:23:43 +01002473def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01002474 # missing argument is ignored
2475 execute
2476 execute # comment
2477
Bram Moolenaarad39c092020-02-26 18:23:43 +01002478 new
2479 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002480 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002481 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002482
Bram Moolenaard2c61702020-09-06 15:58:36 +02002483 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002484 assert_equal('execute-string', getline(1))
2485
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002486 var cmd1 = 'setline(1,'
2487 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002488 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01002489 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002490
Bram Moolenaard2c61702020-09-06 15:58:36 +02002491 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002492 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002493
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002494 var cmd_first = 'call '
2495 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002496 execute cmd_first .. cmd_last
2497 assert_equal('execute-var-var', getline(1))
2498 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02002499
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002500 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002501 execute 'echomsg' (n ? '"true"' : '"no"')
2502 assert_match('^true$', Screenline(&lines))
2503
Bram Moolenaare0de1712020-12-02 17:36:54 +01002504 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002505 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
2506
Bram Moolenaard2c61702020-09-06 15:58:36 +02002507 CheckDefFailure(['execute xxx'], 'E1001:', 1)
2508 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
2509 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002510enddef
2511
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002512def Test_execute_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002513 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002514 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002515 vim9script
2516 execute 'g:someVar'
2517 .. ' = ' ..
2518 '28'
2519 assert_equal(28, g:someVar)
2520 unlet g:someVar
2521 END
2522 CheckScriptSuccess(lines)
2523enddef
2524
Bram Moolenaarad39c092020-02-26 18:23:43 +01002525def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002526 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02002527 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002528 assert_match('^something$', Screenline(&lines))
2529
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002530 echo "some" # comment
2531 echon "thing"
2532 assert_match('^something$', Screenline(&lines))
2533
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002534 var str1 = 'some'
2535 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002536 echo str1 str2
2537 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002538
Bram Moolenaard2c61702020-09-06 15:58:36 +02002539 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01002540enddef
2541
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002542def Test_echomsg_cmd()
2543 echomsg 'some' 'more' # comment
2544 assert_match('^some more$', Screenline(&lines))
2545 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002546 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002547 assert_match('^some more$', Screenline(&lines))
2548
Bram Moolenaard2c61702020-09-06 15:58:36 +02002549 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002550enddef
2551
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002552def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002553 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002554 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002555 vim9script
2556 echomsg 'here'
2557 .. ' is ' ..
2558 'a message'
2559 assert_match('^here is a message$', Screenline(&lines))
2560 END
2561 CheckScriptSuccess(lines)
2562enddef
2563
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002564def Test_echoerr_cmd()
Bram Moolenaar7de62622021-08-07 15:05:47 +02002565 var local = 'local'
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002566 try
Bram Moolenaar7de62622021-08-07 15:05:47 +02002567 echoerr 'something' local 'wrong' # comment
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002568 catch
Bram Moolenaar7de62622021-08-07 15:05:47 +02002569 assert_match('something local wrong', v:exception)
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002570 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002571enddef
2572
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002573def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002574 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002575 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002576 vim9script
2577 try
2578 echoerr 'this'
2579 .. ' is ' ..
2580 'wrong'
2581 catch
2582 assert_match('this is wrong', v:exception)
2583 endtry
2584 END
2585 CheckScriptSuccess(lines)
2586enddef
2587
Bram Moolenaar7de62622021-08-07 15:05:47 +02002588def Test_echoconsole_cmd()
2589 var local = 'local'
2590 echoconsole 'something' local # comment
2591 # output goes anywhere
2592enddef
2593
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002594def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002595 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002596 vim9script
2597 new
2598 for var in range(0, 3)
2599 append(line('$'), var)
2600 endfor
2601 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2602 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01002603
2604 var result = ''
2605 for i in [1, 2, 3]
2606 var loop = ' loop ' .. i
2607 result ..= loop
2608 endfor
2609 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002610 END
2611 writefile(lines, 'Xvim9for.vim')
2612 source Xvim9for.vim
2613 delete('Xvim9for.vim')
2614enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615
rbtnnbebf0692021-08-21 17:26:50 +02002616def Test_for_skipped_block()
2617 # test skipped blocks at outside of function
2618 var lines =<< trim END
2619 var result = []
2620 if true
2621 for n in [1, 2]
2622 result += [n]
2623 endfor
2624 else
2625 for n in [3, 4]
2626 result += [n]
2627 endfor
2628 endif
2629 assert_equal([1, 2], result)
2630
2631 result = []
2632 if false
2633 for n in [1, 2]
2634 result += [n]
2635 endfor
2636 else
2637 for n in [3, 4]
2638 result += [n]
2639 endfor
2640 endif
2641 assert_equal([3, 4], result)
2642 END
2643 CheckDefAndScriptSuccess(lines)
2644
2645 # test skipped blocks at inside of function
2646 lines =<< trim END
2647 def DefTrue()
2648 var result = []
2649 if true
2650 for n in [1, 2]
2651 result += [n]
2652 endfor
2653 else
2654 for n in [3, 4]
2655 result += [n]
2656 endfor
2657 endif
2658 assert_equal([1, 2], result)
2659 enddef
2660 DefTrue()
2661
2662 def DefFalse()
2663 var result = []
2664 if false
2665 for n in [1, 2]
2666 result += [n]
2667 endfor
2668 else
2669 for n in [3, 4]
2670 result += [n]
2671 endfor
2672 endif
2673 assert_equal([3, 4], result)
2674 enddef
2675 DefFalse()
2676 END
2677 CheckDefAndScriptSuccess(lines)
2678enddef
2679
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002680def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002681 var lines =<< trim END
2682 var result = ''
2683 for cnt in range(7)
2684 if cnt == 4
2685 break
2686 endif
2687 if cnt == 2
2688 continue
2689 endif
2690 result ..= cnt .. '_'
2691 endfor
2692 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02002693
Bram Moolenaarf2253962021-04-13 20:53:13 +02002694 var concat = ''
2695 for str in eval('["one", "two"]')
2696 concat ..= str
2697 endfor
2698 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002699
Bram Moolenaarf2253962021-04-13 20:53:13 +02002700 var total = 0
2701 for nr in
2702 [1, 2, 3]
2703 total += nr
2704 endfor
2705 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002706
Bram Moolenaarf2253962021-04-13 20:53:13 +02002707 total = 0
2708 for nr
2709 in [1, 2, 3]
2710 total += nr
2711 endfor
2712 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002713
Bram Moolenaarf2253962021-04-13 20:53:13 +02002714 total = 0
2715 for nr
2716 in
2717 [1, 2, 3]
2718 total += nr
2719 endfor
2720 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01002721
Bram Moolenaara3589a02021-04-14 13:30:46 +02002722 # with type
2723 total = 0
2724 for n: number in [1, 2, 3]
2725 total += n
2726 endfor
2727 assert_equal(6, total)
2728
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002729 var chars = ''
2730 for s: string in 'foobar'
2731 chars ..= s
2732 endfor
2733 assert_equal('foobar', chars)
2734
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02002735 chars = ''
2736 for x: string in {a: 'a', b: 'b'}->values()
2737 chars ..= x
2738 endfor
2739 assert_equal('ab', chars)
2740
Bram Moolenaara3589a02021-04-14 13:30:46 +02002741 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02002742 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02002743 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2744 res ..= n .. s
2745 endfor
2746 assert_equal('1a2b', res)
2747
Bram Moolenaar444d8782021-06-26 12:40:56 +02002748 # unpack with one var
2749 var reslist = []
2750 for [x] in [['aaa'], ['bbb']]
2751 reslist->add(x)
2752 endfor
2753 assert_equal(['aaa', 'bbb'], reslist)
2754
Bram Moolenaara3589a02021-04-14 13:30:46 +02002755 # loop over string
2756 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02002757 for c in 'aéc̀d'
2758 res ..= c .. '-'
2759 endfor
2760 assert_equal('a-é-c̀-d-', res)
2761
2762 res = ''
2763 for c in ''
2764 res ..= c .. '-'
2765 endfor
2766 assert_equal('', res)
2767
2768 res = ''
2769 for c in test_null_string()
2770 res ..= c .. '-'
2771 endfor
2772 assert_equal('', res)
2773
2774 var foo: list<dict<any>> = [
2775 {a: 'Cat'}
2776 ]
2777 for dd in foo
2778 dd.counter = 12
2779 endfor
2780 assert_equal([{a: 'Cat', counter: 12}], foo)
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002781
2782 reslist = []
2783 for _ in range(3)
2784 reslist->add('x')
2785 endfor
2786 assert_equal(['x', 'x', 'x'], reslist)
Bram Moolenaarf2253962021-04-13 20:53:13 +02002787 END
2788 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002789enddef
2790
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02002791def Test_for_loop_with_closure()
2792 var lines =<< trim END
2793 var flist: list<func>
2794 for i in range(5)
2795 var inloop = i
2796 flist[i] = () => inloop
2797 endfor
2798 for i in range(5)
2799 assert_equal(4, flist[i]())
2800 endfor
2801 END
2802 CheckDefAndScriptSuccess(lines)
2803
2804 lines =<< trim END
2805 var flist: list<func>
2806 for i in range(5)
2807 var inloop = i
2808 flist[i] = () => {
2809 return inloop
2810 }
2811 endfor
2812 for i in range(5)
2813 assert_equal(4, flist[i]())
2814 endfor
2815 END
2816 CheckDefAndScriptSuccess(lines)
2817enddef
2818
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002819def Test_for_loop_fails()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002820 CheckDefAndScriptFailure2(['for '], 'E1097:', 'E690:')
2821 CheckDefAndScriptFailure2(['for x'], 'E1097:', 'E690:')
2822 CheckDefAndScriptFailure2(['for x in'], 'E1097:', 'E15:')
2823 CheckDefAndScriptFailure(['for # in range(5)'], 'E690:')
2824 CheckDefAndScriptFailure(['for i In range(5)'], 'E690:')
2825 CheckDefAndScriptFailure2(['var x = 5', 'for x in range(5)', 'endfor'], 'E1017:', 'E1041:')
Bram Moolenaard4ab8072021-07-08 19:22:12 +02002826 CheckScriptFailure(['vim9script', 'var x = 5', 'for x in range(5)', '# comment', 'endfor'], 'E1041:', 3)
Bram Moolenaar822ba242020-05-24 23:00:18 +02002827 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002828 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02002829 CheckDefFailure(['for i in xxx'], 'E1001:')
2830 CheckDefFailure(['endfor'], 'E588:')
2831 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002832
2833 # wrong type detected at compile time
2834 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2835
2836 # wrong type detected at runtime
2837 g:adict = {a: 1}
2838 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2839 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002840
2841 var lines =<< trim END
2842 var d: list<dict<any>> = [{a: 0}]
2843 for e in d
2844 e = {a: 0, b: ''}
2845 endfor
2846 END
2847 CheckDefAndScriptFailure2(lines, 'E1018:', 'E46:', 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002848
2849 lines =<< trim END
2850 for nr: number in ['foo']
2851 endfor
2852 END
2853 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaar404557e2021-07-05 21:41:48 +02002854
2855 lines =<< trim END
2856 for n : number in [1, 2]
2857 echo n
2858 endfor
2859 END
2860 CheckDefAndScriptFailure(lines, 'E1059:', 1)
Bram Moolenaarefc5db52021-07-07 21:21:30 +02002861
2862 lines =<< trim END
2863 var d: dict<number> = {a: 1, b: 2}
2864 for [k: job, v: job] in d->items()
2865 echo k v
2866 endfor
2867 END
2868 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected job but got string', 2)
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002869
2870 lines =<< trim END
2871 var i = 0
2872 for i in [1, 2, 3]
2873 echo i
2874 endfor
2875 END
2876 CheckDefExecAndScriptFailure2(lines, 'E1017:', 'E1041:')
2877
2878 lines =<< trim END
2879 var l = [0]
2880 for l[0] in [1, 2, 3]
2881 echo l[0]
2882 endfor
2883 END
2884 CheckDefExecAndScriptFailure2(lines, 'E461:', 'E1017:')
2885
2886 lines =<< trim END
2887 var d = {x: 0}
2888 for d.x in [1, 2, 3]
2889 echo d.x
2890 endfor
2891 END
2892 CheckDefExecAndScriptFailure2(lines, 'E461:', 'E1017:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002893enddef
2894
Bram Moolenaarea870692020-12-02 14:24:30 +01002895def Test_for_loop_script_var()
2896 # cannot use s:var in a :def function
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002897 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E461:')
Bram Moolenaarea870692020-12-02 14:24:30 +01002898
2899 # can use s:var in Vim9 script, with or without s:
2900 var lines =<< trim END
2901 vim9script
2902 var total = 0
2903 for s:var in [1, 2, 3]
2904 total += s:var
2905 endfor
2906 assert_equal(6, total)
2907
2908 total = 0
2909 for var in [1, 2, 3]
2910 total += var
2911 endfor
2912 assert_equal(6, total)
2913 END
2914enddef
2915
Bram Moolenaar792f7862020-11-23 08:31:18 +01002916def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01002917 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002918 var result = []
2919 for [v1, v2] in [[1, 2], [3, 4]]
2920 result->add(v1)
2921 result->add(v2)
2922 endfor
2923 assert_equal([1, 2, 3, 4], result)
2924
2925 result = []
2926 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2927 result->add(v1)
2928 result->add(v2)
2929 result->add(v3)
2930 endfor
2931 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
2932
2933 result = []
2934 for [&ts, &sw] in [[1, 2], [3, 4]]
2935 result->add(&ts)
2936 result->add(&sw)
2937 endfor
2938 assert_equal([1, 2, 3, 4], result)
2939
2940 var slist: list<string>
2941 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
2942 slist->add($LOOPVAR)
2943 slist->add(@r)
2944 slist->add(v:errmsg)
2945 endfor
2946 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
2947
2948 slist = []
2949 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
2950 slist->add(g:globalvar)
2951 slist->add(b:bufvar)
2952 slist->add(w:winvar)
2953 slist->add(t:tabvar)
2954 endfor
2955 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01002956 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaarb777da92021-05-22 21:40:39 +02002957
2958 var res = []
2959 for [_, n, _] in [[1, 2, 3], [4, 5, 6]]
2960 res->add(n)
2961 endfor
2962 assert_equal([2, 5], res)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002963 END
2964 CheckDefAndScriptSuccess(lines)
2965
2966 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01002967 for [v1, v2] in [[1, 2, 3], [3, 4]]
2968 echo v1 v2
2969 endfor
2970 END
2971 CheckDefExecFailure(lines, 'E710:', 1)
2972
2973 lines =<< trim END
2974 for [v1, v2] in [[1], [3, 4]]
2975 echo v1 v2
2976 endfor
2977 END
2978 CheckDefExecFailure(lines, 'E711:', 1)
2979
2980 lines =<< trim END
2981 for [v1, v1] in [[1, 2], [3, 4]]
2982 echo v1
2983 endfor
2984 END
2985 CheckDefExecFailure(lines, 'E1017:', 1)
2986enddef
2987
Bram Moolenaarc150c092021-02-13 15:02:46 +01002988def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002989 var lines =<< trim END
2990 var looped = 0
2991 var cleanup = 0
2992 for i in range(3)
2993 looped += 1
2994 try
2995 eval [][0]
2996 catch
2997 continue
2998 finally
2999 cleanup += 1
3000 endtry
3001 endfor
3002 assert_equal(3, looped)
3003 assert_equal(3, cleanup)
3004 END
3005 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003006enddef
3007
rbtnnd895b1d2021-08-20 20:54:25 +02003008def Test_while_skipped_block()
3009 # test skipped blocks at outside of function
3010 var lines =<< trim END
3011 var result = []
3012 var n = 0
3013 if true
3014 n = 1
3015 while n < 3
3016 result += [n]
3017 n += 1
3018 endwhile
3019 else
3020 n = 3
3021 while n < 5
3022 result += [n]
3023 n += 1
3024 endwhile
3025 endif
3026 assert_equal([1, 2], result)
3027
3028 result = []
3029 if false
3030 n = 1
3031 while n < 3
3032 result += [n]
3033 n += 1
3034 endwhile
3035 else
3036 n = 3
3037 while n < 5
3038 result += [n]
3039 n += 1
3040 endwhile
3041 endif
3042 assert_equal([3, 4], result)
3043 END
3044 CheckDefAndScriptSuccess(lines)
3045
3046 # test skipped blocks at inside of function
3047 lines =<< trim END
3048 def DefTrue()
3049 var result = []
3050 var n = 0
3051 if true
3052 n = 1
3053 while n < 3
3054 result += [n]
3055 n += 1
3056 endwhile
3057 else
3058 n = 3
3059 while n < 5
3060 result += [n]
3061 n += 1
3062 endwhile
3063 endif
3064 assert_equal([1, 2], result)
3065 enddef
3066 DefTrue()
3067
3068 def DefFalse()
3069 var result = []
3070 var n = 0
3071 if false
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([3, 4], result)
3085 enddef
3086 DefFalse()
3087 END
3088 CheckDefAndScriptSuccess(lines)
3089enddef
3090
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003091def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003092 var result = ''
3093 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003094 while cnt < 555
3095 if cnt == 3
3096 break
3097 endif
3098 cnt += 1
3099 if cnt == 2
3100 continue
3101 endif
3102 result ..= cnt .. '_'
3103 endwhile
3104 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01003105
3106 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01003107 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01003108 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003109enddef
3110
Bram Moolenaar7a53f292021-11-22 18:31:02 +00003111def Test_while_loop_in_script()
3112 var lines =<< trim END
3113 vim9script
3114 var result = ''
3115 var cnt = 0
3116 while cnt < 3
3117 var s = 'v' .. cnt
3118 result ..= s
3119 cnt += 1
3120 endwhile
3121 assert_equal('v0v1v2', result)
3122 END
3123 CheckScriptSuccess(lines)
3124enddef
3125
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003126def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02003127 CheckDefFailure(['while xxx'], 'E1001:')
3128 CheckDefFailure(['endwhile'], 'E588:')
3129 CheckDefFailure(['continue'], 'E586:')
3130 CheckDefFailure(['if true', 'continue'], 'E586:')
3131 CheckDefFailure(['break'], 'E587:')
3132 CheckDefFailure(['if true', 'break'], 'E587:')
3133 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01003134
3135 var lines =<< trim END
3136 var s = ''
3137 while s = ''
3138 endwhile
3139 END
3140 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003141enddef
3142
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003143def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003144 var caught = false
3145 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003146 try
3147 while 1
3148 x += 1
3149 if x == 100
3150 feedkeys("\<C-C>", 'Lt')
3151 endif
3152 endwhile
3153 catch
3154 caught = true
3155 assert_equal(100, x)
3156 endtry
3157 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003158 # consume the CTRL-C
3159 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003160enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01003161
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003162def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003163 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003164 'one',
3165 'two',
3166 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003167 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003168 assert_equal(['one', 'two', 'three'], mylist)
3169
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003170 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003171 ['one']: 1,
3172 ['two']: 2,
3173 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003174 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003175 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01003176 assert_equal({one: 1, two: 2, three: 3}, mydict)
3177 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003178 one: 1, # comment
3179 two: # comment
3180 2, # comment
3181 three: 3 # comment
3182 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003183 assert_equal({one: 1, two: 2, three: 3}, mydict)
3184 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003185 one: 1,
3186 two:
3187 2,
3188 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003189 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003190 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02003191
3192 assert_equal(
3193 ['one', 'two', 'three'],
3194 split('one two three')
3195 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003196enddef
3197
Bram Moolenaar7a092242020-04-16 22:10:49 +02003198def Test_vim9_comment()
3199 CheckScriptSuccess([
3200 'vim9script',
3201 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01003202 '#something',
3203 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003204 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01003205
3206 split Xfile
3207 CheckScriptSuccess([
3208 'vim9script',
3209 'edit #something',
3210 ])
3211 CheckScriptSuccess([
3212 'vim9script',
3213 'edit #{something',
3214 ])
3215 close
3216
Bram Moolenaar7a092242020-04-16 22:10:49 +02003217 CheckScriptFailure([
3218 'vim9script',
3219 ':# something',
3220 ], 'E488:')
3221 CheckScriptFailure([
3222 '# something',
3223 ], 'E488:')
3224 CheckScriptFailure([
3225 ':# something',
3226 ], 'E488:')
3227
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003228 { # block start
3229 } # block end
3230 CheckDefFailure([
3231 '{# comment',
3232 ], 'E488:')
3233 CheckDefFailure([
3234 '{',
3235 '}# comment',
3236 ], 'E488:')
3237
3238 echo "yes" # comment
3239 CheckDefFailure([
3240 'echo "yes"# comment',
3241 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003242 CheckScriptSuccess([
3243 'vim9script',
3244 'echo "yes" # something',
3245 ])
3246 CheckScriptFailure([
3247 'vim9script',
3248 'echo "yes"# something',
3249 ], 'E121:')
3250 CheckScriptFailure([
3251 'vim9script',
3252 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003253 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003254 CheckScriptFailure([
3255 'echo "yes" # something',
3256 ], 'E121:')
3257
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003258 exe "echo" # comment
3259 CheckDefFailure([
3260 'exe "echo"# comment',
3261 ], 'E488:')
3262 CheckScriptSuccess([
3263 'vim9script',
3264 'exe "echo" # something',
3265 ])
3266 CheckScriptFailure([
3267 'vim9script',
3268 'exe "echo"# something',
3269 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003270 CheckScriptFailure([
3271 'vim9script',
3272 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003273 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003274 CheckScriptFailure([
3275 'exe "echo" # something',
3276 ], 'E121:')
3277
Bram Moolenaar7a092242020-04-16 22:10:49 +02003278 CheckDefFailure([
3279 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003280 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003281 'catch',
3282 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003283 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003284 CheckScriptFailure([
3285 'vim9script',
3286 'try# comment',
3287 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003288 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003289 CheckDefFailure([
3290 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003291 ' throw#comment',
3292 'catch',
3293 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003294 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003295 CheckDefFailure([
3296 'try',
3297 ' throw "yes"#comment',
3298 'catch',
3299 'endtry',
3300 ], 'E488:')
3301 CheckDefFailure([
3302 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003303 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003304 'catch# comment',
3305 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003306 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003307 CheckScriptFailure([
3308 'vim9script',
3309 'try',
3310 ' echo "yes"',
3311 'catch# comment',
3312 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003313 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003314 CheckDefFailure([
3315 'try',
3316 ' echo "yes"',
3317 'catch /pat/# comment',
3318 'endtry',
3319 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003320 CheckDefFailure([
3321 'try',
3322 'echo "yes"',
3323 'catch',
3324 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003325 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003326 CheckScriptFailure([
3327 'vim9script',
3328 'try',
3329 ' echo "yes"',
3330 'catch',
3331 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003332 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003333
3334 CheckScriptSuccess([
3335 'vim9script',
3336 'hi # comment',
3337 ])
3338 CheckScriptFailure([
3339 'vim9script',
3340 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003341 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003342 CheckScriptSuccess([
3343 'vim9script',
3344 'hi Search # comment',
3345 ])
3346 CheckScriptFailure([
3347 'vim9script',
3348 'hi Search# comment',
3349 ], 'E416:')
3350 CheckScriptSuccess([
3351 'vim9script',
3352 'hi link This Search # comment',
3353 ])
3354 CheckScriptFailure([
3355 'vim9script',
3356 'hi link This That# comment',
3357 ], 'E413:')
3358 CheckScriptSuccess([
3359 'vim9script',
3360 'hi clear This # comment',
3361 'hi clear # comment',
3362 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003363 # not tested, because it doesn't give an error but a warning:
3364 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02003365 CheckScriptFailure([
3366 'vim9script',
3367 'hi clear# comment',
3368 ], 'E416:')
3369
3370 CheckScriptSuccess([
3371 'vim9script',
3372 'hi Group term=bold',
3373 'match Group /todo/ # comment',
3374 ])
3375 CheckScriptFailure([
3376 'vim9script',
3377 'hi Group term=bold',
3378 'match Group /todo/# comment',
3379 ], 'E488:')
3380 CheckScriptSuccess([
3381 'vim9script',
3382 'match # comment',
3383 ])
3384 CheckScriptFailure([
3385 'vim9script',
3386 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003387 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003388 CheckScriptSuccess([
3389 'vim9script',
3390 'match none # comment',
3391 ])
3392 CheckScriptFailure([
3393 'vim9script',
3394 'match none# comment',
3395 ], 'E475:')
3396
3397 CheckScriptSuccess([
3398 'vim9script',
3399 'menutrans clear # comment',
3400 ])
3401 CheckScriptFailure([
3402 'vim9script',
3403 'menutrans clear# comment text',
3404 ], 'E474:')
3405
3406 CheckScriptSuccess([
3407 'vim9script',
3408 'syntax clear # comment',
3409 ])
3410 CheckScriptFailure([
3411 'vim9script',
3412 'syntax clear# comment text',
3413 ], 'E28:')
3414 CheckScriptSuccess([
3415 'vim9script',
3416 'syntax keyword Word some',
3417 'syntax clear Word # comment',
3418 ])
3419 CheckScriptFailure([
3420 'vim9script',
3421 'syntax keyword Word some',
3422 'syntax clear Word# comment text',
3423 ], 'E28:')
3424
3425 CheckScriptSuccess([
3426 'vim9script',
3427 'syntax list # comment',
3428 ])
3429 CheckScriptFailure([
3430 'vim9script',
3431 'syntax list# comment text',
3432 ], 'E28:')
3433
3434 CheckScriptSuccess([
3435 'vim9script',
3436 'syntax match Word /pat/ oneline # comment',
3437 ])
3438 CheckScriptFailure([
3439 'vim9script',
3440 'syntax match Word /pat/ oneline# comment',
3441 ], 'E475:')
3442
3443 CheckScriptSuccess([
3444 'vim9script',
3445 'syntax keyword Word word # comm[ent',
3446 ])
3447 CheckScriptFailure([
3448 'vim9script',
3449 'syntax keyword Word word# comm[ent',
3450 ], 'E789:')
3451
3452 CheckScriptSuccess([
3453 'vim9script',
3454 'syntax match Word /pat/ # comment',
3455 ])
3456 CheckScriptFailure([
3457 'vim9script',
3458 'syntax match Word /pat/# comment',
3459 ], 'E402:')
3460
3461 CheckScriptSuccess([
3462 'vim9script',
3463 'syntax match Word /pat/ contains=Something # comment',
3464 ])
3465 CheckScriptFailure([
3466 'vim9script',
3467 'syntax match Word /pat/ contains=Something# comment',
3468 ], 'E475:')
3469 CheckScriptFailure([
3470 'vim9script',
3471 'syntax match Word /pat/ contains= # comment',
3472 ], 'E406:')
3473 CheckScriptFailure([
3474 'vim9script',
3475 'syntax match Word /pat/ contains=# comment',
3476 ], 'E475:')
3477
3478 CheckScriptSuccess([
3479 'vim9script',
3480 'syntax region Word start=/pat/ end=/pat/ # comment',
3481 ])
3482 CheckScriptFailure([
3483 'vim9script',
3484 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02003485 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003486
3487 CheckScriptSuccess([
3488 'vim9script',
3489 'syntax sync # comment',
3490 ])
3491 CheckScriptFailure([
3492 'vim9script',
3493 'syntax sync# comment',
3494 ], 'E404:')
3495 CheckScriptSuccess([
3496 'vim9script',
3497 'syntax sync ccomment # comment',
3498 ])
3499 CheckScriptFailure([
3500 'vim9script',
3501 'syntax sync ccomment# comment',
3502 ], 'E404:')
3503
3504 CheckScriptSuccess([
3505 'vim9script',
3506 'syntax cluster Some contains=Word # comment',
3507 ])
3508 CheckScriptFailure([
3509 'vim9script',
3510 'syntax cluster Some contains=Word# comment',
3511 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003512
3513 CheckScriptSuccess([
3514 'vim9script',
3515 'command Echo echo # comment',
3516 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003517 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003518 ])
3519 CheckScriptFailure([
3520 'vim9script',
3521 'command Echo echo# comment',
3522 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003523 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003524 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003525
3526 var curdir = getcwd()
3527 CheckScriptSuccess([
3528 'command Echo cd " comment',
3529 'Echo',
3530 'delcommand Echo',
3531 ])
3532 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01003533 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003534 'command Echo cd # comment',
3535 'Echo',
3536 'delcommand Echo',
3537 ])
3538 CheckScriptFailure([
3539 'vim9script',
3540 'command Echo cd " comment',
3541 'Echo',
3542 ], 'E344:')
3543 delcommand Echo
3544 chdir(curdir)
3545
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003546 CheckScriptFailure([
3547 'vim9script',
3548 'command Echo# comment',
3549 ], 'E182:')
3550 CheckScriptFailure([
3551 'vim9script',
3552 'command Echo echo',
3553 'command Echo# comment',
3554 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003555 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003556
3557 CheckScriptSuccess([
3558 'vim9script',
3559 'function # comment',
3560 ])
3561 CheckScriptFailure([
3562 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02003563 'function " comment',
3564 ], 'E129:')
3565 CheckScriptFailure([
3566 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003567 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003568 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003569 CheckScriptSuccess([
3570 'vim9script',
3571 'function CheckScriptSuccess # comment',
3572 ])
3573 CheckScriptFailure([
3574 'vim9script',
3575 'function CheckScriptSuccess# comment',
3576 ], 'E488:')
3577
3578 CheckScriptSuccess([
3579 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003580 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003581 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003582 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003583 ])
3584 CheckScriptFailure([
3585 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003586 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003587 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003588 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003589 ], 'E488:')
3590
3591 CheckScriptSuccess([
3592 'vim9script',
3593 'call execute("ls") # comment',
3594 ])
3595 CheckScriptFailure([
3596 'vim9script',
3597 'call execute("ls")# comment',
3598 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02003599
3600 CheckScriptFailure([
3601 'def Test() " comment',
3602 'enddef',
3603 ], 'E488:')
3604 CheckScriptFailure([
3605 'vim9script',
3606 'def Test() " comment',
3607 'enddef',
3608 ], 'E488:')
3609
3610 CheckScriptSuccess([
3611 'func Test() " comment',
3612 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003613 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02003614 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02003615 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02003616 'vim9script',
3617 'func Test() " comment',
3618 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02003619 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02003620
3621 CheckScriptSuccess([
3622 'def Test() # comment',
3623 'enddef',
3624 ])
3625 CheckScriptFailure([
3626 'func Test() # comment',
3627 'endfunc',
3628 ], 'E488:')
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003629
3630 var lines =<< trim END
3631 vim9script
3632 syn region Text
3633 \ start='foo'
3634 #\ comment
3635 \ end='bar'
Bram Moolenaar5072b472021-06-03 21:56:10 +02003636 syn region Text start='foo'
3637 #\ comment
3638 \ end='bar'
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003639 END
3640 CheckScriptSuccess(lines)
3641
3642 lines =<< trim END
3643 vim9script
3644 syn region Text
3645 \ start='foo'
3646 "\ comment
3647 \ end='bar'
3648 END
3649 CheckScriptFailure(lines, 'E399:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003650enddef
3651
3652def Test_vim9_comment_gui()
3653 CheckCanRunGui
3654
3655 CheckScriptFailure([
3656 'vim9script',
3657 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003658 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003659 CheckScriptFailure([
3660 'vim9script',
3661 'gui -f#comment'
Bram Moolenaarb8bd2e62021-08-21 17:13:14 +02003662 ], 'E194:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003663enddef
3664
Bram Moolenaara26b9702020-04-18 19:53:28 +02003665def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02003666 au TabEnter *.vim g:entered = 1
3667 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02003668
3669 edit test.vim
3670 doautocmd TabEnter #comment
3671 assert_equal(1, g:entered)
3672
3673 doautocmd TabEnter f.x
3674 assert_equal(2, g:entered)
3675
3676 g:entered = 0
3677 doautocmd TabEnter f.x #comment
3678 assert_equal(2, g:entered)
3679
3680 assert_fails('doautocmd Syntax#comment', 'E216:')
3681
3682 au! TabEnter
3683 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003684
3685 CheckScriptSuccess([
3686 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02003687 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003688 'b:var = 456',
3689 'w:var = 777',
3690 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003691 'unlet g:var w:var # something',
3692 ])
3693
3694 CheckScriptFailure([
3695 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003696 'let var = 123',
3697 ], 'E1126: Cannot use :let in Vim9 script')
3698
3699 CheckScriptFailure([
3700 'vim9script',
3701 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003702 ], 'E1016: Cannot declare a global variable:')
3703
3704 CheckScriptFailure([
3705 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003706 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003707 ], 'E1016: Cannot declare a buffer variable:')
3708
3709 CheckScriptFailure([
3710 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003711 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003712 ], 'E1016: Cannot declare a window variable:')
3713
3714 CheckScriptFailure([
3715 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003716 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003717 ], 'E1016: Cannot declare a tab variable:')
3718
3719 CheckScriptFailure([
3720 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003721 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003722 ], 'E1016: Cannot declare a v: variable:')
3723
3724 CheckScriptFailure([
3725 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003726 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003727 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02003728
3729 CheckScriptFailure([
3730 'vim9script',
3731 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003732 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003733 ], 'E108:')
3734
3735 CheckScriptFailure([
3736 'let g:var = 123',
3737 'unlet g:var # something',
3738 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003739
3740 CheckScriptSuccess([
3741 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003742 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003743 ' echo "yes"',
3744 'elseif 2 #comment',
3745 ' echo "no"',
3746 'endif',
3747 ])
3748
3749 CheckScriptFailure([
3750 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003751 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003752 ' echo "yes"',
3753 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003754 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003755
3756 CheckScriptFailure([
3757 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003758 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003759 ' echo "yes"',
3760 'elseif 2#comment',
3761 ' echo "no"',
3762 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003763 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003764
3765 CheckScriptSuccess([
3766 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003767 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003768 ])
3769
3770 CheckScriptFailure([
3771 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003772 'var v = 1# comment6',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003773 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003774
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003775 CheckScriptSuccess([
3776 'vim9script',
3777 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003778 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003779 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003780 'dsearch /pat/ #comment',
3781 'bwipe!',
3782 ])
3783
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003784 CheckScriptFailure([
3785 'vim9script',
3786 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003787 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003788 ':$',
3789 'dsearch /pat/#comment',
3790 'bwipe!',
3791 ], 'E488:')
3792
3793 CheckScriptFailure([
3794 'vim9script',
3795 'func! SomeFunc()',
3796 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02003797enddef
3798
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003799def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003800 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003801 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02003802 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003803 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02003804 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003805 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02003806 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003807 END
3808 writefile(lines, 'Xfinished')
3809 source Xfinished
3810 assert_equal('two', g:res)
3811
3812 unlet g:res
3813 delete('Xfinished')
3814enddef
3815
Bram Moolenaara5d00772020-05-14 23:20:55 +02003816def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003817 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02003818 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02003819 def GetValue(): string
3820 return theVal
3821 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003822 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02003823 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02003824 theVal = 'else'
3825 g:laterVal = GetValue()
3826 END
3827 writefile(lines, 'Xforward')
3828 source Xforward
3829 assert_equal('something', g:initVal)
3830 assert_equal('else', g:laterVal)
3831
3832 unlet g:initVal
3833 unlet g:laterVal
3834 delete('Xforward')
3835enddef
3836
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003837def Test_source_vim9_from_legacy()
Bram Moolenaara6294952020-12-27 13:39:50 +01003838 var vim9_lines =<< trim END
3839 vim9script
3840 var local = 'local'
3841 g:global = 'global'
3842 export var exported = 'exported'
3843 export def GetText(): string
3844 return 'text'
3845 enddef
3846 END
3847 writefile(vim9_lines, 'Xvim9_script.vim')
3848
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003849 var legacy_lines =<< trim END
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003850 source Xvim9_script.vim
3851
3852 call assert_false(exists('local'))
3853 call assert_false(exists('exported'))
3854 call assert_false(exists('s:exported'))
3855 call assert_equal('global', global)
3856 call assert_equal('global', g:global)
3857
3858 " imported variable becomes script-local
3859 import exported from './Xvim9_script.vim'
3860 call assert_equal('exported', s:exported)
3861 call assert_false(exists('exported'))
3862
3863 " imported function becomes script-local
3864 import GetText from './Xvim9_script.vim'
3865 call assert_equal('text', s:GetText())
3866 call assert_false(exists('*GetText'))
3867 END
3868 writefile(legacy_lines, 'Xlegacy_script.vim')
3869
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003870 source Xlegacy_script.vim
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003871 assert_equal('global', g:global)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003872 unlet g:global
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003873
3874 delete('Xlegacy_script.vim')
3875 delete('Xvim9_script.vim')
3876enddef
Bram Moolenaara5d00772020-05-14 23:20:55 +02003877
Bram Moolenaare535db82021-03-31 21:07:24 +02003878def Test_declare_script_in_func()
3879 var lines =<< trim END
3880 vim9script
3881 func Declare()
3882 let s:local = 123
3883 endfunc
3884 Declare()
3885 assert_equal(123, local)
3886
3887 var error: string
3888 try
3889 local = 'asdf'
3890 catch
3891 error = v:exception
3892 endtry
3893 assert_match('E1012: Type mismatch; expected number but got string', error)
3894
3895 lockvar local
3896 try
3897 local = 999
3898 catch
3899 error = v:exception
3900 endtry
3901 assert_match('E741: Value is locked: local', error)
3902 END
3903 CheckScriptSuccess(lines)
3904enddef
3905
3906
Bram Moolenaar7d699702020-08-14 20:52:28 +02003907func Test_vim9script_not_global()
3908 " check that items defined in Vim9 script are script-local, not global
3909 let vim9lines =<< trim END
3910 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003911 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02003912 func TheFunc()
3913 echo 'local'
3914 endfunc
3915 def DefFunc()
3916 echo 'local'
3917 enddef
3918 END
3919 call writefile(vim9lines, 'Xvim9script.vim')
3920 source Xvim9script.vim
3921 try
3922 echo g:var
3923 assert_report('did not fail')
3924 catch /E121:/
3925 " caught
3926 endtry
3927 try
3928 call TheFunc()
3929 assert_report('did not fail')
3930 catch /E117:/
3931 " caught
3932 endtry
3933 try
3934 call DefFunc()
3935 assert_report('did not fail')
3936 catch /E117:/
3937 " caught
3938 endtry
3939
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003940 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02003941endfunc
3942
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02003943def Test_vim9_copen()
3944 # this was giving an error for setting w:quickfix_title
3945 copen
3946 quit
3947enddef
3948
Bram Moolenaar03290b82020-12-19 16:30:44 +01003949" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003950def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003951 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003952 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01003953 def some#gettest(): string
3954 return 'test'
3955 enddef
3956 g:some#name = 'name'
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003957 g:some#dict = {key: 'value'}
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003958
3959 def some#varargs(a1: string, ...l: list<string>): string
3960 return a1 .. l[0] .. l[1]
3961 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01003962 END
3963
3964 mkdir('Xdir/autoload', 'p')
3965 writefile(lines, 'Xdir/autoload/some.vim')
3966 var save_rtp = &rtp
3967 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3968
3969 assert_equal('test', g:some#gettest())
3970 assert_equal('name', g:some#name)
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003971 assert_equal('value', g:some#dict.key)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003972 g:some#other = 'other'
3973 assert_equal('other', g:some#other)
3974
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003975 assert_equal('abc', some#varargs('a', 'b', 'c'))
3976
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003977 # upper case script name works
3978 lines =<< trim END
3979 vim9script
3980 def Other#getOther(): string
3981 return 'other'
3982 enddef
3983 END
3984 writefile(lines, 'Xdir/autoload/Other.vim')
3985 assert_equal('other', g:Other#getOther())
3986
Bram Moolenaar03290b82020-12-19 16:30:44 +01003987 delete('Xdir', 'rf')
3988 &rtp = save_rtp
3989enddef
3990
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00003991" test disassembling an auto-loaded function starting with "debug"
3992def Test_vim9_autoload_disass()
3993 mkdir('Xdir/autoload', 'p')
3994 var save_rtp = &rtp
3995 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3996
3997 var lines =<< trim END
3998 vim9script
3999 def debugit#test(): string
4000 return 'debug'
4001 enddef
4002 END
4003 writefile(lines, 'Xdir/autoload/debugit.vim')
4004
4005 lines =<< trim END
4006 vim9script
4007 def profileit#test(): string
4008 return 'profile'
4009 enddef
4010 END
4011 writefile(lines, 'Xdir/autoload/profileit.vim')
4012
4013 lines =<< trim END
4014 vim9script
4015 assert_equal('debug', debugit#test())
4016 disass debugit#test
4017 assert_equal('profile', profileit#test())
4018 disass profileit#test
4019 END
4020 CheckScriptSuccess(lines)
4021
4022 delete('Xdir', 'rf')
4023 &rtp = save_rtp
4024enddef
4025
Bram Moolenaar03290b82020-12-19 16:30:44 +01004026" test using a vim9script that is auto-loaded from an autocmd
4027def Test_vim9_aucmd_autoload()
4028 var lines =<< trim END
4029 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004030 def foo#test()
4031 echomsg getreg('"')
4032 enddef
4033 END
4034
4035 mkdir('Xdir/autoload', 'p')
4036 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004037 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004038 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4039 augroup test
4040 autocmd TextYankPost * call foo#test()
4041 augroup END
4042
4043 normal Y
4044
4045 augroup test
4046 autocmd!
4047 augroup END
4048 delete('Xdir', 'rf')
4049 &rtp = save_rtp
4050enddef
4051
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004052" This was causing a crash because suppress_errthrow wasn't reset.
4053def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004054 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004055 vim9script
4056 def crash#func()
4057 try
4058 for x in List()
4059 endfor
4060 catch
4061 endtry
4062 g:ok = true
4063 enddef
4064 fu List()
4065 invalid
4066 endfu
4067 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01004068 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004069 catch /wontmatch/
4070 endtry
4071 END
4072 call mkdir('Xruntime/autoload', 'p')
4073 call writefile(lines, 'Xruntime/autoload/crash.vim')
4074
4075 # run in a separate Vim to avoid the side effects of assert_fails()
4076 lines =<< trim END
4077 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
4078 call crash#func()
4079 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004080 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004081 END
4082 writefile(lines, 'Xscript')
4083 RunVim([], [], '-S Xscript')
4084 assert_equal(['ok'], readfile('Xdidit'))
4085
4086 delete('Xdidit')
4087 delete('Xscript')
4088 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01004089
4090 lines =<< trim END
4091 vim9script
4092 var foo#bar = 'asdf'
4093 END
4094 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004095enddef
4096
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02004097def Test_script_var_in_autocmd()
4098 # using a script variable from an autocommand, defined in a :def function in a
4099 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004100 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02004101 let s:counter = 1
4102 def s:Func()
4103 au! CursorHold
4104 au CursorHold * s:counter += 1
4105 enddef
4106 call s:Func()
4107 doau CursorHold
4108 call assert_equal(2, s:counter)
4109 au! CursorHold
4110 END
4111 CheckScriptSuccess(lines)
4112enddef
4113
Bram Moolenaarb5841b92021-07-15 18:09:53 +02004114def Test_error_in_autoload_script()
4115 var save_rtp = &rtp
4116 var dir = getcwd() .. '/Xruntime'
4117 &rtp = dir
4118 mkdir(dir .. '/autoload', 'p')
4119
4120 var lines =<< trim END
4121 vim9script noclear
4122 def script#autoloaded()
4123 enddef
4124 def Broken()
4125 var x: any = ''
4126 eval x != 0
4127 enddef
4128 Broken()
4129 END
4130 writefile(lines, dir .. '/autoload/script.vim')
4131
4132 lines =<< trim END
4133 vim9script
4134 def CallAutoloaded()
4135 script#autoloaded()
4136 enddef
4137
4138 function Legacy()
4139 try
4140 call s:CallAutoloaded()
4141 catch
4142 call assert_match('E1030: Using a String as a Number', v:exception)
4143 endtry
4144 endfunction
4145
4146 Legacy()
4147 END
4148 CheckScriptSuccess(lines)
4149
4150 &rtp = save_rtp
4151 delete(dir, 'rf')
4152enddef
4153
Bram Moolenaar3896a102020-08-09 14:33:55 +02004154def Test_cmdline_win()
4155 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
4156 # the command line window.
4157 mkdir('rtp/syntax', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004158 var export_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02004159 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004160 export var That = 'yes'
Bram Moolenaar3896a102020-08-09 14:33:55 +02004161 END
4162 writefile(export_lines, 'rtp/syntax/Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004163 var import_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02004164 vim9script
4165 import That from './Xexport.vim'
4166 END
4167 writefile(import_lines, 'rtp/syntax/vim.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004168 var save_rtp = &rtp
Bram Moolenaar3896a102020-08-09 14:33:55 +02004169 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
4170 syntax on
4171 augroup CmdWin
4172 autocmd CmdwinEnter * g:got_there = 'yes'
4173 augroup END
4174 # this will open and also close the cmdline window
4175 feedkeys('q:', 'xt')
4176 assert_equal('yes', g:got_there)
4177
4178 augroup CmdWin
4179 au!
4180 augroup END
4181 &rtp = save_rtp
4182 delete('rtp', 'rf')
4183enddef
4184
Bram Moolenaare3d46852020-08-29 13:39:17 +02004185def Test_invalid_sid()
4186 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02004187
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004188 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02004189 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02004190 endif
4191 delete('Xdidit')
4192enddef
4193
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004194def Test_restoring_cpo()
4195 writefile(['vim9script', 'set nocp'], 'Xsourced')
4196 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
4197 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
4198 assert_equal(['done'], readfile('Xdone'))
4199 endif
4200 delete('Xsourced')
4201 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01004202 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01004203
4204 writefile(['vim9script'], 'XanotherScript')
4205 set cpo=aABceFsMny>
4206 edit XanotherScript
4207 so %
4208 assert_equal('aABceFsMny>', &cpo)
4209 :1del
4210 w
4211 so %
4212 assert_equal('aABceFsMny>', &cpo)
4213
4214 delete('XanotherScript')
4215 set cpo&vim
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004216enddef
4217
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004218" Use :function so we can use Check commands
4219func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004220 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004221 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004222
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004223 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004224 vim9script
4225 def script#func()
4226 enddef
4227 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004228 call mkdir('Xdir/autoload', 'p')
4229 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004230
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004231 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004232 vim9script
4233 set cpo+=M
4234 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02004235 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004236 setline(1, 'some text')
4237 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004238 call writefile(lines, 'XTest_redraw_cpo')
4239 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
4240 call term_sendkeys(buf, "V:")
4241 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004242
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004243 " clean up
4244 call term_sendkeys(buf, "\<Esc>u")
4245 call StopVimInTerminal(buf)
4246 call delete('XTest_redraw_cpo')
4247 call delete('Xdir', 'rf')
4248endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004249
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004250
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02004251def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004252 var lines =<< trim END
4253 var name: any
4254 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02004255 END
4256 CheckDefAndScriptSuccess(lines)
4257enddef
4258
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02004259func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02004260 CheckRunVimInTerminal
4261
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02004262 " call indirectly to avoid compilation error for missing functions
4263 call Run_Test_define_func_at_command_line()
4264endfunc
4265
4266def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004267 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004268 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004269 func CheckAndQuit()
4270 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
4271 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
4272 endfunc
4273 END
4274 writefile([''], 'Xdidcmd')
4275 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01004276 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004277 # define Afunc() on the command line
4278 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
4279 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004280 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004281
4282 call StopVimInTerminal(buf)
4283 delete('XcallFunc')
4284 delete('Xdidcmd')
4285enddef
4286
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004287def Test_script_var_scope()
4288 var lines =<< trim END
4289 vim9script
4290 if true
4291 if true
4292 var one = 'one'
4293 echo one
4294 endif
4295 echo one
4296 endif
4297 END
4298 CheckScriptFailure(lines, 'E121:', 7)
4299
4300 lines =<< trim END
4301 vim9script
4302 if true
4303 if false
4304 var one = 'one'
4305 echo one
4306 else
4307 var one = 'one'
4308 echo one
4309 endif
4310 echo one
4311 endif
4312 END
4313 CheckScriptFailure(lines, 'E121:', 10)
4314
4315 lines =<< trim END
4316 vim9script
4317 while true
4318 var one = 'one'
4319 echo one
4320 break
4321 endwhile
4322 echo one
4323 END
4324 CheckScriptFailure(lines, 'E121:', 7)
4325
4326 lines =<< trim END
4327 vim9script
4328 for i in range(1)
4329 var one = 'one'
4330 echo one
4331 endfor
4332 echo one
4333 END
4334 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02004335
4336 lines =<< trim END
4337 vim9script
4338 {
4339 var one = 'one'
4340 assert_equal('one', one)
4341 }
4342 assert_false(exists('one'))
4343 assert_false(exists('s:one'))
4344 END
4345 CheckScriptSuccess(lines)
4346
4347 lines =<< trim END
4348 vim9script
4349 {
4350 var one = 'one'
4351 echo one
4352 }
4353 echo one
4354 END
4355 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004356enddef
4357
Bram Moolenaar352134b2020-10-17 22:04:08 +02004358def Test_catch_exception_in_callback()
4359 var lines =<< trim END
4360 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02004361 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02004362 try
4363 var x: string
4364 var y: string
4365 # this error should be caught with CHECKLEN
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00004366 var sl = ['']
4367 [x, y] = sl
Bram Moolenaar352134b2020-10-17 22:04:08 +02004368 catch
4369 g:caught = 'yes'
4370 endtry
4371 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004372 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02004373 feedkeys("\r", 'xt')
4374 END
4375 CheckScriptSuccess(lines)
4376
4377 unlet g:caught
4378enddef
4379
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004380def Test_no_unknown_error_after_error()
4381 if !has('unix') || !has('job')
4382 throw 'Skipped: not unix of missing +job feature'
4383 endif
Bram Moolenaarb16ff292021-09-26 20:14:39 +01004384 # FIXME: this check should not be needed
4385 if has('win32')
4386 throw 'Skipped: does not work on MS-Windows'
4387 endif
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004388 var lines =<< trim END
4389 vim9script
4390 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02004391 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004392 eval [][0]
4393 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02004394 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004395 sleep 1m
4396 source += l
4397 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004398 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01004399 while job_status(myjob) == 'run'
4400 sleep 10m
4401 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01004402 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02004403 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004404 END
4405 writefile(lines, 'Xdef')
4406 assert_fails('so Xdef', ['E684:', 'E1012:'])
4407 delete('Xdef')
4408enddef
4409
Bram Moolenaar4324d872020-12-01 20:12:24 +01004410def InvokeNormal()
4411 exe "norm! :m+1\r"
4412enddef
4413
4414def Test_invoke_normal_in_visual_mode()
4415 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
4416 new
4417 setline(1, ['aaa', 'bbb'])
4418 feedkeys("V\<F3>", 'xt')
4419 assert_equal(['bbb', 'aaa'], getline(1, 2))
4420 xunmap <F3>
4421enddef
4422
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004423def Test_white_space_after_command()
4424 var lines =<< trim END
4425 exit_cb: Func})
4426 END
4427 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01004428
4429 lines =<< trim END
4430 e#
4431 END
4432 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004433enddef
4434
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004435def Test_script_var_gone_when_sourced_twice()
4436 var lines =<< trim END
4437 vim9script
4438 if exists('g:guard')
4439 finish
4440 endif
4441 g:guard = 1
4442 var name = 'thename'
4443 def g:GetName(): string
4444 return name
4445 enddef
4446 def g:SetName(arg: string)
4447 name = arg
4448 enddef
4449 END
4450 writefile(lines, 'XscriptTwice.vim')
4451 so XscriptTwice.vim
4452 assert_equal('thename', g:GetName())
4453 g:SetName('newname')
4454 assert_equal('newname', g:GetName())
4455 so XscriptTwice.vim
4456 assert_fails('call g:GetName()', 'E1149:')
4457 assert_fails('call g:SetName("x")', 'E1149:')
4458
4459 delfunc g:GetName
4460 delfunc g:SetName
4461 delete('XscriptTwice.vim')
4462 unlet g:guard
4463enddef
4464
4465def Test_import_gone_when_sourced_twice()
4466 var exportlines =<< trim END
4467 vim9script
4468 if exists('g:guard')
4469 finish
4470 endif
4471 g:guard = 1
4472 export var name = 'someName'
4473 END
4474 writefile(exportlines, 'XexportScript.vim')
4475
4476 var lines =<< trim END
4477 vim9script
4478 import name from './XexportScript.vim'
4479 def g:GetName(): string
4480 return name
4481 enddef
4482 END
4483 writefile(lines, 'XscriptImport.vim')
4484 so XscriptImport.vim
4485 assert_equal('someName', g:GetName())
4486
4487 so XexportScript.vim
4488 assert_fails('call g:GetName()', 'E1149:')
4489
4490 delfunc g:GetName
4491 delete('XexportScript.vim')
4492 delete('XscriptImport.vim')
4493 unlet g:guard
4494enddef
4495
Bram Moolenaar10b94212021-02-19 21:42:57 +01004496def Test_unsupported_commands()
4497 var lines =<< trim END
4498 ka
4499 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004500 CheckDefFailure(lines, 'E476:')
4501 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaar10b94212021-02-19 21:42:57 +01004502
4503 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01004504 :1ka
4505 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004506 CheckDefFailure(lines, 'E476:')
4507 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaarada1d872021-02-20 08:16:51 +01004508
4509 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01004510 t
4511 END
4512 CheckDefFailure(lines, 'E1100:')
4513 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4514
4515 lines =<< trim END
4516 x
4517 END
4518 CheckDefFailure(lines, 'E1100:')
4519 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4520
4521 lines =<< trim END
4522 xit
4523 END
4524 CheckDefFailure(lines, 'E1100:')
4525 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4526enddef
4527
Bram Moolenaarc70fe462021-04-17 17:59:19 +02004528def Test_mapping_line_number()
4529 var lines =<< trim END
4530 vim9script
4531 def g:FuncA()
4532 # Some comment
4533 FuncB(0)
4534 enddef
4535 # Some comment
4536 def FuncB(
4537 # Some comment
4538 n: number
4539 )
4540 exe 'nno '
4541 # Some comment
4542 .. '<F3> a'
4543 .. 'b'
4544 .. 'c'
4545 enddef
4546 END
4547 CheckScriptSuccess(lines)
4548 var res = execute('verbose nmap <F3>')
4549 assert_match('No mapping found', res)
4550
4551 g:FuncA()
4552 res = execute('verbose nmap <F3>')
4553 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
4554
4555 nunmap <F3>
4556 delfunc g:FuncA
4557enddef
4558
Bram Moolenaardeb108b2021-07-08 17:35:36 +02004559def Test_option_set()
4560 # legacy script allows for white space
4561 var lines =<< trim END
4562 set foldlevel =11
4563 call assert_equal(11, &foldlevel)
4564 END
4565 CheckScriptSuccess(lines)
4566
4567 set foldlevel
4568 set foldlevel=12
4569 assert_equal(12, &foldlevel)
4570 set foldlevel+=2
4571 assert_equal(14, &foldlevel)
4572 set foldlevel-=3
4573 assert_equal(11, &foldlevel)
4574
4575 lines =<< trim END
4576 set foldlevel =1
4577 END
4578 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: =1')
4579
4580 lines =<< trim END
4581 set foldlevel +=1
4582 END
4583 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: +=1')
4584
4585 lines =<< trim END
4586 set foldlevel ^=1
4587 END
4588 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: ^=1')
4589
4590 lines =<< trim END
4591 set foldlevel -=1
4592 END
4593 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: -=1')
4594
4595 set foldlevel&
4596enddef
4597
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004598def Test_option_modifier()
Bram Moolenaar1594f312021-07-08 16:40:13 +02004599 # legacy script allows for white space
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004600 var lines =<< trim END
4601 set hlsearch & hlsearch !
4602 call assert_equal(1, &hlsearch)
4603 END
4604 CheckScriptSuccess(lines)
4605
Bram Moolenaar1594f312021-07-08 16:40:13 +02004606 set hlsearch
4607 set hlsearch!
4608 assert_equal(false, &hlsearch)
4609
4610 set hlsearch
4611 set hlsearch&
4612 assert_equal(false, &hlsearch)
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004613
4614 lines =<< trim END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004615 set hlsearch &
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004616 END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004617 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: &')
4618
4619 lines =<< trim END
4620 set hlsearch !
4621 END
4622 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: !')
4623
4624 set hlsearch&
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004625enddef
4626
Bram Moolenaarc03fe662021-07-11 16:52:45 +02004627" This must be called last, it may cause following :def functions to fail
4628def Test_xxx_echoerr_line_number()
4629 var lines =<< trim END
4630 echoerr 'some'
4631 .. ' error'
4632 .. ' continued'
4633 END
4634 CheckDefExecAndScriptFailure(lines, 'some error continued', 1)
4635enddef
4636
Bram Moolenaar9537e372021-12-10 21:05:53 +00004637func Test_debug_with_lambda()
Bram Moolenaar9fffef92021-12-10 16:55:58 +00004638 CheckRunVimInTerminal
4639
Bram Moolenaar9537e372021-12-10 21:05:53 +00004640 " call indirectly to avoid compilation error for missing functions
4641 call Run_Test_debug_with_lambda()
4642endfunc
4643
4644def Run_Test_debug_with_lambda()
Bram Moolenaar9fffef92021-12-10 16:55:58 +00004645 var lines =<< trim END
4646 vim9script
4647 def Func()
4648 var n = 0
4649 echo [0]->filter((_, v) => v == n)
4650 enddef
4651 breakadd func Func
4652 Func()
4653 END
4654 writefile(lines, 'XdebugFunc')
4655 var buf = RunVimInTerminal('-S XdebugFunc', {rows: 6, wait_for_ruler: 0})
4656 WaitForAssert(() => assert_match('^>', term_getline(buf, 6)))
4657
4658 term_sendkeys(buf, "cont\<CR>")
4659 WaitForAssert(() => assert_match('\[0\]', term_getline(buf, 5)))
4660
4661 StopVimInTerminal(buf)
4662 delete('XdebugFunc')
4663enddef
4664
Bram Moolenaar648594e2021-07-11 17:55:01 +02004665def ProfiledWithLambda()
Bram Moolenaard9162552021-07-11 15:26:13 +02004666 var n = 3
4667 echo [[1, 2], [3, 4]]->filter((_, l) => l[0] == n)
4668enddef
4669
Bram Moolenaar648594e2021-07-11 17:55:01 +02004670def ProfiledNested()
4671 var x = 0
4672 def Nested(): any
4673 return x
4674 enddef
4675 Nested()
4676enddef
4677
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004678def ProfiledNestedProfiled()
4679 var x = 0
4680 def Nested(): any
4681 return x
4682 enddef
4683 Nested()
4684enddef
4685
Dominique Pelle923dce22021-11-21 11:36:04 +00004686" Execute this near the end, profiling doesn't stop until Vim exits.
Bram Moolenaard9162552021-07-11 15:26:13 +02004687" This only tests that it works, not the profiling output.
4688def Test_xx_profile_with_lambda()
Bram Moolenaar4ece1522021-07-11 16:31:51 +02004689 CheckFeature profile
4690
Bram Moolenaard9162552021-07-11 15:26:13 +02004691 profile start Xprofile.log
Bram Moolenaar648594e2021-07-11 17:55:01 +02004692 profile func ProfiledWithLambda
4693 ProfiledWithLambda()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004694
Bram Moolenaar648594e2021-07-11 17:55:01 +02004695 profile func ProfiledNested
4696 ProfiledNested()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004697
4698 # Also profile the nested function. Use a different function, although the
4699 # contents is the same, to make sure it was not already compiled.
4700 profile func *
4701 ProfiledNestedProfiled()
4702
4703 profdel func *
4704 profile pause
Bram Moolenaard9162552021-07-11 15:26:13 +02004705enddef
4706
Bram Moolenaar585fea72020-04-02 22:33:21 +02004707" Keep this last, it messes up highlighting.
4708def Test_substitute_cmd()
4709 new
4710 setline(1, 'something')
4711 :substitute(some(other(
4712 assert_equal('otherthing', getline(1))
4713 bwipe!
4714
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004715 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004716 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02004717 vim9script
4718 new
4719 setline(1, 'something')
4720 :substitute(some(other(
4721 assert_equal('otherthing', getline(1))
4722 bwipe!
4723 END
4724 writefile(lines, 'Xvim9lines')
4725 source Xvim9lines
4726
4727 delete('Xvim9lines')
4728enddef
4729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker