blob: 2c7e731006b73ddede47ab3c28c6e3f1c52162d4 [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 Moolenaarc0ceeeb2022-03-30 21:12:27 +0100843def Test_autoload_import_relative()
844 var lines =<< trim END
845 vim9script
846
847 g:loaded = 'yes'
848 export def RelFunc(): string
849 return 'relfunc'
850 enddef
851 def NotExported()
852 echo 'not'
853 enddef
854
855 export var someText = 'some text'
856 var notexp = 'bad'
857 END
858 writefile(lines, 'XimportRel.vim')
859 writefile(lines, 'XimportRel2.vim')
860 writefile(lines, 'XimportRel3.vim')
861
862 lines =<< trim END
863 vim9script
864 g:loaded = 'no'
865 import autoload './XimportRel.vim'
866 assert_equal('no', g:loaded)
867
868 def AFunc(): string
869 var res = ''
870 res ..= XimportRel.RelFunc()
871 res ..= '/'
872 res ..= XimportRel.someText
873 XimportRel.someText = 'from AFunc'
874 return res
875 enddef
876 # script not loaded when compiling
877 defcompile
878 assert_equal('no', g:loaded)
879
880 assert_equal('relfunc/some text', AFunc())
881 assert_equal('yes', g:loaded)
882 unlet g:loaded
883
884 assert_equal('from AFunc', XimportRel.someText)
885 XimportRel.someText = 'from script'
886 assert_equal('from script', XimportRel.someText)
887 END
888 v9.CheckScriptSuccess(lines)
889
890 lines =<< trim END
891 vim9script
892 import autoload './XimportRel.vim'
893 echo XimportRel.NotExported()
894 END
895 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 3)
896
897 lines =<< trim END
898 vim9script
899 import autoload './XimportRel.vim'
900 echo XimportRel.notexp
901 END
902 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
903
904 lines =<< trim END
905 vim9script
906 import autoload './XimportRel.vim'
907 XimportRel.notexp = 'bad'
908 END
909 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
910
911 lines =<< trim END
912 vim9script
913 import autoload './XimportRel.vim'
914 def Func()
915 echo XimportRel.NotExported()
916 enddef
917 Func()
918 END
919 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 1)
920
921 lines =<< trim END
922 vim9script
923 import autoload './XimportRel.vim'
924 def Func()
925 echo XimportRel.notexp
926 enddef
927 Func()
928 END
929 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
930
931 lines =<< trim END
932 vim9script
933 import autoload './XimportRel.vim'
934 def Func()
935 XimportRel.notexp = 'bad'
936 enddef
937 Func()
938 END
939 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
940
941 # does not fail if the script wasn't loaded yet
942 g:loaded = 'no'
943 lines =<< trim END
944 vim9script
945 import autoload './XimportRel2.vim'
946 def Func()
947 echo XimportRel2.notexp
948 enddef
949 defcompile
950 END
951 v9.CheckScriptSuccess(lines)
952 assert_equal('no', g:loaded)
953
954 # fails with a not loaded import
955 lines =<< trim END
956 vim9script
957 import autoload './XimportRel3.vim'
958 def Func()
959 XimportRel3.notexp = 'bad'
960 enddef
961 Func()
962 END
963 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
964 assert_equal('yes', g:loaded)
965 unlet g:loaded
966
967 delete('XimportRel.vim')
968 delete('XimportRel2.vim')
969 delete('XimportRel3.vim')
970enddef
971
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100972def Test_autoload_import_relative_autoload_dir()
973 mkdir('autoload', 'p')
974 var lines =<< trim END
975 vim9script
976 export def Bar()
977 g:called_bar = 'yes'
978 enddef
979 END
980 writefile(lines, 'autoload/script.vim')
981
982 lines =<< trim END
983 vim9script
984 import autoload './autoload/script.vim'
985 def Foo()
986 script.Bar()
987 enddef
988 Foo()
989 assert_equal('yes', g:called_bar)
990 END
991 v9.CheckScriptSuccess(lines)
992
993 unlet g:called_bar
994 delete('autoload', 'rf')
995enddef
996
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000997func Test_import_in_diffexpr()
998 CheckExecutable diff
999
1000 call Run_Test_import_in_diffexpr()
1001endfunc
1002
1003def Run_Test_import_in_diffexpr()
1004 var lines =<< trim END
1005 vim9script
1006
1007 export def DiffExpr()
1008 # Prepend some text to check diff type detection
1009 writefile(['warning', ' message'], v:fname_out)
1010 silent exe '!diff ' .. v:fname_in .. ' '
1011 .. v:fname_new .. '>>' .. v:fname_out
1012 enddef
1013 END
1014 writefile(lines, 'Xdiffexpr')
1015
1016 lines =<< trim END
1017 vim9script
1018 import './Xdiffexpr' as diff
1019
1020 set diffexpr=diff.DiffExpr()
1021 set diffopt=foldcolumn:0
1022 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001023 v9.CheckScriptSuccess(lines)
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001024
1025 enew!
1026 call setline(1, ['one', 'two', 'three'])
1027 diffthis
1028
1029 botright vert new
1030 call setline(1, ['one', 'two', 'three.'])
1031 diffthis
1032 # we only check if this does not cause errors
1033 redraw
1034
1035 diffoff!
1036 bwipe!
1037 bwipe!
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001038 delete('Xdiffexpr')
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001039enddef
1040
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001041def Test_import_in_patchexpr()
1042 var lines =<< trim END
1043 vim9script
1044 export def TPatch()
1045 call writefile(['output file'], v:fname_out)
1046 enddef
1047 END
1048 writefile(lines, 'Xpatchexpr')
1049
1050 lines =<< trim END
1051 vim9script
1052 import './Xpatchexpr' as patch
1053 set patchexpr=patch.TPatch()
1054 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001055 v9.CheckScriptSuccess(lines)
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001056
1057 call writefile(['input file'], 'Xinput')
1058 call writefile(['diff file'], 'Xdiff')
1059 :%bwipe!
1060 edit Xinput
1061 diffpatch Xdiff
1062 call assert_equal('output file', getline(1))
1063
1064 call delete('Xinput')
1065 call delete('Xdiff')
1066 call delete('Xpatchexpr')
1067 set patchexpr&
1068 :%bwipe!
1069enddef
1070
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001071def Test_import_in_formatexpr()
1072 var lines =<< trim END
1073 vim9script
1074 export def MyFormatExpr(): number
1075 g:did_format = 'yes'
1076 return 0
1077 enddef
1078 END
1079 writefile(lines, 'Xformatter')
1080
1081 lines =<< trim END
1082 vim9script
1083 import './Xformatter' as format
1084 set formatexpr=format.MyFormatExpr()
1085 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001086 v9.CheckScriptSuccess(lines)
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001087
1088 new
1089 setline(1, ['a', 'b', 'c'])
1090 normal gqG
1091 assert_equal('yes', g:did_format)
1092
1093 bwipe!
1094 delete('Xformatter')
1095 unlet g:did_format
1096 set formatexpr=
1097enddef
1098
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001099def Test_import_in_includeexpr()
1100 writefile(['found it'], 'Xthisfile')
1101 new
1102
1103 var lines =<< trim END
1104 vim9script
1105 export def DoSub(): string
1106 return substitute(v:fname, 'that', 'this', '')
1107 enddef
1108 END
1109 writefile(lines, 'Xinclude.vim')
1110
1111 lines =<< trim END
1112 vim9script
1113 import './Xinclude.vim'
1114 set includeexpr=Xinclude.DoSub()
1115 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001116 v9.CheckScriptSuccess(lines)
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001117
1118 setline(1, ['Xthatfile'])
1119 exe "normal \<C-W>f"
1120 assert_equal('Xthisfile', expand('%'))
1121
1122 bwipe!
1123 bwipe!
1124 set includeexpr=
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001125 delete('Xinclude.vim')
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001126 delete('Xthisfile')
1127enddef
1128
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001129def Test_import_in_indentexpr()
1130 var lines =<< trim END
1131 vim9script
1132 export def GetIndent(): number
1133 return 5
1134 enddef
1135 END
1136 writefile(lines, 'Xindenter')
1137
1138 lines =<< trim END
1139 vim9script
1140 import './Xindenter' as indent
1141 set indentexpr=indent.GetIndent()
1142 set debug=throw
1143 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001144 v9.CheckScriptSuccess(lines)
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001145
1146 new
1147 setline(1, 'hello')
1148 normal ==
1149 assert_equal(' hello', getline(1))
1150
1151 bwipe!
1152 set indentexpr= debug=
1153 delete('Xindenter')
1154enddef
1155
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001156func Test_import_in_printexpr()
1157 CheckFeature postscript
1158 call Run_Test_import_in_printexpr()
1159endfunc
1160
1161def Run_Test_import_in_printexpr()
1162 var lines =<< trim END
1163 vim9script
1164 export def PrintFile(): bool
1165 g:printed = 'yes'
1166 delete('v:fname_in')
1167 return false
1168 enddef
1169 END
1170 writefile(lines, 'Xprint.vim')
1171
1172 lines =<< trim END
1173 vim9script
1174 import './Xprint.vim'
1175 set printexpr=Xprint.PrintFile()
1176 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001177 v9.CheckScriptSuccess(lines)
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001178
1179 help
1180 hardcopy dummy args
1181 assert_equal('yes', g:printed)
1182
1183 delete('Xprint.vim')
1184 set printexpr=
1185enddef
1186
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001187def Test_import_in_charconvert()
1188 var lines =<< trim END
1189 vim9script
1190 export def MakeUpper(): bool
1191 var data = readfile(v:fname_in)
1192 map(data, 'toupper(v:val)')
1193 writefile(data, v:fname_out)
1194 return false # success
1195 enddef
1196 END
1197 writefile(lines, 'Xconvert.vim')
1198
1199 lines =<< trim END
1200 vim9script
1201 import './Xconvert.vim' as conv
1202 set charconvert=conv.MakeUpper()
1203 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001204 v9.CheckScriptSuccess(lines)
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001205
1206 writefile(['one', 'two'], 'Xfile')
1207 new Xfile
1208 write ++enc=ucase Xfile1
1209 assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1210
1211 delete('Xfile')
1212 delete('Xfile1')
1213 delete('Xconvert.vim')
1214 bwipe!
1215 set charconvert&
1216enddef
1217
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001218func Test_import_in_spellsuggest_expr()
1219 CheckFeature spell
1220 call Run_Test_import_in_spellsuggest_expr()
1221endfunc
1222
1223def Run_Test_import_in_spellsuggest_expr()
1224 var lines =<< trim END
1225 vim9script
1226 export def MySuggest(): list<any>
1227 return [['Fox', 8], ['Fop', 9]]
1228 enddef
1229 END
1230 writefile(lines, 'Xsuggest.vim')
1231
1232 lines =<< trim END
1233 vim9script
1234 import './Xsuggest.vim' as sugg
1235 set spell spellsuggest=expr:sugg.MySuggest()
1236 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001237 v9.CheckScriptSuccess(lines)
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001238
1239 set verbose=1 # report errors
1240 call assert_equal(['Fox', 'Fop'], spellsuggest('Fo', 2))
1241
1242 delete('Xsuggest.vim')
1243 set nospell spellsuggest& verbose=0
1244enddef
1245
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001246def Test_export_shadows_global_function()
1247 mkdir('Xdir/autoload', 'p')
1248 var save_rtp = &rtp
1249 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1250
1251 var lines =<< trim END
1252 vim9script
1253 export def Shadow(): string
1254 return 'Shadow()'
1255 enddef
1256 END
1257 writefile(lines, 'Xdir/autoload/shadow.vim')
1258
1259 lines =<< trim END
1260 vim9script
1261
1262 def g:Shadow(): string
1263 return 'global'
1264 enddef
1265
1266 import autoload 'shadow.vim'
1267 assert_equal('Shadow()', shadow.Shadow())
1268 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001269 v9.CheckScriptSuccess(lines)
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001270
1271 delfunc g:Shadow
1272 bwipe!
1273 delete('Xdir', 'rf')
1274 &rtp = save_rtp
1275enddef
1276
Bram Moolenaard8448622022-01-07 21:39:52 +00001277def Test_export_fails()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001278 v9.CheckScriptFailure(['export var some = 123'], 'E1042:')
1279 v9.CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
1280 v9.CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001281
1282 assert_fails('export something', 'E1043:')
1283enddef
1284
1285func Test_import_fails_without_script()
1286 CheckRunVimInTerminal
1287
1288 " call indirectly to avoid compilation error for missing functions
1289 call Run_Test_import_fails_on_command_line()
1290endfunc
1291
1292def Run_Test_import_fails_on_command_line()
1293 var export =<< trim END
1294 vim9script
1295 export def Foo(): number
1296 return 0
1297 enddef
1298 END
1299 writefile(export, 'XexportCmd.vim')
1300
Bram Moolenaar62aec932022-01-29 21:45:34 +00001301 var buf = g:RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaard8448622022-01-07 21:39:52 +00001302 rows: 6, wait_for_ruler: 0})
Bram Moolenaar62aec932022-01-29 21:45:34 +00001303 g:WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaard8448622022-01-07 21:39:52 +00001304
1305 delete('XexportCmd.vim')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001306 g:StopVimInTerminal(buf)
Bram Moolenaard8448622022-01-07 21:39:52 +00001307enddef
1308
1309def Test_vim9_reload_noclear()
1310 var lines =<< trim END
1311 vim9script
1312 export var exported = 'thexport'
1313
1314 export def TheFunc(x = 0)
1315 enddef
1316 END
1317 writefile(lines, 'XExportReload')
1318 lines =<< trim END
1319 vim9script noclear
1320 g:loadCount += 1
Bram Moolenaara749a422022-02-12 19:52:25 +00001321 var reloaded = 'init'
Bram Moolenaard8448622022-01-07 21:39:52 +00001322 import './XExportReload' as exp
1323
1324 def Again(): string
1325 return 'again'
1326 enddef
1327
1328 exp.TheFunc()
1329
Bram Moolenaara749a422022-02-12 19:52:25 +00001330 if exists('loaded') | finish | endif
1331 var loaded = true
Bram Moolenaard8448622022-01-07 21:39:52 +00001332
Bram Moolenaara749a422022-02-12 19:52:25 +00001333 var notReloaded = 'yes'
1334 reloaded = 'first'
Bram Moolenaard8448622022-01-07 21:39:52 +00001335 def g:Values(): list<string>
Bram Moolenaara749a422022-02-12 19:52:25 +00001336 return [reloaded, notReloaded, Again(), Once(), exp.exported]
Bram Moolenaard8448622022-01-07 21:39:52 +00001337 enddef
1338
1339 def Once(): string
1340 return 'once'
1341 enddef
1342 END
1343 writefile(lines, 'XReloaded')
1344 g:loadCount = 0
1345 source XReloaded
1346 assert_equal(1, g:loadCount)
1347 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
1348 source XReloaded
1349 assert_equal(2, g:loadCount)
1350 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1351 source XReloaded
1352 assert_equal(3, g:loadCount)
1353 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1354
1355 delete('XReloaded')
1356 delete('XExportReload')
1357 delfunc g:Values
1358 unlet g:loadCount
1359
1360 lines =<< trim END
1361 vim9script
1362 def Inner()
1363 enddef
1364 END
1365 lines->writefile('XreloadScript.vim')
1366 source XreloadScript.vim
1367
1368 lines =<< trim END
1369 vim9script
1370 def Outer()
1371 def Inner()
1372 enddef
1373 enddef
1374 defcompile
1375 END
1376 lines->writefile('XreloadScript.vim')
1377 source XreloadScript.vim
1378
1379 delete('XreloadScript.vim')
1380enddef
1381
Bram Moolenaarcd1cda22022-02-16 21:48:25 +00001382def Test_vim_reload_noclear_arg_count()
1383 var lines =<< trim END
1384 vim9script noclear
1385
1386 if !exists('g:didload')
1387 def Test(a: string, b: string)
1388 echo a b
1389 enddef
1390 def Call()
1391 Test('a', 'b')
1392 enddef
1393 else
1394 # redefine with one argument less
1395 def Test(a: string)
1396 echo a
1397 enddef
1398 endif
1399 Call()
1400 g:didload = 1
1401 END
1402 lines->writefile('XreloadScript_1.vim')
1403 source XreloadScript_1.vim
1404 assert_fails('source XreloadScript_1.vim', 'E1106: One argument too many')
1405 unlet g:didload
1406
1407 lines =<< trim END
1408 vim9script noclear
1409
1410 if !exists('g:didload')
1411 def Test(a: string, b: string, c: string)
1412 echo a b
1413 enddef
1414 def Call()
1415 Test('a', 'b', 'c')
1416 enddef
1417 else
1418 # redefine with one argument less
1419 def Test(a: string)
1420 echo a
1421 enddef
1422 endif
1423 Call()
1424 g:didload = 1
1425 END
1426 lines->writefile('XreloadScript_2.vim')
1427 source XreloadScript_2.vim
1428 assert_fails('source XreloadScript_2.vim', 'E1106: 2 arguments too many')
1429 unlet g:didload
1430
1431 lines =<< trim END
1432 vim9script noclear
1433
1434 if !exists('g:didload')
1435 def Test(a: string)
1436 echo a
1437 enddef
1438 def Call()
1439 Test('a')
1440 enddef
1441 else
1442 # redefine with one argument extra
1443 def Test(a: string, b: string)
1444 echo a b
1445 enddef
1446 endif
1447 Call()
1448 g:didload = 1
1449 END
1450 lines->writefile('XreloadScript_3.vim')
1451 source XreloadScript_3.vim
1452 assert_fails('source XreloadScript_3.vim', 'E1190: One argument too few')
1453 unlet g:didload
1454
1455 lines =<< trim END
1456 vim9script noclear
1457
1458 if !exists('g:didload')
1459 def Test(a: string)
1460 echo a
1461 enddef
1462 def Call()
1463 Test('a')
1464 enddef
1465 else
1466 # redefine with two arguments extra
1467 def Test(a: string, b: string, c: string)
1468 echo a b
1469 enddef
1470 endif
1471 Call()
1472 g:didload = 1
1473 END
1474 lines->writefile('XreloadScript_4.vim')
1475 source XreloadScript_4.vim
1476 assert_fails('source XreloadScript_4.vim', 'E1190: 2 arguments too few')
1477 unlet g:didload
1478
1479 delete('XreloadScript_1.vim')
1480 delete('XreloadScript_2.vim')
1481 delete('XreloadScript_3.vim')
1482 delete('XreloadScript_4.vim')
1483enddef
1484
1485def Test_vim9_reload_noclear_error()
1486 var lines =<< trim END
1487 vim9script noclear
1488
1489 if !exists('g:didload')
1490 def Test(a: string)
1491 echo a
1492 enddef
1493 def Call()
1494 Test('a')
1495 enddef
1496 else
1497 # redefine with a compile error
1498 def Test(a: string)
1499 echo ax
1500 enddef
1501 endif
1502 Call()
1503 g:didload = 1
1504 END
1505 lines->writefile('XreloadScriptErr.vim')
1506 source XreloadScriptErr.vim
1507 assert_fails('source XreloadScriptErr.vim', 'E1001: Variable not found: ax')
1508
1509 unlet g:didload
1510 delete('XreloadScriptErr.vim')
1511enddef
1512
Bram Moolenaard8448622022-01-07 21:39:52 +00001513def Test_vim9_reload_import()
1514 var lines =<< trim END
1515 vim9script
1516 const var = ''
1517 var valone = 1234
1518 def MyFunc(arg: string)
1519 valone = 5678
1520 enddef
1521 END
1522 var morelines =<< trim END
1523 var valtwo = 222
1524 export def GetValtwo(): number
1525 return valtwo
1526 enddef
1527 END
1528 writefile(lines + morelines, 'Xreload.vim')
1529 source Xreload.vim
1530 source Xreload.vim
1531 source Xreload.vim
1532
1533 # cannot declare a var twice
1534 lines =<< trim END
1535 vim9script
1536 var valone = 1234
1537 var valone = 5678
1538 END
1539 writefile(lines, 'Xreload.vim')
1540 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
1541
1542 delete('Xreload.vim')
1543 delete('Ximport.vim')
1544enddef
1545
1546" if a script is reloaded with a script-local variable that changed its type, a
1547" compiled function using that variable must fail.
1548def Test_script_reload_change_type()
1549 var lines =<< trim END
1550 vim9script noclear
1551 var str = 'string'
1552 def g:GetStr(): string
1553 return str .. 'xxx'
1554 enddef
1555 END
1556 writefile(lines, 'Xreload.vim')
1557 source Xreload.vim
1558 echo g:GetStr()
1559
1560 lines =<< trim END
1561 vim9script noclear
1562 var str = 1234
1563 END
1564 writefile(lines, 'Xreload.vim')
1565 source Xreload.vim
1566 assert_fails('echo g:GetStr()', 'E1150:')
1567
1568 delfunc g:GetStr
1569 delete('Xreload.vim')
1570enddef
1571
1572" Define CallFunc so that the test can be compiled
1573command CallFunc echo 'nop'
1574
1575def Test_script_reload_from_function()
1576 var lines =<< trim END
1577 vim9script
1578
1579 if exists('g:loaded')
1580 finish
1581 endif
1582 g:loaded = 1
1583 delcommand CallFunc
1584 command CallFunc Func()
1585 def Func()
1586 so XreloadFunc.vim
1587 g:didTheFunc = 1
1588 enddef
1589 END
1590 writefile(lines, 'XreloadFunc.vim')
1591 source XreloadFunc.vim
1592 CallFunc
1593 assert_equal(1, g:didTheFunc)
1594
1595 delete('XreloadFunc.vim')
1596 delcommand CallFunc
1597 unlet g:loaded
1598 unlet g:didTheFunc
1599enddef
1600
1601def s:RetSome(): string
1602 return 'some'
1603enddef
1604
1605" Not exported function that is referenced needs to be accessed by the
1606" script-local name.
1607def Test_vim9_funcref()
1608 var sortlines =<< trim END
1609 vim9script
1610 def Compare(i1: number, i2: number): number
1611 return i2 - i1
1612 enddef
1613
1614 export def FastSort(): list<number>
1615 return range(5)->sort(Compare)
1616 enddef
1617
1618 export def GetString(arg: string): string
1619 return arg
1620 enddef
1621 END
1622 writefile(sortlines, 'Xsort.vim')
1623
1624 var lines =<< trim END
1625 vim9script
1626 import './Xsort.vim'
1627 def Test()
1628 g:result = Xsort.FastSort()
1629 enddef
1630 Test()
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001631 END
1632 writefile(lines, 'Xscript.vim')
1633 source Xscript.vim
1634 assert_equal([4, 3, 2, 1, 0], g:result)
1635 unlet g:result
Bram Moolenaard8448622022-01-07 21:39:52 +00001636
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001637 lines =<< trim END
1638 vim9script
Bram Moolenaard8448622022-01-07 21:39:52 +00001639 # using a function imported with "as"
1640 import './Xsort.vim' as anAlias
1641 assert_equal('yes', anAlias.GetString('yes'))
1642
1643 # using the function from a compiled function
1644 def TestMore(): string
1645 var s = s:anAlias.GetString('foo')
1646 return s .. anAlias.GetString('bar')
1647 enddef
1648 assert_equal('foobar', TestMore())
1649
1650 # error when using a function that isn't exported
1651 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
1652 END
1653 writefile(lines, 'Xscript.vim')
1654
Bram Moolenaard8448622022-01-07 21:39:52 +00001655 delete('Xsort.vim')
1656 delete('Xscript.vim')
1657
1658 var Funcref = function('s:RetSome')
1659 assert_equal('some', Funcref())
1660enddef
1661
1662" Check that when searching for "FilterFunc" it finds the import in the
1663" script where FastFilter() is called from, both as a string and as a direct
1664" function reference.
1665def Test_vim9_funcref_other_script()
1666 var filterLines =<< trim END
1667 vim9script
1668 export def FilterFunc(idx: number, val: number): bool
1669 return idx % 2 == 1
1670 enddef
1671 export def FastFilter(): list<number>
1672 return range(10)->filter('FilterFunc(v:key, v:val)')
1673 enddef
1674 export def FastFilterDirect(): list<number>
1675 return range(10)->filter(FilterFunc)
1676 enddef
1677 END
1678 writefile(filterLines, 'Xfilter.vim')
1679
1680 var lines =<< trim END
1681 vim9script
1682 import './Xfilter.vim' as filter
1683 def Test()
1684 var x: list<number> = filter.FastFilter()
1685 enddef
1686 Test()
1687 def TestDirect()
1688 var x: list<number> = filter.FastFilterDirect()
1689 enddef
1690 TestDirect()
1691 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001692 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +00001693 delete('Xfilter.vim')
1694enddef
1695
1696def Test_import_absolute()
1697 var import_lines = [
1698 'vim9script',
1699 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
1700 'def UseExported()',
1701 ' g:imported_abs = abs.exported',
1702 ' abs.exported = 8888',
1703 ' g:imported_after = abs.exported',
1704 'enddef',
1705 'UseExported()',
1706 'g:import_disassembled = execute("disass UseExported")',
1707 ]
1708 writefile(import_lines, 'Ximport_abs.vim')
1709 writefile(s:export_script_lines, 'Xexport_abs.vim')
1710
1711 source Ximport_abs.vim
1712
1713 assert_equal(9876, g:imported_abs)
1714 assert_equal(8888, g:imported_after)
1715 assert_match('<SNR>\d\+_UseExported\_s*' ..
1716 'g:imported_abs = abs.exported\_s*' ..
1717 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1718 '1 STOREG g:imported_abs\_s*' ..
1719 'abs.exported = 8888\_s*' ..
1720 '2 PUSHNR 8888\_s*' ..
1721 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1722 'g:imported_after = abs.exported\_s*' ..
1723 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1724 '5 STOREG g:imported_after',
1725 g:import_disassembled)
1726
1727 Undo_export_script_lines()
1728 unlet g:imported_abs
1729 unlet g:import_disassembled
1730
1731 delete('Ximport_abs.vim')
1732 delete('Xexport_abs.vim')
1733enddef
1734
1735def Test_import_rtp()
1736 var import_lines = [
1737 'vim9script',
1738 'import "Xexport_rtp.vim" as rtp',
1739 'g:imported_rtp = rtp.exported',
1740 ]
1741 writefile(import_lines, 'Ximport_rtp.vim')
1742 mkdir('import', 'p')
1743 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1744
1745 var save_rtp = &rtp
1746 &rtp = getcwd()
1747 source Ximport_rtp.vim
1748 &rtp = save_rtp
1749
1750 assert_equal(9876, g:imported_rtp)
1751
1752 Undo_export_script_lines()
1753 unlet g:imported_rtp
1754 delete('Ximport_rtp.vim')
1755 delete('import', 'rf')
1756enddef
1757
1758def Test_import_compile_error()
1759 var export_lines = [
1760 'vim9script',
1761 'export def ExpFunc(): string',
1762 ' return notDefined',
1763 'enddef',
1764 ]
1765 writefile(export_lines, 'Xexported.vim')
1766
1767 var import_lines = [
1768 'vim9script',
1769 'import "./Xexported.vim" as expo',
1770 'def ImpFunc()',
1771 ' echo expo.ExpFunc()',
1772 'enddef',
1773 'defcompile',
1774 ]
1775 writefile(import_lines, 'Ximport.vim')
1776
1777 try
1778 source Ximport.vim
1779 catch /E1001/
1780 # Error should be before the Xexported.vim file.
1781 assert_match('E1001: Variable not found: notDefined', v:exception)
1782 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1783 endtry
1784
1785 delete('Xexported.vim')
1786 delete('Ximport.vim')
1787enddef
1788
1789def Test_func_overrules_import_fails()
1790 var export_lines =<< trim END
1791 vim9script
1792 export def Func()
1793 echo 'imported'
1794 enddef
1795 END
1796 writefile(export_lines, 'XexportedFunc.vim')
1797
1798 var lines =<< trim END
1799 vim9script
1800 import './XexportedFunc.vim' as Func
1801 def Func()
1802 echo 'local to function'
1803 enddef
1804 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001805 v9.CheckScriptFailure(lines, 'E1213: Redefining imported item "Func"')
Bram Moolenaard8448622022-01-07 21:39:52 +00001806
1807 lines =<< trim END
1808 vim9script
1809 import './XexportedFunc.vim' as Func
1810 def Outer()
1811 def Func()
1812 echo 'local to function'
1813 enddef
1814 enddef
1815 defcompile
1816 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001817 v9.CheckScriptFailure(lines, 'E1236:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001818
1819 delete('XexportedFunc.vim')
1820enddef
1821
1822def Test_source_vim9_from_legacy()
1823 var vim9_lines =<< trim END
1824 vim9script
1825 var local = 'local'
1826 g:global = 'global'
1827 export var exported = 'exported'
1828 export def GetText(): string
1829 return 'text'
1830 enddef
1831 END
1832 writefile(vim9_lines, 'Xvim9_script.vim')
1833
1834 var legacy_lines =<< trim END
1835 source Xvim9_script.vim
1836
1837 call assert_false(exists('local'))
1838 call assert_false(exists('exported'))
1839 call assert_false(exists('s:exported'))
1840 call assert_equal('global', global)
1841 call assert_equal('global', g:global)
Bram Moolenaard8448622022-01-07 21:39:52 +00001842 END
1843 writefile(legacy_lines, 'Xlegacy_script.vim')
1844
1845 source Xlegacy_script.vim
1846 assert_equal('global', g:global)
1847 unlet g:global
1848
1849 delete('Xlegacy_script.vim')
1850 delete('Xvim9_script.vim')
1851enddef
1852
Bram Moolenaarc43e6232022-01-13 20:51:56 +00001853def Test_import_vim9_from_legacy()
1854 var vim9_lines =<< trim END
1855 vim9script
1856 var local = 'local'
1857 g:global = 'global'
1858 export var exported = 'exported'
1859 export def GetText(): string
1860 return 'text'
1861 enddef
1862 END
1863 writefile(vim9_lines, 'Xvim9_export.vim')
1864
1865 var legacy_lines =<< trim END
1866 import './Xvim9_export.vim' as vim9
1867
1868 call assert_false(exists('vim9'))
1869 call assert_false(exists('local'))
1870 call assert_false(exists('s:vim9.local'))
1871 call assert_equal('global', global)
1872 call assert_equal('global', g:global)
1873 call assert_false(exists('exported'))
1874 call assert_false(exists('s:exported'))
1875 call assert_false(exists('*GetText'))
1876
1877 " imported symbol is script-local
1878 call assert_equal('exported', s:vim9.exported)
1879 call assert_equal('text', s:vim9.GetText())
1880 END
1881 writefile(legacy_lines, 'Xlegacy_script.vim')
1882
1883 source Xlegacy_script.vim
1884 assert_equal('global', g:global)
1885 unlet g:global
1886
1887 delete('Xlegacy_script.vim')
1888 delete('Xvim9_export.vim')
1889enddef
1890
Bram Moolenaard8448622022-01-07 21:39:52 +00001891def Test_cmdline_win()
1892 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
1893 # the command line window.
1894 mkdir('rtp/syntax', 'p')
1895 var export_lines =<< trim END
1896 vim9script
1897 export var That = 'yes'
1898 END
1899 writefile(export_lines, 'rtp/syntax/Xexport.vim')
1900 var import_lines =<< trim END
1901 vim9script
1902 import './Xexport.vim' as exp
1903 echo exp.That
1904 END
1905 writefile(import_lines, 'rtp/syntax/vim.vim')
1906 var save_rtp = &rtp
1907 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
1908 syntax on
1909 augroup CmdWin
1910 autocmd CmdwinEnter * g:got_there = 'yes'
1911 augroup END
1912 # this will open and also close the cmdline window
1913 feedkeys('q:', 'xt')
1914 assert_equal('yes', g:got_there)
1915
1916 augroup CmdWin
1917 au!
1918 augroup END
1919 &rtp = save_rtp
1920 delete('rtp', 'rf')
1921enddef
1922
1923def Test_import_gone_when_sourced_twice()
1924 var exportlines =<< trim END
1925 vim9script
1926 if exists('g:guard')
1927 finish
1928 endif
1929 g:guard = 1
1930 export var name = 'someName'
1931 END
1932 writefile(exportlines, 'XexportScript.vim')
1933
1934 var lines =<< trim END
1935 vim9script
1936 import './XexportScript.vim' as expo
1937 def g:GetName(): string
1938 return expo.name
1939 enddef
1940 END
1941 writefile(lines, 'XscriptImport.vim')
1942 so XscriptImport.vim
1943 assert_equal('someName', g:GetName())
1944
1945 so XexportScript.vim
1946 assert_fails('call g:GetName()', 'E1149:')
1947
1948 delfunc g:GetName
1949 delete('XexportScript.vim')
1950 delete('XscriptImport.vim')
1951 unlet g:guard
1952enddef
1953
Bram Moolenaar160aa862022-01-10 21:29:57 +00001954" test using an auto-loaded function and variable
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001955def Test_vim9_autoload_full_name()
Bram Moolenaar160aa862022-01-10 21:29:57 +00001956 var lines =<< trim END
1957 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001958 export def Gettest(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00001959 return 'test'
1960 enddef
1961 g:some#name = 'name'
1962 g:some#dict = {key: 'value'}
1963
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001964 export def Varargs(a1: string, ...l: list<string>): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00001965 return a1 .. l[0] .. l[1]
1966 enddef
1967 END
1968
1969 mkdir('Xdir/autoload', 'p')
1970 writefile(lines, 'Xdir/autoload/some.vim')
1971 var save_rtp = &rtp
1972 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1973
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001974 assert_equal('test', g:some#Gettest())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001975 assert_equal('name', g:some#name)
1976 assert_equal('value', g:some#dict.key)
1977 g:some#other = 'other'
1978 assert_equal('other', g:some#other)
1979
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001980 assert_equal('abc', some#Varargs('a', 'b', 'c'))
Bram Moolenaar160aa862022-01-10 21:29:57 +00001981
1982 # upper case script name works
1983 lines =<< trim END
1984 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001985 export def GetOther(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00001986 return 'other'
1987 enddef
1988 END
1989 writefile(lines, 'Xdir/autoload/Other.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001990 assert_equal('other', g:Other#GetOther())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001991
1992 delete('Xdir', 'rf')
1993 &rtp = save_rtp
1994enddef
1995
1996def Test_vim9script_autoload()
1997 mkdir('Xdir/autoload', 'p')
1998 var save_rtp = &rtp
1999 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2000
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002001 # when the path has "/autoload/" prefix is not needed
Bram Moolenaar160aa862022-01-10 21:29:57 +00002002 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002003 vim9script
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002004 g:prefixed_loaded += 1
Bram Moolenaar160aa862022-01-10 21:29:57 +00002005
2006 export def Gettest(): string
2007 return 'test'
2008 enddef
2009
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002010 export var name = 'name'
2011
2012 export func GetFunc()
2013 return Gettest() .. 'more' .. s:name
Bram Moolenaar160aa862022-01-10 21:29:57 +00002014 endfunc
2015
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002016 export def GetDef(): string
2017 return Gettest() .. 'more' .. name
2018 enddef
2019
Bram Moolenaar160aa862022-01-10 21:29:57 +00002020 export final fname = 'final'
2021 export const cname = 'const'
2022 END
2023 writefile(lines, 'Xdir/autoload/prefixed.vim')
2024
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002025 g:prefixed_loaded = 0
2026 g:expected_loaded = 0
Bram Moolenaar160aa862022-01-10 21:29:57 +00002027 lines =<< trim END
2028 vim9script
2029 import autoload 'prefixed.vim'
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002030 assert_equal(g:expected_loaded, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002031 assert_equal('test', prefixed.Gettest())
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002032 assert_equal(1, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002033
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002034 assert_equal('testmorename', prefixed.GetFunc())
2035 assert_equal('testmorename', prefixed.GetDef())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002036 assert_equal('name', prefixed.name)
2037 assert_equal('final', prefixed.fname)
2038 assert_equal('const', prefixed.cname)
2039 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002040 v9.CheckScriptSuccess(lines)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002041 # can source it again, autoload script not loaded again
2042 g:expected_loaded = 1
Bram Moolenaar62aec932022-01-29 21:45:34 +00002043 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002044
2045 # can also get the items by autoload name
2046 lines =<< trim END
2047 call assert_equal('test', prefixed#Gettest())
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002048 call assert_equal('testmorename', prefixed#GetFunc())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002049 call assert_equal('name', prefixed#name)
2050 call assert_equal('final', prefixed#fname)
2051 call assert_equal('const', prefixed#cname)
2052 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002053 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002054
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002055 unlet g:prefixed_loaded
2056 unlet g:expected_loaded
2057 delete('Xdir', 'rf')
2058 &rtp = save_rtp
2059enddef
2060
Bram Moolenaard02dce22022-01-18 17:43:04 +00002061def Test_import_autoload_not_exported()
2062 mkdir('Xdir/autoload', 'p')
2063 var save_rtp = &rtp
2064 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2065
2066 # error when using an item that is not exported from an autoload script
2067 var exportLines =<< trim END
2068 vim9script
2069 var notExported = 123
2070 def NotExport()
2071 echo 'nop'
2072 enddef
2073 END
2074 writefile(exportLines, 'Xdir/autoload/notExport1.vim')
2075
2076 var lines =<< trim END
2077 vim9script
2078 import autoload 'notExport1.vim'
2079 echo notExport1.notFound
2080 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002081 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002082
2083 lines =<< trim END
2084 vim9script
2085 import autoload 'notExport1.vim'
2086 echo notExport1.notExported
2087 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002088 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002089
2090 lines =<< trim END
2091 vim9script
2092 import autoload 'notExport1.vim'
2093 echo notExport1.NotFunc()
2094 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002095 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002096
2097 lines =<< trim END
2098 vim9script
2099 import autoload 'notExport1.vim'
2100 echo notExport1.NotExport()
2101 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002102 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002103
2104 lines =<< trim END
2105 vim9script
2106 import autoload 'notExport1.vim'
2107 echo 'text'->notExport1.NotFunc()
2108 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002109 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002110
2111 lines =<< trim END
2112 vim9script
2113 import autoload 'notExport1.vim'
2114 echo 'text'->notExport1.NotExport()
2115 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002116 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002117
2118 # using a :def function we use a different autoload script every time so that
2119 # the function is compiled without the script loaded
2120 writefile(exportLines, 'Xdir/autoload/notExport2.vim')
2121 lines =<< trim END
2122 vim9script
2123 import autoload 'notExport2.vim'
2124 def Testit()
2125 echo notExport2.notFound
2126 enddef
2127 Testit()
2128 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002129 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport2#notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002130
2131 writefile(exportLines, 'Xdir/autoload/notExport3.vim')
2132 lines =<< trim END
2133 vim9script
2134 import autoload 'notExport3.vim'
2135 def Testit()
2136 echo notExport3.notExported
2137 enddef
2138 Testit()
2139 END
2140 # don't get E1049 because it is too complicated to figure out
Bram Moolenaar62aec932022-01-29 21:45:34 +00002141 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport3#notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002142
2143 writefile(exportLines, 'Xdir/autoload/notExport4.vim')
2144 lines =<< trim END
2145 vim9script
2146 import autoload 'notExport4.vim'
2147 def Testit()
2148 echo notExport4.NotFunc()
2149 enddef
2150 Testit()
2151 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002152 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport4#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002153
2154 writefile(exportLines, 'Xdir/autoload/notExport5.vim')
2155 lines =<< trim END
2156 vim9script
2157 import autoload 'notExport5.vim'
2158 def Testit()
2159 echo notExport5.NotExport()
2160 enddef
2161 Testit()
2162 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002163 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport5#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002164
2165 writefile(exportLines, 'Xdir/autoload/notExport6.vim')
2166 lines =<< trim END
2167 vim9script
2168 import autoload 'notExport6.vim'
2169 def Testit()
2170 echo 'text'->notExport6.NotFunc()
2171 enddef
2172 Testit()
2173 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002174 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport6#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002175
2176 writefile(exportLines, 'Xdir/autoload/notExport7.vim')
2177 lines =<< trim END
2178 vim9script
2179 import autoload 'notExport7.vim'
2180 def Testit()
2181 echo 'text'->notExport7.NotExport()
2182 enddef
2183 Testit()
2184 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002185 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport7#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002186
2187 delete('Xdir', 'rf')
2188 &rtp = save_rtp
2189enddef
2190
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002191def Test_vim9script_autoload_call()
2192 mkdir('Xdir/autoload', 'p')
2193 var save_rtp = &rtp
2194 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2195
2196 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002197 vim9script
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002198
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002199 export def RetArg(arg: string): string
2200 return arg
2201 enddef
2202
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002203 export def Getother()
2204 g:result = 'other'
2205 enddef
2206 END
Bram Moolenaar5d982692022-01-12 15:15:27 +00002207 writefile(lines, 'Xdir/autoload/another.vim')
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002208
2209 lines =<< trim END
2210 vim9script
Bram Moolenaar5d982692022-01-12 15:15:27 +00002211 import autoload 'another.vim'
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002212
2213 # compile this before 'another.vim' is loaded
2214 def CallAnother()
2215 assert_equal('foo', 'foo'->another.RetArg())
2216 enddef
2217 CallAnother()
2218
Bram Moolenaar5d982692022-01-12 15:15:27 +00002219 call another.Getother()
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002220 assert_equal('other', g:result)
Bram Moolenaar3d8e25a2022-01-22 11:00:02 +00002221
2222 assert_equal('arg', call('another.RetArg', ['arg']))
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00002223
2224 verbose function another.Getother
2225 # should we disallow this?
2226 verbose function another#Getother
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002227 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002228 v9.CheckScriptSuccess(lines)
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002229
2230 unlet g:result
Bram Moolenaar160aa862022-01-10 21:29:57 +00002231 delete('Xdir', 'rf')
2232 &rtp = save_rtp
2233enddef
2234
Bram Moolenaarb697dc22022-01-22 11:27:29 +00002235def Test_vim9script_noclear_autoload()
2236 mkdir('Xdir/autoload', 'p')
2237 var save_rtp = &rtp
2238 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2239
2240 var lines =<< trim END
2241 vim9script
2242 export def Func(): string
2243 return 'called'
2244 enddef
2245 g:double_loaded = 'yes'
2246 END
2247 writefile(lines, 'Xdir/autoload/double.vim')
2248
2249 lines =<< trim END
2250 vim9script noclear
2251 if exists('g:script_loaded')
2252 finish
2253 endif
2254 g:script_loaded = true
2255
2256 import autoload 'double.vim'
2257 nnoremap <F3> <ScriptCmd>g:result = double.Func()<CR>
2258 END
2259 g:double_loaded = 'no'
2260 writefile(lines, 'Xloaddouble')
2261 source Xloaddouble
2262 assert_equal('no', g:double_loaded)
2263 assert_equal(true, g:script_loaded)
2264 source Xloaddouble
2265 feedkeys("\<F3>", 'xt')
2266 assert_equal('called', g:result)
2267 assert_equal('yes', g:double_loaded)
2268
2269 delete('Xloaddouble')
2270 unlet g:double_loaded
2271 unlet g:script_loaded
2272 unlet g:result
2273 delete('Xdir', 'rf')
2274 &rtp = save_rtp
2275enddef
2276
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002277def Test_vim9script_autoload_duplicate()
2278 mkdir('Xdir/autoload', 'p')
2279
2280 var lines =<< trim END
2281 vim9script
2282
2283 export def Func()
2284 enddef
2285
2286 def Func()
2287 enddef
2288 END
2289 writefile(lines, 'Xdir/autoload/dupfunc.vim')
2290 assert_fails('source Xdir/autoload/dupfunc.vim', 'E1073:')
2291
2292 lines =<< trim END
2293 vim9script
2294
2295 def Func()
2296 enddef
2297
2298 export def Func()
2299 enddef
2300 END
2301 writefile(lines, 'Xdir/autoload/dup2func.vim')
2302 assert_fails('source Xdir/autoload/dup2func.vim', 'E1073:')
2303
2304 lines =<< trim END
2305 vim9script
2306
2307 def Func()
2308 enddef
2309
2310 export var Func = 'asdf'
2311 END
2312 writefile(lines, 'Xdir/autoload/dup3func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002313 assert_fails('source Xdir/autoload/dup3func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002314
2315 lines =<< trim END
2316 vim9script
2317
2318 export var Func = 'asdf'
2319
2320 def Func()
2321 enddef
2322 END
2323 writefile(lines, 'Xdir/autoload/dup4func.vim')
2324 assert_fails('source Xdir/autoload/dup4func.vim', 'E707:')
2325
2326 lines =<< trim END
2327 vim9script
2328
2329 var Func = 'asdf'
2330
2331 export def Func()
2332 enddef
2333 END
2334 writefile(lines, 'Xdir/autoload/dup5func.vim')
2335 assert_fails('source Xdir/autoload/dup5func.vim', 'E707:')
2336
2337 lines =<< trim END
2338 vim9script
2339
2340 export def Func()
2341 enddef
2342
2343 var Func = 'asdf'
2344 END
2345 writefile(lines, 'Xdir/autoload/dup6func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002346 assert_fails('source Xdir/autoload/dup6func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002347
2348 delete('Xdir', 'rf')
2349enddef
2350
Bram Moolenaar2017d6f2022-01-20 19:38:46 +00002351def Test_autoload_missing_function_name()
2352 mkdir('Xdir/autoload', 'p')
2353
2354 var lines =<< trim END
2355 vim9script
2356
2357 def loadme#()
2358 enddef
2359 END
2360 writefile(lines, 'Xdir/autoload/loadme.vim')
2361 assert_fails('source Xdir/autoload/loadme.vim', 'E129:')
2362
2363 delete('Xdir', 'rf')
2364enddef
2365
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002366def Test_autoload_name_wrong()
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002367 var lines =<< trim END
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002368 def Xscriptname#Func()
2369 enddef
2370 END
2371 writefile(lines, 'Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002372 v9.CheckScriptFailure(lines, 'E746:')
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00002373 delete('Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002374
2375 mkdir('Xdir/autoload', 'p')
2376 lines =<< trim END
2377 vim9script
2378 def somescript#Func()
2379 enddef
2380 END
2381 writefile(lines, 'Xdir/autoload/somescript.vim')
2382 assert_fails('source Xdir/autoload/somescript.vim', 'E1263:')
2383
2384 delete('Xdir', 'rf')
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002385enddef
2386
Bram Moolenaard041f422022-01-12 19:54:00 +00002387def Test_import_autoload_postponed()
2388 mkdir('Xdir/autoload', 'p')
2389 var save_rtp = &rtp
2390 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2391
2392 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002393 vim9script
Bram Moolenaard041f422022-01-12 19:54:00 +00002394
2395 g:loaded_postponed = 'true'
2396 export var variable = 'bla'
2397 export def Function(): string
2398 return 'bla'
2399 enddef
2400 END
2401 writefile(lines, 'Xdir/autoload/postponed.vim')
2402
2403 lines =<< trim END
2404 vim9script
2405
2406 import autoload 'postponed.vim'
2407 def Tryit()
2408 echo postponed.variable
2409 echo postponed.Function()
2410 enddef
2411 defcompile
2412 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002413 v9.CheckScriptSuccess(lines)
Bram Moolenaard041f422022-01-12 19:54:00 +00002414 assert_false(exists('g:loaded_postponed'))
Bram Moolenaar62aec932022-01-29 21:45:34 +00002415 v9.CheckScriptSuccess(lines + ['Tryit()'])
Bram Moolenaard041f422022-01-12 19:54:00 +00002416 assert_equal('true', g:loaded_postponed)
2417
2418 unlet g:loaded_postponed
2419 delete('Xdir', 'rf')
2420 &rtp = save_rtp
2421enddef
2422
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002423def Test_import_autoload_override()
2424 mkdir('Xdir/autoload', 'p')
2425 var save_rtp = &rtp
2426 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2427 test_override('autoload', 1)
2428
2429 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002430 vim9script
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002431
2432 g:loaded_override = 'true'
2433 export var variable = 'bla'
2434 export def Function(): string
2435 return 'bla'
2436 enddef
2437 END
2438 writefile(lines, 'Xdir/autoload/override.vim')
2439
2440 lines =<< trim END
2441 vim9script
2442
2443 import autoload 'override.vim'
2444 assert_equal('true', g:loaded_override)
2445
2446 def Tryit()
2447 echo override.doesNotExist
2448 enddef
2449 defcompile
2450 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002451 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1)
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002452
2453 test_override('autoload', 0)
2454 unlet g:loaded_override
2455 delete('Xdir', 'rf')
2456 &rtp = save_rtp
2457enddef
2458
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002459def Test_autoload_mapping()
2460 mkdir('Xdir/autoload', 'p')
2461 var save_rtp = &rtp
2462 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2463
2464 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002465 vim9script
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002466
2467 g:toggle_loaded = 'yes'
2468
2469 export def Toggle(): string
2470 return ":g:toggle_called = 'yes'\<CR>"
2471 enddef
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002472 export def Doit()
2473 g:doit_called = 'yes'
2474 enddef
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002475 END
2476 writefile(lines, 'Xdir/autoload/toggle.vim')
2477
2478 lines =<< trim END
2479 vim9script
2480
2481 import autoload 'toggle.vim'
2482
2483 nnoremap <silent> <expr> tt toggle.Toggle()
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002484 nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR>
2485 nnoremap <silent> yy <Cmd>toggle.Doit()<CR>
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002486 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002487 v9.CheckScriptSuccess(lines)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002488 assert_false(exists("g:toggle_loaded"))
2489 assert_false(exists("g:toggle_called"))
Bram Moolenaar6079da72022-01-18 14:16:59 +00002490 assert_match('\d A: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002491
2492 feedkeys("tt", 'xt')
2493 assert_equal('yes', g:toggle_loaded)
2494 assert_equal('yes', g:toggle_called)
Bram Moolenaar6079da72022-01-18 14:16:59 +00002495 assert_match('\d: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002496
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002497 feedkeys("xx", 'xt')
2498 assert_equal('yes', g:doit_called)
2499
2500 assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle')
2501
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002502 nunmap tt
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002503 nunmap xx
2504 nunmap yy
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002505 unlet g:toggle_loaded
2506 unlet g:toggle_called
2507 delete('Xdir', 'rf')
2508 &rtp = save_rtp
2509enddef
2510
Bram Moolenaar160aa862022-01-10 21:29:57 +00002511def Test_vim9script_autoload_fails()
2512 var lines =<< trim END
2513 vim9script autoload
2514 var n = 0
2515 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002516 v9.CheckScriptFailure(lines, 'E475: Invalid argument: autoload')
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002517
2518 lines =<< trim END
2519 vim9script noclear noclear
2520 var n = 0
2521 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002522 v9.CheckScriptFailure(lines, 'E983: Duplicate argument: noclear')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002523enddef
2524
2525def Test_import_autoload_fails()
2526 var lines =<< trim END
2527 vim9script
2528 import autoload autoload 'prefixed.vim'
2529 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002530 v9.CheckScriptFailure(lines, 'E121: Undefined variable: autoload')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002531
2532 lines =<< trim END
2533 vim9script
Bram Moolenaar1836d612022-01-18 13:14:47 +00002534 import autoload './doesNotExist.vim'
Bram Moolenaar160aa862022-01-10 21:29:57 +00002535 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002536 v9.CheckScriptFailure(lines, 'E282:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002537
2538 lines =<< trim END
2539 vim9script
2540 import autoload '/dir/doesNotExist.vim'
2541 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002542 v9.CheckScriptFailure(lines, 'E282:', 2)
2543
2544 lines =<< trim END
2545 vim9script
2546 import autoload '../testdir'
2547 END
2548 v9.CheckScriptFailure(lines, 'E17:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002549
2550 lines =<< trim END
2551 vim9script
2552 import autoload 'doesNotExist.vim'
2553 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002554 v9.CheckScriptFailure(lines, 'E1053: Could not import "doesNotExist.vim"')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002555enddef
2556
2557" test disassembling an auto-loaded function starting with "debug"
2558def Test_vim9_autoload_disass()
2559 mkdir('Xdir/autoload', 'p')
2560 var save_rtp = &rtp
2561 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2562
2563 var lines =<< trim END
2564 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002565 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002566 return 'debug'
2567 enddef
2568 END
2569 writefile(lines, 'Xdir/autoload/debugit.vim')
2570
2571 lines =<< trim END
2572 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002573 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002574 return 'profile'
2575 enddef
2576 END
2577 writefile(lines, 'Xdir/autoload/profileit.vim')
2578
2579 lines =<< trim END
2580 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002581 assert_equal('debug', debugit#Test())
2582 disass debugit#Test
2583 assert_equal('profile', profileit#Test())
2584 disass profileit#Test
Bram Moolenaar160aa862022-01-10 21:29:57 +00002585 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002586 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002587
2588 delete('Xdir', 'rf')
2589 &rtp = save_rtp
2590enddef
2591
2592" test using a vim9script that is auto-loaded from an autocmd
2593def Test_vim9_aucmd_autoload()
2594 var lines =<< trim END
2595 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002596 export def Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002597 echomsg getreg('"')
2598 enddef
2599 END
2600
2601 mkdir('Xdir/autoload', 'p')
2602 writefile(lines, 'Xdir/autoload/foo.vim')
2603 var save_rtp = &rtp
2604 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2605 augroup test
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002606 autocmd TextYankPost * call foo#Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002607 augroup END
2608
2609 normal Y
2610
2611 augroup test
2612 autocmd!
2613 augroup END
2614 delete('Xdir', 'rf')
2615 &rtp = save_rtp
2616enddef
2617
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002618" test using a autoloaded file that is case sensitive
2619def Test_vim9_autoload_case_sensitive()
2620 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002621 vim9script
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002622 export def CaseSensitive(): string
2623 return 'done'
2624 enddef
2625 END
2626
2627 mkdir('Xdir/autoload', 'p')
2628 writefile(lines, 'Xdir/autoload/CaseSensitive.vim')
2629 var save_rtp = &rtp
2630 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2631
2632 lines =<< trim END
2633 vim9script
2634 import autoload 'CaseSensitive.vim'
2635 assert_equal('done', CaseSensitive.CaseSensitive())
2636 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002637 v9.CheckScriptSuccess(lines)
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002638
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002639 if !has('fname_case')
2640 lines =<< trim END
2641 vim9script
2642 import autoload 'CaseSensitive.vim'
2643 import autoload 'casesensitive.vim'
2644 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002645 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002646 endif
2647
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002648 delete('Xdir', 'rf')
2649 &rtp = save_rtp
2650enddef
2651
Bram Moolenaar160aa862022-01-10 21:29:57 +00002652" This was causing a crash because suppress_errthrow wasn't reset.
2653def Test_vim9_autoload_error()
2654 var lines =<< trim END
2655 vim9script
2656 def crash#func()
2657 try
2658 for x in List()
2659 endfor
2660 catch
2661 endtry
2662 g:ok = true
2663 enddef
2664 fu List()
2665 invalid
2666 endfu
2667 try
2668 alsoinvalid
2669 catch /wontmatch/
2670 endtry
2671 END
2672 call mkdir('Xruntime/autoload', 'p')
2673 call writefile(lines, 'Xruntime/autoload/crash.vim')
2674
2675 # run in a separate Vim to avoid the side effects of assert_fails()
2676 lines =<< trim END
2677 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
2678 call crash#func()
2679 call writefile(['ok'], 'Xdidit')
2680 qall!
2681 END
2682 writefile(lines, 'Xscript')
Bram Moolenaar62aec932022-01-29 21:45:34 +00002683 g:RunVim([], [], '-S Xscript')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002684 assert_equal(['ok'], readfile('Xdidit'))
2685
2686 delete('Xdidit')
2687 delete('Xscript')
2688 delete('Xruntime', 'rf')
2689
2690 lines =<< trim END
2691 vim9script
2692 var foo#bar = 'asdf'
2693 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002694 v9.CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002695enddef
2696
Bram Moolenaard8448622022-01-07 21:39:52 +00002697
2698" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker