blob: 36caf70068a05b78ed1089b3390f67adb7683396 [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
6source vim9.vim
7
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
36def Undo_export_script_lines()
37 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
70 g:imported_name = expo.exp_name
71 expo.exp_name ..= ' Doe'
Bram Moolenaar47036b62022-01-16 21:18:53 +000072 expo.exp_name = expo.exp_name .. ' Maar'
Bram Moolenaard8448622022-01-07 21:39:52 +000073 g:imported_name_appended = expo.exp_name
74 g:exported_later = expo.exported
75
76 expo.theList->add(2)
77 assert_equal([1, 2], expo.theList)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +000078
79 assert_equal('andthensome', 'andthen'->expo.AddSome())
80 assert_equal('awesome', 'awe'->expo.AddRef())
Bram Moolenaard8448622022-01-07 21:39:52 +000081 END
82 writefile(import_script_lines, 'Ximport.vim')
83 source Ximport.vim
84
85 assert_equal('bobbie', g:result)
86 assert_equal('bob', g:localname)
87 assert_equal(9876, g:exported1)
88 assert_equal(9879, g:exported2)
89 assert_equal(9879, g:exported3)
90
91 assert_equal(9884, g:exported_i1)
92 assert_equal(9884, g:exported_i2)
93
94 assert_equal(11, g:exported_s1)
95 assert_equal(11, g:exported_s2)
96 assert_equal(11, g:exported_later)
97
98 assert_equal('Exported', g:imported_func)
99 assert_equal('Exported', g:funcref_result)
100 assert_equal('John', g:imported_name)
Bram Moolenaar47036b62022-01-16 21:18:53 +0000101 assert_equal('John Doe Maar', g:imported_name_appended)
Bram Moolenaard8448622022-01-07 21:39:52 +0000102 assert_false(exists('g:name'))
103
104 Undo_export_script_lines()
105 unlet g:exported1
106 unlet g:exported2
107 unlet g:exported3
108 unlet g:exported_i1
109 unlet g:exported_i2
110 unlet g:exported_later
111 unlet g:imported_func
112 unlet g:imported_name g:imported_name_appended
113 delete('Ximport.vim')
114
115 # similar, with line breaks
116 var import_line_break_script_lines =<< trim END
117 vim9script
118 import './Xexport.vim'
119 as expo
120 g:exported = expo.exported
121 expo.exported += 7
122 g:exported_added = expo.exported
123 g:imported_func = expo.Exported()
124 END
125 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
126 source Ximport_lbr.vim
127
128 assert_equal(11, g:exported)
129 assert_equal(18, g:exported_added)
130 assert_equal('Exported', g:imported_func)
131
132 # exported script not sourced again
133 assert_false(exists('g:result'))
134 unlet g:exported
135 unlet g:exported_added
136 unlet g:imported_func
137 delete('Ximport_lbr.vim')
138
139 var line_break_before_dot =<< trim END
140 vim9script
141 import './Xexport.vim' as expo
142 g:exported = expo
143 .exported
144 END
145 writefile(line_break_before_dot, 'Ximport_lbr_before_dot.vim')
146 assert_fails('source Ximport_lbr_before_dot.vim', 'E1060:', '', 3)
147 delete('Ximport_lbr_before_dot.vim')
148
149 var line_break_after_dot =<< trim END
150 vim9script
151 import './Xexport.vim' as expo
152 g:exported = expo.
153 exported
154 END
155 writefile(line_break_after_dot, 'Ximport_lbr_after_dot.vim')
156 assert_fails('source Ximport_lbr_after_dot.vim', 'E1074:', '', 3)
157 delete('Ximport_lbr_after_dot.vim')
158
159 var import_star_as_lines =<< trim END
160 vim9script
161 import './Xexport.vim' as Export
162 def UseExport()
163 g:exported_def = Export.exported
164 enddef
165 g:exported_script = Export.exported
166 assert_equal(1, exists('Export.exported'))
167 assert_equal(0, exists('Export.notexported'))
168 UseExport()
169 END
170 writefile(import_star_as_lines, 'Ximport.vim')
171 source Ximport.vim
172
173 assert_equal(18, g:exported_def)
174 assert_equal(18, g:exported_script)
175 unlet g:exported_def
176 unlet g:exported_script
177
178 var import_star_as_lines_no_dot =<< trim END
179 vim9script
180 import './Xexport.vim' as Export
181 def Func()
182 var dummy = 1
183 var imported = Export + dummy
184 enddef
185 defcompile
186 END
187 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
188 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
189
190 var import_star_as_lines_dot_space =<< trim END
191 vim9script
192 import './Xexport.vim' as Export
193 def Func()
194 var imported = Export . exported
195 enddef
196 defcompile
197 END
198 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
199 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
200
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000201 writefile(s:export_script_lines, 'Xexport2.vim')
202 var import_as_duplicated =<< trim END
Bram Moolenaard8448622022-01-07 21:39:52 +0000203 vim9script
204 import './Xexport.vim' as expo
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000205 import './Xexport2.vim' as expo
Bram Moolenaard8448622022-01-07 21:39:52 +0000206 END
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000207 writefile(import_as_duplicated, 'Ximport.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000208 assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000209 delete('Xexport2.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000210
211 var import_star_as_lines_script_no_dot =<< trim END
212 vim9script
213 import './Xexport.vim' as Export
214 g:imported_script = Export exported
215 END
216 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
217 assert_fails('source Ximport.vim', 'E1060: Expected dot after name: Export exported')
218
219 var import_star_as_lines_script_space_after_dot =<< trim END
220 vim9script
221 import './Xexport.vim' as Export
222 g:imported_script = Export. exported
223 END
224 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
225 assert_fails('source Ximport.vim', 'E1074:')
226
227 var import_star_as_lines_missing_name =<< trim END
228 vim9script
229 import './Xexport.vim' as Export
230 def Func()
231 var imported = Export.
232 enddef
233 defcompile
234 END
235 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
236 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
237
238 var import_star_as_lbr_lines =<< trim END
239 vim9script
240 import './Xexport.vim'
241 as Export
242 def UseExport()
243 g:exported = Export.exported
244 enddef
245 UseExport()
246 END
247 writefile(import_star_as_lbr_lines, 'Ximport.vim')
248 source Ximport.vim
249 assert_equal(18, g:exported)
250 unlet g:exported
251
252 # try to use something that exists but is not exported
253 var import_not_exported_lines =<< trim END
254 vim9script
255 import './Xexport.vim' as expo
256 echo expo.name
257 END
258 writefile(import_not_exported_lines, 'Ximport.vim')
259 assert_fails('source Ximport.vim', 'E1049:', '', 3, 'Ximport.vim')
260
261 # try to import something that is already defined
262 var import_already_defined =<< trim END
263 vim9script
264 var exported = 'something'
265 import './Xexport.vim' as exported
266 END
267 writefile(import_already_defined, 'Ximport.vim')
268 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
269
270 # try changing an imported const
271 var import_assign_to_const =<< trim END
272 vim9script
273 import './Xexport.vim' as expo
274 def Assign()
275 expo.CONST = 987
276 enddef
277 defcompile
278 END
279 writefile(import_assign_to_const, 'Ximport.vim')
280 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
281
282 # try changing an imported final
283 var import_assign_to_final =<< trim END
284 vim9script
285 import './Xexport.vim' as expo
286 def Assign()
287 expo.theList = [2]
288 enddef
289 defcompile
290 END
291 writefile(import_assign_to_final, 'Ximport.vim')
292 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
293
294 var import_no_as_lines =<< trim END
295 vim9script
296 import './Xexport.vim' name
297 END
298 writefile(import_no_as_lines, 'Ximport.vim')
299 assert_fails('source Ximport.vim', 'E488:', '', 2, 'Ximport.vim')
300
301 var import_invalid_string_lines =<< trim END
302 vim9script
303 import Xexport.vim
304 END
305 writefile(import_invalid_string_lines, 'Ximport.vim')
306 assert_fails('source Ximport.vim', 'E121:', '', 2, 'Ximport.vim')
307
308 var import_wrong_name_lines =<< trim END
309 vim9script
310 import './XnoExport.vim'
311 END
312 writefile(import_wrong_name_lines, 'Ximport.vim')
313 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
314
315 var import_redefining_lines =<< trim END
316 vim9script
317 import './Xexport.vim' as exported
318 var exported = 5
319 END
320 writefile(import_redefining_lines, 'Ximport.vim')
321 assert_fails('source Ximport.vim', 'E1213: Redefining imported item "exported"', '', 3)
322
Bram Moolenaar160aa862022-01-10 21:29:57 +0000323 var import_missing_dot_lines =<< trim END
324 vim9script
325 import './Xexport.vim' as expo
326 def Test()
327 expo = 9
328 enddef
329 defcompile
330 END
331 writefile(import_missing_dot_lines, 'Ximport.vim')
332 assert_fails('source Ximport.vim', 'E1258:', '', 1)
333
334 var import_missing_name_lines =<< trim END
335 vim9script
336 import './Xexport.vim' as expo
337 def Test()
338 expo.99 = 9
339 enddef
340 defcompile
341 END
342 writefile(import_missing_name_lines, 'Ximport.vim')
Bram Moolenaar76283822022-01-10 21:39:03 +0000343 assert_fails('source Ximport.vim', 'E1259:', '', 1)
Bram Moolenaar160aa862022-01-10 21:29:57 +0000344
Bram Moolenaard8448622022-01-07 21:39:52 +0000345 var import_assign_wrong_type_lines =<< trim END
346 vim9script
347 import './Xexport.vim' as expo
348 expo.exported = 'xxx'
349 END
350 writefile(import_assign_wrong_type_lines, 'Ximport.vim')
351 assert_fails('source Ximport.vim', 'E1012: Type mismatch; expected number but got string', '', 3)
352
353 var import_assign_const_lines =<< trim END
354 vim9script
355 import './Xexport.vim' as expo
356 expo.CONST = 4321
357 END
358 writefile(import_assign_const_lines, 'Ximport.vim')
359 assert_fails('source Ximport.vim', 'E46: Cannot change read-only variable "CONST"', '', 3)
360
361 delete('Ximport.vim')
362 delete('Ximport3.vim')
363 delete('Xexport.vim')
364
365 # Check that in a Vim9 script 'cpo' is set to the Vim default.
366 # Flags added or removed are also applied to the restored value.
367 set cpo=abcd
368 var lines =<< trim END
369 vim9script
370 g:cpo_in_vim9script = &cpo
371 set cpo+=f
372 set cpo-=c
373 g:cpo_after_vim9script = &cpo
374 END
375 writefile(lines, 'Xvim9_script')
376 source Xvim9_script
377 assert_equal('fabd', &cpo)
378 set cpo&vim
379 assert_equal(&cpo, g:cpo_in_vim9script)
380 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
381 assert_equal(newcpo, g:cpo_after_vim9script)
382
383 delete('Xvim9_script')
384enddef
385
386def Test_import_funcref()
387 var lines =<< trim END
388 vim9script
389 export def F(): number
390 return 42
391 enddef
392 export const G = F
393 END
394 writefile(lines, 'Xlib.vim')
395
396 lines =<< trim END
397 vim9script
398 import './Xlib.vim' as lib
399 const Foo = lib.G()
400 assert_equal(42, Foo)
401
402 def DoTest()
403 const Goo = lib.G()
404 assert_equal(42, Goo)
405 enddef
406 DoTest()
407 END
408 CheckScriptSuccess(lines)
409
410 delete('Xlib.vim')
411enddef
412
413def Test_import_fails()
414 writefile([], 'Xfoo.vim')
415 var lines =<< trim END
416 import './Xfoo.vim' as foo
417 foo = 'bar'
418 END
419 CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use foo itself'])
420 lines =<< trim END
421 vim9script
422 import './Xfoo.vim' as foo
423 var that = foo
424 END
425 CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
426
427 lines =<< trim END
428 vim9script
429 import './Xfoo.vim' as 9foo
430 END
431 CheckScriptFailure(lines, 'E1047:')
432 lines =<< trim END
433 vim9script
434 import './Xfoo.vim' as the#foo
435 END
436 CheckScriptFailure(lines, 'E1047:')
437 lines =<< trim END
438 vim9script
439 import './Xfoo.vim' as g:foo
440 END
441 CheckScriptFailure(lines, 'E1047:')
442
443 delete('Xfoo.vim')
444
445 lines =<< trim END
446 vim9script
447 def TheFunc()
448 echo 'the func'
449 enddef
450 export var Ref = TheFunc
451 END
452 writefile([], 'Xthat.vim')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000453
Bram Moolenaard8448622022-01-07 21:39:52 +0000454 lines =<< trim END
455 import './Xthat.vim' as That
456 That()
457 END
458 CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use That itself'])
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000459
460 lines =<< trim END
461 import './Xthat.vim' as one
462 import './Xthat.vim' as two
463 END
464 CheckScriptFailure(lines, 'E1262:')
465
466 delete('Xthat.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000467
468 mkdir('Ximport')
469
470 writefile(['vim9script'], 'Ximport/.vim')
471 lines =<< trim END
472 vim9script
473 import './Ximport/.vim'
474 END
475 CheckScriptFailure(lines, 'E1261: Cannot import .vim without using "as"')
476 lines =<< trim END
477 vim9script
478 import './Ximport/.vim' as vim
479 END
480 CheckScriptSuccess(lines)
481
482 writefile(['vim9script'], 'Ximport/.vimrc')
483 lines =<< trim END
484 vim9script
485 import './Ximport/.vimrc'
486 END
487 CheckScriptFailure(lines, 'E1257: Imported script must use "as" or end in .vim')
488 lines =<< trim END
489 vim9script
490 import './Ximport/.vimrc' as vimrc
491 END
492 CheckScriptSuccess(lines)
493
494 delete('Ximport', 'rf')
495enddef
496
497func g:Trigger()
498 source Ximport.vim
499 return "echo 'yes'\<CR>"
500endfunc
501
502def Test_import_export_expr_map()
503 # check that :import and :export work when buffer is locked
504 var export_lines =<< trim END
505 vim9script
506 export def That(): string
507 return 'yes'
508 enddef
509 END
510 writefile(export_lines, 'Xexport_that.vim')
511
512 var import_lines =<< trim END
513 vim9script
514 import './Xexport_that.vim' as that
515 assert_equal('yes', that.That())
516 END
517 writefile(import_lines, 'Ximport.vim')
518
519 nnoremap <expr> trigger g:Trigger()
520 feedkeys('trigger', "xt")
521
522 delete('Xexport_that.vim')
523 delete('Ximport.vim')
524 nunmap trigger
525enddef
526
527def Test_import_in_filetype()
528 # check that :import works when the buffer is locked
529 mkdir('ftplugin', 'p')
530 var export_lines =<< trim END
531 vim9script
532 export var That = 'yes'
533 END
534 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
535
536 var import_lines =<< trim END
537 vim9script
538 import './Xexport_ft.vim' as ft
539 assert_equal('yes', ft.That)
540 g:did_load_mytpe = 1
541 END
542 writefile(import_lines, 'ftplugin/qf.vim')
543
544 var save_rtp = &rtp
545 &rtp = getcwd() .. ',' .. &rtp
546
547 filetype plugin on
548 copen
549 assert_equal(1, g:did_load_mytpe)
550
551 quit!
552 delete('Xexport_ft.vim')
553 delete('ftplugin', 'rf')
554 &rtp = save_rtp
555enddef
556
557def Test_use_import_in_mapping()
558 var lines =<< trim END
559 vim9script
560 export def Funcx()
561 g:result = 42
562 enddef
563 END
564 writefile(lines, 'XsomeExport.vim')
565 lines =<< trim END
566 vim9script
567 import './XsomeExport.vim' as some
568 var Funcy = some.Funcx
569 nnoremap <F3> :call <sid>Funcy()<cr>
570 END
571 writefile(lines, 'Xmapscript.vim')
572
573 source Xmapscript.vim
574 feedkeys("\<F3>", "xt")
575 assert_equal(42, g:result)
576
577 unlet g:result
578 delete('XsomeExport.vim')
579 delete('Xmapscript.vim')
580 nunmap <F3>
581enddef
582
583def Test_export_fails()
584 CheckScriptFailure(['export var some = 123'], 'E1042:')
585 CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
586 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
587
588 assert_fails('export something', 'E1043:')
589enddef
590
591func Test_import_fails_without_script()
592 CheckRunVimInTerminal
593
594 " call indirectly to avoid compilation error for missing functions
595 call Run_Test_import_fails_on_command_line()
596endfunc
597
598def Run_Test_import_fails_on_command_line()
599 var export =<< trim END
600 vim9script
601 export def Foo(): number
602 return 0
603 enddef
604 END
605 writefile(export, 'XexportCmd.vim')
606
607 var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
608 rows: 6, wait_for_ruler: 0})
609 WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
610
611 delete('XexportCmd.vim')
612 StopVimInTerminal(buf)
613enddef
614
615def Test_vim9_reload_noclear()
616 var lines =<< trim END
617 vim9script
618 export var exported = 'thexport'
619
620 export def TheFunc(x = 0)
621 enddef
622 END
623 writefile(lines, 'XExportReload')
624 lines =<< trim END
625 vim9script noclear
626 g:loadCount += 1
627 var s:reloaded = 'init'
628 import './XExportReload' as exp
629
630 def Again(): string
631 return 'again'
632 enddef
633
634 exp.TheFunc()
635
636 if exists('s:loaded') | finish | endif
637 var s:loaded = true
638
639 var s:notReloaded = 'yes'
640 s:reloaded = 'first'
641 def g:Values(): list<string>
642 return [s:reloaded, s:notReloaded, Again(), Once(), exp.exported]
643 enddef
644
645 def Once(): string
646 return 'once'
647 enddef
648 END
649 writefile(lines, 'XReloaded')
650 g:loadCount = 0
651 source XReloaded
652 assert_equal(1, g:loadCount)
653 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
654 source XReloaded
655 assert_equal(2, g:loadCount)
656 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
657 source XReloaded
658 assert_equal(3, g:loadCount)
659 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
660
661 delete('XReloaded')
662 delete('XExportReload')
663 delfunc g:Values
664 unlet g:loadCount
665
666 lines =<< trim END
667 vim9script
668 def Inner()
669 enddef
670 END
671 lines->writefile('XreloadScript.vim')
672 source XreloadScript.vim
673
674 lines =<< trim END
675 vim9script
676 def Outer()
677 def Inner()
678 enddef
679 enddef
680 defcompile
681 END
682 lines->writefile('XreloadScript.vim')
683 source XreloadScript.vim
684
685 delete('XreloadScript.vim')
686enddef
687
688def Test_vim9_reload_import()
689 var lines =<< trim END
690 vim9script
691 const var = ''
692 var valone = 1234
693 def MyFunc(arg: string)
694 valone = 5678
695 enddef
696 END
697 var morelines =<< trim END
698 var valtwo = 222
699 export def GetValtwo(): number
700 return valtwo
701 enddef
702 END
703 writefile(lines + morelines, 'Xreload.vim')
704 source Xreload.vim
705 source Xreload.vim
706 source Xreload.vim
707
708 # cannot declare a var twice
709 lines =<< trim END
710 vim9script
711 var valone = 1234
712 var valone = 5678
713 END
714 writefile(lines, 'Xreload.vim')
715 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
716
717 delete('Xreload.vim')
718 delete('Ximport.vim')
719enddef
720
721" if a script is reloaded with a script-local variable that changed its type, a
722" compiled function using that variable must fail.
723def Test_script_reload_change_type()
724 var lines =<< trim END
725 vim9script noclear
726 var str = 'string'
727 def g:GetStr(): string
728 return str .. 'xxx'
729 enddef
730 END
731 writefile(lines, 'Xreload.vim')
732 source Xreload.vim
733 echo g:GetStr()
734
735 lines =<< trim END
736 vim9script noclear
737 var str = 1234
738 END
739 writefile(lines, 'Xreload.vim')
740 source Xreload.vim
741 assert_fails('echo g:GetStr()', 'E1150:')
742
743 delfunc g:GetStr
744 delete('Xreload.vim')
745enddef
746
747" Define CallFunc so that the test can be compiled
748command CallFunc echo 'nop'
749
750def Test_script_reload_from_function()
751 var lines =<< trim END
752 vim9script
753
754 if exists('g:loaded')
755 finish
756 endif
757 g:loaded = 1
758 delcommand CallFunc
759 command CallFunc Func()
760 def Func()
761 so XreloadFunc.vim
762 g:didTheFunc = 1
763 enddef
764 END
765 writefile(lines, 'XreloadFunc.vim')
766 source XreloadFunc.vim
767 CallFunc
768 assert_equal(1, g:didTheFunc)
769
770 delete('XreloadFunc.vim')
771 delcommand CallFunc
772 unlet g:loaded
773 unlet g:didTheFunc
774enddef
775
776def s:RetSome(): string
777 return 'some'
778enddef
779
780" Not exported function that is referenced needs to be accessed by the
781" script-local name.
782def Test_vim9_funcref()
783 var sortlines =<< trim END
784 vim9script
785 def Compare(i1: number, i2: number): number
786 return i2 - i1
787 enddef
788
789 export def FastSort(): list<number>
790 return range(5)->sort(Compare)
791 enddef
792
793 export def GetString(arg: string): string
794 return arg
795 enddef
796 END
797 writefile(sortlines, 'Xsort.vim')
798
799 var lines =<< trim END
800 vim9script
801 import './Xsort.vim'
802 def Test()
803 g:result = Xsort.FastSort()
804 enddef
805 Test()
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000806 END
807 writefile(lines, 'Xscript.vim')
808 source Xscript.vim
809 assert_equal([4, 3, 2, 1, 0], g:result)
810 unlet g:result
Bram Moolenaard8448622022-01-07 21:39:52 +0000811
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000812 lines =<< trim END
813 vim9script
Bram Moolenaard8448622022-01-07 21:39:52 +0000814 # using a function imported with "as"
815 import './Xsort.vim' as anAlias
816 assert_equal('yes', anAlias.GetString('yes'))
817
818 # using the function from a compiled function
819 def TestMore(): string
820 var s = s:anAlias.GetString('foo')
821 return s .. anAlias.GetString('bar')
822 enddef
823 assert_equal('foobar', TestMore())
824
825 # error when using a function that isn't exported
826 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
827 END
828 writefile(lines, 'Xscript.vim')
829
Bram Moolenaard8448622022-01-07 21:39:52 +0000830 delete('Xsort.vim')
831 delete('Xscript.vim')
832
833 var Funcref = function('s:RetSome')
834 assert_equal('some', Funcref())
835enddef
836
837" Check that when searching for "FilterFunc" it finds the import in the
838" script where FastFilter() is called from, both as a string and as a direct
839" function reference.
840def Test_vim9_funcref_other_script()
841 var filterLines =<< trim END
842 vim9script
843 export def FilterFunc(idx: number, val: number): bool
844 return idx % 2 == 1
845 enddef
846 export def FastFilter(): list<number>
847 return range(10)->filter('FilterFunc(v:key, v:val)')
848 enddef
849 export def FastFilterDirect(): list<number>
850 return range(10)->filter(FilterFunc)
851 enddef
852 END
853 writefile(filterLines, 'Xfilter.vim')
854
855 var lines =<< trim END
856 vim9script
857 import './Xfilter.vim' as filter
858 def Test()
859 var x: list<number> = filter.FastFilter()
860 enddef
861 Test()
862 def TestDirect()
863 var x: list<number> = filter.FastFilterDirect()
864 enddef
865 TestDirect()
866 END
867 CheckScriptSuccess(lines)
868 delete('Xfilter.vim')
869enddef
870
871def Test_import_absolute()
872 var import_lines = [
873 'vim9script',
874 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
875 'def UseExported()',
876 ' g:imported_abs = abs.exported',
877 ' abs.exported = 8888',
878 ' g:imported_after = abs.exported',
879 'enddef',
880 'UseExported()',
881 'g:import_disassembled = execute("disass UseExported")',
882 ]
883 writefile(import_lines, 'Ximport_abs.vim')
884 writefile(s:export_script_lines, 'Xexport_abs.vim')
885
886 source Ximport_abs.vim
887
888 assert_equal(9876, g:imported_abs)
889 assert_equal(8888, g:imported_after)
890 assert_match('<SNR>\d\+_UseExported\_s*' ..
891 'g:imported_abs = abs.exported\_s*' ..
892 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
893 '1 STOREG g:imported_abs\_s*' ..
894 'abs.exported = 8888\_s*' ..
895 '2 PUSHNR 8888\_s*' ..
896 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
897 'g:imported_after = abs.exported\_s*' ..
898 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
899 '5 STOREG g:imported_after',
900 g:import_disassembled)
901
902 Undo_export_script_lines()
903 unlet g:imported_abs
904 unlet g:import_disassembled
905
906 delete('Ximport_abs.vim')
907 delete('Xexport_abs.vim')
908enddef
909
910def Test_import_rtp()
911 var import_lines = [
912 'vim9script',
913 'import "Xexport_rtp.vim" as rtp',
914 'g:imported_rtp = rtp.exported',
915 ]
916 writefile(import_lines, 'Ximport_rtp.vim')
917 mkdir('import', 'p')
918 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
919
920 var save_rtp = &rtp
921 &rtp = getcwd()
922 source Ximport_rtp.vim
923 &rtp = save_rtp
924
925 assert_equal(9876, g:imported_rtp)
926
927 Undo_export_script_lines()
928 unlet g:imported_rtp
929 delete('Ximport_rtp.vim')
930 delete('import', 'rf')
931enddef
932
933def Test_import_compile_error()
934 var export_lines = [
935 'vim9script',
936 'export def ExpFunc(): string',
937 ' return notDefined',
938 'enddef',
939 ]
940 writefile(export_lines, 'Xexported.vim')
941
942 var import_lines = [
943 'vim9script',
944 'import "./Xexported.vim" as expo',
945 'def ImpFunc()',
946 ' echo expo.ExpFunc()',
947 'enddef',
948 'defcompile',
949 ]
950 writefile(import_lines, 'Ximport.vim')
951
952 try
953 source Ximport.vim
954 catch /E1001/
955 # Error should be before the Xexported.vim file.
956 assert_match('E1001: Variable not found: notDefined', v:exception)
957 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
958 endtry
959
960 delete('Xexported.vim')
961 delete('Ximport.vim')
962enddef
963
964def Test_func_overrules_import_fails()
965 var export_lines =<< trim END
966 vim9script
967 export def Func()
968 echo 'imported'
969 enddef
970 END
971 writefile(export_lines, 'XexportedFunc.vim')
972
973 var lines =<< trim END
974 vim9script
975 import './XexportedFunc.vim' as Func
976 def Func()
977 echo 'local to function'
978 enddef
979 END
980 CheckScriptFailure(lines, 'E1236:')
981
982 lines =<< trim END
983 vim9script
984 import './XexportedFunc.vim' as Func
985 def Outer()
986 def Func()
987 echo 'local to function'
988 enddef
989 enddef
990 defcompile
991 END
992 CheckScriptFailure(lines, 'E1236:')
993
994 delete('XexportedFunc.vim')
995enddef
996
997def Test_source_vim9_from_legacy()
998 var vim9_lines =<< trim END
999 vim9script
1000 var local = 'local'
1001 g:global = 'global'
1002 export var exported = 'exported'
1003 export def GetText(): string
1004 return 'text'
1005 enddef
1006 END
1007 writefile(vim9_lines, 'Xvim9_script.vim')
1008
1009 var legacy_lines =<< trim END
1010 source Xvim9_script.vim
1011
1012 call assert_false(exists('local'))
1013 call assert_false(exists('exported'))
1014 call assert_false(exists('s:exported'))
1015 call assert_equal('global', global)
1016 call assert_equal('global', g:global)
Bram Moolenaard8448622022-01-07 21:39:52 +00001017 END
1018 writefile(legacy_lines, 'Xlegacy_script.vim')
1019
1020 source Xlegacy_script.vim
1021 assert_equal('global', g:global)
1022 unlet g:global
1023
1024 delete('Xlegacy_script.vim')
1025 delete('Xvim9_script.vim')
1026enddef
1027
Bram Moolenaarc43e6232022-01-13 20:51:56 +00001028def Test_import_vim9_from_legacy()
1029 var vim9_lines =<< trim END
1030 vim9script
1031 var local = 'local'
1032 g:global = 'global'
1033 export var exported = 'exported'
1034 export def GetText(): string
1035 return 'text'
1036 enddef
1037 END
1038 writefile(vim9_lines, 'Xvim9_export.vim')
1039
1040 var legacy_lines =<< trim END
1041 import './Xvim9_export.vim' as vim9
1042
1043 call assert_false(exists('vim9'))
1044 call assert_false(exists('local'))
1045 call assert_false(exists('s:vim9.local'))
1046 call assert_equal('global', global)
1047 call assert_equal('global', g:global)
1048 call assert_false(exists('exported'))
1049 call assert_false(exists('s:exported'))
1050 call assert_false(exists('*GetText'))
1051
1052 " imported symbol is script-local
1053 call assert_equal('exported', s:vim9.exported)
1054 call assert_equal('text', s:vim9.GetText())
1055 END
1056 writefile(legacy_lines, 'Xlegacy_script.vim')
1057
1058 source Xlegacy_script.vim
1059 assert_equal('global', g:global)
1060 unlet g:global
1061
1062 delete('Xlegacy_script.vim')
1063 delete('Xvim9_export.vim')
1064enddef
1065
Bram Moolenaard8448622022-01-07 21:39:52 +00001066def Test_cmdline_win()
1067 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
1068 # the command line window.
1069 mkdir('rtp/syntax', 'p')
1070 var export_lines =<< trim END
1071 vim9script
1072 export var That = 'yes'
1073 END
1074 writefile(export_lines, 'rtp/syntax/Xexport.vim')
1075 var import_lines =<< trim END
1076 vim9script
1077 import './Xexport.vim' as exp
1078 echo exp.That
1079 END
1080 writefile(import_lines, 'rtp/syntax/vim.vim')
1081 var save_rtp = &rtp
1082 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
1083 syntax on
1084 augroup CmdWin
1085 autocmd CmdwinEnter * g:got_there = 'yes'
1086 augroup END
1087 # this will open and also close the cmdline window
1088 feedkeys('q:', 'xt')
1089 assert_equal('yes', g:got_there)
1090
1091 augroup CmdWin
1092 au!
1093 augroup END
1094 &rtp = save_rtp
1095 delete('rtp', 'rf')
1096enddef
1097
1098def Test_import_gone_when_sourced_twice()
1099 var exportlines =<< trim END
1100 vim9script
1101 if exists('g:guard')
1102 finish
1103 endif
1104 g:guard = 1
1105 export var name = 'someName'
1106 END
1107 writefile(exportlines, 'XexportScript.vim')
1108
1109 var lines =<< trim END
1110 vim9script
1111 import './XexportScript.vim' as expo
1112 def g:GetName(): string
1113 return expo.name
1114 enddef
1115 END
1116 writefile(lines, 'XscriptImport.vim')
1117 so XscriptImport.vim
1118 assert_equal('someName', g:GetName())
1119
1120 so XexportScript.vim
1121 assert_fails('call g:GetName()', 'E1149:')
1122
1123 delfunc g:GetName
1124 delete('XexportScript.vim')
1125 delete('XscriptImport.vim')
1126 unlet g:guard
1127enddef
1128
Bram Moolenaar160aa862022-01-10 21:29:57 +00001129" test using an auto-loaded function and variable
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001130def Test_vim9_autoload_full_name()
Bram Moolenaar160aa862022-01-10 21:29:57 +00001131 var lines =<< trim END
1132 vim9script
1133 def some#gettest(): string
1134 return 'test'
1135 enddef
1136 g:some#name = 'name'
1137 g:some#dict = {key: 'value'}
1138
1139 def some#varargs(a1: string, ...l: list<string>): string
1140 return a1 .. l[0] .. l[1]
1141 enddef
1142 END
1143
1144 mkdir('Xdir/autoload', 'p')
1145 writefile(lines, 'Xdir/autoload/some.vim')
1146 var save_rtp = &rtp
1147 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1148
1149 assert_equal('test', g:some#gettest())
1150 assert_equal('name', g:some#name)
1151 assert_equal('value', g:some#dict.key)
1152 g:some#other = 'other'
1153 assert_equal('other', g:some#other)
1154
1155 assert_equal('abc', some#varargs('a', 'b', 'c'))
1156
1157 # upper case script name works
1158 lines =<< trim END
1159 vim9script
1160 def Other#getOther(): string
1161 return 'other'
1162 enddef
1163 END
1164 writefile(lines, 'Xdir/autoload/Other.vim')
1165 assert_equal('other', g:Other#getOther())
1166
1167 delete('Xdir', 'rf')
1168 &rtp = save_rtp
1169enddef
1170
1171def Test_vim9script_autoload()
1172 mkdir('Xdir/autoload', 'p')
1173 var save_rtp = &rtp
1174 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1175
1176 # when using "vim9script autoload" prefix is not needed
1177 var lines =<< trim END
1178 vim9script autoload
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001179 g:prefixed_loaded += 1
Bram Moolenaar160aa862022-01-10 21:29:57 +00001180
1181 export def Gettest(): string
1182 return 'test'
1183 enddef
1184
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001185 export var name = 'name'
1186
1187 export func GetFunc()
1188 return Gettest() .. 'more' .. s:name
Bram Moolenaar160aa862022-01-10 21:29:57 +00001189 endfunc
1190
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001191 export def GetDef(): string
1192 return Gettest() .. 'more' .. name
1193 enddef
1194
Bram Moolenaar160aa862022-01-10 21:29:57 +00001195 export final fname = 'final'
1196 export const cname = 'const'
1197 END
1198 writefile(lines, 'Xdir/autoload/prefixed.vim')
1199
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001200 g:prefixed_loaded = 0
1201 g:expected_loaded = 0
Bram Moolenaar160aa862022-01-10 21:29:57 +00001202 lines =<< trim END
1203 vim9script
1204 import autoload 'prefixed.vim'
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001205 assert_equal(g:expected_loaded, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00001206 assert_equal('test', prefixed.Gettest())
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001207 assert_equal(1, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00001208
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001209 assert_equal('testmorename', prefixed.GetFunc())
1210 assert_equal('testmorename', prefixed.GetDef())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001211 assert_equal('name', prefixed.name)
1212 assert_equal('final', prefixed.fname)
1213 assert_equal('const', prefixed.cname)
1214 END
1215 CheckScriptSuccess(lines)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001216 # can source it again, autoload script not loaded again
1217 g:expected_loaded = 1
1218 CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00001219
1220 # can also get the items by autoload name
1221 lines =<< trim END
1222 call assert_equal('test', prefixed#Gettest())
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001223 call assert_equal('testmorename', prefixed#GetFunc())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001224 call assert_equal('name', prefixed#name)
1225 call assert_equal('final', prefixed#fname)
1226 call assert_equal('const', prefixed#cname)
1227 END
1228 CheckScriptSuccess(lines)
1229
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001230 unlet g:prefixed_loaded
1231 unlet g:expected_loaded
1232 delete('Xdir', 'rf')
1233 &rtp = save_rtp
1234enddef
1235
1236def Test_vim9script_autoload_call()
1237 mkdir('Xdir/autoload', 'p')
1238 var save_rtp = &rtp
1239 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1240
1241 var lines =<< trim END
1242 vim9script autoload
1243
1244 export def Getother()
1245 g:result = 'other'
1246 enddef
1247 END
Bram Moolenaar5d982692022-01-12 15:15:27 +00001248 writefile(lines, 'Xdir/autoload/another.vim')
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001249
1250 lines =<< trim END
1251 vim9script
Bram Moolenaar5d982692022-01-12 15:15:27 +00001252 import autoload 'another.vim'
1253 call another.Getother()
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001254 assert_equal('other', g:result)
1255 END
1256 CheckScriptSuccess(lines)
1257
1258 unlet g:result
Bram Moolenaar160aa862022-01-10 21:29:57 +00001259 delete('Xdir', 'rf')
1260 &rtp = save_rtp
1261enddef
1262
Bram Moolenaard041f422022-01-12 19:54:00 +00001263def Test_import_autoload_postponed()
1264 mkdir('Xdir/autoload', 'p')
1265 var save_rtp = &rtp
1266 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1267
1268 var lines =<< trim END
1269 vim9script autoload
1270
1271 g:loaded_postponed = 'true'
1272 export var variable = 'bla'
1273 export def Function(): string
1274 return 'bla'
1275 enddef
1276 END
1277 writefile(lines, 'Xdir/autoload/postponed.vim')
1278
1279 lines =<< trim END
1280 vim9script
1281
1282 import autoload 'postponed.vim'
1283 def Tryit()
1284 echo postponed.variable
1285 echo postponed.Function()
1286 enddef
1287 defcompile
1288 END
1289 CheckScriptSuccess(lines)
1290 assert_false(exists('g:loaded_postponed'))
1291 CheckScriptSuccess(lines + ['Tryit()'])
1292 assert_equal('true', g:loaded_postponed)
1293
1294 unlet g:loaded_postponed
1295 delete('Xdir', 'rf')
1296 &rtp = save_rtp
1297enddef
1298
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00001299def Test_import_autoload_override()
1300 mkdir('Xdir/autoload', 'p')
1301 var save_rtp = &rtp
1302 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1303 test_override('autoload', 1)
1304
1305 var lines =<< trim END
1306 vim9script autoload
1307
1308 g:loaded_override = 'true'
1309 export var variable = 'bla'
1310 export def Function(): string
1311 return 'bla'
1312 enddef
1313 END
1314 writefile(lines, 'Xdir/autoload/override.vim')
1315
1316 lines =<< trim END
1317 vim9script
1318
1319 import autoload 'override.vim'
1320 assert_equal('true', g:loaded_override)
1321
1322 def Tryit()
1323 echo override.doesNotExist
1324 enddef
1325 defcompile
1326 END
1327 CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1)
1328
1329 test_override('autoload', 0)
1330 unlet g:loaded_override
1331 delete('Xdir', 'rf')
1332 &rtp = save_rtp
1333enddef
1334
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001335def Test_autoload_mapping()
1336 mkdir('Xdir/autoload', 'p')
1337 var save_rtp = &rtp
1338 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1339
1340 var lines =<< trim END
1341 vim9script autoload
1342
1343 g:toggle_loaded = 'yes'
1344
1345 export def Toggle(): string
1346 return ":g:toggle_called = 'yes'\<CR>"
1347 enddef
Bram Moolenaare32c3c42022-01-15 18:26:04 +00001348 export def Doit()
1349 g:doit_called = 'yes'
1350 enddef
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001351 END
1352 writefile(lines, 'Xdir/autoload/toggle.vim')
1353
1354 lines =<< trim END
1355 vim9script
1356
1357 import autoload 'toggle.vim'
1358
1359 nnoremap <silent> <expr> tt toggle.Toggle()
Bram Moolenaare32c3c42022-01-15 18:26:04 +00001360 nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR>
1361 nnoremap <silent> yy <Cmd>toggle.Doit()<CR>
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001362 END
1363 CheckScriptSuccess(lines)
1364 assert_false(exists("g:toggle_loaded"))
1365 assert_false(exists("g:toggle_called"))
1366
1367 feedkeys("tt", 'xt')
1368 assert_equal('yes', g:toggle_loaded)
1369 assert_equal('yes', g:toggle_called)
1370
Bram Moolenaare32c3c42022-01-15 18:26:04 +00001371 feedkeys("xx", 'xt')
1372 assert_equal('yes', g:doit_called)
1373
1374 assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle')
1375
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001376 nunmap tt
Bram Moolenaare32c3c42022-01-15 18:26:04 +00001377 nunmap xx
1378 nunmap yy
Bram Moolenaar19db9e62022-01-11 11:58:19 +00001379 unlet g:toggle_loaded
1380 unlet g:toggle_called
1381 delete('Xdir', 'rf')
1382 &rtp = save_rtp
1383enddef
1384
Bram Moolenaar160aa862022-01-10 21:29:57 +00001385def Test_vim9script_autoload_fails()
1386 var lines =<< trim END
1387 vim9script autoload
1388 var n = 0
1389 END
1390 CheckScriptFailure(lines, 'E1263:')
1391enddef
1392
1393def Test_import_autoload_fails()
1394 var lines =<< trim END
1395 vim9script
1396 import autoload autoload 'prefixed.vim'
1397 END
1398 CheckScriptFailure(lines, 'E121: Undefined variable: autoload')
1399
1400 lines =<< trim END
1401 vim9script
1402 import autoload 'doesNotExist.vim'
1403 END
1404 CheckScriptFailure(lines, 'E1264:')
1405enddef
1406
1407" test disassembling an auto-loaded function starting with "debug"
1408def Test_vim9_autoload_disass()
1409 mkdir('Xdir/autoload', 'p')
1410 var save_rtp = &rtp
1411 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1412
1413 var lines =<< trim END
1414 vim9script
1415 def debugit#test(): string
1416 return 'debug'
1417 enddef
1418 END
1419 writefile(lines, 'Xdir/autoload/debugit.vim')
1420
1421 lines =<< trim END
1422 vim9script
1423 def profileit#test(): string
1424 return 'profile'
1425 enddef
1426 END
1427 writefile(lines, 'Xdir/autoload/profileit.vim')
1428
1429 lines =<< trim END
1430 vim9script
1431 assert_equal('debug', debugit#test())
1432 disass debugit#test
1433 assert_equal('profile', profileit#test())
1434 disass profileit#test
1435 END
1436 CheckScriptSuccess(lines)
1437
1438 delete('Xdir', 'rf')
1439 &rtp = save_rtp
1440enddef
1441
1442" test using a vim9script that is auto-loaded from an autocmd
1443def Test_vim9_aucmd_autoload()
1444 var lines =<< trim END
1445 vim9script
1446 def foo#test()
1447 echomsg getreg('"')
1448 enddef
1449 END
1450
1451 mkdir('Xdir/autoload', 'p')
1452 writefile(lines, 'Xdir/autoload/foo.vim')
1453 var save_rtp = &rtp
1454 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1455 augroup test
1456 autocmd TextYankPost * call foo#test()
1457 augroup END
1458
1459 normal Y
1460
1461 augroup test
1462 autocmd!
1463 augroup END
1464 delete('Xdir', 'rf')
1465 &rtp = save_rtp
1466enddef
1467
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00001468" test using a autoloaded file that is case sensitive
1469def Test_vim9_autoload_case_sensitive()
1470 var lines =<< trim END
1471 vim9script autoload
1472 export def CaseSensitive(): string
1473 return 'done'
1474 enddef
1475 END
1476
1477 mkdir('Xdir/autoload', 'p')
1478 writefile(lines, 'Xdir/autoload/CaseSensitive.vim')
1479 var save_rtp = &rtp
1480 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1481
1482 lines =<< trim END
1483 vim9script
1484 import autoload 'CaseSensitive.vim'
1485 assert_equal('done', CaseSensitive.CaseSensitive())
1486 END
1487 CheckScriptSuccess(lines)
1488
Bram Moolenaarbfac4092022-01-16 11:12:12 +00001489 if !has('fname_case')
1490 lines =<< trim END
1491 vim9script
1492 import autoload 'CaseSensitive.vim'
1493 import autoload 'casesensitive.vim'
1494 END
1495 CheckScriptFailure(lines, 'E1262:')
1496 endif
1497
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00001498 delete('Xdir', 'rf')
1499 &rtp = save_rtp
1500enddef
1501
Bram Moolenaar160aa862022-01-10 21:29:57 +00001502" This was causing a crash because suppress_errthrow wasn't reset.
1503def Test_vim9_autoload_error()
1504 var lines =<< trim END
1505 vim9script
1506 def crash#func()
1507 try
1508 for x in List()
1509 endfor
1510 catch
1511 endtry
1512 g:ok = true
1513 enddef
1514 fu List()
1515 invalid
1516 endfu
1517 try
1518 alsoinvalid
1519 catch /wontmatch/
1520 endtry
1521 END
1522 call mkdir('Xruntime/autoload', 'p')
1523 call writefile(lines, 'Xruntime/autoload/crash.vim')
1524
1525 # run in a separate Vim to avoid the side effects of assert_fails()
1526 lines =<< trim END
1527 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
1528 call crash#func()
1529 call writefile(['ok'], 'Xdidit')
1530 qall!
1531 END
1532 writefile(lines, 'Xscript')
1533 RunVim([], [], '-S Xscript')
1534 assert_equal(['ok'], readfile('Xdidit'))
1535
1536 delete('Xdidit')
1537 delete('Xscript')
1538 delete('Xruntime', 'rf')
1539
1540 lines =<< trim END
1541 vim9script
1542 var foo#bar = 'asdf'
1543 END
1544 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
1545enddef
1546
Bram Moolenaard8448622022-01-07 21:39:52 +00001547
1548" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker