blob: 14684c0961feba69f86b9edb27f814034235f909 [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
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +0200584 # no requirement for spaces before |
585 try|echo 0|catch|endtry
586
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100587 # return in finally after empty catch
588 def ReturnInFinally(): number
589 try
590 finally
591 return 4
592 endtry
593 return 2
594 enddef
595 assert_equal(4, ReturnInFinally())
Bram Moolenaar8ac681a2021-06-15 20:06:34 +0200596
597 var lines =<< trim END
598 vim9script
599 try
600 acos('0.5')
601 ->setline(1)
602 catch
603 g:caught = v:exception
604 endtry
605 END
606 CheckScriptSuccess(lines)
607 assert_match('E808: Number or Float required', g:caught)
608 unlet g:caught
Bram Moolenaar41978282021-07-04 14:47:30 +0200609
610 # missing catch and/or finally
611 lines =<< trim END
612 vim9script
613 try
614 echo 'something'
615 endtry
616 END
617 CheckScriptFailure(lines, 'E1032:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618enddef
619
Bram Moolenaar3f987b52021-06-30 12:02:24 +0200620def Test_try_in_catch()
621 var lines =<< trim END
622 vim9script
623 var seq = []
624 def DoIt()
625 try
626 seq->add('throw 1')
627 eval [][0]
628 seq->add('notreached')
629 catch
630 seq->add('catch')
631 try
632 seq->add('throw 2')
633 eval [][0]
634 seq->add('notreached')
635 catch /nothing/
636 seq->add('notreached')
637 endtry
638 seq->add('done')
639 endtry
640 enddef
641 DoIt()
642 assert_equal(['throw 1', 'catch', 'throw 2', 'done'], seq)
643 END
644enddef
645
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200646def Test_error_in_catch()
647 var lines =<< trim END
648 try
649 eval [][0]
650 catch /E684:/
651 eval [][0]
652 endtry
653 END
654 CheckDefExecFailure(lines, 'E684:', 4)
655enddef
656
Bram Moolenaar2e34c342021-03-14 12:13:33 +0100657" :while at the very start of a function that :continue jumps to
658def TryContinueFunc()
659 while g:Count < 2
660 g:sequence ..= 't'
661 try
662 echoerr 'Test'
663 catch
664 g:Count += 1
665 g:sequence ..= 'c'
666 continue
667 endtry
668 g:sequence ..= 'e'
669 g:Count += 1
670 endwhile
671enddef
672
673def Test_continue_in_try_in_while()
674 g:Count = 0
675 g:sequence = ''
676 TryContinueFunc()
677 assert_equal('tctc', g:sequence)
678 unlet g:Count
679 unlet g:sequence
680enddef
681
Bram Moolenaar9cb577a2021-02-22 22:45:10 +0100682def Test_nocatch_return_in_try()
683 # return in try block returns normally
684 def ReturnInTry(): string
685 try
686 return '"some message"'
687 catch
688 endtry
689 return 'not reached'
690 enddef
691 exe 'echoerr ' .. ReturnInTry()
692enddef
693
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100694def Test_cnext_works_in_catch()
695 var lines =<< trim END
696 vim9script
Bram Moolenaarc3235272021-07-10 19:42:03 +0200697 au BufEnter * eval 1 + 2
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100698 writefile(['text'], 'Xfile1')
699 writefile(['text'], 'Xfile2')
700 var items = [
701 {lnum: 1, filename: 'Xfile1', valid: true},
702 {lnum: 1, filename: 'Xfile2', valid: true}
703 ]
704 setqflist([], ' ', {items: items})
705 cwindow
706
707 def CnextOrCfirst()
708 # if cnext fails, cfirst is used
709 try
710 cnext
711 catch
712 cfirst
713 endtry
714 enddef
715
716 CnextOrCfirst()
717 CnextOrCfirst()
718 writefile([getqflist({idx: 0}).idx], 'Xresult')
719 qall
720 END
721 writefile(lines, 'XCatchCnext')
722 RunVim([], [], '--clean -S XCatchCnext')
723 assert_equal(['1'], readfile('Xresult'))
724
725 delete('Xfile1')
726 delete('Xfile2')
727 delete('XCatchCnext')
728 delete('Xresult')
729enddef
730
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100731def Test_throw_skipped()
732 if 0
733 throw dontgethere
734 endif
735enddef
736
Bram Moolenaar8f81b222021-01-14 21:47:06 +0100737def Test_nocatch_throw_silenced()
738 var lines =<< trim END
739 vim9script
740 def Func()
741 throw 'error'
742 enddef
743 silent! Func()
744 END
745 writefile(lines, 'XthrowSilenced')
746 source XthrowSilenced
747 delete('XthrowSilenced')
748enddef
749
Bram Moolenaare8593122020-07-18 15:17:02 +0200750def DeletedFunc(): list<any>
751 return ['delete me']
752enddef
753defcompile
754delfunc DeletedFunc
755
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100756def ThrowFromDef()
Bram Moolenaara72cfb82020-04-23 17:07:30 +0200757 throw "getout" # comment
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100758enddef
759
760func CatchInFunc()
761 try
762 call ThrowFromDef()
763 catch
764 let g:thrown_func = v:exception
765 endtry
766endfunc
767
768def CatchInDef()
769 try
770 ThrowFromDef()
771 catch
772 g:thrown_def = v:exception
773 endtry
774enddef
775
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100776def ReturnFinally(): string
777 try
778 return 'intry'
Bram Moolenaar373863e2020-09-26 17:20:53 +0200779 finall
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100780 g:in_finally = 'finally'
781 endtry
782 return 'end'
783enddef
784
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100785def Test_try_catch_nested()
786 CatchInFunc()
787 assert_equal('getout', g:thrown_func)
788
789 CatchInDef()
790 assert_equal('getout', g:thrown_def)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100791
792 assert_equal('intry', ReturnFinally())
793 assert_equal('finally', g:in_finally)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200794
795 var l = []
796 try
797 l->add('1')
798 throw 'bad'
799 l->add('x')
800 catch /bad/
801 l->add('2')
802 try
803 l->add('3')
804 throw 'one'
805 l->add('x')
806 catch /one/
807 l->add('4')
808 try
809 l->add('5')
810 throw 'more'
811 l->add('x')
812 catch /more/
813 l->add('6')
814 endtry
815 endtry
816 endtry
817 assert_equal(['1', '2', '3', '4', '5', '6'], l)
Bram Moolenaar834193a2021-06-30 20:39:15 +0200818
819 l = []
820 try
821 try
822 l->add('1')
823 throw 'foo'
824 l->add('x')
825 catch
826 l->add('2')
827 throw 'bar'
828 l->add('x')
829 finally
830 l->add('3')
831 endtry
832 l->add('x')
833 catch /bar/
834 l->add('4')
835 endtry
836 assert_equal(['1', '2', '3', '4'], l)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100837enddef
838
Bram Moolenaar9939f572020-09-16 22:29:52 +0200839def TryOne(): number
840 try
841 return 0
842 catch
843 endtry
844 return 0
845enddef
846
847def TryTwo(n: number): string
848 try
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200849 var x = {}
Bram Moolenaar9939f572020-09-16 22:29:52 +0200850 catch
851 endtry
852 return 'text'
853enddef
854
855def Test_try_catch_twice()
856 assert_equal('text', TryOne()->TryTwo())
857enddef
858
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100859def Test_try_catch_match()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200860 var seq = 'a'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100861 try
862 throw 'something'
863 catch /nothing/
864 seq ..= 'x'
865 catch /some/
866 seq ..= 'b'
867 catch /asdf/
868 seq ..= 'x'
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200869 catch ?a\?sdf?
870 seq ..= 'y'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100871 finally
872 seq ..= 'c'
873 endtry
874 assert_equal('abc', seq)
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100875enddef
876
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200877def Test_try_catch_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200878 CheckDefFailure(['catch'], 'E603:')
879 CheckDefFailure(['try', 'echo 0', 'catch', 'catch'], 'E1033:')
880 CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
881 CheckDefFailure(['finally'], 'E606:')
882 CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:')
883 CheckDefFailure(['endtry'], 'E602:')
884 CheckDefFailure(['while 1', 'endtry'], 'E170:')
885 CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:')
Bram Moolenaar13106602020-10-04 16:06:05 +0200886 CheckDefFailure(['if 1', 'endtry'], 'E171:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200887 CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:')
Bram Moolenaar585fea72020-04-02 22:33:21 +0200888
Bram Moolenaare4984292020-12-13 14:19:25 +0100889 CheckDefFailure(['throw'], 'E1143:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200890 CheckDefFailure(['throw xxx'], 'E1001:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200891enddef
892
Bram Moolenaar7c5b3c0362021-02-14 22:40:57 +0100893def Try_catch_skipped()
894 var l = []
895 try
896 finally
897 endtry
898
899 if 1
900 else
901 try
902 endtry
903 endif
904enddef
905
906" The skipped try/endtry was updating the wrong instruction.
907def Test_try_catch_skipped()
908 var instr = execute('disassemble Try_catch_skipped')
909 assert_match("NEWLIST size 0\n", instr)
910enddef
911
912
913
Bram Moolenaar006ad482020-06-30 20:55:15 +0200914def Test_throw_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200915 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200916 var lines =<< trim END
Bram Moolenaar006ad482020-06-30 20:55:15 +0200917 vim9script
918 try
919 throw 'one'
920 .. 'two'
921 catch
922 assert_equal('onetwo', v:exception)
923 endtry
924 END
925 CheckScriptSuccess(lines)
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200926
927 lines =<< trim END
928 vim9script
Bram Moolenaar352134b2020-10-17 22:04:08 +0200929 @r = ''
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200930 def Func()
931 throw @r
932 enddef
933 var result = ''
934 try
935 Func()
936 catch /E1129:/
937 result = 'caught'
938 endtry
939 assert_equal('caught', result)
940 END
941 CheckScriptSuccess(lines)
Bram Moolenaar006ad482020-06-30 20:55:15 +0200942enddef
943
Bram Moolenaared677f52020-08-12 16:38:10 +0200944def Test_error_in_nested_function()
Bram Moolenaar03dfde22021-02-14 13:17:22 +0100945 # an error in a nested :function aborts executing in the calling :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200946 var lines =<< trim END
Bram Moolenaared677f52020-08-12 16:38:10 +0200947 vim9script
948 def Func()
949 Error()
950 g:test_var = 1
951 enddef
952 func Error() abort
953 eval [][0]
954 endfunc
955 Func()
956 END
957 g:test_var = 0
958 CheckScriptFailure(lines, 'E684:')
959 assert_equal(0, g:test_var)
960enddef
961
Bram Moolenaar227c58a2021-04-28 20:40:44 +0200962def Test_abort_after_error()
963 var lines =<< trim END
964 vim9script
965 while true
966 echo notfound
967 endwhile
968 g:gotthere = true
969 END
970 g:gotthere = false
971 CheckScriptFailure(lines, 'E121:')
972 assert_false(g:gotthere)
973 unlet g:gotthere
974enddef
975
Bram Moolenaar37c83712020-06-30 21:18:36 +0200976def Test_cexpr_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200977 # only checks line continuation
Bram Moolenaar37c83712020-06-30 21:18:36 +0200978 set errorformat=File\ %f\ line\ %l
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200979 var lines =<< trim END
Bram Moolenaar37c83712020-06-30 21:18:36 +0200980 vim9script
981 cexpr 'File'
982 .. ' someFile' ..
983 ' line 19'
984 assert_equal(19, getqflist()[0].lnum)
985 END
986 CheckScriptSuccess(lines)
987 set errorformat&
988enddef
989
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200990def Test_statusline_syntax()
991 # legacy syntax is used for 'statusline'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200992 var lines =<< trim END
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200993 vim9script
994 func g:Status()
995 return '%{"x" is# "x"}'
996 endfunc
997 set laststatus=2 statusline=%!Status()
998 redrawstatus
999 set laststatus statusline=
1000 END
1001 CheckScriptSuccess(lines)
1002enddef
1003
Bram Moolenaarb2097502020-07-19 17:17:02 +02001004def Test_list_vimscript()
1005 # checks line continuation and comments
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001006 var lines =<< trim END
Bram Moolenaarb2097502020-07-19 17:17:02 +02001007 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001008 var mylist = [
Bram Moolenaarb2097502020-07-19 17:17:02 +02001009 'one',
1010 # comment
1011 'two', # empty line follows
1012
1013 'three',
1014 ]
1015 assert_equal(['one', 'two', 'three'], mylist)
1016 END
1017 CheckScriptSuccess(lines)
Bram Moolenaar66250c92020-08-20 15:02:42 +02001018
1019 # check all lines from heredoc are kept
1020 lines =<< trim END
1021 # comment 1
1022 two
1023 # comment 3
1024
1025 five
1026 # comment 6
1027 END
1028 assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines)
Bram Moolenaar4bce26b2021-01-22 22:06:56 +01001029
1030 lines =<< trim END
1031 [{
1032 a: 0}]->string()->assert_equal("[{'a': 0}]")
1033 END
1034 CheckDefAndScriptSuccess(lines)
Bram Moolenaarb2097502020-07-19 17:17:02 +02001035enddef
1036
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001037if has('channel')
1038 let someJob = test_null_job()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001039
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001040 def FuncWithError()
1041 echomsg g:someJob
1042 enddef
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001043
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001044 func Test_convert_emsg_to_exception()
1045 try
1046 call FuncWithError()
1047 catch
1048 call assert_match('Vim:E908:', v:exception)
1049 endtry
1050 endfunc
1051endif
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053let s:export_script_lines =<< trim END
1054 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001055 var name: string = 'bob'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 def Concat(arg: string): string
1057 return name .. arg
1058 enddef
Bram Moolenaar227a69d2020-05-15 18:17:28 +02001059 g:result = Concat('bie')
1060 g:localname = name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001061
1062 export const CONST = 1234
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001063 export var exported = 9876
1064 export var exp_name = 'John'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 export def Exported(): string
1066 return 'Exported'
1067 enddef
Bram Moolenaar24e93162021-07-18 20:40:33 +02001068 export def ExportedValue(): number
1069 return exported
1070 enddef
1071 export def ExportedInc()
1072 exported += 5
1073 enddef
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001074 export final theList = [1]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075END
1076
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001077def Undo_export_script_lines()
1078 unlet g:result
1079 unlet g:localname
1080enddef
1081
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001082def Test_vim9_import_export()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001083 var import_script_lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 vim9script
Bram Moolenaar24e93162021-07-18 20:40:33 +02001085 import {exported, Exported, ExportedValue} from './Xexport.vim'
1086 g:exported1 = exported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001087 exported += 3
Bram Moolenaar24e93162021-07-18 20:40:33 +02001088 g:exported2 = exported
1089 g:exported3 = ExportedValue()
1090
1091 import ExportedInc from './Xexport.vim'
1092 ExportedInc()
1093 g:exported_i1 = exported
1094 g:exported_i2 = ExportedValue()
1095
1096 exported = 11
1097 g:exported_s1 = exported
1098 g:exported_s2 = ExportedValue()
1099
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100 g:imported_func = Exported()
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001101
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001102 def GetExported(): string
Bram Moolenaare0de1712020-12-02 17:36:54 +01001103 var local_dict = {ref: Exported}
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001104 return local_dict.ref()
1105 enddef
1106 g:funcref_result = GetExported()
1107
Bram Moolenaar4db572e2021-07-18 18:21:38 +02001108 var dir = './'
1109 var ext = ".vim"
1110 import {exp_name} from dir .. 'Xexport' .. ext
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001111 g:imported_name = exp_name
1112 exp_name ..= ' Doe'
1113 g:imported_name_appended = exp_name
Bram Moolenaar24e93162021-07-18 20:40:33 +02001114 g:exported_later = exported
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001115
1116 import theList from './Xexport.vim'
1117 theList->add(2)
1118 assert_equal([1, 2], theList)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 END
1120
1121 writefile(import_script_lines, 'Ximport.vim')
1122 writefile(s:export_script_lines, 'Xexport.vim')
1123
1124 source Ximport.vim
1125
1126 assert_equal('bobbie', g:result)
1127 assert_equal('bob', g:localname)
Bram Moolenaar24e93162021-07-18 20:40:33 +02001128 assert_equal(9876, g:exported1)
1129 assert_equal(9879, g:exported2)
1130 assert_equal(9879, g:exported3)
1131
1132 assert_equal(9884, g:exported_i1)
1133 assert_equal(9884, g:exported_i2)
1134
1135 assert_equal(11, g:exported_s1)
1136 assert_equal(11, g:exported_s2)
1137 assert_equal(11, g:exported_later)
1138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 assert_equal('Exported', g:imported_func)
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001140 assert_equal('Exported', g:funcref_result)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001141 assert_equal('John', g:imported_name)
1142 assert_equal('John Doe', g:imported_name_appended)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001143 assert_false(exists('g:name'))
1144
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001145 Undo_export_script_lines()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001146 unlet g:exported1
1147 unlet g:exported2
1148 unlet g:exported3
1149 unlet g:exported_i1
1150 unlet g:exported_i2
1151 unlet g:exported_later
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 unlet g:imported_func
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001153 unlet g:imported_name g:imported_name_appended
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154 delete('Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001155
Bram Moolenaar1c991142020-07-04 13:15:31 +02001156 # similar, with line breaks
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001157 var import_line_break_script_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001158 vim9script
1159 import {
1160 exported,
1161 Exported,
1162 }
1163 from
1164 './Xexport.vim'
Bram Moolenaar24e93162021-07-18 20:40:33 +02001165 g:exported = exported
1166 exported += 7
1167 g:exported_added = exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001168 g:imported_func = Exported()
1169 END
1170 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
1171 source Ximport_lbr.vim
1172
Bram Moolenaar24e93162021-07-18 20:40:33 +02001173 assert_equal(11, g:exported)
1174 assert_equal(18, g:exported_added)
Bram Moolenaar1c991142020-07-04 13:15:31 +02001175 assert_equal('Exported', g:imported_func)
1176
1177 # exported script not sourced again
1178 assert_false(exists('g:result'))
Bram Moolenaar24e93162021-07-18 20:40:33 +02001179 unlet g:exported
1180 unlet g:exported_added
Bram Moolenaar1c991142020-07-04 13:15:31 +02001181 unlet g:imported_func
1182 delete('Ximport_lbr.vim')
1183
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001184 var import_star_as_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001185 vim9script
1186 import * as Export from './Xexport.vim'
1187 def UseExport()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001188 g:exported_def = Export.exported
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001189 enddef
Bram Moolenaar24e93162021-07-18 20:40:33 +02001190 g:exported_script = Export.exported
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001191 assert_equal(1, exists('Export.exported'))
1192 assert_equal(0, exists('Export.notexported'))
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001193 UseExport()
1194 END
1195 writefile(import_star_as_lines, 'Ximport.vim')
1196 source Ximport.vim
Bram Moolenaar24e93162021-07-18 20:40:33 +02001197
1198 assert_equal(18, g:exported_def)
1199 assert_equal(18, g:exported_script)
1200 unlet g:exported_def
1201 unlet g:exported_script
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001202
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001203 var import_star_as_lines_no_dot =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001204 vim9script
1205 import * as Export from './Xexport.vim'
1206 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001207 var dummy = 1
1208 var imported = Export + dummy
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001209 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001210 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001211 END
1212 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001213 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001214
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001215 var import_star_as_lines_dot_space =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001216 vim9script
1217 import * as Export from './Xexport.vim'
1218 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001219 var imported = Export . exported
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001220 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001221 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001222 END
1223 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001224 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001225
Bram Moolenaara6294952020-12-27 13:39:50 +01001226 var import_star_as_duplicated =<< trim END
1227 vim9script
1228 import * as Export from './Xexport.vim'
1229 var some = 'other'
1230 import * as Export from './Xexport.vim'
1231 defcompile
1232 END
1233 writefile(import_star_as_duplicated, 'Ximport.vim')
1234 assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim')
1235
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001236 var import_star_as_lines_script_no_dot =<< trim END
1237 vim9script
1238 import * as Export from './Xexport.vim'
1239 g:imported_script = Export exported
1240 END
1241 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
1242 assert_fails('source Ximport.vim', 'E1029:')
1243
1244 var import_star_as_lines_script_space_after_dot =<< trim END
1245 vim9script
1246 import * as Export from './Xexport.vim'
1247 g:imported_script = Export. exported
1248 END
1249 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
1250 assert_fails('source Ximport.vim', 'E1074:')
1251
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001252 var import_star_as_lines_missing_name =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001253 vim9script
1254 import * as Export from './Xexport.vim'
1255 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001256 var imported = Export.
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001257 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001258 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001259 END
1260 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001261 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001262
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001263 var import_star_as_lbr_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001264 vim9script
1265 import *
1266 as Export
1267 from
1268 './Xexport.vim'
1269 def UseExport()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001270 g:exported = Export.exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001271 enddef
1272 UseExport()
1273 END
1274 writefile(import_star_as_lbr_lines, 'Ximport.vim')
1275 source Ximport.vim
Bram Moolenaar24e93162021-07-18 20:40:33 +02001276 assert_equal(18, g:exported)
1277 unlet g:exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001278
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001279 var import_star_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001280 vim9script
1281 import * from './Xexport.vim'
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001282 END
1283 writefile(import_star_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001284 assert_fails('source Ximport.vim', 'E1045:', '', 2, 'Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001285
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001286 # try to import something that exists but is not exported
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001287 var import_not_exported_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001288 vim9script
1289 import name from './Xexport.vim'
1290 END
1291 writefile(import_not_exported_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001292 assert_fails('source Ximport.vim', 'E1049:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001293
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001294 # try to import something that is already defined
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001295 var import_already_defined =<< trim END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001296 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001297 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001298 import exported from './Xexport.vim'
1299 END
1300 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001301 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001302
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001303 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001304 import_already_defined =<< trim END
1305 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001306 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001307 import * as exported from './Xexport.vim'
1308 END
1309 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001310 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001311
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001312 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001313 import_already_defined =<< trim END
1314 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001315 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001316 import {exported} from './Xexport.vim'
1317 END
1318 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001319 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001320
Bram Moolenaar918a4242020-12-06 14:37:08 +01001321 # try changing an imported const
1322 var import_assign_to_const =<< trim END
1323 vim9script
1324 import CONST from './Xexport.vim'
1325 def Assign()
1326 CONST = 987
1327 enddef
1328 defcompile
1329 END
1330 writefile(import_assign_to_const, 'Ximport.vim')
1331 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1332
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001333 # try changing an imported final
1334 var import_assign_to_final =<< trim END
1335 vim9script
1336 import theList from './Xexport.vim'
1337 def Assign()
1338 theList = [2]
1339 enddef
1340 defcompile
1341 END
1342 writefile(import_assign_to_final, 'Ximport.vim')
1343 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1344
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001345 # import a very long name, requires making a copy
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001346 var import_long_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001347 vim9script
1348 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
1349 END
1350 writefile(import_long_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001351 assert_fails('source Ximport.vim', 'E1048:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001352
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001353 var import_no_from_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001354 vim9script
1355 import name './Xexport.vim'
1356 END
1357 writefile(import_no_from_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001358 assert_fails('source Ximport.vim', 'E1070:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001359
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001360 var import_invalid_string_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001361 vim9script
1362 import name from Xexport.vim
1363 END
1364 writefile(import_invalid_string_lines, 'Ximport.vim')
Bram Moolenaar4db572e2021-07-18 18:21:38 +02001365 assert_fails('source Ximport.vim', 'E121:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001366
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001367 var import_wrong_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001368 vim9script
1369 import name from './XnoExport.vim'
1370 END
1371 writefile(import_wrong_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001372 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001373
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001374 var import_missing_comma_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001375 vim9script
1376 import {exported name} from './Xexport.vim'
1377 END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001378 writefile(import_missing_comma_lines, 'Ximport3.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001379 assert_fails('source Ximport3.vim', 'E1046:', '', 2, 'Ximport3.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001380
Bram Moolenaar60579352021-07-19 21:45:07 +02001381 var import_redefining_lines =<< trim END
1382 vim9script
1383 import exported from './Xexport.vim'
1384 var exported = 5
1385 END
1386 writefile(import_redefining_lines, 'Ximport.vim')
1387 assert_fails('source Ximport.vim', 'E1213: Redefining imported item "exported"', '', 3)
1388
1389 var import_assign_wrong_type_lines =<< trim END
1390 vim9script
1391 import exported from './Xexport.vim'
1392 exported = 'xxx'
1393 END
1394 writefile(import_assign_wrong_type_lines, 'Ximport.vim')
1395 assert_fails('source Ximport.vim', 'E1012: Type mismatch; expected number but got string', '', 3)
1396
1397 var import_assign_const_lines =<< trim END
1398 vim9script
1399 import CONST from './Xexport.vim'
1400 CONST = 4321
1401 END
1402 writefile(import_assign_const_lines, 'Ximport.vim')
1403 assert_fails('source Ximport.vim', 'E741: Value is locked: CONST', '', 3)
1404
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001405 delete('Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001406 delete('Ximport3.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 delete('Xexport.vim')
1408
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001409 # Check that in a Vim9 script 'cpo' is set to the Vim default.
Bram Moolenaar3e191692021-03-17 17:46:00 +01001410 # Flags added or removed are also applied to the restored value.
1411 set cpo=abcd
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001412 var lines =<< trim END
Bram Moolenaar750802b2020-02-23 18:08:33 +01001413 vim9script
1414 g:cpo_in_vim9script = &cpo
Bram Moolenaar3e191692021-03-17 17:46:00 +01001415 set cpo+=f
1416 set cpo-=c
1417 g:cpo_after_vim9script = &cpo
Bram Moolenaar750802b2020-02-23 18:08:33 +01001418 END
1419 writefile(lines, 'Xvim9_script')
1420 source Xvim9_script
Bram Moolenaar3e191692021-03-17 17:46:00 +01001421 assert_equal('fabd', &cpo)
Bram Moolenaar750802b2020-02-23 18:08:33 +01001422 set cpo&vim
1423 assert_equal(&cpo, g:cpo_in_vim9script)
Bram Moolenaar3e191692021-03-17 17:46:00 +01001424 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
1425 assert_equal(newcpo, g:cpo_after_vim9script)
1426
Bram Moolenaar750802b2020-02-23 18:08:33 +01001427 delete('Xvim9_script')
1428enddef
1429
Bram Moolenaar0a842842021-02-27 22:41:19 +01001430def Test_import_as()
1431 var export_lines =<< trim END
1432 vim9script
1433 export var one = 1
1434 export var yes = 'yes'
Bram Moolenaarc967d572021-07-08 21:38:50 +02001435 export var slist: list<string>
Bram Moolenaar0a842842021-02-27 22:41:19 +01001436 END
1437 writefile(export_lines, 'XexportAs')
1438
1439 var import_lines =<< trim END
1440 vim9script
Bram Moolenaar6c7cc342021-04-17 16:38:50 +02001441 var one = 'notused'
1442 var yes = 777
Bram Moolenaar0a842842021-02-27 22:41:19 +01001443 import one as thatOne from './XexportAs'
1444 assert_equal(1, thatOne)
1445 import yes as yesYes from './XexportAs'
1446 assert_equal('yes', yesYes)
1447 END
1448 CheckScriptSuccess(import_lines)
1449
1450 import_lines =<< trim END
1451 vim9script
1452 import {one as thatOne, yes as yesYes} from './XexportAs'
1453 assert_equal(1, thatOne)
1454 assert_equal('yes', yesYes)
1455 assert_fails('echo one', 'E121:')
1456 assert_fails('echo yes', 'E121:')
1457 END
1458 CheckScriptSuccess(import_lines)
1459
Bram Moolenaarc967d572021-07-08 21:38:50 +02001460 import_lines =<< trim END
1461 vim9script
1462 import {slist as impSlist} from './XexportAs'
1463 impSlist->add(123)
1464 END
1465 CheckScriptFailure(import_lines, 'E1012: Type mismatch; expected string but got number')
1466
Bram Moolenaar0a842842021-02-27 22:41:19 +01001467 delete('XexportAs')
1468enddef
1469
Bram Moolenaar803af682020-08-05 16:20:03 +02001470func g:Trigger()
1471 source Ximport.vim
1472 return "echo 'yes'\<CR>"
1473endfunc
1474
1475def Test_import_export_expr_map()
1476 # check that :import and :export work when buffer is locked
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001477 var export_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001478 vim9script
1479 export def That(): string
1480 return 'yes'
1481 enddef
1482 END
1483 writefile(export_lines, 'Xexport_that.vim')
1484
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001485 var import_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001486 vim9script
1487 import That from './Xexport_that.vim'
1488 assert_equal('yes', That())
1489 END
1490 writefile(import_lines, 'Ximport.vim')
1491
1492 nnoremap <expr> trigger g:Trigger()
1493 feedkeys('trigger', "xt")
1494
Bram Moolenaar730b2482020-08-09 13:02:10 +02001495 delete('Xexport_that.vim')
Bram Moolenaar803af682020-08-05 16:20:03 +02001496 delete('Ximport.vim')
1497 nunmap trigger
1498enddef
1499
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001500def Test_import_in_filetype()
1501 # check that :import works when the buffer is locked
1502 mkdir('ftplugin', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001503 var export_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001504 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001505 export var That = 'yes'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001506 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001507 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001508
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001509 var import_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001510 vim9script
Bram Moolenaar730b2482020-08-09 13:02:10 +02001511 import That from './Xexport_ft.vim'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001512 assert_equal('yes', That)
1513 g:did_load_mytpe = 1
1514 END
1515 writefile(import_lines, 'ftplugin/qf.vim')
1516
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001517 var save_rtp = &rtp
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001518 &rtp = getcwd() .. ',' .. &rtp
1519
1520 filetype plugin on
1521 copen
1522 assert_equal(1, g:did_load_mytpe)
1523
1524 quit!
Bram Moolenaar730b2482020-08-09 13:02:10 +02001525 delete('Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001526 delete('ftplugin', 'rf')
1527 &rtp = save_rtp
1528enddef
1529
Bram Moolenaarefa94442020-08-08 22:16:00 +02001530def Test_use_import_in_mapping()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001531 var lines =<< trim END
Bram Moolenaarefa94442020-08-08 22:16:00 +02001532 vim9script
1533 export def Funcx()
1534 g:result = 42
1535 enddef
1536 END
1537 writefile(lines, 'XsomeExport.vim')
1538 lines =<< trim END
1539 vim9script
1540 import Funcx from './XsomeExport.vim'
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001541 nnoremap <F3> :call <sid>Funcx()<cr>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001542 END
1543 writefile(lines, 'Xmapscript.vim')
1544
1545 source Xmapscript.vim
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001546 feedkeys("\<F3>", "xt")
Bram Moolenaarefa94442020-08-08 22:16:00 +02001547 assert_equal(42, g:result)
1548
1549 unlet g:result
1550 delete('XsomeExport.vim')
1551 delete('Xmapscript.vim')
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001552 nunmap <F3>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001553enddef
1554
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01001555def Test_vim9script_mix()
1556 var lines =<< trim END
1557 if has(g:feature)
1558 " legacy script
1559 let g:legacy = 1
1560 finish
1561 endif
1562 vim9script
1563 g:legacy = 0
1564 END
1565 g:feature = 'eval'
1566 g:legacy = -1
1567 CheckScriptSuccess(lines)
1568 assert_equal(1, g:legacy)
1569
1570 g:feature = 'noteval'
1571 g:legacy = -1
1572 CheckScriptSuccess(lines)
1573 assert_equal(0, g:legacy)
1574enddef
1575
Bram Moolenaar750802b2020-02-23 18:08:33 +01001576def Test_vim9script_fails()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1578 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001579 CheckScriptFailure(['export var some = 123'], 'E1042:')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001580 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001581 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
Bram Moolenaar750802b2020-02-23 18:08:33 +01001582 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
1583
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001584 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001585 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1586
Bram Moolenaare2e40752020-09-04 21:18:46 +02001587 assert_fails('vim9script', 'E1038:')
1588 assert_fails('export something', 'E1043:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589enddef
1590
Bram Moolenaarf7d267e2020-06-17 12:04:54 +02001591func Test_import_fails_without_script()
Bram Moolenaar101f4812020-06-16 23:18:51 +02001592 CheckRunVimInTerminal
1593
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001594 " call indirectly to avoid compilation error for missing functions
Bram Moolenaarc620c052020-07-08 15:16:19 +02001595 call Run_Test_import_fails_on_command_line()
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001596endfunc
1597
Bram Moolenaarc620c052020-07-08 15:16:19 +02001598def Run_Test_import_fails_on_command_line()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001599 var export =<< trim END
Bram Moolenaar101f4812020-06-16 23:18:51 +02001600 vim9script
1601 export def Foo(): number
1602 return 0
1603 enddef
1604 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001605 writefile(export, 'XexportCmd.vim')
Bram Moolenaar101f4812020-06-16 23:18:51 +02001606
Bram Moolenaare0de1712020-12-02 17:36:54 +01001607 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001608 rows: 6, wait_for_ruler: 0})
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001609 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaar101f4812020-06-16 23:18:51 +02001610
Bram Moolenaar730b2482020-08-09 13:02:10 +02001611 delete('XexportCmd.vim')
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001612 StopVimInTerminal(buf)
1613enddef
Bram Moolenaar101f4812020-06-16 23:18:51 +02001614
Bram Moolenaar2b327002020-12-26 15:39:31 +01001615def Test_vim9script_reload_noclear()
1616 var lines =<< trim END
Bram Moolenaara6294952020-12-27 13:39:50 +01001617 vim9script
1618 export var exported = 'thexport'
1619 END
1620 writefile(lines, 'XExportReload')
1621 lines =<< trim END
Bram Moolenaar2b327002020-12-26 15:39:31 +01001622 vim9script noclear
1623 g:loadCount += 1
1624 var s:reloaded = 'init'
Bram Moolenaara6294952020-12-27 13:39:50 +01001625 import exported from './XExportReload'
Bram Moolenaar2b327002020-12-26 15:39:31 +01001626
1627 def Again(): string
1628 return 'again'
1629 enddef
1630
1631 if exists('s:loaded') | finish | endif
1632 var s:loaded = true
1633
1634 var s:notReloaded = 'yes'
1635 s:reloaded = 'first'
1636 def g:Values(): list<string>
Bram Moolenaara6294952020-12-27 13:39:50 +01001637 return [s:reloaded, s:notReloaded, Again(), Once(), exported]
Bram Moolenaar2b327002020-12-26 15:39:31 +01001638 enddef
1639
1640 def Once(): string
1641 return 'once'
1642 enddef
1643 END
1644 writefile(lines, 'XReloaded')
1645 g:loadCount = 0
1646 source XReloaded
1647 assert_equal(1, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001648 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001649 source XReloaded
1650 assert_equal(2, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001651 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001652 source XReloaded
1653 assert_equal(3, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001654 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001655
Bram Moolenaar48e11c12021-01-11 18:47:00 +01001656 delete('XReloaded')
Bram Moolenaara6294952020-12-27 13:39:50 +01001657 delete('XExportReload')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001658 delfunc g:Values
Bram Moolenaar2b327002020-12-26 15:39:31 +01001659 unlet g:loadCount
Bram Moolenaar577dc932021-06-27 15:35:40 +02001660
1661 lines =<< trim END
1662 vim9script
1663 def Inner()
1664 enddef
1665 END
1666 lines->writefile('XreloadScript.vim')
1667 source XreloadScript.vim
1668
1669 lines =<< trim END
1670 vim9script
1671 def Outer()
1672 def Inner()
1673 enddef
1674 enddef
1675 defcompile
1676 END
1677 lines->writefile('XreloadScript.vim')
1678 source XreloadScript.vim
1679
1680 delete('XreloadScript.vim')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001681enddef
1682
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001683def Test_vim9script_reload_import()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001684 var lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 vim9script
1686 const var = ''
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001687 var valone = 1234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688 def MyFunc(arg: string)
1689 valone = 5678
1690 enddef
1691 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001692 var morelines =<< trim END
1693 var valtwo = 222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 export def GetValtwo(): number
1695 return valtwo
1696 enddef
1697 END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001698 writefile(lines + morelines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 source Xreload.vim
1700 source Xreload.vim
1701 source Xreload.vim
1702
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001703 # cannot declare a var twice
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 lines =<< trim END
1705 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001706 var valone = 1234
1707 var valone = 5678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 END
1709 writefile(lines, 'Xreload.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001710 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711
1712 delete('Xreload.vim')
1713 delete('Ximport.vim')
1714enddef
1715
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001716" if a script is reloaded with a script-local variable that changed its type, a
1717" compiled function using that variable must fail.
1718def Test_script_reload_change_type()
1719 var lines =<< trim END
1720 vim9script noclear
1721 var str = 'string'
1722 def g:GetStr(): string
1723 return str .. 'xxx'
1724 enddef
1725 END
1726 writefile(lines, 'Xreload.vim')
1727 source Xreload.vim
1728 echo g:GetStr()
1729
1730 lines =<< trim END
1731 vim9script noclear
1732 var str = 1234
1733 END
1734 writefile(lines, 'Xreload.vim')
1735 source Xreload.vim
1736 assert_fails('echo g:GetStr()', 'E1150:')
1737
1738 delfunc g:GetStr
1739 delete('Xreload.vim')
1740enddef
1741
Bram Moolenaarc970e422021-03-17 15:03:04 +01001742" Define CallFunc so that the test can be compiled
1743command CallFunc echo 'nop'
1744
1745def Test_script_reload_from_function()
1746 var lines =<< trim END
1747 vim9script
1748
1749 if exists('g:loaded')
1750 finish
1751 endif
1752 g:loaded = 1
1753 delcommand CallFunc
1754 command CallFunc Func()
1755 def Func()
Bram Moolenaara4c81be2021-03-17 15:23:16 +01001756 so XreloadFunc.vim
Bram Moolenaarc970e422021-03-17 15:03:04 +01001757 g:didTheFunc = 1
1758 enddef
1759 END
1760 writefile(lines, 'XreloadFunc.vim')
1761 source XreloadFunc.vim
1762 CallFunc
1763 assert_equal(1, g:didTheFunc)
1764
1765 delete('XreloadFunc.vim')
1766 delcommand CallFunc
1767 unlet g:loaded
1768 unlet g:didTheFunc
1769enddef
1770
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001771def Test_script_var_shadows_function()
1772 var lines =<< trim END
1773 vim9script
1774 def Func(): number
1775 return 123
1776 enddef
1777 var Func = 1
1778 END
1779 CheckScriptFailure(lines, 'E1041:', 5)
1780enddef
1781
Bram Moolenaarc3235272021-07-10 19:42:03 +02001782def Test_script_var_shadows_command()
1783 var lines =<< trim END
1784 var undo = 1
1785 undo = 2
1786 assert_equal(2, undo)
1787 END
1788 CheckDefAndScriptSuccess(lines)
1789
1790 lines =<< trim END
1791 var undo = 1
1792 undo
1793 END
1794 CheckDefAndScriptFailure(lines, 'E1207:', 2)
1795enddef
1796
Bram Moolenaar95006e32020-08-29 17:47:08 +02001797def s:RetSome(): string
1798 return 'some'
1799enddef
1800
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001801" Not exported function that is referenced needs to be accessed by the
1802" script-local name.
1803def Test_vim9script_funcref()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001804 var sortlines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001805 vim9script
1806 def Compare(i1: number, i2: number): number
Bram Moolenaarbed36b92020-07-07 23:31:36 +02001807 return i2 - i1
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001808 enddef
1809
1810 export def FastSort(): list<number>
1811 return range(5)->sort(Compare)
1812 enddef
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001813
1814 export def GetString(arg: string): string
1815 return arg
1816 enddef
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001817 END
1818 writefile(sortlines, 'Xsort.vim')
1819
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001820 var lines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001821 vim9script
1822 import FastSort from './Xsort.vim'
1823 def Test()
1824 g:result = FastSort()
1825 enddef
1826 Test()
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001827
1828 # using a function imported with "as"
1829 import * as anAlias from './Xsort.vim'
1830 assert_equal('yes', anAlias.GetString('yes'))
1831
1832 # using the function from a compiled function
1833 def TestMore(): string
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001834 var s = s:anAlias.GetString('foo')
1835 return s .. anAlias.GetString('bar')
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001836 enddef
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001837 assert_equal('foobar', TestMore())
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001838
1839 # error when using a function that isn't exported
1840 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001841 END
1842 writefile(lines, 'Xscript.vim')
1843
1844 source Xscript.vim
1845 assert_equal([4, 3, 2, 1, 0], g:result)
1846
1847 unlet g:result
1848 delete('Xsort.vim')
1849 delete('Xscript.vim')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001850
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001851 var Funcref = function('s:RetSome')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001852 assert_equal('some', Funcref())
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001853enddef
1854
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001855" Check that when searching for "FilterFunc" it finds the import in the
1856" script where FastFilter() is called from, both as a string and as a direct
1857" function reference.
Bram Moolenaarc620c052020-07-08 15:16:19 +02001858def Test_vim9script_funcref_other_script()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001859 var filterLines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001860 vim9script
1861 export def FilterFunc(idx: number, val: number): bool
1862 return idx % 2 == 1
1863 enddef
1864 export def FastFilter(): list<number>
1865 return range(10)->filter('FilterFunc')
1866 enddef
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001867 export def FastFilterDirect(): list<number>
1868 return range(10)->filter(FilterFunc)
1869 enddef
Bram Moolenaarc620c052020-07-08 15:16:19 +02001870 END
1871 writefile(filterLines, 'Xfilter.vim')
1872
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001873 var lines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001874 vim9script
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001875 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim'
Bram Moolenaarc620c052020-07-08 15:16:19 +02001876 def Test()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001877 var x: list<number> = FastFilter()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001878 enddef
1879 Test()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001880 def TestDirect()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001881 var x: list<number> = FastFilterDirect()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001882 enddef
1883 TestDirect()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001884 END
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001885 CheckScriptSuccess(lines)
Bram Moolenaarc620c052020-07-08 15:16:19 +02001886 delete('Xfilter.vim')
Bram Moolenaarc620c052020-07-08 15:16:19 +02001887enddef
1888
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001889def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001890 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001891 vim9script
1892 def FuncYes(): string
1893 return 'yes'
1894 enddef
1895 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001896 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001897 def FuncNo(): string
1898 return 'no'
1899 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001900 def g:DoCheck(no_exists: bool)
1901 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001902 assert_equal('no', FuncNo())
1903 enddef
1904 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001905 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001906 def g:DoCheck(no_exists: bool)
1907 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02001908 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001909 enddef
1910 END
1911
1912 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001913 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001914 source Xreloaded.vim
1915 g:DoCheck(true)
1916
1917 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001918 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001919 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001920 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001921
1922 # FuncNo() is back
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001923 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001924 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01001925 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001926
1927 delete('Xreloaded.vim')
1928enddef
1929
Bram Moolenaar89483d42020-05-10 15:24:44 +02001930def Test_vim9script_reload_delvar()
1931 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001932 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02001933 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001934 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001935 END
1936 writefile(lines, 'XreloadVar.vim')
1937 source XreloadVar.vim
1938
1939 # now write the script using the same variable locally - works
1940 lines =<< trim END
1941 vim9script
1942 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001943 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02001944 enddef
1945 END
1946 writefile(lines, 'XreloadVar.vim')
1947 source XreloadVar.vim
1948
1949 delete('XreloadVar.vim')
1950enddef
1951
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952def Test_import_absolute()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001953 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02001954 'vim9script',
1955 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
1956 'def UseExported()',
1957 ' g:imported_abs = exported',
1958 ' exported = 8888',
1959 ' g:imported_after = exported',
1960 'enddef',
1961 'UseExported()',
1962 'g:import_disassembled = execute("disass UseExported")',
1963 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 writefile(import_lines, 'Ximport_abs.vim')
1965 writefile(s:export_script_lines, 'Xexport_abs.vim')
1966
1967 source Ximport_abs.vim
1968
1969 assert_equal(9876, g:imported_abs)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001970 assert_equal(8888, g:imported_after)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001971 assert_match('<SNR>\d\+_UseExported\_s*' ..
1972 'g:imported_abs = exported\_s*' ..
1973 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1974 '1 STOREG g:imported_abs\_s*' ..
1975 'exported = 8888\_s*' ..
1976 '2 PUSHNR 8888\_s*' ..
1977 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1978 'g:imported_after = exported\_s*' ..
1979 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1980 '5 STOREG g:imported_after',
Bram Moolenaare6085c52020-04-12 20:19:16 +02001981 g:import_disassembled)
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001982
1983 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 unlet g:imported_abs
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001985 unlet g:import_disassembled
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986
1987 delete('Ximport_abs.vim')
1988 delete('Xexport_abs.vim')
1989enddef
1990
1991def Test_import_rtp()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001992 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02001993 'vim9script',
1994 'import exported from "Xexport_rtp.vim"',
1995 'g:imported_rtp = exported',
1996 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 writefile(import_lines, 'Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02001998 mkdir('import', 'p')
1999 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002001 var save_rtp = &rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 &rtp = getcwd()
2003 source Ximport_rtp.vim
2004 &rtp = save_rtp
2005
2006 assert_equal(9876, g:imported_rtp)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002008 Undo_export_script_lines()
2009 unlet g:imported_rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 delete('Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002011 delete('import', 'rf')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012enddef
2013
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002014def Test_import_compile_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002015 var export_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002016 'vim9script',
2017 'export def ExpFunc(): string',
2018 ' return notDefined',
2019 'enddef',
2020 ]
2021 writefile(export_lines, 'Xexported.vim')
2022
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002023 var import_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002024 'vim9script',
2025 'import ExpFunc from "./Xexported.vim"',
2026 'def ImpFunc()',
2027 ' echo ExpFunc()',
2028 'enddef',
2029 'defcompile',
2030 ]
2031 writefile(import_lines, 'Ximport.vim')
2032
2033 try
2034 source Ximport.vim
2035 catch /E1001/
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002036 # Error should be fore the Xexported.vim file.
Bram Moolenaar77072282020-09-16 17:55:40 +02002037 assert_match('E1001: Variable not found: notDefined', v:exception)
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002038 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
2039 endtry
2040
2041 delete('Xexported.vim')
2042 delete('Ximport.vim')
2043enddef
2044
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002045def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002046 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002047 'vim9script',
2048 'def Func()',
2049 ' eval [][0]',
2050 'enddef',
2051 'Func()',
2052 ]
2053 writefile(lines, 'Xtestscript.vim')
2054
2055 for count in range(3)
2056 try
2057 source Xtestscript.vim
2058 catch /E684/
2059 # function name should contain <SNR> every time
2060 assert_match('E684: list index out of range', v:exception)
2061 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
2062 endtry
2063 endfor
2064
2065 delete('Xtestscript.vim')
2066enddef
2067
Bram Moolenaareef21022020-08-01 22:16:43 +02002068def Test_func_overrules_import_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002069 var export_lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002070 vim9script
2071 export def Func()
2072 echo 'imported'
2073 enddef
2074 END
2075 writefile(export_lines, 'XexportedFunc.vim')
2076
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002077 var lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002078 vim9script
2079 import Func from './XexportedFunc.vim'
2080 def Func()
2081 echo 'local to function'
2082 enddef
2083 END
2084 CheckScriptFailure(lines, 'E1073:')
2085
2086 lines =<< trim END
2087 vim9script
2088 import Func from './XexportedFunc.vim'
2089 def Outer()
2090 def Func()
2091 echo 'local to function'
2092 enddef
2093 enddef
2094 defcompile
2095 END
2096 CheckScriptFailure(lines, 'E1073:')
2097
2098 delete('XexportedFunc.vim')
2099enddef
2100
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002101def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002102 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002103 vim9script
2104 def Func()
2105 echo 'one'
2106 enddef
2107 def Func()
2108 echo 'two'
2109 enddef
2110 END
2111 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002112
2113 lines =<< trim END
2114 vim9script
2115 def Foo(): string
2116 return 'foo'
2117 enddef
2118 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002119 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002120 enddef
2121 defcompile
2122 END
2123 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002124enddef
2125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002127 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002128 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129 l->remove(0)
2130 l->add(5)
2131 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002132 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133enddef
2134
Bram Moolenaarae616492020-07-28 20:07:27 +02002135def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002136 CheckDefExecFailure(['a = 1'], 'E1100:')
2137 CheckDefExecFailure(['c = 1'], 'E1100:')
2138 CheckDefExecFailure(['i = 1'], 'E1100:')
2139 CheckDefExecFailure(['t = 1'], 'E1100:')
2140 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002141
Bram Moolenaarae616492020-07-28 20:07:27 +02002142 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
2143 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002144 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
2145 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002146 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
2147 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01002148 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
2149 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002150 CheckScriptFailure(['vim9script', 't'], 'E1100:')
2151 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
2152 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002153enddef
2154
Bram Moolenaar158906c2020-02-06 20:39:45 +01002155def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002156 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01002157 if what == 1
2158 res = "one"
2159 elseif what == 2
2160 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002161 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01002162 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002163 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01002164 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002165enddef
2166
Bram Moolenaar158906c2020-02-06 20:39:45 +01002167def Test_if_elseif_else()
2168 assert_equal('one', IfElse(1))
2169 assert_equal('two', IfElse(2))
2170 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002171enddef
2172
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002173def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002174 CheckDefFailure(['elseif true'], 'E582:')
2175 CheckDefFailure(['else'], 'E581:')
2176 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01002177 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002178 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002179
2180 var lines =<< trim END
2181 var s = ''
2182 if s = ''
2183 endif
2184 END
2185 CheckDefFailure(lines, 'E488:')
2186
2187 lines =<< trim END
2188 var s = ''
2189 if s == ''
2190 elseif s = ''
2191 endif
2192 END
2193 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002194enddef
2195
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002196let g:bool_true = v:true
2197let g:bool_false = v:false
2198
2199def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002200 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002201 if true ? true : false
2202 res = true
2203 endif
2204 assert_equal(true, res)
2205
Bram Moolenaar585fea72020-04-02 22:33:21 +02002206 g:glob = 2
2207 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02002208 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002209 endif
2210 assert_equal(2, g:glob)
2211 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02002212 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002213 endif
2214 assert_equal(3, g:glob)
2215
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002216 res = false
2217 if g:bool_true ? true : false
2218 res = true
2219 endif
2220 assert_equal(true, res)
2221
2222 res = false
2223 if true ? g:bool_true : false
2224 res = true
2225 endif
2226 assert_equal(true, res)
2227
2228 res = false
2229 if true ? true : g:bool_false
2230 res = true
2231 endif
2232 assert_equal(true, res)
2233
2234 res = false
2235 if true ? false : true
2236 res = true
2237 endif
2238 assert_equal(false, res)
2239
2240 res = false
2241 if false ? false : true
2242 res = true
2243 endif
2244 assert_equal(true, res)
2245
2246 res = false
2247 if false ? true : false
2248 res = true
2249 endif
2250 assert_equal(false, res)
2251
2252 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002253 if has('xyz') ? true : false
2254 res = true
2255 endif
2256 assert_equal(false, res)
2257
2258 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002259 if true && true
2260 res = true
2261 endif
2262 assert_equal(true, res)
2263
2264 res = false
2265 if true && false
2266 res = true
2267 endif
2268 assert_equal(false, res)
2269
2270 res = false
2271 if g:bool_true && false
2272 res = true
2273 endif
2274 assert_equal(false, res)
2275
2276 res = false
2277 if true && g:bool_false
2278 res = true
2279 endif
2280 assert_equal(false, res)
2281
2282 res = false
2283 if false && false
2284 res = true
2285 endif
2286 assert_equal(false, res)
2287
2288 res = false
2289 if true || false
2290 res = true
2291 endif
2292 assert_equal(true, res)
2293
2294 res = false
2295 if g:bool_true || false
2296 res = true
2297 endif
2298 assert_equal(true, res)
2299
2300 res = false
2301 if true || g:bool_false
2302 res = true
2303 endif
2304 assert_equal(true, res)
2305
2306 res = false
2307 if false || false
2308 res = true
2309 endif
2310 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02002311
2312 # with constant "false" expression may be invalid so long as the syntax is OK
Bram Moolenaarc3235272021-07-10 19:42:03 +02002313 if false | eval 1 + 2 | endif
Bram Moolenaar3988f642020-08-27 22:43:03 +02002314 if false | eval burp + 234 | endif
2315 if false | echo burp 234 'asd' | endif
2316 if false
2317 burp
2318 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002319enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002320
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002321def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002322 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
2323 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
2324 CheckDefFailure(["if has('aaa'"], 'E110:')
2325 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002326enddef
2327
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002328def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002329 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002330 if i % 2
2331 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002332 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002333 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002334 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002335 endif
2336 x += 1
2337 else
2338 x += 1000
2339 endif
2340 return x
2341enddef
2342
2343def Test_nested_if()
2344 assert_equal(1, RunNested(1))
2345 assert_equal(1000, RunNested(2))
2346enddef
2347
Bram Moolenaarad39c092020-02-26 18:23:43 +01002348def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01002349 # missing argument is ignored
2350 execute
2351 execute # comment
2352
Bram Moolenaarad39c092020-02-26 18:23:43 +01002353 new
2354 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002355 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002356 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002357
Bram Moolenaard2c61702020-09-06 15:58:36 +02002358 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002359 assert_equal('execute-string', getline(1))
2360
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002361 var cmd1 = 'setline(1,'
2362 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002363 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01002364 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002365
Bram Moolenaard2c61702020-09-06 15:58:36 +02002366 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002367 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002368
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002369 var cmd_first = 'call '
2370 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002371 execute cmd_first .. cmd_last
2372 assert_equal('execute-var-var', getline(1))
2373 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02002374
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002375 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002376 execute 'echomsg' (n ? '"true"' : '"no"')
2377 assert_match('^true$', Screenline(&lines))
2378
Bram Moolenaare0de1712020-12-02 17:36:54 +01002379 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002380 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
2381
Bram Moolenaard2c61702020-09-06 15:58:36 +02002382 CheckDefFailure(['execute xxx'], 'E1001:', 1)
2383 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
2384 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002385enddef
2386
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002387def Test_execute_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002388 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002389 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002390 vim9script
2391 execute 'g:someVar'
2392 .. ' = ' ..
2393 '28'
2394 assert_equal(28, g:someVar)
2395 unlet g:someVar
2396 END
2397 CheckScriptSuccess(lines)
2398enddef
2399
Bram Moolenaarad39c092020-02-26 18:23:43 +01002400def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002401 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02002402 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002403 assert_match('^something$', Screenline(&lines))
2404
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002405 echo "some" # comment
2406 echon "thing"
2407 assert_match('^something$', Screenline(&lines))
2408
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002409 var str1 = 'some'
2410 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002411 echo str1 str2
2412 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002413
Bram Moolenaard2c61702020-09-06 15:58:36 +02002414 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01002415enddef
2416
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002417def Test_echomsg_cmd()
2418 echomsg 'some' 'more' # comment
2419 assert_match('^some more$', Screenline(&lines))
2420 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002421 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002422 assert_match('^some more$', Screenline(&lines))
2423
Bram Moolenaard2c61702020-09-06 15:58:36 +02002424 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002425enddef
2426
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002427def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002428 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002429 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002430 vim9script
2431 echomsg 'here'
2432 .. ' is ' ..
2433 'a message'
2434 assert_match('^here is a message$', Screenline(&lines))
2435 END
2436 CheckScriptSuccess(lines)
2437enddef
2438
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002439def Test_echoerr_cmd()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002440 try
2441 echoerr 'something' 'wrong' # comment
2442 catch
2443 assert_match('something wrong', v:exception)
2444 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002445enddef
2446
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002447def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002448 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002449 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002450 vim9script
2451 try
2452 echoerr 'this'
2453 .. ' is ' ..
2454 'wrong'
2455 catch
2456 assert_match('this is wrong', v:exception)
2457 endtry
2458 END
2459 CheckScriptSuccess(lines)
2460enddef
2461
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002462def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002463 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002464 vim9script
2465 new
2466 for var in range(0, 3)
2467 append(line('$'), var)
2468 endfor
2469 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2470 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01002471
2472 var result = ''
2473 for i in [1, 2, 3]
2474 var loop = ' loop ' .. i
2475 result ..= loop
2476 endfor
2477 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002478 END
2479 writefile(lines, 'Xvim9for.vim')
2480 source Xvim9for.vim
2481 delete('Xvim9for.vim')
2482enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002484def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002485 var lines =<< trim END
2486 var result = ''
2487 for cnt in range(7)
2488 if cnt == 4
2489 break
2490 endif
2491 if cnt == 2
2492 continue
2493 endif
2494 result ..= cnt .. '_'
2495 endfor
2496 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02002497
Bram Moolenaarf2253962021-04-13 20:53:13 +02002498 var concat = ''
2499 for str in eval('["one", "two"]')
2500 concat ..= str
2501 endfor
2502 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002503
Bram Moolenaarf2253962021-04-13 20:53:13 +02002504 var total = 0
2505 for nr in
2506 [1, 2, 3]
2507 total += nr
2508 endfor
2509 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002510
Bram Moolenaarf2253962021-04-13 20:53:13 +02002511 total = 0
2512 for nr
2513 in [1, 2, 3]
2514 total += nr
2515 endfor
2516 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002517
Bram Moolenaarf2253962021-04-13 20:53:13 +02002518 total = 0
2519 for nr
2520 in
2521 [1, 2, 3]
2522 total += nr
2523 endfor
2524 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01002525
Bram Moolenaara3589a02021-04-14 13:30:46 +02002526 # with type
2527 total = 0
2528 for n: number in [1, 2, 3]
2529 total += n
2530 endfor
2531 assert_equal(6, total)
2532
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002533 var chars = ''
2534 for s: string in 'foobar'
2535 chars ..= s
2536 endfor
2537 assert_equal('foobar', chars)
2538
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02002539 chars = ''
2540 for x: string in {a: 'a', b: 'b'}->values()
2541 chars ..= x
2542 endfor
2543 assert_equal('ab', chars)
2544
Bram Moolenaara3589a02021-04-14 13:30:46 +02002545 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02002546 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02002547 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2548 res ..= n .. s
2549 endfor
2550 assert_equal('1a2b', res)
2551
Bram Moolenaar444d8782021-06-26 12:40:56 +02002552 # unpack with one var
2553 var reslist = []
2554 for [x] in [['aaa'], ['bbb']]
2555 reslist->add(x)
2556 endfor
2557 assert_equal(['aaa', 'bbb'], reslist)
2558
Bram Moolenaara3589a02021-04-14 13:30:46 +02002559 # loop over string
2560 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02002561 for c in 'aéc̀d'
2562 res ..= c .. '-'
2563 endfor
2564 assert_equal('a-é-c̀-d-', res)
2565
2566 res = ''
2567 for c in ''
2568 res ..= c .. '-'
2569 endfor
2570 assert_equal('', res)
2571
2572 res = ''
2573 for c in test_null_string()
2574 res ..= c .. '-'
2575 endfor
2576 assert_equal('', res)
2577
2578 var foo: list<dict<any>> = [
2579 {a: 'Cat'}
2580 ]
2581 for dd in foo
2582 dd.counter = 12
2583 endfor
2584 assert_equal([{a: 'Cat', counter: 12}], foo)
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002585
2586 reslist = []
2587 for _ in range(3)
2588 reslist->add('x')
2589 endfor
2590 assert_equal(['x', 'x', 'x'], reslist)
Bram Moolenaarf2253962021-04-13 20:53:13 +02002591 END
2592 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002593enddef
2594
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02002595def Test_for_loop_with_closure()
2596 var lines =<< trim END
2597 var flist: list<func>
2598 for i in range(5)
2599 var inloop = i
2600 flist[i] = () => inloop
2601 endfor
2602 for i in range(5)
2603 assert_equal(4, flist[i]())
2604 endfor
2605 END
2606 CheckDefAndScriptSuccess(lines)
2607
2608 lines =<< trim END
2609 var flist: list<func>
2610 for i in range(5)
2611 var inloop = i
2612 flist[i] = () => {
2613 return inloop
2614 }
2615 endfor
2616 for i in range(5)
2617 assert_equal(4, flist[i]())
2618 endfor
2619 END
2620 CheckDefAndScriptSuccess(lines)
2621enddef
2622
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002623def Test_for_loop_fails()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002624 CheckDefAndScriptFailure2(['for '], 'E1097:', 'E690:')
2625 CheckDefAndScriptFailure2(['for x'], 'E1097:', 'E690:')
2626 CheckDefAndScriptFailure2(['for x in'], 'E1097:', 'E15:')
2627 CheckDefAndScriptFailure(['for # in range(5)'], 'E690:')
2628 CheckDefAndScriptFailure(['for i In range(5)'], 'E690:')
2629 CheckDefAndScriptFailure2(['var x = 5', 'for x in range(5)', 'endfor'], 'E1017:', 'E1041:')
Bram Moolenaard4ab8072021-07-08 19:22:12 +02002630 CheckScriptFailure(['vim9script', 'var x = 5', 'for x in range(5)', '# comment', 'endfor'], 'E1041:', 3)
Bram Moolenaar822ba242020-05-24 23:00:18 +02002631 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002632 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02002633 CheckDefFailure(['for i in xxx'], 'E1001:')
2634 CheckDefFailure(['endfor'], 'E588:')
2635 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002636
2637 # wrong type detected at compile time
2638 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2639
2640 # wrong type detected at runtime
2641 g:adict = {a: 1}
2642 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2643 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002644
2645 var lines =<< trim END
2646 var d: list<dict<any>> = [{a: 0}]
2647 for e in d
2648 e = {a: 0, b: ''}
2649 endfor
2650 END
2651 CheckDefAndScriptFailure2(lines, 'E1018:', 'E46:', 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002652
2653 lines =<< trim END
2654 for nr: number in ['foo']
2655 endfor
2656 END
2657 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaar404557e2021-07-05 21:41:48 +02002658
2659 lines =<< trim END
2660 for n : number in [1, 2]
2661 echo n
2662 endfor
2663 END
2664 CheckDefAndScriptFailure(lines, 'E1059:', 1)
Bram Moolenaarefc5db52021-07-07 21:21:30 +02002665
2666 lines =<< trim END
2667 var d: dict<number> = {a: 1, b: 2}
2668 for [k: job, v: job] in d->items()
2669 echo k v
2670 endfor
2671 END
2672 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected job but got string', 2)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002673enddef
2674
Bram Moolenaarea870692020-12-02 14:24:30 +01002675def Test_for_loop_script_var()
2676 # cannot use s:var in a :def function
2677 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1101:')
2678
2679 # can use s:var in Vim9 script, with or without s:
2680 var lines =<< trim END
2681 vim9script
2682 var total = 0
2683 for s:var in [1, 2, 3]
2684 total += s:var
2685 endfor
2686 assert_equal(6, total)
2687
2688 total = 0
2689 for var in [1, 2, 3]
2690 total += var
2691 endfor
2692 assert_equal(6, total)
2693 END
2694enddef
2695
Bram Moolenaar792f7862020-11-23 08:31:18 +01002696def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01002697 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002698 var result = []
2699 for [v1, v2] in [[1, 2], [3, 4]]
2700 result->add(v1)
2701 result->add(v2)
2702 endfor
2703 assert_equal([1, 2, 3, 4], result)
2704
2705 result = []
2706 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2707 result->add(v1)
2708 result->add(v2)
2709 result->add(v3)
2710 endfor
2711 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
2712
2713 result = []
2714 for [&ts, &sw] in [[1, 2], [3, 4]]
2715 result->add(&ts)
2716 result->add(&sw)
2717 endfor
2718 assert_equal([1, 2, 3, 4], result)
2719
2720 var slist: list<string>
2721 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
2722 slist->add($LOOPVAR)
2723 slist->add(@r)
2724 slist->add(v:errmsg)
2725 endfor
2726 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
2727
2728 slist = []
2729 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
2730 slist->add(g:globalvar)
2731 slist->add(b:bufvar)
2732 slist->add(w:winvar)
2733 slist->add(t:tabvar)
2734 endfor
2735 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01002736 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaarb777da92021-05-22 21:40:39 +02002737
2738 var res = []
2739 for [_, n, _] in [[1, 2, 3], [4, 5, 6]]
2740 res->add(n)
2741 endfor
2742 assert_equal([2, 5], res)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002743 END
2744 CheckDefAndScriptSuccess(lines)
2745
2746 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01002747 for [v1, v2] in [[1, 2, 3], [3, 4]]
2748 echo v1 v2
2749 endfor
2750 END
2751 CheckDefExecFailure(lines, 'E710:', 1)
2752
2753 lines =<< trim END
2754 for [v1, v2] in [[1], [3, 4]]
2755 echo v1 v2
2756 endfor
2757 END
2758 CheckDefExecFailure(lines, 'E711:', 1)
2759
2760 lines =<< trim END
2761 for [v1, v1] in [[1, 2], [3, 4]]
2762 echo v1
2763 endfor
2764 END
2765 CheckDefExecFailure(lines, 'E1017:', 1)
2766enddef
2767
Bram Moolenaarc150c092021-02-13 15:02:46 +01002768def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002769 var lines =<< trim END
2770 var looped = 0
2771 var cleanup = 0
2772 for i in range(3)
2773 looped += 1
2774 try
2775 eval [][0]
2776 catch
2777 continue
2778 finally
2779 cleanup += 1
2780 endtry
2781 endfor
2782 assert_equal(3, looped)
2783 assert_equal(3, cleanup)
2784 END
2785 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002786enddef
2787
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002788def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002789 var result = ''
2790 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002791 while cnt < 555
2792 if cnt == 3
2793 break
2794 endif
2795 cnt += 1
2796 if cnt == 2
2797 continue
2798 endif
2799 result ..= cnt .. '_'
2800 endwhile
2801 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002802
2803 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002804 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01002805 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01002806enddef
2807
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002808def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02002809 CheckDefFailure(['while xxx'], 'E1001:')
2810 CheckDefFailure(['endwhile'], 'E588:')
2811 CheckDefFailure(['continue'], 'E586:')
2812 CheckDefFailure(['if true', 'continue'], 'E586:')
2813 CheckDefFailure(['break'], 'E587:')
2814 CheckDefFailure(['if true', 'break'], 'E587:')
2815 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002816
2817 var lines =<< trim END
2818 var s = ''
2819 while s = ''
2820 endwhile
2821 END
2822 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002823enddef
2824
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002825def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002826 var caught = false
2827 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002828 try
2829 while 1
2830 x += 1
2831 if x == 100
2832 feedkeys("\<C-C>", 'Lt')
2833 endif
2834 endwhile
2835 catch
2836 caught = true
2837 assert_equal(100, x)
2838 endtry
2839 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02002840 # consume the CTRL-C
2841 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002842enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01002843
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002844def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002845 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002846 'one',
2847 'two',
2848 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002849 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002850 assert_equal(['one', 'two', 'three'], mylist)
2851
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002852 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01002853 ['one']: 1,
2854 ['two']: 2,
2855 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002856 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002857 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01002858 assert_equal({one: 1, two: 2, three: 3}, mydict)
2859 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002860 one: 1, # comment
2861 two: # comment
2862 2, # comment
2863 three: 3 # comment
2864 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002865 assert_equal({one: 1, two: 2, three: 3}, mydict)
2866 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002867 one: 1,
2868 two:
2869 2,
2870 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002871 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01002872 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002873
2874 assert_equal(
2875 ['one', 'two', 'three'],
2876 split('one two three')
2877 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002878enddef
2879
Bram Moolenaar7a092242020-04-16 22:10:49 +02002880def Test_vim9_comment()
2881 CheckScriptSuccess([
2882 'vim9script',
2883 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002884 '#something',
2885 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002886 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01002887
2888 split Xfile
2889 CheckScriptSuccess([
2890 'vim9script',
2891 'edit #something',
2892 ])
2893 CheckScriptSuccess([
2894 'vim9script',
2895 'edit #{something',
2896 ])
2897 close
2898
Bram Moolenaar7a092242020-04-16 22:10:49 +02002899 CheckScriptFailure([
2900 'vim9script',
2901 ':# something',
2902 ], 'E488:')
2903 CheckScriptFailure([
2904 '# something',
2905 ], 'E488:')
2906 CheckScriptFailure([
2907 ':# something',
2908 ], 'E488:')
2909
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002910 { # block start
2911 } # block end
2912 CheckDefFailure([
2913 '{# comment',
2914 ], 'E488:')
2915 CheckDefFailure([
2916 '{',
2917 '}# comment',
2918 ], 'E488:')
2919
2920 echo "yes" # comment
2921 CheckDefFailure([
2922 'echo "yes"# comment',
2923 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002924 CheckScriptSuccess([
2925 'vim9script',
2926 'echo "yes" # something',
2927 ])
2928 CheckScriptFailure([
2929 'vim9script',
2930 'echo "yes"# something',
2931 ], 'E121:')
2932 CheckScriptFailure([
2933 'vim9script',
2934 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002935 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002936 CheckScriptFailure([
2937 'echo "yes" # something',
2938 ], 'E121:')
2939
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002940 exe "echo" # comment
2941 CheckDefFailure([
2942 'exe "echo"# comment',
2943 ], 'E488:')
2944 CheckScriptSuccess([
2945 'vim9script',
2946 'exe "echo" # something',
2947 ])
2948 CheckScriptFailure([
2949 'vim9script',
2950 'exe "echo"# something',
2951 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002952 CheckScriptFailure([
2953 'vim9script',
2954 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002955 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02002956 CheckScriptFailure([
2957 'exe "echo" # something',
2958 ], 'E121:')
2959
Bram Moolenaar7a092242020-04-16 22:10:49 +02002960 CheckDefFailure([
2961 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002962 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002963 'catch',
2964 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002965 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002966 CheckScriptFailure([
2967 'vim9script',
2968 'try# comment',
2969 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002970 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02002971 CheckDefFailure([
2972 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002973 ' throw#comment',
2974 'catch',
2975 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002976 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002977 CheckDefFailure([
2978 'try',
2979 ' throw "yes"#comment',
2980 'catch',
2981 'endtry',
2982 ], 'E488:')
2983 CheckDefFailure([
2984 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002985 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02002986 'catch# comment',
2987 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002988 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002989 CheckScriptFailure([
2990 'vim9script',
2991 'try',
2992 ' echo "yes"',
2993 'catch# comment',
2994 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01002995 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02002996 CheckDefFailure([
2997 'try',
2998 ' echo "yes"',
2999 'catch /pat/# comment',
3000 'endtry',
3001 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003002 CheckDefFailure([
3003 'try',
3004 'echo "yes"',
3005 'catch',
3006 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003007 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003008 CheckScriptFailure([
3009 'vim9script',
3010 'try',
3011 ' echo "yes"',
3012 'catch',
3013 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003014 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003015
3016 CheckScriptSuccess([
3017 'vim9script',
3018 'hi # comment',
3019 ])
3020 CheckScriptFailure([
3021 'vim9script',
3022 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003023 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003024 CheckScriptSuccess([
3025 'vim9script',
3026 'hi Search # comment',
3027 ])
3028 CheckScriptFailure([
3029 'vim9script',
3030 'hi Search# comment',
3031 ], 'E416:')
3032 CheckScriptSuccess([
3033 'vim9script',
3034 'hi link This Search # comment',
3035 ])
3036 CheckScriptFailure([
3037 'vim9script',
3038 'hi link This That# comment',
3039 ], 'E413:')
3040 CheckScriptSuccess([
3041 'vim9script',
3042 'hi clear This # comment',
3043 'hi clear # comment',
3044 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003045 # not tested, because it doesn't give an error but a warning:
3046 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02003047 CheckScriptFailure([
3048 'vim9script',
3049 'hi clear# comment',
3050 ], 'E416:')
3051
3052 CheckScriptSuccess([
3053 'vim9script',
3054 'hi Group term=bold',
3055 'match Group /todo/ # comment',
3056 ])
3057 CheckScriptFailure([
3058 'vim9script',
3059 'hi Group term=bold',
3060 'match Group /todo/# comment',
3061 ], 'E488:')
3062 CheckScriptSuccess([
3063 'vim9script',
3064 'match # comment',
3065 ])
3066 CheckScriptFailure([
3067 'vim9script',
3068 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003069 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003070 CheckScriptSuccess([
3071 'vim9script',
3072 'match none # comment',
3073 ])
3074 CheckScriptFailure([
3075 'vim9script',
3076 'match none# comment',
3077 ], 'E475:')
3078
3079 CheckScriptSuccess([
3080 'vim9script',
3081 'menutrans clear # comment',
3082 ])
3083 CheckScriptFailure([
3084 'vim9script',
3085 'menutrans clear# comment text',
3086 ], 'E474:')
3087
3088 CheckScriptSuccess([
3089 'vim9script',
3090 'syntax clear # comment',
3091 ])
3092 CheckScriptFailure([
3093 'vim9script',
3094 'syntax clear# comment text',
3095 ], 'E28:')
3096 CheckScriptSuccess([
3097 'vim9script',
3098 'syntax keyword Word some',
3099 'syntax clear Word # comment',
3100 ])
3101 CheckScriptFailure([
3102 'vim9script',
3103 'syntax keyword Word some',
3104 'syntax clear Word# comment text',
3105 ], 'E28:')
3106
3107 CheckScriptSuccess([
3108 'vim9script',
3109 'syntax list # comment',
3110 ])
3111 CheckScriptFailure([
3112 'vim9script',
3113 'syntax list# comment text',
3114 ], 'E28:')
3115
3116 CheckScriptSuccess([
3117 'vim9script',
3118 'syntax match Word /pat/ oneline # comment',
3119 ])
3120 CheckScriptFailure([
3121 'vim9script',
3122 'syntax match Word /pat/ oneline# comment',
3123 ], 'E475:')
3124
3125 CheckScriptSuccess([
3126 'vim9script',
3127 'syntax keyword Word word # comm[ent',
3128 ])
3129 CheckScriptFailure([
3130 'vim9script',
3131 'syntax keyword Word word# comm[ent',
3132 ], 'E789:')
3133
3134 CheckScriptSuccess([
3135 'vim9script',
3136 'syntax match Word /pat/ # comment',
3137 ])
3138 CheckScriptFailure([
3139 'vim9script',
3140 'syntax match Word /pat/# comment',
3141 ], 'E402:')
3142
3143 CheckScriptSuccess([
3144 'vim9script',
3145 'syntax match Word /pat/ contains=Something # comment',
3146 ])
3147 CheckScriptFailure([
3148 'vim9script',
3149 'syntax match Word /pat/ contains=Something# comment',
3150 ], 'E475:')
3151 CheckScriptFailure([
3152 'vim9script',
3153 'syntax match Word /pat/ contains= # comment',
3154 ], 'E406:')
3155 CheckScriptFailure([
3156 'vim9script',
3157 'syntax match Word /pat/ contains=# comment',
3158 ], 'E475:')
3159
3160 CheckScriptSuccess([
3161 'vim9script',
3162 'syntax region Word start=/pat/ end=/pat/ # comment',
3163 ])
3164 CheckScriptFailure([
3165 'vim9script',
3166 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02003167 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003168
3169 CheckScriptSuccess([
3170 'vim9script',
3171 'syntax sync # comment',
3172 ])
3173 CheckScriptFailure([
3174 'vim9script',
3175 'syntax sync# comment',
3176 ], 'E404:')
3177 CheckScriptSuccess([
3178 'vim9script',
3179 'syntax sync ccomment # comment',
3180 ])
3181 CheckScriptFailure([
3182 'vim9script',
3183 'syntax sync ccomment# comment',
3184 ], 'E404:')
3185
3186 CheckScriptSuccess([
3187 'vim9script',
3188 'syntax cluster Some contains=Word # comment',
3189 ])
3190 CheckScriptFailure([
3191 'vim9script',
3192 'syntax cluster Some contains=Word# comment',
3193 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003194
3195 CheckScriptSuccess([
3196 'vim9script',
3197 'command Echo echo # comment',
3198 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003199 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003200 ])
3201 CheckScriptFailure([
3202 'vim9script',
3203 'command Echo echo# comment',
3204 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003205 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003206 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003207
3208 var curdir = getcwd()
3209 CheckScriptSuccess([
3210 'command Echo cd " comment',
3211 'Echo',
3212 'delcommand Echo',
3213 ])
3214 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01003215 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003216 'command Echo cd # comment',
3217 'Echo',
3218 'delcommand Echo',
3219 ])
3220 CheckScriptFailure([
3221 'vim9script',
3222 'command Echo cd " comment',
3223 'Echo',
3224 ], 'E344:')
3225 delcommand Echo
3226 chdir(curdir)
3227
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003228 CheckScriptFailure([
3229 'vim9script',
3230 'command Echo# comment',
3231 ], 'E182:')
3232 CheckScriptFailure([
3233 'vim9script',
3234 'command Echo echo',
3235 'command Echo# comment',
3236 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003237 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003238
3239 CheckScriptSuccess([
3240 'vim9script',
3241 'function # comment',
3242 ])
3243 CheckScriptFailure([
3244 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02003245 'function " comment',
3246 ], 'E129:')
3247 CheckScriptFailure([
3248 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003249 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003250 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003251 CheckScriptSuccess([
3252 'vim9script',
3253 'function CheckScriptSuccess # comment',
3254 ])
3255 CheckScriptFailure([
3256 'vim9script',
3257 'function CheckScriptSuccess# comment',
3258 ], 'E488:')
3259
3260 CheckScriptSuccess([
3261 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003262 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003263 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003264 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003265 ])
3266 CheckScriptFailure([
3267 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003268 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003269 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003270 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003271 ], 'E488:')
3272
3273 CheckScriptSuccess([
3274 'vim9script',
3275 'call execute("ls") # comment',
3276 ])
3277 CheckScriptFailure([
3278 'vim9script',
3279 'call execute("ls")# comment',
3280 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02003281
3282 CheckScriptFailure([
3283 'def Test() " comment',
3284 'enddef',
3285 ], 'E488:')
3286 CheckScriptFailure([
3287 'vim9script',
3288 'def Test() " comment',
3289 'enddef',
3290 ], 'E488:')
3291
3292 CheckScriptSuccess([
3293 'func Test() " comment',
3294 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003295 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02003296 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02003297 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02003298 'vim9script',
3299 'func Test() " comment',
3300 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02003301 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02003302
3303 CheckScriptSuccess([
3304 'def Test() # comment',
3305 'enddef',
3306 ])
3307 CheckScriptFailure([
3308 'func Test() # comment',
3309 'endfunc',
3310 ], 'E488:')
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003311
3312 var lines =<< trim END
3313 vim9script
3314 syn region Text
3315 \ start='foo'
3316 #\ comment
3317 \ end='bar'
Bram Moolenaar5072b472021-06-03 21:56:10 +02003318 syn region Text start='foo'
3319 #\ comment
3320 \ end='bar'
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003321 END
3322 CheckScriptSuccess(lines)
3323
3324 lines =<< trim END
3325 vim9script
3326 syn region Text
3327 \ start='foo'
3328 "\ comment
3329 \ end='bar'
3330 END
3331 CheckScriptFailure(lines, 'E399:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003332enddef
3333
3334def Test_vim9_comment_gui()
3335 CheckCanRunGui
3336
3337 CheckScriptFailure([
3338 'vim9script',
3339 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003340 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003341 CheckScriptFailure([
3342 'vim9script',
3343 'gui -f#comment'
3344 ], 'E499:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003345enddef
3346
Bram Moolenaara26b9702020-04-18 19:53:28 +02003347def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02003348 au TabEnter *.vim g:entered = 1
3349 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02003350
3351 edit test.vim
3352 doautocmd TabEnter #comment
3353 assert_equal(1, g:entered)
3354
3355 doautocmd TabEnter f.x
3356 assert_equal(2, g:entered)
3357
3358 g:entered = 0
3359 doautocmd TabEnter f.x #comment
3360 assert_equal(2, g:entered)
3361
3362 assert_fails('doautocmd Syntax#comment', 'E216:')
3363
3364 au! TabEnter
3365 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003366
3367 CheckScriptSuccess([
3368 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02003369 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003370 'b:var = 456',
3371 'w:var = 777',
3372 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003373 'unlet g:var w:var # something',
3374 ])
3375
3376 CheckScriptFailure([
3377 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003378 'let var = 123',
3379 ], 'E1126: Cannot use :let in Vim9 script')
3380
3381 CheckScriptFailure([
3382 'vim9script',
3383 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003384 ], 'E1016: Cannot declare a global variable:')
3385
3386 CheckScriptFailure([
3387 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003388 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003389 ], 'E1016: Cannot declare a buffer variable:')
3390
3391 CheckScriptFailure([
3392 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003393 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003394 ], 'E1016: Cannot declare a window variable:')
3395
3396 CheckScriptFailure([
3397 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003398 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003399 ], 'E1016: Cannot declare a tab variable:')
3400
3401 CheckScriptFailure([
3402 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003403 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003404 ], 'E1016: Cannot declare a v: variable:')
3405
3406 CheckScriptFailure([
3407 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003408 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003409 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02003410
3411 CheckScriptFailure([
3412 'vim9script',
3413 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003414 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003415 ], 'E108:')
3416
3417 CheckScriptFailure([
3418 'let g:var = 123',
3419 'unlet g:var # something',
3420 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003421
3422 CheckScriptSuccess([
3423 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003424 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003425 ' echo "yes"',
3426 'elseif 2 #comment',
3427 ' echo "no"',
3428 'endif',
3429 ])
3430
3431 CheckScriptFailure([
3432 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003433 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003434 ' echo "yes"',
3435 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003436 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003437
3438 CheckScriptFailure([
3439 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003440 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003441 ' echo "yes"',
3442 'elseif 2#comment',
3443 ' echo "no"',
3444 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003445 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003446
3447 CheckScriptSuccess([
3448 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003449 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003450 ])
3451
3452 CheckScriptFailure([
3453 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003454 'var v = 1# comment6',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003455 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003456
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003457 CheckScriptSuccess([
3458 'vim9script',
3459 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003460 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003461 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003462 'dsearch /pat/ #comment',
3463 'bwipe!',
3464 ])
3465
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003466 CheckScriptFailure([
3467 'vim9script',
3468 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003469 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003470 ':$',
3471 'dsearch /pat/#comment',
3472 'bwipe!',
3473 ], 'E488:')
3474
3475 CheckScriptFailure([
3476 'vim9script',
3477 'func! SomeFunc()',
3478 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02003479enddef
3480
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003481def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003482 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003483 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02003484 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003485 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02003486 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003487 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02003488 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003489 END
3490 writefile(lines, 'Xfinished')
3491 source Xfinished
3492 assert_equal('two', g:res)
3493
3494 unlet g:res
3495 delete('Xfinished')
3496enddef
3497
Bram Moolenaara5d00772020-05-14 23:20:55 +02003498def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003499 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02003500 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02003501 def GetValue(): string
3502 return theVal
3503 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003504 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02003505 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02003506 theVal = 'else'
3507 g:laterVal = GetValue()
3508 END
3509 writefile(lines, 'Xforward')
3510 source Xforward
3511 assert_equal('something', g:initVal)
3512 assert_equal('else', g:laterVal)
3513
3514 unlet g:initVal
3515 unlet g:laterVal
3516 delete('Xforward')
3517enddef
3518
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003519def Test_source_vim9_from_legacy()
Bram Moolenaara6294952020-12-27 13:39:50 +01003520 var vim9_lines =<< trim END
3521 vim9script
3522 var local = 'local'
3523 g:global = 'global'
3524 export var exported = 'exported'
3525 export def GetText(): string
3526 return 'text'
3527 enddef
3528 END
3529 writefile(vim9_lines, 'Xvim9_script.vim')
3530
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003531 var legacy_lines =<< trim END
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003532 source Xvim9_script.vim
3533
3534 call assert_false(exists('local'))
3535 call assert_false(exists('exported'))
3536 call assert_false(exists('s:exported'))
3537 call assert_equal('global', global)
3538 call assert_equal('global', g:global)
3539
3540 " imported variable becomes script-local
3541 import exported from './Xvim9_script.vim'
3542 call assert_equal('exported', s:exported)
3543 call assert_false(exists('exported'))
3544
3545 " imported function becomes script-local
3546 import GetText from './Xvim9_script.vim'
3547 call assert_equal('text', s:GetText())
3548 call assert_false(exists('*GetText'))
3549 END
3550 writefile(legacy_lines, 'Xlegacy_script.vim')
3551
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003552 source Xlegacy_script.vim
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003553 assert_equal('global', g:global)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003554 unlet g:global
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003555
3556 delete('Xlegacy_script.vim')
3557 delete('Xvim9_script.vim')
3558enddef
Bram Moolenaara5d00772020-05-14 23:20:55 +02003559
Bram Moolenaare535db82021-03-31 21:07:24 +02003560def Test_declare_script_in_func()
3561 var lines =<< trim END
3562 vim9script
3563 func Declare()
3564 let s:local = 123
3565 endfunc
3566 Declare()
3567 assert_equal(123, local)
3568
3569 var error: string
3570 try
3571 local = 'asdf'
3572 catch
3573 error = v:exception
3574 endtry
3575 assert_match('E1012: Type mismatch; expected number but got string', error)
3576
3577 lockvar local
3578 try
3579 local = 999
3580 catch
3581 error = v:exception
3582 endtry
3583 assert_match('E741: Value is locked: local', error)
3584 END
3585 CheckScriptSuccess(lines)
3586enddef
3587
3588
Bram Moolenaar7d699702020-08-14 20:52:28 +02003589func Test_vim9script_not_global()
3590 " check that items defined in Vim9 script are script-local, not global
3591 let vim9lines =<< trim END
3592 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003593 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02003594 func TheFunc()
3595 echo 'local'
3596 endfunc
3597 def DefFunc()
3598 echo 'local'
3599 enddef
3600 END
3601 call writefile(vim9lines, 'Xvim9script.vim')
3602 source Xvim9script.vim
3603 try
3604 echo g:var
3605 assert_report('did not fail')
3606 catch /E121:/
3607 " caught
3608 endtry
3609 try
3610 call TheFunc()
3611 assert_report('did not fail')
3612 catch /E117:/
3613 " caught
3614 endtry
3615 try
3616 call DefFunc()
3617 assert_report('did not fail')
3618 catch /E117:/
3619 " caught
3620 endtry
3621
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003622 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02003623endfunc
3624
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02003625def Test_vim9_copen()
3626 # this was giving an error for setting w:quickfix_title
3627 copen
3628 quit
3629enddef
3630
Bram Moolenaar03290b82020-12-19 16:30:44 +01003631" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003632def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003633 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003634 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01003635 def some#gettest(): string
3636 return 'test'
3637 enddef
3638 g:some#name = 'name'
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003639 g:some#dict = {key: 'value'}
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003640
3641 def some#varargs(a1: string, ...l: list<string>): string
3642 return a1 .. l[0] .. l[1]
3643 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01003644 END
3645
3646 mkdir('Xdir/autoload', 'p')
3647 writefile(lines, 'Xdir/autoload/some.vim')
3648 var save_rtp = &rtp
3649 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3650
3651 assert_equal('test', g:some#gettest())
3652 assert_equal('name', g:some#name)
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003653 assert_equal('value', g:some#dict.key)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003654 g:some#other = 'other'
3655 assert_equal('other', g:some#other)
3656
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003657 assert_equal('abc', some#varargs('a', 'b', 'c'))
3658
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003659 # upper case script name works
3660 lines =<< trim END
3661 vim9script
3662 def Other#getOther(): string
3663 return 'other'
3664 enddef
3665 END
3666 writefile(lines, 'Xdir/autoload/Other.vim')
3667 assert_equal('other', g:Other#getOther())
3668
Bram Moolenaar03290b82020-12-19 16:30:44 +01003669 delete('Xdir', 'rf')
3670 &rtp = save_rtp
3671enddef
3672
3673" test using a vim9script that is auto-loaded from an autocmd
3674def Test_vim9_aucmd_autoload()
3675 var lines =<< trim END
3676 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003677 def foo#test()
3678 echomsg getreg('"')
3679 enddef
3680 END
3681
3682 mkdir('Xdir/autoload', 'p')
3683 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003684 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003685 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3686 augroup test
3687 autocmd TextYankPost * call foo#test()
3688 augroup END
3689
3690 normal Y
3691
3692 augroup test
3693 autocmd!
3694 augroup END
3695 delete('Xdir', 'rf')
3696 &rtp = save_rtp
3697enddef
3698
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003699" This was causing a crash because suppress_errthrow wasn't reset.
3700def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003701 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003702 vim9script
3703 def crash#func()
3704 try
3705 for x in List()
3706 endfor
3707 catch
3708 endtry
3709 g:ok = true
3710 enddef
3711 fu List()
3712 invalid
3713 endfu
3714 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01003715 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003716 catch /wontmatch/
3717 endtry
3718 END
3719 call mkdir('Xruntime/autoload', 'p')
3720 call writefile(lines, 'Xruntime/autoload/crash.vim')
3721
3722 # run in a separate Vim to avoid the side effects of assert_fails()
3723 lines =<< trim END
3724 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
3725 call crash#func()
3726 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003727 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003728 END
3729 writefile(lines, 'Xscript')
3730 RunVim([], [], '-S Xscript')
3731 assert_equal(['ok'], readfile('Xdidit'))
3732
3733 delete('Xdidit')
3734 delete('Xscript')
3735 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01003736
3737 lines =<< trim END
3738 vim9script
3739 var foo#bar = 'asdf'
3740 END
3741 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003742enddef
3743
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003744def Test_script_var_in_autocmd()
3745 # using a script variable from an autocommand, defined in a :def function in a
3746 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003747 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02003748 let s:counter = 1
3749 def s:Func()
3750 au! CursorHold
3751 au CursorHold * s:counter += 1
3752 enddef
3753 call s:Func()
3754 doau CursorHold
3755 call assert_equal(2, s:counter)
3756 au! CursorHold
3757 END
3758 CheckScriptSuccess(lines)
3759enddef
3760
Bram Moolenaarb5841b92021-07-15 18:09:53 +02003761def Test_error_in_autoload_script()
3762 var save_rtp = &rtp
3763 var dir = getcwd() .. '/Xruntime'
3764 &rtp = dir
3765 mkdir(dir .. '/autoload', 'p')
3766
3767 var lines =<< trim END
3768 vim9script noclear
3769 def script#autoloaded()
3770 enddef
3771 def Broken()
3772 var x: any = ''
3773 eval x != 0
3774 enddef
3775 Broken()
3776 END
3777 writefile(lines, dir .. '/autoload/script.vim')
3778
3779 lines =<< trim END
3780 vim9script
3781 def CallAutoloaded()
3782 script#autoloaded()
3783 enddef
3784
3785 function Legacy()
3786 try
3787 call s:CallAutoloaded()
3788 catch
3789 call assert_match('E1030: Using a String as a Number', v:exception)
3790 endtry
3791 endfunction
3792
3793 Legacy()
3794 END
3795 CheckScriptSuccess(lines)
3796
3797 &rtp = save_rtp
3798 delete(dir, 'rf')
3799enddef
3800
Bram Moolenaar3896a102020-08-09 14:33:55 +02003801def Test_cmdline_win()
3802 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
3803 # the command line window.
3804 mkdir('rtp/syntax', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003805 var export_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02003806 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003807 export var That = 'yes'
Bram Moolenaar3896a102020-08-09 14:33:55 +02003808 END
3809 writefile(export_lines, 'rtp/syntax/Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003810 var import_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02003811 vim9script
3812 import That from './Xexport.vim'
3813 END
3814 writefile(import_lines, 'rtp/syntax/vim.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003815 var save_rtp = &rtp
Bram Moolenaar3896a102020-08-09 14:33:55 +02003816 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
3817 syntax on
3818 augroup CmdWin
3819 autocmd CmdwinEnter * g:got_there = 'yes'
3820 augroup END
3821 # this will open and also close the cmdline window
3822 feedkeys('q:', 'xt')
3823 assert_equal('yes', g:got_there)
3824
3825 augroup CmdWin
3826 au!
3827 augroup END
3828 &rtp = save_rtp
3829 delete('rtp', 'rf')
3830enddef
3831
Bram Moolenaare3d46852020-08-29 13:39:17 +02003832def Test_invalid_sid()
3833 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003834
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003835 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02003836 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02003837 endif
3838 delete('Xdidit')
3839enddef
3840
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003841def Test_restoring_cpo()
3842 writefile(['vim9script', 'set nocp'], 'Xsourced')
3843 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
3844 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
3845 assert_equal(['done'], readfile('Xdone'))
3846 endif
3847 delete('Xsourced')
3848 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01003849 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01003850
3851 writefile(['vim9script'], 'XanotherScript')
3852 set cpo=aABceFsMny>
3853 edit XanotherScript
3854 so %
3855 assert_equal('aABceFsMny>', &cpo)
3856 :1del
3857 w
3858 so %
3859 assert_equal('aABceFsMny>', &cpo)
3860
3861 delete('XanotherScript')
3862 set cpo&vim
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003863enddef
3864
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003865" Use :function so we can use Check commands
3866func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003867 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003868 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003869
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003870 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003871 vim9script
3872 def script#func()
3873 enddef
3874 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003875 call mkdir('Xdir/autoload', 'p')
3876 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003877
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003878 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003879 vim9script
3880 set cpo+=M
3881 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02003882 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003883 setline(1, 'some text')
3884 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003885 call writefile(lines, 'XTest_redraw_cpo')
3886 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
3887 call term_sendkeys(buf, "V:")
3888 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003889
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01003890 " clean up
3891 call term_sendkeys(buf, "\<Esc>u")
3892 call StopVimInTerminal(buf)
3893 call delete('XTest_redraw_cpo')
3894 call delete('Xdir', 'rf')
3895endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003896
Bram Moolenaar9ec70262020-12-09 17:16:59 +01003897
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003898def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003899 var lines =<< trim END
3900 var name: any
3901 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02003902 END
3903 CheckDefAndScriptSuccess(lines)
3904enddef
3905
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003906func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02003907 CheckRunVimInTerminal
3908
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02003909 " call indirectly to avoid compilation error for missing functions
3910 call Run_Test_define_func_at_command_line()
3911endfunc
3912
3913def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003914 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003915 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003916 func CheckAndQuit()
3917 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
3918 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
3919 endfunc
3920 END
3921 writefile([''], 'Xdidcmd')
3922 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01003923 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003924 # define Afunc() on the command line
3925 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
3926 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003927 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02003928
3929 call StopVimInTerminal(buf)
3930 delete('XcallFunc')
3931 delete('Xdidcmd')
3932enddef
3933
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003934def Test_script_var_scope()
3935 var lines =<< trim END
3936 vim9script
3937 if true
3938 if true
3939 var one = 'one'
3940 echo one
3941 endif
3942 echo one
3943 endif
3944 END
3945 CheckScriptFailure(lines, 'E121:', 7)
3946
3947 lines =<< trim END
3948 vim9script
3949 if true
3950 if false
3951 var one = 'one'
3952 echo one
3953 else
3954 var one = 'one'
3955 echo one
3956 endif
3957 echo one
3958 endif
3959 END
3960 CheckScriptFailure(lines, 'E121:', 10)
3961
3962 lines =<< trim END
3963 vim9script
3964 while true
3965 var one = 'one'
3966 echo one
3967 break
3968 endwhile
3969 echo one
3970 END
3971 CheckScriptFailure(lines, 'E121:', 7)
3972
3973 lines =<< trim END
3974 vim9script
3975 for i in range(1)
3976 var one = 'one'
3977 echo one
3978 endfor
3979 echo one
3980 END
3981 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003982
3983 lines =<< trim END
3984 vim9script
3985 {
3986 var one = 'one'
3987 assert_equal('one', one)
3988 }
3989 assert_false(exists('one'))
3990 assert_false(exists('s:one'))
3991 END
3992 CheckScriptSuccess(lines)
3993
3994 lines =<< trim END
3995 vim9script
3996 {
3997 var one = 'one'
3998 echo one
3999 }
4000 echo one
4001 END
4002 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004003enddef
4004
Bram Moolenaar352134b2020-10-17 22:04:08 +02004005def Test_catch_exception_in_callback()
4006 var lines =<< trim END
4007 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02004008 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02004009 try
4010 var x: string
4011 var y: string
4012 # this error should be caught with CHECKLEN
4013 [x, y] = ['']
4014 catch
4015 g:caught = 'yes'
4016 endtry
4017 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004018 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02004019 feedkeys("\r", 'xt')
4020 END
4021 CheckScriptSuccess(lines)
4022
4023 unlet g:caught
4024enddef
4025
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004026def Test_no_unknown_error_after_error()
4027 if !has('unix') || !has('job')
4028 throw 'Skipped: not unix of missing +job feature'
4029 endif
4030 var lines =<< trim END
4031 vim9script
4032 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02004033 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004034 eval [][0]
4035 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02004036 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004037 sleep 1m
4038 source += l
4039 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004040 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01004041 while job_status(myjob) == 'run'
4042 sleep 10m
4043 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01004044 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02004045 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004046 END
4047 writefile(lines, 'Xdef')
4048 assert_fails('so Xdef', ['E684:', 'E1012:'])
4049 delete('Xdef')
4050enddef
4051
Bram Moolenaar4324d872020-12-01 20:12:24 +01004052def InvokeNormal()
4053 exe "norm! :m+1\r"
4054enddef
4055
4056def Test_invoke_normal_in_visual_mode()
4057 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
4058 new
4059 setline(1, ['aaa', 'bbb'])
4060 feedkeys("V\<F3>", 'xt')
4061 assert_equal(['bbb', 'aaa'], getline(1, 2))
4062 xunmap <F3>
4063enddef
4064
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004065def Test_white_space_after_command()
4066 var lines =<< trim END
4067 exit_cb: Func})
4068 END
4069 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01004070
4071 lines =<< trim END
4072 e#
4073 END
4074 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004075enddef
4076
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004077def Test_script_var_gone_when_sourced_twice()
4078 var lines =<< trim END
4079 vim9script
4080 if exists('g:guard')
4081 finish
4082 endif
4083 g:guard = 1
4084 var name = 'thename'
4085 def g:GetName(): string
4086 return name
4087 enddef
4088 def g:SetName(arg: string)
4089 name = arg
4090 enddef
4091 END
4092 writefile(lines, 'XscriptTwice.vim')
4093 so XscriptTwice.vim
4094 assert_equal('thename', g:GetName())
4095 g:SetName('newname')
4096 assert_equal('newname', g:GetName())
4097 so XscriptTwice.vim
4098 assert_fails('call g:GetName()', 'E1149:')
4099 assert_fails('call g:SetName("x")', 'E1149:')
4100
4101 delfunc g:GetName
4102 delfunc g:SetName
4103 delete('XscriptTwice.vim')
4104 unlet g:guard
4105enddef
4106
4107def Test_import_gone_when_sourced_twice()
4108 var exportlines =<< trim END
4109 vim9script
4110 if exists('g:guard')
4111 finish
4112 endif
4113 g:guard = 1
4114 export var name = 'someName'
4115 END
4116 writefile(exportlines, 'XexportScript.vim')
4117
4118 var lines =<< trim END
4119 vim9script
4120 import name from './XexportScript.vim'
4121 def g:GetName(): string
4122 return name
4123 enddef
4124 END
4125 writefile(lines, 'XscriptImport.vim')
4126 so XscriptImport.vim
4127 assert_equal('someName', g:GetName())
4128
4129 so XexportScript.vim
4130 assert_fails('call g:GetName()', 'E1149:')
4131
4132 delfunc g:GetName
4133 delete('XexportScript.vim')
4134 delete('XscriptImport.vim')
4135 unlet g:guard
4136enddef
4137
Bram Moolenaar10b94212021-02-19 21:42:57 +01004138def Test_unsupported_commands()
4139 var lines =<< trim END
4140 ka
4141 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004142 CheckDefFailure(lines, 'E476:')
4143 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaar10b94212021-02-19 21:42:57 +01004144
4145 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01004146 :1ka
4147 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004148 CheckDefFailure(lines, 'E476:')
4149 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaarada1d872021-02-20 08:16:51 +01004150
4151 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01004152 t
4153 END
4154 CheckDefFailure(lines, 'E1100:')
4155 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4156
4157 lines =<< trim END
4158 x
4159 END
4160 CheckDefFailure(lines, 'E1100:')
4161 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4162
4163 lines =<< trim END
4164 xit
4165 END
4166 CheckDefFailure(lines, 'E1100:')
4167 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4168enddef
4169
Bram Moolenaarc70fe462021-04-17 17:59:19 +02004170def Test_mapping_line_number()
4171 var lines =<< trim END
4172 vim9script
4173 def g:FuncA()
4174 # Some comment
4175 FuncB(0)
4176 enddef
4177 # Some comment
4178 def FuncB(
4179 # Some comment
4180 n: number
4181 )
4182 exe 'nno '
4183 # Some comment
4184 .. '<F3> a'
4185 .. 'b'
4186 .. 'c'
4187 enddef
4188 END
4189 CheckScriptSuccess(lines)
4190 var res = execute('verbose nmap <F3>')
4191 assert_match('No mapping found', res)
4192
4193 g:FuncA()
4194 res = execute('verbose nmap <F3>')
4195 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
4196
4197 nunmap <F3>
4198 delfunc g:FuncA
4199enddef
4200
Bram Moolenaardeb108b2021-07-08 17:35:36 +02004201def Test_option_set()
4202 # legacy script allows for white space
4203 var lines =<< trim END
4204 set foldlevel =11
4205 call assert_equal(11, &foldlevel)
4206 END
4207 CheckScriptSuccess(lines)
4208
4209 set foldlevel
4210 set foldlevel=12
4211 assert_equal(12, &foldlevel)
4212 set foldlevel+=2
4213 assert_equal(14, &foldlevel)
4214 set foldlevel-=3
4215 assert_equal(11, &foldlevel)
4216
4217 lines =<< trim END
4218 set foldlevel =1
4219 END
4220 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: =1')
4221
4222 lines =<< trim END
4223 set foldlevel +=1
4224 END
4225 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: +=1')
4226
4227 lines =<< trim END
4228 set foldlevel ^=1
4229 END
4230 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: ^=1')
4231
4232 lines =<< trim END
4233 set foldlevel -=1
4234 END
4235 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: -=1')
4236
4237 set foldlevel&
4238enddef
4239
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004240def Test_option_modifier()
Bram Moolenaar1594f312021-07-08 16:40:13 +02004241 # legacy script allows for white space
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004242 var lines =<< trim END
4243 set hlsearch & hlsearch !
4244 call assert_equal(1, &hlsearch)
4245 END
4246 CheckScriptSuccess(lines)
4247
Bram Moolenaar1594f312021-07-08 16:40:13 +02004248 set hlsearch
4249 set hlsearch!
4250 assert_equal(false, &hlsearch)
4251
4252 set hlsearch
4253 set hlsearch&
4254 assert_equal(false, &hlsearch)
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004255
4256 lines =<< trim END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004257 set hlsearch &
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004258 END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004259 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: &')
4260
4261 lines =<< trim END
4262 set hlsearch !
4263 END
4264 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: !')
4265
4266 set hlsearch&
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004267enddef
4268
Bram Moolenaarc03fe662021-07-11 16:52:45 +02004269" This must be called last, it may cause following :def functions to fail
4270def Test_xxx_echoerr_line_number()
4271 var lines =<< trim END
4272 echoerr 'some'
4273 .. ' error'
4274 .. ' continued'
4275 END
4276 CheckDefExecAndScriptFailure(lines, 'some error continued', 1)
4277enddef
4278
Bram Moolenaar648594e2021-07-11 17:55:01 +02004279def ProfiledWithLambda()
Bram Moolenaard9162552021-07-11 15:26:13 +02004280 var n = 3
4281 echo [[1, 2], [3, 4]]->filter((_, l) => l[0] == n)
4282enddef
4283
Bram Moolenaar648594e2021-07-11 17:55:01 +02004284def ProfiledNested()
4285 var x = 0
4286 def Nested(): any
4287 return x
4288 enddef
4289 Nested()
4290enddef
4291
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004292def ProfiledNestedProfiled()
4293 var x = 0
4294 def Nested(): any
4295 return x
4296 enddef
4297 Nested()
4298enddef
4299
Bram Moolenaard9162552021-07-11 15:26:13 +02004300" Execute this near the end, profiling doesn't stop until Vim exists.
4301" This only tests that it works, not the profiling output.
4302def Test_xx_profile_with_lambda()
Bram Moolenaar4ece1522021-07-11 16:31:51 +02004303 CheckFeature profile
4304
Bram Moolenaard9162552021-07-11 15:26:13 +02004305 profile start Xprofile.log
Bram Moolenaar648594e2021-07-11 17:55:01 +02004306 profile func ProfiledWithLambda
4307 ProfiledWithLambda()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004308
Bram Moolenaar648594e2021-07-11 17:55:01 +02004309 profile func ProfiledNested
4310 ProfiledNested()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004311
4312 # Also profile the nested function. Use a different function, although the
4313 # contents is the same, to make sure it was not already compiled.
4314 profile func *
4315 ProfiledNestedProfiled()
4316
4317 profdel func *
4318 profile pause
Bram Moolenaard9162552021-07-11 15:26:13 +02004319enddef
4320
Bram Moolenaar585fea72020-04-02 22:33:21 +02004321" Keep this last, it messes up highlighting.
4322def Test_substitute_cmd()
4323 new
4324 setline(1, 'something')
4325 :substitute(some(other(
4326 assert_equal('otherthing', getline(1))
4327 bwipe!
4328
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004329 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004330 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02004331 vim9script
4332 new
4333 setline(1, 'something')
4334 :substitute(some(other(
4335 assert_equal('otherthing', getline(1))
4336 bwipe!
4337 END
4338 writefile(lines, 'Xvim9lines')
4339 source Xvim9lines
4340
4341 delete('Xvim9lines')
4342enddef
4343
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker