blob: 40cdfa39c17e493a0b27cd4d7ad5432fbf99d390 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001" Test various aspects of the Vim9 script language.
2
Bram Moolenaar673660a2020-01-26 16:50:05 +01003source check.vim
Bram Moolenaar101f4812020-06-16 23:18:51 +02004source term_util.vim
Bram Moolenaarad39c092020-02-26 18:23:43 +01005source view_util.vim
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006source vim9.vim
Bram Moolenaare3d46852020-08-29 13:39:17 +02007source shared.vim
Bram Moolenaar37294bd2021-03-10 13:40:08 +01008source screendump.vim
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010def Test_range_only()
11 new
12 setline(1, ['blah', 'Blah'])
13 :/Blah/
14 assert_equal(2, getcurpos()[1])
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020015 bwipe!
16
17 # without range commands use current line
18 new
19 setline(1, ['one', 'two', 'three'])
20 :2
21 print
22 assert_equal('two', Screenline(&lines))
23 :3
24 list
25 assert_equal('three$', Screenline(&lines))
Bram Moolenaarb8554302021-02-15 21:30:30 +010026
27 # missing command does not print the line
28 var lines =<< trim END
29 vim9script
30 :1|
31 assert_equal('three$', Screenline(&lines))
32 :|
33 assert_equal('three$', Screenline(&lines))
34 END
35 CheckScriptSuccess(lines)
36
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020037 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +010038
39 # won't generate anything
40 if false
41 :123
42 endif
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020043enddef
44
Bram Moolenaara6e67e42020-05-15 23:36:40 +020045let g:alist = [7]
46let g:astring = 'text'
Bram Moolenaarf0b9f432020-07-17 23:03:17 +020047let g:anumber = 123
Bram Moolenaar6e587dc2020-02-06 13:15:52 +010048
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020049def Test_delfunction()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020050 # Check function is defined in script namespace
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020051 CheckScriptSuccess([
52 'vim9script',
53 'func CheckMe()',
54 ' return 123',
55 'endfunc',
56 'assert_equal(123, s:CheckMe())',
57 ])
58
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020059 # Check function in script namespace cannot be deleted
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020060 CheckScriptFailure([
61 'vim9script',
62 'func DeleteMe1()',
63 'endfunc',
64 'delfunction DeleteMe1',
65 ], 'E1084:')
66 CheckScriptFailure([
67 'vim9script',
68 'func DeleteMe2()',
69 'endfunc',
70 'def DoThat()',
71 ' delfunction DeleteMe2',
72 'enddef',
73 'DoThat()',
74 ], 'E1084:')
75 CheckScriptFailure([
76 'vim9script',
77 'def DeleteMe3()',
78 'enddef',
79 'delfunction DeleteMe3',
80 ], 'E1084:')
81 CheckScriptFailure([
82 'vim9script',
83 'def DeleteMe4()',
84 'enddef',
85 'def DoThat()',
86 ' delfunction DeleteMe4',
87 'enddef',
88 'DoThat()',
89 ], 'E1084:')
Bram Moolenaar925e9fd2020-07-25 15:41:11 +020090
91 # Check that global :def function can be replaced and deleted
Bram Moolenaarcfcd0112020-09-27 15:19:27 +020092 var lines =<< trim END
Bram Moolenaar925e9fd2020-07-25 15:41:11 +020093 vim9script
94 def g:Global(): string
95 return "yes"
96 enddef
97 assert_equal("yes", g:Global())
98 def! g:Global(): string
99 return "no"
100 enddef
101 assert_equal("no", g:Global())
102 delfunc g:Global
103 assert_false(exists('*g:Global'))
104 END
105 CheckScriptSuccess(lines)
106
107 # Check that global function can be replaced by a :def function and deleted
108 lines =<< trim END
109 vim9script
110 func g:Global()
111 return "yes"
112 endfunc
113 assert_equal("yes", g:Global())
114 def! g:Global(): string
115 return "no"
116 enddef
117 assert_equal("no", g:Global())
118 delfunc g:Global
119 assert_false(exists('*g:Global'))
120 END
121 CheckScriptSuccess(lines)
122
123 # Check that global :def function can be replaced by a function and deleted
124 lines =<< trim END
125 vim9script
126 def g:Global(): string
127 return "yes"
128 enddef
129 assert_equal("yes", g:Global())
130 func! g:Global()
131 return "no"
132 endfunc
133 assert_equal("no", g:Global())
134 delfunc g:Global
135 assert_false(exists('*g:Global'))
136 END
137 CheckScriptSuccess(lines)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200138enddef
139
Bram Moolenaar08052222020-09-14 17:04:31 +0200140def Test_wrong_type()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200141 CheckDefFailure(['var name: list<nothing>'], 'E1010:')
142 CheckDefFailure(['var name: list<list<nothing>>'], 'E1010:')
143 CheckDefFailure(['var name: dict<nothing>'], 'E1010:')
144 CheckDefFailure(['var name: dict<dict<nothing>>'], 'E1010:')
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100145
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200146 CheckDefFailure(['var name: dict<number'], 'E1009:')
147 CheckDefFailure(['var name: dict<list<number>'], 'E1009:')
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100148
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200149 CheckDefFailure(['var name: ally'], 'E1010:')
150 CheckDefFailure(['var name: bram'], 'E1010:')
151 CheckDefFailure(['var name: cathy'], 'E1010:')
152 CheckDefFailure(['var name: dom'], 'E1010:')
153 CheckDefFailure(['var name: freddy'], 'E1010:')
154 CheckDefFailure(['var name: john'], 'E1010:')
155 CheckDefFailure(['var name: larry'], 'E1010:')
156 CheckDefFailure(['var name: ned'], 'E1010:')
157 CheckDefFailure(['var name: pam'], 'E1010:')
158 CheckDefFailure(['var name: sam'], 'E1010:')
159 CheckDefFailure(['var name: vim'], 'E1010:')
Bram Moolenaara0a9f432020-04-28 21:29:34 +0200160
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200161 CheckDefFailure(['var Ref: number', 'Ref()'], 'E1085:')
162 CheckDefFailure(['var Ref: string', 'var res = Ref()'], 'E1085:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200163enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaar10c65862020-10-08 21:16:42 +0200165def Test_script_wrong_type()
166 var lines =<< trim END
167 vim9script
168 var s:dict: dict<string>
169 s:dict['a'] = ['x']
170 END
171 CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got list<string>', 3)
172enddef
173
Bram Moolenaar08052222020-09-14 17:04:31 +0200174def Test_const()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200175 CheckDefFailure(['final name = 234', 'name = 99'], 'E1018:')
176 CheckDefFailure(['final one = 234', 'var one = 99'], 'E1017:')
177 CheckDefFailure(['final list = [1, 2]', 'var list = [3, 4]'], 'E1017:')
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200178 CheckDefFailure(['final two'], 'E1125:')
179 CheckDefFailure(['final &option'], 'E996:')
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200180
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200181 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200182 final list = [1, 2, 3]
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200183 list[0] = 4
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200184 list->assert_equal([4, 2, 3])
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200185 const other = [5, 6, 7]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200186 other->assert_equal([5, 6, 7])
Bram Moolenaar71abe482020-09-14 22:28:30 +0200187
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200188 var varlist = [7, 8]
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200189 const constlist = [1, varlist, 3]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200190 varlist[0] = 77
191 # TODO: does not work yet
192 # constlist[1][1] = 88
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200193 var cl = constlist[1]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200194 cl[1] = 88
195 constlist->assert_equal([1, [77, 88], 3])
196
Bram Moolenaare0de1712020-12-02 17:36:54 +0100197 var vardict = {five: 5, six: 6}
198 const constdict = {one: 1, two: vardict, three: 3}
Bram Moolenaar71abe482020-09-14 22:28:30 +0200199 vardict['five'] = 55
200 # TODO: does not work yet
201 # constdict['two']['six'] = 66
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200202 var cd = constdict['two']
Bram Moolenaar71abe482020-09-14 22:28:30 +0200203 cd['six'] = 66
Bram Moolenaare0de1712020-12-02 17:36:54 +0100204 constdict->assert_equal({one: 1, two: {five: 55, six: 66}, three: 3})
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200205 END
206 CheckDefAndScriptSuccess(lines)
Bram Moolenaar08052222020-09-14 17:04:31 +0200207enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200209def Test_const_bang()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200210 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200211 const var = 234
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200212 var = 99
213 END
214 CheckDefExecFailure(lines, 'E1018:', 2)
215 CheckScriptFailure(['vim9script'] + lines, 'E46:', 3)
216
217 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200218 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200219 ll[0] = 99
220 END
221 CheckDefExecFailure(lines, 'E1119:', 2)
222 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
223
224 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200225 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200226 ll[3] = 99
227 END
228 CheckDefExecFailure(lines, 'E1118:', 2)
229 CheckScriptFailure(['vim9script'] + lines, 'E684:', 3)
230
231 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100232 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200233 dd["one"] = 99
234 END
235 CheckDefExecFailure(lines, 'E1121:', 2)
236 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
237
238 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100239 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200240 dd["three"] = 99
241 END
242 CheckDefExecFailure(lines, 'E1120:')
243 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
244enddef
245
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200246def Test_range_no_colon()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200247 CheckDefFailure(['%s/a/b/'], 'E1050:')
248 CheckDefFailure(['+ s/a/b/'], 'E1050:')
249 CheckDefFailure(['- s/a/b/'], 'E1050:')
250 CheckDefFailure(['. s/a/b/'], 'E1050:')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200251enddef
252
253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254def Test_block()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200255 var outer = 1
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 {
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200257 var inner = 2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258 assert_equal(1, outer)
259 assert_equal(2, inner)
260 }
261 assert_equal(1, outer)
Bram Moolenaar3f1e9f02021-02-27 22:36:43 +0100262
263 {|echo 'yes'|}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264enddef
265
Bram Moolenaar08052222020-09-14 17:04:31 +0200266def Test_block_failure()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200267 CheckDefFailure(['{', 'var inner = 1', '}', 'echo inner'], 'E1001:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200268 CheckDefFailure(['}'], 'E1025:')
269 CheckDefFailure(['{', 'echo 1'], 'E1026:')
270enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200272def Test_block_local_vars()
273 var lines =<< trim END
274 vim9script
Bram Moolenaared234f22020-10-15 20:42:20 +0200275 v:testing = 1
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200276 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200277 var text = ['hello']
278 def SayHello(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200279 return text
280 enddef
281 def SetText(v: string)
Bram Moolenaared234f22020-10-15 20:42:20 +0200282 text = [v]
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200283 enddef
284 endif
285
286 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200287 var text = ['again']
288 def SayAgain(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200289 return text
290 enddef
291 endif
Bram Moolenaared234f22020-10-15 20:42:20 +0200292
293 # test that the "text" variables are not cleaned up
294 test_garbagecollect_now()
295
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200296 defcompile
297
Bram Moolenaared234f22020-10-15 20:42:20 +0200298 assert_equal(['hello'], SayHello())
299 assert_equal(['again'], SayAgain())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200300
301 SetText('foobar')
Bram Moolenaared234f22020-10-15 20:42:20 +0200302 assert_equal(['foobar'], SayHello())
303
304 call writefile(['ok'], 'Xdidit')
305 qall!
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200306 END
Bram Moolenaared234f22020-10-15 20:42:20 +0200307
308 # need to execute this with a separate Vim instance to avoid the current
309 # context gets garbage collected.
310 writefile(lines, 'Xscript')
311 RunVim([], [], '-S Xscript')
312 assert_equal(['ok'], readfile('Xdidit'))
313
314 delete('Xscript')
315 delete('Xdidit')
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200316enddef
317
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200318def Test_block_local_vars_with_func()
319 var lines =<< trim END
320 vim9script
321 if true
322 var foo = 'foo'
323 if true
324 var bar = 'bar'
325 def Func(): list<string>
326 return [foo, bar]
327 enddef
328 endif
329 endif
330 # function is compiled here, after blocks have finished, can still access
331 # "foo" and "bar"
332 assert_equal(['foo', 'bar'], Func())
333 END
334 CheckScriptSuccess(lines)
335enddef
336
Bram Moolenaard032f342020-07-18 18:13:02 +0200337func g:NoSuchFunc()
338 echo 'none'
339endfunc
340
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100341def Test_try_catch_throw()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200342 var l = []
Bram Moolenaar7a092242020-04-16 22:10:49 +0200343 try # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344 add(l, '1')
345 throw 'wrong'
346 add(l, '2')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200347 catch # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100348 add(l, v:exception)
Bram Moolenaar7a092242020-04-16 22:10:49 +0200349 finally # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100350 add(l, '3')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200351 endtry # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100352 assert_equal(['1', 'wrong', '3'], l)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200353
Bram Moolenaare8593122020-07-18 15:17:02 +0200354 l = []
355 try
356 try
357 add(l, '1')
358 throw 'wrong'
359 add(l, '2')
360 catch /right/
361 add(l, v:exception)
362 endtry
363 catch /wrong/
364 add(l, 'caught')
Bram Moolenaar373863e2020-09-26 17:20:53 +0200365 fina
Bram Moolenaare8593122020-07-18 15:17:02 +0200366 add(l, 'finally')
367 endtry
368 assert_equal(['1', 'caught', 'finally'], l)
369
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200370 var n: number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200371 try
372 n = l[3]
373 catch /E684:/
374 n = 99
375 endtry
376 assert_equal(99, n)
377
Bram Moolenaar69f70502021-01-01 16:10:46 +0100378 var done = 'no'
379 if 0
380 try | catch | endtry
381 else
382 done = 'yes'
383 endif
384 assert_equal('yes', done)
385
386 done = 'no'
387 if 1
388 done = 'yes'
389 else
390 try | catch | endtry
391 done = 'never'
392 endif
393 assert_equal('yes', done)
394
395 if 1
396 else
397 try | catch /pat/ | endtry
398 try | catch /pat/
399 endtry
400 try
401 catch /pat/ | endtry
402 try
403 catch /pat/
404 endtry
405 endif
406
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200407 try
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200408 # string slice returns a string, not a number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200409 n = g:astring[3]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200410 catch /E1012:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200411 n = 77
412 endtry
413 assert_equal(77, n)
414
415 try
416 n = l[g:astring]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200417 catch /E1012:/
Bram Moolenaar56acb092020-08-16 14:48:19 +0200418 n = 88
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200419 endtry
Bram Moolenaar56acb092020-08-16 14:48:19 +0200420 assert_equal(88, n)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200421
422 try
423 n = s:does_not_exist
424 catch /E121:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200425 n = 111
426 endtry
427 assert_equal(111, n)
428
429 try
430 n = g:does_not_exist
431 catch /E121:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200432 n = 121
433 endtry
434 assert_equal(121, n)
435
Bram Moolenaare0de1712020-12-02 17:36:54 +0100436 var d = {one: 1}
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200437 try
438 n = d[g:astring]
439 catch /E716:/
440 n = 222
441 endtry
442 assert_equal(222, n)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200443
444 try
445 n = -g:astring
446 catch /E39:/
447 n = 233
448 endtry
449 assert_equal(233, n)
450
451 try
452 n = +g:astring
453 catch /E1030:/
454 n = 244
455 endtry
456 assert_equal(244, n)
457
458 try
459 n = +g:alist
460 catch /E745:/
461 n = 255
462 endtry
463 assert_equal(255, n)
464
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200465 var nd: dict<any>
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200466 try
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100467 nd = {[g:alist]: 1}
468 catch /E1105:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200469 n = 266
470 endtry
471 assert_equal(266, n)
472
473 try
474 [n] = [1, 2, 3]
475 catch /E1093:/
476 n = 277
477 endtry
478 assert_equal(277, n)
479
Bram Moolenaare8593122020-07-18 15:17:02 +0200480 try
481 &ts = g:astring
Bram Moolenaar5e654232020-09-16 15:22:00 +0200482 catch /E1012:/
Bram Moolenaare8593122020-07-18 15:17:02 +0200483 n = 288
484 endtry
485 assert_equal(288, n)
486
487 try
488 &backspace = 'asdf'
489 catch /E474:/
490 n = 299
491 endtry
492 assert_equal(299, n)
493
494 l = [1]
495 try
496 l[3] = 3
497 catch /E684:/
498 n = 300
499 endtry
500 assert_equal(300, n)
501
502 try
Bram Moolenaare8593122020-07-18 15:17:02 +0200503 unlet g:does_not_exist
504 catch /E108:/
505 n = 322
506 endtry
507 assert_equal(322, n)
508
509 try
Bram Moolenaar2bede172020-11-19 18:53:18 +0100510 d = {text: 1, [g:astring]: 2}
Bram Moolenaare8593122020-07-18 15:17:02 +0200511 catch /E721:/
512 n = 333
513 endtry
514 assert_equal(333, n)
515
516 try
517 l = DeletedFunc()
518 catch /E933:/
519 n = 344
520 endtry
521 assert_equal(344, n)
Bram Moolenaard032f342020-07-18 18:13:02 +0200522
523 try
524 echo len(v:true)
525 catch /E701:/
526 n = 355
527 endtry
528 assert_equal(355, n)
529
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200530 var P = function('g:NoSuchFunc')
Bram Moolenaard032f342020-07-18 18:13:02 +0200531 delfunc g:NoSuchFunc
532 try
533 echo P()
534 catch /E117:/
535 n = 366
536 endtry
537 assert_equal(366, n)
538
539 try
540 echo g:NoSuchFunc()
541 catch /E117:/
542 n = 377
543 endtry
544 assert_equal(377, n)
545
546 try
547 echo g:alist + 4
548 catch /E745:/
549 n = 388
550 endtry
551 assert_equal(388, n)
552
553 try
554 echo 4 + g:alist
555 catch /E745:/
556 n = 399
557 endtry
558 assert_equal(399, n)
559
560 try
561 echo g:alist.member
562 catch /E715:/
563 n = 400
564 endtry
565 assert_equal(400, n)
566
567 try
568 echo d.member
569 catch /E716:/
570 n = 411
571 endtry
572 assert_equal(411, n)
Bram Moolenaard9d77892021-02-12 21:32:47 +0100573
574 var counter = 0
575 for i in range(4)
576 try
577 eval [][0]
578 catch
579 endtry
580 counter += 1
581 endfor
582 assert_equal(4, counter)
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100583
584 # return in finally after empty catch
585 def ReturnInFinally(): number
586 try
587 finally
588 return 4
589 endtry
590 return 2
591 enddef
592 assert_equal(4, ReturnInFinally())
Bram Moolenaar8ac681a2021-06-15 20:06:34 +0200593
594 var lines =<< trim END
595 vim9script
596 try
597 acos('0.5')
598 ->setline(1)
599 catch
600 g:caught = v:exception
601 endtry
602 END
603 CheckScriptSuccess(lines)
604 assert_match('E808: Number or Float required', g:caught)
605 unlet g:caught
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606enddef
607
Bram Moolenaar3f987b52021-06-30 12:02:24 +0200608def Test_try_in_catch()
609 var lines =<< trim END
610 vim9script
611 var seq = []
612 def DoIt()
613 try
614 seq->add('throw 1')
615 eval [][0]
616 seq->add('notreached')
617 catch
618 seq->add('catch')
619 try
620 seq->add('throw 2')
621 eval [][0]
622 seq->add('notreached')
623 catch /nothing/
624 seq->add('notreached')
625 endtry
626 seq->add('done')
627 endtry
628 enddef
629 DoIt()
630 assert_equal(['throw 1', 'catch', 'throw 2', 'done'], seq)
631 END
632enddef
633
Bram Moolenaar2e34c342021-03-14 12:13:33 +0100634" :while at the very start of a function that :continue jumps to
635def TryContinueFunc()
636 while g:Count < 2
637 g:sequence ..= 't'
638 try
639 echoerr 'Test'
640 catch
641 g:Count += 1
642 g:sequence ..= 'c'
643 continue
644 endtry
645 g:sequence ..= 'e'
646 g:Count += 1
647 endwhile
648enddef
649
650def Test_continue_in_try_in_while()
651 g:Count = 0
652 g:sequence = ''
653 TryContinueFunc()
654 assert_equal('tctc', g:sequence)
655 unlet g:Count
656 unlet g:sequence
657enddef
658
Bram Moolenaar9cb577a2021-02-22 22:45:10 +0100659def Test_nocatch_return_in_try()
660 # return in try block returns normally
661 def ReturnInTry(): string
662 try
663 return '"some message"'
664 catch
665 endtry
666 return 'not reached'
667 enddef
668 exe 'echoerr ' .. ReturnInTry()
669enddef
670
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100671def Test_cnext_works_in_catch()
672 var lines =<< trim END
673 vim9script
674 au BufEnter * eval 0
675 writefile(['text'], 'Xfile1')
676 writefile(['text'], 'Xfile2')
677 var items = [
678 {lnum: 1, filename: 'Xfile1', valid: true},
679 {lnum: 1, filename: 'Xfile2', valid: true}
680 ]
681 setqflist([], ' ', {items: items})
682 cwindow
683
684 def CnextOrCfirst()
685 # if cnext fails, cfirst is used
686 try
687 cnext
688 catch
689 cfirst
690 endtry
691 enddef
692
693 CnextOrCfirst()
694 CnextOrCfirst()
695 writefile([getqflist({idx: 0}).idx], 'Xresult')
696 qall
697 END
698 writefile(lines, 'XCatchCnext')
699 RunVim([], [], '--clean -S XCatchCnext')
700 assert_equal(['1'], readfile('Xresult'))
701
702 delete('Xfile1')
703 delete('Xfile2')
704 delete('XCatchCnext')
705 delete('Xresult')
706enddef
707
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100708def Test_throw_skipped()
709 if 0
710 throw dontgethere
711 endif
712enddef
713
Bram Moolenaar8f81b222021-01-14 21:47:06 +0100714def Test_nocatch_throw_silenced()
715 var lines =<< trim END
716 vim9script
717 def Func()
718 throw 'error'
719 enddef
720 silent! Func()
721 END
722 writefile(lines, 'XthrowSilenced')
723 source XthrowSilenced
724 delete('XthrowSilenced')
725enddef
726
Bram Moolenaare8593122020-07-18 15:17:02 +0200727def DeletedFunc(): list<any>
728 return ['delete me']
729enddef
730defcompile
731delfunc DeletedFunc
732
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100733def ThrowFromDef()
Bram Moolenaara72cfb82020-04-23 17:07:30 +0200734 throw "getout" # comment
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100735enddef
736
737func CatchInFunc()
738 try
739 call ThrowFromDef()
740 catch
741 let g:thrown_func = v:exception
742 endtry
743endfunc
744
745def CatchInDef()
746 try
747 ThrowFromDef()
748 catch
749 g:thrown_def = v:exception
750 endtry
751enddef
752
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100753def ReturnFinally(): string
754 try
755 return 'intry'
Bram Moolenaar373863e2020-09-26 17:20:53 +0200756 finall
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100757 g:in_finally = 'finally'
758 endtry
759 return 'end'
760enddef
761
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100762def Test_try_catch_nested()
763 CatchInFunc()
764 assert_equal('getout', g:thrown_func)
765
766 CatchInDef()
767 assert_equal('getout', g:thrown_def)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100768
769 assert_equal('intry', ReturnFinally())
770 assert_equal('finally', g:in_finally)
771enddef
772
Bram Moolenaar9939f572020-09-16 22:29:52 +0200773def TryOne(): number
774 try
775 return 0
776 catch
777 endtry
778 return 0
779enddef
780
781def TryTwo(n: number): string
782 try
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200783 var x = {}
Bram Moolenaar9939f572020-09-16 22:29:52 +0200784 catch
785 endtry
786 return 'text'
787enddef
788
789def Test_try_catch_twice()
790 assert_equal('text', TryOne()->TryTwo())
791enddef
792
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100793def Test_try_catch_match()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200794 var seq = 'a'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100795 try
796 throw 'something'
797 catch /nothing/
798 seq ..= 'x'
799 catch /some/
800 seq ..= 'b'
801 catch /asdf/
802 seq ..= 'x'
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200803 catch ?a\?sdf?
804 seq ..= 'y'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100805 finally
806 seq ..= 'c'
807 endtry
808 assert_equal('abc', seq)
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100809enddef
810
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200811def Test_try_catch_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200812 CheckDefFailure(['catch'], 'E603:')
813 CheckDefFailure(['try', 'echo 0', 'catch', 'catch'], 'E1033:')
814 CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
815 CheckDefFailure(['finally'], 'E606:')
816 CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:')
817 CheckDefFailure(['endtry'], 'E602:')
818 CheckDefFailure(['while 1', 'endtry'], 'E170:')
819 CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:')
Bram Moolenaar13106602020-10-04 16:06:05 +0200820 CheckDefFailure(['if 1', 'endtry'], 'E171:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200821 CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:')
Bram Moolenaar585fea72020-04-02 22:33:21 +0200822
Bram Moolenaare4984292020-12-13 14:19:25 +0100823 CheckDefFailure(['throw'], 'E1143:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200824 CheckDefFailure(['throw xxx'], 'E1001:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200825enddef
826
Bram Moolenaar7c5b3c0362021-02-14 22:40:57 +0100827def Try_catch_skipped()
828 var l = []
829 try
830 finally
831 endtry
832
833 if 1
834 else
835 try
836 endtry
837 endif
838enddef
839
840" The skipped try/endtry was updating the wrong instruction.
841def Test_try_catch_skipped()
842 var instr = execute('disassemble Try_catch_skipped')
843 assert_match("NEWLIST size 0\n", instr)
844enddef
845
846
847
Bram Moolenaar006ad482020-06-30 20:55:15 +0200848def Test_throw_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200849 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200850 var lines =<< trim END
Bram Moolenaar006ad482020-06-30 20:55:15 +0200851 vim9script
852 try
853 throw 'one'
854 .. 'two'
855 catch
856 assert_equal('onetwo', v:exception)
857 endtry
858 END
859 CheckScriptSuccess(lines)
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200860
861 lines =<< trim END
862 vim9script
Bram Moolenaar352134b2020-10-17 22:04:08 +0200863 @r = ''
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200864 def Func()
865 throw @r
866 enddef
867 var result = ''
868 try
869 Func()
870 catch /E1129:/
871 result = 'caught'
872 endtry
873 assert_equal('caught', result)
874 END
875 CheckScriptSuccess(lines)
Bram Moolenaar006ad482020-06-30 20:55:15 +0200876enddef
877
Bram Moolenaared677f52020-08-12 16:38:10 +0200878def Test_error_in_nested_function()
Bram Moolenaar03dfde22021-02-14 13:17:22 +0100879 # an error in a nested :function aborts executing in the calling :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200880 var lines =<< trim END
Bram Moolenaared677f52020-08-12 16:38:10 +0200881 vim9script
882 def Func()
883 Error()
884 g:test_var = 1
885 enddef
886 func Error() abort
887 eval [][0]
888 endfunc
889 Func()
890 END
891 g:test_var = 0
892 CheckScriptFailure(lines, 'E684:')
893 assert_equal(0, g:test_var)
894enddef
895
Bram Moolenaar227c58a2021-04-28 20:40:44 +0200896def Test_abort_after_error()
897 var lines =<< trim END
898 vim9script
899 while true
900 echo notfound
901 endwhile
902 g:gotthere = true
903 END
904 g:gotthere = false
905 CheckScriptFailure(lines, 'E121:')
906 assert_false(g:gotthere)
907 unlet g:gotthere
908enddef
909
Bram Moolenaar37c83712020-06-30 21:18:36 +0200910def Test_cexpr_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200911 # only checks line continuation
Bram Moolenaar37c83712020-06-30 21:18:36 +0200912 set errorformat=File\ %f\ line\ %l
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200913 var lines =<< trim END
Bram Moolenaar37c83712020-06-30 21:18:36 +0200914 vim9script
915 cexpr 'File'
916 .. ' someFile' ..
917 ' line 19'
918 assert_equal(19, getqflist()[0].lnum)
919 END
920 CheckScriptSuccess(lines)
921 set errorformat&
922enddef
923
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200924def Test_statusline_syntax()
925 # legacy syntax is used for 'statusline'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200926 var lines =<< trim END
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200927 vim9script
928 func g:Status()
929 return '%{"x" is# "x"}'
930 endfunc
931 set laststatus=2 statusline=%!Status()
932 redrawstatus
933 set laststatus statusline=
934 END
935 CheckScriptSuccess(lines)
936enddef
937
Bram Moolenaarb2097502020-07-19 17:17:02 +0200938def Test_list_vimscript()
939 # checks line continuation and comments
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200940 var lines =<< trim END
Bram Moolenaarb2097502020-07-19 17:17:02 +0200941 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200942 var mylist = [
Bram Moolenaarb2097502020-07-19 17:17:02 +0200943 'one',
944 # comment
945 'two', # empty line follows
946
947 'three',
948 ]
949 assert_equal(['one', 'two', 'three'], mylist)
950 END
951 CheckScriptSuccess(lines)
Bram Moolenaar66250c92020-08-20 15:02:42 +0200952
953 # check all lines from heredoc are kept
954 lines =<< trim END
955 # comment 1
956 two
957 # comment 3
958
959 five
960 # comment 6
961 END
962 assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines)
Bram Moolenaar4bce26b2021-01-22 22:06:56 +0100963
964 lines =<< trim END
965 [{
966 a: 0}]->string()->assert_equal("[{'a': 0}]")
967 END
968 CheckDefAndScriptSuccess(lines)
Bram Moolenaarb2097502020-07-19 17:17:02 +0200969enddef
970
Bram Moolenaar2a1381c2020-05-05 23:32:58 +0200971if has('channel')
972 let someJob = test_null_job()
Bram Moolenaar40ee4662020-05-05 22:08:26 +0200973
Bram Moolenaar2a1381c2020-05-05 23:32:58 +0200974 def FuncWithError()
975 echomsg g:someJob
976 enddef
Bram Moolenaar40ee4662020-05-05 22:08:26 +0200977
Bram Moolenaar2a1381c2020-05-05 23:32:58 +0200978 func Test_convert_emsg_to_exception()
979 try
980 call FuncWithError()
981 catch
982 call assert_match('Vim:E908:', v:exception)
983 endtry
984 endfunc
985endif
Bram Moolenaar40ee4662020-05-05 22:08:26 +0200986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987let s:export_script_lines =<< trim END
988 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200989 var name: string = 'bob'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 def Concat(arg: string): string
991 return name .. arg
992 enddef
Bram Moolenaar227a69d2020-05-15 18:17:28 +0200993 g:result = Concat('bie')
994 g:localname = name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995
996 export const CONST = 1234
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200997 export var exported = 9876
998 export var exp_name = 'John'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999 export def Exported(): string
1000 return 'Exported'
1001 enddef
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001002 export final theList = [1]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003END
1004
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001005def Undo_export_script_lines()
1006 unlet g:result
1007 unlet g:localname
1008enddef
1009
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001010def Test_vim9_import_export()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001011 var import_script_lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 vim9script
1013 import {exported, Exported} from './Xexport.vim'
1014 g:imported = exported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001015 exported += 3
1016 g:imported_added = exported
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001017 g:imported_func = Exported()
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001018
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001019 def GetExported(): string
Bram Moolenaare0de1712020-12-02 17:36:54 +01001020 var local_dict = {ref: Exported}
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001021 return local_dict.ref()
1022 enddef
1023 g:funcref_result = GetExported()
1024
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001025 import {exp_name} from './Xexport.vim'
1026 g:imported_name = exp_name
1027 exp_name ..= ' Doe'
1028 g:imported_name_appended = exp_name
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001029 g:imported_later = exported
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001030
1031 import theList from './Xexport.vim'
1032 theList->add(2)
1033 assert_equal([1, 2], theList)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001034 END
1035
1036 writefile(import_script_lines, 'Ximport.vim')
1037 writefile(s:export_script_lines, 'Xexport.vim')
1038
1039 source Ximport.vim
1040
1041 assert_equal('bobbie', g:result)
1042 assert_equal('bob', g:localname)
1043 assert_equal(9876, g:imported)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001044 assert_equal(9879, g:imported_added)
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001045 assert_equal(9879, g:imported_later)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046 assert_equal('Exported', g:imported_func)
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001047 assert_equal('Exported', g:funcref_result)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001048 assert_equal('John', g:imported_name)
1049 assert_equal('John Doe', g:imported_name_appended)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 assert_false(exists('g:name'))
1051
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001052 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 unlet g:imported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001054 unlet g:imported_added
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001055 unlet g:imported_later
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 unlet g:imported_func
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001057 unlet g:imported_name g:imported_name_appended
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058 delete('Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001059
Bram Moolenaar1c991142020-07-04 13:15:31 +02001060 # similar, with line breaks
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001061 var import_line_break_script_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001062 vim9script
1063 import {
1064 exported,
1065 Exported,
1066 }
1067 from
1068 './Xexport.vim'
1069 g:imported = exported
1070 exported += 5
1071 g:imported_added = exported
1072 g:imported_func = Exported()
1073 END
1074 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
1075 source Ximport_lbr.vim
1076
1077 assert_equal(9876, g:imported)
1078 assert_equal(9881, g:imported_added)
1079 assert_equal('Exported', g:imported_func)
1080
1081 # exported script not sourced again
1082 assert_false(exists('g:result'))
1083 unlet g:imported
1084 unlet g:imported_added
1085 unlet g:imported_func
1086 delete('Ximport_lbr.vim')
1087
1088 # import inside :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001089 var import_in_def_lines =<< trim END
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001090 vim9script
1091 def ImportInDef()
1092 import exported from './Xexport.vim'
1093 g:imported = exported
1094 exported += 7
1095 g:imported_added = exported
1096 enddef
1097 ImportInDef()
1098 END
1099 writefile(import_in_def_lines, 'Ximport2.vim')
1100 source Ximport2.vim
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001101 # TODO: this should be 9879
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001102 assert_equal(9876, g:imported)
1103 assert_equal(9883, g:imported_added)
1104 unlet g:imported
1105 unlet g:imported_added
1106 delete('Ximport2.vim')
1107
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001108 var import_star_as_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001109 vim9script
1110 import * as Export from './Xexport.vim'
1111 def UseExport()
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001112 g:imported_def = Export.exported
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001113 enddef
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001114 g:imported_script = Export.exported
1115 assert_equal(1, exists('Export.exported'))
1116 assert_equal(0, exists('Export.notexported'))
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001117 UseExport()
1118 END
1119 writefile(import_star_as_lines, 'Ximport.vim')
1120 source Ximport.vim
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001121 assert_equal(9883, g:imported_def)
1122 assert_equal(9883, g:imported_script)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001123
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001124 var import_star_as_lines_no_dot =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001125 vim9script
1126 import * as Export from './Xexport.vim'
1127 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001128 var dummy = 1
1129 var imported = Export + dummy
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001130 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001131 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001132 END
1133 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001134 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001135
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001136 var import_star_as_lines_dot_space =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001137 vim9script
1138 import * as Export from './Xexport.vim'
1139 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001140 var imported = Export . exported
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001141 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001142 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001143 END
1144 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001145 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001146
Bram Moolenaara6294952020-12-27 13:39:50 +01001147 var import_star_as_duplicated =<< trim END
1148 vim9script
1149 import * as Export from './Xexport.vim'
1150 var some = 'other'
1151 import * as Export from './Xexport.vim'
1152 defcompile
1153 END
1154 writefile(import_star_as_duplicated, 'Ximport.vim')
1155 assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim')
1156
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001157 var import_star_as_lines_script_no_dot =<< trim END
1158 vim9script
1159 import * as Export from './Xexport.vim'
1160 g:imported_script = Export exported
1161 END
1162 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
1163 assert_fails('source Ximport.vim', 'E1029:')
1164
1165 var import_star_as_lines_script_space_after_dot =<< trim END
1166 vim9script
1167 import * as Export from './Xexport.vim'
1168 g:imported_script = Export. exported
1169 END
1170 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
1171 assert_fails('source Ximport.vim', 'E1074:')
1172
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001173 var import_star_as_lines_missing_name =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001174 vim9script
1175 import * as Export from './Xexport.vim'
1176 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001177 var imported = Export.
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001178 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001179 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001180 END
1181 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001182 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001183
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001184 var import_star_as_lbr_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001185 vim9script
1186 import *
1187 as Export
1188 from
1189 './Xexport.vim'
1190 def UseExport()
1191 g:imported = Export.exported
1192 enddef
1193 UseExport()
1194 END
1195 writefile(import_star_as_lbr_lines, 'Ximport.vim')
1196 source Ximport.vim
1197 assert_equal(9883, g:imported)
1198
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001199 var import_star_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001200 vim9script
1201 import * from './Xexport.vim'
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001202 END
1203 writefile(import_star_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001204 assert_fails('source Ximport.vim', 'E1045:', '', 2, 'Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001205
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001206 # try to import something that exists but is not exported
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001207 var import_not_exported_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001208 vim9script
1209 import name from './Xexport.vim'
1210 END
1211 writefile(import_not_exported_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001212 assert_fails('source Ximport.vim', 'E1049:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001213
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001214 # try to import something that is already defined
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001215 var import_already_defined =<< trim END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001216 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001217 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001218 import exported from './Xexport.vim'
1219 END
1220 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001221 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001222
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001223 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001224 import_already_defined =<< trim END
1225 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001226 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001227 import * as exported from './Xexport.vim'
1228 END
1229 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001230 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001231
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001232 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001233 import_already_defined =<< trim END
1234 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001235 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001236 import {exported} from './Xexport.vim'
1237 END
1238 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001239 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001240
Bram Moolenaar918a4242020-12-06 14:37:08 +01001241 # try changing an imported const
1242 var import_assign_to_const =<< trim END
1243 vim9script
1244 import CONST from './Xexport.vim'
1245 def Assign()
1246 CONST = 987
1247 enddef
1248 defcompile
1249 END
1250 writefile(import_assign_to_const, 'Ximport.vim')
1251 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1252
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001253 # try changing an imported final
1254 var import_assign_to_final =<< trim END
1255 vim9script
1256 import theList from './Xexport.vim'
1257 def Assign()
1258 theList = [2]
1259 enddef
1260 defcompile
1261 END
1262 writefile(import_assign_to_final, 'Ximport.vim')
1263 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1264
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001265 # import a very long name, requires making a copy
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001266 var import_long_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001267 vim9script
1268 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
1269 END
1270 writefile(import_long_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001271 assert_fails('source Ximport.vim', 'E1048:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001272
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001273 var import_no_from_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001274 vim9script
1275 import name './Xexport.vim'
1276 END
1277 writefile(import_no_from_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001278 assert_fails('source Ximport.vim', 'E1070:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001279
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001280 var import_invalid_string_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001281 vim9script
1282 import name from Xexport.vim
1283 END
1284 writefile(import_invalid_string_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001285 assert_fails('source Ximport.vim', 'E1071:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001286
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001287 var import_wrong_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001288 vim9script
1289 import name from './XnoExport.vim'
1290 END
1291 writefile(import_wrong_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001292 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001293
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001294 var import_missing_comma_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001295 vim9script
1296 import {exported name} from './Xexport.vim'
1297 END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001298 writefile(import_missing_comma_lines, 'Ximport3.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001299 assert_fails('source Ximport3.vim', 'E1046:', '', 2, 'Ximport3.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001300
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001301 delete('Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001302 delete('Ximport3.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 delete('Xexport.vim')
1304
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001305 # Check that in a Vim9 script 'cpo' is set to the Vim default.
Bram Moolenaar3e191692021-03-17 17:46:00 +01001306 # Flags added or removed are also applied to the restored value.
1307 set cpo=abcd
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001308 var lines =<< trim END
Bram Moolenaar750802b2020-02-23 18:08:33 +01001309 vim9script
1310 g:cpo_in_vim9script = &cpo
Bram Moolenaar3e191692021-03-17 17:46:00 +01001311 set cpo+=f
1312 set cpo-=c
1313 g:cpo_after_vim9script = &cpo
Bram Moolenaar750802b2020-02-23 18:08:33 +01001314 END
1315 writefile(lines, 'Xvim9_script')
1316 source Xvim9_script
Bram Moolenaar3e191692021-03-17 17:46:00 +01001317 assert_equal('fabd', &cpo)
Bram Moolenaar750802b2020-02-23 18:08:33 +01001318 set cpo&vim
1319 assert_equal(&cpo, g:cpo_in_vim9script)
Bram Moolenaar3e191692021-03-17 17:46:00 +01001320 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
1321 assert_equal(newcpo, g:cpo_after_vim9script)
1322
Bram Moolenaar750802b2020-02-23 18:08:33 +01001323 delete('Xvim9_script')
1324enddef
1325
Bram Moolenaar0a842842021-02-27 22:41:19 +01001326def Test_import_as()
1327 var export_lines =<< trim END
1328 vim9script
1329 export var one = 1
1330 export var yes = 'yes'
1331 END
1332 writefile(export_lines, 'XexportAs')
1333
1334 var import_lines =<< trim END
1335 vim9script
Bram Moolenaar6c7cc342021-04-17 16:38:50 +02001336 var one = 'notused'
1337 var yes = 777
Bram Moolenaar0a842842021-02-27 22:41:19 +01001338 import one as thatOne from './XexportAs'
1339 assert_equal(1, thatOne)
1340 import yes as yesYes from './XexportAs'
1341 assert_equal('yes', yesYes)
1342 END
1343 CheckScriptSuccess(import_lines)
1344
1345 import_lines =<< trim END
1346 vim9script
1347 import {one as thatOne, yes as yesYes} from './XexportAs'
1348 assert_equal(1, thatOne)
1349 assert_equal('yes', yesYes)
1350 assert_fails('echo one', 'E121:')
1351 assert_fails('echo yes', 'E121:')
1352 END
1353 CheckScriptSuccess(import_lines)
1354
1355 delete('XexportAs')
1356enddef
1357
Bram Moolenaar803af682020-08-05 16:20:03 +02001358func g:Trigger()
1359 source Ximport.vim
1360 return "echo 'yes'\<CR>"
1361endfunc
1362
1363def Test_import_export_expr_map()
1364 # check that :import and :export work when buffer is locked
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001365 var export_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001366 vim9script
1367 export def That(): string
1368 return 'yes'
1369 enddef
1370 END
1371 writefile(export_lines, 'Xexport_that.vim')
1372
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001373 var import_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001374 vim9script
1375 import That from './Xexport_that.vim'
1376 assert_equal('yes', That())
1377 END
1378 writefile(import_lines, 'Ximport.vim')
1379
1380 nnoremap <expr> trigger g:Trigger()
1381 feedkeys('trigger', "xt")
1382
Bram Moolenaar730b2482020-08-09 13:02:10 +02001383 delete('Xexport_that.vim')
Bram Moolenaar803af682020-08-05 16:20:03 +02001384 delete('Ximport.vim')
1385 nunmap trigger
1386enddef
1387
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001388def Test_import_in_filetype()
1389 # check that :import works when the buffer is locked
1390 mkdir('ftplugin', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001391 var export_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001392 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001393 export var That = 'yes'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001394 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001395 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001396
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001397 var import_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001398 vim9script
Bram Moolenaar730b2482020-08-09 13:02:10 +02001399 import That from './Xexport_ft.vim'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001400 assert_equal('yes', That)
1401 g:did_load_mytpe = 1
1402 END
1403 writefile(import_lines, 'ftplugin/qf.vim')
1404
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001405 var save_rtp = &rtp
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001406 &rtp = getcwd() .. ',' .. &rtp
1407
1408 filetype plugin on
1409 copen
1410 assert_equal(1, g:did_load_mytpe)
1411
1412 quit!
Bram Moolenaar730b2482020-08-09 13:02:10 +02001413 delete('Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001414 delete('ftplugin', 'rf')
1415 &rtp = save_rtp
1416enddef
1417
Bram Moolenaarefa94442020-08-08 22:16:00 +02001418def Test_use_import_in_mapping()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001419 var lines =<< trim END
Bram Moolenaarefa94442020-08-08 22:16:00 +02001420 vim9script
1421 export def Funcx()
1422 g:result = 42
1423 enddef
1424 END
1425 writefile(lines, 'XsomeExport.vim')
1426 lines =<< trim END
1427 vim9script
1428 import Funcx from './XsomeExport.vim'
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001429 nnoremap <F3> :call <sid>Funcx()<cr>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001430 END
1431 writefile(lines, 'Xmapscript.vim')
1432
1433 source Xmapscript.vim
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001434 feedkeys("\<F3>", "xt")
Bram Moolenaarefa94442020-08-08 22:16:00 +02001435 assert_equal(42, g:result)
1436
1437 unlet g:result
1438 delete('XsomeExport.vim')
1439 delete('Xmapscript.vim')
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001440 nunmap <F3>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001441enddef
1442
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01001443def Test_vim9script_mix()
1444 var lines =<< trim END
1445 if has(g:feature)
1446 " legacy script
1447 let g:legacy = 1
1448 finish
1449 endif
1450 vim9script
1451 g:legacy = 0
1452 END
1453 g:feature = 'eval'
1454 g:legacy = -1
1455 CheckScriptSuccess(lines)
1456 assert_equal(1, g:legacy)
1457
1458 g:feature = 'noteval'
1459 g:legacy = -1
1460 CheckScriptSuccess(lines)
1461 assert_equal(0, g:legacy)
1462enddef
1463
Bram Moolenaar750802b2020-02-23 18:08:33 +01001464def Test_vim9script_fails()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1466 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001467 CheckScriptFailure(['export var some = 123'], 'E1042:')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001468 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001469 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
Bram Moolenaar750802b2020-02-23 18:08:33 +01001470 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
1471
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001472 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001473 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1474
Bram Moolenaare2e40752020-09-04 21:18:46 +02001475 assert_fails('vim9script', 'E1038:')
1476 assert_fails('export something', 'E1043:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001477enddef
1478
Bram Moolenaarf7d267e2020-06-17 12:04:54 +02001479func Test_import_fails_without_script()
Bram Moolenaar101f4812020-06-16 23:18:51 +02001480 CheckRunVimInTerminal
1481
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001482 " call indirectly to avoid compilation error for missing functions
Bram Moolenaarc620c052020-07-08 15:16:19 +02001483 call Run_Test_import_fails_on_command_line()
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001484endfunc
1485
Bram Moolenaarc620c052020-07-08 15:16:19 +02001486def Run_Test_import_fails_on_command_line()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001487 var export =<< trim END
Bram Moolenaar101f4812020-06-16 23:18:51 +02001488 vim9script
1489 export def Foo(): number
1490 return 0
1491 enddef
1492 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001493 writefile(export, 'XexportCmd.vim')
Bram Moolenaar101f4812020-06-16 23:18:51 +02001494
Bram Moolenaare0de1712020-12-02 17:36:54 +01001495 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001496 rows: 6, wait_for_ruler: 0})
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001497 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaar101f4812020-06-16 23:18:51 +02001498
Bram Moolenaar730b2482020-08-09 13:02:10 +02001499 delete('XexportCmd.vim')
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001500 StopVimInTerminal(buf)
1501enddef
Bram Moolenaar101f4812020-06-16 23:18:51 +02001502
Bram Moolenaar2b327002020-12-26 15:39:31 +01001503def Test_vim9script_reload_noclear()
1504 var lines =<< trim END
Bram Moolenaara6294952020-12-27 13:39:50 +01001505 vim9script
1506 export var exported = 'thexport'
1507 END
1508 writefile(lines, 'XExportReload')
1509 lines =<< trim END
Bram Moolenaar2b327002020-12-26 15:39:31 +01001510 vim9script noclear
1511 g:loadCount += 1
1512 var s:reloaded = 'init'
Bram Moolenaara6294952020-12-27 13:39:50 +01001513 import exported from './XExportReload'
Bram Moolenaar2b327002020-12-26 15:39:31 +01001514
1515 def Again(): string
1516 return 'again'
1517 enddef
1518
1519 if exists('s:loaded') | finish | endif
1520 var s:loaded = true
1521
1522 var s:notReloaded = 'yes'
1523 s:reloaded = 'first'
1524 def g:Values(): list<string>
Bram Moolenaara6294952020-12-27 13:39:50 +01001525 return [s:reloaded, s:notReloaded, Again(), Once(), exported]
Bram Moolenaar2b327002020-12-26 15:39:31 +01001526 enddef
1527
1528 def Once(): string
1529 return 'once'
1530 enddef
1531 END
1532 writefile(lines, 'XReloaded')
1533 g:loadCount = 0
1534 source XReloaded
1535 assert_equal(1, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001536 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001537 source XReloaded
1538 assert_equal(2, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001539 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001540 source XReloaded
1541 assert_equal(3, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001542 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001543
Bram Moolenaar48e11c12021-01-11 18:47:00 +01001544 delete('XReloaded')
Bram Moolenaara6294952020-12-27 13:39:50 +01001545 delete('XExportReload')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001546 delfunc g:Values
Bram Moolenaar2b327002020-12-26 15:39:31 +01001547 unlet g:loadCount
Bram Moolenaar577dc932021-06-27 15:35:40 +02001548
1549 lines =<< trim END
1550 vim9script
1551 def Inner()
1552 enddef
1553 END
1554 lines->writefile('XreloadScript.vim')
1555 source XreloadScript.vim
1556
1557 lines =<< trim END
1558 vim9script
1559 def Outer()
1560 def Inner()
1561 enddef
1562 enddef
1563 defcompile
1564 END
1565 lines->writefile('XreloadScript.vim')
1566 source XreloadScript.vim
1567
1568 delete('XreloadScript.vim')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001569enddef
1570
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001571def Test_vim9script_reload_import()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001572 var lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 vim9script
1574 const var = ''
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001575 var valone = 1234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 def MyFunc(arg: string)
1577 valone = 5678
1578 enddef
1579 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001580 var morelines =<< trim END
1581 var valtwo = 222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 export def GetValtwo(): number
1583 return valtwo
1584 enddef
1585 END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001586 writefile(lines + morelines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 source Xreload.vim
1588 source Xreload.vim
1589 source Xreload.vim
1590
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001591 var testlines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 vim9script
1593 def TheFunc()
1594 import GetValtwo from './Xreload.vim'
1595 assert_equal(222, GetValtwo())
1596 enddef
1597 TheFunc()
1598 END
1599 writefile(testlines, 'Ximport.vim')
1600 source Ximport.vim
1601
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001602 # Test that when not using "morelines" GetValtwo() and valtwo are still
1603 # defined, because import doesn't reload a script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 writefile(lines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 source Ximport.vim
1606
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001607 # cannot declare a var twice
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 lines =<< trim END
1609 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001610 var valone = 1234
1611 var valone = 5678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 END
1613 writefile(lines, 'Xreload.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001614 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615
1616 delete('Xreload.vim')
1617 delete('Ximport.vim')
1618enddef
1619
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001620" if a script is reloaded with a script-local variable that changed its type, a
1621" compiled function using that variable must fail.
1622def Test_script_reload_change_type()
1623 var lines =<< trim END
1624 vim9script noclear
1625 var str = 'string'
1626 def g:GetStr(): string
1627 return str .. 'xxx'
1628 enddef
1629 END
1630 writefile(lines, 'Xreload.vim')
1631 source Xreload.vim
1632 echo g:GetStr()
1633
1634 lines =<< trim END
1635 vim9script noclear
1636 var str = 1234
1637 END
1638 writefile(lines, 'Xreload.vim')
1639 source Xreload.vim
1640 assert_fails('echo g:GetStr()', 'E1150:')
1641
1642 delfunc g:GetStr
1643 delete('Xreload.vim')
1644enddef
1645
Bram Moolenaarc970e422021-03-17 15:03:04 +01001646" Define CallFunc so that the test can be compiled
1647command CallFunc echo 'nop'
1648
1649def Test_script_reload_from_function()
1650 var lines =<< trim END
1651 vim9script
1652
1653 if exists('g:loaded')
1654 finish
1655 endif
1656 g:loaded = 1
1657 delcommand CallFunc
1658 command CallFunc Func()
1659 def Func()
Bram Moolenaara4c81be2021-03-17 15:23:16 +01001660 so XreloadFunc.vim
Bram Moolenaarc970e422021-03-17 15:03:04 +01001661 g:didTheFunc = 1
1662 enddef
1663 END
1664 writefile(lines, 'XreloadFunc.vim')
1665 source XreloadFunc.vim
1666 CallFunc
1667 assert_equal(1, g:didTheFunc)
1668
1669 delete('XreloadFunc.vim')
1670 delcommand CallFunc
1671 unlet g:loaded
1672 unlet g:didTheFunc
1673enddef
1674
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001675def Test_script_var_shadows_function()
1676 var lines =<< trim END
1677 vim9script
1678 def Func(): number
1679 return 123
1680 enddef
1681 var Func = 1
1682 END
1683 CheckScriptFailure(lines, 'E1041:', 5)
1684enddef
1685
Bram Moolenaar95006e32020-08-29 17:47:08 +02001686def s:RetSome(): string
1687 return 'some'
1688enddef
1689
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001690" Not exported function that is referenced needs to be accessed by the
1691" script-local name.
1692def Test_vim9script_funcref()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001693 var sortlines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001694 vim9script
1695 def Compare(i1: number, i2: number): number
Bram Moolenaarbed36b92020-07-07 23:31:36 +02001696 return i2 - i1
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001697 enddef
1698
1699 export def FastSort(): list<number>
1700 return range(5)->sort(Compare)
1701 enddef
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001702
1703 export def GetString(arg: string): string
1704 return arg
1705 enddef
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001706 END
1707 writefile(sortlines, 'Xsort.vim')
1708
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001709 var lines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001710 vim9script
1711 import FastSort from './Xsort.vim'
1712 def Test()
1713 g:result = FastSort()
1714 enddef
1715 Test()
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001716
1717 # using a function imported with "as"
1718 import * as anAlias from './Xsort.vim'
1719 assert_equal('yes', anAlias.GetString('yes'))
1720
1721 # using the function from a compiled function
1722 def TestMore(): string
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001723 var s = s:anAlias.GetString('foo')
1724 return s .. anAlias.GetString('bar')
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001725 enddef
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001726 assert_equal('foobar', TestMore())
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001727
1728 # error when using a function that isn't exported
1729 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001730 END
1731 writefile(lines, 'Xscript.vim')
1732
1733 source Xscript.vim
1734 assert_equal([4, 3, 2, 1, 0], g:result)
1735
1736 unlet g:result
1737 delete('Xsort.vim')
1738 delete('Xscript.vim')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001739
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001740 var Funcref = function('s:RetSome')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001741 assert_equal('some', Funcref())
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001742enddef
1743
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001744" Check that when searching for "FilterFunc" it finds the import in the
1745" script where FastFilter() is called from, both as a string and as a direct
1746" function reference.
Bram Moolenaarc620c052020-07-08 15:16:19 +02001747def Test_vim9script_funcref_other_script()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001748 var filterLines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001749 vim9script
1750 export def FilterFunc(idx: number, val: number): bool
1751 return idx % 2 == 1
1752 enddef
1753 export def FastFilter(): list<number>
1754 return range(10)->filter('FilterFunc')
1755 enddef
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001756 export def FastFilterDirect(): list<number>
1757 return range(10)->filter(FilterFunc)
1758 enddef
Bram Moolenaarc620c052020-07-08 15:16:19 +02001759 END
1760 writefile(filterLines, 'Xfilter.vim')
1761
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001762 var lines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001763 vim9script
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001764 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim'
Bram Moolenaarc620c052020-07-08 15:16:19 +02001765 def Test()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001766 var x: list<number> = FastFilter()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001767 enddef
1768 Test()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001769 def TestDirect()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001770 var x: list<number> = FastFilterDirect()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001771 enddef
1772 TestDirect()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001773 END
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001774 CheckScriptSuccess(lines)
Bram Moolenaarc620c052020-07-08 15:16:19 +02001775 delete('Xfilter.vim')
Bram Moolenaarc620c052020-07-08 15:16:19 +02001776enddef
1777
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001778def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001779 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001780 vim9script
1781 def FuncYes(): string
1782 return 'yes'
1783 enddef
1784 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001785 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001786 def FuncNo(): string
1787 return 'no'
1788 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001789 def g:DoCheck(no_exists: bool)
1790 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001791 assert_equal('no', FuncNo())
1792 enddef
1793 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001794 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001795 def g:DoCheck(no_exists: bool)
1796 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02001797 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001798 enddef
1799 END
1800
1801 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001802 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001803 source Xreloaded.vim
1804 g:DoCheck(true)
1805
1806 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001807 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001808 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001809 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001810
1811 # FuncNo() is back
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001812 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001813 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001814 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001815
1816 delete('Xreloaded.vim')
1817enddef
1818
Bram Moolenaar89483d42020-05-10 15:24:44 +02001819def Test_vim9script_reload_delvar()
1820 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001821 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02001822 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001823 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001824 END
1825 writefile(lines, 'XreloadVar.vim')
1826 source XreloadVar.vim
1827
1828 # now write the script using the same variable locally - works
1829 lines =<< trim END
1830 vim9script
1831 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001832 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001833 enddef
1834 END
1835 writefile(lines, 'XreloadVar.vim')
1836 source XreloadVar.vim
1837
1838 delete('XreloadVar.vim')
1839enddef
1840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841def Test_import_absolute()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001842 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02001843 'vim9script',
1844 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
1845 'def UseExported()',
1846 ' g:imported_abs = exported',
1847 ' exported = 8888',
1848 ' g:imported_after = exported',
1849 'enddef',
1850 'UseExported()',
1851 'g:import_disassembled = execute("disass UseExported")',
1852 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853 writefile(import_lines, 'Ximport_abs.vim')
1854 writefile(s:export_script_lines, 'Xexport_abs.vim')
1855
1856 source Ximport_abs.vim
1857
1858 assert_equal(9876, g:imported_abs)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001859 assert_equal(8888, g:imported_after)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001860 assert_match('<SNR>\d\+_UseExported\_s*' ..
1861 'g:imported_abs = exported\_s*' ..
1862 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1863 '1 STOREG g:imported_abs\_s*' ..
1864 'exported = 8888\_s*' ..
1865 '2 PUSHNR 8888\_s*' ..
1866 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1867 'g:imported_after = exported\_s*' ..
1868 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1869 '5 STOREG g:imported_after',
Bram Moolenaare6085c52020-04-12 20:19:16 +02001870 g:import_disassembled)
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001871
1872 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873 unlet g:imported_abs
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001874 unlet g:import_disassembled
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875
1876 delete('Ximport_abs.vim')
1877 delete('Xexport_abs.vim')
1878enddef
1879
1880def Test_import_rtp()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001881 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02001882 'vim9script',
1883 'import exported from "Xexport_rtp.vim"',
1884 'g:imported_rtp = exported',
1885 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001886 writefile(import_lines, 'Ximport_rtp.vim')
1887 mkdir('import')
1888 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1889
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001890 var save_rtp = &rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 &rtp = getcwd()
1892 source Ximport_rtp.vim
1893 &rtp = save_rtp
1894
1895 assert_equal(9876, g:imported_rtp)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001897 Undo_export_script_lines()
1898 unlet g:imported_rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 delete('Ximport_rtp.vim')
Bram Moolenaar89483d42020-05-10 15:24:44 +02001900 delete('import', 'rf')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901enddef
1902
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001903def Test_import_compile_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001904 var export_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001905 'vim9script',
1906 'export def ExpFunc(): string',
1907 ' return notDefined',
1908 'enddef',
1909 ]
1910 writefile(export_lines, 'Xexported.vim')
1911
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001912 var import_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001913 'vim9script',
1914 'import ExpFunc from "./Xexported.vim"',
1915 'def ImpFunc()',
1916 ' echo ExpFunc()',
1917 'enddef',
1918 'defcompile',
1919 ]
1920 writefile(import_lines, 'Ximport.vim')
1921
1922 try
1923 source Ximport.vim
1924 catch /E1001/
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001925 # Error should be fore the Xexported.vim file.
Bram Moolenaar77072282020-09-16 17:55:40 +02001926 assert_match('E1001: Variable not found: notDefined', v:exception)
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001927 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1928 endtry
1929
1930 delete('Xexported.vim')
1931 delete('Ximport.vim')
1932enddef
1933
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001934def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001935 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001936 'vim9script',
1937 'def Func()',
1938 ' eval [][0]',
1939 'enddef',
1940 'Func()',
1941 ]
1942 writefile(lines, 'Xtestscript.vim')
1943
1944 for count in range(3)
1945 try
1946 source Xtestscript.vim
1947 catch /E684/
1948 # function name should contain <SNR> every time
1949 assert_match('E684: list index out of range', v:exception)
1950 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
1951 endtry
1952 endfor
1953
1954 delete('Xtestscript.vim')
1955enddef
1956
Bram Moolenaareef21022020-08-01 22:16:43 +02001957def Test_func_overrules_import_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001958 var export_lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02001959 vim9script
1960 export def Func()
1961 echo 'imported'
1962 enddef
1963 END
1964 writefile(export_lines, 'XexportedFunc.vim')
1965
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001966 var lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02001967 vim9script
1968 import Func from './XexportedFunc.vim'
1969 def Func()
1970 echo 'local to function'
1971 enddef
1972 END
1973 CheckScriptFailure(lines, 'E1073:')
1974
1975 lines =<< trim END
1976 vim9script
1977 import Func from './XexportedFunc.vim'
1978 def Outer()
1979 def Func()
1980 echo 'local to function'
1981 enddef
1982 enddef
1983 defcompile
1984 END
1985 CheckScriptFailure(lines, 'E1073:')
1986
1987 delete('XexportedFunc.vim')
1988enddef
1989
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02001990def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001991 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02001992 vim9script
1993 def Func()
1994 echo 'one'
1995 enddef
1996 def Func()
1997 echo 'two'
1998 enddef
1999 END
2000 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002001
2002 lines =<< trim END
2003 vim9script
2004 def Foo(): string
2005 return 'foo'
2006 enddef
2007 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002008 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002009 enddef
2010 defcompile
2011 END
2012 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002013enddef
2014
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002016 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002017 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018 l->remove(0)
2019 l->add(5)
2020 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002021 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022enddef
2023
Bram Moolenaarae616492020-07-28 20:07:27 +02002024def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002025 CheckDefExecFailure(['a = 1'], 'E1100:')
2026 CheckDefExecFailure(['c = 1'], 'E1100:')
2027 CheckDefExecFailure(['i = 1'], 'E1100:')
2028 CheckDefExecFailure(['t = 1'], 'E1100:')
2029 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002030
Bram Moolenaarae616492020-07-28 20:07:27 +02002031 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
2032 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002033 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
2034 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002035 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
2036 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01002037 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
2038 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002039 CheckScriptFailure(['vim9script', 't'], 'E1100:')
2040 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
2041 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002042enddef
2043
Bram Moolenaar158906c2020-02-06 20:39:45 +01002044def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002045 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01002046 if what == 1
2047 res = "one"
2048 elseif what == 2
2049 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002050 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01002051 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002052 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01002053 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002054enddef
2055
Bram Moolenaar158906c2020-02-06 20:39:45 +01002056def Test_if_elseif_else()
2057 assert_equal('one', IfElse(1))
2058 assert_equal('two', IfElse(2))
2059 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002060enddef
2061
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002062def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002063 CheckDefFailure(['elseif true'], 'E582:')
2064 CheckDefFailure(['else'], 'E581:')
2065 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01002066 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002067 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002068
2069 var lines =<< trim END
2070 var s = ''
2071 if s = ''
2072 endif
2073 END
2074 CheckDefFailure(lines, 'E488:')
2075
2076 lines =<< trim END
2077 var s = ''
2078 if s == ''
2079 elseif s = ''
2080 endif
2081 END
2082 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002083enddef
2084
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002085let g:bool_true = v:true
2086let g:bool_false = v:false
2087
2088def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002089 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002090 if true ? true : false
2091 res = true
2092 endif
2093 assert_equal(true, res)
2094
Bram Moolenaar585fea72020-04-02 22:33:21 +02002095 g:glob = 2
2096 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02002097 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002098 endif
2099 assert_equal(2, g:glob)
2100 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02002101 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002102 endif
2103 assert_equal(3, g:glob)
2104
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002105 res = false
2106 if g:bool_true ? true : false
2107 res = true
2108 endif
2109 assert_equal(true, res)
2110
2111 res = false
2112 if true ? g:bool_true : false
2113 res = true
2114 endif
2115 assert_equal(true, res)
2116
2117 res = false
2118 if true ? true : g:bool_false
2119 res = true
2120 endif
2121 assert_equal(true, res)
2122
2123 res = false
2124 if true ? false : true
2125 res = true
2126 endif
2127 assert_equal(false, res)
2128
2129 res = false
2130 if false ? false : true
2131 res = true
2132 endif
2133 assert_equal(true, res)
2134
2135 res = false
2136 if false ? true : false
2137 res = true
2138 endif
2139 assert_equal(false, res)
2140
2141 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002142 if has('xyz') ? true : false
2143 res = true
2144 endif
2145 assert_equal(false, res)
2146
2147 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002148 if true && true
2149 res = true
2150 endif
2151 assert_equal(true, res)
2152
2153 res = false
2154 if true && false
2155 res = true
2156 endif
2157 assert_equal(false, res)
2158
2159 res = false
2160 if g:bool_true && false
2161 res = true
2162 endif
2163 assert_equal(false, res)
2164
2165 res = false
2166 if true && g:bool_false
2167 res = true
2168 endif
2169 assert_equal(false, res)
2170
2171 res = false
2172 if false && false
2173 res = true
2174 endif
2175 assert_equal(false, res)
2176
2177 res = false
2178 if true || false
2179 res = true
2180 endif
2181 assert_equal(true, res)
2182
2183 res = false
2184 if g:bool_true || false
2185 res = true
2186 endif
2187 assert_equal(true, res)
2188
2189 res = false
2190 if true || g:bool_false
2191 res = true
2192 endif
2193 assert_equal(true, res)
2194
2195 res = false
2196 if false || false
2197 res = true
2198 endif
2199 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02002200
2201 # with constant "false" expression may be invalid so long as the syntax is OK
2202 if false | eval 0 | endif
2203 if false | eval burp + 234 | endif
2204 if false | echo burp 234 'asd' | endif
2205 if false
2206 burp
2207 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002208enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002209
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002210def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002211 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
2212 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
2213 CheckDefFailure(["if has('aaa'"], 'E110:')
2214 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002215enddef
2216
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002217def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002218 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002219 if i % 2
2220 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002221 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002222 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002223 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002224 endif
2225 x += 1
2226 else
2227 x += 1000
2228 endif
2229 return x
2230enddef
2231
2232def Test_nested_if()
2233 assert_equal(1, RunNested(1))
2234 assert_equal(1000, RunNested(2))
2235enddef
2236
Bram Moolenaarad39c092020-02-26 18:23:43 +01002237def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01002238 # missing argument is ignored
2239 execute
2240 execute # comment
2241
Bram Moolenaarad39c092020-02-26 18:23:43 +01002242 new
2243 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002244 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002245 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002246
Bram Moolenaard2c61702020-09-06 15:58:36 +02002247 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002248 assert_equal('execute-string', getline(1))
2249
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002250 var cmd1 = 'setline(1,'
2251 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002252 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01002253 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002254
Bram Moolenaard2c61702020-09-06 15:58:36 +02002255 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002256 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002257
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002258 var cmd_first = 'call '
2259 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002260 execute cmd_first .. cmd_last
2261 assert_equal('execute-var-var', getline(1))
2262 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02002263
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002264 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002265 execute 'echomsg' (n ? '"true"' : '"no"')
2266 assert_match('^true$', Screenline(&lines))
2267
Bram Moolenaare0de1712020-12-02 17:36:54 +01002268 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002269 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
2270
Bram Moolenaard2c61702020-09-06 15:58:36 +02002271 CheckDefFailure(['execute xxx'], 'E1001:', 1)
2272 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
2273 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002274enddef
2275
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002276def Test_execute_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 execute 'g:someVar'
2281 .. ' = ' ..
2282 '28'
2283 assert_equal(28, g:someVar)
2284 unlet g:someVar
2285 END
2286 CheckScriptSuccess(lines)
2287enddef
2288
Bram Moolenaarad39c092020-02-26 18:23:43 +01002289def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002290 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02002291 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002292 assert_match('^something$', Screenline(&lines))
2293
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002294 echo "some" # comment
2295 echon "thing"
2296 assert_match('^something$', Screenline(&lines))
2297
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002298 var str1 = 'some'
2299 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002300 echo str1 str2
2301 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002302
Bram Moolenaard2c61702020-09-06 15:58:36 +02002303 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01002304enddef
2305
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002306def Test_echomsg_cmd()
2307 echomsg 'some' 'more' # comment
2308 assert_match('^some more$', Screenline(&lines))
2309 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002310 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002311 assert_match('^some more$', Screenline(&lines))
2312
Bram Moolenaard2c61702020-09-06 15:58:36 +02002313 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002314enddef
2315
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002316def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002317 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002318 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002319 vim9script
2320 echomsg 'here'
2321 .. ' is ' ..
2322 'a message'
2323 assert_match('^here is a message$', Screenline(&lines))
2324 END
2325 CheckScriptSuccess(lines)
2326enddef
2327
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002328def Test_echoerr_cmd()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002329 try
2330 echoerr 'something' 'wrong' # comment
2331 catch
2332 assert_match('something wrong', v:exception)
2333 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002334enddef
2335
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002336def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002337 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002338 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002339 vim9script
2340 try
2341 echoerr 'this'
2342 .. ' is ' ..
2343 'wrong'
2344 catch
2345 assert_match('this is wrong', v:exception)
2346 endtry
2347 END
2348 CheckScriptSuccess(lines)
2349enddef
2350
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002351def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002352 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002353 vim9script
2354 new
2355 for var in range(0, 3)
2356 append(line('$'), var)
2357 endfor
2358 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2359 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01002360
2361 var result = ''
2362 for i in [1, 2, 3]
2363 var loop = ' loop ' .. i
2364 result ..= loop
2365 endfor
2366 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002367 END
2368 writefile(lines, 'Xvim9for.vim')
2369 source Xvim9for.vim
2370 delete('Xvim9for.vim')
2371enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002373def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002374 var lines =<< trim END
2375 var result = ''
2376 for cnt in range(7)
2377 if cnt == 4
2378 break
2379 endif
2380 if cnt == 2
2381 continue
2382 endif
2383 result ..= cnt .. '_'
2384 endfor
2385 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02002386
Bram Moolenaarf2253962021-04-13 20:53:13 +02002387 var concat = ''
2388 for str in eval('["one", "two"]')
2389 concat ..= str
2390 endfor
2391 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002392
Bram Moolenaarf2253962021-04-13 20:53:13 +02002393 var total = 0
2394 for nr in
2395 [1, 2, 3]
2396 total += nr
2397 endfor
2398 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002399
Bram Moolenaarf2253962021-04-13 20:53:13 +02002400 total = 0
2401 for nr
2402 in [1, 2, 3]
2403 total += nr
2404 endfor
2405 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002406
Bram Moolenaarf2253962021-04-13 20:53:13 +02002407 total = 0
2408 for nr
2409 in
2410 [1, 2, 3]
2411 total += nr
2412 endfor
2413 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01002414
Bram Moolenaara3589a02021-04-14 13:30:46 +02002415 # with type
2416 total = 0
2417 for n: number in [1, 2, 3]
2418 total += n
2419 endfor
2420 assert_equal(6, total)
2421
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002422 var chars = ''
2423 for s: string in 'foobar'
2424 chars ..= s
2425 endfor
2426 assert_equal('foobar', chars)
2427
Bram Moolenaara3589a02021-04-14 13:30:46 +02002428 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02002429 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02002430 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2431 res ..= n .. s
2432 endfor
2433 assert_equal('1a2b', res)
2434
Bram Moolenaar444d8782021-06-26 12:40:56 +02002435 # unpack with one var
2436 var reslist = []
2437 for [x] in [['aaa'], ['bbb']]
2438 reslist->add(x)
2439 endfor
2440 assert_equal(['aaa', 'bbb'], reslist)
2441
Bram Moolenaara3589a02021-04-14 13:30:46 +02002442 # loop over string
2443 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02002444 for c in 'aéc̀d'
2445 res ..= c .. '-'
2446 endfor
2447 assert_equal('a-é-c̀-d-', res)
2448
2449 res = ''
2450 for c in ''
2451 res ..= c .. '-'
2452 endfor
2453 assert_equal('', res)
2454
2455 res = ''
2456 for c in test_null_string()
2457 res ..= c .. '-'
2458 endfor
2459 assert_equal('', res)
2460
2461 var foo: list<dict<any>> = [
2462 {a: 'Cat'}
2463 ]
2464 for dd in foo
2465 dd.counter = 12
2466 endfor
2467 assert_equal([{a: 'Cat', counter: 12}], foo)
2468 END
2469 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002470enddef
2471
2472def Test_for_loop_fails()
Bram Moolenaar025cb1c2020-12-14 18:31:27 +01002473 CheckDefFailure(['for '], 'E1097:')
2474 CheckDefFailure(['for x'], 'E1097:')
2475 CheckDefFailure(['for x in'], 'E1097:')
Bram Moolenaar675f7162020-04-12 22:53:54 +02002476 CheckDefFailure(['for # in range(5)'], 'E690:')
2477 CheckDefFailure(['for i In range(5)'], 'E690:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002478 CheckDefFailure(['var x = 5', 'for x in range(5)'], 'E1017:')
Bram Moolenaar822ba242020-05-24 23:00:18 +02002479 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002480 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02002481 CheckDefFailure(['for i in xxx'], 'E1001:')
2482 CheckDefFailure(['endfor'], 'E588:')
2483 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002484
2485 # wrong type detected at compile time
2486 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2487
2488 # wrong type detected at runtime
2489 g:adict = {a: 1}
2490 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2491 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002492
2493 var lines =<< trim END
2494 var d: list<dict<any>> = [{a: 0}]
2495 for e in d
2496 e = {a: 0, b: ''}
2497 endfor
2498 END
2499 CheckDefAndScriptFailure2(lines, 'E1018:', 'E46:', 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002500
2501 lines =<< trim END
2502 for nr: number in ['foo']
2503 endfor
2504 END
2505 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002506enddef
2507
Bram Moolenaarea870692020-12-02 14:24:30 +01002508def Test_for_loop_script_var()
2509 # cannot use s:var in a :def function
2510 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1101:')
2511
2512 # can use s:var in Vim9 script, with or without s:
2513 var lines =<< trim END
2514 vim9script
2515 var total = 0
2516 for s:var in [1, 2, 3]
2517 total += s:var
2518 endfor
2519 assert_equal(6, total)
2520
2521 total = 0
2522 for var in [1, 2, 3]
2523 total += var
2524 endfor
2525 assert_equal(6, total)
2526 END
2527enddef
2528
Bram Moolenaar792f7862020-11-23 08:31:18 +01002529def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01002530 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002531 var result = []
2532 for [v1, v2] in [[1, 2], [3, 4]]
2533 result->add(v1)
2534 result->add(v2)
2535 endfor
2536 assert_equal([1, 2, 3, 4], result)
2537
2538 result = []
2539 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2540 result->add(v1)
2541 result->add(v2)
2542 result->add(v3)
2543 endfor
2544 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
2545
2546 result = []
2547 for [&ts, &sw] in [[1, 2], [3, 4]]
2548 result->add(&ts)
2549 result->add(&sw)
2550 endfor
2551 assert_equal([1, 2, 3, 4], result)
2552
2553 var slist: list<string>
2554 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
2555 slist->add($LOOPVAR)
2556 slist->add(@r)
2557 slist->add(v:errmsg)
2558 endfor
2559 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
2560
2561 slist = []
2562 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
2563 slist->add(g:globalvar)
2564 slist->add(b:bufvar)
2565 slist->add(w:winvar)
2566 slist->add(t:tabvar)
2567 endfor
2568 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01002569 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaarb777da92021-05-22 21:40:39 +02002570
2571 var res = []
2572 for [_, n, _] in [[1, 2, 3], [4, 5, 6]]
2573 res->add(n)
2574 endfor
2575 assert_equal([2, 5], res)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002576 END
2577 CheckDefAndScriptSuccess(lines)
2578
2579 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01002580 for [v1, v2] in [[1, 2, 3], [3, 4]]
2581 echo v1 v2
2582 endfor
2583 END
2584 CheckDefExecFailure(lines, 'E710:', 1)
2585
2586 lines =<< trim END
2587 for [v1, v2] in [[1], [3, 4]]
2588 echo v1 v2
2589 endfor
2590 END
2591 CheckDefExecFailure(lines, 'E711:', 1)
2592
2593 lines =<< trim END
2594 for [v1, v1] in [[1, 2], [3, 4]]
2595 echo v1
2596 endfor
2597 END
2598 CheckDefExecFailure(lines, 'E1017:', 1)
2599enddef
2600
Bram Moolenaarc150c092021-02-13 15:02:46 +01002601def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002602 var lines =<< trim END
2603 var looped = 0
2604 var cleanup = 0
2605 for i in range(3)
2606 looped += 1
2607 try
2608 eval [][0]
2609 catch
2610 continue
2611 finally
2612 cleanup += 1
2613 endtry
2614 endfor
2615 assert_equal(3, looped)
2616 assert_equal(3, cleanup)
2617 END
2618 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002619enddef
2620
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002621def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002622 var result = ''
2623 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002624 while cnt < 555
2625 if cnt == 3
2626 break
2627 endif
2628 cnt += 1
2629 if cnt == 2
2630 continue
2631 endif
2632 result ..= cnt .. '_'
2633 endwhile
2634 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002635
2636 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002637 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002638 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002639enddef
2640
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002641def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02002642 CheckDefFailure(['while xxx'], 'E1001:')
2643 CheckDefFailure(['endwhile'], 'E588:')
2644 CheckDefFailure(['continue'], 'E586:')
2645 CheckDefFailure(['if true', 'continue'], 'E586:')
2646 CheckDefFailure(['break'], 'E587:')
2647 CheckDefFailure(['if true', 'break'], 'E587:')
2648 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002649
2650 var lines =<< trim END
2651 var s = ''
2652 while s = ''
2653 endwhile
2654 END
2655 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002656enddef
2657
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002658def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002659 var caught = false
2660 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002661 try
2662 while 1
2663 x += 1
2664 if x == 100
2665 feedkeys("\<C-C>", 'Lt')
2666 endif
2667 endwhile
2668 catch
2669 caught = true
2670 assert_equal(100, x)
2671 endtry
2672 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02002673 # consume the CTRL-C
2674 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002675enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01002676
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002677def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002678 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002679 'one',
2680 'two',
2681 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002682 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002683 assert_equal(['one', 'two', 'three'], mylist)
2684
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002685 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01002686 ['one']: 1,
2687 ['two']: 2,
2688 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002689 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002690 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01002691 assert_equal({one: 1, two: 2, three: 3}, mydict)
2692 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002693 one: 1, # comment
2694 two: # comment
2695 2, # comment
2696 three: 3 # comment
2697 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002698 assert_equal({one: 1, two: 2, three: 3}, mydict)
2699 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002700 one: 1,
2701 two:
2702 2,
2703 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002704 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002705 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002706
2707 assert_equal(
2708 ['one', 'two', 'three'],
2709 split('one two three')
2710 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002711enddef
2712
Bram Moolenaar7a092242020-04-16 22:10:49 +02002713def Test_vim9_comment()
2714 CheckScriptSuccess([
2715 'vim9script',
2716 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002717 '#something',
2718 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002719 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002720
2721 split Xfile
2722 CheckScriptSuccess([
2723 'vim9script',
2724 'edit #something',
2725 ])
2726 CheckScriptSuccess([
2727 'vim9script',
2728 'edit #{something',
2729 ])
2730 close
2731
Bram Moolenaar7a092242020-04-16 22:10:49 +02002732 CheckScriptFailure([
2733 'vim9script',
2734 ':# something',
2735 ], 'E488:')
2736 CheckScriptFailure([
2737 '# something',
2738 ], 'E488:')
2739 CheckScriptFailure([
2740 ':# something',
2741 ], 'E488:')
2742
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002743 { # block start
2744 } # block end
2745 CheckDefFailure([
2746 '{# comment',
2747 ], 'E488:')
2748 CheckDefFailure([
2749 '{',
2750 '}# comment',
2751 ], 'E488:')
2752
2753 echo "yes" # comment
2754 CheckDefFailure([
2755 'echo "yes"# comment',
2756 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002757 CheckScriptSuccess([
2758 'vim9script',
2759 'echo "yes" # something',
2760 ])
2761 CheckScriptFailure([
2762 'vim9script',
2763 'echo "yes"# something',
2764 ], 'E121:')
2765 CheckScriptFailure([
2766 'vim9script',
2767 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002768 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002769 CheckScriptFailure([
2770 'echo "yes" # something',
2771 ], 'E121:')
2772
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002773 exe "echo" # comment
2774 CheckDefFailure([
2775 'exe "echo"# comment',
2776 ], 'E488:')
2777 CheckScriptSuccess([
2778 'vim9script',
2779 'exe "echo" # something',
2780 ])
2781 CheckScriptFailure([
2782 'vim9script',
2783 'exe "echo"# something',
2784 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002785 CheckScriptFailure([
2786 'vim9script',
2787 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002788 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002789 CheckScriptFailure([
2790 'exe "echo" # something',
2791 ], 'E121:')
2792
Bram Moolenaar7a092242020-04-16 22:10:49 +02002793 CheckDefFailure([
2794 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002795 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002796 'catch',
2797 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002798 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002799 CheckScriptFailure([
2800 'vim9script',
2801 'try# comment',
2802 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002803 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002804 CheckDefFailure([
2805 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002806 ' throw#comment',
2807 'catch',
2808 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002809 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002810 CheckDefFailure([
2811 'try',
2812 ' throw "yes"#comment',
2813 'catch',
2814 'endtry',
2815 ], 'E488:')
2816 CheckDefFailure([
2817 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002818 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002819 'catch# comment',
2820 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002821 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002822 CheckScriptFailure([
2823 'vim9script',
2824 'try',
2825 ' echo "yes"',
2826 'catch# comment',
2827 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002828 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002829 CheckDefFailure([
2830 'try',
2831 ' echo "yes"',
2832 'catch /pat/# comment',
2833 'endtry',
2834 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002835 CheckDefFailure([
2836 'try',
2837 'echo "yes"',
2838 'catch',
2839 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002840 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002841 CheckScriptFailure([
2842 'vim9script',
2843 'try',
2844 ' echo "yes"',
2845 'catch',
2846 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002847 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002848
2849 CheckScriptSuccess([
2850 'vim9script',
2851 'hi # comment',
2852 ])
2853 CheckScriptFailure([
2854 'vim9script',
2855 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002856 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002857 CheckScriptSuccess([
2858 'vim9script',
2859 'hi Search # comment',
2860 ])
2861 CheckScriptFailure([
2862 'vim9script',
2863 'hi Search# comment',
2864 ], 'E416:')
2865 CheckScriptSuccess([
2866 'vim9script',
2867 'hi link This Search # comment',
2868 ])
2869 CheckScriptFailure([
2870 'vim9script',
2871 'hi link This That# comment',
2872 ], 'E413:')
2873 CheckScriptSuccess([
2874 'vim9script',
2875 'hi clear This # comment',
2876 'hi clear # comment',
2877 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002878 # not tested, because it doesn't give an error but a warning:
2879 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02002880 CheckScriptFailure([
2881 'vim9script',
2882 'hi clear# comment',
2883 ], 'E416:')
2884
2885 CheckScriptSuccess([
2886 'vim9script',
2887 'hi Group term=bold',
2888 'match Group /todo/ # comment',
2889 ])
2890 CheckScriptFailure([
2891 'vim9script',
2892 'hi Group term=bold',
2893 'match Group /todo/# comment',
2894 ], 'E488:')
2895 CheckScriptSuccess([
2896 'vim9script',
2897 'match # comment',
2898 ])
2899 CheckScriptFailure([
2900 'vim9script',
2901 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002902 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002903 CheckScriptSuccess([
2904 'vim9script',
2905 'match none # comment',
2906 ])
2907 CheckScriptFailure([
2908 'vim9script',
2909 'match none# comment',
2910 ], 'E475:')
2911
2912 CheckScriptSuccess([
2913 'vim9script',
2914 'menutrans clear # comment',
2915 ])
2916 CheckScriptFailure([
2917 'vim9script',
2918 'menutrans clear# comment text',
2919 ], 'E474:')
2920
2921 CheckScriptSuccess([
2922 'vim9script',
2923 'syntax clear # comment',
2924 ])
2925 CheckScriptFailure([
2926 'vim9script',
2927 'syntax clear# comment text',
2928 ], 'E28:')
2929 CheckScriptSuccess([
2930 'vim9script',
2931 'syntax keyword Word some',
2932 'syntax clear Word # comment',
2933 ])
2934 CheckScriptFailure([
2935 'vim9script',
2936 'syntax keyword Word some',
2937 'syntax clear Word# comment text',
2938 ], 'E28:')
2939
2940 CheckScriptSuccess([
2941 'vim9script',
2942 'syntax list # comment',
2943 ])
2944 CheckScriptFailure([
2945 'vim9script',
2946 'syntax list# comment text',
2947 ], 'E28:')
2948
2949 CheckScriptSuccess([
2950 'vim9script',
2951 'syntax match Word /pat/ oneline # comment',
2952 ])
2953 CheckScriptFailure([
2954 'vim9script',
2955 'syntax match Word /pat/ oneline# comment',
2956 ], 'E475:')
2957
2958 CheckScriptSuccess([
2959 'vim9script',
2960 'syntax keyword Word word # comm[ent',
2961 ])
2962 CheckScriptFailure([
2963 'vim9script',
2964 'syntax keyword Word word# comm[ent',
2965 ], 'E789:')
2966
2967 CheckScriptSuccess([
2968 'vim9script',
2969 'syntax match Word /pat/ # comment',
2970 ])
2971 CheckScriptFailure([
2972 'vim9script',
2973 'syntax match Word /pat/# comment',
2974 ], 'E402:')
2975
2976 CheckScriptSuccess([
2977 'vim9script',
2978 'syntax match Word /pat/ contains=Something # comment',
2979 ])
2980 CheckScriptFailure([
2981 'vim9script',
2982 'syntax match Word /pat/ contains=Something# comment',
2983 ], 'E475:')
2984 CheckScriptFailure([
2985 'vim9script',
2986 'syntax match Word /pat/ contains= # comment',
2987 ], 'E406:')
2988 CheckScriptFailure([
2989 'vim9script',
2990 'syntax match Word /pat/ contains=# comment',
2991 ], 'E475:')
2992
2993 CheckScriptSuccess([
2994 'vim9script',
2995 'syntax region Word start=/pat/ end=/pat/ # comment',
2996 ])
2997 CheckScriptFailure([
2998 'vim9script',
2999 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02003000 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003001
3002 CheckScriptSuccess([
3003 'vim9script',
3004 'syntax sync # comment',
3005 ])
3006 CheckScriptFailure([
3007 'vim9script',
3008 'syntax sync# comment',
3009 ], 'E404:')
3010 CheckScriptSuccess([
3011 'vim9script',
3012 'syntax sync ccomment # comment',
3013 ])
3014 CheckScriptFailure([
3015 'vim9script',
3016 'syntax sync ccomment# comment',
3017 ], 'E404:')
3018
3019 CheckScriptSuccess([
3020 'vim9script',
3021 'syntax cluster Some contains=Word # comment',
3022 ])
3023 CheckScriptFailure([
3024 'vim9script',
3025 'syntax cluster Some contains=Word# comment',
3026 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003027
3028 CheckScriptSuccess([
3029 'vim9script',
3030 'command Echo echo # comment',
3031 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003032 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003033 ])
3034 CheckScriptFailure([
3035 'vim9script',
3036 'command Echo echo# comment',
3037 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003038 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003039 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003040
3041 var curdir = getcwd()
3042 CheckScriptSuccess([
3043 'command Echo cd " comment',
3044 'Echo',
3045 'delcommand Echo',
3046 ])
3047 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01003048 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003049 'command Echo cd # comment',
3050 'Echo',
3051 'delcommand Echo',
3052 ])
3053 CheckScriptFailure([
3054 'vim9script',
3055 'command Echo cd " comment',
3056 'Echo',
3057 ], 'E344:')
3058 delcommand Echo
3059 chdir(curdir)
3060
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003061 CheckScriptFailure([
3062 'vim9script',
3063 'command Echo# comment',
3064 ], 'E182:')
3065 CheckScriptFailure([
3066 'vim9script',
3067 'command Echo echo',
3068 'command Echo# comment',
3069 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003070 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003071
3072 CheckScriptSuccess([
3073 'vim9script',
3074 'function # comment',
3075 ])
3076 CheckScriptFailure([
3077 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02003078 'function " comment',
3079 ], 'E129:')
3080 CheckScriptFailure([
3081 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003082 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003083 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003084 CheckScriptSuccess([
3085 'vim9script',
3086 'function CheckScriptSuccess # comment',
3087 ])
3088 CheckScriptFailure([
3089 'vim9script',
3090 'function CheckScriptSuccess# comment',
3091 ], 'E488:')
3092
3093 CheckScriptSuccess([
3094 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003095 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003096 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003097 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003098 ])
3099 CheckScriptFailure([
3100 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003101 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003102 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003103 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003104 ], 'E488:')
3105
3106 CheckScriptSuccess([
3107 'vim9script',
3108 'call execute("ls") # comment',
3109 ])
3110 CheckScriptFailure([
3111 'vim9script',
3112 'call execute("ls")# comment',
3113 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02003114
3115 CheckScriptFailure([
3116 'def Test() " comment',
3117 'enddef',
3118 ], 'E488:')
3119 CheckScriptFailure([
3120 'vim9script',
3121 'def Test() " comment',
3122 'enddef',
3123 ], 'E488:')
3124
3125 CheckScriptSuccess([
3126 'func Test() " comment',
3127 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003128 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02003129 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02003130 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02003131 'vim9script',
3132 'func Test() " comment',
3133 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02003134 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02003135
3136 CheckScriptSuccess([
3137 'def Test() # comment',
3138 'enddef',
3139 ])
3140 CheckScriptFailure([
3141 'func Test() # comment',
3142 'endfunc',
3143 ], 'E488:')
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003144
3145 var lines =<< trim END
3146 vim9script
3147 syn region Text
3148 \ start='foo'
3149 #\ comment
3150 \ end='bar'
Bram Moolenaar5072b472021-06-03 21:56:10 +02003151 syn region Text start='foo'
3152 #\ comment
3153 \ end='bar'
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003154 END
3155 CheckScriptSuccess(lines)
3156
3157 lines =<< trim END
3158 vim9script
3159 syn region Text
3160 \ start='foo'
3161 "\ comment
3162 \ end='bar'
3163 END
3164 CheckScriptFailure(lines, 'E399:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003165enddef
3166
3167def Test_vim9_comment_gui()
3168 CheckCanRunGui
3169
3170 CheckScriptFailure([
3171 'vim9script',
3172 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003173 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003174 CheckScriptFailure([
3175 'vim9script',
3176 'gui -f#comment'
3177 ], 'E499:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003178enddef
3179
Bram Moolenaara26b9702020-04-18 19:53:28 +02003180def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02003181 au TabEnter *.vim g:entered = 1
3182 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02003183
3184 edit test.vim
3185 doautocmd TabEnter #comment
3186 assert_equal(1, g:entered)
3187
3188 doautocmd TabEnter f.x
3189 assert_equal(2, g:entered)
3190
3191 g:entered = 0
3192 doautocmd TabEnter f.x #comment
3193 assert_equal(2, g:entered)
3194
3195 assert_fails('doautocmd Syntax#comment', 'E216:')
3196
3197 au! TabEnter
3198 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003199
3200 CheckScriptSuccess([
3201 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02003202 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003203 'b:var = 456',
3204 'w:var = 777',
3205 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003206 'unlet g:var w:var # something',
3207 ])
3208
3209 CheckScriptFailure([
3210 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003211 'let var = 123',
3212 ], 'E1126: Cannot use :let in Vim9 script')
3213
3214 CheckScriptFailure([
3215 'vim9script',
3216 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003217 ], 'E1016: Cannot declare a global variable:')
3218
3219 CheckScriptFailure([
3220 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003221 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003222 ], 'E1016: Cannot declare a buffer variable:')
3223
3224 CheckScriptFailure([
3225 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003226 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003227 ], 'E1016: Cannot declare a window variable:')
3228
3229 CheckScriptFailure([
3230 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003231 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003232 ], 'E1016: Cannot declare a tab variable:')
3233
3234 CheckScriptFailure([
3235 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003236 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003237 ], 'E1016: Cannot declare a v: variable:')
3238
3239 CheckScriptFailure([
3240 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003241 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003242 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02003243
3244 CheckScriptFailure([
3245 'vim9script',
3246 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003247 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003248 ], 'E108:')
3249
3250 CheckScriptFailure([
3251 'let g:var = 123',
3252 'unlet g:var # something',
3253 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003254
3255 CheckScriptSuccess([
3256 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003257 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003258 ' echo "yes"',
3259 'elseif 2 #comment',
3260 ' echo "no"',
3261 'endif',
3262 ])
3263
3264 CheckScriptFailure([
3265 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003266 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003267 ' echo "yes"',
3268 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003269 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003270
3271 CheckScriptFailure([
3272 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003273 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003274 ' echo "yes"',
3275 'elseif 2#comment',
3276 ' echo "no"',
3277 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003278 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003279
3280 CheckScriptSuccess([
3281 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003282 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003283 ])
3284
3285 CheckScriptFailure([
3286 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003287 'var v = 1# comment6',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003288 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003289
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003290 CheckScriptSuccess([
3291 'vim9script',
3292 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003293 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003294 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003295 'dsearch /pat/ #comment',
3296 'bwipe!',
3297 ])
3298
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003299 CheckScriptFailure([
3300 'vim9script',
3301 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003302 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003303 ':$',
3304 'dsearch /pat/#comment',
3305 'bwipe!',
3306 ], 'E488:')
3307
3308 CheckScriptFailure([
3309 'vim9script',
3310 'func! SomeFunc()',
3311 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02003312enddef
3313
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003314def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003315 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003316 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02003317 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003318 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02003319 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003320 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02003321 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003322 END
3323 writefile(lines, 'Xfinished')
3324 source Xfinished
3325 assert_equal('two', g:res)
3326
3327 unlet g:res
3328 delete('Xfinished')
3329enddef
3330
Bram Moolenaara5d00772020-05-14 23:20:55 +02003331def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003332 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02003333 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02003334 def GetValue(): string
3335 return theVal
3336 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003337 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02003338 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02003339 theVal = 'else'
3340 g:laterVal = GetValue()
3341 END
3342 writefile(lines, 'Xforward')
3343 source Xforward
3344 assert_equal('something', g:initVal)
3345 assert_equal('else', g:laterVal)
3346
3347 unlet g:initVal
3348 unlet g:laterVal
3349 delete('Xforward')
3350enddef
3351
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003352def Test_source_vim9_from_legacy()
Bram Moolenaara6294952020-12-27 13:39:50 +01003353 var vim9_lines =<< trim END
3354 vim9script
3355 var local = 'local'
3356 g:global = 'global'
3357 export var exported = 'exported'
3358 export def GetText(): string
3359 return 'text'
3360 enddef
3361 END
3362 writefile(vim9_lines, 'Xvim9_script.vim')
3363
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003364 var legacy_lines =<< trim END
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003365 source Xvim9_script.vim
3366
3367 call assert_false(exists('local'))
3368 call assert_false(exists('exported'))
3369 call assert_false(exists('s:exported'))
3370 call assert_equal('global', global)
3371 call assert_equal('global', g:global)
3372
3373 " imported variable becomes script-local
3374 import exported from './Xvim9_script.vim'
3375 call assert_equal('exported', s:exported)
3376 call assert_false(exists('exported'))
3377
3378 " imported function becomes script-local
3379 import GetText from './Xvim9_script.vim'
3380 call assert_equal('text', s:GetText())
3381 call assert_false(exists('*GetText'))
3382 END
3383 writefile(legacy_lines, 'Xlegacy_script.vim')
3384
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003385 source Xlegacy_script.vim
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003386 assert_equal('global', g:global)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003387 unlet g:global
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003388
3389 delete('Xlegacy_script.vim')
3390 delete('Xvim9_script.vim')
3391enddef
Bram Moolenaara5d00772020-05-14 23:20:55 +02003392
Bram Moolenaare535db82021-03-31 21:07:24 +02003393def Test_declare_script_in_func()
3394 var lines =<< trim END
3395 vim9script
3396 func Declare()
3397 let s:local = 123
3398 endfunc
3399 Declare()
3400 assert_equal(123, local)
3401
3402 var error: string
3403 try
3404 local = 'asdf'
3405 catch
3406 error = v:exception
3407 endtry
3408 assert_match('E1012: Type mismatch; expected number but got string', error)
3409
3410 lockvar local
3411 try
3412 local = 999
3413 catch
3414 error = v:exception
3415 endtry
3416 assert_match('E741: Value is locked: local', error)
3417 END
3418 CheckScriptSuccess(lines)
3419enddef
3420
3421
Bram Moolenaar7d699702020-08-14 20:52:28 +02003422func Test_vim9script_not_global()
3423 " check that items defined in Vim9 script are script-local, not global
3424 let vim9lines =<< trim END
3425 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003426 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02003427 func TheFunc()
3428 echo 'local'
3429 endfunc
3430 def DefFunc()
3431 echo 'local'
3432 enddef
3433 END
3434 call writefile(vim9lines, 'Xvim9script.vim')
3435 source Xvim9script.vim
3436 try
3437 echo g:var
3438 assert_report('did not fail')
3439 catch /E121:/
3440 " caught
3441 endtry
3442 try
3443 call TheFunc()
3444 assert_report('did not fail')
3445 catch /E117:/
3446 " caught
3447 endtry
3448 try
3449 call DefFunc()
3450 assert_report('did not fail')
3451 catch /E117:/
3452 " caught
3453 endtry
3454
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003455 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02003456endfunc
3457
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02003458def Test_vim9_copen()
3459 # this was giving an error for setting w:quickfix_title
3460 copen
3461 quit
3462enddef
3463
Bram Moolenaar03290b82020-12-19 16:30:44 +01003464" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003465def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003466 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003467 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01003468 def some#gettest(): string
3469 return 'test'
3470 enddef
3471 g:some#name = 'name'
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003472 g:some#dict = {key: 'value'}
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003473
3474 def some#varargs(a1: string, ...l: list<string>): string
3475 return a1 .. l[0] .. l[1]
3476 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01003477 END
3478
3479 mkdir('Xdir/autoload', 'p')
3480 writefile(lines, 'Xdir/autoload/some.vim')
3481 var save_rtp = &rtp
3482 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3483
3484 assert_equal('test', g:some#gettest())
3485 assert_equal('name', g:some#name)
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003486 assert_equal('value', g:some#dict.key)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003487 g:some#other = 'other'
3488 assert_equal('other', g:some#other)
3489
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003490 assert_equal('abc', some#varargs('a', 'b', 'c'))
3491
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003492 # upper case script name works
3493 lines =<< trim END
3494 vim9script
3495 def Other#getOther(): string
3496 return 'other'
3497 enddef
3498 END
3499 writefile(lines, 'Xdir/autoload/Other.vim')
3500 assert_equal('other', g:Other#getOther())
3501
Bram Moolenaar03290b82020-12-19 16:30:44 +01003502 delete('Xdir', 'rf')
3503 &rtp = save_rtp
3504enddef
3505
3506" test using a vim9script that is auto-loaded from an autocmd
3507def Test_vim9_aucmd_autoload()
3508 var lines =<< trim END
3509 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003510 def foo#test()
3511 echomsg getreg('"')
3512 enddef
3513 END
3514
3515 mkdir('Xdir/autoload', 'p')
3516 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003517 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003518 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3519 augroup test
3520 autocmd TextYankPost * call foo#test()
3521 augroup END
3522
3523 normal Y
3524
3525 augroup test
3526 autocmd!
3527 augroup END
3528 delete('Xdir', 'rf')
3529 &rtp = save_rtp
3530enddef
3531
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003532" This was causing a crash because suppress_errthrow wasn't reset.
3533def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003534 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003535 vim9script
3536 def crash#func()
3537 try
3538 for x in List()
3539 endfor
3540 catch
3541 endtry
3542 g:ok = true
3543 enddef
3544 fu List()
3545 invalid
3546 endfu
3547 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01003548 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003549 catch /wontmatch/
3550 endtry
3551 END
3552 call mkdir('Xruntime/autoload', 'p')
3553 call writefile(lines, 'Xruntime/autoload/crash.vim')
3554
3555 # run in a separate Vim to avoid the side effects of assert_fails()
3556 lines =<< trim END
3557 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
3558 call crash#func()
3559 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003560 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003561 END
3562 writefile(lines, 'Xscript')
3563 RunVim([], [], '-S Xscript')
3564 assert_equal(['ok'], readfile('Xdidit'))
3565
3566 delete('Xdidit')
3567 delete('Xscript')
3568 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01003569
3570 lines =<< trim END
3571 vim9script
3572 var foo#bar = 'asdf'
3573 END
3574 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003575enddef
3576
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003577def Test_script_var_in_autocmd()
3578 # using a script variable from an autocommand, defined in a :def function in a
3579 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003580 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003581 let s:counter = 1
3582 def s:Func()
3583 au! CursorHold
3584 au CursorHold * s:counter += 1
3585 enddef
3586 call s:Func()
3587 doau CursorHold
3588 call assert_equal(2, s:counter)
3589 au! CursorHold
3590 END
3591 CheckScriptSuccess(lines)
3592enddef
3593
Bram Moolenaar3896a102020-08-09 14:33:55 +02003594def Test_cmdline_win()
3595 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
3596 # the command line window.
3597 mkdir('rtp/syntax', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003598 var export_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02003599 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003600 export var That = 'yes'
Bram Moolenaar3896a102020-08-09 14:33:55 +02003601 END
3602 writefile(export_lines, 'rtp/syntax/Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003603 var import_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02003604 vim9script
3605 import That from './Xexport.vim'
3606 END
3607 writefile(import_lines, 'rtp/syntax/vim.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003608 var save_rtp = &rtp
Bram Moolenaar3896a102020-08-09 14:33:55 +02003609 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
3610 syntax on
3611 augroup CmdWin
3612 autocmd CmdwinEnter * g:got_there = 'yes'
3613 augroup END
3614 # this will open and also close the cmdline window
3615 feedkeys('q:', 'xt')
3616 assert_equal('yes', g:got_there)
3617
3618 augroup CmdWin
3619 au!
3620 augroup END
3621 &rtp = save_rtp
3622 delete('rtp', 'rf')
3623enddef
3624
Bram Moolenaare3d46852020-08-29 13:39:17 +02003625def Test_invalid_sid()
3626 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003627
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003628 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02003629 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02003630 endif
3631 delete('Xdidit')
3632enddef
3633
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003634def Test_restoring_cpo()
3635 writefile(['vim9script', 'set nocp'], 'Xsourced')
3636 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
3637 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
3638 assert_equal(['done'], readfile('Xdone'))
3639 endif
3640 delete('Xsourced')
3641 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01003642 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01003643
3644 writefile(['vim9script'], 'XanotherScript')
3645 set cpo=aABceFsMny>
3646 edit XanotherScript
3647 so %
3648 assert_equal('aABceFsMny>', &cpo)
3649 :1del
3650 w
3651 so %
3652 assert_equal('aABceFsMny>', &cpo)
3653
3654 delete('XanotherScript')
3655 set cpo&vim
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003656enddef
3657
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003658" Use :function so we can use Check commands
3659func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003660 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003661 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003662
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003663 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003664 vim9script
3665 def script#func()
3666 enddef
3667 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003668 call mkdir('Xdir/autoload', 'p')
3669 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003670
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003671 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003672 vim9script
3673 set cpo+=M
3674 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02003675 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003676 setline(1, 'some text')
3677 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003678 call writefile(lines, 'XTest_redraw_cpo')
3679 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
3680 call term_sendkeys(buf, "V:")
3681 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003682
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003683 " clean up
3684 call term_sendkeys(buf, "\<Esc>u")
3685 call StopVimInTerminal(buf)
3686 call delete('XTest_redraw_cpo')
3687 call delete('Xdir', 'rf')
3688endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003689
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003690
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003691def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003692 var lines =<< trim END
3693 var name: any
3694 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003695 END
3696 CheckDefAndScriptSuccess(lines)
3697enddef
3698
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003699func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02003700 CheckRunVimInTerminal
3701
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003702 " call indirectly to avoid compilation error for missing functions
3703 call Run_Test_define_func_at_command_line()
3704endfunc
3705
3706def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003707 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003708 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003709 func CheckAndQuit()
3710 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
3711 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
3712 endfunc
3713 END
3714 writefile([''], 'Xdidcmd')
3715 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01003716 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003717 # define Afunc() on the command line
3718 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
3719 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003720 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003721
3722 call StopVimInTerminal(buf)
3723 delete('XcallFunc')
3724 delete('Xdidcmd')
3725enddef
3726
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003727def Test_script_var_scope()
3728 var lines =<< trim END
3729 vim9script
3730 if true
3731 if true
3732 var one = 'one'
3733 echo one
3734 endif
3735 echo one
3736 endif
3737 END
3738 CheckScriptFailure(lines, 'E121:', 7)
3739
3740 lines =<< trim END
3741 vim9script
3742 if true
3743 if false
3744 var one = 'one'
3745 echo one
3746 else
3747 var one = 'one'
3748 echo one
3749 endif
3750 echo one
3751 endif
3752 END
3753 CheckScriptFailure(lines, 'E121:', 10)
3754
3755 lines =<< trim END
3756 vim9script
3757 while true
3758 var one = 'one'
3759 echo one
3760 break
3761 endwhile
3762 echo one
3763 END
3764 CheckScriptFailure(lines, 'E121:', 7)
3765
3766 lines =<< trim END
3767 vim9script
3768 for i in range(1)
3769 var one = 'one'
3770 echo one
3771 endfor
3772 echo one
3773 END
3774 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003775
3776 lines =<< trim END
3777 vim9script
3778 {
3779 var one = 'one'
3780 assert_equal('one', one)
3781 }
3782 assert_false(exists('one'))
3783 assert_false(exists('s:one'))
3784 END
3785 CheckScriptSuccess(lines)
3786
3787 lines =<< trim END
3788 vim9script
3789 {
3790 var one = 'one'
3791 echo one
3792 }
3793 echo one
3794 END
3795 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003796enddef
3797
Bram Moolenaar352134b2020-10-17 22:04:08 +02003798def Test_catch_exception_in_callback()
3799 var lines =<< trim END
3800 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02003801 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02003802 try
3803 var x: string
3804 var y: string
3805 # this error should be caught with CHECKLEN
3806 [x, y] = ['']
3807 catch
3808 g:caught = 'yes'
3809 endtry
3810 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01003811 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02003812 feedkeys("\r", 'xt')
3813 END
3814 CheckScriptSuccess(lines)
3815
3816 unlet g:caught
3817enddef
3818
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003819def Test_no_unknown_error_after_error()
3820 if !has('unix') || !has('job')
3821 throw 'Skipped: not unix of missing +job feature'
3822 endif
3823 var lines =<< trim END
3824 vim9script
3825 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02003826 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003827 eval [][0]
3828 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02003829 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003830 sleep 1m
3831 source += l
3832 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01003833 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01003834 while job_status(myjob) == 'run'
3835 sleep 10m
3836 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01003837 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02003838 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003839 END
3840 writefile(lines, 'Xdef')
3841 assert_fails('so Xdef', ['E684:', 'E1012:'])
3842 delete('Xdef')
3843enddef
3844
Bram Moolenaar4324d872020-12-01 20:12:24 +01003845def InvokeNormal()
3846 exe "norm! :m+1\r"
3847enddef
3848
3849def Test_invoke_normal_in_visual_mode()
3850 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
3851 new
3852 setline(1, ['aaa', 'bbb'])
3853 feedkeys("V\<F3>", 'xt')
3854 assert_equal(['bbb', 'aaa'], getline(1, 2))
3855 xunmap <F3>
3856enddef
3857
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003858def Test_white_space_after_command()
3859 var lines =<< trim END
3860 exit_cb: Func})
3861 END
3862 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003863
3864 lines =<< trim END
3865 e#
3866 END
3867 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003868enddef
3869
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003870def Test_script_var_gone_when_sourced_twice()
3871 var lines =<< trim END
3872 vim9script
3873 if exists('g:guard')
3874 finish
3875 endif
3876 g:guard = 1
3877 var name = 'thename'
3878 def g:GetName(): string
3879 return name
3880 enddef
3881 def g:SetName(arg: string)
3882 name = arg
3883 enddef
3884 END
3885 writefile(lines, 'XscriptTwice.vim')
3886 so XscriptTwice.vim
3887 assert_equal('thename', g:GetName())
3888 g:SetName('newname')
3889 assert_equal('newname', g:GetName())
3890 so XscriptTwice.vim
3891 assert_fails('call g:GetName()', 'E1149:')
3892 assert_fails('call g:SetName("x")', 'E1149:')
3893
3894 delfunc g:GetName
3895 delfunc g:SetName
3896 delete('XscriptTwice.vim')
3897 unlet g:guard
3898enddef
3899
3900def Test_import_gone_when_sourced_twice()
3901 var exportlines =<< trim END
3902 vim9script
3903 if exists('g:guard')
3904 finish
3905 endif
3906 g:guard = 1
3907 export var name = 'someName'
3908 END
3909 writefile(exportlines, 'XexportScript.vim')
3910
3911 var lines =<< trim END
3912 vim9script
3913 import name from './XexportScript.vim'
3914 def g:GetName(): string
3915 return name
3916 enddef
3917 END
3918 writefile(lines, 'XscriptImport.vim')
3919 so XscriptImport.vim
3920 assert_equal('someName', g:GetName())
3921
3922 so XexportScript.vim
3923 assert_fails('call g:GetName()', 'E1149:')
3924
3925 delfunc g:GetName
3926 delete('XexportScript.vim')
3927 delete('XscriptImport.vim')
3928 unlet g:guard
3929enddef
3930
Bram Moolenaar10b94212021-02-19 21:42:57 +01003931def Test_unsupported_commands()
3932 var lines =<< trim END
3933 ka
3934 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003935 CheckDefFailure(lines, 'E476:')
3936 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaar10b94212021-02-19 21:42:57 +01003937
3938 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01003939 :1ka
3940 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003941 CheckDefFailure(lines, 'E476:')
3942 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaarada1d872021-02-20 08:16:51 +01003943
3944 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01003945 t
3946 END
3947 CheckDefFailure(lines, 'E1100:')
3948 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
3949
3950 lines =<< trim END
3951 x
3952 END
3953 CheckDefFailure(lines, 'E1100:')
3954 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
3955
3956 lines =<< trim END
3957 xit
3958 END
3959 CheckDefFailure(lines, 'E1100:')
3960 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
3961enddef
3962
Bram Moolenaarc70fe462021-04-17 17:59:19 +02003963def Test_mapping_line_number()
3964 var lines =<< trim END
3965 vim9script
3966 def g:FuncA()
3967 # Some comment
3968 FuncB(0)
3969 enddef
3970 # Some comment
3971 def FuncB(
3972 # Some comment
3973 n: number
3974 )
3975 exe 'nno '
3976 # Some comment
3977 .. '<F3> a'
3978 .. 'b'
3979 .. 'c'
3980 enddef
3981 END
3982 CheckScriptSuccess(lines)
3983 var res = execute('verbose nmap <F3>')
3984 assert_match('No mapping found', res)
3985
3986 g:FuncA()
3987 res = execute('verbose nmap <F3>')
3988 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
3989
3990 nunmap <F3>
3991 delfunc g:FuncA
3992enddef
3993
Bram Moolenaar208f0b42021-06-20 12:40:08 +02003994def Test_option_modifier()
3995 var lines =<< trim END
3996 set hlsearch & hlsearch !
3997 call assert_equal(1, &hlsearch)
3998 END
3999 CheckScriptSuccess(lines)
4000
4001 lines =<< trim END
4002 vim9script
4003 set hlsearch &
4004 END
4005 CheckScriptFailure(lines, 'E518:')
4006
4007 lines =<< trim END
4008 vim9script
4009 set hlsearch & hlsearch !
4010 END
4011 CheckScriptFailure(lines, 'E518:')
4012enddef
4013
Bram Moolenaar585fea72020-04-02 22:33:21 +02004014" Keep this last, it messes up highlighting.
4015def Test_substitute_cmd()
4016 new
4017 setline(1, 'something')
4018 :substitute(some(other(
4019 assert_equal('otherthing', getline(1))
4020 bwipe!
4021
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004022 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004023 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02004024 vim9script
4025 new
4026 setline(1, 'something')
4027 :substitute(some(other(
4028 assert_equal('otherthing', getline(1))
4029 bwipe!
4030 END
4031 writefile(lines, 'Xvim9lines')
4032 source Xvim9lines
4033
4034 delete('Xvim9lines')
4035enddef
4036
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker