blob: 19eee76c784da81c85d8793f7ca915b200ba7d25 [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 Moolenaarb79ee0c2022-01-01 12:17:00 +000010def Test_vim9script_feature()
11 # example from the help, here the feature is always present
12 var lines =<< trim END
13 " old style comment
14 if !has('vim9script')
15 " legacy commands would go here
16 finish
17 endif
18 vim9script
19 # Vim9 script commands go here
20 g:didit = true
21 END
22 CheckScriptSuccess(lines)
23 assert_equal(true, g:didit)
24 unlet g:didit
25enddef
26
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020027def Test_range_only()
28 new
29 setline(1, ['blah', 'Blah'])
30 :/Blah/
31 assert_equal(2, getcurpos()[1])
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020032 bwipe!
33
34 # without range commands use current line
35 new
36 setline(1, ['one', 'two', 'three'])
37 :2
38 print
39 assert_equal('two', Screenline(&lines))
40 :3
41 list
42 assert_equal('three$', Screenline(&lines))
Bram Moolenaarb8554302021-02-15 21:30:30 +010043
44 # missing command does not print the line
45 var lines =<< trim END
46 vim9script
47 :1|
48 assert_equal('three$', Screenline(&lines))
49 :|
50 assert_equal('three$', Screenline(&lines))
51 END
52 CheckScriptSuccess(lines)
53
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020054 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +010055
56 # won't generate anything
57 if false
58 :123
59 endif
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020060enddef
61
Bram Moolenaara6e67e42020-05-15 23:36:40 +020062let g:alist = [7]
63let g:astring = 'text'
Bram Moolenaarf0b9f432020-07-17 23:03:17 +020064let g:anumber = 123
Bram Moolenaar6e587dc2020-02-06 13:15:52 +010065
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020066def Test_delfunction()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020067 # Check function is defined in script namespace
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020068 CheckScriptSuccess([
69 'vim9script',
70 'func CheckMe()',
71 ' return 123',
72 'endfunc',
73 'assert_equal(123, s:CheckMe())',
74 ])
75
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020076 # Check function in script namespace cannot be deleted
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020077 CheckScriptFailure([
78 'vim9script',
79 'func DeleteMe1()',
80 'endfunc',
81 'delfunction DeleteMe1',
82 ], 'E1084:')
83 CheckScriptFailure([
84 'vim9script',
85 'func DeleteMe2()',
86 'endfunc',
87 'def DoThat()',
88 ' delfunction DeleteMe2',
89 'enddef',
90 'DoThat()',
91 ], 'E1084:')
92 CheckScriptFailure([
93 'vim9script',
94 'def DeleteMe3()',
95 'enddef',
96 'delfunction DeleteMe3',
97 ], 'E1084:')
98 CheckScriptFailure([
99 'vim9script',
100 'def DeleteMe4()',
101 'enddef',
102 'def DoThat()',
103 ' delfunction DeleteMe4',
104 'enddef',
105 'DoThat()',
106 ], 'E1084:')
Bram Moolenaar925e9fd2020-07-25 15:41:11 +0200107
108 # Check that global :def function can be replaced and deleted
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200109 var lines =<< trim END
Bram Moolenaar925e9fd2020-07-25 15:41:11 +0200110 vim9script
111 def g:Global(): string
112 return "yes"
113 enddef
114 assert_equal("yes", g:Global())
115 def! g:Global(): string
116 return "no"
117 enddef
118 assert_equal("no", g:Global())
119 delfunc g:Global
120 assert_false(exists('*g:Global'))
121 END
122 CheckScriptSuccess(lines)
123
124 # Check that global function can be replaced by a :def function and deleted
125 lines =<< trim END
126 vim9script
127 func g:Global()
128 return "yes"
129 endfunc
130 assert_equal("yes", g:Global())
131 def! g:Global(): string
132 return "no"
133 enddef
134 assert_equal("no", g:Global())
135 delfunc g:Global
136 assert_false(exists('*g:Global'))
137 END
138 CheckScriptSuccess(lines)
139
140 # Check that global :def function can be replaced by a function and deleted
141 lines =<< trim END
142 vim9script
143 def g:Global(): string
144 return "yes"
145 enddef
146 assert_equal("yes", g:Global())
147 func! g:Global()
148 return "no"
149 endfunc
150 assert_equal("no", g:Global())
151 delfunc g:Global
152 assert_false(exists('*g:Global'))
153 END
154 CheckScriptSuccess(lines)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200155enddef
156
Bram Moolenaar08052222020-09-14 17:04:31 +0200157def Test_wrong_type()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200158 CheckDefFailure(['var name: list<nothing>'], 'E1010:')
159 CheckDefFailure(['var name: list<list<nothing>>'], 'E1010:')
160 CheckDefFailure(['var name: dict<nothing>'], 'E1010:')
161 CheckDefFailure(['var name: dict<dict<nothing>>'], 'E1010:')
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100162
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200163 CheckDefFailure(['var name: dict<number'], 'E1009:')
164 CheckDefFailure(['var name: dict<list<number>'], 'E1009:')
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100165
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200166 CheckDefFailure(['var name: ally'], 'E1010:')
167 CheckDefFailure(['var name: bram'], 'E1010:')
168 CheckDefFailure(['var name: cathy'], 'E1010:')
169 CheckDefFailure(['var name: dom'], 'E1010:')
170 CheckDefFailure(['var name: freddy'], 'E1010:')
171 CheckDefFailure(['var name: john'], 'E1010:')
172 CheckDefFailure(['var name: larry'], 'E1010:')
173 CheckDefFailure(['var name: ned'], 'E1010:')
174 CheckDefFailure(['var name: pam'], 'E1010:')
175 CheckDefFailure(['var name: sam'], 'E1010:')
176 CheckDefFailure(['var name: vim'], 'E1010:')
Bram Moolenaara0a9f432020-04-28 21:29:34 +0200177
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200178 CheckDefFailure(['var Ref: number', 'Ref()'], 'E1085:')
179 CheckDefFailure(['var Ref: string', 'var res = Ref()'], 'E1085:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200180enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181
Bram Moolenaar10c65862020-10-08 21:16:42 +0200182def Test_script_wrong_type()
183 var lines =<< trim END
184 vim9script
185 var s:dict: dict<string>
186 s:dict['a'] = ['x']
187 END
188 CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got list<string>', 3)
189enddef
190
Bram Moolenaar08052222020-09-14 17:04:31 +0200191def Test_const()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200192 CheckDefFailure(['final name = 234', 'name = 99'], 'E1018:')
193 CheckDefFailure(['final one = 234', 'var one = 99'], 'E1017:')
194 CheckDefFailure(['final list = [1, 2]', 'var list = [3, 4]'], 'E1017:')
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200195 CheckDefFailure(['final two'], 'E1125:')
196 CheckDefFailure(['final &option'], 'E996:')
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200197
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200198 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200199 final list = [1, 2, 3]
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200200 list[0] = 4
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200201 list->assert_equal([4, 2, 3])
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200202 const other = [5, 6, 7]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200203 other->assert_equal([5, 6, 7])
Bram Moolenaar71abe482020-09-14 22:28:30 +0200204
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200205 var varlist = [7, 8]
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200206 const constlist = [1, varlist, 3]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200207 varlist[0] = 77
Bram Moolenaar23e2e112021-08-03 21:16:18 +0200208 constlist[1][1] = 88
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200209 var cl = constlist[1]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200210 cl[1] = 88
211 constlist->assert_equal([1, [77, 88], 3])
212
Bram Moolenaare0de1712020-12-02 17:36:54 +0100213 var vardict = {five: 5, six: 6}
214 const constdict = {one: 1, two: vardict, three: 3}
Bram Moolenaar71abe482020-09-14 22:28:30 +0200215 vardict['five'] = 55
Bram Moolenaar23e2e112021-08-03 21:16:18 +0200216 constdict['two']['six'] = 66
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200217 var cd = constdict['two']
Bram Moolenaar71abe482020-09-14 22:28:30 +0200218 cd['six'] = 66
Bram Moolenaare0de1712020-12-02 17:36:54 +0100219 constdict->assert_equal({one: 1, two: {five: 55, six: 66}, three: 3})
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200220 END
221 CheckDefAndScriptSuccess(lines)
Bram Moolenaar08052222020-09-14 17:04:31 +0200222enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100223
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200224def Test_const_bang()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200225 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200226 const var = 234
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200227 var = 99
228 END
229 CheckDefExecFailure(lines, 'E1018:', 2)
230 CheckScriptFailure(['vim9script'] + lines, 'E46:', 3)
231
232 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200233 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200234 ll[0] = 99
235 END
236 CheckDefExecFailure(lines, 'E1119:', 2)
237 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
238
239 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200240 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200241 ll[3] = 99
242 END
243 CheckDefExecFailure(lines, 'E1118:', 2)
244 CheckScriptFailure(['vim9script'] + lines, 'E684:', 3)
245
246 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100247 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200248 dd["one"] = 99
249 END
250 CheckDefExecFailure(lines, 'E1121:', 2)
251 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
252
253 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100254 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200255 dd["three"] = 99
256 END
257 CheckDefExecFailure(lines, 'E1120:')
258 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
259enddef
260
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200261def Test_range_no_colon()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200262 CheckDefFailure(['%s/a/b/'], 'E1050:')
263 CheckDefFailure(['+ s/a/b/'], 'E1050:')
264 CheckDefFailure(['- s/a/b/'], 'E1050:')
265 CheckDefFailure(['. s/a/b/'], 'E1050:')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200266enddef
267
268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269def Test_block()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200270 var outer = 1
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 {
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200272 var inner = 2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100273 assert_equal(1, outer)
274 assert_equal(2, inner)
275 }
276 assert_equal(1, outer)
Bram Moolenaar3f1e9f02021-02-27 22:36:43 +0100277
278 {|echo 'yes'|}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279enddef
280
Bram Moolenaar08052222020-09-14 17:04:31 +0200281def Test_block_failure()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200282 CheckDefFailure(['{', 'var inner = 1', '}', 'echo inner'], 'E1001:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200283 CheckDefFailure(['}'], 'E1025:')
284 CheckDefFailure(['{', 'echo 1'], 'E1026:')
285enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100286
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200287def Test_block_local_vars()
288 var lines =<< trim END
289 vim9script
Bram Moolenaared234f22020-10-15 20:42:20 +0200290 v:testing = 1
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200291 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200292 var text = ['hello']
293 def SayHello(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200294 return text
295 enddef
296 def SetText(v: string)
Bram Moolenaared234f22020-10-15 20:42:20 +0200297 text = [v]
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200298 enddef
299 endif
300
301 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200302 var text = ['again']
303 def SayAgain(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200304 return text
305 enddef
306 endif
Bram Moolenaared234f22020-10-15 20:42:20 +0200307
308 # test that the "text" variables are not cleaned up
309 test_garbagecollect_now()
310
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200311 defcompile
312
Bram Moolenaared234f22020-10-15 20:42:20 +0200313 assert_equal(['hello'], SayHello())
314 assert_equal(['again'], SayAgain())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200315
316 SetText('foobar')
Bram Moolenaared234f22020-10-15 20:42:20 +0200317 assert_equal(['foobar'], SayHello())
318
319 call writefile(['ok'], 'Xdidit')
320 qall!
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200321 END
Bram Moolenaared234f22020-10-15 20:42:20 +0200322
323 # need to execute this with a separate Vim instance to avoid the current
324 # context gets garbage collected.
325 writefile(lines, 'Xscript')
326 RunVim([], [], '-S Xscript')
327 assert_equal(['ok'], readfile('Xdidit'))
328
329 delete('Xscript')
330 delete('Xdidit')
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200331enddef
332
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200333def Test_block_local_vars_with_func()
334 var lines =<< trim END
335 vim9script
336 if true
337 var foo = 'foo'
338 if true
339 var bar = 'bar'
340 def Func(): list<string>
341 return [foo, bar]
342 enddef
343 endif
344 endif
345 # function is compiled here, after blocks have finished, can still access
346 # "foo" and "bar"
347 assert_equal(['foo', 'bar'], Func())
348 END
349 CheckScriptSuccess(lines)
350enddef
351
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200352" legacy func for command that's defined later
353func InvokeSomeCommand()
354 SomeCommand
355endfunc
356
357def Test_autocommand_block()
358 com SomeCommand {
359 g:someVar = 'some'
360 }
361 InvokeSomeCommand()
362 assert_equal('some', g:someVar)
363
364 delcommand SomeCommand
365 unlet g:someVar
366enddef
367
368def Test_command_block()
369 au BufNew *.xml {
370 g:otherVar = 'other'
371 }
372 split other.xml
373 assert_equal('other', g:otherVar)
374
375 bwipe!
376 au! BufNew *.xml
377 unlet g:otherVar
378enddef
379
Bram Moolenaard032f342020-07-18 18:13:02 +0200380func g:NoSuchFunc()
381 echo 'none'
382endfunc
383
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100384def Test_try_catch_throw()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200385 var l = []
Bram Moolenaar7a092242020-04-16 22:10:49 +0200386 try # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100387 add(l, '1')
388 throw 'wrong'
389 add(l, '2')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200390 catch # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391 add(l, v:exception)
Bram Moolenaar7a092242020-04-16 22:10:49 +0200392 finally # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393 add(l, '3')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200394 endtry # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100395 assert_equal(['1', 'wrong', '3'], l)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200396
Bram Moolenaare8593122020-07-18 15:17:02 +0200397 l = []
398 try
399 try
400 add(l, '1')
401 throw 'wrong'
402 add(l, '2')
403 catch /right/
404 add(l, v:exception)
405 endtry
406 catch /wrong/
407 add(l, 'caught')
Bram Moolenaar373863e2020-09-26 17:20:53 +0200408 fina
Bram Moolenaare8593122020-07-18 15:17:02 +0200409 add(l, 'finally')
410 endtry
411 assert_equal(['1', 'caught', 'finally'], l)
412
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200413 var n: number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200414 try
415 n = l[3]
416 catch /E684:/
417 n = 99
418 endtry
419 assert_equal(99, n)
420
Bram Moolenaar69f70502021-01-01 16:10:46 +0100421 var done = 'no'
422 if 0
423 try | catch | endtry
424 else
425 done = 'yes'
426 endif
427 assert_equal('yes', done)
428
429 done = 'no'
430 if 1
431 done = 'yes'
432 else
433 try | catch | endtry
434 done = 'never'
435 endif
436 assert_equal('yes', done)
437
438 if 1
439 else
440 try | catch /pat/ | endtry
441 try | catch /pat/
442 endtry
443 try
444 catch /pat/ | endtry
445 try
446 catch /pat/
447 endtry
448 endif
449
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200450 try
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200451 # string slice returns a string, not a number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200452 n = g:astring[3]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200453 catch /E1012:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200454 n = 77
455 endtry
456 assert_equal(77, n)
457
458 try
459 n = l[g:astring]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200460 catch /E1012:/
Bram Moolenaar56acb092020-08-16 14:48:19 +0200461 n = 88
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200462 endtry
Bram Moolenaar56acb092020-08-16 14:48:19 +0200463 assert_equal(88, n)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200464
465 try
466 n = s:does_not_exist
467 catch /E121:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200468 n = 111
469 endtry
470 assert_equal(111, n)
471
472 try
473 n = g:does_not_exist
474 catch /E121:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200475 n = 121
476 endtry
477 assert_equal(121, n)
478
Bram Moolenaare0de1712020-12-02 17:36:54 +0100479 var d = {one: 1}
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200480 try
481 n = d[g:astring]
482 catch /E716:/
483 n = 222
484 endtry
485 assert_equal(222, n)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200486
487 try
488 n = -g:astring
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +0200489 catch /E1012:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200490 n = 233
491 endtry
492 assert_equal(233, n)
493
494 try
495 n = +g:astring
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +0200496 catch /E1012:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200497 n = 244
498 endtry
499 assert_equal(244, n)
500
501 try
502 n = +g:alist
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +0200503 catch /E1012:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200504 n = 255
505 endtry
506 assert_equal(255, n)
507
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200508 var nd: dict<any>
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200509 try
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100510 nd = {[g:alist]: 1}
511 catch /E1105:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200512 n = 266
513 endtry
514 assert_equal(266, n)
515
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +0000516 l = [1, 2, 3]
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200517 try
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +0000518 [n] = l
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200519 catch /E1093:/
520 n = 277
521 endtry
522 assert_equal(277, n)
523
Bram Moolenaare8593122020-07-18 15:17:02 +0200524 try
525 &ts = g:astring
Bram Moolenaar5e654232020-09-16 15:22:00 +0200526 catch /E1012:/
Bram Moolenaare8593122020-07-18 15:17:02 +0200527 n = 288
528 endtry
529 assert_equal(288, n)
530
531 try
532 &backspace = 'asdf'
533 catch /E474:/
534 n = 299
535 endtry
536 assert_equal(299, n)
537
538 l = [1]
539 try
540 l[3] = 3
541 catch /E684:/
542 n = 300
543 endtry
544 assert_equal(300, n)
545
546 try
Bram Moolenaare8593122020-07-18 15:17:02 +0200547 unlet g:does_not_exist
548 catch /E108:/
549 n = 322
550 endtry
551 assert_equal(322, n)
552
553 try
Bram Moolenaar2bede172020-11-19 18:53:18 +0100554 d = {text: 1, [g:astring]: 2}
Bram Moolenaare8593122020-07-18 15:17:02 +0200555 catch /E721:/
556 n = 333
557 endtry
558 assert_equal(333, n)
559
560 try
561 l = DeletedFunc()
562 catch /E933:/
563 n = 344
564 endtry
565 assert_equal(344, n)
Bram Moolenaard032f342020-07-18 18:13:02 +0200566
567 try
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200568 echo range(1, 2, 0)
569 catch /E726:/
Bram Moolenaard032f342020-07-18 18:13:02 +0200570 n = 355
571 endtry
572 assert_equal(355, n)
573
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200574 var P = function('g:NoSuchFunc')
Bram Moolenaard032f342020-07-18 18:13:02 +0200575 delfunc g:NoSuchFunc
576 try
577 echo P()
578 catch /E117:/
579 n = 366
580 endtry
581 assert_equal(366, n)
582
583 try
584 echo g:NoSuchFunc()
585 catch /E117:/
586 n = 377
587 endtry
588 assert_equal(377, n)
589
590 try
591 echo g:alist + 4
592 catch /E745:/
593 n = 388
594 endtry
595 assert_equal(388, n)
596
597 try
598 echo 4 + g:alist
599 catch /E745:/
600 n = 399
601 endtry
602 assert_equal(399, n)
603
604 try
605 echo g:alist.member
606 catch /E715:/
607 n = 400
608 endtry
609 assert_equal(400, n)
610
611 try
612 echo d.member
613 catch /E716:/
614 n = 411
615 endtry
616 assert_equal(411, n)
Bram Moolenaard9d77892021-02-12 21:32:47 +0100617
618 var counter = 0
619 for i in range(4)
620 try
621 eval [][0]
622 catch
623 endtry
624 counter += 1
625 endfor
626 assert_equal(4, counter)
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100627
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +0200628 # no requirement for spaces before |
629 try|echo 0|catch|endtry
630
Bram Moolenaar003312b2021-12-20 10:55:35 +0000631 # return in try with finally
632 def ReturnInTry(): number
633 var ret = 4
634 try
635 return ret
636 catch /this/
637 return -1
638 catch /that/
639 return -1
640 finally
641 # changing ret has no effect
642 ret = 7
643 endtry
644 return -2
645 enddef
646 assert_equal(4, ReturnInTry())
647
648 # return in catch with finally
649 def ReturnInCatch(): number
650 var ret = 5
651 try
652 throw 'getout'
653 return -1
654 catch /getout/
655 # ret is evaluated here
656 return ret
657 finally
658 # changing ret later has no effect
659 ret = -3
660 endtry
661 return -2
662 enddef
663 assert_equal(5, ReturnInCatch())
664
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100665 # return in finally after empty catch
666 def ReturnInFinally(): number
667 try
668 finally
Bram Moolenaar003312b2021-12-20 10:55:35 +0000669 return 6
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100670 endtry
Bram Moolenaar003312b2021-12-20 10:55:35 +0000671 return -1
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100672 enddef
Bram Moolenaar003312b2021-12-20 10:55:35 +0000673 assert_equal(6, ReturnInFinally())
Bram Moolenaar8ac681a2021-06-15 20:06:34 +0200674
675 var lines =<< trim END
676 vim9script
677 try
678 acos('0.5')
679 ->setline(1)
680 catch
681 g:caught = v:exception
682 endtry
683 END
684 CheckScriptSuccess(lines)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200685 assert_match('E1219: Float or Number required for argument 1', g:caught)
Bram Moolenaar8ac681a2021-06-15 20:06:34 +0200686 unlet g:caught
Bram Moolenaar41978282021-07-04 14:47:30 +0200687
688 # missing catch and/or finally
689 lines =<< trim END
690 vim9script
691 try
692 echo 'something'
693 endtry
694 END
695 CheckScriptFailure(lines, 'E1032:')
rbtnn84934992021-08-07 13:26:53 +0200696
697 # skipping try-finally-endtry when try-finally-endtry is used in another block
698 lines =<< trim END
699 if v:true
700 try
701 finally
702 endtry
703 else
704 try
705 finally
706 endtry
707 endif
708 END
709 CheckDefAndScriptSuccess(lines)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100710enddef
711
Bram Moolenaar3f987b52021-06-30 12:02:24 +0200712def Test_try_in_catch()
713 var lines =<< trim END
714 vim9script
715 var seq = []
716 def DoIt()
717 try
718 seq->add('throw 1')
719 eval [][0]
720 seq->add('notreached')
721 catch
722 seq->add('catch')
723 try
724 seq->add('throw 2')
725 eval [][0]
726 seq->add('notreached')
727 catch /nothing/
728 seq->add('notreached')
729 endtry
730 seq->add('done')
731 endtry
732 enddef
733 DoIt()
734 assert_equal(['throw 1', 'catch', 'throw 2', 'done'], seq)
735 END
736enddef
737
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200738def Test_error_in_catch()
739 var lines =<< trim END
740 try
741 eval [][0]
742 catch /E684:/
743 eval [][0]
744 endtry
745 END
746 CheckDefExecFailure(lines, 'E684:', 4)
747enddef
748
Bram Moolenaar2e34c342021-03-14 12:13:33 +0100749" :while at the very start of a function that :continue jumps to
750def TryContinueFunc()
751 while g:Count < 2
752 g:sequence ..= 't'
753 try
754 echoerr 'Test'
755 catch
756 g:Count += 1
757 g:sequence ..= 'c'
758 continue
759 endtry
760 g:sequence ..= 'e'
761 g:Count += 1
762 endwhile
763enddef
764
765def Test_continue_in_try_in_while()
766 g:Count = 0
767 g:sequence = ''
768 TryContinueFunc()
769 assert_equal('tctc', g:sequence)
770 unlet g:Count
771 unlet g:sequence
772enddef
773
Bram Moolenaar9cb577a2021-02-22 22:45:10 +0100774def Test_nocatch_return_in_try()
775 # return in try block returns normally
776 def ReturnInTry(): string
777 try
778 return '"some message"'
779 catch
780 endtry
781 return 'not reached'
782 enddef
783 exe 'echoerr ' .. ReturnInTry()
784enddef
785
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100786def Test_cnext_works_in_catch()
787 var lines =<< trim END
788 vim9script
Bram Moolenaarc3235272021-07-10 19:42:03 +0200789 au BufEnter * eval 1 + 2
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100790 writefile(['text'], 'Xfile1')
791 writefile(['text'], 'Xfile2')
792 var items = [
793 {lnum: 1, filename: 'Xfile1', valid: true},
794 {lnum: 1, filename: 'Xfile2', valid: true}
795 ]
796 setqflist([], ' ', {items: items})
797 cwindow
798
799 def CnextOrCfirst()
800 # if cnext fails, cfirst is used
801 try
802 cnext
803 catch
804 cfirst
805 endtry
806 enddef
807
808 CnextOrCfirst()
809 CnextOrCfirst()
810 writefile([getqflist({idx: 0}).idx], 'Xresult')
811 qall
812 END
813 writefile(lines, 'XCatchCnext')
814 RunVim([], [], '--clean -S XCatchCnext')
815 assert_equal(['1'], readfile('Xresult'))
816
817 delete('Xfile1')
818 delete('Xfile2')
819 delete('XCatchCnext')
820 delete('Xresult')
821enddef
822
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100823def Test_throw_skipped()
824 if 0
825 throw dontgethere
826 endif
827enddef
828
Bram Moolenaar8f81b222021-01-14 21:47:06 +0100829def Test_nocatch_throw_silenced()
830 var lines =<< trim END
831 vim9script
832 def Func()
833 throw 'error'
834 enddef
835 silent! Func()
836 END
837 writefile(lines, 'XthrowSilenced')
838 source XthrowSilenced
839 delete('XthrowSilenced')
840enddef
841
Bram Moolenaare8593122020-07-18 15:17:02 +0200842def DeletedFunc(): list<any>
843 return ['delete me']
844enddef
845defcompile
846delfunc DeletedFunc
847
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100848def ThrowFromDef()
Bram Moolenaara72cfb82020-04-23 17:07:30 +0200849 throw "getout" # comment
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100850enddef
851
852func CatchInFunc()
853 try
854 call ThrowFromDef()
855 catch
856 let g:thrown_func = v:exception
857 endtry
858endfunc
859
860def CatchInDef()
861 try
862 ThrowFromDef()
863 catch
864 g:thrown_def = v:exception
865 endtry
866enddef
867
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100868def ReturnFinally(): string
869 try
870 return 'intry'
Bram Moolenaar373863e2020-09-26 17:20:53 +0200871 finall
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100872 g:in_finally = 'finally'
873 endtry
874 return 'end'
875enddef
876
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100877def Test_try_catch_nested()
878 CatchInFunc()
879 assert_equal('getout', g:thrown_func)
880
881 CatchInDef()
882 assert_equal('getout', g:thrown_def)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100883
884 assert_equal('intry', ReturnFinally())
885 assert_equal('finally', g:in_finally)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200886
887 var l = []
888 try
889 l->add('1')
890 throw 'bad'
891 l->add('x')
892 catch /bad/
893 l->add('2')
894 try
895 l->add('3')
896 throw 'one'
897 l->add('x')
898 catch /one/
899 l->add('4')
900 try
901 l->add('5')
902 throw 'more'
903 l->add('x')
904 catch /more/
905 l->add('6')
906 endtry
907 endtry
908 endtry
909 assert_equal(['1', '2', '3', '4', '5', '6'], l)
Bram Moolenaar834193a2021-06-30 20:39:15 +0200910
911 l = []
912 try
913 try
914 l->add('1')
915 throw 'foo'
916 l->add('x')
917 catch
918 l->add('2')
919 throw 'bar'
920 l->add('x')
921 finally
922 l->add('3')
923 endtry
924 l->add('x')
925 catch /bar/
926 l->add('4')
927 endtry
928 assert_equal(['1', '2', '3', '4'], l)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100929enddef
930
Bram Moolenaar9939f572020-09-16 22:29:52 +0200931def TryOne(): number
932 try
933 return 0
934 catch
935 endtry
936 return 0
937enddef
938
939def TryTwo(n: number): string
940 try
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200941 var x = {}
Bram Moolenaar9939f572020-09-16 22:29:52 +0200942 catch
943 endtry
944 return 'text'
945enddef
946
947def Test_try_catch_twice()
948 assert_equal('text', TryOne()->TryTwo())
949enddef
950
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100951def Test_try_catch_match()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200952 var seq = 'a'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100953 try
954 throw 'something'
955 catch /nothing/
956 seq ..= 'x'
957 catch /some/
958 seq ..= 'b'
959 catch /asdf/
960 seq ..= 'x'
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200961 catch ?a\?sdf?
962 seq ..= 'y'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100963 finally
964 seq ..= 'c'
965 endtry
966 assert_equal('abc', seq)
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100967enddef
968
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200969def Test_try_catch_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200970 CheckDefFailure(['catch'], 'E603:')
971 CheckDefFailure(['try', 'echo 0', 'catch', 'catch'], 'E1033:')
972 CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
973 CheckDefFailure(['finally'], 'E606:')
974 CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:')
975 CheckDefFailure(['endtry'], 'E602:')
976 CheckDefFailure(['while 1', 'endtry'], 'E170:')
977 CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:')
Bram Moolenaar13106602020-10-04 16:06:05 +0200978 CheckDefFailure(['if 1', 'endtry'], 'E171:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200979 CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:')
Bram Moolenaar585fea72020-04-02 22:33:21 +0200980
Bram Moolenaare4984292020-12-13 14:19:25 +0100981 CheckDefFailure(['throw'], 'E1143:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200982 CheckDefFailure(['throw xxx'], 'E1001:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200983enddef
984
Bram Moolenaar7c5b3c0362021-02-14 22:40:57 +0100985def Try_catch_skipped()
986 var l = []
987 try
988 finally
989 endtry
990
991 if 1
992 else
993 try
994 endtry
995 endif
996enddef
997
998" The skipped try/endtry was updating the wrong instruction.
999def Test_try_catch_skipped()
1000 var instr = execute('disassemble Try_catch_skipped')
1001 assert_match("NEWLIST size 0\n", instr)
1002enddef
1003
1004
1005
Bram Moolenaar006ad482020-06-30 20:55:15 +02001006def Test_throw_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001007 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001008 var lines =<< trim END
Bram Moolenaar006ad482020-06-30 20:55:15 +02001009 vim9script
1010 try
1011 throw 'one'
1012 .. 'two'
1013 catch
1014 assert_equal('onetwo', v:exception)
1015 endtry
1016 END
1017 CheckScriptSuccess(lines)
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001018
1019 lines =<< trim END
1020 vim9script
Bram Moolenaar352134b2020-10-17 22:04:08 +02001021 @r = ''
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001022 def Func()
1023 throw @r
1024 enddef
1025 var result = ''
1026 try
1027 Func()
1028 catch /E1129:/
1029 result = 'caught'
1030 endtry
1031 assert_equal('caught', result)
1032 END
1033 CheckScriptSuccess(lines)
Bram Moolenaar006ad482020-06-30 20:55:15 +02001034enddef
1035
Bram Moolenaared677f52020-08-12 16:38:10 +02001036def Test_error_in_nested_function()
Bram Moolenaar03dfde22021-02-14 13:17:22 +01001037 # an error in a nested :function aborts executing in the calling :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001038 var lines =<< trim END
Bram Moolenaared677f52020-08-12 16:38:10 +02001039 vim9script
1040 def Func()
1041 Error()
1042 g:test_var = 1
1043 enddef
1044 func Error() abort
1045 eval [][0]
1046 endfunc
1047 Func()
1048 END
1049 g:test_var = 0
1050 CheckScriptFailure(lines, 'E684:')
1051 assert_equal(0, g:test_var)
1052enddef
1053
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001054def Test_abort_after_error()
1055 var lines =<< trim END
1056 vim9script
1057 while true
1058 echo notfound
1059 endwhile
1060 g:gotthere = true
1061 END
1062 g:gotthere = false
1063 CheckScriptFailure(lines, 'E121:')
1064 assert_false(g:gotthere)
1065 unlet g:gotthere
1066enddef
1067
Bram Moolenaar37c83712020-06-30 21:18:36 +02001068def Test_cexpr_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001069 # only checks line continuation
Bram Moolenaar37c83712020-06-30 21:18:36 +02001070 set errorformat=File\ %f\ line\ %l
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001071 var lines =<< trim END
Bram Moolenaar37c83712020-06-30 21:18:36 +02001072 vim9script
1073 cexpr 'File'
1074 .. ' someFile' ..
1075 ' line 19'
1076 assert_equal(19, getqflist()[0].lnum)
1077 END
1078 CheckScriptSuccess(lines)
1079 set errorformat&
1080enddef
1081
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +02001082def Test_statusline_syntax()
1083 # legacy syntax is used for 'statusline'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001084 var lines =<< trim END
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +02001085 vim9script
1086 func g:Status()
1087 return '%{"x" is# "x"}'
1088 endfunc
1089 set laststatus=2 statusline=%!Status()
1090 redrawstatus
1091 set laststatus statusline=
1092 END
1093 CheckScriptSuccess(lines)
1094enddef
1095
Bram Moolenaarb2097502020-07-19 17:17:02 +02001096def Test_list_vimscript()
1097 # checks line continuation and comments
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001098 var lines =<< trim END
Bram Moolenaarb2097502020-07-19 17:17:02 +02001099 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001100 var mylist = [
Bram Moolenaarb2097502020-07-19 17:17:02 +02001101 'one',
1102 # comment
1103 'two', # empty line follows
1104
1105 'three',
1106 ]
1107 assert_equal(['one', 'two', 'three'], mylist)
1108 END
1109 CheckScriptSuccess(lines)
Bram Moolenaar66250c92020-08-20 15:02:42 +02001110
1111 # check all lines from heredoc are kept
1112 lines =<< trim END
1113 # comment 1
1114 two
1115 # comment 3
1116
1117 five
1118 # comment 6
1119 END
1120 assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines)
Bram Moolenaar4bce26b2021-01-22 22:06:56 +01001121
1122 lines =<< trim END
1123 [{
1124 a: 0}]->string()->assert_equal("[{'a': 0}]")
1125 END
1126 CheckDefAndScriptSuccess(lines)
Bram Moolenaarb2097502020-07-19 17:17:02 +02001127enddef
1128
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001129if has('channel')
1130 let someJob = test_null_job()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001131
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001132 def FuncWithError()
1133 echomsg g:someJob
1134 enddef
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001135
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001136 func Test_convert_emsg_to_exception()
1137 try
1138 call FuncWithError()
1139 catch
1140 call assert_match('Vim:E908:', v:exception)
1141 endtry
1142 endfunc
1143endif
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001144
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145let s:export_script_lines =<< trim END
1146 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001147 var name: string = 'bob'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 def Concat(arg: string): string
1149 return name .. arg
1150 enddef
Bram Moolenaar227a69d2020-05-15 18:17:28 +02001151 g:result = Concat('bie')
1152 g:localname = name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153
1154 export const CONST = 1234
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001155 export var exported = 9876
1156 export var exp_name = 'John'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 export def Exported(): string
1158 return 'Exported'
1159 enddef
Bram Moolenaar24e93162021-07-18 20:40:33 +02001160 export def ExportedValue(): number
1161 return exported
1162 enddef
1163 export def ExportedInc()
1164 exported += 5
1165 enddef
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001166 export final theList = [1]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167END
1168
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001169def Undo_export_script_lines()
1170 unlet g:result
1171 unlet g:localname
1172enddef
1173
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001174def Test_vim9_import_export()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001175 writefile(s:export_script_lines, 'Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001176 var import_script_lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001178 var dir = './'
1179 var ext = ".vim"
1180 import dir .. 'Xexport' .. ext as expo
Bram Moolenaar24e93162021-07-18 20:40:33 +02001181
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001182 g:exported1 = expo.exported
1183 expo.exported += 3
1184 g:exported2 = expo.exported
1185 g:exported3 = expo.ExportedValue()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001186
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001187 expo.ExportedInc()
1188 g:exported_i1 = expo.exported
1189 g:exported_i2 = expo.ExportedValue()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001190
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001191 expo.exported = 11
1192 g:exported_s1 = expo.exported
1193 g:exported_s2 = expo.ExportedValue()
1194
1195 g:imported_func = expo.Exported()
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001196
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001197 def GetExported(): string
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001198 var local_dict = {ref: expo.Exported}
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001199 return local_dict.ref()
1200 enddef
1201 g:funcref_result = GetExported()
1202
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001203 g:imported_name = expo.exp_name
1204 expo.exp_name ..= ' Doe'
1205 g:imported_name_appended = expo.exp_name
1206 g:exported_later = expo.exported
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001207
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001208 expo.theList->add(2)
1209 assert_equal([1, 2], expo.theList)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 writefile(import_script_lines, 'Ximport.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 source Ximport.vim
1213
1214 assert_equal('bobbie', g:result)
1215 assert_equal('bob', g:localname)
Bram Moolenaar24e93162021-07-18 20:40:33 +02001216 assert_equal(9876, g:exported1)
1217 assert_equal(9879, g:exported2)
1218 assert_equal(9879, g:exported3)
1219
1220 assert_equal(9884, g:exported_i1)
1221 assert_equal(9884, g:exported_i2)
1222
1223 assert_equal(11, g:exported_s1)
1224 assert_equal(11, g:exported_s2)
1225 assert_equal(11, g:exported_later)
1226
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227 assert_equal('Exported', g:imported_func)
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001228 assert_equal('Exported', g:funcref_result)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001229 assert_equal('John', g:imported_name)
1230 assert_equal('John Doe', g:imported_name_appended)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231 assert_false(exists('g:name'))
1232
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001233 Undo_export_script_lines()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001234 unlet g:exported1
1235 unlet g:exported2
1236 unlet g:exported3
1237 unlet g:exported_i1
1238 unlet g:exported_i2
1239 unlet g:exported_later
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 unlet g:imported_func
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001241 unlet g:imported_name g:imported_name_appended
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242 delete('Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001243
Bram Moolenaar1c991142020-07-04 13:15:31 +02001244 # similar, with line breaks
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001245 var import_line_break_script_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001246 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001247 import './Xexport.vim'
1248 as expo
1249 g:exported = expo.exported
1250 expo.exported += 7
1251 g:exported_added = expo.exported
1252 g:imported_func = expo.Exported()
Bram Moolenaar1c991142020-07-04 13:15:31 +02001253 END
1254 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
1255 source Ximport_lbr.vim
1256
Bram Moolenaar24e93162021-07-18 20:40:33 +02001257 assert_equal(11, g:exported)
1258 assert_equal(18, g:exported_added)
Bram Moolenaar1c991142020-07-04 13:15:31 +02001259 assert_equal('Exported', g:imported_func)
1260
1261 # exported script not sourced again
1262 assert_false(exists('g:result'))
Bram Moolenaar24e93162021-07-18 20:40:33 +02001263 unlet g:exported
1264 unlet g:exported_added
Bram Moolenaar1c991142020-07-04 13:15:31 +02001265 unlet g:imported_func
1266 delete('Ximport_lbr.vim')
1267
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001268 var line_break_before_dot =<< trim END
1269 vim9script
1270 import './Xexport.vim' as expo
1271 g:exported = expo
1272 .exported
1273 END
1274 writefile(line_break_before_dot, 'Ximport_lbr_before_dot.vim')
1275 assert_fails('source Ximport_lbr_before_dot.vim', 'E1060:', '', 3)
1276 delete('Ximport_lbr_before_dot.vim')
1277
1278 var line_break_after_dot =<< trim END
1279 vim9script
1280 import './Xexport.vim' as expo
1281 g:exported = expo.
1282 exported
1283 END
1284 writefile(line_break_after_dot, 'Ximport_lbr_after_dot.vim')
1285 assert_fails('source Ximport_lbr_after_dot.vim', 'E1074:', '', 3)
1286 delete('Ximport_lbr_after_dot.vim')
1287
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001288 var import_star_as_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001289 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001290 import './Xexport.vim' as Export
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001291 def UseExport()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001292 g:exported_def = Export.exported
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001293 enddef
Bram Moolenaar24e93162021-07-18 20:40:33 +02001294 g:exported_script = Export.exported
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001295 assert_equal(1, exists('Export.exported'))
1296 assert_equal(0, exists('Export.notexported'))
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001297 UseExport()
1298 END
1299 writefile(import_star_as_lines, 'Ximport.vim')
1300 source Ximport.vim
Bram Moolenaar24e93162021-07-18 20:40:33 +02001301
1302 assert_equal(18, g:exported_def)
1303 assert_equal(18, g:exported_script)
1304 unlet g:exported_def
1305 unlet g:exported_script
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001306
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001307 var import_star_as_lines_no_dot =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001308 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001309 import './Xexport.vim' as Export
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001310 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001311 var dummy = 1
1312 var imported = Export + dummy
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001313 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001314 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001315 END
1316 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001317 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001318
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001319 var import_star_as_lines_dot_space =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001320 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001321 import './Xexport.vim' as Export
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001322 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001323 var imported = Export . exported
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001324 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001325 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001326 END
1327 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001328 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001329
Bram Moolenaar921ba522021-07-29 22:25:05 +02001330 var import_func_duplicated =<< trim END
1331 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001332 import './Xexport.vim' as expo
1333 import './Xexport.vim' as expo
Bram Moolenaar921ba522021-07-29 22:25:05 +02001334
1335 ExportedInc()
1336 END
1337 writefile(import_func_duplicated, 'Ximport.vim')
1338 assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
1339
Bram Moolenaara6294952020-12-27 13:39:50 +01001340 var import_star_as_duplicated =<< trim END
1341 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001342 import './Xexport.vim' as Export
Bram Moolenaara6294952020-12-27 13:39:50 +01001343 var some = 'other'
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001344 import './Xexport.vim' as Export
Bram Moolenaara6294952020-12-27 13:39:50 +01001345 defcompile
1346 END
1347 writefile(import_star_as_duplicated, 'Ximport.vim')
1348 assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim')
1349
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001350 var import_star_as_lines_script_no_dot =<< trim END
1351 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001352 import './Xexport.vim' as Export
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001353 g:imported_script = Export exported
1354 END
1355 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001356 assert_fails('source Ximport.vim', 'E1060: Expected dot after name: Export exported')
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001357
1358 var import_star_as_lines_script_space_after_dot =<< trim END
1359 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001360 import './Xexport.vim' as Export
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001361 g:imported_script = Export. exported
1362 END
1363 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
1364 assert_fails('source Ximport.vim', 'E1074:')
1365
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001366 var import_star_as_lines_missing_name =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001367 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001368 import './Xexport.vim' as Export
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001369 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001370 var imported = Export.
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001371 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001372 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001373 END
1374 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001375 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001376
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001377 var import_star_as_lbr_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001378 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001379 import './Xexport.vim'
Bram Moolenaar1c991142020-07-04 13:15:31 +02001380 as Export
Bram Moolenaar1c991142020-07-04 13:15:31 +02001381 def UseExport()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001382 g:exported = Export.exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001383 enddef
1384 UseExport()
1385 END
1386 writefile(import_star_as_lbr_lines, 'Ximport.vim')
1387 source Ximport.vim
Bram Moolenaar24e93162021-07-18 20:40:33 +02001388 assert_equal(18, g:exported)
1389 unlet g:exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001390
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001391 # try to use something that exists but is not exported
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001392 var import_not_exported_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001393 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001394 import './Xexport.vim' as expo
1395 echo expo.name
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001396 END
1397 writefile(import_not_exported_lines, 'Ximport.vim')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001398 assert_fails('source Ximport.vim', 'E1049:', '', 3, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001399
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001400 # try to import something that is already defined
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001401 var import_already_defined =<< trim END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001402 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001403 var exported = 'something'
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001404 import './Xexport.vim' as exported
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001405 END
1406 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001407 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001408
Bram Moolenaar918a4242020-12-06 14:37:08 +01001409 # try changing an imported const
1410 var import_assign_to_const =<< trim END
1411 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001412 import './Xexport.vim' as expo
Bram Moolenaar918a4242020-12-06 14:37:08 +01001413 def Assign()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001414 expo.CONST = 987
Bram Moolenaar918a4242020-12-06 14:37:08 +01001415 enddef
1416 defcompile
1417 END
1418 writefile(import_assign_to_const, 'Ximport.vim')
1419 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1420
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001421 # try changing an imported final
1422 var import_assign_to_final =<< trim END
1423 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001424 import './Xexport.vim' as expo
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001425 def Assign()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001426 expo.theList = [2]
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001427 enddef
1428 defcompile
1429 END
1430 writefile(import_assign_to_final, 'Ximport.vim')
1431 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1432
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001433 var import_no_as_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001434 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001435 import './Xexport.vim' name
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001436 END
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001437 writefile(import_no_as_lines, 'Ximport.vim')
1438 assert_fails('source Ximport.vim', 'E488:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001439
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001440 var import_invalid_string_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001441 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001442 import Xexport.vim
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001443 END
1444 writefile(import_invalid_string_lines, 'Ximport.vim')
Bram Moolenaar4db572e2021-07-18 18:21:38 +02001445 assert_fails('source Ximport.vim', 'E121:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001446
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001447 var import_wrong_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001448 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001449 import './XnoExport.vim'
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001450 END
1451 writefile(import_wrong_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001452 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001453
Bram Moolenaar60579352021-07-19 21:45:07 +02001454 var import_redefining_lines =<< trim END
1455 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001456 import './Xexport.vim' as exported
Bram Moolenaar60579352021-07-19 21:45:07 +02001457 var exported = 5
1458 END
1459 writefile(import_redefining_lines, 'Ximport.vim')
1460 assert_fails('source Ximport.vim', 'E1213: Redefining imported item "exported"', '', 3)
1461
1462 var import_assign_wrong_type_lines =<< trim END
1463 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001464 import './Xexport.vim' as expo
1465 expo.exported = 'xxx'
Bram Moolenaar60579352021-07-19 21:45:07 +02001466 END
1467 writefile(import_assign_wrong_type_lines, 'Ximport.vim')
1468 assert_fails('source Ximport.vim', 'E1012: Type mismatch; expected number but got string', '', 3)
1469
1470 var import_assign_const_lines =<< trim END
1471 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001472 import './Xexport.vim' as expo
1473 expo.CONST = 4321
Bram Moolenaar60579352021-07-19 21:45:07 +02001474 END
1475 writefile(import_assign_const_lines, 'Ximport.vim')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001476 assert_fails('source Ximport.vim', 'E46: Cannot change read-only variable "CONST"', '', 3)
Bram Moolenaar60579352021-07-19 21:45:07 +02001477
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001478 delete('Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001479 delete('Ximport3.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480 delete('Xexport.vim')
1481
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001482 # Check that in a Vim9 script 'cpo' is set to the Vim default.
Bram Moolenaar3e191692021-03-17 17:46:00 +01001483 # Flags added or removed are also applied to the restored value.
1484 set cpo=abcd
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001485 var lines =<< trim END
Bram Moolenaar750802b2020-02-23 18:08:33 +01001486 vim9script
1487 g:cpo_in_vim9script = &cpo
Bram Moolenaar3e191692021-03-17 17:46:00 +01001488 set cpo+=f
1489 set cpo-=c
1490 g:cpo_after_vim9script = &cpo
Bram Moolenaar750802b2020-02-23 18:08:33 +01001491 END
1492 writefile(lines, 'Xvim9_script')
1493 source Xvim9_script
Bram Moolenaar3e191692021-03-17 17:46:00 +01001494 assert_equal('fabd', &cpo)
Bram Moolenaar750802b2020-02-23 18:08:33 +01001495 set cpo&vim
1496 assert_equal(&cpo, g:cpo_in_vim9script)
Bram Moolenaar3e191692021-03-17 17:46:00 +01001497 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
1498 assert_equal(newcpo, g:cpo_after_vim9script)
1499
Bram Moolenaar750802b2020-02-23 18:08:33 +01001500 delete('Xvim9_script')
1501enddef
1502
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001503def Test_import_funcref()
1504 var lines =<< trim END
1505 vim9script
1506 export def F(): number
1507 return 42
1508 enddef
1509 export const G = F
1510 END
1511 writefile(lines, 'Xlib.vim')
1512
1513 lines =<< trim END
1514 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001515 import './Xlib.vim' as lib
1516 const Foo = lib.G()
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001517 assert_equal(42, Foo)
1518
1519 def DoTest()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001520 const Goo = lib.G()
Bram Moolenaar06ca48a2021-10-23 10:25:21 +01001521 assert_equal(42, Goo)
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001522 enddef
1523 DoTest()
1524 END
1525 CheckScriptSuccess(lines)
1526
1527 delete('Xlib.vim')
1528enddef
1529
Bram Moolenaar6853c382021-09-07 22:12:19 +02001530def Test_import_star_fails()
1531 writefile([], 'Xfoo.vim')
1532 var lines =<< trim END
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001533 import './Xfoo.vim' as foo
Bram Moolenaar6853c382021-09-07 22:12:19 +02001534 foo = 'bar'
1535 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00001536 CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use foo itself'])
Bram Moolenaar6853c382021-09-07 22:12:19 +02001537 lines =<< trim END
1538 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001539 import './Xfoo.vim' as foo
Bram Moolenaar6853c382021-09-07 22:12:19 +02001540 var that = foo
1541 END
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001542 CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaara9e3d562021-09-08 12:31:35 +02001543
1544 lines =<< trim END
1545 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001546 import './Xfoo.vim' as 9foo
Bram Moolenaara9e3d562021-09-08 12:31:35 +02001547 END
1548 CheckScriptFailure(lines, 'E1047:')
1549 lines =<< trim END
1550 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001551 import './Xfoo.vim' as the#foo
Bram Moolenaara9e3d562021-09-08 12:31:35 +02001552 END
1553 CheckScriptFailure(lines, 'E1047:')
1554 lines =<< trim END
1555 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001556 import './Xfoo.vim' as g:foo
Bram Moolenaara9e3d562021-09-08 12:31:35 +02001557 END
1558 CheckScriptFailure(lines, 'E1047:')
1559
Bram Moolenaaraf2d5d22021-09-07 22:35:34 +02001560 delete('Xfoo.vim')
Bram Moolenaarf8a79fc2021-12-14 12:06:16 +00001561
1562 lines =<< trim END
1563 vim9script
1564 def TheFunc()
1565 echo 'the func'
1566 enddef
1567 export var Ref = TheFunc
1568 END
1569 writefile([], 'Xthat.vim')
1570 lines =<< trim END
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001571 import './Xthat.vim' as That
Bram Moolenaarf8a79fc2021-12-14 12:06:16 +00001572 That()
1573 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00001574 CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use That itself'])
Bram Moolenaarf8a79fc2021-12-14 12:06:16 +00001575 delete('Xthat.vim')
Bram Moolenaar6853c382021-09-07 22:12:19 +02001576enddef
1577
Bram Moolenaar803af682020-08-05 16:20:03 +02001578func g:Trigger()
1579 source Ximport.vim
1580 return "echo 'yes'\<CR>"
1581endfunc
1582
1583def Test_import_export_expr_map()
1584 # check that :import and :export work when buffer is locked
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001585 var export_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001586 vim9script
1587 export def That(): string
1588 return 'yes'
1589 enddef
1590 END
1591 writefile(export_lines, 'Xexport_that.vim')
1592
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001593 var import_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001594 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001595 import './Xexport_that.vim' as that
1596 assert_equal('yes', that.That())
Bram Moolenaar803af682020-08-05 16:20:03 +02001597 END
1598 writefile(import_lines, 'Ximport.vim')
1599
1600 nnoremap <expr> trigger g:Trigger()
1601 feedkeys('trigger', "xt")
1602
Bram Moolenaar730b2482020-08-09 13:02:10 +02001603 delete('Xexport_that.vim')
Bram Moolenaar803af682020-08-05 16:20:03 +02001604 delete('Ximport.vim')
1605 nunmap trigger
1606enddef
1607
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001608def Test_import_in_filetype()
1609 # check that :import works when the buffer is locked
1610 mkdir('ftplugin', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001611 var export_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001612 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001613 export var That = 'yes'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001614 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001615 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001616
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001617 var import_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001618 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001619 import './Xexport_ft.vim' as ft
1620 assert_equal('yes', ft.That)
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001621 g:did_load_mytpe = 1
1622 END
1623 writefile(import_lines, 'ftplugin/qf.vim')
1624
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001625 var save_rtp = &rtp
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001626 &rtp = getcwd() .. ',' .. &rtp
1627
1628 filetype plugin on
1629 copen
1630 assert_equal(1, g:did_load_mytpe)
1631
1632 quit!
Bram Moolenaar730b2482020-08-09 13:02:10 +02001633 delete('Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001634 delete('ftplugin', 'rf')
1635 &rtp = save_rtp
1636enddef
1637
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001638" FIXME
1639"def Test_use_import_in_mapping()
1640" var lines =<< trim END
1641" vim9script
1642" export def Funcx()
1643" g:result = 42
1644" enddef
1645" END
1646" writefile(lines, 'XsomeExport.vim')
1647" lines =<< trim END
1648" vim9script
1649" import './XsomeExport.vim' as some
1650" var Funcy = some.Funcx
1651" nnoremap <F3> :call <sid>Funcy()<cr>
1652" END
1653" writefile(lines, 'Xmapscript.vim')
1654"
1655" source Xmapscript.vim
1656" feedkeys("\<F3>", "xt")
1657" assert_equal(42, g:result)
1658"
1659" unlet g:result
1660" delete('XsomeExport.vim')
1661" delete('Xmapscript.vim')
1662" nunmap <F3>
1663"enddef
Bram Moolenaarefa94442020-08-08 22:16:00 +02001664
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01001665def Test_vim9script_mix()
1666 var lines =<< trim END
1667 if has(g:feature)
1668 " legacy script
1669 let g:legacy = 1
1670 finish
1671 endif
1672 vim9script
1673 g:legacy = 0
1674 END
1675 g:feature = 'eval'
1676 g:legacy = -1
1677 CheckScriptSuccess(lines)
1678 assert_equal(1, g:legacy)
1679
1680 g:feature = 'noteval'
1681 g:legacy = -1
1682 CheckScriptSuccess(lines)
1683 assert_equal(0, g:legacy)
1684enddef
1685
Bram Moolenaar750802b2020-02-23 18:08:33 +01001686def Test_vim9script_fails()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1688 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001689 CheckScriptFailure(['export var some = 123'], 'E1042:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001690 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
Bram Moolenaar750802b2020-02-23 18:08:33 +01001691 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
1692
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001693 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001694 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1695
Bram Moolenaare2e40752020-09-04 21:18:46 +02001696 assert_fails('vim9script', 'E1038:')
1697 assert_fails('export something', 'E1043:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698enddef
1699
Bram Moolenaarf7d267e2020-06-17 12:04:54 +02001700func Test_import_fails_without_script()
Bram Moolenaar101f4812020-06-16 23:18:51 +02001701 CheckRunVimInTerminal
1702
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001703 " call indirectly to avoid compilation error for missing functions
Bram Moolenaarc620c052020-07-08 15:16:19 +02001704 call Run_Test_import_fails_on_command_line()
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001705endfunc
1706
Bram Moolenaarc620c052020-07-08 15:16:19 +02001707def Run_Test_import_fails_on_command_line()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001708 var export =<< trim END
Bram Moolenaar101f4812020-06-16 23:18:51 +02001709 vim9script
1710 export def Foo(): number
1711 return 0
1712 enddef
1713 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001714 writefile(export, 'XexportCmd.vim')
Bram Moolenaar101f4812020-06-16 23:18:51 +02001715
Bram Moolenaare0de1712020-12-02 17:36:54 +01001716 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001717 rows: 6, wait_for_ruler: 0})
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001718 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaar101f4812020-06-16 23:18:51 +02001719
Bram Moolenaar730b2482020-08-09 13:02:10 +02001720 delete('XexportCmd.vim')
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001721 StopVimInTerminal(buf)
1722enddef
Bram Moolenaar101f4812020-06-16 23:18:51 +02001723
Bram Moolenaar2b327002020-12-26 15:39:31 +01001724def Test_vim9script_reload_noclear()
1725 var lines =<< trim END
Bram Moolenaara6294952020-12-27 13:39:50 +01001726 vim9script
1727 export var exported = 'thexport'
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001728
1729 export def TheFunc(x = 0)
1730 enddef
Bram Moolenaara6294952020-12-27 13:39:50 +01001731 END
1732 writefile(lines, 'XExportReload')
1733 lines =<< trim END
Bram Moolenaar2b327002020-12-26 15:39:31 +01001734 vim9script noclear
1735 g:loadCount += 1
1736 var s:reloaded = 'init'
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001737 import './XExportReload' as exp
Bram Moolenaar2b327002020-12-26 15:39:31 +01001738
1739 def Again(): string
1740 return 'again'
1741 enddef
1742
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001743 exp.TheFunc()
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001744
Bram Moolenaar2b327002020-12-26 15:39:31 +01001745 if exists('s:loaded') | finish | endif
1746 var s:loaded = true
1747
1748 var s:notReloaded = 'yes'
1749 s:reloaded = 'first'
1750 def g:Values(): list<string>
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001751 return [s:reloaded, s:notReloaded, Again(), Once(), exp.exported]
Bram Moolenaar2b327002020-12-26 15:39:31 +01001752 enddef
1753
1754 def Once(): string
1755 return 'once'
1756 enddef
1757 END
1758 writefile(lines, 'XReloaded')
1759 g:loadCount = 0
1760 source XReloaded
1761 assert_equal(1, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001762 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001763 source XReloaded
1764 assert_equal(2, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001765 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001766 source XReloaded
1767 assert_equal(3, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001768 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001769
Bram Moolenaar48e11c12021-01-11 18:47:00 +01001770 delete('XReloaded')
Bram Moolenaara6294952020-12-27 13:39:50 +01001771 delete('XExportReload')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001772 delfunc g:Values
Bram Moolenaar2b327002020-12-26 15:39:31 +01001773 unlet g:loadCount
Bram Moolenaar577dc932021-06-27 15:35:40 +02001774
1775 lines =<< trim END
1776 vim9script
1777 def Inner()
1778 enddef
1779 END
1780 lines->writefile('XreloadScript.vim')
1781 source XreloadScript.vim
1782
1783 lines =<< trim END
1784 vim9script
1785 def Outer()
1786 def Inner()
1787 enddef
1788 enddef
1789 defcompile
1790 END
1791 lines->writefile('XreloadScript.vim')
1792 source XreloadScript.vim
1793
1794 delete('XreloadScript.vim')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001795enddef
1796
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001797def Test_vim9script_reload_import()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001798 var lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799 vim9script
1800 const var = ''
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001801 var valone = 1234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 def MyFunc(arg: string)
1803 valone = 5678
1804 enddef
1805 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001806 var morelines =<< trim END
1807 var valtwo = 222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 export def GetValtwo(): number
1809 return valtwo
1810 enddef
1811 END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001812 writefile(lines + morelines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813 source Xreload.vim
1814 source Xreload.vim
1815 source Xreload.vim
1816
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001817 # cannot declare a var twice
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 lines =<< trim END
1819 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001820 var valone = 1234
1821 var valone = 5678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 END
1823 writefile(lines, 'Xreload.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001824 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825
1826 delete('Xreload.vim')
1827 delete('Ximport.vim')
1828enddef
1829
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001830" if a script is reloaded with a script-local variable that changed its type, a
1831" compiled function using that variable must fail.
1832def Test_script_reload_change_type()
1833 var lines =<< trim END
1834 vim9script noclear
1835 var str = 'string'
1836 def g:GetStr(): string
1837 return str .. 'xxx'
1838 enddef
1839 END
1840 writefile(lines, 'Xreload.vim')
1841 source Xreload.vim
1842 echo g:GetStr()
1843
1844 lines =<< trim END
1845 vim9script noclear
1846 var str = 1234
1847 END
1848 writefile(lines, 'Xreload.vim')
1849 source Xreload.vim
1850 assert_fails('echo g:GetStr()', 'E1150:')
1851
1852 delfunc g:GetStr
1853 delete('Xreload.vim')
1854enddef
1855
Bram Moolenaarc970e422021-03-17 15:03:04 +01001856" Define CallFunc so that the test can be compiled
1857command CallFunc echo 'nop'
1858
1859def Test_script_reload_from_function()
1860 var lines =<< trim END
1861 vim9script
1862
1863 if exists('g:loaded')
1864 finish
1865 endif
1866 g:loaded = 1
1867 delcommand CallFunc
1868 command CallFunc Func()
1869 def Func()
Bram Moolenaara4c81be2021-03-17 15:23:16 +01001870 so XreloadFunc.vim
Bram Moolenaarc970e422021-03-17 15:03:04 +01001871 g:didTheFunc = 1
1872 enddef
1873 END
1874 writefile(lines, 'XreloadFunc.vim')
1875 source XreloadFunc.vim
1876 CallFunc
1877 assert_equal(1, g:didTheFunc)
1878
1879 delete('XreloadFunc.vim')
1880 delcommand CallFunc
1881 unlet g:loaded
1882 unlet g:didTheFunc
1883enddef
1884
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001885def Test_script_var_shadows_function()
1886 var lines =<< trim END
1887 vim9script
1888 def Func(): number
1889 return 123
1890 enddef
1891 var Func = 1
1892 END
1893 CheckScriptFailure(lines, 'E1041:', 5)
1894enddef
1895
Bram Moolenaar052ff292021-12-11 13:54:46 +00001896def Test_function_shadows_script_var()
1897 var lines =<< trim END
1898 vim9script
1899 var Func = 1
1900 def Func(): number
1901 return 123
1902 enddef
1903 END
1904 CheckScriptFailure(lines, 'E1041:', 3)
1905enddef
1906
Bram Moolenaarc3235272021-07-10 19:42:03 +02001907def Test_script_var_shadows_command()
1908 var lines =<< trim END
1909 var undo = 1
1910 undo = 2
1911 assert_equal(2, undo)
1912 END
1913 CheckDefAndScriptSuccess(lines)
1914
1915 lines =<< trim END
1916 var undo = 1
1917 undo
1918 END
1919 CheckDefAndScriptFailure(lines, 'E1207:', 2)
1920enddef
1921
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001922def Test_vim9script_call_wrong_type()
1923 var lines =<< trim END
1924 vim9script
1925 var Time = 'localtime'
1926 Time()
1927 END
1928 CheckScriptFailure(lines, 'E1085:')
1929enddef
1930
Bram Moolenaar95006e32020-08-29 17:47:08 +02001931def s:RetSome(): string
1932 return 'some'
1933enddef
1934
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001935" Not exported function that is referenced needs to be accessed by the
1936" script-local name.
1937def Test_vim9script_funcref()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001938 var sortlines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001939 vim9script
1940 def Compare(i1: number, i2: number): number
Bram Moolenaarbed36b92020-07-07 23:31:36 +02001941 return i2 - i1
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001942 enddef
1943
1944 export def FastSort(): list<number>
1945 return range(5)->sort(Compare)
1946 enddef
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001947
1948 export def GetString(arg: string): string
1949 return arg
1950 enddef
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001951 END
1952 writefile(sortlines, 'Xsort.vim')
1953
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001954 var lines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001955 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001956 import './Xsort.vim'
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001957 def Test()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001958 g:result = Xsort.FastSort()
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001959 enddef
1960 Test()
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001961
1962 # using a function imported with "as"
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001963 import './Xsort.vim' as anAlias
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001964 assert_equal('yes', anAlias.GetString('yes'))
1965
1966 # using the function from a compiled function
1967 def TestMore(): string
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001968 var s = s:anAlias.GetString('foo')
1969 return s .. anAlias.GetString('bar')
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001970 enddef
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001971 assert_equal('foobar', TestMore())
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001972
1973 # error when using a function that isn't exported
1974 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001975 END
1976 writefile(lines, 'Xscript.vim')
1977
1978 source Xscript.vim
1979 assert_equal([4, 3, 2, 1, 0], g:result)
1980
1981 unlet g:result
1982 delete('Xsort.vim')
1983 delete('Xscript.vim')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001984
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001985 var Funcref = function('s:RetSome')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001986 assert_equal('some', Funcref())
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001987enddef
1988
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001989" Check that when searching for "FilterFunc" it finds the import in the
1990" script where FastFilter() is called from, both as a string and as a direct
1991" function reference.
Bram Moolenaarc620c052020-07-08 15:16:19 +02001992def Test_vim9script_funcref_other_script()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001993 var filterLines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001994 vim9script
1995 export def FilterFunc(idx: number, val: number): bool
1996 return idx % 2 == 1
1997 enddef
1998 export def FastFilter(): list<number>
Bram Moolenaar18024052021-12-25 21:43:28 +00001999 return range(10)->filter('FilterFunc(v:key, v:val)')
Bram Moolenaarc620c052020-07-08 15:16:19 +02002000 enddef
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002001 export def FastFilterDirect(): list<number>
2002 return range(10)->filter(FilterFunc)
2003 enddef
Bram Moolenaarc620c052020-07-08 15:16:19 +02002004 END
2005 writefile(filterLines, 'Xfilter.vim')
2006
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002007 var lines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02002008 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002009 import './Xfilter.vim' as filter
Bram Moolenaarc620c052020-07-08 15:16:19 +02002010 def Test()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002011 var x: list<number> = filter.FastFilter()
Bram Moolenaarc620c052020-07-08 15:16:19 +02002012 enddef
2013 Test()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002014 def TestDirect()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002015 var x: list<number> = filter.FastFilterDirect()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002016 enddef
2017 TestDirect()
Bram Moolenaarc620c052020-07-08 15:16:19 +02002018 END
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002019 CheckScriptSuccess(lines)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002020 delete('Xfilter.vim')
Bram Moolenaarc620c052020-07-08 15:16:19 +02002021enddef
2022
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002023def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002024 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002025 vim9script
2026 def FuncYes(): string
2027 return 'yes'
2028 enddef
2029 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002030 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002031 def FuncNo(): string
2032 return 'no'
2033 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002034 def g:DoCheck(no_exists: bool)
2035 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002036 assert_equal('no', FuncNo())
2037 enddef
2038 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002039 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002040 def g:DoCheck(no_exists: bool)
2041 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02002042 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002043 enddef
2044 END
2045
2046 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002047 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002048 source Xreloaded.vim
2049 g:DoCheck(true)
2050
2051 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002052 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002053 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01002054 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002055
2056 # FuncNo() is back
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002057 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002058 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01002059 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002060
2061 delete('Xreloaded.vim')
2062enddef
2063
Bram Moolenaar89483d42020-05-10 15:24:44 +02002064def Test_vim9script_reload_delvar()
2065 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002066 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02002067 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002068 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02002069 END
2070 writefile(lines, 'XreloadVar.vim')
2071 source XreloadVar.vim
2072
2073 # now write the script using the same variable locally - works
2074 lines =<< trim END
2075 vim9script
2076 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002077 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02002078 enddef
2079 END
2080 writefile(lines, 'XreloadVar.vim')
2081 source XreloadVar.vim
2082
2083 delete('XreloadVar.vim')
2084enddef
2085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086def Test_import_absolute()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002087 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02002088 'vim9script',
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002089 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
Bram Moolenaare6085c52020-04-12 20:19:16 +02002090 'def UseExported()',
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002091 ' g:imported_abs = abs.exported',
2092 ' abs.exported = 8888',
2093 ' g:imported_after = abs.exported',
Bram Moolenaare6085c52020-04-12 20:19:16 +02002094 'enddef',
2095 'UseExported()',
2096 'g:import_disassembled = execute("disass UseExported")',
2097 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 writefile(import_lines, 'Ximport_abs.vim')
2099 writefile(s:export_script_lines, 'Xexport_abs.vim')
2100
2101 source Ximport_abs.vim
2102
2103 assert_equal(9876, g:imported_abs)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002104 assert_equal(8888, g:imported_after)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002105 assert_match('<SNR>\d\+_UseExported\_s*' ..
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002106 'g:imported_abs = abs.exported\_s*' ..
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002107 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
2108 '1 STOREG g:imported_abs\_s*' ..
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002109 'abs.exported = 8888\_s*' ..
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002110 '2 PUSHNR 8888\_s*' ..
2111 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002112 'g:imported_after = abs.exported\_s*' ..
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002113 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
2114 '5 STOREG g:imported_after',
Bram Moolenaare6085c52020-04-12 20:19:16 +02002115 g:import_disassembled)
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002116
2117 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 unlet g:imported_abs
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002119 unlet g:import_disassembled
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120
2121 delete('Ximport_abs.vim')
2122 delete('Xexport_abs.vim')
2123enddef
2124
2125def Test_import_rtp()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002126 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02002127 'vim9script',
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002128 'import "Xexport_rtp.vim" as rtp',
2129 'g:imported_rtp = rtp.exported',
Bram Moolenaare6085c52020-04-12 20:19:16 +02002130 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 writefile(import_lines, 'Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002132 mkdir('import', 'p')
2133 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002135 var save_rtp = &rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 &rtp = getcwd()
2137 source Ximport_rtp.vim
2138 &rtp = save_rtp
2139
2140 assert_equal(9876, g:imported_rtp)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002142 Undo_export_script_lines()
2143 unlet g:imported_rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144 delete('Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002145 delete('import', 'rf')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146enddef
2147
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002148def Test_import_compile_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002149 var export_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002150 'vim9script',
2151 'export def ExpFunc(): string',
2152 ' return notDefined',
2153 'enddef',
2154 ]
2155 writefile(export_lines, 'Xexported.vim')
2156
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002157 var import_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002158 'vim9script',
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002159 'import "./Xexported.vim" as expo',
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002160 'def ImpFunc()',
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002161 ' echo expo.ExpFunc()',
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002162 'enddef',
2163 'defcompile',
2164 ]
2165 writefile(import_lines, 'Ximport.vim')
2166
2167 try
2168 source Ximport.vim
2169 catch /E1001/
Dominique Pelle923dce22021-11-21 11:36:04 +00002170 # Error should be before the Xexported.vim file.
Bram Moolenaar77072282020-09-16 17:55:40 +02002171 assert_match('E1001: Variable not found: notDefined', v:exception)
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002172 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
2173 endtry
2174
2175 delete('Xexported.vim')
2176 delete('Ximport.vim')
2177enddef
2178
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002179def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002180 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002181 'vim9script',
2182 'def Func()',
2183 ' eval [][0]',
2184 'enddef',
2185 'Func()',
2186 ]
2187 writefile(lines, 'Xtestscript.vim')
2188
2189 for count in range(3)
2190 try
2191 source Xtestscript.vim
2192 catch /E684/
2193 # function name should contain <SNR> every time
2194 assert_match('E684: list index out of range', v:exception)
2195 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
2196 endtry
2197 endfor
2198
2199 delete('Xtestscript.vim')
2200enddef
2201
Bram Moolenaareef21022020-08-01 22:16:43 +02002202def Test_func_overrules_import_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002203 var export_lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002204 vim9script
2205 export def Func()
2206 echo 'imported'
2207 enddef
2208 END
2209 writefile(export_lines, 'XexportedFunc.vim')
2210
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002211 var lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002212 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002213 import './XexportedFunc.vim' as Func
Bram Moolenaareef21022020-08-01 22:16:43 +02002214 def Func()
2215 echo 'local to function'
2216 enddef
2217 END
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002218 CheckScriptFailure(lines, 'E1236:')
Bram Moolenaareef21022020-08-01 22:16:43 +02002219
2220 lines =<< trim END
2221 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002222 import './XexportedFunc.vim' as Func
Bram Moolenaareef21022020-08-01 22:16:43 +02002223 def Outer()
2224 def Func()
2225 echo 'local to function'
2226 enddef
2227 enddef
2228 defcompile
2229 END
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002230 CheckScriptFailure(lines, 'E1236:')
Bram Moolenaareef21022020-08-01 22:16:43 +02002231
2232 delete('XexportedFunc.vim')
2233enddef
2234
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002235def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002236 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002237 vim9script
2238 def Func()
2239 echo 'one'
2240 enddef
2241 def Func()
2242 echo 'two'
2243 enddef
2244 END
2245 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002246
2247 lines =<< trim END
2248 vim9script
2249 def Foo(): string
2250 return 'foo'
Bram Moolenaar052ff292021-12-11 13:54:46 +00002251 enddef
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002252 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002253 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002254 enddef
2255 defcompile
2256 END
2257 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002258enddef
2259
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002261 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002262 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 l->remove(0)
2264 l->add(5)
2265 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002266 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267enddef
2268
Bram Moolenaarae616492020-07-28 20:07:27 +02002269def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002270 CheckDefExecFailure(['a = 1'], 'E1100:')
2271 CheckDefExecFailure(['c = 1'], 'E1100:')
2272 CheckDefExecFailure(['i = 1'], 'E1100:')
2273 CheckDefExecFailure(['t = 1'], 'E1100:')
2274 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002275
Bram Moolenaarae616492020-07-28 20:07:27 +02002276 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
2277 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002278 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
2279 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002280 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
2281 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01002282 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
2283 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002284 CheckScriptFailure(['vim9script', 't'], 'E1100:')
2285 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
2286 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002287enddef
2288
Bram Moolenaar158906c2020-02-06 20:39:45 +01002289def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002290 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01002291 if what == 1
2292 res = "one"
2293 elseif what == 2
2294 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002295 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01002296 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002297 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01002298 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002299enddef
2300
Bram Moolenaar158906c2020-02-06 20:39:45 +01002301def Test_if_elseif_else()
2302 assert_equal('one', IfElse(1))
2303 assert_equal('two', IfElse(2))
2304 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002305enddef
2306
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002307def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002308 CheckDefFailure(['elseif true'], 'E582:')
2309 CheckDefFailure(['else'], 'E581:')
2310 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01002311 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002312 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002313
2314 var lines =<< trim END
2315 var s = ''
2316 if s = ''
2317 endif
2318 END
2319 CheckDefFailure(lines, 'E488:')
2320
2321 lines =<< trim END
2322 var s = ''
2323 if s == ''
2324 elseif s = ''
2325 endif
2326 END
2327 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002328enddef
2329
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002330let g:bool_true = v:true
2331let g:bool_false = v:false
2332
2333def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002334 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002335 if true ? true : false
2336 res = true
2337 endif
2338 assert_equal(true, res)
2339
Bram Moolenaar585fea72020-04-02 22:33:21 +02002340 g:glob = 2
2341 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02002342 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002343 endif
2344 assert_equal(2, g:glob)
2345 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02002346 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002347 endif
2348 assert_equal(3, g:glob)
2349
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002350 res = false
2351 if g:bool_true ? true : false
2352 res = true
2353 endif
2354 assert_equal(true, res)
2355
2356 res = false
2357 if true ? g:bool_true : false
2358 res = true
2359 endif
2360 assert_equal(true, res)
2361
2362 res = false
2363 if true ? true : g:bool_false
2364 res = true
2365 endif
2366 assert_equal(true, res)
2367
2368 res = false
2369 if true ? false : true
2370 res = true
2371 endif
2372 assert_equal(false, res)
2373
2374 res = false
2375 if false ? false : true
2376 res = true
2377 endif
2378 assert_equal(true, res)
2379
2380 res = false
2381 if false ? true : false
2382 res = true
2383 endif
2384 assert_equal(false, res)
2385
2386 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002387 if has('xyz') ? true : false
2388 res = true
2389 endif
2390 assert_equal(false, res)
2391
2392 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002393 if true && true
2394 res = true
2395 endif
2396 assert_equal(true, res)
2397
2398 res = false
2399 if true && false
2400 res = true
2401 endif
2402 assert_equal(false, res)
2403
2404 res = false
2405 if g:bool_true && false
2406 res = true
2407 endif
2408 assert_equal(false, res)
2409
2410 res = false
2411 if true && g:bool_false
2412 res = true
2413 endif
2414 assert_equal(false, res)
2415
2416 res = false
2417 if false && false
2418 res = true
2419 endif
2420 assert_equal(false, res)
2421
2422 res = false
2423 if true || false
2424 res = true
2425 endif
2426 assert_equal(true, res)
2427
2428 res = false
2429 if g:bool_true || false
2430 res = true
2431 endif
2432 assert_equal(true, res)
2433
2434 res = false
2435 if true || g:bool_false
2436 res = true
2437 endif
2438 assert_equal(true, res)
2439
2440 res = false
2441 if false || false
2442 res = true
2443 endif
2444 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02002445
2446 # with constant "false" expression may be invalid so long as the syntax is OK
Bram Moolenaarc3235272021-07-10 19:42:03 +02002447 if false | eval 1 + 2 | endif
Bram Moolenaar3988f642020-08-27 22:43:03 +02002448 if false | eval burp + 234 | endif
2449 if false | echo burp 234 'asd' | endif
2450 if false
2451 burp
2452 endif
Bram Moolenaare525bdd2021-08-07 18:12:40 +02002453
2454 # expression with line breaks skipped
2455 if false
2456 ('aaa'
2457 .. 'bbb'
2458 .. 'ccc'
2459 )->setline(1)
2460 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002461enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002462
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002463def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002464 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
2465 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
2466 CheckDefFailure(["if has('aaa'"], 'E110:')
2467 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002468enddef
2469
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002470def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002471 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002472 if i % 2
2473 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002474 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002475 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002476 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002477 endif
2478 x += 1
2479 else
2480 x += 1000
2481 endif
2482 return x
2483enddef
2484
2485def Test_nested_if()
2486 assert_equal(1, RunNested(1))
2487 assert_equal(1000, RunNested(2))
2488enddef
2489
Bram Moolenaarad39c092020-02-26 18:23:43 +01002490def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01002491 # missing argument is ignored
2492 execute
2493 execute # comment
2494
Bram Moolenaarad39c092020-02-26 18:23:43 +01002495 new
2496 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002497 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002498 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002499
Bram Moolenaard2c61702020-09-06 15:58:36 +02002500 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002501 assert_equal('execute-string', getline(1))
2502
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002503 var cmd1 = 'setline(1,'
2504 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002505 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01002506 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002507
Bram Moolenaard2c61702020-09-06 15:58:36 +02002508 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002509 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002510
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002511 var cmd_first = 'call '
2512 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002513 execute cmd_first .. cmd_last
2514 assert_equal('execute-var-var', getline(1))
2515 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02002516
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002517 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002518 execute 'echomsg' (n ? '"true"' : '"no"')
2519 assert_match('^true$', Screenline(&lines))
2520
Bram Moolenaare0de1712020-12-02 17:36:54 +01002521 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002522 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
2523
Bram Moolenaard2c61702020-09-06 15:58:36 +02002524 CheckDefFailure(['execute xxx'], 'E1001:', 1)
2525 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
2526 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002527enddef
2528
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002529def Test_execute_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002530 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002531 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002532 vim9script
2533 execute 'g:someVar'
2534 .. ' = ' ..
2535 '28'
2536 assert_equal(28, g:someVar)
2537 unlet g:someVar
2538 END
2539 CheckScriptSuccess(lines)
2540enddef
2541
Bram Moolenaarad39c092020-02-26 18:23:43 +01002542def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002543 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02002544 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002545 assert_match('^something$', Screenline(&lines))
2546
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002547 echo "some" # comment
2548 echon "thing"
2549 assert_match('^something$', Screenline(&lines))
2550
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002551 var str1 = 'some'
2552 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002553 echo str1 str2
2554 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002555
Bram Moolenaard2c61702020-09-06 15:58:36 +02002556 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01002557enddef
2558
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002559def Test_echomsg_cmd()
2560 echomsg 'some' 'more' # comment
2561 assert_match('^some more$', Screenline(&lines))
2562 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002563 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002564 assert_match('^some more$', Screenline(&lines))
2565
Bram Moolenaard2c61702020-09-06 15:58:36 +02002566 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002567enddef
2568
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002569def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002570 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002571 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002572 vim9script
2573 echomsg 'here'
2574 .. ' is ' ..
2575 'a message'
2576 assert_match('^here is a message$', Screenline(&lines))
2577 END
2578 CheckScriptSuccess(lines)
2579enddef
2580
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002581def Test_echoerr_cmd()
Bram Moolenaar7de62622021-08-07 15:05:47 +02002582 var local = 'local'
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002583 try
Bram Moolenaar7de62622021-08-07 15:05:47 +02002584 echoerr 'something' local 'wrong' # comment
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002585 catch
Bram Moolenaar7de62622021-08-07 15:05:47 +02002586 assert_match('something local wrong', v:exception)
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002587 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002588enddef
2589
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002590def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002591 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002592 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002593 vim9script
2594 try
2595 echoerr 'this'
2596 .. ' is ' ..
2597 'wrong'
2598 catch
2599 assert_match('this is wrong', v:exception)
2600 endtry
2601 END
2602 CheckScriptSuccess(lines)
2603enddef
2604
Bram Moolenaar7de62622021-08-07 15:05:47 +02002605def Test_echoconsole_cmd()
2606 var local = 'local'
2607 echoconsole 'something' local # comment
2608 # output goes anywhere
2609enddef
2610
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002611def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002612 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002613 vim9script
2614 new
2615 for var in range(0, 3)
2616 append(line('$'), var)
2617 endfor
2618 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2619 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01002620
2621 var result = ''
2622 for i in [1, 2, 3]
2623 var loop = ' loop ' .. i
2624 result ..= loop
2625 endfor
2626 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002627 END
2628 writefile(lines, 'Xvim9for.vim')
2629 source Xvim9for.vim
2630 delete('Xvim9for.vim')
2631enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632
rbtnnbebf0692021-08-21 17:26:50 +02002633def Test_for_skipped_block()
2634 # test skipped blocks at outside of function
2635 var lines =<< trim END
2636 var result = []
2637 if true
2638 for n in [1, 2]
2639 result += [n]
2640 endfor
2641 else
2642 for n in [3, 4]
2643 result += [n]
2644 endfor
2645 endif
2646 assert_equal([1, 2], result)
2647
2648 result = []
2649 if false
2650 for n in [1, 2]
2651 result += [n]
2652 endfor
2653 else
2654 for n in [3, 4]
2655 result += [n]
2656 endfor
2657 endif
2658 assert_equal([3, 4], result)
2659 END
2660 CheckDefAndScriptSuccess(lines)
2661
2662 # test skipped blocks at inside of function
2663 lines =<< trim END
2664 def DefTrue()
2665 var result = []
2666 if true
2667 for n in [1, 2]
2668 result += [n]
2669 endfor
2670 else
2671 for n in [3, 4]
2672 result += [n]
2673 endfor
2674 endif
2675 assert_equal([1, 2], result)
2676 enddef
2677 DefTrue()
2678
2679 def DefFalse()
2680 var result = []
2681 if false
2682 for n in [1, 2]
2683 result += [n]
2684 endfor
2685 else
2686 for n in [3, 4]
2687 result += [n]
2688 endfor
2689 endif
2690 assert_equal([3, 4], result)
2691 enddef
2692 DefFalse()
2693 END
2694 CheckDefAndScriptSuccess(lines)
2695enddef
2696
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002697def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002698 var lines =<< trim END
2699 var result = ''
2700 for cnt in range(7)
2701 if cnt == 4
2702 break
2703 endif
2704 if cnt == 2
2705 continue
2706 endif
2707 result ..= cnt .. '_'
2708 endfor
2709 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02002710
Bram Moolenaarf2253962021-04-13 20:53:13 +02002711 var concat = ''
2712 for str in eval('["one", "two"]')
2713 concat ..= str
2714 endfor
2715 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002716
Bram Moolenaarf2253962021-04-13 20:53:13 +02002717 var total = 0
2718 for nr in
2719 [1, 2, 3]
2720 total += nr
2721 endfor
2722 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002723
Bram Moolenaarf2253962021-04-13 20:53:13 +02002724 total = 0
2725 for nr
2726 in [1, 2, 3]
2727 total += nr
2728 endfor
2729 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002730
Bram Moolenaarf2253962021-04-13 20:53:13 +02002731 total = 0
2732 for nr
2733 in
2734 [1, 2, 3]
2735 total += nr
2736 endfor
2737 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01002738
Bram Moolenaara3589a02021-04-14 13:30:46 +02002739 # with type
2740 total = 0
2741 for n: number in [1, 2, 3]
2742 total += n
2743 endfor
2744 assert_equal(6, total)
2745
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002746 var chars = ''
2747 for s: string in 'foobar'
2748 chars ..= s
2749 endfor
2750 assert_equal('foobar', chars)
2751
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02002752 chars = ''
2753 for x: string in {a: 'a', b: 'b'}->values()
2754 chars ..= x
2755 endfor
2756 assert_equal('ab', chars)
2757
Bram Moolenaara3589a02021-04-14 13:30:46 +02002758 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02002759 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02002760 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2761 res ..= n .. s
2762 endfor
2763 assert_equal('1a2b', res)
2764
Bram Moolenaar444d8782021-06-26 12:40:56 +02002765 # unpack with one var
2766 var reslist = []
2767 for [x] in [['aaa'], ['bbb']]
2768 reslist->add(x)
2769 endfor
2770 assert_equal(['aaa', 'bbb'], reslist)
2771
Bram Moolenaara3589a02021-04-14 13:30:46 +02002772 # loop over string
2773 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02002774 for c in 'aéc̀d'
2775 res ..= c .. '-'
2776 endfor
2777 assert_equal('a-é-c̀-d-', res)
2778
2779 res = ''
2780 for c in ''
2781 res ..= c .. '-'
2782 endfor
2783 assert_equal('', res)
2784
2785 res = ''
2786 for c in test_null_string()
2787 res ..= c .. '-'
2788 endfor
2789 assert_equal('', res)
2790
2791 var foo: list<dict<any>> = [
2792 {a: 'Cat'}
2793 ]
2794 for dd in foo
2795 dd.counter = 12
2796 endfor
2797 assert_equal([{a: 'Cat', counter: 12}], foo)
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002798
2799 reslist = []
2800 for _ in range(3)
2801 reslist->add('x')
2802 endfor
2803 assert_equal(['x', 'x', 'x'], reslist)
Bram Moolenaarf2253962021-04-13 20:53:13 +02002804 END
2805 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002806enddef
2807
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02002808def Test_for_loop_with_closure()
2809 var lines =<< trim END
2810 var flist: list<func>
2811 for i in range(5)
2812 var inloop = i
2813 flist[i] = () => inloop
2814 endfor
2815 for i in range(5)
2816 assert_equal(4, flist[i]())
2817 endfor
2818 END
2819 CheckDefAndScriptSuccess(lines)
2820
2821 lines =<< trim END
2822 var flist: list<func>
2823 for i in range(5)
2824 var inloop = i
2825 flist[i] = () => {
2826 return inloop
2827 }
2828 endfor
2829 for i in range(5)
2830 assert_equal(4, flist[i]())
2831 endfor
2832 END
2833 CheckDefAndScriptSuccess(lines)
2834enddef
2835
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002836def Test_for_loop_fails()
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002837 CheckDefAndScriptFailure(['for '], ['E1097:', 'E690:'])
2838 CheckDefAndScriptFailure(['for x'], ['E1097:', 'E690:'])
2839 CheckDefAndScriptFailure(['for x in'], ['E1097:', 'E15:'])
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002840 CheckDefAndScriptFailure(['for # in range(5)'], 'E690:')
2841 CheckDefAndScriptFailure(['for i In range(5)'], 'E690:')
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002842 CheckDefAndScriptFailure(['var x = 5', 'for x in range(5)', 'endfor'], ['E1017:', 'E1041:'])
Bram Moolenaard4ab8072021-07-08 19:22:12 +02002843 CheckScriptFailure(['vim9script', 'var x = 5', 'for x in range(5)', '# comment', 'endfor'], 'E1041:', 3)
Bram Moolenaar822ba242020-05-24 23:00:18 +02002844 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002845 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02002846 CheckDefFailure(['for i in xxx'], 'E1001:')
2847 CheckDefFailure(['endfor'], 'E588:')
2848 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002849
2850 # wrong type detected at compile time
2851 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2852
2853 # wrong type detected at runtime
2854 g:adict = {a: 1}
2855 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2856 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002857
2858 var lines =<< trim END
2859 var d: list<dict<any>> = [{a: 0}]
2860 for e in d
2861 e = {a: 0, b: ''}
2862 endfor
2863 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002864 CheckDefAndScriptFailure(lines, ['E1018:', 'E46:'], 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002865
2866 lines =<< trim END
2867 for nr: number in ['foo']
2868 endfor
2869 END
2870 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaar404557e2021-07-05 21:41:48 +02002871
2872 lines =<< trim END
2873 for n : number in [1, 2]
2874 echo n
2875 endfor
2876 END
2877 CheckDefAndScriptFailure(lines, 'E1059:', 1)
Bram Moolenaarefc5db52021-07-07 21:21:30 +02002878
2879 lines =<< trim END
2880 var d: dict<number> = {a: 1, b: 2}
2881 for [k: job, v: job] in d->items()
2882 echo k v
2883 endfor
2884 END
2885 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected job but got string', 2)
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002886
2887 lines =<< trim END
2888 var i = 0
2889 for i in [1, 2, 3]
2890 echo i
2891 endfor
2892 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002893 CheckDefExecAndScriptFailure(lines, ['E1017:', 'E1041:'])
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002894
2895 lines =<< trim END
2896 var l = [0]
2897 for l[0] in [1, 2, 3]
2898 echo l[0]
2899 endfor
2900 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002901 CheckDefExecAndScriptFailure(lines, ['E461:', 'E1017:'])
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002902
2903 lines =<< trim END
2904 var d = {x: 0}
2905 for d.x in [1, 2, 3]
2906 echo d.x
2907 endfor
2908 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002909 CheckDefExecAndScriptFailure(lines, ['E461:', 'E1017:'])
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002910enddef
2911
Bram Moolenaarea870692020-12-02 14:24:30 +01002912def Test_for_loop_script_var()
2913 # cannot use s:var in a :def function
Bram Moolenaara99fb232021-12-20 12:25:03 +00002914 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1254:')
Bram Moolenaarea870692020-12-02 14:24:30 +01002915
2916 # can use s:var in Vim9 script, with or without s:
2917 var lines =<< trim END
2918 vim9script
2919 var total = 0
2920 for s:var in [1, 2, 3]
2921 total += s:var
2922 endfor
2923 assert_equal(6, total)
2924
2925 total = 0
2926 for var in [1, 2, 3]
2927 total += var
2928 endfor
2929 assert_equal(6, total)
2930 END
2931enddef
2932
Bram Moolenaar792f7862020-11-23 08:31:18 +01002933def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01002934 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002935 var result = []
2936 for [v1, v2] in [[1, 2], [3, 4]]
2937 result->add(v1)
2938 result->add(v2)
2939 endfor
2940 assert_equal([1, 2, 3, 4], result)
2941
2942 result = []
2943 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2944 result->add(v1)
2945 result->add(v2)
2946 result->add(v3)
2947 endfor
2948 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
2949
2950 result = []
2951 for [&ts, &sw] in [[1, 2], [3, 4]]
2952 result->add(&ts)
2953 result->add(&sw)
2954 endfor
2955 assert_equal([1, 2, 3, 4], result)
2956
2957 var slist: list<string>
2958 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
2959 slist->add($LOOPVAR)
2960 slist->add(@r)
2961 slist->add(v:errmsg)
2962 endfor
2963 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
2964
2965 slist = []
2966 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
2967 slist->add(g:globalvar)
2968 slist->add(b:bufvar)
2969 slist->add(w:winvar)
2970 slist->add(t:tabvar)
2971 endfor
2972 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01002973 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaarb777da92021-05-22 21:40:39 +02002974
2975 var res = []
2976 for [_, n, _] in [[1, 2, 3], [4, 5, 6]]
2977 res->add(n)
2978 endfor
2979 assert_equal([2, 5], res)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002980 END
2981 CheckDefAndScriptSuccess(lines)
2982
2983 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01002984 for [v1, v2] in [[1, 2, 3], [3, 4]]
2985 echo v1 v2
2986 endfor
2987 END
2988 CheckDefExecFailure(lines, 'E710:', 1)
2989
2990 lines =<< trim END
2991 for [v1, v2] in [[1], [3, 4]]
2992 echo v1 v2
2993 endfor
2994 END
2995 CheckDefExecFailure(lines, 'E711:', 1)
2996
2997 lines =<< trim END
2998 for [v1, v1] in [[1, 2], [3, 4]]
2999 echo v1
3000 endfor
3001 END
3002 CheckDefExecFailure(lines, 'E1017:', 1)
3003enddef
3004
Bram Moolenaarc150c092021-02-13 15:02:46 +01003005def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02003006 var lines =<< trim END
3007 var looped = 0
3008 var cleanup = 0
3009 for i in range(3)
3010 looped += 1
3011 try
3012 eval [][0]
3013 catch
3014 continue
3015 finally
3016 cleanup += 1
3017 endtry
3018 endfor
3019 assert_equal(3, looped)
3020 assert_equal(3, cleanup)
3021 END
3022 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003023enddef
3024
rbtnnd895b1d2021-08-20 20:54:25 +02003025def Test_while_skipped_block()
3026 # test skipped blocks at outside of function
3027 var lines =<< trim END
3028 var result = []
3029 var n = 0
3030 if true
3031 n = 1
3032 while n < 3
3033 result += [n]
3034 n += 1
3035 endwhile
3036 else
3037 n = 3
3038 while n < 5
3039 result += [n]
3040 n += 1
3041 endwhile
3042 endif
3043 assert_equal([1, 2], result)
3044
3045 result = []
3046 if false
3047 n = 1
3048 while n < 3
3049 result += [n]
3050 n += 1
3051 endwhile
3052 else
3053 n = 3
3054 while n < 5
3055 result += [n]
3056 n += 1
3057 endwhile
3058 endif
3059 assert_equal([3, 4], result)
3060 END
3061 CheckDefAndScriptSuccess(lines)
3062
3063 # test skipped blocks at inside of function
3064 lines =<< trim END
3065 def DefTrue()
3066 var result = []
3067 var n = 0
3068 if true
3069 n = 1
3070 while n < 3
3071 result += [n]
3072 n += 1
3073 endwhile
3074 else
3075 n = 3
3076 while n < 5
3077 result += [n]
3078 n += 1
3079 endwhile
3080 endif
3081 assert_equal([1, 2], result)
3082 enddef
3083 DefTrue()
3084
3085 def DefFalse()
3086 var result = []
3087 var n = 0
3088 if false
3089 n = 1
3090 while n < 3
3091 result += [n]
3092 n += 1
3093 endwhile
3094 else
3095 n = 3
3096 while n < 5
3097 result += [n]
3098 n += 1
3099 endwhile
3100 endif
3101 assert_equal([3, 4], result)
3102 enddef
3103 DefFalse()
3104 END
3105 CheckDefAndScriptSuccess(lines)
3106enddef
3107
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003108def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003109 var result = ''
3110 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003111 while cnt < 555
3112 if cnt == 3
3113 break
3114 endif
3115 cnt += 1
3116 if cnt == 2
3117 continue
3118 endif
3119 result ..= cnt .. '_'
3120 endwhile
3121 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01003122
3123 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01003124 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01003125 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003126enddef
3127
Bram Moolenaar7a53f292021-11-22 18:31:02 +00003128def Test_while_loop_in_script()
3129 var lines =<< trim END
3130 vim9script
3131 var result = ''
3132 var cnt = 0
3133 while cnt < 3
3134 var s = 'v' .. cnt
3135 result ..= s
3136 cnt += 1
3137 endwhile
3138 assert_equal('v0v1v2', result)
3139 END
3140 CheckScriptSuccess(lines)
3141enddef
3142
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003143def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02003144 CheckDefFailure(['while xxx'], 'E1001:')
3145 CheckDefFailure(['endwhile'], 'E588:')
3146 CheckDefFailure(['continue'], 'E586:')
3147 CheckDefFailure(['if true', 'continue'], 'E586:')
3148 CheckDefFailure(['break'], 'E587:')
3149 CheckDefFailure(['if true', 'break'], 'E587:')
3150 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01003151
3152 var lines =<< trim END
3153 var s = ''
3154 while s = ''
3155 endwhile
3156 END
3157 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003158enddef
3159
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003160def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003161 var caught = false
3162 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003163 try
3164 while 1
3165 x += 1
3166 if x == 100
3167 feedkeys("\<C-C>", 'Lt')
3168 endif
3169 endwhile
3170 catch
3171 caught = true
3172 assert_equal(100, x)
3173 endtry
3174 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003175 # consume the CTRL-C
3176 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003177enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01003178
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003179def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003180 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003181 'one',
3182 'two',
3183 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003184 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003185 assert_equal(['one', 'two', 'three'], mylist)
3186
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003187 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003188 ['one']: 1,
3189 ['two']: 2,
3190 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003191 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003192 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01003193 assert_equal({one: 1, two: 2, three: 3}, mydict)
3194 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003195 one: 1, # comment
3196 two: # comment
3197 2, # comment
3198 three: 3 # comment
3199 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003200 assert_equal({one: 1, two: 2, three: 3}, mydict)
3201 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003202 one: 1,
3203 two:
3204 2,
3205 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003206 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003207 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02003208
3209 assert_equal(
3210 ['one', 'two', 'three'],
3211 split('one two three')
3212 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003213enddef
3214
Bram Moolenaar7a092242020-04-16 22:10:49 +02003215def Test_vim9_comment()
3216 CheckScriptSuccess([
3217 'vim9script',
3218 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01003219 '#something',
3220 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003221 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01003222
3223 split Xfile
3224 CheckScriptSuccess([
3225 'vim9script',
3226 'edit #something',
3227 ])
3228 CheckScriptSuccess([
3229 'vim9script',
3230 'edit #{something',
3231 ])
3232 close
3233
Bram Moolenaar7a092242020-04-16 22:10:49 +02003234 CheckScriptFailure([
3235 'vim9script',
3236 ':# something',
3237 ], 'E488:')
3238 CheckScriptFailure([
3239 '# something',
3240 ], 'E488:')
3241 CheckScriptFailure([
3242 ':# something',
3243 ], 'E488:')
3244
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003245 { # block start
3246 } # block end
3247 CheckDefFailure([
3248 '{# comment',
3249 ], 'E488:')
3250 CheckDefFailure([
3251 '{',
3252 '}# comment',
3253 ], 'E488:')
3254
3255 echo "yes" # comment
3256 CheckDefFailure([
3257 'echo "yes"# comment',
3258 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003259 CheckScriptSuccess([
3260 'vim9script',
3261 'echo "yes" # something',
3262 ])
3263 CheckScriptFailure([
3264 'vim9script',
3265 'echo "yes"# something',
3266 ], 'E121:')
3267 CheckScriptFailure([
3268 'vim9script',
3269 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003270 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003271 CheckScriptFailure([
3272 'echo "yes" # something',
3273 ], 'E121:')
3274
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003275 exe "echo" # comment
3276 CheckDefFailure([
3277 'exe "echo"# comment',
3278 ], 'E488:')
3279 CheckScriptSuccess([
3280 'vim9script',
3281 'exe "echo" # something',
3282 ])
3283 CheckScriptFailure([
3284 'vim9script',
3285 'exe "echo"# something',
3286 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003287 CheckScriptFailure([
3288 'vim9script',
3289 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003290 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003291 CheckScriptFailure([
3292 'exe "echo" # something',
3293 ], 'E121:')
3294
Bram Moolenaar7a092242020-04-16 22:10:49 +02003295 CheckDefFailure([
3296 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003297 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003298 'catch',
3299 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003300 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003301 CheckScriptFailure([
3302 'vim9script',
3303 'try# comment',
3304 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003305 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003306 CheckDefFailure([
3307 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003308 ' throw#comment',
3309 'catch',
3310 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003311 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003312 CheckDefFailure([
3313 'try',
3314 ' throw "yes"#comment',
3315 'catch',
3316 'endtry',
3317 ], 'E488:')
3318 CheckDefFailure([
3319 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003320 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003321 'catch# comment',
3322 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003323 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003324 CheckScriptFailure([
3325 'vim9script',
3326 'try',
3327 ' echo "yes"',
3328 'catch# comment',
3329 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003330 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003331 CheckDefFailure([
3332 'try',
3333 ' echo "yes"',
3334 'catch /pat/# comment',
3335 'endtry',
3336 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003337 CheckDefFailure([
3338 'try',
3339 'echo "yes"',
3340 'catch',
3341 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003342 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003343 CheckScriptFailure([
3344 'vim9script',
3345 'try',
3346 ' echo "yes"',
3347 'catch',
3348 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003349 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003350
3351 CheckScriptSuccess([
3352 'vim9script',
3353 'hi # comment',
3354 ])
3355 CheckScriptFailure([
3356 'vim9script',
3357 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003358 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003359 CheckScriptSuccess([
3360 'vim9script',
3361 'hi Search # comment',
3362 ])
3363 CheckScriptFailure([
3364 'vim9script',
3365 'hi Search# comment',
3366 ], 'E416:')
3367 CheckScriptSuccess([
3368 'vim9script',
3369 'hi link This Search # comment',
3370 ])
3371 CheckScriptFailure([
3372 'vim9script',
3373 'hi link This That# comment',
3374 ], 'E413:')
3375 CheckScriptSuccess([
3376 'vim9script',
3377 'hi clear This # comment',
3378 'hi clear # comment',
3379 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003380 # not tested, because it doesn't give an error but a warning:
3381 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02003382 CheckScriptFailure([
3383 'vim9script',
3384 'hi clear# comment',
3385 ], 'E416:')
3386
3387 CheckScriptSuccess([
3388 'vim9script',
3389 'hi Group term=bold',
3390 'match Group /todo/ # comment',
3391 ])
3392 CheckScriptFailure([
3393 'vim9script',
3394 'hi Group term=bold',
3395 'match Group /todo/# comment',
3396 ], 'E488:')
3397 CheckScriptSuccess([
3398 'vim9script',
3399 'match # comment',
3400 ])
3401 CheckScriptFailure([
3402 'vim9script',
3403 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003404 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003405 CheckScriptSuccess([
3406 'vim9script',
3407 'match none # comment',
3408 ])
3409 CheckScriptFailure([
3410 'vim9script',
3411 'match none# comment',
3412 ], 'E475:')
3413
3414 CheckScriptSuccess([
3415 'vim9script',
3416 'menutrans clear # comment',
3417 ])
3418 CheckScriptFailure([
3419 'vim9script',
3420 'menutrans clear# comment text',
3421 ], 'E474:')
3422
3423 CheckScriptSuccess([
3424 'vim9script',
3425 'syntax clear # comment',
3426 ])
3427 CheckScriptFailure([
3428 'vim9script',
3429 'syntax clear# comment text',
3430 ], 'E28:')
3431 CheckScriptSuccess([
3432 'vim9script',
3433 'syntax keyword Word some',
3434 'syntax clear Word # comment',
3435 ])
3436 CheckScriptFailure([
3437 'vim9script',
3438 'syntax keyword Word some',
3439 'syntax clear Word# comment text',
3440 ], 'E28:')
3441
3442 CheckScriptSuccess([
3443 'vim9script',
3444 'syntax list # comment',
3445 ])
3446 CheckScriptFailure([
3447 'vim9script',
3448 'syntax list# comment text',
3449 ], 'E28:')
3450
3451 CheckScriptSuccess([
3452 'vim9script',
3453 'syntax match Word /pat/ oneline # comment',
3454 ])
3455 CheckScriptFailure([
3456 'vim9script',
3457 'syntax match Word /pat/ oneline# comment',
3458 ], 'E475:')
3459
3460 CheckScriptSuccess([
3461 'vim9script',
3462 'syntax keyword Word word # comm[ent',
3463 ])
3464 CheckScriptFailure([
3465 'vim9script',
3466 'syntax keyword Word word# comm[ent',
3467 ], 'E789:')
3468
3469 CheckScriptSuccess([
3470 'vim9script',
3471 'syntax match Word /pat/ # comment',
3472 ])
3473 CheckScriptFailure([
3474 'vim9script',
3475 'syntax match Word /pat/# comment',
3476 ], 'E402:')
3477
3478 CheckScriptSuccess([
3479 'vim9script',
3480 'syntax match Word /pat/ contains=Something # comment',
3481 ])
3482 CheckScriptFailure([
3483 'vim9script',
3484 'syntax match Word /pat/ contains=Something# comment',
3485 ], 'E475:')
3486 CheckScriptFailure([
3487 'vim9script',
3488 'syntax match Word /pat/ contains= # comment',
3489 ], 'E406:')
3490 CheckScriptFailure([
3491 'vim9script',
3492 'syntax match Word /pat/ contains=# comment',
3493 ], 'E475:')
3494
3495 CheckScriptSuccess([
3496 'vim9script',
3497 'syntax region Word start=/pat/ end=/pat/ # comment',
3498 ])
3499 CheckScriptFailure([
3500 'vim9script',
3501 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02003502 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003503
3504 CheckScriptSuccess([
3505 'vim9script',
3506 'syntax sync # comment',
3507 ])
3508 CheckScriptFailure([
3509 'vim9script',
3510 'syntax sync# comment',
3511 ], 'E404:')
3512 CheckScriptSuccess([
3513 'vim9script',
3514 'syntax sync ccomment # comment',
3515 ])
3516 CheckScriptFailure([
3517 'vim9script',
3518 'syntax sync ccomment# comment',
3519 ], 'E404:')
3520
3521 CheckScriptSuccess([
3522 'vim9script',
3523 'syntax cluster Some contains=Word # comment',
3524 ])
3525 CheckScriptFailure([
3526 'vim9script',
3527 'syntax cluster Some contains=Word# comment',
3528 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003529
3530 CheckScriptSuccess([
3531 'vim9script',
3532 'command Echo echo # comment',
3533 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003534 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003535 ])
3536 CheckScriptFailure([
3537 'vim9script',
3538 'command Echo echo# comment',
3539 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003540 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003541 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003542
3543 var curdir = getcwd()
3544 CheckScriptSuccess([
3545 'command Echo cd " comment',
3546 'Echo',
3547 'delcommand Echo',
3548 ])
3549 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01003550 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003551 'command Echo cd # comment',
3552 'Echo',
3553 'delcommand Echo',
3554 ])
3555 CheckScriptFailure([
3556 'vim9script',
3557 'command Echo cd " comment',
3558 'Echo',
3559 ], 'E344:')
3560 delcommand Echo
3561 chdir(curdir)
3562
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003563 CheckScriptFailure([
3564 'vim9script',
3565 'command Echo# comment',
3566 ], 'E182:')
3567 CheckScriptFailure([
3568 'vim9script',
3569 'command Echo echo',
3570 'command Echo# comment',
3571 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003572 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003573
3574 CheckScriptSuccess([
3575 'vim9script',
3576 'function # comment',
3577 ])
3578 CheckScriptFailure([
3579 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02003580 'function " comment',
3581 ], 'E129:')
3582 CheckScriptFailure([
3583 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003584 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003585 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003586 CheckScriptSuccess([
3587 'vim9script',
3588 'function CheckScriptSuccess # comment',
3589 ])
3590 CheckScriptFailure([
3591 'vim9script',
3592 'function CheckScriptSuccess# comment',
3593 ], 'E488:')
3594
3595 CheckScriptSuccess([
3596 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003597 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003598 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003599 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003600 ])
3601 CheckScriptFailure([
3602 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003603 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003604 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003605 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003606 ], 'E488:')
3607
3608 CheckScriptSuccess([
3609 'vim9script',
3610 'call execute("ls") # comment',
3611 ])
3612 CheckScriptFailure([
3613 'vim9script',
3614 'call execute("ls")# comment',
3615 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02003616
3617 CheckScriptFailure([
3618 'def Test() " comment',
3619 'enddef',
3620 ], 'E488:')
3621 CheckScriptFailure([
3622 'vim9script',
3623 'def Test() " comment',
3624 'enddef',
3625 ], 'E488:')
3626
3627 CheckScriptSuccess([
3628 'func Test() " comment',
3629 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003630 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02003631 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02003632 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02003633 'vim9script',
3634 'func Test() " comment',
3635 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02003636 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02003637
3638 CheckScriptSuccess([
3639 'def Test() # comment',
3640 'enddef',
3641 ])
3642 CheckScriptFailure([
3643 'func Test() # comment',
3644 'endfunc',
3645 ], 'E488:')
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003646
3647 var lines =<< trim END
3648 vim9script
3649 syn region Text
3650 \ start='foo'
3651 #\ comment
3652 \ end='bar'
Bram Moolenaar5072b472021-06-03 21:56:10 +02003653 syn region Text start='foo'
3654 #\ comment
3655 \ end='bar'
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003656 END
3657 CheckScriptSuccess(lines)
3658
3659 lines =<< trim END
3660 vim9script
3661 syn region Text
3662 \ start='foo'
3663 "\ comment
3664 \ end='bar'
3665 END
3666 CheckScriptFailure(lines, 'E399:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003667enddef
3668
3669def Test_vim9_comment_gui()
3670 CheckCanRunGui
3671
3672 CheckScriptFailure([
3673 'vim9script',
3674 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003675 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003676 CheckScriptFailure([
3677 'vim9script',
3678 'gui -f#comment'
Bram Moolenaarb8bd2e62021-08-21 17:13:14 +02003679 ], 'E194:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003680enddef
3681
Bram Moolenaara26b9702020-04-18 19:53:28 +02003682def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02003683 au TabEnter *.vim g:entered = 1
3684 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02003685
3686 edit test.vim
3687 doautocmd TabEnter #comment
3688 assert_equal(1, g:entered)
3689
3690 doautocmd TabEnter f.x
3691 assert_equal(2, g:entered)
3692
3693 g:entered = 0
3694 doautocmd TabEnter f.x #comment
3695 assert_equal(2, g:entered)
3696
3697 assert_fails('doautocmd Syntax#comment', 'E216:')
3698
3699 au! TabEnter
3700 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003701
3702 CheckScriptSuccess([
3703 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02003704 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003705 'b:var = 456',
3706 'w:var = 777',
3707 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003708 'unlet g:var w:var # something',
3709 ])
3710
3711 CheckScriptFailure([
3712 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003713 'let var = 123',
3714 ], 'E1126: Cannot use :let in Vim9 script')
3715
3716 CheckScriptFailure([
3717 'vim9script',
3718 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003719 ], 'E1016: Cannot declare a global variable:')
3720
3721 CheckScriptFailure([
3722 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003723 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003724 ], 'E1016: Cannot declare a buffer variable:')
3725
3726 CheckScriptFailure([
3727 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003728 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003729 ], 'E1016: Cannot declare a window variable:')
3730
3731 CheckScriptFailure([
3732 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003733 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003734 ], 'E1016: Cannot declare a tab variable:')
3735
3736 CheckScriptFailure([
3737 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003738 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003739 ], 'E1016: Cannot declare a v: variable:')
3740
3741 CheckScriptFailure([
3742 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003743 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003744 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02003745
3746 CheckScriptFailure([
3747 'vim9script',
3748 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003749 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003750 ], 'E108:')
3751
3752 CheckScriptFailure([
3753 'let g:var = 123',
3754 'unlet g:var # something',
3755 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003756
3757 CheckScriptSuccess([
3758 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003759 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003760 ' echo "yes"',
3761 'elseif 2 #comment',
3762 ' echo "no"',
3763 'endif',
3764 ])
3765
3766 CheckScriptFailure([
3767 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003768 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003769 ' echo "yes"',
3770 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003771 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003772
3773 CheckScriptFailure([
3774 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003775 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003776 ' echo "yes"',
3777 'elseif 2#comment',
3778 ' echo "no"',
3779 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003780 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003781
3782 CheckScriptSuccess([
3783 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003784 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003785 ])
3786
3787 CheckScriptFailure([
3788 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003789 'var v = 1# comment6',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003790 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003791
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003792 CheckScriptSuccess([
3793 'vim9script',
3794 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003795 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003796 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003797 'dsearch /pat/ #comment',
3798 'bwipe!',
3799 ])
3800
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003801 CheckScriptFailure([
3802 'vim9script',
3803 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003804 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003805 ':$',
3806 'dsearch /pat/#comment',
3807 'bwipe!',
3808 ], 'E488:')
3809
3810 CheckScriptFailure([
3811 'vim9script',
3812 'func! SomeFunc()',
3813 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02003814enddef
3815
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003816def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003817 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003818 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02003819 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003820 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02003821 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003822 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02003823 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003824 END
3825 writefile(lines, 'Xfinished')
3826 source Xfinished
3827 assert_equal('two', g:res)
3828
3829 unlet g:res
3830 delete('Xfinished')
3831enddef
3832
Bram Moolenaara5d00772020-05-14 23:20:55 +02003833def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003834 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02003835 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02003836 def GetValue(): string
3837 return theVal
3838 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003839 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02003840 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02003841 theVal = 'else'
3842 g:laterVal = GetValue()
3843 END
3844 writefile(lines, 'Xforward')
3845 source Xforward
3846 assert_equal('something', g:initVal)
3847 assert_equal('else', g:laterVal)
3848
3849 unlet g:initVal
3850 unlet g:laterVal
3851 delete('Xforward')
3852enddef
3853
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003854def Test_source_vim9_from_legacy()
Bram Moolenaara6294952020-12-27 13:39:50 +01003855 var vim9_lines =<< trim END
3856 vim9script
3857 var local = 'local'
3858 g:global = 'global'
3859 export var exported = 'exported'
3860 export def GetText(): string
3861 return 'text'
3862 enddef
3863 END
3864 writefile(vim9_lines, 'Xvim9_script.vim')
3865
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003866 var legacy_lines =<< trim END
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003867 source Xvim9_script.vim
3868
3869 call assert_false(exists('local'))
3870 call assert_false(exists('exported'))
3871 call assert_false(exists('s:exported'))
3872 call assert_equal('global', global)
3873 call assert_equal('global', g:global)
3874
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003875 "" imported variable becomes script-local
3876 "import exported from './Xvim9_script.vim'
3877 "call assert_equal('exported', s:exported)
3878 "call assert_false(exists('exported'))
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003879
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003880 "" imported function becomes script-local
3881 "import GetText from './Xvim9_script.vim'
3882 "call assert_equal('text', s:GetText())
3883 "call assert_false(exists('*GetText'))
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003884 END
3885 writefile(legacy_lines, 'Xlegacy_script.vim')
3886
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003887 source Xlegacy_script.vim
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003888 assert_equal('global', g:global)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003889 unlet g:global
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003890
3891 delete('Xlegacy_script.vim')
3892 delete('Xvim9_script.vim')
3893enddef
Bram Moolenaara5d00772020-05-14 23:20:55 +02003894
Bram Moolenaare535db82021-03-31 21:07:24 +02003895def Test_declare_script_in_func()
3896 var lines =<< trim END
3897 vim9script
3898 func Declare()
3899 let s:local = 123
3900 endfunc
3901 Declare()
3902 assert_equal(123, local)
3903
3904 var error: string
3905 try
3906 local = 'asdf'
3907 catch
3908 error = v:exception
3909 endtry
3910 assert_match('E1012: Type mismatch; expected number but got string', error)
3911
3912 lockvar local
3913 try
3914 local = 999
3915 catch
3916 error = v:exception
3917 endtry
3918 assert_match('E741: Value is locked: local', error)
3919 END
3920 CheckScriptSuccess(lines)
3921enddef
3922
3923
Bram Moolenaar7d699702020-08-14 20:52:28 +02003924func Test_vim9script_not_global()
3925 " check that items defined in Vim9 script are script-local, not global
3926 let vim9lines =<< trim END
3927 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003928 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02003929 func TheFunc()
3930 echo 'local'
3931 endfunc
3932 def DefFunc()
3933 echo 'local'
3934 enddef
3935 END
3936 call writefile(vim9lines, 'Xvim9script.vim')
3937 source Xvim9script.vim
3938 try
3939 echo g:var
3940 assert_report('did not fail')
3941 catch /E121:/
3942 " caught
3943 endtry
3944 try
3945 call TheFunc()
3946 assert_report('did not fail')
3947 catch /E117:/
3948 " caught
3949 endtry
3950 try
3951 call DefFunc()
3952 assert_report('did not fail')
3953 catch /E117:/
3954 " caught
3955 endtry
3956
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003957 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02003958endfunc
3959
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02003960def Test_vim9_copen()
3961 # this was giving an error for setting w:quickfix_title
3962 copen
3963 quit
3964enddef
3965
Bram Moolenaar03290b82020-12-19 16:30:44 +01003966" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003967def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003968 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003969 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01003970 def some#gettest(): string
3971 return 'test'
3972 enddef
3973 g:some#name = 'name'
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003974 g:some#dict = {key: 'value'}
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003975
3976 def some#varargs(a1: string, ...l: list<string>): string
3977 return a1 .. l[0] .. l[1]
3978 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01003979 END
3980
3981 mkdir('Xdir/autoload', 'p')
3982 writefile(lines, 'Xdir/autoload/some.vim')
3983 var save_rtp = &rtp
3984 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3985
3986 assert_equal('test', g:some#gettest())
3987 assert_equal('name', g:some#name)
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003988 assert_equal('value', g:some#dict.key)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003989 g:some#other = 'other'
3990 assert_equal('other', g:some#other)
3991
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003992 assert_equal('abc', some#varargs('a', 'b', 'c'))
3993
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003994 # upper case script name works
3995 lines =<< trim END
3996 vim9script
3997 def Other#getOther(): string
3998 return 'other'
3999 enddef
4000 END
4001 writefile(lines, 'Xdir/autoload/Other.vim')
4002 assert_equal('other', g:Other#getOther())
4003
Bram Moolenaar03290b82020-12-19 16:30:44 +01004004 delete('Xdir', 'rf')
4005 &rtp = save_rtp
4006enddef
4007
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00004008" test disassembling an auto-loaded function starting with "debug"
4009def Test_vim9_autoload_disass()
4010 mkdir('Xdir/autoload', 'p')
4011 var save_rtp = &rtp
4012 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4013
4014 var lines =<< trim END
4015 vim9script
4016 def debugit#test(): string
4017 return 'debug'
4018 enddef
4019 END
4020 writefile(lines, 'Xdir/autoload/debugit.vim')
4021
4022 lines =<< trim END
4023 vim9script
4024 def profileit#test(): string
4025 return 'profile'
4026 enddef
4027 END
4028 writefile(lines, 'Xdir/autoload/profileit.vim')
4029
4030 lines =<< trim END
4031 vim9script
4032 assert_equal('debug', debugit#test())
4033 disass debugit#test
4034 assert_equal('profile', profileit#test())
4035 disass profileit#test
4036 END
4037 CheckScriptSuccess(lines)
4038
4039 delete('Xdir', 'rf')
4040 &rtp = save_rtp
4041enddef
4042
Bram Moolenaar03290b82020-12-19 16:30:44 +01004043" test using a vim9script that is auto-loaded from an autocmd
4044def Test_vim9_aucmd_autoload()
4045 var lines =<< trim END
4046 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004047 def foo#test()
4048 echomsg getreg('"')
4049 enddef
4050 END
4051
4052 mkdir('Xdir/autoload', 'p')
4053 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004054 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004055 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4056 augroup test
4057 autocmd TextYankPost * call foo#test()
4058 augroup END
4059
4060 normal Y
4061
4062 augroup test
4063 autocmd!
4064 augroup END
4065 delete('Xdir', 'rf')
4066 &rtp = save_rtp
4067enddef
4068
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004069" This was causing a crash because suppress_errthrow wasn't reset.
4070def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004071 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004072 vim9script
4073 def crash#func()
4074 try
4075 for x in List()
4076 endfor
4077 catch
4078 endtry
4079 g:ok = true
4080 enddef
4081 fu List()
4082 invalid
4083 endfu
4084 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01004085 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004086 catch /wontmatch/
4087 endtry
4088 END
4089 call mkdir('Xruntime/autoload', 'p')
4090 call writefile(lines, 'Xruntime/autoload/crash.vim')
4091
4092 # run in a separate Vim to avoid the side effects of assert_fails()
4093 lines =<< trim END
4094 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
4095 call crash#func()
4096 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004097 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004098 END
4099 writefile(lines, 'Xscript')
4100 RunVim([], [], '-S Xscript')
4101 assert_equal(['ok'], readfile('Xdidit'))
4102
4103 delete('Xdidit')
4104 delete('Xscript')
4105 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01004106
4107 lines =<< trim END
4108 vim9script
4109 var foo#bar = 'asdf'
4110 END
4111 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004112enddef
4113
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02004114def Test_script_var_in_autocmd()
4115 # using a script variable from an autocommand, defined in a :def function in a
4116 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004117 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02004118 let s:counter = 1
4119 def s:Func()
4120 au! CursorHold
4121 au CursorHold * s:counter += 1
4122 enddef
4123 call s:Func()
4124 doau CursorHold
4125 call assert_equal(2, s:counter)
4126 au! CursorHold
4127 END
4128 CheckScriptSuccess(lines)
4129enddef
4130
Bram Moolenaarb5841b92021-07-15 18:09:53 +02004131def Test_error_in_autoload_script()
4132 var save_rtp = &rtp
4133 var dir = getcwd() .. '/Xruntime'
4134 &rtp = dir
4135 mkdir(dir .. '/autoload', 'p')
4136
4137 var lines =<< trim END
4138 vim9script noclear
4139 def script#autoloaded()
4140 enddef
4141 def Broken()
4142 var x: any = ''
4143 eval x != 0
4144 enddef
4145 Broken()
4146 END
4147 writefile(lines, dir .. '/autoload/script.vim')
4148
4149 lines =<< trim END
4150 vim9script
4151 def CallAutoloaded()
4152 script#autoloaded()
4153 enddef
4154
4155 function Legacy()
4156 try
4157 call s:CallAutoloaded()
4158 catch
4159 call assert_match('E1030: Using a String as a Number', v:exception)
4160 endtry
4161 endfunction
4162
4163 Legacy()
4164 END
4165 CheckScriptSuccess(lines)
4166
4167 &rtp = save_rtp
4168 delete(dir, 'rf')
4169enddef
4170
Bram Moolenaar3896a102020-08-09 14:33:55 +02004171def Test_cmdline_win()
4172 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
4173 # the command line window.
4174 mkdir('rtp/syntax', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004175 var export_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02004176 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004177 export var That = 'yes'
Bram Moolenaar3896a102020-08-09 14:33:55 +02004178 END
4179 writefile(export_lines, 'rtp/syntax/Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004180 var import_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02004181 vim9script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004182 import './Xexport.vim' as exp
4183 echo exp.That
Bram Moolenaar3896a102020-08-09 14:33:55 +02004184 END
4185 writefile(import_lines, 'rtp/syntax/vim.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004186 var save_rtp = &rtp
Bram Moolenaar3896a102020-08-09 14:33:55 +02004187 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
4188 syntax on
4189 augroup CmdWin
4190 autocmd CmdwinEnter * g:got_there = 'yes'
4191 augroup END
4192 # this will open and also close the cmdline window
4193 feedkeys('q:', 'xt')
4194 assert_equal('yes', g:got_there)
4195
4196 augroup CmdWin
4197 au!
4198 augroup END
4199 &rtp = save_rtp
4200 delete('rtp', 'rf')
4201enddef
4202
Bram Moolenaare3d46852020-08-29 13:39:17 +02004203def Test_invalid_sid()
4204 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02004205
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004206 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02004207 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02004208 endif
4209 delete('Xdidit')
4210enddef
4211
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004212def Test_restoring_cpo()
4213 writefile(['vim9script', 'set nocp'], 'Xsourced')
4214 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
4215 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
4216 assert_equal(['done'], readfile('Xdone'))
4217 endif
4218 delete('Xsourced')
4219 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01004220 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01004221
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00004222 writefile(['vim9script', 'g:cpoval = &cpo'], 'XanotherScript')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01004223 set cpo=aABceFsMny>
4224 edit XanotherScript
4225 so %
4226 assert_equal('aABceFsMny>', &cpo)
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00004227 assert_equal('aABceFs', g:cpoval)
Bram Moolenaar0123cc12021-02-07 17:17:58 +01004228 :1del
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00004229 setline(1, 'let g:cpoval = &cpo')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01004230 w
4231 so %
4232 assert_equal('aABceFsMny>', &cpo)
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00004233 assert_equal('aABceFsMny>', g:cpoval)
Bram Moolenaar0123cc12021-02-07 17:17:58 +01004234
4235 delete('XanotherScript')
4236 set cpo&vim
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00004237 unlet g:cpoval
4238
4239 if has('unix')
4240 # 'cpo' is not restored in main vimrc
4241 var save_HOME = $HOME
4242 $HOME = getcwd() .. '/Xhome'
4243 mkdir('Xhome')
4244 var lines =<< trim END
4245 vim9script
4246 writefile(['before: ' .. &cpo], 'Xresult')
4247 set cpo+=M
4248 writefile(['after: ' .. &cpo], 'Xresult', 'a')
4249 END
4250 writefile(lines, 'Xhome/.vimrc')
4251
4252 lines =<< trim END
4253 call writefile(['later: ' .. &cpo], 'Xresult', 'a')
4254 END
4255 writefile(lines, 'Xlegacy')
4256
4257 lines =<< trim END
4258 vim9script
4259 call writefile(['vim9: ' .. &cpo], 'Xresult', 'a')
4260 qa
4261 END
4262 writefile(lines, 'Xvim9')
4263
4264 var cmd = GetVimCommand() .. " -S Xlegacy -S Xvim9"
4265 cmd = substitute(cmd, '-u NONE', '', '')
4266 exe "silent !" .. cmd
4267
4268 assert_equal([
4269 'before: aABceFs',
4270 'after: aABceFsM',
4271 'later: aABceFsM',
4272 'vim9: aABceFs'], readfile('Xresult'))
4273
4274 $HOME = save_HOME
4275 delete('Xhome', 'rf')
4276 delete('Xlegacy')
4277 delete('Xvim9')
4278 delete('Xresult')
4279 endif
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004280enddef
4281
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004282" Use :function so we can use Check commands
4283func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004284 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004285 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004286
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004287 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004288 vim9script
4289 def script#func()
4290 enddef
4291 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004292 call mkdir('Xdir/autoload', 'p')
4293 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004294
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004295 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004296 vim9script
4297 set cpo+=M
4298 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02004299 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004300 setline(1, 'some text')
4301 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004302 call writefile(lines, 'XTest_redraw_cpo')
4303 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
4304 call term_sendkeys(buf, "V:")
4305 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004306
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004307 " clean up
4308 call term_sendkeys(buf, "\<Esc>u")
4309 call StopVimInTerminal(buf)
4310 call delete('XTest_redraw_cpo')
4311 call delete('Xdir', 'rf')
4312endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004313
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004314
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02004315def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004316 var lines =<< trim END
4317 var name: any
4318 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02004319 END
4320 CheckDefAndScriptSuccess(lines)
4321enddef
4322
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02004323func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02004324 CheckRunVimInTerminal
4325
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02004326 " call indirectly to avoid compilation error for missing functions
4327 call Run_Test_define_func_at_command_line()
4328endfunc
4329
4330def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004331 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004332 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004333 func CheckAndQuit()
4334 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
4335 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
4336 endfunc
4337 END
4338 writefile([''], 'Xdidcmd')
4339 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01004340 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004341 # define Afunc() on the command line
4342 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
4343 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004344 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004345
4346 call StopVimInTerminal(buf)
4347 delete('XcallFunc')
4348 delete('Xdidcmd')
4349enddef
4350
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004351def Test_script_var_scope()
4352 var lines =<< trim END
4353 vim9script
4354 if true
4355 if true
4356 var one = 'one'
4357 echo one
4358 endif
4359 echo one
4360 endif
4361 END
4362 CheckScriptFailure(lines, 'E121:', 7)
4363
4364 lines =<< trim END
4365 vim9script
4366 if true
4367 if false
4368 var one = 'one'
4369 echo one
4370 else
4371 var one = 'one'
4372 echo one
4373 endif
4374 echo one
4375 endif
4376 END
4377 CheckScriptFailure(lines, 'E121:', 10)
4378
4379 lines =<< trim END
4380 vim9script
4381 while true
4382 var one = 'one'
4383 echo one
4384 break
4385 endwhile
4386 echo one
4387 END
4388 CheckScriptFailure(lines, 'E121:', 7)
4389
4390 lines =<< trim END
4391 vim9script
4392 for i in range(1)
4393 var one = 'one'
4394 echo one
4395 endfor
4396 echo one
4397 END
4398 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02004399
4400 lines =<< trim END
4401 vim9script
4402 {
4403 var one = 'one'
4404 assert_equal('one', one)
4405 }
4406 assert_false(exists('one'))
4407 assert_false(exists('s:one'))
4408 END
4409 CheckScriptSuccess(lines)
4410
4411 lines =<< trim END
4412 vim9script
4413 {
4414 var one = 'one'
4415 echo one
4416 }
4417 echo one
4418 END
4419 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004420enddef
4421
Bram Moolenaar352134b2020-10-17 22:04:08 +02004422def Test_catch_exception_in_callback()
4423 var lines =<< trim END
4424 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02004425 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02004426 try
4427 var x: string
4428 var y: string
4429 # this error should be caught with CHECKLEN
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00004430 var sl = ['']
4431 [x, y] = sl
Bram Moolenaar352134b2020-10-17 22:04:08 +02004432 catch
4433 g:caught = 'yes'
4434 endtry
4435 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004436 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02004437 feedkeys("\r", 'xt')
4438 END
4439 CheckScriptSuccess(lines)
4440
4441 unlet g:caught
4442enddef
4443
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004444def Test_no_unknown_error_after_error()
4445 if !has('unix') || !has('job')
4446 throw 'Skipped: not unix of missing +job feature'
4447 endif
Bram Moolenaarb16ff292021-09-26 20:14:39 +01004448 # FIXME: this check should not be needed
4449 if has('win32')
4450 throw 'Skipped: does not work on MS-Windows'
4451 endif
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004452 var lines =<< trim END
4453 vim9script
4454 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02004455 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004456 eval [][0]
4457 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02004458 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004459 sleep 1m
4460 source += l
4461 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004462 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01004463 while job_status(myjob) == 'run'
4464 sleep 10m
4465 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01004466 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02004467 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004468 END
4469 writefile(lines, 'Xdef')
4470 assert_fails('so Xdef', ['E684:', 'E1012:'])
4471 delete('Xdef')
4472enddef
4473
Bram Moolenaar4324d872020-12-01 20:12:24 +01004474def InvokeNormal()
4475 exe "norm! :m+1\r"
4476enddef
4477
4478def Test_invoke_normal_in_visual_mode()
4479 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
4480 new
4481 setline(1, ['aaa', 'bbb'])
4482 feedkeys("V\<F3>", 'xt')
4483 assert_equal(['bbb', 'aaa'], getline(1, 2))
4484 xunmap <F3>
4485enddef
4486
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004487def Test_white_space_after_command()
4488 var lines =<< trim END
4489 exit_cb: Func})
4490 END
4491 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01004492
4493 lines =<< trim END
4494 e#
4495 END
4496 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004497enddef
4498
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004499def Test_script_var_gone_when_sourced_twice()
4500 var lines =<< trim END
4501 vim9script
4502 if exists('g:guard')
4503 finish
4504 endif
4505 g:guard = 1
4506 var name = 'thename'
4507 def g:GetName(): string
4508 return name
4509 enddef
4510 def g:SetName(arg: string)
4511 name = arg
4512 enddef
4513 END
4514 writefile(lines, 'XscriptTwice.vim')
4515 so XscriptTwice.vim
4516 assert_equal('thename', g:GetName())
4517 g:SetName('newname')
4518 assert_equal('newname', g:GetName())
4519 so XscriptTwice.vim
4520 assert_fails('call g:GetName()', 'E1149:')
4521 assert_fails('call g:SetName("x")', 'E1149:')
4522
4523 delfunc g:GetName
4524 delfunc g:SetName
4525 delete('XscriptTwice.vim')
4526 unlet g:guard
4527enddef
4528
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004529"def Test_import_gone_when_sourced_twice()
4530" var exportlines =<< trim END
4531" vim9script
4532" if exists('g:guard')
4533" finish
4534" endif
4535" g:guard = 1
4536" export var name = 'someName'
4537" END
4538" writefile(exportlines, 'XexportScript.vim')
4539"
4540" var lines =<< trim END
4541" vim9script
4542" import name from './XexportScript.vim'
4543" def g:GetName(): string
4544" return name
4545" enddef
4546" END
4547" writefile(lines, 'XscriptImport.vim')
4548" so XscriptImport.vim
4549" assert_equal('someName', g:GetName())
4550"
4551" so XexportScript.vim
4552" assert_fails('call g:GetName()', 'E1149:')
4553"
4554" delfunc g:GetName
4555" delete('XexportScript.vim')
4556" delete('XscriptImport.vim')
4557" unlet g:guard
4558"enddef
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004559
Bram Moolenaar10b94212021-02-19 21:42:57 +01004560def Test_unsupported_commands()
4561 var lines =<< trim END
4562 ka
4563 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004564 CheckDefFailure(lines, 'E476:')
4565 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaar10b94212021-02-19 21:42:57 +01004566
4567 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01004568 :1ka
4569 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004570 CheckDefFailure(lines, 'E476:')
4571 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaarada1d872021-02-20 08:16:51 +01004572
4573 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01004574 t
4575 END
4576 CheckDefFailure(lines, 'E1100:')
4577 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4578
4579 lines =<< trim END
4580 x
4581 END
4582 CheckDefFailure(lines, 'E1100:')
4583 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4584
4585 lines =<< trim END
4586 xit
4587 END
4588 CheckDefFailure(lines, 'E1100:')
4589 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4590enddef
4591
Bram Moolenaarc70fe462021-04-17 17:59:19 +02004592def Test_mapping_line_number()
4593 var lines =<< trim END
4594 vim9script
4595 def g:FuncA()
4596 # Some comment
4597 FuncB(0)
4598 enddef
4599 # Some comment
4600 def FuncB(
4601 # Some comment
4602 n: number
4603 )
4604 exe 'nno '
4605 # Some comment
4606 .. '<F3> a'
4607 .. 'b'
4608 .. 'c'
4609 enddef
4610 END
4611 CheckScriptSuccess(lines)
4612 var res = execute('verbose nmap <F3>')
4613 assert_match('No mapping found', res)
4614
4615 g:FuncA()
4616 res = execute('verbose nmap <F3>')
4617 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
4618
4619 nunmap <F3>
4620 delfunc g:FuncA
4621enddef
4622
Bram Moolenaardeb108b2021-07-08 17:35:36 +02004623def Test_option_set()
4624 # legacy script allows for white space
4625 var lines =<< trim END
4626 set foldlevel =11
4627 call assert_equal(11, &foldlevel)
4628 END
4629 CheckScriptSuccess(lines)
4630
4631 set foldlevel
4632 set foldlevel=12
4633 assert_equal(12, &foldlevel)
4634 set foldlevel+=2
4635 assert_equal(14, &foldlevel)
4636 set foldlevel-=3
4637 assert_equal(11, &foldlevel)
4638
4639 lines =<< trim END
4640 set foldlevel =1
4641 END
4642 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: =1')
4643
4644 lines =<< trim END
4645 set foldlevel +=1
4646 END
4647 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: +=1')
4648
4649 lines =<< trim END
4650 set foldlevel ^=1
4651 END
4652 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: ^=1')
4653
4654 lines =<< trim END
4655 set foldlevel -=1
4656 END
4657 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: -=1')
4658
4659 set foldlevel&
4660enddef
4661
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004662def Test_option_modifier()
Bram Moolenaar1594f312021-07-08 16:40:13 +02004663 # legacy script allows for white space
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004664 var lines =<< trim END
4665 set hlsearch & hlsearch !
4666 call assert_equal(1, &hlsearch)
4667 END
4668 CheckScriptSuccess(lines)
4669
Bram Moolenaar1594f312021-07-08 16:40:13 +02004670 set hlsearch
4671 set hlsearch!
4672 assert_equal(false, &hlsearch)
4673
4674 set hlsearch
4675 set hlsearch&
4676 assert_equal(false, &hlsearch)
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004677
4678 lines =<< trim END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004679 set hlsearch &
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004680 END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004681 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: &')
4682
4683 lines =<< trim END
4684 set hlsearch !
4685 END
4686 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: !')
4687
4688 set hlsearch&
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004689enddef
4690
Bram Moolenaarc03fe662021-07-11 16:52:45 +02004691" This must be called last, it may cause following :def functions to fail
4692def Test_xxx_echoerr_line_number()
4693 var lines =<< trim END
4694 echoerr 'some'
4695 .. ' error'
4696 .. ' continued'
4697 END
4698 CheckDefExecAndScriptFailure(lines, 'some error continued', 1)
4699enddef
4700
Bram Moolenaar9537e372021-12-10 21:05:53 +00004701func Test_debug_with_lambda()
Bram Moolenaar9fffef92021-12-10 16:55:58 +00004702 CheckRunVimInTerminal
4703
Bram Moolenaar9537e372021-12-10 21:05:53 +00004704 " call indirectly to avoid compilation error for missing functions
4705 call Run_Test_debug_with_lambda()
4706endfunc
4707
4708def Run_Test_debug_with_lambda()
Bram Moolenaar9fffef92021-12-10 16:55:58 +00004709 var lines =<< trim END
4710 vim9script
4711 def Func()
4712 var n = 0
4713 echo [0]->filter((_, v) => v == n)
4714 enddef
4715 breakadd func Func
4716 Func()
4717 END
4718 writefile(lines, 'XdebugFunc')
4719 var buf = RunVimInTerminal('-S XdebugFunc', {rows: 6, wait_for_ruler: 0})
4720 WaitForAssert(() => assert_match('^>', term_getline(buf, 6)))
4721
4722 term_sendkeys(buf, "cont\<CR>")
4723 WaitForAssert(() => assert_match('\[0\]', term_getline(buf, 5)))
4724
4725 StopVimInTerminal(buf)
4726 delete('XdebugFunc')
4727enddef
4728
Bram Moolenaar310091d2021-12-23 21:14:37 +00004729func Test_debug_running_out_of_lines()
4730 CheckRunVimInTerminal
4731
4732 " call indirectly to avoid compilation error for missing functions
4733 call Run_Test_debug_running_out_of_lines()
4734endfunc
4735
4736def Run_Test_debug_running_out_of_lines()
4737 var lines =<< trim END
4738 vim9script
4739 def Crash()
4740 #
4741 #
4742 #
4743 #
4744 #
4745 #
4746 #
4747 if true
4748 #
4749 endif
4750 enddef
4751 breakadd func Crash
4752 Crash()
4753 END
4754 writefile(lines, 'XdebugFunc')
4755 var buf = RunVimInTerminal('-S XdebugFunc', {rows: 6, wait_for_ruler: 0})
4756 WaitForAssert(() => assert_match('^>', term_getline(buf, 6)))
4757
4758 term_sendkeys(buf, "next\<CR>")
4759 TermWait(buf)
4760 WaitForAssert(() => assert_match('^>', term_getline(buf, 6)))
4761
4762 term_sendkeys(buf, "cont\<CR>")
4763 TermWait(buf)
4764
4765 StopVimInTerminal(buf)
4766 delete('XdebugFunc')
4767enddef
4768
Bram Moolenaar648594e2021-07-11 17:55:01 +02004769def ProfiledWithLambda()
Bram Moolenaard9162552021-07-11 15:26:13 +02004770 var n = 3
4771 echo [[1, 2], [3, 4]]->filter((_, l) => l[0] == n)
4772enddef
4773
Bram Moolenaar648594e2021-07-11 17:55:01 +02004774def ProfiledNested()
4775 var x = 0
4776 def Nested(): any
4777 return x
4778 enddef
4779 Nested()
4780enddef
4781
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004782def ProfiledNestedProfiled()
4783 var x = 0
4784 def Nested(): any
4785 return x
4786 enddef
4787 Nested()
4788enddef
4789
Dominique Pelle923dce22021-11-21 11:36:04 +00004790" Execute this near the end, profiling doesn't stop until Vim exits.
Bram Moolenaard9162552021-07-11 15:26:13 +02004791" This only tests that it works, not the profiling output.
4792def Test_xx_profile_with_lambda()
Bram Moolenaar4ece1522021-07-11 16:31:51 +02004793 CheckFeature profile
4794
Bram Moolenaard9162552021-07-11 15:26:13 +02004795 profile start Xprofile.log
Bram Moolenaar648594e2021-07-11 17:55:01 +02004796 profile func ProfiledWithLambda
4797 ProfiledWithLambda()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004798
Bram Moolenaar648594e2021-07-11 17:55:01 +02004799 profile func ProfiledNested
4800 ProfiledNested()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004801
4802 # Also profile the nested function. Use a different function, although the
4803 # contents is the same, to make sure it was not already compiled.
4804 profile func *
4805 ProfiledNestedProfiled()
4806
4807 profdel func *
4808 profile pause
Bram Moolenaard9162552021-07-11 15:26:13 +02004809enddef
4810
Bram Moolenaar585fea72020-04-02 22:33:21 +02004811" Keep this last, it messes up highlighting.
4812def Test_substitute_cmd()
4813 new
4814 setline(1, 'something')
4815 :substitute(some(other(
4816 assert_equal('otherthing', getline(1))
4817 bwipe!
4818
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004819 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004820 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02004821 vim9script
4822 new
4823 setline(1, 'something')
4824 :substitute(some(other(
4825 assert_equal('otherthing', getline(1))
4826 bwipe!
4827 END
4828 writefile(lines, 'Xvim9lines')
4829 source Xvim9lines
4830
4831 delete('Xvim9lines')
4832enddef
4833
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker