blob: 1810c5bc82297ad39978e1b3ee76dcae86e41b72 [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
191 # TODO: does not work yet
192 # constlist[1][1] = 88
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200193 var cl = constlist[1]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200194 cl[1] = 88
195 constlist->assert_equal([1, [77, 88], 3])
196
Bram Moolenaare0de1712020-12-02 17:36:54 +0100197 var vardict = {five: 5, six: 6}
198 const constdict = {one: 1, two: vardict, three: 3}
Bram Moolenaar71abe482020-09-14 22:28:30 +0200199 vardict['five'] = 55
200 # TODO: does not work yet
201 # constdict['two']['six'] = 66
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200202 var cd = constdict['two']
Bram Moolenaar71abe482020-09-14 22:28:30 +0200203 cd['six'] = 66
Bram Moolenaare0de1712020-12-02 17:36:54 +0100204 constdict->assert_equal({one: 1, two: {five: 55, six: 66}, three: 3})
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200205 END
206 CheckDefAndScriptSuccess(lines)
Bram Moolenaar08052222020-09-14 17:04:31 +0200207enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200209def Test_const_bang()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200210 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200211 const var = 234
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200212 var = 99
213 END
214 CheckDefExecFailure(lines, 'E1018:', 2)
215 CheckScriptFailure(['vim9script'] + lines, 'E46:', 3)
216
217 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200218 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200219 ll[0] = 99
220 END
221 CheckDefExecFailure(lines, 'E1119:', 2)
222 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
223
224 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200225 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200226 ll[3] = 99
227 END
228 CheckDefExecFailure(lines, 'E1118:', 2)
229 CheckScriptFailure(['vim9script'] + lines, 'E684:', 3)
230
231 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100232 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200233 dd["one"] = 99
234 END
235 CheckDefExecFailure(lines, 'E1121:', 2)
236 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
237
238 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100239 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200240 dd["three"] = 99
241 END
242 CheckDefExecFailure(lines, 'E1120:')
243 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
244enddef
245
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200246def Test_range_no_colon()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200247 CheckDefFailure(['%s/a/b/'], 'E1050:')
248 CheckDefFailure(['+ s/a/b/'], 'E1050:')
249 CheckDefFailure(['- s/a/b/'], 'E1050:')
250 CheckDefFailure(['. s/a/b/'], 'E1050:')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200251enddef
252
253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254def Test_block()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200255 var outer = 1
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 {
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200257 var inner = 2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258 assert_equal(1, outer)
259 assert_equal(2, inner)
260 }
261 assert_equal(1, outer)
Bram Moolenaar3f1e9f02021-02-27 22:36:43 +0100262
263 {|echo 'yes'|}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264enddef
265
Bram Moolenaar08052222020-09-14 17:04:31 +0200266def Test_block_failure()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200267 CheckDefFailure(['{', 'var inner = 1', '}', 'echo inner'], 'E1001:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200268 CheckDefFailure(['}'], 'E1025:')
269 CheckDefFailure(['{', 'echo 1'], 'E1026:')
270enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200272def Test_block_local_vars()
273 var lines =<< trim END
274 vim9script
Bram Moolenaared234f22020-10-15 20:42:20 +0200275 v:testing = 1
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200276 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200277 var text = ['hello']
278 def SayHello(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200279 return text
280 enddef
281 def SetText(v: string)
Bram Moolenaared234f22020-10-15 20:42:20 +0200282 text = [v]
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200283 enddef
284 endif
285
286 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200287 var text = ['again']
288 def SayAgain(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200289 return text
290 enddef
291 endif
Bram Moolenaared234f22020-10-15 20:42:20 +0200292
293 # test that the "text" variables are not cleaned up
294 test_garbagecollect_now()
295
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200296 defcompile
297
Bram Moolenaared234f22020-10-15 20:42:20 +0200298 assert_equal(['hello'], SayHello())
299 assert_equal(['again'], SayAgain())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200300
301 SetText('foobar')
Bram Moolenaared234f22020-10-15 20:42:20 +0200302 assert_equal(['foobar'], SayHello())
303
304 call writefile(['ok'], 'Xdidit')
305 qall!
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200306 END
Bram Moolenaared234f22020-10-15 20:42:20 +0200307
308 # need to execute this with a separate Vim instance to avoid the current
309 # context gets garbage collected.
310 writefile(lines, 'Xscript')
311 RunVim([], [], '-S Xscript')
312 assert_equal(['ok'], readfile('Xdidit'))
313
314 delete('Xscript')
315 delete('Xdidit')
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200316enddef
317
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200318def Test_block_local_vars_with_func()
319 var lines =<< trim END
320 vim9script
321 if true
322 var foo = 'foo'
323 if true
324 var bar = 'bar'
325 def Func(): list<string>
326 return [foo, bar]
327 enddef
328 endif
329 endif
330 # function is compiled here, after blocks have finished, can still access
331 # "foo" and "bar"
332 assert_equal(['foo', 'bar'], Func())
333 END
334 CheckScriptSuccess(lines)
335enddef
336
Bram Moolenaard032f342020-07-18 18:13:02 +0200337func g:NoSuchFunc()
338 echo 'none'
339endfunc
340
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100341def Test_try_catch_throw()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200342 var l = []
Bram Moolenaar7a092242020-04-16 22:10:49 +0200343 try # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344 add(l, '1')
345 throw 'wrong'
346 add(l, '2')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200347 catch # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100348 add(l, v:exception)
Bram Moolenaar7a092242020-04-16 22:10:49 +0200349 finally # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100350 add(l, '3')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200351 endtry # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100352 assert_equal(['1', 'wrong', '3'], l)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200353
Bram Moolenaare8593122020-07-18 15:17:02 +0200354 l = []
355 try
356 try
357 add(l, '1')
358 throw 'wrong'
359 add(l, '2')
360 catch /right/
361 add(l, v:exception)
362 endtry
363 catch /wrong/
364 add(l, 'caught')
Bram Moolenaar373863e2020-09-26 17:20:53 +0200365 fina
Bram Moolenaare8593122020-07-18 15:17:02 +0200366 add(l, 'finally')
367 endtry
368 assert_equal(['1', 'caught', 'finally'], l)
369
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200370 var n: number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200371 try
372 n = l[3]
373 catch /E684:/
374 n = 99
375 endtry
376 assert_equal(99, n)
377
Bram Moolenaar69f70502021-01-01 16:10:46 +0100378 var done = 'no'
379 if 0
380 try | catch | endtry
381 else
382 done = 'yes'
383 endif
384 assert_equal('yes', done)
385
386 done = 'no'
387 if 1
388 done = 'yes'
389 else
390 try | catch | endtry
391 done = 'never'
392 endif
393 assert_equal('yes', done)
394
395 if 1
396 else
397 try | catch /pat/ | endtry
398 try | catch /pat/
399 endtry
400 try
401 catch /pat/ | endtry
402 try
403 catch /pat/
404 endtry
405 endif
406
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200407 try
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200408 # string slice returns a string, not a number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200409 n = g:astring[3]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200410 catch /E1012:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200411 n = 77
412 endtry
413 assert_equal(77, n)
414
415 try
416 n = l[g:astring]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200417 catch /E1012:/
Bram Moolenaar56acb092020-08-16 14:48:19 +0200418 n = 88
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200419 endtry
Bram Moolenaar56acb092020-08-16 14:48:19 +0200420 assert_equal(88, n)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200421
422 try
423 n = s:does_not_exist
424 catch /E121:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200425 n = 111
426 endtry
427 assert_equal(111, n)
428
429 try
430 n = g:does_not_exist
431 catch /E121:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200432 n = 121
433 endtry
434 assert_equal(121, n)
435
Bram Moolenaare0de1712020-12-02 17:36:54 +0100436 var d = {one: 1}
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200437 try
438 n = d[g:astring]
439 catch /E716:/
440 n = 222
441 endtry
442 assert_equal(222, n)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200443
444 try
445 n = -g:astring
446 catch /E39:/
447 n = 233
448 endtry
449 assert_equal(233, n)
450
451 try
452 n = +g:astring
453 catch /E1030:/
454 n = 244
455 endtry
456 assert_equal(244, n)
457
458 try
459 n = +g:alist
460 catch /E745:/
461 n = 255
462 endtry
463 assert_equal(255, n)
464
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200465 var nd: dict<any>
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200466 try
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100467 nd = {[g:alist]: 1}
468 catch /E1105:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200469 n = 266
470 endtry
471 assert_equal(266, n)
472
473 try
474 [n] = [1, 2, 3]
475 catch /E1093:/
476 n = 277
477 endtry
478 assert_equal(277, n)
479
Bram Moolenaare8593122020-07-18 15:17:02 +0200480 try
481 &ts = g:astring
Bram Moolenaar5e654232020-09-16 15:22:00 +0200482 catch /E1012:/
Bram Moolenaare8593122020-07-18 15:17:02 +0200483 n = 288
484 endtry
485 assert_equal(288, n)
486
487 try
488 &backspace = 'asdf'
489 catch /E474:/
490 n = 299
491 endtry
492 assert_equal(299, n)
493
494 l = [1]
495 try
496 l[3] = 3
497 catch /E684:/
498 n = 300
499 endtry
500 assert_equal(300, n)
501
502 try
Bram Moolenaare8593122020-07-18 15:17:02 +0200503 unlet g:does_not_exist
504 catch /E108:/
505 n = 322
506 endtry
507 assert_equal(322, n)
508
509 try
Bram Moolenaar2bede172020-11-19 18:53:18 +0100510 d = {text: 1, [g:astring]: 2}
Bram Moolenaare8593122020-07-18 15:17:02 +0200511 catch /E721:/
512 n = 333
513 endtry
514 assert_equal(333, n)
515
516 try
517 l = DeletedFunc()
518 catch /E933:/
519 n = 344
520 endtry
521 assert_equal(344, n)
Bram Moolenaard032f342020-07-18 18:13:02 +0200522
523 try
524 echo len(v:true)
525 catch /E701:/
526 n = 355
527 endtry
528 assert_equal(355, n)
529
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200530 var P = function('g:NoSuchFunc')
Bram Moolenaard032f342020-07-18 18:13:02 +0200531 delfunc g:NoSuchFunc
532 try
533 echo P()
534 catch /E117:/
535 n = 366
536 endtry
537 assert_equal(366, n)
538
539 try
540 echo g:NoSuchFunc()
541 catch /E117:/
542 n = 377
543 endtry
544 assert_equal(377, n)
545
546 try
547 echo g:alist + 4
548 catch /E745:/
549 n = 388
550 endtry
551 assert_equal(388, n)
552
553 try
554 echo 4 + g:alist
555 catch /E745:/
556 n = 399
557 endtry
558 assert_equal(399, n)
559
560 try
561 echo g:alist.member
562 catch /E715:/
563 n = 400
564 endtry
565 assert_equal(400, n)
566
567 try
568 echo d.member
569 catch /E716:/
570 n = 411
571 endtry
572 assert_equal(411, n)
Bram Moolenaard9d77892021-02-12 21:32:47 +0100573
574 var counter = 0
575 for i in range(4)
576 try
577 eval [][0]
578 catch
579 endtry
580 counter += 1
581 endfor
582 assert_equal(4, counter)
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100583
584 # return in finally after empty catch
585 def ReturnInFinally(): number
586 try
587 finally
588 return 4
589 endtry
590 return 2
591 enddef
592 assert_equal(4, ReturnInFinally())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593enddef
594
Bram Moolenaar2e34c342021-03-14 12:13:33 +0100595" :while at the very start of a function that :continue jumps to
596def TryContinueFunc()
597 while g:Count < 2
598 g:sequence ..= 't'
599 try
600 echoerr 'Test'
601 catch
602 g:Count += 1
603 g:sequence ..= 'c'
604 continue
605 endtry
606 g:sequence ..= 'e'
607 g:Count += 1
608 endwhile
609enddef
610
611def Test_continue_in_try_in_while()
612 g:Count = 0
613 g:sequence = ''
614 TryContinueFunc()
615 assert_equal('tctc', g:sequence)
616 unlet g:Count
617 unlet g:sequence
618enddef
619
Bram Moolenaar9cb577a2021-02-22 22:45:10 +0100620def Test_nocatch_return_in_try()
621 # return in try block returns normally
622 def ReturnInTry(): string
623 try
624 return '"some message"'
625 catch
626 endtry
627 return 'not reached'
628 enddef
629 exe 'echoerr ' .. ReturnInTry()
630enddef
631
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100632def Test_cnext_works_in_catch()
633 var lines =<< trim END
634 vim9script
635 au BufEnter * eval 0
636 writefile(['text'], 'Xfile1')
637 writefile(['text'], 'Xfile2')
638 var items = [
639 {lnum: 1, filename: 'Xfile1', valid: true},
640 {lnum: 1, filename: 'Xfile2', valid: true}
641 ]
642 setqflist([], ' ', {items: items})
643 cwindow
644
645 def CnextOrCfirst()
646 # if cnext fails, cfirst is used
647 try
648 cnext
649 catch
650 cfirst
651 endtry
652 enddef
653
654 CnextOrCfirst()
655 CnextOrCfirst()
656 writefile([getqflist({idx: 0}).idx], 'Xresult')
657 qall
658 END
659 writefile(lines, 'XCatchCnext')
660 RunVim([], [], '--clean -S XCatchCnext')
661 assert_equal(['1'], readfile('Xresult'))
662
663 delete('Xfile1')
664 delete('Xfile2')
665 delete('XCatchCnext')
666 delete('Xresult')
667enddef
668
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100669def Test_throw_skipped()
670 if 0
671 throw dontgethere
672 endif
673enddef
674
Bram Moolenaar8f81b222021-01-14 21:47:06 +0100675def Test_nocatch_throw_silenced()
676 var lines =<< trim END
677 vim9script
678 def Func()
679 throw 'error'
680 enddef
681 silent! Func()
682 END
683 writefile(lines, 'XthrowSilenced')
684 source XthrowSilenced
685 delete('XthrowSilenced')
686enddef
687
Bram Moolenaare8593122020-07-18 15:17:02 +0200688def DeletedFunc(): list<any>
689 return ['delete me']
690enddef
691defcompile
692delfunc DeletedFunc
693
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100694def ThrowFromDef()
Bram Moolenaara72cfb82020-04-23 17:07:30 +0200695 throw "getout" # comment
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100696enddef
697
698func CatchInFunc()
699 try
700 call ThrowFromDef()
701 catch
702 let g:thrown_func = v:exception
703 endtry
704endfunc
705
706def CatchInDef()
707 try
708 ThrowFromDef()
709 catch
710 g:thrown_def = v:exception
711 endtry
712enddef
713
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100714def ReturnFinally(): string
715 try
716 return 'intry'
Bram Moolenaar373863e2020-09-26 17:20:53 +0200717 finall
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100718 g:in_finally = 'finally'
719 endtry
720 return 'end'
721enddef
722
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100723def Test_try_catch_nested()
724 CatchInFunc()
725 assert_equal('getout', g:thrown_func)
726
727 CatchInDef()
728 assert_equal('getout', g:thrown_def)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100729
730 assert_equal('intry', ReturnFinally())
731 assert_equal('finally', g:in_finally)
732enddef
733
Bram Moolenaar9939f572020-09-16 22:29:52 +0200734def TryOne(): number
735 try
736 return 0
737 catch
738 endtry
739 return 0
740enddef
741
742def TryTwo(n: number): string
743 try
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200744 var x = {}
Bram Moolenaar9939f572020-09-16 22:29:52 +0200745 catch
746 endtry
747 return 'text'
748enddef
749
750def Test_try_catch_twice()
751 assert_equal('text', TryOne()->TryTwo())
752enddef
753
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100754def Test_try_catch_match()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200755 var seq = 'a'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100756 try
757 throw 'something'
758 catch /nothing/
759 seq ..= 'x'
760 catch /some/
761 seq ..= 'b'
762 catch /asdf/
763 seq ..= 'x'
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200764 catch ?a\?sdf?
765 seq ..= 'y'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100766 finally
767 seq ..= 'c'
768 endtry
769 assert_equal('abc', seq)
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100770enddef
771
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200772def Test_try_catch_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200773 CheckDefFailure(['catch'], 'E603:')
774 CheckDefFailure(['try', 'echo 0', 'catch', 'catch'], 'E1033:')
775 CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
776 CheckDefFailure(['finally'], 'E606:')
777 CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:')
778 CheckDefFailure(['endtry'], 'E602:')
779 CheckDefFailure(['while 1', 'endtry'], 'E170:')
780 CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:')
Bram Moolenaar13106602020-10-04 16:06:05 +0200781 CheckDefFailure(['if 1', 'endtry'], 'E171:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200782 CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:')
Bram Moolenaar585fea72020-04-02 22:33:21 +0200783
Bram Moolenaare4984292020-12-13 14:19:25 +0100784 CheckDefFailure(['throw'], 'E1143:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200785 CheckDefFailure(['throw xxx'], 'E1001:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200786enddef
787
Bram Moolenaar7c5b3c0362021-02-14 22:40:57 +0100788def Try_catch_skipped()
789 var l = []
790 try
791 finally
792 endtry
793
794 if 1
795 else
796 try
797 endtry
798 endif
799enddef
800
801" The skipped try/endtry was updating the wrong instruction.
802def Test_try_catch_skipped()
803 var instr = execute('disassemble Try_catch_skipped')
804 assert_match("NEWLIST size 0\n", instr)
805enddef
806
807
808
Bram Moolenaar006ad482020-06-30 20:55:15 +0200809def Test_throw_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200810 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200811 var lines =<< trim END
Bram Moolenaar006ad482020-06-30 20:55:15 +0200812 vim9script
813 try
814 throw 'one'
815 .. 'two'
816 catch
817 assert_equal('onetwo', v:exception)
818 endtry
819 END
820 CheckScriptSuccess(lines)
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200821
822 lines =<< trim END
823 vim9script
Bram Moolenaar352134b2020-10-17 22:04:08 +0200824 @r = ''
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200825 def Func()
826 throw @r
827 enddef
828 var result = ''
829 try
830 Func()
831 catch /E1129:/
832 result = 'caught'
833 endtry
834 assert_equal('caught', result)
835 END
836 CheckScriptSuccess(lines)
Bram Moolenaar006ad482020-06-30 20:55:15 +0200837enddef
838
Bram Moolenaared677f52020-08-12 16:38:10 +0200839def Test_error_in_nested_function()
Bram Moolenaar03dfde22021-02-14 13:17:22 +0100840 # an error in a nested :function aborts executing in the calling :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200841 var lines =<< trim END
Bram Moolenaared677f52020-08-12 16:38:10 +0200842 vim9script
843 def Func()
844 Error()
845 g:test_var = 1
846 enddef
847 func Error() abort
848 eval [][0]
849 endfunc
850 Func()
851 END
852 g:test_var = 0
853 CheckScriptFailure(lines, 'E684:')
854 assert_equal(0, g:test_var)
855enddef
856
Bram Moolenaar37c83712020-06-30 21:18:36 +0200857def Test_cexpr_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200858 # only checks line continuation
Bram Moolenaar37c83712020-06-30 21:18:36 +0200859 set errorformat=File\ %f\ line\ %l
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200860 var lines =<< trim END
Bram Moolenaar37c83712020-06-30 21:18:36 +0200861 vim9script
862 cexpr 'File'
863 .. ' someFile' ..
864 ' line 19'
865 assert_equal(19, getqflist()[0].lnum)
866 END
867 CheckScriptSuccess(lines)
868 set errorformat&
869enddef
870
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200871def Test_statusline_syntax()
872 # legacy syntax is used for 'statusline'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200873 var lines =<< trim END
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200874 vim9script
875 func g:Status()
876 return '%{"x" is# "x"}'
877 endfunc
878 set laststatus=2 statusline=%!Status()
879 redrawstatus
880 set laststatus statusline=
881 END
882 CheckScriptSuccess(lines)
883enddef
884
Bram Moolenaarb2097502020-07-19 17:17:02 +0200885def Test_list_vimscript()
886 # checks line continuation and comments
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200887 var lines =<< trim END
Bram Moolenaarb2097502020-07-19 17:17:02 +0200888 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200889 var mylist = [
Bram Moolenaarb2097502020-07-19 17:17:02 +0200890 'one',
891 # comment
892 'two', # empty line follows
893
894 'three',
895 ]
896 assert_equal(['one', 'two', 'three'], mylist)
897 END
898 CheckScriptSuccess(lines)
Bram Moolenaar66250c92020-08-20 15:02:42 +0200899
900 # check all lines from heredoc are kept
901 lines =<< trim END
902 # comment 1
903 two
904 # comment 3
905
906 five
907 # comment 6
908 END
909 assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines)
Bram Moolenaar4bce26b2021-01-22 22:06:56 +0100910
911 lines =<< trim END
912 [{
913 a: 0}]->string()->assert_equal("[{'a': 0}]")
914 END
915 CheckDefAndScriptSuccess(lines)
Bram Moolenaarb2097502020-07-19 17:17:02 +0200916enddef
917
Bram Moolenaar2a1381c2020-05-05 23:32:58 +0200918if has('channel')
919 let someJob = test_null_job()
Bram Moolenaar40ee4662020-05-05 22:08:26 +0200920
Bram Moolenaar2a1381c2020-05-05 23:32:58 +0200921 def FuncWithError()
922 echomsg g:someJob
923 enddef
Bram Moolenaar40ee4662020-05-05 22:08:26 +0200924
Bram Moolenaar2a1381c2020-05-05 23:32:58 +0200925 func Test_convert_emsg_to_exception()
926 try
927 call FuncWithError()
928 catch
929 call assert_match('Vim:E908:', v:exception)
930 endtry
931 endfunc
932endif
Bram Moolenaar40ee4662020-05-05 22:08:26 +0200933
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934let s:export_script_lines =<< trim END
935 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200936 var name: string = 'bob'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937 def Concat(arg: string): string
938 return name .. arg
939 enddef
Bram Moolenaar227a69d2020-05-15 18:17:28 +0200940 g:result = Concat('bie')
941 g:localname = name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942
943 export const CONST = 1234
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200944 export var exported = 9876
945 export var exp_name = 'John'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946 export def Exported(): string
947 return 'Exported'
948 enddef
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +0100949 export final theList = [1]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950END
951
Bram Moolenaarb3ca9822020-08-09 14:43:58 +0200952def Undo_export_script_lines()
953 unlet g:result
954 unlet g:localname
955enddef
956
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100957def Test_vim9_import_export()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200958 var import_script_lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 vim9script
960 import {exported, Exported} from './Xexport.vim'
961 g:imported = exported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +0100962 exported += 3
963 g:imported_added = exported
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964 g:imported_func = Exported()
Bram Moolenaar6e587dc2020-02-06 13:15:52 +0100965
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200966 def GetExported(): string
Bram Moolenaare0de1712020-12-02 17:36:54 +0100967 var local_dict = {ref: Exported}
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200968 return local_dict.ref()
969 enddef
970 g:funcref_result = GetExported()
971
Bram Moolenaar6e587dc2020-02-06 13:15:52 +0100972 import {exp_name} from './Xexport.vim'
973 g:imported_name = exp_name
974 exp_name ..= ' Doe'
975 g:imported_name_appended = exp_name
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100976 g:imported_later = exported
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +0100977
978 import theList from './Xexport.vim'
979 theList->add(2)
980 assert_equal([1, 2], theList)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 END
982
983 writefile(import_script_lines, 'Ximport.vim')
984 writefile(s:export_script_lines, 'Xexport.vim')
985
986 source Ximport.vim
987
988 assert_equal('bobbie', g:result)
989 assert_equal('bob', g:localname)
990 assert_equal(9876, g:imported)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +0100991 assert_equal(9879, g:imported_added)
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100992 assert_equal(9879, g:imported_later)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993 assert_equal('Exported', g:imported_func)
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200994 assert_equal('Exported', g:funcref_result)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +0100995 assert_equal('John', g:imported_name)
996 assert_equal('John Doe', g:imported_name_appended)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997 assert_false(exists('g:name'))
998
Bram Moolenaarb3ca9822020-08-09 14:43:58 +0200999 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001000 unlet g:imported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001001 unlet g:imported_added
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001002 unlet g:imported_later
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003 unlet g:imported_func
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001004 unlet g:imported_name g:imported_name_appended
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 delete('Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001006
Bram Moolenaar1c991142020-07-04 13:15:31 +02001007 # similar, with line breaks
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001008 var import_line_break_script_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001009 vim9script
1010 import {
1011 exported,
1012 Exported,
1013 }
1014 from
1015 './Xexport.vim'
1016 g:imported = exported
1017 exported += 5
1018 g:imported_added = exported
1019 g:imported_func = Exported()
1020 END
1021 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
1022 source Ximport_lbr.vim
1023
1024 assert_equal(9876, g:imported)
1025 assert_equal(9881, g:imported_added)
1026 assert_equal('Exported', g:imported_func)
1027
1028 # exported script not sourced again
1029 assert_false(exists('g:result'))
1030 unlet g:imported
1031 unlet g:imported_added
1032 unlet g:imported_func
1033 delete('Ximport_lbr.vim')
1034
1035 # import inside :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001036 var import_in_def_lines =<< trim END
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001037 vim9script
1038 def ImportInDef()
1039 import exported from './Xexport.vim'
1040 g:imported = exported
1041 exported += 7
1042 g:imported_added = exported
1043 enddef
1044 ImportInDef()
1045 END
1046 writefile(import_in_def_lines, 'Ximport2.vim')
1047 source Ximport2.vim
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001048 # TODO: this should be 9879
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001049 assert_equal(9876, g:imported)
1050 assert_equal(9883, g:imported_added)
1051 unlet g:imported
1052 unlet g:imported_added
1053 delete('Ximport2.vim')
1054
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001055 var import_star_as_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001056 vim9script
1057 import * as Export from './Xexport.vim'
1058 def UseExport()
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001059 g:imported_def = Export.exported
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001060 enddef
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001061 g:imported_script = Export.exported
1062 assert_equal(1, exists('Export.exported'))
1063 assert_equal(0, exists('Export.notexported'))
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001064 UseExport()
1065 END
1066 writefile(import_star_as_lines, 'Ximport.vim')
1067 source Ximport.vim
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001068 assert_equal(9883, g:imported_def)
1069 assert_equal(9883, g:imported_script)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001070
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001071 var import_star_as_lines_no_dot =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001072 vim9script
1073 import * as Export from './Xexport.vim'
1074 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001075 var dummy = 1
1076 var imported = Export + dummy
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001077 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001078 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001079 END
1080 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001081 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001082
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001083 var import_star_as_lines_dot_space =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001084 vim9script
1085 import * as Export from './Xexport.vim'
1086 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001087 var imported = Export . exported
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001088 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001089 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001090 END
1091 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001092 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001093
Bram Moolenaara6294952020-12-27 13:39:50 +01001094 var import_star_as_duplicated =<< trim END
1095 vim9script
1096 import * as Export from './Xexport.vim'
1097 var some = 'other'
1098 import * as Export from './Xexport.vim'
1099 defcompile
1100 END
1101 writefile(import_star_as_duplicated, 'Ximport.vim')
1102 assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim')
1103
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001104 var import_star_as_lines_script_no_dot =<< trim END
1105 vim9script
1106 import * as Export from './Xexport.vim'
1107 g:imported_script = Export exported
1108 END
1109 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
1110 assert_fails('source Ximport.vim', 'E1029:')
1111
1112 var import_star_as_lines_script_space_after_dot =<< trim END
1113 vim9script
1114 import * as Export from './Xexport.vim'
1115 g:imported_script = Export. exported
1116 END
1117 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
1118 assert_fails('source Ximport.vim', 'E1074:')
1119
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001120 var import_star_as_lines_missing_name =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001121 vim9script
1122 import * as Export from './Xexport.vim'
1123 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001124 var imported = Export.
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001125 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001126 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001127 END
1128 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001129 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001130
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001131 var import_star_as_lbr_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001132 vim9script
1133 import *
1134 as Export
1135 from
1136 './Xexport.vim'
1137 def UseExport()
1138 g:imported = Export.exported
1139 enddef
1140 UseExport()
1141 END
1142 writefile(import_star_as_lbr_lines, 'Ximport.vim')
1143 source Ximport.vim
1144 assert_equal(9883, g:imported)
1145
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001146 var import_star_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001147 vim9script
1148 import * from './Xexport.vim'
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001149 END
1150 writefile(import_star_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001151 assert_fails('source Ximport.vim', 'E1045:', '', 2, 'Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001152
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001153 # try to import something that exists but is not exported
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001154 var import_not_exported_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001155 vim9script
1156 import name from './Xexport.vim'
1157 END
1158 writefile(import_not_exported_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001159 assert_fails('source Ximport.vim', 'E1049:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001160
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001161 # try to import something that is already defined
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001162 var import_already_defined =<< trim END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001163 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001164 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001165 import exported from './Xexport.vim'
1166 END
1167 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001168 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001169
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001170 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001171 import_already_defined =<< trim END
1172 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001173 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001174 import * as exported from './Xexport.vim'
1175 END
1176 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001177 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001178
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001179 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001180 import_already_defined =<< trim END
1181 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001182 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001183 import {exported} from './Xexport.vim'
1184 END
1185 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001186 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001187
Bram Moolenaar918a4242020-12-06 14:37:08 +01001188 # try changing an imported const
1189 var import_assign_to_const =<< trim END
1190 vim9script
1191 import CONST from './Xexport.vim'
1192 def Assign()
1193 CONST = 987
1194 enddef
1195 defcompile
1196 END
1197 writefile(import_assign_to_const, 'Ximport.vim')
1198 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1199
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001200 # try changing an imported final
1201 var import_assign_to_final =<< trim END
1202 vim9script
1203 import theList from './Xexport.vim'
1204 def Assign()
1205 theList = [2]
1206 enddef
1207 defcompile
1208 END
1209 writefile(import_assign_to_final, 'Ximport.vim')
1210 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1211
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001212 # import a very long name, requires making a copy
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001213 var import_long_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001214 vim9script
1215 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
1216 END
1217 writefile(import_long_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001218 assert_fails('source Ximport.vim', 'E1048:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001219
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001220 var import_no_from_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001221 vim9script
1222 import name './Xexport.vim'
1223 END
1224 writefile(import_no_from_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001225 assert_fails('source Ximport.vim', 'E1070:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001226
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001227 var import_invalid_string_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001228 vim9script
1229 import name from Xexport.vim
1230 END
1231 writefile(import_invalid_string_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001232 assert_fails('source Ximport.vim', 'E1071:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001233
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001234 var import_wrong_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001235 vim9script
1236 import name from './XnoExport.vim'
1237 END
1238 writefile(import_wrong_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001239 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001240
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001241 var import_missing_comma_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001242 vim9script
1243 import {exported name} from './Xexport.vim'
1244 END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001245 writefile(import_missing_comma_lines, 'Ximport3.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001246 assert_fails('source Ximport3.vim', 'E1046:', '', 2, 'Ximport3.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001247
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001248 delete('Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001249 delete('Ximport3.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250 delete('Xexport.vim')
1251
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001252 # Check that in a Vim9 script 'cpo' is set to the Vim default.
Bram Moolenaar3e191692021-03-17 17:46:00 +01001253 # Flags added or removed are also applied to the restored value.
1254 set cpo=abcd
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001255 var lines =<< trim END
Bram Moolenaar750802b2020-02-23 18:08:33 +01001256 vim9script
1257 g:cpo_in_vim9script = &cpo
Bram Moolenaar3e191692021-03-17 17:46:00 +01001258 set cpo+=f
1259 set cpo-=c
1260 g:cpo_after_vim9script = &cpo
Bram Moolenaar750802b2020-02-23 18:08:33 +01001261 END
1262 writefile(lines, 'Xvim9_script')
1263 source Xvim9_script
Bram Moolenaar3e191692021-03-17 17:46:00 +01001264 assert_equal('fabd', &cpo)
Bram Moolenaar750802b2020-02-23 18:08:33 +01001265 set cpo&vim
1266 assert_equal(&cpo, g:cpo_in_vim9script)
Bram Moolenaar3e191692021-03-17 17:46:00 +01001267 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
1268 assert_equal(newcpo, g:cpo_after_vim9script)
1269
Bram Moolenaar750802b2020-02-23 18:08:33 +01001270 delete('Xvim9_script')
1271enddef
1272
Bram Moolenaar0a842842021-02-27 22:41:19 +01001273def Test_import_as()
1274 var export_lines =<< trim END
1275 vim9script
1276 export var one = 1
1277 export var yes = 'yes'
1278 END
1279 writefile(export_lines, 'XexportAs')
1280
1281 var import_lines =<< trim END
1282 vim9script
Bram Moolenaar6c7cc342021-04-17 16:38:50 +02001283 var one = 'notused'
1284 var yes = 777
Bram Moolenaar0a842842021-02-27 22:41:19 +01001285 import one as thatOne from './XexportAs'
1286 assert_equal(1, thatOne)
1287 import yes as yesYes from './XexportAs'
1288 assert_equal('yes', yesYes)
1289 END
1290 CheckScriptSuccess(import_lines)
1291
1292 import_lines =<< trim END
1293 vim9script
1294 import {one as thatOne, yes as yesYes} from './XexportAs'
1295 assert_equal(1, thatOne)
1296 assert_equal('yes', yesYes)
1297 assert_fails('echo one', 'E121:')
1298 assert_fails('echo yes', 'E121:')
1299 END
1300 CheckScriptSuccess(import_lines)
1301
1302 delete('XexportAs')
1303enddef
1304
Bram Moolenaar803af682020-08-05 16:20:03 +02001305func g:Trigger()
1306 source Ximport.vim
1307 return "echo 'yes'\<CR>"
1308endfunc
1309
1310def Test_import_export_expr_map()
1311 # check that :import and :export work when buffer is locked
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001312 var export_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001313 vim9script
1314 export def That(): string
1315 return 'yes'
1316 enddef
1317 END
1318 writefile(export_lines, 'Xexport_that.vim')
1319
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001320 var import_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001321 vim9script
1322 import That from './Xexport_that.vim'
1323 assert_equal('yes', That())
1324 END
1325 writefile(import_lines, 'Ximport.vim')
1326
1327 nnoremap <expr> trigger g:Trigger()
1328 feedkeys('trigger', "xt")
1329
Bram Moolenaar730b2482020-08-09 13:02:10 +02001330 delete('Xexport_that.vim')
Bram Moolenaar803af682020-08-05 16:20:03 +02001331 delete('Ximport.vim')
1332 nunmap trigger
1333enddef
1334
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001335def Test_import_in_filetype()
1336 # check that :import works when the buffer is locked
1337 mkdir('ftplugin', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001338 var export_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001339 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001340 export var That = 'yes'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001341 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001342 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001343
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001344 var import_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001345 vim9script
Bram Moolenaar730b2482020-08-09 13:02:10 +02001346 import That from './Xexport_ft.vim'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001347 assert_equal('yes', That)
1348 g:did_load_mytpe = 1
1349 END
1350 writefile(import_lines, 'ftplugin/qf.vim')
1351
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001352 var save_rtp = &rtp
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001353 &rtp = getcwd() .. ',' .. &rtp
1354
1355 filetype plugin on
1356 copen
1357 assert_equal(1, g:did_load_mytpe)
1358
1359 quit!
Bram Moolenaar730b2482020-08-09 13:02:10 +02001360 delete('Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001361 delete('ftplugin', 'rf')
1362 &rtp = save_rtp
1363enddef
1364
Bram Moolenaarefa94442020-08-08 22:16:00 +02001365def Test_use_import_in_mapping()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001366 var lines =<< trim END
Bram Moolenaarefa94442020-08-08 22:16:00 +02001367 vim9script
1368 export def Funcx()
1369 g:result = 42
1370 enddef
1371 END
1372 writefile(lines, 'XsomeExport.vim')
1373 lines =<< trim END
1374 vim9script
1375 import Funcx from './XsomeExport.vim'
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001376 nnoremap <F3> :call <sid>Funcx()<cr>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001377 END
1378 writefile(lines, 'Xmapscript.vim')
1379
1380 source Xmapscript.vim
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001381 feedkeys("\<F3>", "xt")
Bram Moolenaarefa94442020-08-08 22:16:00 +02001382 assert_equal(42, g:result)
1383
1384 unlet g:result
1385 delete('XsomeExport.vim')
1386 delete('Xmapscript.vim')
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001387 nunmap <F3>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001388enddef
1389
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01001390def Test_vim9script_mix()
1391 var lines =<< trim END
1392 if has(g:feature)
1393 " legacy script
1394 let g:legacy = 1
1395 finish
1396 endif
1397 vim9script
1398 g:legacy = 0
1399 END
1400 g:feature = 'eval'
1401 g:legacy = -1
1402 CheckScriptSuccess(lines)
1403 assert_equal(1, g:legacy)
1404
1405 g:feature = 'noteval'
1406 g:legacy = -1
1407 CheckScriptSuccess(lines)
1408 assert_equal(0, g:legacy)
1409enddef
1410
Bram Moolenaar750802b2020-02-23 18:08:33 +01001411def Test_vim9script_fails()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1413 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001414 CheckScriptFailure(['export var some = 123'], 'E1042:')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001415 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001416 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
Bram Moolenaar750802b2020-02-23 18:08:33 +01001417 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
1418
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001419 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001420 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1421
Bram Moolenaare2e40752020-09-04 21:18:46 +02001422 assert_fails('vim9script', 'E1038:')
1423 assert_fails('export something', 'E1043:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424enddef
1425
Bram Moolenaarf7d267e2020-06-17 12:04:54 +02001426func Test_import_fails_without_script()
Bram Moolenaar101f4812020-06-16 23:18:51 +02001427 CheckRunVimInTerminal
1428
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001429 " call indirectly to avoid compilation error for missing functions
Bram Moolenaarc620c052020-07-08 15:16:19 +02001430 call Run_Test_import_fails_on_command_line()
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001431endfunc
1432
Bram Moolenaarc620c052020-07-08 15:16:19 +02001433def Run_Test_import_fails_on_command_line()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001434 var export =<< trim END
Bram Moolenaar101f4812020-06-16 23:18:51 +02001435 vim9script
1436 export def Foo(): number
1437 return 0
1438 enddef
1439 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001440 writefile(export, 'XexportCmd.vim')
Bram Moolenaar101f4812020-06-16 23:18:51 +02001441
Bram Moolenaare0de1712020-12-02 17:36:54 +01001442 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001443 rows: 6, wait_for_ruler: 0})
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001444 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaar101f4812020-06-16 23:18:51 +02001445
Bram Moolenaar730b2482020-08-09 13:02:10 +02001446 delete('XexportCmd.vim')
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001447 StopVimInTerminal(buf)
1448enddef
Bram Moolenaar101f4812020-06-16 23:18:51 +02001449
Bram Moolenaar2b327002020-12-26 15:39:31 +01001450def Test_vim9script_reload_noclear()
1451 var lines =<< trim END
Bram Moolenaara6294952020-12-27 13:39:50 +01001452 vim9script
1453 export var exported = 'thexport'
1454 END
1455 writefile(lines, 'XExportReload')
1456 lines =<< trim END
Bram Moolenaar2b327002020-12-26 15:39:31 +01001457 vim9script noclear
1458 g:loadCount += 1
1459 var s:reloaded = 'init'
Bram Moolenaara6294952020-12-27 13:39:50 +01001460 import exported from './XExportReload'
Bram Moolenaar2b327002020-12-26 15:39:31 +01001461
1462 def Again(): string
1463 return 'again'
1464 enddef
1465
1466 if exists('s:loaded') | finish | endif
1467 var s:loaded = true
1468
1469 var s:notReloaded = 'yes'
1470 s:reloaded = 'first'
1471 def g:Values(): list<string>
Bram Moolenaara6294952020-12-27 13:39:50 +01001472 return [s:reloaded, s:notReloaded, Again(), Once(), exported]
Bram Moolenaar2b327002020-12-26 15:39:31 +01001473 enddef
1474
1475 def Once(): string
1476 return 'once'
1477 enddef
1478 END
1479 writefile(lines, 'XReloaded')
1480 g:loadCount = 0
1481 source XReloaded
1482 assert_equal(1, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001483 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001484 source XReloaded
1485 assert_equal(2, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001486 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001487 source XReloaded
1488 assert_equal(3, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001489 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001490
Bram Moolenaar48e11c12021-01-11 18:47:00 +01001491 delete('XReloaded')
Bram Moolenaara6294952020-12-27 13:39:50 +01001492 delete('XExportReload')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001493 delfunc g:Values
Bram Moolenaar2b327002020-12-26 15:39:31 +01001494 unlet g:loadCount
1495enddef
1496
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001497def Test_vim9script_reload_import()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001498 var lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 vim9script
1500 const var = ''
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001501 var valone = 1234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502 def MyFunc(arg: string)
1503 valone = 5678
1504 enddef
1505 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001506 var morelines =<< trim END
1507 var valtwo = 222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508 export def GetValtwo(): number
1509 return valtwo
1510 enddef
1511 END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001512 writefile(lines + morelines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 source Xreload.vim
1514 source Xreload.vim
1515 source Xreload.vim
1516
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001517 var testlines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 vim9script
1519 def TheFunc()
1520 import GetValtwo from './Xreload.vim'
1521 assert_equal(222, GetValtwo())
1522 enddef
1523 TheFunc()
1524 END
1525 writefile(testlines, 'Ximport.vim')
1526 source Ximport.vim
1527
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001528 # Test that when not using "morelines" GetValtwo() and valtwo are still
1529 # defined, because import doesn't reload a script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 writefile(lines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001531 source Ximport.vim
1532
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001533 # cannot declare a var twice
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001534 lines =<< trim END
1535 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001536 var valone = 1234
1537 var valone = 5678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 END
1539 writefile(lines, 'Xreload.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001540 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541
1542 delete('Xreload.vim')
1543 delete('Ximport.vim')
1544enddef
1545
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001546" if a script is reloaded with a script-local variable that changed its type, a
1547" compiled function using that variable must fail.
1548def Test_script_reload_change_type()
1549 var lines =<< trim END
1550 vim9script noclear
1551 var str = 'string'
1552 def g:GetStr(): string
1553 return str .. 'xxx'
1554 enddef
1555 END
1556 writefile(lines, 'Xreload.vim')
1557 source Xreload.vim
1558 echo g:GetStr()
1559
1560 lines =<< trim END
1561 vim9script noclear
1562 var str = 1234
1563 END
1564 writefile(lines, 'Xreload.vim')
1565 source Xreload.vim
1566 assert_fails('echo g:GetStr()', 'E1150:')
1567
1568 delfunc g:GetStr
1569 delete('Xreload.vim')
1570enddef
1571
Bram Moolenaarc970e422021-03-17 15:03:04 +01001572" Define CallFunc so that the test can be compiled
1573command CallFunc echo 'nop'
1574
1575def Test_script_reload_from_function()
1576 var lines =<< trim END
1577 vim9script
1578
1579 if exists('g:loaded')
1580 finish
1581 endif
1582 g:loaded = 1
1583 delcommand CallFunc
1584 command CallFunc Func()
1585 def Func()
Bram Moolenaara4c81be2021-03-17 15:23:16 +01001586 so XreloadFunc.vim
Bram Moolenaarc970e422021-03-17 15:03:04 +01001587 g:didTheFunc = 1
1588 enddef
1589 END
1590 writefile(lines, 'XreloadFunc.vim')
1591 source XreloadFunc.vim
1592 CallFunc
1593 assert_equal(1, g:didTheFunc)
1594
1595 delete('XreloadFunc.vim')
1596 delcommand CallFunc
1597 unlet g:loaded
1598 unlet g:didTheFunc
1599enddef
1600
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001601def Test_script_var_shadows_function()
1602 var lines =<< trim END
1603 vim9script
1604 def Func(): number
1605 return 123
1606 enddef
1607 var Func = 1
1608 END
1609 CheckScriptFailure(lines, 'E1041:', 5)
1610enddef
1611
Bram Moolenaar95006e32020-08-29 17:47:08 +02001612def s:RetSome(): string
1613 return 'some'
1614enddef
1615
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001616" Not exported function that is referenced needs to be accessed by the
1617" script-local name.
1618def Test_vim9script_funcref()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001619 var sortlines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001620 vim9script
1621 def Compare(i1: number, i2: number): number
Bram Moolenaarbed36b92020-07-07 23:31:36 +02001622 return i2 - i1
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001623 enddef
1624
1625 export def FastSort(): list<number>
1626 return range(5)->sort(Compare)
1627 enddef
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001628
1629 export def GetString(arg: string): string
1630 return arg
1631 enddef
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001632 END
1633 writefile(sortlines, 'Xsort.vim')
1634
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001635 var lines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001636 vim9script
1637 import FastSort from './Xsort.vim'
1638 def Test()
1639 g:result = FastSort()
1640 enddef
1641 Test()
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001642
1643 # using a function imported with "as"
1644 import * as anAlias from './Xsort.vim'
1645 assert_equal('yes', anAlias.GetString('yes'))
1646
1647 # using the function from a compiled function
1648 def TestMore(): string
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001649 var s = s:anAlias.GetString('foo')
1650 return s .. anAlias.GetString('bar')
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001651 enddef
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001652 assert_equal('foobar', TestMore())
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001653
1654 # error when using a function that isn't exported
1655 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001656 END
1657 writefile(lines, 'Xscript.vim')
1658
1659 source Xscript.vim
1660 assert_equal([4, 3, 2, 1, 0], g:result)
1661
1662 unlet g:result
1663 delete('Xsort.vim')
1664 delete('Xscript.vim')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001665
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001666 var Funcref = function('s:RetSome')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001667 assert_equal('some', Funcref())
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001668enddef
1669
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001670" Check that when searching for "FilterFunc" it finds the import in the
1671" script where FastFilter() is called from, both as a string and as a direct
1672" function reference.
Bram Moolenaarc620c052020-07-08 15:16:19 +02001673def Test_vim9script_funcref_other_script()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001674 var filterLines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001675 vim9script
1676 export def FilterFunc(idx: number, val: number): bool
1677 return idx % 2 == 1
1678 enddef
1679 export def FastFilter(): list<number>
1680 return range(10)->filter('FilterFunc')
1681 enddef
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001682 export def FastFilterDirect(): list<number>
1683 return range(10)->filter(FilterFunc)
1684 enddef
Bram Moolenaarc620c052020-07-08 15:16:19 +02001685 END
1686 writefile(filterLines, 'Xfilter.vim')
1687
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001688 var lines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001689 vim9script
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001690 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim'
Bram Moolenaarc620c052020-07-08 15:16:19 +02001691 def Test()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001692 var x: list<number> = FastFilter()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001693 enddef
1694 Test()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001695 def TestDirect()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001696 var x: list<number> = FastFilterDirect()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001697 enddef
1698 TestDirect()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001699 END
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001700 CheckScriptSuccess(lines)
Bram Moolenaarc620c052020-07-08 15:16:19 +02001701 delete('Xfilter.vim')
Bram Moolenaarc620c052020-07-08 15:16:19 +02001702enddef
1703
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001704def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001705 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001706 vim9script
1707 def FuncYes(): string
1708 return 'yes'
1709 enddef
1710 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001711 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001712 def FuncNo(): string
1713 return 'no'
1714 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001715 def g:DoCheck(no_exists: bool)
1716 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001717 assert_equal('no', FuncNo())
1718 enddef
1719 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001720 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001721 def g:DoCheck(no_exists: bool)
1722 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02001723 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001724 enddef
1725 END
1726
1727 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001728 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001729 source Xreloaded.vim
1730 g:DoCheck(true)
1731
1732 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001733 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001734 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001735 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001736
1737 # FuncNo() is back
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001738 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001739 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001740 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001741
1742 delete('Xreloaded.vim')
1743enddef
1744
Bram Moolenaar89483d42020-05-10 15:24:44 +02001745def Test_vim9script_reload_delvar()
1746 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001747 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02001748 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001749 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001750 END
1751 writefile(lines, 'XreloadVar.vim')
1752 source XreloadVar.vim
1753
1754 # now write the script using the same variable locally - works
1755 lines =<< trim END
1756 vim9script
1757 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001758 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001759 enddef
1760 END
1761 writefile(lines, 'XreloadVar.vim')
1762 source XreloadVar.vim
1763
1764 delete('XreloadVar.vim')
1765enddef
1766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767def Test_import_absolute()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001768 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02001769 'vim9script',
1770 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
1771 'def UseExported()',
1772 ' g:imported_abs = exported',
1773 ' exported = 8888',
1774 ' g:imported_after = exported',
1775 'enddef',
1776 'UseExported()',
1777 'g:import_disassembled = execute("disass UseExported")',
1778 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 writefile(import_lines, 'Ximport_abs.vim')
1780 writefile(s:export_script_lines, 'Xexport_abs.vim')
1781
1782 source Ximport_abs.vim
1783
1784 assert_equal(9876, g:imported_abs)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001785 assert_equal(8888, g:imported_after)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001786 assert_match('<SNR>\d\+_UseExported\_s*' ..
1787 'g:imported_abs = exported\_s*' ..
1788 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1789 '1 STOREG g:imported_abs\_s*' ..
1790 'exported = 8888\_s*' ..
1791 '2 PUSHNR 8888\_s*' ..
1792 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1793 'g:imported_after = exported\_s*' ..
1794 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1795 '5 STOREG g:imported_after',
Bram Moolenaare6085c52020-04-12 20:19:16 +02001796 g:import_disassembled)
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001797
1798 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799 unlet g:imported_abs
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001800 unlet g:import_disassembled
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801
1802 delete('Ximport_abs.vim')
1803 delete('Xexport_abs.vim')
1804enddef
1805
1806def Test_import_rtp()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001807 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02001808 'vim9script',
1809 'import exported from "Xexport_rtp.vim"',
1810 'g:imported_rtp = exported',
1811 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812 writefile(import_lines, 'Ximport_rtp.vim')
1813 mkdir('import')
1814 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1815
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001816 var save_rtp = &rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 &rtp = getcwd()
1818 source Ximport_rtp.vim
1819 &rtp = save_rtp
1820
1821 assert_equal(9876, g:imported_rtp)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001823 Undo_export_script_lines()
1824 unlet g:imported_rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 delete('Ximport_rtp.vim')
Bram Moolenaar89483d42020-05-10 15:24:44 +02001826 delete('import', 'rf')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827enddef
1828
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001829def Test_import_compile_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001830 var export_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001831 'vim9script',
1832 'export def ExpFunc(): string',
1833 ' return notDefined',
1834 'enddef',
1835 ]
1836 writefile(export_lines, 'Xexported.vim')
1837
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001838 var import_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001839 'vim9script',
1840 'import ExpFunc from "./Xexported.vim"',
1841 'def ImpFunc()',
1842 ' echo ExpFunc()',
1843 'enddef',
1844 'defcompile',
1845 ]
1846 writefile(import_lines, 'Ximport.vim')
1847
1848 try
1849 source Ximport.vim
1850 catch /E1001/
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001851 # Error should be fore the Xexported.vim file.
Bram Moolenaar77072282020-09-16 17:55:40 +02001852 assert_match('E1001: Variable not found: notDefined', v:exception)
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001853 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1854 endtry
1855
1856 delete('Xexported.vim')
1857 delete('Ximport.vim')
1858enddef
1859
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001860def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001861 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001862 'vim9script',
1863 'def Func()',
1864 ' eval [][0]',
1865 'enddef',
1866 'Func()',
1867 ]
1868 writefile(lines, 'Xtestscript.vim')
1869
1870 for count in range(3)
1871 try
1872 source Xtestscript.vim
1873 catch /E684/
1874 # function name should contain <SNR> every time
1875 assert_match('E684: list index out of range', v:exception)
1876 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
1877 endtry
1878 endfor
1879
1880 delete('Xtestscript.vim')
1881enddef
1882
Bram Moolenaareef21022020-08-01 22:16:43 +02001883def Test_func_overrules_import_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001884 var export_lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02001885 vim9script
1886 export def Func()
1887 echo 'imported'
1888 enddef
1889 END
1890 writefile(export_lines, 'XexportedFunc.vim')
1891
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001892 var lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02001893 vim9script
1894 import Func from './XexportedFunc.vim'
1895 def Func()
1896 echo 'local to function'
1897 enddef
1898 END
1899 CheckScriptFailure(lines, 'E1073:')
1900
1901 lines =<< trim END
1902 vim9script
1903 import Func from './XexportedFunc.vim'
1904 def Outer()
1905 def Func()
1906 echo 'local to function'
1907 enddef
1908 enddef
1909 defcompile
1910 END
1911 CheckScriptFailure(lines, 'E1073:')
1912
1913 delete('XexportedFunc.vim')
1914enddef
1915
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02001916def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001917 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02001918 vim9script
1919 def Func()
1920 echo 'one'
1921 enddef
1922 def Func()
1923 echo 'two'
1924 enddef
1925 END
1926 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02001927
1928 lines =<< trim END
1929 vim9script
1930 def Foo(): string
1931 return 'foo'
1932 enddef
1933 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001934 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02001935 enddef
1936 defcompile
1937 END
1938 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02001939enddef
1940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001942 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001943 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 l->remove(0)
1945 l->add(5)
1946 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001947 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948enddef
1949
Bram Moolenaarae616492020-07-28 20:07:27 +02001950def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001951 CheckDefExecFailure(['a = 1'], 'E1100:')
1952 CheckDefExecFailure(['c = 1'], 'E1100:')
1953 CheckDefExecFailure(['i = 1'], 'E1100:')
1954 CheckDefExecFailure(['t = 1'], 'E1100:')
1955 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02001956
Bram Moolenaarae616492020-07-28 20:07:27 +02001957 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
1958 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02001959 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
1960 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02001961 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
1962 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01001963 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
1964 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02001965 CheckScriptFailure(['vim9script', 't'], 'E1100:')
1966 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
1967 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02001968enddef
1969
Bram Moolenaar158906c2020-02-06 20:39:45 +01001970def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001971 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01001972 if what == 1
1973 res = "one"
1974 elseif what == 2
1975 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01001976 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01001977 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01001978 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01001979 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01001980enddef
1981
Bram Moolenaar158906c2020-02-06 20:39:45 +01001982def Test_if_elseif_else()
1983 assert_equal('one', IfElse(1))
1984 assert_equal('two', IfElse(2))
1985 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001986enddef
1987
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02001988def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001989 CheckDefFailure(['elseif true'], 'E582:')
1990 CheckDefFailure(['else'], 'E581:')
1991 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01001992 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02001993 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01001994
1995 var lines =<< trim END
1996 var s = ''
1997 if s = ''
1998 endif
1999 END
2000 CheckDefFailure(lines, 'E488:')
2001
2002 lines =<< trim END
2003 var s = ''
2004 if s == ''
2005 elseif s = ''
2006 endif
2007 END
2008 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002009enddef
2010
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002011let g:bool_true = v:true
2012let g:bool_false = v:false
2013
2014def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002015 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002016 if true ? true : false
2017 res = true
2018 endif
2019 assert_equal(true, res)
2020
Bram Moolenaar585fea72020-04-02 22:33:21 +02002021 g:glob = 2
2022 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02002023 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002024 endif
2025 assert_equal(2, g:glob)
2026 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02002027 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002028 endif
2029 assert_equal(3, g:glob)
2030
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002031 res = false
2032 if g:bool_true ? true : false
2033 res = true
2034 endif
2035 assert_equal(true, res)
2036
2037 res = false
2038 if true ? g:bool_true : false
2039 res = true
2040 endif
2041 assert_equal(true, res)
2042
2043 res = false
2044 if true ? true : g:bool_false
2045 res = true
2046 endif
2047 assert_equal(true, res)
2048
2049 res = false
2050 if true ? false : true
2051 res = true
2052 endif
2053 assert_equal(false, res)
2054
2055 res = false
2056 if false ? false : true
2057 res = true
2058 endif
2059 assert_equal(true, res)
2060
2061 res = false
2062 if false ? true : false
2063 res = true
2064 endif
2065 assert_equal(false, res)
2066
2067 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002068 if has('xyz') ? true : false
2069 res = true
2070 endif
2071 assert_equal(false, res)
2072
2073 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002074 if true && true
2075 res = true
2076 endif
2077 assert_equal(true, res)
2078
2079 res = false
2080 if true && false
2081 res = true
2082 endif
2083 assert_equal(false, res)
2084
2085 res = false
2086 if g:bool_true && false
2087 res = true
2088 endif
2089 assert_equal(false, res)
2090
2091 res = false
2092 if true && g:bool_false
2093 res = true
2094 endif
2095 assert_equal(false, res)
2096
2097 res = false
2098 if false && false
2099 res = true
2100 endif
2101 assert_equal(false, res)
2102
2103 res = false
2104 if true || false
2105 res = true
2106 endif
2107 assert_equal(true, res)
2108
2109 res = false
2110 if g:bool_true || false
2111 res = true
2112 endif
2113 assert_equal(true, res)
2114
2115 res = false
2116 if true || g:bool_false
2117 res = true
2118 endif
2119 assert_equal(true, res)
2120
2121 res = false
2122 if false || false
2123 res = true
2124 endif
2125 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02002126
2127 # with constant "false" expression may be invalid so long as the syntax is OK
2128 if false | eval 0 | endif
2129 if false | eval burp + 234 | endif
2130 if false | echo burp 234 'asd' | endif
2131 if false
2132 burp
2133 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002134enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002135
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002136def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002137 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
2138 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
2139 CheckDefFailure(["if has('aaa'"], 'E110:')
2140 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002141enddef
2142
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002143def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002144 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002145 if i % 2
2146 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002147 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002148 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002149 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002150 endif
2151 x += 1
2152 else
2153 x += 1000
2154 endif
2155 return x
2156enddef
2157
2158def Test_nested_if()
2159 assert_equal(1, RunNested(1))
2160 assert_equal(1000, RunNested(2))
2161enddef
2162
Bram Moolenaarad39c092020-02-26 18:23:43 +01002163def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01002164 # missing argument is ignored
2165 execute
2166 execute # comment
2167
Bram Moolenaarad39c092020-02-26 18:23:43 +01002168 new
2169 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002170 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002171 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002172
Bram Moolenaard2c61702020-09-06 15:58:36 +02002173 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002174 assert_equal('execute-string', getline(1))
2175
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002176 var cmd1 = 'setline(1,'
2177 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002178 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01002179 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002180
Bram Moolenaard2c61702020-09-06 15:58:36 +02002181 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002182 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002183
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002184 var cmd_first = 'call '
2185 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002186 execute cmd_first .. cmd_last
2187 assert_equal('execute-var-var', getline(1))
2188 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02002189
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002190 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002191 execute 'echomsg' (n ? '"true"' : '"no"')
2192 assert_match('^true$', Screenline(&lines))
2193
Bram Moolenaare0de1712020-12-02 17:36:54 +01002194 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002195 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
2196
Bram Moolenaard2c61702020-09-06 15:58:36 +02002197 CheckDefFailure(['execute xxx'], 'E1001:', 1)
2198 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
2199 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002200enddef
2201
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002202def Test_execute_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002203 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002204 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002205 vim9script
2206 execute 'g:someVar'
2207 .. ' = ' ..
2208 '28'
2209 assert_equal(28, g:someVar)
2210 unlet g:someVar
2211 END
2212 CheckScriptSuccess(lines)
2213enddef
2214
Bram Moolenaarad39c092020-02-26 18:23:43 +01002215def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002216 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02002217 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002218 assert_match('^something$', Screenline(&lines))
2219
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002220 echo "some" # comment
2221 echon "thing"
2222 assert_match('^something$', Screenline(&lines))
2223
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002224 var str1 = 'some'
2225 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002226 echo str1 str2
2227 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002228
Bram Moolenaard2c61702020-09-06 15:58:36 +02002229 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01002230enddef
2231
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002232def Test_echomsg_cmd()
2233 echomsg 'some' 'more' # comment
2234 assert_match('^some more$', Screenline(&lines))
2235 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002236 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002237 assert_match('^some more$', Screenline(&lines))
2238
Bram Moolenaard2c61702020-09-06 15:58:36 +02002239 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002240enddef
2241
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002242def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002243 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002244 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002245 vim9script
2246 echomsg 'here'
2247 .. ' is ' ..
2248 'a message'
2249 assert_match('^here is a message$', Screenline(&lines))
2250 END
2251 CheckScriptSuccess(lines)
2252enddef
2253
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002254def Test_echoerr_cmd()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002255 try
2256 echoerr 'something' 'wrong' # comment
2257 catch
2258 assert_match('something wrong', v:exception)
2259 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002260enddef
2261
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002262def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002263 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002264 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002265 vim9script
2266 try
2267 echoerr 'this'
2268 .. ' is ' ..
2269 'wrong'
2270 catch
2271 assert_match('this is wrong', v:exception)
2272 endtry
2273 END
2274 CheckScriptSuccess(lines)
2275enddef
2276
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002277def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002278 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002279 vim9script
2280 new
2281 for var in range(0, 3)
2282 append(line('$'), var)
2283 endfor
2284 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2285 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01002286
2287 var result = ''
2288 for i in [1, 2, 3]
2289 var loop = ' loop ' .. i
2290 result ..= loop
2291 endfor
2292 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002293 END
2294 writefile(lines, 'Xvim9for.vim')
2295 source Xvim9for.vim
2296 delete('Xvim9for.vim')
2297enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002299def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002300 var lines =<< trim END
2301 var result = ''
2302 for cnt in range(7)
2303 if cnt == 4
2304 break
2305 endif
2306 if cnt == 2
2307 continue
2308 endif
2309 result ..= cnt .. '_'
2310 endfor
2311 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02002312
Bram Moolenaarf2253962021-04-13 20:53:13 +02002313 var concat = ''
2314 for str in eval('["one", "two"]')
2315 concat ..= str
2316 endfor
2317 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002318
Bram Moolenaarf2253962021-04-13 20:53:13 +02002319 var total = 0
2320 for nr in
2321 [1, 2, 3]
2322 total += nr
2323 endfor
2324 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002325
Bram Moolenaarf2253962021-04-13 20:53:13 +02002326 total = 0
2327 for nr
2328 in [1, 2, 3]
2329 total += nr
2330 endfor
2331 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002332
Bram Moolenaarf2253962021-04-13 20:53:13 +02002333 total = 0
2334 for nr
2335 in
2336 [1, 2, 3]
2337 total += nr
2338 endfor
2339 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01002340
Bram Moolenaara3589a02021-04-14 13:30:46 +02002341 # with type
2342 total = 0
2343 for n: number in [1, 2, 3]
2344 total += n
2345 endfor
2346 assert_equal(6, total)
2347
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002348 var chars = ''
2349 for s: string in 'foobar'
2350 chars ..= s
2351 endfor
2352 assert_equal('foobar', chars)
2353
Bram Moolenaara3589a02021-04-14 13:30:46 +02002354 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02002355 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02002356 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2357 res ..= n .. s
2358 endfor
2359 assert_equal('1a2b', res)
2360
2361 # loop over string
2362 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02002363 for c in 'aéc̀d'
2364 res ..= c .. '-'
2365 endfor
2366 assert_equal('a-é-c̀-d-', res)
2367
2368 res = ''
2369 for c in ''
2370 res ..= c .. '-'
2371 endfor
2372 assert_equal('', res)
2373
2374 res = ''
2375 for c in test_null_string()
2376 res ..= c .. '-'
2377 endfor
2378 assert_equal('', res)
2379
2380 var foo: list<dict<any>> = [
2381 {a: 'Cat'}
2382 ]
2383 for dd in foo
2384 dd.counter = 12
2385 endfor
2386 assert_equal([{a: 'Cat', counter: 12}], foo)
2387 END
2388 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002389enddef
2390
2391def Test_for_loop_fails()
Bram Moolenaar025cb1c2020-12-14 18:31:27 +01002392 CheckDefFailure(['for '], 'E1097:')
2393 CheckDefFailure(['for x'], 'E1097:')
2394 CheckDefFailure(['for x in'], 'E1097:')
Bram Moolenaar675f7162020-04-12 22:53:54 +02002395 CheckDefFailure(['for # in range(5)'], 'E690:')
2396 CheckDefFailure(['for i In range(5)'], 'E690:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002397 CheckDefFailure(['var x = 5', 'for x in range(5)'], 'E1017:')
Bram Moolenaar822ba242020-05-24 23:00:18 +02002398 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002399 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02002400 CheckDefFailure(['for i in xxx'], 'E1001:')
2401 CheckDefFailure(['endfor'], 'E588:')
2402 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002403
2404 # wrong type detected at compile time
2405 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2406
2407 # wrong type detected at runtime
2408 g:adict = {a: 1}
2409 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2410 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002411
2412 var lines =<< trim END
2413 var d: list<dict<any>> = [{a: 0}]
2414 for e in d
2415 e = {a: 0, b: ''}
2416 endfor
2417 END
2418 CheckDefAndScriptFailure2(lines, 'E1018:', 'E46:', 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002419
2420 lines =<< trim END
2421 for nr: number in ['foo']
2422 endfor
2423 END
2424 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002425enddef
2426
Bram Moolenaarea870692020-12-02 14:24:30 +01002427def Test_for_loop_script_var()
2428 # cannot use s:var in a :def function
2429 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1101:')
2430
2431 # can use s:var in Vim9 script, with or without s:
2432 var lines =<< trim END
2433 vim9script
2434 var total = 0
2435 for s:var in [1, 2, 3]
2436 total += s:var
2437 endfor
2438 assert_equal(6, total)
2439
2440 total = 0
2441 for var in [1, 2, 3]
2442 total += var
2443 endfor
2444 assert_equal(6, total)
2445 END
2446enddef
2447
Bram Moolenaar792f7862020-11-23 08:31:18 +01002448def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01002449 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002450 var result = []
2451 for [v1, v2] in [[1, 2], [3, 4]]
2452 result->add(v1)
2453 result->add(v2)
2454 endfor
2455 assert_equal([1, 2, 3, 4], result)
2456
2457 result = []
2458 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2459 result->add(v1)
2460 result->add(v2)
2461 result->add(v3)
2462 endfor
2463 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
2464
2465 result = []
2466 for [&ts, &sw] in [[1, 2], [3, 4]]
2467 result->add(&ts)
2468 result->add(&sw)
2469 endfor
2470 assert_equal([1, 2, 3, 4], result)
2471
2472 var slist: list<string>
2473 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
2474 slist->add($LOOPVAR)
2475 slist->add(@r)
2476 slist->add(v:errmsg)
2477 endfor
2478 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
2479
2480 slist = []
2481 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
2482 slist->add(g:globalvar)
2483 slist->add(b:bufvar)
2484 slist->add(w:winvar)
2485 slist->add(t:tabvar)
2486 endfor
2487 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01002488 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002489 END
2490 CheckDefAndScriptSuccess(lines)
2491
2492 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01002493 for [v1, v2] in [[1, 2, 3], [3, 4]]
2494 echo v1 v2
2495 endfor
2496 END
2497 CheckDefExecFailure(lines, 'E710:', 1)
2498
2499 lines =<< trim END
2500 for [v1, v2] in [[1], [3, 4]]
2501 echo v1 v2
2502 endfor
2503 END
2504 CheckDefExecFailure(lines, 'E711:', 1)
2505
2506 lines =<< trim END
2507 for [v1, v1] in [[1, 2], [3, 4]]
2508 echo v1
2509 endfor
2510 END
2511 CheckDefExecFailure(lines, 'E1017:', 1)
2512enddef
2513
Bram Moolenaarc150c092021-02-13 15:02:46 +01002514def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002515 var lines =<< trim END
2516 var looped = 0
2517 var cleanup = 0
2518 for i in range(3)
2519 looped += 1
2520 try
2521 eval [][0]
2522 catch
2523 continue
2524 finally
2525 cleanup += 1
2526 endtry
2527 endfor
2528 assert_equal(3, looped)
2529 assert_equal(3, cleanup)
2530 END
2531 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002532enddef
2533
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002534def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002535 var result = ''
2536 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002537 while cnt < 555
2538 if cnt == 3
2539 break
2540 endif
2541 cnt += 1
2542 if cnt == 2
2543 continue
2544 endif
2545 result ..= cnt .. '_'
2546 endwhile
2547 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002548
2549 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002550 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002551 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002552enddef
2553
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002554def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02002555 CheckDefFailure(['while xxx'], 'E1001:')
2556 CheckDefFailure(['endwhile'], 'E588:')
2557 CheckDefFailure(['continue'], 'E586:')
2558 CheckDefFailure(['if true', 'continue'], 'E586:')
2559 CheckDefFailure(['break'], 'E587:')
2560 CheckDefFailure(['if true', 'break'], 'E587:')
2561 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002562
2563 var lines =<< trim END
2564 var s = ''
2565 while s = ''
2566 endwhile
2567 END
2568 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002569enddef
2570
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002571def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002572 var caught = false
2573 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002574 try
2575 while 1
2576 x += 1
2577 if x == 100
2578 feedkeys("\<C-C>", 'Lt')
2579 endif
2580 endwhile
2581 catch
2582 caught = true
2583 assert_equal(100, x)
2584 endtry
2585 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02002586 # consume the CTRL-C
2587 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002588enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01002589
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002590def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002591 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002592 'one',
2593 'two',
2594 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002595 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002596 assert_equal(['one', 'two', 'three'], mylist)
2597
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002598 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01002599 ['one']: 1,
2600 ['two']: 2,
2601 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002602 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002603 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01002604 assert_equal({one: 1, two: 2, three: 3}, mydict)
2605 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002606 one: 1, # comment
2607 two: # comment
2608 2, # comment
2609 three: 3 # comment
2610 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002611 assert_equal({one: 1, two: 2, three: 3}, mydict)
2612 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002613 one: 1,
2614 two:
2615 2,
2616 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002617 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002618 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002619
2620 assert_equal(
2621 ['one', 'two', 'three'],
2622 split('one two three')
2623 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002624enddef
2625
Bram Moolenaar7a092242020-04-16 22:10:49 +02002626def Test_vim9_comment()
2627 CheckScriptSuccess([
2628 'vim9script',
2629 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002630 '#something',
2631 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002632 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002633
2634 split Xfile
2635 CheckScriptSuccess([
2636 'vim9script',
2637 'edit #something',
2638 ])
2639 CheckScriptSuccess([
2640 'vim9script',
2641 'edit #{something',
2642 ])
2643 close
2644
Bram Moolenaar7a092242020-04-16 22:10:49 +02002645 CheckScriptFailure([
2646 'vim9script',
2647 ':# something',
2648 ], 'E488:')
2649 CheckScriptFailure([
2650 '# something',
2651 ], 'E488:')
2652 CheckScriptFailure([
2653 ':# something',
2654 ], 'E488:')
2655
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002656 { # block start
2657 } # block end
2658 CheckDefFailure([
2659 '{# comment',
2660 ], 'E488:')
2661 CheckDefFailure([
2662 '{',
2663 '}# comment',
2664 ], 'E488:')
2665
2666 echo "yes" # comment
2667 CheckDefFailure([
2668 'echo "yes"# comment',
2669 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002670 CheckScriptSuccess([
2671 'vim9script',
2672 'echo "yes" # something',
2673 ])
2674 CheckScriptFailure([
2675 'vim9script',
2676 'echo "yes"# something',
2677 ], 'E121:')
2678 CheckScriptFailure([
2679 'vim9script',
2680 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002681 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002682 CheckScriptFailure([
2683 'echo "yes" # something',
2684 ], 'E121:')
2685
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002686 exe "echo" # comment
2687 CheckDefFailure([
2688 'exe "echo"# comment',
2689 ], 'E488:')
2690 CheckScriptSuccess([
2691 'vim9script',
2692 'exe "echo" # something',
2693 ])
2694 CheckScriptFailure([
2695 'vim9script',
2696 'exe "echo"# something',
2697 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002698 CheckScriptFailure([
2699 'vim9script',
2700 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002701 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002702 CheckScriptFailure([
2703 'exe "echo" # something',
2704 ], 'E121:')
2705
Bram Moolenaar7a092242020-04-16 22:10:49 +02002706 CheckDefFailure([
2707 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002708 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002709 'catch',
2710 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002711 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002712 CheckScriptFailure([
2713 'vim9script',
2714 'try# comment',
2715 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002716 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002717 CheckDefFailure([
2718 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002719 ' throw#comment',
2720 'catch',
2721 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002722 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002723 CheckDefFailure([
2724 'try',
2725 ' throw "yes"#comment',
2726 'catch',
2727 'endtry',
2728 ], 'E488:')
2729 CheckDefFailure([
2730 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002731 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002732 'catch# comment',
2733 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002734 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002735 CheckScriptFailure([
2736 'vim9script',
2737 'try',
2738 ' echo "yes"',
2739 'catch# comment',
2740 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002741 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002742 CheckDefFailure([
2743 'try',
2744 ' echo "yes"',
2745 'catch /pat/# comment',
2746 'endtry',
2747 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002748 CheckDefFailure([
2749 'try',
2750 'echo "yes"',
2751 'catch',
2752 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002753 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002754 CheckScriptFailure([
2755 'vim9script',
2756 'try',
2757 ' echo "yes"',
2758 'catch',
2759 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002760 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002761
2762 CheckScriptSuccess([
2763 'vim9script',
2764 'hi # comment',
2765 ])
2766 CheckScriptFailure([
2767 'vim9script',
2768 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002769 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002770 CheckScriptSuccess([
2771 'vim9script',
2772 'hi Search # comment',
2773 ])
2774 CheckScriptFailure([
2775 'vim9script',
2776 'hi Search# comment',
2777 ], 'E416:')
2778 CheckScriptSuccess([
2779 'vim9script',
2780 'hi link This Search # comment',
2781 ])
2782 CheckScriptFailure([
2783 'vim9script',
2784 'hi link This That# comment',
2785 ], 'E413:')
2786 CheckScriptSuccess([
2787 'vim9script',
2788 'hi clear This # comment',
2789 'hi clear # comment',
2790 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002791 # not tested, because it doesn't give an error but a warning:
2792 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02002793 CheckScriptFailure([
2794 'vim9script',
2795 'hi clear# comment',
2796 ], 'E416:')
2797
2798 CheckScriptSuccess([
2799 'vim9script',
2800 'hi Group term=bold',
2801 'match Group /todo/ # comment',
2802 ])
2803 CheckScriptFailure([
2804 'vim9script',
2805 'hi Group term=bold',
2806 'match Group /todo/# comment',
2807 ], 'E488:')
2808 CheckScriptSuccess([
2809 'vim9script',
2810 'match # comment',
2811 ])
2812 CheckScriptFailure([
2813 'vim9script',
2814 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002815 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002816 CheckScriptSuccess([
2817 'vim9script',
2818 'match none # comment',
2819 ])
2820 CheckScriptFailure([
2821 'vim9script',
2822 'match none# comment',
2823 ], 'E475:')
2824
2825 CheckScriptSuccess([
2826 'vim9script',
2827 'menutrans clear # comment',
2828 ])
2829 CheckScriptFailure([
2830 'vim9script',
2831 'menutrans clear# comment text',
2832 ], 'E474:')
2833
2834 CheckScriptSuccess([
2835 'vim9script',
2836 'syntax clear # comment',
2837 ])
2838 CheckScriptFailure([
2839 'vim9script',
2840 'syntax clear# comment text',
2841 ], 'E28:')
2842 CheckScriptSuccess([
2843 'vim9script',
2844 'syntax keyword Word some',
2845 'syntax clear Word # comment',
2846 ])
2847 CheckScriptFailure([
2848 'vim9script',
2849 'syntax keyword Word some',
2850 'syntax clear Word# comment text',
2851 ], 'E28:')
2852
2853 CheckScriptSuccess([
2854 'vim9script',
2855 'syntax list # comment',
2856 ])
2857 CheckScriptFailure([
2858 'vim9script',
2859 'syntax list# comment text',
2860 ], 'E28:')
2861
2862 CheckScriptSuccess([
2863 'vim9script',
2864 'syntax match Word /pat/ oneline # comment',
2865 ])
2866 CheckScriptFailure([
2867 'vim9script',
2868 'syntax match Word /pat/ oneline# comment',
2869 ], 'E475:')
2870
2871 CheckScriptSuccess([
2872 'vim9script',
2873 'syntax keyword Word word # comm[ent',
2874 ])
2875 CheckScriptFailure([
2876 'vim9script',
2877 'syntax keyword Word word# comm[ent',
2878 ], 'E789:')
2879
2880 CheckScriptSuccess([
2881 'vim9script',
2882 'syntax match Word /pat/ # comment',
2883 ])
2884 CheckScriptFailure([
2885 'vim9script',
2886 'syntax match Word /pat/# comment',
2887 ], 'E402:')
2888
2889 CheckScriptSuccess([
2890 'vim9script',
2891 'syntax match Word /pat/ contains=Something # comment',
2892 ])
2893 CheckScriptFailure([
2894 'vim9script',
2895 'syntax match Word /pat/ contains=Something# comment',
2896 ], 'E475:')
2897 CheckScriptFailure([
2898 'vim9script',
2899 'syntax match Word /pat/ contains= # comment',
2900 ], 'E406:')
2901 CheckScriptFailure([
2902 'vim9script',
2903 'syntax match Word /pat/ contains=# comment',
2904 ], 'E475:')
2905
2906 CheckScriptSuccess([
2907 'vim9script',
2908 'syntax region Word start=/pat/ end=/pat/ # comment',
2909 ])
2910 CheckScriptFailure([
2911 'vim9script',
2912 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02002913 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002914
2915 CheckScriptSuccess([
2916 'vim9script',
2917 'syntax sync # comment',
2918 ])
2919 CheckScriptFailure([
2920 'vim9script',
2921 'syntax sync# comment',
2922 ], 'E404:')
2923 CheckScriptSuccess([
2924 'vim9script',
2925 'syntax sync ccomment # comment',
2926 ])
2927 CheckScriptFailure([
2928 'vim9script',
2929 'syntax sync ccomment# comment',
2930 ], 'E404:')
2931
2932 CheckScriptSuccess([
2933 'vim9script',
2934 'syntax cluster Some contains=Word # comment',
2935 ])
2936 CheckScriptFailure([
2937 'vim9script',
2938 'syntax cluster Some contains=Word# comment',
2939 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002940
2941 CheckScriptSuccess([
2942 'vim9script',
2943 'command Echo echo # comment',
2944 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002945 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002946 ])
2947 CheckScriptFailure([
2948 'vim9script',
2949 'command Echo echo# comment',
2950 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002951 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002952 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01002953
2954 var curdir = getcwd()
2955 CheckScriptSuccess([
2956 'command Echo cd " comment',
2957 'Echo',
2958 'delcommand Echo',
2959 ])
2960 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01002961 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01002962 'command Echo cd # comment',
2963 'Echo',
2964 'delcommand Echo',
2965 ])
2966 CheckScriptFailure([
2967 'vim9script',
2968 'command Echo cd " comment',
2969 'Echo',
2970 ], 'E344:')
2971 delcommand Echo
2972 chdir(curdir)
2973
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002974 CheckScriptFailure([
2975 'vim9script',
2976 'command Echo# comment',
2977 ], 'E182:')
2978 CheckScriptFailure([
2979 'vim9script',
2980 'command Echo echo',
2981 'command Echo# comment',
2982 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002983 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002984
2985 CheckScriptSuccess([
2986 'vim9script',
2987 'function # comment',
2988 ])
2989 CheckScriptFailure([
2990 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02002991 'function " comment',
2992 ], 'E129:')
2993 CheckScriptFailure([
2994 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002995 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002996 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002997 CheckScriptSuccess([
2998 'vim9script',
2999 'function CheckScriptSuccess # comment',
3000 ])
3001 CheckScriptFailure([
3002 'vim9script',
3003 'function CheckScriptSuccess# comment',
3004 ], 'E488:')
3005
3006 CheckScriptSuccess([
3007 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003008 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003009 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003010 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003011 ])
3012 CheckScriptFailure([
3013 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003014 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003015 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003016 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003017 ], 'E488:')
3018
3019 CheckScriptSuccess([
3020 'vim9script',
3021 'call execute("ls") # comment',
3022 ])
3023 CheckScriptFailure([
3024 'vim9script',
3025 'call execute("ls")# comment',
3026 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02003027
3028 CheckScriptFailure([
3029 'def Test() " comment',
3030 'enddef',
3031 ], 'E488:')
3032 CheckScriptFailure([
3033 'vim9script',
3034 'def Test() " comment',
3035 'enddef',
3036 ], 'E488:')
3037
3038 CheckScriptSuccess([
3039 'func Test() " comment',
3040 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003041 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02003042 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02003043 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02003044 'vim9script',
3045 'func Test() " comment',
3046 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02003047 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02003048
3049 CheckScriptSuccess([
3050 'def Test() # comment',
3051 'enddef',
3052 ])
3053 CheckScriptFailure([
3054 'func Test() # comment',
3055 'endfunc',
3056 ], 'E488:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003057enddef
3058
3059def Test_vim9_comment_gui()
3060 CheckCanRunGui
3061
3062 CheckScriptFailure([
3063 'vim9script',
3064 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003065 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003066 CheckScriptFailure([
3067 'vim9script',
3068 'gui -f#comment'
3069 ], 'E499:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003070enddef
3071
Bram Moolenaara26b9702020-04-18 19:53:28 +02003072def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02003073 au TabEnter *.vim g:entered = 1
3074 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02003075
3076 edit test.vim
3077 doautocmd TabEnter #comment
3078 assert_equal(1, g:entered)
3079
3080 doautocmd TabEnter f.x
3081 assert_equal(2, g:entered)
3082
3083 g:entered = 0
3084 doautocmd TabEnter f.x #comment
3085 assert_equal(2, g:entered)
3086
3087 assert_fails('doautocmd Syntax#comment', 'E216:')
3088
3089 au! TabEnter
3090 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003091
3092 CheckScriptSuccess([
3093 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02003094 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003095 'b:var = 456',
3096 'w:var = 777',
3097 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003098 'unlet g:var w:var # something',
3099 ])
3100
3101 CheckScriptFailure([
3102 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003103 'let var = 123',
3104 ], 'E1126: Cannot use :let in Vim9 script')
3105
3106 CheckScriptFailure([
3107 'vim9script',
3108 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003109 ], 'E1016: Cannot declare a global variable:')
3110
3111 CheckScriptFailure([
3112 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003113 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003114 ], 'E1016: Cannot declare a buffer variable:')
3115
3116 CheckScriptFailure([
3117 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003118 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003119 ], 'E1016: Cannot declare a window variable:')
3120
3121 CheckScriptFailure([
3122 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003123 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003124 ], 'E1016: Cannot declare a tab variable:')
3125
3126 CheckScriptFailure([
3127 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003128 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003129 ], 'E1016: Cannot declare a v: variable:')
3130
3131 CheckScriptFailure([
3132 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003133 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003134 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02003135
3136 CheckScriptFailure([
3137 'vim9script',
3138 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003139 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003140 ], 'E108:')
3141
3142 CheckScriptFailure([
3143 'let g:var = 123',
3144 'unlet g:var # something',
3145 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003146
3147 CheckScriptSuccess([
3148 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003149 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003150 ' echo "yes"',
3151 'elseif 2 #comment',
3152 ' echo "no"',
3153 'endif',
3154 ])
3155
3156 CheckScriptFailure([
3157 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003158 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003159 ' echo "yes"',
3160 'endif',
3161 ], 'E15:')
3162
3163 CheckScriptFailure([
3164 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003165 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003166 ' echo "yes"',
3167 'elseif 2#comment',
3168 ' echo "no"',
3169 'endif',
3170 ], 'E15:')
3171
3172 CheckScriptSuccess([
3173 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003174 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003175 ])
3176
3177 CheckScriptFailure([
3178 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003179 'var v = 1# comment6',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003180 ], 'E15:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003181
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003182 CheckScriptSuccess([
3183 'vim9script',
3184 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003185 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003186 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003187 'dsearch /pat/ #comment',
3188 'bwipe!',
3189 ])
3190
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003191 CheckScriptFailure([
3192 'vim9script',
3193 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003194 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003195 ':$',
3196 'dsearch /pat/#comment',
3197 'bwipe!',
3198 ], 'E488:')
3199
3200 CheckScriptFailure([
3201 'vim9script',
3202 'func! SomeFunc()',
3203 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02003204enddef
3205
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003206def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003207 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003208 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02003209 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003210 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02003211 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003212 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02003213 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003214 END
3215 writefile(lines, 'Xfinished')
3216 source Xfinished
3217 assert_equal('two', g:res)
3218
3219 unlet g:res
3220 delete('Xfinished')
3221enddef
3222
Bram Moolenaara5d00772020-05-14 23:20:55 +02003223def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003224 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02003225 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02003226 def GetValue(): string
3227 return theVal
3228 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003229 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02003230 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02003231 theVal = 'else'
3232 g:laterVal = GetValue()
3233 END
3234 writefile(lines, 'Xforward')
3235 source Xforward
3236 assert_equal('something', g:initVal)
3237 assert_equal('else', g:laterVal)
3238
3239 unlet g:initVal
3240 unlet g:laterVal
3241 delete('Xforward')
3242enddef
3243
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003244def Test_source_vim9_from_legacy()
Bram Moolenaara6294952020-12-27 13:39:50 +01003245 var vim9_lines =<< trim END
3246 vim9script
3247 var local = 'local'
3248 g:global = 'global'
3249 export var exported = 'exported'
3250 export def GetText(): string
3251 return 'text'
3252 enddef
3253 END
3254 writefile(vim9_lines, 'Xvim9_script.vim')
3255
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003256 var legacy_lines =<< trim END
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003257 source Xvim9_script.vim
3258
3259 call assert_false(exists('local'))
3260 call assert_false(exists('exported'))
3261 call assert_false(exists('s:exported'))
3262 call assert_equal('global', global)
3263 call assert_equal('global', g:global)
3264
3265 " imported variable becomes script-local
3266 import exported from './Xvim9_script.vim'
3267 call assert_equal('exported', s:exported)
3268 call assert_false(exists('exported'))
3269
3270 " imported function becomes script-local
3271 import GetText from './Xvim9_script.vim'
3272 call assert_equal('text', s:GetText())
3273 call assert_false(exists('*GetText'))
3274 END
3275 writefile(legacy_lines, 'Xlegacy_script.vim')
3276
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003277 source Xlegacy_script.vim
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003278 assert_equal('global', g:global)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003279 unlet g:global
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003280
3281 delete('Xlegacy_script.vim')
3282 delete('Xvim9_script.vim')
3283enddef
Bram Moolenaara5d00772020-05-14 23:20:55 +02003284
Bram Moolenaare535db82021-03-31 21:07:24 +02003285def Test_declare_script_in_func()
3286 var lines =<< trim END
3287 vim9script
3288 func Declare()
3289 let s:local = 123
3290 endfunc
3291 Declare()
3292 assert_equal(123, local)
3293
3294 var error: string
3295 try
3296 local = 'asdf'
3297 catch
3298 error = v:exception
3299 endtry
3300 assert_match('E1012: Type mismatch; expected number but got string', error)
3301
3302 lockvar local
3303 try
3304 local = 999
3305 catch
3306 error = v:exception
3307 endtry
3308 assert_match('E741: Value is locked: local', error)
3309 END
3310 CheckScriptSuccess(lines)
3311enddef
3312
3313
Bram Moolenaar7d699702020-08-14 20:52:28 +02003314func Test_vim9script_not_global()
3315 " check that items defined in Vim9 script are script-local, not global
3316 let vim9lines =<< trim END
3317 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003318 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02003319 func TheFunc()
3320 echo 'local'
3321 endfunc
3322 def DefFunc()
3323 echo 'local'
3324 enddef
3325 END
3326 call writefile(vim9lines, 'Xvim9script.vim')
3327 source Xvim9script.vim
3328 try
3329 echo g:var
3330 assert_report('did not fail')
3331 catch /E121:/
3332 " caught
3333 endtry
3334 try
3335 call TheFunc()
3336 assert_report('did not fail')
3337 catch /E117:/
3338 " caught
3339 endtry
3340 try
3341 call DefFunc()
3342 assert_report('did not fail')
3343 catch /E117:/
3344 " caught
3345 endtry
3346
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003347 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02003348endfunc
3349
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02003350def Test_vim9_copen()
3351 # this was giving an error for setting w:quickfix_title
3352 copen
3353 quit
3354enddef
3355
Bram Moolenaar03290b82020-12-19 16:30:44 +01003356" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003357def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003358 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003359 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01003360 def some#gettest(): string
3361 return 'test'
3362 enddef
3363 g:some#name = 'name'
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003364
3365 def some#varargs(a1: string, ...l: list<string>): string
3366 return a1 .. l[0] .. l[1]
3367 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01003368 END
3369
3370 mkdir('Xdir/autoload', 'p')
3371 writefile(lines, 'Xdir/autoload/some.vim')
3372 var save_rtp = &rtp
3373 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3374
3375 assert_equal('test', g:some#gettest())
3376 assert_equal('name', g:some#name)
3377 g:some#other = 'other'
3378 assert_equal('other', g:some#other)
3379
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003380 assert_equal('abc', some#varargs('a', 'b', 'c'))
3381
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003382 # upper case script name works
3383 lines =<< trim END
3384 vim9script
3385 def Other#getOther(): string
3386 return 'other'
3387 enddef
3388 END
3389 writefile(lines, 'Xdir/autoload/Other.vim')
3390 assert_equal('other', g:Other#getOther())
3391
Bram Moolenaar03290b82020-12-19 16:30:44 +01003392 delete('Xdir', 'rf')
3393 &rtp = save_rtp
3394enddef
3395
3396" test using a vim9script that is auto-loaded from an autocmd
3397def Test_vim9_aucmd_autoload()
3398 var lines =<< trim END
3399 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003400 def foo#test()
3401 echomsg getreg('"')
3402 enddef
3403 END
3404
3405 mkdir('Xdir/autoload', 'p')
3406 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003407 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003408 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3409 augroup test
3410 autocmd TextYankPost * call foo#test()
3411 augroup END
3412
3413 normal Y
3414
3415 augroup test
3416 autocmd!
3417 augroup END
3418 delete('Xdir', 'rf')
3419 &rtp = save_rtp
3420enddef
3421
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003422" This was causing a crash because suppress_errthrow wasn't reset.
3423def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003424 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003425 vim9script
3426 def crash#func()
3427 try
3428 for x in List()
3429 endfor
3430 catch
3431 endtry
3432 g:ok = true
3433 enddef
3434 fu List()
3435 invalid
3436 endfu
3437 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01003438 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003439 catch /wontmatch/
3440 endtry
3441 END
3442 call mkdir('Xruntime/autoload', 'p')
3443 call writefile(lines, 'Xruntime/autoload/crash.vim')
3444
3445 # run in a separate Vim to avoid the side effects of assert_fails()
3446 lines =<< trim END
3447 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
3448 call crash#func()
3449 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003450 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003451 END
3452 writefile(lines, 'Xscript')
3453 RunVim([], [], '-S Xscript')
3454 assert_equal(['ok'], readfile('Xdidit'))
3455
3456 delete('Xdidit')
3457 delete('Xscript')
3458 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01003459
3460 lines =<< trim END
3461 vim9script
3462 var foo#bar = 'asdf'
3463 END
3464 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003465enddef
3466
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003467def Test_script_var_in_autocmd()
3468 # using a script variable from an autocommand, defined in a :def function in a
3469 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003470 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003471 let s:counter = 1
3472 def s:Func()
3473 au! CursorHold
3474 au CursorHold * s:counter += 1
3475 enddef
3476 call s:Func()
3477 doau CursorHold
3478 call assert_equal(2, s:counter)
3479 au! CursorHold
3480 END
3481 CheckScriptSuccess(lines)
3482enddef
3483
Bram Moolenaar3896a102020-08-09 14:33:55 +02003484def Test_cmdline_win()
3485 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
3486 # the command line window.
3487 mkdir('rtp/syntax', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003488 var export_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02003489 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003490 export var That = 'yes'
Bram Moolenaar3896a102020-08-09 14:33:55 +02003491 END
3492 writefile(export_lines, 'rtp/syntax/Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003493 var import_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02003494 vim9script
3495 import That from './Xexport.vim'
3496 END
3497 writefile(import_lines, 'rtp/syntax/vim.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003498 var save_rtp = &rtp
Bram Moolenaar3896a102020-08-09 14:33:55 +02003499 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
3500 syntax on
3501 augroup CmdWin
3502 autocmd CmdwinEnter * g:got_there = 'yes'
3503 augroup END
3504 # this will open and also close the cmdline window
3505 feedkeys('q:', 'xt')
3506 assert_equal('yes', g:got_there)
3507
3508 augroup CmdWin
3509 au!
3510 augroup END
3511 &rtp = save_rtp
3512 delete('rtp', 'rf')
3513enddef
3514
Bram Moolenaare3d46852020-08-29 13:39:17 +02003515def Test_invalid_sid()
3516 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003517
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003518 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02003519 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02003520 endif
3521 delete('Xdidit')
3522enddef
3523
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003524def Test_restoring_cpo()
3525 writefile(['vim9script', 'set nocp'], 'Xsourced')
3526 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
3527 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
3528 assert_equal(['done'], readfile('Xdone'))
3529 endif
3530 delete('Xsourced')
3531 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01003532 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01003533
3534 writefile(['vim9script'], 'XanotherScript')
3535 set cpo=aABceFsMny>
3536 edit XanotherScript
3537 so %
3538 assert_equal('aABceFsMny>', &cpo)
3539 :1del
3540 w
3541 so %
3542 assert_equal('aABceFsMny>', &cpo)
3543
3544 delete('XanotherScript')
3545 set cpo&vim
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003546enddef
3547
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003548" Use :function so we can use Check commands
3549func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003550 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003551 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003552
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003553 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003554 vim9script
3555 def script#func()
3556 enddef
3557 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003558 call mkdir('Xdir/autoload', 'p')
3559 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003560
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003561 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003562 vim9script
3563 set cpo+=M
3564 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02003565 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003566 setline(1, 'some text')
3567 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003568 call writefile(lines, 'XTest_redraw_cpo')
3569 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
3570 call term_sendkeys(buf, "V:")
3571 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003572
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003573 " clean up
3574 call term_sendkeys(buf, "\<Esc>u")
3575 call StopVimInTerminal(buf)
3576 call delete('XTest_redraw_cpo')
3577 call delete('Xdir', 'rf')
3578endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003579
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003580
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003581def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003582 var lines =<< trim END
3583 var name: any
3584 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003585 END
3586 CheckDefAndScriptSuccess(lines)
3587enddef
3588
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003589func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02003590 CheckRunVimInTerminal
3591
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003592 " call indirectly to avoid compilation error for missing functions
3593 call Run_Test_define_func_at_command_line()
3594endfunc
3595
3596def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003597 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003598 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003599 func CheckAndQuit()
3600 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
3601 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
3602 endfunc
3603 END
3604 writefile([''], 'Xdidcmd')
3605 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01003606 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003607 # define Afunc() on the command line
3608 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
3609 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003610 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003611
3612 call StopVimInTerminal(buf)
3613 delete('XcallFunc')
3614 delete('Xdidcmd')
3615enddef
3616
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003617def Test_script_var_scope()
3618 var lines =<< trim END
3619 vim9script
3620 if true
3621 if true
3622 var one = 'one'
3623 echo one
3624 endif
3625 echo one
3626 endif
3627 END
3628 CheckScriptFailure(lines, 'E121:', 7)
3629
3630 lines =<< trim END
3631 vim9script
3632 if true
3633 if false
3634 var one = 'one'
3635 echo one
3636 else
3637 var one = 'one'
3638 echo one
3639 endif
3640 echo one
3641 endif
3642 END
3643 CheckScriptFailure(lines, 'E121:', 10)
3644
3645 lines =<< trim END
3646 vim9script
3647 while true
3648 var one = 'one'
3649 echo one
3650 break
3651 endwhile
3652 echo one
3653 END
3654 CheckScriptFailure(lines, 'E121:', 7)
3655
3656 lines =<< trim END
3657 vim9script
3658 for i in range(1)
3659 var one = 'one'
3660 echo one
3661 endfor
3662 echo one
3663 END
3664 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003665
3666 lines =<< trim END
3667 vim9script
3668 {
3669 var one = 'one'
3670 assert_equal('one', one)
3671 }
3672 assert_false(exists('one'))
3673 assert_false(exists('s:one'))
3674 END
3675 CheckScriptSuccess(lines)
3676
3677 lines =<< trim END
3678 vim9script
3679 {
3680 var one = 'one'
3681 echo one
3682 }
3683 echo one
3684 END
3685 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003686enddef
3687
Bram Moolenaar352134b2020-10-17 22:04:08 +02003688def Test_catch_exception_in_callback()
3689 var lines =<< trim END
3690 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02003691 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02003692 try
3693 var x: string
3694 var y: string
3695 # this error should be caught with CHECKLEN
3696 [x, y] = ['']
3697 catch
3698 g:caught = 'yes'
3699 endtry
3700 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01003701 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02003702 feedkeys("\r", 'xt')
3703 END
3704 CheckScriptSuccess(lines)
3705
3706 unlet g:caught
3707enddef
3708
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003709def Test_no_unknown_error_after_error()
3710 if !has('unix') || !has('job')
3711 throw 'Skipped: not unix of missing +job feature'
3712 endif
3713 var lines =<< trim END
3714 vim9script
3715 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02003716 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003717 eval [][0]
3718 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02003719 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003720 sleep 1m
3721 source += l
3722 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01003723 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01003724 while job_status(myjob) == 'run'
3725 sleep 10m
3726 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01003727 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02003728 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003729 END
3730 writefile(lines, 'Xdef')
3731 assert_fails('so Xdef', ['E684:', 'E1012:'])
3732 delete('Xdef')
3733enddef
3734
Bram Moolenaar4324d872020-12-01 20:12:24 +01003735def InvokeNormal()
3736 exe "norm! :m+1\r"
3737enddef
3738
3739def Test_invoke_normal_in_visual_mode()
3740 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
3741 new
3742 setline(1, ['aaa', 'bbb'])
3743 feedkeys("V\<F3>", 'xt')
3744 assert_equal(['bbb', 'aaa'], getline(1, 2))
3745 xunmap <F3>
3746enddef
3747
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003748def Test_white_space_after_command()
3749 var lines =<< trim END
3750 exit_cb: Func})
3751 END
3752 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003753
3754 lines =<< trim END
3755 e#
3756 END
3757 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003758enddef
3759
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003760def Test_script_var_gone_when_sourced_twice()
3761 var lines =<< trim END
3762 vim9script
3763 if exists('g:guard')
3764 finish
3765 endif
3766 g:guard = 1
3767 var name = 'thename'
3768 def g:GetName(): string
3769 return name
3770 enddef
3771 def g:SetName(arg: string)
3772 name = arg
3773 enddef
3774 END
3775 writefile(lines, 'XscriptTwice.vim')
3776 so XscriptTwice.vim
3777 assert_equal('thename', g:GetName())
3778 g:SetName('newname')
3779 assert_equal('newname', g:GetName())
3780 so XscriptTwice.vim
3781 assert_fails('call g:GetName()', 'E1149:')
3782 assert_fails('call g:SetName("x")', 'E1149:')
3783
3784 delfunc g:GetName
3785 delfunc g:SetName
3786 delete('XscriptTwice.vim')
3787 unlet g:guard
3788enddef
3789
3790def Test_import_gone_when_sourced_twice()
3791 var exportlines =<< trim END
3792 vim9script
3793 if exists('g:guard')
3794 finish
3795 endif
3796 g:guard = 1
3797 export var name = 'someName'
3798 END
3799 writefile(exportlines, 'XexportScript.vim')
3800
3801 var lines =<< trim END
3802 vim9script
3803 import name from './XexportScript.vim'
3804 def g:GetName(): string
3805 return name
3806 enddef
3807 END
3808 writefile(lines, 'XscriptImport.vim')
3809 so XscriptImport.vim
3810 assert_equal('someName', g:GetName())
3811
3812 so XexportScript.vim
3813 assert_fails('call g:GetName()', 'E1149:')
3814
3815 delfunc g:GetName
3816 delete('XexportScript.vim')
3817 delete('XscriptImport.vim')
3818 unlet g:guard
3819enddef
3820
Bram Moolenaar10b94212021-02-19 21:42:57 +01003821def Test_unsupported_commands()
3822 var lines =<< trim END
3823 ka
3824 END
3825 CheckDefAndScriptFailure(lines, 'E1100:')
3826
3827 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01003828 :1ka
3829 END
3830 CheckDefAndScriptFailure(lines, 'E481:')
3831
3832 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01003833 t
3834 END
3835 CheckDefFailure(lines, 'E1100:')
3836 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
3837
3838 lines =<< trim END
3839 x
3840 END
3841 CheckDefFailure(lines, 'E1100:')
3842 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
3843
3844 lines =<< trim END
3845 xit
3846 END
3847 CheckDefFailure(lines, 'E1100:')
3848 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
3849enddef
3850
Bram Moolenaarc70fe462021-04-17 17:59:19 +02003851def Test_mapping_line_number()
3852 var lines =<< trim END
3853 vim9script
3854 def g:FuncA()
3855 # Some comment
3856 FuncB(0)
3857 enddef
3858 # Some comment
3859 def FuncB(
3860 # Some comment
3861 n: number
3862 )
3863 exe 'nno '
3864 # Some comment
3865 .. '<F3> a'
3866 .. 'b'
3867 .. 'c'
3868 enddef
3869 END
3870 CheckScriptSuccess(lines)
3871 var res = execute('verbose nmap <F3>')
3872 assert_match('No mapping found', res)
3873
3874 g:FuncA()
3875 res = execute('verbose nmap <F3>')
3876 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
3877
3878 nunmap <F3>
3879 delfunc g:FuncA
3880enddef
3881
Bram Moolenaar585fea72020-04-02 22:33:21 +02003882" Keep this last, it messes up highlighting.
3883def Test_substitute_cmd()
3884 new
3885 setline(1, 'something')
3886 :substitute(some(other(
3887 assert_equal('otherthing', getline(1))
3888 bwipe!
3889
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003890 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003891 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02003892 vim9script
3893 new
3894 setline(1, 'something')
3895 :substitute(some(other(
3896 assert_equal('otherthing', getline(1))
3897 bwipe!
3898 END
3899 writefile(lines, 'Xvim9lines')
3900 source Xvim9lines
3901
3902 delete('Xvim9lines')
3903enddef
3904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker