blob: 2ce825f2ba13f00610d2c937621c698927ae6a6b [file] [log] [blame]
Bram Moolenaard8448622022-01-07 21:39:52 +00001" Test import/export of the Vim9 script language.
Bram Moolenaar160aa862022-01-10 21:29:57 +00002" Also the autoload mechanism.
Bram Moolenaard8448622022-01-07 21:39:52 +00003
4source check.vim
5source term_util.vim
Bram Moolenaar62aec932022-01-29 21:45:34 +00006import './vim9.vim' as v9
Bram Moolenaard8448622022-01-07 21:39:52 +00007
8let s:export_script_lines =<< trim END
9 vim9script
10 var name: string = 'bob'
11 def Concat(arg: string): string
12 return name .. arg
13 enddef
14 g:result = Concat('bie')
15 g:localname = name
16
17 export const CONST = 1234
18 export var exported = 9876
19 export var exp_name = 'John'
20 export def Exported(): string
21 return 'Exported'
22 enddef
23 export def ExportedValue(): number
24 return exported
25 enddef
26 export def ExportedInc()
27 exported += 5
28 enddef
29 export final theList = [1]
Bram Moolenaar857c8bb2022-01-15 21:08:19 +000030 export def AddSome(s: string): string
31 return s .. 'some'
32 enddef
33 export var AddRef = AddSome
Bram Moolenaard8448622022-01-07 21:39:52 +000034END
35
Bram Moolenaar62aec932022-01-29 21:45:34 +000036def s:Undo_export_script_lines()
Bram Moolenaard8448622022-01-07 21:39:52 +000037 unlet g:result
38 unlet g:localname
39enddef
40
41def Test_vim9_import_export()
42 writefile(s:export_script_lines, 'Xexport.vim')
43 var import_script_lines =<< trim END
44 vim9script
45 var dir = './'
46 var ext = ".vim"
47 import dir .. 'Xexport' .. ext as expo
48
49 g:exported1 = expo.exported
50 expo.exported += 3
51 g:exported2 = expo.exported
52 g:exported3 = expo.ExportedValue()
53
54 expo.ExportedInc()
55 g:exported_i1 = expo.exported
56 g:exported_i2 = expo.ExportedValue()
57
58 expo.exported = 11
59 g:exported_s1 = expo.exported
60 g:exported_s2 = expo.ExportedValue()
61
62 g:imported_func = expo.Exported()
63
64 def GetExported(): string
65 var local_dict = {ref: expo.Exported}
66 return local_dict.ref()
67 enddef
68 g:funcref_result = GetExported()
69
Bram Moolenaar21f0d6c2022-01-20 17:35:49 +000070 def GetName(): string
71 return expo.exp_name .. 'son'
72 enddef
73 g:long_name = GetName()
74
Bram Moolenaard8448622022-01-07 21:39:52 +000075 g:imported_name = expo.exp_name
76 expo.exp_name ..= ' Doe'
Bram Moolenaar47036b62022-01-16 21:18:53 +000077 expo.exp_name = expo.exp_name .. ' Maar'
Bram Moolenaard8448622022-01-07 21:39:52 +000078 g:imported_name_appended = expo.exp_name
79 g:exported_later = expo.exported
80
81 expo.theList->add(2)
82 assert_equal([1, 2], expo.theList)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +000083
84 assert_equal('andthensome', 'andthen'->expo.AddSome())
85 assert_equal('awesome', 'awe'->expo.AddRef())
Bram Moolenaard8448622022-01-07 21:39:52 +000086 END
87 writefile(import_script_lines, 'Ximport.vim')
88 source Ximport.vim
89
90 assert_equal('bobbie', g:result)
91 assert_equal('bob', g:localname)
92 assert_equal(9876, g:exported1)
93 assert_equal(9879, g:exported2)
94 assert_equal(9879, g:exported3)
95
96 assert_equal(9884, g:exported_i1)
97 assert_equal(9884, g:exported_i2)
98
99 assert_equal(11, g:exported_s1)
100 assert_equal(11, g:exported_s2)
101 assert_equal(11, g:exported_later)
102
103 assert_equal('Exported', g:imported_func)
104 assert_equal('Exported', g:funcref_result)
105 assert_equal('John', g:imported_name)
Bram Moolenaar21f0d6c2022-01-20 17:35:49 +0000106 assert_equal('Johnson', g:long_name)
Bram Moolenaar47036b62022-01-16 21:18:53 +0000107 assert_equal('John Doe Maar', g:imported_name_appended)
Bram Moolenaard8448622022-01-07 21:39:52 +0000108 assert_false(exists('g:name'))
109
110 Undo_export_script_lines()
111 unlet g:exported1
112 unlet g:exported2
113 unlet g:exported3
114 unlet g:exported_i1
115 unlet g:exported_i2
116 unlet g:exported_later
117 unlet g:imported_func
Bram Moolenaar21f0d6c2022-01-20 17:35:49 +0000118 unlet g:imported_name g:long_name g:imported_name_appended
Bram Moolenaard8448622022-01-07 21:39:52 +0000119 delete('Ximport.vim')
120
121 # similar, with line breaks
122 var import_line_break_script_lines =<< trim END
123 vim9script
124 import './Xexport.vim'
125 as expo
126 g:exported = expo.exported
127 expo.exported += 7
128 g:exported_added = expo.exported
129 g:imported_func = expo.Exported()
130 END
131 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
132 source Ximport_lbr.vim
133
134 assert_equal(11, g:exported)
135 assert_equal(18, g:exported_added)
136 assert_equal('Exported', g:imported_func)
137
138 # exported script not sourced again
139 assert_false(exists('g:result'))
140 unlet g:exported
141 unlet g:exported_added
142 unlet g:imported_func
143 delete('Ximport_lbr.vim')
144
Bram Moolenaar68854a82022-01-31 18:59:13 +0000145 var import_shadows_cmdmod_lines =<< trim END
146 vim9script
147 import './Xexport.vim' as vim9
148 vim9.exp_name = 'Shadow'
149 assert_equal('Shadow', vim9.exp_name)
150 END
151 v9.CheckScriptSuccess(import_shadows_cmdmod_lines)
152
Bram Moolenaard8448622022-01-07 21:39:52 +0000153 var line_break_before_dot =<< trim END
154 vim9script
155 import './Xexport.vim' as expo
156 g:exported = expo
157 .exported
158 END
159 writefile(line_break_before_dot, 'Ximport_lbr_before_dot.vim')
160 assert_fails('source Ximport_lbr_before_dot.vim', 'E1060:', '', 3)
161 delete('Ximport_lbr_before_dot.vim')
162
163 var line_break_after_dot =<< trim END
164 vim9script
165 import './Xexport.vim' as expo
166 g:exported = expo.
167 exported
168 END
169 writefile(line_break_after_dot, 'Ximport_lbr_after_dot.vim')
170 assert_fails('source Ximport_lbr_after_dot.vim', 'E1074:', '', 3)
171 delete('Ximport_lbr_after_dot.vim')
172
173 var import_star_as_lines =<< trim END
174 vim9script
175 import './Xexport.vim' as Export
176 def UseExport()
177 g:exported_def = Export.exported
178 enddef
179 g:exported_script = Export.exported
180 assert_equal(1, exists('Export.exported'))
181 assert_equal(0, exists('Export.notexported'))
182 UseExport()
183 END
184 writefile(import_star_as_lines, 'Ximport.vim')
185 source Ximport.vim
186
187 assert_equal(18, g:exported_def)
188 assert_equal(18, g:exported_script)
189 unlet g:exported_def
190 unlet g:exported_script
191
192 var import_star_as_lines_no_dot =<< trim END
193 vim9script
194 import './Xexport.vim' as Export
195 def Func()
196 var dummy = 1
197 var imported = Export + dummy
198 enddef
199 defcompile
200 END
201 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
202 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
203
204 var import_star_as_lines_dot_space =<< trim END
205 vim9script
206 import './Xexport.vim' as Export
207 def Func()
208 var imported = Export . exported
209 enddef
210 defcompile
211 END
212 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
213 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
214
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000215 writefile(s:export_script_lines, 'Xexport2.vim')
216 var import_as_duplicated =<< trim END
Bram Moolenaard8448622022-01-07 21:39:52 +0000217 vim9script
218 import './Xexport.vim' as expo
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000219 import './Xexport2.vim' as expo
Bram Moolenaard8448622022-01-07 21:39:52 +0000220 END
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000221 writefile(import_as_duplicated, 'Ximport.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000222 assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000223 delete('Xexport2.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000224
225 var import_star_as_lines_script_no_dot =<< trim END
226 vim9script
227 import './Xexport.vim' as Export
228 g:imported_script = Export exported
229 END
230 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
231 assert_fails('source Ximport.vim', 'E1060: Expected dot after name: Export exported')
232
233 var import_star_as_lines_script_space_after_dot =<< trim END
234 vim9script
235 import './Xexport.vim' as Export
236 g:imported_script = Export. exported
237 END
238 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
239 assert_fails('source Ximport.vim', 'E1074:')
240
241 var import_star_as_lines_missing_name =<< trim END
242 vim9script
243 import './Xexport.vim' as Export
244 def Func()
245 var imported = Export.
246 enddef
247 defcompile
248 END
249 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
250 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
251
252 var import_star_as_lbr_lines =<< trim END
253 vim9script
254 import './Xexport.vim'
255 as Export
256 def UseExport()
257 g:exported = Export.exported
258 enddef
259 UseExport()
260 END
261 writefile(import_star_as_lbr_lines, 'Ximport.vim')
262 source Ximport.vim
263 assert_equal(18, g:exported)
264 unlet g:exported
265
266 # try to use something that exists but is not exported
267 var import_not_exported_lines =<< trim END
268 vim9script
269 import './Xexport.vim' as expo
270 echo expo.name
271 END
272 writefile(import_not_exported_lines, 'Ximport.vim')
273 assert_fails('source Ximport.vim', 'E1049:', '', 3, 'Ximport.vim')
274
275 # try to import something that is already defined
276 var import_already_defined =<< trim END
277 vim9script
278 var exported = 'something'
279 import './Xexport.vim' as exported
280 END
281 writefile(import_already_defined, 'Ximport.vim')
282 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
283
284 # try changing an imported const
285 var import_assign_to_const =<< trim END
286 vim9script
287 import './Xexport.vim' as expo
288 def Assign()
289 expo.CONST = 987
290 enddef
291 defcompile
292 END
293 writefile(import_assign_to_const, 'Ximport.vim')
294 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
295
296 # try changing an imported final
297 var import_assign_to_final =<< trim END
298 vim9script
299 import './Xexport.vim' as expo
300 def Assign()
301 expo.theList = [2]
302 enddef
303 defcompile
304 END
305 writefile(import_assign_to_final, 'Ximport.vim')
306 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
307
308 var import_no_as_lines =<< trim END
309 vim9script
310 import './Xexport.vim' name
311 END
312 writefile(import_no_as_lines, 'Ximport.vim')
313 assert_fails('source Ximport.vim', 'E488:', '', 2, 'Ximport.vim')
314
315 var import_invalid_string_lines =<< trim END
316 vim9script
317 import Xexport.vim
318 END
319 writefile(import_invalid_string_lines, 'Ximport.vim')
320 assert_fails('source Ximport.vim', 'E121:', '', 2, 'Ximport.vim')
321
322 var import_wrong_name_lines =<< trim END
323 vim9script
324 import './XnoExport.vim'
325 END
326 writefile(import_wrong_name_lines, 'Ximport.vim')
327 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
328
329 var import_redefining_lines =<< trim END
330 vim9script
331 import './Xexport.vim' as exported
332 var exported = 5
333 END
334 writefile(import_redefining_lines, 'Ximport.vim')
335 assert_fails('source Ximport.vim', 'E1213: Redefining imported item "exported"', '', 3)
336
Bram Moolenaar160aa862022-01-10 21:29:57 +0000337 var import_missing_dot_lines =<< trim END
338 vim9script
339 import './Xexport.vim' as expo
340 def Test()
341 expo = 9
342 enddef
343 defcompile
344 END
345 writefile(import_missing_dot_lines, 'Ximport.vim')
346 assert_fails('source Ximport.vim', 'E1258:', '', 1)
347
348 var import_missing_name_lines =<< trim END
349 vim9script
350 import './Xexport.vim' as expo
351 def Test()
352 expo.99 = 9
353 enddef
354 defcompile
355 END
356 writefile(import_missing_name_lines, 'Ximport.vim')
Bram Moolenaar76283822022-01-10 21:39:03 +0000357 assert_fails('source Ximport.vim', 'E1259:', '', 1)
Bram Moolenaar160aa862022-01-10 21:29:57 +0000358
Bram Moolenaard8448622022-01-07 21:39:52 +0000359 var import_assign_wrong_type_lines =<< trim END
360 vim9script
361 import './Xexport.vim' as expo
362 expo.exported = 'xxx'
363 END
364 writefile(import_assign_wrong_type_lines, 'Ximport.vim')
365 assert_fails('source Ximport.vim', 'E1012: Type mismatch; expected number but got string', '', 3)
366
367 var import_assign_const_lines =<< trim END
368 vim9script
369 import './Xexport.vim' as expo
370 expo.CONST = 4321
371 END
372 writefile(import_assign_const_lines, 'Ximport.vim')
373 assert_fails('source Ximport.vim', 'E46: Cannot change read-only variable "CONST"', '', 3)
374
375 delete('Ximport.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000376 delete('Xexport.vim')
377
378 # Check that in a Vim9 script 'cpo' is set to the Vim default.
379 # Flags added or removed are also applied to the restored value.
380 set cpo=abcd
381 var lines =<< trim END
382 vim9script
383 g:cpo_in_vim9script = &cpo
384 set cpo+=f
385 set cpo-=c
386 g:cpo_after_vim9script = &cpo
387 END
388 writefile(lines, 'Xvim9_script')
389 source Xvim9_script
390 assert_equal('fabd', &cpo)
391 set cpo&vim
392 assert_equal(&cpo, g:cpo_in_vim9script)
393 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
394 assert_equal(newcpo, g:cpo_after_vim9script)
395
396 delete('Xvim9_script')
397enddef
398
399def Test_import_funcref()
400 var lines =<< trim END
401 vim9script
402 export def F(): number
403 return 42
404 enddef
405 export const G = F
406 END
407 writefile(lines, 'Xlib.vim')
408
409 lines =<< trim END
410 vim9script
411 import './Xlib.vim' as lib
412 const Foo = lib.G()
413 assert_equal(42, Foo)
414
415 def DoTest()
416 const Goo = lib.G()
417 assert_equal(42, Goo)
418 enddef
419 DoTest()
420 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000421 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000422
423 delete('Xlib.vim')
424enddef
425
Bram Moolenaarc2f17f72022-02-21 13:13:50 +0000426def Test_import_duplicate_function()
427 # Function Hover() exists in both scripts, partial should refer to the right
428 # one.
429 var lines =<< trim END
430 vim9script
431
432 def Hover(d: dict<any>): string
433 return 'found it'
434 enddef
435
436 export def NewLspServer(): dict<any>
437 var d: dict<any> = {}
438 d->extend({hover: function('Hover', [d])})
439 return d
440 enddef
441
442 NewLspServer()
443 END
444 writefile(lines, 'Xserver.vim')
445
446 lines =<< trim END
447 vim9script
448
449 import './Xserver.vim' as server
450
451 export def Hover()
452 enddef
453
454 def AddServer()
455 var d: dict<any> = server.NewLspServer()
456 assert_equal('found it', d.hover())
457 enddef
458 AddServer()
459 END
460 v9.CheckScriptSuccess(lines)
461
462 delete('Xserver.vim')
463enddef
464
465
Bram Moolenaard8448622022-01-07 21:39:52 +0000466def Test_import_fails()
467 writefile([], 'Xfoo.vim')
468 var lines =<< trim END
469 import './Xfoo.vim' as foo
470 foo = 'bar'
471 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000472 v9.CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use foo itself'])
Bram Moolenaard8448622022-01-07 21:39:52 +0000473 lines =<< trim END
474 vim9script
475 import './Xfoo.vim' as foo
476 var that = foo
477 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000478 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaardd5893b2022-01-20 21:32:54 +0000479 lines =<< trim END
480 vim9script
481 import './Xfoo.vim' as foo
482 var that: any
483 that += foo
484 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000485 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaardd5893b2022-01-20 21:32:54 +0000486 lines =<< trim END
487 vim9script
488 import './Xfoo.vim' as foo
489 foo += 9
490 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000491 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaard8448622022-01-07 21:39:52 +0000492
493 lines =<< trim END
494 vim9script
495 import './Xfoo.vim' as 9foo
496 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000497 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000498 lines =<< trim END
499 vim9script
500 import './Xfoo.vim' as the#foo
501 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000502 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000503 lines =<< trim END
504 vim9script
505 import './Xfoo.vim' as g:foo
506 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000507 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000508
509 delete('Xfoo.vim')
510
511 lines =<< trim END
512 vim9script
513 def TheFunc()
514 echo 'the func'
515 enddef
516 export var Ref = TheFunc
517 END
518 writefile([], 'Xthat.vim')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000519
Bram Moolenaard8448622022-01-07 21:39:52 +0000520 lines =<< trim END
521 import './Xthat.vim' as That
522 That()
523 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000524 v9.CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use That itself'])
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000525
526 lines =<< trim END
Bram Moolenaar937610b2022-01-19 17:21:29 +0000527 vim9script
528 import './Xthat.vim' as That
529 def Func()
530 echo That()
531 enddef
532 Func()
533 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000534 v9.CheckScriptFailure(lines, 'E1236: Cannot use That itself')
Bram Moolenaar937610b2022-01-19 17:21:29 +0000535
536 lines =<< trim END
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000537 import './Xthat.vim' as one
538 import './Xthat.vim' as two
539 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000540 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000541
542 delete('Xthat.vim')
Bram Moolenaar779aeff2022-02-08 19:12:19 +0000543
544 lines =<< trim END
545 vim9script
546 export var item = 'hello'
547 import './Xyourself.vim'
548 END
549 writefile(lines, 'Xyourself.vim')
550 assert_fails('source Xyourself.vim', 'E1088:')
551 delete('Xyourself.vim')
552
Bram Moolenaard8448622022-01-07 21:39:52 +0000553 mkdir('Ximport')
554
555 writefile(['vim9script'], 'Ximport/.vim')
556 lines =<< trim END
557 vim9script
558 import './Ximport/.vim'
559 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000560 v9.CheckScriptFailure(lines, 'E1261: Cannot import .vim without using "as"')
Bram Moolenaard8448622022-01-07 21:39:52 +0000561 lines =<< trim END
562 vim9script
563 import './Ximport/.vim' as vim
564 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000565 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000566
567 writefile(['vim9script'], 'Ximport/.vimrc')
568 lines =<< trim END
569 vim9script
570 import './Ximport/.vimrc'
571 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000572 v9.CheckScriptFailure(lines, 'E1257: Imported script must use "as" or end in .vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000573 lines =<< trim END
574 vim9script
575 import './Ximport/.vimrc' as vimrc
576 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000577 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000578
579 delete('Ximport', 'rf')
580enddef
581
582func g:Trigger()
583 source Ximport.vim
584 return "echo 'yes'\<CR>"
585endfunc
586
587def Test_import_export_expr_map()
588 # check that :import and :export work when buffer is locked
589 var export_lines =<< trim END
590 vim9script
591 export def That(): string
592 return 'yes'
593 enddef
594 END
595 writefile(export_lines, 'Xexport_that.vim')
596
597 var import_lines =<< trim END
598 vim9script
599 import './Xexport_that.vim' as that
600 assert_equal('yes', that.That())
601 END
602 writefile(import_lines, 'Ximport.vim')
603
604 nnoremap <expr> trigger g:Trigger()
605 feedkeys('trigger', "xt")
606
607 delete('Xexport_that.vim')
608 delete('Ximport.vim')
609 nunmap trigger
610enddef
611
612def Test_import_in_filetype()
613 # check that :import works when the buffer is locked
614 mkdir('ftplugin', 'p')
615 var export_lines =<< trim END
616 vim9script
617 export var That = 'yes'
618 END
619 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
620
621 var import_lines =<< trim END
622 vim9script
623 import './Xexport_ft.vim' as ft
624 assert_equal('yes', ft.That)
625 g:did_load_mytpe = 1
626 END
627 writefile(import_lines, 'ftplugin/qf.vim')
628
629 var save_rtp = &rtp
630 &rtp = getcwd() .. ',' .. &rtp
631
632 filetype plugin on
633 copen
634 assert_equal(1, g:did_load_mytpe)
635
636 quit!
637 delete('Xexport_ft.vim')
638 delete('ftplugin', 'rf')
639 &rtp = save_rtp
640enddef
641
642def Test_use_import_in_mapping()
643 var lines =<< trim END
644 vim9script
645 export def Funcx()
646 g:result = 42
647 enddef
648 END
649 writefile(lines, 'XsomeExport.vim')
650 lines =<< trim END
651 vim9script
652 import './XsomeExport.vim' as some
653 var Funcy = some.Funcx
654 nnoremap <F3> :call <sid>Funcy()<cr>
655 END
656 writefile(lines, 'Xmapscript.vim')
657
658 source Xmapscript.vim
659 feedkeys("\<F3>", "xt")
660 assert_equal(42, g:result)
661
662 unlet g:result
663 delete('XsomeExport.vim')
664 delete('Xmapscript.vim')
665 nunmap <F3>
666enddef
667
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000668def Test_use_import_in_command_completion()
Bram Moolenaar15d16352022-01-17 20:09:08 +0000669 var lines =<< trim END
670 vim9script
671 export def Complete(..._): list<string>
672 return ['abcd']
673 enddef
674 END
675 writefile(lines, 'Xscript.vim')
676
677 lines =<< trim END
678 vim9script
679 import './Xscript.vim'
680
681 command -nargs=1 -complete=customlist,Xscript.Complete Cmd echo 'ok'
682 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
683 assert_equal('#Cmd abcd', @:)
684 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000685 v9.CheckScriptSuccess(lines)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000686
687 delcommand Cmd
688 delete('Xscript.vim')
689enddef
690
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000691def Test_use_autoload_import_in_insert_completion()
692 mkdir('Xdir/autoload', 'p')
693 var save_rtp = &rtp
694 exe 'set rtp^=' .. getcwd() .. '/Xdir'
695
696 var lines =<< trim END
697 vim9script
698 export def ThesaurusFunc(findbase: bool, _): any
699 if findbase
700 return 1
701 endif
702 return [
703 'check',
704 'experiment',
705 'test',
706 'verification'
707 ]
708 enddef
709 g:completion_loaded = 'yes'
710 END
711 writefile(lines, 'Xdir/autoload/completion.vim')
712
713 new
714 lines =<< trim END
715 vim9script
716 g:completion_loaded = 'no'
717 import autoload 'completion.vim'
718 set thesaurusfunc=completion.ThesaurusFunc
719 assert_equal('no', g:completion_loaded)
720 feedkeys("i\<C-X>\<C-T>\<C-N>\<Esc>", 'xt')
721 assert_equal('experiment', getline(1))
722 assert_equal('yes', g:completion_loaded)
723 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000724 v9.CheckScriptSuccess(lines)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000725
726 set thesaurusfunc=
727 bwipe!
728 delete('Xdir', 'rf')
729 &rtp = save_rtp
730enddef
731
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000732def Test_use_autoload_import_partial_in_opfunc()
733 mkdir('Xdir/autoload', 'p')
734 var save_rtp = &rtp
735 exe 'set rtp^=' .. getcwd() .. '/Xdir'
736
737 var lines =<< trim END
738 vim9script
739 export def Opfunc(..._)
740 g:opfunc_called = 'yes'
741 enddef
742 END
743 writefile(lines, 'Xdir/autoload/opfunc.vim')
744
745 new
746 lines =<< trim END
747 vim9script
748 import autoload 'opfunc.vim'
749 nnoremap <expr> <F3> TheFunc()
750 def TheFunc(): string
751 &operatorfunc = function('opfunc.Opfunc', [0])
752 return 'g@'
753 enddef
754 feedkeys("\<F3>l", 'xt')
755 assert_equal('yes', g:opfunc_called)
756 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000757 v9.CheckScriptSuccess(lines)
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000758
759 set opfunc=
760 bwipe!
761 delete('Xdir', 'rf')
Bram Moolenaar06b77222022-01-25 15:51:56 +0000762 nunmap <F3>
763 &rtp = save_rtp
764enddef
765
766def Test_set_opfunc_to_autoload_func_directly()
767 mkdir('Xdir/autoload', 'p')
768 var save_rtp = &rtp
769 exe 'set rtp^=' .. getcwd() .. '/Xdir'
770
771 var lines =<< trim END
772 vim9script
773 export def Opfunc(..._)
774 g:opfunc_called = 'yes'
775 enddef
776 END
777 writefile(lines, 'Xdir/autoload/opfunc.vim')
778
779 new
780 lines =<< trim END
781 vim9script
782 import autoload 'opfunc.vim'
783 nnoremap <expr> <F3> TheFunc()
784 def TheFunc(): string
785 &operatorfunc = opfunc.Opfunc
786 return 'g@'
787 enddef
788 feedkeys("\<F3>l", 'xt')
789 assert_equal('yes', g:opfunc_called)
790 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000791 v9.CheckScriptSuccess(lines)
Bram Moolenaar06b77222022-01-25 15:51:56 +0000792
793 set opfunc=
794 bwipe!
795 delete('Xdir', 'rf')
796 nunmap <F3>
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000797 &rtp = save_rtp
798enddef
799
Bram Moolenaare70dd112022-01-21 16:31:11 +0000800def Test_use_autoload_import_in_fold_expression()
801 mkdir('Xdir/autoload', 'p')
802 var save_rtp = &rtp
803 exe 'set rtp^=' .. getcwd() .. '/Xdir'
804
805 var lines =<< trim END
806 vim9script
807 export def Expr(): string
808 return getline(v:lnum) =~ '^#' ? '>1' : '1'
809 enddef
Bram Moolenaar9530b582022-01-22 13:39:08 +0000810 export def Text(): string
811 return 'fold text'
812 enddef
Bram Moolenaare70dd112022-01-21 16:31:11 +0000813 g:fold_loaded = 'yes'
814 END
815 writefile(lines, 'Xdir/autoload/fold.vim')
816
817 lines =<< trim END
818 vim9script
819 import autoload 'fold.vim'
820 &foldexpr = 'fold.Expr()'
Bram Moolenaar9530b582022-01-22 13:39:08 +0000821 &foldtext = 'fold.Text()'
Bram Moolenaare70dd112022-01-21 16:31:11 +0000822 &foldmethod = 'expr'
823 &debug = 'throw'
824 END
825 new
826 setline(1, ['# one', 'text', '# two', 'text'])
827 g:fold_loaded = 'no'
Bram Moolenaar62aec932022-01-29 21:45:34 +0000828 v9.CheckScriptSuccess(lines)
Bram Moolenaare70dd112022-01-21 16:31:11 +0000829 assert_equal('no', g:fold_loaded)
830 redraw
831 assert_equal('yes', g:fold_loaded)
832
833 # Check that script context of 'foldexpr' is copied to another buffer.
834 edit! otherfile
835 redraw
836
Bram Moolenaar9530b582022-01-22 13:39:08 +0000837 set foldexpr= foldtext& foldmethod& debug=
Bram Moolenaare70dd112022-01-21 16:31:11 +0000838 bwipe!
839 delete('Xdir', 'rf')
840 &rtp = save_rtp
841enddef
842
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000843func Test_import_in_diffexpr()
844 CheckExecutable diff
845
846 call Run_Test_import_in_diffexpr()
847endfunc
848
849def Run_Test_import_in_diffexpr()
850 var lines =<< trim END
851 vim9script
852
853 export def DiffExpr()
854 # Prepend some text to check diff type detection
855 writefile(['warning', ' message'], v:fname_out)
856 silent exe '!diff ' .. v:fname_in .. ' '
857 .. v:fname_new .. '>>' .. v:fname_out
858 enddef
859 END
860 writefile(lines, 'Xdiffexpr')
861
862 lines =<< trim END
863 vim9script
864 import './Xdiffexpr' as diff
865
866 set diffexpr=diff.DiffExpr()
867 set diffopt=foldcolumn:0
868 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000869 v9.CheckScriptSuccess(lines)
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000870
871 enew!
872 call setline(1, ['one', 'two', 'three'])
873 diffthis
874
875 botright vert new
876 call setline(1, ['one', 'two', 'three.'])
877 diffthis
878 # we only check if this does not cause errors
879 redraw
880
881 diffoff!
882 bwipe!
883 bwipe!
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +0000884 delete('Xdiffexpr')
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000885enddef
886
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000887def Test_import_in_patchexpr()
888 var lines =<< trim END
889 vim9script
890 export def TPatch()
891 call writefile(['output file'], v:fname_out)
892 enddef
893 END
894 writefile(lines, 'Xpatchexpr')
895
896 lines =<< trim END
897 vim9script
898 import './Xpatchexpr' as patch
899 set patchexpr=patch.TPatch()
900 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000901 v9.CheckScriptSuccess(lines)
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000902
903 call writefile(['input file'], 'Xinput')
904 call writefile(['diff file'], 'Xdiff')
905 :%bwipe!
906 edit Xinput
907 diffpatch Xdiff
908 call assert_equal('output file', getline(1))
909
910 call delete('Xinput')
911 call delete('Xdiff')
912 call delete('Xpatchexpr')
913 set patchexpr&
914 :%bwipe!
915enddef
916
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000917def Test_import_in_formatexpr()
918 var lines =<< trim END
919 vim9script
920 export def MyFormatExpr(): number
921 g:did_format = 'yes'
922 return 0
923 enddef
924 END
925 writefile(lines, 'Xformatter')
926
927 lines =<< trim END
928 vim9script
929 import './Xformatter' as format
930 set formatexpr=format.MyFormatExpr()
931 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000932 v9.CheckScriptSuccess(lines)
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000933
934 new
935 setline(1, ['a', 'b', 'c'])
936 normal gqG
937 assert_equal('yes', g:did_format)
938
939 bwipe!
940 delete('Xformatter')
941 unlet g:did_format
942 set formatexpr=
943enddef
944
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +0000945def Test_import_in_includeexpr()
946 writefile(['found it'], 'Xthisfile')
947 new
948
949 var lines =<< trim END
950 vim9script
951 export def DoSub(): string
952 return substitute(v:fname, 'that', 'this', '')
953 enddef
954 END
955 writefile(lines, 'Xinclude.vim')
956
957 lines =<< trim END
958 vim9script
959 import './Xinclude.vim'
960 set includeexpr=Xinclude.DoSub()
961 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000962 v9.CheckScriptSuccess(lines)
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +0000963
964 setline(1, ['Xthatfile'])
965 exe "normal \<C-W>f"
966 assert_equal('Xthisfile', expand('%'))
967
968 bwipe!
969 bwipe!
970 set includeexpr=
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +0000971 delete('Xinclude.vim')
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +0000972 delete('Xthisfile')
973enddef
974
Bram Moolenaar28e60cc2022-01-22 20:32:00 +0000975def Test_import_in_indentexpr()
976 var lines =<< trim END
977 vim9script
978 export def GetIndent(): number
979 return 5
980 enddef
981 END
982 writefile(lines, 'Xindenter')
983
984 lines =<< trim END
985 vim9script
986 import './Xindenter' as indent
987 set indentexpr=indent.GetIndent()
988 set debug=throw
989 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000990 v9.CheckScriptSuccess(lines)
Bram Moolenaar28e60cc2022-01-22 20:32:00 +0000991
992 new
993 setline(1, 'hello')
994 normal ==
995 assert_equal(' hello', getline(1))
996
997 bwipe!
998 set indentexpr= debug=
999 delete('Xindenter')
1000enddef
1001
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001002func Test_import_in_printexpr()
1003 CheckFeature postscript
1004 call Run_Test_import_in_printexpr()
1005endfunc
1006
1007def Run_Test_import_in_printexpr()
1008 var lines =<< trim END
1009 vim9script
1010 export def PrintFile(): bool
1011 g:printed = 'yes'
1012 delete('v:fname_in')
1013 return false
1014 enddef
1015 END
1016 writefile(lines, 'Xprint.vim')
1017
1018 lines =<< trim END
1019 vim9script
1020 import './Xprint.vim'
1021 set printexpr=Xprint.PrintFile()
1022 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001023 v9.CheckScriptSuccess(lines)
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001024
1025 help
1026 hardcopy dummy args
1027 assert_equal('yes', g:printed)
1028
1029 delete('Xprint.vim')
1030 set printexpr=
1031enddef
1032
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001033def Test_import_in_charconvert()
1034 var lines =<< trim END
1035 vim9script
1036 export def MakeUpper(): bool
1037 var data = readfile(v:fname_in)
1038 map(data, 'toupper(v:val)')
1039 writefile(data, v:fname_out)
1040 return false # success
1041 enddef
1042 END
1043 writefile(lines, 'Xconvert.vim')
1044
1045 lines =<< trim END
1046 vim9script
1047 import './Xconvert.vim' as conv
1048 set charconvert=conv.MakeUpper()
1049 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001050 v9.CheckScriptSuccess(lines)
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001051
1052 writefile(['one', 'two'], 'Xfile')
1053 new Xfile
1054 write ++enc=ucase Xfile1
1055 assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1056
1057 delete('Xfile')
1058 delete('Xfile1')
1059 delete('Xconvert.vim')
1060 bwipe!
1061 set charconvert&
1062enddef
1063
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001064func Test_import_in_spellsuggest_expr()
1065 CheckFeature spell
1066 call Run_Test_import_in_spellsuggest_expr()
1067endfunc
1068
1069def Run_Test_import_in_spellsuggest_expr()
1070 var lines =<< trim END
1071 vim9script
1072 export def MySuggest(): list<any>
1073 return [['Fox', 8], ['Fop', 9]]
1074 enddef
1075 END
1076 writefile(lines, 'Xsuggest.vim')
1077
1078 lines =<< trim END
1079 vim9script
1080 import './Xsuggest.vim' as sugg
1081 set spell spellsuggest=expr:sugg.MySuggest()
1082 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001083 v9.CheckScriptSuccess(lines)
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001084
1085 set verbose=1 # report errors
1086 call assert_equal(['Fox', 'Fop'], spellsuggest('Fo', 2))
1087
1088 delete('Xsuggest.vim')
1089 set nospell spellsuggest& verbose=0
1090enddef
1091
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001092def Test_export_shadows_global_function()
1093 mkdir('Xdir/autoload', 'p')
1094 var save_rtp = &rtp
1095 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1096
1097 var lines =<< trim END
1098 vim9script
1099 export def Shadow(): string
1100 return 'Shadow()'
1101 enddef
1102 END
1103 writefile(lines, 'Xdir/autoload/shadow.vim')
1104
1105 lines =<< trim END
1106 vim9script
1107
1108 def g:Shadow(): string
1109 return 'global'
1110 enddef
1111
1112 import autoload 'shadow.vim'
1113 assert_equal('Shadow()', shadow.Shadow())
1114 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001115 v9.CheckScriptSuccess(lines)
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001116
1117 delfunc g:Shadow
1118 bwipe!
1119 delete('Xdir', 'rf')
1120 &rtp = save_rtp
1121enddef
1122
Bram Moolenaard8448622022-01-07 21:39:52 +00001123def Test_export_fails()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001124 v9.CheckScriptFailure(['export var some = 123'], 'E1042:')
1125 v9.CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
1126 v9.CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001127
1128 assert_fails('export something', 'E1043:')
1129enddef
1130
1131func Test_import_fails_without_script()
1132 CheckRunVimInTerminal
1133
1134 " call indirectly to avoid compilation error for missing functions
1135 call Run_Test_import_fails_on_command_line()
1136endfunc
1137
1138def Run_Test_import_fails_on_command_line()
1139 var export =<< trim END
1140 vim9script
1141 export def Foo(): number
1142 return 0
1143 enddef
1144 END
1145 writefile(export, 'XexportCmd.vim')
1146
Bram Moolenaar62aec932022-01-29 21:45:34 +00001147 var buf = g:RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaard8448622022-01-07 21:39:52 +00001148 rows: 6, wait_for_ruler: 0})
Bram Moolenaar62aec932022-01-29 21:45:34 +00001149 g:WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaard8448622022-01-07 21:39:52 +00001150
1151 delete('XexportCmd.vim')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001152 g:StopVimInTerminal(buf)
Bram Moolenaard8448622022-01-07 21:39:52 +00001153enddef
1154
1155def Test_vim9_reload_noclear()
1156 var lines =<< trim END
1157 vim9script
1158 export var exported = 'thexport'
1159
1160 export def TheFunc(x = 0)
1161 enddef
1162 END
1163 writefile(lines, 'XExportReload')
1164 lines =<< trim END
1165 vim9script noclear
1166 g:loadCount += 1
Bram Moolenaara749a422022-02-12 19:52:25 +00001167 var reloaded = 'init'
Bram Moolenaard8448622022-01-07 21:39:52 +00001168 import './XExportReload' as exp
1169
1170 def Again(): string
1171 return 'again'
1172 enddef
1173
1174 exp.TheFunc()
1175
Bram Moolenaara749a422022-02-12 19:52:25 +00001176 if exists('loaded') | finish | endif
1177 var loaded = true
Bram Moolenaard8448622022-01-07 21:39:52 +00001178
Bram Moolenaara749a422022-02-12 19:52:25 +00001179 var notReloaded = 'yes'
1180 reloaded = 'first'
Bram Moolenaard8448622022-01-07 21:39:52 +00001181 def g:Values(): list<string>
Bram Moolenaara749a422022-02-12 19:52:25 +00001182 return [reloaded, notReloaded, Again(), Once(), exp.exported]
Bram Moolenaard8448622022-01-07 21:39:52 +00001183 enddef
1184
1185 def Once(): string
1186 return 'once'
1187 enddef
1188 END
1189 writefile(lines, 'XReloaded')
1190 g:loadCount = 0
1191 source XReloaded
1192 assert_equal(1, g:loadCount)
1193 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
1194 source XReloaded
1195 assert_equal(2, g:loadCount)
1196 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1197 source XReloaded
1198 assert_equal(3, g:loadCount)
1199 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1200
1201 delete('XReloaded')
1202 delete('XExportReload')
1203 delfunc g:Values
1204 unlet g:loadCount
1205
1206 lines =<< trim END
1207 vim9script
1208 def Inner()
1209 enddef
1210 END
1211 lines->writefile('XreloadScript.vim')
1212 source XreloadScript.vim
1213
1214 lines =<< trim END
1215 vim9script
1216 def Outer()
1217 def Inner()
1218 enddef
1219 enddef
1220 defcompile
1221 END
1222 lines->writefile('XreloadScript.vim')
1223 source XreloadScript.vim
1224
1225 delete('XreloadScript.vim')
1226enddef
1227
Bram Moolenaarcd1cda22022-02-16 21:48:25 +00001228def Test_vim_reload_noclear_arg_count()
1229 var lines =<< trim END
1230 vim9script noclear
1231
1232 if !exists('g:didload')
1233 def Test(a: string, b: string)
1234 echo a b
1235 enddef
1236 def Call()
1237 Test('a', 'b')
1238 enddef
1239 else
1240 # redefine with one argument less
1241 def Test(a: string)
1242 echo a
1243 enddef
1244 endif
1245 Call()
1246 g:didload = 1
1247 END
1248 lines->writefile('XreloadScript_1.vim')
1249 source XreloadScript_1.vim
1250 assert_fails('source XreloadScript_1.vim', 'E1106: One argument too many')
1251 unlet g:didload
1252
1253 lines =<< trim END
1254 vim9script noclear
1255
1256 if !exists('g:didload')
1257 def Test(a: string, b: string, c: string)
1258 echo a b
1259 enddef
1260 def Call()
1261 Test('a', 'b', 'c')
1262 enddef
1263 else
1264 # redefine with one argument less
1265 def Test(a: string)
1266 echo a
1267 enddef
1268 endif
1269 Call()
1270 g:didload = 1
1271 END
1272 lines->writefile('XreloadScript_2.vim')
1273 source XreloadScript_2.vim
1274 assert_fails('source XreloadScript_2.vim', 'E1106: 2 arguments too many')
1275 unlet g:didload
1276
1277 lines =<< trim END
1278 vim9script noclear
1279
1280 if !exists('g:didload')
1281 def Test(a: string)
1282 echo a
1283 enddef
1284 def Call()
1285 Test('a')
1286 enddef
1287 else
1288 # redefine with one argument extra
1289 def Test(a: string, b: string)
1290 echo a b
1291 enddef
1292 endif
1293 Call()
1294 g:didload = 1
1295 END
1296 lines->writefile('XreloadScript_3.vim')
1297 source XreloadScript_3.vim
1298 assert_fails('source XreloadScript_3.vim', 'E1190: One argument too few')
1299 unlet g:didload
1300
1301 lines =<< trim END
1302 vim9script noclear
1303
1304 if !exists('g:didload')
1305 def Test(a: string)
1306 echo a
1307 enddef
1308 def Call()
1309 Test('a')
1310 enddef
1311 else
1312 # redefine with two arguments extra
1313 def Test(a: string, b: string, c: string)
1314 echo a b
1315 enddef
1316 endif
1317 Call()
1318 g:didload = 1
1319 END
1320 lines->writefile('XreloadScript_4.vim')
1321 source XreloadScript_4.vim
1322 assert_fails('source XreloadScript_4.vim', 'E1190: 2 arguments too few')
1323 unlet g:didload
1324
1325 delete('XreloadScript_1.vim')
1326 delete('XreloadScript_2.vim')
1327 delete('XreloadScript_3.vim')
1328 delete('XreloadScript_4.vim')
1329enddef
1330
1331def Test_vim9_reload_noclear_error()
1332 var lines =<< trim END
1333 vim9script noclear
1334
1335 if !exists('g:didload')
1336 def Test(a: string)
1337 echo a
1338 enddef
1339 def Call()
1340 Test('a')
1341 enddef
1342 else
1343 # redefine with a compile error
1344 def Test(a: string)
1345 echo ax
1346 enddef
1347 endif
1348 Call()
1349 g:didload = 1
1350 END
1351 lines->writefile('XreloadScriptErr.vim')
1352 source XreloadScriptErr.vim
1353 assert_fails('source XreloadScriptErr.vim', 'E1001: Variable not found: ax')
1354
1355 unlet g:didload
1356 delete('XreloadScriptErr.vim')
1357enddef
1358
Bram Moolenaard8448622022-01-07 21:39:52 +00001359def Test_vim9_reload_import()
1360 var lines =<< trim END
1361 vim9script
1362 const var = ''
1363 var valone = 1234
1364 def MyFunc(arg: string)
1365 valone = 5678
1366 enddef
1367 END
1368 var morelines =<< trim END
1369 var valtwo = 222
1370 export def GetValtwo(): number
1371 return valtwo
1372 enddef
1373 END
1374 writefile(lines + morelines, 'Xreload.vim')
1375 source Xreload.vim
1376 source Xreload.vim
1377 source Xreload.vim
1378
1379 # cannot declare a var twice
1380 lines =<< trim END
1381 vim9script
1382 var valone = 1234
1383 var valone = 5678
1384 END
1385 writefile(lines, 'Xreload.vim')
1386 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
1387
1388 delete('Xreload.vim')
1389 delete('Ximport.vim')
1390enddef
1391
1392" if a script is reloaded with a script-local variable that changed its type, a
1393" compiled function using that variable must fail.
1394def Test_script_reload_change_type()
1395 var lines =<< trim END
1396 vim9script noclear
1397 var str = 'string'
1398 def g:GetStr(): string
1399 return str .. 'xxx'
1400 enddef
1401 END
1402 writefile(lines, 'Xreload.vim')
1403 source Xreload.vim
1404 echo g:GetStr()
1405
1406 lines =<< trim END
1407 vim9script noclear
1408 var str = 1234
1409 END
1410 writefile(lines, 'Xreload.vim')
1411 source Xreload.vim
1412 assert_fails('echo g:GetStr()', 'E1150:')
1413
1414 delfunc g:GetStr
1415 delete('Xreload.vim')
1416enddef
1417
1418" Define CallFunc so that the test can be compiled
1419command CallFunc echo 'nop'
1420
1421def Test_script_reload_from_function()
1422 var lines =<< trim END
1423 vim9script
1424
1425 if exists('g:loaded')
1426 finish
1427 endif
1428 g:loaded = 1
1429 delcommand CallFunc
1430 command CallFunc Func()
1431 def Func()
1432 so XreloadFunc.vim
1433 g:didTheFunc = 1
1434 enddef
1435 END
1436 writefile(lines, 'XreloadFunc.vim')
1437 source XreloadFunc.vim
1438 CallFunc
1439 assert_equal(1, g:didTheFunc)
1440
1441 delete('XreloadFunc.vim')
1442 delcommand CallFunc
1443 unlet g:loaded
1444 unlet g:didTheFunc
1445enddef
1446
1447def s:RetSome(): string
1448 return 'some'
1449enddef
1450
1451" Not exported function that is referenced needs to be accessed by the
1452" script-local name.
1453def Test_vim9_funcref()
1454 var sortlines =<< trim END
1455 vim9script
1456 def Compare(i1: number, i2: number): number
1457 return i2 - i1
1458 enddef
1459
1460 export def FastSort(): list<number>
1461 return range(5)->sort(Compare)
1462 enddef
1463
1464 export def GetString(arg: string): string
1465 return arg
1466 enddef
1467 END
1468 writefile(sortlines, 'Xsort.vim')
1469
1470 var lines =<< trim END
1471 vim9script
1472 import './Xsort.vim'
1473 def Test()
1474 g:result = Xsort.FastSort()
1475 enddef
1476 Test()
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001477 END
1478 writefile(lines, 'Xscript.vim')
1479 source Xscript.vim
1480 assert_equal([4, 3, 2, 1, 0], g:result)
1481 unlet g:result
Bram Moolenaard8448622022-01-07 21:39:52 +00001482
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001483 lines =<< trim END
1484 vim9script
Bram Moolenaard8448622022-01-07 21:39:52 +00001485 # using a function imported with "as"
1486 import './Xsort.vim' as anAlias
1487 assert_equal('yes', anAlias.GetString('yes'))
1488
1489 # using the function from a compiled function
1490 def TestMore(): string
1491 var s = s:anAlias.GetString('foo')
1492 return s .. anAlias.GetString('bar')
1493 enddef
1494 assert_equal('foobar', TestMore())
1495
1496 # error when using a function that isn't exported
1497 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
1498 END
1499 writefile(lines, 'Xscript.vim')
1500
Bram Moolenaard8448622022-01-07 21:39:52 +00001501 delete('Xsort.vim')
1502 delete('Xscript.vim')
1503
1504 var Funcref = function('s:RetSome')
1505 assert_equal('some', Funcref())
1506enddef
1507
1508" Check that when searching for "FilterFunc" it finds the import in the
1509" script where FastFilter() is called from, both as a string and as a direct
1510" function reference.
1511def Test_vim9_funcref_other_script()
1512 var filterLines =<< trim END
1513 vim9script
1514 export def FilterFunc(idx: number, val: number): bool
1515 return idx % 2 == 1
1516 enddef
1517 export def FastFilter(): list<number>
1518 return range(10)->filter('FilterFunc(v:key, v:val)')
1519 enddef
1520 export def FastFilterDirect(): list<number>
1521 return range(10)->filter(FilterFunc)
1522 enddef
1523 END
1524 writefile(filterLines, 'Xfilter.vim')
1525
1526 var lines =<< trim END
1527 vim9script
1528 import './Xfilter.vim' as filter
1529 def Test()
1530 var x: list<number> = filter.FastFilter()
1531 enddef
1532 Test()
1533 def TestDirect()
1534 var x: list<number> = filter.FastFilterDirect()
1535 enddef
1536 TestDirect()
1537 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001538 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +00001539 delete('Xfilter.vim')
1540enddef
1541
1542def Test_import_absolute()
1543 var import_lines = [
1544 'vim9script',
1545 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
1546 'def UseExported()',
1547 ' g:imported_abs = abs.exported',
1548 ' abs.exported = 8888',
1549 ' g:imported_after = abs.exported',
1550 'enddef',
1551 'UseExported()',
1552 'g:import_disassembled = execute("disass UseExported")',
1553 ]
1554 writefile(import_lines, 'Ximport_abs.vim')
1555 writefile(s:export_script_lines, 'Xexport_abs.vim')
1556
1557 source Ximport_abs.vim
1558
1559 assert_equal(9876, g:imported_abs)
1560 assert_equal(8888, g:imported_after)
1561 assert_match('<SNR>\d\+_UseExported\_s*' ..
1562 'g:imported_abs = abs.exported\_s*' ..
1563 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1564 '1 STOREG g:imported_abs\_s*' ..
1565 'abs.exported = 8888\_s*' ..
1566 '2 PUSHNR 8888\_s*' ..
1567 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1568 'g:imported_after = abs.exported\_s*' ..
1569 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1570 '5 STOREG g:imported_after',
1571 g:import_disassembled)
1572
1573 Undo_export_script_lines()
1574 unlet g:imported_abs
1575 unlet g:import_disassembled
1576
1577 delete('Ximport_abs.vim')
1578 delete('Xexport_abs.vim')
1579enddef
1580
1581def Test_import_rtp()
1582 var import_lines = [
1583 'vim9script',
1584 'import "Xexport_rtp.vim" as rtp',
1585 'g:imported_rtp = rtp.exported',
1586 ]
1587 writefile(import_lines, 'Ximport_rtp.vim')
1588 mkdir('import', 'p')
1589 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1590
1591 var save_rtp = &rtp
1592 &rtp = getcwd()
1593 source Ximport_rtp.vim
1594 &rtp = save_rtp
1595
1596 assert_equal(9876, g:imported_rtp)
1597
1598 Undo_export_script_lines()
1599 unlet g:imported_rtp
1600 delete('Ximport_rtp.vim')
1601 delete('import', 'rf')
1602enddef
1603
1604def Test_import_compile_error()
1605 var export_lines = [
1606 'vim9script',
1607 'export def ExpFunc(): string',
1608 ' return notDefined',
1609 'enddef',
1610 ]
1611 writefile(export_lines, 'Xexported.vim')
1612
1613 var import_lines = [
1614 'vim9script',
1615 'import "./Xexported.vim" as expo',
1616 'def ImpFunc()',
1617 ' echo expo.ExpFunc()',
1618 'enddef',
1619 'defcompile',
1620 ]
1621 writefile(import_lines, 'Ximport.vim')
1622
1623 try
1624 source Ximport.vim
1625 catch /E1001/
1626 # Error should be before the Xexported.vim file.
1627 assert_match('E1001: Variable not found: notDefined', v:exception)
1628 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1629 endtry
1630
1631 delete('Xexported.vim')
1632 delete('Ximport.vim')
1633enddef
1634
1635def Test_func_overrules_import_fails()
1636 var export_lines =<< trim END
1637 vim9script
1638 export def Func()
1639 echo 'imported'
1640 enddef
1641 END
1642 writefile(export_lines, 'XexportedFunc.vim')
1643
1644 var lines =<< trim END
1645 vim9script
1646 import './XexportedFunc.vim' as Func
1647 def Func()
1648 echo 'local to function'
1649 enddef
1650 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001651 v9.CheckScriptFailure(lines, 'E1213: Redefining imported item "Func"')
Bram Moolenaard8448622022-01-07 21:39:52 +00001652
1653 lines =<< trim END
1654 vim9script
1655 import './XexportedFunc.vim' as Func
1656 def Outer()
1657 def Func()
1658 echo 'local to function'
1659 enddef
1660 enddef
1661 defcompile
1662 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001663 v9.CheckScriptFailure(lines, 'E1236:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001664
1665 delete('XexportedFunc.vim')
1666enddef
1667
1668def Test_source_vim9_from_legacy()
1669 var vim9_lines =<< trim END
1670 vim9script
1671 var local = 'local'
1672 g:global = 'global'
1673 export var exported = 'exported'
1674 export def GetText(): string
1675 return 'text'
1676 enddef
1677 END
1678 writefile(vim9_lines, 'Xvim9_script.vim')
1679
1680 var legacy_lines =<< trim END
1681 source Xvim9_script.vim
1682
1683 call assert_false(exists('local'))
1684 call assert_false(exists('exported'))
1685 call assert_false(exists('s:exported'))
1686 call assert_equal('global', global)
1687 call assert_equal('global', g:global)
Bram Moolenaard8448622022-01-07 21:39:52 +00001688 END
1689 writefile(legacy_lines, 'Xlegacy_script.vim')
1690
1691 source Xlegacy_script.vim
1692 assert_equal('global', g:global)
1693 unlet g:global
1694
1695 delete('Xlegacy_script.vim')
1696 delete('Xvim9_script.vim')
1697enddef
1698
Bram Moolenaarc43e6232022-01-13 20:51:56 +00001699def Test_import_vim9_from_legacy()
1700 var vim9_lines =<< trim END
1701 vim9script
1702 var local = 'local'
1703 g:global = 'global'
1704 export var exported = 'exported'
1705 export def GetText(): string
1706 return 'text'
1707 enddef
1708 END
1709 writefile(vim9_lines, 'Xvim9_export.vim')
1710
1711 var legacy_lines =<< trim END
1712 import './Xvim9_export.vim' as vim9
1713
1714 call assert_false(exists('vim9'))
1715 call assert_false(exists('local'))
1716 call assert_false(exists('s:vim9.local'))
1717 call assert_equal('global', global)
1718 call assert_equal('global', g:global)
1719 call assert_false(exists('exported'))
1720 call assert_false(exists('s:exported'))
1721 call assert_false(exists('*GetText'))
1722
1723 " imported symbol is script-local
1724 call assert_equal('exported', s:vim9.exported)
1725 call assert_equal('text', s:vim9.GetText())
1726 END
1727 writefile(legacy_lines, 'Xlegacy_script.vim')
1728
1729 source Xlegacy_script.vim
1730 assert_equal('global', g:global)
1731 unlet g:global
1732
1733 delete('Xlegacy_script.vim')
1734 delete('Xvim9_export.vim')
1735enddef
1736
Bram Moolenaard8448622022-01-07 21:39:52 +00001737def Test_cmdline_win()
1738 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
1739 # the command line window.
1740 mkdir('rtp/syntax', 'p')
1741 var export_lines =<< trim END
1742 vim9script
1743 export var That = 'yes'
1744 END
1745 writefile(export_lines, 'rtp/syntax/Xexport.vim')
1746 var import_lines =<< trim END
1747 vim9script
1748 import './Xexport.vim' as exp
1749 echo exp.That
1750 END
1751 writefile(import_lines, 'rtp/syntax/vim.vim')
1752 var save_rtp = &rtp
1753 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
1754 syntax on
1755 augroup CmdWin
1756 autocmd CmdwinEnter * g:got_there = 'yes'
1757 augroup END
1758 # this will open and also close the cmdline window
1759 feedkeys('q:', 'xt')
1760 assert_equal('yes', g:got_there)
1761
1762 augroup CmdWin
1763 au!
1764 augroup END
1765 &rtp = save_rtp
1766 delete('rtp', 'rf')
1767enddef
1768
1769def Test_import_gone_when_sourced_twice()
1770 var exportlines =<< trim END
1771 vim9script
1772 if exists('g:guard')
1773 finish
1774 endif
1775 g:guard = 1
1776 export var name = 'someName'
1777 END
1778 writefile(exportlines, 'XexportScript.vim')
1779
1780 var lines =<< trim END
1781 vim9script
1782 import './XexportScript.vim' as expo
1783 def g:GetName(): string
1784 return expo.name
1785 enddef
1786 END
1787 writefile(lines, 'XscriptImport.vim')
1788 so XscriptImport.vim
1789 assert_equal('someName', g:GetName())
1790
1791 so XexportScript.vim
1792 assert_fails('call g:GetName()', 'E1149:')
1793
1794 delfunc g:GetName
1795 delete('XexportScript.vim')
1796 delete('XscriptImport.vim')
1797 unlet g:guard
1798enddef
1799
Bram Moolenaar160aa862022-01-10 21:29:57 +00001800" test using an auto-loaded function and variable
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001801def Test_vim9_autoload_full_name()
Bram Moolenaar160aa862022-01-10 21:29:57 +00001802 var lines =<< trim END
1803 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001804 export def Gettest(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00001805 return 'test'
1806 enddef
1807 g:some#name = 'name'
1808 g:some#dict = {key: 'value'}
1809
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001810 export def Varargs(a1: string, ...l: list<string>): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00001811 return a1 .. l[0] .. l[1]
1812 enddef
1813 END
1814
1815 mkdir('Xdir/autoload', 'p')
1816 writefile(lines, 'Xdir/autoload/some.vim')
1817 var save_rtp = &rtp
1818 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1819
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001820 assert_equal('test', g:some#Gettest())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001821 assert_equal('name', g:some#name)
1822 assert_equal('value', g:some#dict.key)
1823 g:some#other = 'other'
1824 assert_equal('other', g:some#other)
1825
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001826 assert_equal('abc', some#Varargs('a', 'b', 'c'))
Bram Moolenaar160aa862022-01-10 21:29:57 +00001827
1828 # upper case script name works
1829 lines =<< trim END
1830 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001831 export def GetOther(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00001832 return 'other'
1833 enddef
1834 END
1835 writefile(lines, 'Xdir/autoload/Other.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001836 assert_equal('other', g:Other#GetOther())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001837
1838 delete('Xdir', 'rf')
1839 &rtp = save_rtp
1840enddef
1841
1842def Test_vim9script_autoload()
1843 mkdir('Xdir/autoload', 'p')
1844 var save_rtp = &rtp
1845 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1846
Bram Moolenaarfd218c82022-01-18 16:26:24 +00001847 # when the path has "/autoload/" prefix is not needed
Bram Moolenaar160aa862022-01-10 21:29:57 +00001848 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00001849 vim9script
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001850 g:prefixed_loaded += 1
Bram Moolenaar160aa862022-01-10 21:29:57 +00001851
1852 export def Gettest(): string
1853 return 'test'
1854 enddef
1855
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001856 export var name = 'name'
1857
1858 export func GetFunc()
1859 return Gettest() .. 'more' .. s:name
Bram Moolenaar160aa862022-01-10 21:29:57 +00001860 endfunc
1861
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001862 export def GetDef(): string
1863 return Gettest() .. 'more' .. name
1864 enddef
1865
Bram Moolenaar160aa862022-01-10 21:29:57 +00001866 export final fname = 'final'
1867 export const cname = 'const'
1868 END
1869 writefile(lines, 'Xdir/autoload/prefixed.vim')
1870
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001871 g:prefixed_loaded = 0
1872 g:expected_loaded = 0
Bram Moolenaar160aa862022-01-10 21:29:57 +00001873 lines =<< trim END
1874 vim9script
1875 import autoload 'prefixed.vim'
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001876 assert_equal(g:expected_loaded, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00001877 assert_equal('test', prefixed.Gettest())
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001878 assert_equal(1, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00001879
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001880 assert_equal('testmorename', prefixed.GetFunc())
1881 assert_equal('testmorename', prefixed.GetDef())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001882 assert_equal('name', prefixed.name)
1883 assert_equal('final', prefixed.fname)
1884 assert_equal('const', prefixed.cname)
1885 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001886 v9.CheckScriptSuccess(lines)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001887 # can source it again, autoload script not loaded again
1888 g:expected_loaded = 1
Bram Moolenaar62aec932022-01-29 21:45:34 +00001889 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00001890
1891 # can also get the items by autoload name
1892 lines =<< trim END
1893 call assert_equal('test', prefixed#Gettest())
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001894 call assert_equal('testmorename', prefixed#GetFunc())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001895 call assert_equal('name', prefixed#name)
1896 call assert_equal('final', prefixed#fname)
1897 call assert_equal('const', prefixed#cname)
1898 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001899 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00001900
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001901 unlet g:prefixed_loaded
1902 unlet g:expected_loaded
1903 delete('Xdir', 'rf')
1904 &rtp = save_rtp
1905enddef
1906
Bram Moolenaard02dce22022-01-18 17:43:04 +00001907def Test_import_autoload_not_exported()
1908 mkdir('Xdir/autoload', 'p')
1909 var save_rtp = &rtp
1910 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1911
1912 # error when using an item that is not exported from an autoload script
1913 var exportLines =<< trim END
1914 vim9script
1915 var notExported = 123
1916 def NotExport()
1917 echo 'nop'
1918 enddef
1919 END
1920 writefile(exportLines, 'Xdir/autoload/notExport1.vim')
1921
1922 var lines =<< trim END
1923 vim9script
1924 import autoload 'notExport1.vim'
1925 echo notExport1.notFound
1926 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001927 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001928
1929 lines =<< trim END
1930 vim9script
1931 import autoload 'notExport1.vim'
1932 echo notExport1.notExported
1933 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001934 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001935
1936 lines =<< trim END
1937 vim9script
1938 import autoload 'notExport1.vim'
1939 echo notExport1.NotFunc()
1940 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001941 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001942
1943 lines =<< trim END
1944 vim9script
1945 import autoload 'notExport1.vim'
1946 echo notExport1.NotExport()
1947 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001948 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001949
1950 lines =<< trim END
1951 vim9script
1952 import autoload 'notExport1.vim'
1953 echo 'text'->notExport1.NotFunc()
1954 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001955 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001956
1957 lines =<< trim END
1958 vim9script
1959 import autoload 'notExport1.vim'
1960 echo 'text'->notExport1.NotExport()
1961 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001962 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001963
1964 # using a :def function we use a different autoload script every time so that
1965 # the function is compiled without the script loaded
1966 writefile(exportLines, 'Xdir/autoload/notExport2.vim')
1967 lines =<< trim END
1968 vim9script
1969 import autoload 'notExport2.vim'
1970 def Testit()
1971 echo notExport2.notFound
1972 enddef
1973 Testit()
1974 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001975 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport2#notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001976
1977 writefile(exportLines, 'Xdir/autoload/notExport3.vim')
1978 lines =<< trim END
1979 vim9script
1980 import autoload 'notExport3.vim'
1981 def Testit()
1982 echo notExport3.notExported
1983 enddef
1984 Testit()
1985 END
1986 # don't get E1049 because it is too complicated to figure out
Bram Moolenaar62aec932022-01-29 21:45:34 +00001987 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport3#notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001988
1989 writefile(exportLines, 'Xdir/autoload/notExport4.vim')
1990 lines =<< trim END
1991 vim9script
1992 import autoload 'notExport4.vim'
1993 def Testit()
1994 echo notExport4.NotFunc()
1995 enddef
1996 Testit()
1997 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001998 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport4#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001999
2000 writefile(exportLines, 'Xdir/autoload/notExport5.vim')
2001 lines =<< trim END
2002 vim9script
2003 import autoload 'notExport5.vim'
2004 def Testit()
2005 echo notExport5.NotExport()
2006 enddef
2007 Testit()
2008 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002009 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport5#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002010
2011 writefile(exportLines, 'Xdir/autoload/notExport6.vim')
2012 lines =<< trim END
2013 vim9script
2014 import autoload 'notExport6.vim'
2015 def Testit()
2016 echo 'text'->notExport6.NotFunc()
2017 enddef
2018 Testit()
2019 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002020 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport6#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002021
2022 writefile(exportLines, 'Xdir/autoload/notExport7.vim')
2023 lines =<< trim END
2024 vim9script
2025 import autoload 'notExport7.vim'
2026 def Testit()
2027 echo 'text'->notExport7.NotExport()
2028 enddef
2029 Testit()
2030 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002031 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport7#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002032
2033 delete('Xdir', 'rf')
2034 &rtp = save_rtp
2035enddef
2036
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002037def Test_vim9script_autoload_call()
2038 mkdir('Xdir/autoload', 'p')
2039 var save_rtp = &rtp
2040 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2041
2042 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002043 vim9script
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002044
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002045 export def RetArg(arg: string): string
2046 return arg
2047 enddef
2048
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002049 export def Getother()
2050 g:result = 'other'
2051 enddef
2052 END
Bram Moolenaar5d982692022-01-12 15:15:27 +00002053 writefile(lines, 'Xdir/autoload/another.vim')
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002054
2055 lines =<< trim END
2056 vim9script
Bram Moolenaar5d982692022-01-12 15:15:27 +00002057 import autoload 'another.vim'
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002058
2059 # compile this before 'another.vim' is loaded
2060 def CallAnother()
2061 assert_equal('foo', 'foo'->another.RetArg())
2062 enddef
2063 CallAnother()
2064
Bram Moolenaar5d982692022-01-12 15:15:27 +00002065 call another.Getother()
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002066 assert_equal('other', g:result)
Bram Moolenaar3d8e25a2022-01-22 11:00:02 +00002067
2068 assert_equal('arg', call('another.RetArg', ['arg']))
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00002069
2070 verbose function another.Getother
2071 # should we disallow this?
2072 verbose function another#Getother
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002073 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002074 v9.CheckScriptSuccess(lines)
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002075
2076 unlet g:result
Bram Moolenaar160aa862022-01-10 21:29:57 +00002077 delete('Xdir', 'rf')
2078 &rtp = save_rtp
2079enddef
2080
Bram Moolenaarb697dc22022-01-22 11:27:29 +00002081def Test_vim9script_noclear_autoload()
2082 mkdir('Xdir/autoload', 'p')
2083 var save_rtp = &rtp
2084 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2085
2086 var lines =<< trim END
2087 vim9script
2088 export def Func(): string
2089 return 'called'
2090 enddef
2091 g:double_loaded = 'yes'
2092 END
2093 writefile(lines, 'Xdir/autoload/double.vim')
2094
2095 lines =<< trim END
2096 vim9script noclear
2097 if exists('g:script_loaded')
2098 finish
2099 endif
2100 g:script_loaded = true
2101
2102 import autoload 'double.vim'
2103 nnoremap <F3> <ScriptCmd>g:result = double.Func()<CR>
2104 END
2105 g:double_loaded = 'no'
2106 writefile(lines, 'Xloaddouble')
2107 source Xloaddouble
2108 assert_equal('no', g:double_loaded)
2109 assert_equal(true, g:script_loaded)
2110 source Xloaddouble
2111 feedkeys("\<F3>", 'xt')
2112 assert_equal('called', g:result)
2113 assert_equal('yes', g:double_loaded)
2114
2115 delete('Xloaddouble')
2116 unlet g:double_loaded
2117 unlet g:script_loaded
2118 unlet g:result
2119 delete('Xdir', 'rf')
2120 &rtp = save_rtp
2121enddef
2122
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002123def Test_vim9script_autoload_duplicate()
2124 mkdir('Xdir/autoload', 'p')
2125
2126 var lines =<< trim END
2127 vim9script
2128
2129 export def Func()
2130 enddef
2131
2132 def Func()
2133 enddef
2134 END
2135 writefile(lines, 'Xdir/autoload/dupfunc.vim')
2136 assert_fails('source Xdir/autoload/dupfunc.vim', 'E1073:')
2137
2138 lines =<< trim END
2139 vim9script
2140
2141 def Func()
2142 enddef
2143
2144 export def Func()
2145 enddef
2146 END
2147 writefile(lines, 'Xdir/autoload/dup2func.vim')
2148 assert_fails('source Xdir/autoload/dup2func.vim', 'E1073:')
2149
2150 lines =<< trim END
2151 vim9script
2152
2153 def Func()
2154 enddef
2155
2156 export var Func = 'asdf'
2157 END
2158 writefile(lines, 'Xdir/autoload/dup3func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002159 assert_fails('source Xdir/autoload/dup3func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002160
2161 lines =<< trim END
2162 vim9script
2163
2164 export var Func = 'asdf'
2165
2166 def Func()
2167 enddef
2168 END
2169 writefile(lines, 'Xdir/autoload/dup4func.vim')
2170 assert_fails('source Xdir/autoload/dup4func.vim', 'E707:')
2171
2172 lines =<< trim END
2173 vim9script
2174
2175 var Func = 'asdf'
2176
2177 export def Func()
2178 enddef
2179 END
2180 writefile(lines, 'Xdir/autoload/dup5func.vim')
2181 assert_fails('source Xdir/autoload/dup5func.vim', 'E707:')
2182
2183 lines =<< trim END
2184 vim9script
2185
2186 export def Func()
2187 enddef
2188
2189 var Func = 'asdf'
2190 END
2191 writefile(lines, 'Xdir/autoload/dup6func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002192 assert_fails('source Xdir/autoload/dup6func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002193
2194 delete('Xdir', 'rf')
2195enddef
2196
Bram Moolenaar2017d6f2022-01-20 19:38:46 +00002197def Test_autoload_missing_function_name()
2198 mkdir('Xdir/autoload', 'p')
2199
2200 var lines =<< trim END
2201 vim9script
2202
2203 def loadme#()
2204 enddef
2205 END
2206 writefile(lines, 'Xdir/autoload/loadme.vim')
2207 assert_fails('source Xdir/autoload/loadme.vim', 'E129:')
2208
2209 delete('Xdir', 'rf')
2210enddef
2211
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002212def Test_autoload_name_wrong()
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002213 var lines =<< trim END
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002214 def Xscriptname#Func()
2215 enddef
2216 END
2217 writefile(lines, 'Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002218 v9.CheckScriptFailure(lines, 'E746:')
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00002219 delete('Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002220
2221 mkdir('Xdir/autoload', 'p')
2222 lines =<< trim END
2223 vim9script
2224 def somescript#Func()
2225 enddef
2226 END
2227 writefile(lines, 'Xdir/autoload/somescript.vim')
2228 assert_fails('source Xdir/autoload/somescript.vim', 'E1263:')
2229
2230 delete('Xdir', 'rf')
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002231enddef
2232
Bram Moolenaard041f422022-01-12 19:54:00 +00002233def Test_import_autoload_postponed()
2234 mkdir('Xdir/autoload', 'p')
2235 var save_rtp = &rtp
2236 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2237
2238 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002239 vim9script
Bram Moolenaard041f422022-01-12 19:54:00 +00002240
2241 g:loaded_postponed = 'true'
2242 export var variable = 'bla'
2243 export def Function(): string
2244 return 'bla'
2245 enddef
2246 END
2247 writefile(lines, 'Xdir/autoload/postponed.vim')
2248
2249 lines =<< trim END
2250 vim9script
2251
2252 import autoload 'postponed.vim'
2253 def Tryit()
2254 echo postponed.variable
2255 echo postponed.Function()
2256 enddef
2257 defcompile
2258 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002259 v9.CheckScriptSuccess(lines)
Bram Moolenaard041f422022-01-12 19:54:00 +00002260 assert_false(exists('g:loaded_postponed'))
Bram Moolenaar62aec932022-01-29 21:45:34 +00002261 v9.CheckScriptSuccess(lines + ['Tryit()'])
Bram Moolenaard041f422022-01-12 19:54:00 +00002262 assert_equal('true', g:loaded_postponed)
2263
2264 unlet g:loaded_postponed
2265 delete('Xdir', 'rf')
2266 &rtp = save_rtp
2267enddef
2268
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002269def Test_import_autoload_override()
2270 mkdir('Xdir/autoload', 'p')
2271 var save_rtp = &rtp
2272 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2273 test_override('autoload', 1)
2274
2275 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002276 vim9script
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002277
2278 g:loaded_override = 'true'
2279 export var variable = 'bla'
2280 export def Function(): string
2281 return 'bla'
2282 enddef
2283 END
2284 writefile(lines, 'Xdir/autoload/override.vim')
2285
2286 lines =<< trim END
2287 vim9script
2288
2289 import autoload 'override.vim'
2290 assert_equal('true', g:loaded_override)
2291
2292 def Tryit()
2293 echo override.doesNotExist
2294 enddef
2295 defcompile
2296 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002297 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1)
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002298
2299 test_override('autoload', 0)
2300 unlet g:loaded_override
2301 delete('Xdir', 'rf')
2302 &rtp = save_rtp
2303enddef
2304
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002305def Test_autoload_mapping()
2306 mkdir('Xdir/autoload', 'p')
2307 var save_rtp = &rtp
2308 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2309
2310 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002311 vim9script
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002312
2313 g:toggle_loaded = 'yes'
2314
2315 export def Toggle(): string
2316 return ":g:toggle_called = 'yes'\<CR>"
2317 enddef
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002318 export def Doit()
2319 g:doit_called = 'yes'
2320 enddef
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002321 END
2322 writefile(lines, 'Xdir/autoload/toggle.vim')
2323
2324 lines =<< trim END
2325 vim9script
2326
2327 import autoload 'toggle.vim'
2328
2329 nnoremap <silent> <expr> tt toggle.Toggle()
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002330 nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR>
2331 nnoremap <silent> yy <Cmd>toggle.Doit()<CR>
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002332 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002333 v9.CheckScriptSuccess(lines)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002334 assert_false(exists("g:toggle_loaded"))
2335 assert_false(exists("g:toggle_called"))
Bram Moolenaar6079da72022-01-18 14:16:59 +00002336 assert_match('\d A: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002337
2338 feedkeys("tt", 'xt')
2339 assert_equal('yes', g:toggle_loaded)
2340 assert_equal('yes', g:toggle_called)
Bram Moolenaar6079da72022-01-18 14:16:59 +00002341 assert_match('\d: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002342
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002343 feedkeys("xx", 'xt')
2344 assert_equal('yes', g:doit_called)
2345
2346 assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle')
2347
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002348 nunmap tt
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002349 nunmap xx
2350 nunmap yy
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002351 unlet g:toggle_loaded
2352 unlet g:toggle_called
2353 delete('Xdir', 'rf')
2354 &rtp = save_rtp
2355enddef
2356
Bram Moolenaar160aa862022-01-10 21:29:57 +00002357def Test_vim9script_autoload_fails()
2358 var lines =<< trim END
2359 vim9script autoload
2360 var n = 0
2361 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002362 v9.CheckScriptFailure(lines, 'E475: Invalid argument: autoload')
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002363
2364 lines =<< trim END
2365 vim9script noclear noclear
2366 var n = 0
2367 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002368 v9.CheckScriptFailure(lines, 'E983: Duplicate argument: noclear')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002369enddef
2370
2371def Test_import_autoload_fails()
2372 var lines =<< trim END
2373 vim9script
2374 import autoload autoload 'prefixed.vim'
2375 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002376 v9.CheckScriptFailure(lines, 'E121: Undefined variable: autoload')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002377
2378 lines =<< trim END
2379 vim9script
Bram Moolenaar1836d612022-01-18 13:14:47 +00002380 import autoload './doesNotExist.vim'
Bram Moolenaar160aa862022-01-10 21:29:57 +00002381 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002382 v9.CheckScriptFailure(lines, 'E1264:')
Bram Moolenaar1836d612022-01-18 13:14:47 +00002383
2384 lines =<< trim END
2385 vim9script
2386 import autoload '/dir/doesNotExist.vim'
2387 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002388 v9.CheckScriptFailure(lines, 'E1264:')
Bram Moolenaar1836d612022-01-18 13:14:47 +00002389
2390 lines =<< trim END
2391 vim9script
2392 import autoload 'doesNotExist.vim'
2393 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002394 v9.CheckScriptFailure(lines, 'E1053: Could not import "doesNotExist.vim"')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002395enddef
2396
2397" test disassembling an auto-loaded function starting with "debug"
2398def Test_vim9_autoload_disass()
2399 mkdir('Xdir/autoload', 'p')
2400 var save_rtp = &rtp
2401 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2402
2403 var lines =<< trim END
2404 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002405 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002406 return 'debug'
2407 enddef
2408 END
2409 writefile(lines, 'Xdir/autoload/debugit.vim')
2410
2411 lines =<< trim END
2412 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002413 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002414 return 'profile'
2415 enddef
2416 END
2417 writefile(lines, 'Xdir/autoload/profileit.vim')
2418
2419 lines =<< trim END
2420 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002421 assert_equal('debug', debugit#Test())
2422 disass debugit#Test
2423 assert_equal('profile', profileit#Test())
2424 disass profileit#Test
Bram Moolenaar160aa862022-01-10 21:29:57 +00002425 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002426 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002427
2428 delete('Xdir', 'rf')
2429 &rtp = save_rtp
2430enddef
2431
2432" test using a vim9script that is auto-loaded from an autocmd
2433def Test_vim9_aucmd_autoload()
2434 var lines =<< trim END
2435 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002436 export def Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002437 echomsg getreg('"')
2438 enddef
2439 END
2440
2441 mkdir('Xdir/autoload', 'p')
2442 writefile(lines, 'Xdir/autoload/foo.vim')
2443 var save_rtp = &rtp
2444 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2445 augroup test
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002446 autocmd TextYankPost * call foo#Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002447 augroup END
2448
2449 normal Y
2450
2451 augroup test
2452 autocmd!
2453 augroup END
2454 delete('Xdir', 'rf')
2455 &rtp = save_rtp
2456enddef
2457
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002458" test using a autoloaded file that is case sensitive
2459def Test_vim9_autoload_case_sensitive()
2460 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002461 vim9script
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002462 export def CaseSensitive(): string
2463 return 'done'
2464 enddef
2465 END
2466
2467 mkdir('Xdir/autoload', 'p')
2468 writefile(lines, 'Xdir/autoload/CaseSensitive.vim')
2469 var save_rtp = &rtp
2470 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2471
2472 lines =<< trim END
2473 vim9script
2474 import autoload 'CaseSensitive.vim'
2475 assert_equal('done', CaseSensitive.CaseSensitive())
2476 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002477 v9.CheckScriptSuccess(lines)
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002478
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002479 if !has('fname_case')
2480 lines =<< trim END
2481 vim9script
2482 import autoload 'CaseSensitive.vim'
2483 import autoload 'casesensitive.vim'
2484 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002485 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002486 endif
2487
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002488 delete('Xdir', 'rf')
2489 &rtp = save_rtp
2490enddef
2491
Bram Moolenaar160aa862022-01-10 21:29:57 +00002492" This was causing a crash because suppress_errthrow wasn't reset.
2493def Test_vim9_autoload_error()
2494 var lines =<< trim END
2495 vim9script
2496 def crash#func()
2497 try
2498 for x in List()
2499 endfor
2500 catch
2501 endtry
2502 g:ok = true
2503 enddef
2504 fu List()
2505 invalid
2506 endfu
2507 try
2508 alsoinvalid
2509 catch /wontmatch/
2510 endtry
2511 END
2512 call mkdir('Xruntime/autoload', 'p')
2513 call writefile(lines, 'Xruntime/autoload/crash.vim')
2514
2515 # run in a separate Vim to avoid the side effects of assert_fails()
2516 lines =<< trim END
2517 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
2518 call crash#func()
2519 call writefile(['ok'], 'Xdidit')
2520 qall!
2521 END
2522 writefile(lines, 'Xscript')
Bram Moolenaar62aec932022-01-29 21:45:34 +00002523 g:RunVim([], [], '-S Xscript')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002524 assert_equal(['ok'], readfile('Xdidit'))
2525
2526 delete('Xdidit')
2527 delete('Xscript')
2528 delete('Xruntime', 'rf')
2529
2530 lines =<< trim END
2531 vim9script
2532 var foo#bar = 'asdf'
2533 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002534 v9.CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002535enddef
2536
Bram Moolenaard8448622022-01-07 21:39:52 +00002537
2538" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker