blob: 159e51b2febaebe50b7cc1c59f18c815a5c5cc80 [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
Bram Moolenaard8448622022-01-07 21:39:52 +0000509 lines =<< trim END
510 vim9script
511 def TheFunc()
512 echo 'the func'
513 enddef
514 export var Ref = TheFunc
515 END
516 writefile([], 'Xthat.vim')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000517
Bram Moolenaard8448622022-01-07 21:39:52 +0000518 lines =<< trim END
519 import './Xthat.vim' as That
520 That()
521 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000522 v9.CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use That itself'])
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000523
524 lines =<< trim END
Bram Moolenaar937610b2022-01-19 17:21:29 +0000525 vim9script
526 import './Xthat.vim' as That
527 def Func()
528 echo That()
529 enddef
530 Func()
531 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000532 v9.CheckScriptFailure(lines, 'E1236: Cannot use That itself')
Bram Moolenaar937610b2022-01-19 17:21:29 +0000533
534 lines =<< trim END
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000535 import './Xthat.vim' as one
536 import './Xthat.vim' as two
537 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000538 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000539
540 delete('Xthat.vim')
Bram Moolenaar779aeff2022-02-08 19:12:19 +0000541
542 lines =<< trim END
543 vim9script
544 export var item = 'hello'
545 import './Xyourself.vim'
546 END
547 writefile(lines, 'Xyourself.vim')
548 assert_fails('source Xyourself.vim', 'E1088:')
549 delete('Xyourself.vim')
550
Bram Moolenaard8448622022-01-07 21:39:52 +0000551 mkdir('Ximport')
552
553 writefile(['vim9script'], 'Ximport/.vim')
554 lines =<< trim END
555 vim9script
556 import './Ximport/.vim'
557 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000558 v9.CheckScriptFailure(lines, 'E1261: Cannot import .vim without using "as"')
Bram Moolenaard8448622022-01-07 21:39:52 +0000559 lines =<< trim END
560 vim9script
561 import './Ximport/.vim' as vim
562 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000563 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000564
565 writefile(['vim9script'], 'Ximport/.vimrc')
566 lines =<< trim END
567 vim9script
568 import './Ximport/.vimrc'
569 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000570 v9.CheckScriptFailure(lines, 'E1257: Imported script must use "as" or end in .vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000571 lines =<< trim END
572 vim9script
573 import './Ximport/.vimrc' as vimrc
574 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000575 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000576
Yegappan Lakshmanan885de442022-04-23 10:51:14 +0100577 new
578 setline(1, ['vim9script', 'import "" as abc'])
579 assert_fails('source', 'E1071: Invalid string for :import: "" as abc')
580 setline(2, 'import [] as abc')
581 assert_fails('source', 'E1071: Invalid string for :import: [] as abc')
582 setline(2, 'import test_null_string() as abc')
583 assert_fails('source', 'E1071: Invalid string for :import: test_null_string() as abc')
584 bw!
585 call writefile(['vim9script', "import './Xfoo.vim' ask expo"], 'Xbar.vim')
586 assert_fails('source Xbar.vim', 'E488: Trailing characters: ask expo')
587 writefile([], 'Xtemp')
588 call writefile(['vim9script', "import './Xtemp'"], 'Xbar.vim')
589 assert_fails('source Xbar.vim', 'E1257: Imported script must use "as" or end in .vim: Xtemp')
590 delete('Xtemp')
591 call writefile(['vim9script', "import './Xfoo.vim' as abc | foobar"], 'Xbar.vim')
592 assert_fails('source Xbar.vim', 'E492: Not an editor command: foobar')
593 call delete('Xbar.vim')
594
Bram Moolenaard8448622022-01-07 21:39:52 +0000595 delete('Ximport', 'rf')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +0100596 delete('Xfoo.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000597enddef
598
599func g:Trigger()
600 source Ximport.vim
601 return "echo 'yes'\<CR>"
602endfunc
603
604def Test_import_export_expr_map()
605 # check that :import and :export work when buffer is locked
606 var export_lines =<< trim END
607 vim9script
608 export def That(): string
609 return 'yes'
610 enddef
611 END
612 writefile(export_lines, 'Xexport_that.vim')
613
614 var import_lines =<< trim END
615 vim9script
616 import './Xexport_that.vim' as that
617 assert_equal('yes', that.That())
618 END
619 writefile(import_lines, 'Ximport.vim')
620
621 nnoremap <expr> trigger g:Trigger()
622 feedkeys('trigger', "xt")
623
624 delete('Xexport_that.vim')
625 delete('Ximport.vim')
626 nunmap trigger
627enddef
628
629def Test_import_in_filetype()
630 # check that :import works when the buffer is locked
631 mkdir('ftplugin', 'p')
632 var export_lines =<< trim END
633 vim9script
634 export var That = 'yes'
635 END
636 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
637
638 var import_lines =<< trim END
639 vim9script
640 import './Xexport_ft.vim' as ft
641 assert_equal('yes', ft.That)
642 g:did_load_mytpe = 1
643 END
644 writefile(import_lines, 'ftplugin/qf.vim')
645
646 var save_rtp = &rtp
647 &rtp = getcwd() .. ',' .. &rtp
648
649 filetype plugin on
650 copen
651 assert_equal(1, g:did_load_mytpe)
652
653 quit!
654 delete('Xexport_ft.vim')
655 delete('ftplugin', 'rf')
656 &rtp = save_rtp
657enddef
658
659def Test_use_import_in_mapping()
660 var lines =<< trim END
661 vim9script
Bram Moolenaar89445512022-04-14 12:58:23 +0100662 export def Funcx(nr: number)
663 g:result = nr
Bram Moolenaard8448622022-01-07 21:39:52 +0000664 enddef
665 END
666 writefile(lines, 'XsomeExport.vim')
667 lines =<< trim END
668 vim9script
669 import './XsomeExport.vim' as some
670 var Funcy = some.Funcx
Bram Moolenaar89445512022-04-14 12:58:23 +0100671 nnoremap <F3> :call <sid>Funcy(42)<cr>
672 nnoremap <F4> :call <sid>some.Funcx(44)<cr>
Bram Moolenaard8448622022-01-07 21:39:52 +0000673 END
674 writefile(lines, 'Xmapscript.vim')
675
676 source Xmapscript.vim
677 feedkeys("\<F3>", "xt")
678 assert_equal(42, g:result)
Bram Moolenaar89445512022-04-14 12:58:23 +0100679 feedkeys("\<F4>", "xt")
680 assert_equal(44, g:result)
Bram Moolenaard8448622022-01-07 21:39:52 +0000681
682 unlet g:result
683 delete('XsomeExport.vim')
684 delete('Xmapscript.vim')
685 nunmap <F3>
Bram Moolenaar89445512022-04-14 12:58:23 +0100686 nunmap <F4>
687enddef
688
Bram Moolenaar648dd882022-04-14 21:36:15 +0100689def Test_use_relative_autoload_import_in_mapping()
Bram Moolenaar89445512022-04-14 12:58:23 +0100690 var lines =<< trim END
691 vim9script
692 export def Func()
693 g:result = 42
694 enddef
695 END
Bram Moolenaar648dd882022-04-14 21:36:15 +0100696 writefile(lines, 'XrelautoloadExport.vim')
Bram Moolenaar89445512022-04-14 12:58:23 +0100697 lines =<< trim END
698 vim9script
Bram Moolenaar648dd882022-04-14 21:36:15 +0100699 import autoload './XrelautoloadExport.vim' as some
Bram Moolenaar89445512022-04-14 12:58:23 +0100700 nnoremap <F3> :call <SID>some.Func()<CR>
701 END
702 writefile(lines, 'Xmapscript.vim')
703
704 source Xmapscript.vim
Bram Moolenaar648dd882022-04-14 21:36:15 +0100705 assert_match('\d\+ A: .*XrelautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
Bram Moolenaar89445512022-04-14 12:58:23 +0100706 feedkeys("\<F3>", "xt")
707 assert_equal(42, g:result)
708
709 unlet g:result
Bram Moolenaar648dd882022-04-14 21:36:15 +0100710 delete('XrelautoloadExport.vim')
Bram Moolenaar89445512022-04-14 12:58:23 +0100711 delete('Xmapscript.vim')
712 nunmap <F3>
Bram Moolenaard8448622022-01-07 21:39:52 +0000713enddef
714
Bram Moolenaar648dd882022-04-14 21:36:15 +0100715def Test_use_autoload_import_in_mapping()
716 var lines =<< trim END
717 vim9script
718 export def Func()
719 g:result = 49
720 enddef
721 END
722 mkdir('Xdir/autoload', 'p')
723 writefile(lines, 'Xdir/autoload/XautoloadExport.vim')
724 var save_rtp = &rtp
725 exe 'set rtp^=' .. getcwd() .. '/Xdir'
726
727 lines =<< trim END
728 vim9script
729 import autoload 'XautoloadExport.vim' as some
730 nnoremap <F3> :call <SID>some.Func()<CR>
731 END
732 writefile(lines, 'Xmapscript.vim')
733
734 source Xmapscript.vim
735 assert_match('\d\+ A: .*autoload/XautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
736 feedkeys("\<F3>", "xt")
737 assert_equal(49, g:result)
738
739 unlet g:result
740 delete('Xmapscript.vim')
741 nunmap <F3>
742 delete('Xdir', 'rf')
743 &rtp = save_rtp
744enddef
745
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000746def Test_use_import_in_command_completion()
Bram Moolenaar15d16352022-01-17 20:09:08 +0000747 var lines =<< trim END
748 vim9script
749 export def Complete(..._): list<string>
750 return ['abcd']
751 enddef
752 END
753 writefile(lines, 'Xscript.vim')
754
755 lines =<< trim END
756 vim9script
757 import './Xscript.vim'
758
759 command -nargs=1 -complete=customlist,Xscript.Complete Cmd echo 'ok'
760 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
761 assert_equal('#Cmd abcd', @:)
762 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000763 v9.CheckScriptSuccess(lines)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000764
765 delcommand Cmd
766 delete('Xscript.vim')
767enddef
768
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100769def Test_use_import_with_funcref_in_command_completion()
770 var lines =<< trim END
771 vim9script
772 export def Complete(..._): list<string>
773 return ['abcd']
774 enddef
775 END
776 writefile(lines, 'Xscript.vim')
777
778 lines =<< trim END
779 vim9script
780 import './Xscript.vim'
781
782 var Ref = Xscript.Complete
783 exe "command -nargs=1 -complete=customlist," .. expand('<SID>') .. "Ref Cmd echo 'ok'"
784 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
785 assert_equal('#Cmd abcd', @:)
786 END
787 v9.CheckScriptSuccess(lines)
788
789 delcommand Cmd
790 delete('Xscript.vim')
791enddef
792
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000793def Test_use_autoload_import_in_insert_completion()
794 mkdir('Xdir/autoload', 'p')
795 var save_rtp = &rtp
796 exe 'set rtp^=' .. getcwd() .. '/Xdir'
797
798 var lines =<< trim END
799 vim9script
800 export def ThesaurusFunc(findbase: bool, _): any
801 if findbase
802 return 1
803 endif
804 return [
805 'check',
806 'experiment',
807 'test',
808 'verification'
809 ]
810 enddef
811 g:completion_loaded = 'yes'
812 END
813 writefile(lines, 'Xdir/autoload/completion.vim')
814
815 new
816 lines =<< trim END
817 vim9script
818 g:completion_loaded = 'no'
819 import autoload 'completion.vim'
820 set thesaurusfunc=completion.ThesaurusFunc
821 assert_equal('no', g:completion_loaded)
822 feedkeys("i\<C-X>\<C-T>\<C-N>\<Esc>", 'xt')
823 assert_equal('experiment', getline(1))
824 assert_equal('yes', g:completion_loaded)
825 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000826 v9.CheckScriptSuccess(lines)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000827
828 set thesaurusfunc=
829 bwipe!
830 delete('Xdir', 'rf')
831 &rtp = save_rtp
832enddef
833
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000834def Test_use_autoload_import_partial_in_opfunc()
835 mkdir('Xdir/autoload', 'p')
836 var save_rtp = &rtp
837 exe 'set rtp^=' .. getcwd() .. '/Xdir'
838
839 var lines =<< trim END
840 vim9script
841 export def Opfunc(..._)
842 g:opfunc_called = 'yes'
843 enddef
844 END
845 writefile(lines, 'Xdir/autoload/opfunc.vim')
846
847 new
848 lines =<< trim END
849 vim9script
850 import autoload 'opfunc.vim'
851 nnoremap <expr> <F3> TheFunc()
852 def TheFunc(): string
853 &operatorfunc = function('opfunc.Opfunc', [0])
854 return 'g@'
855 enddef
856 feedkeys("\<F3>l", 'xt')
857 assert_equal('yes', g:opfunc_called)
858 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000859 v9.CheckScriptSuccess(lines)
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000860
861 set opfunc=
862 bwipe!
863 delete('Xdir', 'rf')
Bram Moolenaar06b77222022-01-25 15:51:56 +0000864 nunmap <F3>
865 &rtp = save_rtp
866enddef
867
868def Test_set_opfunc_to_autoload_func_directly()
869 mkdir('Xdir/autoload', 'p')
870 var save_rtp = &rtp
871 exe 'set rtp^=' .. getcwd() .. '/Xdir'
872
873 var lines =<< trim END
874 vim9script
875 export def Opfunc(..._)
876 g:opfunc_called = 'yes'
877 enddef
878 END
879 writefile(lines, 'Xdir/autoload/opfunc.vim')
880
881 new
882 lines =<< trim END
883 vim9script
884 import autoload 'opfunc.vim'
885 nnoremap <expr> <F3> TheFunc()
886 def TheFunc(): string
887 &operatorfunc = opfunc.Opfunc
888 return 'g@'
889 enddef
890 feedkeys("\<F3>l", 'xt')
891 assert_equal('yes', g:opfunc_called)
892 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000893 v9.CheckScriptSuccess(lines)
Bram Moolenaar06b77222022-01-25 15:51:56 +0000894
895 set opfunc=
896 bwipe!
897 delete('Xdir', 'rf')
898 nunmap <F3>
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000899 &rtp = save_rtp
900enddef
901
Bram Moolenaare70dd112022-01-21 16:31:11 +0000902def Test_use_autoload_import_in_fold_expression()
903 mkdir('Xdir/autoload', 'p')
904 var save_rtp = &rtp
905 exe 'set rtp^=' .. getcwd() .. '/Xdir'
906
907 var lines =<< trim END
908 vim9script
909 export def Expr(): string
910 return getline(v:lnum) =~ '^#' ? '>1' : '1'
911 enddef
Bram Moolenaar9530b582022-01-22 13:39:08 +0000912 export def Text(): string
913 return 'fold text'
914 enddef
Bram Moolenaare70dd112022-01-21 16:31:11 +0000915 g:fold_loaded = 'yes'
916 END
917 writefile(lines, 'Xdir/autoload/fold.vim')
918
919 lines =<< trim END
920 vim9script
921 import autoload 'fold.vim'
922 &foldexpr = 'fold.Expr()'
Bram Moolenaar9530b582022-01-22 13:39:08 +0000923 &foldtext = 'fold.Text()'
Bram Moolenaare70dd112022-01-21 16:31:11 +0000924 &foldmethod = 'expr'
925 &debug = 'throw'
926 END
927 new
928 setline(1, ['# one', 'text', '# two', 'text'])
929 g:fold_loaded = 'no'
Bram Moolenaar62aec932022-01-29 21:45:34 +0000930 v9.CheckScriptSuccess(lines)
Bram Moolenaare70dd112022-01-21 16:31:11 +0000931 assert_equal('no', g:fold_loaded)
932 redraw
933 assert_equal('yes', g:fold_loaded)
934
935 # Check that script context of 'foldexpr' is copied to another buffer.
936 edit! otherfile
937 redraw
938
Bram Moolenaar9530b582022-01-22 13:39:08 +0000939 set foldexpr= foldtext& foldmethod& debug=
Bram Moolenaare70dd112022-01-21 16:31:11 +0000940 bwipe!
941 delete('Xdir', 'rf')
942 &rtp = save_rtp
943enddef
944
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100945def Test_autoload_import_relative()
946 var lines =<< trim END
947 vim9script
948
949 g:loaded = 'yes'
950 export def RelFunc(): string
951 return 'relfunc'
952 enddef
953 def NotExported()
954 echo 'not'
955 enddef
956
957 export var someText = 'some text'
958 var notexp = 'bad'
959 END
960 writefile(lines, 'XimportRel.vim')
961 writefile(lines, 'XimportRel2.vim')
962 writefile(lines, 'XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +0100963 writefile(lines, 'XimportRel4.vim')
964 writefile(lines, 'XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100965
966 lines =<< trim END
967 vim9script
968 g:loaded = 'no'
969 import autoload './XimportRel.vim'
970 assert_equal('no', g:loaded)
971
972 def AFunc(): string
973 var res = ''
974 res ..= XimportRel.RelFunc()
975 res ..= '/'
976 res ..= XimportRel.someText
977 XimportRel.someText = 'from AFunc'
978 return res
979 enddef
980 # script not loaded when compiling
981 defcompile
982 assert_equal('no', g:loaded)
983
984 assert_equal('relfunc/some text', AFunc())
985 assert_equal('yes', g:loaded)
986 unlet g:loaded
987
988 assert_equal('from AFunc', XimportRel.someText)
989 XimportRel.someText = 'from script'
990 assert_equal('from script', XimportRel.someText)
991 END
992 v9.CheckScriptSuccess(lines)
993
994 lines =<< trim END
995 vim9script
996 import autoload './XimportRel.vim'
997 echo XimportRel.NotExported()
998 END
999 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 3)
1000
1001 lines =<< trim END
1002 vim9script
1003 import autoload './XimportRel.vim'
1004 echo XimportRel.notexp
1005 END
1006 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
1007
1008 lines =<< trim END
1009 vim9script
1010 import autoload './XimportRel.vim'
1011 XimportRel.notexp = 'bad'
1012 END
1013 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
1014
1015 lines =<< trim END
1016 vim9script
1017 import autoload './XimportRel.vim'
1018 def Func()
1019 echo XimportRel.NotExported()
1020 enddef
1021 Func()
1022 END
1023 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 1)
1024
1025 lines =<< trim END
1026 vim9script
1027 import autoload './XimportRel.vim'
1028 def Func()
1029 echo XimportRel.notexp
1030 enddef
1031 Func()
1032 END
1033 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1034
Bram Moolenaar10611952022-04-03 21:11:34 +01001035 # Same, script not imported before
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001036 lines =<< trim END
1037 vim9script
Bram Moolenaar10611952022-04-03 21:11:34 +01001038 import autoload './XimportRel4.vim'
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001039 def Func()
Bram Moolenaar10611952022-04-03 21:11:34 +01001040 echo XimportRel4.notexp
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001041 enddef
1042 Func()
1043 END
1044 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1045
Bram Moolenaar10611952022-04-03 21:11:34 +01001046 # does not fail if the script wasn't loaded yet and only compiling
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001047 g:loaded = 'no'
1048 lines =<< trim END
1049 vim9script
1050 import autoload './XimportRel2.vim'
1051 def Func()
1052 echo XimportRel2.notexp
1053 enddef
1054 defcompile
1055 END
1056 v9.CheckScriptSuccess(lines)
1057 assert_equal('no', g:loaded)
1058
Bram Moolenaar10611952022-04-03 21:11:34 +01001059 lines =<< trim END
1060 vim9script
1061 import autoload './XimportRel.vim'
1062 def Func()
1063 XimportRel.notexp = 'bad'
1064 enddef
1065 Func()
1066 END
1067 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1068
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001069 # fails with a not loaded import
1070 lines =<< trim END
1071 vim9script
1072 import autoload './XimportRel3.vim'
1073 def Func()
1074 XimportRel3.notexp = 'bad'
1075 enddef
1076 Func()
1077 END
1078 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1079 assert_equal('yes', g:loaded)
1080 unlet g:loaded
1081
Bram Moolenaar10611952022-04-03 21:11:34 +01001082 lines =<< trim END
1083 vim9script
1084 import autoload './XimportRel5.vim'
1085 def Func()
1086 XimportRel5.nosuchvar = 'bad'
1087 enddef
1088 Func()
1089 END
1090 v9.CheckScriptFailure(lines, 'E121: Undefined variable: nosuchvar', 1)
1091 unlet g:loaded
1092
1093 # nasty: delete script after compiling function
1094 writefile(['vim9script'], 'XimportRelDel.vim')
1095 lines =<< trim END
1096 vim9script
1097
1098 import autoload './XimportRelDel.vim'
1099 def DoIt()
1100 echo XimportRelDel.var
1101 enddef
1102 defcompile
1103 delete('XimportRelDel.vim')
1104 DoIt()
1105 END
Bram Moolenaar242c1522022-04-03 21:52:51 +01001106 v9.CheckScriptFailure(lines, 'E484:')
Bram Moolenaar10611952022-04-03 21:11:34 +01001107
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001108 delete('XimportRel.vim')
1109 delete('XimportRel2.vim')
1110 delete('XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +01001111 delete('XimportRel4.vim')
1112 delete('XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001113enddef
1114
Bram Moolenaarccbfd482022-03-31 16:18:23 +01001115def Test_autoload_import_relative_autoload_dir()
1116 mkdir('autoload', 'p')
1117 var lines =<< trim END
1118 vim9script
1119 export def Bar()
1120 g:called_bar = 'yes'
1121 enddef
1122 END
1123 writefile(lines, 'autoload/script.vim')
1124
1125 lines =<< trim END
1126 vim9script
1127 import autoload './autoload/script.vim'
1128 def Foo()
1129 script.Bar()
1130 enddef
1131 Foo()
1132 assert_equal('yes', g:called_bar)
1133 END
1134 v9.CheckScriptSuccess(lines)
1135
1136 unlet g:called_bar
1137 delete('autoload', 'rf')
1138enddef
1139
Bram Moolenaaraac12da2022-04-24 21:33:20 +01001140def Test_autoload_import_deleted()
1141 var lines =<< trim END
1142 vim9script
1143 export const FOO = 1
1144 END
1145 writefile(lines, 'Xa.vim')
1146
1147 lines =<< trim END
1148 vim9script
1149 import autoload './Xa.vim'
1150
1151 delete('Xa.vim')
1152 var x = Xa.FOO
1153 END
1154 v9.CheckScriptFailure(lines, 'E484:')
1155
1156 delete('Xdir', 'rf')
1157enddef
1158
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001159func Test_import_in_diffexpr()
1160 CheckExecutable diff
1161
1162 call Run_Test_import_in_diffexpr()
1163endfunc
1164
1165def Run_Test_import_in_diffexpr()
1166 var lines =<< trim END
1167 vim9script
1168
1169 export def DiffExpr()
1170 # Prepend some text to check diff type detection
1171 writefile(['warning', ' message'], v:fname_out)
1172 silent exe '!diff ' .. v:fname_in .. ' '
1173 .. v:fname_new .. '>>' .. v:fname_out
1174 enddef
1175 END
1176 writefile(lines, 'Xdiffexpr')
1177
1178 lines =<< trim END
1179 vim9script
1180 import './Xdiffexpr' as diff
1181
1182 set diffexpr=diff.DiffExpr()
1183 set diffopt=foldcolumn:0
1184 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001185 v9.CheckScriptSuccess(lines)
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001186
1187 enew!
1188 call setline(1, ['one', 'two', 'three'])
1189 diffthis
1190
1191 botright vert new
1192 call setline(1, ['one', 'two', 'three.'])
1193 diffthis
1194 # we only check if this does not cause errors
1195 redraw
1196
1197 diffoff!
1198 bwipe!
1199 bwipe!
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001200 delete('Xdiffexpr')
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001201enddef
1202
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001203def Test_import_in_patchexpr()
1204 var lines =<< trim END
1205 vim9script
1206 export def TPatch()
1207 call writefile(['output file'], v:fname_out)
1208 enddef
1209 END
1210 writefile(lines, 'Xpatchexpr')
1211
1212 lines =<< trim END
1213 vim9script
1214 import './Xpatchexpr' as patch
1215 set patchexpr=patch.TPatch()
1216 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001217 v9.CheckScriptSuccess(lines)
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001218
1219 call writefile(['input file'], 'Xinput')
1220 call writefile(['diff file'], 'Xdiff')
1221 :%bwipe!
1222 edit Xinput
1223 diffpatch Xdiff
1224 call assert_equal('output file', getline(1))
1225
1226 call delete('Xinput')
1227 call delete('Xdiff')
1228 call delete('Xpatchexpr')
1229 set patchexpr&
1230 :%bwipe!
1231enddef
1232
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001233def Test_import_in_formatexpr()
1234 var lines =<< trim END
1235 vim9script
1236 export def MyFormatExpr(): number
1237 g:did_format = 'yes'
1238 return 0
1239 enddef
1240 END
1241 writefile(lines, 'Xformatter')
1242
1243 lines =<< trim END
1244 vim9script
1245 import './Xformatter' as format
1246 set formatexpr=format.MyFormatExpr()
1247 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001248 v9.CheckScriptSuccess(lines)
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001249
1250 new
1251 setline(1, ['a', 'b', 'c'])
1252 normal gqG
1253 assert_equal('yes', g:did_format)
1254
1255 bwipe!
1256 delete('Xformatter')
1257 unlet g:did_format
1258 set formatexpr=
1259enddef
1260
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001261def Test_import_in_includeexpr()
1262 writefile(['found it'], 'Xthisfile')
1263 new
1264
1265 var lines =<< trim END
1266 vim9script
1267 export def DoSub(): string
1268 return substitute(v:fname, 'that', 'this', '')
1269 enddef
1270 END
1271 writefile(lines, 'Xinclude.vim')
1272
1273 lines =<< trim END
1274 vim9script
1275 import './Xinclude.vim'
1276 set includeexpr=Xinclude.DoSub()
1277 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001278 v9.CheckScriptSuccess(lines)
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001279
1280 setline(1, ['Xthatfile'])
1281 exe "normal \<C-W>f"
1282 assert_equal('Xthisfile', expand('%'))
1283
1284 bwipe!
1285 bwipe!
1286 set includeexpr=
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001287 delete('Xinclude.vim')
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001288 delete('Xthisfile')
1289enddef
1290
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001291def Test_import_in_indentexpr()
1292 var lines =<< trim END
1293 vim9script
1294 export def GetIndent(): number
1295 return 5
1296 enddef
1297 END
1298 writefile(lines, 'Xindenter')
1299
1300 lines =<< trim END
1301 vim9script
1302 import './Xindenter' as indent
1303 set indentexpr=indent.GetIndent()
1304 set debug=throw
1305 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001306 v9.CheckScriptSuccess(lines)
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001307
1308 new
1309 setline(1, 'hello')
1310 normal ==
1311 assert_equal(' hello', getline(1))
1312
1313 bwipe!
1314 set indentexpr= debug=
1315 delete('Xindenter')
1316enddef
1317
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001318func Test_import_in_printexpr()
1319 CheckFeature postscript
1320 call Run_Test_import_in_printexpr()
1321endfunc
1322
1323def Run_Test_import_in_printexpr()
1324 var lines =<< trim END
1325 vim9script
1326 export def PrintFile(): bool
1327 g:printed = 'yes'
1328 delete('v:fname_in')
1329 return false
1330 enddef
1331 END
1332 writefile(lines, 'Xprint.vim')
1333
1334 lines =<< trim END
1335 vim9script
1336 import './Xprint.vim'
1337 set printexpr=Xprint.PrintFile()
1338 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001339 v9.CheckScriptSuccess(lines)
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001340
1341 help
1342 hardcopy dummy args
1343 assert_equal('yes', g:printed)
1344
1345 delete('Xprint.vim')
1346 set printexpr=
1347enddef
1348
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001349def Test_import_in_charconvert()
1350 var lines =<< trim END
1351 vim9script
1352 export def MakeUpper(): bool
1353 var data = readfile(v:fname_in)
1354 map(data, 'toupper(v:val)')
1355 writefile(data, v:fname_out)
1356 return false # success
1357 enddef
1358 END
1359 writefile(lines, 'Xconvert.vim')
1360
1361 lines =<< trim END
1362 vim9script
1363 import './Xconvert.vim' as conv
1364 set charconvert=conv.MakeUpper()
1365 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001366 v9.CheckScriptSuccess(lines)
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001367
1368 writefile(['one', 'two'], 'Xfile')
1369 new Xfile
1370 write ++enc=ucase Xfile1
1371 assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1372
1373 delete('Xfile')
1374 delete('Xfile1')
1375 delete('Xconvert.vim')
1376 bwipe!
1377 set charconvert&
1378enddef
1379
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001380func Test_import_in_spellsuggest_expr()
1381 CheckFeature spell
1382 call Run_Test_import_in_spellsuggest_expr()
1383endfunc
1384
1385def Run_Test_import_in_spellsuggest_expr()
1386 var lines =<< trim END
1387 vim9script
1388 export def MySuggest(): list<any>
1389 return [['Fox', 8], ['Fop', 9]]
1390 enddef
1391 END
1392 writefile(lines, 'Xsuggest.vim')
1393
1394 lines =<< trim END
1395 vim9script
1396 import './Xsuggest.vim' as sugg
1397 set spell spellsuggest=expr:sugg.MySuggest()
1398 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001399 v9.CheckScriptSuccess(lines)
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001400
1401 set verbose=1 # report errors
1402 call assert_equal(['Fox', 'Fop'], spellsuggest('Fo', 2))
1403
1404 delete('Xsuggest.vim')
1405 set nospell spellsuggest& verbose=0
1406enddef
1407
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001408def Test_export_shadows_global_function()
1409 mkdir('Xdir/autoload', 'p')
1410 var save_rtp = &rtp
1411 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1412
1413 var lines =<< trim END
1414 vim9script
1415 export def Shadow(): string
1416 return 'Shadow()'
1417 enddef
1418 END
1419 writefile(lines, 'Xdir/autoload/shadow.vim')
1420
1421 lines =<< trim END
1422 vim9script
1423
1424 def g:Shadow(): string
1425 return 'global'
1426 enddef
1427
1428 import autoload 'shadow.vim'
1429 assert_equal('Shadow()', shadow.Shadow())
1430 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001431 v9.CheckScriptSuccess(lines)
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001432
1433 delfunc g:Shadow
1434 bwipe!
1435 delete('Xdir', 'rf')
1436 &rtp = save_rtp
1437enddef
1438
Bram Moolenaard8448622022-01-07 21:39:52 +00001439def Test_export_fails()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001440 v9.CheckScriptFailure(['export var some = 123'], 'E1042:')
1441 v9.CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
1442 v9.CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +01001443 v9.CheckScriptFailure(['vim9script', 'export function /a1b2c3'], 'E1044:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001444
1445 assert_fails('export something', 'E1043:')
1446enddef
1447
1448func Test_import_fails_without_script()
1449 CheckRunVimInTerminal
1450
1451 " call indirectly to avoid compilation error for missing functions
1452 call Run_Test_import_fails_on_command_line()
1453endfunc
1454
1455def Run_Test_import_fails_on_command_line()
1456 var export =<< trim END
1457 vim9script
1458 export def Foo(): number
1459 return 0
1460 enddef
1461 END
1462 writefile(export, 'XexportCmd.vim')
1463
Bram Moolenaar62aec932022-01-29 21:45:34 +00001464 var buf = g:RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaard8448622022-01-07 21:39:52 +00001465 rows: 6, wait_for_ruler: 0})
Bram Moolenaar62aec932022-01-29 21:45:34 +00001466 g:WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaard8448622022-01-07 21:39:52 +00001467
1468 delete('XexportCmd.vim')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001469 g:StopVimInTerminal(buf)
Bram Moolenaard8448622022-01-07 21:39:52 +00001470enddef
1471
1472def Test_vim9_reload_noclear()
1473 var lines =<< trim END
1474 vim9script
1475 export var exported = 'thexport'
1476
1477 export def TheFunc(x = 0)
1478 enddef
1479 END
1480 writefile(lines, 'XExportReload')
1481 lines =<< trim END
1482 vim9script noclear
1483 g:loadCount += 1
Bram Moolenaara749a422022-02-12 19:52:25 +00001484 var reloaded = 'init'
Bram Moolenaard8448622022-01-07 21:39:52 +00001485 import './XExportReload' as exp
1486
1487 def Again(): string
1488 return 'again'
1489 enddef
1490
1491 exp.TheFunc()
1492
Bram Moolenaara749a422022-02-12 19:52:25 +00001493 if exists('loaded') | finish | endif
1494 var loaded = true
Bram Moolenaard8448622022-01-07 21:39:52 +00001495
Bram Moolenaara749a422022-02-12 19:52:25 +00001496 var notReloaded = 'yes'
1497 reloaded = 'first'
Bram Moolenaard8448622022-01-07 21:39:52 +00001498 def g:Values(): list<string>
Bram Moolenaara749a422022-02-12 19:52:25 +00001499 return [reloaded, notReloaded, Again(), Once(), exp.exported]
Bram Moolenaard8448622022-01-07 21:39:52 +00001500 enddef
1501
1502 def Once(): string
1503 return 'once'
1504 enddef
1505 END
1506 writefile(lines, 'XReloaded')
1507 g:loadCount = 0
1508 source XReloaded
1509 assert_equal(1, g:loadCount)
1510 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
1511 source XReloaded
1512 assert_equal(2, g:loadCount)
1513 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1514 source XReloaded
1515 assert_equal(3, g:loadCount)
1516 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1517
1518 delete('XReloaded')
1519 delete('XExportReload')
1520 delfunc g:Values
1521 unlet g:loadCount
1522
1523 lines =<< trim END
1524 vim9script
1525 def Inner()
1526 enddef
1527 END
1528 lines->writefile('XreloadScript.vim')
1529 source XreloadScript.vim
1530
1531 lines =<< trim END
1532 vim9script
1533 def Outer()
1534 def Inner()
1535 enddef
1536 enddef
1537 defcompile
1538 END
1539 lines->writefile('XreloadScript.vim')
1540 source XreloadScript.vim
1541
1542 delete('XreloadScript.vim')
1543enddef
1544
Bram Moolenaarcd1cda22022-02-16 21:48:25 +00001545def Test_vim_reload_noclear_arg_count()
1546 var lines =<< trim END
1547 vim9script noclear
1548
1549 if !exists('g:didload')
1550 def Test(a: string, b: string)
1551 echo a b
1552 enddef
1553 def Call()
1554 Test('a', 'b')
1555 enddef
1556 else
1557 # redefine with one argument less
1558 def Test(a: string)
1559 echo a
1560 enddef
1561 endif
1562 Call()
1563 g:didload = 1
1564 END
1565 lines->writefile('XreloadScript_1.vim')
1566 source XreloadScript_1.vim
1567 assert_fails('source XreloadScript_1.vim', 'E1106: One argument too many')
1568 unlet g:didload
1569
1570 lines =<< trim END
1571 vim9script noclear
1572
1573 if !exists('g:didload')
1574 def Test(a: string, b: string, c: string)
1575 echo a b
1576 enddef
1577 def Call()
1578 Test('a', 'b', 'c')
1579 enddef
1580 else
1581 # redefine with one argument less
1582 def Test(a: string)
1583 echo a
1584 enddef
1585 endif
1586 Call()
1587 g:didload = 1
1588 END
1589 lines->writefile('XreloadScript_2.vim')
1590 source XreloadScript_2.vim
1591 assert_fails('source XreloadScript_2.vim', 'E1106: 2 arguments too many')
1592 unlet g:didload
1593
1594 lines =<< trim END
1595 vim9script noclear
1596
1597 if !exists('g:didload')
1598 def Test(a: string)
1599 echo a
1600 enddef
1601 def Call()
1602 Test('a')
1603 enddef
1604 else
1605 # redefine with one argument extra
1606 def Test(a: string, b: string)
1607 echo a b
1608 enddef
1609 endif
1610 Call()
1611 g:didload = 1
1612 END
1613 lines->writefile('XreloadScript_3.vim')
1614 source XreloadScript_3.vim
1615 assert_fails('source XreloadScript_3.vim', 'E1190: One argument too few')
1616 unlet g:didload
1617
1618 lines =<< trim END
1619 vim9script noclear
1620
1621 if !exists('g:didload')
1622 def Test(a: string)
1623 echo a
1624 enddef
1625 def Call()
1626 Test('a')
1627 enddef
1628 else
1629 # redefine with two arguments extra
1630 def Test(a: string, b: string, c: string)
1631 echo a b
1632 enddef
1633 endif
1634 Call()
1635 g:didload = 1
1636 END
1637 lines->writefile('XreloadScript_4.vim')
1638 source XreloadScript_4.vim
1639 assert_fails('source XreloadScript_4.vim', 'E1190: 2 arguments too few')
1640 unlet g:didload
1641
1642 delete('XreloadScript_1.vim')
1643 delete('XreloadScript_2.vim')
1644 delete('XreloadScript_3.vim')
1645 delete('XreloadScript_4.vim')
1646enddef
1647
1648def Test_vim9_reload_noclear_error()
1649 var lines =<< trim END
1650 vim9script noclear
1651
1652 if !exists('g:didload')
1653 def Test(a: string)
1654 echo a
1655 enddef
1656 def Call()
1657 Test('a')
1658 enddef
1659 else
1660 # redefine with a compile error
1661 def Test(a: string)
1662 echo ax
1663 enddef
1664 endif
1665 Call()
1666 g:didload = 1
1667 END
1668 lines->writefile('XreloadScriptErr.vim')
1669 source XreloadScriptErr.vim
1670 assert_fails('source XreloadScriptErr.vim', 'E1001: Variable not found: ax')
1671
1672 unlet g:didload
1673 delete('XreloadScriptErr.vim')
1674enddef
1675
Bram Moolenaard8448622022-01-07 21:39:52 +00001676def Test_vim9_reload_import()
1677 var lines =<< trim END
1678 vim9script
1679 const var = ''
1680 var valone = 1234
1681 def MyFunc(arg: string)
1682 valone = 5678
1683 enddef
1684 END
1685 var morelines =<< trim END
1686 var valtwo = 222
1687 export def GetValtwo(): number
1688 return valtwo
1689 enddef
1690 END
1691 writefile(lines + morelines, 'Xreload.vim')
1692 source Xreload.vim
1693 source Xreload.vim
1694 source Xreload.vim
1695
1696 # cannot declare a var twice
1697 lines =<< trim END
1698 vim9script
1699 var valone = 1234
1700 var valone = 5678
1701 END
1702 writefile(lines, 'Xreload.vim')
1703 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
1704
1705 delete('Xreload.vim')
1706 delete('Ximport.vim')
1707enddef
1708
1709" if a script is reloaded with a script-local variable that changed its type, a
1710" compiled function using that variable must fail.
1711def Test_script_reload_change_type()
1712 var lines =<< trim END
1713 vim9script noclear
1714 var str = 'string'
1715 def g:GetStr(): string
1716 return str .. 'xxx'
1717 enddef
1718 END
1719 writefile(lines, 'Xreload.vim')
1720 source Xreload.vim
1721 echo g:GetStr()
1722
1723 lines =<< trim END
1724 vim9script noclear
1725 var str = 1234
1726 END
1727 writefile(lines, 'Xreload.vim')
1728 source Xreload.vim
1729 assert_fails('echo g:GetStr()', 'E1150:')
1730
1731 delfunc g:GetStr
1732 delete('Xreload.vim')
1733enddef
1734
1735" Define CallFunc so that the test can be compiled
1736command CallFunc echo 'nop'
1737
1738def Test_script_reload_from_function()
1739 var lines =<< trim END
1740 vim9script
1741
Bram Moolenaar10611952022-04-03 21:11:34 +01001742 if exists('g:loadedThis')
Bram Moolenaard8448622022-01-07 21:39:52 +00001743 finish
1744 endif
Bram Moolenaar10611952022-04-03 21:11:34 +01001745 g:loadedThis = 1
Bram Moolenaard8448622022-01-07 21:39:52 +00001746 delcommand CallFunc
1747 command CallFunc Func()
1748 def Func()
1749 so XreloadFunc.vim
1750 g:didTheFunc = 1
1751 enddef
1752 END
1753 writefile(lines, 'XreloadFunc.vim')
1754 source XreloadFunc.vim
1755 CallFunc
1756 assert_equal(1, g:didTheFunc)
1757
1758 delete('XreloadFunc.vim')
1759 delcommand CallFunc
Bram Moolenaar10611952022-04-03 21:11:34 +01001760 unlet g:loadedThis
Bram Moolenaard8448622022-01-07 21:39:52 +00001761 unlet g:didTheFunc
1762enddef
1763
1764def s:RetSome(): string
1765 return 'some'
1766enddef
1767
1768" Not exported function that is referenced needs to be accessed by the
1769" script-local name.
1770def Test_vim9_funcref()
1771 var sortlines =<< trim END
1772 vim9script
1773 def Compare(i1: number, i2: number): number
1774 return i2 - i1
1775 enddef
1776
1777 export def FastSort(): list<number>
1778 return range(5)->sort(Compare)
1779 enddef
1780
1781 export def GetString(arg: string): string
1782 return arg
1783 enddef
1784 END
1785 writefile(sortlines, 'Xsort.vim')
1786
1787 var lines =<< trim END
1788 vim9script
1789 import './Xsort.vim'
1790 def Test()
1791 g:result = Xsort.FastSort()
1792 enddef
1793 Test()
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001794 END
1795 writefile(lines, 'Xscript.vim')
1796 source Xscript.vim
1797 assert_equal([4, 3, 2, 1, 0], g:result)
1798 unlet g:result
Bram Moolenaard8448622022-01-07 21:39:52 +00001799
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001800 lines =<< trim END
1801 vim9script
Bram Moolenaard8448622022-01-07 21:39:52 +00001802 # using a function imported with "as"
1803 import './Xsort.vim' as anAlias
1804 assert_equal('yes', anAlias.GetString('yes'))
1805
1806 # using the function from a compiled function
1807 def TestMore(): string
1808 var s = s:anAlias.GetString('foo')
1809 return s .. anAlias.GetString('bar')
1810 enddef
1811 assert_equal('foobar', TestMore())
1812
1813 # error when using a function that isn't exported
1814 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
1815 END
1816 writefile(lines, 'Xscript.vim')
1817
Bram Moolenaard8448622022-01-07 21:39:52 +00001818 delete('Xsort.vim')
1819 delete('Xscript.vim')
1820
1821 var Funcref = function('s:RetSome')
1822 assert_equal('some', Funcref())
1823enddef
1824
1825" Check that when searching for "FilterFunc" it finds the import in the
1826" script where FastFilter() is called from, both as a string and as a direct
1827" function reference.
1828def Test_vim9_funcref_other_script()
1829 var filterLines =<< trim END
1830 vim9script
1831 export def FilterFunc(idx: number, val: number): bool
1832 return idx % 2 == 1
1833 enddef
1834 export def FastFilter(): list<number>
1835 return range(10)->filter('FilterFunc(v:key, v:val)')
1836 enddef
1837 export def FastFilterDirect(): list<number>
1838 return range(10)->filter(FilterFunc)
1839 enddef
1840 END
1841 writefile(filterLines, 'Xfilter.vim')
1842
1843 var lines =<< trim END
1844 vim9script
1845 import './Xfilter.vim' as filter
1846 def Test()
1847 var x: list<number> = filter.FastFilter()
1848 enddef
1849 Test()
1850 def TestDirect()
1851 var x: list<number> = filter.FastFilterDirect()
1852 enddef
1853 TestDirect()
1854 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001855 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +00001856 delete('Xfilter.vim')
1857enddef
1858
1859def Test_import_absolute()
1860 var import_lines = [
1861 'vim9script',
1862 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
1863 'def UseExported()',
1864 ' g:imported_abs = abs.exported',
1865 ' abs.exported = 8888',
1866 ' g:imported_after = abs.exported',
1867 'enddef',
1868 'UseExported()',
1869 'g:import_disassembled = execute("disass UseExported")',
1870 ]
1871 writefile(import_lines, 'Ximport_abs.vim')
1872 writefile(s:export_script_lines, 'Xexport_abs.vim')
1873
1874 source Ximport_abs.vim
1875
1876 assert_equal(9876, g:imported_abs)
1877 assert_equal(8888, g:imported_after)
1878 assert_match('<SNR>\d\+_UseExported\_s*' ..
1879 'g:imported_abs = abs.exported\_s*' ..
1880 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1881 '1 STOREG g:imported_abs\_s*' ..
1882 'abs.exported = 8888\_s*' ..
1883 '2 PUSHNR 8888\_s*' ..
1884 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1885 'g:imported_after = abs.exported\_s*' ..
1886 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1887 '5 STOREG g:imported_after',
1888 g:import_disassembled)
1889
1890 Undo_export_script_lines()
1891 unlet g:imported_abs
1892 unlet g:import_disassembled
1893
1894 delete('Ximport_abs.vim')
1895 delete('Xexport_abs.vim')
1896enddef
1897
1898def Test_import_rtp()
1899 var import_lines = [
1900 'vim9script',
1901 'import "Xexport_rtp.vim" as rtp',
1902 'g:imported_rtp = rtp.exported',
1903 ]
1904 writefile(import_lines, 'Ximport_rtp.vim')
1905 mkdir('import', 'p')
1906 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1907
1908 var save_rtp = &rtp
1909 &rtp = getcwd()
1910 source Ximport_rtp.vim
1911 &rtp = save_rtp
1912
1913 assert_equal(9876, g:imported_rtp)
1914
1915 Undo_export_script_lines()
1916 unlet g:imported_rtp
1917 delete('Ximport_rtp.vim')
1918 delete('import', 'rf')
1919enddef
1920
1921def Test_import_compile_error()
1922 var export_lines = [
1923 'vim9script',
1924 'export def ExpFunc(): string',
1925 ' return notDefined',
1926 'enddef',
1927 ]
1928 writefile(export_lines, 'Xexported.vim')
1929
1930 var import_lines = [
1931 'vim9script',
1932 'import "./Xexported.vim" as expo',
1933 'def ImpFunc()',
1934 ' echo expo.ExpFunc()',
1935 'enddef',
1936 'defcompile',
1937 ]
1938 writefile(import_lines, 'Ximport.vim')
1939
1940 try
1941 source Ximport.vim
1942 catch /E1001/
1943 # Error should be before the Xexported.vim file.
1944 assert_match('E1001: Variable not found: notDefined', v:exception)
1945 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1946 endtry
1947
1948 delete('Xexported.vim')
1949 delete('Ximport.vim')
1950enddef
1951
1952def Test_func_overrules_import_fails()
1953 var export_lines =<< trim END
1954 vim9script
1955 export def Func()
1956 echo 'imported'
1957 enddef
1958 END
1959 writefile(export_lines, 'XexportedFunc.vim')
1960
1961 var lines =<< trim END
1962 vim9script
1963 import './XexportedFunc.vim' as Func
1964 def Func()
1965 echo 'local to function'
1966 enddef
1967 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001968 v9.CheckScriptFailure(lines, 'E1213: Redefining imported item "Func"')
Bram Moolenaard8448622022-01-07 21:39:52 +00001969
1970 lines =<< trim END
1971 vim9script
1972 import './XexportedFunc.vim' as Func
1973 def Outer()
1974 def Func()
1975 echo 'local to function'
1976 enddef
1977 enddef
1978 defcompile
1979 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001980 v9.CheckScriptFailure(lines, 'E1236:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001981
1982 delete('XexportedFunc.vim')
1983enddef
1984
1985def Test_source_vim9_from_legacy()
1986 var vim9_lines =<< trim END
1987 vim9script
1988 var local = 'local'
1989 g:global = 'global'
1990 export var exported = 'exported'
1991 export def GetText(): string
1992 return 'text'
1993 enddef
1994 END
1995 writefile(vim9_lines, 'Xvim9_script.vim')
1996
1997 var legacy_lines =<< trim END
1998 source Xvim9_script.vim
1999
2000 call assert_false(exists('local'))
2001 call assert_false(exists('exported'))
2002 call assert_false(exists('s:exported'))
2003 call assert_equal('global', global)
2004 call assert_equal('global', g:global)
Bram Moolenaard8448622022-01-07 21:39:52 +00002005 END
2006 writefile(legacy_lines, 'Xlegacy_script.vim')
2007
2008 source Xlegacy_script.vim
2009 assert_equal('global', g:global)
2010 unlet g:global
2011
2012 delete('Xlegacy_script.vim')
2013 delete('Xvim9_script.vim')
2014enddef
2015
Bram Moolenaarc43e6232022-01-13 20:51:56 +00002016def Test_import_vim9_from_legacy()
2017 var vim9_lines =<< trim END
2018 vim9script
2019 var local = 'local'
2020 g:global = 'global'
2021 export var exported = 'exported'
2022 export def GetText(): string
2023 return 'text'
2024 enddef
2025 END
2026 writefile(vim9_lines, 'Xvim9_export.vim')
2027
2028 var legacy_lines =<< trim END
2029 import './Xvim9_export.vim' as vim9
2030
2031 call assert_false(exists('vim9'))
2032 call assert_false(exists('local'))
2033 call assert_false(exists('s:vim9.local'))
2034 call assert_equal('global', global)
2035 call assert_equal('global', g:global)
2036 call assert_false(exists('exported'))
2037 call assert_false(exists('s:exported'))
2038 call assert_false(exists('*GetText'))
2039
2040 " imported symbol is script-local
2041 call assert_equal('exported', s:vim9.exported)
2042 call assert_equal('text', s:vim9.GetText())
2043 END
2044 writefile(legacy_lines, 'Xlegacy_script.vim')
2045
2046 source Xlegacy_script.vim
2047 assert_equal('global', g:global)
2048 unlet g:global
2049
2050 delete('Xlegacy_script.vim')
2051 delete('Xvim9_export.vim')
2052enddef
2053
Bram Moolenaard8448622022-01-07 21:39:52 +00002054def Test_cmdline_win()
2055 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
2056 # the command line window.
2057 mkdir('rtp/syntax', 'p')
2058 var export_lines =<< trim END
2059 vim9script
2060 export var That = 'yes'
2061 END
2062 writefile(export_lines, 'rtp/syntax/Xexport.vim')
2063 var import_lines =<< trim END
2064 vim9script
2065 import './Xexport.vim' as exp
2066 echo exp.That
2067 END
2068 writefile(import_lines, 'rtp/syntax/vim.vim')
2069 var save_rtp = &rtp
2070 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
2071 syntax on
2072 augroup CmdWin
2073 autocmd CmdwinEnter * g:got_there = 'yes'
2074 augroup END
2075 # this will open and also close the cmdline window
2076 feedkeys('q:', 'xt')
2077 assert_equal('yes', g:got_there)
2078
2079 augroup CmdWin
2080 au!
2081 augroup END
2082 &rtp = save_rtp
2083 delete('rtp', 'rf')
2084enddef
2085
2086def Test_import_gone_when_sourced_twice()
2087 var exportlines =<< trim END
2088 vim9script
2089 if exists('g:guard')
2090 finish
2091 endif
2092 g:guard = 1
2093 export var name = 'someName'
2094 END
2095 writefile(exportlines, 'XexportScript.vim')
2096
2097 var lines =<< trim END
2098 vim9script
2099 import './XexportScript.vim' as expo
2100 def g:GetName(): string
2101 return expo.name
2102 enddef
2103 END
2104 writefile(lines, 'XscriptImport.vim')
2105 so XscriptImport.vim
2106 assert_equal('someName', g:GetName())
2107
2108 so XexportScript.vim
2109 assert_fails('call g:GetName()', 'E1149:')
2110
2111 delfunc g:GetName
2112 delete('XexportScript.vim')
2113 delete('XscriptImport.vim')
2114 unlet g:guard
2115enddef
2116
Bram Moolenaar160aa862022-01-10 21:29:57 +00002117" test using an auto-loaded function and variable
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002118def Test_vim9_autoload_full_name()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002119 var lines =<< trim END
2120 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002121 export def Gettest(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002122 return 'test'
2123 enddef
2124 g:some#name = 'name'
2125 g:some#dict = {key: 'value'}
2126
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002127 export def Varargs(a1: string, ...l: list<string>): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002128 return a1 .. l[0] .. l[1]
2129 enddef
2130 END
2131
2132 mkdir('Xdir/autoload', 'p')
2133 writefile(lines, 'Xdir/autoload/some.vim')
2134 var save_rtp = &rtp
2135 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2136
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002137 assert_equal('test', g:some#Gettest())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002138 assert_equal('name', g:some#name)
2139 assert_equal('value', g:some#dict.key)
2140 g:some#other = 'other'
2141 assert_equal('other', g:some#other)
2142
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002143 assert_equal('abc', some#Varargs('a', 'b', 'c'))
Bram Moolenaar160aa862022-01-10 21:29:57 +00002144
2145 # upper case script name works
2146 lines =<< trim END
2147 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002148 export def GetOther(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002149 return 'other'
2150 enddef
2151 END
2152 writefile(lines, 'Xdir/autoload/Other.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002153 assert_equal('other', g:Other#GetOther())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002154
2155 delete('Xdir', 'rf')
2156 &rtp = save_rtp
2157enddef
2158
2159def Test_vim9script_autoload()
2160 mkdir('Xdir/autoload', 'p')
2161 var save_rtp = &rtp
2162 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2163
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002164 # when the path has "/autoload/" prefix is not needed
Bram Moolenaar160aa862022-01-10 21:29:57 +00002165 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002166 vim9script
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002167 g:prefixed_loaded += 1
Bram Moolenaar160aa862022-01-10 21:29:57 +00002168
2169 export def Gettest(): string
2170 return 'test'
2171 enddef
2172
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002173 export var name = 'name'
2174
2175 export func GetFunc()
2176 return Gettest() .. 'more' .. s:name
Bram Moolenaar160aa862022-01-10 21:29:57 +00002177 endfunc
2178
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002179 export def GetDef(): string
2180 return Gettest() .. 'more' .. name
2181 enddef
2182
Bram Moolenaar160aa862022-01-10 21:29:57 +00002183 export final fname = 'final'
2184 export const cname = 'const'
2185 END
2186 writefile(lines, 'Xdir/autoload/prefixed.vim')
2187
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002188 g:prefixed_loaded = 0
2189 g:expected_loaded = 0
Bram Moolenaar160aa862022-01-10 21:29:57 +00002190 lines =<< trim END
2191 vim9script
2192 import autoload 'prefixed.vim'
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002193 assert_equal(g:expected_loaded, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002194 assert_equal('test', prefixed.Gettest())
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002195 assert_equal(1, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002196
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002197 assert_equal('testmorename', prefixed.GetFunc())
2198 assert_equal('testmorename', prefixed.GetDef())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002199 assert_equal('name', prefixed.name)
2200 assert_equal('final', prefixed.fname)
2201 assert_equal('const', prefixed.cname)
2202 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002203 v9.CheckScriptSuccess(lines)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002204 # can source it again, autoload script not loaded again
2205 g:expected_loaded = 1
Bram Moolenaar62aec932022-01-29 21:45:34 +00002206 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002207
2208 # can also get the items by autoload name
2209 lines =<< trim END
2210 call assert_equal('test', prefixed#Gettest())
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002211 call assert_equal('testmorename', prefixed#GetFunc())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002212 call assert_equal('name', prefixed#name)
2213 call assert_equal('final', prefixed#fname)
2214 call assert_equal('const', prefixed#cname)
2215 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002216 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002217
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002218 unlet g:prefixed_loaded
2219 unlet g:expected_loaded
2220 delete('Xdir', 'rf')
2221 &rtp = save_rtp
2222enddef
2223
Bram Moolenaard02dce22022-01-18 17:43:04 +00002224def Test_import_autoload_not_exported()
2225 mkdir('Xdir/autoload', 'p')
2226 var save_rtp = &rtp
2227 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2228
2229 # error when using an item that is not exported from an autoload script
2230 var exportLines =<< trim END
2231 vim9script
2232 var notExported = 123
2233 def NotExport()
2234 echo 'nop'
2235 enddef
2236 END
2237 writefile(exportLines, 'Xdir/autoload/notExport1.vim')
2238
2239 var lines =<< trim END
2240 vim9script
2241 import autoload 'notExport1.vim'
2242 echo notExport1.notFound
2243 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002244 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002245
2246 lines =<< trim END
2247 vim9script
2248 import autoload 'notExport1.vim'
2249 echo notExport1.notExported
2250 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002251 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002252
2253 lines =<< trim END
2254 vim9script
2255 import autoload 'notExport1.vim'
2256 echo notExport1.NotFunc()
2257 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002258 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002259
2260 lines =<< trim END
2261 vim9script
2262 import autoload 'notExport1.vim'
2263 echo notExport1.NotExport()
2264 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002265 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002266
2267 lines =<< trim END
2268 vim9script
2269 import autoload 'notExport1.vim'
2270 echo 'text'->notExport1.NotFunc()
2271 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002272 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002273
2274 lines =<< trim END
2275 vim9script
2276 import autoload 'notExport1.vim'
2277 echo 'text'->notExport1.NotExport()
2278 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002279 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002280
2281 # using a :def function we use a different autoload script every time so that
2282 # the function is compiled without the script loaded
2283 writefile(exportLines, 'Xdir/autoload/notExport2.vim')
2284 lines =<< trim END
2285 vim9script
2286 import autoload 'notExport2.vim'
2287 def Testit()
2288 echo notExport2.notFound
2289 enddef
2290 Testit()
2291 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002292 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport2#notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002293
2294 writefile(exportLines, 'Xdir/autoload/notExport3.vim')
2295 lines =<< trim END
2296 vim9script
2297 import autoload 'notExport3.vim'
2298 def Testit()
2299 echo notExport3.notExported
2300 enddef
2301 Testit()
2302 END
2303 # don't get E1049 because it is too complicated to figure out
Bram Moolenaar62aec932022-01-29 21:45:34 +00002304 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport3#notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002305
2306 writefile(exportLines, 'Xdir/autoload/notExport4.vim')
2307 lines =<< trim END
2308 vim9script
2309 import autoload 'notExport4.vim'
2310 def Testit()
2311 echo notExport4.NotFunc()
2312 enddef
2313 Testit()
2314 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002315 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport4#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002316
2317 writefile(exportLines, 'Xdir/autoload/notExport5.vim')
2318 lines =<< trim END
2319 vim9script
2320 import autoload 'notExport5.vim'
2321 def Testit()
2322 echo notExport5.NotExport()
2323 enddef
2324 Testit()
2325 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002326 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport5#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002327
2328 writefile(exportLines, 'Xdir/autoload/notExport6.vim')
2329 lines =<< trim END
2330 vim9script
2331 import autoload 'notExport6.vim'
2332 def Testit()
2333 echo 'text'->notExport6.NotFunc()
2334 enddef
2335 Testit()
2336 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002337 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport6#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002338
2339 writefile(exportLines, 'Xdir/autoload/notExport7.vim')
2340 lines =<< trim END
2341 vim9script
2342 import autoload 'notExport7.vim'
2343 def Testit()
2344 echo 'text'->notExport7.NotExport()
2345 enddef
2346 Testit()
2347 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002348 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport7#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002349
2350 delete('Xdir', 'rf')
2351 &rtp = save_rtp
2352enddef
2353
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002354def Test_vim9script_autoload_call()
2355 mkdir('Xdir/autoload', 'p')
2356 var save_rtp = &rtp
2357 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2358
2359 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002360 vim9script
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002361
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002362 export def RetArg(arg: string): string
2363 return arg
2364 enddef
2365
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002366 export def Getother()
2367 g:result = 'other'
2368 enddef
2369 END
Bram Moolenaar5d982692022-01-12 15:15:27 +00002370 writefile(lines, 'Xdir/autoload/another.vim')
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002371
2372 lines =<< trim END
2373 vim9script
Bram Moolenaar5d982692022-01-12 15:15:27 +00002374 import autoload 'another.vim'
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002375
2376 # compile this before 'another.vim' is loaded
2377 def CallAnother()
2378 assert_equal('foo', 'foo'->another.RetArg())
2379 enddef
2380 CallAnother()
2381
Bram Moolenaar5d982692022-01-12 15:15:27 +00002382 call another.Getother()
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002383 assert_equal('other', g:result)
Bram Moolenaar3d8e25a2022-01-22 11:00:02 +00002384
2385 assert_equal('arg', call('another.RetArg', ['arg']))
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00002386
2387 verbose function another.Getother
2388 # should we disallow this?
2389 verbose function another#Getother
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002390 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002391 v9.CheckScriptSuccess(lines)
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002392
2393 unlet g:result
Bram Moolenaar160aa862022-01-10 21:29:57 +00002394 delete('Xdir', 'rf')
2395 &rtp = save_rtp
2396enddef
2397
Bram Moolenaarb697dc22022-01-22 11:27:29 +00002398def Test_vim9script_noclear_autoload()
2399 mkdir('Xdir/autoload', 'p')
2400 var save_rtp = &rtp
2401 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2402
2403 var lines =<< trim END
2404 vim9script
2405 export def Func(): string
2406 return 'called'
2407 enddef
2408 g:double_loaded = 'yes'
2409 END
2410 writefile(lines, 'Xdir/autoload/double.vim')
2411
2412 lines =<< trim END
2413 vim9script noclear
2414 if exists('g:script_loaded')
2415 finish
2416 endif
2417 g:script_loaded = true
2418
2419 import autoload 'double.vim'
2420 nnoremap <F3> <ScriptCmd>g:result = double.Func()<CR>
2421 END
2422 g:double_loaded = 'no'
2423 writefile(lines, 'Xloaddouble')
2424 source Xloaddouble
2425 assert_equal('no', g:double_loaded)
2426 assert_equal(true, g:script_loaded)
2427 source Xloaddouble
2428 feedkeys("\<F3>", 'xt')
2429 assert_equal('called', g:result)
2430 assert_equal('yes', g:double_loaded)
2431
2432 delete('Xloaddouble')
2433 unlet g:double_loaded
2434 unlet g:script_loaded
2435 unlet g:result
2436 delete('Xdir', 'rf')
2437 &rtp = save_rtp
2438enddef
2439
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002440def Test_vim9script_autoload_duplicate()
2441 mkdir('Xdir/autoload', 'p')
2442
2443 var lines =<< trim END
2444 vim9script
2445
2446 export def Func()
2447 enddef
2448
2449 def Func()
2450 enddef
2451 END
2452 writefile(lines, 'Xdir/autoload/dupfunc.vim')
2453 assert_fails('source Xdir/autoload/dupfunc.vim', 'E1073:')
2454
2455 lines =<< trim END
2456 vim9script
2457
2458 def Func()
2459 enddef
2460
2461 export def Func()
2462 enddef
2463 END
2464 writefile(lines, 'Xdir/autoload/dup2func.vim')
2465 assert_fails('source Xdir/autoload/dup2func.vim', 'E1073:')
2466
2467 lines =<< trim END
2468 vim9script
2469
2470 def Func()
2471 enddef
2472
2473 export var Func = 'asdf'
2474 END
2475 writefile(lines, 'Xdir/autoload/dup3func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002476 assert_fails('source Xdir/autoload/dup3func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002477
2478 lines =<< trim END
2479 vim9script
2480
2481 export var Func = 'asdf'
2482
2483 def Func()
2484 enddef
2485 END
2486 writefile(lines, 'Xdir/autoload/dup4func.vim')
2487 assert_fails('source Xdir/autoload/dup4func.vim', 'E707:')
2488
2489 lines =<< trim END
2490 vim9script
2491
2492 var Func = 'asdf'
2493
2494 export def Func()
2495 enddef
2496 END
2497 writefile(lines, 'Xdir/autoload/dup5func.vim')
2498 assert_fails('source Xdir/autoload/dup5func.vim', 'E707:')
2499
2500 lines =<< trim END
2501 vim9script
2502
2503 export def Func()
2504 enddef
2505
2506 var Func = 'asdf'
2507 END
2508 writefile(lines, 'Xdir/autoload/dup6func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002509 assert_fails('source Xdir/autoload/dup6func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002510
2511 delete('Xdir', 'rf')
2512enddef
2513
Bram Moolenaar2017d6f2022-01-20 19:38:46 +00002514def Test_autoload_missing_function_name()
2515 mkdir('Xdir/autoload', 'p')
2516
2517 var lines =<< trim END
2518 vim9script
2519
2520 def loadme#()
2521 enddef
2522 END
2523 writefile(lines, 'Xdir/autoload/loadme.vim')
2524 assert_fails('source Xdir/autoload/loadme.vim', 'E129:')
2525
2526 delete('Xdir', 'rf')
2527enddef
2528
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002529def Test_autoload_name_wrong()
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002530 var lines =<< trim END
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002531 def Xscriptname#Func()
2532 enddef
2533 END
2534 writefile(lines, 'Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002535 v9.CheckScriptFailure(lines, 'E746:')
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00002536 delete('Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002537
2538 mkdir('Xdir/autoload', 'p')
2539 lines =<< trim END
2540 vim9script
2541 def somescript#Func()
2542 enddef
2543 END
2544 writefile(lines, 'Xdir/autoload/somescript.vim')
2545 assert_fails('source Xdir/autoload/somescript.vim', 'E1263:')
2546
2547 delete('Xdir', 'rf')
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002548enddef
2549
Bram Moolenaard041f422022-01-12 19:54:00 +00002550def Test_import_autoload_postponed()
2551 mkdir('Xdir/autoload', 'p')
2552 var save_rtp = &rtp
2553 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2554
2555 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002556 vim9script
Bram Moolenaard041f422022-01-12 19:54:00 +00002557
2558 g:loaded_postponed = 'true'
2559 export var variable = 'bla'
2560 export def Function(): string
2561 return 'bla'
2562 enddef
2563 END
2564 writefile(lines, 'Xdir/autoload/postponed.vim')
2565
2566 lines =<< trim END
2567 vim9script
2568
2569 import autoload 'postponed.vim'
2570 def Tryit()
2571 echo postponed.variable
2572 echo postponed.Function()
2573 enddef
2574 defcompile
2575 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002576 v9.CheckScriptSuccess(lines)
Bram Moolenaard041f422022-01-12 19:54:00 +00002577 assert_false(exists('g:loaded_postponed'))
Bram Moolenaar62aec932022-01-29 21:45:34 +00002578 v9.CheckScriptSuccess(lines + ['Tryit()'])
Bram Moolenaard041f422022-01-12 19:54:00 +00002579 assert_equal('true', g:loaded_postponed)
2580
2581 unlet g:loaded_postponed
2582 delete('Xdir', 'rf')
2583 &rtp = save_rtp
2584enddef
2585
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002586def Test_import_autoload_override()
2587 mkdir('Xdir/autoload', 'p')
2588 var save_rtp = &rtp
2589 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2590 test_override('autoload', 1)
2591
2592 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002593 vim9script
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002594
2595 g:loaded_override = 'true'
2596 export var variable = 'bla'
2597 export def Function(): string
2598 return 'bla'
2599 enddef
2600 END
2601 writefile(lines, 'Xdir/autoload/override.vim')
2602
2603 lines =<< trim END
2604 vim9script
2605
2606 import autoload 'override.vim'
2607 assert_equal('true', g:loaded_override)
2608
2609 def Tryit()
2610 echo override.doesNotExist
2611 enddef
2612 defcompile
2613 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002614 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1)
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002615
2616 test_override('autoload', 0)
2617 unlet g:loaded_override
2618 delete('Xdir', 'rf')
2619 &rtp = save_rtp
2620enddef
2621
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002622def Test_autoload_mapping()
2623 mkdir('Xdir/autoload', 'p')
2624 var save_rtp = &rtp
2625 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2626
2627 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002628 vim9script
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002629
2630 g:toggle_loaded = 'yes'
2631
2632 export def Toggle(): string
2633 return ":g:toggle_called = 'yes'\<CR>"
2634 enddef
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002635 export def Doit()
2636 g:doit_called = 'yes'
2637 enddef
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002638 END
2639 writefile(lines, 'Xdir/autoload/toggle.vim')
2640
2641 lines =<< trim END
2642 vim9script
2643
2644 import autoload 'toggle.vim'
2645
2646 nnoremap <silent> <expr> tt toggle.Toggle()
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002647 nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR>
2648 nnoremap <silent> yy <Cmd>toggle.Doit()<CR>
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002649 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002650 v9.CheckScriptSuccess(lines)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002651 assert_false(exists("g:toggle_loaded"))
2652 assert_false(exists("g:toggle_called"))
Bram Moolenaar6079da72022-01-18 14:16:59 +00002653 assert_match('\d A: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002654
2655 feedkeys("tt", 'xt')
2656 assert_equal('yes', g:toggle_loaded)
2657 assert_equal('yes', g:toggle_called)
Bram Moolenaar6079da72022-01-18 14:16:59 +00002658 assert_match('\d: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002659
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002660 feedkeys("xx", 'xt')
2661 assert_equal('yes', g:doit_called)
2662
2663 assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle')
2664
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002665 nunmap tt
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002666 nunmap xx
2667 nunmap yy
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002668 unlet g:toggle_loaded
2669 unlet g:toggle_called
2670 delete('Xdir', 'rf')
2671 &rtp = save_rtp
2672enddef
2673
Bram Moolenaar160aa862022-01-10 21:29:57 +00002674def Test_vim9script_autoload_fails()
2675 var lines =<< trim END
2676 vim9script autoload
2677 var n = 0
2678 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002679 v9.CheckScriptFailure(lines, 'E475: Invalid argument: autoload')
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002680
2681 lines =<< trim END
2682 vim9script noclear noclear
2683 var n = 0
2684 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002685 v9.CheckScriptFailure(lines, 'E983: Duplicate argument: noclear')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +01002686
2687 lines =<< trim END
2688 vim9script noclears
2689 var n = 0
2690 END
2691 v9.CheckScriptFailure(lines, 'E475: Invalid argument: noclears')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002692enddef
2693
2694def Test_import_autoload_fails()
2695 var lines =<< trim END
2696 vim9script
2697 import autoload autoload 'prefixed.vim'
2698 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002699 v9.CheckScriptFailure(lines, 'E121: Undefined variable: autoload')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002700
2701 lines =<< trim END
2702 vim9script
Bram Moolenaar1836d612022-01-18 13:14:47 +00002703 import autoload './doesNotExist.vim'
Bram Moolenaar160aa862022-01-10 21:29:57 +00002704 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002705 v9.CheckScriptFailure(lines, 'E282:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002706
2707 lines =<< trim END
2708 vim9script
2709 import autoload '/dir/doesNotExist.vim'
2710 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002711 v9.CheckScriptFailure(lines, 'E282:', 2)
2712
2713 lines =<< trim END
2714 vim9script
2715 import autoload '../testdir'
2716 END
2717 v9.CheckScriptFailure(lines, 'E17:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002718
2719 lines =<< trim END
2720 vim9script
2721 import autoload 'doesNotExist.vim'
2722 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002723 v9.CheckScriptFailure(lines, 'E1053: Could not import "doesNotExist.vim"')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002724enddef
2725
2726" test disassembling an auto-loaded function starting with "debug"
2727def Test_vim9_autoload_disass()
2728 mkdir('Xdir/autoload', 'p')
2729 var save_rtp = &rtp
2730 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2731
2732 var lines =<< trim END
2733 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002734 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002735 return 'debug'
2736 enddef
2737 END
2738 writefile(lines, 'Xdir/autoload/debugit.vim')
2739
2740 lines =<< trim END
2741 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002742 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002743 return 'profile'
2744 enddef
2745 END
2746 writefile(lines, 'Xdir/autoload/profileit.vim')
2747
2748 lines =<< trim END
2749 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002750 assert_equal('debug', debugit#Test())
2751 disass debugit#Test
2752 assert_equal('profile', profileit#Test())
2753 disass profileit#Test
Bram Moolenaar160aa862022-01-10 21:29:57 +00002754 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002755 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002756
2757 delete('Xdir', 'rf')
2758 &rtp = save_rtp
2759enddef
2760
2761" test using a vim9script that is auto-loaded from an autocmd
2762def Test_vim9_aucmd_autoload()
2763 var lines =<< trim END
2764 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002765 export def Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002766 echomsg getreg('"')
2767 enddef
2768 END
2769
2770 mkdir('Xdir/autoload', 'p')
2771 writefile(lines, 'Xdir/autoload/foo.vim')
2772 var save_rtp = &rtp
2773 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2774 augroup test
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002775 autocmd TextYankPost * call foo#Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002776 augroup END
2777
2778 normal Y
2779
2780 augroup test
2781 autocmd!
2782 augroup END
2783 delete('Xdir', 'rf')
2784 &rtp = save_rtp
2785enddef
2786
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002787" test using a autoloaded file that is case sensitive
2788def Test_vim9_autoload_case_sensitive()
2789 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002790 vim9script
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002791 export def CaseSensitive(): string
2792 return 'done'
2793 enddef
2794 END
2795
2796 mkdir('Xdir/autoload', 'p')
2797 writefile(lines, 'Xdir/autoload/CaseSensitive.vim')
2798 var save_rtp = &rtp
2799 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2800
2801 lines =<< trim END
2802 vim9script
2803 import autoload 'CaseSensitive.vim'
2804 assert_equal('done', CaseSensitive.CaseSensitive())
2805 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002806 v9.CheckScriptSuccess(lines)
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002807
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002808 if !has('fname_case')
2809 lines =<< trim END
2810 vim9script
2811 import autoload 'CaseSensitive.vim'
2812 import autoload 'casesensitive.vim'
2813 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002814 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002815 endif
2816
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002817 delete('Xdir', 'rf')
2818 &rtp = save_rtp
2819enddef
2820
Bram Moolenaar160aa862022-01-10 21:29:57 +00002821" This was causing a crash because suppress_errthrow wasn't reset.
2822def Test_vim9_autoload_error()
2823 var lines =<< trim END
2824 vim9script
2825 def crash#func()
2826 try
2827 for x in List()
2828 endfor
2829 catch
2830 endtry
2831 g:ok = true
2832 enddef
2833 fu List()
2834 invalid
2835 endfu
2836 try
2837 alsoinvalid
2838 catch /wontmatch/
2839 endtry
2840 END
2841 call mkdir('Xruntime/autoload', 'p')
2842 call writefile(lines, 'Xruntime/autoload/crash.vim')
2843
2844 # run in a separate Vim to avoid the side effects of assert_fails()
2845 lines =<< trim END
2846 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
2847 call crash#func()
2848 call writefile(['ok'], 'Xdidit')
2849 qall!
2850 END
2851 writefile(lines, 'Xscript')
Bram Moolenaar62aec932022-01-29 21:45:34 +00002852 g:RunVim([], [], '-S Xscript')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002853 assert_equal(['ok'], readfile('Xdidit'))
2854
2855 delete('Xdidit')
2856 delete('Xscript')
2857 delete('Xruntime', 'rf')
2858
2859 lines =<< trim END
2860 vim9script
2861 var foo#bar = 'asdf'
2862 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002863 v9.CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002864enddef
2865
Bram Moolenaard8448622022-01-07 21:39:52 +00002866
2867" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker