blob: 8aea267591ae335971ff17626f9bfd4303e8d8af [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 Moolenaarcfcd0112020-09-27 15:19:27 +02001175 var import_script_lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 vim9script
Bram Moolenaar24e93162021-07-18 20:40:33 +02001177 import {exported, Exported, ExportedValue} from './Xexport.vim'
1178 g:exported1 = exported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001179 exported += 3
Bram Moolenaar24e93162021-07-18 20:40:33 +02001180 g:exported2 = exported
1181 g:exported3 = ExportedValue()
1182
1183 import ExportedInc from './Xexport.vim'
1184 ExportedInc()
1185 g:exported_i1 = exported
1186 g:exported_i2 = ExportedValue()
1187
1188 exported = 11
1189 g:exported_s1 = exported
1190 g:exported_s2 = ExportedValue()
1191
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 g:imported_func = Exported()
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001193
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001194 def GetExported(): string
Bram Moolenaare0de1712020-12-02 17:36:54 +01001195 var local_dict = {ref: Exported}
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001196 return local_dict.ref()
1197 enddef
1198 g:funcref_result = GetExported()
1199
Bram Moolenaar4db572e2021-07-18 18:21:38 +02001200 var dir = './'
1201 var ext = ".vim"
1202 import {exp_name} from dir .. 'Xexport' .. ext
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001203 g:imported_name = exp_name
1204 exp_name ..= ' Doe'
1205 g:imported_name_appended = exp_name
Bram Moolenaar24e93162021-07-18 20:40:33 +02001206 g:exported_later = exported
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001207
1208 import theList from './Xexport.vim'
1209 theList->add(2)
1210 assert_equal([1, 2], theList)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 END
1212
1213 writefile(import_script_lines, 'Ximport.vim')
1214 writefile(s:export_script_lines, 'Xexport.vim')
1215
1216 source Ximport.vim
1217
1218 assert_equal('bobbie', g:result)
1219 assert_equal('bob', g:localname)
Bram Moolenaar24e93162021-07-18 20:40:33 +02001220 assert_equal(9876, g:exported1)
1221 assert_equal(9879, g:exported2)
1222 assert_equal(9879, g:exported3)
1223
1224 assert_equal(9884, g:exported_i1)
1225 assert_equal(9884, g:exported_i2)
1226
1227 assert_equal(11, g:exported_s1)
1228 assert_equal(11, g:exported_s2)
1229 assert_equal(11, g:exported_later)
1230
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231 assert_equal('Exported', g:imported_func)
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001232 assert_equal('Exported', g:funcref_result)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001233 assert_equal('John', g:imported_name)
1234 assert_equal('John Doe', g:imported_name_appended)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235 assert_false(exists('g:name'))
1236
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001237 Undo_export_script_lines()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001238 unlet g:exported1
1239 unlet g:exported2
1240 unlet g:exported3
1241 unlet g:exported_i1
1242 unlet g:exported_i2
1243 unlet g:exported_later
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001244 unlet g:imported_func
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001245 unlet g:imported_name g:imported_name_appended
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246 delete('Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001247
Bram Moolenaar1c991142020-07-04 13:15:31 +02001248 # similar, with line breaks
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001249 var import_line_break_script_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001250 vim9script
1251 import {
1252 exported,
1253 Exported,
1254 }
1255 from
1256 './Xexport.vim'
Bram Moolenaar24e93162021-07-18 20:40:33 +02001257 g:exported = exported
1258 exported += 7
1259 g:exported_added = exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001260 g:imported_func = Exported()
1261 END
1262 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
1263 source Ximport_lbr.vim
1264
Bram Moolenaar24e93162021-07-18 20:40:33 +02001265 assert_equal(11, g:exported)
1266 assert_equal(18, g:exported_added)
Bram Moolenaar1c991142020-07-04 13:15:31 +02001267 assert_equal('Exported', g:imported_func)
1268
1269 # exported script not sourced again
1270 assert_false(exists('g:result'))
Bram Moolenaar24e93162021-07-18 20:40:33 +02001271 unlet g:exported
1272 unlet g:exported_added
Bram Moolenaar1c991142020-07-04 13:15:31 +02001273 unlet g:imported_func
1274 delete('Ximport_lbr.vim')
1275
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001276 var import_star_as_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001277 vim9script
1278 import * as Export from './Xexport.vim'
1279 def UseExport()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001280 g:exported_def = Export.exported
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001281 enddef
Bram Moolenaar24e93162021-07-18 20:40:33 +02001282 g:exported_script = Export.exported
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001283 assert_equal(1, exists('Export.exported'))
1284 assert_equal(0, exists('Export.notexported'))
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001285 UseExport()
1286 END
1287 writefile(import_star_as_lines, 'Ximport.vim')
1288 source Ximport.vim
Bram Moolenaar24e93162021-07-18 20:40:33 +02001289
1290 assert_equal(18, g:exported_def)
1291 assert_equal(18, g:exported_script)
1292 unlet g:exported_def
1293 unlet g:exported_script
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001294
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001295 var import_star_as_lines_no_dot =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001296 vim9script
1297 import * as Export from './Xexport.vim'
1298 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001299 var dummy = 1
1300 var imported = Export + dummy
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001301 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001302 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001303 END
1304 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001305 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001306
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001307 var import_star_as_lines_dot_space =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001308 vim9script
1309 import * as Export from './Xexport.vim'
1310 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001311 var imported = Export . exported
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001312 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001313 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001314 END
1315 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001316 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001317
Bram Moolenaar921ba522021-07-29 22:25:05 +02001318 var import_func_duplicated =<< trim END
1319 vim9script
1320 import ExportedInc from './Xexport.vim'
1321 import ExportedInc from './Xexport.vim'
1322
1323 ExportedInc()
1324 END
1325 writefile(import_func_duplicated, 'Ximport.vim')
1326 assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
1327
Bram Moolenaara6294952020-12-27 13:39:50 +01001328 var import_star_as_duplicated =<< trim END
1329 vim9script
1330 import * as Export from './Xexport.vim'
1331 var some = 'other'
1332 import * as Export from './Xexport.vim'
1333 defcompile
1334 END
1335 writefile(import_star_as_duplicated, 'Ximport.vim')
1336 assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim')
1337
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001338 var import_star_as_lines_script_no_dot =<< trim END
1339 vim9script
1340 import * as Export from './Xexport.vim'
1341 g:imported_script = Export exported
1342 END
1343 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
1344 assert_fails('source Ximport.vim', 'E1029:')
1345
1346 var import_star_as_lines_script_space_after_dot =<< trim END
1347 vim9script
1348 import * as Export from './Xexport.vim'
1349 g:imported_script = Export. exported
1350 END
1351 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
1352 assert_fails('source Ximport.vim', 'E1074:')
1353
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001354 var import_star_as_lines_missing_name =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001355 vim9script
1356 import * as Export from './Xexport.vim'
1357 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001358 var imported = Export.
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001359 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001360 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001361 END
1362 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001363 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001364
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001365 var import_star_as_lbr_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001366 vim9script
1367 import *
1368 as Export
1369 from
1370 './Xexport.vim'
1371 def UseExport()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001372 g:exported = Export.exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001373 enddef
1374 UseExport()
1375 END
1376 writefile(import_star_as_lbr_lines, 'Ximport.vim')
1377 source Ximport.vim
Bram Moolenaar24e93162021-07-18 20:40:33 +02001378 assert_equal(18, g:exported)
1379 unlet g:exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001380
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001381 var import_star_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001382 vim9script
1383 import * from './Xexport.vim'
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001384 END
1385 writefile(import_star_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001386 assert_fails('source Ximport.vim', 'E1045:', '', 2, 'Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001387
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001388 # try to import something that exists but is not exported
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001389 var import_not_exported_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001390 vim9script
1391 import name from './Xexport.vim'
1392 END
1393 writefile(import_not_exported_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001394 assert_fails('source Ximport.vim', 'E1049:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001395
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001396 # try to import something that is already defined
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001397 var import_already_defined =<< trim END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001398 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001399 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001400 import exported from './Xexport.vim'
1401 END
1402 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001403 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001404
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001405 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001406 import_already_defined =<< trim END
1407 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001408 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001409 import * as exported from './Xexport.vim'
1410 END
1411 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001412 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001413
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001414 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001415 import_already_defined =<< trim END
1416 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001417 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001418 import {exported} from './Xexport.vim'
1419 END
1420 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001421 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001422
Bram Moolenaar918a4242020-12-06 14:37:08 +01001423 # try changing an imported const
1424 var import_assign_to_const =<< trim END
1425 vim9script
1426 import CONST from './Xexport.vim'
1427 def Assign()
1428 CONST = 987
1429 enddef
1430 defcompile
1431 END
1432 writefile(import_assign_to_const, 'Ximport.vim')
1433 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1434
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001435 # try changing an imported final
1436 var import_assign_to_final =<< trim END
1437 vim9script
1438 import theList from './Xexport.vim'
1439 def Assign()
1440 theList = [2]
1441 enddef
1442 defcompile
1443 END
1444 writefile(import_assign_to_final, 'Ximport.vim')
1445 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1446
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001447 # import a very long name, requires making a copy
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001448 var import_long_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001449 vim9script
1450 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
1451 END
1452 writefile(import_long_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001453 assert_fails('source Ximport.vim', 'E1048:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001454
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001455 var import_no_from_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001456 vim9script
1457 import name './Xexport.vim'
1458 END
1459 writefile(import_no_from_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001460 assert_fails('source Ximport.vim', 'E1070:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001461
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001462 var import_invalid_string_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001463 vim9script
1464 import name from Xexport.vim
1465 END
1466 writefile(import_invalid_string_lines, 'Ximport.vim')
Bram Moolenaar4db572e2021-07-18 18:21:38 +02001467 assert_fails('source Ximport.vim', 'E121:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001468
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001469 var import_wrong_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001470 vim9script
1471 import name from './XnoExport.vim'
1472 END
1473 writefile(import_wrong_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001474 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001475
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001476 var import_missing_comma_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001477 vim9script
1478 import {exported name} from './Xexport.vim'
1479 END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001480 writefile(import_missing_comma_lines, 'Ximport3.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001481 assert_fails('source Ximport3.vim', 'E1046:', '', 2, 'Ximport3.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001482
Bram Moolenaar60579352021-07-19 21:45:07 +02001483 var import_redefining_lines =<< trim END
1484 vim9script
1485 import exported from './Xexport.vim'
1486 var exported = 5
1487 END
1488 writefile(import_redefining_lines, 'Ximport.vim')
1489 assert_fails('source Ximport.vim', 'E1213: Redefining imported item "exported"', '', 3)
1490
1491 var import_assign_wrong_type_lines =<< trim END
1492 vim9script
1493 import exported from './Xexport.vim'
1494 exported = 'xxx'
1495 END
1496 writefile(import_assign_wrong_type_lines, 'Ximport.vim')
1497 assert_fails('source Ximport.vim', 'E1012: Type mismatch; expected number but got string', '', 3)
1498
1499 var import_assign_const_lines =<< trim END
1500 vim9script
1501 import CONST from './Xexport.vim'
1502 CONST = 4321
1503 END
1504 writefile(import_assign_const_lines, 'Ximport.vim')
1505 assert_fails('source Ximport.vim', 'E741: Value is locked: CONST', '', 3)
1506
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001507 delete('Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001508 delete('Ximport3.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 delete('Xexport.vim')
1510
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001511 # Check that in a Vim9 script 'cpo' is set to the Vim default.
Bram Moolenaar3e191692021-03-17 17:46:00 +01001512 # Flags added or removed are also applied to the restored value.
1513 set cpo=abcd
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001514 var lines =<< trim END
Bram Moolenaar750802b2020-02-23 18:08:33 +01001515 vim9script
1516 g:cpo_in_vim9script = &cpo
Bram Moolenaar3e191692021-03-17 17:46:00 +01001517 set cpo+=f
1518 set cpo-=c
1519 g:cpo_after_vim9script = &cpo
Bram Moolenaar750802b2020-02-23 18:08:33 +01001520 END
1521 writefile(lines, 'Xvim9_script')
1522 source Xvim9_script
Bram Moolenaar3e191692021-03-17 17:46:00 +01001523 assert_equal('fabd', &cpo)
Bram Moolenaar750802b2020-02-23 18:08:33 +01001524 set cpo&vim
1525 assert_equal(&cpo, g:cpo_in_vim9script)
Bram Moolenaar3e191692021-03-17 17:46:00 +01001526 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
1527 assert_equal(newcpo, g:cpo_after_vim9script)
1528
Bram Moolenaar750802b2020-02-23 18:08:33 +01001529 delete('Xvim9_script')
1530enddef
1531
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001532def Test_import_funcref()
1533 var lines =<< trim END
1534 vim9script
1535 export def F(): number
1536 return 42
1537 enddef
1538 export const G = F
1539 END
1540 writefile(lines, 'Xlib.vim')
1541
1542 lines =<< trim END
1543 vim9script
1544 import {G} from './Xlib.vim'
1545 const Foo = G()
1546 assert_equal(42, Foo)
1547
1548 def DoTest()
1549 const Goo = G()
Bram Moolenaar06ca48a2021-10-23 10:25:21 +01001550 assert_equal(42, Goo)
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001551 enddef
1552 DoTest()
1553 END
1554 CheckScriptSuccess(lines)
1555
1556 delete('Xlib.vim')
1557enddef
1558
Bram Moolenaar6853c382021-09-07 22:12:19 +02001559def Test_import_star_fails()
1560 writefile([], 'Xfoo.vim')
1561 var lines =<< trim END
Bram Moolenaaraf2d5d22021-09-07 22:35:34 +02001562 import * as foo from './Xfoo.vim'
Bram Moolenaar6853c382021-09-07 22:12:19 +02001563 foo = 'bar'
1564 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00001565 CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use foo itself'])
Bram Moolenaar6853c382021-09-07 22:12:19 +02001566 lines =<< trim END
1567 vim9script
Bram Moolenaaraf2d5d22021-09-07 22:35:34 +02001568 import * as foo from './Xfoo.vim'
Bram Moolenaar6853c382021-09-07 22:12:19 +02001569 var that = foo
1570 END
1571 CheckScriptFailure(lines, 'E1029: Expected ''.''')
Bram Moolenaara9e3d562021-09-08 12:31:35 +02001572
1573 lines =<< trim END
1574 vim9script
1575 import * as 9foo from './Xfoo.vim'
1576 END
1577 CheckScriptFailure(lines, 'E1047:')
1578 lines =<< trim END
1579 vim9script
1580 import * as the#foo from './Xfoo.vim'
1581 END
1582 CheckScriptFailure(lines, 'E1047:')
1583 lines =<< trim END
1584 vim9script
1585 import * as g:foo from './Xfoo.vim'
1586 END
1587 CheckScriptFailure(lines, 'E1047:')
1588
Bram Moolenaaraf2d5d22021-09-07 22:35:34 +02001589 delete('Xfoo.vim')
Bram Moolenaarf8a79fc2021-12-14 12:06:16 +00001590
1591 lines =<< trim END
1592 vim9script
1593 def TheFunc()
1594 echo 'the func'
1595 enddef
1596 export var Ref = TheFunc
1597 END
1598 writefile([], 'Xthat.vim')
1599 lines =<< trim END
1600 import * as That from './Xthat.vim'
1601 That()
1602 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00001603 CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use That itself'])
Bram Moolenaarf8a79fc2021-12-14 12:06:16 +00001604 delete('Xthat.vim')
Bram Moolenaar6853c382021-09-07 22:12:19 +02001605enddef
1606
Bram Moolenaar0a842842021-02-27 22:41:19 +01001607def Test_import_as()
1608 var export_lines =<< trim END
1609 vim9script
1610 export var one = 1
1611 export var yes = 'yes'
Bram Moolenaarc967d572021-07-08 21:38:50 +02001612 export var slist: list<string>
Bram Moolenaar0a842842021-02-27 22:41:19 +01001613 END
1614 writefile(export_lines, 'XexportAs')
1615
1616 var import_lines =<< trim END
1617 vim9script
Bram Moolenaar6c7cc342021-04-17 16:38:50 +02001618 var one = 'notused'
1619 var yes = 777
Bram Moolenaar0a842842021-02-27 22:41:19 +01001620 import one as thatOne from './XexportAs'
1621 assert_equal(1, thatOne)
1622 import yes as yesYes from './XexportAs'
1623 assert_equal('yes', yesYes)
1624 END
1625 CheckScriptSuccess(import_lines)
1626
1627 import_lines =<< trim END
1628 vim9script
1629 import {one as thatOne, yes as yesYes} from './XexportAs'
1630 assert_equal(1, thatOne)
1631 assert_equal('yes', yesYes)
1632 assert_fails('echo one', 'E121:')
1633 assert_fails('echo yes', 'E121:')
1634 END
1635 CheckScriptSuccess(import_lines)
1636
Bram Moolenaarc967d572021-07-08 21:38:50 +02001637 import_lines =<< trim END
1638 vim9script
1639 import {slist as impSlist} from './XexportAs'
1640 impSlist->add(123)
1641 END
1642 CheckScriptFailure(import_lines, 'E1012: Type mismatch; expected string but got number')
1643
Bram Moolenaar0a842842021-02-27 22:41:19 +01001644 delete('XexportAs')
1645enddef
1646
Bram Moolenaar803af682020-08-05 16:20:03 +02001647func g:Trigger()
1648 source Ximport.vim
1649 return "echo 'yes'\<CR>"
1650endfunc
1651
1652def Test_import_export_expr_map()
1653 # check that :import and :export work when buffer is locked
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001654 var export_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001655 vim9script
1656 export def That(): string
1657 return 'yes'
1658 enddef
1659 END
1660 writefile(export_lines, 'Xexport_that.vim')
1661
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001662 var import_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001663 vim9script
1664 import That from './Xexport_that.vim'
1665 assert_equal('yes', That())
1666 END
1667 writefile(import_lines, 'Ximport.vim')
1668
1669 nnoremap <expr> trigger g:Trigger()
1670 feedkeys('trigger', "xt")
1671
Bram Moolenaar730b2482020-08-09 13:02:10 +02001672 delete('Xexport_that.vim')
Bram Moolenaar803af682020-08-05 16:20:03 +02001673 delete('Ximport.vim')
1674 nunmap trigger
1675enddef
1676
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001677def Test_import_in_filetype()
1678 # check that :import works when the buffer is locked
1679 mkdir('ftplugin', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001680 var export_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001681 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001682 export var That = 'yes'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001683 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001684 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001685
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001686 var import_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001687 vim9script
Bram Moolenaar730b2482020-08-09 13:02:10 +02001688 import That from './Xexport_ft.vim'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001689 assert_equal('yes', That)
1690 g:did_load_mytpe = 1
1691 END
1692 writefile(import_lines, 'ftplugin/qf.vim')
1693
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001694 var save_rtp = &rtp
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001695 &rtp = getcwd() .. ',' .. &rtp
1696
1697 filetype plugin on
1698 copen
1699 assert_equal(1, g:did_load_mytpe)
1700
1701 quit!
Bram Moolenaar730b2482020-08-09 13:02:10 +02001702 delete('Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001703 delete('ftplugin', 'rf')
1704 &rtp = save_rtp
1705enddef
1706
Bram Moolenaarefa94442020-08-08 22:16:00 +02001707def Test_use_import_in_mapping()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001708 var lines =<< trim END
Bram Moolenaarefa94442020-08-08 22:16:00 +02001709 vim9script
1710 export def Funcx()
1711 g:result = 42
1712 enddef
1713 END
1714 writefile(lines, 'XsomeExport.vim')
1715 lines =<< trim END
1716 vim9script
1717 import Funcx from './XsomeExport.vim'
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001718 nnoremap <F3> :call <sid>Funcx()<cr>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001719 END
1720 writefile(lines, 'Xmapscript.vim')
1721
1722 source Xmapscript.vim
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001723 feedkeys("\<F3>", "xt")
Bram Moolenaarefa94442020-08-08 22:16:00 +02001724 assert_equal(42, g:result)
1725
1726 unlet g:result
1727 delete('XsomeExport.vim')
1728 delete('Xmapscript.vim')
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001729 nunmap <F3>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001730enddef
1731
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01001732def Test_vim9script_mix()
1733 var lines =<< trim END
1734 if has(g:feature)
1735 " legacy script
1736 let g:legacy = 1
1737 finish
1738 endif
1739 vim9script
1740 g:legacy = 0
1741 END
1742 g:feature = 'eval'
1743 g:legacy = -1
1744 CheckScriptSuccess(lines)
1745 assert_equal(1, g:legacy)
1746
1747 g:feature = 'noteval'
1748 g:legacy = -1
1749 CheckScriptSuccess(lines)
1750 assert_equal(0, g:legacy)
1751enddef
1752
Bram Moolenaar750802b2020-02-23 18:08:33 +01001753def Test_vim9script_fails()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1755 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001756 CheckScriptFailure(['export var some = 123'], 'E1042:')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001757 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001758 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
Bram Moolenaar750802b2020-02-23 18:08:33 +01001759 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
1760
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001761 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001762 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1763
Bram Moolenaare2e40752020-09-04 21:18:46 +02001764 assert_fails('vim9script', 'E1038:')
1765 assert_fails('export something', 'E1043:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001766enddef
1767
Bram Moolenaarf7d267e2020-06-17 12:04:54 +02001768func Test_import_fails_without_script()
Bram Moolenaar101f4812020-06-16 23:18:51 +02001769 CheckRunVimInTerminal
1770
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001771 " call indirectly to avoid compilation error for missing functions
Bram Moolenaarc620c052020-07-08 15:16:19 +02001772 call Run_Test_import_fails_on_command_line()
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001773endfunc
1774
Bram Moolenaarc620c052020-07-08 15:16:19 +02001775def Run_Test_import_fails_on_command_line()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001776 var export =<< trim END
Bram Moolenaar101f4812020-06-16 23:18:51 +02001777 vim9script
1778 export def Foo(): number
1779 return 0
1780 enddef
1781 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001782 writefile(export, 'XexportCmd.vim')
Bram Moolenaar101f4812020-06-16 23:18:51 +02001783
Bram Moolenaare0de1712020-12-02 17:36:54 +01001784 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001785 rows: 6, wait_for_ruler: 0})
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001786 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaar101f4812020-06-16 23:18:51 +02001787
Bram Moolenaar730b2482020-08-09 13:02:10 +02001788 delete('XexportCmd.vim')
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001789 StopVimInTerminal(buf)
1790enddef
Bram Moolenaar101f4812020-06-16 23:18:51 +02001791
Bram Moolenaar2b327002020-12-26 15:39:31 +01001792def Test_vim9script_reload_noclear()
1793 var lines =<< trim END
Bram Moolenaara6294952020-12-27 13:39:50 +01001794 vim9script
1795 export var exported = 'thexport'
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001796
1797 export def TheFunc(x = 0)
1798 enddef
Bram Moolenaara6294952020-12-27 13:39:50 +01001799 END
1800 writefile(lines, 'XExportReload')
1801 lines =<< trim END
Bram Moolenaar2b327002020-12-26 15:39:31 +01001802 vim9script noclear
1803 g:loadCount += 1
1804 var s:reloaded = 'init'
Bram Moolenaara6294952020-12-27 13:39:50 +01001805 import exported from './XExportReload'
Bram Moolenaar2b327002020-12-26 15:39:31 +01001806
1807 def Again(): string
1808 return 'again'
1809 enddef
1810
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001811 import TheFunc from './XExportReload'
1812 TheFunc()
1813
Bram Moolenaar2b327002020-12-26 15:39:31 +01001814 if exists('s:loaded') | finish | endif
1815 var s:loaded = true
1816
1817 var s:notReloaded = 'yes'
1818 s:reloaded = 'first'
1819 def g:Values(): list<string>
Bram Moolenaara6294952020-12-27 13:39:50 +01001820 return [s:reloaded, s:notReloaded, Again(), Once(), exported]
Bram Moolenaar2b327002020-12-26 15:39:31 +01001821 enddef
1822
1823 def Once(): string
1824 return 'once'
1825 enddef
1826 END
1827 writefile(lines, 'XReloaded')
1828 g:loadCount = 0
1829 source XReloaded
1830 assert_equal(1, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001831 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001832 source XReloaded
1833 assert_equal(2, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001834 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001835 source XReloaded
1836 assert_equal(3, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001837 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001838
Bram Moolenaar48e11c12021-01-11 18:47:00 +01001839 delete('XReloaded')
Bram Moolenaara6294952020-12-27 13:39:50 +01001840 delete('XExportReload')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001841 delfunc g:Values
Bram Moolenaar2b327002020-12-26 15:39:31 +01001842 unlet g:loadCount
Bram Moolenaar577dc932021-06-27 15:35:40 +02001843
1844 lines =<< trim END
1845 vim9script
1846 def Inner()
1847 enddef
1848 END
1849 lines->writefile('XreloadScript.vim')
1850 source XreloadScript.vim
1851
1852 lines =<< trim END
1853 vim9script
1854 def Outer()
1855 def Inner()
1856 enddef
1857 enddef
1858 defcompile
1859 END
1860 lines->writefile('XreloadScript.vim')
1861 source XreloadScript.vim
1862
1863 delete('XreloadScript.vim')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001864enddef
1865
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001866def Test_vim9script_reload_import()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001867 var lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 vim9script
1869 const var = ''
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001870 var valone = 1234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871 def MyFunc(arg: string)
1872 valone = 5678
1873 enddef
1874 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001875 var morelines =<< trim END
1876 var valtwo = 222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 export def GetValtwo(): number
1878 return valtwo
1879 enddef
1880 END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001881 writefile(lines + morelines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882 source Xreload.vim
1883 source Xreload.vim
1884 source Xreload.vim
1885
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001886 # cannot declare a var twice
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 lines =<< trim END
1888 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001889 var valone = 1234
1890 var valone = 5678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 END
1892 writefile(lines, 'Xreload.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001893 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894
1895 delete('Xreload.vim')
1896 delete('Ximport.vim')
1897enddef
1898
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001899" if a script is reloaded with a script-local variable that changed its type, a
1900" compiled function using that variable must fail.
1901def Test_script_reload_change_type()
1902 var lines =<< trim END
1903 vim9script noclear
1904 var str = 'string'
1905 def g:GetStr(): string
1906 return str .. 'xxx'
1907 enddef
1908 END
1909 writefile(lines, 'Xreload.vim')
1910 source Xreload.vim
1911 echo g:GetStr()
1912
1913 lines =<< trim END
1914 vim9script noclear
1915 var str = 1234
1916 END
1917 writefile(lines, 'Xreload.vim')
1918 source Xreload.vim
1919 assert_fails('echo g:GetStr()', 'E1150:')
1920
1921 delfunc g:GetStr
1922 delete('Xreload.vim')
1923enddef
1924
Bram Moolenaarc970e422021-03-17 15:03:04 +01001925" Define CallFunc so that the test can be compiled
1926command CallFunc echo 'nop'
1927
1928def Test_script_reload_from_function()
1929 var lines =<< trim END
1930 vim9script
1931
1932 if exists('g:loaded')
1933 finish
1934 endif
1935 g:loaded = 1
1936 delcommand CallFunc
1937 command CallFunc Func()
1938 def Func()
Bram Moolenaara4c81be2021-03-17 15:23:16 +01001939 so XreloadFunc.vim
Bram Moolenaarc970e422021-03-17 15:03:04 +01001940 g:didTheFunc = 1
1941 enddef
1942 END
1943 writefile(lines, 'XreloadFunc.vim')
1944 source XreloadFunc.vim
1945 CallFunc
1946 assert_equal(1, g:didTheFunc)
1947
1948 delete('XreloadFunc.vim')
1949 delcommand CallFunc
1950 unlet g:loaded
1951 unlet g:didTheFunc
1952enddef
1953
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001954def Test_script_var_shadows_function()
1955 var lines =<< trim END
1956 vim9script
1957 def Func(): number
1958 return 123
1959 enddef
1960 var Func = 1
1961 END
1962 CheckScriptFailure(lines, 'E1041:', 5)
1963enddef
1964
Bram Moolenaar052ff292021-12-11 13:54:46 +00001965def Test_function_shadows_script_var()
1966 var lines =<< trim END
1967 vim9script
1968 var Func = 1
1969 def Func(): number
1970 return 123
1971 enddef
1972 END
1973 CheckScriptFailure(lines, 'E1041:', 3)
1974enddef
1975
Bram Moolenaarc3235272021-07-10 19:42:03 +02001976def Test_script_var_shadows_command()
1977 var lines =<< trim END
1978 var undo = 1
1979 undo = 2
1980 assert_equal(2, undo)
1981 END
1982 CheckDefAndScriptSuccess(lines)
1983
1984 lines =<< trim END
1985 var undo = 1
1986 undo
1987 END
1988 CheckDefAndScriptFailure(lines, 'E1207:', 2)
1989enddef
1990
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001991def Test_vim9script_call_wrong_type()
1992 var lines =<< trim END
1993 vim9script
1994 var Time = 'localtime'
1995 Time()
1996 END
1997 CheckScriptFailure(lines, 'E1085:')
1998enddef
1999
Bram Moolenaar95006e32020-08-29 17:47:08 +02002000def s:RetSome(): string
2001 return 'some'
2002enddef
2003
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002004" Not exported function that is referenced needs to be accessed by the
2005" script-local name.
2006def Test_vim9script_funcref()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002007 var sortlines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002008 vim9script
2009 def Compare(i1: number, i2: number): number
Bram Moolenaarbed36b92020-07-07 23:31:36 +02002010 return i2 - i1
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002011 enddef
2012
2013 export def FastSort(): list<number>
2014 return range(5)->sort(Compare)
2015 enddef
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002016
2017 export def GetString(arg: string): string
2018 return arg
2019 enddef
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002020 END
2021 writefile(sortlines, 'Xsort.vim')
2022
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002023 var lines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002024 vim9script
2025 import FastSort from './Xsort.vim'
2026 def Test()
2027 g:result = FastSort()
2028 enddef
2029 Test()
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002030
2031 # using a function imported with "as"
2032 import * as anAlias from './Xsort.vim'
2033 assert_equal('yes', anAlias.GetString('yes'))
2034
2035 # using the function from a compiled function
2036 def TestMore(): string
Bram Moolenaarca51cc02021-04-01 21:38:53 +02002037 var s = s:anAlias.GetString('foo')
2038 return s .. anAlias.GetString('bar')
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002039 enddef
Bram Moolenaarca51cc02021-04-01 21:38:53 +02002040 assert_equal('foobar', TestMore())
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002041
2042 # error when using a function that isn't exported
2043 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002044 END
2045 writefile(lines, 'Xscript.vim')
2046
2047 source Xscript.vim
2048 assert_equal([4, 3, 2, 1, 0], g:result)
2049
2050 unlet g:result
2051 delete('Xsort.vim')
2052 delete('Xscript.vim')
Bram Moolenaar95006e32020-08-29 17:47:08 +02002053
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002054 var Funcref = function('s:RetSome')
Bram Moolenaar95006e32020-08-29 17:47:08 +02002055 assert_equal('some', Funcref())
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002056enddef
2057
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002058" Check that when searching for "FilterFunc" it finds the import in the
2059" script where FastFilter() is called from, both as a string and as a direct
2060" function reference.
Bram Moolenaarc620c052020-07-08 15:16:19 +02002061def Test_vim9script_funcref_other_script()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002062 var filterLines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02002063 vim9script
2064 export def FilterFunc(idx: number, val: number): bool
2065 return idx % 2 == 1
2066 enddef
2067 export def FastFilter(): list<number>
Bram Moolenaar18024052021-12-25 21:43:28 +00002068 return range(10)->filter('FilterFunc(v:key, v:val)')
Bram Moolenaarc620c052020-07-08 15:16:19 +02002069 enddef
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002070 export def FastFilterDirect(): list<number>
2071 return range(10)->filter(FilterFunc)
2072 enddef
Bram Moolenaarc620c052020-07-08 15:16:19 +02002073 END
2074 writefile(filterLines, 'Xfilter.vim')
2075
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002076 var lines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02002077 vim9script
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002078 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim'
Bram Moolenaarc620c052020-07-08 15:16:19 +02002079 def Test()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002080 var x: list<number> = FastFilter()
Bram Moolenaarc620c052020-07-08 15:16:19 +02002081 enddef
2082 Test()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002083 def TestDirect()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002084 var x: list<number> = FastFilterDirect()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002085 enddef
2086 TestDirect()
Bram Moolenaarc620c052020-07-08 15:16:19 +02002087 END
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002088 CheckScriptSuccess(lines)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002089 delete('Xfilter.vim')
Bram Moolenaarc620c052020-07-08 15:16:19 +02002090enddef
2091
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002092def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002093 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002094 vim9script
2095 def FuncYes(): string
2096 return 'yes'
2097 enddef
2098 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002099 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002100 def FuncNo(): string
2101 return 'no'
2102 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002103 def g:DoCheck(no_exists: bool)
2104 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002105 assert_equal('no', FuncNo())
2106 enddef
2107 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002108 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002109 def g:DoCheck(no_exists: bool)
2110 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02002111 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002112 enddef
2113 END
2114
2115 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002116 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002117 source Xreloaded.vim
2118 g:DoCheck(true)
2119
2120 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002121 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002122 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01002123 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002124
2125 # FuncNo() is back
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002126 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002127 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01002128 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002129
2130 delete('Xreloaded.vim')
2131enddef
2132
Bram Moolenaar89483d42020-05-10 15:24:44 +02002133def Test_vim9script_reload_delvar()
2134 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002135 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02002136 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002137 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02002138 END
2139 writefile(lines, 'XreloadVar.vim')
2140 source XreloadVar.vim
2141
2142 # now write the script using the same variable locally - works
2143 lines =<< trim END
2144 vim9script
2145 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002146 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02002147 enddef
2148 END
2149 writefile(lines, 'XreloadVar.vim')
2150 source XreloadVar.vim
2151
2152 delete('XreloadVar.vim')
2153enddef
2154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155def Test_import_absolute()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002156 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02002157 'vim9script',
2158 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
2159 'def UseExported()',
2160 ' g:imported_abs = exported',
2161 ' exported = 8888',
2162 ' g:imported_after = exported',
2163 'enddef',
2164 'UseExported()',
2165 'g:import_disassembled = execute("disass UseExported")',
2166 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002167 writefile(import_lines, 'Ximport_abs.vim')
2168 writefile(s:export_script_lines, 'Xexport_abs.vim')
2169
2170 source Ximport_abs.vim
2171
2172 assert_equal(9876, g:imported_abs)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002173 assert_equal(8888, g:imported_after)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002174 assert_match('<SNR>\d\+_UseExported\_s*' ..
2175 'g:imported_abs = exported\_s*' ..
2176 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
2177 '1 STOREG g:imported_abs\_s*' ..
2178 'exported = 8888\_s*' ..
2179 '2 PUSHNR 8888\_s*' ..
2180 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
2181 'g:imported_after = exported\_s*' ..
2182 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
2183 '5 STOREG g:imported_after',
Bram Moolenaare6085c52020-04-12 20:19:16 +02002184 g:import_disassembled)
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002185
2186 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 unlet g:imported_abs
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002188 unlet g:import_disassembled
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002189
2190 delete('Ximport_abs.vim')
2191 delete('Xexport_abs.vim')
2192enddef
2193
2194def Test_import_rtp()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002195 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02002196 'vim9script',
2197 'import exported from "Xexport_rtp.vim"',
2198 'g:imported_rtp = exported',
2199 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 writefile(import_lines, 'Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002201 mkdir('import', 'p')
2202 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002203
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002204 var save_rtp = &rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 &rtp = getcwd()
2206 source Ximport_rtp.vim
2207 &rtp = save_rtp
2208
2209 assert_equal(9876, g:imported_rtp)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002211 Undo_export_script_lines()
2212 unlet g:imported_rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213 delete('Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002214 delete('import', 'rf')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215enddef
2216
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002217def Test_import_compile_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002218 var export_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002219 'vim9script',
2220 'export def ExpFunc(): string',
2221 ' return notDefined',
2222 'enddef',
2223 ]
2224 writefile(export_lines, 'Xexported.vim')
2225
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002226 var import_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002227 'vim9script',
2228 'import ExpFunc from "./Xexported.vim"',
2229 'def ImpFunc()',
2230 ' echo ExpFunc()',
2231 'enddef',
2232 'defcompile',
2233 ]
2234 writefile(import_lines, 'Ximport.vim')
2235
2236 try
2237 source Ximport.vim
2238 catch /E1001/
Dominique Pelle923dce22021-11-21 11:36:04 +00002239 # Error should be before the Xexported.vim file.
Bram Moolenaar77072282020-09-16 17:55:40 +02002240 assert_match('E1001: Variable not found: notDefined', v:exception)
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002241 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
2242 endtry
2243
2244 delete('Xexported.vim')
2245 delete('Ximport.vim')
2246enddef
2247
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002248def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002249 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002250 'vim9script',
2251 'def Func()',
2252 ' eval [][0]',
2253 'enddef',
2254 'Func()',
2255 ]
2256 writefile(lines, 'Xtestscript.vim')
2257
2258 for count in range(3)
2259 try
2260 source Xtestscript.vim
2261 catch /E684/
2262 # function name should contain <SNR> every time
2263 assert_match('E684: list index out of range', v:exception)
2264 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
2265 endtry
2266 endfor
2267
2268 delete('Xtestscript.vim')
2269enddef
2270
Bram Moolenaareef21022020-08-01 22:16:43 +02002271def Test_func_overrules_import_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002272 var export_lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002273 vim9script
2274 export def Func()
2275 echo 'imported'
2276 enddef
2277 END
2278 writefile(export_lines, 'XexportedFunc.vim')
2279
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002280 var lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002281 vim9script
2282 import Func from './XexportedFunc.vim'
2283 def Func()
2284 echo 'local to function'
2285 enddef
2286 END
Bram Moolenaar052ff292021-12-11 13:54:46 +00002287 CheckScriptFailure(lines, 'E1041:')
Bram Moolenaareef21022020-08-01 22:16:43 +02002288
2289 lines =<< trim END
2290 vim9script
2291 import Func from './XexportedFunc.vim'
2292 def Outer()
2293 def Func()
2294 echo 'local to function'
2295 enddef
2296 enddef
2297 defcompile
2298 END
2299 CheckScriptFailure(lines, 'E1073:')
2300
2301 delete('XexportedFunc.vim')
2302enddef
2303
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002304def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002305 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002306 vim9script
2307 def Func()
2308 echo 'one'
2309 enddef
2310 def Func()
2311 echo 'two'
2312 enddef
2313 END
2314 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002315
2316 lines =<< trim END
2317 vim9script
2318 def Foo(): string
2319 return 'foo'
Bram Moolenaar052ff292021-12-11 13:54:46 +00002320 enddef
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002321 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002322 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002323 enddef
2324 defcompile
2325 END
2326 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002327enddef
2328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002330 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002331 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 l->remove(0)
2333 l->add(5)
2334 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002335 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336enddef
2337
Bram Moolenaarae616492020-07-28 20:07:27 +02002338def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002339 CheckDefExecFailure(['a = 1'], 'E1100:')
2340 CheckDefExecFailure(['c = 1'], 'E1100:')
2341 CheckDefExecFailure(['i = 1'], 'E1100:')
2342 CheckDefExecFailure(['t = 1'], 'E1100:')
2343 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002344
Bram Moolenaarae616492020-07-28 20:07:27 +02002345 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
2346 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002347 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
2348 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002349 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
2350 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01002351 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
2352 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002353 CheckScriptFailure(['vim9script', 't'], 'E1100:')
2354 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
2355 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002356enddef
2357
Bram Moolenaar158906c2020-02-06 20:39:45 +01002358def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002359 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01002360 if what == 1
2361 res = "one"
2362 elseif what == 2
2363 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002364 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01002365 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002366 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01002367 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002368enddef
2369
Bram Moolenaar158906c2020-02-06 20:39:45 +01002370def Test_if_elseif_else()
2371 assert_equal('one', IfElse(1))
2372 assert_equal('two', IfElse(2))
2373 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002374enddef
2375
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002376def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002377 CheckDefFailure(['elseif true'], 'E582:')
2378 CheckDefFailure(['else'], 'E581:')
2379 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01002380 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002381 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002382
2383 var lines =<< trim END
2384 var s = ''
2385 if s = ''
2386 endif
2387 END
2388 CheckDefFailure(lines, 'E488:')
2389
2390 lines =<< trim END
2391 var s = ''
2392 if s == ''
2393 elseif s = ''
2394 endif
2395 END
2396 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002397enddef
2398
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002399let g:bool_true = v:true
2400let g:bool_false = v:false
2401
2402def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002403 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002404 if true ? true : false
2405 res = true
2406 endif
2407 assert_equal(true, res)
2408
Bram Moolenaar585fea72020-04-02 22:33:21 +02002409 g:glob = 2
2410 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02002411 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002412 endif
2413 assert_equal(2, g:glob)
2414 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02002415 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002416 endif
2417 assert_equal(3, g:glob)
2418
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002419 res = false
2420 if g:bool_true ? true : false
2421 res = true
2422 endif
2423 assert_equal(true, res)
2424
2425 res = false
2426 if true ? g:bool_true : false
2427 res = true
2428 endif
2429 assert_equal(true, res)
2430
2431 res = false
2432 if true ? true : g:bool_false
2433 res = true
2434 endif
2435 assert_equal(true, res)
2436
2437 res = false
2438 if true ? false : true
2439 res = true
2440 endif
2441 assert_equal(false, res)
2442
2443 res = false
2444 if false ? false : true
2445 res = true
2446 endif
2447 assert_equal(true, res)
2448
2449 res = false
2450 if false ? true : false
2451 res = true
2452 endif
2453 assert_equal(false, res)
2454
2455 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002456 if has('xyz') ? true : false
2457 res = true
2458 endif
2459 assert_equal(false, res)
2460
2461 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002462 if true && true
2463 res = true
2464 endif
2465 assert_equal(true, res)
2466
2467 res = false
2468 if true && false
2469 res = true
2470 endif
2471 assert_equal(false, res)
2472
2473 res = false
2474 if g:bool_true && false
2475 res = true
2476 endif
2477 assert_equal(false, res)
2478
2479 res = false
2480 if true && g:bool_false
2481 res = true
2482 endif
2483 assert_equal(false, res)
2484
2485 res = false
2486 if false && false
2487 res = true
2488 endif
2489 assert_equal(false, res)
2490
2491 res = false
2492 if true || false
2493 res = true
2494 endif
2495 assert_equal(true, res)
2496
2497 res = false
2498 if g:bool_true || false
2499 res = true
2500 endif
2501 assert_equal(true, res)
2502
2503 res = false
2504 if true || g:bool_false
2505 res = true
2506 endif
2507 assert_equal(true, res)
2508
2509 res = false
2510 if false || false
2511 res = true
2512 endif
2513 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02002514
2515 # with constant "false" expression may be invalid so long as the syntax is OK
Bram Moolenaarc3235272021-07-10 19:42:03 +02002516 if false | eval 1 + 2 | endif
Bram Moolenaar3988f642020-08-27 22:43:03 +02002517 if false | eval burp + 234 | endif
2518 if false | echo burp 234 'asd' | endif
2519 if false
2520 burp
2521 endif
Bram Moolenaare525bdd2021-08-07 18:12:40 +02002522
2523 # expression with line breaks skipped
2524 if false
2525 ('aaa'
2526 .. 'bbb'
2527 .. 'ccc'
2528 )->setline(1)
2529 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002530enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002531
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002532def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002533 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
2534 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
2535 CheckDefFailure(["if has('aaa'"], 'E110:')
2536 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002537enddef
2538
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002539def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002540 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002541 if i % 2
2542 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002543 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002544 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002545 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002546 endif
2547 x += 1
2548 else
2549 x += 1000
2550 endif
2551 return x
2552enddef
2553
2554def Test_nested_if()
2555 assert_equal(1, RunNested(1))
2556 assert_equal(1000, RunNested(2))
2557enddef
2558
Bram Moolenaarad39c092020-02-26 18:23:43 +01002559def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01002560 # missing argument is ignored
2561 execute
2562 execute # comment
2563
Bram Moolenaarad39c092020-02-26 18:23:43 +01002564 new
2565 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002566 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002567 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002568
Bram Moolenaard2c61702020-09-06 15:58:36 +02002569 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002570 assert_equal('execute-string', getline(1))
2571
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002572 var cmd1 = 'setline(1,'
2573 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002574 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01002575 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002576
Bram Moolenaard2c61702020-09-06 15:58:36 +02002577 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002578 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002579
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002580 var cmd_first = 'call '
2581 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002582 execute cmd_first .. cmd_last
2583 assert_equal('execute-var-var', getline(1))
2584 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02002585
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002586 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002587 execute 'echomsg' (n ? '"true"' : '"no"')
2588 assert_match('^true$', Screenline(&lines))
2589
Bram Moolenaare0de1712020-12-02 17:36:54 +01002590 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002591 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
2592
Bram Moolenaard2c61702020-09-06 15:58:36 +02002593 CheckDefFailure(['execute xxx'], 'E1001:', 1)
2594 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
2595 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002596enddef
2597
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002598def Test_execute_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002599 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002600 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002601 vim9script
2602 execute 'g:someVar'
2603 .. ' = ' ..
2604 '28'
2605 assert_equal(28, g:someVar)
2606 unlet g:someVar
2607 END
2608 CheckScriptSuccess(lines)
2609enddef
2610
Bram Moolenaarad39c092020-02-26 18:23:43 +01002611def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002612 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02002613 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002614 assert_match('^something$', Screenline(&lines))
2615
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002616 echo "some" # comment
2617 echon "thing"
2618 assert_match('^something$', Screenline(&lines))
2619
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002620 var str1 = 'some'
2621 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002622 echo str1 str2
2623 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002624
Bram Moolenaard2c61702020-09-06 15:58:36 +02002625 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01002626enddef
2627
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002628def Test_echomsg_cmd()
2629 echomsg 'some' 'more' # comment
2630 assert_match('^some more$', Screenline(&lines))
2631 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002632 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002633 assert_match('^some more$', Screenline(&lines))
2634
Bram Moolenaard2c61702020-09-06 15:58:36 +02002635 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002636enddef
2637
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002638def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002639 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002640 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002641 vim9script
2642 echomsg 'here'
2643 .. ' is ' ..
2644 'a message'
2645 assert_match('^here is a message$', Screenline(&lines))
2646 END
2647 CheckScriptSuccess(lines)
2648enddef
2649
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002650def Test_echoerr_cmd()
Bram Moolenaar7de62622021-08-07 15:05:47 +02002651 var local = 'local'
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002652 try
Bram Moolenaar7de62622021-08-07 15:05:47 +02002653 echoerr 'something' local 'wrong' # comment
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002654 catch
Bram Moolenaar7de62622021-08-07 15:05:47 +02002655 assert_match('something local wrong', v:exception)
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002656 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002657enddef
2658
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002659def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002660 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002661 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002662 vim9script
2663 try
2664 echoerr 'this'
2665 .. ' is ' ..
2666 'wrong'
2667 catch
2668 assert_match('this is wrong', v:exception)
2669 endtry
2670 END
2671 CheckScriptSuccess(lines)
2672enddef
2673
Bram Moolenaar7de62622021-08-07 15:05:47 +02002674def Test_echoconsole_cmd()
2675 var local = 'local'
2676 echoconsole 'something' local # comment
2677 # output goes anywhere
2678enddef
2679
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002680def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002681 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002682 vim9script
2683 new
2684 for var in range(0, 3)
2685 append(line('$'), var)
2686 endfor
2687 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2688 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01002689
2690 var result = ''
2691 for i in [1, 2, 3]
2692 var loop = ' loop ' .. i
2693 result ..= loop
2694 endfor
2695 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002696 END
2697 writefile(lines, 'Xvim9for.vim')
2698 source Xvim9for.vim
2699 delete('Xvim9for.vim')
2700enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701
rbtnnbebf0692021-08-21 17:26:50 +02002702def Test_for_skipped_block()
2703 # test skipped blocks at outside of function
2704 var lines =<< trim END
2705 var result = []
2706 if true
2707 for n in [1, 2]
2708 result += [n]
2709 endfor
2710 else
2711 for n in [3, 4]
2712 result += [n]
2713 endfor
2714 endif
2715 assert_equal([1, 2], result)
2716
2717 result = []
2718 if false
2719 for n in [1, 2]
2720 result += [n]
2721 endfor
2722 else
2723 for n in [3, 4]
2724 result += [n]
2725 endfor
2726 endif
2727 assert_equal([3, 4], result)
2728 END
2729 CheckDefAndScriptSuccess(lines)
2730
2731 # test skipped blocks at inside of function
2732 lines =<< trim END
2733 def DefTrue()
2734 var result = []
2735 if true
2736 for n in [1, 2]
2737 result += [n]
2738 endfor
2739 else
2740 for n in [3, 4]
2741 result += [n]
2742 endfor
2743 endif
2744 assert_equal([1, 2], result)
2745 enddef
2746 DefTrue()
2747
2748 def DefFalse()
2749 var result = []
2750 if false
2751 for n in [1, 2]
2752 result += [n]
2753 endfor
2754 else
2755 for n in [3, 4]
2756 result += [n]
2757 endfor
2758 endif
2759 assert_equal([3, 4], result)
2760 enddef
2761 DefFalse()
2762 END
2763 CheckDefAndScriptSuccess(lines)
2764enddef
2765
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002766def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002767 var lines =<< trim END
2768 var result = ''
2769 for cnt in range(7)
2770 if cnt == 4
2771 break
2772 endif
2773 if cnt == 2
2774 continue
2775 endif
2776 result ..= cnt .. '_'
2777 endfor
2778 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02002779
Bram Moolenaarf2253962021-04-13 20:53:13 +02002780 var concat = ''
2781 for str in eval('["one", "two"]')
2782 concat ..= str
2783 endfor
2784 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002785
Bram Moolenaarf2253962021-04-13 20:53:13 +02002786 var total = 0
2787 for nr in
2788 [1, 2, 3]
2789 total += nr
2790 endfor
2791 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002792
Bram Moolenaarf2253962021-04-13 20:53:13 +02002793 total = 0
2794 for nr
2795 in [1, 2, 3]
2796 total += nr
2797 endfor
2798 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002799
Bram Moolenaarf2253962021-04-13 20:53:13 +02002800 total = 0
2801 for nr
2802 in
2803 [1, 2, 3]
2804 total += nr
2805 endfor
2806 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01002807
Bram Moolenaara3589a02021-04-14 13:30:46 +02002808 # with type
2809 total = 0
2810 for n: number in [1, 2, 3]
2811 total += n
2812 endfor
2813 assert_equal(6, total)
2814
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002815 var chars = ''
2816 for s: string in 'foobar'
2817 chars ..= s
2818 endfor
2819 assert_equal('foobar', chars)
2820
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02002821 chars = ''
2822 for x: string in {a: 'a', b: 'b'}->values()
2823 chars ..= x
2824 endfor
2825 assert_equal('ab', chars)
2826
Bram Moolenaara3589a02021-04-14 13:30:46 +02002827 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02002828 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02002829 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2830 res ..= n .. s
2831 endfor
2832 assert_equal('1a2b', res)
2833
Bram Moolenaar444d8782021-06-26 12:40:56 +02002834 # unpack with one var
2835 var reslist = []
2836 for [x] in [['aaa'], ['bbb']]
2837 reslist->add(x)
2838 endfor
2839 assert_equal(['aaa', 'bbb'], reslist)
2840
Bram Moolenaara3589a02021-04-14 13:30:46 +02002841 # loop over string
2842 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02002843 for c in 'aéc̀d'
2844 res ..= c .. '-'
2845 endfor
2846 assert_equal('a-é-c̀-d-', res)
2847
2848 res = ''
2849 for c in ''
2850 res ..= c .. '-'
2851 endfor
2852 assert_equal('', res)
2853
2854 res = ''
2855 for c in test_null_string()
2856 res ..= c .. '-'
2857 endfor
2858 assert_equal('', res)
2859
2860 var foo: list<dict<any>> = [
2861 {a: 'Cat'}
2862 ]
2863 for dd in foo
2864 dd.counter = 12
2865 endfor
2866 assert_equal([{a: 'Cat', counter: 12}], foo)
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002867
2868 reslist = []
2869 for _ in range(3)
2870 reslist->add('x')
2871 endfor
2872 assert_equal(['x', 'x', 'x'], reslist)
Bram Moolenaarf2253962021-04-13 20:53:13 +02002873 END
2874 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002875enddef
2876
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02002877def Test_for_loop_with_closure()
2878 var lines =<< trim END
2879 var flist: list<func>
2880 for i in range(5)
2881 var inloop = i
2882 flist[i] = () => inloop
2883 endfor
2884 for i in range(5)
2885 assert_equal(4, flist[i]())
2886 endfor
2887 END
2888 CheckDefAndScriptSuccess(lines)
2889
2890 lines =<< trim END
2891 var flist: list<func>
2892 for i in range(5)
2893 var inloop = i
2894 flist[i] = () => {
2895 return inloop
2896 }
2897 endfor
2898 for i in range(5)
2899 assert_equal(4, flist[i]())
2900 endfor
2901 END
2902 CheckDefAndScriptSuccess(lines)
2903enddef
2904
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002905def Test_for_loop_fails()
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002906 CheckDefAndScriptFailure(['for '], ['E1097:', 'E690:'])
2907 CheckDefAndScriptFailure(['for x'], ['E1097:', 'E690:'])
2908 CheckDefAndScriptFailure(['for x in'], ['E1097:', 'E15:'])
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002909 CheckDefAndScriptFailure(['for # in range(5)'], 'E690:')
2910 CheckDefAndScriptFailure(['for i In range(5)'], 'E690:')
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002911 CheckDefAndScriptFailure(['var x = 5', 'for x in range(5)', 'endfor'], ['E1017:', 'E1041:'])
Bram Moolenaard4ab8072021-07-08 19:22:12 +02002912 CheckScriptFailure(['vim9script', 'var x = 5', 'for x in range(5)', '# comment', 'endfor'], 'E1041:', 3)
Bram Moolenaar822ba242020-05-24 23:00:18 +02002913 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002914 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02002915 CheckDefFailure(['for i in xxx'], 'E1001:')
2916 CheckDefFailure(['endfor'], 'E588:')
2917 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002918
2919 # wrong type detected at compile time
2920 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2921
2922 # wrong type detected at runtime
2923 g:adict = {a: 1}
2924 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2925 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002926
2927 var lines =<< trim END
2928 var d: list<dict<any>> = [{a: 0}]
2929 for e in d
2930 e = {a: 0, b: ''}
2931 endfor
2932 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002933 CheckDefAndScriptFailure(lines, ['E1018:', 'E46:'], 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002934
2935 lines =<< trim END
2936 for nr: number in ['foo']
2937 endfor
2938 END
2939 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaar404557e2021-07-05 21:41:48 +02002940
2941 lines =<< trim END
2942 for n : number in [1, 2]
2943 echo n
2944 endfor
2945 END
2946 CheckDefAndScriptFailure(lines, 'E1059:', 1)
Bram Moolenaarefc5db52021-07-07 21:21:30 +02002947
2948 lines =<< trim END
2949 var d: dict<number> = {a: 1, b: 2}
2950 for [k: job, v: job] in d->items()
2951 echo k v
2952 endfor
2953 END
2954 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected job but got string', 2)
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002955
2956 lines =<< trim END
2957 var i = 0
2958 for i in [1, 2, 3]
2959 echo i
2960 endfor
2961 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002962 CheckDefExecAndScriptFailure(lines, ['E1017:', 'E1041:'])
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002963
2964 lines =<< trim END
2965 var l = [0]
2966 for l[0] in [1, 2, 3]
2967 echo l[0]
2968 endfor
2969 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002970 CheckDefExecAndScriptFailure(lines, ['E461:', 'E1017:'])
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002971
2972 lines =<< trim END
2973 var d = {x: 0}
2974 for d.x in [1, 2, 3]
2975 echo d.x
2976 endfor
2977 END
Bram Moolenaar86b3ab42021-12-19 18:33:23 +00002978 CheckDefExecAndScriptFailure(lines, ['E461:', 'E1017:'])
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002979enddef
2980
Bram Moolenaarea870692020-12-02 14:24:30 +01002981def Test_for_loop_script_var()
2982 # cannot use s:var in a :def function
Bram Moolenaara99fb232021-12-20 12:25:03 +00002983 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1254:')
Bram Moolenaarea870692020-12-02 14:24:30 +01002984
2985 # can use s:var in Vim9 script, with or without s:
2986 var lines =<< trim END
2987 vim9script
2988 var total = 0
2989 for s:var in [1, 2, 3]
2990 total += s:var
2991 endfor
2992 assert_equal(6, total)
2993
2994 total = 0
2995 for var in [1, 2, 3]
2996 total += var
2997 endfor
2998 assert_equal(6, total)
2999 END
3000enddef
3001
Bram Moolenaar792f7862020-11-23 08:31:18 +01003002def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01003003 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01003004 var result = []
3005 for [v1, v2] in [[1, 2], [3, 4]]
3006 result->add(v1)
3007 result->add(v2)
3008 endfor
3009 assert_equal([1, 2, 3, 4], result)
3010
3011 result = []
3012 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
3013 result->add(v1)
3014 result->add(v2)
3015 result->add(v3)
3016 endfor
3017 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
3018
3019 result = []
3020 for [&ts, &sw] in [[1, 2], [3, 4]]
3021 result->add(&ts)
3022 result->add(&sw)
3023 endfor
3024 assert_equal([1, 2, 3, 4], result)
3025
3026 var slist: list<string>
3027 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
3028 slist->add($LOOPVAR)
3029 slist->add(@r)
3030 slist->add(v:errmsg)
3031 endfor
3032 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
3033
3034 slist = []
3035 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
3036 slist->add(g:globalvar)
3037 slist->add(b:bufvar)
3038 slist->add(w:winvar)
3039 slist->add(t:tabvar)
3040 endfor
3041 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01003042 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaarb777da92021-05-22 21:40:39 +02003043
3044 var res = []
3045 for [_, n, _] in [[1, 2, 3], [4, 5, 6]]
3046 res->add(n)
3047 endfor
3048 assert_equal([2, 5], res)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01003049 END
3050 CheckDefAndScriptSuccess(lines)
3051
3052 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01003053 for [v1, v2] in [[1, 2, 3], [3, 4]]
3054 echo v1 v2
3055 endfor
3056 END
3057 CheckDefExecFailure(lines, 'E710:', 1)
3058
3059 lines =<< trim END
3060 for [v1, v2] in [[1], [3, 4]]
3061 echo v1 v2
3062 endfor
3063 END
3064 CheckDefExecFailure(lines, 'E711:', 1)
3065
3066 lines =<< trim END
3067 for [v1, v1] in [[1, 2], [3, 4]]
3068 echo v1
3069 endfor
3070 END
3071 CheckDefExecFailure(lines, 'E1017:', 1)
3072enddef
3073
Bram Moolenaarc150c092021-02-13 15:02:46 +01003074def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02003075 var lines =<< trim END
3076 var looped = 0
3077 var cleanup = 0
3078 for i in range(3)
3079 looped += 1
3080 try
3081 eval [][0]
3082 catch
3083 continue
3084 finally
3085 cleanup += 1
3086 endtry
3087 endfor
3088 assert_equal(3, looped)
3089 assert_equal(3, cleanup)
3090 END
3091 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003092enddef
3093
rbtnnd895b1d2021-08-20 20:54:25 +02003094def Test_while_skipped_block()
3095 # test skipped blocks at outside of function
3096 var lines =<< trim END
3097 var result = []
3098 var n = 0
3099 if true
3100 n = 1
3101 while n < 3
3102 result += [n]
3103 n += 1
3104 endwhile
3105 else
3106 n = 3
3107 while n < 5
3108 result += [n]
3109 n += 1
3110 endwhile
3111 endif
3112 assert_equal([1, 2], result)
3113
3114 result = []
3115 if false
3116 n = 1
3117 while n < 3
3118 result += [n]
3119 n += 1
3120 endwhile
3121 else
3122 n = 3
3123 while n < 5
3124 result += [n]
3125 n += 1
3126 endwhile
3127 endif
3128 assert_equal([3, 4], result)
3129 END
3130 CheckDefAndScriptSuccess(lines)
3131
3132 # test skipped blocks at inside of function
3133 lines =<< trim END
3134 def DefTrue()
3135 var result = []
3136 var n = 0
3137 if true
3138 n = 1
3139 while n < 3
3140 result += [n]
3141 n += 1
3142 endwhile
3143 else
3144 n = 3
3145 while n < 5
3146 result += [n]
3147 n += 1
3148 endwhile
3149 endif
3150 assert_equal([1, 2], result)
3151 enddef
3152 DefTrue()
3153
3154 def DefFalse()
3155 var result = []
3156 var n = 0
3157 if false
3158 n = 1
3159 while n < 3
3160 result += [n]
3161 n += 1
3162 endwhile
3163 else
3164 n = 3
3165 while n < 5
3166 result += [n]
3167 n += 1
3168 endwhile
3169 endif
3170 assert_equal([3, 4], result)
3171 enddef
3172 DefFalse()
3173 END
3174 CheckDefAndScriptSuccess(lines)
3175enddef
3176
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003177def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003178 var result = ''
3179 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003180 while cnt < 555
3181 if cnt == 3
3182 break
3183 endif
3184 cnt += 1
3185 if cnt == 2
3186 continue
3187 endif
3188 result ..= cnt .. '_'
3189 endwhile
3190 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01003191
3192 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01003193 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01003194 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003195enddef
3196
Bram Moolenaar7a53f292021-11-22 18:31:02 +00003197def Test_while_loop_in_script()
3198 var lines =<< trim END
3199 vim9script
3200 var result = ''
3201 var cnt = 0
3202 while cnt < 3
3203 var s = 'v' .. cnt
3204 result ..= s
3205 cnt += 1
3206 endwhile
3207 assert_equal('v0v1v2', result)
3208 END
3209 CheckScriptSuccess(lines)
3210enddef
3211
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003212def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02003213 CheckDefFailure(['while xxx'], 'E1001:')
3214 CheckDefFailure(['endwhile'], 'E588:')
3215 CheckDefFailure(['continue'], 'E586:')
3216 CheckDefFailure(['if true', 'continue'], 'E586:')
3217 CheckDefFailure(['break'], 'E587:')
3218 CheckDefFailure(['if true', 'break'], 'E587:')
3219 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01003220
3221 var lines =<< trim END
3222 var s = ''
3223 while s = ''
3224 endwhile
3225 END
3226 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003227enddef
3228
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003229def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003230 var caught = false
3231 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003232 try
3233 while 1
3234 x += 1
3235 if x == 100
3236 feedkeys("\<C-C>", 'Lt')
3237 endif
3238 endwhile
3239 catch
3240 caught = true
3241 assert_equal(100, x)
3242 endtry
3243 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003244 # consume the CTRL-C
3245 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003246enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01003247
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003248def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003249 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003250 'one',
3251 'two',
3252 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003253 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003254 assert_equal(['one', 'two', 'three'], mylist)
3255
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003256 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003257 ['one']: 1,
3258 ['two']: 2,
3259 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003260 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003261 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01003262 assert_equal({one: 1, two: 2, three: 3}, mydict)
3263 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003264 one: 1, # comment
3265 two: # comment
3266 2, # comment
3267 three: 3 # comment
3268 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003269 assert_equal({one: 1, two: 2, three: 3}, mydict)
3270 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003271 one: 1,
3272 two:
3273 2,
3274 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003275 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003276 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02003277
3278 assert_equal(
3279 ['one', 'two', 'three'],
3280 split('one two three')
3281 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003282enddef
3283
Bram Moolenaar7a092242020-04-16 22:10:49 +02003284def Test_vim9_comment()
3285 CheckScriptSuccess([
3286 'vim9script',
3287 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01003288 '#something',
3289 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003290 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01003291
3292 split Xfile
3293 CheckScriptSuccess([
3294 'vim9script',
3295 'edit #something',
3296 ])
3297 CheckScriptSuccess([
3298 'vim9script',
3299 'edit #{something',
3300 ])
3301 close
3302
Bram Moolenaar7a092242020-04-16 22:10:49 +02003303 CheckScriptFailure([
3304 'vim9script',
3305 ':# something',
3306 ], 'E488:')
3307 CheckScriptFailure([
3308 '# something',
3309 ], 'E488:')
3310 CheckScriptFailure([
3311 ':# something',
3312 ], 'E488:')
3313
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003314 { # block start
3315 } # block end
3316 CheckDefFailure([
3317 '{# comment',
3318 ], 'E488:')
3319 CheckDefFailure([
3320 '{',
3321 '}# comment',
3322 ], 'E488:')
3323
3324 echo "yes" # comment
3325 CheckDefFailure([
3326 'echo "yes"# comment',
3327 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003328 CheckScriptSuccess([
3329 'vim9script',
3330 'echo "yes" # something',
3331 ])
3332 CheckScriptFailure([
3333 'vim9script',
3334 'echo "yes"# something',
3335 ], 'E121:')
3336 CheckScriptFailure([
3337 'vim9script',
3338 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003339 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003340 CheckScriptFailure([
3341 'echo "yes" # something',
3342 ], 'E121:')
3343
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003344 exe "echo" # comment
3345 CheckDefFailure([
3346 'exe "echo"# comment',
3347 ], 'E488:')
3348 CheckScriptSuccess([
3349 'vim9script',
3350 'exe "echo" # something',
3351 ])
3352 CheckScriptFailure([
3353 'vim9script',
3354 'exe "echo"# something',
3355 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003356 CheckScriptFailure([
3357 'vim9script',
3358 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003359 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003360 CheckScriptFailure([
3361 'exe "echo" # something',
3362 ], 'E121:')
3363
Bram Moolenaar7a092242020-04-16 22:10:49 +02003364 CheckDefFailure([
3365 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003366 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003367 'catch',
3368 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003369 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003370 CheckScriptFailure([
3371 'vim9script',
3372 'try# comment',
3373 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003374 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003375 CheckDefFailure([
3376 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003377 ' throw#comment',
3378 'catch',
3379 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003380 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003381 CheckDefFailure([
3382 'try',
3383 ' throw "yes"#comment',
3384 'catch',
3385 'endtry',
3386 ], 'E488:')
3387 CheckDefFailure([
3388 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003389 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003390 'catch# comment',
3391 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003392 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003393 CheckScriptFailure([
3394 'vim9script',
3395 'try',
3396 ' echo "yes"',
3397 'catch# comment',
3398 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003399 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003400 CheckDefFailure([
3401 'try',
3402 ' echo "yes"',
3403 'catch /pat/# comment',
3404 'endtry',
3405 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003406 CheckDefFailure([
3407 'try',
3408 'echo "yes"',
3409 'catch',
3410 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003411 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003412 CheckScriptFailure([
3413 'vim9script',
3414 'try',
3415 ' echo "yes"',
3416 'catch',
3417 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003418 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003419
3420 CheckScriptSuccess([
3421 'vim9script',
3422 'hi # comment',
3423 ])
3424 CheckScriptFailure([
3425 'vim9script',
3426 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003427 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003428 CheckScriptSuccess([
3429 'vim9script',
3430 'hi Search # comment',
3431 ])
3432 CheckScriptFailure([
3433 'vim9script',
3434 'hi Search# comment',
3435 ], 'E416:')
3436 CheckScriptSuccess([
3437 'vim9script',
3438 'hi link This Search # comment',
3439 ])
3440 CheckScriptFailure([
3441 'vim9script',
3442 'hi link This That# comment',
3443 ], 'E413:')
3444 CheckScriptSuccess([
3445 'vim9script',
3446 'hi clear This # comment',
3447 'hi clear # comment',
3448 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003449 # not tested, because it doesn't give an error but a warning:
3450 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02003451 CheckScriptFailure([
3452 'vim9script',
3453 'hi clear# comment',
3454 ], 'E416:')
3455
3456 CheckScriptSuccess([
3457 'vim9script',
3458 'hi Group term=bold',
3459 'match Group /todo/ # comment',
3460 ])
3461 CheckScriptFailure([
3462 'vim9script',
3463 'hi Group term=bold',
3464 'match Group /todo/# comment',
3465 ], 'E488:')
3466 CheckScriptSuccess([
3467 'vim9script',
3468 'match # comment',
3469 ])
3470 CheckScriptFailure([
3471 'vim9script',
3472 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003473 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003474 CheckScriptSuccess([
3475 'vim9script',
3476 'match none # comment',
3477 ])
3478 CheckScriptFailure([
3479 'vim9script',
3480 'match none# comment',
3481 ], 'E475:')
3482
3483 CheckScriptSuccess([
3484 'vim9script',
3485 'menutrans clear # comment',
3486 ])
3487 CheckScriptFailure([
3488 'vim9script',
3489 'menutrans clear# comment text',
3490 ], 'E474:')
3491
3492 CheckScriptSuccess([
3493 'vim9script',
3494 'syntax clear # comment',
3495 ])
3496 CheckScriptFailure([
3497 'vim9script',
3498 'syntax clear# comment text',
3499 ], 'E28:')
3500 CheckScriptSuccess([
3501 'vim9script',
3502 'syntax keyword Word some',
3503 'syntax clear Word # comment',
3504 ])
3505 CheckScriptFailure([
3506 'vim9script',
3507 'syntax keyword Word some',
3508 'syntax clear Word# comment text',
3509 ], 'E28:')
3510
3511 CheckScriptSuccess([
3512 'vim9script',
3513 'syntax list # comment',
3514 ])
3515 CheckScriptFailure([
3516 'vim9script',
3517 'syntax list# comment text',
3518 ], 'E28:')
3519
3520 CheckScriptSuccess([
3521 'vim9script',
3522 'syntax match Word /pat/ oneline # comment',
3523 ])
3524 CheckScriptFailure([
3525 'vim9script',
3526 'syntax match Word /pat/ oneline# comment',
3527 ], 'E475:')
3528
3529 CheckScriptSuccess([
3530 'vim9script',
3531 'syntax keyword Word word # comm[ent',
3532 ])
3533 CheckScriptFailure([
3534 'vim9script',
3535 'syntax keyword Word word# comm[ent',
3536 ], 'E789:')
3537
3538 CheckScriptSuccess([
3539 'vim9script',
3540 'syntax match Word /pat/ # comment',
3541 ])
3542 CheckScriptFailure([
3543 'vim9script',
3544 'syntax match Word /pat/# comment',
3545 ], 'E402:')
3546
3547 CheckScriptSuccess([
3548 'vim9script',
3549 'syntax match Word /pat/ contains=Something # comment',
3550 ])
3551 CheckScriptFailure([
3552 'vim9script',
3553 'syntax match Word /pat/ contains=Something# comment',
3554 ], 'E475:')
3555 CheckScriptFailure([
3556 'vim9script',
3557 'syntax match Word /pat/ contains= # comment',
3558 ], 'E406:')
3559 CheckScriptFailure([
3560 'vim9script',
3561 'syntax match Word /pat/ contains=# comment',
3562 ], 'E475:')
3563
3564 CheckScriptSuccess([
3565 'vim9script',
3566 'syntax region Word start=/pat/ end=/pat/ # comment',
3567 ])
3568 CheckScriptFailure([
3569 'vim9script',
3570 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02003571 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003572
3573 CheckScriptSuccess([
3574 'vim9script',
3575 'syntax sync # comment',
3576 ])
3577 CheckScriptFailure([
3578 'vim9script',
3579 'syntax sync# comment',
3580 ], 'E404:')
3581 CheckScriptSuccess([
3582 'vim9script',
3583 'syntax sync ccomment # comment',
3584 ])
3585 CheckScriptFailure([
3586 'vim9script',
3587 'syntax sync ccomment# comment',
3588 ], 'E404:')
3589
3590 CheckScriptSuccess([
3591 'vim9script',
3592 'syntax cluster Some contains=Word # comment',
3593 ])
3594 CheckScriptFailure([
3595 'vim9script',
3596 'syntax cluster Some contains=Word# comment',
3597 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003598
3599 CheckScriptSuccess([
3600 'vim9script',
3601 'command Echo echo # comment',
3602 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003603 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003604 ])
3605 CheckScriptFailure([
3606 'vim9script',
3607 'command Echo echo# comment',
3608 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003609 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003610 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003611
3612 var curdir = getcwd()
3613 CheckScriptSuccess([
3614 'command Echo cd " comment',
3615 'Echo',
3616 'delcommand Echo',
3617 ])
3618 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01003619 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003620 'command Echo cd # comment',
3621 'Echo',
3622 'delcommand Echo',
3623 ])
3624 CheckScriptFailure([
3625 'vim9script',
3626 'command Echo cd " comment',
3627 'Echo',
3628 ], 'E344:')
3629 delcommand Echo
3630 chdir(curdir)
3631
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003632 CheckScriptFailure([
3633 'vim9script',
3634 'command Echo# comment',
3635 ], 'E182:')
3636 CheckScriptFailure([
3637 'vim9script',
3638 'command Echo echo',
3639 'command Echo# comment',
3640 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003641 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003642
3643 CheckScriptSuccess([
3644 'vim9script',
3645 'function # comment',
3646 ])
3647 CheckScriptFailure([
3648 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02003649 'function " comment',
3650 ], 'E129:')
3651 CheckScriptFailure([
3652 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003653 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003654 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003655 CheckScriptSuccess([
3656 'vim9script',
3657 'function CheckScriptSuccess # comment',
3658 ])
3659 CheckScriptFailure([
3660 'vim9script',
3661 'function CheckScriptSuccess# comment',
3662 ], 'E488:')
3663
3664 CheckScriptSuccess([
3665 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003666 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003667 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003668 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003669 ])
3670 CheckScriptFailure([
3671 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003672 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003673 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003674 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003675 ], 'E488:')
3676
3677 CheckScriptSuccess([
3678 'vim9script',
3679 'call execute("ls") # comment',
3680 ])
3681 CheckScriptFailure([
3682 'vim9script',
3683 'call execute("ls")# comment',
3684 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02003685
3686 CheckScriptFailure([
3687 'def Test() " comment',
3688 'enddef',
3689 ], 'E488:')
3690 CheckScriptFailure([
3691 'vim9script',
3692 'def Test() " comment',
3693 'enddef',
3694 ], 'E488:')
3695
3696 CheckScriptSuccess([
3697 'func Test() " comment',
3698 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003699 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02003700 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02003701 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02003702 'vim9script',
3703 'func Test() " comment',
3704 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02003705 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02003706
3707 CheckScriptSuccess([
3708 'def Test() # comment',
3709 'enddef',
3710 ])
3711 CheckScriptFailure([
3712 'func Test() # comment',
3713 'endfunc',
3714 ], 'E488:')
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003715
3716 var lines =<< trim END
3717 vim9script
3718 syn region Text
3719 \ start='foo'
3720 #\ comment
3721 \ end='bar'
Bram Moolenaar5072b472021-06-03 21:56:10 +02003722 syn region Text start='foo'
3723 #\ comment
3724 \ end='bar'
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003725 END
3726 CheckScriptSuccess(lines)
3727
3728 lines =<< trim END
3729 vim9script
3730 syn region Text
3731 \ start='foo'
3732 "\ comment
3733 \ end='bar'
3734 END
3735 CheckScriptFailure(lines, 'E399:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003736enddef
3737
3738def Test_vim9_comment_gui()
3739 CheckCanRunGui
3740
3741 CheckScriptFailure([
3742 'vim9script',
3743 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003744 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003745 CheckScriptFailure([
3746 'vim9script',
3747 'gui -f#comment'
Bram Moolenaarb8bd2e62021-08-21 17:13:14 +02003748 ], 'E194:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003749enddef
3750
Bram Moolenaara26b9702020-04-18 19:53:28 +02003751def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02003752 au TabEnter *.vim g:entered = 1
3753 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02003754
3755 edit test.vim
3756 doautocmd TabEnter #comment
3757 assert_equal(1, g:entered)
3758
3759 doautocmd TabEnter f.x
3760 assert_equal(2, g:entered)
3761
3762 g:entered = 0
3763 doautocmd TabEnter f.x #comment
3764 assert_equal(2, g:entered)
3765
3766 assert_fails('doautocmd Syntax#comment', 'E216:')
3767
3768 au! TabEnter
3769 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003770
3771 CheckScriptSuccess([
3772 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02003773 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003774 'b:var = 456',
3775 'w:var = 777',
3776 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003777 'unlet g:var w:var # something',
3778 ])
3779
3780 CheckScriptFailure([
3781 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003782 'let var = 123',
3783 ], 'E1126: Cannot use :let in Vim9 script')
3784
3785 CheckScriptFailure([
3786 'vim9script',
3787 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003788 ], 'E1016: Cannot declare a global variable:')
3789
3790 CheckScriptFailure([
3791 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003792 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003793 ], 'E1016: Cannot declare a buffer variable:')
3794
3795 CheckScriptFailure([
3796 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003797 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003798 ], 'E1016: Cannot declare a window variable:')
3799
3800 CheckScriptFailure([
3801 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003802 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003803 ], 'E1016: Cannot declare a tab variable:')
3804
3805 CheckScriptFailure([
3806 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003807 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003808 ], 'E1016: Cannot declare a v: variable:')
3809
3810 CheckScriptFailure([
3811 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003812 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003813 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02003814
3815 CheckScriptFailure([
3816 'vim9script',
3817 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003818 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003819 ], 'E108:')
3820
3821 CheckScriptFailure([
3822 'let g:var = 123',
3823 'unlet g:var # something',
3824 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003825
3826 CheckScriptSuccess([
3827 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003828 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003829 ' echo "yes"',
3830 'elseif 2 #comment',
3831 ' echo "no"',
3832 'endif',
3833 ])
3834
3835 CheckScriptFailure([
3836 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003837 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003838 ' echo "yes"',
3839 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003840 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003841
3842 CheckScriptFailure([
3843 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003844 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003845 ' echo "yes"',
3846 'elseif 2#comment',
3847 ' echo "no"',
3848 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003849 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003850
3851 CheckScriptSuccess([
3852 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003853 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003854 ])
3855
3856 CheckScriptFailure([
3857 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003858 'var v = 1# comment6',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003859 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003860
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003861 CheckScriptSuccess([
3862 'vim9script',
3863 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003864 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003865 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003866 'dsearch /pat/ #comment',
3867 'bwipe!',
3868 ])
3869
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003870 CheckScriptFailure([
3871 'vim9script',
3872 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003873 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003874 ':$',
3875 'dsearch /pat/#comment',
3876 'bwipe!',
3877 ], 'E488:')
3878
3879 CheckScriptFailure([
3880 'vim9script',
3881 'func! SomeFunc()',
3882 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02003883enddef
3884
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003885def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003886 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003887 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02003888 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003889 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02003890 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003891 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02003892 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003893 END
3894 writefile(lines, 'Xfinished')
3895 source Xfinished
3896 assert_equal('two', g:res)
3897
3898 unlet g:res
3899 delete('Xfinished')
3900enddef
3901
Bram Moolenaara5d00772020-05-14 23:20:55 +02003902def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003903 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02003904 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02003905 def GetValue(): string
3906 return theVal
3907 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003908 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02003909 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02003910 theVal = 'else'
3911 g:laterVal = GetValue()
3912 END
3913 writefile(lines, 'Xforward')
3914 source Xforward
3915 assert_equal('something', g:initVal)
3916 assert_equal('else', g:laterVal)
3917
3918 unlet g:initVal
3919 unlet g:laterVal
3920 delete('Xforward')
3921enddef
3922
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003923def Test_source_vim9_from_legacy()
Bram Moolenaara6294952020-12-27 13:39:50 +01003924 var vim9_lines =<< trim END
3925 vim9script
3926 var local = 'local'
3927 g:global = 'global'
3928 export var exported = 'exported'
3929 export def GetText(): string
3930 return 'text'
3931 enddef
3932 END
3933 writefile(vim9_lines, 'Xvim9_script.vim')
3934
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003935 var legacy_lines =<< trim END
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003936 source Xvim9_script.vim
3937
3938 call assert_false(exists('local'))
3939 call assert_false(exists('exported'))
3940 call assert_false(exists('s:exported'))
3941 call assert_equal('global', global)
3942 call assert_equal('global', g:global)
3943
3944 " imported variable becomes script-local
3945 import exported from './Xvim9_script.vim'
3946 call assert_equal('exported', s:exported)
3947 call assert_false(exists('exported'))
3948
3949 " imported function becomes script-local
3950 import GetText from './Xvim9_script.vim'
3951 call assert_equal('text', s:GetText())
3952 call assert_false(exists('*GetText'))
3953 END
3954 writefile(legacy_lines, 'Xlegacy_script.vim')
3955
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003956 source Xlegacy_script.vim
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003957 assert_equal('global', g:global)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003958 unlet g:global
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003959
3960 delete('Xlegacy_script.vim')
3961 delete('Xvim9_script.vim')
3962enddef
Bram Moolenaara5d00772020-05-14 23:20:55 +02003963
Bram Moolenaare535db82021-03-31 21:07:24 +02003964def Test_declare_script_in_func()
3965 var lines =<< trim END
3966 vim9script
3967 func Declare()
3968 let s:local = 123
3969 endfunc
3970 Declare()
3971 assert_equal(123, local)
3972
3973 var error: string
3974 try
3975 local = 'asdf'
3976 catch
3977 error = v:exception
3978 endtry
3979 assert_match('E1012: Type mismatch; expected number but got string', error)
3980
3981 lockvar local
3982 try
3983 local = 999
3984 catch
3985 error = v:exception
3986 endtry
3987 assert_match('E741: Value is locked: local', error)
3988 END
3989 CheckScriptSuccess(lines)
3990enddef
3991
3992
Bram Moolenaar7d699702020-08-14 20:52:28 +02003993func Test_vim9script_not_global()
3994 " check that items defined in Vim9 script are script-local, not global
3995 let vim9lines =<< trim END
3996 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003997 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02003998 func TheFunc()
3999 echo 'local'
4000 endfunc
4001 def DefFunc()
4002 echo 'local'
4003 enddef
4004 END
4005 call writefile(vim9lines, 'Xvim9script.vim')
4006 source Xvim9script.vim
4007 try
4008 echo g:var
4009 assert_report('did not fail')
4010 catch /E121:/
4011 " caught
4012 endtry
4013 try
4014 call TheFunc()
4015 assert_report('did not fail')
4016 catch /E117:/
4017 " caught
4018 endtry
4019 try
4020 call DefFunc()
4021 assert_report('did not fail')
4022 catch /E117:/
4023 " caught
4024 endtry
4025
Bram Moolenaar25859dd2020-08-30 12:54:53 +02004026 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02004027endfunc
4028
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02004029def Test_vim9_copen()
4030 # this was giving an error for setting w:quickfix_title
4031 copen
4032 quit
4033enddef
4034
Bram Moolenaar03290b82020-12-19 16:30:44 +01004035" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004036def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004037 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004038 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01004039 def some#gettest(): string
4040 return 'test'
4041 enddef
4042 g:some#name = 'name'
Bram Moolenaar227c58a2021-04-28 20:40:44 +02004043 g:some#dict = {key: 'value'}
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01004044
4045 def some#varargs(a1: string, ...l: list<string>): string
4046 return a1 .. l[0] .. l[1]
4047 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01004048 END
4049
4050 mkdir('Xdir/autoload', 'p')
4051 writefile(lines, 'Xdir/autoload/some.vim')
4052 var save_rtp = &rtp
4053 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4054
4055 assert_equal('test', g:some#gettest())
4056 assert_equal('name', g:some#name)
Bram Moolenaar227c58a2021-04-28 20:40:44 +02004057 assert_equal('value', g:some#dict.key)
Bram Moolenaar03290b82020-12-19 16:30:44 +01004058 g:some#other = 'other'
4059 assert_equal('other', g:some#other)
4060
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01004061 assert_equal('abc', some#varargs('a', 'b', 'c'))
4062
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004063 # upper case script name works
4064 lines =<< trim END
4065 vim9script
4066 def Other#getOther(): string
4067 return 'other'
4068 enddef
4069 END
4070 writefile(lines, 'Xdir/autoload/Other.vim')
4071 assert_equal('other', g:Other#getOther())
4072
Bram Moolenaar03290b82020-12-19 16:30:44 +01004073 delete('Xdir', 'rf')
4074 &rtp = save_rtp
4075enddef
4076
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00004077" test disassembling an auto-loaded function starting with "debug"
4078def Test_vim9_autoload_disass()
4079 mkdir('Xdir/autoload', 'p')
4080 var save_rtp = &rtp
4081 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4082
4083 var lines =<< trim END
4084 vim9script
4085 def debugit#test(): string
4086 return 'debug'
4087 enddef
4088 END
4089 writefile(lines, 'Xdir/autoload/debugit.vim')
4090
4091 lines =<< trim END
4092 vim9script
4093 def profileit#test(): string
4094 return 'profile'
4095 enddef
4096 END
4097 writefile(lines, 'Xdir/autoload/profileit.vim')
4098
4099 lines =<< trim END
4100 vim9script
4101 assert_equal('debug', debugit#test())
4102 disass debugit#test
4103 assert_equal('profile', profileit#test())
4104 disass profileit#test
4105 END
4106 CheckScriptSuccess(lines)
4107
4108 delete('Xdir', 'rf')
4109 &rtp = save_rtp
4110enddef
4111
Bram Moolenaar03290b82020-12-19 16:30:44 +01004112" test using a vim9script that is auto-loaded from an autocmd
4113def Test_vim9_aucmd_autoload()
4114 var lines =<< trim END
4115 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004116 def foo#test()
4117 echomsg getreg('"')
4118 enddef
4119 END
4120
4121 mkdir('Xdir/autoload', 'p')
4122 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004123 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004124 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4125 augroup test
4126 autocmd TextYankPost * call foo#test()
4127 augroup END
4128
4129 normal Y
4130
4131 augroup test
4132 autocmd!
4133 augroup END
4134 delete('Xdir', 'rf')
4135 &rtp = save_rtp
4136enddef
4137
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004138" This was causing a crash because suppress_errthrow wasn't reset.
4139def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004140 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004141 vim9script
4142 def crash#func()
4143 try
4144 for x in List()
4145 endfor
4146 catch
4147 endtry
4148 g:ok = true
4149 enddef
4150 fu List()
4151 invalid
4152 endfu
4153 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01004154 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004155 catch /wontmatch/
4156 endtry
4157 END
4158 call mkdir('Xruntime/autoload', 'p')
4159 call writefile(lines, 'Xruntime/autoload/crash.vim')
4160
4161 # run in a separate Vim to avoid the side effects of assert_fails()
4162 lines =<< trim END
4163 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
4164 call crash#func()
4165 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004166 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004167 END
4168 writefile(lines, 'Xscript')
4169 RunVim([], [], '-S Xscript')
4170 assert_equal(['ok'], readfile('Xdidit'))
4171
4172 delete('Xdidit')
4173 delete('Xscript')
4174 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01004175
4176 lines =<< trim END
4177 vim9script
4178 var foo#bar = 'asdf'
4179 END
4180 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004181enddef
4182
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02004183def Test_script_var_in_autocmd()
4184 # using a script variable from an autocommand, defined in a :def function in a
4185 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004186 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02004187 let s:counter = 1
4188 def s:Func()
4189 au! CursorHold
4190 au CursorHold * s:counter += 1
4191 enddef
4192 call s:Func()
4193 doau CursorHold
4194 call assert_equal(2, s:counter)
4195 au! CursorHold
4196 END
4197 CheckScriptSuccess(lines)
4198enddef
4199
Bram Moolenaarb5841b92021-07-15 18:09:53 +02004200def Test_error_in_autoload_script()
4201 var save_rtp = &rtp
4202 var dir = getcwd() .. '/Xruntime'
4203 &rtp = dir
4204 mkdir(dir .. '/autoload', 'p')
4205
4206 var lines =<< trim END
4207 vim9script noclear
4208 def script#autoloaded()
4209 enddef
4210 def Broken()
4211 var x: any = ''
4212 eval x != 0
4213 enddef
4214 Broken()
4215 END
4216 writefile(lines, dir .. '/autoload/script.vim')
4217
4218 lines =<< trim END
4219 vim9script
4220 def CallAutoloaded()
4221 script#autoloaded()
4222 enddef
4223
4224 function Legacy()
4225 try
4226 call s:CallAutoloaded()
4227 catch
4228 call assert_match('E1030: Using a String as a Number', v:exception)
4229 endtry
4230 endfunction
4231
4232 Legacy()
4233 END
4234 CheckScriptSuccess(lines)
4235
4236 &rtp = save_rtp
4237 delete(dir, 'rf')
4238enddef
4239
Bram Moolenaar3896a102020-08-09 14:33:55 +02004240def Test_cmdline_win()
4241 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
4242 # the command line window.
4243 mkdir('rtp/syntax', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004244 var export_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02004245 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004246 export var That = 'yes'
Bram Moolenaar3896a102020-08-09 14:33:55 +02004247 END
4248 writefile(export_lines, 'rtp/syntax/Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004249 var import_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02004250 vim9script
4251 import That from './Xexport.vim'
4252 END
4253 writefile(import_lines, 'rtp/syntax/vim.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004254 var save_rtp = &rtp
Bram Moolenaar3896a102020-08-09 14:33:55 +02004255 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
4256 syntax on
4257 augroup CmdWin
4258 autocmd CmdwinEnter * g:got_there = 'yes'
4259 augroup END
4260 # this will open and also close the cmdline window
4261 feedkeys('q:', 'xt')
4262 assert_equal('yes', g:got_there)
4263
4264 augroup CmdWin
4265 au!
4266 augroup END
4267 &rtp = save_rtp
4268 delete('rtp', 'rf')
4269enddef
4270
Bram Moolenaare3d46852020-08-29 13:39:17 +02004271def Test_invalid_sid()
4272 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02004273
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004274 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02004275 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02004276 endif
4277 delete('Xdidit')
4278enddef
4279
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004280def Test_restoring_cpo()
4281 writefile(['vim9script', 'set nocp'], 'Xsourced')
4282 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
4283 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
4284 assert_equal(['done'], readfile('Xdone'))
4285 endif
4286 delete('Xsourced')
4287 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01004288 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01004289
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00004290 writefile(['vim9script', 'g:cpoval = &cpo'], 'XanotherScript')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01004291 set cpo=aABceFsMny>
4292 edit XanotherScript
4293 so %
4294 assert_equal('aABceFsMny>', &cpo)
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00004295 assert_equal('aABceFs', g:cpoval)
Bram Moolenaar0123cc12021-02-07 17:17:58 +01004296 :1del
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00004297 setline(1, 'let g:cpoval = &cpo')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01004298 w
4299 so %
4300 assert_equal('aABceFsMny>', &cpo)
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00004301 assert_equal('aABceFsMny>', g:cpoval)
Bram Moolenaar0123cc12021-02-07 17:17:58 +01004302
4303 delete('XanotherScript')
4304 set cpo&vim
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00004305 unlet g:cpoval
4306
4307 if has('unix')
4308 # 'cpo' is not restored in main vimrc
4309 var save_HOME = $HOME
4310 $HOME = getcwd() .. '/Xhome'
4311 mkdir('Xhome')
4312 var lines =<< trim END
4313 vim9script
4314 writefile(['before: ' .. &cpo], 'Xresult')
4315 set cpo+=M
4316 writefile(['after: ' .. &cpo], 'Xresult', 'a')
4317 END
4318 writefile(lines, 'Xhome/.vimrc')
4319
4320 lines =<< trim END
4321 call writefile(['later: ' .. &cpo], 'Xresult', 'a')
4322 END
4323 writefile(lines, 'Xlegacy')
4324
4325 lines =<< trim END
4326 vim9script
4327 call writefile(['vim9: ' .. &cpo], 'Xresult', 'a')
4328 qa
4329 END
4330 writefile(lines, 'Xvim9')
4331
4332 var cmd = GetVimCommand() .. " -S Xlegacy -S Xvim9"
4333 cmd = substitute(cmd, '-u NONE', '', '')
4334 exe "silent !" .. cmd
4335
4336 assert_equal([
4337 'before: aABceFs',
4338 'after: aABceFsM',
4339 'later: aABceFsM',
4340 'vim9: aABceFs'], readfile('Xresult'))
4341
4342 $HOME = save_HOME
4343 delete('Xhome', 'rf')
4344 delete('Xlegacy')
4345 delete('Xvim9')
4346 delete('Xresult')
4347 endif
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004348enddef
4349
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004350" Use :function so we can use Check commands
4351func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004352 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004353 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004354
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004355 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004356 vim9script
4357 def script#func()
4358 enddef
4359 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004360 call mkdir('Xdir/autoload', 'p')
4361 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004362
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004363 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004364 vim9script
4365 set cpo+=M
4366 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02004367 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004368 setline(1, 'some text')
4369 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004370 call writefile(lines, 'XTest_redraw_cpo')
4371 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
4372 call term_sendkeys(buf, "V:")
4373 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004374
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004375 " clean up
4376 call term_sendkeys(buf, "\<Esc>u")
4377 call StopVimInTerminal(buf)
4378 call delete('XTest_redraw_cpo')
4379 call delete('Xdir', 'rf')
4380endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004381
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004382
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02004383def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004384 var lines =<< trim END
4385 var name: any
4386 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02004387 END
4388 CheckDefAndScriptSuccess(lines)
4389enddef
4390
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02004391func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02004392 CheckRunVimInTerminal
4393
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02004394 " call indirectly to avoid compilation error for missing functions
4395 call Run_Test_define_func_at_command_line()
4396endfunc
4397
4398def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004399 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004400 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004401 func CheckAndQuit()
4402 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
4403 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
4404 endfunc
4405 END
4406 writefile([''], 'Xdidcmd')
4407 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01004408 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004409 # define Afunc() on the command line
4410 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
4411 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004412 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004413
4414 call StopVimInTerminal(buf)
4415 delete('XcallFunc')
4416 delete('Xdidcmd')
4417enddef
4418
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004419def Test_script_var_scope()
4420 var lines =<< trim END
4421 vim9script
4422 if true
4423 if true
4424 var one = 'one'
4425 echo one
4426 endif
4427 echo one
4428 endif
4429 END
4430 CheckScriptFailure(lines, 'E121:', 7)
4431
4432 lines =<< trim END
4433 vim9script
4434 if true
4435 if false
4436 var one = 'one'
4437 echo one
4438 else
4439 var one = 'one'
4440 echo one
4441 endif
4442 echo one
4443 endif
4444 END
4445 CheckScriptFailure(lines, 'E121:', 10)
4446
4447 lines =<< trim END
4448 vim9script
4449 while true
4450 var one = 'one'
4451 echo one
4452 break
4453 endwhile
4454 echo one
4455 END
4456 CheckScriptFailure(lines, 'E121:', 7)
4457
4458 lines =<< trim END
4459 vim9script
4460 for i in range(1)
4461 var one = 'one'
4462 echo one
4463 endfor
4464 echo one
4465 END
4466 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02004467
4468 lines =<< trim END
4469 vim9script
4470 {
4471 var one = 'one'
4472 assert_equal('one', one)
4473 }
4474 assert_false(exists('one'))
4475 assert_false(exists('s:one'))
4476 END
4477 CheckScriptSuccess(lines)
4478
4479 lines =<< trim END
4480 vim9script
4481 {
4482 var one = 'one'
4483 echo one
4484 }
4485 echo one
4486 END
4487 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004488enddef
4489
Bram Moolenaar352134b2020-10-17 22:04:08 +02004490def Test_catch_exception_in_callback()
4491 var lines =<< trim END
4492 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02004493 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02004494 try
4495 var x: string
4496 var y: string
4497 # this error should be caught with CHECKLEN
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00004498 var sl = ['']
4499 [x, y] = sl
Bram Moolenaar352134b2020-10-17 22:04:08 +02004500 catch
4501 g:caught = 'yes'
4502 endtry
4503 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004504 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02004505 feedkeys("\r", 'xt')
4506 END
4507 CheckScriptSuccess(lines)
4508
4509 unlet g:caught
4510enddef
4511
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004512def Test_no_unknown_error_after_error()
4513 if !has('unix') || !has('job')
4514 throw 'Skipped: not unix of missing +job feature'
4515 endif
Bram Moolenaarb16ff292021-09-26 20:14:39 +01004516 # FIXME: this check should not be needed
4517 if has('win32')
4518 throw 'Skipped: does not work on MS-Windows'
4519 endif
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004520 var lines =<< trim END
4521 vim9script
4522 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02004523 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004524 eval [][0]
4525 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02004526 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004527 sleep 1m
4528 source += l
4529 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004530 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01004531 while job_status(myjob) == 'run'
4532 sleep 10m
4533 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01004534 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02004535 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004536 END
4537 writefile(lines, 'Xdef')
4538 assert_fails('so Xdef', ['E684:', 'E1012:'])
4539 delete('Xdef')
4540enddef
4541
Bram Moolenaar4324d872020-12-01 20:12:24 +01004542def InvokeNormal()
4543 exe "norm! :m+1\r"
4544enddef
4545
4546def Test_invoke_normal_in_visual_mode()
4547 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
4548 new
4549 setline(1, ['aaa', 'bbb'])
4550 feedkeys("V\<F3>", 'xt')
4551 assert_equal(['bbb', 'aaa'], getline(1, 2))
4552 xunmap <F3>
4553enddef
4554
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004555def Test_white_space_after_command()
4556 var lines =<< trim END
4557 exit_cb: Func})
4558 END
4559 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01004560
4561 lines =<< trim END
4562 e#
4563 END
4564 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004565enddef
4566
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004567def Test_script_var_gone_when_sourced_twice()
4568 var lines =<< trim END
4569 vim9script
4570 if exists('g:guard')
4571 finish
4572 endif
4573 g:guard = 1
4574 var name = 'thename'
4575 def g:GetName(): string
4576 return name
4577 enddef
4578 def g:SetName(arg: string)
4579 name = arg
4580 enddef
4581 END
4582 writefile(lines, 'XscriptTwice.vim')
4583 so XscriptTwice.vim
4584 assert_equal('thename', g:GetName())
4585 g:SetName('newname')
4586 assert_equal('newname', g:GetName())
4587 so XscriptTwice.vim
4588 assert_fails('call g:GetName()', 'E1149:')
4589 assert_fails('call g:SetName("x")', 'E1149:')
4590
4591 delfunc g:GetName
4592 delfunc g:SetName
4593 delete('XscriptTwice.vim')
4594 unlet g:guard
4595enddef
4596
4597def Test_import_gone_when_sourced_twice()
4598 var exportlines =<< trim END
4599 vim9script
4600 if exists('g:guard')
4601 finish
4602 endif
4603 g:guard = 1
4604 export var name = 'someName'
4605 END
4606 writefile(exportlines, 'XexportScript.vim')
4607
4608 var lines =<< trim END
4609 vim9script
4610 import name from './XexportScript.vim'
4611 def g:GetName(): string
4612 return name
4613 enddef
4614 END
4615 writefile(lines, 'XscriptImport.vim')
4616 so XscriptImport.vim
4617 assert_equal('someName', g:GetName())
4618
4619 so XexportScript.vim
4620 assert_fails('call g:GetName()', 'E1149:')
4621
4622 delfunc g:GetName
4623 delete('XexportScript.vim')
4624 delete('XscriptImport.vim')
4625 unlet g:guard
4626enddef
4627
Bram Moolenaar10b94212021-02-19 21:42:57 +01004628def Test_unsupported_commands()
4629 var lines =<< trim END
4630 ka
4631 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004632 CheckDefFailure(lines, 'E476:')
4633 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaar10b94212021-02-19 21:42:57 +01004634
4635 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01004636 :1ka
4637 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004638 CheckDefFailure(lines, 'E476:')
4639 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaarada1d872021-02-20 08:16:51 +01004640
4641 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01004642 t
4643 END
4644 CheckDefFailure(lines, 'E1100:')
4645 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4646
4647 lines =<< trim END
4648 x
4649 END
4650 CheckDefFailure(lines, 'E1100:')
4651 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4652
4653 lines =<< trim END
4654 xit
4655 END
4656 CheckDefFailure(lines, 'E1100:')
4657 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4658enddef
4659
Bram Moolenaarc70fe462021-04-17 17:59:19 +02004660def Test_mapping_line_number()
4661 var lines =<< trim END
4662 vim9script
4663 def g:FuncA()
4664 # Some comment
4665 FuncB(0)
4666 enddef
4667 # Some comment
4668 def FuncB(
4669 # Some comment
4670 n: number
4671 )
4672 exe 'nno '
4673 # Some comment
4674 .. '<F3> a'
4675 .. 'b'
4676 .. 'c'
4677 enddef
4678 END
4679 CheckScriptSuccess(lines)
4680 var res = execute('verbose nmap <F3>')
4681 assert_match('No mapping found', res)
4682
4683 g:FuncA()
4684 res = execute('verbose nmap <F3>')
4685 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
4686
4687 nunmap <F3>
4688 delfunc g:FuncA
4689enddef
4690
Bram Moolenaardeb108b2021-07-08 17:35:36 +02004691def Test_option_set()
4692 # legacy script allows for white space
4693 var lines =<< trim END
4694 set foldlevel =11
4695 call assert_equal(11, &foldlevel)
4696 END
4697 CheckScriptSuccess(lines)
4698
4699 set foldlevel
4700 set foldlevel=12
4701 assert_equal(12, &foldlevel)
4702 set foldlevel+=2
4703 assert_equal(14, &foldlevel)
4704 set foldlevel-=3
4705 assert_equal(11, &foldlevel)
4706
4707 lines =<< trim END
4708 set foldlevel =1
4709 END
4710 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: =1')
4711
4712 lines =<< trim END
4713 set foldlevel +=1
4714 END
4715 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: +=1')
4716
4717 lines =<< trim END
4718 set foldlevel ^=1
4719 END
4720 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: ^=1')
4721
4722 lines =<< trim END
4723 set foldlevel -=1
4724 END
4725 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: -=1')
4726
4727 set foldlevel&
4728enddef
4729
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004730def Test_option_modifier()
Bram Moolenaar1594f312021-07-08 16:40:13 +02004731 # legacy script allows for white space
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004732 var lines =<< trim END
4733 set hlsearch & hlsearch !
4734 call assert_equal(1, &hlsearch)
4735 END
4736 CheckScriptSuccess(lines)
4737
Bram Moolenaar1594f312021-07-08 16:40:13 +02004738 set hlsearch
4739 set hlsearch!
4740 assert_equal(false, &hlsearch)
4741
4742 set hlsearch
4743 set hlsearch&
4744 assert_equal(false, &hlsearch)
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004745
4746 lines =<< trim END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004747 set hlsearch &
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004748 END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004749 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: &')
4750
4751 lines =<< trim END
4752 set hlsearch !
4753 END
4754 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: !')
4755
4756 set hlsearch&
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004757enddef
4758
Bram Moolenaarc03fe662021-07-11 16:52:45 +02004759" This must be called last, it may cause following :def functions to fail
4760def Test_xxx_echoerr_line_number()
4761 var lines =<< trim END
4762 echoerr 'some'
4763 .. ' error'
4764 .. ' continued'
4765 END
4766 CheckDefExecAndScriptFailure(lines, 'some error continued', 1)
4767enddef
4768
Bram Moolenaar9537e372021-12-10 21:05:53 +00004769func Test_debug_with_lambda()
Bram Moolenaar9fffef92021-12-10 16:55:58 +00004770 CheckRunVimInTerminal
4771
Bram Moolenaar9537e372021-12-10 21:05:53 +00004772 " call indirectly to avoid compilation error for missing functions
4773 call Run_Test_debug_with_lambda()
4774endfunc
4775
4776def Run_Test_debug_with_lambda()
Bram Moolenaar9fffef92021-12-10 16:55:58 +00004777 var lines =<< trim END
4778 vim9script
4779 def Func()
4780 var n = 0
4781 echo [0]->filter((_, v) => v == n)
4782 enddef
4783 breakadd func Func
4784 Func()
4785 END
4786 writefile(lines, 'XdebugFunc')
4787 var buf = RunVimInTerminal('-S XdebugFunc', {rows: 6, wait_for_ruler: 0})
4788 WaitForAssert(() => assert_match('^>', term_getline(buf, 6)))
4789
4790 term_sendkeys(buf, "cont\<CR>")
4791 WaitForAssert(() => assert_match('\[0\]', term_getline(buf, 5)))
4792
4793 StopVimInTerminal(buf)
4794 delete('XdebugFunc')
4795enddef
4796
Bram Moolenaar310091d2021-12-23 21:14:37 +00004797func Test_debug_running_out_of_lines()
4798 CheckRunVimInTerminal
4799
4800 " call indirectly to avoid compilation error for missing functions
4801 call Run_Test_debug_running_out_of_lines()
4802endfunc
4803
4804def Run_Test_debug_running_out_of_lines()
4805 var lines =<< trim END
4806 vim9script
4807 def Crash()
4808 #
4809 #
4810 #
4811 #
4812 #
4813 #
4814 #
4815 if true
4816 #
4817 endif
4818 enddef
4819 breakadd func Crash
4820 Crash()
4821 END
4822 writefile(lines, 'XdebugFunc')
4823 var buf = RunVimInTerminal('-S XdebugFunc', {rows: 6, wait_for_ruler: 0})
4824 WaitForAssert(() => assert_match('^>', term_getline(buf, 6)))
4825
4826 term_sendkeys(buf, "next\<CR>")
4827 TermWait(buf)
4828 WaitForAssert(() => assert_match('^>', term_getline(buf, 6)))
4829
4830 term_sendkeys(buf, "cont\<CR>")
4831 TermWait(buf)
4832
4833 StopVimInTerminal(buf)
4834 delete('XdebugFunc')
4835enddef
4836
Bram Moolenaar648594e2021-07-11 17:55:01 +02004837def ProfiledWithLambda()
Bram Moolenaard9162552021-07-11 15:26:13 +02004838 var n = 3
4839 echo [[1, 2], [3, 4]]->filter((_, l) => l[0] == n)
4840enddef
4841
Bram Moolenaar648594e2021-07-11 17:55:01 +02004842def ProfiledNested()
4843 var x = 0
4844 def Nested(): any
4845 return x
4846 enddef
4847 Nested()
4848enddef
4849
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004850def ProfiledNestedProfiled()
4851 var x = 0
4852 def Nested(): any
4853 return x
4854 enddef
4855 Nested()
4856enddef
4857
Dominique Pelle923dce22021-11-21 11:36:04 +00004858" Execute this near the end, profiling doesn't stop until Vim exits.
Bram Moolenaard9162552021-07-11 15:26:13 +02004859" This only tests that it works, not the profiling output.
4860def Test_xx_profile_with_lambda()
Bram Moolenaar4ece1522021-07-11 16:31:51 +02004861 CheckFeature profile
4862
Bram Moolenaard9162552021-07-11 15:26:13 +02004863 profile start Xprofile.log
Bram Moolenaar648594e2021-07-11 17:55:01 +02004864 profile func ProfiledWithLambda
4865 ProfiledWithLambda()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004866
Bram Moolenaar648594e2021-07-11 17:55:01 +02004867 profile func ProfiledNested
4868 ProfiledNested()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004869
4870 # Also profile the nested function. Use a different function, although the
4871 # contents is the same, to make sure it was not already compiled.
4872 profile func *
4873 ProfiledNestedProfiled()
4874
4875 profdel func *
4876 profile pause
Bram Moolenaard9162552021-07-11 15:26:13 +02004877enddef
4878
Bram Moolenaar585fea72020-04-02 22:33:21 +02004879" Keep this last, it messes up highlighting.
4880def Test_substitute_cmd()
4881 new
4882 setline(1, 'something')
4883 :substitute(some(other(
4884 assert_equal('otherthing', getline(1))
4885 bwipe!
4886
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004887 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004888 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02004889 vim9script
4890 new
4891 setline(1, 'something')
4892 :substitute(some(other(
4893 assert_equal('otherthing', getline(1))
4894 bwipe!
4895 END
4896 writefile(lines, 'Xvim9lines')
4897 source Xvim9lines
4898
4899 delete('Xvim9lines')
4900enddef
4901
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker