blob: 989627f95f11ae13e91d506e6598f8e092e84331 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001" Test various aspects of the Vim9 script language.
2
Bram Moolenaar673660a2020-01-26 16:50:05 +01003source check.vim
Bram Moolenaar101f4812020-06-16 23:18:51 +02004source term_util.vim
Bram Moolenaarad39c092020-02-26 18:23:43 +01005source view_util.vim
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006source vim9.vim
Bram Moolenaare3d46852020-08-29 13:39:17 +02007source shared.vim
Bram Moolenaar37294bd2021-03-10 13:40:08 +01008source screendump.vim
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010def Test_range_only()
11 new
12 setline(1, ['blah', 'Blah'])
13 :/Blah/
14 assert_equal(2, getcurpos()[1])
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020015 bwipe!
16
17 # without range commands use current line
18 new
19 setline(1, ['one', 'two', 'three'])
20 :2
21 print
22 assert_equal('two', Screenline(&lines))
23 :3
24 list
25 assert_equal('three$', Screenline(&lines))
Bram Moolenaarb8554302021-02-15 21:30:30 +010026
27 # missing command does not print the line
28 var lines =<< trim END
29 vim9script
30 :1|
31 assert_equal('three$', Screenline(&lines))
32 :|
33 assert_equal('three$', Screenline(&lines))
34 END
35 CheckScriptSuccess(lines)
36
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020037 bwipe!
Bram Moolenaar025cb1c2020-12-14 18:31:27 +010038
39 # won't generate anything
40 if false
41 :123
42 endif
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020043enddef
44
Bram Moolenaara6e67e42020-05-15 23:36:40 +020045let g:alist = [7]
46let g:astring = 'text'
Bram Moolenaarf0b9f432020-07-17 23:03:17 +020047let g:anumber = 123
Bram Moolenaar6e587dc2020-02-06 13:15:52 +010048
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020049def Test_delfunction()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020050 # Check function is defined in script namespace
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020051 CheckScriptSuccess([
52 'vim9script',
53 'func CheckMe()',
54 ' return 123',
55 'endfunc',
56 'assert_equal(123, s:CheckMe())',
57 ])
58
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020059 # Check function in script namespace cannot be deleted
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020060 CheckScriptFailure([
61 'vim9script',
62 'func DeleteMe1()',
63 'endfunc',
64 'delfunction DeleteMe1',
65 ], 'E1084:')
66 CheckScriptFailure([
67 'vim9script',
68 'func DeleteMe2()',
69 'endfunc',
70 'def DoThat()',
71 ' delfunction DeleteMe2',
72 'enddef',
73 'DoThat()',
74 ], 'E1084:')
75 CheckScriptFailure([
76 'vim9script',
77 'def DeleteMe3()',
78 'enddef',
79 'delfunction DeleteMe3',
80 ], 'E1084:')
81 CheckScriptFailure([
82 'vim9script',
83 'def DeleteMe4()',
84 'enddef',
85 'def DoThat()',
86 ' delfunction DeleteMe4',
87 'enddef',
88 'DoThat()',
89 ], 'E1084:')
Bram Moolenaar925e9fd2020-07-25 15:41:11 +020090
91 # Check that global :def function can be replaced and deleted
Bram Moolenaarcfcd0112020-09-27 15:19:27 +020092 var lines =<< trim END
Bram Moolenaar925e9fd2020-07-25 15:41:11 +020093 vim9script
94 def g:Global(): string
95 return "yes"
96 enddef
97 assert_equal("yes", g:Global())
98 def! g:Global(): string
99 return "no"
100 enddef
101 assert_equal("no", g:Global())
102 delfunc g:Global
103 assert_false(exists('*g:Global'))
104 END
105 CheckScriptSuccess(lines)
106
107 # Check that global function can be replaced by a :def function and deleted
108 lines =<< trim END
109 vim9script
110 func g:Global()
111 return "yes"
112 endfunc
113 assert_equal("yes", g:Global())
114 def! g:Global(): string
115 return "no"
116 enddef
117 assert_equal("no", g:Global())
118 delfunc g:Global
119 assert_false(exists('*g:Global'))
120 END
121 CheckScriptSuccess(lines)
122
123 # Check that global :def function can be replaced by a function and deleted
124 lines =<< trim END
125 vim9script
126 def g:Global(): string
127 return "yes"
128 enddef
129 assert_equal("yes", g:Global())
130 func! g:Global()
131 return "no"
132 endfunc
133 assert_equal("no", g:Global())
134 delfunc g:Global
135 assert_false(exists('*g:Global'))
136 END
137 CheckScriptSuccess(lines)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200138enddef
139
Bram Moolenaar08052222020-09-14 17:04:31 +0200140def Test_wrong_type()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200141 CheckDefFailure(['var name: list<nothing>'], 'E1010:')
142 CheckDefFailure(['var name: list<list<nothing>>'], 'E1010:')
143 CheckDefFailure(['var name: dict<nothing>'], 'E1010:')
144 CheckDefFailure(['var name: dict<dict<nothing>>'], 'E1010:')
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100145
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200146 CheckDefFailure(['var name: dict<number'], 'E1009:')
147 CheckDefFailure(['var name: dict<list<number>'], 'E1009:')
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100148
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200149 CheckDefFailure(['var name: ally'], 'E1010:')
150 CheckDefFailure(['var name: bram'], 'E1010:')
151 CheckDefFailure(['var name: cathy'], 'E1010:')
152 CheckDefFailure(['var name: dom'], 'E1010:')
153 CheckDefFailure(['var name: freddy'], 'E1010:')
154 CheckDefFailure(['var name: john'], 'E1010:')
155 CheckDefFailure(['var name: larry'], 'E1010:')
156 CheckDefFailure(['var name: ned'], 'E1010:')
157 CheckDefFailure(['var name: pam'], 'E1010:')
158 CheckDefFailure(['var name: sam'], 'E1010:')
159 CheckDefFailure(['var name: vim'], 'E1010:')
Bram Moolenaara0a9f432020-04-28 21:29:34 +0200160
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200161 CheckDefFailure(['var Ref: number', 'Ref()'], 'E1085:')
162 CheckDefFailure(['var Ref: string', 'var res = Ref()'], 'E1085:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200163enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaar10c65862020-10-08 21:16:42 +0200165def Test_script_wrong_type()
166 var lines =<< trim END
167 vim9script
168 var s:dict: dict<string>
169 s:dict['a'] = ['x']
170 END
171 CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got list<string>', 3)
172enddef
173
Bram Moolenaar08052222020-09-14 17:04:31 +0200174def Test_const()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200175 CheckDefFailure(['final name = 234', 'name = 99'], 'E1018:')
176 CheckDefFailure(['final one = 234', 'var one = 99'], 'E1017:')
177 CheckDefFailure(['final list = [1, 2]', 'var list = [3, 4]'], 'E1017:')
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200178 CheckDefFailure(['final two'], 'E1125:')
179 CheckDefFailure(['final &option'], 'E996:')
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200180
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200181 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200182 final list = [1, 2, 3]
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200183 list[0] = 4
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200184 list->assert_equal([4, 2, 3])
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200185 const other = [5, 6, 7]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200186 other->assert_equal([5, 6, 7])
Bram Moolenaar71abe482020-09-14 22:28:30 +0200187
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200188 var varlist = [7, 8]
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200189 const constlist = [1, varlist, 3]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200190 varlist[0] = 77
Bram Moolenaar23e2e112021-08-03 21:16:18 +0200191 constlist[1][1] = 88
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200192 var cl = constlist[1]
Bram Moolenaar71abe482020-09-14 22:28:30 +0200193 cl[1] = 88
194 constlist->assert_equal([1, [77, 88], 3])
195
Bram Moolenaare0de1712020-12-02 17:36:54 +0100196 var vardict = {five: 5, six: 6}
197 const constdict = {one: 1, two: vardict, three: 3}
Bram Moolenaar71abe482020-09-14 22:28:30 +0200198 vardict['five'] = 55
Bram Moolenaar23e2e112021-08-03 21:16:18 +0200199 constdict['two']['six'] = 66
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200200 var cd = constdict['two']
Bram Moolenaar71abe482020-09-14 22:28:30 +0200201 cd['six'] = 66
Bram Moolenaare0de1712020-12-02 17:36:54 +0100202 constdict->assert_equal({one: 1, two: {five: 55, six: 66}, three: 3})
Bram Moolenaardbeecb22020-09-14 18:15:09 +0200203 END
204 CheckDefAndScriptSuccess(lines)
Bram Moolenaar08052222020-09-14 17:04:31 +0200205enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200207def Test_const_bang()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200208 var lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200209 const var = 234
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200210 var = 99
211 END
212 CheckDefExecFailure(lines, 'E1018:', 2)
213 CheckScriptFailure(['vim9script'] + lines, 'E46:', 3)
214
215 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200216 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200217 ll[0] = 99
218 END
219 CheckDefExecFailure(lines, 'E1119:', 2)
220 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
221
222 lines =<< trim END
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200223 const ll = [2, 3, 4]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200224 ll[3] = 99
225 END
226 CheckDefExecFailure(lines, 'E1118:', 2)
227 CheckScriptFailure(['vim9script'] + lines, 'E684:', 3)
228
229 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100230 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200231 dd["one"] = 99
232 END
233 CheckDefExecFailure(lines, 'E1121:', 2)
234 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
235
236 lines =<< trim END
Bram Moolenaare0de1712020-12-02 17:36:54 +0100237 const dd = {one: 1, two: 2}
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200238 dd["three"] = 99
239 END
240 CheckDefExecFailure(lines, 'E1120:')
241 CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
242enddef
243
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200244def Test_range_no_colon()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200245 CheckDefFailure(['%s/a/b/'], 'E1050:')
246 CheckDefFailure(['+ s/a/b/'], 'E1050:')
247 CheckDefFailure(['- s/a/b/'], 'E1050:')
248 CheckDefFailure(['. s/a/b/'], 'E1050:')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200249enddef
250
251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252def Test_block()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200253 var outer = 1
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254 {
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200255 var inner = 2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 assert_equal(1, outer)
257 assert_equal(2, inner)
258 }
259 assert_equal(1, outer)
Bram Moolenaar3f1e9f02021-02-27 22:36:43 +0100260
261 {|echo 'yes'|}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262enddef
263
Bram Moolenaar08052222020-09-14 17:04:31 +0200264def Test_block_failure()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200265 CheckDefFailure(['{', 'var inner = 1', '}', 'echo inner'], 'E1001:')
Bram Moolenaar08052222020-09-14 17:04:31 +0200266 CheckDefFailure(['}'], 'E1025:')
267 CheckDefFailure(['{', 'echo 1'], 'E1026:')
268enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200270def Test_block_local_vars()
271 var lines =<< trim END
272 vim9script
Bram Moolenaared234f22020-10-15 20:42:20 +0200273 v:testing = 1
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200274 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200275 var text = ['hello']
276 def SayHello(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200277 return text
278 enddef
279 def SetText(v: string)
Bram Moolenaared234f22020-10-15 20:42:20 +0200280 text = [v]
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200281 enddef
282 endif
283
284 if true
Bram Moolenaared234f22020-10-15 20:42:20 +0200285 var text = ['again']
286 def SayAgain(): list<string>
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200287 return text
288 enddef
289 endif
Bram Moolenaared234f22020-10-15 20:42:20 +0200290
291 # test that the "text" variables are not cleaned up
292 test_garbagecollect_now()
293
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200294 defcompile
295
Bram Moolenaared234f22020-10-15 20:42:20 +0200296 assert_equal(['hello'], SayHello())
297 assert_equal(['again'], SayAgain())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200298
299 SetText('foobar')
Bram Moolenaared234f22020-10-15 20:42:20 +0200300 assert_equal(['foobar'], SayHello())
301
302 call writefile(['ok'], 'Xdidit')
303 qall!
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200304 END
Bram Moolenaared234f22020-10-15 20:42:20 +0200305
306 # need to execute this with a separate Vim instance to avoid the current
307 # context gets garbage collected.
308 writefile(lines, 'Xscript')
309 RunVim([], [], '-S Xscript')
310 assert_equal(['ok'], readfile('Xdidit'))
311
312 delete('Xscript')
313 delete('Xdidit')
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200314enddef
315
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200316def Test_block_local_vars_with_func()
317 var lines =<< trim END
318 vim9script
319 if true
320 var foo = 'foo'
321 if true
322 var bar = 'bar'
323 def Func(): list<string>
324 return [foo, bar]
325 enddef
326 endif
327 endif
328 # function is compiled here, after blocks have finished, can still access
329 # "foo" and "bar"
330 assert_equal(['foo', 'bar'], Func())
331 END
332 CheckScriptSuccess(lines)
333enddef
334
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200335" legacy func for command that's defined later
336func InvokeSomeCommand()
337 SomeCommand
338endfunc
339
340def Test_autocommand_block()
341 com SomeCommand {
342 g:someVar = 'some'
343 }
344 InvokeSomeCommand()
345 assert_equal('some', g:someVar)
346
347 delcommand SomeCommand
348 unlet g:someVar
349enddef
350
351def Test_command_block()
352 au BufNew *.xml {
353 g:otherVar = 'other'
354 }
355 split other.xml
356 assert_equal('other', g:otherVar)
357
358 bwipe!
359 au! BufNew *.xml
360 unlet g:otherVar
361enddef
362
Bram Moolenaard032f342020-07-18 18:13:02 +0200363func g:NoSuchFunc()
364 echo 'none'
365endfunc
366
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100367def Test_try_catch_throw()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200368 var l = []
Bram Moolenaar7a092242020-04-16 22:10:49 +0200369 try # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370 add(l, '1')
371 throw 'wrong'
372 add(l, '2')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200373 catch # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100374 add(l, v:exception)
Bram Moolenaar7a092242020-04-16 22:10:49 +0200375 finally # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100376 add(l, '3')
Bram Moolenaar7a092242020-04-16 22:10:49 +0200377 endtry # comment
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378 assert_equal(['1', 'wrong', '3'], l)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200379
Bram Moolenaare8593122020-07-18 15:17:02 +0200380 l = []
381 try
382 try
383 add(l, '1')
384 throw 'wrong'
385 add(l, '2')
386 catch /right/
387 add(l, v:exception)
388 endtry
389 catch /wrong/
390 add(l, 'caught')
Bram Moolenaar373863e2020-09-26 17:20:53 +0200391 fina
Bram Moolenaare8593122020-07-18 15:17:02 +0200392 add(l, 'finally')
393 endtry
394 assert_equal(['1', 'caught', 'finally'], l)
395
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200396 var n: number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200397 try
398 n = l[3]
399 catch /E684:/
400 n = 99
401 endtry
402 assert_equal(99, n)
403
Bram Moolenaar69f70502021-01-01 16:10:46 +0100404 var done = 'no'
405 if 0
406 try | catch | endtry
407 else
408 done = 'yes'
409 endif
410 assert_equal('yes', done)
411
412 done = 'no'
413 if 1
414 done = 'yes'
415 else
416 try | catch | endtry
417 done = 'never'
418 endif
419 assert_equal('yes', done)
420
421 if 1
422 else
423 try | catch /pat/ | endtry
424 try | catch /pat/
425 endtry
426 try
427 catch /pat/ | endtry
428 try
429 catch /pat/
430 endtry
431 endif
432
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200433 try
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200434 # string slice returns a string, not a number
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200435 n = g:astring[3]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200436 catch /E1012:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200437 n = 77
438 endtry
439 assert_equal(77, n)
440
441 try
442 n = l[g:astring]
Bram Moolenaar5e654232020-09-16 15:22:00 +0200443 catch /E1012:/
Bram Moolenaar56acb092020-08-16 14:48:19 +0200444 n = 88
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200445 endtry
Bram Moolenaar56acb092020-08-16 14:48:19 +0200446 assert_equal(88, n)
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200447
448 try
449 n = s:does_not_exist
450 catch /E121:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200451 n = 111
452 endtry
453 assert_equal(111, n)
454
455 try
456 n = g:does_not_exist
457 catch /E121:/
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200458 n = 121
459 endtry
460 assert_equal(121, n)
461
Bram Moolenaare0de1712020-12-02 17:36:54 +0100462 var d = {one: 1}
Bram Moolenaar68d130c2020-07-17 22:06:44 +0200463 try
464 n = d[g:astring]
465 catch /E716:/
466 n = 222
467 endtry
468 assert_equal(222, n)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200469
470 try
471 n = -g:astring
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +0200472 catch /E1012:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200473 n = 233
474 endtry
475 assert_equal(233, n)
476
477 try
478 n = +g:astring
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +0200479 catch /E1012:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200480 n = 244
481 endtry
482 assert_equal(244, n)
483
484 try
485 n = +g:alist
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +0200486 catch /E1012:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200487 n = 255
488 endtry
489 assert_equal(255, n)
490
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200491 var nd: dict<any>
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200492 try
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100493 nd = {[g:alist]: 1}
494 catch /E1105:/
Bram Moolenaarf0b9f432020-07-17 23:03:17 +0200495 n = 266
496 endtry
497 assert_equal(266, n)
498
499 try
500 [n] = [1, 2, 3]
501 catch /E1093:/
502 n = 277
503 endtry
504 assert_equal(277, n)
505
Bram Moolenaare8593122020-07-18 15:17:02 +0200506 try
507 &ts = g:astring
Bram Moolenaar5e654232020-09-16 15:22:00 +0200508 catch /E1012:/
Bram Moolenaare8593122020-07-18 15:17:02 +0200509 n = 288
510 endtry
511 assert_equal(288, n)
512
513 try
514 &backspace = 'asdf'
515 catch /E474:/
516 n = 299
517 endtry
518 assert_equal(299, n)
519
520 l = [1]
521 try
522 l[3] = 3
523 catch /E684:/
524 n = 300
525 endtry
526 assert_equal(300, n)
527
528 try
Bram Moolenaare8593122020-07-18 15:17:02 +0200529 unlet g:does_not_exist
530 catch /E108:/
531 n = 322
532 endtry
533 assert_equal(322, n)
534
535 try
Bram Moolenaar2bede172020-11-19 18:53:18 +0100536 d = {text: 1, [g:astring]: 2}
Bram Moolenaare8593122020-07-18 15:17:02 +0200537 catch /E721:/
538 n = 333
539 endtry
540 assert_equal(333, n)
541
542 try
543 l = DeletedFunc()
544 catch /E933:/
545 n = 344
546 endtry
547 assert_equal(344, n)
Bram Moolenaard032f342020-07-18 18:13:02 +0200548
549 try
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200550 echo range(1, 2, 0)
551 catch /E726:/
Bram Moolenaard032f342020-07-18 18:13:02 +0200552 n = 355
553 endtry
554 assert_equal(355, n)
555
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200556 var P = function('g:NoSuchFunc')
Bram Moolenaard032f342020-07-18 18:13:02 +0200557 delfunc g:NoSuchFunc
558 try
559 echo P()
560 catch /E117:/
561 n = 366
562 endtry
563 assert_equal(366, n)
564
565 try
566 echo g:NoSuchFunc()
567 catch /E117:/
568 n = 377
569 endtry
570 assert_equal(377, n)
571
572 try
573 echo g:alist + 4
574 catch /E745:/
575 n = 388
576 endtry
577 assert_equal(388, n)
578
579 try
580 echo 4 + g:alist
581 catch /E745:/
582 n = 399
583 endtry
584 assert_equal(399, n)
585
586 try
587 echo g:alist.member
588 catch /E715:/
589 n = 400
590 endtry
591 assert_equal(400, n)
592
593 try
594 echo d.member
595 catch /E716:/
596 n = 411
597 endtry
598 assert_equal(411, n)
Bram Moolenaard9d77892021-02-12 21:32:47 +0100599
600 var counter = 0
601 for i in range(4)
602 try
603 eval [][0]
604 catch
605 endtry
606 counter += 1
607 endfor
608 assert_equal(4, counter)
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100609
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +0200610 # no requirement for spaces before |
611 try|echo 0|catch|endtry
612
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +0100613 # return in finally after empty catch
614 def ReturnInFinally(): number
615 try
616 finally
617 return 4
618 endtry
619 return 2
620 enddef
621 assert_equal(4, ReturnInFinally())
Bram Moolenaar8ac681a2021-06-15 20:06:34 +0200622
623 var lines =<< trim END
624 vim9script
625 try
626 acos('0.5')
627 ->setline(1)
628 catch
629 g:caught = v:exception
630 endtry
631 END
632 CheckScriptSuccess(lines)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200633 assert_match('E1219: Float or Number required for argument 1', g:caught)
Bram Moolenaar8ac681a2021-06-15 20:06:34 +0200634 unlet g:caught
Bram Moolenaar41978282021-07-04 14:47:30 +0200635
636 # missing catch and/or finally
637 lines =<< trim END
638 vim9script
639 try
640 echo 'something'
641 endtry
642 END
643 CheckScriptFailure(lines, 'E1032:')
rbtnn84934992021-08-07 13:26:53 +0200644
645 # skipping try-finally-endtry when try-finally-endtry is used in another block
646 lines =<< trim END
647 if v:true
648 try
649 finally
650 endtry
651 else
652 try
653 finally
654 endtry
655 endif
656 END
657 CheckDefAndScriptSuccess(lines)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658enddef
659
Bram Moolenaar3f987b52021-06-30 12:02:24 +0200660def Test_try_in_catch()
661 var lines =<< trim END
662 vim9script
663 var seq = []
664 def DoIt()
665 try
666 seq->add('throw 1')
667 eval [][0]
668 seq->add('notreached')
669 catch
670 seq->add('catch')
671 try
672 seq->add('throw 2')
673 eval [][0]
674 seq->add('notreached')
675 catch /nothing/
676 seq->add('notreached')
677 endtry
678 seq->add('done')
679 endtry
680 enddef
681 DoIt()
682 assert_equal(['throw 1', 'catch', 'throw 2', 'done'], seq)
683 END
684enddef
685
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200686def Test_error_in_catch()
687 var lines =<< trim END
688 try
689 eval [][0]
690 catch /E684:/
691 eval [][0]
692 endtry
693 END
694 CheckDefExecFailure(lines, 'E684:', 4)
695enddef
696
Bram Moolenaar2e34c342021-03-14 12:13:33 +0100697" :while at the very start of a function that :continue jumps to
698def TryContinueFunc()
699 while g:Count < 2
700 g:sequence ..= 't'
701 try
702 echoerr 'Test'
703 catch
704 g:Count += 1
705 g:sequence ..= 'c'
706 continue
707 endtry
708 g:sequence ..= 'e'
709 g:Count += 1
710 endwhile
711enddef
712
713def Test_continue_in_try_in_while()
714 g:Count = 0
715 g:sequence = ''
716 TryContinueFunc()
717 assert_equal('tctc', g:sequence)
718 unlet g:Count
719 unlet g:sequence
720enddef
721
Bram Moolenaar9cb577a2021-02-22 22:45:10 +0100722def Test_nocatch_return_in_try()
723 # return in try block returns normally
724 def ReturnInTry(): string
725 try
726 return '"some message"'
727 catch
728 endtry
729 return 'not reached'
730 enddef
731 exe 'echoerr ' .. ReturnInTry()
732enddef
733
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100734def Test_cnext_works_in_catch()
735 var lines =<< trim END
736 vim9script
Bram Moolenaarc3235272021-07-10 19:42:03 +0200737 au BufEnter * eval 1 + 2
Bram Moolenaar1430cee2021-01-17 19:20:32 +0100738 writefile(['text'], 'Xfile1')
739 writefile(['text'], 'Xfile2')
740 var items = [
741 {lnum: 1, filename: 'Xfile1', valid: true},
742 {lnum: 1, filename: 'Xfile2', valid: true}
743 ]
744 setqflist([], ' ', {items: items})
745 cwindow
746
747 def CnextOrCfirst()
748 # if cnext fails, cfirst is used
749 try
750 cnext
751 catch
752 cfirst
753 endtry
754 enddef
755
756 CnextOrCfirst()
757 CnextOrCfirst()
758 writefile([getqflist({idx: 0}).idx], 'Xresult')
759 qall
760 END
761 writefile(lines, 'XCatchCnext')
762 RunVim([], [], '--clean -S XCatchCnext')
763 assert_equal(['1'], readfile('Xresult'))
764
765 delete('Xfile1')
766 delete('Xfile2')
767 delete('XCatchCnext')
768 delete('Xresult')
769enddef
770
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100771def Test_throw_skipped()
772 if 0
773 throw dontgethere
774 endif
775enddef
776
Bram Moolenaar8f81b222021-01-14 21:47:06 +0100777def Test_nocatch_throw_silenced()
778 var lines =<< trim END
779 vim9script
780 def Func()
781 throw 'error'
782 enddef
783 silent! Func()
784 END
785 writefile(lines, 'XthrowSilenced')
786 source XthrowSilenced
787 delete('XthrowSilenced')
788enddef
789
Bram Moolenaare8593122020-07-18 15:17:02 +0200790def DeletedFunc(): list<any>
791 return ['delete me']
792enddef
793defcompile
794delfunc DeletedFunc
795
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100796def ThrowFromDef()
Bram Moolenaara72cfb82020-04-23 17:07:30 +0200797 throw "getout" # comment
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100798enddef
799
800func CatchInFunc()
801 try
802 call ThrowFromDef()
803 catch
804 let g:thrown_func = v:exception
805 endtry
806endfunc
807
808def CatchInDef()
809 try
810 ThrowFromDef()
811 catch
812 g:thrown_def = v:exception
813 endtry
814enddef
815
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100816def ReturnFinally(): string
817 try
818 return 'intry'
Bram Moolenaar373863e2020-09-26 17:20:53 +0200819 finall
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100820 g:in_finally = 'finally'
821 endtry
822 return 'end'
823enddef
824
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100825def Test_try_catch_nested()
826 CatchInFunc()
827 assert_equal('getout', g:thrown_func)
828
829 CatchInDef()
830 assert_equal('getout', g:thrown_def)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100831
832 assert_equal('intry', ReturnFinally())
833 assert_equal('finally', g:in_finally)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +0200834
835 var l = []
836 try
837 l->add('1')
838 throw 'bad'
839 l->add('x')
840 catch /bad/
841 l->add('2')
842 try
843 l->add('3')
844 throw 'one'
845 l->add('x')
846 catch /one/
847 l->add('4')
848 try
849 l->add('5')
850 throw 'more'
851 l->add('x')
852 catch /more/
853 l->add('6')
854 endtry
855 endtry
856 endtry
857 assert_equal(['1', '2', '3', '4', '5', '6'], l)
Bram Moolenaar834193a2021-06-30 20:39:15 +0200858
859 l = []
860 try
861 try
862 l->add('1')
863 throw 'foo'
864 l->add('x')
865 catch
866 l->add('2')
867 throw 'bar'
868 l->add('x')
869 finally
870 l->add('3')
871 endtry
872 l->add('x')
873 catch /bar/
874 l->add('4')
875 endtry
876 assert_equal(['1', '2', '3', '4'], l)
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100877enddef
878
Bram Moolenaar9939f572020-09-16 22:29:52 +0200879def TryOne(): number
880 try
881 return 0
882 catch
883 endtry
884 return 0
885enddef
886
887def TryTwo(n: number): string
888 try
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200889 var x = {}
Bram Moolenaar9939f572020-09-16 22:29:52 +0200890 catch
891 endtry
892 return 'text'
893enddef
894
895def Test_try_catch_twice()
896 assert_equal('text', TryOne()->TryTwo())
897enddef
898
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100899def Test_try_catch_match()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200900 var seq = 'a'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100901 try
902 throw 'something'
903 catch /nothing/
904 seq ..= 'x'
905 catch /some/
906 seq ..= 'b'
907 catch /asdf/
908 seq ..= 'x'
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200909 catch ?a\?sdf?
910 seq ..= 'y'
Bram Moolenaarf575adf2020-02-20 20:41:06 +0100911 finally
912 seq ..= 'c'
913 endtry
914 assert_equal('abc', seq)
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100915enddef
916
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200917def Test_try_catch_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +0200918 CheckDefFailure(['catch'], 'E603:')
919 CheckDefFailure(['try', 'echo 0', 'catch', 'catch'], 'E1033:')
920 CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
921 CheckDefFailure(['finally'], 'E606:')
922 CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:')
923 CheckDefFailure(['endtry'], 'E602:')
924 CheckDefFailure(['while 1', 'endtry'], 'E170:')
925 CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:')
Bram Moolenaar13106602020-10-04 16:06:05 +0200926 CheckDefFailure(['if 1', 'endtry'], 'E171:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200927 CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:')
Bram Moolenaar585fea72020-04-02 22:33:21 +0200928
Bram Moolenaare4984292020-12-13 14:19:25 +0100929 CheckDefFailure(['throw'], 'E1143:')
Bram Moolenaard2c61702020-09-06 15:58:36 +0200930 CheckDefFailure(['throw xxx'], 'E1001:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200931enddef
932
Bram Moolenaar7c5b3c0362021-02-14 22:40:57 +0100933def Try_catch_skipped()
934 var l = []
935 try
936 finally
937 endtry
938
939 if 1
940 else
941 try
942 endtry
943 endif
944enddef
945
946" The skipped try/endtry was updating the wrong instruction.
947def Test_try_catch_skipped()
948 var instr = execute('disassemble Try_catch_skipped')
949 assert_match("NEWLIST size 0\n", instr)
950enddef
951
952
953
Bram Moolenaar006ad482020-06-30 20:55:15 +0200954def Test_throw_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200955 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200956 var lines =<< trim END
Bram Moolenaar006ad482020-06-30 20:55:15 +0200957 vim9script
958 try
959 throw 'one'
960 .. 'two'
961 catch
962 assert_equal('onetwo', v:exception)
963 endtry
964 END
965 CheckScriptSuccess(lines)
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200966
967 lines =<< trim END
968 vim9script
Bram Moolenaar352134b2020-10-17 22:04:08 +0200969 @r = ''
Bram Moolenaar1e021e62020-10-16 20:25:23 +0200970 def Func()
971 throw @r
972 enddef
973 var result = ''
974 try
975 Func()
976 catch /E1129:/
977 result = 'caught'
978 endtry
979 assert_equal('caught', result)
980 END
981 CheckScriptSuccess(lines)
Bram Moolenaar006ad482020-06-30 20:55:15 +0200982enddef
983
Bram Moolenaared677f52020-08-12 16:38:10 +0200984def Test_error_in_nested_function()
Bram Moolenaar03dfde22021-02-14 13:17:22 +0100985 # an error in a nested :function aborts executing in the calling :def function
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200986 var lines =<< trim END
Bram Moolenaared677f52020-08-12 16:38:10 +0200987 vim9script
988 def Func()
989 Error()
990 g:test_var = 1
991 enddef
992 func Error() abort
993 eval [][0]
994 endfunc
995 Func()
996 END
997 g:test_var = 0
998 CheckScriptFailure(lines, 'E684:')
999 assert_equal(0, g:test_var)
1000enddef
1001
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001002def Test_abort_after_error()
1003 var lines =<< trim END
1004 vim9script
1005 while true
1006 echo notfound
1007 endwhile
1008 g:gotthere = true
1009 END
1010 g:gotthere = false
1011 CheckScriptFailure(lines, 'E121:')
1012 assert_false(g:gotthere)
1013 unlet g:gotthere
1014enddef
1015
Bram Moolenaar37c83712020-06-30 21:18:36 +02001016def Test_cexpr_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001017 # only checks line continuation
Bram Moolenaar37c83712020-06-30 21:18:36 +02001018 set errorformat=File\ %f\ line\ %l
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001019 var lines =<< trim END
Bram Moolenaar37c83712020-06-30 21:18:36 +02001020 vim9script
1021 cexpr 'File'
1022 .. ' someFile' ..
1023 ' line 19'
1024 assert_equal(19, getqflist()[0].lnum)
1025 END
1026 CheckScriptSuccess(lines)
1027 set errorformat&
1028enddef
1029
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +02001030def Test_statusline_syntax()
1031 # legacy syntax is used for 'statusline'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001032 var lines =<< trim END
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +02001033 vim9script
1034 func g:Status()
1035 return '%{"x" is# "x"}'
1036 endfunc
1037 set laststatus=2 statusline=%!Status()
1038 redrawstatus
1039 set laststatus statusline=
1040 END
1041 CheckScriptSuccess(lines)
1042enddef
1043
Bram Moolenaarb2097502020-07-19 17:17:02 +02001044def Test_list_vimscript()
1045 # checks line continuation and comments
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001046 var lines =<< trim END
Bram Moolenaarb2097502020-07-19 17:17:02 +02001047 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001048 var mylist = [
Bram Moolenaarb2097502020-07-19 17:17:02 +02001049 'one',
1050 # comment
1051 'two', # empty line follows
1052
1053 'three',
1054 ]
1055 assert_equal(['one', 'two', 'three'], mylist)
1056 END
1057 CheckScriptSuccess(lines)
Bram Moolenaar66250c92020-08-20 15:02:42 +02001058
1059 # check all lines from heredoc are kept
1060 lines =<< trim END
1061 # comment 1
1062 two
1063 # comment 3
1064
1065 five
1066 # comment 6
1067 END
1068 assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines)
Bram Moolenaar4bce26b2021-01-22 22:06:56 +01001069
1070 lines =<< trim END
1071 [{
1072 a: 0}]->string()->assert_equal("[{'a': 0}]")
1073 END
1074 CheckDefAndScriptSuccess(lines)
Bram Moolenaarb2097502020-07-19 17:17:02 +02001075enddef
1076
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001077if has('channel')
1078 let someJob = test_null_job()
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001079
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001080 def FuncWithError()
1081 echomsg g:someJob
1082 enddef
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001083
Bram Moolenaar2a1381c2020-05-05 23:32:58 +02001084 func Test_convert_emsg_to_exception()
1085 try
1086 call FuncWithError()
1087 catch
1088 call assert_match('Vim:E908:', v:exception)
1089 endtry
1090 endfunc
1091endif
Bram Moolenaar40ee4662020-05-05 22:08:26 +02001092
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093let s:export_script_lines =<< trim END
1094 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001095 var name: string = 'bob'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 def Concat(arg: string): string
1097 return name .. arg
1098 enddef
Bram Moolenaar227a69d2020-05-15 18:17:28 +02001099 g:result = Concat('bie')
1100 g:localname = name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101
1102 export const CONST = 1234
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001103 export var exported = 9876
1104 export var exp_name = 'John'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 export def Exported(): string
1106 return 'Exported'
1107 enddef
Bram Moolenaar24e93162021-07-18 20:40:33 +02001108 export def ExportedValue(): number
1109 return exported
1110 enddef
1111 export def ExportedInc()
1112 exported += 5
1113 enddef
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001114 export final theList = [1]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001115END
1116
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001117def Undo_export_script_lines()
1118 unlet g:result
1119 unlet g:localname
1120enddef
1121
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001122def Test_vim9_import_export()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001123 var import_script_lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001124 vim9script
Bram Moolenaar24e93162021-07-18 20:40:33 +02001125 import {exported, Exported, ExportedValue} from './Xexport.vim'
1126 g:exported1 = exported
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001127 exported += 3
Bram Moolenaar24e93162021-07-18 20:40:33 +02001128 g:exported2 = exported
1129 g:exported3 = ExportedValue()
1130
1131 import ExportedInc from './Xexport.vim'
1132 ExportedInc()
1133 g:exported_i1 = exported
1134 g:exported_i2 = ExportedValue()
1135
1136 exported = 11
1137 g:exported_s1 = exported
1138 g:exported_s2 = ExportedValue()
1139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 g:imported_func = Exported()
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001141
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001142 def GetExported(): string
Bram Moolenaare0de1712020-12-02 17:36:54 +01001143 var local_dict = {ref: Exported}
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001144 return local_dict.ref()
1145 enddef
1146 g:funcref_result = GetExported()
1147
Bram Moolenaar4db572e2021-07-18 18:21:38 +02001148 var dir = './'
1149 var ext = ".vim"
1150 import {exp_name} from dir .. 'Xexport' .. ext
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001151 g:imported_name = exp_name
1152 exp_name ..= ' Doe'
1153 g:imported_name_appended = exp_name
Bram Moolenaar24e93162021-07-18 20:40:33 +02001154 g:exported_later = exported
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001155
1156 import theList from './Xexport.vim'
1157 theList->add(2)
1158 assert_equal([1, 2], theList)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001159 END
1160
1161 writefile(import_script_lines, 'Ximport.vim')
1162 writefile(s:export_script_lines, 'Xexport.vim')
1163
1164 source Ximport.vim
1165
1166 assert_equal('bobbie', g:result)
1167 assert_equal('bob', g:localname)
Bram Moolenaar24e93162021-07-18 20:40:33 +02001168 assert_equal(9876, g:exported1)
1169 assert_equal(9879, g:exported2)
1170 assert_equal(9879, g:exported3)
1171
1172 assert_equal(9884, g:exported_i1)
1173 assert_equal(9884, g:exported_i2)
1174
1175 assert_equal(11, g:exported_s1)
1176 assert_equal(11, g:exported_s2)
1177 assert_equal(11, g:exported_later)
1178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 assert_equal('Exported', g:imported_func)
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001180 assert_equal('Exported', g:funcref_result)
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001181 assert_equal('John', g:imported_name)
1182 assert_equal('John Doe', g:imported_name_appended)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183 assert_false(exists('g:name'))
1184
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001185 Undo_export_script_lines()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001186 unlet g:exported1
1187 unlet g:exported2
1188 unlet g:exported3
1189 unlet g:exported_i1
1190 unlet g:exported_i2
1191 unlet g:exported_later
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 unlet g:imported_func
Bram Moolenaar6e587dc2020-02-06 13:15:52 +01001193 unlet g:imported_name g:imported_name_appended
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 delete('Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001195
Bram Moolenaar1c991142020-07-04 13:15:31 +02001196 # similar, with line breaks
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001197 var import_line_break_script_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001198 vim9script
1199 import {
1200 exported,
1201 Exported,
1202 }
1203 from
1204 './Xexport.vim'
Bram Moolenaar24e93162021-07-18 20:40:33 +02001205 g:exported = exported
1206 exported += 7
1207 g:exported_added = exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001208 g:imported_func = Exported()
1209 END
1210 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
1211 source Ximport_lbr.vim
1212
Bram Moolenaar24e93162021-07-18 20:40:33 +02001213 assert_equal(11, g:exported)
1214 assert_equal(18, g:exported_added)
Bram Moolenaar1c991142020-07-04 13:15:31 +02001215 assert_equal('Exported', g:imported_func)
1216
1217 # exported script not sourced again
1218 assert_false(exists('g:result'))
Bram Moolenaar24e93162021-07-18 20:40:33 +02001219 unlet g:exported
1220 unlet g:exported_added
Bram Moolenaar1c991142020-07-04 13:15:31 +02001221 unlet g:imported_func
1222 delete('Ximport_lbr.vim')
1223
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001224 var import_star_as_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001225 vim9script
1226 import * as Export from './Xexport.vim'
1227 def UseExport()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001228 g:exported_def = Export.exported
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001229 enddef
Bram Moolenaar24e93162021-07-18 20:40:33 +02001230 g:exported_script = Export.exported
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001231 assert_equal(1, exists('Export.exported'))
1232 assert_equal(0, exists('Export.notexported'))
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001233 UseExport()
1234 END
1235 writefile(import_star_as_lines, 'Ximport.vim')
1236 source Ximport.vim
Bram Moolenaar24e93162021-07-18 20:40:33 +02001237
1238 assert_equal(18, g:exported_def)
1239 assert_equal(18, g:exported_script)
1240 unlet g:exported_def
1241 unlet g:exported_script
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001242
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001243 var import_star_as_lines_no_dot =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001244 vim9script
1245 import * as Export from './Xexport.vim'
1246 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001247 var dummy = 1
1248 var imported = Export + dummy
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001249 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001250 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001251 END
1252 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001253 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001254
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001255 var import_star_as_lines_dot_space =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001256 vim9script
1257 import * as Export from './Xexport.vim'
1258 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001259 var imported = Export . exported
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001260 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001261 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001262 END
1263 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001264 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001265
Bram Moolenaar921ba522021-07-29 22:25:05 +02001266 var import_func_duplicated =<< trim END
1267 vim9script
1268 import ExportedInc from './Xexport.vim'
1269 import ExportedInc from './Xexport.vim'
1270
1271 ExportedInc()
1272 END
1273 writefile(import_func_duplicated, 'Ximport.vim')
1274 assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
1275
Bram Moolenaara6294952020-12-27 13:39:50 +01001276 var import_star_as_duplicated =<< trim END
1277 vim9script
1278 import * as Export from './Xexport.vim'
1279 var some = 'other'
1280 import * as Export from './Xexport.vim'
1281 defcompile
1282 END
1283 writefile(import_star_as_duplicated, 'Ximport.vim')
1284 assert_fails('source Ximport.vim', 'E1073:', '', 4, 'Ximport.vim')
1285
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001286 var import_star_as_lines_script_no_dot =<< trim END
1287 vim9script
1288 import * as Export from './Xexport.vim'
1289 g:imported_script = Export exported
1290 END
1291 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
1292 assert_fails('source Ximport.vim', 'E1029:')
1293
1294 var import_star_as_lines_script_space_after_dot =<< trim END
1295 vim9script
1296 import * as Export from './Xexport.vim'
1297 g:imported_script = Export. exported
1298 END
1299 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
1300 assert_fails('source Ximport.vim', 'E1074:')
1301
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001302 var import_star_as_lines_missing_name =<< trim END
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001303 vim9script
1304 import * as Export from './Xexport.vim'
1305 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001306 var imported = Export.
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001307 enddef
Bram Moolenaar822ba242020-05-24 23:00:18 +02001308 defcompile
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001309 END
1310 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001311 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001312
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001313 var import_star_as_lbr_lines =<< trim END
Bram Moolenaar1c991142020-07-04 13:15:31 +02001314 vim9script
1315 import *
1316 as Export
1317 from
1318 './Xexport.vim'
1319 def UseExport()
Bram Moolenaar24e93162021-07-18 20:40:33 +02001320 g:exported = Export.exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001321 enddef
1322 UseExport()
1323 END
1324 writefile(import_star_as_lbr_lines, 'Ximport.vim')
1325 source Ximport.vim
Bram Moolenaar24e93162021-07-18 20:40:33 +02001326 assert_equal(18, g:exported)
1327 unlet g:exported
Bram Moolenaar1c991142020-07-04 13:15:31 +02001328
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001329 var import_star_lines =<< trim END
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001330 vim9script
1331 import * from './Xexport.vim'
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001332 END
1333 writefile(import_star_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001334 assert_fails('source Ximport.vim', 'E1045:', '', 2, 'Ximport.vim')
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001335
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001336 # try to import something that exists but is not exported
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001337 var import_not_exported_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001338 vim9script
1339 import name from './Xexport.vim'
1340 END
1341 writefile(import_not_exported_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001342 assert_fails('source Ximport.vim', 'E1049:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001343
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001344 # try to import something that is already defined
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001345 var import_already_defined =<< trim END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001346 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001347 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001348 import exported from './Xexport.vim'
1349 END
1350 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001351 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001352
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001353 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001354 import_already_defined =<< trim END
1355 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001356 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001357 import * as exported from './Xexport.vim'
1358 END
1359 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001360 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001361
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001362 # try to import something that is already defined
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001363 import_already_defined =<< trim END
1364 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001365 var exported = 'something'
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001366 import {exported} from './Xexport.vim'
1367 END
1368 writefile(import_already_defined, 'Ximport.vim')
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001369 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001370
Bram Moolenaar918a4242020-12-06 14:37:08 +01001371 # try changing an imported const
1372 var import_assign_to_const =<< trim END
1373 vim9script
1374 import CONST from './Xexport.vim'
1375 def Assign()
1376 CONST = 987
1377 enddef
1378 defcompile
1379 END
1380 writefile(import_assign_to_const, 'Ximport.vim')
1381 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1382
Bram Moolenaar0f2a5cc2021-02-27 22:33:21 +01001383 # try changing an imported final
1384 var import_assign_to_final =<< trim END
1385 vim9script
1386 import theList from './Xexport.vim'
1387 def Assign()
1388 theList = [2]
1389 enddef
1390 defcompile
1391 END
1392 writefile(import_assign_to_final, 'Ximport.vim')
1393 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
1394
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001395 # import a very long name, requires making a copy
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001396 var import_long_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001397 vim9script
1398 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
1399 END
1400 writefile(import_long_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001401 assert_fails('source Ximport.vim', 'E1048:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001402
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001403 var import_no_from_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001404 vim9script
1405 import name './Xexport.vim'
1406 END
1407 writefile(import_no_from_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001408 assert_fails('source Ximport.vim', 'E1070:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001409
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001410 var import_invalid_string_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001411 vim9script
1412 import name from Xexport.vim
1413 END
1414 writefile(import_invalid_string_lines, 'Ximport.vim')
Bram Moolenaar4db572e2021-07-18 18:21:38 +02001415 assert_fails('source Ximport.vim', 'E121:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001416
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001417 var import_wrong_name_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001418 vim9script
1419 import name from './XnoExport.vim'
1420 END
1421 writefile(import_wrong_name_lines, 'Ximport.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001422 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001423
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001424 var import_missing_comma_lines =<< trim END
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001425 vim9script
1426 import {exported name} from './Xexport.vim'
1427 END
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001428 writefile(import_missing_comma_lines, 'Ximport3.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001429 assert_fails('source Ximport3.vim', 'E1046:', '', 2, 'Ximport3.vim')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +01001430
Bram Moolenaar60579352021-07-19 21:45:07 +02001431 var import_redefining_lines =<< trim END
1432 vim9script
1433 import exported from './Xexport.vim'
1434 var exported = 5
1435 END
1436 writefile(import_redefining_lines, 'Ximport.vim')
1437 assert_fails('source Ximport.vim', 'E1213: Redefining imported item "exported"', '', 3)
1438
1439 var import_assign_wrong_type_lines =<< trim END
1440 vim9script
1441 import exported from './Xexport.vim'
1442 exported = 'xxx'
1443 END
1444 writefile(import_assign_wrong_type_lines, 'Ximport.vim')
1445 assert_fails('source Ximport.vim', 'E1012: Type mismatch; expected number but got string', '', 3)
1446
1447 var import_assign_const_lines =<< trim END
1448 vim9script
1449 import CONST from './Xexport.vim'
1450 CONST = 4321
1451 END
1452 writefile(import_assign_const_lines, 'Ximport.vim')
1453 assert_fails('source Ximport.vim', 'E741: Value is locked: CONST', '', 3)
1454
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001455 delete('Ximport.vim')
Bram Moolenaar5269bd22020-03-09 19:25:27 +01001456 delete('Ximport3.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 delete('Xexport.vim')
1458
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001459 # Check that in a Vim9 script 'cpo' is set to the Vim default.
Bram Moolenaar3e191692021-03-17 17:46:00 +01001460 # Flags added or removed are also applied to the restored value.
1461 set cpo=abcd
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001462 var lines =<< trim END
Bram Moolenaar750802b2020-02-23 18:08:33 +01001463 vim9script
1464 g:cpo_in_vim9script = &cpo
Bram Moolenaar3e191692021-03-17 17:46:00 +01001465 set cpo+=f
1466 set cpo-=c
1467 g:cpo_after_vim9script = &cpo
Bram Moolenaar750802b2020-02-23 18:08:33 +01001468 END
1469 writefile(lines, 'Xvim9_script')
1470 source Xvim9_script
Bram Moolenaar3e191692021-03-17 17:46:00 +01001471 assert_equal('fabd', &cpo)
Bram Moolenaar750802b2020-02-23 18:08:33 +01001472 set cpo&vim
1473 assert_equal(&cpo, g:cpo_in_vim9script)
Bram Moolenaar3e191692021-03-17 17:46:00 +01001474 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
1475 assert_equal(newcpo, g:cpo_after_vim9script)
1476
Bram Moolenaar750802b2020-02-23 18:08:33 +01001477 delete('Xvim9_script')
1478enddef
1479
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001480def Test_import_funcref()
1481 var lines =<< trim END
1482 vim9script
1483 export def F(): number
1484 return 42
1485 enddef
1486 export const G = F
1487 END
1488 writefile(lines, 'Xlib.vim')
1489
1490 lines =<< trim END
1491 vim9script
1492 import {G} from './Xlib.vim'
1493 const Foo = G()
1494 assert_equal(42, Foo)
1495
1496 def DoTest()
1497 const Goo = G()
Bram Moolenaar06ca48a2021-10-23 10:25:21 +01001498 assert_equal(42, Goo)
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001499 enddef
1500 DoTest()
1501 END
1502 CheckScriptSuccess(lines)
1503
1504 delete('Xlib.vim')
1505enddef
1506
Bram Moolenaar6853c382021-09-07 22:12:19 +02001507def Test_import_star_fails()
1508 writefile([], 'Xfoo.vim')
1509 var lines =<< trim END
Bram Moolenaaraf2d5d22021-09-07 22:35:34 +02001510 import * as foo from './Xfoo.vim'
Bram Moolenaar6853c382021-09-07 22:12:19 +02001511 foo = 'bar'
1512 END
1513 CheckDefAndScriptFailure2(lines, 'E1094:', 'E1236: Cannot use foo itself')
1514 lines =<< trim END
1515 vim9script
Bram Moolenaaraf2d5d22021-09-07 22:35:34 +02001516 import * as foo from './Xfoo.vim'
Bram Moolenaar6853c382021-09-07 22:12:19 +02001517 var that = foo
1518 END
1519 CheckScriptFailure(lines, 'E1029: Expected ''.''')
Bram Moolenaara9e3d562021-09-08 12:31:35 +02001520
1521 lines =<< trim END
1522 vim9script
1523 import * as 9foo from './Xfoo.vim'
1524 END
1525 CheckScriptFailure(lines, 'E1047:')
1526 lines =<< trim END
1527 vim9script
1528 import * as the#foo from './Xfoo.vim'
1529 END
1530 CheckScriptFailure(lines, 'E1047:')
1531 lines =<< trim END
1532 vim9script
1533 import * as g:foo from './Xfoo.vim'
1534 END
1535 CheckScriptFailure(lines, 'E1047:')
1536
Bram Moolenaaraf2d5d22021-09-07 22:35:34 +02001537 delete('Xfoo.vim')
Bram Moolenaar6853c382021-09-07 22:12:19 +02001538enddef
1539
Bram Moolenaar0a842842021-02-27 22:41:19 +01001540def Test_import_as()
1541 var export_lines =<< trim END
1542 vim9script
1543 export var one = 1
1544 export var yes = 'yes'
Bram Moolenaarc967d572021-07-08 21:38:50 +02001545 export var slist: list<string>
Bram Moolenaar0a842842021-02-27 22:41:19 +01001546 END
1547 writefile(export_lines, 'XexportAs')
1548
1549 var import_lines =<< trim END
1550 vim9script
Bram Moolenaar6c7cc342021-04-17 16:38:50 +02001551 var one = 'notused'
1552 var yes = 777
Bram Moolenaar0a842842021-02-27 22:41:19 +01001553 import one as thatOne from './XexportAs'
1554 assert_equal(1, thatOne)
1555 import yes as yesYes from './XexportAs'
1556 assert_equal('yes', yesYes)
1557 END
1558 CheckScriptSuccess(import_lines)
1559
1560 import_lines =<< trim END
1561 vim9script
1562 import {one as thatOne, yes as yesYes} from './XexportAs'
1563 assert_equal(1, thatOne)
1564 assert_equal('yes', yesYes)
1565 assert_fails('echo one', 'E121:')
1566 assert_fails('echo yes', 'E121:')
1567 END
1568 CheckScriptSuccess(import_lines)
1569
Bram Moolenaarc967d572021-07-08 21:38:50 +02001570 import_lines =<< trim END
1571 vim9script
1572 import {slist as impSlist} from './XexportAs'
1573 impSlist->add(123)
1574 END
1575 CheckScriptFailure(import_lines, 'E1012: Type mismatch; expected string but got number')
1576
Bram Moolenaar0a842842021-02-27 22:41:19 +01001577 delete('XexportAs')
1578enddef
1579
Bram Moolenaar803af682020-08-05 16:20:03 +02001580func g:Trigger()
1581 source Ximport.vim
1582 return "echo 'yes'\<CR>"
1583endfunc
1584
1585def Test_import_export_expr_map()
1586 # check that :import and :export work when buffer is locked
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001587 var export_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001588 vim9script
1589 export def That(): string
1590 return 'yes'
1591 enddef
1592 END
1593 writefile(export_lines, 'Xexport_that.vim')
1594
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001595 var import_lines =<< trim END
Bram Moolenaar803af682020-08-05 16:20:03 +02001596 vim9script
1597 import That from './Xexport_that.vim'
1598 assert_equal('yes', That())
1599 END
1600 writefile(import_lines, 'Ximport.vim')
1601
1602 nnoremap <expr> trigger g:Trigger()
1603 feedkeys('trigger', "xt")
1604
Bram Moolenaar730b2482020-08-09 13:02:10 +02001605 delete('Xexport_that.vim')
Bram Moolenaar803af682020-08-05 16:20:03 +02001606 delete('Ximport.vim')
1607 nunmap trigger
1608enddef
1609
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001610def Test_import_in_filetype()
1611 # check that :import works when the buffer is locked
1612 mkdir('ftplugin', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001613 var export_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001614 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001615 export var That = 'yes'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001616 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001617 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001618
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001619 var import_lines =<< trim END
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001620 vim9script
Bram Moolenaar730b2482020-08-09 13:02:10 +02001621 import That from './Xexport_ft.vim'
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001622 assert_equal('yes', That)
1623 g:did_load_mytpe = 1
1624 END
1625 writefile(import_lines, 'ftplugin/qf.vim')
1626
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001627 var save_rtp = &rtp
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001628 &rtp = getcwd() .. ',' .. &rtp
1629
1630 filetype plugin on
1631 copen
1632 assert_equal(1, g:did_load_mytpe)
1633
1634 quit!
Bram Moolenaar730b2482020-08-09 13:02:10 +02001635 delete('Xexport_ft.vim')
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02001636 delete('ftplugin', 'rf')
1637 &rtp = save_rtp
1638enddef
1639
Bram Moolenaarefa94442020-08-08 22:16:00 +02001640def Test_use_import_in_mapping()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001641 var lines =<< trim END
Bram Moolenaarefa94442020-08-08 22:16:00 +02001642 vim9script
1643 export def Funcx()
1644 g:result = 42
1645 enddef
1646 END
1647 writefile(lines, 'XsomeExport.vim')
1648 lines =<< trim END
1649 vim9script
1650 import Funcx from './XsomeExport.vim'
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001651 nnoremap <F3> :call <sid>Funcx()<cr>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001652 END
1653 writefile(lines, 'Xmapscript.vim')
1654
1655 source Xmapscript.vim
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001656 feedkeys("\<F3>", "xt")
Bram Moolenaarefa94442020-08-08 22:16:00 +02001657 assert_equal(42, g:result)
1658
1659 unlet g:result
1660 delete('XsomeExport.vim')
1661 delete('Xmapscript.vim')
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02001662 nunmap <F3>
Bram Moolenaarefa94442020-08-08 22:16:00 +02001663enddef
1664
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01001665def Test_vim9script_mix()
1666 var lines =<< trim END
1667 if has(g:feature)
1668 " legacy script
1669 let g:legacy = 1
1670 finish
1671 endif
1672 vim9script
1673 g:legacy = 0
1674 END
1675 g:feature = 'eval'
1676 g:legacy = -1
1677 CheckScriptSuccess(lines)
1678 assert_equal(1, g:legacy)
1679
1680 g:feature = 'noteval'
1681 g:legacy = -1
1682 CheckScriptSuccess(lines)
1683 assert_equal(0, g:legacy)
1684enddef
1685
Bram Moolenaar750802b2020-02-23 18:08:33 +01001686def Test_vim9script_fails()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
1688 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001689 CheckScriptFailure(['export var some = 123'], 'E1042:')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001690 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001691 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
Bram Moolenaar750802b2020-02-23 18:08:33 +01001692 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
1693
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001694 CheckScriptFailure(['vim9script', 'var str: string', 'str = 1234'], 'E1012:')
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001695 CheckScriptFailure(['vim9script', 'const str = "asdf"', 'str = "xxx"'], 'E46:')
1696
Bram Moolenaare2e40752020-09-04 21:18:46 +02001697 assert_fails('vim9script', 'E1038:')
1698 assert_fails('export something', 'E1043:')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699enddef
1700
Bram Moolenaarf7d267e2020-06-17 12:04:54 +02001701func Test_import_fails_without_script()
Bram Moolenaar101f4812020-06-16 23:18:51 +02001702 CheckRunVimInTerminal
1703
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001704 " call indirectly to avoid compilation error for missing functions
Bram Moolenaarc620c052020-07-08 15:16:19 +02001705 call Run_Test_import_fails_on_command_line()
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001706endfunc
1707
Bram Moolenaarc620c052020-07-08 15:16:19 +02001708def Run_Test_import_fails_on_command_line()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001709 var export =<< trim END
Bram Moolenaar101f4812020-06-16 23:18:51 +02001710 vim9script
1711 export def Foo(): number
1712 return 0
1713 enddef
1714 END
Bram Moolenaar730b2482020-08-09 13:02:10 +02001715 writefile(export, 'XexportCmd.vim')
Bram Moolenaar101f4812020-06-16 23:18:51 +02001716
Bram Moolenaare0de1712020-12-02 17:36:54 +01001717 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001718 rows: 6, wait_for_ruler: 0})
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001719 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaar101f4812020-06-16 23:18:51 +02001720
Bram Moolenaar730b2482020-08-09 13:02:10 +02001721 delete('XexportCmd.vim')
Bram Moolenaar9bb3eb32020-06-17 20:03:36 +02001722 StopVimInTerminal(buf)
1723enddef
Bram Moolenaar101f4812020-06-16 23:18:51 +02001724
Bram Moolenaar2b327002020-12-26 15:39:31 +01001725def Test_vim9script_reload_noclear()
1726 var lines =<< trim END
Bram Moolenaara6294952020-12-27 13:39:50 +01001727 vim9script
1728 export var exported = 'thexport'
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001729
1730 export def TheFunc(x = 0)
1731 enddef
Bram Moolenaara6294952020-12-27 13:39:50 +01001732 END
1733 writefile(lines, 'XExportReload')
1734 lines =<< trim END
Bram Moolenaar2b327002020-12-26 15:39:31 +01001735 vim9script noclear
1736 g:loadCount += 1
1737 var s:reloaded = 'init'
Bram Moolenaara6294952020-12-27 13:39:50 +01001738 import exported from './XExportReload'
Bram Moolenaar2b327002020-12-26 15:39:31 +01001739
1740 def Again(): string
1741 return 'again'
1742 enddef
1743
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001744 import TheFunc from './XExportReload'
1745 TheFunc()
1746
Bram Moolenaar2b327002020-12-26 15:39:31 +01001747 if exists('s:loaded') | finish | endif
1748 var s:loaded = true
1749
1750 var s:notReloaded = 'yes'
1751 s:reloaded = 'first'
1752 def g:Values(): list<string>
Bram Moolenaara6294952020-12-27 13:39:50 +01001753 return [s:reloaded, s:notReloaded, Again(), Once(), exported]
Bram Moolenaar2b327002020-12-26 15:39:31 +01001754 enddef
1755
1756 def Once(): string
1757 return 'once'
1758 enddef
1759 END
1760 writefile(lines, 'XReloaded')
1761 g:loadCount = 0
1762 source XReloaded
1763 assert_equal(1, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001764 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001765 source XReloaded
1766 assert_equal(2, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001767 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001768 source XReloaded
1769 assert_equal(3, g:loadCount)
Bram Moolenaara6294952020-12-27 13:39:50 +01001770 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
Bram Moolenaar2b327002020-12-26 15:39:31 +01001771
Bram Moolenaar48e11c12021-01-11 18:47:00 +01001772 delete('XReloaded')
Bram Moolenaara6294952020-12-27 13:39:50 +01001773 delete('XExportReload')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001774 delfunc g:Values
Bram Moolenaar2b327002020-12-26 15:39:31 +01001775 unlet g:loadCount
Bram Moolenaar577dc932021-06-27 15:35:40 +02001776
1777 lines =<< trim END
1778 vim9script
1779 def Inner()
1780 enddef
1781 END
1782 lines->writefile('XreloadScript.vim')
1783 source XreloadScript.vim
1784
1785 lines =<< trim END
1786 vim9script
1787 def Outer()
1788 def Inner()
1789 enddef
1790 enddef
1791 defcompile
1792 END
1793 lines->writefile('XreloadScript.vim')
1794 source XreloadScript.vim
1795
1796 delete('XreloadScript.vim')
Bram Moolenaar2b327002020-12-26 15:39:31 +01001797enddef
1798
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001799def Test_vim9script_reload_import()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001800 var lines =<< trim END
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 vim9script
1802 const var = ''
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001803 var valone = 1234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 def MyFunc(arg: string)
1805 valone = 5678
1806 enddef
1807 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001808 var morelines =<< trim END
1809 var valtwo = 222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 export def GetValtwo(): number
1811 return valtwo
1812 enddef
1813 END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001814 writefile(lines + morelines, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 source Xreload.vim
1816 source Xreload.vim
1817 source Xreload.vim
1818
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001819 # cannot declare a var twice
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 lines =<< trim END
1821 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001822 var valone = 1234
1823 var valone = 5678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 END
1825 writefile(lines, 'Xreload.vim')
Bram Moolenaar44d66522020-09-06 22:26:57 +02001826 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827
1828 delete('Xreload.vim')
1829 delete('Ximport.vim')
1830enddef
1831
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001832" if a script is reloaded with a script-local variable that changed its type, a
1833" compiled function using that variable must fail.
1834def Test_script_reload_change_type()
1835 var lines =<< trim END
1836 vim9script noclear
1837 var str = 'string'
1838 def g:GetStr(): string
1839 return str .. 'xxx'
1840 enddef
1841 END
1842 writefile(lines, 'Xreload.vim')
1843 source Xreload.vim
1844 echo g:GetStr()
1845
1846 lines =<< trim END
1847 vim9script noclear
1848 var str = 1234
1849 END
1850 writefile(lines, 'Xreload.vim')
1851 source Xreload.vim
1852 assert_fails('echo g:GetStr()', 'E1150:')
1853
1854 delfunc g:GetStr
1855 delete('Xreload.vim')
1856enddef
1857
Bram Moolenaarc970e422021-03-17 15:03:04 +01001858" Define CallFunc so that the test can be compiled
1859command CallFunc echo 'nop'
1860
1861def Test_script_reload_from_function()
1862 var lines =<< trim END
1863 vim9script
1864
1865 if exists('g:loaded')
1866 finish
1867 endif
1868 g:loaded = 1
1869 delcommand CallFunc
1870 command CallFunc Func()
1871 def Func()
Bram Moolenaara4c81be2021-03-17 15:23:16 +01001872 so XreloadFunc.vim
Bram Moolenaarc970e422021-03-17 15:03:04 +01001873 g:didTheFunc = 1
1874 enddef
1875 END
1876 writefile(lines, 'XreloadFunc.vim')
1877 source XreloadFunc.vim
1878 CallFunc
1879 assert_equal(1, g:didTheFunc)
1880
1881 delete('XreloadFunc.vim')
1882 delcommand CallFunc
1883 unlet g:loaded
1884 unlet g:didTheFunc
1885enddef
1886
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001887def Test_script_var_shadows_function()
1888 var lines =<< trim END
1889 vim9script
1890 def Func(): number
1891 return 123
1892 enddef
1893 var Func = 1
1894 END
1895 CheckScriptFailure(lines, 'E1041:', 5)
1896enddef
1897
Bram Moolenaarc3235272021-07-10 19:42:03 +02001898def Test_script_var_shadows_command()
1899 var lines =<< trim END
1900 var undo = 1
1901 undo = 2
1902 assert_equal(2, undo)
1903 END
1904 CheckDefAndScriptSuccess(lines)
1905
1906 lines =<< trim END
1907 var undo = 1
1908 undo
1909 END
1910 CheckDefAndScriptFailure(lines, 'E1207:', 2)
1911enddef
1912
Bram Moolenaar95006e32020-08-29 17:47:08 +02001913def s:RetSome(): string
1914 return 'some'
1915enddef
1916
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001917" Not exported function that is referenced needs to be accessed by the
1918" script-local name.
1919def Test_vim9script_funcref()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001920 var sortlines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001921 vim9script
1922 def Compare(i1: number, i2: number): number
Bram Moolenaarbed36b92020-07-07 23:31:36 +02001923 return i2 - i1
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001924 enddef
1925
1926 export def FastSort(): list<number>
1927 return range(5)->sort(Compare)
1928 enddef
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001929
1930 export def GetString(arg: string): string
1931 return arg
1932 enddef
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001933 END
1934 writefile(sortlines, 'Xsort.vim')
1935
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001936 var lines =<< trim END
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001937 vim9script
1938 import FastSort from './Xsort.vim'
1939 def Test()
1940 g:result = FastSort()
1941 enddef
1942 Test()
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001943
1944 # using a function imported with "as"
1945 import * as anAlias from './Xsort.vim'
1946 assert_equal('yes', anAlias.GetString('yes'))
1947
1948 # using the function from a compiled function
1949 def TestMore(): string
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001950 var s = s:anAlias.GetString('foo')
1951 return s .. anAlias.GetString('bar')
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001952 enddef
Bram Moolenaarca51cc02021-04-01 21:38:53 +02001953 assert_equal('foobar', TestMore())
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02001954
1955 # error when using a function that isn't exported
1956 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001957 END
1958 writefile(lines, 'Xscript.vim')
1959
1960 source Xscript.vim
1961 assert_equal([4, 3, 2, 1, 0], g:result)
1962
1963 unlet g:result
1964 delete('Xsort.vim')
1965 delete('Xscript.vim')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001966
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001967 var Funcref = function('s:RetSome')
Bram Moolenaar95006e32020-08-29 17:47:08 +02001968 assert_equal('some', Funcref())
Bram Moolenaarfe465a02020-07-07 22:50:12 +02001969enddef
1970
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001971" Check that when searching for "FilterFunc" it finds the import in the
1972" script where FastFilter() is called from, both as a string and as a direct
1973" function reference.
Bram Moolenaarc620c052020-07-08 15:16:19 +02001974def Test_vim9script_funcref_other_script()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001975 var filterLines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001976 vim9script
1977 export def FilterFunc(idx: number, val: number): bool
1978 return idx % 2 == 1
1979 enddef
1980 export def FastFilter(): list<number>
1981 return range(10)->filter('FilterFunc')
1982 enddef
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001983 export def FastFilterDirect(): list<number>
1984 return range(10)->filter(FilterFunc)
1985 enddef
Bram Moolenaarc620c052020-07-08 15:16:19 +02001986 END
1987 writefile(filterLines, 'Xfilter.vim')
1988
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001989 var lines =<< trim END
Bram Moolenaarc620c052020-07-08 15:16:19 +02001990 vim9script
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001991 import {FilterFunc, FastFilter, FastFilterDirect} from './Xfilter.vim'
Bram Moolenaarc620c052020-07-08 15:16:19 +02001992 def Test()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001993 var x: list<number> = FastFilter()
Bram Moolenaarc620c052020-07-08 15:16:19 +02001994 enddef
1995 Test()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001996 def TestDirect()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001997 var x: list<number> = FastFilterDirect()
Bram Moolenaar601e76a2020-08-27 21:33:10 +02001998 enddef
1999 TestDirect()
Bram Moolenaarc620c052020-07-08 15:16:19 +02002000 END
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002001 CheckScriptSuccess(lines)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002002 delete('Xfilter.vim')
Bram Moolenaarc620c052020-07-08 15:16:19 +02002003enddef
2004
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002005def Test_vim9script_reload_delfunc()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002006 var first_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002007 vim9script
2008 def FuncYes(): string
2009 return 'yes'
2010 enddef
2011 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002012 var withno_lines =<< trim END
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002013 def FuncNo(): string
2014 return 'no'
2015 enddef
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002016 def g:DoCheck(no_exists: bool)
2017 assert_equal('yes', FuncYes())
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002018 assert_equal('no', FuncNo())
2019 enddef
2020 END
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002021 var nono_lines =<< trim END
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002022 def g:DoCheck(no_exists: bool)
2023 assert_equal('yes', FuncYes())
Bram Moolenaar44d66522020-09-06 22:26:57 +02002024 assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002025 enddef
2026 END
2027
2028 # FuncNo() is defined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002029 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002030 source Xreloaded.vim
2031 g:DoCheck(true)
2032
2033 # FuncNo() is not redefined
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002034 writefile(first_lines + nono_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002035 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01002036 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002037
2038 # FuncNo() is back
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02002039 writefile(first_lines + withno_lines, 'Xreloaded.vim')
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002040 source Xreloaded.vim
Bram Moolenaar50824712020-12-20 21:10:17 +01002041 g:DoCheck(false)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002042
2043 delete('Xreloaded.vim')
2044enddef
2045
Bram Moolenaar89483d42020-05-10 15:24:44 +02002046def Test_vim9script_reload_delvar()
2047 # write the script with a script-local variable
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002048 var lines =<< trim END
Bram Moolenaar89483d42020-05-10 15:24:44 +02002049 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002050 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02002051 END
2052 writefile(lines, 'XreloadVar.vim')
2053 source XreloadVar.vim
2054
2055 # now write the script using the same variable locally - works
2056 lines =<< trim END
2057 vim9script
2058 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002059 var name = 'string'
Bram Moolenaar89483d42020-05-10 15:24:44 +02002060 enddef
2061 END
2062 writefile(lines, 'XreloadVar.vim')
2063 source XreloadVar.vim
2064
2065 delete('XreloadVar.vim')
2066enddef
2067
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068def Test_import_absolute()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002069 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02002070 'vim9script',
2071 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"',
2072 'def UseExported()',
2073 ' g:imported_abs = exported',
2074 ' exported = 8888',
2075 ' g:imported_after = exported',
2076 'enddef',
2077 'UseExported()',
2078 'g:import_disassembled = execute("disass UseExported")',
2079 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080 writefile(import_lines, 'Ximport_abs.vim')
2081 writefile(s:export_script_lines, 'Xexport_abs.vim')
2082
2083 source Ximport_abs.vim
2084
2085 assert_equal(9876, g:imported_abs)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002086 assert_equal(8888, g:imported_after)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002087 assert_match('<SNR>\d\+_UseExported\_s*' ..
2088 'g:imported_abs = exported\_s*' ..
2089 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
2090 '1 STOREG g:imported_abs\_s*' ..
2091 'exported = 8888\_s*' ..
2092 '2 PUSHNR 8888\_s*' ..
2093 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
2094 'g:imported_after = exported\_s*' ..
2095 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
2096 '5 STOREG g:imported_after',
Bram Moolenaare6085c52020-04-12 20:19:16 +02002097 g:import_disassembled)
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002098
2099 Undo_export_script_lines()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 unlet g:imported_abs
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002101 unlet g:import_disassembled
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102
2103 delete('Ximport_abs.vim')
2104 delete('Xexport_abs.vim')
2105enddef
2106
2107def Test_import_rtp()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002108 var import_lines = [
Bram Moolenaare6085c52020-04-12 20:19:16 +02002109 'vim9script',
2110 'import exported from "Xexport_rtp.vim"',
2111 'g:imported_rtp = exported',
2112 ]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 writefile(import_lines, 'Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002114 mkdir('import', 'p')
2115 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002117 var save_rtp = &rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 &rtp = getcwd()
2119 source Ximport_rtp.vim
2120 &rtp = save_rtp
2121
2122 assert_equal(9876, g:imported_rtp)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123
Bram Moolenaarb3ca9822020-08-09 14:43:58 +02002124 Undo_export_script_lines()
2125 unlet g:imported_rtp
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 delete('Ximport_rtp.vim')
Bram Moolenaarb885a7c2021-07-08 22:02:11 +02002127 delete('import', 'rf')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128enddef
2129
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002130def Test_import_compile_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002131 var export_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002132 'vim9script',
2133 'export def ExpFunc(): string',
2134 ' return notDefined',
2135 'enddef',
2136 ]
2137 writefile(export_lines, 'Xexported.vim')
2138
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002139 var import_lines = [
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002140 'vim9script',
2141 'import ExpFunc from "./Xexported.vim"',
2142 'def ImpFunc()',
2143 ' echo ExpFunc()',
2144 'enddef',
2145 'defcompile',
2146 ]
2147 writefile(import_lines, 'Ximport.vim')
2148
2149 try
2150 source Ximport.vim
2151 catch /E1001/
Dominique Pelle923dce22021-11-21 11:36:04 +00002152 # Error should be before the Xexported.vim file.
Bram Moolenaar77072282020-09-16 17:55:40 +02002153 assert_match('E1001: Variable not found: notDefined', v:exception)
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002154 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
2155 endtry
2156
2157 delete('Xexported.vim')
2158 delete('Ximport.vim')
2159enddef
2160
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002161def Test_func_redefine_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002162 var lines = [
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002163 'vim9script',
2164 'def Func()',
2165 ' eval [][0]',
2166 'enddef',
2167 'Func()',
2168 ]
2169 writefile(lines, 'Xtestscript.vim')
2170
2171 for count in range(3)
2172 try
2173 source Xtestscript.vim
2174 catch /E684/
2175 # function name should contain <SNR> every time
2176 assert_match('E684: list index out of range', v:exception)
2177 assert_match('function <SNR>\d\+_Func, line 1', v:throwpoint)
2178 endtry
2179 endfor
2180
2181 delete('Xtestscript.vim')
2182enddef
2183
Bram Moolenaareef21022020-08-01 22:16:43 +02002184def Test_func_overrules_import_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002185 var export_lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002186 vim9script
2187 export def Func()
2188 echo 'imported'
2189 enddef
2190 END
2191 writefile(export_lines, 'XexportedFunc.vim')
2192
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002193 var lines =<< trim END
Bram Moolenaareef21022020-08-01 22:16:43 +02002194 vim9script
2195 import Func from './XexportedFunc.vim'
2196 def Func()
2197 echo 'local to function'
2198 enddef
2199 END
2200 CheckScriptFailure(lines, 'E1073:')
2201
2202 lines =<< trim END
2203 vim9script
2204 import Func from './XexportedFunc.vim'
2205 def Outer()
2206 def Func()
2207 echo 'local to function'
2208 enddef
2209 enddef
2210 defcompile
2211 END
2212 CheckScriptFailure(lines, 'E1073:')
2213
2214 delete('XexportedFunc.vim')
2215enddef
2216
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002217def Test_func_redefine_fails()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002218 var lines =<< trim END
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002219 vim9script
2220 def Func()
2221 echo 'one'
2222 enddef
2223 def Func()
2224 echo 'two'
2225 enddef
2226 END
2227 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002228
2229 lines =<< trim END
2230 vim9script
2231 def Foo(): string
2232 return 'foo'
2233 enddef
2234 def Func()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002235 var Foo = {-> 'lambda'}
Bram Moolenaarfa211f32020-08-07 22:00:26 +02002236 enddef
2237 defcompile
2238 END
2239 CheckScriptFailure(lines, 'E1073:')
Bram Moolenaarb9a2cac2020-08-01 22:23:20 +02002240enddef
2241
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242def Test_fixed_size_list()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002243 # will be allocated as one piece of memory, check that changes work
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002244 var l = [1, 2, 3, 4]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 l->remove(0)
2246 l->add(5)
2247 l->insert(99, 1)
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002248 assert_equal([2, 99, 3, 4, 5], l)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249enddef
2250
Bram Moolenaarae616492020-07-28 20:07:27 +02002251def Test_no_insert_xit()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002252 CheckDefExecFailure(['a = 1'], 'E1100:')
2253 CheckDefExecFailure(['c = 1'], 'E1100:')
2254 CheckDefExecFailure(['i = 1'], 'E1100:')
2255 CheckDefExecFailure(['t = 1'], 'E1100:')
2256 CheckDefExecFailure(['x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002257
Bram Moolenaarae616492020-07-28 20:07:27 +02002258 CheckScriptFailure(['vim9script', 'a = 1'], 'E488:')
2259 CheckScriptFailure(['vim9script', 'a'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002260 CheckScriptFailure(['vim9script', 'c = 1'], 'E488:')
2261 CheckScriptFailure(['vim9script', 'c'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002262 CheckScriptFailure(['vim9script', 'i = 1'], 'E488:')
2263 CheckScriptFailure(['vim9script', 'i'], 'E1100:')
Bram Moolenaar65088802021-03-13 21:07:21 +01002264 CheckScriptFailure(['vim9script', 'o = 1'], 'E1100:')
2265 CheckScriptFailure(['vim9script', 'o'], 'E1100:')
Bram Moolenaarf5a48012020-08-01 17:00:03 +02002266 CheckScriptFailure(['vim9script', 't'], 'E1100:')
2267 CheckScriptFailure(['vim9script', 't = 1'], 'E1100:')
2268 CheckScriptFailure(['vim9script', 'x = 1'], 'E1100:')
Bram Moolenaarae616492020-07-28 20:07:27 +02002269enddef
2270
Bram Moolenaar158906c2020-02-06 20:39:45 +01002271def IfElse(what: number): string
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002272 var res = ''
Bram Moolenaar158906c2020-02-06 20:39:45 +01002273 if what == 1
2274 res = "one"
2275 elseif what == 2
2276 res = "two"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002277 else
Bram Moolenaar158906c2020-02-06 20:39:45 +01002278 res = "three"
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002279 endif
Bram Moolenaar158906c2020-02-06 20:39:45 +01002280 return res
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002281enddef
2282
Bram Moolenaar158906c2020-02-06 20:39:45 +01002283def Test_if_elseif_else()
2284 assert_equal('one', IfElse(1))
2285 assert_equal('two', IfElse(2))
2286 assert_equal('three', IfElse(3))
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002287enddef
2288
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002289def Test_if_elseif_else_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002290 CheckDefFailure(['elseif true'], 'E582:')
2291 CheckDefFailure(['else'], 'E581:')
2292 CheckDefFailure(['endif'], 'E580:')
Bram Moolenaarced68a02021-01-24 17:53:47 +01002293 CheckDefFailure(['if g:abool', 'elseif xxx'], 'E1001:')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002294 CheckDefFailure(['if true', 'echo 1'], 'E171:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01002295
2296 var lines =<< trim END
2297 var s = ''
2298 if s = ''
2299 endif
2300 END
2301 CheckDefFailure(lines, 'E488:')
2302
2303 lines =<< trim END
2304 var s = ''
2305 if s == ''
2306 elseif s = ''
2307 endif
2308 END
2309 CheckDefFailure(lines, 'E488:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002310enddef
2311
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002312let g:bool_true = v:true
2313let g:bool_false = v:false
2314
2315def Test_if_const_expr()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002316 var res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002317 if true ? true : false
2318 res = true
2319 endif
2320 assert_equal(true, res)
2321
Bram Moolenaar585fea72020-04-02 22:33:21 +02002322 g:glob = 2
2323 if false
Bram Moolenaar67979662020-06-20 22:50:47 +02002324 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002325 endif
2326 assert_equal(2, g:glob)
2327 if true
Bram Moolenaar67979662020-06-20 22:50:47 +02002328 execute('g:glob = 3')
Bram Moolenaar585fea72020-04-02 22:33:21 +02002329 endif
2330 assert_equal(3, g:glob)
2331
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002332 res = false
2333 if g:bool_true ? true : false
2334 res = true
2335 endif
2336 assert_equal(true, res)
2337
2338 res = false
2339 if true ? g:bool_true : false
2340 res = true
2341 endif
2342 assert_equal(true, res)
2343
2344 res = false
2345 if true ? true : g:bool_false
2346 res = true
2347 endif
2348 assert_equal(true, res)
2349
2350 res = false
2351 if true ? false : true
2352 res = true
2353 endif
2354 assert_equal(false, res)
2355
2356 res = false
2357 if false ? false : true
2358 res = true
2359 endif
2360 assert_equal(true, res)
2361
2362 res = false
2363 if false ? true : false
2364 res = true
2365 endif
2366 assert_equal(false, res)
2367
2368 res = false
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002369 if has('xyz') ? true : false
2370 res = true
2371 endif
2372 assert_equal(false, res)
2373
2374 res = false
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002375 if true && true
2376 res = true
2377 endif
2378 assert_equal(true, res)
2379
2380 res = false
2381 if true && false
2382 res = true
2383 endif
2384 assert_equal(false, res)
2385
2386 res = false
2387 if g:bool_true && false
2388 res = true
2389 endif
2390 assert_equal(false, res)
2391
2392 res = false
2393 if true && g:bool_false
2394 res = true
2395 endif
2396 assert_equal(false, res)
2397
2398 res = false
2399 if false && false
2400 res = true
2401 endif
2402 assert_equal(false, res)
2403
2404 res = false
2405 if true || false
2406 res = true
2407 endif
2408 assert_equal(true, res)
2409
2410 res = false
2411 if g:bool_true || false
2412 res = true
2413 endif
2414 assert_equal(true, res)
2415
2416 res = false
2417 if true || g:bool_false
2418 res = true
2419 endif
2420 assert_equal(true, res)
2421
2422 res = false
2423 if false || false
2424 res = true
2425 endif
2426 assert_equal(false, res)
Bram Moolenaar3988f642020-08-27 22:43:03 +02002427
2428 # with constant "false" expression may be invalid so long as the syntax is OK
Bram Moolenaarc3235272021-07-10 19:42:03 +02002429 if false | eval 1 + 2 | endif
Bram Moolenaar3988f642020-08-27 22:43:03 +02002430 if false | eval burp + 234 | endif
2431 if false | echo burp 234 'asd' | endif
2432 if false
2433 burp
2434 endif
Bram Moolenaare525bdd2021-08-07 18:12:40 +02002435
2436 # expression with line breaks skipped
2437 if false
2438 ('aaa'
2439 .. 'bbb'
2440 .. 'ccc'
2441 )->setline(1)
2442 endif
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002443enddef
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002444
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02002445def Test_if_const_expr_fails()
Bram Moolenaard2c61702020-09-06 15:58:36 +02002446 CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
2447 CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
2448 CheckDefFailure(["if has('aaa'"], 'E110:')
2449 CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
Bram Moolenaar6d69bf62020-03-03 19:02:12 +01002450enddef
2451
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002452def RunNested(i: number): number
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002453 var x: number = 0
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002454 if i % 2
2455 if 1
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002456 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002457 else
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002458 # comment
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002459 endif
2460 x += 1
2461 else
2462 x += 1000
2463 endif
2464 return x
2465enddef
2466
2467def Test_nested_if()
2468 assert_equal(1, RunNested(1))
2469 assert_equal(1000, RunNested(2))
2470enddef
2471
Bram Moolenaarad39c092020-02-26 18:23:43 +01002472def Test_execute_cmd()
Bram Moolenaare4984292020-12-13 14:19:25 +01002473 # missing argument is ignored
2474 execute
2475 execute # comment
2476
Bram Moolenaarad39c092020-02-26 18:23:43 +01002477 new
2478 setline(1, 'default')
Bram Moolenaard2c61702020-09-06 15:58:36 +02002479 execute 'setline(1, "execute-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002480 assert_equal('execute-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002481
Bram Moolenaard2c61702020-09-06 15:58:36 +02002482 execute "setline(1, 'execute-string')"
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002483 assert_equal('execute-string', getline(1))
2484
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002485 var cmd1 = 'setline(1,'
2486 var cmd2 = '"execute-var")'
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002487 execute cmd1 cmd2 # comment
Bram Moolenaarad39c092020-02-26 18:23:43 +01002488 assert_equal('execute-var', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002489
Bram Moolenaard2c61702020-09-06 15:58:36 +02002490 execute cmd1 cmd2 '|setline(1, "execute-var-string")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002491 assert_equal('execute-var-string', getline(1))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002492
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002493 var cmd_first = 'call '
2494 var cmd_last = 'setline(1, "execute-var-var")'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002495 execute cmd_first .. cmd_last
2496 assert_equal('execute-var-var', getline(1))
2497 bwipe!
Bram Moolenaar585fea72020-04-02 22:33:21 +02002498
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002499 var n = true
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002500 execute 'echomsg' (n ? '"true"' : '"no"')
2501 assert_match('^true$', Screenline(&lines))
2502
Bram Moolenaare0de1712020-12-02 17:36:54 +01002503 echomsg [1, 2, 3] {a: 1, b: 2}
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002504 assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
2505
Bram Moolenaard2c61702020-09-06 15:58:36 +02002506 CheckDefFailure(['execute xxx'], 'E1001:', 1)
2507 CheckDefExecFailure(['execute "tabnext " .. 8'], 'E475:', 1)
2508 CheckDefFailure(['execute "cmd"# comment'], 'E488:', 1)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002509enddef
2510
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002511def Test_execute_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002512 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002513 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002514 vim9script
2515 execute 'g:someVar'
2516 .. ' = ' ..
2517 '28'
2518 assert_equal(28, g:someVar)
2519 unlet g:someVar
2520 END
2521 CheckScriptSuccess(lines)
2522enddef
2523
Bram Moolenaarad39c092020-02-26 18:23:43 +01002524def Test_echo_cmd()
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002525 echo 'some' # comment
Bram Moolenaar585fea72020-04-02 22:33:21 +02002526 echon 'thing'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002527 assert_match('^something$', Screenline(&lines))
2528
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002529 echo "some" # comment
2530 echon "thing"
2531 assert_match('^something$', Screenline(&lines))
2532
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002533 var str1 = 'some'
2534 var str2 = 'more'
Bram Moolenaarad39c092020-02-26 18:23:43 +01002535 echo str1 str2
2536 assert_match('^some more$', Screenline(&lines))
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002537
Bram Moolenaard2c61702020-09-06 15:58:36 +02002538 CheckDefFailure(['echo "xxx"# comment'], 'E488:')
Bram Moolenaarad39c092020-02-26 18:23:43 +01002539enddef
2540
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002541def Test_echomsg_cmd()
2542 echomsg 'some' 'more' # comment
2543 assert_match('^some more$', Screenline(&lines))
2544 echo 'clear'
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002545 :1messages
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002546 assert_match('^some more$', Screenline(&lines))
2547
Bram Moolenaard2c61702020-09-06 15:58:36 +02002548 CheckDefFailure(['echomsg "xxx"# comment'], 'E488:')
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002549enddef
2550
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002551def Test_echomsg_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002552 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002553 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002554 vim9script
2555 echomsg 'here'
2556 .. ' is ' ..
2557 'a message'
2558 assert_match('^here is a message$', Screenline(&lines))
2559 END
2560 CheckScriptSuccess(lines)
2561enddef
2562
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002563def Test_echoerr_cmd()
Bram Moolenaar7de62622021-08-07 15:05:47 +02002564 var local = 'local'
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002565 try
Bram Moolenaar7de62622021-08-07 15:05:47 +02002566 echoerr 'something' local 'wrong' # comment
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002567 catch
Bram Moolenaar7de62622021-08-07 15:05:47 +02002568 assert_match('something local wrong', v:exception)
Bram Moolenaar40ee4662020-05-05 22:08:26 +02002569 endtry
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002570enddef
2571
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002572def Test_echoerr_cmd_vimscript()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002573 # only checks line continuation
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002574 var lines =<< trim END
Bram Moolenaar47e880d2020-06-30 22:02:02 +02002575 vim9script
2576 try
2577 echoerr 'this'
2578 .. ' is ' ..
2579 'wrong'
2580 catch
2581 assert_match('this is wrong', v:exception)
2582 endtry
2583 END
2584 CheckScriptSuccess(lines)
2585enddef
2586
Bram Moolenaar7de62622021-08-07 15:05:47 +02002587def Test_echoconsole_cmd()
2588 var local = 'local'
2589 echoconsole 'something' local # comment
2590 # output goes anywhere
2591enddef
2592
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002593def Test_for_outside_of_function()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02002594 var lines =<< trim END
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002595 vim9script
2596 new
2597 for var in range(0, 3)
2598 append(line('$'), var)
2599 endfor
2600 assert_equal(['', '0', '1', '2', '3'], getline(1, '$'))
2601 bwipe!
Bram Moolenaar522eefd2021-03-26 18:49:22 +01002602
2603 var result = ''
2604 for i in [1, 2, 3]
2605 var loop = ' loop ' .. i
2606 result ..= loop
2607 endfor
2608 assert_equal(' loop 1 loop 2 loop 3', result)
Bram Moolenaar41fe0612020-03-01 16:22:40 +01002609 END
2610 writefile(lines, 'Xvim9for.vim')
2611 source Xvim9for.vim
2612 delete('Xvim9for.vim')
2613enddef
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614
rbtnnbebf0692021-08-21 17:26:50 +02002615def Test_for_skipped_block()
2616 # test skipped blocks at outside of function
2617 var lines =<< trim END
2618 var result = []
2619 if true
2620 for n in [1, 2]
2621 result += [n]
2622 endfor
2623 else
2624 for n in [3, 4]
2625 result += [n]
2626 endfor
2627 endif
2628 assert_equal([1, 2], result)
2629
2630 result = []
2631 if false
2632 for n in [1, 2]
2633 result += [n]
2634 endfor
2635 else
2636 for n in [3, 4]
2637 result += [n]
2638 endfor
2639 endif
2640 assert_equal([3, 4], result)
2641 END
2642 CheckDefAndScriptSuccess(lines)
2643
2644 # test skipped blocks at inside of function
2645 lines =<< trim END
2646 def DefTrue()
2647 var result = []
2648 if true
2649 for n in [1, 2]
2650 result += [n]
2651 endfor
2652 else
2653 for n in [3, 4]
2654 result += [n]
2655 endfor
2656 endif
2657 assert_equal([1, 2], result)
2658 enddef
2659 DefTrue()
2660
2661 def DefFalse()
2662 var result = []
2663 if false
2664 for n in [1, 2]
2665 result += [n]
2666 endfor
2667 else
2668 for n in [3, 4]
2669 result += [n]
2670 endfor
2671 endif
2672 assert_equal([3, 4], result)
2673 enddef
2674 DefFalse()
2675 END
2676 CheckDefAndScriptSuccess(lines)
2677enddef
2678
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002679def Test_for_loop()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002680 var lines =<< trim END
2681 var result = ''
2682 for cnt in range(7)
2683 if cnt == 4
2684 break
2685 endif
2686 if cnt == 2
2687 continue
2688 endif
2689 result ..= cnt .. '_'
2690 endfor
2691 assert_equal('0_1_3_', result)
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02002692
Bram Moolenaarf2253962021-04-13 20:53:13 +02002693 var concat = ''
2694 for str in eval('["one", "two"]')
2695 concat ..= str
2696 endfor
2697 assert_equal('onetwo', concat)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002698
Bram Moolenaarf2253962021-04-13 20:53:13 +02002699 var total = 0
2700 for nr in
2701 [1, 2, 3]
2702 total += nr
2703 endfor
2704 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002705
Bram Moolenaarf2253962021-04-13 20:53:13 +02002706 total = 0
2707 for nr
2708 in [1, 2, 3]
2709 total += nr
2710 endfor
2711 assert_equal(6, total)
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01002712
Bram Moolenaarf2253962021-04-13 20:53:13 +02002713 total = 0
2714 for nr
2715 in
2716 [1, 2, 3]
2717 total += nr
2718 endfor
2719 assert_equal(6, total)
Bram Moolenaar036d0712021-01-17 20:23:38 +01002720
Bram Moolenaara3589a02021-04-14 13:30:46 +02002721 # with type
2722 total = 0
2723 for n: number in [1, 2, 3]
2724 total += n
2725 endfor
2726 assert_equal(6, total)
2727
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002728 var chars = ''
2729 for s: string in 'foobar'
2730 chars ..= s
2731 endfor
2732 assert_equal('foobar', chars)
2733
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02002734 chars = ''
2735 for x: string in {a: 'a', b: 'b'}->values()
2736 chars ..= x
2737 endfor
2738 assert_equal('ab', chars)
2739
Bram Moolenaara3589a02021-04-14 13:30:46 +02002740 # unpack with type
Bram Moolenaarf2253962021-04-13 20:53:13 +02002741 var res = ''
Bram Moolenaara3589a02021-04-14 13:30:46 +02002742 for [n: number, s: string] in [[1, 'a'], [2, 'b']]
2743 res ..= n .. s
2744 endfor
2745 assert_equal('1a2b', res)
2746
Bram Moolenaar444d8782021-06-26 12:40:56 +02002747 # unpack with one var
2748 var reslist = []
2749 for [x] in [['aaa'], ['bbb']]
2750 reslist->add(x)
2751 endfor
2752 assert_equal(['aaa', 'bbb'], reslist)
2753
Bram Moolenaara3589a02021-04-14 13:30:46 +02002754 # loop over string
2755 res = ''
Bram Moolenaarf2253962021-04-13 20:53:13 +02002756 for c in 'aéc̀d'
2757 res ..= c .. '-'
2758 endfor
2759 assert_equal('a-é-c̀-d-', res)
2760
2761 res = ''
2762 for c in ''
2763 res ..= c .. '-'
2764 endfor
2765 assert_equal('', res)
2766
2767 res = ''
2768 for c in test_null_string()
2769 res ..= c .. '-'
2770 endfor
2771 assert_equal('', res)
2772
2773 var foo: list<dict<any>> = [
2774 {a: 'Cat'}
2775 ]
2776 for dd in foo
2777 dd.counter = 12
2778 endfor
2779 assert_equal([{a: 'Cat', counter: 12}], foo)
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002780
2781 reslist = []
2782 for _ in range(3)
2783 reslist->add('x')
2784 endfor
2785 assert_equal(['x', 'x', 'x'], reslist)
Bram Moolenaarf2253962021-04-13 20:53:13 +02002786 END
2787 CheckDefAndScriptSuccess(lines)
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002788enddef
2789
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02002790def Test_for_loop_with_closure()
2791 var lines =<< trim END
2792 var flist: list<func>
2793 for i in range(5)
2794 var inloop = i
2795 flist[i] = () => inloop
2796 endfor
2797 for i in range(5)
2798 assert_equal(4, flist[i]())
2799 endfor
2800 END
2801 CheckDefAndScriptSuccess(lines)
2802
2803 lines =<< trim END
2804 var flist: list<func>
2805 for i in range(5)
2806 var inloop = i
2807 flist[i] = () => {
2808 return inloop
2809 }
2810 endfor
2811 for i in range(5)
2812 assert_equal(4, flist[i]())
2813 endfor
2814 END
2815 CheckDefAndScriptSuccess(lines)
2816enddef
2817
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002818def Test_for_loop_fails()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002819 CheckDefAndScriptFailure2(['for '], 'E1097:', 'E690:')
2820 CheckDefAndScriptFailure2(['for x'], 'E1097:', 'E690:')
2821 CheckDefAndScriptFailure2(['for x in'], 'E1097:', 'E15:')
2822 CheckDefAndScriptFailure(['for # in range(5)'], 'E690:')
2823 CheckDefAndScriptFailure(['for i In range(5)'], 'E690:')
2824 CheckDefAndScriptFailure2(['var x = 5', 'for x in range(5)', 'endfor'], 'E1017:', 'E1041:')
Bram Moolenaard4ab8072021-07-08 19:22:12 +02002825 CheckScriptFailure(['vim9script', 'var x = 5', 'for x in range(5)', '# comment', 'endfor'], 'E1041:', 3)
Bram Moolenaar822ba242020-05-24 23:00:18 +02002826 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01002827 delfunc! g:Func
Bram Moolenaar675f7162020-04-12 22:53:54 +02002828 CheckDefFailure(['for i in xxx'], 'E1001:')
2829 CheckDefFailure(['endfor'], 'E588:')
2830 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002831
2832 # wrong type detected at compile time
2833 CheckDefFailure(['for i in {a: 1}', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2834
2835 # wrong type detected at runtime
2836 g:adict = {a: 1}
2837 CheckDefExecFailure(['for i in g:adict', 'echo 3', 'endfor'], 'E1177: For loop on dict not supported')
2838 unlet g:adict
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002839
2840 var lines =<< trim END
2841 var d: list<dict<any>> = [{a: 0}]
2842 for e in d
2843 e = {a: 0, b: ''}
2844 endfor
2845 END
2846 CheckDefAndScriptFailure2(lines, 'E1018:', 'E46:', 3)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02002847
2848 lines =<< trim END
2849 for nr: number in ['foo']
2850 endfor
2851 END
2852 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
Bram Moolenaar404557e2021-07-05 21:41:48 +02002853
2854 lines =<< trim END
2855 for n : number in [1, 2]
2856 echo n
2857 endfor
2858 END
2859 CheckDefAndScriptFailure(lines, 'E1059:', 1)
Bram Moolenaarefc5db52021-07-07 21:21:30 +02002860
2861 lines =<< trim END
2862 var d: dict<number> = {a: 1, b: 2}
2863 for [k: job, v: job] in d->items()
2864 echo k v
2865 endfor
2866 END
2867 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected job but got string', 2)
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002868
2869 lines =<< trim END
2870 var i = 0
2871 for i in [1, 2, 3]
2872 echo i
2873 endfor
2874 END
2875 CheckDefExecAndScriptFailure2(lines, 'E1017:', 'E1041:')
2876
2877 lines =<< trim END
2878 var l = [0]
2879 for l[0] in [1, 2, 3]
2880 echo l[0]
2881 endfor
2882 END
2883 CheckDefExecAndScriptFailure2(lines, 'E461:', 'E1017:')
2884
2885 lines =<< trim END
2886 var d = {x: 0}
2887 for d.x in [1, 2, 3]
2888 echo d.x
2889 endfor
2890 END
2891 CheckDefExecAndScriptFailure2(lines, 'E461:', 'E1017:')
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002892enddef
2893
Bram Moolenaarea870692020-12-02 14:24:30 +01002894def Test_for_loop_script_var()
2895 # cannot use s:var in a :def function
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00002896 CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E461:')
Bram Moolenaarea870692020-12-02 14:24:30 +01002897
2898 # can use s:var in Vim9 script, with or without s:
2899 var lines =<< trim END
2900 vim9script
2901 var total = 0
2902 for s:var in [1, 2, 3]
2903 total += s:var
2904 endfor
2905 assert_equal(6, total)
2906
2907 total = 0
2908 for var in [1, 2, 3]
2909 total += var
2910 endfor
2911 assert_equal(6, total)
2912 END
2913enddef
2914
Bram Moolenaar792f7862020-11-23 08:31:18 +01002915def Test_for_loop_unpack()
Bram Moolenaar792f7862020-11-23 08:31:18 +01002916 var lines =<< trim END
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002917 var result = []
2918 for [v1, v2] in [[1, 2], [3, 4]]
2919 result->add(v1)
2920 result->add(v2)
2921 endfor
2922 assert_equal([1, 2, 3, 4], result)
2923
2924 result = []
2925 for [v1, v2; v3] in [[1, 2], [3, 4, 5, 6]]
2926 result->add(v1)
2927 result->add(v2)
2928 result->add(v3)
2929 endfor
2930 assert_equal([1, 2, [], 3, 4, [5, 6]], result)
2931
2932 result = []
2933 for [&ts, &sw] in [[1, 2], [3, 4]]
2934 result->add(&ts)
2935 result->add(&sw)
2936 endfor
2937 assert_equal([1, 2, 3, 4], result)
2938
2939 var slist: list<string>
2940 for [$LOOPVAR, @r, v:errmsg] in [['a', 'b', 'c'], ['d', 'e', 'f']]
2941 slist->add($LOOPVAR)
2942 slist->add(@r)
2943 slist->add(v:errmsg)
2944 endfor
2945 assert_equal(['a', 'b', 'c', 'd', 'e', 'f'], slist)
2946
2947 slist = []
2948 for [g:globalvar, b:bufvar, w:winvar, t:tabvar] in [['global', 'buf', 'win', 'tab'], ['1', '2', '3', '4']]
2949 slist->add(g:globalvar)
2950 slist->add(b:bufvar)
2951 slist->add(w:winvar)
2952 slist->add(t:tabvar)
2953 endfor
2954 assert_equal(['global', 'buf', 'win', 'tab', '1', '2', '3', '4'], slist)
Bram Moolenaarf6c177a2020-12-04 17:38:00 +01002955 unlet! g:globalvar b:bufvar w:winvar t:tabvar
Bram Moolenaarb777da92021-05-22 21:40:39 +02002956
2957 var res = []
2958 for [_, n, _] in [[1, 2, 3], [4, 5, 6]]
2959 res->add(n)
2960 endfor
2961 assert_equal([2, 5], res)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002962 END
2963 CheckDefAndScriptSuccess(lines)
2964
2965 lines =<< trim END
Bram Moolenaar792f7862020-11-23 08:31:18 +01002966 for [v1, v2] in [[1, 2, 3], [3, 4]]
2967 echo v1 v2
2968 endfor
2969 END
2970 CheckDefExecFailure(lines, 'E710:', 1)
2971
2972 lines =<< trim END
2973 for [v1, v2] in [[1], [3, 4]]
2974 echo v1 v2
2975 endfor
2976 END
2977 CheckDefExecFailure(lines, 'E711:', 1)
2978
2979 lines =<< trim END
2980 for [v1, v1] in [[1, 2], [3, 4]]
2981 echo v1
2982 endfor
2983 END
2984 CheckDefExecFailure(lines, 'E1017:', 1)
2985enddef
2986
Bram Moolenaarc150c092021-02-13 15:02:46 +01002987def Test_for_loop_with_try_continue()
Bram Moolenaarf2253962021-04-13 20:53:13 +02002988 var lines =<< trim END
2989 var looped = 0
2990 var cleanup = 0
2991 for i in range(3)
2992 looped += 1
2993 try
2994 eval [][0]
2995 catch
2996 continue
2997 finally
2998 cleanup += 1
2999 endtry
3000 endfor
3001 assert_equal(3, looped)
3002 assert_equal(3, cleanup)
3003 END
3004 CheckDefAndScriptSuccess(lines)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003005enddef
3006
rbtnnd895b1d2021-08-20 20:54:25 +02003007def Test_while_skipped_block()
3008 # test skipped blocks at outside of function
3009 var lines =<< trim END
3010 var result = []
3011 var n = 0
3012 if true
3013 n = 1
3014 while n < 3
3015 result += [n]
3016 n += 1
3017 endwhile
3018 else
3019 n = 3
3020 while n < 5
3021 result += [n]
3022 n += 1
3023 endwhile
3024 endif
3025 assert_equal([1, 2], result)
3026
3027 result = []
3028 if false
3029 n = 1
3030 while n < 3
3031 result += [n]
3032 n += 1
3033 endwhile
3034 else
3035 n = 3
3036 while n < 5
3037 result += [n]
3038 n += 1
3039 endwhile
3040 endif
3041 assert_equal([3, 4], result)
3042 END
3043 CheckDefAndScriptSuccess(lines)
3044
3045 # test skipped blocks at inside of function
3046 lines =<< trim END
3047 def DefTrue()
3048 var result = []
3049 var n = 0
3050 if true
3051 n = 1
3052 while n < 3
3053 result += [n]
3054 n += 1
3055 endwhile
3056 else
3057 n = 3
3058 while n < 5
3059 result += [n]
3060 n += 1
3061 endwhile
3062 endif
3063 assert_equal([1, 2], result)
3064 enddef
3065 DefTrue()
3066
3067 def DefFalse()
3068 var result = []
3069 var n = 0
3070 if false
3071 n = 1
3072 while n < 3
3073 result += [n]
3074 n += 1
3075 endwhile
3076 else
3077 n = 3
3078 while n < 5
3079 result += [n]
3080 n += 1
3081 endwhile
3082 endif
3083 assert_equal([3, 4], result)
3084 enddef
3085 DefFalse()
3086 END
3087 CheckDefAndScriptSuccess(lines)
3088enddef
3089
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003090def Test_while_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003091 var result = ''
3092 var cnt = 0
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003093 while cnt < 555
3094 if cnt == 3
3095 break
3096 endif
3097 cnt += 1
3098 if cnt == 2
3099 continue
3100 endif
3101 result ..= cnt .. '_'
3102 endwhile
3103 assert_equal('1_3_', result)
Bram Moolenaardee37dc2021-02-07 16:40:05 +01003104
3105 var s = ''
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01003106 while s == 'x' # {comment}
Bram Moolenaardee37dc2021-02-07 16:40:05 +01003107 endwhile
Bram Moolenaard0df1aa2020-03-04 21:50:46 +01003108enddef
3109
Bram Moolenaar7a53f292021-11-22 18:31:02 +00003110def Test_while_loop_in_script()
3111 var lines =<< trim END
3112 vim9script
3113 var result = ''
3114 var cnt = 0
3115 while cnt < 3
3116 var s = 'v' .. cnt
3117 result ..= s
3118 cnt += 1
3119 endwhile
3120 assert_equal('v0v1v2', result)
3121 END
3122 CheckScriptSuccess(lines)
3123enddef
3124
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003125def Test_while_loop_fails()
Bram Moolenaar675f7162020-04-12 22:53:54 +02003126 CheckDefFailure(['while xxx'], 'E1001:')
3127 CheckDefFailure(['endwhile'], 'E588:')
3128 CheckDefFailure(['continue'], 'E586:')
3129 CheckDefFailure(['if true', 'continue'], 'E586:')
3130 CheckDefFailure(['break'], 'E587:')
3131 CheckDefFailure(['if true', 'break'], 'E587:')
3132 CheckDefFailure(['while 1', 'echo 3'], 'E170:')
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01003133
3134 var lines =<< trim END
3135 var s = ''
3136 while s = ''
3137 endwhile
3138 END
3139 CheckDefFailure(lines, 'E488:')
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003140enddef
3141
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003142def Test_interrupt_loop()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003143 var caught = false
3144 var x = 0
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003145 try
3146 while 1
3147 x += 1
3148 if x == 100
3149 feedkeys("\<C-C>", 'Lt')
3150 endif
3151 endwhile
3152 catch
3153 caught = true
3154 assert_equal(100, x)
3155 endtry
3156 assert_true(caught, 'should have caught an exception')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003157 # consume the CTRL-C
3158 getchar(0)
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003159enddef
Bram Moolenaar20431c92020-03-20 18:39:46 +01003160
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003161def Test_automatic_line_continuation()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003162 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003163 'one',
3164 'two',
3165 'three',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003166 ] # comment
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003167 assert_equal(['one', 'two', 'three'], mylist)
3168
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003169 var mydict = {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003170 ['one']: 1,
3171 ['two']: 2,
3172 ['three']:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003173 3,
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003174 } # comment
Bram Moolenaare0de1712020-12-02 17:36:54 +01003175 assert_equal({one: 1, two: 2, three: 3}, mydict)
3176 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003177 one: 1, # comment
3178 two: # comment
3179 2, # comment
3180 three: 3 # comment
3181 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003182 assert_equal({one: 1, two: 2, three: 3}, mydict)
3183 mydict = {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003184 one: 1,
3185 two:
3186 2,
3187 three: 3
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003188 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003189 assert_equal({one: 1, two: 2, three: 3}, mydict)
Bram Moolenaare6085c52020-04-12 20:19:16 +02003190
3191 assert_equal(
3192 ['one', 'two', 'three'],
3193 split('one two three')
3194 )
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003195enddef
3196
Bram Moolenaar7a092242020-04-16 22:10:49 +02003197def Test_vim9_comment()
3198 CheckScriptSuccess([
3199 'vim9script',
3200 '# something',
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01003201 '#something',
3202 '#{something',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003203 ])
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01003204
3205 split Xfile
3206 CheckScriptSuccess([
3207 'vim9script',
3208 'edit #something',
3209 ])
3210 CheckScriptSuccess([
3211 'vim9script',
3212 'edit #{something',
3213 ])
3214 close
3215
Bram Moolenaar7a092242020-04-16 22:10:49 +02003216 CheckScriptFailure([
3217 'vim9script',
3218 ':# something',
3219 ], 'E488:')
3220 CheckScriptFailure([
3221 '# something',
3222 ], 'E488:')
3223 CheckScriptFailure([
3224 ':# something',
3225 ], 'E488:')
3226
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003227 { # block start
3228 } # block end
3229 CheckDefFailure([
3230 '{# comment',
3231 ], 'E488:')
3232 CheckDefFailure([
3233 '{',
3234 '}# comment',
3235 ], 'E488:')
3236
3237 echo "yes" # comment
3238 CheckDefFailure([
3239 'echo "yes"# comment',
3240 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003241 CheckScriptSuccess([
3242 'vim9script',
3243 'echo "yes" # something',
3244 ])
3245 CheckScriptFailure([
3246 'vim9script',
3247 'echo "yes"# something',
3248 ], 'E121:')
3249 CheckScriptFailure([
3250 'vim9script',
3251 'echo# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003252 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003253 CheckScriptFailure([
3254 'echo "yes" # something',
3255 ], 'E121:')
3256
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003257 exe "echo" # comment
3258 CheckDefFailure([
3259 'exe "echo"# comment',
3260 ], 'E488:')
3261 CheckScriptSuccess([
3262 'vim9script',
3263 'exe "echo" # something',
3264 ])
3265 CheckScriptFailure([
3266 'vim9script',
3267 'exe "echo"# something',
3268 ], 'E121:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003269 CheckScriptFailure([
3270 'vim9script',
3271 'exe# something',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003272 ], 'E1144:')
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02003273 CheckScriptFailure([
3274 'exe "echo" # something',
3275 ], 'E121:')
3276
Bram Moolenaar7a092242020-04-16 22:10:49 +02003277 CheckDefFailure([
3278 'try# comment',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003279 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003280 'catch',
3281 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003282 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003283 CheckScriptFailure([
3284 'vim9script',
3285 'try# comment',
3286 'echo "yes"',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003287 ], 'E1144:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003288 CheckDefFailure([
3289 'try',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003290 ' throw#comment',
3291 'catch',
3292 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003293 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003294 CheckDefFailure([
3295 'try',
3296 ' throw "yes"#comment',
3297 'catch',
3298 'endtry',
3299 ], 'E488:')
3300 CheckDefFailure([
3301 'try',
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003302 ' echo "yes"',
Bram Moolenaar7a092242020-04-16 22:10:49 +02003303 'catch# comment',
3304 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003305 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003306 CheckScriptFailure([
3307 'vim9script',
3308 'try',
3309 ' echo "yes"',
3310 'catch# comment',
3311 'endtry',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003312 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003313 CheckDefFailure([
3314 'try',
3315 ' echo "yes"',
3316 'catch /pat/# comment',
3317 'endtry',
3318 ], 'E488:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003319 CheckDefFailure([
3320 'try',
3321 'echo "yes"',
3322 'catch',
3323 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003324 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003325 CheckScriptFailure([
3326 'vim9script',
3327 'try',
3328 ' echo "yes"',
3329 'catch',
3330 'endtry# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003331 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003332
3333 CheckScriptSuccess([
3334 'vim9script',
3335 'hi # comment',
3336 ])
3337 CheckScriptFailure([
3338 'vim9script',
3339 'hi# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003340 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003341 CheckScriptSuccess([
3342 'vim9script',
3343 'hi Search # comment',
3344 ])
3345 CheckScriptFailure([
3346 'vim9script',
3347 'hi Search# comment',
3348 ], 'E416:')
3349 CheckScriptSuccess([
3350 'vim9script',
3351 'hi link This Search # comment',
3352 ])
3353 CheckScriptFailure([
3354 'vim9script',
3355 'hi link This That# comment',
3356 ], 'E413:')
3357 CheckScriptSuccess([
3358 'vim9script',
3359 'hi clear This # comment',
3360 'hi clear # comment',
3361 ])
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003362 # not tested, because it doesn't give an error but a warning:
3363 # hi clear This# comment',
Bram Moolenaar1966c242020-04-20 22:42:32 +02003364 CheckScriptFailure([
3365 'vim9script',
3366 'hi clear# comment',
3367 ], 'E416:')
3368
3369 CheckScriptSuccess([
3370 'vim9script',
3371 'hi Group term=bold',
3372 'match Group /todo/ # comment',
3373 ])
3374 CheckScriptFailure([
3375 'vim9script',
3376 'hi Group term=bold',
3377 'match Group /todo/# comment',
3378 ], 'E488:')
3379 CheckScriptSuccess([
3380 'vim9script',
3381 'match # comment',
3382 ])
3383 CheckScriptFailure([
3384 'vim9script',
3385 'match# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003386 ], 'E1144:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003387 CheckScriptSuccess([
3388 'vim9script',
3389 'match none # comment',
3390 ])
3391 CheckScriptFailure([
3392 'vim9script',
3393 'match none# comment',
3394 ], 'E475:')
3395
3396 CheckScriptSuccess([
3397 'vim9script',
3398 'menutrans clear # comment',
3399 ])
3400 CheckScriptFailure([
3401 'vim9script',
3402 'menutrans clear# comment text',
3403 ], 'E474:')
3404
3405 CheckScriptSuccess([
3406 'vim9script',
3407 'syntax clear # comment',
3408 ])
3409 CheckScriptFailure([
3410 'vim9script',
3411 'syntax clear# comment text',
3412 ], 'E28:')
3413 CheckScriptSuccess([
3414 'vim9script',
3415 'syntax keyword Word some',
3416 'syntax clear Word # comment',
3417 ])
3418 CheckScriptFailure([
3419 'vim9script',
3420 'syntax keyword Word some',
3421 'syntax clear Word# comment text',
3422 ], 'E28:')
3423
3424 CheckScriptSuccess([
3425 'vim9script',
3426 'syntax list # comment',
3427 ])
3428 CheckScriptFailure([
3429 'vim9script',
3430 'syntax list# comment text',
3431 ], 'E28:')
3432
3433 CheckScriptSuccess([
3434 'vim9script',
3435 'syntax match Word /pat/ oneline # comment',
3436 ])
3437 CheckScriptFailure([
3438 'vim9script',
3439 'syntax match Word /pat/ oneline# comment',
3440 ], 'E475:')
3441
3442 CheckScriptSuccess([
3443 'vim9script',
3444 'syntax keyword Word word # comm[ent',
3445 ])
3446 CheckScriptFailure([
3447 'vim9script',
3448 'syntax keyword Word word# comm[ent',
3449 ], 'E789:')
3450
3451 CheckScriptSuccess([
3452 'vim9script',
3453 'syntax match Word /pat/ # comment',
3454 ])
3455 CheckScriptFailure([
3456 'vim9script',
3457 'syntax match Word /pat/# comment',
3458 ], 'E402:')
3459
3460 CheckScriptSuccess([
3461 'vim9script',
3462 'syntax match Word /pat/ contains=Something # comment',
3463 ])
3464 CheckScriptFailure([
3465 'vim9script',
3466 'syntax match Word /pat/ contains=Something# comment',
3467 ], 'E475:')
3468 CheckScriptFailure([
3469 'vim9script',
3470 'syntax match Word /pat/ contains= # comment',
3471 ], 'E406:')
3472 CheckScriptFailure([
3473 'vim9script',
3474 'syntax match Word /pat/ contains=# comment',
3475 ], 'E475:')
3476
3477 CheckScriptSuccess([
3478 'vim9script',
3479 'syntax region Word start=/pat/ end=/pat/ # comment',
3480 ])
3481 CheckScriptFailure([
3482 'vim9script',
3483 'syntax region Word start=/pat/ end=/pat/# comment',
Bram Moolenaard032f342020-07-18 18:13:02 +02003484 ], 'E402:')
Bram Moolenaar1966c242020-04-20 22:42:32 +02003485
3486 CheckScriptSuccess([
3487 'vim9script',
3488 'syntax sync # comment',
3489 ])
3490 CheckScriptFailure([
3491 'vim9script',
3492 'syntax sync# comment',
3493 ], 'E404:')
3494 CheckScriptSuccess([
3495 'vim9script',
3496 'syntax sync ccomment # comment',
3497 ])
3498 CheckScriptFailure([
3499 'vim9script',
3500 'syntax sync ccomment# comment',
3501 ], 'E404:')
3502
3503 CheckScriptSuccess([
3504 'vim9script',
3505 'syntax cluster Some contains=Word # comment',
3506 ])
3507 CheckScriptFailure([
3508 'vim9script',
3509 'syntax cluster Some contains=Word# comment',
3510 ], 'E475:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003511
3512 CheckScriptSuccess([
3513 'vim9script',
3514 'command Echo echo # comment',
3515 'command Echo # comment',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003516 'delcommand Echo',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003517 ])
3518 CheckScriptFailure([
3519 'vim9script',
3520 'command Echo echo# comment',
3521 'Echo',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003522 ], 'E1144:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003523 delcommand Echo
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003524
3525 var curdir = getcwd()
3526 CheckScriptSuccess([
3527 'command Echo cd " comment',
3528 'Echo',
3529 'delcommand Echo',
3530 ])
3531 CheckScriptSuccess([
Bram Moolenaar090728a2020-12-20 15:43:31 +01003532 'vim9script',
Bram Moolenaar70249ee2020-12-10 21:01:30 +01003533 'command Echo cd # comment',
3534 'Echo',
3535 'delcommand Echo',
3536 ])
3537 CheckScriptFailure([
3538 'vim9script',
3539 'command Echo cd " comment',
3540 'Echo',
3541 ], 'E344:')
3542 delcommand Echo
3543 chdir(curdir)
3544
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003545 CheckScriptFailure([
3546 'vim9script',
3547 'command Echo# comment',
3548 ], 'E182:')
3549 CheckScriptFailure([
3550 'vim9script',
3551 'command Echo echo',
3552 'command Echo# comment',
3553 ], 'E182:')
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003554 delcommand Echo
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003555
3556 CheckScriptSuccess([
3557 'vim9script',
3558 'function # comment',
3559 ])
3560 CheckScriptFailure([
3561 'vim9script',
Bram Moolenaar98981072020-07-29 14:40:25 +02003562 'function " comment',
3563 ], 'E129:')
3564 CheckScriptFailure([
3565 'vim9script',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003566 'function# comment',
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003567 ], 'E1144:')
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003568 CheckScriptSuccess([
3569 'vim9script',
3570 'function CheckScriptSuccess # comment',
3571 ])
3572 CheckScriptFailure([
3573 'vim9script',
3574 'function CheckScriptSuccess# comment',
3575 ], 'E488:')
3576
3577 CheckScriptSuccess([
3578 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003579 'func g:DeleteMeA()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003580 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003581 'delfunction g:DeleteMeA # comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003582 ])
3583 CheckScriptFailure([
3584 'vim9script',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003585 'func g:DeleteMeB()',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003586 'endfunc',
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003587 'delfunction g:DeleteMeB# comment',
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003588 ], 'E488:')
3589
3590 CheckScriptSuccess([
3591 'vim9script',
3592 'call execute("ls") # comment',
3593 ])
3594 CheckScriptFailure([
3595 'vim9script',
3596 'call execute("ls")# comment',
3597 ], 'E488:')
Bram Moolenaare7e48382020-07-22 18:17:08 +02003598
3599 CheckScriptFailure([
3600 'def Test() " comment',
3601 'enddef',
3602 ], 'E488:')
3603 CheckScriptFailure([
3604 'vim9script',
3605 'def Test() " comment',
3606 'enddef',
3607 ], 'E488:')
3608
3609 CheckScriptSuccess([
3610 'func Test() " comment',
3611 'endfunc',
Bram Moolenaar2d870f82020-12-05 13:41:01 +01003612 'delfunc Test',
Bram Moolenaare7e48382020-07-22 18:17:08 +02003613 ])
Bram Moolenaar98981072020-07-29 14:40:25 +02003614 CheckScriptSuccess([
Bram Moolenaare7e48382020-07-22 18:17:08 +02003615 'vim9script',
3616 'func Test() " comment',
3617 'endfunc',
Bram Moolenaar98981072020-07-29 14:40:25 +02003618 ])
Bram Moolenaare7e48382020-07-22 18:17:08 +02003619
3620 CheckScriptSuccess([
3621 'def Test() # comment',
3622 'enddef',
3623 ])
3624 CheckScriptFailure([
3625 'func Test() # comment',
3626 'endfunc',
3627 ], 'E488:')
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003628
3629 var lines =<< trim END
3630 vim9script
3631 syn region Text
3632 \ start='foo'
3633 #\ comment
3634 \ end='bar'
Bram Moolenaar5072b472021-06-03 21:56:10 +02003635 syn region Text start='foo'
3636 #\ comment
3637 \ end='bar'
Bram Moolenaar0f37e352021-06-02 15:28:15 +02003638 END
3639 CheckScriptSuccess(lines)
3640
3641 lines =<< trim END
3642 vim9script
3643 syn region Text
3644 \ start='foo'
3645 "\ comment
3646 \ end='bar'
3647 END
3648 CheckScriptFailure(lines, 'E399:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003649enddef
3650
3651def Test_vim9_comment_gui()
3652 CheckCanRunGui
3653
3654 CheckScriptFailure([
3655 'vim9script',
3656 'gui#comment'
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003657 ], 'E1144:')
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02003658 CheckScriptFailure([
3659 'vim9script',
3660 'gui -f#comment'
Bram Moolenaarb8bd2e62021-08-21 17:13:14 +02003661 ], 'E194:')
Bram Moolenaar7a092242020-04-16 22:10:49 +02003662enddef
3663
Bram Moolenaara26b9702020-04-18 19:53:28 +02003664def Test_vim9_comment_not_compiled()
Bram Moolenaar67979662020-06-20 22:50:47 +02003665 au TabEnter *.vim g:entered = 1
3666 au TabEnter *.x g:entered = 2
Bram Moolenaara26b9702020-04-18 19:53:28 +02003667
3668 edit test.vim
3669 doautocmd TabEnter #comment
3670 assert_equal(1, g:entered)
3671
3672 doautocmd TabEnter f.x
3673 assert_equal(2, g:entered)
3674
3675 g:entered = 0
3676 doautocmd TabEnter f.x #comment
3677 assert_equal(2, g:entered)
3678
3679 assert_fails('doautocmd Syntax#comment', 'E216:')
3680
3681 au! TabEnter
3682 unlet g:entered
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003683
3684 CheckScriptSuccess([
3685 'vim9script',
Bram Moolenaar67979662020-06-20 22:50:47 +02003686 'g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003687 'b:var = 456',
3688 'w:var = 777',
3689 't:var = 888',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003690 'unlet g:var w:var # something',
3691 ])
3692
3693 CheckScriptFailure([
3694 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003695 'let var = 123',
3696 ], 'E1126: Cannot use :let in Vim9 script')
3697
3698 CheckScriptFailure([
3699 'vim9script',
3700 'var g:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003701 ], 'E1016: Cannot declare a global variable:')
3702
3703 CheckScriptFailure([
3704 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003705 'var b:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003706 ], 'E1016: Cannot declare a buffer variable:')
3707
3708 CheckScriptFailure([
3709 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003710 'var w:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003711 ], 'E1016: Cannot declare a window variable:')
3712
3713 CheckScriptFailure([
3714 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003715 'var t:var = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003716 ], 'E1016: Cannot declare a tab variable:')
3717
3718 CheckScriptFailure([
3719 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003720 'var v:version = 123',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003721 ], 'E1016: Cannot declare a v: variable:')
3722
3723 CheckScriptFailure([
3724 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003725 'var $VARIABLE = "text"',
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003726 ], 'E1016: Cannot declare an environment variable:')
Bram Moolenaar67979662020-06-20 22:50:47 +02003727
3728 CheckScriptFailure([
3729 'vim9script',
3730 'g:var = 123',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003731 'unlet g:var# comment1',
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003732 ], 'E108:')
3733
3734 CheckScriptFailure([
3735 'let g:var = 123',
3736 'unlet g:var # something',
3737 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003738
3739 CheckScriptSuccess([
3740 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003741 'if 1 # comment2',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003742 ' echo "yes"',
3743 'elseif 2 #comment',
3744 ' echo "no"',
3745 'endif',
3746 ])
3747
3748 CheckScriptFailure([
3749 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003750 'if 1# comment3',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003751 ' echo "yes"',
3752 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003753 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003754
3755 CheckScriptFailure([
3756 'vim9script',
Bram Moolenaar32e35112020-05-14 22:41:15 +02003757 'if 0 # comment4',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003758 ' echo "yes"',
3759 'elseif 2#comment',
3760 ' echo "no"',
3761 'endif',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003762 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003763
3764 CheckScriptSuccess([
3765 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003766 'var v = 1 # comment5',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003767 ])
3768
3769 CheckScriptFailure([
3770 'vim9script',
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003771 'var v = 1# comment6',
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003772 ], 'E488:')
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003773
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003774 CheckScriptSuccess([
3775 'vim9script',
3776 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003777 'setline(1, ["# define pat", "last"])',
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003778 ':$',
Bram Moolenaarfaac4102020-04-20 17:46:14 +02003779 'dsearch /pat/ #comment',
3780 'bwipe!',
3781 ])
3782
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003783 CheckScriptFailure([
3784 'vim9script',
3785 'new'
Bram Moolenaard2c61702020-09-06 15:58:36 +02003786 'setline(1, ["# define pat", "last"])',
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003787 ':$',
3788 'dsearch /pat/#comment',
3789 'bwipe!',
3790 ], 'E488:')
3791
3792 CheckScriptFailure([
3793 'vim9script',
3794 'func! SomeFunc()',
3795 ], 'E477:')
Bram Moolenaara26b9702020-04-18 19:53:28 +02003796enddef
3797
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003798def Test_finish()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003799 var lines =<< trim END
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003800 vim9script
Bram Moolenaar67979662020-06-20 22:50:47 +02003801 g:res = 'one'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003802 if v:false | finish | endif
Bram Moolenaar67979662020-06-20 22:50:47 +02003803 g:res = 'two'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003804 finish
Bram Moolenaar67979662020-06-20 22:50:47 +02003805 g:res = 'three'
Bram Moolenaar7e5bd912020-05-10 21:20:29 +02003806 END
3807 writefile(lines, 'Xfinished')
3808 source Xfinished
3809 assert_equal('two', g:res)
3810
3811 unlet g:res
3812 delete('Xfinished')
3813enddef
3814
Bram Moolenaara5d00772020-05-14 23:20:55 +02003815def Test_forward_declaration()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003816 var lines =<< trim END
Bram Moolenaara5d00772020-05-14 23:20:55 +02003817 vim9script
Bram Moolenaara5d00772020-05-14 23:20:55 +02003818 def GetValue(): string
3819 return theVal
3820 enddef
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003821 var theVal = 'something'
Bram Moolenaar822ba242020-05-24 23:00:18 +02003822 g:initVal = GetValue()
Bram Moolenaara5d00772020-05-14 23:20:55 +02003823 theVal = 'else'
3824 g:laterVal = GetValue()
3825 END
3826 writefile(lines, 'Xforward')
3827 source Xforward
3828 assert_equal('something', g:initVal)
3829 assert_equal('else', g:laterVal)
3830
3831 unlet g:initVal
3832 unlet g:laterVal
3833 delete('Xforward')
3834enddef
3835
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003836def Test_source_vim9_from_legacy()
Bram Moolenaara6294952020-12-27 13:39:50 +01003837 var vim9_lines =<< trim END
3838 vim9script
3839 var local = 'local'
3840 g:global = 'global'
3841 export var exported = 'exported'
3842 export def GetText(): string
3843 return 'text'
3844 enddef
3845 END
3846 writefile(vim9_lines, 'Xvim9_script.vim')
3847
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003848 var legacy_lines =<< trim END
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003849 source Xvim9_script.vim
3850
3851 call assert_false(exists('local'))
3852 call assert_false(exists('exported'))
3853 call assert_false(exists('s:exported'))
3854 call assert_equal('global', global)
3855 call assert_equal('global', g:global)
3856
3857 " imported variable becomes script-local
3858 import exported from './Xvim9_script.vim'
3859 call assert_equal('exported', s:exported)
3860 call assert_false(exists('exported'))
3861
3862 " imported function becomes script-local
3863 import GetText from './Xvim9_script.vim'
3864 call assert_equal('text', s:GetText())
3865 call assert_false(exists('*GetText'))
3866 END
3867 writefile(legacy_lines, 'Xlegacy_script.vim')
3868
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003869 source Xlegacy_script.vim
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003870 assert_equal('global', g:global)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003871 unlet g:global
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003872
3873 delete('Xlegacy_script.vim')
3874 delete('Xvim9_script.vim')
3875enddef
Bram Moolenaara5d00772020-05-14 23:20:55 +02003876
Bram Moolenaare535db82021-03-31 21:07:24 +02003877def Test_declare_script_in_func()
3878 var lines =<< trim END
3879 vim9script
3880 func Declare()
3881 let s:local = 123
3882 endfunc
3883 Declare()
3884 assert_equal(123, local)
3885
3886 var error: string
3887 try
3888 local = 'asdf'
3889 catch
3890 error = v:exception
3891 endtry
3892 assert_match('E1012: Type mismatch; expected number but got string', error)
3893
3894 lockvar local
3895 try
3896 local = 999
3897 catch
3898 error = v:exception
3899 endtry
3900 assert_match('E741: Value is locked: local', error)
3901 END
3902 CheckScriptSuccess(lines)
3903enddef
3904
3905
Bram Moolenaar7d699702020-08-14 20:52:28 +02003906func Test_vim9script_not_global()
3907 " check that items defined in Vim9 script are script-local, not global
3908 let vim9lines =<< trim END
3909 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003910 var name = 'local'
Bram Moolenaar7d699702020-08-14 20:52:28 +02003911 func TheFunc()
3912 echo 'local'
3913 endfunc
3914 def DefFunc()
3915 echo 'local'
3916 enddef
3917 END
3918 call writefile(vim9lines, 'Xvim9script.vim')
3919 source Xvim9script.vim
3920 try
3921 echo g:var
3922 assert_report('did not fail')
3923 catch /E121:/
3924 " caught
3925 endtry
3926 try
3927 call TheFunc()
3928 assert_report('did not fail')
3929 catch /E117:/
3930 " caught
3931 endtry
3932 try
3933 call DefFunc()
3934 assert_report('did not fail')
3935 catch /E117:/
3936 " caught
3937 endtry
3938
Bram Moolenaar25859dd2020-08-30 12:54:53 +02003939 call delete('Xvim9script.vim')
Bram Moolenaar7d699702020-08-14 20:52:28 +02003940endfunc
3941
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02003942def Test_vim9_copen()
3943 # this was giving an error for setting w:quickfix_title
3944 copen
3945 quit
3946enddef
3947
Bram Moolenaar03290b82020-12-19 16:30:44 +01003948" test using an auto-loaded function and variable
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003949def Test_vim9_autoload()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003950 var lines =<< trim END
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003951 vim9script
Bram Moolenaar03290b82020-12-19 16:30:44 +01003952 def some#gettest(): string
3953 return 'test'
3954 enddef
3955 g:some#name = 'name'
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003956 g:some#dict = {key: 'value'}
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003957
3958 def some#varargs(a1: string, ...l: list<string>): string
3959 return a1 .. l[0] .. l[1]
3960 enddef
Bram Moolenaar03290b82020-12-19 16:30:44 +01003961 END
3962
3963 mkdir('Xdir/autoload', 'p')
3964 writefile(lines, 'Xdir/autoload/some.vim')
3965 var save_rtp = &rtp
3966 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3967
3968 assert_equal('test', g:some#gettest())
3969 assert_equal('name', g:some#name)
Bram Moolenaar227c58a2021-04-28 20:40:44 +02003970 assert_equal('value', g:some#dict.key)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003971 g:some#other = 'other'
3972 assert_equal('other', g:some#other)
3973
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01003974 assert_equal('abc', some#varargs('a', 'b', 'c'))
3975
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003976 # upper case script name works
3977 lines =<< trim END
3978 vim9script
3979 def Other#getOther(): string
3980 return 'other'
3981 enddef
3982 END
3983 writefile(lines, 'Xdir/autoload/Other.vim')
3984 assert_equal('other', g:Other#getOther())
3985
Bram Moolenaar03290b82020-12-19 16:30:44 +01003986 delete('Xdir', 'rf')
3987 &rtp = save_rtp
3988enddef
3989
3990" test using a vim9script that is auto-loaded from an autocmd
3991def Test_vim9_aucmd_autoload()
3992 var lines =<< trim END
3993 vim9script
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003994 def foo#test()
3995 echomsg getreg('"')
3996 enddef
3997 END
3998
3999 mkdir('Xdir/autoload', 'p')
4000 writefile(lines, 'Xdir/autoload/foo.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004001 var save_rtp = &rtp
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004002 exe 'set rtp^=' .. getcwd() .. '/Xdir'
4003 augroup test
4004 autocmd TextYankPost * call foo#test()
4005 augroup END
4006
4007 normal Y
4008
4009 augroup test
4010 autocmd!
4011 augroup END
4012 delete('Xdir', 'rf')
4013 &rtp = save_rtp
4014enddef
4015
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004016" This was causing a crash because suppress_errthrow wasn't reset.
4017def Test_vim9_autoload_error()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004018 var lines =<< trim END
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004019 vim9script
4020 def crash#func()
4021 try
4022 for x in List()
4023 endfor
4024 catch
4025 endtry
4026 g:ok = true
4027 enddef
4028 fu List()
4029 invalid
4030 endfu
4031 try
Bram Moolenaar48e11c12021-01-11 18:47:00 +01004032 alsoinvalid
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004033 catch /wontmatch/
4034 endtry
4035 END
4036 call mkdir('Xruntime/autoload', 'p')
4037 call writefile(lines, 'Xruntime/autoload/crash.vim')
4038
4039 # run in a separate Vim to avoid the side effects of assert_fails()
4040 lines =<< trim END
4041 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
4042 call crash#func()
4043 call writefile(['ok'], 'Xdidit')
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004044 qall!
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004045 END
4046 writefile(lines, 'Xscript')
4047 RunVim([], [], '-S Xscript')
4048 assert_equal(['ok'], readfile('Xdidit'))
4049
4050 delete('Xdidit')
4051 delete('Xscript')
4052 delete('Xruntime', 'rf')
Bram Moolenaar03290b82020-12-19 16:30:44 +01004053
4054 lines =<< trim END
4055 vim9script
4056 var foo#bar = 'asdf'
4057 END
4058 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004059enddef
4060
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02004061def Test_script_var_in_autocmd()
4062 # using a script variable from an autocommand, defined in a :def function in a
4063 # legacy Vim script, cannot check the variable type.
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004064 var lines =<< trim END
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02004065 let s:counter = 1
4066 def s:Func()
4067 au! CursorHold
4068 au CursorHold * s:counter += 1
4069 enddef
4070 call s:Func()
4071 doau CursorHold
4072 call assert_equal(2, s:counter)
4073 au! CursorHold
4074 END
4075 CheckScriptSuccess(lines)
4076enddef
4077
Bram Moolenaarb5841b92021-07-15 18:09:53 +02004078def Test_error_in_autoload_script()
4079 var save_rtp = &rtp
4080 var dir = getcwd() .. '/Xruntime'
4081 &rtp = dir
4082 mkdir(dir .. '/autoload', 'p')
4083
4084 var lines =<< trim END
4085 vim9script noclear
4086 def script#autoloaded()
4087 enddef
4088 def Broken()
4089 var x: any = ''
4090 eval x != 0
4091 enddef
4092 Broken()
4093 END
4094 writefile(lines, dir .. '/autoload/script.vim')
4095
4096 lines =<< trim END
4097 vim9script
4098 def CallAutoloaded()
4099 script#autoloaded()
4100 enddef
4101
4102 function Legacy()
4103 try
4104 call s:CallAutoloaded()
4105 catch
4106 call assert_match('E1030: Using a String as a Number', v:exception)
4107 endtry
4108 endfunction
4109
4110 Legacy()
4111 END
4112 CheckScriptSuccess(lines)
4113
4114 &rtp = save_rtp
4115 delete(dir, 'rf')
4116enddef
4117
Bram Moolenaar3896a102020-08-09 14:33:55 +02004118def Test_cmdline_win()
4119 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
4120 # the command line window.
4121 mkdir('rtp/syntax', 'p')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004122 var export_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02004123 vim9script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004124 export var That = 'yes'
Bram Moolenaar3896a102020-08-09 14:33:55 +02004125 END
4126 writefile(export_lines, 'rtp/syntax/Xexport.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004127 var import_lines =<< trim END
Bram Moolenaar3896a102020-08-09 14:33:55 +02004128 vim9script
4129 import That from './Xexport.vim'
4130 END
4131 writefile(import_lines, 'rtp/syntax/vim.vim')
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004132 var save_rtp = &rtp
Bram Moolenaar3896a102020-08-09 14:33:55 +02004133 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
4134 syntax on
4135 augroup CmdWin
4136 autocmd CmdwinEnter * g:got_there = 'yes'
4137 augroup END
4138 # this will open and also close the cmdline window
4139 feedkeys('q:', 'xt')
4140 assert_equal('yes', g:got_there)
4141
4142 augroup CmdWin
4143 au!
4144 augroup END
4145 &rtp = save_rtp
4146 delete('rtp', 'rf')
4147enddef
4148
Bram Moolenaare3d46852020-08-29 13:39:17 +02004149def Test_invalid_sid()
4150 assert_fails('func <SNR>1234_func', 'E123:')
Bram Moolenaar25859dd2020-08-30 12:54:53 +02004151
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004152 if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
Bram Moolenaard2c61702020-09-06 15:58:36 +02004153 assert_equal([], readfile('Xdidit'))
Bram Moolenaare3d46852020-08-29 13:39:17 +02004154 endif
4155 delete('Xdidit')
4156enddef
4157
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004158def Test_restoring_cpo()
4159 writefile(['vim9script', 'set nocp'], 'Xsourced')
4160 writefile(['call writefile(["done"], "Xdone")', 'quit!'], 'Xclose')
4161 if RunVim([], [], '-u NONE +"set cpo+=a" -S Xsourced -S Xclose')
4162 assert_equal(['done'], readfile('Xdone'))
4163 endif
4164 delete('Xsourced')
4165 delete('Xclose')
Bram Moolenaar090728a2020-12-20 15:43:31 +01004166 delete('Xdone')
Bram Moolenaar0123cc12021-02-07 17:17:58 +01004167
4168 writefile(['vim9script'], 'XanotherScript')
4169 set cpo=aABceFsMny>
4170 edit XanotherScript
4171 so %
4172 assert_equal('aABceFsMny>', &cpo)
4173 :1del
4174 w
4175 so %
4176 assert_equal('aABceFsMny>', &cpo)
4177
4178 delete('XanotherScript')
4179 set cpo&vim
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004180enddef
4181
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004182" Use :function so we can use Check commands
4183func Test_no_redraw_when_restoring_cpo()
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004184 CheckScreendump
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004185 CheckFeature timers
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004186
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004187 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004188 vim9script
4189 def script#func()
4190 enddef
4191 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004192 call mkdir('Xdir/autoload', 'p')
4193 call writefile(lines, 'Xdir/autoload/script.vim')
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004194
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004195 let lines =<< trim END
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004196 vim9script
4197 set cpo+=M
4198 exe 'set rtp^=' .. getcwd() .. '/Xdir'
Bram Moolenaar767034c2021-04-09 17:24:52 +02004199 au CmdlineEnter : ++once timer_start(0, (_) => script#func())
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004200 setline(1, 'some text')
4201 END
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004202 call writefile(lines, 'XTest_redraw_cpo')
4203 let buf = RunVimInTerminal('-S XTest_redraw_cpo', {'rows': 6})
4204 call term_sendkeys(buf, "V:")
4205 call VerifyScreenDump(buf, 'Test_vim9_no_redraw', {})
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004206
Bram Moolenaarffb7dcd2021-03-10 14:00:18 +01004207 " clean up
4208 call term_sendkeys(buf, "\<Esc>u")
4209 call StopVimInTerminal(buf)
4210 call delete('XTest_redraw_cpo')
4211 call delete('Xdir', 'rf')
4212endfunc
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004213
Bram Moolenaar9ec70262020-12-09 17:16:59 +01004214
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02004215def Test_unset_any_variable()
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004216 var lines =<< trim END
4217 var name: any
4218 assert_equal(0, name)
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +02004219 END
4220 CheckDefAndScriptSuccess(lines)
4221enddef
4222
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02004223func Test_define_func_at_command_line()
Bram Moolenaar58dbef32020-09-25 22:13:05 +02004224 CheckRunVimInTerminal
4225
Bram Moolenaar7e9210e2020-09-25 23:12:51 +02004226 " call indirectly to avoid compilation error for missing functions
4227 call Run_Test_define_func_at_command_line()
4228endfunc
4229
4230def Run_Test_define_func_at_command_line()
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004231 # run in a separate Vim instance to avoid the script context
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004232 var lines =<< trim END
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004233 func CheckAndQuit()
4234 call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
4235 call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
4236 endfunc
4237 END
4238 writefile([''], 'Xdidcmd')
4239 writefile(lines, 'XcallFunc')
Bram Moolenaare0de1712020-12-02 17:36:54 +01004240 var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004241 # define Afunc() on the command line
4242 term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
4243 term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004244 WaitForAssert(() => assert_equal(['errors: []'], readfile('Xdidcmd')))
Bram Moolenaar9c4f5522020-09-25 21:47:28 +02004245
4246 call StopVimInTerminal(buf)
4247 delete('XcallFunc')
4248 delete('Xdidcmd')
4249enddef
4250
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004251def Test_script_var_scope()
4252 var lines =<< trim END
4253 vim9script
4254 if true
4255 if true
4256 var one = 'one'
4257 echo one
4258 endif
4259 echo one
4260 endif
4261 END
4262 CheckScriptFailure(lines, 'E121:', 7)
4263
4264 lines =<< trim END
4265 vim9script
4266 if true
4267 if false
4268 var one = 'one'
4269 echo one
4270 else
4271 var one = 'one'
4272 echo one
4273 endif
4274 echo one
4275 endif
4276 END
4277 CheckScriptFailure(lines, 'E121:', 10)
4278
4279 lines =<< trim END
4280 vim9script
4281 while true
4282 var one = 'one'
4283 echo one
4284 break
4285 endwhile
4286 echo one
4287 END
4288 CheckScriptFailure(lines, 'E121:', 7)
4289
4290 lines =<< trim END
4291 vim9script
4292 for i in range(1)
4293 var one = 'one'
4294 echo one
4295 endfor
4296 echo one
4297 END
4298 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaar9becdf22020-10-10 21:33:48 +02004299
4300 lines =<< trim END
4301 vim9script
4302 {
4303 var one = 'one'
4304 assert_equal('one', one)
4305 }
4306 assert_false(exists('one'))
4307 assert_false(exists('s:one'))
4308 END
4309 CheckScriptSuccess(lines)
4310
4311 lines =<< trim END
4312 vim9script
4313 {
4314 var one = 'one'
4315 echo one
4316 }
4317 echo one
4318 END
4319 CheckScriptFailure(lines, 'E121:', 6)
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02004320enddef
4321
Bram Moolenaar352134b2020-10-17 22:04:08 +02004322def Test_catch_exception_in_callback()
4323 var lines =<< trim END
4324 vim9script
Bram Moolenaar2a389082021-04-09 20:24:31 +02004325 def Callback(...l: list<any>)
Bram Moolenaar352134b2020-10-17 22:04:08 +02004326 try
4327 var x: string
4328 var y: string
4329 # this error should be caught with CHECKLEN
4330 [x, y] = ['']
4331 catch
4332 g:caught = 'yes'
4333 endtry
4334 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004335 popup_menu('popup', {callback: Callback})
Bram Moolenaar352134b2020-10-17 22:04:08 +02004336 feedkeys("\r", 'xt')
4337 END
4338 CheckScriptSuccess(lines)
4339
4340 unlet g:caught
4341enddef
4342
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004343def Test_no_unknown_error_after_error()
4344 if !has('unix') || !has('job')
4345 throw 'Skipped: not unix of missing +job feature'
4346 endif
Bram Moolenaarb16ff292021-09-26 20:14:39 +01004347 # FIXME: this check should not be needed
4348 if has('win32')
4349 throw 'Skipped: does not work on MS-Windows'
4350 endif
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004351 var lines =<< trim END
4352 vim9script
4353 var source: list<number>
Bram Moolenaar2a389082021-04-09 20:24:31 +02004354 def Out_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004355 eval [][0]
4356 enddef
Bram Moolenaar2a389082021-04-09 20:24:31 +02004357 def Exit_cb(...l: list<any>)
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004358 sleep 1m
4359 source += l
4360 enddef
Bram Moolenaare0de1712020-12-02 17:36:54 +01004361 var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
Bram Moolenaar6f17a3f2020-12-21 18:11:24 +01004362 while job_status(myjob) == 'run'
4363 sleep 10m
4364 endwhile
Bram Moolenaar206c2a62021-01-31 14:04:44 +01004365 # wait for Exit_cb() to be called
Bram Moolenaarfe95b942021-04-10 20:52:43 +02004366 sleep 200m
Bram Moolenaar631e8f92020-11-04 15:07:16 +01004367 END
4368 writefile(lines, 'Xdef')
4369 assert_fails('so Xdef', ['E684:', 'E1012:'])
4370 delete('Xdef')
4371enddef
4372
Bram Moolenaar4324d872020-12-01 20:12:24 +01004373def InvokeNormal()
4374 exe "norm! :m+1\r"
4375enddef
4376
4377def Test_invoke_normal_in_visual_mode()
4378 xnoremap <F3> <Cmd>call <SID>InvokeNormal()<CR>
4379 new
4380 setline(1, ['aaa', 'bbb'])
4381 feedkeys("V\<F3>", 'xt')
4382 assert_equal(['bbb', 'aaa'], getline(1, 2))
4383 xunmap <F3>
4384enddef
4385
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004386def Test_white_space_after_command()
4387 var lines =<< trim END
4388 exit_cb: Func})
4389 END
4390 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarf8103f22020-12-25 17:36:27 +01004391
4392 lines =<< trim END
4393 e#
4394 END
4395 CheckDefAndScriptFailure(lines, 'E1144:', 1)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004396enddef
4397
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004398def Test_script_var_gone_when_sourced_twice()
4399 var lines =<< trim END
4400 vim9script
4401 if exists('g:guard')
4402 finish
4403 endif
4404 g:guard = 1
4405 var name = 'thename'
4406 def g:GetName(): string
4407 return name
4408 enddef
4409 def g:SetName(arg: string)
4410 name = arg
4411 enddef
4412 END
4413 writefile(lines, 'XscriptTwice.vim')
4414 so XscriptTwice.vim
4415 assert_equal('thename', g:GetName())
4416 g:SetName('newname')
4417 assert_equal('newname', g:GetName())
4418 so XscriptTwice.vim
4419 assert_fails('call g:GetName()', 'E1149:')
4420 assert_fails('call g:SetName("x")', 'E1149:')
4421
4422 delfunc g:GetName
4423 delfunc g:SetName
4424 delete('XscriptTwice.vim')
4425 unlet g:guard
4426enddef
4427
4428def Test_import_gone_when_sourced_twice()
4429 var exportlines =<< trim END
4430 vim9script
4431 if exists('g:guard')
4432 finish
4433 endif
4434 g:guard = 1
4435 export var name = 'someName'
4436 END
4437 writefile(exportlines, 'XexportScript.vim')
4438
4439 var lines =<< trim END
4440 vim9script
4441 import name from './XexportScript.vim'
4442 def g:GetName(): string
4443 return name
4444 enddef
4445 END
4446 writefile(lines, 'XscriptImport.vim')
4447 so XscriptImport.vim
4448 assert_equal('someName', g:GetName())
4449
4450 so XexportScript.vim
4451 assert_fails('call g:GetName()', 'E1149:')
4452
4453 delfunc g:GetName
4454 delete('XexportScript.vim')
4455 delete('XscriptImport.vim')
4456 unlet g:guard
4457enddef
4458
Bram Moolenaar10b94212021-02-19 21:42:57 +01004459def Test_unsupported_commands()
4460 var lines =<< trim END
4461 ka
4462 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004463 CheckDefFailure(lines, 'E476:')
4464 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaar10b94212021-02-19 21:42:57 +01004465
4466 lines =<< trim END
Bram Moolenaarada1d872021-02-20 08:16:51 +01004467 :1ka
4468 END
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004469 CheckDefFailure(lines, 'E476:')
4470 CheckScriptFailure(['vim9script'] + lines, 'E492:')
Bram Moolenaarada1d872021-02-20 08:16:51 +01004471
4472 lines =<< trim END
Bram Moolenaar10b94212021-02-19 21:42:57 +01004473 t
4474 END
4475 CheckDefFailure(lines, 'E1100:')
4476 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4477
4478 lines =<< trim END
4479 x
4480 END
4481 CheckDefFailure(lines, 'E1100:')
4482 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4483
4484 lines =<< trim END
4485 xit
4486 END
4487 CheckDefFailure(lines, 'E1100:')
4488 CheckScriptFailure(['vim9script'] + lines, 'E1100:')
4489enddef
4490
Bram Moolenaarc70fe462021-04-17 17:59:19 +02004491def Test_mapping_line_number()
4492 var lines =<< trim END
4493 vim9script
4494 def g:FuncA()
4495 # Some comment
4496 FuncB(0)
4497 enddef
4498 # Some comment
4499 def FuncB(
4500 # Some comment
4501 n: number
4502 )
4503 exe 'nno '
4504 # Some comment
4505 .. '<F3> a'
4506 .. 'b'
4507 .. 'c'
4508 enddef
4509 END
4510 CheckScriptSuccess(lines)
4511 var res = execute('verbose nmap <F3>')
4512 assert_match('No mapping found', res)
4513
4514 g:FuncA()
4515 res = execute('verbose nmap <F3>')
4516 assert_match(' <F3> .* abc.*Last set from .*XScriptSuccess\d\+ line 11', res)
4517
4518 nunmap <F3>
4519 delfunc g:FuncA
4520enddef
4521
Bram Moolenaardeb108b2021-07-08 17:35:36 +02004522def Test_option_set()
4523 # legacy script allows for white space
4524 var lines =<< trim END
4525 set foldlevel =11
4526 call assert_equal(11, &foldlevel)
4527 END
4528 CheckScriptSuccess(lines)
4529
4530 set foldlevel
4531 set foldlevel=12
4532 assert_equal(12, &foldlevel)
4533 set foldlevel+=2
4534 assert_equal(14, &foldlevel)
4535 set foldlevel-=3
4536 assert_equal(11, &foldlevel)
4537
4538 lines =<< trim END
4539 set foldlevel =1
4540 END
4541 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: =1')
4542
4543 lines =<< trim END
4544 set foldlevel +=1
4545 END
4546 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: +=1')
4547
4548 lines =<< trim END
4549 set foldlevel ^=1
4550 END
4551 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: ^=1')
4552
4553 lines =<< trim END
4554 set foldlevel -=1
4555 END
4556 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: -=1')
4557
4558 set foldlevel&
4559enddef
4560
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004561def Test_option_modifier()
Bram Moolenaar1594f312021-07-08 16:40:13 +02004562 # legacy script allows for white space
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004563 var lines =<< trim END
4564 set hlsearch & hlsearch !
4565 call assert_equal(1, &hlsearch)
4566 END
4567 CheckScriptSuccess(lines)
4568
Bram Moolenaar1594f312021-07-08 16:40:13 +02004569 set hlsearch
4570 set hlsearch!
4571 assert_equal(false, &hlsearch)
4572
4573 set hlsearch
4574 set hlsearch&
4575 assert_equal(false, &hlsearch)
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004576
4577 lines =<< trim END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004578 set hlsearch &
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004579 END
Bram Moolenaar1594f312021-07-08 16:40:13 +02004580 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: &')
4581
4582 lines =<< trim END
4583 set hlsearch !
4584 END
4585 CheckDefExecAndScriptFailure(lines, 'E1205: No white space allowed between option and: !')
4586
4587 set hlsearch&
Bram Moolenaar208f0b42021-06-20 12:40:08 +02004588enddef
4589
Bram Moolenaarc03fe662021-07-11 16:52:45 +02004590" This must be called last, it may cause following :def functions to fail
4591def Test_xxx_echoerr_line_number()
4592 var lines =<< trim END
4593 echoerr 'some'
4594 .. ' error'
4595 .. ' continued'
4596 END
4597 CheckDefExecAndScriptFailure(lines, 'some error continued', 1)
4598enddef
4599
Bram Moolenaar648594e2021-07-11 17:55:01 +02004600def ProfiledWithLambda()
Bram Moolenaard9162552021-07-11 15:26:13 +02004601 var n = 3
4602 echo [[1, 2], [3, 4]]->filter((_, l) => l[0] == n)
4603enddef
4604
Bram Moolenaar648594e2021-07-11 17:55:01 +02004605def ProfiledNested()
4606 var x = 0
4607 def Nested(): any
4608 return x
4609 enddef
4610 Nested()
4611enddef
4612
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004613def ProfiledNestedProfiled()
4614 var x = 0
4615 def Nested(): any
4616 return x
4617 enddef
4618 Nested()
4619enddef
4620
Dominique Pelle923dce22021-11-21 11:36:04 +00004621" Execute this near the end, profiling doesn't stop until Vim exits.
Bram Moolenaard9162552021-07-11 15:26:13 +02004622" This only tests that it works, not the profiling output.
4623def Test_xx_profile_with_lambda()
Bram Moolenaar4ece1522021-07-11 16:31:51 +02004624 CheckFeature profile
4625
Bram Moolenaard9162552021-07-11 15:26:13 +02004626 profile start Xprofile.log
Bram Moolenaar648594e2021-07-11 17:55:01 +02004627 profile func ProfiledWithLambda
4628 ProfiledWithLambda()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004629
Bram Moolenaar648594e2021-07-11 17:55:01 +02004630 profile func ProfiledNested
4631 ProfiledNested()
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02004632
4633 # Also profile the nested function. Use a different function, although the
4634 # contents is the same, to make sure it was not already compiled.
4635 profile func *
4636 ProfiledNestedProfiled()
4637
4638 profdel func *
4639 profile pause
Bram Moolenaard9162552021-07-11 15:26:13 +02004640enddef
4641
Bram Moolenaar585fea72020-04-02 22:33:21 +02004642" Keep this last, it messes up highlighting.
4643def Test_substitute_cmd()
4644 new
4645 setline(1, 'something')
4646 :substitute(some(other(
4647 assert_equal('otherthing', getline(1))
4648 bwipe!
4649
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004650 # also when the context is Vim9 script
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02004651 var lines =<< trim END
Bram Moolenaar585fea72020-04-02 22:33:21 +02004652 vim9script
4653 new
4654 setline(1, 'something')
4655 :substitute(some(other(
4656 assert_equal('otherthing', getline(1))
4657 bwipe!
4658 END
4659 writefile(lines, 'Xvim9lines')
4660 source Xvim9lines
4661
4662 delete('Xvim9lines')
4663enddef
4664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker