blob: f78eace212badda446b8411bd2535a8bec4fe9f9 [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 Moolenaar41978282021-07-04 14:47:30 +0200606
607 # missing catch and/or finally
608 lines =<< trim END
609 vim9script
610 try
611 echo 'something'
612 endtry
613 END
614 CheckScriptFailure(lines, 'E1032:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615enddef
616
Bram Moolenaar3f987b52021-06-30 12:02:24 +0200617def Test_try_in_catch()
618 var lines =<< trim END
619 vim9script
620 var seq = []
621 def DoIt()
622 try
623 seq->add('throw 1')
624 eval [][0]
625 seq->add('notreached')
626 catch
627 seq->add('catch')
628 try
629 seq->add('throw 2')
630 eval [][0]
631 seq->add('notreached')
632 catch /nothing/
633 seq->add('notreached')
634 endtry
635 seq->add('done')
636 endtry
637 enddef
638 DoIt()
639 assert_equal(['throw 1', 'catch', 'throw 2', 'done'], seq)
640 END
641enddef
642
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200643def Test_error_in_catch()
644 var lines =<< trim END
645 try
646 eval [][0]
647 catch /E684:/
648 eval [][0]
649 endtry
650 END
651 CheckDefExecFailure(lines, 'E684:', 4)
652enddef
653
Bram Moolenaar2e34c342021-03-14 12:13:33 +0100654" :while at the very start of a function that :continue jumps to
655def TryContinueFunc()
656 while g:Count < 2
657 g:sequence ..= 't'
658 try
659 echoerr 'Test'
660 catch
661 g:Count += 1
662 g:sequence ..= 'c'
663 continue
664 endtry
665 g:sequence ..= 'e'
666 g:Count += 1
667 endwhile
668enddef
669
670def Test_continue_in_try_in_while()
671 g:Count = 0
672 g:sequence = ''
673 TryContinueFunc()
674 assert_equal('tctc', g:sequence)
675 unlet g:Count
676 unlet g:sequence
677enddef
678
Bram Moolenaar9cb577a2021-02-22 22:45:10 +0100679def Test_nocatch_return_in_try()
680 # return in try block returns normally
681 def ReturnInTry(): string
682 try
683 return '"some message"'
684 catch
685 endtry
686 return 'not reached'
687 enddef
688 exe 'echoerr ' .. ReturnInTry()
689enddef
690
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100691def Test_cnext_works_in_catch()
692 var lines =<< trim END
693 vim9script
694 au BufEnter * eval 0
695 writefile(['text'], 'Xfile1')
696 writefile(['text'], 'Xfile2')
697 var items = [
698 {lnum: 1, filename: 'Xfile1', valid: true},
699 {lnum: 1, filename: 'Xfile2', valid: true}
700 ]
701 setqflist([], ' ', {items: items})
702 cwindow
703
704 def CnextOrCfirst()
705 # if cnext fails, cfirst is used
706 try
707 cnext
708 catch
709 cfirst
710 endtry
711 enddef
712
713 CnextOrCfirst()
714 CnextOrCfirst()
715 writefile([getqflist({idx: 0}).idx], 'Xresult')
716 qall
717 END
718 writefile(lines, 'XCatchCnext')
719 RunVim([], [], '--clean -S XCatchCnext')
720 assert_equal(['1'], readfile('Xresult'))
721
722 delete('Xfile1')
723 delete('Xfile2')
724 delete('XCatchCnext')
725 delete('Xresult')
726enddef
727
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100728def Test_throw_skipped()
729 if 0
730 throw dontgethere
731 endif
732enddef
733
Bram Moolenaar8f81b222021-01-14 21:47:06 +0100734def Test_nocatch_throw_silenced()
735 var lines =<< trim END
736 vim9script
737 def Func()
738 throw 'error'
739 enddef
740 silent! Func()
741 END
742 writefile(lines, 'XthrowSilenced')
743 source XthrowSilenced
744 delete('XthrowSilenced')
745enddef
746
Bram Moolenaare8593122020-07-18 15:17:02 +0200747def DeletedFunc(): list<any>
748 return ['delete me']
749enddef
750defcompile
751delfunc DeletedFunc
752
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100753def ThrowFromDef()
Bram Moolenaara72cfb82020-04-23 17:07:30 +0200754 throw "getout" # comment
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100755enddef
756
757func CatchInFunc()
758 try
759 call ThrowFromDef()
760 catch
761 let g:thrown_func = v:exception
762 endtry
763endfunc
764
765def CatchInDef()
766 try
767 ThrowFromDef()
768 catch
769 g:thrown_def = v:exception
770 endtry
771enddef
772
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100773def ReturnFinally(): string
774 try
775 return 'intry'
Bram Moolenaar373863e2020-09-26 17:20:53 +0200776 finall
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100777 g:in_finally = 'finally'
778 endtry
779 return 'end'
780enddef
781
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100782def Test_try_catch_nested()
783 CatchInFunc()
784 assert_equal('getout', g:thrown_func)
785
786 CatchInDef()
787 assert_equal('getout', g:thrown_def)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100788
789 assert_equal('intry', ReturnFinally())
790 assert_equal('finally', g:in_finally)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200791
792 var l = []
793 try
794 l->add('1')
795 throw 'bad'
796 l->add('x')
797 catch /bad/
798 l->add('2')
799 try
800 l->add('3')
801 throw 'one'
802 l->add('x')
803 catch /one/
804 l->add('4')
805 try
806 l->add('5')
807 throw 'more'
808 l->add('x')
809 catch /more/
810 l->add('6')
811 endtry
812 endtry
813 endtry
814 assert_equal(['1', '2', '3', '4', '5', '6'], l)
Bram Moolenaar834193a2021-06-30 20:39:15 +0200815
816 l = []
817 try
818 try
819 l->add('1')
820 throw 'foo'
821 l->add('x')
822 catch
823 l->add('2')
824 throw 'bar'
825 l->add('x')
826 finally
827 l->add('3')
828 endtry
829 l->add('x')
830 catch /bar/
831 l->add('4')
832 endtry
833 assert_equal(['1', '2', '3', '4'], l)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100834enddef
835
Bram Moolenaar9939f572020-09-16 22:29:52 +0200836def TryOne(): number
837 try
838 return 0
839 catch
840 endtry
841 return 0
842enddef
843
844def TryTwo(n: number): string
845 try
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200846 var x = {}
Bram Moolenaar9939f572020-09-16 22:29:52 +0200847 catch
848 endtry
849 return 'text'
850enddef
851
852def Test_try_catch_twice()
853 assert_equal('text', TryOne()->TryTwo())
854enddef
855
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100856def Test_try_catch_match()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200857 var seq = 'a'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100858 try
859 throw 'something'
860 catch /nothing/
861 seq ..= 'x'
862 catch /some/
863 seq ..= 'b'
864 catch /asdf/
865 seq ..= 'x'
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200866 catch ?a\?sdf?
867 seq ..= 'y'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100868 finally
869 seq ..= 'c'
870 endtry
871 assert_equal('abc', seq)
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100872enddef
873
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200874def Test_try_catch_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200875 CheckDefFailure(['catch'], 'E603:')
876 CheckDefFailure(['try', 'echo 0', 'catch', 'catch'], 'E1033:')
877 CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
878 CheckDefFailure(['finally'], 'E606:')
879 CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:')
880 CheckDefFailure(['endtry'], 'E602:')
881 CheckDefFailure(['while 1', 'endtry'], 'E170:')
882 CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:')
Bram Moolenaar13106602020-10-04 16:06:05 +0200883 CheckDefFailure(['if 1', 'endtry'], 'E171:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200884 CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:')
Bram Moolenaar585fea72020-04-02 22:33:21 +0200885
Bram Moolenaare4984292020-12-13 14:19:25 +0100886 CheckDefFailure(['throw'], 'E1143:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200887 CheckDefFailure(['throw xxx'], 'E1001:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200888enddef
889
Bram Moolenaar7c5b3c0362021-02-14 22:40:57 +0100890def Try_catch_skipped()
891 var l = []
892 try
893 finally
894 endtry
895
896 if 1
897 else
898 try
899 endtry
900 endif
901enddef
902
903" The skipped try/endtry was updating the wrong instruction.
904def Test_try_catch_skipped()
905 var instr = execute('disassemble Try_catch_skipped')
906 assert_match("NEWLIST size 0\n", instr)
907enddef
908
909
910
Bram Moolenaar006ad482020-06-30 20:55:15 +0200911def Test_throw_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200912 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200913 var lines =<< trim END
Bram Moolenaar006ad482020-06-30 20:55:15 +0200914 vim9script
915 try
916 throw 'one'
917 .. 'two'
918 catch
919 assert_equal('onetwo', v:exception)
920 endtry
921 END
922 CheckScriptSuccess(lines)
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200923
924 lines =<< trim END
925 vim9script
Bram Moolenaar352134b2020-10-17 22:04:08 +0200926 @r = ''
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200927 def Func()
928 throw @r
929 enddef
930 var result = ''
931 try
932 Func()
933 catch /E1129:/
934 result = 'caught'
935 endtry
936 assert_equal('caught', result)
937 END
938 CheckScriptSuccess(lines)
Bram Moolenaar006ad482020-06-30 20:55:15 +0200939enddef
940
Bram Moolenaared677f52020-08-12 16:38:10 +0200941def Test_error_in_nested_function()
Bram Moolenaar03dfde22021-02-14 13:17:22 +0100942 # an error in a nested :function aborts executing in the calling :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200943 var lines =<< trim END
Bram Moolenaared677f52020-08-12 16:38:10 +0200944 vim9script
945 def Func()
946 Error()
947 g:test_var = 1
948 enddef
949 func Error() abort
950 eval [][0]
951 endfunc
952 Func()
953 END
954 g:test_var = 0
955 CheckScriptFailure(lines, 'E684:')
956 assert_equal(0, g:test_var)
957enddef
958
Bram Moolenaar227c58a2021-04-28 20:40:44 +0200959def Test_abort_after_error()
960 var lines =<< trim END
961 vim9script
962 while true
963 echo notfound
964 endwhile
965 g:gotthere = true
966 END
967 g:gotthere = false
968 CheckScriptFailure(lines, 'E121:')
969 assert_false(g:gotthere)
970 unlet g:gotthere
971enddef
972
Bram Moolenaar37c83712020-06-30 21:18:36 +0200973def Test_cexpr_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200974 # only checks line continuation
Bram Moolenaar37c83712020-06-30 21:18:36 +0200975 set errorformat=File\ %f\ line\ %l
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200976 var lines =<< trim END
Bram Moolenaar37c83712020-06-30 21:18:36 +0200977 vim9script
978 cexpr 'File'
979 .. ' someFile' ..
980 ' line 19'
981 assert_equal(19, getqflist()[0].lnum)
982 END
983 CheckScriptSuccess(lines)
984 set errorformat&
985enddef
986
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200987def Test_statusline_syntax()
988 # legacy syntax is used for 'statusline'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200989 var lines =<< trim END
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200990 vim9script
991 func g:Status()
992 return '%{"x" is# "x"}'
993 endfunc
994 set laststatus=2 statusline=%!Status()
995 redrawstatus
996 set laststatus statusline=
997 END
998 CheckScriptSuccess(lines)
999enddef
1000
Bram Moolenaarb2097502020-07-19 17:17:02 +02001001def Test_list_vimscript()
1002 # checks line continuation and comments
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001003 var lines =<< trim END
Bram Moolenaarb2097502020-07-19 17:17:02 +02001004 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001005 var mylist = [
Bram Moolenaarb2097502020-07-19 17:17:02 +02001006 'one',
1007 # comment
1008 'two', # empty line follows
1009
1010 'three',
1011 ]
1012 assert_equal(['one', 'two', 'three'], mylist)
1013 END
1014 CheckScriptSuccess(lines)
Bram Moolenaar66250c92020-08-20 15:02:42 +02001015
1016 # check all lines from heredoc are kept
1017 lines =<< trim END
1018 # comment 1
1019 two
1020 # comment 3
1021
1022 five
1023 # comment 6
1024 END
1025 assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines)
Bram Moolenaar4bce26b2021-01-22 22:06:56 +01001026
1027 lines =<< trim END
1028 [{
1029 a: 0}]->string()->assert_equal("[{'a': 0}]")
1030 END
1031 CheckDefAndScriptSuccess(lines)
Bram Moolenaarb2097502020-07-19 17:17:02 +02001032enddef
1033
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001034if has('channel')
1035 let someJob = test_null_job()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001036
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001037 def FuncWithError()
1038 echomsg g:someJob
1039 enddef
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001040
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001041 func Test_convert_emsg_to_exception()
1042 try
1043 call FuncWithError()
1044 catch
1045 call assert_match('Vim:E908:', v:exception)
1046 endtry
1047 endfunc
1048endif
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050let s:export_script_lines =<< trim END
1051 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001052 var name: string = 'bob'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 def Concat(arg: string): string
1054 return name .. arg
1055 enddef
Bram Moolenaar227a69d2020-05-15 18:17:28 +02001056 g:result = Concat('bie')
1057 g:localname = name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058
1059 export const CONST = 1234
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001060 export var exported = 9876
1061 export var exp_name = 'John'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001062 export def Exported(): string
1063 return 'Exported'
1064 enddef
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001065 export final theList = [1]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066END
1067
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001068def Undo_export_script_lines()
1069 unlet g:result
1070 unlet g:localname
1071enddef
1072
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001073def Test_vim9_import_export()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001074 var import_script_lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 vim9script
1076 import {exported, Exported} from './Xexport.vim'
1077 g:imported = exported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001078 exported += 3
1079 g:imported_added = exported
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 g:imported_func = Exported()
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001081
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001082 def GetExported(): string
Bram Moolenaare0de1712020-12-02 17:36:54 +01001083 var local_dict = {ref: Exported}
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001084 return local_dict.ref()
1085 enddef
1086 g:funcref_result = GetExported()
1087
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001088 import {exp_name} from './Xexport.vim'
1089 g:imported_name = exp_name
1090 exp_name ..= ' Doe'
1091 g:imported_name_appended = exp_name
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001092 g:imported_later = exported
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001093
1094 import theList from './Xexport.vim'
1095 theList->add(2)
1096 assert_equal([1, 2], theList)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 END
1098
1099 writefile(import_script_lines, 'Ximport.vim')
1100 writefile(s:export_script_lines, 'Xexport.vim')
1101
1102 source Ximport.vim
1103
1104 assert_equal('bobbie', g:result)
1105 assert_equal('bob', g:localname)
1106 assert_equal(9876, g:imported)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001107 assert_equal(9879, g:imported_added)
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001108 assert_equal(9879, g:imported_later)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 assert_equal('Exported', g:imported_func)
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001110 assert_equal('Exported', g:funcref_result)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001111 assert_equal('John', g:imported_name)
1112 assert_equal('John Doe', g:imported_name_appended)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113 assert_false(exists('g:name'))
1114
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001115 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 unlet g:imported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001117 unlet g:imported_added
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001118 unlet g:imported_later
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 unlet g:imported_func
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001120 unlet g:imported_name g:imported_name_appended
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 delete('Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001122
Bram Moolenaar1c991142020-07-04 13:15:31 +02001123 # similar, with line breaks
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001124 var import_line_break_script_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001125 vim9script
1126 import {
1127 exported,
1128 Exported,
1129 }
1130 from
1131 './Xexport.vim'
1132 g:imported = exported
1133 exported += 5
1134 g:imported_added = exported
1135 g:imported_func = Exported()
1136 END
1137 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
1138 source Ximport_lbr.vim
1139
1140 assert_equal(9876, g:imported)
1141 assert_equal(9881, g:imported_added)
1142 assert_equal('Exported', g:imported_func)
1143
1144 # exported script not sourced again
1145 assert_false(exists('g:result'))
1146 unlet g:imported
1147 unlet g:imported_added
1148 unlet g:imported_func
1149 delete('Ximport_lbr.vim')
1150
1151 # import inside :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001152 var import_in_def_lines =<< trim END
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001153 vim9script
1154 def ImportInDef()
1155 import exported from './Xexport.vim'
1156 g:imported = exported
1157 exported += 7
1158 g:imported_added = exported
1159 enddef
1160 ImportInDef()
1161 END
1162 writefile(import_in_def_lines, 'Ximport2.vim')
1163 source Ximport2.vim
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001164 # TODO: this should be 9879
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001165 assert_equal(9876, g:imported)
1166 assert_equal(9883, g:imported_added)
1167 unlet g:imported
1168 unlet g:imported_added
1169 delete('Ximport2.vim')
1170
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001171 var import_star_as_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001172 vim9script
1173 import * as Export from './Xexport.vim'
1174 def UseExport()
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001175 g:imported_def = Export.exported
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001176 enddef
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001177 g:imported_script = Export.exported
1178 assert_equal(1, exists('Export.exported'))
1179 assert_equal(0, exists('Export.notexported'))
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001180 UseExport()
1181 END
1182 writefile(import_star_as_lines, 'Ximport.vim')
1183 source Ximport.vim
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001184 assert_equal(9883, g:imported_def)
1185 assert_equal(9883, g:imported_script)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001186
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001187 var import_star_as_lines_no_dot =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001188 vim9script
1189 import * as Export from './Xexport.vim'
1190 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001191 var dummy = 1
1192 var imported = Export + dummy
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001193 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001194 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001195 END
1196 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001197 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001198
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001199 var import_star_as_lines_dot_space =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001200 vim9script
1201 import * as Export from './Xexport.vim'
1202 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001203 var imported = Export . exported
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001204 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001205 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001206 END
1207 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001208 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001209
Bram Moolenaara6294952020-12-27 13:39:50 +01001210 var import_star_as_duplicated =<< trim END
1211 vim9script
1212 import * as Export from './Xexport.vim'
1213 var some = 'other'
1214 import * as Export from './Xexport.vim'
1215 defcompile
1216 END
1217 writefile(import_star_as_duplicated, 'Ximport.vim')
1218 assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim')
1219
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001220 var import_star_as_lines_script_no_dot =<< trim END
1221 vim9script
1222 import * as Export from './Xexport.vim'
1223 g:imported_script = Export exported
1224 END
1225 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
1226 assert_fails('source Ximport.vim', 'E1029:')
1227
1228 var import_star_as_lines_script_space_after_dot =<< trim END
1229 vim9script
1230 import * as Export from './Xexport.vim'
1231 g:imported_script = Export. exported
1232 END
1233 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
1234 assert_fails('source Ximport.vim', 'E1074:')
1235
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001236 var import_star_as_lines_missing_name =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001237 vim9script
1238 import * as Export from './Xexport.vim'
1239 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001240 var imported = Export.
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001241 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001242 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001243 END
1244 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001245 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001246
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001247 var import_star_as_lbr_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001248 vim9script
1249 import *
1250 as Export
1251 from
1252 './Xexport.vim'
1253 def UseExport()
1254 g:imported = Export.exported
1255 enddef
1256 UseExport()
1257 END
1258 writefile(import_star_as_lbr_lines, 'Ximport.vim')
1259 source Ximport.vim
1260 assert_equal(9883, g:imported)
1261
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001262 var import_star_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001263 vim9script
1264 import * from './Xexport.vim'
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001265 END
1266 writefile(import_star_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001267 assert_fails('source Ximport.vim', 'E1045:', '', 2, 'Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001268
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001269 # try to import something that exists but is not exported
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001270 var import_not_exported_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001271 vim9script
1272 import name from './Xexport.vim'
1273 END
1274 writefile(import_not_exported_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001275 assert_fails('source Ximport.vim', 'E1049:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001276
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001277 # try to import something that is already defined
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001278 var import_already_defined =<< trim END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001279 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001280 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001281 import exported from './Xexport.vim'
1282 END
1283 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001284 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001285
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001286 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001287 import_already_defined =<< trim END
1288 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001289 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001290 import * as exported from './Xexport.vim'
1291 END
1292 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001293 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001294
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001295 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001296 import_already_defined =<< trim END
1297 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001298 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001299 import {exported} from './Xexport.vim'
1300 END
1301 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001302 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001303
Bram Moolenaar918a4242020-12-06 14:37:08 +01001304 # try changing an imported const
1305 var import_assign_to_const =<< trim END
1306 vim9script
1307 import CONST from './Xexport.vim'
1308 def Assign()
1309 CONST = 987
1310 enddef
1311 defcompile
1312 END
1313 writefile(import_assign_to_const, 'Ximport.vim')
1314 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1315
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001316 # try changing an imported final
1317 var import_assign_to_final =<< trim END
1318 vim9script
1319 import theList from './Xexport.vim'
1320 def Assign()
1321 theList = [2]
1322 enddef
1323 defcompile
1324 END
1325 writefile(import_assign_to_final, 'Ximport.vim')
1326 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1327
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001328 # import a very long name, requires making a copy
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001329 var import_long_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001330 vim9script
1331 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
1332 END
1333 writefile(import_long_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001334 assert_fails('source Ximport.vim', 'E1048:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001335
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001336 var import_no_from_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001337 vim9script
1338 import name './Xexport.vim'
1339 END
1340 writefile(import_no_from_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001341 assert_fails('source Ximport.vim', 'E1070:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001342
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001343 var import_invalid_string_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001344 vim9script
1345 import name from Xexport.vim
1346 END
1347 writefile(import_invalid_string_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001348 assert_fails('source Ximport.vim', 'E1071:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001349
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001350 var import_wrong_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001351 vim9script
1352 import name from './XnoExport.vim'
1353 END
1354 writefile(import_wrong_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001355 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001356
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001357 var import_missing_comma_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001358 vim9script
1359 import {exported name} from './Xexport.vim'
1360 END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001361 writefile(import_missing_comma_lines, 'Ximport3.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001362 assert_fails('source Ximport3.vim', 'E1046:', '', 2, 'Ximport3.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001363
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001364 delete('Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001365 delete('Ximport3.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 delete('Xexport.vim')
1367
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001368 # Check that in a Vim9 script 'cpo' is set to the Vim default.
Bram Moolenaar3e191692021-03-17 17:46:00 +01001369 # Flags added or removed are also applied to the restored value.
1370 set cpo=abcd
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001371 var lines =<< trim END
Bram Moolenaar750802b2020-02-23 18:08:33 +01001372 vim9script
1373 g:cpo_in_vim9script = &cpo
Bram Moolenaar3e191692021-03-17 17:46:00 +01001374 set cpo+=f
1375 set cpo-=c
1376 g:cpo_after_vim9script = &cpo
Bram Moolenaar750802b2020-02-23 18:08:33 +01001377 END
1378 writefile(lines, 'Xvim9_script')
1379 source Xvim9_script
Bram Moolenaar3e191692021-03-17 17:46:00 +01001380 assert_equal('fabd', &cpo)
Bram Moolenaar750802b2020-02-23 18:08:33 +01001381 set cpo&vim
1382 assert_equal(&cpo, g:cpo_in_vim9script)
Bram Moolenaar3e191692021-03-17 17:46:00 +01001383 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
1384 assert_equal(newcpo, g:cpo_after_vim9script)
1385
Bram Moolenaar750802b2020-02-23 18:08:33 +01001386 delete('Xvim9_script')
1387enddef
1388
Bram Moolenaar0a842842021-02-27 22:41:19 +01001389def Test_import_as()
1390 var export_lines =<< trim END
1391 vim9script
1392 export var one = 1
1393 export var yes = 'yes'
1394 END
1395 writefile(export_lines, 'XexportAs')
1396
1397 var import_lines =<< trim END
1398 vim9script
Bram Moolenaar6c7cc342021-04-17 16:38:50 +02001399 var one = 'notused'
1400 var yes = 777
Bram Moolenaar0a842842021-02-27 22:41:19 +01001401 import one as thatOne from './XexportAs'
1402 assert_equal(1, thatOne)
1403 import yes as yesYes from './XexportAs'
1404 assert_equal('yes', yesYes)
1405 END
1406 CheckScriptSuccess(import_lines)
1407
1408 import_lines =<< trim END
1409 vim9script
1410 import {one as thatOne, yes as yesYes} from './XexportAs'
1411 assert_equal(1, thatOne)
1412 assert_equal('yes', yesYes)
1413 assert_fails('echo one', 'E121:')
1414 assert_fails('echo yes', 'E121:')
1415 END
1416 CheckScriptSuccess(import_lines)
1417
1418 delete('XexportAs')
1419enddef
1420
Bram Moolenaar803af682020-08-05 16:20:03 +02001421func g:Trigger()
1422 source Ximport.vim
1423 return "echo 'yes'\<CR>"
1424endfunc
1425
1426def Test_import_export_expr_map()
1427 # check that :import and :export work when buffer is locked
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001428 var export_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001429 vim9script
1430 export def That(): string
1431 return 'yes'
1432 enddef
1433 END
1434 writefile(export_lines, 'Xexport_that.vim')
1435
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001436 var import_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001437 vim9script
1438 import That from './Xexport_that.vim'
1439 assert_equal('yes', That())
1440 END
1441 writefile(import_lines, 'Ximport.vim')
1442
1443 nnoremap <expr> trigger g:Trigger()
1444 feedkeys('trigger', "xt")
1445
Bram Moolenaar730b2482020-08-09 13:02:10 +02001446 delete('Xexport_that.vim')
Bram Moolenaar803af682020-08-05 16:20:03 +02001447 delete('Ximport.vim')
1448 nunmap trigger
1449enddef
1450
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001451def Test_import_in_filetype()
1452 # check that :import works when the buffer is locked
1453 mkdir('ftplugin', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001454 var export_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001455 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001456 export var That = 'yes'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001457 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001458 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001459
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001460 var import_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001461 vim9script
Bram Moolenaar730b2482020-08-09 13:02:10 +02001462 import That from './Xexport_ft.vim'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001463 assert_equal('yes', That)
1464 g:did_load_mytpe = 1
1465 END
1466 writefile(import_lines, 'ftplugin/qf.vim')
1467
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001468 var save_rtp = &rtp
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001469 &rtp = getcwd() .. ',' .. &rtp
1470
1471 filetype plugin on
1472 copen
1473 assert_equal(1, g:did_load_mytpe)
1474
1475 quit!
Bram Moolenaar730b2482020-08-09 13:02:10 +02001476 delete('Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001477 delete('ftplugin', 'rf')
1478 &rtp = save_rtp
1479enddef
1480
Bram Moolenaarefa94442020-08-08 22:16:00 +02001481def Test_use_import_in_mapping()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001482 var lines =<< trim END
Bram Moolenaarefa94442020-08-08 22:16:00 +02001483 vim9script
1484 export def Funcx()
1485 g:result = 42
1486 enddef
1487 END
1488 writefile(lines, 'XsomeExport.vim')
1489 lines =<< trim END
1490 vim9script
1491 import Funcx from './XsomeExport.vim'
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001492 nnoremap <F3> :call <sid>Funcx()<cr>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001493 END
1494 writefile(lines, 'Xmapscript.vim')
1495
1496 source Xmapscript.vim
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001497 feedkeys("\<F3>", "xt")
Bram Moolenaarefa94442020-08-08 22:16:00 +02001498 assert_equal(42, g:result)
1499
1500 unlet g:result
1501 delete('XsomeExport.vim')
1502 delete('Xmapscript.vim')
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001503 nunmap <F3>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001504enddef
1505
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01001506def Test_vim9script_mix()
1507 var lines =<< trim END
1508 if has(g:feature)
1509 " legacy script
1510 let g:legacy = 1
1511 finish
1512 endif
1513 vim9script
1514 g:legacy = 0
1515 END
1516 g:feature = 'eval'
1517 g:legacy = -1
1518 CheckScriptSuccess(lines)
1519 assert_equal(1, g:legacy)
1520
1521 g:feature = 'noteval'
1522 g:legacy = -1
1523 CheckScriptSuccess(lines)
1524 assert_equal(0, g:legacy)
1525enddef
1526
Bram Moolenaar750802b2020-02-23 18:08:33 +01001527def Test_vim9script_fails()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1529 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001530 CheckScriptFailure(['export var some = 123'], 'E1042:')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001531 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001532 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
Bram Moolenaar750802b2020-02-23 18:08:33 +01001533 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
1534
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001535 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001536 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1537
Bram Moolenaare2e40752020-09-04 21:18:46 +02001538 assert_fails('vim9script', 'E1038:')
1539 assert_fails('export something', 'E1043:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540enddef
1541
Bram Moolenaarf7d267e2020-06-17 12:04:54 +02001542func Test_import_fails_without_script()
Bram Moolenaar101f4812020-06-16 23:18:51 +02001543 CheckRunVimInTerminal
1544
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001545 " call indirectly to avoid compilation error for missing functions
Bram Moolenaarc620c052020-07-08 15:16:19 +02001546 call Run_Test_import_fails_on_command_line()
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001547endfunc
1548
Bram Moolenaarc620c052020-07-08 15:16:19 +02001549def Run_Test_import_fails_on_command_line()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001550 var export =<< trim END
Bram Moolenaar101f4812020-06-16 23:18:51 +02001551 vim9script
1552 export def Foo(): number
1553 return 0
1554 enddef
1555 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001556 writefile(export, 'XexportCmd.vim')
Bram Moolenaar101f4812020-06-16 23:18:51 +02001557
Bram Moolenaare0de1712020-12-02 17:36:54 +01001558 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001559 rows: 6, wait_for_ruler: 0})
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001560 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaar101f4812020-06-16 23:18:51 +02001561
Bram Moolenaar730b2482020-08-09 13:02:10 +02001562 delete('XexportCmd.vim')
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001563 StopVimInTerminal(buf)
1564enddef
Bram Moolenaar101f4812020-06-16 23:18:51 +02001565
Bram Moolenaar2b327002020-12-26 15:39:31 +01001566def Test_vim9script_reload_noclear()
1567 var lines =<< trim END
Bram Moolenaara6294952020-12-27 13:39:50 +01001568 vim9script
1569 export var exported = 'thexport'
1570 END
1571 writefile(lines, 'XExportReload')
1572 lines =<< trim END
Bram Moolenaar2b327002020-12-26 15:39:31 +01001573 vim9script noclear
1574 g:loadCount += 1
1575 var s:reloaded = 'init'
Bram Moolenaara6294952020-12-27 13:39:50 +01001576 import exported from './XExportReload'
Bram Moolenaar2b327002020-12-26 15:39:31 +01001577
1578 def Again(): string
1579 return 'again'
1580 enddef
1581
1582 if exists('s:loaded') | finish | endif
1583 var s:loaded = true
1584
1585 var s:notReloaded = 'yes'
1586 s:reloaded = 'first'
1587 def g:Values(): list<string>
Bram Moolenaara6294952020-12-27 13:39:50 +01001588 return [s:reloaded, s:notReloaded, Again(), Once(), exported]
Bram Moolenaar2b327002020-12-26 15:39:31 +01001589 enddef
1590
1591 def Once(): string
1592 return 'once'
1593 enddef
1594 END
1595 writefile(lines, 'XReloaded')
1596 g:loadCount = 0
1597 source XReloaded
1598 assert_equal(1, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001599 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001600 source XReloaded
1601 assert_equal(2, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001602 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001603 source XReloaded
1604 assert_equal(3, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001605 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001606
Bram Moolenaar48e11c12021-01-11 18:47:00 +01001607 delete('XReloaded')
Bram Moolenaara6294952020-12-27 13:39:50 +01001608 delete('XExportReload')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001609 delfunc g:Values
Bram Moolenaar2b327002020-12-26 15:39:31 +01001610 unlet g:loadCount
Bram Moolenaar577dc932021-06-27 15:35:40 +02001611
1612 lines =<< trim END
1613 vim9script
1614 def Inner()
1615 enddef
1616 END
1617 lines->writefile('XreloadScript.vim')
1618 source XreloadScript.vim
1619
1620 lines =<< trim END
1621 vim9script
1622 def Outer()
1623 def Inner()
1624 enddef
1625 enddef
1626 defcompile
1627 END
1628 lines->writefile('XreloadScript.vim')
1629 source XreloadScript.vim
1630
1631 delete('XreloadScript.vim')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001632enddef
1633
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001634def Test_vim9script_reload_import()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001635 var lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636 vim9script
1637 const var = ''
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001638 var valone = 1234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 def MyFunc(arg: string)
1640 valone = 5678
1641 enddef
1642 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001643 var morelines =<< trim END
1644 var valtwo = 222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 export def GetValtwo(): number
1646 return valtwo
1647 enddef
1648 END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001649 writefile(lines + morelines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650 source Xreload.vim
1651 source Xreload.vim
1652 source Xreload.vim
1653
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001654 var testlines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 vim9script
1656 def TheFunc()
1657 import GetValtwo from './Xreload.vim'
1658 assert_equal(222, GetValtwo())
1659 enddef
1660 TheFunc()
1661 END
1662 writefile(testlines, 'Ximport.vim')
1663 source Ximport.vim
1664
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001665 # Test that when not using "morelines" GetValtwo() and valtwo are still
1666 # defined, because import doesn't reload a script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 writefile(lines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668 source Ximport.vim
1669
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001670 # cannot declare a var twice
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 lines =<< trim END
1672 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001673 var valone = 1234
1674 var valone = 5678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675 END
1676 writefile(lines, 'Xreload.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001677 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678
1679 delete('Xreload.vim')
1680 delete('Ximport.vim')
1681enddef
1682
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001683" if a script is reloaded with a script-local variable that changed its type, a
1684" compiled function using that variable must fail.
1685def Test_script_reload_change_type()
1686 var lines =<< trim END
1687 vim9script noclear
1688 var str = 'string'
1689 def g:GetStr(): string
1690 return str .. 'xxx'
1691 enddef
1692 END
1693 writefile(lines, 'Xreload.vim')
1694 source Xreload.vim
1695 echo g:GetStr()
1696
1697 lines =<< trim END
1698 vim9script noclear
1699 var str = 1234
1700 END
1701 writefile(lines, 'Xreload.vim')
1702 source Xreload.vim
1703 assert_fails('echo g:GetStr()', 'E1150:')
1704
1705 delfunc g:GetStr
1706 delete('Xreload.vim')
1707enddef
1708
Bram Moolenaarc970e422021-03-17 15:03:04 +01001709" Define CallFunc so that the test can be compiled
1710command CallFunc echo 'nop'
1711
1712def Test_script_reload_from_function()
1713 var lines =<< trim END
1714 vim9script
1715
1716 if exists('g:loaded')
1717 finish
1718 endif
1719 g:loaded = 1
1720 delcommand CallFunc
1721 command CallFunc Func()
1722 def Func()
Bram Moolenaara4c81be2021-03-17 15:23:16 +01001723 so XreloadFunc.vim
Bram Moolenaarc970e422021-03-17 15:03:04 +01001724 g:didTheFunc = 1
1725 enddef
1726 END
1727 writefile(lines, 'XreloadFunc.vim')
1728 source XreloadFunc.vim
1729 CallFunc
1730 assert_equal(1, g:didTheFunc)
1731
1732 delete('XreloadFunc.vim')
1733 delcommand CallFunc
1734 unlet g:loaded
1735 unlet g:didTheFunc
1736enddef
1737
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001738def Test_script_var_shadows_function()
1739 var lines =<< trim END
1740 vim9script
1741 def Func(): number
1742 return 123
1743 enddef
1744 var Func = 1
1745 END
1746 CheckScriptFailure(lines, 'E1041:', 5)
1747enddef
1748
Bram Moolenaar95006e32020-08-29 17:47:08 +02001749def s:RetSome(): string
1750 return 'some'
1751enddef
1752
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001753" Not exported function that is referenced needs to be accessed by the
1754" script-local name.
1755def Test_vim9script_funcref()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001756 var sortlines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001757 vim9script
1758 def Compare(i1: number, i2: number): number
Bram Moolenaarbed36b92020-07-07 23:31:36 +02001759 return i2 - i1
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001760 enddef
1761
1762 export def FastSort(): list<number>
1763 return range(5)->sort(Compare)
1764 enddef
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001765
1766 export def GetString(arg: string): string
1767 return arg
1768 enddef
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001769 END
1770 writefile(sortlines, 'Xsort.vim')
1771
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001772 var lines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001773 vim9script
1774 import FastSort from './Xsort.vim'
1775 def Test()
1776 g:result = FastSort()
1777 enddef
1778 Test()
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001779
1780 # using a function imported with "as"
1781 import * as anAlias from './Xsort.vim'
1782 assert_equal('yes', anAlias.GetString('yes'))
1783
1784 # using the function from a compiled function
1785 def TestMore(): string
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001786 var s = s:anAlias.GetString('foo')
1787 return s .. anAlias.GetString('bar')
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001788 enddef
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001789 assert_equal('foobar', TestMore())
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001790
1791 # error when using a function that isn't exported
1792 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001793 END
1794 writefile(lines, 'Xscript.vim')
1795
1796 source Xscript.vim
1797 assert_equal([4, 3, 2, 1, 0], g:result)
1798
1799 unlet g:result
1800 delete('Xsort.vim')
1801 delete('Xscript.vim')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001802
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001803 var Funcref = function('s:RetSome')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001804 assert_equal('some', Funcref())
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001805enddef
1806
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001807" Check that when searching for "FilterFunc" it finds the import in the
1808" script where FastFilter() is called from, both as a string and as a direct
1809" function reference.
Bram Moolenaarc620c052020-07-08 15:16:19 +02001810def Test_vim9script_funcref_other_script()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001811 var filterLines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001812 vim9script
1813 export def FilterFunc(idx: number, val: number): bool
1814 return idx % 2 == 1
1815 enddef
1816 export def FastFilter(): list<number>
1817 return range(10)->filter('FilterFunc')
1818 enddef
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001819 export def FastFilterDirect(): list<number>
1820 return range(10)->filter(FilterFunc)
1821 enddef
Bram Moolenaarc620c052020-07-08 15:16:19 +02001822 END
1823 writefile(filterLines, 'Xfilter.vim')
1824
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001825 var lines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001826 vim9script
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001827 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim'
Bram Moolenaarc620c052020-07-08 15:16:19 +02001828 def Test()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001829 var x: list<number> = FastFilter()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001830 enddef
1831 Test()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001832 def TestDirect()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001833 var x: list<number> = FastFilterDirect()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001834 enddef
1835 TestDirect()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001836 END
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001837 CheckScriptSuccess(lines)
Bram Moolenaarc620c052020-07-08 15:16:19 +02001838 delete('Xfilter.vim')
Bram Moolenaarc620c052020-07-08 15:16:19 +02001839enddef
1840
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001841def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001842 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001843 vim9script
1844 def FuncYes(): string
1845 return 'yes'
1846 enddef
1847 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001848 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001849 def FuncNo(): string
1850 return 'no'
1851 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001852 def g:DoCheck(no_exists: bool)
1853 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001854 assert_equal('no', FuncNo())
1855 enddef
1856 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001857 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001858 def g:DoCheck(no_exists: bool)
1859 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02001860 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001861 enddef
1862 END
1863
1864 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001865 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001866 source Xreloaded.vim
1867 g:DoCheck(true)
1868
1869 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001870 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001871 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001872 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001873
1874 # FuncNo() is back
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001875 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001876 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001877 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001878
1879 delete('Xreloaded.vim')
1880enddef
1881
Bram Moolenaar89483d42020-05-10 15:24:44 +02001882def Test_vim9script_reload_delvar()
1883 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001884 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02001885 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001886 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001887 END
1888 writefile(lines, 'XreloadVar.vim')
1889 source XreloadVar.vim
1890
1891 # now write the script using the same variable locally - works
1892 lines =<< trim END
1893 vim9script
1894 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001895 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001896 enddef
1897 END
1898 writefile(lines, 'XreloadVar.vim')
1899 source XreloadVar.vim
1900
1901 delete('XreloadVar.vim')
1902enddef
1903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904def Test_import_absolute()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001905 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02001906 'vim9script',
1907 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
1908 'def UseExported()',
1909 ' g:imported_abs = exported',
1910 ' exported = 8888',
1911 ' g:imported_after = exported',
1912 'enddef',
1913 'UseExported()',
1914 'g:import_disassembled = execute("disass UseExported")',
1915 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 writefile(import_lines, 'Ximport_abs.vim')
1917 writefile(s:export_script_lines, 'Xexport_abs.vim')
1918
1919 source Ximport_abs.vim
1920
1921 assert_equal(9876, g:imported_abs)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001922 assert_equal(8888, g:imported_after)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001923 assert_match('<SNR>\d\+_UseExported\_s*' ..
1924 'g:imported_abs = exported\_s*' ..
1925 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1926 '1 STOREG g:imported_abs\_s*' ..
1927 'exported = 8888\_s*' ..
1928 '2 PUSHNR 8888\_s*' ..
1929 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1930 'g:imported_after = exported\_s*' ..
1931 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1932 '5 STOREG g:imported_after',
Bram Moolenaare6085c52020-04-12 20:19:16 +02001933 g:import_disassembled)
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001934
1935 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936 unlet g:imported_abs
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001937 unlet g:import_disassembled
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938
1939 delete('Ximport_abs.vim')
1940 delete('Xexport_abs.vim')
1941enddef
1942
1943def Test_import_rtp()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001944 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02001945 'vim9script',
1946 'import exported from "Xexport_rtp.vim"',
1947 'g:imported_rtp = exported',
1948 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 writefile(import_lines, 'Ximport_rtp.vim')
1950 mkdir('import')
1951 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1952
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001953 var save_rtp = &rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001954 &rtp = getcwd()
1955 source Ximport_rtp.vim
1956 &rtp = save_rtp
1957
1958 assert_equal(9876, g:imported_rtp)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001960 Undo_export_script_lines()
1961 unlet g:imported_rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962 delete('Ximport_rtp.vim')
Bram Moolenaar89483d42020-05-10 15:24:44 +02001963 delete('import', 'rf')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964enddef
1965
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001966def Test_import_compile_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001967 var export_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001968 'vim9script',
1969 'export def ExpFunc(): string',
1970 ' return notDefined',
1971 'enddef',
1972 ]
1973 writefile(export_lines, 'Xexported.vim')
1974
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001975 var import_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001976 'vim9script',
1977 'import ExpFunc from "./Xexported.vim"',
1978 'def ImpFunc()',
1979 ' echo ExpFunc()',
1980 'enddef',
1981 'defcompile',
1982 ]
1983 writefile(import_lines, 'Ximport.vim')
1984
1985 try
1986 source Ximport.vim
1987 catch /E1001/
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001988 # Error should be fore the Xexported.vim file.
Bram Moolenaar77072282020-09-16 17:55:40 +02001989 assert_match('E1001: Variable not found: notDefined', v:exception)
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001990 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1991 endtry
1992
1993 delete('Xexported.vim')
1994 delete('Ximport.vim')
1995enddef
1996
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001997def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001998 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001999 'vim9script',
2000 'def Func()',
2001 ' eval [][0]',
2002 'enddef',
2003 'Func()',
2004 ]
2005 writefile(lines, 'Xtestscript.vim')
2006
2007 for count in range(3)
2008 try
2009 source Xtestscript.vim
2010 catch /E684/
2011 # function name should contain <SNR> every time
2012 assert_match('E684: list index out of range', v:exception)
2013 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
2014 endtry
2015 endfor
2016
2017 delete('Xtestscript.vim')
2018enddef
2019
Bram Moolenaareef21022020-08-01 22:16:43 +02002020def Test_func_overrules_import_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002021 var export_lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002022 vim9script
2023 export def Func()
2024 echo 'imported'
2025 enddef
2026 END
2027 writefile(export_lines, 'XexportedFunc.vim')
2028
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002029 var lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002030 vim9script
2031 import Func from './XexportedFunc.vim'
2032 def Func()
2033 echo 'local to function'
2034 enddef
2035 END
2036 CheckScriptFailure(lines, 'E1073:')
2037
2038 lines =<< trim END
2039 vim9script
2040 import Func from './XexportedFunc.vim'
2041 def Outer()
2042 def Func()
2043 echo 'local to function'
2044 enddef
2045 enddef
2046 defcompile
2047 END
2048 CheckScriptFailure(lines, 'E1073:')
2049
2050 delete('XexportedFunc.vim')
2051enddef
2052
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002053def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002054 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002055 vim9script
2056 def Func()
2057 echo 'one'
2058 enddef
2059 def Func()
2060 echo 'two'
2061 enddef
2062 END
2063 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002064
2065 lines =<< trim END
2066 vim9script
2067 def Foo(): string
2068 return 'foo'
2069 enddef
2070 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002071 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002072 enddef
2073 defcompile
2074 END
2075 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002076enddef
2077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002079 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002080 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081 l->remove(0)
2082 l->add(5)
2083 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002084 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085enddef
2086
Bram Moolenaarae616492020-07-28 20:07:27 +02002087def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002088 CheckDefExecFailure(['a = 1'], 'E1100:')
2089 CheckDefExecFailure(['c = 1'], 'E1100:')
2090 CheckDefExecFailure(['i = 1'], 'E1100:')
2091 CheckDefExecFailure(['t = 1'], 'E1100:')
2092 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002093
Bram Moolenaarae616492020-07-28 20:07:27 +02002094 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
2095 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002096 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
2097 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002098 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
2099 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01002100 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
2101 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002102 CheckScriptFailure(['vim9script', 't'], 'E1100:')
2103 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
2104 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002105enddef
2106
Bram Moolenaar158906c2020-02-06 20:39:45 +01002107def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002108 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01002109 if what == 1
2110 res = "one"
2111 elseif what == 2
2112 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002113 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01002114 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002115 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01002116 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002117enddef
2118
Bram Moolenaar158906c2020-02-06 20:39:45 +01002119def Test_if_elseif_else()
2120 assert_equal('one', IfElse(1))
2121 assert_equal('two', IfElse(2))
2122 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002123enddef
2124
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002125def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002126 CheckDefFailure(['elseif true'], 'E582:')
2127 CheckDefFailure(['else'], 'E581:')
2128 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01002129 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002130 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002131
2132 var lines =<< trim END
2133 var s = ''
2134 if s = ''
2135 endif
2136 END
2137 CheckDefFailure(lines, 'E488:')
2138
2139 lines =<< trim END
2140 var s = ''
2141 if s == ''
2142 elseif s = ''
2143 endif
2144 END
2145 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002146enddef
2147
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002148let g:bool_true = v:true
2149let g:bool_false = v:false
2150
2151def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002152 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002153 if true ? true : false
2154 res = true
2155 endif
2156 assert_equal(true, res)
2157
Bram Moolenaar585fea72020-04-02 22:33:21 +02002158 g:glob = 2
2159 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02002160 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002161 endif
2162 assert_equal(2, g:glob)
2163 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02002164 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002165 endif
2166 assert_equal(3, g:glob)
2167
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002168 res = false
2169 if g:bool_true ? true : false
2170 res = true
2171 endif
2172 assert_equal(true, res)
2173
2174 res = false
2175 if true ? g:bool_true : false
2176 res = true
2177 endif
2178 assert_equal(true, res)
2179
2180 res = false
2181 if true ? true : g:bool_false
2182 res = true
2183 endif
2184 assert_equal(true, res)
2185
2186 res = false
2187 if true ? false : true
2188 res = true
2189 endif
2190 assert_equal(false, res)
2191
2192 res = false
2193 if false ? false : true
2194 res = true
2195 endif
2196 assert_equal(true, res)
2197
2198 res = false
2199 if false ? true : false
2200 res = true
2201 endif
2202 assert_equal(false, res)
2203
2204 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002205 if has('xyz') ? true : false
2206 res = true
2207 endif
2208 assert_equal(false, res)
2209
2210 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002211 if true && true
2212 res = true
2213 endif
2214 assert_equal(true, res)
2215
2216 res = false
2217 if true && false
2218 res = true
2219 endif
2220 assert_equal(false, res)
2221
2222 res = false
2223 if g:bool_true && false
2224 res = true
2225 endif
2226 assert_equal(false, res)
2227
2228 res = false
2229 if true && g:bool_false
2230 res = true
2231 endif
2232 assert_equal(false, res)
2233
2234 res = false
2235 if false && false
2236 res = true
2237 endif
2238 assert_equal(false, res)
2239
2240 res = false
2241 if true || false
2242 res = true
2243 endif
2244 assert_equal(true, res)
2245
2246 res = false
2247 if g:bool_true || false
2248 res = true
2249 endif
2250 assert_equal(true, res)
2251
2252 res = false
2253 if true || g:bool_false
2254 res = true
2255 endif
2256 assert_equal(true, res)
2257
2258 res = false
2259 if false || false
2260 res = true
2261 endif
2262 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02002263
2264 # with constant "false" expression may be invalid so long as the syntax is OK
2265 if false | eval 0 | endif
2266 if false | eval burp + 234 | endif
2267 if false | echo burp 234 'asd' | endif
2268 if false
2269 burp
2270 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002271enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002272
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002273def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002274 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
2275 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
2276 CheckDefFailure(["if has('aaa'"], 'E110:')
2277 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002278enddef
2279
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002280def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002281 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002282 if i % 2
2283 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002284 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002285 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002286 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002287 endif
2288 x += 1
2289 else
2290 x += 1000
2291 endif
2292 return x
2293enddef
2294
2295def Test_nested_if()
2296 assert_equal(1, RunNested(1))
2297 assert_equal(1000, RunNested(2))
2298enddef
2299
Bram Moolenaarad39c092020-02-26 18:23:43 +01002300def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01002301 # missing argument is ignored
2302 execute
2303 execute # comment
2304
Bram Moolenaarad39c092020-02-26 18:23:43 +01002305 new
2306 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002307 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002308 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002309
Bram Moolenaard2c61702020-09-06 15:58:36 +02002310 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002311 assert_equal('execute-string', getline(1))
2312
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002313 var cmd1 = 'setline(1,'
2314 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002315 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01002316 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002317
Bram Moolenaard2c61702020-09-06 15:58:36 +02002318 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002319 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002320
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002321 var cmd_first = 'call '
2322 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002323 execute cmd_first .. cmd_last
2324 assert_equal('execute-var-var', getline(1))
2325 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02002326
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002327 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002328 execute 'echomsg' (n ? '"true"' : '"no"')
2329 assert_match('^true$', Screenline(&lines))
2330
Bram Moolenaare0de1712020-12-02 17:36:54 +01002331 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002332 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
2333
Bram Moolenaard2c61702020-09-06 15:58:36 +02002334 CheckDefFailure(['execute xxx'], 'E1001:', 1)
2335 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
2336 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002337enddef
2338
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002339def Test_execute_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002340 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002341 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002342 vim9script
2343 execute 'g:someVar'
2344 .. ' = ' ..
2345 '28'
2346 assert_equal(28, g:someVar)
2347 unlet g:someVar
2348 END
2349 CheckScriptSuccess(lines)
2350enddef
2351
Bram Moolenaarad39c092020-02-26 18:23:43 +01002352def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002353 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02002354 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002355 assert_match('^something$', Screenline(&lines))
2356
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002357 echo "some" # comment
2358 echon "thing"
2359 assert_match('^something$', Screenline(&lines))
2360
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002361 var str1 = 'some'
2362 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002363 echo str1 str2
2364 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002365
Bram Moolenaard2c61702020-09-06 15:58:36 +02002366 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01002367enddef
2368
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002369def Test_echomsg_cmd()
2370 echomsg 'some' 'more' # comment
2371 assert_match('^some more$', Screenline(&lines))
2372 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002373 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002374 assert_match('^some more$', Screenline(&lines))
2375
Bram Moolenaard2c61702020-09-06 15:58:36 +02002376 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002377enddef
2378
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002379def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002380 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002381 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002382 vim9script
2383 echomsg 'here'
2384 .. ' is ' ..
2385 'a message'
2386 assert_match('^here is a message$', Screenline(&lines))
2387 END
2388 CheckScriptSuccess(lines)
2389enddef
2390
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002391def Test_echoerr_cmd()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002392 try
2393 echoerr 'something' 'wrong' # comment
2394 catch
2395 assert_match('something wrong', v:exception)
2396 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002397enddef
2398
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002399def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002400 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002401 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002402 vim9script
2403 try
2404 echoerr 'this'
2405 .. ' is ' ..
2406 'wrong'
2407 catch
2408 assert_match('this is wrong', v:exception)
2409 endtry
2410 END
2411 CheckScriptSuccess(lines)
2412enddef
2413
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002414def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002415 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002416 vim9script
2417 new
2418 for var in range(0, 3)
2419 append(line('$'), var)
2420 endfor
2421 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2422 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01002423
2424 var result = ''
2425 for i in [1, 2, 3]
2426 var loop = ' loop ' .. i
2427 result ..= loop
2428 endfor
2429 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002430 END
2431 writefile(lines, 'Xvim9for.vim')
2432 source Xvim9for.vim
2433 delete('Xvim9for.vim')
2434enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002436def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002437 var lines =<< trim END
2438 var result = ''
2439 for cnt in range(7)
2440 if cnt == 4
2441 break
2442 endif
2443 if cnt == 2
2444 continue
2445 endif
2446 result ..= cnt .. '_'
2447 endfor
2448 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02002449
Bram Moolenaarf2253962021-04-13 20:53:13 +02002450 var concat = ''
2451 for str in eval('["one", "two"]')
2452 concat ..= str
2453 endfor
2454 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002455
Bram Moolenaarf2253962021-04-13 20:53:13 +02002456 var total = 0
2457 for nr in
2458 [1, 2, 3]
2459 total += nr
2460 endfor
2461 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002462
Bram Moolenaarf2253962021-04-13 20:53:13 +02002463 total = 0
2464 for nr
2465 in [1, 2, 3]
2466 total += nr
2467 endfor
2468 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002469
Bram Moolenaarf2253962021-04-13 20:53:13 +02002470 total = 0
2471 for nr
2472 in
2473 [1, 2, 3]
2474 total += nr
2475 endfor
2476 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01002477
Bram Moolenaara3589a02021-04-14 13:30:46 +02002478 # with type
2479 total = 0
2480 for n: number in [1, 2, 3]
2481 total += n
2482 endfor
2483 assert_equal(6, total)
2484
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002485 var chars = ''
2486 for s: string in 'foobar'
2487 chars ..= s
2488 endfor
2489 assert_equal('foobar', chars)
2490
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02002491 chars = ''
2492 for x: string in {a: 'a', b: 'b'}->values()
2493 chars ..= x
2494 endfor
2495 assert_equal('ab', chars)
2496
Bram Moolenaara3589a02021-04-14 13:30:46 +02002497 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02002498 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02002499 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2500 res ..= n .. s
2501 endfor
2502 assert_equal('1a2b', res)
2503
Bram Moolenaar444d8782021-06-26 12:40:56 +02002504 # unpack with one var
2505 var reslist = []
2506 for [x] in [['aaa'], ['bbb']]
2507 reslist->add(x)
2508 endfor
2509 assert_equal(['aaa', 'bbb'], reslist)
2510
Bram Moolenaara3589a02021-04-14 13:30:46 +02002511 # loop over string
2512 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02002513 for c in 'aéc̀d'
2514 res ..= c .. '-'
2515 endfor
2516 assert_equal('a-é-c̀-d-', res)
2517
2518 res = ''
2519 for c in ''
2520 res ..= c .. '-'
2521 endfor
2522 assert_equal('', res)
2523
2524 res = ''
2525 for c in test_null_string()
2526 res ..= c .. '-'
2527 endfor
2528 assert_equal('', res)
2529
2530 var foo: list<dict<any>> = [
2531 {a: 'Cat'}
2532 ]
2533 for dd in foo
2534 dd.counter = 12
2535 endfor
2536 assert_equal([{a: 'Cat', counter: 12}], foo)
2537 END
2538 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002539enddef
2540
2541def Test_for_loop_fails()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002542 CheckDefAndScriptFailure2(['for '], 'E1097:', 'E690:')
2543 CheckDefAndScriptFailure2(['for x'], 'E1097:', 'E690:')
2544 CheckDefAndScriptFailure2(['for x in'], 'E1097:', 'E15:')
2545 CheckDefAndScriptFailure(['for # in range(5)'], 'E690:')
2546 CheckDefAndScriptFailure(['for i In range(5)'], 'E690:')
2547 CheckDefAndScriptFailure2(['var x = 5', 'for x in range(5)', 'endfor'], 'E1017:', 'E1041:')
Bram Moolenaar822ba242020-05-24 23:00:18 +02002548 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002549 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02002550 CheckDefFailure(['for i in xxx'], 'E1001:')
2551 CheckDefFailure(['endfor'], 'E588:')
2552 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002553
2554 # wrong type detected at compile time
2555 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2556
2557 # wrong type detected at runtime
2558 g:adict = {a: 1}
2559 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2560 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002561
2562 var lines =<< trim END
2563 var d: list<dict<any>> = [{a: 0}]
2564 for e in d
2565 e = {a: 0, b: ''}
2566 endfor
2567 END
2568 CheckDefAndScriptFailure2(lines, 'E1018:', 'E46:', 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002569
2570 lines =<< trim END
2571 for nr: number in ['foo']
2572 endfor
2573 END
2574 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaar404557e2021-07-05 21:41:48 +02002575
2576 lines =<< trim END
2577 for n : number in [1, 2]
2578 echo n
2579 endfor
2580 END
2581 CheckDefAndScriptFailure(lines, 'E1059:', 1)
Bram Moolenaarefc5db52021-07-07 21:21:30 +02002582
2583 lines =<< trim END
2584 var d: dict<number> = {a: 1, b: 2}
2585 for [k: job, v: job] in d->items()
2586 echo k v
2587 endfor
2588 END
2589 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected job but got string', 2)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002590enddef
2591
Bram Moolenaarea870692020-12-02 14:24:30 +01002592def Test_for_loop_script_var()
2593 # cannot use s:var in a :def function
2594 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1101:')
2595
2596 # can use s:var in Vim9 script, with or without s:
2597 var lines =<< trim END
2598 vim9script
2599 var total = 0
2600 for s:var in [1, 2, 3]
2601 total += s:var
2602 endfor
2603 assert_equal(6, total)
2604
2605 total = 0
2606 for var in [1, 2, 3]
2607 total += var
2608 endfor
2609 assert_equal(6, total)
2610 END
2611enddef
2612
Bram Moolenaar792f7862020-11-23 08:31:18 +01002613def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01002614 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002615 var result = []
2616 for [v1, v2] in [[1, 2], [3, 4]]
2617 result->add(v1)
2618 result->add(v2)
2619 endfor
2620 assert_equal([1, 2, 3, 4], result)
2621
2622 result = []
2623 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2624 result->add(v1)
2625 result->add(v2)
2626 result->add(v3)
2627 endfor
2628 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
2629
2630 result = []
2631 for [&ts, &sw] in [[1, 2], [3, 4]]
2632 result->add(&ts)
2633 result->add(&sw)
2634 endfor
2635 assert_equal([1, 2, 3, 4], result)
2636
2637 var slist: list<string>
2638 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
2639 slist->add($LOOPVAR)
2640 slist->add(@r)
2641 slist->add(v:errmsg)
2642 endfor
2643 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
2644
2645 slist = []
2646 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
2647 slist->add(g:globalvar)
2648 slist->add(b:bufvar)
2649 slist->add(w:winvar)
2650 slist->add(t:tabvar)
2651 endfor
2652 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01002653 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaarb777da92021-05-22 21:40:39 +02002654
2655 var res = []
2656 for [_, n, _] in [[1, 2, 3], [4, 5, 6]]
2657 res->add(n)
2658 endfor
2659 assert_equal([2, 5], res)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002660 END
2661 CheckDefAndScriptSuccess(lines)
2662
2663 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01002664 for [v1, v2] in [[1, 2, 3], [3, 4]]
2665 echo v1 v2
2666 endfor
2667 END
2668 CheckDefExecFailure(lines, 'E710:', 1)
2669
2670 lines =<< trim END
2671 for [v1, v2] in [[1], [3, 4]]
2672 echo v1 v2
2673 endfor
2674 END
2675 CheckDefExecFailure(lines, 'E711:', 1)
2676
2677 lines =<< trim END
2678 for [v1, v1] in [[1, 2], [3, 4]]
2679 echo v1
2680 endfor
2681 END
2682 CheckDefExecFailure(lines, 'E1017:', 1)
2683enddef
2684
Bram Moolenaarc150c092021-02-13 15:02:46 +01002685def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002686 var lines =<< trim END
2687 var looped = 0
2688 var cleanup = 0
2689 for i in range(3)
2690 looped += 1
2691 try
2692 eval [][0]
2693 catch
2694 continue
2695 finally
2696 cleanup += 1
2697 endtry
2698 endfor
2699 assert_equal(3, looped)
2700 assert_equal(3, cleanup)
2701 END
2702 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002703enddef
2704
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002705def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002706 var result = ''
2707 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002708 while cnt < 555
2709 if cnt == 3
2710 break
2711 endif
2712 cnt += 1
2713 if cnt == 2
2714 continue
2715 endif
2716 result ..= cnt .. '_'
2717 endwhile
2718 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002719
2720 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002721 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002722 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002723enddef
2724
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002725def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02002726 CheckDefFailure(['while xxx'], 'E1001:')
2727 CheckDefFailure(['endwhile'], 'E588:')
2728 CheckDefFailure(['continue'], 'E586:')
2729 CheckDefFailure(['if true', 'continue'], 'E586:')
2730 CheckDefFailure(['break'], 'E587:')
2731 CheckDefFailure(['if true', 'break'], 'E587:')
2732 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002733
2734 var lines =<< trim END
2735 var s = ''
2736 while s = ''
2737 endwhile
2738 END
2739 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002740enddef
2741
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002742def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002743 var caught = false
2744 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002745 try
2746 while 1
2747 x += 1
2748 if x == 100
2749 feedkeys("\<C-C>", 'Lt')
2750 endif
2751 endwhile
2752 catch
2753 caught = true
2754 assert_equal(100, x)
2755 endtry
2756 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02002757 # consume the CTRL-C
2758 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002759enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01002760
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002761def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002762 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002763 'one',
2764 'two',
2765 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002766 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002767 assert_equal(['one', 'two', 'three'], mylist)
2768
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002769 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01002770 ['one']: 1,
2771 ['two']: 2,
2772 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002773 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002774 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01002775 assert_equal({one: 1, two: 2, three: 3}, mydict)
2776 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002777 one: 1, # comment
2778 two: # comment
2779 2, # comment
2780 three: 3 # comment
2781 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002782 assert_equal({one: 1, two: 2, three: 3}, mydict)
2783 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002784 one: 1,
2785 two:
2786 2,
2787 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002788 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002789 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002790
2791 assert_equal(
2792 ['one', 'two', 'three'],
2793 split('one two three')
2794 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002795enddef
2796
Bram Moolenaar7a092242020-04-16 22:10:49 +02002797def Test_vim9_comment()
2798 CheckScriptSuccess([
2799 'vim9script',
2800 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002801 '#something',
2802 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002803 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002804
2805 split Xfile
2806 CheckScriptSuccess([
2807 'vim9script',
2808 'edit #something',
2809 ])
2810 CheckScriptSuccess([
2811 'vim9script',
2812 'edit #{something',
2813 ])
2814 close
2815
Bram Moolenaar7a092242020-04-16 22:10:49 +02002816 CheckScriptFailure([
2817 'vim9script',
2818 ':# something',
2819 ], 'E488:')
2820 CheckScriptFailure([
2821 '# something',
2822 ], 'E488:')
2823 CheckScriptFailure([
2824 ':# something',
2825 ], 'E488:')
2826
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002827 { # block start
2828 } # block end
2829 CheckDefFailure([
2830 '{# comment',
2831 ], 'E488:')
2832 CheckDefFailure([
2833 '{',
2834 '}# comment',
2835 ], 'E488:')
2836
2837 echo "yes" # comment
2838 CheckDefFailure([
2839 'echo "yes"# comment',
2840 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002841 CheckScriptSuccess([
2842 'vim9script',
2843 'echo "yes" # something',
2844 ])
2845 CheckScriptFailure([
2846 'vim9script',
2847 'echo "yes"# something',
2848 ], 'E121:')
2849 CheckScriptFailure([
2850 'vim9script',
2851 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002852 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002853 CheckScriptFailure([
2854 'echo "yes" # something',
2855 ], 'E121:')
2856
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002857 exe "echo" # comment
2858 CheckDefFailure([
2859 'exe "echo"# comment',
2860 ], 'E488:')
2861 CheckScriptSuccess([
2862 'vim9script',
2863 'exe "echo" # something',
2864 ])
2865 CheckScriptFailure([
2866 'vim9script',
2867 'exe "echo"# something',
2868 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002869 CheckScriptFailure([
2870 'vim9script',
2871 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002872 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002873 CheckScriptFailure([
2874 'exe "echo" # something',
2875 ], 'E121:')
2876
Bram Moolenaar7a092242020-04-16 22:10:49 +02002877 CheckDefFailure([
2878 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002879 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002880 'catch',
2881 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002882 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002883 CheckScriptFailure([
2884 'vim9script',
2885 'try# comment',
2886 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002887 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002888 CheckDefFailure([
2889 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002890 ' throw#comment',
2891 'catch',
2892 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002893 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002894 CheckDefFailure([
2895 'try',
2896 ' throw "yes"#comment',
2897 'catch',
2898 'endtry',
2899 ], 'E488:')
2900 CheckDefFailure([
2901 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002902 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002903 'catch# comment',
2904 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002905 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002906 CheckScriptFailure([
2907 'vim9script',
2908 'try',
2909 ' echo "yes"',
2910 'catch# comment',
2911 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002912 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002913 CheckDefFailure([
2914 'try',
2915 ' echo "yes"',
2916 'catch /pat/# comment',
2917 'endtry',
2918 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002919 CheckDefFailure([
2920 'try',
2921 'echo "yes"',
2922 'catch',
2923 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002924 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002925 CheckScriptFailure([
2926 'vim9script',
2927 'try',
2928 ' echo "yes"',
2929 'catch',
2930 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002931 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002932
2933 CheckScriptSuccess([
2934 'vim9script',
2935 'hi # comment',
2936 ])
2937 CheckScriptFailure([
2938 'vim9script',
2939 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002940 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002941 CheckScriptSuccess([
2942 'vim9script',
2943 'hi Search # comment',
2944 ])
2945 CheckScriptFailure([
2946 'vim9script',
2947 'hi Search# comment',
2948 ], 'E416:')
2949 CheckScriptSuccess([
2950 'vim9script',
2951 'hi link This Search # comment',
2952 ])
2953 CheckScriptFailure([
2954 'vim9script',
2955 'hi link This That# comment',
2956 ], 'E413:')
2957 CheckScriptSuccess([
2958 'vim9script',
2959 'hi clear This # comment',
2960 'hi clear # comment',
2961 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002962 # not tested, because it doesn't give an error but a warning:
2963 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02002964 CheckScriptFailure([
2965 'vim9script',
2966 'hi clear# comment',
2967 ], 'E416:')
2968
2969 CheckScriptSuccess([
2970 'vim9script',
2971 'hi Group term=bold',
2972 'match Group /todo/ # comment',
2973 ])
2974 CheckScriptFailure([
2975 'vim9script',
2976 'hi Group term=bold',
2977 'match Group /todo/# comment',
2978 ], 'E488:')
2979 CheckScriptSuccess([
2980 'vim9script',
2981 'match # comment',
2982 ])
2983 CheckScriptFailure([
2984 'vim9script',
2985 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002986 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002987 CheckScriptSuccess([
2988 'vim9script',
2989 'match none # comment',
2990 ])
2991 CheckScriptFailure([
2992 'vim9script',
2993 'match none# comment',
2994 ], 'E475:')
2995
2996 CheckScriptSuccess([
2997 'vim9script',
2998 'menutrans clear # comment',
2999 ])
3000 CheckScriptFailure([
3001 'vim9script',
3002 'menutrans clear# comment text',
3003 ], 'E474:')
3004
3005 CheckScriptSuccess([
3006 'vim9script',
3007 'syntax clear # comment',
3008 ])
3009 CheckScriptFailure([
3010 'vim9script',
3011 'syntax clear# comment text',
3012 ], 'E28:')
3013 CheckScriptSuccess([
3014 'vim9script',
3015 'syntax keyword Word some',
3016 'syntax clear Word # comment',
3017 ])
3018 CheckScriptFailure([
3019 'vim9script',
3020 'syntax keyword Word some',
3021 'syntax clear Word# comment text',
3022 ], 'E28:')
3023
3024 CheckScriptSuccess([
3025 'vim9script',
3026 'syntax list # comment',
3027 ])
3028 CheckScriptFailure([
3029 'vim9script',
3030 'syntax list# comment text',
3031 ], 'E28:')
3032
3033 CheckScriptSuccess([
3034 'vim9script',
3035 'syntax match Word /pat/ oneline # comment',
3036 ])
3037 CheckScriptFailure([
3038 'vim9script',
3039 'syntax match Word /pat/ oneline# comment',
3040 ], 'E475:')
3041
3042 CheckScriptSuccess([
3043 'vim9script',
3044 'syntax keyword Word word # comm[ent',
3045 ])
3046 CheckScriptFailure([
3047 'vim9script',
3048 'syntax keyword Word word# comm[ent',
3049 ], 'E789:')
3050
3051 CheckScriptSuccess([
3052 'vim9script',
3053 'syntax match Word /pat/ # comment',
3054 ])
3055 CheckScriptFailure([
3056 'vim9script',
3057 'syntax match Word /pat/# comment',
3058 ], 'E402:')
3059
3060 CheckScriptSuccess([
3061 'vim9script',
3062 'syntax match Word /pat/ contains=Something # comment',
3063 ])
3064 CheckScriptFailure([
3065 'vim9script',
3066 'syntax match Word /pat/ contains=Something# comment',
3067 ], 'E475:')
3068 CheckScriptFailure([
3069 'vim9script',
3070 'syntax match Word /pat/ contains= # comment',
3071 ], 'E406:')
3072 CheckScriptFailure([
3073 'vim9script',
3074 'syntax match Word /pat/ contains=# comment',
3075 ], 'E475:')
3076
3077 CheckScriptSuccess([
3078 'vim9script',
3079 'syntax region Word start=/pat/ end=/pat/ # comment',
3080 ])
3081 CheckScriptFailure([
3082 'vim9script',
3083 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02003084 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003085
3086 CheckScriptSuccess([
3087 'vim9script',
3088 'syntax sync # comment',
3089 ])
3090 CheckScriptFailure([
3091 'vim9script',
3092 'syntax sync# comment',
3093 ], 'E404:')
3094 CheckScriptSuccess([
3095 'vim9script',
3096 'syntax sync ccomment # comment',
3097 ])
3098 CheckScriptFailure([
3099 'vim9script',
3100 'syntax sync ccomment# comment',
3101 ], 'E404:')
3102
3103 CheckScriptSuccess([
3104 'vim9script',
3105 'syntax cluster Some contains=Word # comment',
3106 ])
3107 CheckScriptFailure([
3108 'vim9script',
3109 'syntax cluster Some contains=Word# comment',
3110 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003111
3112 CheckScriptSuccess([
3113 'vim9script',
3114 'command Echo echo # comment',
3115 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003116 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003117 ])
3118 CheckScriptFailure([
3119 'vim9script',
3120 'command Echo echo# comment',
3121 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003122 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003123 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003124
3125 var curdir = getcwd()
3126 CheckScriptSuccess([
3127 'command Echo cd " comment',
3128 'Echo',
3129 'delcommand Echo',
3130 ])
3131 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01003132 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003133 'command Echo cd # comment',
3134 'Echo',
3135 'delcommand Echo',
3136 ])
3137 CheckScriptFailure([
3138 'vim9script',
3139 'command Echo cd " comment',
3140 'Echo',
3141 ], 'E344:')
3142 delcommand Echo
3143 chdir(curdir)
3144
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003145 CheckScriptFailure([
3146 'vim9script',
3147 'command Echo# comment',
3148 ], 'E182:')
3149 CheckScriptFailure([
3150 'vim9script',
3151 'command Echo echo',
3152 'command Echo# comment',
3153 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003154 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003155
3156 CheckScriptSuccess([
3157 'vim9script',
3158 'function # comment',
3159 ])
3160 CheckScriptFailure([
3161 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02003162 'function " comment',
3163 ], 'E129:')
3164 CheckScriptFailure([
3165 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003166 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003167 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003168 CheckScriptSuccess([
3169 'vim9script',
3170 'function CheckScriptSuccess # comment',
3171 ])
3172 CheckScriptFailure([
3173 'vim9script',
3174 'function CheckScriptSuccess# comment',
3175 ], 'E488:')
3176
3177 CheckScriptSuccess([
3178 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003179 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003180 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003181 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003182 ])
3183 CheckScriptFailure([
3184 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003185 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003186 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003187 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003188 ], 'E488:')
3189
3190 CheckScriptSuccess([
3191 'vim9script',
3192 'call execute("ls") # comment',
3193 ])
3194 CheckScriptFailure([
3195 'vim9script',
3196 'call execute("ls")# comment',
3197 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02003198
3199 CheckScriptFailure([
3200 'def Test() " comment',
3201 'enddef',
3202 ], 'E488:')
3203 CheckScriptFailure([
3204 'vim9script',
3205 'def Test() " comment',
3206 'enddef',
3207 ], 'E488:')
3208
3209 CheckScriptSuccess([
3210 'func Test() " comment',
3211 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003212 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02003213 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02003214 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02003215 'vim9script',
3216 'func Test() " comment',
3217 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02003218 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02003219
3220 CheckScriptSuccess([
3221 'def Test() # comment',
3222 'enddef',
3223 ])
3224 CheckScriptFailure([
3225 'func Test() # comment',
3226 'endfunc',
3227 ], 'E488:')
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003228
3229 var lines =<< trim END
3230 vim9script
3231 syn region Text
3232 \ start='foo'
3233 #\ comment
3234 \ end='bar'
Bram Moolenaar5072b472021-06-03 21:56:10 +02003235 syn region Text start='foo'
3236 #\ comment
3237 \ end='bar'
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003238 END
3239 CheckScriptSuccess(lines)
3240
3241 lines =<< trim END
3242 vim9script
3243 syn region Text
3244 \ start='foo'
3245 "\ comment
3246 \ end='bar'
3247 END
3248 CheckScriptFailure(lines, 'E399:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003249enddef
3250
3251def Test_vim9_comment_gui()
3252 CheckCanRunGui
3253
3254 CheckScriptFailure([
3255 'vim9script',
3256 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003257 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003258 CheckScriptFailure([
3259 'vim9script',
3260 'gui -f#comment'
3261 ], 'E499:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003262enddef
3263
Bram Moolenaara26b9702020-04-18 19:53:28 +02003264def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02003265 au TabEnter *.vim g:entered = 1
3266 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02003267
3268 edit test.vim
3269 doautocmd TabEnter #comment
3270 assert_equal(1, g:entered)
3271
3272 doautocmd TabEnter f.x
3273 assert_equal(2, g:entered)
3274
3275 g:entered = 0
3276 doautocmd TabEnter f.x #comment
3277 assert_equal(2, g:entered)
3278
3279 assert_fails('doautocmd Syntax#comment', 'E216:')
3280
3281 au! TabEnter
3282 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003283
3284 CheckScriptSuccess([
3285 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02003286 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003287 'b:var = 456',
3288 'w:var = 777',
3289 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003290 'unlet g:var w:var # something',
3291 ])
3292
3293 CheckScriptFailure([
3294 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003295 'let var = 123',
3296 ], 'E1126: Cannot use :let in Vim9 script')
3297
3298 CheckScriptFailure([
3299 'vim9script',
3300 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003301 ], 'E1016: Cannot declare a global variable:')
3302
3303 CheckScriptFailure([
3304 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003305 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003306 ], 'E1016: Cannot declare a buffer variable:')
3307
3308 CheckScriptFailure([
3309 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003310 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003311 ], 'E1016: Cannot declare a window variable:')
3312
3313 CheckScriptFailure([
3314 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003315 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003316 ], 'E1016: Cannot declare a tab variable:')
3317
3318 CheckScriptFailure([
3319 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003320 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003321 ], 'E1016: Cannot declare a v: variable:')
3322
3323 CheckScriptFailure([
3324 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003325 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003326 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02003327
3328 CheckScriptFailure([
3329 'vim9script',
3330 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003331 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003332 ], 'E108:')
3333
3334 CheckScriptFailure([
3335 'let g:var = 123',
3336 'unlet g:var # something',
3337 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003338
3339 CheckScriptSuccess([
3340 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003341 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003342 ' echo "yes"',
3343 'elseif 2 #comment',
3344 ' echo "no"',
3345 'endif',
3346 ])
3347
3348 CheckScriptFailure([
3349 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003350 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003351 ' echo "yes"',
3352 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003353 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003354
3355 CheckScriptFailure([
3356 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003357 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003358 ' echo "yes"',
3359 'elseif 2#comment',
3360 ' echo "no"',
3361 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003362 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003363
3364 CheckScriptSuccess([
3365 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003366 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003367 ])
3368
3369 CheckScriptFailure([
3370 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003371 'var v = 1# comment6',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003372 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003373
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003374 CheckScriptSuccess([
3375 'vim9script',
3376 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003377 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003378 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003379 'dsearch /pat/ #comment',
3380 'bwipe!',
3381 ])
3382
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003383 CheckScriptFailure([
3384 'vim9script',
3385 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003386 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003387 ':$',
3388 'dsearch /pat/#comment',
3389 'bwipe!',
3390 ], 'E488:')
3391
3392 CheckScriptFailure([
3393 'vim9script',
3394 'func! SomeFunc()',
3395 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02003396enddef
3397
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003398def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003399 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003400 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02003401 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003402 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02003403 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003404 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02003405 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003406 END
3407 writefile(lines, 'Xfinished')
3408 source Xfinished
3409 assert_equal('two', g:res)
3410
3411 unlet g:res
3412 delete('Xfinished')
3413enddef
3414
Bram Moolenaara5d00772020-05-14 23:20:55 +02003415def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003416 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02003417 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02003418 def GetValue(): string
3419 return theVal
3420 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003421 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02003422 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02003423 theVal = 'else'
3424 g:laterVal = GetValue()
3425 END
3426 writefile(lines, 'Xforward')
3427 source Xforward
3428 assert_equal('something', g:initVal)
3429 assert_equal('else', g:laterVal)
3430
3431 unlet g:initVal
3432 unlet g:laterVal
3433 delete('Xforward')
3434enddef
3435
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003436def Test_source_vim9_from_legacy()
Bram Moolenaara6294952020-12-27 13:39:50 +01003437 var vim9_lines =<< trim END
3438 vim9script
3439 var local = 'local'
3440 g:global = 'global'
3441 export var exported = 'exported'
3442 export def GetText(): string
3443 return 'text'
3444 enddef
3445 END
3446 writefile(vim9_lines, 'Xvim9_script.vim')
3447
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003448 var legacy_lines =<< trim END
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003449 source Xvim9_script.vim
3450
3451 call assert_false(exists('local'))
3452 call assert_false(exists('exported'))
3453 call assert_false(exists('s:exported'))
3454 call assert_equal('global', global)
3455 call assert_equal('global', g:global)
3456
3457 " imported variable becomes script-local
3458 import exported from './Xvim9_script.vim'
3459 call assert_equal('exported', s:exported)
3460 call assert_false(exists('exported'))
3461
3462 " imported function becomes script-local
3463 import GetText from './Xvim9_script.vim'
3464 call assert_equal('text', s:GetText())
3465 call assert_false(exists('*GetText'))
3466 END
3467 writefile(legacy_lines, 'Xlegacy_script.vim')
3468
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003469 source Xlegacy_script.vim
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003470 assert_equal('global', g:global)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003471 unlet g:global
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003472
3473 delete('Xlegacy_script.vim')
3474 delete('Xvim9_script.vim')
3475enddef
Bram Moolenaara5d00772020-05-14 23:20:55 +02003476
Bram Moolenaare535db82021-03-31 21:07:24 +02003477def Test_declare_script_in_func()
3478 var lines =<< trim END
3479 vim9script
3480 func Declare()
3481 let s:local = 123
3482 endfunc
3483 Declare()
3484 assert_equal(123, local)
3485
3486 var error: string
3487 try
3488 local = 'asdf'
3489 catch
3490 error = v:exception
3491 endtry
3492 assert_match('E1012: Type mismatch; expected number but got string', error)
3493
3494 lockvar local
3495 try
3496 local = 999
3497 catch
3498 error = v:exception
3499 endtry
3500 assert_match('E741: Value is locked: local', error)
3501 END
3502 CheckScriptSuccess(lines)
3503enddef
3504
3505
Bram Moolenaar7d699702020-08-14 20:52:28 +02003506func Test_vim9script_not_global()
3507 " check that items defined in Vim9 script are script-local, not global
3508 let vim9lines =<< trim END
3509 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003510 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02003511 func TheFunc()
3512 echo 'local'
3513 endfunc
3514 def DefFunc()
3515 echo 'local'
3516 enddef
3517 END
3518 call writefile(vim9lines, 'Xvim9script.vim')
3519 source Xvim9script.vim
3520 try
3521 echo g:var
3522 assert_report('did not fail')
3523 catch /E121:/
3524 " caught
3525 endtry
3526 try
3527 call TheFunc()
3528 assert_report('did not fail')
3529 catch /E117:/
3530 " caught
3531 endtry
3532 try
3533 call DefFunc()
3534 assert_report('did not fail')
3535 catch /E117:/
3536 " caught
3537 endtry
3538
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003539 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02003540endfunc
3541
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02003542def Test_vim9_copen()
3543 # this was giving an error for setting w:quickfix_title
3544 copen
3545 quit
3546enddef
3547
Bram Moolenaar03290b82020-12-19 16:30:44 +01003548" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003549def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003550 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003551 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01003552 def some#gettest(): string
3553 return 'test'
3554 enddef
3555 g:some#name = 'name'
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003556 g:some#dict = {key: 'value'}
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003557
3558 def some#varargs(a1: string, ...l: list<string>): string
3559 return a1 .. l[0] .. l[1]
3560 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01003561 END
3562
3563 mkdir('Xdir/autoload', 'p')
3564 writefile(lines, 'Xdir/autoload/some.vim')
3565 var save_rtp = &rtp
3566 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3567
3568 assert_equal('test', g:some#gettest())
3569 assert_equal('name', g:some#name)
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003570 assert_equal('value', g:some#dict.key)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003571 g:some#other = 'other'
3572 assert_equal('other', g:some#other)
3573
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003574 assert_equal('abc', some#varargs('a', 'b', 'c'))
3575
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003576 # upper case script name works
3577 lines =<< trim END
3578 vim9script
3579 def Other#getOther(): string
3580 return 'other'
3581 enddef
3582 END
3583 writefile(lines, 'Xdir/autoload/Other.vim')
3584 assert_equal('other', g:Other#getOther())
3585
Bram Moolenaar03290b82020-12-19 16:30:44 +01003586 delete('Xdir', 'rf')
3587 &rtp = save_rtp
3588enddef
3589
3590" test using a vim9script that is auto-loaded from an autocmd
3591def Test_vim9_aucmd_autoload()
3592 var lines =<< trim END
3593 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003594 def foo#test()
3595 echomsg getreg('"')
3596 enddef
3597 END
3598
3599 mkdir('Xdir/autoload', 'p')
3600 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003601 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003602 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3603 augroup test
3604 autocmd TextYankPost * call foo#test()
3605 augroup END
3606
3607 normal Y
3608
3609 augroup test
3610 autocmd!
3611 augroup END
3612 delete('Xdir', 'rf')
3613 &rtp = save_rtp
3614enddef
3615
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003616" This was causing a crash because suppress_errthrow wasn't reset.
3617def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003618 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003619 vim9script
3620 def crash#func()
3621 try
3622 for x in List()
3623 endfor
3624 catch
3625 endtry
3626 g:ok = true
3627 enddef
3628 fu List()
3629 invalid
3630 endfu
3631 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01003632 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003633 catch /wontmatch/
3634 endtry
3635 END
3636 call mkdir('Xruntime/autoload', 'p')
3637 call writefile(lines, 'Xruntime/autoload/crash.vim')
3638
3639 # run in a separate Vim to avoid the side effects of assert_fails()
3640 lines =<< trim END
3641 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
3642 call crash#func()
3643 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003644 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003645 END
3646 writefile(lines, 'Xscript')
3647 RunVim([], [], '-S Xscript')
3648 assert_equal(['ok'], readfile('Xdidit'))
3649
3650 delete('Xdidit')
3651 delete('Xscript')
3652 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01003653
3654 lines =<< trim END
3655 vim9script
3656 var foo#bar = 'asdf'
3657 END
3658 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003659enddef
3660
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003661def Test_script_var_in_autocmd()
3662 # using a script variable from an autocommand, defined in a :def function in a
3663 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003664 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003665 let s:counter = 1
3666 def s:Func()
3667 au! CursorHold
3668 au CursorHold * s:counter += 1
3669 enddef
3670 call s:Func()
3671 doau CursorHold
3672 call assert_equal(2, s:counter)
3673 au! CursorHold
3674 END
3675 CheckScriptSuccess(lines)
3676enddef
3677
Bram Moolenaar3896a102020-08-09 14:33:55 +02003678def Test_cmdline_win()
3679 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
3680 # the command line window.
3681 mkdir('rtp/syntax', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003682 var export_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02003683 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003684 export var That = 'yes'
Bram Moolenaar3896a102020-08-09 14:33:55 +02003685 END
3686 writefile(export_lines, 'rtp/syntax/Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003687 var import_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02003688 vim9script
3689 import That from './Xexport.vim'
3690 END
3691 writefile(import_lines, 'rtp/syntax/vim.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003692 var save_rtp = &rtp
Bram Moolenaar3896a102020-08-09 14:33:55 +02003693 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
3694 syntax on
3695 augroup CmdWin
3696 autocmd CmdwinEnter * g:got_there = 'yes'
3697 augroup END
3698 # this will open and also close the cmdline window
3699 feedkeys('q:', 'xt')
3700 assert_equal('yes', g:got_there)
3701
3702 augroup CmdWin
3703 au!
3704 augroup END
3705 &rtp = save_rtp
3706 delete('rtp', 'rf')
3707enddef
3708
Bram Moolenaare3d46852020-08-29 13:39:17 +02003709def Test_invalid_sid()
3710 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003711
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003712 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02003713 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02003714 endif
3715 delete('Xdidit')
3716enddef
3717
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003718def Test_restoring_cpo()
3719 writefile(['vim9script', 'set nocp'], 'Xsourced')
3720 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
3721 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
3722 assert_equal(['done'], readfile('Xdone'))
3723 endif
3724 delete('Xsourced')
3725 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01003726 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01003727
3728 writefile(['vim9script'], 'XanotherScript')
3729 set cpo=aABceFsMny>
3730 edit XanotherScript
3731 so %
3732 assert_equal('aABceFsMny>', &cpo)
3733 :1del
3734 w
3735 so %
3736 assert_equal('aABceFsMny>', &cpo)
3737
3738 delete('XanotherScript')
3739 set cpo&vim
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003740enddef
3741
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003742" Use :function so we can use Check commands
3743func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003744 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003745 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003746
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003747 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003748 vim9script
3749 def script#func()
3750 enddef
3751 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003752 call mkdir('Xdir/autoload', 'p')
3753 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003754
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003755 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003756 vim9script
3757 set cpo+=M
3758 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02003759 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003760 setline(1, 'some text')
3761 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003762 call writefile(lines, 'XTest_redraw_cpo')
3763 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
3764 call term_sendkeys(buf, "V:")
3765 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003766
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003767 " clean up
3768 call term_sendkeys(buf, "\<Esc>u")
3769 call StopVimInTerminal(buf)
3770 call delete('XTest_redraw_cpo')
3771 call delete('Xdir', 'rf')
3772endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003773
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003774
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003775def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003776 var lines =<< trim END
3777 var name: any
3778 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003779 END
3780 CheckDefAndScriptSuccess(lines)
3781enddef
3782
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003783func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02003784 CheckRunVimInTerminal
3785
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003786 " call indirectly to avoid compilation error for missing functions
3787 call Run_Test_define_func_at_command_line()
3788endfunc
3789
3790def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003791 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003792 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003793 func CheckAndQuit()
3794 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
3795 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
3796 endfunc
3797 END
3798 writefile([''], 'Xdidcmd')
3799 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01003800 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003801 # define Afunc() on the command line
3802 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
3803 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003804 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003805
3806 call StopVimInTerminal(buf)
3807 delete('XcallFunc')
3808 delete('Xdidcmd')
3809enddef
3810
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003811def Test_script_var_scope()
3812 var lines =<< trim END
3813 vim9script
3814 if true
3815 if true
3816 var one = 'one'
3817 echo one
3818 endif
3819 echo one
3820 endif
3821 END
3822 CheckScriptFailure(lines, 'E121:', 7)
3823
3824 lines =<< trim END
3825 vim9script
3826 if true
3827 if false
3828 var one = 'one'
3829 echo one
3830 else
3831 var one = 'one'
3832 echo one
3833 endif
3834 echo one
3835 endif
3836 END
3837 CheckScriptFailure(lines, 'E121:', 10)
3838
3839 lines =<< trim END
3840 vim9script
3841 while true
3842 var one = 'one'
3843 echo one
3844 break
3845 endwhile
3846 echo one
3847 END
3848 CheckScriptFailure(lines, 'E121:', 7)
3849
3850 lines =<< trim END
3851 vim9script
3852 for i in range(1)
3853 var one = 'one'
3854 echo one
3855 endfor
3856 echo one
3857 END
3858 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003859
3860 lines =<< trim END
3861 vim9script
3862 {
3863 var one = 'one'
3864 assert_equal('one', one)
3865 }
3866 assert_false(exists('one'))
3867 assert_false(exists('s:one'))
3868 END
3869 CheckScriptSuccess(lines)
3870
3871 lines =<< trim END
3872 vim9script
3873 {
3874 var one = 'one'
3875 echo one
3876 }
3877 echo one
3878 END
3879 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003880enddef
3881
Bram Moolenaar352134b2020-10-17 22:04:08 +02003882def Test_catch_exception_in_callback()
3883 var lines =<< trim END
3884 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02003885 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02003886 try
3887 var x: string
3888 var y: string
3889 # this error should be caught with CHECKLEN
3890 [x, y] = ['']
3891 catch
3892 g:caught = 'yes'
3893 endtry
3894 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01003895 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02003896 feedkeys("\r", 'xt')
3897 END
3898 CheckScriptSuccess(lines)
3899
3900 unlet g:caught
3901enddef
3902
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003903def Test_no_unknown_error_after_error()
3904 if !has('unix') || !has('job')
3905 throw 'Skipped: not unix of missing +job feature'
3906 endif
3907 var lines =<< trim END
3908 vim9script
3909 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02003910 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003911 eval [][0]
3912 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02003913 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003914 sleep 1m
3915 source += l
3916 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01003917 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01003918 while job_status(myjob) == 'run'
3919 sleep 10m
3920 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01003921 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02003922 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003923 END
3924 writefile(lines, 'Xdef')
3925 assert_fails('so Xdef', ['E684:', 'E1012:'])
3926 delete('Xdef')
3927enddef
3928
Bram Moolenaar4324d872020-12-01 20:12:24 +01003929def InvokeNormal()
3930 exe "norm! :m+1\r"
3931enddef
3932
3933def Test_invoke_normal_in_visual_mode()
3934 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
3935 new
3936 setline(1, ['aaa', 'bbb'])
3937 feedkeys("V\<F3>", 'xt')
3938 assert_equal(['bbb', 'aaa'], getline(1, 2))
3939 xunmap <F3>
3940enddef
3941
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003942def Test_white_space_after_command()
3943 var lines =<< trim END
3944 exit_cb: Func})
3945 END
3946 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003947
3948 lines =<< trim END
3949 e#
3950 END
3951 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003952enddef
3953
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003954def Test_script_var_gone_when_sourced_twice()
3955 var lines =<< trim END
3956 vim9script
3957 if exists('g:guard')
3958 finish
3959 endif
3960 g:guard = 1
3961 var name = 'thename'
3962 def g:GetName(): string
3963 return name
3964 enddef
3965 def g:SetName(arg: string)
3966 name = arg
3967 enddef
3968 END
3969 writefile(lines, 'XscriptTwice.vim')
3970 so XscriptTwice.vim
3971 assert_equal('thename', g:GetName())
3972 g:SetName('newname')
3973 assert_equal('newname', g:GetName())
3974 so XscriptTwice.vim
3975 assert_fails('call g:GetName()', 'E1149:')
3976 assert_fails('call g:SetName("x")', 'E1149:')
3977
3978 delfunc g:GetName
3979 delfunc g:SetName
3980 delete('XscriptTwice.vim')
3981 unlet g:guard
3982enddef
3983
3984def Test_import_gone_when_sourced_twice()
3985 var exportlines =<< trim END
3986 vim9script
3987 if exists('g:guard')
3988 finish
3989 endif
3990 g:guard = 1
3991 export var name = 'someName'
3992 END
3993 writefile(exportlines, 'XexportScript.vim')
3994
3995 var lines =<< trim END
3996 vim9script
3997 import name from './XexportScript.vim'
3998 def g:GetName(): string
3999 return name
4000 enddef
4001 END
4002 writefile(lines, 'XscriptImport.vim')
4003 so XscriptImport.vim
4004 assert_equal('someName', g:GetName())
4005
4006 so XexportScript.vim
4007 assert_fails('call g:GetName()', 'E1149:')
4008
4009 delfunc g:GetName
4010 delete('XexportScript.vim')
4011 delete('XscriptImport.vim')
4012 unlet g:guard
4013enddef
4014
Bram Moolenaar10b94212021-02-19 21:42:57 +01004015def Test_unsupported_commands()
4016 var lines =<< trim END
4017 ka
4018 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004019 CheckDefFailure(lines, 'E476:')
4020 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaar10b94212021-02-19 21:42:57 +01004021
4022 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01004023 :1ka
4024 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004025 CheckDefFailure(lines, 'E476:')
4026 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaarada1d872021-02-20 08:16:51 +01004027
4028 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01004029 t
4030 END
4031 CheckDefFailure(lines, 'E1100:')
4032 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4033
4034 lines =<< trim END
4035 x
4036 END
4037 CheckDefFailure(lines, 'E1100:')
4038 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4039
4040 lines =<< trim END
4041 xit
4042 END
4043 CheckDefFailure(lines, 'E1100:')
4044 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4045enddef
4046
Bram Moolenaarc70fe462021-04-17 17:59:19 +02004047def Test_mapping_line_number()
4048 var lines =<< trim END
4049 vim9script
4050 def g:FuncA()
4051 # Some comment
4052 FuncB(0)
4053 enddef
4054 # Some comment
4055 def FuncB(
4056 # Some comment
4057 n: number
4058 )
4059 exe 'nno '
4060 # Some comment
4061 .. '<F3> a'
4062 .. 'b'
4063 .. 'c'
4064 enddef
4065 END
4066 CheckScriptSuccess(lines)
4067 var res = execute('verbose nmap <F3>')
4068 assert_match('No mapping found', res)
4069
4070 g:FuncA()
4071 res = execute('verbose nmap <F3>')
4072 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
4073
4074 nunmap <F3>
4075 delfunc g:FuncA
4076enddef
4077
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004078def Test_option_modifier()
Bram Moolenaar1594f312021-07-08 16:40:13 +02004079 # legacy script allows for white space
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004080 var lines =<< trim END
4081 set hlsearch & hlsearch !
4082 call assert_equal(1, &hlsearch)
4083 END
4084 CheckScriptSuccess(lines)
4085
Bram Moolenaar1594f312021-07-08 16:40:13 +02004086 set hlsearch
4087 set hlsearch!
4088 assert_equal(false, &hlsearch)
4089
4090 set hlsearch
4091 set hlsearch&
4092 assert_equal(false, &hlsearch)
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004093
4094 lines =<< trim END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004095 set hlsearch &
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004096 END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004097 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: &')
4098
4099 lines =<< trim END
4100 set hlsearch !
4101 END
4102 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: !')
4103
4104 set hlsearch&
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004105enddef
4106
Bram Moolenaar585fea72020-04-02 22:33:21 +02004107" Keep this last, it messes up highlighting.
4108def Test_substitute_cmd()
4109 new
4110 setline(1, 'something')
4111 :substitute(some(other(
4112 assert_equal('otherthing', getline(1))
4113 bwipe!
4114
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004115 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004116 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02004117 vim9script
4118 new
4119 setline(1, 'something')
4120 :substitute(some(other(
4121 assert_equal('otherthing', getline(1))
4122 bwipe!
4123 END
4124 writefile(lines, 'Xvim9lines')
4125 source Xvim9lines
4126
4127 delete('Xvim9lines')
4128enddef
4129
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker