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