blob: 9baa1ff5aa7d1dd00a6e1e647725d73adb07a744 [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
Bram Moolenaarc3235272021-07-10 19:42:03 +0200694 au BufEnter * eval 1 + 2
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100695 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 Moolenaar4db572e2021-07-18 18:21:38 +02001088 var dir = './'
1089 var ext = ".vim"
1090 import {exp_name} from dir .. 'Xexport' .. ext
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001091 g:imported_name = exp_name
1092 exp_name ..= ' Doe'
1093 g:imported_name_appended = exp_name
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001094 g:imported_later = exported
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001095
1096 import theList from './Xexport.vim'
1097 theList->add(2)
1098 assert_equal([1, 2], theList)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099 END
1100
1101 writefile(import_script_lines, 'Ximport.vim')
1102 writefile(s:export_script_lines, 'Xexport.vim')
1103
1104 source Ximport.vim
1105
1106 assert_equal('bobbie', g:result)
1107 assert_equal('bob', g:localname)
1108 assert_equal(9876, g:imported)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001109 assert_equal(9879, g:imported_added)
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001110 assert_equal(9879, g:imported_later)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111 assert_equal('Exported', g:imported_func)
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001112 assert_equal('Exported', g:funcref_result)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001113 assert_equal('John', g:imported_name)
1114 assert_equal('John Doe', g:imported_name_appended)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001115 assert_false(exists('g:name'))
1116
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001117 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 unlet g:imported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001119 unlet g:imported_added
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001120 unlet g:imported_later
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 unlet g:imported_func
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001122 unlet g:imported_name g:imported_name_appended
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 delete('Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001124
Bram Moolenaar1c991142020-07-04 13:15:31 +02001125 # similar, with line breaks
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001126 var import_line_break_script_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001127 vim9script
1128 import {
1129 exported,
1130 Exported,
1131 }
1132 from
1133 './Xexport.vim'
1134 g:imported = exported
1135 exported += 5
1136 g:imported_added = exported
1137 g:imported_func = Exported()
1138 END
1139 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
1140 source Ximport_lbr.vim
1141
1142 assert_equal(9876, g:imported)
1143 assert_equal(9881, g:imported_added)
1144 assert_equal('Exported', g:imported_func)
1145
1146 # exported script not sourced again
1147 assert_false(exists('g:result'))
1148 unlet g:imported
1149 unlet g:imported_added
1150 unlet g:imported_func
1151 delete('Ximport_lbr.vim')
1152
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001153 var import_star_as_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001154 vim9script
1155 import * as Export from './Xexport.vim'
1156 def UseExport()
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001157 g:imported_def = Export.exported
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001158 enddef
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001159 g:imported_script = Export.exported
1160 assert_equal(1, exists('Export.exported'))
1161 assert_equal(0, exists('Export.notexported'))
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001162 UseExport()
1163 END
1164 writefile(import_star_as_lines, 'Ximport.vim')
1165 source Ximport.vim
Bram Moolenaar4db572e2021-07-18 18:21:38 +02001166 # FIXME: this should be 9881
1167 assert_equal(9876, g:imported_def)
1168 assert_equal(9876, g:imported_script)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001169
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001170 var import_star_as_lines_no_dot =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001171 vim9script
1172 import * as Export from './Xexport.vim'
1173 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001174 var dummy = 1
1175 var imported = Export + dummy
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001176 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001177 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001178 END
1179 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001180 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001181
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001182 var import_star_as_lines_dot_space =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001183 vim9script
1184 import * as Export from './Xexport.vim'
1185 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001186 var imported = Export . exported
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001187 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001188 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001189 END
1190 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001191 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001192
Bram Moolenaara6294952020-12-27 13:39:50 +01001193 var import_star_as_duplicated =<< trim END
1194 vim9script
1195 import * as Export from './Xexport.vim'
1196 var some = 'other'
1197 import * as Export from './Xexport.vim'
1198 defcompile
1199 END
1200 writefile(import_star_as_duplicated, 'Ximport.vim')
1201 assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim')
1202
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001203 var import_star_as_lines_script_no_dot =<< trim END
1204 vim9script
1205 import * as Export from './Xexport.vim'
1206 g:imported_script = Export exported
1207 END
1208 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
1209 assert_fails('source Ximport.vim', 'E1029:')
1210
1211 var import_star_as_lines_script_space_after_dot =<< trim END
1212 vim9script
1213 import * as Export from './Xexport.vim'
1214 g:imported_script = Export. exported
1215 END
1216 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
1217 assert_fails('source Ximport.vim', 'E1074:')
1218
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001219 var import_star_as_lines_missing_name =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001220 vim9script
1221 import * as Export from './Xexport.vim'
1222 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001223 var imported = Export.
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001224 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001225 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001226 END
1227 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001228 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001229
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001230 var import_star_as_lbr_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001231 vim9script
1232 import *
1233 as Export
1234 from
1235 './Xexport.vim'
1236 def UseExport()
1237 g:imported = Export.exported
1238 enddef
1239 UseExport()
1240 END
1241 writefile(import_star_as_lbr_lines, 'Ximport.vim')
1242 source Ximport.vim
Bram Moolenaar4db572e2021-07-18 18:21:38 +02001243 assert_equal(9876, g:imported)
Bram Moolenaar1c991142020-07-04 13:15:31 +02001244
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001245 var import_star_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001246 vim9script
1247 import * from './Xexport.vim'
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001248 END
1249 writefile(import_star_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001250 assert_fails('source Ximport.vim', 'E1045:', '', 2, 'Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001251
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001252 # try to import something that exists but is not exported
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001253 var import_not_exported_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001254 vim9script
1255 import name from './Xexport.vim'
1256 END
1257 writefile(import_not_exported_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001258 assert_fails('source Ximport.vim', 'E1049:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001259
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001260 # try to import something that is already defined
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001261 var import_already_defined =<< trim END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001262 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001263 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001264 import exported from './Xexport.vim'
1265 END
1266 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001267 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001268
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001269 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001270 import_already_defined =<< trim END
1271 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001272 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001273 import * as exported from './Xexport.vim'
1274 END
1275 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001276 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001277
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001278 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001279 import_already_defined =<< trim END
1280 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001281 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001282 import {exported} from './Xexport.vim'
1283 END
1284 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001285 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001286
Bram Moolenaar918a4242020-12-06 14:37:08 +01001287 # try changing an imported const
1288 var import_assign_to_const =<< trim END
1289 vim9script
1290 import CONST from './Xexport.vim'
1291 def Assign()
1292 CONST = 987
1293 enddef
1294 defcompile
1295 END
1296 writefile(import_assign_to_const, 'Ximport.vim')
1297 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1298
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001299 # try changing an imported final
1300 var import_assign_to_final =<< trim END
1301 vim9script
1302 import theList from './Xexport.vim'
1303 def Assign()
1304 theList = [2]
1305 enddef
1306 defcompile
1307 END
1308 writefile(import_assign_to_final, 'Ximport.vim')
1309 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1310
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001311 # import a very long name, requires making a copy
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001312 var import_long_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001313 vim9script
1314 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
1315 END
1316 writefile(import_long_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001317 assert_fails('source Ximport.vim', 'E1048:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001318
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001319 var import_no_from_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001320 vim9script
1321 import name './Xexport.vim'
1322 END
1323 writefile(import_no_from_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001324 assert_fails('source Ximport.vim', 'E1070:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001325
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001326 var import_invalid_string_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001327 vim9script
1328 import name from Xexport.vim
1329 END
1330 writefile(import_invalid_string_lines, 'Ximport.vim')
Bram Moolenaar4db572e2021-07-18 18:21:38 +02001331 assert_fails('source Ximport.vim', 'E121:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001332
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001333 var import_wrong_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001334 vim9script
1335 import name from './XnoExport.vim'
1336 END
1337 writefile(import_wrong_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001338 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001339
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001340 var import_missing_comma_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001341 vim9script
1342 import {exported name} from './Xexport.vim'
1343 END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001344 writefile(import_missing_comma_lines, 'Ximport3.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001345 assert_fails('source Ximport3.vim', 'E1046:', '', 2, 'Ximport3.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001346
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001347 delete('Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001348 delete('Ximport3.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 delete('Xexport.vim')
1350
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001351 # Check that in a Vim9 script 'cpo' is set to the Vim default.
Bram Moolenaar3e191692021-03-17 17:46:00 +01001352 # Flags added or removed are also applied to the restored value.
1353 set cpo=abcd
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001354 var lines =<< trim END
Bram Moolenaar750802b2020-02-23 18:08:33 +01001355 vim9script
1356 g:cpo_in_vim9script = &cpo
Bram Moolenaar3e191692021-03-17 17:46:00 +01001357 set cpo+=f
1358 set cpo-=c
1359 g:cpo_after_vim9script = &cpo
Bram Moolenaar750802b2020-02-23 18:08:33 +01001360 END
1361 writefile(lines, 'Xvim9_script')
1362 source Xvim9_script
Bram Moolenaar3e191692021-03-17 17:46:00 +01001363 assert_equal('fabd', &cpo)
Bram Moolenaar750802b2020-02-23 18:08:33 +01001364 set cpo&vim
1365 assert_equal(&cpo, g:cpo_in_vim9script)
Bram Moolenaar3e191692021-03-17 17:46:00 +01001366 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
1367 assert_equal(newcpo, g:cpo_after_vim9script)
1368
Bram Moolenaar750802b2020-02-23 18:08:33 +01001369 delete('Xvim9_script')
1370enddef
1371
Bram Moolenaar0a842842021-02-27 22:41:19 +01001372def Test_import_as()
1373 var export_lines =<< trim END
1374 vim9script
1375 export var one = 1
1376 export var yes = 'yes'
Bram Moolenaarc967d572021-07-08 21:38:50 +02001377 export var slist: list<string>
Bram Moolenaar0a842842021-02-27 22:41:19 +01001378 END
1379 writefile(export_lines, 'XexportAs')
1380
1381 var import_lines =<< trim END
1382 vim9script
Bram Moolenaar6c7cc342021-04-17 16:38:50 +02001383 var one = 'notused'
1384 var yes = 777
Bram Moolenaar0a842842021-02-27 22:41:19 +01001385 import one as thatOne from './XexportAs'
1386 assert_equal(1, thatOne)
1387 import yes as yesYes from './XexportAs'
1388 assert_equal('yes', yesYes)
1389 END
1390 CheckScriptSuccess(import_lines)
1391
1392 import_lines =<< trim END
1393 vim9script
1394 import {one as thatOne, yes as yesYes} from './XexportAs'
1395 assert_equal(1, thatOne)
1396 assert_equal('yes', yesYes)
1397 assert_fails('echo one', 'E121:')
1398 assert_fails('echo yes', 'E121:')
1399 END
1400 CheckScriptSuccess(import_lines)
1401
Bram Moolenaarc967d572021-07-08 21:38:50 +02001402 import_lines =<< trim END
1403 vim9script
1404 import {slist as impSlist} from './XexportAs'
1405 impSlist->add(123)
1406 END
1407 CheckScriptFailure(import_lines, 'E1012: Type mismatch; expected string but got number')
1408
Bram Moolenaar0a842842021-02-27 22:41:19 +01001409 delete('XexportAs')
1410enddef
1411
Bram Moolenaar803af682020-08-05 16:20:03 +02001412func g:Trigger()
1413 source Ximport.vim
1414 return "echo 'yes'\<CR>"
1415endfunc
1416
1417def Test_import_export_expr_map()
1418 # check that :import and :export work when buffer is locked
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001419 var export_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001420 vim9script
1421 export def That(): string
1422 return 'yes'
1423 enddef
1424 END
1425 writefile(export_lines, 'Xexport_that.vim')
1426
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001427 var import_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001428 vim9script
1429 import That from './Xexport_that.vim'
1430 assert_equal('yes', That())
1431 END
1432 writefile(import_lines, 'Ximport.vim')
1433
1434 nnoremap <expr> trigger g:Trigger()
1435 feedkeys('trigger', "xt")
1436
Bram Moolenaar730b2482020-08-09 13:02:10 +02001437 delete('Xexport_that.vim')
Bram Moolenaar803af682020-08-05 16:20:03 +02001438 delete('Ximport.vim')
1439 nunmap trigger
1440enddef
1441
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001442def Test_import_in_filetype()
1443 # check that :import works when the buffer is locked
1444 mkdir('ftplugin', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001445 var export_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001446 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001447 export var That = 'yes'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001448 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001449 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001450
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001451 var import_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001452 vim9script
Bram Moolenaar730b2482020-08-09 13:02:10 +02001453 import That from './Xexport_ft.vim'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001454 assert_equal('yes', That)
1455 g:did_load_mytpe = 1
1456 END
1457 writefile(import_lines, 'ftplugin/qf.vim')
1458
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001459 var save_rtp = &rtp
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001460 &rtp = getcwd() .. ',' .. &rtp
1461
1462 filetype plugin on
1463 copen
1464 assert_equal(1, g:did_load_mytpe)
1465
1466 quit!
Bram Moolenaar730b2482020-08-09 13:02:10 +02001467 delete('Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001468 delete('ftplugin', 'rf')
1469 &rtp = save_rtp
1470enddef
1471
Bram Moolenaarefa94442020-08-08 22:16:00 +02001472def Test_use_import_in_mapping()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001473 var lines =<< trim END
Bram Moolenaarefa94442020-08-08 22:16:00 +02001474 vim9script
1475 export def Funcx()
1476 g:result = 42
1477 enddef
1478 END
1479 writefile(lines, 'XsomeExport.vim')
1480 lines =<< trim END
1481 vim9script
1482 import Funcx from './XsomeExport.vim'
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001483 nnoremap <F3> :call <sid>Funcx()<cr>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001484 END
1485 writefile(lines, 'Xmapscript.vim')
1486
1487 source Xmapscript.vim
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001488 feedkeys("\<F3>", "xt")
Bram Moolenaarefa94442020-08-08 22:16:00 +02001489 assert_equal(42, g:result)
1490
1491 unlet g:result
1492 delete('XsomeExport.vim')
1493 delete('Xmapscript.vim')
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001494 nunmap <F3>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001495enddef
1496
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01001497def Test_vim9script_mix()
1498 var lines =<< trim END
1499 if has(g:feature)
1500 " legacy script
1501 let g:legacy = 1
1502 finish
1503 endif
1504 vim9script
1505 g:legacy = 0
1506 END
1507 g:feature = 'eval'
1508 g:legacy = -1
1509 CheckScriptSuccess(lines)
1510 assert_equal(1, g:legacy)
1511
1512 g:feature = 'noteval'
1513 g:legacy = -1
1514 CheckScriptSuccess(lines)
1515 assert_equal(0, g:legacy)
1516enddef
1517
Bram Moolenaar750802b2020-02-23 18:08:33 +01001518def Test_vim9script_fails()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1520 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001521 CheckScriptFailure(['export var some = 123'], 'E1042:')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001522 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001523 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
Bram Moolenaar750802b2020-02-23 18:08:33 +01001524 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
1525
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001526 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001527 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1528
Bram Moolenaare2e40752020-09-04 21:18:46 +02001529 assert_fails('vim9script', 'E1038:')
1530 assert_fails('export something', 'E1043:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001531enddef
1532
Bram Moolenaarf7d267e2020-06-17 12:04:54 +02001533func Test_import_fails_without_script()
Bram Moolenaar101f4812020-06-16 23:18:51 +02001534 CheckRunVimInTerminal
1535
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001536 " call indirectly to avoid compilation error for missing functions
Bram Moolenaarc620c052020-07-08 15:16:19 +02001537 call Run_Test_import_fails_on_command_line()
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001538endfunc
1539
Bram Moolenaarc620c052020-07-08 15:16:19 +02001540def Run_Test_import_fails_on_command_line()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001541 var export =<< trim END
Bram Moolenaar101f4812020-06-16 23:18:51 +02001542 vim9script
1543 export def Foo(): number
1544 return 0
1545 enddef
1546 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001547 writefile(export, 'XexportCmd.vim')
Bram Moolenaar101f4812020-06-16 23:18:51 +02001548
Bram Moolenaare0de1712020-12-02 17:36:54 +01001549 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001550 rows: 6, wait_for_ruler: 0})
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001551 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaar101f4812020-06-16 23:18:51 +02001552
Bram Moolenaar730b2482020-08-09 13:02:10 +02001553 delete('XexportCmd.vim')
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001554 StopVimInTerminal(buf)
1555enddef
Bram Moolenaar101f4812020-06-16 23:18:51 +02001556
Bram Moolenaar2b327002020-12-26 15:39:31 +01001557def Test_vim9script_reload_noclear()
1558 var lines =<< trim END
Bram Moolenaara6294952020-12-27 13:39:50 +01001559 vim9script
1560 export var exported = 'thexport'
1561 END
1562 writefile(lines, 'XExportReload')
1563 lines =<< trim END
Bram Moolenaar2b327002020-12-26 15:39:31 +01001564 vim9script noclear
1565 g:loadCount += 1
1566 var s:reloaded = 'init'
Bram Moolenaara6294952020-12-27 13:39:50 +01001567 import exported from './XExportReload'
Bram Moolenaar2b327002020-12-26 15:39:31 +01001568
1569 def Again(): string
1570 return 'again'
1571 enddef
1572
1573 if exists('s:loaded') | finish | endif
1574 var s:loaded = true
1575
1576 var s:notReloaded = 'yes'
1577 s:reloaded = 'first'
1578 def g:Values(): list<string>
Bram Moolenaara6294952020-12-27 13:39:50 +01001579 return [s:reloaded, s:notReloaded, Again(), Once(), exported]
Bram Moolenaar2b327002020-12-26 15:39:31 +01001580 enddef
1581
1582 def Once(): string
1583 return 'once'
1584 enddef
1585 END
1586 writefile(lines, 'XReloaded')
1587 g:loadCount = 0
1588 source XReloaded
1589 assert_equal(1, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001590 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001591 source XReloaded
1592 assert_equal(2, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001593 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001594 source XReloaded
1595 assert_equal(3, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001596 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001597
Bram Moolenaar48e11c12021-01-11 18:47:00 +01001598 delete('XReloaded')
Bram Moolenaara6294952020-12-27 13:39:50 +01001599 delete('XExportReload')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001600 delfunc g:Values
Bram Moolenaar2b327002020-12-26 15:39:31 +01001601 unlet g:loadCount
Bram Moolenaar577dc932021-06-27 15:35:40 +02001602
1603 lines =<< trim END
1604 vim9script
1605 def Inner()
1606 enddef
1607 END
1608 lines->writefile('XreloadScript.vim')
1609 source XreloadScript.vim
1610
1611 lines =<< trim END
1612 vim9script
1613 def Outer()
1614 def Inner()
1615 enddef
1616 enddef
1617 defcompile
1618 END
1619 lines->writefile('XreloadScript.vim')
1620 source XreloadScript.vim
1621
1622 delete('XreloadScript.vim')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001623enddef
1624
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001625def Test_vim9script_reload_import()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001626 var lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627 vim9script
1628 const var = ''
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001629 var valone = 1234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630 def MyFunc(arg: string)
1631 valone = 5678
1632 enddef
1633 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001634 var morelines =<< trim END
1635 var valtwo = 222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636 export def GetValtwo(): number
1637 return valtwo
1638 enddef
1639 END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001640 writefile(lines + morelines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 source Xreload.vim
1642 source Xreload.vim
1643 source Xreload.vim
1644
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001645 # cannot declare a var twice
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 lines =<< trim END
1647 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001648 var valone = 1234
1649 var valone = 5678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650 END
1651 writefile(lines, 'Xreload.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001652 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653
1654 delete('Xreload.vim')
1655 delete('Ximport.vim')
1656enddef
1657
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001658" if a script is reloaded with a script-local variable that changed its type, a
1659" compiled function using that variable must fail.
1660def Test_script_reload_change_type()
1661 var lines =<< trim END
1662 vim9script noclear
1663 var str = 'string'
1664 def g:GetStr(): string
1665 return str .. 'xxx'
1666 enddef
1667 END
1668 writefile(lines, 'Xreload.vim')
1669 source Xreload.vim
1670 echo g:GetStr()
1671
1672 lines =<< trim END
1673 vim9script noclear
1674 var str = 1234
1675 END
1676 writefile(lines, 'Xreload.vim')
1677 source Xreload.vim
1678 assert_fails('echo g:GetStr()', 'E1150:')
1679
1680 delfunc g:GetStr
1681 delete('Xreload.vim')
1682enddef
1683
Bram Moolenaarc970e422021-03-17 15:03:04 +01001684" Define CallFunc so that the test can be compiled
1685command CallFunc echo 'nop'
1686
1687def Test_script_reload_from_function()
1688 var lines =<< trim END
1689 vim9script
1690
1691 if exists('g:loaded')
1692 finish
1693 endif
1694 g:loaded = 1
1695 delcommand CallFunc
1696 command CallFunc Func()
1697 def Func()
Bram Moolenaara4c81be2021-03-17 15:23:16 +01001698 so XreloadFunc.vim
Bram Moolenaarc970e422021-03-17 15:03:04 +01001699 g:didTheFunc = 1
1700 enddef
1701 END
1702 writefile(lines, 'XreloadFunc.vim')
1703 source XreloadFunc.vim
1704 CallFunc
1705 assert_equal(1, g:didTheFunc)
1706
1707 delete('XreloadFunc.vim')
1708 delcommand CallFunc
1709 unlet g:loaded
1710 unlet g:didTheFunc
1711enddef
1712
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001713def Test_script_var_shadows_function()
1714 var lines =<< trim END
1715 vim9script
1716 def Func(): number
1717 return 123
1718 enddef
1719 var Func = 1
1720 END
1721 CheckScriptFailure(lines, 'E1041:', 5)
1722enddef
1723
Bram Moolenaarc3235272021-07-10 19:42:03 +02001724def Test_script_var_shadows_command()
1725 var lines =<< trim END
1726 var undo = 1
1727 undo = 2
1728 assert_equal(2, undo)
1729 END
1730 CheckDefAndScriptSuccess(lines)
1731
1732 lines =<< trim END
1733 var undo = 1
1734 undo
1735 END
1736 CheckDefAndScriptFailure(lines, 'E1207:', 2)
1737enddef
1738
Bram Moolenaar95006e32020-08-29 17:47:08 +02001739def s:RetSome(): string
1740 return 'some'
1741enddef
1742
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001743" Not exported function that is referenced needs to be accessed by the
1744" script-local name.
1745def Test_vim9script_funcref()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001746 var sortlines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001747 vim9script
1748 def Compare(i1: number, i2: number): number
Bram Moolenaarbed36b92020-07-07 23:31:36 +02001749 return i2 - i1
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001750 enddef
1751
1752 export def FastSort(): list<number>
1753 return range(5)->sort(Compare)
1754 enddef
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001755
1756 export def GetString(arg: string): string
1757 return arg
1758 enddef
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001759 END
1760 writefile(sortlines, 'Xsort.vim')
1761
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001762 var lines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001763 vim9script
1764 import FastSort from './Xsort.vim'
1765 def Test()
1766 g:result = FastSort()
1767 enddef
1768 Test()
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001769
1770 # using a function imported with "as"
1771 import * as anAlias from './Xsort.vim'
1772 assert_equal('yes', anAlias.GetString('yes'))
1773
1774 # using the function from a compiled function
1775 def TestMore(): string
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001776 var s = s:anAlias.GetString('foo')
1777 return s .. anAlias.GetString('bar')
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001778 enddef
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001779 assert_equal('foobar', TestMore())
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001780
1781 # error when using a function that isn't exported
1782 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001783 END
1784 writefile(lines, 'Xscript.vim')
1785
1786 source Xscript.vim
1787 assert_equal([4, 3, 2, 1, 0], g:result)
1788
1789 unlet g:result
1790 delete('Xsort.vim')
1791 delete('Xscript.vim')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001792
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001793 var Funcref = function('s:RetSome')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001794 assert_equal('some', Funcref())
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001795enddef
1796
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001797" Check that when searching for "FilterFunc" it finds the import in the
1798" script where FastFilter() is called from, both as a string and as a direct
1799" function reference.
Bram Moolenaarc620c052020-07-08 15:16:19 +02001800def Test_vim9script_funcref_other_script()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001801 var filterLines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001802 vim9script
1803 export def FilterFunc(idx: number, val: number): bool
1804 return idx % 2 == 1
1805 enddef
1806 export def FastFilter(): list<number>
1807 return range(10)->filter('FilterFunc')
1808 enddef
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001809 export def FastFilterDirect(): list<number>
1810 return range(10)->filter(FilterFunc)
1811 enddef
Bram Moolenaarc620c052020-07-08 15:16:19 +02001812 END
1813 writefile(filterLines, 'Xfilter.vim')
1814
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001815 var lines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001816 vim9script
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001817 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim'
Bram Moolenaarc620c052020-07-08 15:16:19 +02001818 def Test()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001819 var x: list<number> = FastFilter()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001820 enddef
1821 Test()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001822 def TestDirect()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001823 var x: list<number> = FastFilterDirect()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001824 enddef
1825 TestDirect()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001826 END
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001827 CheckScriptSuccess(lines)
Bram Moolenaarc620c052020-07-08 15:16:19 +02001828 delete('Xfilter.vim')
Bram Moolenaarc620c052020-07-08 15:16:19 +02001829enddef
1830
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001831def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001832 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001833 vim9script
1834 def FuncYes(): string
1835 return 'yes'
1836 enddef
1837 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001838 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001839 def FuncNo(): string
1840 return 'no'
1841 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001842 def g:DoCheck(no_exists: bool)
1843 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001844 assert_equal('no', FuncNo())
1845 enddef
1846 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001847 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001848 def g:DoCheck(no_exists: bool)
1849 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02001850 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001851 enddef
1852 END
1853
1854 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001855 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001856 source Xreloaded.vim
1857 g:DoCheck(true)
1858
1859 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001860 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001861 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001862 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001863
1864 # FuncNo() is back
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
Bram Moolenaar50824712020-12-20 21:10:17 +01001867 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001868
1869 delete('Xreloaded.vim')
1870enddef
1871
Bram Moolenaar89483d42020-05-10 15:24:44 +02001872def Test_vim9script_reload_delvar()
1873 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001874 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02001875 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001876 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001877 END
1878 writefile(lines, 'XreloadVar.vim')
1879 source XreloadVar.vim
1880
1881 # now write the script using the same variable locally - works
1882 lines =<< trim END
1883 vim9script
1884 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001885 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001886 enddef
1887 END
1888 writefile(lines, 'XreloadVar.vim')
1889 source XreloadVar.vim
1890
1891 delete('XreloadVar.vim')
1892enddef
1893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894def Test_import_absolute()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001895 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02001896 'vim9script',
1897 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
1898 'def UseExported()',
1899 ' g:imported_abs = exported',
1900 ' exported = 8888',
1901 ' g:imported_after = exported',
1902 'enddef',
1903 'UseExported()',
1904 'g:import_disassembled = execute("disass UseExported")',
1905 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 writefile(import_lines, 'Ximport_abs.vim')
1907 writefile(s:export_script_lines, 'Xexport_abs.vim')
1908
1909 source Ximport_abs.vim
1910
1911 assert_equal(9876, g:imported_abs)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001912 assert_equal(8888, g:imported_after)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001913 assert_match('<SNR>\d\+_UseExported\_s*' ..
1914 'g:imported_abs = exported\_s*' ..
1915 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1916 '1 STOREG g:imported_abs\_s*' ..
1917 'exported = 8888\_s*' ..
1918 '2 PUSHNR 8888\_s*' ..
1919 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1920 'g:imported_after = exported\_s*' ..
1921 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1922 '5 STOREG g:imported_after',
Bram Moolenaare6085c52020-04-12 20:19:16 +02001923 g:import_disassembled)
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001924
1925 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 unlet g:imported_abs
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001927 unlet g:import_disassembled
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928
1929 delete('Ximport_abs.vim')
1930 delete('Xexport_abs.vim')
1931enddef
1932
1933def Test_import_rtp()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001934 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02001935 'vim9script',
1936 'import exported from "Xexport_rtp.vim"',
1937 'g:imported_rtp = exported',
1938 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 writefile(import_lines, 'Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02001940 mkdir('import', 'p')
1941 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001943 var save_rtp = &rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 &rtp = getcwd()
1945 source Ximport_rtp.vim
1946 &rtp = save_rtp
1947
1948 assert_equal(9876, g:imported_rtp)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001950 Undo_export_script_lines()
1951 unlet g:imported_rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 delete('Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02001953 delete('import', 'rf')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001954enddef
1955
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001956def Test_import_compile_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001957 var export_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001958 'vim9script',
1959 'export def ExpFunc(): string',
1960 ' return notDefined',
1961 'enddef',
1962 ]
1963 writefile(export_lines, 'Xexported.vim')
1964
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001965 var import_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001966 'vim9script',
1967 'import ExpFunc from "./Xexported.vim"',
1968 'def ImpFunc()',
1969 ' echo ExpFunc()',
1970 'enddef',
1971 'defcompile',
1972 ]
1973 writefile(import_lines, 'Ximport.vim')
1974
1975 try
1976 source Ximport.vim
1977 catch /E1001/
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001978 # Error should be fore the Xexported.vim file.
Bram Moolenaar77072282020-09-16 17:55:40 +02001979 assert_match('E1001: Variable not found: notDefined', v:exception)
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001980 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1981 endtry
1982
1983 delete('Xexported.vim')
1984 delete('Ximport.vim')
1985enddef
1986
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001987def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001988 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001989 'vim9script',
1990 'def Func()',
1991 ' eval [][0]',
1992 'enddef',
1993 'Func()',
1994 ]
1995 writefile(lines, 'Xtestscript.vim')
1996
1997 for count in range(3)
1998 try
1999 source Xtestscript.vim
2000 catch /E684/
2001 # function name should contain <SNR> every time
2002 assert_match('E684: list index out of range', v:exception)
2003 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
2004 endtry
2005 endfor
2006
2007 delete('Xtestscript.vim')
2008enddef
2009
Bram Moolenaareef21022020-08-01 22:16:43 +02002010def Test_func_overrules_import_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002011 var export_lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002012 vim9script
2013 export def Func()
2014 echo 'imported'
2015 enddef
2016 END
2017 writefile(export_lines, 'XexportedFunc.vim')
2018
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002019 var lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002020 vim9script
2021 import Func from './XexportedFunc.vim'
2022 def Func()
2023 echo 'local to function'
2024 enddef
2025 END
2026 CheckScriptFailure(lines, 'E1073:')
2027
2028 lines =<< trim END
2029 vim9script
2030 import Func from './XexportedFunc.vim'
2031 def Outer()
2032 def Func()
2033 echo 'local to function'
2034 enddef
2035 enddef
2036 defcompile
2037 END
2038 CheckScriptFailure(lines, 'E1073:')
2039
2040 delete('XexportedFunc.vim')
2041enddef
2042
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002043def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002044 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002045 vim9script
2046 def Func()
2047 echo 'one'
2048 enddef
2049 def Func()
2050 echo 'two'
2051 enddef
2052 END
2053 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002054
2055 lines =<< trim END
2056 vim9script
2057 def Foo(): string
2058 return 'foo'
2059 enddef
2060 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002061 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002062 enddef
2063 defcompile
2064 END
2065 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002066enddef
2067
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002069 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002070 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 l->remove(0)
2072 l->add(5)
2073 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002074 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075enddef
2076
Bram Moolenaarae616492020-07-28 20:07:27 +02002077def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002078 CheckDefExecFailure(['a = 1'], 'E1100:')
2079 CheckDefExecFailure(['c = 1'], 'E1100:')
2080 CheckDefExecFailure(['i = 1'], 'E1100:')
2081 CheckDefExecFailure(['t = 1'], 'E1100:')
2082 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002083
Bram Moolenaarae616492020-07-28 20:07:27 +02002084 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
2085 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002086 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
2087 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002088 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
2089 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01002090 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
2091 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002092 CheckScriptFailure(['vim9script', 't'], 'E1100:')
2093 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
2094 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002095enddef
2096
Bram Moolenaar158906c2020-02-06 20:39:45 +01002097def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002098 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01002099 if what == 1
2100 res = "one"
2101 elseif what == 2
2102 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002103 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01002104 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002105 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01002106 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002107enddef
2108
Bram Moolenaar158906c2020-02-06 20:39:45 +01002109def Test_if_elseif_else()
2110 assert_equal('one', IfElse(1))
2111 assert_equal('two', IfElse(2))
2112 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002113enddef
2114
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002115def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002116 CheckDefFailure(['elseif true'], 'E582:')
2117 CheckDefFailure(['else'], 'E581:')
2118 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01002119 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002120 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002121
2122 var lines =<< trim END
2123 var s = ''
2124 if s = ''
2125 endif
2126 END
2127 CheckDefFailure(lines, 'E488:')
2128
2129 lines =<< trim END
2130 var s = ''
2131 if s == ''
2132 elseif s = ''
2133 endif
2134 END
2135 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002136enddef
2137
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002138let g:bool_true = v:true
2139let g:bool_false = v:false
2140
2141def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002142 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002143 if true ? true : false
2144 res = true
2145 endif
2146 assert_equal(true, res)
2147
Bram Moolenaar585fea72020-04-02 22:33:21 +02002148 g:glob = 2
2149 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02002150 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002151 endif
2152 assert_equal(2, g:glob)
2153 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02002154 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002155 endif
2156 assert_equal(3, g:glob)
2157
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002158 res = false
2159 if g:bool_true ? true : false
2160 res = true
2161 endif
2162 assert_equal(true, res)
2163
2164 res = false
2165 if true ? g:bool_true : false
2166 res = true
2167 endif
2168 assert_equal(true, res)
2169
2170 res = false
2171 if true ? true : g:bool_false
2172 res = true
2173 endif
2174 assert_equal(true, res)
2175
2176 res = false
2177 if true ? false : true
2178 res = true
2179 endif
2180 assert_equal(false, res)
2181
2182 res = false
2183 if false ? false : true
2184 res = true
2185 endif
2186 assert_equal(true, res)
2187
2188 res = false
2189 if false ? true : false
2190 res = true
2191 endif
2192 assert_equal(false, res)
2193
2194 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002195 if has('xyz') ? true : false
2196 res = true
2197 endif
2198 assert_equal(false, res)
2199
2200 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002201 if true && true
2202 res = true
2203 endif
2204 assert_equal(true, res)
2205
2206 res = false
2207 if true && false
2208 res = true
2209 endif
2210 assert_equal(false, res)
2211
2212 res = false
2213 if g:bool_true && false
2214 res = true
2215 endif
2216 assert_equal(false, res)
2217
2218 res = false
2219 if true && g:bool_false
2220 res = true
2221 endif
2222 assert_equal(false, res)
2223
2224 res = false
2225 if false && false
2226 res = true
2227 endif
2228 assert_equal(false, res)
2229
2230 res = false
2231 if true || false
2232 res = true
2233 endif
2234 assert_equal(true, res)
2235
2236 res = false
2237 if g:bool_true || false
2238 res = true
2239 endif
2240 assert_equal(true, res)
2241
2242 res = false
2243 if true || g:bool_false
2244 res = true
2245 endif
2246 assert_equal(true, res)
2247
2248 res = false
2249 if false || false
2250 res = true
2251 endif
2252 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02002253
2254 # with constant "false" expression may be invalid so long as the syntax is OK
Bram Moolenaarc3235272021-07-10 19:42:03 +02002255 if false | eval 1 + 2 | endif
Bram Moolenaar3988f642020-08-27 22:43:03 +02002256 if false | eval burp + 234 | endif
2257 if false | echo burp 234 'asd' | endif
2258 if false
2259 burp
2260 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002261enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002262
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002263def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002264 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
2265 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
2266 CheckDefFailure(["if has('aaa'"], 'E110:')
2267 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002268enddef
2269
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002270def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002271 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002272 if i % 2
2273 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002274 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002275 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002276 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002277 endif
2278 x += 1
2279 else
2280 x += 1000
2281 endif
2282 return x
2283enddef
2284
2285def Test_nested_if()
2286 assert_equal(1, RunNested(1))
2287 assert_equal(1000, RunNested(2))
2288enddef
2289
Bram Moolenaarad39c092020-02-26 18:23:43 +01002290def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01002291 # missing argument is ignored
2292 execute
2293 execute # comment
2294
Bram Moolenaarad39c092020-02-26 18:23:43 +01002295 new
2296 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002297 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002298 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002299
Bram Moolenaard2c61702020-09-06 15:58:36 +02002300 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002301 assert_equal('execute-string', getline(1))
2302
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002303 var cmd1 = 'setline(1,'
2304 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002305 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01002306 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002307
Bram Moolenaard2c61702020-09-06 15:58:36 +02002308 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002309 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002310
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002311 var cmd_first = 'call '
2312 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002313 execute cmd_first .. cmd_last
2314 assert_equal('execute-var-var', getline(1))
2315 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02002316
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002317 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002318 execute 'echomsg' (n ? '"true"' : '"no"')
2319 assert_match('^true$', Screenline(&lines))
2320
Bram Moolenaare0de1712020-12-02 17:36:54 +01002321 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002322 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
2323
Bram Moolenaard2c61702020-09-06 15:58:36 +02002324 CheckDefFailure(['execute xxx'], 'E1001:', 1)
2325 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
2326 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002327enddef
2328
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002329def Test_execute_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002330 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002331 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002332 vim9script
2333 execute 'g:someVar'
2334 .. ' = ' ..
2335 '28'
2336 assert_equal(28, g:someVar)
2337 unlet g:someVar
2338 END
2339 CheckScriptSuccess(lines)
2340enddef
2341
Bram Moolenaarad39c092020-02-26 18:23:43 +01002342def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002343 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02002344 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002345 assert_match('^something$', Screenline(&lines))
2346
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002347 echo "some" # comment
2348 echon "thing"
2349 assert_match('^something$', Screenline(&lines))
2350
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002351 var str1 = 'some'
2352 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002353 echo str1 str2
2354 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002355
Bram Moolenaard2c61702020-09-06 15:58:36 +02002356 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01002357enddef
2358
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002359def Test_echomsg_cmd()
2360 echomsg 'some' 'more' # comment
2361 assert_match('^some more$', Screenline(&lines))
2362 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002363 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002364 assert_match('^some more$', Screenline(&lines))
2365
Bram Moolenaard2c61702020-09-06 15:58:36 +02002366 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002367enddef
2368
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002369def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002370 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002371 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002372 vim9script
2373 echomsg 'here'
2374 .. ' is ' ..
2375 'a message'
2376 assert_match('^here is a message$', Screenline(&lines))
2377 END
2378 CheckScriptSuccess(lines)
2379enddef
2380
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002381def Test_echoerr_cmd()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002382 try
2383 echoerr 'something' 'wrong' # comment
2384 catch
2385 assert_match('something wrong', v:exception)
2386 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002387enddef
2388
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002389def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002390 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002391 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002392 vim9script
2393 try
2394 echoerr 'this'
2395 .. ' is ' ..
2396 'wrong'
2397 catch
2398 assert_match('this is wrong', v:exception)
2399 endtry
2400 END
2401 CheckScriptSuccess(lines)
2402enddef
2403
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002404def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002405 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002406 vim9script
2407 new
2408 for var in range(0, 3)
2409 append(line('$'), var)
2410 endfor
2411 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2412 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01002413
2414 var result = ''
2415 for i in [1, 2, 3]
2416 var loop = ' loop ' .. i
2417 result ..= loop
2418 endfor
2419 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002420 END
2421 writefile(lines, 'Xvim9for.vim')
2422 source Xvim9for.vim
2423 delete('Xvim9for.vim')
2424enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002426def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002427 var lines =<< trim END
2428 var result = ''
2429 for cnt in range(7)
2430 if cnt == 4
2431 break
2432 endif
2433 if cnt == 2
2434 continue
2435 endif
2436 result ..= cnt .. '_'
2437 endfor
2438 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02002439
Bram Moolenaarf2253962021-04-13 20:53:13 +02002440 var concat = ''
2441 for str in eval('["one", "two"]')
2442 concat ..= str
2443 endfor
2444 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002445
Bram Moolenaarf2253962021-04-13 20:53:13 +02002446 var total = 0
2447 for nr in
2448 [1, 2, 3]
2449 total += nr
2450 endfor
2451 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002452
Bram Moolenaarf2253962021-04-13 20:53:13 +02002453 total = 0
2454 for nr
2455 in [1, 2, 3]
2456 total += nr
2457 endfor
2458 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002459
Bram Moolenaarf2253962021-04-13 20:53:13 +02002460 total = 0
2461 for nr
2462 in
2463 [1, 2, 3]
2464 total += nr
2465 endfor
2466 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01002467
Bram Moolenaara3589a02021-04-14 13:30:46 +02002468 # with type
2469 total = 0
2470 for n: number in [1, 2, 3]
2471 total += n
2472 endfor
2473 assert_equal(6, total)
2474
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002475 var chars = ''
2476 for s: string in 'foobar'
2477 chars ..= s
2478 endfor
2479 assert_equal('foobar', chars)
2480
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02002481 chars = ''
2482 for x: string in {a: 'a', b: 'b'}->values()
2483 chars ..= x
2484 endfor
2485 assert_equal('ab', chars)
2486
Bram Moolenaara3589a02021-04-14 13:30:46 +02002487 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02002488 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02002489 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2490 res ..= n .. s
2491 endfor
2492 assert_equal('1a2b', res)
2493
Bram Moolenaar444d8782021-06-26 12:40:56 +02002494 # unpack with one var
2495 var reslist = []
2496 for [x] in [['aaa'], ['bbb']]
2497 reslist->add(x)
2498 endfor
2499 assert_equal(['aaa', 'bbb'], reslist)
2500
Bram Moolenaara3589a02021-04-14 13:30:46 +02002501 # loop over string
2502 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02002503 for c in 'aéc̀d'
2504 res ..= c .. '-'
2505 endfor
2506 assert_equal('a-é-c̀-d-', res)
2507
2508 res = ''
2509 for c in ''
2510 res ..= c .. '-'
2511 endfor
2512 assert_equal('', res)
2513
2514 res = ''
2515 for c in test_null_string()
2516 res ..= c .. '-'
2517 endfor
2518 assert_equal('', res)
2519
2520 var foo: list<dict<any>> = [
2521 {a: 'Cat'}
2522 ]
2523 for dd in foo
2524 dd.counter = 12
2525 endfor
2526 assert_equal([{a: 'Cat', counter: 12}], foo)
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002527
2528 reslist = []
2529 for _ in range(3)
2530 reslist->add('x')
2531 endfor
2532 assert_equal(['x', 'x', 'x'], reslist)
Bram Moolenaarf2253962021-04-13 20:53:13 +02002533 END
2534 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002535enddef
2536
2537def Test_for_loop_fails()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002538 CheckDefAndScriptFailure2(['for '], 'E1097:', 'E690:')
2539 CheckDefAndScriptFailure2(['for x'], 'E1097:', 'E690:')
2540 CheckDefAndScriptFailure2(['for x in'], 'E1097:', 'E15:')
2541 CheckDefAndScriptFailure(['for # in range(5)'], 'E690:')
2542 CheckDefAndScriptFailure(['for i In range(5)'], 'E690:')
2543 CheckDefAndScriptFailure2(['var x = 5', 'for x in range(5)', 'endfor'], 'E1017:', 'E1041:')
Bram Moolenaard4ab8072021-07-08 19:22:12 +02002544 CheckScriptFailure(['vim9script', 'var x = 5', 'for x in range(5)', '# comment', 'endfor'], 'E1041:', 3)
Bram Moolenaar822ba242020-05-24 23:00:18 +02002545 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002546 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02002547 CheckDefFailure(['for i in xxx'], 'E1001:')
2548 CheckDefFailure(['endfor'], 'E588:')
2549 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002550
2551 # wrong type detected at compile time
2552 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2553
2554 # wrong type detected at runtime
2555 g:adict = {a: 1}
2556 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2557 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002558
2559 var lines =<< trim END
2560 var d: list<dict<any>> = [{a: 0}]
2561 for e in d
2562 e = {a: 0, b: ''}
2563 endfor
2564 END
2565 CheckDefAndScriptFailure2(lines, 'E1018:', 'E46:', 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002566
2567 lines =<< trim END
2568 for nr: number in ['foo']
2569 endfor
2570 END
2571 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaar404557e2021-07-05 21:41:48 +02002572
2573 lines =<< trim END
2574 for n : number in [1, 2]
2575 echo n
2576 endfor
2577 END
2578 CheckDefAndScriptFailure(lines, 'E1059:', 1)
Bram Moolenaarefc5db52021-07-07 21:21:30 +02002579
2580 lines =<< trim END
2581 var d: dict<number> = {a: 1, b: 2}
2582 for [k: job, v: job] in d->items()
2583 echo k v
2584 endfor
2585 END
2586 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected job but got string', 2)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002587enddef
2588
Bram Moolenaarea870692020-12-02 14:24:30 +01002589def Test_for_loop_script_var()
2590 # cannot use s:var in a :def function
2591 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1101:')
2592
2593 # can use s:var in Vim9 script, with or without s:
2594 var lines =<< trim END
2595 vim9script
2596 var total = 0
2597 for s:var in [1, 2, 3]
2598 total += s:var
2599 endfor
2600 assert_equal(6, total)
2601
2602 total = 0
2603 for var in [1, 2, 3]
2604 total += var
2605 endfor
2606 assert_equal(6, total)
2607 END
2608enddef
2609
Bram Moolenaar792f7862020-11-23 08:31:18 +01002610def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01002611 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002612 var result = []
2613 for [v1, v2] in [[1, 2], [3, 4]]
2614 result->add(v1)
2615 result->add(v2)
2616 endfor
2617 assert_equal([1, 2, 3, 4], result)
2618
2619 result = []
2620 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2621 result->add(v1)
2622 result->add(v2)
2623 result->add(v3)
2624 endfor
2625 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
2626
2627 result = []
2628 for [&ts, &sw] in [[1, 2], [3, 4]]
2629 result->add(&ts)
2630 result->add(&sw)
2631 endfor
2632 assert_equal([1, 2, 3, 4], result)
2633
2634 var slist: list<string>
2635 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
2636 slist->add($LOOPVAR)
2637 slist->add(@r)
2638 slist->add(v:errmsg)
2639 endfor
2640 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
2641
2642 slist = []
2643 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
2644 slist->add(g:globalvar)
2645 slist->add(b:bufvar)
2646 slist->add(w:winvar)
2647 slist->add(t:tabvar)
2648 endfor
2649 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01002650 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaarb777da92021-05-22 21:40:39 +02002651
2652 var res = []
2653 for [_, n, _] in [[1, 2, 3], [4, 5, 6]]
2654 res->add(n)
2655 endfor
2656 assert_equal([2, 5], res)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002657 END
2658 CheckDefAndScriptSuccess(lines)
2659
2660 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01002661 for [v1, v2] in [[1, 2, 3], [3, 4]]
2662 echo v1 v2
2663 endfor
2664 END
2665 CheckDefExecFailure(lines, 'E710:', 1)
2666
2667 lines =<< trim END
2668 for [v1, v2] in [[1], [3, 4]]
2669 echo v1 v2
2670 endfor
2671 END
2672 CheckDefExecFailure(lines, 'E711:', 1)
2673
2674 lines =<< trim END
2675 for [v1, v1] in [[1, 2], [3, 4]]
2676 echo v1
2677 endfor
2678 END
2679 CheckDefExecFailure(lines, 'E1017:', 1)
2680enddef
2681
Bram Moolenaarc150c092021-02-13 15:02:46 +01002682def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002683 var lines =<< trim END
2684 var looped = 0
2685 var cleanup = 0
2686 for i in range(3)
2687 looped += 1
2688 try
2689 eval [][0]
2690 catch
2691 continue
2692 finally
2693 cleanup += 1
2694 endtry
2695 endfor
2696 assert_equal(3, looped)
2697 assert_equal(3, cleanup)
2698 END
2699 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002700enddef
2701
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002702def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002703 var result = ''
2704 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002705 while cnt < 555
2706 if cnt == 3
2707 break
2708 endif
2709 cnt += 1
2710 if cnt == 2
2711 continue
2712 endif
2713 result ..= cnt .. '_'
2714 endwhile
2715 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002716
2717 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002718 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002719 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002720enddef
2721
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002722def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02002723 CheckDefFailure(['while xxx'], 'E1001:')
2724 CheckDefFailure(['endwhile'], 'E588:')
2725 CheckDefFailure(['continue'], 'E586:')
2726 CheckDefFailure(['if true', 'continue'], 'E586:')
2727 CheckDefFailure(['break'], 'E587:')
2728 CheckDefFailure(['if true', 'break'], 'E587:')
2729 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002730
2731 var lines =<< trim END
2732 var s = ''
2733 while s = ''
2734 endwhile
2735 END
2736 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002737enddef
2738
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002739def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002740 var caught = false
2741 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002742 try
2743 while 1
2744 x += 1
2745 if x == 100
2746 feedkeys("\<C-C>", 'Lt')
2747 endif
2748 endwhile
2749 catch
2750 caught = true
2751 assert_equal(100, x)
2752 endtry
2753 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02002754 # consume the CTRL-C
2755 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002756enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01002757
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002758def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002759 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002760 'one',
2761 'two',
2762 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002763 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002764 assert_equal(['one', 'two', 'three'], mylist)
2765
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002766 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01002767 ['one']: 1,
2768 ['two']: 2,
2769 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002770 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002771 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01002772 assert_equal({one: 1, two: 2, three: 3}, mydict)
2773 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002774 one: 1, # comment
2775 two: # comment
2776 2, # comment
2777 three: 3 # comment
2778 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002779 assert_equal({one: 1, two: 2, three: 3}, mydict)
2780 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002781 one: 1,
2782 two:
2783 2,
2784 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002785 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002786 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002787
2788 assert_equal(
2789 ['one', 'two', 'three'],
2790 split('one two three')
2791 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002792enddef
2793
Bram Moolenaar7a092242020-04-16 22:10:49 +02002794def Test_vim9_comment()
2795 CheckScriptSuccess([
2796 'vim9script',
2797 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002798 '#something',
2799 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002800 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002801
2802 split Xfile
2803 CheckScriptSuccess([
2804 'vim9script',
2805 'edit #something',
2806 ])
2807 CheckScriptSuccess([
2808 'vim9script',
2809 'edit #{something',
2810 ])
2811 close
2812
Bram Moolenaar7a092242020-04-16 22:10:49 +02002813 CheckScriptFailure([
2814 'vim9script',
2815 ':# something',
2816 ], 'E488:')
2817 CheckScriptFailure([
2818 '# something',
2819 ], 'E488:')
2820 CheckScriptFailure([
2821 ':# something',
2822 ], 'E488:')
2823
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002824 { # block start
2825 } # block end
2826 CheckDefFailure([
2827 '{# comment',
2828 ], 'E488:')
2829 CheckDefFailure([
2830 '{',
2831 '}# comment',
2832 ], 'E488:')
2833
2834 echo "yes" # comment
2835 CheckDefFailure([
2836 'echo "yes"# comment',
2837 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002838 CheckScriptSuccess([
2839 'vim9script',
2840 'echo "yes" # something',
2841 ])
2842 CheckScriptFailure([
2843 'vim9script',
2844 'echo "yes"# something',
2845 ], 'E121:')
2846 CheckScriptFailure([
2847 'vim9script',
2848 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002849 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002850 CheckScriptFailure([
2851 'echo "yes" # something',
2852 ], 'E121:')
2853
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002854 exe "echo" # comment
2855 CheckDefFailure([
2856 'exe "echo"# comment',
2857 ], 'E488:')
2858 CheckScriptSuccess([
2859 'vim9script',
2860 'exe "echo" # something',
2861 ])
2862 CheckScriptFailure([
2863 'vim9script',
2864 'exe "echo"# something',
2865 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002866 CheckScriptFailure([
2867 'vim9script',
2868 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002869 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002870 CheckScriptFailure([
2871 'exe "echo" # something',
2872 ], 'E121:')
2873
Bram Moolenaar7a092242020-04-16 22:10:49 +02002874 CheckDefFailure([
2875 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002876 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002877 'catch',
2878 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002879 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002880 CheckScriptFailure([
2881 'vim9script',
2882 'try# comment',
2883 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002884 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002885 CheckDefFailure([
2886 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002887 ' throw#comment',
2888 'catch',
2889 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002890 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002891 CheckDefFailure([
2892 'try',
2893 ' throw "yes"#comment',
2894 'catch',
2895 'endtry',
2896 ], 'E488:')
2897 CheckDefFailure([
2898 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002899 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002900 'catch# comment',
2901 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002902 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002903 CheckScriptFailure([
2904 'vim9script',
2905 'try',
2906 ' echo "yes"',
2907 'catch# comment',
2908 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002909 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002910 CheckDefFailure([
2911 'try',
2912 ' echo "yes"',
2913 'catch /pat/# comment',
2914 'endtry',
2915 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002916 CheckDefFailure([
2917 'try',
2918 'echo "yes"',
2919 'catch',
2920 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002921 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002922 CheckScriptFailure([
2923 'vim9script',
2924 'try',
2925 ' echo "yes"',
2926 'catch',
2927 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002928 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002929
2930 CheckScriptSuccess([
2931 'vim9script',
2932 'hi # comment',
2933 ])
2934 CheckScriptFailure([
2935 'vim9script',
2936 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002937 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002938 CheckScriptSuccess([
2939 'vim9script',
2940 'hi Search # comment',
2941 ])
2942 CheckScriptFailure([
2943 'vim9script',
2944 'hi Search# comment',
2945 ], 'E416:')
2946 CheckScriptSuccess([
2947 'vim9script',
2948 'hi link This Search # comment',
2949 ])
2950 CheckScriptFailure([
2951 'vim9script',
2952 'hi link This That# comment',
2953 ], 'E413:')
2954 CheckScriptSuccess([
2955 'vim9script',
2956 'hi clear This # comment',
2957 'hi clear # comment',
2958 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002959 # not tested, because it doesn't give an error but a warning:
2960 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02002961 CheckScriptFailure([
2962 'vim9script',
2963 'hi clear# comment',
2964 ], 'E416:')
2965
2966 CheckScriptSuccess([
2967 'vim9script',
2968 'hi Group term=bold',
2969 'match Group /todo/ # comment',
2970 ])
2971 CheckScriptFailure([
2972 'vim9script',
2973 'hi Group term=bold',
2974 'match Group /todo/# comment',
2975 ], 'E488:')
2976 CheckScriptSuccess([
2977 'vim9script',
2978 'match # comment',
2979 ])
2980 CheckScriptFailure([
2981 'vim9script',
2982 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002983 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02002984 CheckScriptSuccess([
2985 'vim9script',
2986 'match none # comment',
2987 ])
2988 CheckScriptFailure([
2989 'vim9script',
2990 'match none# comment',
2991 ], 'E475:')
2992
2993 CheckScriptSuccess([
2994 'vim9script',
2995 'menutrans clear # comment',
2996 ])
2997 CheckScriptFailure([
2998 'vim9script',
2999 'menutrans clear# comment text',
3000 ], 'E474:')
3001
3002 CheckScriptSuccess([
3003 'vim9script',
3004 'syntax clear # comment',
3005 ])
3006 CheckScriptFailure([
3007 'vim9script',
3008 'syntax clear# comment text',
3009 ], 'E28:')
3010 CheckScriptSuccess([
3011 'vim9script',
3012 'syntax keyword Word some',
3013 'syntax clear Word # comment',
3014 ])
3015 CheckScriptFailure([
3016 'vim9script',
3017 'syntax keyword Word some',
3018 'syntax clear Word# comment text',
3019 ], 'E28:')
3020
3021 CheckScriptSuccess([
3022 'vim9script',
3023 'syntax list # comment',
3024 ])
3025 CheckScriptFailure([
3026 'vim9script',
3027 'syntax list# comment text',
3028 ], 'E28:')
3029
3030 CheckScriptSuccess([
3031 'vim9script',
3032 'syntax match Word /pat/ oneline # comment',
3033 ])
3034 CheckScriptFailure([
3035 'vim9script',
3036 'syntax match Word /pat/ oneline# comment',
3037 ], 'E475:')
3038
3039 CheckScriptSuccess([
3040 'vim9script',
3041 'syntax keyword Word word # comm[ent',
3042 ])
3043 CheckScriptFailure([
3044 'vim9script',
3045 'syntax keyword Word word# comm[ent',
3046 ], 'E789:')
3047
3048 CheckScriptSuccess([
3049 'vim9script',
3050 'syntax match Word /pat/ # comment',
3051 ])
3052 CheckScriptFailure([
3053 'vim9script',
3054 'syntax match Word /pat/# comment',
3055 ], 'E402:')
3056
3057 CheckScriptSuccess([
3058 'vim9script',
3059 'syntax match Word /pat/ contains=Something # comment',
3060 ])
3061 CheckScriptFailure([
3062 'vim9script',
3063 'syntax match Word /pat/ contains=Something# comment',
3064 ], 'E475:')
3065 CheckScriptFailure([
3066 'vim9script',
3067 'syntax match Word /pat/ contains= # comment',
3068 ], 'E406:')
3069 CheckScriptFailure([
3070 'vim9script',
3071 'syntax match Word /pat/ contains=# comment',
3072 ], 'E475:')
3073
3074 CheckScriptSuccess([
3075 'vim9script',
3076 'syntax region Word start=/pat/ end=/pat/ # comment',
3077 ])
3078 CheckScriptFailure([
3079 'vim9script',
3080 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02003081 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003082
3083 CheckScriptSuccess([
3084 'vim9script',
3085 'syntax sync # comment',
3086 ])
3087 CheckScriptFailure([
3088 'vim9script',
3089 'syntax sync# comment',
3090 ], 'E404:')
3091 CheckScriptSuccess([
3092 'vim9script',
3093 'syntax sync ccomment # comment',
3094 ])
3095 CheckScriptFailure([
3096 'vim9script',
3097 'syntax sync ccomment# comment',
3098 ], 'E404:')
3099
3100 CheckScriptSuccess([
3101 'vim9script',
3102 'syntax cluster Some contains=Word # comment',
3103 ])
3104 CheckScriptFailure([
3105 'vim9script',
3106 'syntax cluster Some contains=Word# comment',
3107 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003108
3109 CheckScriptSuccess([
3110 'vim9script',
3111 'command Echo echo # comment',
3112 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003113 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003114 ])
3115 CheckScriptFailure([
3116 'vim9script',
3117 'command Echo echo# comment',
3118 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003119 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003120 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003121
3122 var curdir = getcwd()
3123 CheckScriptSuccess([
3124 'command Echo cd " comment',
3125 'Echo',
3126 'delcommand Echo',
3127 ])
3128 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01003129 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003130 'command Echo cd # comment',
3131 'Echo',
3132 'delcommand Echo',
3133 ])
3134 CheckScriptFailure([
3135 'vim9script',
3136 'command Echo cd " comment',
3137 'Echo',
3138 ], 'E344:')
3139 delcommand Echo
3140 chdir(curdir)
3141
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003142 CheckScriptFailure([
3143 'vim9script',
3144 'command Echo# comment',
3145 ], 'E182:')
3146 CheckScriptFailure([
3147 'vim9script',
3148 'command Echo echo',
3149 'command Echo# comment',
3150 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003151 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003152
3153 CheckScriptSuccess([
3154 'vim9script',
3155 'function # comment',
3156 ])
3157 CheckScriptFailure([
3158 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02003159 'function " comment',
3160 ], 'E129:')
3161 CheckScriptFailure([
3162 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003163 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003164 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003165 CheckScriptSuccess([
3166 'vim9script',
3167 'function CheckScriptSuccess # comment',
3168 ])
3169 CheckScriptFailure([
3170 'vim9script',
3171 'function CheckScriptSuccess# comment',
3172 ], 'E488:')
3173
3174 CheckScriptSuccess([
3175 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003176 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003177 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003178 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003179 ])
3180 CheckScriptFailure([
3181 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003182 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003183 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003184 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003185 ], 'E488:')
3186
3187 CheckScriptSuccess([
3188 'vim9script',
3189 'call execute("ls") # comment',
3190 ])
3191 CheckScriptFailure([
3192 'vim9script',
3193 'call execute("ls")# comment',
3194 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02003195
3196 CheckScriptFailure([
3197 'def Test() " comment',
3198 'enddef',
3199 ], 'E488:')
3200 CheckScriptFailure([
3201 'vim9script',
3202 'def Test() " comment',
3203 'enddef',
3204 ], 'E488:')
3205
3206 CheckScriptSuccess([
3207 'func Test() " comment',
3208 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003209 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02003210 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02003211 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02003212 'vim9script',
3213 'func Test() " comment',
3214 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02003215 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02003216
3217 CheckScriptSuccess([
3218 'def Test() # comment',
3219 'enddef',
3220 ])
3221 CheckScriptFailure([
3222 'func Test() # comment',
3223 'endfunc',
3224 ], 'E488:')
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003225
3226 var lines =<< trim END
3227 vim9script
3228 syn region Text
3229 \ start='foo'
3230 #\ comment
3231 \ end='bar'
Bram Moolenaar5072b472021-06-03 21:56:10 +02003232 syn region Text start='foo'
3233 #\ comment
3234 \ end='bar'
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003235 END
3236 CheckScriptSuccess(lines)
3237
3238 lines =<< trim END
3239 vim9script
3240 syn region Text
3241 \ start='foo'
3242 "\ comment
3243 \ end='bar'
3244 END
3245 CheckScriptFailure(lines, 'E399:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003246enddef
3247
3248def Test_vim9_comment_gui()
3249 CheckCanRunGui
3250
3251 CheckScriptFailure([
3252 'vim9script',
3253 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003254 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003255 CheckScriptFailure([
3256 'vim9script',
3257 'gui -f#comment'
3258 ], 'E499:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003259enddef
3260
Bram Moolenaara26b9702020-04-18 19:53:28 +02003261def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02003262 au TabEnter *.vim g:entered = 1
3263 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02003264
3265 edit test.vim
3266 doautocmd TabEnter #comment
3267 assert_equal(1, g:entered)
3268
3269 doautocmd TabEnter f.x
3270 assert_equal(2, g:entered)
3271
3272 g:entered = 0
3273 doautocmd TabEnter f.x #comment
3274 assert_equal(2, g:entered)
3275
3276 assert_fails('doautocmd Syntax#comment', 'E216:')
3277
3278 au! TabEnter
3279 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003280
3281 CheckScriptSuccess([
3282 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02003283 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003284 'b:var = 456',
3285 'w:var = 777',
3286 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003287 'unlet g:var w:var # something',
3288 ])
3289
3290 CheckScriptFailure([
3291 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003292 'let var = 123',
3293 ], 'E1126: Cannot use :let in Vim9 script')
3294
3295 CheckScriptFailure([
3296 'vim9script',
3297 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003298 ], 'E1016: Cannot declare a global variable:')
3299
3300 CheckScriptFailure([
3301 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003302 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003303 ], 'E1016: Cannot declare a buffer variable:')
3304
3305 CheckScriptFailure([
3306 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003307 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003308 ], 'E1016: Cannot declare a window variable:')
3309
3310 CheckScriptFailure([
3311 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003312 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003313 ], 'E1016: Cannot declare a tab variable:')
3314
3315 CheckScriptFailure([
3316 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003317 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003318 ], 'E1016: Cannot declare a v: variable:')
3319
3320 CheckScriptFailure([
3321 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003322 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003323 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02003324
3325 CheckScriptFailure([
3326 'vim9script',
3327 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003328 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003329 ], 'E108:')
3330
3331 CheckScriptFailure([
3332 'let g:var = 123',
3333 'unlet g:var # something',
3334 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003335
3336 CheckScriptSuccess([
3337 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003338 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003339 ' echo "yes"',
3340 'elseif 2 #comment',
3341 ' echo "no"',
3342 'endif',
3343 ])
3344
3345 CheckScriptFailure([
3346 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003347 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003348 ' echo "yes"',
3349 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003350 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003351
3352 CheckScriptFailure([
3353 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003354 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003355 ' echo "yes"',
3356 'elseif 2#comment',
3357 ' echo "no"',
3358 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003359 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003360
3361 CheckScriptSuccess([
3362 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003363 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003364 ])
3365
3366 CheckScriptFailure([
3367 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003368 'var v = 1# comment6',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003369 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003370
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003371 CheckScriptSuccess([
3372 'vim9script',
3373 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003374 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003375 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003376 'dsearch /pat/ #comment',
3377 'bwipe!',
3378 ])
3379
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003380 CheckScriptFailure([
3381 'vim9script',
3382 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003383 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003384 ':$',
3385 'dsearch /pat/#comment',
3386 'bwipe!',
3387 ], 'E488:')
3388
3389 CheckScriptFailure([
3390 'vim9script',
3391 'func! SomeFunc()',
3392 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02003393enddef
3394
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003395def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003396 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003397 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02003398 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003399 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02003400 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003401 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02003402 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003403 END
3404 writefile(lines, 'Xfinished')
3405 source Xfinished
3406 assert_equal('two', g:res)
3407
3408 unlet g:res
3409 delete('Xfinished')
3410enddef
3411
Bram Moolenaara5d00772020-05-14 23:20:55 +02003412def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003413 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02003414 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02003415 def GetValue(): string
3416 return theVal
3417 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003418 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02003419 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02003420 theVal = 'else'
3421 g:laterVal = GetValue()
3422 END
3423 writefile(lines, 'Xforward')
3424 source Xforward
3425 assert_equal('something', g:initVal)
3426 assert_equal('else', g:laterVal)
3427
3428 unlet g:initVal
3429 unlet g:laterVal
3430 delete('Xforward')
3431enddef
3432
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003433def Test_source_vim9_from_legacy()
Bram Moolenaara6294952020-12-27 13:39:50 +01003434 var vim9_lines =<< trim END
3435 vim9script
3436 var local = 'local'
3437 g:global = 'global'
3438 export var exported = 'exported'
3439 export def GetText(): string
3440 return 'text'
3441 enddef
3442 END
3443 writefile(vim9_lines, 'Xvim9_script.vim')
3444
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003445 var legacy_lines =<< trim END
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003446 source Xvim9_script.vim
3447
3448 call assert_false(exists('local'))
3449 call assert_false(exists('exported'))
3450 call assert_false(exists('s:exported'))
3451 call assert_equal('global', global)
3452 call assert_equal('global', g:global)
3453
3454 " imported variable becomes script-local
3455 import exported from './Xvim9_script.vim'
3456 call assert_equal('exported', s:exported)
3457 call assert_false(exists('exported'))
3458
3459 " imported function becomes script-local
3460 import GetText from './Xvim9_script.vim'
3461 call assert_equal('text', s:GetText())
3462 call assert_false(exists('*GetText'))
3463 END
3464 writefile(legacy_lines, 'Xlegacy_script.vim')
3465
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003466 source Xlegacy_script.vim
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003467 assert_equal('global', g:global)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003468 unlet g:global
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003469
3470 delete('Xlegacy_script.vim')
3471 delete('Xvim9_script.vim')
3472enddef
Bram Moolenaara5d00772020-05-14 23:20:55 +02003473
Bram Moolenaare535db82021-03-31 21:07:24 +02003474def Test_declare_script_in_func()
3475 var lines =<< trim END
3476 vim9script
3477 func Declare()
3478 let s:local = 123
3479 endfunc
3480 Declare()
3481 assert_equal(123, local)
3482
3483 var error: string
3484 try
3485 local = 'asdf'
3486 catch
3487 error = v:exception
3488 endtry
3489 assert_match('E1012: Type mismatch; expected number but got string', error)
3490
3491 lockvar local
3492 try
3493 local = 999
3494 catch
3495 error = v:exception
3496 endtry
3497 assert_match('E741: Value is locked: local', error)
3498 END
3499 CheckScriptSuccess(lines)
3500enddef
3501
3502
Bram Moolenaar7d699702020-08-14 20:52:28 +02003503func Test_vim9script_not_global()
3504 " check that items defined in Vim9 script are script-local, not global
3505 let vim9lines =<< trim END
3506 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003507 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02003508 func TheFunc()
3509 echo 'local'
3510 endfunc
3511 def DefFunc()
3512 echo 'local'
3513 enddef
3514 END
3515 call writefile(vim9lines, 'Xvim9script.vim')
3516 source Xvim9script.vim
3517 try
3518 echo g:var
3519 assert_report('did not fail')
3520 catch /E121:/
3521 " caught
3522 endtry
3523 try
3524 call TheFunc()
3525 assert_report('did not fail')
3526 catch /E117:/
3527 " caught
3528 endtry
3529 try
3530 call DefFunc()
3531 assert_report('did not fail')
3532 catch /E117:/
3533 " caught
3534 endtry
3535
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003536 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02003537endfunc
3538
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02003539def Test_vim9_copen()
3540 # this was giving an error for setting w:quickfix_title
3541 copen
3542 quit
3543enddef
3544
Bram Moolenaar03290b82020-12-19 16:30:44 +01003545" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003546def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003547 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003548 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01003549 def some#gettest(): string
3550 return 'test'
3551 enddef
3552 g:some#name = 'name'
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003553 g:some#dict = {key: 'value'}
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003554
3555 def some#varargs(a1: string, ...l: list<string>): string
3556 return a1 .. l[0] .. l[1]
3557 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01003558 END
3559
3560 mkdir('Xdir/autoload', 'p')
3561 writefile(lines, 'Xdir/autoload/some.vim')
3562 var save_rtp = &rtp
3563 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3564
3565 assert_equal('test', g:some#gettest())
3566 assert_equal('name', g:some#name)
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003567 assert_equal('value', g:some#dict.key)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003568 g:some#other = 'other'
3569 assert_equal('other', g:some#other)
3570
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003571 assert_equal('abc', some#varargs('a', 'b', 'c'))
3572
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003573 # upper case script name works
3574 lines =<< trim END
3575 vim9script
3576 def Other#getOther(): string
3577 return 'other'
3578 enddef
3579 END
3580 writefile(lines, 'Xdir/autoload/Other.vim')
3581 assert_equal('other', g:Other#getOther())
3582
Bram Moolenaar03290b82020-12-19 16:30:44 +01003583 delete('Xdir', 'rf')
3584 &rtp = save_rtp
3585enddef
3586
3587" test using a vim9script that is auto-loaded from an autocmd
3588def Test_vim9_aucmd_autoload()
3589 var lines =<< trim END
3590 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003591 def foo#test()
3592 echomsg getreg('"')
3593 enddef
3594 END
3595
3596 mkdir('Xdir/autoload', 'p')
3597 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003598 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003599 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3600 augroup test
3601 autocmd TextYankPost * call foo#test()
3602 augroup END
3603
3604 normal Y
3605
3606 augroup test
3607 autocmd!
3608 augroup END
3609 delete('Xdir', 'rf')
3610 &rtp = save_rtp
3611enddef
3612
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003613" This was causing a crash because suppress_errthrow wasn't reset.
3614def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003615 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003616 vim9script
3617 def crash#func()
3618 try
3619 for x in List()
3620 endfor
3621 catch
3622 endtry
3623 g:ok = true
3624 enddef
3625 fu List()
3626 invalid
3627 endfu
3628 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01003629 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003630 catch /wontmatch/
3631 endtry
3632 END
3633 call mkdir('Xruntime/autoload', 'p')
3634 call writefile(lines, 'Xruntime/autoload/crash.vim')
3635
3636 # run in a separate Vim to avoid the side effects of assert_fails()
3637 lines =<< trim END
3638 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
3639 call crash#func()
3640 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003641 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003642 END
3643 writefile(lines, 'Xscript')
3644 RunVim([], [], '-S Xscript')
3645 assert_equal(['ok'], readfile('Xdidit'))
3646
3647 delete('Xdidit')
3648 delete('Xscript')
3649 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01003650
3651 lines =<< trim END
3652 vim9script
3653 var foo#bar = 'asdf'
3654 END
3655 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003656enddef
3657
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003658def Test_script_var_in_autocmd()
3659 # using a script variable from an autocommand, defined in a :def function in a
3660 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003661 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003662 let s:counter = 1
3663 def s:Func()
3664 au! CursorHold
3665 au CursorHold * s:counter += 1
3666 enddef
3667 call s:Func()
3668 doau CursorHold
3669 call assert_equal(2, s:counter)
3670 au! CursorHold
3671 END
3672 CheckScriptSuccess(lines)
3673enddef
3674
Bram Moolenaarb5841b92021-07-15 18:09:53 +02003675def Test_error_in_autoload_script()
3676 var save_rtp = &rtp
3677 var dir = getcwd() .. '/Xruntime'
3678 &rtp = dir
3679 mkdir(dir .. '/autoload', 'p')
3680
3681 var lines =<< trim END
3682 vim9script noclear
3683 def script#autoloaded()
3684 enddef
3685 def Broken()
3686 var x: any = ''
3687 eval x != 0
3688 enddef
3689 Broken()
3690 END
3691 writefile(lines, dir .. '/autoload/script.vim')
3692
3693 lines =<< trim END
3694 vim9script
3695 def CallAutoloaded()
3696 script#autoloaded()
3697 enddef
3698
3699 function Legacy()
3700 try
3701 call s:CallAutoloaded()
3702 catch
3703 call assert_match('E1030: Using a String as a Number', v:exception)
3704 endtry
3705 endfunction
3706
3707 Legacy()
3708 END
3709 CheckScriptSuccess(lines)
3710
3711 &rtp = save_rtp
3712 delete(dir, 'rf')
3713enddef
3714
Bram Moolenaar3896a102020-08-09 14:33:55 +02003715def Test_cmdline_win()
3716 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
3717 # the command line window.
3718 mkdir('rtp/syntax', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003719 var export_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02003720 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003721 export var That = 'yes'
Bram Moolenaar3896a102020-08-09 14:33:55 +02003722 END
3723 writefile(export_lines, 'rtp/syntax/Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003724 var import_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02003725 vim9script
3726 import That from './Xexport.vim'
3727 END
3728 writefile(import_lines, 'rtp/syntax/vim.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003729 var save_rtp = &rtp
Bram Moolenaar3896a102020-08-09 14:33:55 +02003730 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
3731 syntax on
3732 augroup CmdWin
3733 autocmd CmdwinEnter * g:got_there = 'yes'
3734 augroup END
3735 # this will open and also close the cmdline window
3736 feedkeys('q:', 'xt')
3737 assert_equal('yes', g:got_there)
3738
3739 augroup CmdWin
3740 au!
3741 augroup END
3742 &rtp = save_rtp
3743 delete('rtp', 'rf')
3744enddef
3745
Bram Moolenaare3d46852020-08-29 13:39:17 +02003746def Test_invalid_sid()
3747 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003748
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003749 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02003750 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02003751 endif
3752 delete('Xdidit')
3753enddef
3754
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003755def Test_restoring_cpo()
3756 writefile(['vim9script', 'set nocp'], 'Xsourced')
3757 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
3758 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
3759 assert_equal(['done'], readfile('Xdone'))
3760 endif
3761 delete('Xsourced')
3762 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01003763 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01003764
3765 writefile(['vim9script'], 'XanotherScript')
3766 set cpo=aABceFsMny>
3767 edit XanotherScript
3768 so %
3769 assert_equal('aABceFsMny>', &cpo)
3770 :1del
3771 w
3772 so %
3773 assert_equal('aABceFsMny>', &cpo)
3774
3775 delete('XanotherScript')
3776 set cpo&vim
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003777enddef
3778
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003779" Use :function so we can use Check commands
3780func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003781 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003782 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003783
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003784 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003785 vim9script
3786 def script#func()
3787 enddef
3788 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003789 call mkdir('Xdir/autoload', 'p')
3790 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003791
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003792 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003793 vim9script
3794 set cpo+=M
3795 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02003796 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003797 setline(1, 'some text')
3798 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003799 call writefile(lines, 'XTest_redraw_cpo')
3800 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
3801 call term_sendkeys(buf, "V:")
3802 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003803
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003804 " clean up
3805 call term_sendkeys(buf, "\<Esc>u")
3806 call StopVimInTerminal(buf)
3807 call delete('XTest_redraw_cpo')
3808 call delete('Xdir', 'rf')
3809endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003810
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003811
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003812def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003813 var lines =<< trim END
3814 var name: any
3815 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003816 END
3817 CheckDefAndScriptSuccess(lines)
3818enddef
3819
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003820func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02003821 CheckRunVimInTerminal
3822
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003823 " call indirectly to avoid compilation error for missing functions
3824 call Run_Test_define_func_at_command_line()
3825endfunc
3826
3827def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003828 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003829 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003830 func CheckAndQuit()
3831 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
3832 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
3833 endfunc
3834 END
3835 writefile([''], 'Xdidcmd')
3836 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01003837 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003838 # define Afunc() on the command line
3839 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
3840 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003841 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003842
3843 call StopVimInTerminal(buf)
3844 delete('XcallFunc')
3845 delete('Xdidcmd')
3846enddef
3847
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003848def Test_script_var_scope()
3849 var lines =<< trim END
3850 vim9script
3851 if true
3852 if true
3853 var one = 'one'
3854 echo one
3855 endif
3856 echo one
3857 endif
3858 END
3859 CheckScriptFailure(lines, 'E121:', 7)
3860
3861 lines =<< trim END
3862 vim9script
3863 if true
3864 if false
3865 var one = 'one'
3866 echo one
3867 else
3868 var one = 'one'
3869 echo one
3870 endif
3871 echo one
3872 endif
3873 END
3874 CheckScriptFailure(lines, 'E121:', 10)
3875
3876 lines =<< trim END
3877 vim9script
3878 while true
3879 var one = 'one'
3880 echo one
3881 break
3882 endwhile
3883 echo one
3884 END
3885 CheckScriptFailure(lines, 'E121:', 7)
3886
3887 lines =<< trim END
3888 vim9script
3889 for i in range(1)
3890 var one = 'one'
3891 echo one
3892 endfor
3893 echo one
3894 END
3895 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003896
3897 lines =<< trim END
3898 vim9script
3899 {
3900 var one = 'one'
3901 assert_equal('one', one)
3902 }
3903 assert_false(exists('one'))
3904 assert_false(exists('s:one'))
3905 END
3906 CheckScriptSuccess(lines)
3907
3908 lines =<< trim END
3909 vim9script
3910 {
3911 var one = 'one'
3912 echo one
3913 }
3914 echo one
3915 END
3916 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003917enddef
3918
Bram Moolenaar352134b2020-10-17 22:04:08 +02003919def Test_catch_exception_in_callback()
3920 var lines =<< trim END
3921 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02003922 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02003923 try
3924 var x: string
3925 var y: string
3926 # this error should be caught with CHECKLEN
3927 [x, y] = ['']
3928 catch
3929 g:caught = 'yes'
3930 endtry
3931 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01003932 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02003933 feedkeys("\r", 'xt')
3934 END
3935 CheckScriptSuccess(lines)
3936
3937 unlet g:caught
3938enddef
3939
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003940def Test_no_unknown_error_after_error()
3941 if !has('unix') || !has('job')
3942 throw 'Skipped: not unix of missing +job feature'
3943 endif
3944 var lines =<< trim END
3945 vim9script
3946 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02003947 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003948 eval [][0]
3949 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02003950 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003951 sleep 1m
3952 source += l
3953 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01003954 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01003955 while job_status(myjob) == 'run'
3956 sleep 10m
3957 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01003958 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02003959 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01003960 END
3961 writefile(lines, 'Xdef')
3962 assert_fails('so Xdef', ['E684:', 'E1012:'])
3963 delete('Xdef')
3964enddef
3965
Bram Moolenaar4324d872020-12-01 20:12:24 +01003966def InvokeNormal()
3967 exe "norm! :m+1\r"
3968enddef
3969
3970def Test_invoke_normal_in_visual_mode()
3971 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
3972 new
3973 setline(1, ['aaa', 'bbb'])
3974 feedkeys("V\<F3>", 'xt')
3975 assert_equal(['bbb', 'aaa'], getline(1, 2))
3976 xunmap <F3>
3977enddef
3978
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003979def Test_white_space_after_command()
3980 var lines =<< trim END
3981 exit_cb: Func})
3982 END
3983 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003984
3985 lines =<< trim END
3986 e#
3987 END
3988 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003989enddef
3990
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003991def Test_script_var_gone_when_sourced_twice()
3992 var lines =<< trim END
3993 vim9script
3994 if exists('g:guard')
3995 finish
3996 endif
3997 g:guard = 1
3998 var name = 'thename'
3999 def g:GetName(): string
4000 return name
4001 enddef
4002 def g:SetName(arg: string)
4003 name = arg
4004 enddef
4005 END
4006 writefile(lines, 'XscriptTwice.vim')
4007 so XscriptTwice.vim
4008 assert_equal('thename', g:GetName())
4009 g:SetName('newname')
4010 assert_equal('newname', g:GetName())
4011 so XscriptTwice.vim
4012 assert_fails('call g:GetName()', 'E1149:')
4013 assert_fails('call g:SetName("x")', 'E1149:')
4014
4015 delfunc g:GetName
4016 delfunc g:SetName
4017 delete('XscriptTwice.vim')
4018 unlet g:guard
4019enddef
4020
4021def Test_import_gone_when_sourced_twice()
4022 var exportlines =<< trim END
4023 vim9script
4024 if exists('g:guard')
4025 finish
4026 endif
4027 g:guard = 1
4028 export var name = 'someName'
4029 END
4030 writefile(exportlines, 'XexportScript.vim')
4031
4032 var lines =<< trim END
4033 vim9script
4034 import name from './XexportScript.vim'
4035 def g:GetName(): string
4036 return name
4037 enddef
4038 END
4039 writefile(lines, 'XscriptImport.vim')
4040 so XscriptImport.vim
4041 assert_equal('someName', g:GetName())
4042
4043 so XexportScript.vim
4044 assert_fails('call g:GetName()', 'E1149:')
4045
4046 delfunc g:GetName
4047 delete('XexportScript.vim')
4048 delete('XscriptImport.vim')
4049 unlet g:guard
4050enddef
4051
Bram Moolenaar10b94212021-02-19 21:42:57 +01004052def Test_unsupported_commands()
4053 var lines =<< trim END
4054 ka
4055 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004056 CheckDefFailure(lines, 'E476:')
4057 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaar10b94212021-02-19 21:42:57 +01004058
4059 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01004060 :1ka
4061 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004062 CheckDefFailure(lines, 'E476:')
4063 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaarada1d872021-02-20 08:16:51 +01004064
4065 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01004066 t
4067 END
4068 CheckDefFailure(lines, 'E1100:')
4069 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4070
4071 lines =<< trim END
4072 x
4073 END
4074 CheckDefFailure(lines, 'E1100:')
4075 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4076
4077 lines =<< trim END
4078 xit
4079 END
4080 CheckDefFailure(lines, 'E1100:')
4081 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4082enddef
4083
Bram Moolenaarc70fe462021-04-17 17:59:19 +02004084def Test_mapping_line_number()
4085 var lines =<< trim END
4086 vim9script
4087 def g:FuncA()
4088 # Some comment
4089 FuncB(0)
4090 enddef
4091 # Some comment
4092 def FuncB(
4093 # Some comment
4094 n: number
4095 )
4096 exe 'nno '
4097 # Some comment
4098 .. '<F3> a'
4099 .. 'b'
4100 .. 'c'
4101 enddef
4102 END
4103 CheckScriptSuccess(lines)
4104 var res = execute('verbose nmap <F3>')
4105 assert_match('No mapping found', res)
4106
4107 g:FuncA()
4108 res = execute('verbose nmap <F3>')
4109 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
4110
4111 nunmap <F3>
4112 delfunc g:FuncA
4113enddef
4114
Bram Moolenaardeb108b2021-07-08 17:35:36 +02004115def Test_option_set()
4116 # legacy script allows for white space
4117 var lines =<< trim END
4118 set foldlevel =11
4119 call assert_equal(11, &foldlevel)
4120 END
4121 CheckScriptSuccess(lines)
4122
4123 set foldlevel
4124 set foldlevel=12
4125 assert_equal(12, &foldlevel)
4126 set foldlevel+=2
4127 assert_equal(14, &foldlevel)
4128 set foldlevel-=3
4129 assert_equal(11, &foldlevel)
4130
4131 lines =<< trim END
4132 set foldlevel =1
4133 END
4134 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: =1')
4135
4136 lines =<< trim END
4137 set foldlevel +=1
4138 END
4139 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: +=1')
4140
4141 lines =<< trim END
4142 set foldlevel ^=1
4143 END
4144 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: ^=1')
4145
4146 lines =<< trim END
4147 set foldlevel -=1
4148 END
4149 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: -=1')
4150
4151 set foldlevel&
4152enddef
4153
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004154def Test_option_modifier()
Bram Moolenaar1594f312021-07-08 16:40:13 +02004155 # legacy script allows for white space
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004156 var lines =<< trim END
4157 set hlsearch & hlsearch !
4158 call assert_equal(1, &hlsearch)
4159 END
4160 CheckScriptSuccess(lines)
4161
Bram Moolenaar1594f312021-07-08 16:40:13 +02004162 set hlsearch
4163 set hlsearch!
4164 assert_equal(false, &hlsearch)
4165
4166 set hlsearch
4167 set hlsearch&
4168 assert_equal(false, &hlsearch)
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004169
4170 lines =<< trim END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004171 set hlsearch &
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004172 END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004173 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: &')
4174
4175 lines =<< trim END
4176 set hlsearch !
4177 END
4178 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: !')
4179
4180 set hlsearch&
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004181enddef
4182
Bram Moolenaarc03fe662021-07-11 16:52:45 +02004183" This must be called last, it may cause following :def functions to fail
4184def Test_xxx_echoerr_line_number()
4185 var lines =<< trim END
4186 echoerr 'some'
4187 .. ' error'
4188 .. ' continued'
4189 END
4190 CheckDefExecAndScriptFailure(lines, 'some error continued', 1)
4191enddef
4192
Bram Moolenaar648594e2021-07-11 17:55:01 +02004193def ProfiledWithLambda()
Bram Moolenaard9162552021-07-11 15:26:13 +02004194 var n = 3
4195 echo [[1, 2], [3, 4]]->filter((_, l) => l[0] == n)
4196enddef
4197
Bram Moolenaar648594e2021-07-11 17:55:01 +02004198def ProfiledNested()
4199 var x = 0
4200 def Nested(): any
4201 return x
4202 enddef
4203 Nested()
4204enddef
4205
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004206def ProfiledNestedProfiled()
4207 var x = 0
4208 def Nested(): any
4209 return x
4210 enddef
4211 Nested()
4212enddef
4213
Bram Moolenaard9162552021-07-11 15:26:13 +02004214" Execute this near the end, profiling doesn't stop until Vim exists.
4215" This only tests that it works, not the profiling output.
4216def Test_xx_profile_with_lambda()
Bram Moolenaar4ece1522021-07-11 16:31:51 +02004217 CheckFeature profile
4218
Bram Moolenaard9162552021-07-11 15:26:13 +02004219 profile start Xprofile.log
Bram Moolenaar648594e2021-07-11 17:55:01 +02004220 profile func ProfiledWithLambda
4221 ProfiledWithLambda()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004222
Bram Moolenaar648594e2021-07-11 17:55:01 +02004223 profile func ProfiledNested
4224 ProfiledNested()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004225
4226 # Also profile the nested function. Use a different function, although the
4227 # contents is the same, to make sure it was not already compiled.
4228 profile func *
4229 ProfiledNestedProfiled()
4230
4231 profdel func *
4232 profile pause
Bram Moolenaard9162552021-07-11 15:26:13 +02004233enddef
4234
Bram Moolenaar585fea72020-04-02 22:33:21 +02004235" Keep this last, it messes up highlighting.
4236def Test_substitute_cmd()
4237 new
4238 setline(1, 'something')
4239 :substitute(some(other(
4240 assert_equal('otherthing', getline(1))
4241 bwipe!
4242
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004243 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004244 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02004245 vim9script
4246 new
4247 setline(1, 'something')
4248 :substitute(some(other(
4249 assert_equal('otherthing', getline(1))
4250 bwipe!
4251 END
4252 writefile(lines, 'Xvim9lines')
4253 source Xvim9lines
4254
4255 delete('Xvim9lines')
4256enddef
4257
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker