blob: ee324950fed58fd34ba6ba5a0a98427880dc2bea [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001" Test various aspects of the Vim9 script language.
2
Bram Moolenaar673660a2020-01-26 16:50:05 +01003source check.vim
Bram Moolenaar101f4812020-06-16 23:18:51 +02004source term_util.vim
Bram Moolenaarad39c092020-02-26 18:23:43 +01005source view_util.vim
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006source vim9.vim
Bram Moolenaare3d46852020-08-29 13:39:17 +02007source shared.vim
Bram Moolenaar37294bd2021-03-10 13:40:08 +01008source screendump.vim
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010def Test_range_only()
11 new
12 setline(1, ['blah', 'Blah'])
13 :/Blah/
14 assert_equal(2, getcurpos()[1])
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020015 bwipe!
16
17 # without range commands use current line
18 new
19 setline(1, ['one', 'two', 'three'])
20 :2
21 print
22 assert_equal('two', Screenline(&lines))
23 :3
24 list
25 assert_equal('three$', Screenline(&lines))
Bram Moolenaarb8554302021-02-15 21:30:30 +010026
27 # missing command does not print the line
28 var lines =<< trim END
29 vim9script
30 :1|
31 assert_equal('three$', Screenline(&lines))
32 :|
33 assert_equal('three$', Screenline(&lines))
34 END
35 CheckScriptSuccess(lines)
36
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020037 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +010038
39 # won't generate anything
40 if false
41 :123
42 endif
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020043enddef
44
Bram Moolenaara6e67e42020-05-15 23:36:40 +020045let g:alist = [7]
46let g:astring = 'text'
Bram Moolenaarf0b9f432020-07-17 23:03:17 +020047let g:anumber = 123
Bram Moolenaar6e587dc2020-02-06 13:15:52 +010048
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020049def Test_delfunction()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020050 # Check function is defined in script namespace
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020051 CheckScriptSuccess([
52 'vim9script',
53 'func CheckMe()',
54 ' return 123',
55 'endfunc',
56 'assert_equal(123, s:CheckMe())',
57 ])
58
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020059 # Check function in script namespace cannot be deleted
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020060 CheckScriptFailure([
61 'vim9script',
62 'func DeleteMe1()',
63 'endfunc',
64 'delfunction DeleteMe1',
65 ], 'E1084:')
66 CheckScriptFailure([
67 'vim9script',
68 'func DeleteMe2()',
69 'endfunc',
70 'def DoThat()',
71 ' delfunction DeleteMe2',
72 'enddef',
73 'DoThat()',
74 ], 'E1084:')
75 CheckScriptFailure([
76 'vim9script',
77 'def DeleteMe3()',
78 'enddef',
79 'delfunction DeleteMe3',
80 ], 'E1084:')
81 CheckScriptFailure([
82 'vim9script',
83 'def DeleteMe4()',
84 'enddef',
85 'def DoThat()',
86 ' delfunction DeleteMe4',
87 'enddef',
88 'DoThat()',
89 ], 'E1084:')
Bram Moolenaar925e9fd2020-07-25 15:41:11 +020090
91 # Check that global :def function can be replaced and deleted
Bram Moolenaarcfcd0112020-09-27 15:19:27 +020092 var lines =<< trim END
Bram Moolenaar925e9fd2020-07-25 15:41:11 +020093 vim9script
94 def g:Global(): string
95 return "yes"
96 enddef
97 assert_equal("yes", g:Global())
98 def! g:Global(): string
99 return "no"
100 enddef
101 assert_equal("no", g:Global())
102 delfunc g:Global
103 assert_false(exists('*g:Global'))
104 END
105 CheckScriptSuccess(lines)
106
107 # Check that global function can be replaced by a :def function and deleted
108 lines =<< trim END
109 vim9script
110 func g:Global()
111 return "yes"
112 endfunc
113 assert_equal("yes", g:Global())
114 def! g:Global(): string
115 return "no"
116 enddef
117 assert_equal("no", g:Global())
118 delfunc g:Global
119 assert_false(exists('*g:Global'))
120 END
121 CheckScriptSuccess(lines)
122
123 # Check that global :def function can be replaced by a function and deleted
124 lines =<< trim END
125 vim9script
126 def g:Global(): string
127 return "yes"
128 enddef
129 assert_equal("yes", g:Global())
130 func! g:Global()
131 return "no"
132 endfunc
133 assert_equal("no", g:Global())
134 delfunc g:Global
135 assert_false(exists('*g:Global'))
136 END
137 CheckScriptSuccess(lines)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200138enddef
139
Bram Moolenaar08052222020-09-14 17:04:31 +0200140def Test_wrong_type()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200141 CheckDefFailure(['var name: list<nothing>'], 'E1010:')
142 CheckDefFailure(['var name: list<list<nothing>>'], 'E1010:')
143 CheckDefFailure(['var name: dict<nothing>'], 'E1010:')
144 CheckDefFailure(['var name: dict<dict<nothing>>'], 'E1010:')
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100145
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200146 CheckDefFailure(['var name: dict<number'], 'E1009:')
147 CheckDefFailure(['var name: dict<list<number>'], 'E1009:')
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100148
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200149 CheckDefFailure(['var name: ally'], 'E1010:')
150 CheckDefFailure(['var name: bram'], 'E1010:')
151 CheckDefFailure(['var name: cathy'], 'E1010:')
152 CheckDefFailure(['var name: dom'], 'E1010:')
153 CheckDefFailure(['var name: freddy'], 'E1010:')
154 CheckDefFailure(['var name: john'], 'E1010:')
155 CheckDefFailure(['var name: larry'], 'E1010:')
156 CheckDefFailure(['var name: ned'], 'E1010:')
157 CheckDefFailure(['var name: pam'], 'E1010:')
158 CheckDefFailure(['var name: sam'], 'E1010:')
159 CheckDefFailure(['var name: vim'], 'E1010:')
Bram Moolenaara0a9f432020-04-28 21:29:34 +0200160
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200161 CheckDefFailure(['var Ref: number', 'Ref()'], 'E1085:')
162 CheckDefFailure(['var Ref: string', 'var res = Ref()'], 'E1085:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200163enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaar10c65862020-10-08 21:16:42 +0200165def Test_script_wrong_type()
166 var lines =<< trim END
167 vim9script
168 var s:dict: dict<string>
169 s:dict['a'] = ['x']
170 END
171 CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got list<string>', 3)
172enddef
173
Bram Moolenaar08052222020-09-14 17:04:31 +0200174def Test_const()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200175 CheckDefFailure(['final name = 234', 'name = 99'], 'E1018:')
176 CheckDefFailure(['final one = 234', 'var one = 99'], 'E1017:')
177 CheckDefFailure(['final list = [1, 2]', 'var list = [3, 4]'], 'E1017:')
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200178 CheckDefFailure(['final two'], 'E1125:')
179 CheckDefFailure(['final &option'], 'E996:')
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200180
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200181 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200182 final list = [1, 2, 3]
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200183 list[0] = 4
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200184 list->assert_equal([4, 2, 3])
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200185 const other = [5, 6, 7]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200186 other->assert_equal([5, 6, 7])
Bram Moolenaar71abe482020-09-14 22:28:30 +0200187
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200188 var varlist = [7, 8]
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200189 const constlist = [1, varlist, 3]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200190 varlist[0] = 77
191 # TODO: does not work yet
192 # constlist[1][1] = 88
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200193 var cl = constlist[1]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200194 cl[1] = 88
195 constlist->assert_equal([1, [77, 88], 3])
196
Bram Moolenaare0de1712020-12-02 17:36:54 +0100197 var vardict = {five: 5, six: 6}
198 const constdict = {one: 1, two: vardict, three: 3}
Bram Moolenaar71abe482020-09-14 22:28:30 +0200199 vardict['five'] = 55
200 # TODO: does not work yet
201 # constdict['two']['six'] = 66
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200202 var cd = constdict['two']
Bram Moolenaar71abe482020-09-14 22:28:30 +0200203 cd['six'] = 66
Bram Moolenaare0de1712020-12-02 17:36:54 +0100204 constdict->assert_equal({one: 1, two: {five: 55, six: 66}, three: 3})
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200205 END
206 CheckDefAndScriptSuccess(lines)
Bram Moolenaar08052222020-09-14 17:04:31 +0200207enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200209def Test_const_bang()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200210 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200211 const var = 234
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200212 var = 99
213 END
214 CheckDefExecFailure(lines, 'E1018:', 2)
215 CheckScriptFailure(['vim9script'] + lines, 'E46:', 3)
216
217 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200218 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200219 ll[0] = 99
220 END
221 CheckDefExecFailure(lines, 'E1119:', 2)
222 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
223
224 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200225 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200226 ll[3] = 99
227 END
228 CheckDefExecFailure(lines, 'E1118:', 2)
229 CheckScriptFailure(['vim9script'] + lines, 'E684:', 3)
230
231 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100232 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200233 dd["one"] = 99
234 END
235 CheckDefExecFailure(lines, 'E1121:', 2)
236 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
237
238 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100239 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200240 dd["three"] = 99
241 END
242 CheckDefExecFailure(lines, 'E1120:')
243 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
244enddef
245
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200246def Test_range_no_colon()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200247 CheckDefFailure(['%s/a/b/'], 'E1050:')
248 CheckDefFailure(['+ s/a/b/'], 'E1050:')
249 CheckDefFailure(['- s/a/b/'], 'E1050:')
250 CheckDefFailure(['. s/a/b/'], 'E1050:')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200251enddef
252
253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254def Test_block()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200255 var outer = 1
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 {
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200257 var inner = 2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258 assert_equal(1, outer)
259 assert_equal(2, inner)
260 }
261 assert_equal(1, outer)
Bram Moolenaar3f1e9f02021-02-27 22:36:43 +0100262
263 {|echo 'yes'|}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264enddef
265
Bram Moolenaar08052222020-09-14 17:04:31 +0200266def Test_block_failure()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200267 CheckDefFailure(['{', 'var inner = 1', '}', 'echo inner'], 'E1001:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200268 CheckDefFailure(['}'], 'E1025:')
269 CheckDefFailure(['{', 'echo 1'], 'E1026:')
270enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200272def Test_block_local_vars()
273 var lines =<< trim END
274 vim9script
Bram Moolenaared234f22020-10-15 20:42:20 +0200275 v:testing = 1
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200276 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200277 var text = ['hello']
278 def SayHello(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200279 return text
280 enddef
281 def SetText(v: string)
Bram Moolenaared234f22020-10-15 20:42:20 +0200282 text = [v]
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200283 enddef
284 endif
285
286 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200287 var text = ['again']
288 def SayAgain(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200289 return text
290 enddef
291 endif
Bram Moolenaared234f22020-10-15 20:42:20 +0200292
293 # test that the "text" variables are not cleaned up
294 test_garbagecollect_now()
295
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200296 defcompile
297
Bram Moolenaared234f22020-10-15 20:42:20 +0200298 assert_equal(['hello'], SayHello())
299 assert_equal(['again'], SayAgain())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200300
301 SetText('foobar')
Bram Moolenaared234f22020-10-15 20:42:20 +0200302 assert_equal(['foobar'], SayHello())
303
304 call writefile(['ok'], 'Xdidit')
305 qall!
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200306 END
Bram Moolenaared234f22020-10-15 20:42:20 +0200307
308 # need to execute this with a separate Vim instance to avoid the current
309 # context gets garbage collected.
310 writefile(lines, 'Xscript')
311 RunVim([], [], '-S Xscript')
312 assert_equal(['ok'], readfile('Xdidit'))
313
314 delete('Xscript')
315 delete('Xdidit')
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200316enddef
317
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200318def Test_block_local_vars_with_func()
319 var lines =<< trim END
320 vim9script
321 if true
322 var foo = 'foo'
323 if true
324 var bar = 'bar'
325 def Func(): list<string>
326 return [foo, bar]
327 enddef
328 endif
329 endif
330 # function is compiled here, after blocks have finished, can still access
331 # "foo" and "bar"
332 assert_equal(['foo', 'bar'], Func())
333 END
334 CheckScriptSuccess(lines)
335enddef
336
Bram Moolenaard032f342020-07-18 18:13:02 +0200337func g:NoSuchFunc()
338 echo 'none'
339endfunc
340
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100341def Test_try_catch_throw()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200342 var l = []
Bram Moolenaar7a092242020-04-16 22:10:49 +0200343 try # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344 add(l, '1')
345 throw 'wrong'
346 add(l, '2')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200347 catch # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100348 add(l, v:exception)
Bram Moolenaar7a092242020-04-16 22:10:49 +0200349 finally # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100350 add(l, '3')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200351 endtry # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100352 assert_equal(['1', 'wrong', '3'], l)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200353
Bram Moolenaare8593122020-07-18 15:17:02 +0200354 l = []
355 try
356 try
357 add(l, '1')
358 throw 'wrong'
359 add(l, '2')
360 catch /right/
361 add(l, v:exception)
362 endtry
363 catch /wrong/
364 add(l, 'caught')
Bram Moolenaar373863e2020-09-26 17:20:53 +0200365 fina
Bram Moolenaare8593122020-07-18 15:17:02 +0200366 add(l, 'finally')
367 endtry
368 assert_equal(['1', 'caught', 'finally'], l)
369
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200370 var n: number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200371 try
372 n = l[3]
373 catch /E684:/
374 n = 99
375 endtry
376 assert_equal(99, n)
377
Bram Moolenaar69f70502021-01-01 16:10:46 +0100378 var done = 'no'
379 if 0
380 try | catch | endtry
381 else
382 done = 'yes'
383 endif
384 assert_equal('yes', done)
385
386 done = 'no'
387 if 1
388 done = 'yes'
389 else
390 try | catch | endtry
391 done = 'never'
392 endif
393 assert_equal('yes', done)
394
395 if 1
396 else
397 try | catch /pat/ | endtry
398 try | catch /pat/
399 endtry
400 try
401 catch /pat/ | endtry
402 try
403 catch /pat/
404 endtry
405 endif
406
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200407 try
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200408 # string slice returns a string, not a number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200409 n = g:astring[3]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200410 catch /E1012:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200411 n = 77
412 endtry
413 assert_equal(77, n)
414
415 try
416 n = l[g:astring]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200417 catch /E1012:/
Bram Moolenaar56acb092020-08-16 14:48:19 +0200418 n = 88
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200419 endtry
Bram Moolenaar56acb092020-08-16 14:48:19 +0200420 assert_equal(88, n)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200421
422 try
423 n = s:does_not_exist
424 catch /E121:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200425 n = 111
426 endtry
427 assert_equal(111, n)
428
429 try
430 n = g:does_not_exist
431 catch /E121:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200432 n = 121
433 endtry
434 assert_equal(121, n)
435
Bram Moolenaare0de1712020-12-02 17:36:54 +0100436 var d = {one: 1}
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200437 try
438 n = d[g:astring]
439 catch /E716:/
440 n = 222
441 endtry
442 assert_equal(222, n)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200443
444 try
445 n = -g:astring
446 catch /E39:/
447 n = 233
448 endtry
449 assert_equal(233, n)
450
451 try
452 n = +g:astring
453 catch /E1030:/
454 n = 244
455 endtry
456 assert_equal(244, n)
457
458 try
459 n = +g:alist
460 catch /E745:/
461 n = 255
462 endtry
463 assert_equal(255, n)
464
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200465 var nd: dict<any>
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200466 try
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100467 nd = {[g:alist]: 1}
468 catch /E1105:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200469 n = 266
470 endtry
471 assert_equal(266, n)
472
473 try
474 [n] = [1, 2, 3]
475 catch /E1093:/
476 n = 277
477 endtry
478 assert_equal(277, n)
479
Bram Moolenaare8593122020-07-18 15:17:02 +0200480 try
481 &ts = g:astring
Bram Moolenaar5e654232020-09-16 15:22:00 +0200482 catch /E1012:/
Bram Moolenaare8593122020-07-18 15:17:02 +0200483 n = 288
484 endtry
485 assert_equal(288, n)
486
487 try
488 &backspace = 'asdf'
489 catch /E474:/
490 n = 299
491 endtry
492 assert_equal(299, n)
493
494 l = [1]
495 try
496 l[3] = 3
497 catch /E684:/
498 n = 300
499 endtry
500 assert_equal(300, n)
501
502 try
Bram Moolenaare8593122020-07-18 15:17:02 +0200503 unlet g:does_not_exist
504 catch /E108:/
505 n = 322
506 endtry
507 assert_equal(322, n)
508
509 try
Bram Moolenaar2bede172020-11-19 18:53:18 +0100510 d = {text: 1, [g:astring]: 2}
Bram Moolenaare8593122020-07-18 15:17:02 +0200511 catch /E721:/
512 n = 333
513 endtry
514 assert_equal(333, n)
515
516 try
517 l = DeletedFunc()
518 catch /E933:/
519 n = 344
520 endtry
521 assert_equal(344, n)
Bram Moolenaard032f342020-07-18 18:13:02 +0200522
523 try
524 echo len(v:true)
525 catch /E701:/
526 n = 355
527 endtry
528 assert_equal(355, n)
529
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200530 var P = function('g:NoSuchFunc')
Bram Moolenaard032f342020-07-18 18:13:02 +0200531 delfunc g:NoSuchFunc
532 try
533 echo P()
534 catch /E117:/
535 n = 366
536 endtry
537 assert_equal(366, n)
538
539 try
540 echo g:NoSuchFunc()
541 catch /E117:/
542 n = 377
543 endtry
544 assert_equal(377, n)
545
546 try
547 echo g:alist + 4
548 catch /E745:/
549 n = 388
550 endtry
551 assert_equal(388, n)
552
553 try
554 echo 4 + g:alist
555 catch /E745:/
556 n = 399
557 endtry
558 assert_equal(399, n)
559
560 try
561 echo g:alist.member
562 catch /E715:/
563 n = 400
564 endtry
565 assert_equal(400, n)
566
567 try
568 echo d.member
569 catch /E716:/
570 n = 411
571 endtry
572 assert_equal(411, n)
Bram Moolenaard9d77892021-02-12 21:32:47 +0100573
574 var counter = 0
575 for i in range(4)
576 try
577 eval [][0]
578 catch
579 endtry
580 counter += 1
581 endfor
582 assert_equal(4, counter)
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100583
584 # return in finally after empty catch
585 def ReturnInFinally(): number
586 try
587 finally
588 return 4
589 endtry
590 return 2
591 enddef
592 assert_equal(4, ReturnInFinally())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593enddef
594
Bram Moolenaar2e34c342021-03-14 12:13:33 +0100595" :while at the very start of a function that :continue jumps to
596def TryContinueFunc()
597 while g:Count < 2
598 g:sequence ..= 't'
599 try
600 echoerr 'Test'
601 catch
602 g:Count += 1
603 g:sequence ..= 'c'
604 continue
605 endtry
606 g:sequence ..= 'e'
607 g:Count += 1
608 endwhile
609enddef
610
611def Test_continue_in_try_in_while()
612 g:Count = 0
613 g:sequence = ''
614 TryContinueFunc()
615 assert_equal('tctc', g:sequence)
616 unlet g:Count
617 unlet g:sequence
618enddef
619
Bram Moolenaar9cb577a2021-02-22 22:45:10 +0100620def Test_nocatch_return_in_try()
621 # return in try block returns normally
622 def ReturnInTry(): string
623 try
624 return '"some message"'
625 catch
626 endtry
627 return 'not reached'
628 enddef
629 exe 'echoerr ' .. ReturnInTry()
630enddef
631
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100632def Test_cnext_works_in_catch()
633 var lines =<< trim END
634 vim9script
635 au BufEnter * eval 0
636 writefile(['text'], 'Xfile1')
637 writefile(['text'], 'Xfile2')
638 var items = [
639 {lnum: 1, filename: 'Xfile1', valid: true},
640 {lnum: 1, filename: 'Xfile2', valid: true}
641 ]
642 setqflist([], ' ', {items: items})
643 cwindow
644
645 def CnextOrCfirst()
646 # if cnext fails, cfirst is used
647 try
648 cnext
649 catch
650 cfirst
651 endtry
652 enddef
653
654 CnextOrCfirst()
655 CnextOrCfirst()
656 writefile([getqflist({idx: 0}).idx], 'Xresult')
657 qall
658 END
659 writefile(lines, 'XCatchCnext')
660 RunVim([], [], '--clean -S XCatchCnext')
661 assert_equal(['1'], readfile('Xresult'))
662
663 delete('Xfile1')
664 delete('Xfile2')
665 delete('XCatchCnext')
666 delete('Xresult')
667enddef
668
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100669def Test_throw_skipped()
670 if 0
671 throw dontgethere
672 endif
673enddef
674
Bram Moolenaar8f81b222021-01-14 21:47:06 +0100675def Test_nocatch_throw_silenced()
676 var lines =<< trim END
677 vim9script
678 def Func()
679 throw 'error'
680 enddef
681 silent! Func()
682 END
683 writefile(lines, 'XthrowSilenced')
684 source XthrowSilenced
685 delete('XthrowSilenced')
686enddef
687
Bram Moolenaare8593122020-07-18 15:17:02 +0200688def DeletedFunc(): list<any>
689 return ['delete me']
690enddef
691defcompile
692delfunc DeletedFunc
693
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100694def ThrowFromDef()
Bram Moolenaara72cfb82020-04-23 17:07:30 +0200695 throw "getout" # comment
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100696enddef
697
698func CatchInFunc()
699 try
700 call ThrowFromDef()
701 catch
702 let g:thrown_func = v:exception
703 endtry
704endfunc
705
706def CatchInDef()
707 try
708 ThrowFromDef()
709 catch
710 g:thrown_def = v:exception
711 endtry
712enddef
713
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100714def ReturnFinally(): string
715 try
716 return 'intry'
Bram Moolenaar373863e2020-09-26 17:20:53 +0200717 finall
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100718 g:in_finally = 'finally'
719 endtry
720 return 'end'
721enddef
722
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100723def Test_try_catch_nested()
724 CatchInFunc()
725 assert_equal('getout', g:thrown_func)
726
727 CatchInDef()
728 assert_equal('getout', g:thrown_def)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100729
730 assert_equal('intry', ReturnFinally())
731 assert_equal('finally', g:in_finally)
732enddef
733
Bram Moolenaar9939f572020-09-16 22:29:52 +0200734def TryOne(): number
735 try
736 return 0
737 catch
738 endtry
739 return 0
740enddef
741
742def TryTwo(n: number): string
743 try
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200744 var x = {}
Bram Moolenaar9939f572020-09-16 22:29:52 +0200745 catch
746 endtry
747 return 'text'
748enddef
749
750def Test_try_catch_twice()
751 assert_equal('text', TryOne()->TryTwo())
752enddef
753
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100754def Test_try_catch_match()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200755 var seq = 'a'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100756 try
757 throw 'something'
758 catch /nothing/
759 seq ..= 'x'
760 catch /some/
761 seq ..= 'b'
762 catch /asdf/
763 seq ..= 'x'
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200764 catch ?a\?sdf?
765 seq ..= 'y'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100766 finally
767 seq ..= 'c'
768 endtry
769 assert_equal('abc', seq)
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100770enddef
771
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200772def Test_try_catch_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200773 CheckDefFailure(['catch'], 'E603:')
774 CheckDefFailure(['try', 'echo 0', 'catch', 'catch'], 'E1033:')
775 CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
776 CheckDefFailure(['finally'], 'E606:')
777 CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:')
778 CheckDefFailure(['endtry'], 'E602:')
779 CheckDefFailure(['while 1', 'endtry'], 'E170:')
780 CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:')
Bram Moolenaar13106602020-10-04 16:06:05 +0200781 CheckDefFailure(['if 1', 'endtry'], 'E171:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200782 CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:')
Bram Moolenaar585fea72020-04-02 22:33:21 +0200783
Bram Moolenaare4984292020-12-13 14:19:25 +0100784 CheckDefFailure(['throw'], 'E1143:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200785 CheckDefFailure(['throw xxx'], 'E1001:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200786enddef
787
Bram Moolenaar7c5b3c0362021-02-14 22:40:57 +0100788def Try_catch_skipped()
789 var l = []
790 try
791 finally
792 endtry
793
794 if 1
795 else
796 try
797 endtry
798 endif
799enddef
800
801" The skipped try/endtry was updating the wrong instruction.
802def Test_try_catch_skipped()
803 var instr = execute('disassemble Try_catch_skipped')
804 assert_match("NEWLIST size 0\n", instr)
805enddef
806
807
808
Bram Moolenaar006ad482020-06-30 20:55:15 +0200809def Test_throw_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200810 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200811 var lines =<< trim END
Bram Moolenaar006ad482020-06-30 20:55:15 +0200812 vim9script
813 try
814 throw 'one'
815 .. 'two'
816 catch
817 assert_equal('onetwo', v:exception)
818 endtry
819 END
820 CheckScriptSuccess(lines)
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200821
822 lines =<< trim END
823 vim9script
Bram Moolenaar352134b2020-10-17 22:04:08 +0200824 @r = ''
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200825 def Func()
826 throw @r
827 enddef
828 var result = ''
829 try
830 Func()
831 catch /E1129:/
832 result = 'caught'
833 endtry
834 assert_equal('caught', result)
835 END
836 CheckScriptSuccess(lines)
Bram Moolenaar006ad482020-06-30 20:55:15 +0200837enddef
838
Bram Moolenaared677f52020-08-12 16:38:10 +0200839def Test_error_in_nested_function()
Bram Moolenaar03dfde22021-02-14 13:17:22 +0100840 # an error in a nested :function aborts executing in the calling :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200841 var lines =<< trim END
Bram Moolenaared677f52020-08-12 16:38:10 +0200842 vim9script
843 def Func()
844 Error()
845 g:test_var = 1
846 enddef
847 func Error() abort
848 eval [][0]
849 endfunc
850 Func()
851 END
852 g:test_var = 0
853 CheckScriptFailure(lines, 'E684:')
854 assert_equal(0, g:test_var)
855enddef
856
Bram Moolenaar227c58a2021-04-28 20:40:44 +0200857def Test_abort_after_error()
858 var lines =<< trim END
859 vim9script
860 while true
861 echo notfound
862 endwhile
863 g:gotthere = true
864 END
865 g:gotthere = false
866 CheckScriptFailure(lines, 'E121:')
867 assert_false(g:gotthere)
868 unlet g:gotthere
869enddef
870
Bram Moolenaar37c83712020-06-30 21:18:36 +0200871def Test_cexpr_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200872 # only checks line continuation
Bram Moolenaar37c83712020-06-30 21:18:36 +0200873 set errorformat=File\ %f\ line\ %l
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200874 var lines =<< trim END
Bram Moolenaar37c83712020-06-30 21:18:36 +0200875 vim9script
876 cexpr 'File'
877 .. ' someFile' ..
878 ' line 19'
879 assert_equal(19, getqflist()[0].lnum)
880 END
881 CheckScriptSuccess(lines)
882 set errorformat&
883enddef
884
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200885def Test_statusline_syntax()
886 # legacy syntax is used for 'statusline'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200887 var lines =<< trim END
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200888 vim9script
889 func g:Status()
890 return '%{"x" is# "x"}'
891 endfunc
892 set laststatus=2 statusline=%!Status()
893 redrawstatus
894 set laststatus statusline=
895 END
896 CheckScriptSuccess(lines)
897enddef
898
Bram Moolenaarb2097502020-07-19 17:17:02 +0200899def Test_list_vimscript()
900 # checks line continuation and comments
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200901 var lines =<< trim END
Bram Moolenaarb2097502020-07-19 17:17:02 +0200902 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200903 var mylist = [
Bram Moolenaarb2097502020-07-19 17:17:02 +0200904 'one',
905 # comment
906 'two', # empty line follows
907
908 'three',
909 ]
910 assert_equal(['one', 'two', 'three'], mylist)
911 END
912 CheckScriptSuccess(lines)
Bram Moolenaar66250c92020-08-20 15:02:42 +0200913
914 # check all lines from heredoc are kept
915 lines =<< trim END
916 # comment 1
917 two
918 # comment 3
919
920 five
921 # comment 6
922 END
923 assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines)
Bram Moolenaar4bce26b2021-01-22 22:06:56 +0100924
925 lines =<< trim END
926 [{
927 a: 0}]->string()->assert_equal("[{'a': 0}]")
928 END
929 CheckDefAndScriptSuccess(lines)
Bram Moolenaarb2097502020-07-19 17:17:02 +0200930enddef
931
Bram Moolenaar2a1381c2020-05-05 23:32:58 +0200932if has('channel')
933 let someJob = test_null_job()
Bram Moolenaar40ee4662020-05-05 22:08:26 +0200934
Bram Moolenaar2a1381c2020-05-05 23:32:58 +0200935 def FuncWithError()
936 echomsg g:someJob
937 enddef
Bram Moolenaar40ee4662020-05-05 22:08:26 +0200938
Bram Moolenaar2a1381c2020-05-05 23:32:58 +0200939 func Test_convert_emsg_to_exception()
940 try
941 call FuncWithError()
942 catch
943 call assert_match('Vim:E908:', v:exception)
944 endtry
945 endfunc
946endif
Bram Moolenaar40ee4662020-05-05 22:08:26 +0200947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948let s:export_script_lines =<< trim END
949 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200950 var name: string = 'bob'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 def Concat(arg: string): string
952 return name .. arg
953 enddef
Bram Moolenaar227a69d2020-05-15 18:17:28 +0200954 g:result = Concat('bie')
955 g:localname = name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956
957 export const CONST = 1234
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200958 export var exported = 9876
959 export var exp_name = 'John'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100960 export def Exported(): string
961 return 'Exported'
962 enddef
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +0100963 export final theList = [1]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964END
965
Bram Moolenaarb3ca9822020-08-09 14:43:58 +0200966def Undo_export_script_lines()
967 unlet g:result
968 unlet g:localname
969enddef
970
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100971def Test_vim9_import_export()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200972 var import_script_lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973 vim9script
974 import {exported, Exported} from './Xexport.vim'
975 g:imported = exported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +0100976 exported += 3
977 g:imported_added = exported
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100978 g:imported_func = Exported()
Bram Moolenaar6e587dc2020-02-06 13:15:52 +0100979
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200980 def GetExported(): string
Bram Moolenaare0de1712020-12-02 17:36:54 +0100981 var local_dict = {ref: Exported}
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200982 return local_dict.ref()
983 enddef
984 g:funcref_result = GetExported()
985
Bram Moolenaar6e587dc2020-02-06 13:15:52 +0100986 import {exp_name} from './Xexport.vim'
987 g:imported_name = exp_name
988 exp_name ..= ' Doe'
989 g:imported_name_appended = exp_name
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100990 g:imported_later = exported
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +0100991
992 import theList from './Xexport.vim'
993 theList->add(2)
994 assert_equal([1, 2], theList)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995 END
996
997 writefile(import_script_lines, 'Ximport.vim')
998 writefile(s:export_script_lines, 'Xexport.vim')
999
1000 source Ximport.vim
1001
1002 assert_equal('bobbie', g:result)
1003 assert_equal('bob', g:localname)
1004 assert_equal(9876, g:imported)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001005 assert_equal(9879, g:imported_added)
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001006 assert_equal(9879, g:imported_later)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 assert_equal('Exported', g:imported_func)
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001008 assert_equal('Exported', g:funcref_result)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001009 assert_equal('John', g:imported_name)
1010 assert_equal('John Doe', g:imported_name_appended)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 assert_false(exists('g:name'))
1012
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001013 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014 unlet g:imported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001015 unlet g:imported_added
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001016 unlet g:imported_later
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001017 unlet g:imported_func
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001018 unlet g:imported_name g:imported_name_appended
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 delete('Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001020
Bram Moolenaar1c991142020-07-04 13:15:31 +02001021 # similar, with line breaks
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001022 var import_line_break_script_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001023 vim9script
1024 import {
1025 exported,
1026 Exported,
1027 }
1028 from
1029 './Xexport.vim'
1030 g:imported = exported
1031 exported += 5
1032 g:imported_added = exported
1033 g:imported_func = Exported()
1034 END
1035 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
1036 source Ximport_lbr.vim
1037
1038 assert_equal(9876, g:imported)
1039 assert_equal(9881, g:imported_added)
1040 assert_equal('Exported', g:imported_func)
1041
1042 # exported script not sourced again
1043 assert_false(exists('g:result'))
1044 unlet g:imported
1045 unlet g:imported_added
1046 unlet g:imported_func
1047 delete('Ximport_lbr.vim')
1048
1049 # import inside :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001050 var import_in_def_lines =<< trim END
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001051 vim9script
1052 def ImportInDef()
1053 import exported from './Xexport.vim'
1054 g:imported = exported
1055 exported += 7
1056 g:imported_added = exported
1057 enddef
1058 ImportInDef()
1059 END
1060 writefile(import_in_def_lines, 'Ximport2.vim')
1061 source Ximport2.vim
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001062 # TODO: this should be 9879
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001063 assert_equal(9876, g:imported)
1064 assert_equal(9883, g:imported_added)
1065 unlet g:imported
1066 unlet g:imported_added
1067 delete('Ximport2.vim')
1068
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001069 var import_star_as_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001070 vim9script
1071 import * as Export from './Xexport.vim'
1072 def UseExport()
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001073 g:imported_def = Export.exported
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001074 enddef
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001075 g:imported_script = Export.exported
1076 assert_equal(1, exists('Export.exported'))
1077 assert_equal(0, exists('Export.notexported'))
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001078 UseExport()
1079 END
1080 writefile(import_star_as_lines, 'Ximport.vim')
1081 source Ximport.vim
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001082 assert_equal(9883, g:imported_def)
1083 assert_equal(9883, g:imported_script)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001084
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001085 var import_star_as_lines_no_dot =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001086 vim9script
1087 import * as Export from './Xexport.vim'
1088 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001089 var dummy = 1
1090 var imported = Export + dummy
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001091 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001092 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001093 END
1094 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001095 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001096
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001097 var import_star_as_lines_dot_space =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001098 vim9script
1099 import * as Export from './Xexport.vim'
1100 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001101 var imported = Export . exported
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001102 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001103 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001104 END
1105 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001106 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001107
Bram Moolenaara6294952020-12-27 13:39:50 +01001108 var import_star_as_duplicated =<< trim END
1109 vim9script
1110 import * as Export from './Xexport.vim'
1111 var some = 'other'
1112 import * as Export from './Xexport.vim'
1113 defcompile
1114 END
1115 writefile(import_star_as_duplicated, 'Ximport.vim')
1116 assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim')
1117
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001118 var import_star_as_lines_script_no_dot =<< trim END
1119 vim9script
1120 import * as Export from './Xexport.vim'
1121 g:imported_script = Export exported
1122 END
1123 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
1124 assert_fails('source Ximport.vim', 'E1029:')
1125
1126 var import_star_as_lines_script_space_after_dot =<< trim END
1127 vim9script
1128 import * as Export from './Xexport.vim'
1129 g:imported_script = Export. exported
1130 END
1131 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
1132 assert_fails('source Ximport.vim', 'E1074:')
1133
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001134 var import_star_as_lines_missing_name =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001135 vim9script
1136 import * as Export from './Xexport.vim'
1137 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001138 var imported = Export.
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001139 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001140 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001141 END
1142 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001143 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001144
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001145 var import_star_as_lbr_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001146 vim9script
1147 import *
1148 as Export
1149 from
1150 './Xexport.vim'
1151 def UseExport()
1152 g:imported = Export.exported
1153 enddef
1154 UseExport()
1155 END
1156 writefile(import_star_as_lbr_lines, 'Ximport.vim')
1157 source Ximport.vim
1158 assert_equal(9883, g:imported)
1159
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001160 var import_star_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001161 vim9script
1162 import * from './Xexport.vim'
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001163 END
1164 writefile(import_star_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001165 assert_fails('source Ximport.vim', 'E1045:', '', 2, 'Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001166
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001167 # try to import something that exists but is not exported
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001168 var import_not_exported_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001169 vim9script
1170 import name from './Xexport.vim'
1171 END
1172 writefile(import_not_exported_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001173 assert_fails('source Ximport.vim', 'E1049:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001174
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001175 # try to import something that is already defined
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001176 var import_already_defined =<< trim END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001177 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001178 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001179 import exported from './Xexport.vim'
1180 END
1181 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001182 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001183
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001184 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001185 import_already_defined =<< trim END
1186 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001187 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001188 import * as exported from './Xexport.vim'
1189 END
1190 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001191 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001192
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001193 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001194 import_already_defined =<< trim END
1195 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001196 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001197 import {exported} from './Xexport.vim'
1198 END
1199 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001200 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001201
Bram Moolenaar918a4242020-12-06 14:37:08 +01001202 # try changing an imported const
1203 var import_assign_to_const =<< trim END
1204 vim9script
1205 import CONST from './Xexport.vim'
1206 def Assign()
1207 CONST = 987
1208 enddef
1209 defcompile
1210 END
1211 writefile(import_assign_to_const, 'Ximport.vim')
1212 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1213
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001214 # try changing an imported final
1215 var import_assign_to_final =<< trim END
1216 vim9script
1217 import theList from './Xexport.vim'
1218 def Assign()
1219 theList = [2]
1220 enddef
1221 defcompile
1222 END
1223 writefile(import_assign_to_final, 'Ximport.vim')
1224 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1225
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001226 # import a very long name, requires making a copy
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001227 var import_long_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001228 vim9script
1229 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
1230 END
1231 writefile(import_long_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001232 assert_fails('source Ximport.vim', 'E1048:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001233
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001234 var import_no_from_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001235 vim9script
1236 import name './Xexport.vim'
1237 END
1238 writefile(import_no_from_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001239 assert_fails('source Ximport.vim', 'E1070:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001240
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001241 var import_invalid_string_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001242 vim9script
1243 import name from Xexport.vim
1244 END
1245 writefile(import_invalid_string_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001246 assert_fails('source Ximport.vim', 'E1071:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001247
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001248 var import_wrong_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001249 vim9script
1250 import name from './XnoExport.vim'
1251 END
1252 writefile(import_wrong_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001253 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001254
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001255 var import_missing_comma_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001256 vim9script
1257 import {exported name} from './Xexport.vim'
1258 END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001259 writefile(import_missing_comma_lines, 'Ximport3.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001260 assert_fails('source Ximport3.vim', 'E1046:', '', 2, 'Ximport3.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001261
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001262 delete('Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001263 delete('Ximport3.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264 delete('Xexport.vim')
1265
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001266 # Check that in a Vim9 script 'cpo' is set to the Vim default.
Bram Moolenaar3e191692021-03-17 17:46:00 +01001267 # Flags added or removed are also applied to the restored value.
1268 set cpo=abcd
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001269 var lines =<< trim END
Bram Moolenaar750802b2020-02-23 18:08:33 +01001270 vim9script
1271 g:cpo_in_vim9script = &cpo
Bram Moolenaar3e191692021-03-17 17:46:00 +01001272 set cpo+=f
1273 set cpo-=c
1274 g:cpo_after_vim9script = &cpo
Bram Moolenaar750802b2020-02-23 18:08:33 +01001275 END
1276 writefile(lines, 'Xvim9_script')
1277 source Xvim9_script
Bram Moolenaar3e191692021-03-17 17:46:00 +01001278 assert_equal('fabd', &cpo)
Bram Moolenaar750802b2020-02-23 18:08:33 +01001279 set cpo&vim
1280 assert_equal(&cpo, g:cpo_in_vim9script)
Bram Moolenaar3e191692021-03-17 17:46:00 +01001281 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
1282 assert_equal(newcpo, g:cpo_after_vim9script)
1283
Bram Moolenaar750802b2020-02-23 18:08:33 +01001284 delete('Xvim9_script')
1285enddef
1286
Bram Moolenaar0a842842021-02-27 22:41:19 +01001287def Test_import_as()
1288 var export_lines =<< trim END
1289 vim9script
1290 export var one = 1
1291 export var yes = 'yes'
1292 END
1293 writefile(export_lines, 'XexportAs')
1294
1295 var import_lines =<< trim END
1296 vim9script
Bram Moolenaar6c7cc342021-04-17 16:38:50 +02001297 var one = 'notused'
1298 var yes = 777
Bram Moolenaar0a842842021-02-27 22:41:19 +01001299 import one as thatOne from './XexportAs'
1300 assert_equal(1, thatOne)
1301 import yes as yesYes from './XexportAs'
1302 assert_equal('yes', yesYes)
1303 END
1304 CheckScriptSuccess(import_lines)
1305
1306 import_lines =<< trim END
1307 vim9script
1308 import {one as thatOne, yes as yesYes} from './XexportAs'
1309 assert_equal(1, thatOne)
1310 assert_equal('yes', yesYes)
1311 assert_fails('echo one', 'E121:')
1312 assert_fails('echo yes', 'E121:')
1313 END
1314 CheckScriptSuccess(import_lines)
1315
1316 delete('XexportAs')
1317enddef
1318
Bram Moolenaar803af682020-08-05 16:20:03 +02001319func g:Trigger()
1320 source Ximport.vim
1321 return "echo 'yes'\<CR>"
1322endfunc
1323
1324def Test_import_export_expr_map()
1325 # check that :import and :export work when buffer is locked
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001326 var export_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001327 vim9script
1328 export def That(): string
1329 return 'yes'
1330 enddef
1331 END
1332 writefile(export_lines, 'Xexport_that.vim')
1333
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001334 var import_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001335 vim9script
1336 import That from './Xexport_that.vim'
1337 assert_equal('yes', That())
1338 END
1339 writefile(import_lines, 'Ximport.vim')
1340
1341 nnoremap <expr> trigger g:Trigger()
1342 feedkeys('trigger', "xt")
1343
Bram Moolenaar730b2482020-08-09 13:02:10 +02001344 delete('Xexport_that.vim')
Bram Moolenaar803af682020-08-05 16:20:03 +02001345 delete('Ximport.vim')
1346 nunmap trigger
1347enddef
1348
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001349def Test_import_in_filetype()
1350 # check that :import works when the buffer is locked
1351 mkdir('ftplugin', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001352 var export_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001353 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001354 export var That = 'yes'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001355 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001356 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001357
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001358 var import_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001359 vim9script
Bram Moolenaar730b2482020-08-09 13:02:10 +02001360 import That from './Xexport_ft.vim'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001361 assert_equal('yes', That)
1362 g:did_load_mytpe = 1
1363 END
1364 writefile(import_lines, 'ftplugin/qf.vim')
1365
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001366 var save_rtp = &rtp
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001367 &rtp = getcwd() .. ',' .. &rtp
1368
1369 filetype plugin on
1370 copen
1371 assert_equal(1, g:did_load_mytpe)
1372
1373 quit!
Bram Moolenaar730b2482020-08-09 13:02:10 +02001374 delete('Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001375 delete('ftplugin', 'rf')
1376 &rtp = save_rtp
1377enddef
1378
Bram Moolenaarefa94442020-08-08 22:16:00 +02001379def Test_use_import_in_mapping()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001380 var lines =<< trim END
Bram Moolenaarefa94442020-08-08 22:16:00 +02001381 vim9script
1382 export def Funcx()
1383 g:result = 42
1384 enddef
1385 END
1386 writefile(lines, 'XsomeExport.vim')
1387 lines =<< trim END
1388 vim9script
1389 import Funcx from './XsomeExport.vim'
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001390 nnoremap <F3> :call <sid>Funcx()<cr>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001391 END
1392 writefile(lines, 'Xmapscript.vim')
1393
1394 source Xmapscript.vim
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001395 feedkeys("\<F3>", "xt")
Bram Moolenaarefa94442020-08-08 22:16:00 +02001396 assert_equal(42, g:result)
1397
1398 unlet g:result
1399 delete('XsomeExport.vim')
1400 delete('Xmapscript.vim')
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001401 nunmap <F3>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001402enddef
1403
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01001404def Test_vim9script_mix()
1405 var lines =<< trim END
1406 if has(g:feature)
1407 " legacy script
1408 let g:legacy = 1
1409 finish
1410 endif
1411 vim9script
1412 g:legacy = 0
1413 END
1414 g:feature = 'eval'
1415 g:legacy = -1
1416 CheckScriptSuccess(lines)
1417 assert_equal(1, g:legacy)
1418
1419 g:feature = 'noteval'
1420 g:legacy = -1
1421 CheckScriptSuccess(lines)
1422 assert_equal(0, g:legacy)
1423enddef
1424
Bram Moolenaar750802b2020-02-23 18:08:33 +01001425def Test_vim9script_fails()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001426 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1427 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001428 CheckScriptFailure(['export var some = 123'], 'E1042:')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001429 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001430 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
Bram Moolenaar750802b2020-02-23 18:08:33 +01001431 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
1432
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001433 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001434 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1435
Bram Moolenaare2e40752020-09-04 21:18:46 +02001436 assert_fails('vim9script', 'E1038:')
1437 assert_fails('export something', 'E1043:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438enddef
1439
Bram Moolenaarf7d267e2020-06-17 12:04:54 +02001440func Test_import_fails_without_script()
Bram Moolenaar101f4812020-06-16 23:18:51 +02001441 CheckRunVimInTerminal
1442
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001443 " call indirectly to avoid compilation error for missing functions
Bram Moolenaarc620c052020-07-08 15:16:19 +02001444 call Run_Test_import_fails_on_command_line()
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001445endfunc
1446
Bram Moolenaarc620c052020-07-08 15:16:19 +02001447def Run_Test_import_fails_on_command_line()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001448 var export =<< trim END
Bram Moolenaar101f4812020-06-16 23:18:51 +02001449 vim9script
1450 export def Foo(): number
1451 return 0
1452 enddef
1453 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001454 writefile(export, 'XexportCmd.vim')
Bram Moolenaar101f4812020-06-16 23:18:51 +02001455
Bram Moolenaare0de1712020-12-02 17:36:54 +01001456 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001457 rows: 6, wait_for_ruler: 0})
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001458 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaar101f4812020-06-16 23:18:51 +02001459
Bram Moolenaar730b2482020-08-09 13:02:10 +02001460 delete('XexportCmd.vim')
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001461 StopVimInTerminal(buf)
1462enddef
Bram Moolenaar101f4812020-06-16 23:18:51 +02001463
Bram Moolenaar2b327002020-12-26 15:39:31 +01001464def Test_vim9script_reload_noclear()
1465 var lines =<< trim END
Bram Moolenaara6294952020-12-27 13:39:50 +01001466 vim9script
1467 export var exported = 'thexport'
1468 END
1469 writefile(lines, 'XExportReload')
1470 lines =<< trim END
Bram Moolenaar2b327002020-12-26 15:39:31 +01001471 vim9script noclear
1472 g:loadCount += 1
1473 var s:reloaded = 'init'
Bram Moolenaara6294952020-12-27 13:39:50 +01001474 import exported from './XExportReload'
Bram Moolenaar2b327002020-12-26 15:39:31 +01001475
1476 def Again(): string
1477 return 'again'
1478 enddef
1479
1480 if exists('s:loaded') | finish | endif
1481 var s:loaded = true
1482
1483 var s:notReloaded = 'yes'
1484 s:reloaded = 'first'
1485 def g:Values(): list<string>
Bram Moolenaara6294952020-12-27 13:39:50 +01001486 return [s:reloaded, s:notReloaded, Again(), Once(), exported]
Bram Moolenaar2b327002020-12-26 15:39:31 +01001487 enddef
1488
1489 def Once(): string
1490 return 'once'
1491 enddef
1492 END
1493 writefile(lines, 'XReloaded')
1494 g:loadCount = 0
1495 source XReloaded
1496 assert_equal(1, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001497 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001498 source XReloaded
1499 assert_equal(2, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001500 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001501 source XReloaded
1502 assert_equal(3, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001503 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001504
Bram Moolenaar48e11c12021-01-11 18:47:00 +01001505 delete('XReloaded')
Bram Moolenaara6294952020-12-27 13:39:50 +01001506 delete('XExportReload')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001507 delfunc g:Values
Bram Moolenaar2b327002020-12-26 15:39:31 +01001508 unlet g:loadCount
1509enddef
1510
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001511def Test_vim9script_reload_import()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001512 var lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 vim9script
1514 const var = ''
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001515 var valone = 1234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001516 def MyFunc(arg: string)
1517 valone = 5678
1518 enddef
1519 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001520 var morelines =<< trim END
1521 var valtwo = 222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 export def GetValtwo(): number
1523 return valtwo
1524 enddef
1525 END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001526 writefile(lines + morelines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527 source Xreload.vim
1528 source Xreload.vim
1529 source Xreload.vim
1530
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001531 var testlines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532 vim9script
1533 def TheFunc()
1534 import GetValtwo from './Xreload.vim'
1535 assert_equal(222, GetValtwo())
1536 enddef
1537 TheFunc()
1538 END
1539 writefile(testlines, 'Ximport.vim')
1540 source Ximport.vim
1541
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001542 # Test that when not using "morelines" GetValtwo() and valtwo are still
1543 # defined, because import doesn't reload a script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 writefile(lines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545 source Ximport.vim
1546
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001547 # cannot declare a var twice
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548 lines =<< trim END
1549 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001550 var valone = 1234
1551 var valone = 5678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 END
1553 writefile(lines, 'Xreload.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001554 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555
1556 delete('Xreload.vim')
1557 delete('Ximport.vim')
1558enddef
1559
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001560" if a script is reloaded with a script-local variable that changed its type, a
1561" compiled function using that variable must fail.
1562def Test_script_reload_change_type()
1563 var lines =<< trim END
1564 vim9script noclear
1565 var str = 'string'
1566 def g:GetStr(): string
1567 return str .. 'xxx'
1568 enddef
1569 END
1570 writefile(lines, 'Xreload.vim')
1571 source Xreload.vim
1572 echo g:GetStr()
1573
1574 lines =<< trim END
1575 vim9script noclear
1576 var str = 1234
1577 END
1578 writefile(lines, 'Xreload.vim')
1579 source Xreload.vim
1580 assert_fails('echo g:GetStr()', 'E1150:')
1581
1582 delfunc g:GetStr
1583 delete('Xreload.vim')
1584enddef
1585
Bram Moolenaarc970e422021-03-17 15:03:04 +01001586" Define CallFunc so that the test can be compiled
1587command CallFunc echo 'nop'
1588
1589def Test_script_reload_from_function()
1590 var lines =<< trim END
1591 vim9script
1592
1593 if exists('g:loaded')
1594 finish
1595 endif
1596 g:loaded = 1
1597 delcommand CallFunc
1598 command CallFunc Func()
1599 def Func()
Bram Moolenaara4c81be2021-03-17 15:23:16 +01001600 so XreloadFunc.vim
Bram Moolenaarc970e422021-03-17 15:03:04 +01001601 g:didTheFunc = 1
1602 enddef
1603 END
1604 writefile(lines, 'XreloadFunc.vim')
1605 source XreloadFunc.vim
1606 CallFunc
1607 assert_equal(1, g:didTheFunc)
1608
1609 delete('XreloadFunc.vim')
1610 delcommand CallFunc
1611 unlet g:loaded
1612 unlet g:didTheFunc
1613enddef
1614
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001615def Test_script_var_shadows_function()
1616 var lines =<< trim END
1617 vim9script
1618 def Func(): number
1619 return 123
1620 enddef
1621 var Func = 1
1622 END
1623 CheckScriptFailure(lines, 'E1041:', 5)
1624enddef
1625
Bram Moolenaar95006e32020-08-29 17:47:08 +02001626def s:RetSome(): string
1627 return 'some'
1628enddef
1629
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001630" Not exported function that is referenced needs to be accessed by the
1631" script-local name.
1632def Test_vim9script_funcref()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001633 var sortlines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001634 vim9script
1635 def Compare(i1: number, i2: number): number
Bram Moolenaarbed36b92020-07-07 23:31:36 +02001636 return i2 - i1
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001637 enddef
1638
1639 export def FastSort(): list<number>
1640 return range(5)->sort(Compare)
1641 enddef
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001642
1643 export def GetString(arg: string): string
1644 return arg
1645 enddef
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001646 END
1647 writefile(sortlines, 'Xsort.vim')
1648
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001649 var lines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001650 vim9script
1651 import FastSort from './Xsort.vim'
1652 def Test()
1653 g:result = FastSort()
1654 enddef
1655 Test()
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001656
1657 # using a function imported with "as"
1658 import * as anAlias from './Xsort.vim'
1659 assert_equal('yes', anAlias.GetString('yes'))
1660
1661 # using the function from a compiled function
1662 def TestMore(): string
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001663 var s = s:anAlias.GetString('foo')
1664 return s .. anAlias.GetString('bar')
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001665 enddef
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001666 assert_equal('foobar', TestMore())
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001667
1668 # error when using a function that isn't exported
1669 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001670 END
1671 writefile(lines, 'Xscript.vim')
1672
1673 source Xscript.vim
1674 assert_equal([4, 3, 2, 1, 0], g:result)
1675
1676 unlet g:result
1677 delete('Xsort.vim')
1678 delete('Xscript.vim')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001679
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001680 var Funcref = function('s:RetSome')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001681 assert_equal('some', Funcref())
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001682enddef
1683
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001684" Check that when searching for "FilterFunc" it finds the import in the
1685" script where FastFilter() is called from, both as a string and as a direct
1686" function reference.
Bram Moolenaarc620c052020-07-08 15:16:19 +02001687def Test_vim9script_funcref_other_script()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001688 var filterLines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001689 vim9script
1690 export def FilterFunc(idx: number, val: number): bool
1691 return idx % 2 == 1
1692 enddef
1693 export def FastFilter(): list<number>
1694 return range(10)->filter('FilterFunc')
1695 enddef
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001696 export def FastFilterDirect(): list<number>
1697 return range(10)->filter(FilterFunc)
1698 enddef
Bram Moolenaarc620c052020-07-08 15:16:19 +02001699 END
1700 writefile(filterLines, 'Xfilter.vim')
1701
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001702 var lines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001703 vim9script
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001704 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim'
Bram Moolenaarc620c052020-07-08 15:16:19 +02001705 def Test()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001706 var x: list<number> = FastFilter()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001707 enddef
1708 Test()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001709 def TestDirect()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001710 var x: list<number> = FastFilterDirect()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001711 enddef
1712 TestDirect()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001713 END
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001714 CheckScriptSuccess(lines)
Bram Moolenaarc620c052020-07-08 15:16:19 +02001715 delete('Xfilter.vim')
Bram Moolenaarc620c052020-07-08 15:16:19 +02001716enddef
1717
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001718def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001719 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001720 vim9script
1721 def FuncYes(): string
1722 return 'yes'
1723 enddef
1724 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001725 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001726 def FuncNo(): string
1727 return 'no'
1728 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001729 def g:DoCheck(no_exists: bool)
1730 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001731 assert_equal('no', FuncNo())
1732 enddef
1733 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001734 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001735 def g:DoCheck(no_exists: bool)
1736 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02001737 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001738 enddef
1739 END
1740
1741 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001742 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001743 source Xreloaded.vim
1744 g:DoCheck(true)
1745
1746 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001747 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001748 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001749 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001750
1751 # FuncNo() is back
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001752 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001753 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001754 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001755
1756 delete('Xreloaded.vim')
1757enddef
1758
Bram Moolenaar89483d42020-05-10 15:24:44 +02001759def Test_vim9script_reload_delvar()
1760 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001761 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02001762 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001763 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001764 END
1765 writefile(lines, 'XreloadVar.vim')
1766 source XreloadVar.vim
1767
1768 # now write the script using the same variable locally - works
1769 lines =<< trim END
1770 vim9script
1771 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001772 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001773 enddef
1774 END
1775 writefile(lines, 'XreloadVar.vim')
1776 source XreloadVar.vim
1777
1778 delete('XreloadVar.vim')
1779enddef
1780
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781def Test_import_absolute()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001782 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02001783 'vim9script',
1784 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
1785 'def UseExported()',
1786 ' g:imported_abs = exported',
1787 ' exported = 8888',
1788 ' g:imported_after = exported',
1789 'enddef',
1790 'UseExported()',
1791 'g:import_disassembled = execute("disass UseExported")',
1792 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 writefile(import_lines, 'Ximport_abs.vim')
1794 writefile(s:export_script_lines, 'Xexport_abs.vim')
1795
1796 source Ximport_abs.vim
1797
1798 assert_equal(9876, g:imported_abs)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001799 assert_equal(8888, g:imported_after)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001800 assert_match('<SNR>\d\+_UseExported\_s*' ..
1801 'g:imported_abs = exported\_s*' ..
1802 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1803 '1 STOREG g:imported_abs\_s*' ..
1804 'exported = 8888\_s*' ..
1805 '2 PUSHNR 8888\_s*' ..
1806 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1807 'g:imported_after = exported\_s*' ..
1808 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1809 '5 STOREG g:imported_after',
Bram Moolenaare6085c52020-04-12 20:19:16 +02001810 g:import_disassembled)
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001811
1812 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813 unlet g:imported_abs
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001814 unlet g:import_disassembled
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815
1816 delete('Ximport_abs.vim')
1817 delete('Xexport_abs.vim')
1818enddef
1819
1820def Test_import_rtp()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001821 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02001822 'vim9script',
1823 'import exported from "Xexport_rtp.vim"',
1824 'g:imported_rtp = exported',
1825 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 writefile(import_lines, 'Ximport_rtp.vim')
1827 mkdir('import')
1828 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1829
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001830 var save_rtp = &rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 &rtp = getcwd()
1832 source Ximport_rtp.vim
1833 &rtp = save_rtp
1834
1835 assert_equal(9876, g:imported_rtp)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001837 Undo_export_script_lines()
1838 unlet g:imported_rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 delete('Ximport_rtp.vim')
Bram Moolenaar89483d42020-05-10 15:24:44 +02001840 delete('import', 'rf')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841enddef
1842
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001843def Test_import_compile_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001844 var export_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001845 'vim9script',
1846 'export def ExpFunc(): string',
1847 ' return notDefined',
1848 'enddef',
1849 ]
1850 writefile(export_lines, 'Xexported.vim')
1851
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001852 var import_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001853 'vim9script',
1854 'import ExpFunc from "./Xexported.vim"',
1855 'def ImpFunc()',
1856 ' echo ExpFunc()',
1857 'enddef',
1858 'defcompile',
1859 ]
1860 writefile(import_lines, 'Ximport.vim')
1861
1862 try
1863 source Ximport.vim
1864 catch /E1001/
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001865 # Error should be fore the Xexported.vim file.
Bram Moolenaar77072282020-09-16 17:55:40 +02001866 assert_match('E1001: Variable not found: notDefined', v:exception)
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001867 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1868 endtry
1869
1870 delete('Xexported.vim')
1871 delete('Ximport.vim')
1872enddef
1873
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001874def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001875 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001876 'vim9script',
1877 'def Func()',
1878 ' eval [][0]',
1879 'enddef',
1880 'Func()',
1881 ]
1882 writefile(lines, 'Xtestscript.vim')
1883
1884 for count in range(3)
1885 try
1886 source Xtestscript.vim
1887 catch /E684/
1888 # function name should contain <SNR> every time
1889 assert_match('E684: list index out of range', v:exception)
1890 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
1891 endtry
1892 endfor
1893
1894 delete('Xtestscript.vim')
1895enddef
1896
Bram Moolenaareef21022020-08-01 22:16:43 +02001897def Test_func_overrules_import_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001898 var export_lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02001899 vim9script
1900 export def Func()
1901 echo 'imported'
1902 enddef
1903 END
1904 writefile(export_lines, 'XexportedFunc.vim')
1905
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001906 var lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02001907 vim9script
1908 import Func from './XexportedFunc.vim'
1909 def Func()
1910 echo 'local to function'
1911 enddef
1912 END
1913 CheckScriptFailure(lines, 'E1073:')
1914
1915 lines =<< trim END
1916 vim9script
1917 import Func from './XexportedFunc.vim'
1918 def Outer()
1919 def Func()
1920 echo 'local to function'
1921 enddef
1922 enddef
1923 defcompile
1924 END
1925 CheckScriptFailure(lines, 'E1073:')
1926
1927 delete('XexportedFunc.vim')
1928enddef
1929
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02001930def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001931 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02001932 vim9script
1933 def Func()
1934 echo 'one'
1935 enddef
1936 def Func()
1937 echo 'two'
1938 enddef
1939 END
1940 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02001941
1942 lines =<< trim END
1943 vim9script
1944 def Foo(): string
1945 return 'foo'
1946 enddef
1947 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001948 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02001949 enddef
1950 defcompile
1951 END
1952 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02001953enddef
1954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001956 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001957 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 l->remove(0)
1959 l->add(5)
1960 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001961 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962enddef
1963
Bram Moolenaarae616492020-07-28 20:07:27 +02001964def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02001965 CheckDefExecFailure(['a = 1'], 'E1100:')
1966 CheckDefExecFailure(['c = 1'], 'E1100:')
1967 CheckDefExecFailure(['i = 1'], 'E1100:')
1968 CheckDefExecFailure(['t = 1'], 'E1100:')
1969 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02001970
Bram Moolenaarae616492020-07-28 20:07:27 +02001971 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
1972 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02001973 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
1974 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02001975 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
1976 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01001977 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
1978 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02001979 CheckScriptFailure(['vim9script', 't'], 'E1100:')
1980 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
1981 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02001982enddef
1983
Bram Moolenaar158906c2020-02-06 20:39:45 +01001984def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001985 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01001986 if what == 1
1987 res = "one"
1988 elseif what == 2
1989 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01001990 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01001991 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01001992 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01001993 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01001994enddef
1995
Bram Moolenaar158906c2020-02-06 20:39:45 +01001996def Test_if_elseif_else()
1997 assert_equal('one', IfElse(1))
1998 assert_equal('two', IfElse(2))
1999 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002000enddef
2001
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002002def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002003 CheckDefFailure(['elseif true'], 'E582:')
2004 CheckDefFailure(['else'], 'E581:')
2005 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01002006 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002007 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002008
2009 var lines =<< trim END
2010 var s = ''
2011 if s = ''
2012 endif
2013 END
2014 CheckDefFailure(lines, 'E488:')
2015
2016 lines =<< trim END
2017 var s = ''
2018 if s == ''
2019 elseif s = ''
2020 endif
2021 END
2022 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002023enddef
2024
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002025let g:bool_true = v:true
2026let g:bool_false = v:false
2027
2028def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002029 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002030 if true ? true : false
2031 res = true
2032 endif
2033 assert_equal(true, res)
2034
Bram Moolenaar585fea72020-04-02 22:33:21 +02002035 g:glob = 2
2036 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02002037 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002038 endif
2039 assert_equal(2, g:glob)
2040 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02002041 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002042 endif
2043 assert_equal(3, g:glob)
2044
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002045 res = false
2046 if g:bool_true ? true : false
2047 res = true
2048 endif
2049 assert_equal(true, res)
2050
2051 res = false
2052 if true ? g:bool_true : false
2053 res = true
2054 endif
2055 assert_equal(true, res)
2056
2057 res = false
2058 if true ? true : g:bool_false
2059 res = true
2060 endif
2061 assert_equal(true, res)
2062
2063 res = false
2064 if true ? false : true
2065 res = true
2066 endif
2067 assert_equal(false, res)
2068
2069 res = false
2070 if false ? false : true
2071 res = true
2072 endif
2073 assert_equal(true, res)
2074
2075 res = false
2076 if false ? true : false
2077 res = true
2078 endif
2079 assert_equal(false, res)
2080
2081 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002082 if has('xyz') ? true : false
2083 res = true
2084 endif
2085 assert_equal(false, res)
2086
2087 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002088 if true && true
2089 res = true
2090 endif
2091 assert_equal(true, res)
2092
2093 res = false
2094 if true && false
2095 res = true
2096 endif
2097 assert_equal(false, res)
2098
2099 res = false
2100 if g:bool_true && false
2101 res = true
2102 endif
2103 assert_equal(false, res)
2104
2105 res = false
2106 if true && g:bool_false
2107 res = true
2108 endif
2109 assert_equal(false, res)
2110
2111 res = false
2112 if false && false
2113 res = true
2114 endif
2115 assert_equal(false, res)
2116
2117 res = false
2118 if true || false
2119 res = true
2120 endif
2121 assert_equal(true, res)
2122
2123 res = false
2124 if g:bool_true || false
2125 res = true
2126 endif
2127 assert_equal(true, res)
2128
2129 res = false
2130 if true || g:bool_false
2131 res = true
2132 endif
2133 assert_equal(true, res)
2134
2135 res = false
2136 if false || false
2137 res = true
2138 endif
2139 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02002140
2141 # with constant "false" expression may be invalid so long as the syntax is OK
2142 if false | eval 0 | endif
2143 if false | eval burp + 234 | endif
2144 if false | echo burp 234 'asd' | endif
2145 if false
2146 burp
2147 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002148enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002149
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002150def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002151 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
2152 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
2153 CheckDefFailure(["if has('aaa'"], 'E110:')
2154 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002155enddef
2156
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002157def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002158 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002159 if i % 2
2160 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002161 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002162 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002163 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002164 endif
2165 x += 1
2166 else
2167 x += 1000
2168 endif
2169 return x
2170enddef
2171
2172def Test_nested_if()
2173 assert_equal(1, RunNested(1))
2174 assert_equal(1000, RunNested(2))
2175enddef
2176
Bram Moolenaarad39c092020-02-26 18:23:43 +01002177def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01002178 # missing argument is ignored
2179 execute
2180 execute # comment
2181
Bram Moolenaarad39c092020-02-26 18:23:43 +01002182 new
2183 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002184 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002185 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002186
Bram Moolenaard2c61702020-09-06 15:58:36 +02002187 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002188 assert_equal('execute-string', getline(1))
2189
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002190 var cmd1 = 'setline(1,'
2191 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002192 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01002193 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002194
Bram Moolenaard2c61702020-09-06 15:58:36 +02002195 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002196 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002197
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002198 var cmd_first = 'call '
2199 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002200 execute cmd_first .. cmd_last
2201 assert_equal('execute-var-var', getline(1))
2202 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02002203
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002204 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002205 execute 'echomsg' (n ? '"true"' : '"no"')
2206 assert_match('^true$', Screenline(&lines))
2207
Bram Moolenaare0de1712020-12-02 17:36:54 +01002208 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002209 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
2210
Bram Moolenaard2c61702020-09-06 15:58:36 +02002211 CheckDefFailure(['execute xxx'], 'E1001:', 1)
2212 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
2213 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002214enddef
2215
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002216def Test_execute_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002217 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002218 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002219 vim9script
2220 execute 'g:someVar'
2221 .. ' = ' ..
2222 '28'
2223 assert_equal(28, g:someVar)
2224 unlet g:someVar
2225 END
2226 CheckScriptSuccess(lines)
2227enddef
2228
Bram Moolenaarad39c092020-02-26 18:23:43 +01002229def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002230 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02002231 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002232 assert_match('^something$', Screenline(&lines))
2233
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002234 echo "some" # comment
2235 echon "thing"
2236 assert_match('^something$', Screenline(&lines))
2237
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002238 var str1 = 'some'
2239 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002240 echo str1 str2
2241 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002242
Bram Moolenaard2c61702020-09-06 15:58:36 +02002243 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01002244enddef
2245
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002246def Test_echomsg_cmd()
2247 echomsg 'some' 'more' # comment
2248 assert_match('^some more$', Screenline(&lines))
2249 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002250 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002251 assert_match('^some more$', Screenline(&lines))
2252
Bram Moolenaard2c61702020-09-06 15:58:36 +02002253 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002254enddef
2255
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002256def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002257 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002258 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002259 vim9script
2260 echomsg 'here'
2261 .. ' is ' ..
2262 'a message'
2263 assert_match('^here is a message$', Screenline(&lines))
2264 END
2265 CheckScriptSuccess(lines)
2266enddef
2267
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002268def Test_echoerr_cmd()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002269 try
2270 echoerr 'something' 'wrong' # comment
2271 catch
2272 assert_match('something wrong', v:exception)
2273 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002274enddef
2275
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002276def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002277 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002278 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002279 vim9script
2280 try
2281 echoerr 'this'
2282 .. ' is ' ..
2283 'wrong'
2284 catch
2285 assert_match('this is wrong', v:exception)
2286 endtry
2287 END
2288 CheckScriptSuccess(lines)
2289enddef
2290
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002291def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002292 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002293 vim9script
2294 new
2295 for var in range(0, 3)
2296 append(line('$'), var)
2297 endfor
2298 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2299 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01002300
2301 var result = ''
2302 for i in [1, 2, 3]
2303 var loop = ' loop ' .. i
2304 result ..= loop
2305 endfor
2306 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002307 END
2308 writefile(lines, 'Xvim9for.vim')
2309 source Xvim9for.vim
2310 delete('Xvim9for.vim')
2311enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002313def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002314 var lines =<< trim END
2315 var result = ''
2316 for cnt in range(7)
2317 if cnt == 4
2318 break
2319 endif
2320 if cnt == 2
2321 continue
2322 endif
2323 result ..= cnt .. '_'
2324 endfor
2325 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02002326
Bram Moolenaarf2253962021-04-13 20:53:13 +02002327 var concat = ''
2328 for str in eval('["one", "two"]')
2329 concat ..= str
2330 endfor
2331 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002332
Bram Moolenaarf2253962021-04-13 20:53:13 +02002333 var total = 0
2334 for nr in
2335 [1, 2, 3]
2336 total += nr
2337 endfor
2338 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002339
Bram Moolenaarf2253962021-04-13 20:53:13 +02002340 total = 0
2341 for nr
2342 in [1, 2, 3]
2343 total += nr
2344 endfor
2345 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002346
Bram Moolenaarf2253962021-04-13 20:53:13 +02002347 total = 0
2348 for nr
2349 in
2350 [1, 2, 3]
2351 total += nr
2352 endfor
2353 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01002354
Bram Moolenaara3589a02021-04-14 13:30:46 +02002355 # with type
2356 total = 0
2357 for n: number in [1, 2, 3]
2358 total += n
2359 endfor
2360 assert_equal(6, total)
2361
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002362 var chars = ''
2363 for s: string in 'foobar'
2364 chars ..= s
2365 endfor
2366 assert_equal('foobar', chars)
2367
Bram Moolenaara3589a02021-04-14 13:30:46 +02002368 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02002369 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02002370 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2371 res ..= n .. s
2372 endfor
2373 assert_equal('1a2b', res)
2374
2375 # loop over string
2376 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02002377 for c in 'aéc̀d'
2378 res ..= c .. '-'
2379 endfor
2380 assert_equal('a-é-c̀-d-', res)
2381
2382 res = ''
2383 for c in ''
2384 res ..= c .. '-'
2385 endfor
2386 assert_equal('', res)
2387
2388 res = ''
2389 for c in test_null_string()
2390 res ..= c .. '-'
2391 endfor
2392 assert_equal('', res)
2393
2394 var foo: list<dict<any>> = [
2395 {a: 'Cat'}
2396 ]
2397 for dd in foo
2398 dd.counter = 12
2399 endfor
2400 assert_equal([{a: 'Cat', counter: 12}], foo)
2401 END
2402 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002403enddef
2404
2405def Test_for_loop_fails()
Bram Moolenaar025cb1c2020-12-14 18:31:27 +01002406 CheckDefFailure(['for '], 'E1097:')
2407 CheckDefFailure(['for x'], 'E1097:')
2408 CheckDefFailure(['for x in'], 'E1097:')
Bram Moolenaar675f7162020-04-12 22:53:54 +02002409 CheckDefFailure(['for # in range(5)'], 'E690:')
2410 CheckDefFailure(['for i In range(5)'], 'E690:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002411 CheckDefFailure(['var x = 5', 'for x in range(5)'], 'E1017:')
Bram Moolenaar822ba242020-05-24 23:00:18 +02002412 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002413 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02002414 CheckDefFailure(['for i in xxx'], 'E1001:')
2415 CheckDefFailure(['endfor'], 'E588:')
2416 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002417
2418 # wrong type detected at compile time
2419 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2420
2421 # wrong type detected at runtime
2422 g:adict = {a: 1}
2423 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2424 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002425
2426 var lines =<< trim END
2427 var d: list<dict<any>> = [{a: 0}]
2428 for e in d
2429 e = {a: 0, b: ''}
2430 endfor
2431 END
2432 CheckDefAndScriptFailure2(lines, 'E1018:', 'E46:', 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002433
2434 lines =<< trim END
2435 for nr: number in ['foo']
2436 endfor
2437 END
2438 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002439enddef
2440
Bram Moolenaarea870692020-12-02 14:24:30 +01002441def Test_for_loop_script_var()
2442 # cannot use s:var in a :def function
2443 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1101:')
2444
2445 # can use s:var in Vim9 script, with or without s:
2446 var lines =<< trim END
2447 vim9script
2448 var total = 0
2449 for s:var in [1, 2, 3]
2450 total += s:var
2451 endfor
2452 assert_equal(6, total)
2453
2454 total = 0
2455 for var in [1, 2, 3]
2456 total += var
2457 endfor
2458 assert_equal(6, total)
2459 END
2460enddef
2461
Bram Moolenaar792f7862020-11-23 08:31:18 +01002462def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01002463 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002464 var result = []
2465 for [v1, v2] in [[1, 2], [3, 4]]
2466 result->add(v1)
2467 result->add(v2)
2468 endfor
2469 assert_equal([1, 2, 3, 4], result)
2470
2471 result = []
2472 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2473 result->add(v1)
2474 result->add(v2)
2475 result->add(v3)
2476 endfor
2477 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
2478
2479 result = []
2480 for [&ts, &sw] in [[1, 2], [3, 4]]
2481 result->add(&ts)
2482 result->add(&sw)
2483 endfor
2484 assert_equal([1, 2, 3, 4], result)
2485
2486 var slist: list<string>
2487 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
2488 slist->add($LOOPVAR)
2489 slist->add(@r)
2490 slist->add(v:errmsg)
2491 endfor
2492 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
2493
2494 slist = []
2495 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
2496 slist->add(g:globalvar)
2497 slist->add(b:bufvar)
2498 slist->add(w:winvar)
2499 slist->add(t:tabvar)
2500 endfor
2501 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01002502 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaarb777da92021-05-22 21:40:39 +02002503
2504 var res = []
2505 for [_, n, _] in [[1, 2, 3], [4, 5, 6]]
2506 res->add(n)
2507 endfor
2508 assert_equal([2, 5], res)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002509 END
2510 CheckDefAndScriptSuccess(lines)
2511
2512 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01002513 for [v1, v2] in [[1, 2, 3], [3, 4]]
2514 echo v1 v2
2515 endfor
2516 END
2517 CheckDefExecFailure(lines, 'E710:', 1)
2518
2519 lines =<< trim END
2520 for [v1, v2] in [[1], [3, 4]]
2521 echo v1 v2
2522 endfor
2523 END
2524 CheckDefExecFailure(lines, 'E711:', 1)
2525
2526 lines =<< trim END
2527 for [v1, v1] in [[1, 2], [3, 4]]
2528 echo v1
2529 endfor
2530 END
2531 CheckDefExecFailure(lines, 'E1017:', 1)
2532enddef
2533
Bram Moolenaarc150c092021-02-13 15:02:46 +01002534def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002535 var lines =<< trim END
2536 var looped = 0
2537 var cleanup = 0
2538 for i in range(3)
2539 looped += 1
2540 try
2541 eval [][0]
2542 catch
2543 continue
2544 finally
2545 cleanup += 1
2546 endtry
2547 endfor
2548 assert_equal(3, looped)
2549 assert_equal(3, cleanup)
2550 END
2551 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002552enddef
2553
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002554def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002555 var result = ''
2556 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002557 while cnt < 555
2558 if cnt == 3
2559 break
2560 endif
2561 cnt += 1
2562 if cnt == 2
2563 continue
2564 endif
2565 result ..= cnt .. '_'
2566 endwhile
2567 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002568
2569 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002570 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002571 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002572enddef
2573
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002574def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02002575 CheckDefFailure(['while xxx'], 'E1001:')
2576 CheckDefFailure(['endwhile'], 'E588:')
2577 CheckDefFailure(['continue'], 'E586:')
2578 CheckDefFailure(['if true', 'continue'], 'E586:')
2579 CheckDefFailure(['break'], 'E587:')
2580 CheckDefFailure(['if true', 'break'], 'E587:')
2581 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002582
2583 var lines =<< trim END
2584 var s = ''
2585 while s = ''
2586 endwhile
2587 END
2588 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002589enddef
2590
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002591def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002592 var caught = false
2593 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002594 try
2595 while 1
2596 x += 1
2597 if x == 100
2598 feedkeys("\<C-C>", 'Lt')
2599 endif
2600 endwhile
2601 catch
2602 caught = true
2603 assert_equal(100, x)
2604 endtry
2605 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02002606 # consume the CTRL-C
2607 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002608enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01002609
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002610def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002611 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002612 'one',
2613 'two',
2614 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002615 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002616 assert_equal(['one', 'two', 'three'], mylist)
2617
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002618 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01002619 ['one']: 1,
2620 ['two']: 2,
2621 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002622 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002623 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01002624 assert_equal({one: 1, two: 2, three: 3}, mydict)
2625 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002626 one: 1, # comment
2627 two: # comment
2628 2, # comment
2629 three: 3 # comment
2630 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002631 assert_equal({one: 1, two: 2, three: 3}, mydict)
2632 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002633 one: 1,
2634 two:
2635 2,
2636 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002637 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002638 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002639
2640 assert_equal(
2641 ['one', 'two', 'three'],
2642 split('one two three')
2643 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002644enddef
2645
Bram Moolenaar7a092242020-04-16 22:10:49 +02002646def Test_vim9_comment()
2647 CheckScriptSuccess([
2648 'vim9script',
2649 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002650 '#something',
2651 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002652 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002653
2654 split Xfile
2655 CheckScriptSuccess([
2656 'vim9script',
2657 'edit #something',
2658 ])
2659 CheckScriptSuccess([
2660 'vim9script',
2661 'edit #{something',
2662 ])
2663 close
2664
Bram Moolenaar7a092242020-04-16 22:10:49 +02002665 CheckScriptFailure([
2666 'vim9script',
2667 ':# something',
2668 ], 'E488:')
2669 CheckScriptFailure([
2670 '# something',
2671 ], 'E488:')
2672 CheckScriptFailure([
2673 ':# something',
2674 ], 'E488:')
2675
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002676 { # block start
2677 } # block end
2678 CheckDefFailure([
2679 '{# comment',
2680 ], 'E488:')
2681 CheckDefFailure([
2682 '{',
2683 '}# comment',
2684 ], 'E488:')
2685
2686 echo "yes" # comment
2687 CheckDefFailure([
2688 'echo "yes"# comment',
2689 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002690 CheckScriptSuccess([
2691 'vim9script',
2692 'echo "yes" # something',
2693 ])
2694 CheckScriptFailure([
2695 'vim9script',
2696 'echo "yes"# something',
2697 ], 'E121:')
2698 CheckScriptFailure([
2699 'vim9script',
2700 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002701 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002702 CheckScriptFailure([
2703 'echo "yes" # something',
2704 ], 'E121:')
2705
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002706 exe "echo" # comment
2707 CheckDefFailure([
2708 'exe "echo"# comment',
2709 ], 'E488:')
2710 CheckScriptSuccess([
2711 'vim9script',
2712 'exe "echo" # something',
2713 ])
2714 CheckScriptFailure([
2715 'vim9script',
2716 'exe "echo"# something',
2717 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002718 CheckScriptFailure([
2719 'vim9script',
2720 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002721 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002722 CheckScriptFailure([
2723 'exe "echo" # something',
2724 ], 'E121:')
2725
Bram Moolenaar7a092242020-04-16 22:10:49 +02002726 CheckDefFailure([
2727 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002728 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002729 'catch',
2730 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002731 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002732 CheckScriptFailure([
2733 'vim9script',
2734 'try# comment',
2735 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002736 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002737 CheckDefFailure([
2738 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002739 ' throw#comment',
2740 'catch',
2741 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002742 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002743 CheckDefFailure([
2744 'try',
2745 ' throw "yes"#comment',
2746 'catch',
2747 'endtry',
2748 ], 'E488:')
2749 CheckDefFailure([
2750 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002751 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002752 'catch# comment',
2753 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002754 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002755 CheckScriptFailure([
2756 'vim9script',
2757 'try',
2758 ' echo "yes"',
2759 'catch# comment',
2760 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002761 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002762 CheckDefFailure([
2763 'try',
2764 ' echo "yes"',
2765 'catch /pat/# comment',
2766 'endtry',
2767 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002768 CheckDefFailure([
2769 'try',
2770 'echo "yes"',
2771 'catch',
2772 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002773 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002774 CheckScriptFailure([
2775 'vim9script',
2776 'try',
2777 ' echo "yes"',
2778 'catch',
2779 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002780 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002781
2782 CheckScriptSuccess([
2783 'vim9script',
2784 'hi # comment',
2785 ])
2786 CheckScriptFailure([
2787 'vim9script',
2788 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002789 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002790 CheckScriptSuccess([
2791 'vim9script',
2792 'hi Search # comment',
2793 ])
2794 CheckScriptFailure([
2795 'vim9script',
2796 'hi Search# comment',
2797 ], 'E416:')
2798 CheckScriptSuccess([
2799 'vim9script',
2800 'hi link This Search # comment',
2801 ])
2802 CheckScriptFailure([
2803 'vim9script',
2804 'hi link This That# comment',
2805 ], 'E413:')
2806 CheckScriptSuccess([
2807 'vim9script',
2808 'hi clear This # comment',
2809 'hi clear # comment',
2810 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002811 # not tested, because it doesn't give an error but a warning:
2812 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02002813 CheckScriptFailure([
2814 'vim9script',
2815 'hi clear# comment',
2816 ], 'E416:')
2817
2818 CheckScriptSuccess([
2819 'vim9script',
2820 'hi Group term=bold',
2821 'match Group /todo/ # comment',
2822 ])
2823 CheckScriptFailure([
2824 'vim9script',
2825 'hi Group term=bold',
2826 'match Group /todo/# comment',
2827 ], 'E488:')
2828 CheckScriptSuccess([
2829 'vim9script',
2830 'match # comment',
2831 ])
2832 CheckScriptFailure([
2833 'vim9script',
2834 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002835 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002836 CheckScriptSuccess([
2837 'vim9script',
2838 'match none # comment',
2839 ])
2840 CheckScriptFailure([
2841 'vim9script',
2842 'match none# comment',
2843 ], 'E475:')
2844
2845 CheckScriptSuccess([
2846 'vim9script',
2847 'menutrans clear # comment',
2848 ])
2849 CheckScriptFailure([
2850 'vim9script',
2851 'menutrans clear# comment text',
2852 ], 'E474:')
2853
2854 CheckScriptSuccess([
2855 'vim9script',
2856 'syntax clear # comment',
2857 ])
2858 CheckScriptFailure([
2859 'vim9script',
2860 'syntax clear# comment text',
2861 ], 'E28:')
2862 CheckScriptSuccess([
2863 'vim9script',
2864 'syntax keyword Word some',
2865 'syntax clear Word # comment',
2866 ])
2867 CheckScriptFailure([
2868 'vim9script',
2869 'syntax keyword Word some',
2870 'syntax clear Word# comment text',
2871 ], 'E28:')
2872
2873 CheckScriptSuccess([
2874 'vim9script',
2875 'syntax list # comment',
2876 ])
2877 CheckScriptFailure([
2878 'vim9script',
2879 'syntax list# comment text',
2880 ], 'E28:')
2881
2882 CheckScriptSuccess([
2883 'vim9script',
2884 'syntax match Word /pat/ oneline # comment',
2885 ])
2886 CheckScriptFailure([
2887 'vim9script',
2888 'syntax match Word /pat/ oneline# comment',
2889 ], 'E475:')
2890
2891 CheckScriptSuccess([
2892 'vim9script',
2893 'syntax keyword Word word # comm[ent',
2894 ])
2895 CheckScriptFailure([
2896 'vim9script',
2897 'syntax keyword Word word# comm[ent',
2898 ], 'E789:')
2899
2900 CheckScriptSuccess([
2901 'vim9script',
2902 'syntax match Word /pat/ # comment',
2903 ])
2904 CheckScriptFailure([
2905 'vim9script',
2906 'syntax match Word /pat/# comment',
2907 ], 'E402:')
2908
2909 CheckScriptSuccess([
2910 'vim9script',
2911 'syntax match Word /pat/ contains=Something # comment',
2912 ])
2913 CheckScriptFailure([
2914 'vim9script',
2915 'syntax match Word /pat/ contains=Something# comment',
2916 ], 'E475:')
2917 CheckScriptFailure([
2918 'vim9script',
2919 'syntax match Word /pat/ contains= # comment',
2920 ], 'E406:')
2921 CheckScriptFailure([
2922 'vim9script',
2923 'syntax match Word /pat/ contains=# comment',
2924 ], 'E475:')
2925
2926 CheckScriptSuccess([
2927 'vim9script',
2928 'syntax region Word start=/pat/ end=/pat/ # comment',
2929 ])
2930 CheckScriptFailure([
2931 'vim9script',
2932 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02002933 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002934
2935 CheckScriptSuccess([
2936 'vim9script',
2937 'syntax sync # comment',
2938 ])
2939 CheckScriptFailure([
2940 'vim9script',
2941 'syntax sync# comment',
2942 ], 'E404:')
2943 CheckScriptSuccess([
2944 'vim9script',
2945 'syntax sync ccomment # comment',
2946 ])
2947 CheckScriptFailure([
2948 'vim9script',
2949 'syntax sync ccomment# comment',
2950 ], 'E404:')
2951
2952 CheckScriptSuccess([
2953 'vim9script',
2954 'syntax cluster Some contains=Word # comment',
2955 ])
2956 CheckScriptFailure([
2957 'vim9script',
2958 'syntax cluster Some contains=Word# comment',
2959 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002960
2961 CheckScriptSuccess([
2962 'vim9script',
2963 'command Echo echo # comment',
2964 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002965 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002966 ])
2967 CheckScriptFailure([
2968 'vim9script',
2969 'command Echo echo# comment',
2970 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002971 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002972 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01002973
2974 var curdir = getcwd()
2975 CheckScriptSuccess([
2976 'command Echo cd " comment',
2977 'Echo',
2978 'delcommand Echo',
2979 ])
2980 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01002981 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01002982 'command Echo cd # comment',
2983 'Echo',
2984 'delcommand Echo',
2985 ])
2986 CheckScriptFailure([
2987 'vim9script',
2988 'command Echo cd " comment',
2989 'Echo',
2990 ], 'E344:')
2991 delcommand Echo
2992 chdir(curdir)
2993
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002994 CheckScriptFailure([
2995 'vim9script',
2996 'command Echo# comment',
2997 ], 'E182:')
2998 CheckScriptFailure([
2999 'vim9script',
3000 'command Echo echo',
3001 'command Echo# comment',
3002 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003003 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003004
3005 CheckScriptSuccess([
3006 'vim9script',
3007 'function # comment',
3008 ])
3009 CheckScriptFailure([
3010 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02003011 'function " comment',
3012 ], 'E129:')
3013 CheckScriptFailure([
3014 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003015 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003016 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003017 CheckScriptSuccess([
3018 'vim9script',
3019 'function CheckScriptSuccess # comment',
3020 ])
3021 CheckScriptFailure([
3022 'vim9script',
3023 'function CheckScriptSuccess# comment',
3024 ], 'E488:')
3025
3026 CheckScriptSuccess([
3027 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003028 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003029 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003030 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003031 ])
3032 CheckScriptFailure([
3033 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003034 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003035 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003036 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003037 ], 'E488:')
3038
3039 CheckScriptSuccess([
3040 'vim9script',
3041 'call execute("ls") # comment',
3042 ])
3043 CheckScriptFailure([
3044 'vim9script',
3045 'call execute("ls")# comment',
3046 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02003047
3048 CheckScriptFailure([
3049 'def Test() " comment',
3050 'enddef',
3051 ], 'E488:')
3052 CheckScriptFailure([
3053 'vim9script',
3054 'def Test() " comment',
3055 'enddef',
3056 ], 'E488:')
3057
3058 CheckScriptSuccess([
3059 'func Test() " comment',
3060 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003061 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02003062 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02003063 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02003064 'vim9script',
3065 'func Test() " comment',
3066 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02003067 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02003068
3069 CheckScriptSuccess([
3070 'def Test() # comment',
3071 'enddef',
3072 ])
3073 CheckScriptFailure([
3074 'func Test() # comment',
3075 'endfunc',
3076 ], 'E488:')
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003077
3078 var lines =<< trim END
3079 vim9script
3080 syn region Text
3081 \ start='foo'
3082 #\ comment
3083 \ end='bar'
3084 END
3085 CheckScriptSuccess(lines)
3086
3087 lines =<< trim END
3088 vim9script
3089 syn region Text
3090 \ start='foo'
3091 "\ comment
3092 \ end='bar'
3093 END
3094 CheckScriptFailure(lines, 'E399:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003095enddef
3096
3097def Test_vim9_comment_gui()
3098 CheckCanRunGui
3099
3100 CheckScriptFailure([
3101 'vim9script',
3102 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003103 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003104 CheckScriptFailure([
3105 'vim9script',
3106 'gui -f#comment'
3107 ], 'E499:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003108enddef
3109
Bram Moolenaara26b9702020-04-18 19:53:28 +02003110def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02003111 au TabEnter *.vim g:entered = 1
3112 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02003113
3114 edit test.vim
3115 doautocmd TabEnter #comment
3116 assert_equal(1, g:entered)
3117
3118 doautocmd TabEnter f.x
3119 assert_equal(2, g:entered)
3120
3121 g:entered = 0
3122 doautocmd TabEnter f.x #comment
3123 assert_equal(2, g:entered)
3124
3125 assert_fails('doautocmd Syntax#comment', 'E216:')
3126
3127 au! TabEnter
3128 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003129
3130 CheckScriptSuccess([
3131 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02003132 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003133 'b:var = 456',
3134 'w:var = 777',
3135 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003136 'unlet g:var w:var # something',
3137 ])
3138
3139 CheckScriptFailure([
3140 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003141 'let var = 123',
3142 ], 'E1126: Cannot use :let in Vim9 script')
3143
3144 CheckScriptFailure([
3145 'vim9script',
3146 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003147 ], 'E1016: Cannot declare a global variable:')
3148
3149 CheckScriptFailure([
3150 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003151 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003152 ], 'E1016: Cannot declare a buffer variable:')
3153
3154 CheckScriptFailure([
3155 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003156 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003157 ], 'E1016: Cannot declare a window variable:')
3158
3159 CheckScriptFailure([
3160 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003161 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003162 ], 'E1016: Cannot declare a tab variable:')
3163
3164 CheckScriptFailure([
3165 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003166 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003167 ], 'E1016: Cannot declare a v: variable:')
3168
3169 CheckScriptFailure([
3170 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003171 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003172 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02003173
3174 CheckScriptFailure([
3175 'vim9script',
3176 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003177 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003178 ], 'E108:')
3179
3180 CheckScriptFailure([
3181 'let g:var = 123',
3182 'unlet g:var # something',
3183 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003184
3185 CheckScriptSuccess([
3186 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003187 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003188 ' echo "yes"',
3189 'elseif 2 #comment',
3190 ' echo "no"',
3191 'endif',
3192 ])
3193
3194 CheckScriptFailure([
3195 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003196 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003197 ' echo "yes"',
3198 'endif',
3199 ], 'E15:')
3200
3201 CheckScriptFailure([
3202 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003203 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003204 ' echo "yes"',
3205 'elseif 2#comment',
3206 ' echo "no"',
3207 'endif',
3208 ], 'E15:')
3209
3210 CheckScriptSuccess([
3211 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003212 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003213 ])
3214
3215 CheckScriptFailure([
3216 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003217 'var v = 1# comment6',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003218 ], 'E15:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003219
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003220 CheckScriptSuccess([
3221 'vim9script',
3222 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003223 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003224 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003225 'dsearch /pat/ #comment',
3226 'bwipe!',
3227 ])
3228
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003229 CheckScriptFailure([
3230 'vim9script',
3231 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003232 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003233 ':$',
3234 'dsearch /pat/#comment',
3235 'bwipe!',
3236 ], 'E488:')
3237
3238 CheckScriptFailure([
3239 'vim9script',
3240 'func! SomeFunc()',
3241 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02003242enddef
3243
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003244def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003245 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003246 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02003247 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003248 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02003249 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003250 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02003251 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003252 END
3253 writefile(lines, 'Xfinished')
3254 source Xfinished
3255 assert_equal('two', g:res)
3256
3257 unlet g:res
3258 delete('Xfinished')
3259enddef
3260
Bram Moolenaara5d00772020-05-14 23:20:55 +02003261def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003262 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02003263 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02003264 def GetValue(): string
3265 return theVal
3266 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003267 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02003268 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02003269 theVal = 'else'
3270 g:laterVal = GetValue()
3271 END
3272 writefile(lines, 'Xforward')
3273 source Xforward
3274 assert_equal('something', g:initVal)
3275 assert_equal('else', g:laterVal)
3276
3277 unlet g:initVal
3278 unlet g:laterVal
3279 delete('Xforward')
3280enddef
3281
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003282def Test_source_vim9_from_legacy()
Bram Moolenaara6294952020-12-27 13:39:50 +01003283 var vim9_lines =<< trim END
3284 vim9script
3285 var local = 'local'
3286 g:global = 'global'
3287 export var exported = 'exported'
3288 export def GetText(): string
3289 return 'text'
3290 enddef
3291 END
3292 writefile(vim9_lines, 'Xvim9_script.vim')
3293
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003294 var legacy_lines =<< trim END
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003295 source Xvim9_script.vim
3296
3297 call assert_false(exists('local'))
3298 call assert_false(exists('exported'))
3299 call assert_false(exists('s:exported'))
3300 call assert_equal('global', global)
3301 call assert_equal('global', g:global)
3302
3303 " imported variable becomes script-local
3304 import exported from './Xvim9_script.vim'
3305 call assert_equal('exported', s:exported)
3306 call assert_false(exists('exported'))
3307
3308 " imported function becomes script-local
3309 import GetText from './Xvim9_script.vim'
3310 call assert_equal('text', s:GetText())
3311 call assert_false(exists('*GetText'))
3312 END
3313 writefile(legacy_lines, 'Xlegacy_script.vim')
3314
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003315 source Xlegacy_script.vim
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003316 assert_equal('global', g:global)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003317 unlet g:global
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003318
3319 delete('Xlegacy_script.vim')
3320 delete('Xvim9_script.vim')
3321enddef
Bram Moolenaara5d00772020-05-14 23:20:55 +02003322
Bram Moolenaare535db82021-03-31 21:07:24 +02003323def Test_declare_script_in_func()
3324 var lines =<< trim END
3325 vim9script
3326 func Declare()
3327 let s:local = 123
3328 endfunc
3329 Declare()
3330 assert_equal(123, local)
3331
3332 var error: string
3333 try
3334 local = 'asdf'
3335 catch
3336 error = v:exception
3337 endtry
3338 assert_match('E1012: Type mismatch; expected number but got string', error)
3339
3340 lockvar local
3341 try
3342 local = 999
3343 catch
3344 error = v:exception
3345 endtry
3346 assert_match('E741: Value is locked: local', error)
3347 END
3348 CheckScriptSuccess(lines)
3349enddef
3350
3351
Bram Moolenaar7d699702020-08-14 20:52:28 +02003352func Test_vim9script_not_global()
3353 " check that items defined in Vim9 script are script-local, not global
3354 let vim9lines =<< trim END
3355 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003356 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02003357 func TheFunc()
3358 echo 'local'
3359 endfunc
3360 def DefFunc()
3361 echo 'local'
3362 enddef
3363 END
3364 call writefile(vim9lines, 'Xvim9script.vim')
3365 source Xvim9script.vim
3366 try
3367 echo g:var
3368 assert_report('did not fail')
3369 catch /E121:/
3370 " caught
3371 endtry
3372 try
3373 call TheFunc()
3374 assert_report('did not fail')
3375 catch /E117:/
3376 " caught
3377 endtry
3378 try
3379 call DefFunc()
3380 assert_report('did not fail')
3381 catch /E117:/
3382 " caught
3383 endtry
3384
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003385 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02003386endfunc
3387
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02003388def Test_vim9_copen()
3389 # this was giving an error for setting w:quickfix_title
3390 copen
3391 quit
3392enddef
3393
Bram Moolenaar03290b82020-12-19 16:30:44 +01003394" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003395def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003396 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003397 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01003398 def some#gettest(): string
3399 return 'test'
3400 enddef
3401 g:some#name = 'name'
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003402 g:some#dict = {key: 'value'}
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003403
3404 def some#varargs(a1: string, ...l: list<string>): string
3405 return a1 .. l[0] .. l[1]
3406 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01003407 END
3408
3409 mkdir('Xdir/autoload', 'p')
3410 writefile(lines, 'Xdir/autoload/some.vim')
3411 var save_rtp = &rtp
3412 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3413
3414 assert_equal('test', g:some#gettest())
3415 assert_equal('name', g:some#name)
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003416 assert_equal('value', g:some#dict.key)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003417 g:some#other = 'other'
3418 assert_equal('other', g:some#other)
3419
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003420 assert_equal('abc', some#varargs('a', 'b', 'c'))
3421
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003422 # upper case script name works
3423 lines =<< trim END
3424 vim9script
3425 def Other#getOther(): string
3426 return 'other'
3427 enddef
3428 END
3429 writefile(lines, 'Xdir/autoload/Other.vim')
3430 assert_equal('other', g:Other#getOther())
3431
Bram Moolenaar03290b82020-12-19 16:30:44 +01003432 delete('Xdir', 'rf')
3433 &rtp = save_rtp
3434enddef
3435
3436" test using a vim9script that is auto-loaded from an autocmd
3437def Test_vim9_aucmd_autoload()
3438 var lines =<< trim END
3439 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003440 def foo#test()
3441 echomsg getreg('"')
3442 enddef
3443 END
3444
3445 mkdir('Xdir/autoload', 'p')
3446 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003447 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003448 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3449 augroup test
3450 autocmd TextYankPost * call foo#test()
3451 augroup END
3452
3453 normal Y
3454
3455 augroup test
3456 autocmd!
3457 augroup END
3458 delete('Xdir', 'rf')
3459 &rtp = save_rtp
3460enddef
3461
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003462" This was causing a crash because suppress_errthrow wasn't reset.
3463def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003464 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003465 vim9script
3466 def crash#func()
3467 try
3468 for x in List()
3469 endfor
3470 catch
3471 endtry
3472 g:ok = true
3473 enddef
3474 fu List()
3475 invalid
3476 endfu
3477 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01003478 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003479 catch /wontmatch/
3480 endtry
3481 END
3482 call mkdir('Xruntime/autoload', 'p')
3483 call writefile(lines, 'Xruntime/autoload/crash.vim')
3484
3485 # run in a separate Vim to avoid the side effects of assert_fails()
3486 lines =<< trim END
3487 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
3488 call crash#func()
3489 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003490 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003491 END
3492 writefile(lines, 'Xscript')
3493 RunVim([], [], '-S Xscript')
3494 assert_equal(['ok'], readfile('Xdidit'))
3495
3496 delete('Xdidit')
3497 delete('Xscript')
3498 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01003499
3500 lines =<< trim END
3501 vim9script
3502 var foo#bar = 'asdf'
3503 END
3504 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003505enddef
3506
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003507def Test_script_var_in_autocmd()
3508 # using a script variable from an autocommand, defined in a :def function in a
3509 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003510 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003511 let s:counter = 1
3512 def s:Func()
3513 au! CursorHold
3514 au CursorHold * s:counter += 1
3515 enddef
3516 call s:Func()
3517 doau CursorHold
3518 call assert_equal(2, s:counter)
3519 au! CursorHold
3520 END
3521 CheckScriptSuccess(lines)
3522enddef
3523
Bram Moolenaar3896a102020-08-09 14:33:55 +02003524def Test_cmdline_win()
3525 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
3526 # the command line window.
3527 mkdir('rtp/syntax', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003528 var export_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02003529 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003530 export var That = 'yes'
Bram Moolenaar3896a102020-08-09 14:33:55 +02003531 END
3532 writefile(export_lines, 'rtp/syntax/Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003533 var import_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02003534 vim9script
3535 import That from './Xexport.vim'
3536 END
3537 writefile(import_lines, 'rtp/syntax/vim.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003538 var save_rtp = &rtp
Bram Moolenaar3896a102020-08-09 14:33:55 +02003539 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
3540 syntax on
3541 augroup CmdWin
3542 autocmd CmdwinEnter * g:got_there = 'yes'
3543 augroup END
3544 # this will open and also close the cmdline window
3545 feedkeys('q:', 'xt')
3546 assert_equal('yes', g:got_there)
3547
3548 augroup CmdWin
3549 au!
3550 augroup END
3551 &rtp = save_rtp
3552 delete('rtp', 'rf')
3553enddef
3554
Bram Moolenaare3d46852020-08-29 13:39:17 +02003555def Test_invalid_sid()
3556 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003557
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003558 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02003559 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02003560 endif
3561 delete('Xdidit')
3562enddef
3563
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003564def Test_restoring_cpo()
3565 writefile(['vim9script', 'set nocp'], 'Xsourced')
3566 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
3567 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
3568 assert_equal(['done'], readfile('Xdone'))
3569 endif
3570 delete('Xsourced')
3571 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01003572 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01003573
3574 writefile(['vim9script'], 'XanotherScript')
3575 set cpo=aABceFsMny>
3576 edit XanotherScript
3577 so %
3578 assert_equal('aABceFsMny>', &cpo)
3579 :1del
3580 w
3581 so %
3582 assert_equal('aABceFsMny>', &cpo)
3583
3584 delete('XanotherScript')
3585 set cpo&vim
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003586enddef
3587
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003588" Use :function so we can use Check commands
3589func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003590 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003591 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003592
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003593 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003594 vim9script
3595 def script#func()
3596 enddef
3597 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003598 call mkdir('Xdir/autoload', 'p')
3599 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003600
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003601 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003602 vim9script
3603 set cpo+=M
3604 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02003605 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003606 setline(1, 'some text')
3607 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003608 call writefile(lines, 'XTest_redraw_cpo')
3609 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
3610 call term_sendkeys(buf, "V:")
3611 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003612
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003613 " clean up
3614 call term_sendkeys(buf, "\<Esc>u")
3615 call StopVimInTerminal(buf)
3616 call delete('XTest_redraw_cpo')
3617 call delete('Xdir', 'rf')
3618endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003619
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003620
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003621def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003622 var lines =<< trim END
3623 var name: any
3624 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003625 END
3626 CheckDefAndScriptSuccess(lines)
3627enddef
3628
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003629func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02003630 CheckRunVimInTerminal
3631
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003632 " call indirectly to avoid compilation error for missing functions
3633 call Run_Test_define_func_at_command_line()
3634endfunc
3635
3636def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003637 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003638 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003639 func CheckAndQuit()
3640 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
3641 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
3642 endfunc
3643 END
3644 writefile([''], 'Xdidcmd')
3645 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01003646 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003647 # define Afunc() on the command line
3648 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
3649 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003650 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003651
3652 call StopVimInTerminal(buf)
3653 delete('XcallFunc')
3654 delete('Xdidcmd')
3655enddef
3656
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003657def Test_script_var_scope()
3658 var lines =<< trim END
3659 vim9script
3660 if true
3661 if true
3662 var one = 'one'
3663 echo one
3664 endif
3665 echo one
3666 endif
3667 END
3668 CheckScriptFailure(lines, 'E121:', 7)
3669
3670 lines =<< trim END
3671 vim9script
3672 if true
3673 if false
3674 var one = 'one'
3675 echo one
3676 else
3677 var one = 'one'
3678 echo one
3679 endif
3680 echo one
3681 endif
3682 END
3683 CheckScriptFailure(lines, 'E121:', 10)
3684
3685 lines =<< trim END
3686 vim9script
3687 while true
3688 var one = 'one'
3689 echo one
3690 break
3691 endwhile
3692 echo one
3693 END
3694 CheckScriptFailure(lines, 'E121:', 7)
3695
3696 lines =<< trim END
3697 vim9script
3698 for i in range(1)
3699 var one = 'one'
3700 echo one
3701 endfor
3702 echo one
3703 END
3704 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003705
3706 lines =<< trim END
3707 vim9script
3708 {
3709 var one = 'one'
3710 assert_equal('one', one)
3711 }
3712 assert_false(exists('one'))
3713 assert_false(exists('s:one'))
3714 END
3715 CheckScriptSuccess(lines)
3716
3717 lines =<< trim END
3718 vim9script
3719 {
3720 var one = 'one'
3721 echo one
3722 }
3723 echo one
3724 END
3725 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003726enddef
3727
Bram Moolenaar352134b2020-10-17 22:04:08 +02003728def Test_catch_exception_in_callback()
3729 var lines =<< trim END
3730 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02003731 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02003732 try
3733 var x: string
3734 var y: string
3735 # this error should be caught with CHECKLEN
3736 [x, y] = ['']
3737 catch
3738 g:caught = 'yes'
3739 endtry
3740 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01003741 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02003742 feedkeys("\r", 'xt')
3743 END
3744 CheckScriptSuccess(lines)
3745
3746 unlet g:caught
3747enddef
3748
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003749def Test_no_unknown_error_after_error()
3750 if !has('unix') || !has('job')
3751 throw 'Skipped: not unix of missing +job feature'
3752 endif
3753 var lines =<< trim END
3754 vim9script
3755 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02003756 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003757 eval [][0]
3758 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02003759 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003760 sleep 1m
3761 source += l
3762 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01003763 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01003764 while job_status(myjob) == 'run'
3765 sleep 10m
3766 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01003767 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02003768 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003769 END
3770 writefile(lines, 'Xdef')
3771 assert_fails('so Xdef', ['E684:', 'E1012:'])
3772 delete('Xdef')
3773enddef
3774
Bram Moolenaar4324d872020-12-01 20:12:24 +01003775def InvokeNormal()
3776 exe "norm! :m+1\r"
3777enddef
3778
3779def Test_invoke_normal_in_visual_mode()
3780 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
3781 new
3782 setline(1, ['aaa', 'bbb'])
3783 feedkeys("V\<F3>", 'xt')
3784 assert_equal(['bbb', 'aaa'], getline(1, 2))
3785 xunmap <F3>
3786enddef
3787
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003788def Test_white_space_after_command()
3789 var lines =<< trim END
3790 exit_cb: Func})
3791 END
3792 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003793
3794 lines =<< trim END
3795 e#
3796 END
3797 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003798enddef
3799
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003800def Test_script_var_gone_when_sourced_twice()
3801 var lines =<< trim END
3802 vim9script
3803 if exists('g:guard')
3804 finish
3805 endif
3806 g:guard = 1
3807 var name = 'thename'
3808 def g:GetName(): string
3809 return name
3810 enddef
3811 def g:SetName(arg: string)
3812 name = arg
3813 enddef
3814 END
3815 writefile(lines, 'XscriptTwice.vim')
3816 so XscriptTwice.vim
3817 assert_equal('thename', g:GetName())
3818 g:SetName('newname')
3819 assert_equal('newname', g:GetName())
3820 so XscriptTwice.vim
3821 assert_fails('call g:GetName()', 'E1149:')
3822 assert_fails('call g:SetName("x")', 'E1149:')
3823
3824 delfunc g:GetName
3825 delfunc g:SetName
3826 delete('XscriptTwice.vim')
3827 unlet g:guard
3828enddef
3829
3830def Test_import_gone_when_sourced_twice()
3831 var exportlines =<< trim END
3832 vim9script
3833 if exists('g:guard')
3834 finish
3835 endif
3836 g:guard = 1
3837 export var name = 'someName'
3838 END
3839 writefile(exportlines, 'XexportScript.vim')
3840
3841 var lines =<< trim END
3842 vim9script
3843 import name from './XexportScript.vim'
3844 def g:GetName(): string
3845 return name
3846 enddef
3847 END
3848 writefile(lines, 'XscriptImport.vim')
3849 so XscriptImport.vim
3850 assert_equal('someName', g:GetName())
3851
3852 so XexportScript.vim
3853 assert_fails('call g:GetName()', 'E1149:')
3854
3855 delfunc g:GetName
3856 delete('XexportScript.vim')
3857 delete('XscriptImport.vim')
3858 unlet g:guard
3859enddef
3860
Bram Moolenaar10b94212021-02-19 21:42:57 +01003861def Test_unsupported_commands()
3862 var lines =<< trim END
3863 ka
3864 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003865 CheckDefFailure(lines, 'E476:')
3866 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaar10b94212021-02-19 21:42:57 +01003867
3868 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01003869 :1ka
3870 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003871 CheckDefFailure(lines, 'E476:')
3872 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaarada1d872021-02-20 08:16:51 +01003873
3874 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01003875 t
3876 END
3877 CheckDefFailure(lines, 'E1100:')
3878 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
3879
3880 lines =<< trim END
3881 x
3882 END
3883 CheckDefFailure(lines, 'E1100:')
3884 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
3885
3886 lines =<< trim END
3887 xit
3888 END
3889 CheckDefFailure(lines, 'E1100:')
3890 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
3891enddef
3892
Bram Moolenaarc70fe462021-04-17 17:59:19 +02003893def Test_mapping_line_number()
3894 var lines =<< trim END
3895 vim9script
3896 def g:FuncA()
3897 # Some comment
3898 FuncB(0)
3899 enddef
3900 # Some comment
3901 def FuncB(
3902 # Some comment
3903 n: number
3904 )
3905 exe 'nno '
3906 # Some comment
3907 .. '<F3> a'
3908 .. 'b'
3909 .. 'c'
3910 enddef
3911 END
3912 CheckScriptSuccess(lines)
3913 var res = execute('verbose nmap <F3>')
3914 assert_match('No mapping found', res)
3915
3916 g:FuncA()
3917 res = execute('verbose nmap <F3>')
3918 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
3919
3920 nunmap <F3>
3921 delfunc g:FuncA
3922enddef
3923
Bram Moolenaar585fea72020-04-02 22:33:21 +02003924" Keep this last, it messes up highlighting.
3925def Test_substitute_cmd()
3926 new
3927 setline(1, 'something')
3928 :substitute(some(other(
3929 assert_equal('otherthing', getline(1))
3930 bwipe!
3931
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003932 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003933 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02003934 vim9script
3935 new
3936 setline(1, 'something')
3937 :substitute(some(other(
3938 assert_equal('otherthing', getline(1))
3939 bwipe!
3940 END
3941 writefile(lines, 'Xvim9lines')
3942 source Xvim9lines
3943
3944 delete('Xvim9lines')
3945enddef
3946
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker