blob: 4b0ab2bad71cbc890df90d521d8c32ab67c98b98 [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 Moolenaar8ac681a2021-06-15 20:06:34 +0200593
594 var lines =<< trim END
595 vim9script
596 try
597 acos('0.5')
598 ->setline(1)
599 catch
600 g:caught = v:exception
601 endtry
602 END
603 CheckScriptSuccess(lines)
604 assert_match('E808: Number or Float required', g:caught)
605 unlet g:caught
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606enddef
607
Bram Moolenaar2e34c342021-03-14 12:13:33 +0100608" :while at the very start of a function that :continue jumps to
609def TryContinueFunc()
610 while g:Count < 2
611 g:sequence ..= 't'
612 try
613 echoerr 'Test'
614 catch
615 g:Count += 1
616 g:sequence ..= 'c'
617 continue
618 endtry
619 g:sequence ..= 'e'
620 g:Count += 1
621 endwhile
622enddef
623
624def Test_continue_in_try_in_while()
625 g:Count = 0
626 g:sequence = ''
627 TryContinueFunc()
628 assert_equal('tctc', g:sequence)
629 unlet g:Count
630 unlet g:sequence
631enddef
632
Bram Moolenaar9cb577a2021-02-22 22:45:10 +0100633def Test_nocatch_return_in_try()
634 # return in try block returns normally
635 def ReturnInTry(): string
636 try
637 return '"some message"'
638 catch
639 endtry
640 return 'not reached'
641 enddef
642 exe 'echoerr ' .. ReturnInTry()
643enddef
644
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100645def Test_cnext_works_in_catch()
646 var lines =<< trim END
647 vim9script
648 au BufEnter * eval 0
649 writefile(['text'], 'Xfile1')
650 writefile(['text'], 'Xfile2')
651 var items = [
652 {lnum: 1, filename: 'Xfile1', valid: true},
653 {lnum: 1, filename: 'Xfile2', valid: true}
654 ]
655 setqflist([], ' ', {items: items})
656 cwindow
657
658 def CnextOrCfirst()
659 # if cnext fails, cfirst is used
660 try
661 cnext
662 catch
663 cfirst
664 endtry
665 enddef
666
667 CnextOrCfirst()
668 CnextOrCfirst()
669 writefile([getqflist({idx: 0}).idx], 'Xresult')
670 qall
671 END
672 writefile(lines, 'XCatchCnext')
673 RunVim([], [], '--clean -S XCatchCnext')
674 assert_equal(['1'], readfile('Xresult'))
675
676 delete('Xfile1')
677 delete('Xfile2')
678 delete('XCatchCnext')
679 delete('Xresult')
680enddef
681
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100682def Test_throw_skipped()
683 if 0
684 throw dontgethere
685 endif
686enddef
687
Bram Moolenaar8f81b222021-01-14 21:47:06 +0100688def Test_nocatch_throw_silenced()
689 var lines =<< trim END
690 vim9script
691 def Func()
692 throw 'error'
693 enddef
694 silent! Func()
695 END
696 writefile(lines, 'XthrowSilenced')
697 source XthrowSilenced
698 delete('XthrowSilenced')
699enddef
700
Bram Moolenaare8593122020-07-18 15:17:02 +0200701def DeletedFunc(): list<any>
702 return ['delete me']
703enddef
704defcompile
705delfunc DeletedFunc
706
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100707def ThrowFromDef()
Bram Moolenaara72cfb82020-04-23 17:07:30 +0200708 throw "getout" # comment
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100709enddef
710
711func CatchInFunc()
712 try
713 call ThrowFromDef()
714 catch
715 let g:thrown_func = v:exception
716 endtry
717endfunc
718
719def CatchInDef()
720 try
721 ThrowFromDef()
722 catch
723 g:thrown_def = v:exception
724 endtry
725enddef
726
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100727def ReturnFinally(): string
728 try
729 return 'intry'
Bram Moolenaar373863e2020-09-26 17:20:53 +0200730 finall
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100731 g:in_finally = 'finally'
732 endtry
733 return 'end'
734enddef
735
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100736def Test_try_catch_nested()
737 CatchInFunc()
738 assert_equal('getout', g:thrown_func)
739
740 CatchInDef()
741 assert_equal('getout', g:thrown_def)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100742
743 assert_equal('intry', ReturnFinally())
744 assert_equal('finally', g:in_finally)
745enddef
746
Bram Moolenaar9939f572020-09-16 22:29:52 +0200747def TryOne(): number
748 try
749 return 0
750 catch
751 endtry
752 return 0
753enddef
754
755def TryTwo(n: number): string
756 try
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200757 var x = {}
Bram Moolenaar9939f572020-09-16 22:29:52 +0200758 catch
759 endtry
760 return 'text'
761enddef
762
763def Test_try_catch_twice()
764 assert_equal('text', TryOne()->TryTwo())
765enddef
766
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100767def Test_try_catch_match()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200768 var seq = 'a'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100769 try
770 throw 'something'
771 catch /nothing/
772 seq ..= 'x'
773 catch /some/
774 seq ..= 'b'
775 catch /asdf/
776 seq ..= 'x'
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200777 catch ?a\?sdf?
778 seq ..= 'y'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100779 finally
780 seq ..= 'c'
781 endtry
782 assert_equal('abc', seq)
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100783enddef
784
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200785def Test_try_catch_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200786 CheckDefFailure(['catch'], 'E603:')
787 CheckDefFailure(['try', 'echo 0', 'catch', 'catch'], 'E1033:')
788 CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
789 CheckDefFailure(['finally'], 'E606:')
790 CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:')
791 CheckDefFailure(['endtry'], 'E602:')
792 CheckDefFailure(['while 1', 'endtry'], 'E170:')
793 CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:')
Bram Moolenaar13106602020-10-04 16:06:05 +0200794 CheckDefFailure(['if 1', 'endtry'], 'E171:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200795 CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:')
Bram Moolenaar585fea72020-04-02 22:33:21 +0200796
Bram Moolenaare4984292020-12-13 14:19:25 +0100797 CheckDefFailure(['throw'], 'E1143:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200798 CheckDefFailure(['throw xxx'], 'E1001:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200799enddef
800
Bram Moolenaar7c5b3c0362021-02-14 22:40:57 +0100801def Try_catch_skipped()
802 var l = []
803 try
804 finally
805 endtry
806
807 if 1
808 else
809 try
810 endtry
811 endif
812enddef
813
814" The skipped try/endtry was updating the wrong instruction.
815def Test_try_catch_skipped()
816 var instr = execute('disassemble Try_catch_skipped')
817 assert_match("NEWLIST size 0\n", instr)
818enddef
819
820
821
Bram Moolenaar006ad482020-06-30 20:55:15 +0200822def Test_throw_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200823 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200824 var lines =<< trim END
Bram Moolenaar006ad482020-06-30 20:55:15 +0200825 vim9script
826 try
827 throw 'one'
828 .. 'two'
829 catch
830 assert_equal('onetwo', v:exception)
831 endtry
832 END
833 CheckScriptSuccess(lines)
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200834
835 lines =<< trim END
836 vim9script
Bram Moolenaar352134b2020-10-17 22:04:08 +0200837 @r = ''
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200838 def Func()
839 throw @r
840 enddef
841 var result = ''
842 try
843 Func()
844 catch /E1129:/
845 result = 'caught'
846 endtry
847 assert_equal('caught', result)
848 END
849 CheckScriptSuccess(lines)
Bram Moolenaar006ad482020-06-30 20:55:15 +0200850enddef
851
Bram Moolenaared677f52020-08-12 16:38:10 +0200852def Test_error_in_nested_function()
Bram Moolenaar03dfde22021-02-14 13:17:22 +0100853 # an error in a nested :function aborts executing in the calling :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200854 var lines =<< trim END
Bram Moolenaared677f52020-08-12 16:38:10 +0200855 vim9script
856 def Func()
857 Error()
858 g:test_var = 1
859 enddef
860 func Error() abort
861 eval [][0]
862 endfunc
863 Func()
864 END
865 g:test_var = 0
866 CheckScriptFailure(lines, 'E684:')
867 assert_equal(0, g:test_var)
868enddef
869
Bram Moolenaar227c58a2021-04-28 20:40:44 +0200870def Test_abort_after_error()
871 var lines =<< trim END
872 vim9script
873 while true
874 echo notfound
875 endwhile
876 g:gotthere = true
877 END
878 g:gotthere = false
879 CheckScriptFailure(lines, 'E121:')
880 assert_false(g:gotthere)
881 unlet g:gotthere
882enddef
883
Bram Moolenaar37c83712020-06-30 21:18:36 +0200884def Test_cexpr_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200885 # only checks line continuation
Bram Moolenaar37c83712020-06-30 21:18:36 +0200886 set errorformat=File\ %f\ line\ %l
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200887 var lines =<< trim END
Bram Moolenaar37c83712020-06-30 21:18:36 +0200888 vim9script
889 cexpr 'File'
890 .. ' someFile' ..
891 ' line 19'
892 assert_equal(19, getqflist()[0].lnum)
893 END
894 CheckScriptSuccess(lines)
895 set errorformat&
896enddef
897
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200898def Test_statusline_syntax()
899 # legacy syntax is used for 'statusline'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200900 var lines =<< trim END
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200901 vim9script
902 func g:Status()
903 return '%{"x" is# "x"}'
904 endfunc
905 set laststatus=2 statusline=%!Status()
906 redrawstatus
907 set laststatus statusline=
908 END
909 CheckScriptSuccess(lines)
910enddef
911
Bram Moolenaarb2097502020-07-19 17:17:02 +0200912def Test_list_vimscript()
913 # checks line continuation and comments
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200914 var lines =<< trim END
Bram Moolenaarb2097502020-07-19 17:17:02 +0200915 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200916 var mylist = [
Bram Moolenaarb2097502020-07-19 17:17:02 +0200917 'one',
918 # comment
919 'two', # empty line follows
920
921 'three',
922 ]
923 assert_equal(['one', 'two', 'three'], mylist)
924 END
925 CheckScriptSuccess(lines)
Bram Moolenaar66250c92020-08-20 15:02:42 +0200926
927 # check all lines from heredoc are kept
928 lines =<< trim END
929 # comment 1
930 two
931 # comment 3
932
933 five
934 # comment 6
935 END
936 assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines)
Bram Moolenaar4bce26b2021-01-22 22:06:56 +0100937
938 lines =<< trim END
939 [{
940 a: 0}]->string()->assert_equal("[{'a': 0}]")
941 END
942 CheckDefAndScriptSuccess(lines)
Bram Moolenaarb2097502020-07-19 17:17:02 +0200943enddef
944
Bram Moolenaar2a1381c2020-05-05 23:32:58 +0200945if has('channel')
946 let someJob = test_null_job()
Bram Moolenaar40ee4662020-05-05 22:08:26 +0200947
Bram Moolenaar2a1381c2020-05-05 23:32:58 +0200948 def FuncWithError()
949 echomsg g:someJob
950 enddef
Bram Moolenaar40ee4662020-05-05 22:08:26 +0200951
Bram Moolenaar2a1381c2020-05-05 23:32:58 +0200952 func Test_convert_emsg_to_exception()
953 try
954 call FuncWithError()
955 catch
956 call assert_match('Vim:E908:', v:exception)
957 endtry
958 endfunc
959endif
Bram Moolenaar40ee4662020-05-05 22:08:26 +0200960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100961let s:export_script_lines =<< trim END
962 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200963 var name: string = 'bob'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964 def Concat(arg: string): string
965 return name .. arg
966 enddef
Bram Moolenaar227a69d2020-05-15 18:17:28 +0200967 g:result = Concat('bie')
968 g:localname = name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969
970 export const CONST = 1234
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200971 export var exported = 9876
972 export var exp_name = 'John'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973 export def Exported(): string
974 return 'Exported'
975 enddef
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +0100976 export final theList = [1]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977END
978
Bram Moolenaarb3ca9822020-08-09 14:43:58 +0200979def Undo_export_script_lines()
980 unlet g:result
981 unlet g:localname
982enddef
983
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100984def Test_vim9_import_export()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200985 var import_script_lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 vim9script
987 import {exported, Exported} from './Xexport.vim'
988 g:imported = exported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +0100989 exported += 3
990 g:imported_added = exported
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991 g:imported_func = Exported()
Bram Moolenaar6e587dc2020-02-06 13:15:52 +0100992
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200993 def GetExported(): string
Bram Moolenaare0de1712020-12-02 17:36:54 +0100994 var local_dict = {ref: Exported}
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200995 return local_dict.ref()
996 enddef
997 g:funcref_result = GetExported()
998
Bram Moolenaar6e587dc2020-02-06 13:15:52 +0100999 import {exp_name} from './Xexport.vim'
1000 g:imported_name = exp_name
1001 exp_name ..= ' Doe'
1002 g:imported_name_appended = exp_name
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001003 g:imported_later = exported
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001004
1005 import theList from './Xexport.vim'
1006 theList->add(2)
1007 assert_equal([1, 2], theList)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008 END
1009
1010 writefile(import_script_lines, 'Ximport.vim')
1011 writefile(s:export_script_lines, 'Xexport.vim')
1012
1013 source Ximport.vim
1014
1015 assert_equal('bobbie', g:result)
1016 assert_equal('bob', g:localname)
1017 assert_equal(9876, g:imported)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001018 assert_equal(9879, g:imported_added)
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001019 assert_equal(9879, g:imported_later)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 assert_equal('Exported', g:imported_func)
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001021 assert_equal('Exported', g:funcref_result)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001022 assert_equal('John', g:imported_name)
1023 assert_equal('John Doe', g:imported_name_appended)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 assert_false(exists('g:name'))
1025
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001026 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001027 unlet g:imported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001028 unlet g:imported_added
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001029 unlet g:imported_later
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 unlet g:imported_func
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001031 unlet g:imported_name g:imported_name_appended
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 delete('Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001033
Bram Moolenaar1c991142020-07-04 13:15:31 +02001034 # similar, with line breaks
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001035 var import_line_break_script_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001036 vim9script
1037 import {
1038 exported,
1039 Exported,
1040 }
1041 from
1042 './Xexport.vim'
1043 g:imported = exported
1044 exported += 5
1045 g:imported_added = exported
1046 g:imported_func = Exported()
1047 END
1048 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
1049 source Ximport_lbr.vim
1050
1051 assert_equal(9876, g:imported)
1052 assert_equal(9881, g:imported_added)
1053 assert_equal('Exported', g:imported_func)
1054
1055 # exported script not sourced again
1056 assert_false(exists('g:result'))
1057 unlet g:imported
1058 unlet g:imported_added
1059 unlet g:imported_func
1060 delete('Ximport_lbr.vim')
1061
1062 # import inside :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001063 var import_in_def_lines =<< trim END
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001064 vim9script
1065 def ImportInDef()
1066 import exported from './Xexport.vim'
1067 g:imported = exported
1068 exported += 7
1069 g:imported_added = exported
1070 enddef
1071 ImportInDef()
1072 END
1073 writefile(import_in_def_lines, 'Ximport2.vim')
1074 source Ximport2.vim
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001075 # TODO: this should be 9879
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001076 assert_equal(9876, g:imported)
1077 assert_equal(9883, g:imported_added)
1078 unlet g:imported
1079 unlet g:imported_added
1080 delete('Ximport2.vim')
1081
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001082 var import_star_as_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001083 vim9script
1084 import * as Export from './Xexport.vim'
1085 def UseExport()
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001086 g:imported_def = Export.exported
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001087 enddef
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001088 g:imported_script = Export.exported
1089 assert_equal(1, exists('Export.exported'))
1090 assert_equal(0, exists('Export.notexported'))
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001091 UseExport()
1092 END
1093 writefile(import_star_as_lines, 'Ximport.vim')
1094 source Ximport.vim
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001095 assert_equal(9883, g:imported_def)
1096 assert_equal(9883, g:imported_script)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001097
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001098 var import_star_as_lines_no_dot =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001099 vim9script
1100 import * as Export from './Xexport.vim'
1101 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001102 var dummy = 1
1103 var imported = Export + dummy
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001104 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001105 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001106 END
1107 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001108 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001109
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001110 var import_star_as_lines_dot_space =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001111 vim9script
1112 import * as Export from './Xexport.vim'
1113 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001114 var imported = Export . exported
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001115 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001116 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001117 END
1118 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001119 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001120
Bram Moolenaara6294952020-12-27 13:39:50 +01001121 var import_star_as_duplicated =<< trim END
1122 vim9script
1123 import * as Export from './Xexport.vim'
1124 var some = 'other'
1125 import * as Export from './Xexport.vim'
1126 defcompile
1127 END
1128 writefile(import_star_as_duplicated, 'Ximport.vim')
1129 assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim')
1130
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001131 var import_star_as_lines_script_no_dot =<< trim END
1132 vim9script
1133 import * as Export from './Xexport.vim'
1134 g:imported_script = Export exported
1135 END
1136 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
1137 assert_fails('source Ximport.vim', 'E1029:')
1138
1139 var import_star_as_lines_script_space_after_dot =<< trim END
1140 vim9script
1141 import * as Export from './Xexport.vim'
1142 g:imported_script = Export. exported
1143 END
1144 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
1145 assert_fails('source Ximport.vim', 'E1074:')
1146
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001147 var import_star_as_lines_missing_name =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001148 vim9script
1149 import * as Export from './Xexport.vim'
1150 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001151 var imported = Export.
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001152 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001153 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001154 END
1155 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001156 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001157
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001158 var import_star_as_lbr_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001159 vim9script
1160 import *
1161 as Export
1162 from
1163 './Xexport.vim'
1164 def UseExport()
1165 g:imported = Export.exported
1166 enddef
1167 UseExport()
1168 END
1169 writefile(import_star_as_lbr_lines, 'Ximport.vim')
1170 source Ximport.vim
1171 assert_equal(9883, g:imported)
1172
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001173 var import_star_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001174 vim9script
1175 import * from './Xexport.vim'
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001176 END
1177 writefile(import_star_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001178 assert_fails('source Ximport.vim', 'E1045:', '', 2, 'Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001179
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001180 # try to import something that exists but is not exported
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001181 var import_not_exported_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001182 vim9script
1183 import name from './Xexport.vim'
1184 END
1185 writefile(import_not_exported_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001186 assert_fails('source Ximport.vim', 'E1049:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001187
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001188 # try to import something that is already defined
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001189 var import_already_defined =<< trim END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001190 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001191 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001192 import exported from './Xexport.vim'
1193 END
1194 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001195 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001196
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001197 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001198 import_already_defined =<< trim END
1199 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001200 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001201 import * as exported from './Xexport.vim'
1202 END
1203 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001204 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001205
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001206 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001207 import_already_defined =<< trim END
1208 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001209 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001210 import {exported} from './Xexport.vim'
1211 END
1212 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001213 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001214
Bram Moolenaar918a4242020-12-06 14:37:08 +01001215 # try changing an imported const
1216 var import_assign_to_const =<< trim END
1217 vim9script
1218 import CONST from './Xexport.vim'
1219 def Assign()
1220 CONST = 987
1221 enddef
1222 defcompile
1223 END
1224 writefile(import_assign_to_const, 'Ximport.vim')
1225 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1226
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001227 # try changing an imported final
1228 var import_assign_to_final =<< trim END
1229 vim9script
1230 import theList from './Xexport.vim'
1231 def Assign()
1232 theList = [2]
1233 enddef
1234 defcompile
1235 END
1236 writefile(import_assign_to_final, 'Ximport.vim')
1237 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1238
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001239 # import a very long name, requires making a copy
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001240 var import_long_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001241 vim9script
1242 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
1243 END
1244 writefile(import_long_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001245 assert_fails('source Ximport.vim', 'E1048:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001246
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001247 var import_no_from_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001248 vim9script
1249 import name './Xexport.vim'
1250 END
1251 writefile(import_no_from_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001252 assert_fails('source Ximport.vim', 'E1070:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001253
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001254 var import_invalid_string_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001255 vim9script
1256 import name from Xexport.vim
1257 END
1258 writefile(import_invalid_string_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001259 assert_fails('source Ximport.vim', 'E1071:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001260
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001261 var import_wrong_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001262 vim9script
1263 import name from './XnoExport.vim'
1264 END
1265 writefile(import_wrong_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001266 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001267
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001268 var import_missing_comma_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001269 vim9script
1270 import {exported name} from './Xexport.vim'
1271 END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001272 writefile(import_missing_comma_lines, 'Ximport3.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001273 assert_fails('source Ximport3.vim', 'E1046:', '', 2, 'Ximport3.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001274
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001275 delete('Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001276 delete('Ximport3.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 delete('Xexport.vim')
1278
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001279 # Check that in a Vim9 script 'cpo' is set to the Vim default.
Bram Moolenaar3e191692021-03-17 17:46:00 +01001280 # Flags added or removed are also applied to the restored value.
1281 set cpo=abcd
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001282 var lines =<< trim END
Bram Moolenaar750802b2020-02-23 18:08:33 +01001283 vim9script
1284 g:cpo_in_vim9script = &cpo
Bram Moolenaar3e191692021-03-17 17:46:00 +01001285 set cpo+=f
1286 set cpo-=c
1287 g:cpo_after_vim9script = &cpo
Bram Moolenaar750802b2020-02-23 18:08:33 +01001288 END
1289 writefile(lines, 'Xvim9_script')
1290 source Xvim9_script
Bram Moolenaar3e191692021-03-17 17:46:00 +01001291 assert_equal('fabd', &cpo)
Bram Moolenaar750802b2020-02-23 18:08:33 +01001292 set cpo&vim
1293 assert_equal(&cpo, g:cpo_in_vim9script)
Bram Moolenaar3e191692021-03-17 17:46:00 +01001294 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
1295 assert_equal(newcpo, g:cpo_after_vim9script)
1296
Bram Moolenaar750802b2020-02-23 18:08:33 +01001297 delete('Xvim9_script')
1298enddef
1299
Bram Moolenaar0a842842021-02-27 22:41:19 +01001300def Test_import_as()
1301 var export_lines =<< trim END
1302 vim9script
1303 export var one = 1
1304 export var yes = 'yes'
1305 END
1306 writefile(export_lines, 'XexportAs')
1307
1308 var import_lines =<< trim END
1309 vim9script
Bram Moolenaar6c7cc342021-04-17 16:38:50 +02001310 var one = 'notused'
1311 var yes = 777
Bram Moolenaar0a842842021-02-27 22:41:19 +01001312 import one as thatOne from './XexportAs'
1313 assert_equal(1, thatOne)
1314 import yes as yesYes from './XexportAs'
1315 assert_equal('yes', yesYes)
1316 END
1317 CheckScriptSuccess(import_lines)
1318
1319 import_lines =<< trim END
1320 vim9script
1321 import {one as thatOne, yes as yesYes} from './XexportAs'
1322 assert_equal(1, thatOne)
1323 assert_equal('yes', yesYes)
1324 assert_fails('echo one', 'E121:')
1325 assert_fails('echo yes', 'E121:')
1326 END
1327 CheckScriptSuccess(import_lines)
1328
1329 delete('XexportAs')
1330enddef
1331
Bram Moolenaar803af682020-08-05 16:20:03 +02001332func g:Trigger()
1333 source Ximport.vim
1334 return "echo 'yes'\<CR>"
1335endfunc
1336
1337def Test_import_export_expr_map()
1338 # check that :import and :export work when buffer is locked
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001339 var export_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001340 vim9script
1341 export def That(): string
1342 return 'yes'
1343 enddef
1344 END
1345 writefile(export_lines, 'Xexport_that.vim')
1346
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001347 var import_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001348 vim9script
1349 import That from './Xexport_that.vim'
1350 assert_equal('yes', That())
1351 END
1352 writefile(import_lines, 'Ximport.vim')
1353
1354 nnoremap <expr> trigger g:Trigger()
1355 feedkeys('trigger', "xt")
1356
Bram Moolenaar730b2482020-08-09 13:02:10 +02001357 delete('Xexport_that.vim')
Bram Moolenaar803af682020-08-05 16:20:03 +02001358 delete('Ximport.vim')
1359 nunmap trigger
1360enddef
1361
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001362def Test_import_in_filetype()
1363 # check that :import works when the buffer is locked
1364 mkdir('ftplugin', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001365 var export_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001366 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001367 export var That = 'yes'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001368 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001369 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001370
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001371 var import_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001372 vim9script
Bram Moolenaar730b2482020-08-09 13:02:10 +02001373 import That from './Xexport_ft.vim'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001374 assert_equal('yes', That)
1375 g:did_load_mytpe = 1
1376 END
1377 writefile(import_lines, 'ftplugin/qf.vim')
1378
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001379 var save_rtp = &rtp
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001380 &rtp = getcwd() .. ',' .. &rtp
1381
1382 filetype plugin on
1383 copen
1384 assert_equal(1, g:did_load_mytpe)
1385
1386 quit!
Bram Moolenaar730b2482020-08-09 13:02:10 +02001387 delete('Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001388 delete('ftplugin', 'rf')
1389 &rtp = save_rtp
1390enddef
1391
Bram Moolenaarefa94442020-08-08 22:16:00 +02001392def Test_use_import_in_mapping()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001393 var lines =<< trim END
Bram Moolenaarefa94442020-08-08 22:16:00 +02001394 vim9script
1395 export def Funcx()
1396 g:result = 42
1397 enddef
1398 END
1399 writefile(lines, 'XsomeExport.vim')
1400 lines =<< trim END
1401 vim9script
1402 import Funcx from './XsomeExport.vim'
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001403 nnoremap <F3> :call <sid>Funcx()<cr>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001404 END
1405 writefile(lines, 'Xmapscript.vim')
1406
1407 source Xmapscript.vim
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001408 feedkeys("\<F3>", "xt")
Bram Moolenaarefa94442020-08-08 22:16:00 +02001409 assert_equal(42, g:result)
1410
1411 unlet g:result
1412 delete('XsomeExport.vim')
1413 delete('Xmapscript.vim')
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001414 nunmap <F3>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001415enddef
1416
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01001417def Test_vim9script_mix()
1418 var lines =<< trim END
1419 if has(g:feature)
1420 " legacy script
1421 let g:legacy = 1
1422 finish
1423 endif
1424 vim9script
1425 g:legacy = 0
1426 END
1427 g:feature = 'eval'
1428 g:legacy = -1
1429 CheckScriptSuccess(lines)
1430 assert_equal(1, g:legacy)
1431
1432 g:feature = 'noteval'
1433 g:legacy = -1
1434 CheckScriptSuccess(lines)
1435 assert_equal(0, g:legacy)
1436enddef
1437
Bram Moolenaar750802b2020-02-23 18:08:33 +01001438def Test_vim9script_fails()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1440 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001441 CheckScriptFailure(['export var some = 123'], 'E1042:')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001442 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001443 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
Bram Moolenaar750802b2020-02-23 18:08:33 +01001444 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
1445
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001446 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001447 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1448
Bram Moolenaare2e40752020-09-04 21:18:46 +02001449 assert_fails('vim9script', 'E1038:')
1450 assert_fails('export something', 'E1043:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451enddef
1452
Bram Moolenaarf7d267e2020-06-17 12:04:54 +02001453func Test_import_fails_without_script()
Bram Moolenaar101f4812020-06-16 23:18:51 +02001454 CheckRunVimInTerminal
1455
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001456 " call indirectly to avoid compilation error for missing functions
Bram Moolenaarc620c052020-07-08 15:16:19 +02001457 call Run_Test_import_fails_on_command_line()
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001458endfunc
1459
Bram Moolenaarc620c052020-07-08 15:16:19 +02001460def Run_Test_import_fails_on_command_line()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001461 var export =<< trim END
Bram Moolenaar101f4812020-06-16 23:18:51 +02001462 vim9script
1463 export def Foo(): number
1464 return 0
1465 enddef
1466 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001467 writefile(export, 'XexportCmd.vim')
Bram Moolenaar101f4812020-06-16 23:18:51 +02001468
Bram Moolenaare0de1712020-12-02 17:36:54 +01001469 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001470 rows: 6, wait_for_ruler: 0})
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001471 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaar101f4812020-06-16 23:18:51 +02001472
Bram Moolenaar730b2482020-08-09 13:02:10 +02001473 delete('XexportCmd.vim')
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001474 StopVimInTerminal(buf)
1475enddef
Bram Moolenaar101f4812020-06-16 23:18:51 +02001476
Bram Moolenaar2b327002020-12-26 15:39:31 +01001477def Test_vim9script_reload_noclear()
1478 var lines =<< trim END
Bram Moolenaara6294952020-12-27 13:39:50 +01001479 vim9script
1480 export var exported = 'thexport'
1481 END
1482 writefile(lines, 'XExportReload')
1483 lines =<< trim END
Bram Moolenaar2b327002020-12-26 15:39:31 +01001484 vim9script noclear
1485 g:loadCount += 1
1486 var s:reloaded = 'init'
Bram Moolenaara6294952020-12-27 13:39:50 +01001487 import exported from './XExportReload'
Bram Moolenaar2b327002020-12-26 15:39:31 +01001488
1489 def Again(): string
1490 return 'again'
1491 enddef
1492
1493 if exists('s:loaded') | finish | endif
1494 var s:loaded = true
1495
1496 var s:notReloaded = 'yes'
1497 s:reloaded = 'first'
1498 def g:Values(): list<string>
Bram Moolenaara6294952020-12-27 13:39:50 +01001499 return [s:reloaded, s:notReloaded, Again(), Once(), exported]
Bram Moolenaar2b327002020-12-26 15:39:31 +01001500 enddef
1501
1502 def Once(): string
1503 return 'once'
1504 enddef
1505 END
1506 writefile(lines, 'XReloaded')
1507 g:loadCount = 0
1508 source XReloaded
1509 assert_equal(1, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001510 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001511 source XReloaded
1512 assert_equal(2, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001513 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001514 source XReloaded
1515 assert_equal(3, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001516 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001517
Bram Moolenaar48e11c12021-01-11 18:47:00 +01001518 delete('XReloaded')
Bram Moolenaara6294952020-12-27 13:39:50 +01001519 delete('XExportReload')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001520 delfunc g:Values
Bram Moolenaar2b327002020-12-26 15:39:31 +01001521 unlet g:loadCount
1522enddef
1523
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001524def Test_vim9script_reload_import()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001525 var lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526 vim9script
1527 const var = ''
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001528 var valone = 1234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001529 def MyFunc(arg: string)
1530 valone = 5678
1531 enddef
1532 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001533 var morelines =<< trim END
1534 var valtwo = 222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 export def GetValtwo(): number
1536 return valtwo
1537 enddef
1538 END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001539 writefile(lines + morelines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 source Xreload.vim
1541 source Xreload.vim
1542 source Xreload.vim
1543
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001544 var testlines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545 vim9script
1546 def TheFunc()
1547 import GetValtwo from './Xreload.vim'
1548 assert_equal(222, GetValtwo())
1549 enddef
1550 TheFunc()
1551 END
1552 writefile(testlines, 'Ximport.vim')
1553 source Ximport.vim
1554
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001555 # Test that when not using "morelines" GetValtwo() and valtwo are still
1556 # defined, because import doesn't reload a script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 writefile(lines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 source Ximport.vim
1559
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001560 # cannot declare a var twice
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 lines =<< trim END
1562 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001563 var valone = 1234
1564 var valone = 5678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 END
1566 writefile(lines, 'Xreload.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001567 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568
1569 delete('Xreload.vim')
1570 delete('Ximport.vim')
1571enddef
1572
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001573" if a script is reloaded with a script-local variable that changed its type, a
1574" compiled function using that variable must fail.
1575def Test_script_reload_change_type()
1576 var lines =<< trim END
1577 vim9script noclear
1578 var str = 'string'
1579 def g:GetStr(): string
1580 return str .. 'xxx'
1581 enddef
1582 END
1583 writefile(lines, 'Xreload.vim')
1584 source Xreload.vim
1585 echo g:GetStr()
1586
1587 lines =<< trim END
1588 vim9script noclear
1589 var str = 1234
1590 END
1591 writefile(lines, 'Xreload.vim')
1592 source Xreload.vim
1593 assert_fails('echo g:GetStr()', 'E1150:')
1594
1595 delfunc g:GetStr
1596 delete('Xreload.vim')
1597enddef
1598
Bram Moolenaarc970e422021-03-17 15:03:04 +01001599" Define CallFunc so that the test can be compiled
1600command CallFunc echo 'nop'
1601
1602def Test_script_reload_from_function()
1603 var lines =<< trim END
1604 vim9script
1605
1606 if exists('g:loaded')
1607 finish
1608 endif
1609 g:loaded = 1
1610 delcommand CallFunc
1611 command CallFunc Func()
1612 def Func()
Bram Moolenaara4c81be2021-03-17 15:23:16 +01001613 so XreloadFunc.vim
Bram Moolenaarc970e422021-03-17 15:03:04 +01001614 g:didTheFunc = 1
1615 enddef
1616 END
1617 writefile(lines, 'XreloadFunc.vim')
1618 source XreloadFunc.vim
1619 CallFunc
1620 assert_equal(1, g:didTheFunc)
1621
1622 delete('XreloadFunc.vim')
1623 delcommand CallFunc
1624 unlet g:loaded
1625 unlet g:didTheFunc
1626enddef
1627
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001628def Test_script_var_shadows_function()
1629 var lines =<< trim END
1630 vim9script
1631 def Func(): number
1632 return 123
1633 enddef
1634 var Func = 1
1635 END
1636 CheckScriptFailure(lines, 'E1041:', 5)
1637enddef
1638
Bram Moolenaar95006e32020-08-29 17:47:08 +02001639def s:RetSome(): string
1640 return 'some'
1641enddef
1642
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001643" Not exported function that is referenced needs to be accessed by the
1644" script-local name.
1645def Test_vim9script_funcref()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001646 var sortlines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001647 vim9script
1648 def Compare(i1: number, i2: number): number
Bram Moolenaarbed36b92020-07-07 23:31:36 +02001649 return i2 - i1
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001650 enddef
1651
1652 export def FastSort(): list<number>
1653 return range(5)->sort(Compare)
1654 enddef
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001655
1656 export def GetString(arg: string): string
1657 return arg
1658 enddef
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001659 END
1660 writefile(sortlines, 'Xsort.vim')
1661
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001662 var lines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001663 vim9script
1664 import FastSort from './Xsort.vim'
1665 def Test()
1666 g:result = FastSort()
1667 enddef
1668 Test()
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001669
1670 # using a function imported with "as"
1671 import * as anAlias from './Xsort.vim'
1672 assert_equal('yes', anAlias.GetString('yes'))
1673
1674 # using the function from a compiled function
1675 def TestMore(): string
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001676 var s = s:anAlias.GetString('foo')
1677 return s .. anAlias.GetString('bar')
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001678 enddef
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001679 assert_equal('foobar', TestMore())
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001680
1681 # error when using a function that isn't exported
1682 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001683 END
1684 writefile(lines, 'Xscript.vim')
1685
1686 source Xscript.vim
1687 assert_equal([4, 3, 2, 1, 0], g:result)
1688
1689 unlet g:result
1690 delete('Xsort.vim')
1691 delete('Xscript.vim')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001692
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001693 var Funcref = function('s:RetSome')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001694 assert_equal('some', Funcref())
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001695enddef
1696
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001697" Check that when searching for "FilterFunc" it finds the import in the
1698" script where FastFilter() is called from, both as a string and as a direct
1699" function reference.
Bram Moolenaarc620c052020-07-08 15:16:19 +02001700def Test_vim9script_funcref_other_script()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001701 var filterLines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001702 vim9script
1703 export def FilterFunc(idx: number, val: number): bool
1704 return idx % 2 == 1
1705 enddef
1706 export def FastFilter(): list<number>
1707 return range(10)->filter('FilterFunc')
1708 enddef
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001709 export def FastFilterDirect(): list<number>
1710 return range(10)->filter(FilterFunc)
1711 enddef
Bram Moolenaarc620c052020-07-08 15:16:19 +02001712 END
1713 writefile(filterLines, 'Xfilter.vim')
1714
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001715 var lines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001716 vim9script
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001717 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim'
Bram Moolenaarc620c052020-07-08 15:16:19 +02001718 def Test()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001719 var x: list<number> = FastFilter()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001720 enddef
1721 Test()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001722 def TestDirect()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001723 var x: list<number> = FastFilterDirect()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001724 enddef
1725 TestDirect()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001726 END
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001727 CheckScriptSuccess(lines)
Bram Moolenaarc620c052020-07-08 15:16:19 +02001728 delete('Xfilter.vim')
Bram Moolenaarc620c052020-07-08 15:16:19 +02001729enddef
1730
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001731def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001732 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001733 vim9script
1734 def FuncYes(): string
1735 return 'yes'
1736 enddef
1737 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001738 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001739 def FuncNo(): string
1740 return 'no'
1741 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001742 def g:DoCheck(no_exists: bool)
1743 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001744 assert_equal('no', FuncNo())
1745 enddef
1746 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001747 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001748 def g:DoCheck(no_exists: bool)
1749 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02001750 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001751 enddef
1752 END
1753
1754 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001755 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001756 source Xreloaded.vim
1757 g:DoCheck(true)
1758
1759 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001760 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001761 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001762 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001763
1764 # FuncNo() is back
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001765 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001766 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001767 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001768
1769 delete('Xreloaded.vim')
1770enddef
1771
Bram Moolenaar89483d42020-05-10 15:24:44 +02001772def Test_vim9script_reload_delvar()
1773 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001774 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02001775 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001776 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001777 END
1778 writefile(lines, 'XreloadVar.vim')
1779 source XreloadVar.vim
1780
1781 # now write the script using the same variable locally - works
1782 lines =<< trim END
1783 vim9script
1784 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001785 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001786 enddef
1787 END
1788 writefile(lines, 'XreloadVar.vim')
1789 source XreloadVar.vim
1790
1791 delete('XreloadVar.vim')
1792enddef
1793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794def Test_import_absolute()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001795 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02001796 'vim9script',
1797 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
1798 'def UseExported()',
1799 ' g:imported_abs = exported',
1800 ' exported = 8888',
1801 ' g:imported_after = exported',
1802 'enddef',
1803 'UseExported()',
1804 'g:import_disassembled = execute("disass UseExported")',
1805 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 writefile(import_lines, 'Ximport_abs.vim')
1807 writefile(s:export_script_lines, 'Xexport_abs.vim')
1808
1809 source Ximport_abs.vim
1810
1811 assert_equal(9876, g:imported_abs)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001812 assert_equal(8888, g:imported_after)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001813 assert_match('<SNR>\d\+_UseExported\_s*' ..
1814 'g:imported_abs = exported\_s*' ..
1815 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1816 '1 STOREG g:imported_abs\_s*' ..
1817 'exported = 8888\_s*' ..
1818 '2 PUSHNR 8888\_s*' ..
1819 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1820 'g:imported_after = exported\_s*' ..
1821 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1822 '5 STOREG g:imported_after',
Bram Moolenaare6085c52020-04-12 20:19:16 +02001823 g:import_disassembled)
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001824
1825 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 unlet g:imported_abs
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001827 unlet g:import_disassembled
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828
1829 delete('Ximport_abs.vim')
1830 delete('Xexport_abs.vim')
1831enddef
1832
1833def Test_import_rtp()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001834 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02001835 'vim9script',
1836 'import exported from "Xexport_rtp.vim"',
1837 'g:imported_rtp = exported',
1838 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 writefile(import_lines, 'Ximport_rtp.vim')
1840 mkdir('import')
1841 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1842
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001843 var save_rtp = &rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844 &rtp = getcwd()
1845 source Ximport_rtp.vim
1846 &rtp = save_rtp
1847
1848 assert_equal(9876, g:imported_rtp)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001850 Undo_export_script_lines()
1851 unlet g:imported_rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 delete('Ximport_rtp.vim')
Bram Moolenaar89483d42020-05-10 15:24:44 +02001853 delete('import', 'rf')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854enddef
1855
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001856def Test_import_compile_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001857 var export_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001858 'vim9script',
1859 'export def ExpFunc(): string',
1860 ' return notDefined',
1861 'enddef',
1862 ]
1863 writefile(export_lines, 'Xexported.vim')
1864
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001865 var import_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001866 'vim9script',
1867 'import ExpFunc from "./Xexported.vim"',
1868 'def ImpFunc()',
1869 ' echo ExpFunc()',
1870 'enddef',
1871 'defcompile',
1872 ]
1873 writefile(import_lines, 'Ximport.vim')
1874
1875 try
1876 source Ximport.vim
1877 catch /E1001/
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001878 # Error should be fore the Xexported.vim file.
Bram Moolenaar77072282020-09-16 17:55:40 +02001879 assert_match('E1001: Variable not found: notDefined', v:exception)
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001880 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1881 endtry
1882
1883 delete('Xexported.vim')
1884 delete('Ximport.vim')
1885enddef
1886
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001887def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001888 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001889 'vim9script',
1890 'def Func()',
1891 ' eval [][0]',
1892 'enddef',
1893 'Func()',
1894 ]
1895 writefile(lines, 'Xtestscript.vim')
1896
1897 for count in range(3)
1898 try
1899 source Xtestscript.vim
1900 catch /E684/
1901 # function name should contain <SNR> every time
1902 assert_match('E684: list index out of range', v:exception)
1903 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
1904 endtry
1905 endfor
1906
1907 delete('Xtestscript.vim')
1908enddef
1909
Bram Moolenaareef21022020-08-01 22:16:43 +02001910def Test_func_overrules_import_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001911 var export_lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02001912 vim9script
1913 export def Func()
1914 echo 'imported'
1915 enddef
1916 END
1917 writefile(export_lines, 'XexportedFunc.vim')
1918
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001919 var lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02001920 vim9script
1921 import Func from './XexportedFunc.vim'
1922 def Func()
1923 echo 'local to function'
1924 enddef
1925 END
1926 CheckScriptFailure(lines, 'E1073:')
1927
1928 lines =<< trim END
1929 vim9script
1930 import Func from './XexportedFunc.vim'
1931 def Outer()
1932 def Func()
1933 echo 'local to function'
1934 enddef
1935 enddef
1936 defcompile
1937 END
1938 CheckScriptFailure(lines, 'E1073:')
1939
1940 delete('XexportedFunc.vim')
1941enddef
1942
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02001943def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001944 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02001945 vim9script
1946 def Func()
1947 echo 'one'
1948 enddef
1949 def Func()
1950 echo 'two'
1951 enddef
1952 END
1953 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02001954
1955 lines =<< trim END
1956 vim9script
1957 def Foo(): string
1958 return 'foo'
1959 enddef
1960 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001961 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02001962 enddef
1963 defcompile
1964 END
1965 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02001966enddef
1967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001969 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001970 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971 l->remove(0)
1972 l->add(5)
1973 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001974 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975enddef
1976
Bram Moolenaarae616492020-07-28 20:07:27 +02001977def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001978 CheckDefExecFailure(['a = 1'], 'E1100:')
1979 CheckDefExecFailure(['c = 1'], 'E1100:')
1980 CheckDefExecFailure(['i = 1'], 'E1100:')
1981 CheckDefExecFailure(['t = 1'], 'E1100:')
1982 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02001983
Bram Moolenaarae616492020-07-28 20:07:27 +02001984 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
1985 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02001986 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
1987 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02001988 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
1989 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01001990 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
1991 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02001992 CheckScriptFailure(['vim9script', 't'], 'E1100:')
1993 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
1994 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02001995enddef
1996
Bram Moolenaar158906c2020-02-06 20:39:45 +01001997def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001998 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01001999 if what == 1
2000 res = "one"
2001 elseif what == 2
2002 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002003 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01002004 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002005 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01002006 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002007enddef
2008
Bram Moolenaar158906c2020-02-06 20:39:45 +01002009def Test_if_elseif_else()
2010 assert_equal('one', IfElse(1))
2011 assert_equal('two', IfElse(2))
2012 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002013enddef
2014
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002015def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002016 CheckDefFailure(['elseif true'], 'E582:')
2017 CheckDefFailure(['else'], 'E581:')
2018 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01002019 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002020 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002021
2022 var lines =<< trim END
2023 var s = ''
2024 if s = ''
2025 endif
2026 END
2027 CheckDefFailure(lines, 'E488:')
2028
2029 lines =<< trim END
2030 var s = ''
2031 if s == ''
2032 elseif s = ''
2033 endif
2034 END
2035 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002036enddef
2037
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002038let g:bool_true = v:true
2039let g:bool_false = v:false
2040
2041def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002042 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002043 if true ? true : false
2044 res = true
2045 endif
2046 assert_equal(true, res)
2047
Bram Moolenaar585fea72020-04-02 22:33:21 +02002048 g:glob = 2
2049 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02002050 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002051 endif
2052 assert_equal(2, g:glob)
2053 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02002054 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002055 endif
2056 assert_equal(3, g:glob)
2057
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002058 res = false
2059 if g:bool_true ? true : false
2060 res = true
2061 endif
2062 assert_equal(true, res)
2063
2064 res = false
2065 if true ? g:bool_true : false
2066 res = true
2067 endif
2068 assert_equal(true, res)
2069
2070 res = false
2071 if true ? true : g:bool_false
2072 res = true
2073 endif
2074 assert_equal(true, res)
2075
2076 res = false
2077 if true ? false : true
2078 res = true
2079 endif
2080 assert_equal(false, res)
2081
2082 res = false
2083 if false ? false : true
2084 res = true
2085 endif
2086 assert_equal(true, res)
2087
2088 res = false
2089 if false ? true : false
2090 res = true
2091 endif
2092 assert_equal(false, res)
2093
2094 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002095 if has('xyz') ? true : false
2096 res = true
2097 endif
2098 assert_equal(false, res)
2099
2100 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002101 if true && true
2102 res = true
2103 endif
2104 assert_equal(true, res)
2105
2106 res = false
2107 if true && false
2108 res = true
2109 endif
2110 assert_equal(false, res)
2111
2112 res = false
2113 if g:bool_true && false
2114 res = true
2115 endif
2116 assert_equal(false, res)
2117
2118 res = false
2119 if true && g:bool_false
2120 res = true
2121 endif
2122 assert_equal(false, res)
2123
2124 res = false
2125 if false && false
2126 res = true
2127 endif
2128 assert_equal(false, res)
2129
2130 res = false
2131 if true || false
2132 res = true
2133 endif
2134 assert_equal(true, res)
2135
2136 res = false
2137 if g:bool_true || false
2138 res = true
2139 endif
2140 assert_equal(true, res)
2141
2142 res = false
2143 if true || g:bool_false
2144 res = true
2145 endif
2146 assert_equal(true, res)
2147
2148 res = false
2149 if false || false
2150 res = true
2151 endif
2152 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02002153
2154 # with constant "false" expression may be invalid so long as the syntax is OK
2155 if false | eval 0 | endif
2156 if false | eval burp + 234 | endif
2157 if false | echo burp 234 'asd' | endif
2158 if false
2159 burp
2160 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002161enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002162
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002163def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002164 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
2165 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
2166 CheckDefFailure(["if has('aaa'"], 'E110:')
2167 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002168enddef
2169
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002170def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002171 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002172 if i % 2
2173 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002174 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002175 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002176 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002177 endif
2178 x += 1
2179 else
2180 x += 1000
2181 endif
2182 return x
2183enddef
2184
2185def Test_nested_if()
2186 assert_equal(1, RunNested(1))
2187 assert_equal(1000, RunNested(2))
2188enddef
2189
Bram Moolenaarad39c092020-02-26 18:23:43 +01002190def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01002191 # missing argument is ignored
2192 execute
2193 execute # comment
2194
Bram Moolenaarad39c092020-02-26 18:23:43 +01002195 new
2196 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002197 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002198 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002199
Bram Moolenaard2c61702020-09-06 15:58:36 +02002200 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002201 assert_equal('execute-string', getline(1))
2202
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002203 var cmd1 = 'setline(1,'
2204 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002205 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01002206 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002207
Bram Moolenaard2c61702020-09-06 15:58:36 +02002208 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002209 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002210
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002211 var cmd_first = 'call '
2212 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002213 execute cmd_first .. cmd_last
2214 assert_equal('execute-var-var', getline(1))
2215 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02002216
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002217 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002218 execute 'echomsg' (n ? '"true"' : '"no"')
2219 assert_match('^true$', Screenline(&lines))
2220
Bram Moolenaare0de1712020-12-02 17:36:54 +01002221 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002222 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
2223
Bram Moolenaard2c61702020-09-06 15:58:36 +02002224 CheckDefFailure(['execute xxx'], 'E1001:', 1)
2225 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
2226 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002227enddef
2228
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002229def Test_execute_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002230 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002231 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002232 vim9script
2233 execute 'g:someVar'
2234 .. ' = ' ..
2235 '28'
2236 assert_equal(28, g:someVar)
2237 unlet g:someVar
2238 END
2239 CheckScriptSuccess(lines)
2240enddef
2241
Bram Moolenaarad39c092020-02-26 18:23:43 +01002242def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002243 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02002244 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002245 assert_match('^something$', Screenline(&lines))
2246
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002247 echo "some" # comment
2248 echon "thing"
2249 assert_match('^something$', Screenline(&lines))
2250
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002251 var str1 = 'some'
2252 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002253 echo str1 str2
2254 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002255
Bram Moolenaard2c61702020-09-06 15:58:36 +02002256 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01002257enddef
2258
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002259def Test_echomsg_cmd()
2260 echomsg 'some' 'more' # comment
2261 assert_match('^some more$', Screenline(&lines))
2262 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002263 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002264 assert_match('^some more$', Screenline(&lines))
2265
Bram Moolenaard2c61702020-09-06 15:58:36 +02002266 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002267enddef
2268
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002269def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002270 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002271 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002272 vim9script
2273 echomsg 'here'
2274 .. ' is ' ..
2275 'a message'
2276 assert_match('^here is a message$', Screenline(&lines))
2277 END
2278 CheckScriptSuccess(lines)
2279enddef
2280
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002281def Test_echoerr_cmd()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002282 try
2283 echoerr 'something' 'wrong' # comment
2284 catch
2285 assert_match('something wrong', v:exception)
2286 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002287enddef
2288
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002289def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002290 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002291 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002292 vim9script
2293 try
2294 echoerr 'this'
2295 .. ' is ' ..
2296 'wrong'
2297 catch
2298 assert_match('this is wrong', v:exception)
2299 endtry
2300 END
2301 CheckScriptSuccess(lines)
2302enddef
2303
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002304def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002305 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002306 vim9script
2307 new
2308 for var in range(0, 3)
2309 append(line('$'), var)
2310 endfor
2311 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2312 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01002313
2314 var result = ''
2315 for i in [1, 2, 3]
2316 var loop = ' loop ' .. i
2317 result ..= loop
2318 endfor
2319 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002320 END
2321 writefile(lines, 'Xvim9for.vim')
2322 source Xvim9for.vim
2323 delete('Xvim9for.vim')
2324enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002326def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002327 var lines =<< trim END
2328 var result = ''
2329 for cnt in range(7)
2330 if cnt == 4
2331 break
2332 endif
2333 if cnt == 2
2334 continue
2335 endif
2336 result ..= cnt .. '_'
2337 endfor
2338 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02002339
Bram Moolenaarf2253962021-04-13 20:53:13 +02002340 var concat = ''
2341 for str in eval('["one", "two"]')
2342 concat ..= str
2343 endfor
2344 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002345
Bram Moolenaarf2253962021-04-13 20:53:13 +02002346 var total = 0
2347 for nr in
2348 [1, 2, 3]
2349 total += nr
2350 endfor
2351 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002352
Bram Moolenaarf2253962021-04-13 20:53:13 +02002353 total = 0
2354 for nr
2355 in [1, 2, 3]
2356 total += nr
2357 endfor
2358 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002359
Bram Moolenaarf2253962021-04-13 20:53:13 +02002360 total = 0
2361 for nr
2362 in
2363 [1, 2, 3]
2364 total += nr
2365 endfor
2366 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01002367
Bram Moolenaara3589a02021-04-14 13:30:46 +02002368 # with type
2369 total = 0
2370 for n: number in [1, 2, 3]
2371 total += n
2372 endfor
2373 assert_equal(6, total)
2374
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002375 var chars = ''
2376 for s: string in 'foobar'
2377 chars ..= s
2378 endfor
2379 assert_equal('foobar', chars)
2380
Bram Moolenaara3589a02021-04-14 13:30:46 +02002381 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02002382 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02002383 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2384 res ..= n .. s
2385 endfor
2386 assert_equal('1a2b', res)
2387
2388 # loop over string
2389 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02002390 for c in 'aéc̀d'
2391 res ..= c .. '-'
2392 endfor
2393 assert_equal('a-é-c̀-d-', res)
2394
2395 res = ''
2396 for c in ''
2397 res ..= c .. '-'
2398 endfor
2399 assert_equal('', res)
2400
2401 res = ''
2402 for c in test_null_string()
2403 res ..= c .. '-'
2404 endfor
2405 assert_equal('', res)
2406
2407 var foo: list<dict<any>> = [
2408 {a: 'Cat'}
2409 ]
2410 for dd in foo
2411 dd.counter = 12
2412 endfor
2413 assert_equal([{a: 'Cat', counter: 12}], foo)
2414 END
2415 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002416enddef
2417
2418def Test_for_loop_fails()
Bram Moolenaar025cb1c2020-12-14 18:31:27 +01002419 CheckDefFailure(['for '], 'E1097:')
2420 CheckDefFailure(['for x'], 'E1097:')
2421 CheckDefFailure(['for x in'], 'E1097:')
Bram Moolenaar675f7162020-04-12 22:53:54 +02002422 CheckDefFailure(['for # in range(5)'], 'E690:')
2423 CheckDefFailure(['for i In range(5)'], 'E690:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002424 CheckDefFailure(['var x = 5', 'for x in range(5)'], 'E1017:')
Bram Moolenaar822ba242020-05-24 23:00:18 +02002425 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002426 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02002427 CheckDefFailure(['for i in xxx'], 'E1001:')
2428 CheckDefFailure(['endfor'], 'E588:')
2429 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002430
2431 # wrong type detected at compile time
2432 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2433
2434 # wrong type detected at runtime
2435 g:adict = {a: 1}
2436 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2437 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002438
2439 var lines =<< trim END
2440 var d: list<dict<any>> = [{a: 0}]
2441 for e in d
2442 e = {a: 0, b: ''}
2443 endfor
2444 END
2445 CheckDefAndScriptFailure2(lines, 'E1018:', 'E46:', 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002446
2447 lines =<< trim END
2448 for nr: number in ['foo']
2449 endfor
2450 END
2451 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002452enddef
2453
Bram Moolenaarea870692020-12-02 14:24:30 +01002454def Test_for_loop_script_var()
2455 # cannot use s:var in a :def function
2456 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1101:')
2457
2458 # can use s:var in Vim9 script, with or without s:
2459 var lines =<< trim END
2460 vim9script
2461 var total = 0
2462 for s:var in [1, 2, 3]
2463 total += s:var
2464 endfor
2465 assert_equal(6, total)
2466
2467 total = 0
2468 for var in [1, 2, 3]
2469 total += var
2470 endfor
2471 assert_equal(6, total)
2472 END
2473enddef
2474
Bram Moolenaar792f7862020-11-23 08:31:18 +01002475def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01002476 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002477 var result = []
2478 for [v1, v2] in [[1, 2], [3, 4]]
2479 result->add(v1)
2480 result->add(v2)
2481 endfor
2482 assert_equal([1, 2, 3, 4], result)
2483
2484 result = []
2485 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2486 result->add(v1)
2487 result->add(v2)
2488 result->add(v3)
2489 endfor
2490 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
2491
2492 result = []
2493 for [&ts, &sw] in [[1, 2], [3, 4]]
2494 result->add(&ts)
2495 result->add(&sw)
2496 endfor
2497 assert_equal([1, 2, 3, 4], result)
2498
2499 var slist: list<string>
2500 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
2501 slist->add($LOOPVAR)
2502 slist->add(@r)
2503 slist->add(v:errmsg)
2504 endfor
2505 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
2506
2507 slist = []
2508 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
2509 slist->add(g:globalvar)
2510 slist->add(b:bufvar)
2511 slist->add(w:winvar)
2512 slist->add(t:tabvar)
2513 endfor
2514 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01002515 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaarb777da92021-05-22 21:40:39 +02002516
2517 var res = []
2518 for [_, n, _] in [[1, 2, 3], [4, 5, 6]]
2519 res->add(n)
2520 endfor
2521 assert_equal([2, 5], res)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002522 END
2523 CheckDefAndScriptSuccess(lines)
2524
2525 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01002526 for [v1, v2] in [[1, 2, 3], [3, 4]]
2527 echo v1 v2
2528 endfor
2529 END
2530 CheckDefExecFailure(lines, 'E710:', 1)
2531
2532 lines =<< trim END
2533 for [v1, v2] in [[1], [3, 4]]
2534 echo v1 v2
2535 endfor
2536 END
2537 CheckDefExecFailure(lines, 'E711:', 1)
2538
2539 lines =<< trim END
2540 for [v1, v1] in [[1, 2], [3, 4]]
2541 echo v1
2542 endfor
2543 END
2544 CheckDefExecFailure(lines, 'E1017:', 1)
2545enddef
2546
Bram Moolenaarc150c092021-02-13 15:02:46 +01002547def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002548 var lines =<< trim END
2549 var looped = 0
2550 var cleanup = 0
2551 for i in range(3)
2552 looped += 1
2553 try
2554 eval [][0]
2555 catch
2556 continue
2557 finally
2558 cleanup += 1
2559 endtry
2560 endfor
2561 assert_equal(3, looped)
2562 assert_equal(3, cleanup)
2563 END
2564 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002565enddef
2566
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002567def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002568 var result = ''
2569 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002570 while cnt < 555
2571 if cnt == 3
2572 break
2573 endif
2574 cnt += 1
2575 if cnt == 2
2576 continue
2577 endif
2578 result ..= cnt .. '_'
2579 endwhile
2580 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002581
2582 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002583 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002584 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002585enddef
2586
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002587def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02002588 CheckDefFailure(['while xxx'], 'E1001:')
2589 CheckDefFailure(['endwhile'], 'E588:')
2590 CheckDefFailure(['continue'], 'E586:')
2591 CheckDefFailure(['if true', 'continue'], 'E586:')
2592 CheckDefFailure(['break'], 'E587:')
2593 CheckDefFailure(['if true', 'break'], 'E587:')
2594 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002595
2596 var lines =<< trim END
2597 var s = ''
2598 while s = ''
2599 endwhile
2600 END
2601 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002602enddef
2603
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002604def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002605 var caught = false
2606 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002607 try
2608 while 1
2609 x += 1
2610 if x == 100
2611 feedkeys("\<C-C>", 'Lt')
2612 endif
2613 endwhile
2614 catch
2615 caught = true
2616 assert_equal(100, x)
2617 endtry
2618 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02002619 # consume the CTRL-C
2620 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002621enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01002622
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002623def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002624 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002625 'one',
2626 'two',
2627 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002628 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002629 assert_equal(['one', 'two', 'three'], mylist)
2630
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002631 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01002632 ['one']: 1,
2633 ['two']: 2,
2634 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002635 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002636 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01002637 assert_equal({one: 1, two: 2, three: 3}, mydict)
2638 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002639 one: 1, # comment
2640 two: # comment
2641 2, # comment
2642 three: 3 # comment
2643 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002644 assert_equal({one: 1, two: 2, three: 3}, mydict)
2645 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002646 one: 1,
2647 two:
2648 2,
2649 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002650 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002651 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002652
2653 assert_equal(
2654 ['one', 'two', 'three'],
2655 split('one two three')
2656 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002657enddef
2658
Bram Moolenaar7a092242020-04-16 22:10:49 +02002659def Test_vim9_comment()
2660 CheckScriptSuccess([
2661 'vim9script',
2662 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002663 '#something',
2664 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002665 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002666
2667 split Xfile
2668 CheckScriptSuccess([
2669 'vim9script',
2670 'edit #something',
2671 ])
2672 CheckScriptSuccess([
2673 'vim9script',
2674 'edit #{something',
2675 ])
2676 close
2677
Bram Moolenaar7a092242020-04-16 22:10:49 +02002678 CheckScriptFailure([
2679 'vim9script',
2680 ':# something',
2681 ], 'E488:')
2682 CheckScriptFailure([
2683 '# something',
2684 ], 'E488:')
2685 CheckScriptFailure([
2686 ':# something',
2687 ], 'E488:')
2688
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002689 { # block start
2690 } # block end
2691 CheckDefFailure([
2692 '{# comment',
2693 ], 'E488:')
2694 CheckDefFailure([
2695 '{',
2696 '}# comment',
2697 ], 'E488:')
2698
2699 echo "yes" # comment
2700 CheckDefFailure([
2701 'echo "yes"# comment',
2702 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002703 CheckScriptSuccess([
2704 'vim9script',
2705 'echo "yes" # something',
2706 ])
2707 CheckScriptFailure([
2708 'vim9script',
2709 'echo "yes"# something',
2710 ], 'E121:')
2711 CheckScriptFailure([
2712 'vim9script',
2713 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002714 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002715 CheckScriptFailure([
2716 'echo "yes" # something',
2717 ], 'E121:')
2718
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002719 exe "echo" # comment
2720 CheckDefFailure([
2721 'exe "echo"# comment',
2722 ], 'E488:')
2723 CheckScriptSuccess([
2724 'vim9script',
2725 'exe "echo" # something',
2726 ])
2727 CheckScriptFailure([
2728 'vim9script',
2729 'exe "echo"# something',
2730 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002731 CheckScriptFailure([
2732 'vim9script',
2733 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002734 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002735 CheckScriptFailure([
2736 'exe "echo" # something',
2737 ], 'E121:')
2738
Bram Moolenaar7a092242020-04-16 22:10:49 +02002739 CheckDefFailure([
2740 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002741 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002742 'catch',
2743 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002744 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002745 CheckScriptFailure([
2746 'vim9script',
2747 'try# comment',
2748 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002749 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002750 CheckDefFailure([
2751 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002752 ' throw#comment',
2753 'catch',
2754 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002755 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002756 CheckDefFailure([
2757 'try',
2758 ' throw "yes"#comment',
2759 'catch',
2760 'endtry',
2761 ], 'E488:')
2762 CheckDefFailure([
2763 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002764 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002765 'catch# comment',
2766 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002767 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002768 CheckScriptFailure([
2769 'vim9script',
2770 'try',
2771 ' echo "yes"',
2772 'catch# comment',
2773 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002774 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002775 CheckDefFailure([
2776 'try',
2777 ' echo "yes"',
2778 'catch /pat/# comment',
2779 'endtry',
2780 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002781 CheckDefFailure([
2782 'try',
2783 'echo "yes"',
2784 'catch',
2785 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002786 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002787 CheckScriptFailure([
2788 'vim9script',
2789 'try',
2790 ' echo "yes"',
2791 'catch',
2792 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002793 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002794
2795 CheckScriptSuccess([
2796 'vim9script',
2797 'hi # comment',
2798 ])
2799 CheckScriptFailure([
2800 'vim9script',
2801 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002802 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002803 CheckScriptSuccess([
2804 'vim9script',
2805 'hi Search # comment',
2806 ])
2807 CheckScriptFailure([
2808 'vim9script',
2809 'hi Search# comment',
2810 ], 'E416:')
2811 CheckScriptSuccess([
2812 'vim9script',
2813 'hi link This Search # comment',
2814 ])
2815 CheckScriptFailure([
2816 'vim9script',
2817 'hi link This That# comment',
2818 ], 'E413:')
2819 CheckScriptSuccess([
2820 'vim9script',
2821 'hi clear This # comment',
2822 'hi clear # comment',
2823 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002824 # not tested, because it doesn't give an error but a warning:
2825 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02002826 CheckScriptFailure([
2827 'vim9script',
2828 'hi clear# comment',
2829 ], 'E416:')
2830
2831 CheckScriptSuccess([
2832 'vim9script',
2833 'hi Group term=bold',
2834 'match Group /todo/ # comment',
2835 ])
2836 CheckScriptFailure([
2837 'vim9script',
2838 'hi Group term=bold',
2839 'match Group /todo/# comment',
2840 ], 'E488:')
2841 CheckScriptSuccess([
2842 'vim9script',
2843 'match # comment',
2844 ])
2845 CheckScriptFailure([
2846 'vim9script',
2847 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002848 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002849 CheckScriptSuccess([
2850 'vim9script',
2851 'match none # comment',
2852 ])
2853 CheckScriptFailure([
2854 'vim9script',
2855 'match none# comment',
2856 ], 'E475:')
2857
2858 CheckScriptSuccess([
2859 'vim9script',
2860 'menutrans clear # comment',
2861 ])
2862 CheckScriptFailure([
2863 'vim9script',
2864 'menutrans clear# comment text',
2865 ], 'E474:')
2866
2867 CheckScriptSuccess([
2868 'vim9script',
2869 'syntax clear # comment',
2870 ])
2871 CheckScriptFailure([
2872 'vim9script',
2873 'syntax clear# comment text',
2874 ], 'E28:')
2875 CheckScriptSuccess([
2876 'vim9script',
2877 'syntax keyword Word some',
2878 'syntax clear Word # comment',
2879 ])
2880 CheckScriptFailure([
2881 'vim9script',
2882 'syntax keyword Word some',
2883 'syntax clear Word# comment text',
2884 ], 'E28:')
2885
2886 CheckScriptSuccess([
2887 'vim9script',
2888 'syntax list # comment',
2889 ])
2890 CheckScriptFailure([
2891 'vim9script',
2892 'syntax list# comment text',
2893 ], 'E28:')
2894
2895 CheckScriptSuccess([
2896 'vim9script',
2897 'syntax match Word /pat/ oneline # comment',
2898 ])
2899 CheckScriptFailure([
2900 'vim9script',
2901 'syntax match Word /pat/ oneline# comment',
2902 ], 'E475:')
2903
2904 CheckScriptSuccess([
2905 'vim9script',
2906 'syntax keyword Word word # comm[ent',
2907 ])
2908 CheckScriptFailure([
2909 'vim9script',
2910 'syntax keyword Word word# comm[ent',
2911 ], 'E789:')
2912
2913 CheckScriptSuccess([
2914 'vim9script',
2915 'syntax match Word /pat/ # comment',
2916 ])
2917 CheckScriptFailure([
2918 'vim9script',
2919 'syntax match Word /pat/# comment',
2920 ], 'E402:')
2921
2922 CheckScriptSuccess([
2923 'vim9script',
2924 'syntax match Word /pat/ contains=Something # comment',
2925 ])
2926 CheckScriptFailure([
2927 'vim9script',
2928 'syntax match Word /pat/ contains=Something# comment',
2929 ], 'E475:')
2930 CheckScriptFailure([
2931 'vim9script',
2932 'syntax match Word /pat/ contains= # comment',
2933 ], 'E406:')
2934 CheckScriptFailure([
2935 'vim9script',
2936 'syntax match Word /pat/ contains=# comment',
2937 ], 'E475:')
2938
2939 CheckScriptSuccess([
2940 'vim9script',
2941 'syntax region Word start=/pat/ end=/pat/ # comment',
2942 ])
2943 CheckScriptFailure([
2944 'vim9script',
2945 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02002946 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002947
2948 CheckScriptSuccess([
2949 'vim9script',
2950 'syntax sync # comment',
2951 ])
2952 CheckScriptFailure([
2953 'vim9script',
2954 'syntax sync# comment',
2955 ], 'E404:')
2956 CheckScriptSuccess([
2957 'vim9script',
2958 'syntax sync ccomment # comment',
2959 ])
2960 CheckScriptFailure([
2961 'vim9script',
2962 'syntax sync ccomment# comment',
2963 ], 'E404:')
2964
2965 CheckScriptSuccess([
2966 'vim9script',
2967 'syntax cluster Some contains=Word # comment',
2968 ])
2969 CheckScriptFailure([
2970 'vim9script',
2971 'syntax cluster Some contains=Word# comment',
2972 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002973
2974 CheckScriptSuccess([
2975 'vim9script',
2976 'command Echo echo # comment',
2977 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002978 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002979 ])
2980 CheckScriptFailure([
2981 'vim9script',
2982 'command Echo echo# comment',
2983 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002984 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002985 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01002986
2987 var curdir = getcwd()
2988 CheckScriptSuccess([
2989 'command Echo cd " comment',
2990 'Echo',
2991 'delcommand Echo',
2992 ])
2993 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01002994 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01002995 'command Echo cd # comment',
2996 'Echo',
2997 'delcommand Echo',
2998 ])
2999 CheckScriptFailure([
3000 'vim9script',
3001 'command Echo cd " comment',
3002 'Echo',
3003 ], 'E344:')
3004 delcommand Echo
3005 chdir(curdir)
3006
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003007 CheckScriptFailure([
3008 'vim9script',
3009 'command Echo# comment',
3010 ], 'E182:')
3011 CheckScriptFailure([
3012 'vim9script',
3013 'command Echo echo',
3014 'command Echo# comment',
3015 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003016 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003017
3018 CheckScriptSuccess([
3019 'vim9script',
3020 'function # comment',
3021 ])
3022 CheckScriptFailure([
3023 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02003024 'function " comment',
3025 ], 'E129:')
3026 CheckScriptFailure([
3027 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003028 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003029 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003030 CheckScriptSuccess([
3031 'vim9script',
3032 'function CheckScriptSuccess # comment',
3033 ])
3034 CheckScriptFailure([
3035 'vim9script',
3036 'function CheckScriptSuccess# comment',
3037 ], 'E488:')
3038
3039 CheckScriptSuccess([
3040 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003041 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003042 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003043 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003044 ])
3045 CheckScriptFailure([
3046 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003047 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003048 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003049 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003050 ], 'E488:')
3051
3052 CheckScriptSuccess([
3053 'vim9script',
3054 'call execute("ls") # comment',
3055 ])
3056 CheckScriptFailure([
3057 'vim9script',
3058 'call execute("ls")# comment',
3059 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02003060
3061 CheckScriptFailure([
3062 'def Test() " comment',
3063 'enddef',
3064 ], 'E488:')
3065 CheckScriptFailure([
3066 'vim9script',
3067 'def Test() " comment',
3068 'enddef',
3069 ], 'E488:')
3070
3071 CheckScriptSuccess([
3072 'func Test() " comment',
3073 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003074 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02003075 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02003076 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02003077 'vim9script',
3078 'func Test() " comment',
3079 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02003080 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02003081
3082 CheckScriptSuccess([
3083 'def Test() # comment',
3084 'enddef',
3085 ])
3086 CheckScriptFailure([
3087 'func Test() # comment',
3088 'endfunc',
3089 ], 'E488:')
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003090
3091 var lines =<< trim END
3092 vim9script
3093 syn region Text
3094 \ start='foo'
3095 #\ comment
3096 \ end='bar'
Bram Moolenaar5072b472021-06-03 21:56:10 +02003097 syn region Text start='foo'
3098 #\ comment
3099 \ end='bar'
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003100 END
3101 CheckScriptSuccess(lines)
3102
3103 lines =<< trim END
3104 vim9script
3105 syn region Text
3106 \ start='foo'
3107 "\ comment
3108 \ end='bar'
3109 END
3110 CheckScriptFailure(lines, 'E399:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003111enddef
3112
3113def Test_vim9_comment_gui()
3114 CheckCanRunGui
3115
3116 CheckScriptFailure([
3117 'vim9script',
3118 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003119 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003120 CheckScriptFailure([
3121 'vim9script',
3122 'gui -f#comment'
3123 ], 'E499:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003124enddef
3125
Bram Moolenaara26b9702020-04-18 19:53:28 +02003126def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02003127 au TabEnter *.vim g:entered = 1
3128 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02003129
3130 edit test.vim
3131 doautocmd TabEnter #comment
3132 assert_equal(1, g:entered)
3133
3134 doautocmd TabEnter f.x
3135 assert_equal(2, g:entered)
3136
3137 g:entered = 0
3138 doautocmd TabEnter f.x #comment
3139 assert_equal(2, g:entered)
3140
3141 assert_fails('doautocmd Syntax#comment', 'E216:')
3142
3143 au! TabEnter
3144 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003145
3146 CheckScriptSuccess([
3147 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02003148 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003149 'b:var = 456',
3150 'w:var = 777',
3151 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003152 'unlet g:var w:var # something',
3153 ])
3154
3155 CheckScriptFailure([
3156 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003157 'let var = 123',
3158 ], 'E1126: Cannot use :let in Vim9 script')
3159
3160 CheckScriptFailure([
3161 'vim9script',
3162 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003163 ], 'E1016: Cannot declare a global variable:')
3164
3165 CheckScriptFailure([
3166 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003167 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003168 ], 'E1016: Cannot declare a buffer variable:')
3169
3170 CheckScriptFailure([
3171 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003172 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003173 ], 'E1016: Cannot declare a window variable:')
3174
3175 CheckScriptFailure([
3176 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003177 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003178 ], 'E1016: Cannot declare a tab variable:')
3179
3180 CheckScriptFailure([
3181 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003182 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003183 ], 'E1016: Cannot declare a v: variable:')
3184
3185 CheckScriptFailure([
3186 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003187 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003188 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02003189
3190 CheckScriptFailure([
3191 'vim9script',
3192 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003193 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003194 ], 'E108:')
3195
3196 CheckScriptFailure([
3197 'let g:var = 123',
3198 'unlet g:var # something',
3199 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003200
3201 CheckScriptSuccess([
3202 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003203 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003204 ' echo "yes"',
3205 'elseif 2 #comment',
3206 ' echo "no"',
3207 'endif',
3208 ])
3209
3210 CheckScriptFailure([
3211 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003212 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003213 ' echo "yes"',
3214 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003215 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003216
3217 CheckScriptFailure([
3218 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003219 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003220 ' echo "yes"',
3221 'elseif 2#comment',
3222 ' echo "no"',
3223 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003224 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003225
3226 CheckScriptSuccess([
3227 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003228 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003229 ])
3230
3231 CheckScriptFailure([
3232 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003233 'var v = 1# comment6',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003234 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003235
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003236 CheckScriptSuccess([
3237 'vim9script',
3238 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003239 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003240 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003241 'dsearch /pat/ #comment',
3242 'bwipe!',
3243 ])
3244
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003245 CheckScriptFailure([
3246 'vim9script',
3247 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003248 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003249 ':$',
3250 'dsearch /pat/#comment',
3251 'bwipe!',
3252 ], 'E488:')
3253
3254 CheckScriptFailure([
3255 'vim9script',
3256 'func! SomeFunc()',
3257 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02003258enddef
3259
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003260def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003261 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003262 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02003263 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003264 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02003265 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003266 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02003267 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003268 END
3269 writefile(lines, 'Xfinished')
3270 source Xfinished
3271 assert_equal('two', g:res)
3272
3273 unlet g:res
3274 delete('Xfinished')
3275enddef
3276
Bram Moolenaara5d00772020-05-14 23:20:55 +02003277def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003278 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02003279 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02003280 def GetValue(): string
3281 return theVal
3282 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003283 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02003284 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02003285 theVal = 'else'
3286 g:laterVal = GetValue()
3287 END
3288 writefile(lines, 'Xforward')
3289 source Xforward
3290 assert_equal('something', g:initVal)
3291 assert_equal('else', g:laterVal)
3292
3293 unlet g:initVal
3294 unlet g:laterVal
3295 delete('Xforward')
3296enddef
3297
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003298def Test_source_vim9_from_legacy()
Bram Moolenaara6294952020-12-27 13:39:50 +01003299 var vim9_lines =<< trim END
3300 vim9script
3301 var local = 'local'
3302 g:global = 'global'
3303 export var exported = 'exported'
3304 export def GetText(): string
3305 return 'text'
3306 enddef
3307 END
3308 writefile(vim9_lines, 'Xvim9_script.vim')
3309
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003310 var legacy_lines =<< trim END
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003311 source Xvim9_script.vim
3312
3313 call assert_false(exists('local'))
3314 call assert_false(exists('exported'))
3315 call assert_false(exists('s:exported'))
3316 call assert_equal('global', global)
3317 call assert_equal('global', g:global)
3318
3319 " imported variable becomes script-local
3320 import exported from './Xvim9_script.vim'
3321 call assert_equal('exported', s:exported)
3322 call assert_false(exists('exported'))
3323
3324 " imported function becomes script-local
3325 import GetText from './Xvim9_script.vim'
3326 call assert_equal('text', s:GetText())
3327 call assert_false(exists('*GetText'))
3328 END
3329 writefile(legacy_lines, 'Xlegacy_script.vim')
3330
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003331 source Xlegacy_script.vim
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003332 assert_equal('global', g:global)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003333 unlet g:global
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003334
3335 delete('Xlegacy_script.vim')
3336 delete('Xvim9_script.vim')
3337enddef
Bram Moolenaara5d00772020-05-14 23:20:55 +02003338
Bram Moolenaare535db82021-03-31 21:07:24 +02003339def Test_declare_script_in_func()
3340 var lines =<< trim END
3341 vim9script
3342 func Declare()
3343 let s:local = 123
3344 endfunc
3345 Declare()
3346 assert_equal(123, local)
3347
3348 var error: string
3349 try
3350 local = 'asdf'
3351 catch
3352 error = v:exception
3353 endtry
3354 assert_match('E1012: Type mismatch; expected number but got string', error)
3355
3356 lockvar local
3357 try
3358 local = 999
3359 catch
3360 error = v:exception
3361 endtry
3362 assert_match('E741: Value is locked: local', error)
3363 END
3364 CheckScriptSuccess(lines)
3365enddef
3366
3367
Bram Moolenaar7d699702020-08-14 20:52:28 +02003368func Test_vim9script_not_global()
3369 " check that items defined in Vim9 script are script-local, not global
3370 let vim9lines =<< trim END
3371 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003372 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02003373 func TheFunc()
3374 echo 'local'
3375 endfunc
3376 def DefFunc()
3377 echo 'local'
3378 enddef
3379 END
3380 call writefile(vim9lines, 'Xvim9script.vim')
3381 source Xvim9script.vim
3382 try
3383 echo g:var
3384 assert_report('did not fail')
3385 catch /E121:/
3386 " caught
3387 endtry
3388 try
3389 call TheFunc()
3390 assert_report('did not fail')
3391 catch /E117:/
3392 " caught
3393 endtry
3394 try
3395 call DefFunc()
3396 assert_report('did not fail')
3397 catch /E117:/
3398 " caught
3399 endtry
3400
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003401 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02003402endfunc
3403
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02003404def Test_vim9_copen()
3405 # this was giving an error for setting w:quickfix_title
3406 copen
3407 quit
3408enddef
3409
Bram Moolenaar03290b82020-12-19 16:30:44 +01003410" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003411def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003412 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003413 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01003414 def some#gettest(): string
3415 return 'test'
3416 enddef
3417 g:some#name = 'name'
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003418 g:some#dict = {key: 'value'}
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003419
3420 def some#varargs(a1: string, ...l: list<string>): string
3421 return a1 .. l[0] .. l[1]
3422 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01003423 END
3424
3425 mkdir('Xdir/autoload', 'p')
3426 writefile(lines, 'Xdir/autoload/some.vim')
3427 var save_rtp = &rtp
3428 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3429
3430 assert_equal('test', g:some#gettest())
3431 assert_equal('name', g:some#name)
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003432 assert_equal('value', g:some#dict.key)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003433 g:some#other = 'other'
3434 assert_equal('other', g:some#other)
3435
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003436 assert_equal('abc', some#varargs('a', 'b', 'c'))
3437
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003438 # upper case script name works
3439 lines =<< trim END
3440 vim9script
3441 def Other#getOther(): string
3442 return 'other'
3443 enddef
3444 END
3445 writefile(lines, 'Xdir/autoload/Other.vim')
3446 assert_equal('other', g:Other#getOther())
3447
Bram Moolenaar03290b82020-12-19 16:30:44 +01003448 delete('Xdir', 'rf')
3449 &rtp = save_rtp
3450enddef
3451
3452" test using a vim9script that is auto-loaded from an autocmd
3453def Test_vim9_aucmd_autoload()
3454 var lines =<< trim END
3455 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003456 def foo#test()
3457 echomsg getreg('"')
3458 enddef
3459 END
3460
3461 mkdir('Xdir/autoload', 'p')
3462 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003463 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003464 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3465 augroup test
3466 autocmd TextYankPost * call foo#test()
3467 augroup END
3468
3469 normal Y
3470
3471 augroup test
3472 autocmd!
3473 augroup END
3474 delete('Xdir', 'rf')
3475 &rtp = save_rtp
3476enddef
3477
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003478" This was causing a crash because suppress_errthrow wasn't reset.
3479def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003480 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003481 vim9script
3482 def crash#func()
3483 try
3484 for x in List()
3485 endfor
3486 catch
3487 endtry
3488 g:ok = true
3489 enddef
3490 fu List()
3491 invalid
3492 endfu
3493 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01003494 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003495 catch /wontmatch/
3496 endtry
3497 END
3498 call mkdir('Xruntime/autoload', 'p')
3499 call writefile(lines, 'Xruntime/autoload/crash.vim')
3500
3501 # run in a separate Vim to avoid the side effects of assert_fails()
3502 lines =<< trim END
3503 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
3504 call crash#func()
3505 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003506 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003507 END
3508 writefile(lines, 'Xscript')
3509 RunVim([], [], '-S Xscript')
3510 assert_equal(['ok'], readfile('Xdidit'))
3511
3512 delete('Xdidit')
3513 delete('Xscript')
3514 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01003515
3516 lines =<< trim END
3517 vim9script
3518 var foo#bar = 'asdf'
3519 END
3520 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003521enddef
3522
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003523def Test_script_var_in_autocmd()
3524 # using a script variable from an autocommand, defined in a :def function in a
3525 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003526 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003527 let s:counter = 1
3528 def s:Func()
3529 au! CursorHold
3530 au CursorHold * s:counter += 1
3531 enddef
3532 call s:Func()
3533 doau CursorHold
3534 call assert_equal(2, s:counter)
3535 au! CursorHold
3536 END
3537 CheckScriptSuccess(lines)
3538enddef
3539
Bram Moolenaar3896a102020-08-09 14:33:55 +02003540def Test_cmdline_win()
3541 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
3542 # the command line window.
3543 mkdir('rtp/syntax', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003544 var export_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02003545 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003546 export var That = 'yes'
Bram Moolenaar3896a102020-08-09 14:33:55 +02003547 END
3548 writefile(export_lines, 'rtp/syntax/Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003549 var import_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02003550 vim9script
3551 import That from './Xexport.vim'
3552 END
3553 writefile(import_lines, 'rtp/syntax/vim.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003554 var save_rtp = &rtp
Bram Moolenaar3896a102020-08-09 14:33:55 +02003555 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
3556 syntax on
3557 augroup CmdWin
3558 autocmd CmdwinEnter * g:got_there = 'yes'
3559 augroup END
3560 # this will open and also close the cmdline window
3561 feedkeys('q:', 'xt')
3562 assert_equal('yes', g:got_there)
3563
3564 augroup CmdWin
3565 au!
3566 augroup END
3567 &rtp = save_rtp
3568 delete('rtp', 'rf')
3569enddef
3570
Bram Moolenaare3d46852020-08-29 13:39:17 +02003571def Test_invalid_sid()
3572 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003573
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003574 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02003575 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02003576 endif
3577 delete('Xdidit')
3578enddef
3579
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003580def Test_restoring_cpo()
3581 writefile(['vim9script', 'set nocp'], 'Xsourced')
3582 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
3583 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
3584 assert_equal(['done'], readfile('Xdone'))
3585 endif
3586 delete('Xsourced')
3587 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01003588 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01003589
3590 writefile(['vim9script'], 'XanotherScript')
3591 set cpo=aABceFsMny>
3592 edit XanotherScript
3593 so %
3594 assert_equal('aABceFsMny>', &cpo)
3595 :1del
3596 w
3597 so %
3598 assert_equal('aABceFsMny>', &cpo)
3599
3600 delete('XanotherScript')
3601 set cpo&vim
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003602enddef
3603
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003604" Use :function so we can use Check commands
3605func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003606 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003607 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003608
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003609 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003610 vim9script
3611 def script#func()
3612 enddef
3613 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003614 call mkdir('Xdir/autoload', 'p')
3615 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003616
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003617 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003618 vim9script
3619 set cpo+=M
3620 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02003621 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003622 setline(1, 'some text')
3623 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003624 call writefile(lines, 'XTest_redraw_cpo')
3625 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
3626 call term_sendkeys(buf, "V:")
3627 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003628
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003629 " clean up
3630 call term_sendkeys(buf, "\<Esc>u")
3631 call StopVimInTerminal(buf)
3632 call delete('XTest_redraw_cpo')
3633 call delete('Xdir', 'rf')
3634endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003635
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003636
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003637def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003638 var lines =<< trim END
3639 var name: any
3640 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003641 END
3642 CheckDefAndScriptSuccess(lines)
3643enddef
3644
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003645func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02003646 CheckRunVimInTerminal
3647
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003648 " call indirectly to avoid compilation error for missing functions
3649 call Run_Test_define_func_at_command_line()
3650endfunc
3651
3652def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003653 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003654 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003655 func CheckAndQuit()
3656 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
3657 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
3658 endfunc
3659 END
3660 writefile([''], 'Xdidcmd')
3661 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01003662 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003663 # define Afunc() on the command line
3664 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
3665 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003666 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003667
3668 call StopVimInTerminal(buf)
3669 delete('XcallFunc')
3670 delete('Xdidcmd')
3671enddef
3672
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003673def Test_script_var_scope()
3674 var lines =<< trim END
3675 vim9script
3676 if true
3677 if true
3678 var one = 'one'
3679 echo one
3680 endif
3681 echo one
3682 endif
3683 END
3684 CheckScriptFailure(lines, 'E121:', 7)
3685
3686 lines =<< trim END
3687 vim9script
3688 if true
3689 if false
3690 var one = 'one'
3691 echo one
3692 else
3693 var one = 'one'
3694 echo one
3695 endif
3696 echo one
3697 endif
3698 END
3699 CheckScriptFailure(lines, 'E121:', 10)
3700
3701 lines =<< trim END
3702 vim9script
3703 while true
3704 var one = 'one'
3705 echo one
3706 break
3707 endwhile
3708 echo one
3709 END
3710 CheckScriptFailure(lines, 'E121:', 7)
3711
3712 lines =<< trim END
3713 vim9script
3714 for i in range(1)
3715 var one = 'one'
3716 echo one
3717 endfor
3718 echo one
3719 END
3720 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003721
3722 lines =<< trim END
3723 vim9script
3724 {
3725 var one = 'one'
3726 assert_equal('one', one)
3727 }
3728 assert_false(exists('one'))
3729 assert_false(exists('s:one'))
3730 END
3731 CheckScriptSuccess(lines)
3732
3733 lines =<< trim END
3734 vim9script
3735 {
3736 var one = 'one'
3737 echo one
3738 }
3739 echo one
3740 END
3741 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003742enddef
3743
Bram Moolenaar352134b2020-10-17 22:04:08 +02003744def Test_catch_exception_in_callback()
3745 var lines =<< trim END
3746 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02003747 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02003748 try
3749 var x: string
3750 var y: string
3751 # this error should be caught with CHECKLEN
3752 [x, y] = ['']
3753 catch
3754 g:caught = 'yes'
3755 endtry
3756 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01003757 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02003758 feedkeys("\r", 'xt')
3759 END
3760 CheckScriptSuccess(lines)
3761
3762 unlet g:caught
3763enddef
3764
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003765def Test_no_unknown_error_after_error()
3766 if !has('unix') || !has('job')
3767 throw 'Skipped: not unix of missing +job feature'
3768 endif
3769 var lines =<< trim END
3770 vim9script
3771 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02003772 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003773 eval [][0]
3774 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02003775 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003776 sleep 1m
3777 source += l
3778 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01003779 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01003780 while job_status(myjob) == 'run'
3781 sleep 10m
3782 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01003783 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02003784 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003785 END
3786 writefile(lines, 'Xdef')
3787 assert_fails('so Xdef', ['E684:', 'E1012:'])
3788 delete('Xdef')
3789enddef
3790
Bram Moolenaar4324d872020-12-01 20:12:24 +01003791def InvokeNormal()
3792 exe "norm! :m+1\r"
3793enddef
3794
3795def Test_invoke_normal_in_visual_mode()
3796 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
3797 new
3798 setline(1, ['aaa', 'bbb'])
3799 feedkeys("V\<F3>", 'xt')
3800 assert_equal(['bbb', 'aaa'], getline(1, 2))
3801 xunmap <F3>
3802enddef
3803
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003804def Test_white_space_after_command()
3805 var lines =<< trim END
3806 exit_cb: Func})
3807 END
3808 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003809
3810 lines =<< trim END
3811 e#
3812 END
3813 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003814enddef
3815
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003816def Test_script_var_gone_when_sourced_twice()
3817 var lines =<< trim END
3818 vim9script
3819 if exists('g:guard')
3820 finish
3821 endif
3822 g:guard = 1
3823 var name = 'thename'
3824 def g:GetName(): string
3825 return name
3826 enddef
3827 def g:SetName(arg: string)
3828 name = arg
3829 enddef
3830 END
3831 writefile(lines, 'XscriptTwice.vim')
3832 so XscriptTwice.vim
3833 assert_equal('thename', g:GetName())
3834 g:SetName('newname')
3835 assert_equal('newname', g:GetName())
3836 so XscriptTwice.vim
3837 assert_fails('call g:GetName()', 'E1149:')
3838 assert_fails('call g:SetName("x")', 'E1149:')
3839
3840 delfunc g:GetName
3841 delfunc g:SetName
3842 delete('XscriptTwice.vim')
3843 unlet g:guard
3844enddef
3845
3846def Test_import_gone_when_sourced_twice()
3847 var exportlines =<< trim END
3848 vim9script
3849 if exists('g:guard')
3850 finish
3851 endif
3852 g:guard = 1
3853 export var name = 'someName'
3854 END
3855 writefile(exportlines, 'XexportScript.vim')
3856
3857 var lines =<< trim END
3858 vim9script
3859 import name from './XexportScript.vim'
3860 def g:GetName(): string
3861 return name
3862 enddef
3863 END
3864 writefile(lines, 'XscriptImport.vim')
3865 so XscriptImport.vim
3866 assert_equal('someName', g:GetName())
3867
3868 so XexportScript.vim
3869 assert_fails('call g:GetName()', 'E1149:')
3870
3871 delfunc g:GetName
3872 delete('XexportScript.vim')
3873 delete('XscriptImport.vim')
3874 unlet g:guard
3875enddef
3876
Bram Moolenaar10b94212021-02-19 21:42:57 +01003877def Test_unsupported_commands()
3878 var lines =<< trim END
3879 ka
3880 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003881 CheckDefFailure(lines, 'E476:')
3882 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaar10b94212021-02-19 21:42:57 +01003883
3884 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01003885 :1ka
3886 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003887 CheckDefFailure(lines, 'E476:')
3888 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaarada1d872021-02-20 08:16:51 +01003889
3890 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01003891 t
3892 END
3893 CheckDefFailure(lines, 'E1100:')
3894 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
3895
3896 lines =<< trim END
3897 x
3898 END
3899 CheckDefFailure(lines, 'E1100:')
3900 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
3901
3902 lines =<< trim END
3903 xit
3904 END
3905 CheckDefFailure(lines, 'E1100:')
3906 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
3907enddef
3908
Bram Moolenaarc70fe462021-04-17 17:59:19 +02003909def Test_mapping_line_number()
3910 var lines =<< trim END
3911 vim9script
3912 def g:FuncA()
3913 # Some comment
3914 FuncB(0)
3915 enddef
3916 # Some comment
3917 def FuncB(
3918 # Some comment
3919 n: number
3920 )
3921 exe 'nno '
3922 # Some comment
3923 .. '<F3> a'
3924 .. 'b'
3925 .. 'c'
3926 enddef
3927 END
3928 CheckScriptSuccess(lines)
3929 var res = execute('verbose nmap <F3>')
3930 assert_match('No mapping found', res)
3931
3932 g:FuncA()
3933 res = execute('verbose nmap <F3>')
3934 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
3935
3936 nunmap <F3>
3937 delfunc g:FuncA
3938enddef
3939
Bram Moolenaar585fea72020-04-02 22:33:21 +02003940" Keep this last, it messes up highlighting.
3941def Test_substitute_cmd()
3942 new
3943 setline(1, 'something')
3944 :substitute(some(other(
3945 assert_equal('otherthing', getline(1))
3946 bwipe!
3947
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003948 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003949 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02003950 vim9script
3951 new
3952 setline(1, 'something')
3953 :substitute(some(other(
3954 assert_equal('otherthing', getline(1))
3955 bwipe!
3956 END
3957 writefile(lines, 'Xvim9lines')
3958 source Xvim9lines
3959
3960 delete('Xvim9lines')
3961enddef
3962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker