blob: 82fea6efe3383a3b08c8bb4120ba77f5e6a4a5d4 [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 Moolenaar7b29f6a2022-01-22 17:58:13 +00001140func Test_import_in_diffexpr()
1141 CheckExecutable diff
1142
1143 call Run_Test_import_in_diffexpr()
1144endfunc
1145
1146def Run_Test_import_in_diffexpr()
1147 var lines =<< trim END
1148 vim9script
1149
1150 export def DiffExpr()
1151 # Prepend some text to check diff type detection
1152 writefile(['warning', ' message'], v:fname_out)
1153 silent exe '!diff ' .. v:fname_in .. ' '
1154 .. v:fname_new .. '>>' .. v:fname_out
1155 enddef
1156 END
1157 writefile(lines, 'Xdiffexpr')
1158
1159 lines =<< trim END
1160 vim9script
1161 import './Xdiffexpr' as diff
1162
1163 set diffexpr=diff.DiffExpr()
1164 set diffopt=foldcolumn:0
1165 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001166 v9.CheckScriptSuccess(lines)
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001167
1168 enew!
1169 call setline(1, ['one', 'two', 'three'])
1170 diffthis
1171
1172 botright vert new
1173 call setline(1, ['one', 'two', 'three.'])
1174 diffthis
1175 # we only check if this does not cause errors
1176 redraw
1177
1178 diffoff!
1179 bwipe!
1180 bwipe!
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001181 delete('Xdiffexpr')
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001182enddef
1183
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001184def Test_import_in_patchexpr()
1185 var lines =<< trim END
1186 vim9script
1187 export def TPatch()
1188 call writefile(['output file'], v:fname_out)
1189 enddef
1190 END
1191 writefile(lines, 'Xpatchexpr')
1192
1193 lines =<< trim END
1194 vim9script
1195 import './Xpatchexpr' as patch
1196 set patchexpr=patch.TPatch()
1197 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001198 v9.CheckScriptSuccess(lines)
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001199
1200 call writefile(['input file'], 'Xinput')
1201 call writefile(['diff file'], 'Xdiff')
1202 :%bwipe!
1203 edit Xinput
1204 diffpatch Xdiff
1205 call assert_equal('output file', getline(1))
1206
1207 call delete('Xinput')
1208 call delete('Xdiff')
1209 call delete('Xpatchexpr')
1210 set patchexpr&
1211 :%bwipe!
1212enddef
1213
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001214def Test_import_in_formatexpr()
1215 var lines =<< trim END
1216 vim9script
1217 export def MyFormatExpr(): number
1218 g:did_format = 'yes'
1219 return 0
1220 enddef
1221 END
1222 writefile(lines, 'Xformatter')
1223
1224 lines =<< trim END
1225 vim9script
1226 import './Xformatter' as format
1227 set formatexpr=format.MyFormatExpr()
1228 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001229 v9.CheckScriptSuccess(lines)
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001230
1231 new
1232 setline(1, ['a', 'b', 'c'])
1233 normal gqG
1234 assert_equal('yes', g:did_format)
1235
1236 bwipe!
1237 delete('Xformatter')
1238 unlet g:did_format
1239 set formatexpr=
1240enddef
1241
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001242def Test_import_in_includeexpr()
1243 writefile(['found it'], 'Xthisfile')
1244 new
1245
1246 var lines =<< trim END
1247 vim9script
1248 export def DoSub(): string
1249 return substitute(v:fname, 'that', 'this', '')
1250 enddef
1251 END
1252 writefile(lines, 'Xinclude.vim')
1253
1254 lines =<< trim END
1255 vim9script
1256 import './Xinclude.vim'
1257 set includeexpr=Xinclude.DoSub()
1258 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001259 v9.CheckScriptSuccess(lines)
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001260
1261 setline(1, ['Xthatfile'])
1262 exe "normal \<C-W>f"
1263 assert_equal('Xthisfile', expand('%'))
1264
1265 bwipe!
1266 bwipe!
1267 set includeexpr=
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001268 delete('Xinclude.vim')
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001269 delete('Xthisfile')
1270enddef
1271
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001272def Test_import_in_indentexpr()
1273 var lines =<< trim END
1274 vim9script
1275 export def GetIndent(): number
1276 return 5
1277 enddef
1278 END
1279 writefile(lines, 'Xindenter')
1280
1281 lines =<< trim END
1282 vim9script
1283 import './Xindenter' as indent
1284 set indentexpr=indent.GetIndent()
1285 set debug=throw
1286 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001287 v9.CheckScriptSuccess(lines)
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001288
1289 new
1290 setline(1, 'hello')
1291 normal ==
1292 assert_equal(' hello', getline(1))
1293
1294 bwipe!
1295 set indentexpr= debug=
1296 delete('Xindenter')
1297enddef
1298
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001299func Test_import_in_printexpr()
1300 CheckFeature postscript
1301 call Run_Test_import_in_printexpr()
1302endfunc
1303
1304def Run_Test_import_in_printexpr()
1305 var lines =<< trim END
1306 vim9script
1307 export def PrintFile(): bool
1308 g:printed = 'yes'
1309 delete('v:fname_in')
1310 return false
1311 enddef
1312 END
1313 writefile(lines, 'Xprint.vim')
1314
1315 lines =<< trim END
1316 vim9script
1317 import './Xprint.vim'
1318 set printexpr=Xprint.PrintFile()
1319 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001320 v9.CheckScriptSuccess(lines)
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001321
1322 help
1323 hardcopy dummy args
1324 assert_equal('yes', g:printed)
1325
1326 delete('Xprint.vim')
1327 set printexpr=
1328enddef
1329
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001330def Test_import_in_charconvert()
1331 var lines =<< trim END
1332 vim9script
1333 export def MakeUpper(): bool
1334 var data = readfile(v:fname_in)
1335 map(data, 'toupper(v:val)')
1336 writefile(data, v:fname_out)
1337 return false # success
1338 enddef
1339 END
1340 writefile(lines, 'Xconvert.vim')
1341
1342 lines =<< trim END
1343 vim9script
1344 import './Xconvert.vim' as conv
1345 set charconvert=conv.MakeUpper()
1346 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001347 v9.CheckScriptSuccess(lines)
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001348
1349 writefile(['one', 'two'], 'Xfile')
1350 new Xfile
1351 write ++enc=ucase Xfile1
1352 assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1353
1354 delete('Xfile')
1355 delete('Xfile1')
1356 delete('Xconvert.vim')
1357 bwipe!
1358 set charconvert&
1359enddef
1360
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001361func Test_import_in_spellsuggest_expr()
1362 CheckFeature spell
1363 call Run_Test_import_in_spellsuggest_expr()
1364endfunc
1365
1366def Run_Test_import_in_spellsuggest_expr()
1367 var lines =<< trim END
1368 vim9script
1369 export def MySuggest(): list<any>
1370 return [['Fox', 8], ['Fop', 9]]
1371 enddef
1372 END
1373 writefile(lines, 'Xsuggest.vim')
1374
1375 lines =<< trim END
1376 vim9script
1377 import './Xsuggest.vim' as sugg
1378 set spell spellsuggest=expr:sugg.MySuggest()
1379 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001380 v9.CheckScriptSuccess(lines)
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001381
1382 set verbose=1 # report errors
1383 call assert_equal(['Fox', 'Fop'], spellsuggest('Fo', 2))
1384
1385 delete('Xsuggest.vim')
1386 set nospell spellsuggest& verbose=0
1387enddef
1388
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001389def Test_export_shadows_global_function()
1390 mkdir('Xdir/autoload', 'p')
1391 var save_rtp = &rtp
1392 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1393
1394 var lines =<< trim END
1395 vim9script
1396 export def Shadow(): string
1397 return 'Shadow()'
1398 enddef
1399 END
1400 writefile(lines, 'Xdir/autoload/shadow.vim')
1401
1402 lines =<< trim END
1403 vim9script
1404
1405 def g:Shadow(): string
1406 return 'global'
1407 enddef
1408
1409 import autoload 'shadow.vim'
1410 assert_equal('Shadow()', shadow.Shadow())
1411 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001412 v9.CheckScriptSuccess(lines)
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001413
1414 delfunc g:Shadow
1415 bwipe!
1416 delete('Xdir', 'rf')
1417 &rtp = save_rtp
1418enddef
1419
Bram Moolenaard8448622022-01-07 21:39:52 +00001420def Test_export_fails()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001421 v9.CheckScriptFailure(['export var some = 123'], 'E1042:')
1422 v9.CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
1423 v9.CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +01001424 v9.CheckScriptFailure(['vim9script', 'export function /a1b2c3'], 'E1044:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001425
1426 assert_fails('export something', 'E1043:')
1427enddef
1428
1429func Test_import_fails_without_script()
1430 CheckRunVimInTerminal
1431
1432 " call indirectly to avoid compilation error for missing functions
1433 call Run_Test_import_fails_on_command_line()
1434endfunc
1435
1436def Run_Test_import_fails_on_command_line()
1437 var export =<< trim END
1438 vim9script
1439 export def Foo(): number
1440 return 0
1441 enddef
1442 END
1443 writefile(export, 'XexportCmd.vim')
1444
Bram Moolenaar62aec932022-01-29 21:45:34 +00001445 var buf = g:RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaard8448622022-01-07 21:39:52 +00001446 rows: 6, wait_for_ruler: 0})
Bram Moolenaar62aec932022-01-29 21:45:34 +00001447 g:WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaard8448622022-01-07 21:39:52 +00001448
1449 delete('XexportCmd.vim')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001450 g:StopVimInTerminal(buf)
Bram Moolenaard8448622022-01-07 21:39:52 +00001451enddef
1452
1453def Test_vim9_reload_noclear()
1454 var lines =<< trim END
1455 vim9script
1456 export var exported = 'thexport'
1457
1458 export def TheFunc(x = 0)
1459 enddef
1460 END
1461 writefile(lines, 'XExportReload')
1462 lines =<< trim END
1463 vim9script noclear
1464 g:loadCount += 1
Bram Moolenaara749a422022-02-12 19:52:25 +00001465 var reloaded = 'init'
Bram Moolenaard8448622022-01-07 21:39:52 +00001466 import './XExportReload' as exp
1467
1468 def Again(): string
1469 return 'again'
1470 enddef
1471
1472 exp.TheFunc()
1473
Bram Moolenaara749a422022-02-12 19:52:25 +00001474 if exists('loaded') | finish | endif
1475 var loaded = true
Bram Moolenaard8448622022-01-07 21:39:52 +00001476
Bram Moolenaara749a422022-02-12 19:52:25 +00001477 var notReloaded = 'yes'
1478 reloaded = 'first'
Bram Moolenaard8448622022-01-07 21:39:52 +00001479 def g:Values(): list<string>
Bram Moolenaara749a422022-02-12 19:52:25 +00001480 return [reloaded, notReloaded, Again(), Once(), exp.exported]
Bram Moolenaard8448622022-01-07 21:39:52 +00001481 enddef
1482
1483 def Once(): string
1484 return 'once'
1485 enddef
1486 END
1487 writefile(lines, 'XReloaded')
1488 g:loadCount = 0
1489 source XReloaded
1490 assert_equal(1, g:loadCount)
1491 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
1492 source XReloaded
1493 assert_equal(2, g:loadCount)
1494 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1495 source XReloaded
1496 assert_equal(3, g:loadCount)
1497 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1498
1499 delete('XReloaded')
1500 delete('XExportReload')
1501 delfunc g:Values
1502 unlet g:loadCount
1503
1504 lines =<< trim END
1505 vim9script
1506 def Inner()
1507 enddef
1508 END
1509 lines->writefile('XreloadScript.vim')
1510 source XreloadScript.vim
1511
1512 lines =<< trim END
1513 vim9script
1514 def Outer()
1515 def Inner()
1516 enddef
1517 enddef
1518 defcompile
1519 END
1520 lines->writefile('XreloadScript.vim')
1521 source XreloadScript.vim
1522
1523 delete('XreloadScript.vim')
1524enddef
1525
Bram Moolenaarcd1cda22022-02-16 21:48:25 +00001526def Test_vim_reload_noclear_arg_count()
1527 var lines =<< trim END
1528 vim9script noclear
1529
1530 if !exists('g:didload')
1531 def Test(a: string, b: string)
1532 echo a b
1533 enddef
1534 def Call()
1535 Test('a', 'b')
1536 enddef
1537 else
1538 # redefine with one argument less
1539 def Test(a: string)
1540 echo a
1541 enddef
1542 endif
1543 Call()
1544 g:didload = 1
1545 END
1546 lines->writefile('XreloadScript_1.vim')
1547 source XreloadScript_1.vim
1548 assert_fails('source XreloadScript_1.vim', 'E1106: One argument too many')
1549 unlet g:didload
1550
1551 lines =<< trim END
1552 vim9script noclear
1553
1554 if !exists('g:didload')
1555 def Test(a: string, b: string, c: string)
1556 echo a b
1557 enddef
1558 def Call()
1559 Test('a', 'b', 'c')
1560 enddef
1561 else
1562 # redefine with one argument less
1563 def Test(a: string)
1564 echo a
1565 enddef
1566 endif
1567 Call()
1568 g:didload = 1
1569 END
1570 lines->writefile('XreloadScript_2.vim')
1571 source XreloadScript_2.vim
1572 assert_fails('source XreloadScript_2.vim', 'E1106: 2 arguments too many')
1573 unlet g:didload
1574
1575 lines =<< trim END
1576 vim9script noclear
1577
1578 if !exists('g:didload')
1579 def Test(a: string)
1580 echo a
1581 enddef
1582 def Call()
1583 Test('a')
1584 enddef
1585 else
1586 # redefine with one argument extra
1587 def Test(a: string, b: string)
1588 echo a b
1589 enddef
1590 endif
1591 Call()
1592 g:didload = 1
1593 END
1594 lines->writefile('XreloadScript_3.vim')
1595 source XreloadScript_3.vim
1596 assert_fails('source XreloadScript_3.vim', 'E1190: One argument too few')
1597 unlet g:didload
1598
1599 lines =<< trim END
1600 vim9script noclear
1601
1602 if !exists('g:didload')
1603 def Test(a: string)
1604 echo a
1605 enddef
1606 def Call()
1607 Test('a')
1608 enddef
1609 else
1610 # redefine with two arguments extra
1611 def Test(a: string, b: string, c: string)
1612 echo a b
1613 enddef
1614 endif
1615 Call()
1616 g:didload = 1
1617 END
1618 lines->writefile('XreloadScript_4.vim')
1619 source XreloadScript_4.vim
1620 assert_fails('source XreloadScript_4.vim', 'E1190: 2 arguments too few')
1621 unlet g:didload
1622
1623 delete('XreloadScript_1.vim')
1624 delete('XreloadScript_2.vim')
1625 delete('XreloadScript_3.vim')
1626 delete('XreloadScript_4.vim')
1627enddef
1628
1629def Test_vim9_reload_noclear_error()
1630 var lines =<< trim END
1631 vim9script noclear
1632
1633 if !exists('g:didload')
1634 def Test(a: string)
1635 echo a
1636 enddef
1637 def Call()
1638 Test('a')
1639 enddef
1640 else
1641 # redefine with a compile error
1642 def Test(a: string)
1643 echo ax
1644 enddef
1645 endif
1646 Call()
1647 g:didload = 1
1648 END
1649 lines->writefile('XreloadScriptErr.vim')
1650 source XreloadScriptErr.vim
1651 assert_fails('source XreloadScriptErr.vim', 'E1001: Variable not found: ax')
1652
1653 unlet g:didload
1654 delete('XreloadScriptErr.vim')
1655enddef
1656
Bram Moolenaard8448622022-01-07 21:39:52 +00001657def Test_vim9_reload_import()
1658 var lines =<< trim END
1659 vim9script
1660 const var = ''
1661 var valone = 1234
1662 def MyFunc(arg: string)
1663 valone = 5678
1664 enddef
1665 END
1666 var morelines =<< trim END
1667 var valtwo = 222
1668 export def GetValtwo(): number
1669 return valtwo
1670 enddef
1671 END
1672 writefile(lines + morelines, 'Xreload.vim')
1673 source Xreload.vim
1674 source Xreload.vim
1675 source Xreload.vim
1676
1677 # cannot declare a var twice
1678 lines =<< trim END
1679 vim9script
1680 var valone = 1234
1681 var valone = 5678
1682 END
1683 writefile(lines, 'Xreload.vim')
1684 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
1685
1686 delete('Xreload.vim')
1687 delete('Ximport.vim')
1688enddef
1689
1690" if a script is reloaded with a script-local variable that changed its type, a
1691" compiled function using that variable must fail.
1692def Test_script_reload_change_type()
1693 var lines =<< trim END
1694 vim9script noclear
1695 var str = 'string'
1696 def g:GetStr(): string
1697 return str .. 'xxx'
1698 enddef
1699 END
1700 writefile(lines, 'Xreload.vim')
1701 source Xreload.vim
1702 echo g:GetStr()
1703
1704 lines =<< trim END
1705 vim9script noclear
1706 var str = 1234
1707 END
1708 writefile(lines, 'Xreload.vim')
1709 source Xreload.vim
1710 assert_fails('echo g:GetStr()', 'E1150:')
1711
1712 delfunc g:GetStr
1713 delete('Xreload.vim')
1714enddef
1715
1716" Define CallFunc so that the test can be compiled
1717command CallFunc echo 'nop'
1718
1719def Test_script_reload_from_function()
1720 var lines =<< trim END
1721 vim9script
1722
Bram Moolenaar10611952022-04-03 21:11:34 +01001723 if exists('g:loadedThis')
Bram Moolenaard8448622022-01-07 21:39:52 +00001724 finish
1725 endif
Bram Moolenaar10611952022-04-03 21:11:34 +01001726 g:loadedThis = 1
Bram Moolenaard8448622022-01-07 21:39:52 +00001727 delcommand CallFunc
1728 command CallFunc Func()
1729 def Func()
1730 so XreloadFunc.vim
1731 g:didTheFunc = 1
1732 enddef
1733 END
1734 writefile(lines, 'XreloadFunc.vim')
1735 source XreloadFunc.vim
1736 CallFunc
1737 assert_equal(1, g:didTheFunc)
1738
1739 delete('XreloadFunc.vim')
1740 delcommand CallFunc
Bram Moolenaar10611952022-04-03 21:11:34 +01001741 unlet g:loadedThis
Bram Moolenaard8448622022-01-07 21:39:52 +00001742 unlet g:didTheFunc
1743enddef
1744
1745def s:RetSome(): string
1746 return 'some'
1747enddef
1748
1749" Not exported function that is referenced needs to be accessed by the
1750" script-local name.
1751def Test_vim9_funcref()
1752 var sortlines =<< trim END
1753 vim9script
1754 def Compare(i1: number, i2: number): number
1755 return i2 - i1
1756 enddef
1757
1758 export def FastSort(): list<number>
1759 return range(5)->sort(Compare)
1760 enddef
1761
1762 export def GetString(arg: string): string
1763 return arg
1764 enddef
1765 END
1766 writefile(sortlines, 'Xsort.vim')
1767
1768 var lines =<< trim END
1769 vim9script
1770 import './Xsort.vim'
1771 def Test()
1772 g:result = Xsort.FastSort()
1773 enddef
1774 Test()
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001775 END
1776 writefile(lines, 'Xscript.vim')
1777 source Xscript.vim
1778 assert_equal([4, 3, 2, 1, 0], g:result)
1779 unlet g:result
Bram Moolenaard8448622022-01-07 21:39:52 +00001780
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001781 lines =<< trim END
1782 vim9script
Bram Moolenaard8448622022-01-07 21:39:52 +00001783 # using a function imported with "as"
1784 import './Xsort.vim' as anAlias
1785 assert_equal('yes', anAlias.GetString('yes'))
1786
1787 # using the function from a compiled function
1788 def TestMore(): string
1789 var s = s:anAlias.GetString('foo')
1790 return s .. anAlias.GetString('bar')
1791 enddef
1792 assert_equal('foobar', TestMore())
1793
1794 # error when using a function that isn't exported
1795 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
1796 END
1797 writefile(lines, 'Xscript.vim')
1798
Bram Moolenaard8448622022-01-07 21:39:52 +00001799 delete('Xsort.vim')
1800 delete('Xscript.vim')
1801
1802 var Funcref = function('s:RetSome')
1803 assert_equal('some', Funcref())
1804enddef
1805
1806" Check that when searching for "FilterFunc" it finds the import in the
1807" script where FastFilter() is called from, both as a string and as a direct
1808" function reference.
1809def Test_vim9_funcref_other_script()
1810 var filterLines =<< trim END
1811 vim9script
1812 export def FilterFunc(idx: number, val: number): bool
1813 return idx % 2 == 1
1814 enddef
1815 export def FastFilter(): list<number>
1816 return range(10)->filter('FilterFunc(v:key, v:val)')
1817 enddef
1818 export def FastFilterDirect(): list<number>
1819 return range(10)->filter(FilterFunc)
1820 enddef
1821 END
1822 writefile(filterLines, 'Xfilter.vim')
1823
1824 var lines =<< trim END
1825 vim9script
1826 import './Xfilter.vim' as filter
1827 def Test()
1828 var x: list<number> = filter.FastFilter()
1829 enddef
1830 Test()
1831 def TestDirect()
1832 var x: list<number> = filter.FastFilterDirect()
1833 enddef
1834 TestDirect()
1835 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001836 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +00001837 delete('Xfilter.vim')
1838enddef
1839
1840def Test_import_absolute()
1841 var import_lines = [
1842 'vim9script',
1843 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
1844 'def UseExported()',
1845 ' g:imported_abs = abs.exported',
1846 ' abs.exported = 8888',
1847 ' g:imported_after = abs.exported',
1848 'enddef',
1849 'UseExported()',
1850 'g:import_disassembled = execute("disass UseExported")',
1851 ]
1852 writefile(import_lines, 'Ximport_abs.vim')
1853 writefile(s:export_script_lines, 'Xexport_abs.vim')
1854
1855 source Ximport_abs.vim
1856
1857 assert_equal(9876, g:imported_abs)
1858 assert_equal(8888, g:imported_after)
1859 assert_match('<SNR>\d\+_UseExported\_s*' ..
1860 'g:imported_abs = abs.exported\_s*' ..
1861 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1862 '1 STOREG g:imported_abs\_s*' ..
1863 'abs.exported = 8888\_s*' ..
1864 '2 PUSHNR 8888\_s*' ..
1865 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1866 'g:imported_after = abs.exported\_s*' ..
1867 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1868 '5 STOREG g:imported_after',
1869 g:import_disassembled)
1870
1871 Undo_export_script_lines()
1872 unlet g:imported_abs
1873 unlet g:import_disassembled
1874
1875 delete('Ximport_abs.vim')
1876 delete('Xexport_abs.vim')
1877enddef
1878
1879def Test_import_rtp()
1880 var import_lines = [
1881 'vim9script',
1882 'import "Xexport_rtp.vim" as rtp',
1883 'g:imported_rtp = rtp.exported',
1884 ]
1885 writefile(import_lines, 'Ximport_rtp.vim')
1886 mkdir('import', 'p')
1887 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1888
1889 var save_rtp = &rtp
1890 &rtp = getcwd()
1891 source Ximport_rtp.vim
1892 &rtp = save_rtp
1893
1894 assert_equal(9876, g:imported_rtp)
1895
1896 Undo_export_script_lines()
1897 unlet g:imported_rtp
1898 delete('Ximport_rtp.vim')
1899 delete('import', 'rf')
1900enddef
1901
1902def Test_import_compile_error()
1903 var export_lines = [
1904 'vim9script',
1905 'export def ExpFunc(): string',
1906 ' return notDefined',
1907 'enddef',
1908 ]
1909 writefile(export_lines, 'Xexported.vim')
1910
1911 var import_lines = [
1912 'vim9script',
1913 'import "./Xexported.vim" as expo',
1914 'def ImpFunc()',
1915 ' echo expo.ExpFunc()',
1916 'enddef',
1917 'defcompile',
1918 ]
1919 writefile(import_lines, 'Ximport.vim')
1920
1921 try
1922 source Ximport.vim
1923 catch /E1001/
1924 # Error should be before the Xexported.vim file.
1925 assert_match('E1001: Variable not found: notDefined', v:exception)
1926 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1927 endtry
1928
1929 delete('Xexported.vim')
1930 delete('Ximport.vim')
1931enddef
1932
1933def Test_func_overrules_import_fails()
1934 var export_lines =<< trim END
1935 vim9script
1936 export def Func()
1937 echo 'imported'
1938 enddef
1939 END
1940 writefile(export_lines, 'XexportedFunc.vim')
1941
1942 var lines =<< trim END
1943 vim9script
1944 import './XexportedFunc.vim' as Func
1945 def Func()
1946 echo 'local to function'
1947 enddef
1948 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001949 v9.CheckScriptFailure(lines, 'E1213: Redefining imported item "Func"')
Bram Moolenaard8448622022-01-07 21:39:52 +00001950
1951 lines =<< trim END
1952 vim9script
1953 import './XexportedFunc.vim' as Func
1954 def Outer()
1955 def Func()
1956 echo 'local to function'
1957 enddef
1958 enddef
1959 defcompile
1960 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001961 v9.CheckScriptFailure(lines, 'E1236:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001962
1963 delete('XexportedFunc.vim')
1964enddef
1965
1966def Test_source_vim9_from_legacy()
1967 var vim9_lines =<< trim END
1968 vim9script
1969 var local = 'local'
1970 g:global = 'global'
1971 export var exported = 'exported'
1972 export def GetText(): string
1973 return 'text'
1974 enddef
1975 END
1976 writefile(vim9_lines, 'Xvim9_script.vim')
1977
1978 var legacy_lines =<< trim END
1979 source Xvim9_script.vim
1980
1981 call assert_false(exists('local'))
1982 call assert_false(exists('exported'))
1983 call assert_false(exists('s:exported'))
1984 call assert_equal('global', global)
1985 call assert_equal('global', g:global)
Bram Moolenaard8448622022-01-07 21:39:52 +00001986 END
1987 writefile(legacy_lines, 'Xlegacy_script.vim')
1988
1989 source Xlegacy_script.vim
1990 assert_equal('global', g:global)
1991 unlet g:global
1992
1993 delete('Xlegacy_script.vim')
1994 delete('Xvim9_script.vim')
1995enddef
1996
Bram Moolenaarc43e6232022-01-13 20:51:56 +00001997def Test_import_vim9_from_legacy()
1998 var vim9_lines =<< trim END
1999 vim9script
2000 var local = 'local'
2001 g:global = 'global'
2002 export var exported = 'exported'
2003 export def GetText(): string
2004 return 'text'
2005 enddef
2006 END
2007 writefile(vim9_lines, 'Xvim9_export.vim')
2008
2009 var legacy_lines =<< trim END
2010 import './Xvim9_export.vim' as vim9
2011
2012 call assert_false(exists('vim9'))
2013 call assert_false(exists('local'))
2014 call assert_false(exists('s:vim9.local'))
2015 call assert_equal('global', global)
2016 call assert_equal('global', g:global)
2017 call assert_false(exists('exported'))
2018 call assert_false(exists('s:exported'))
2019 call assert_false(exists('*GetText'))
2020
2021 " imported symbol is script-local
2022 call assert_equal('exported', s:vim9.exported)
2023 call assert_equal('text', s:vim9.GetText())
2024 END
2025 writefile(legacy_lines, 'Xlegacy_script.vim')
2026
2027 source Xlegacy_script.vim
2028 assert_equal('global', g:global)
2029 unlet g:global
2030
2031 delete('Xlegacy_script.vim')
2032 delete('Xvim9_export.vim')
2033enddef
2034
Bram Moolenaard8448622022-01-07 21:39:52 +00002035def Test_cmdline_win()
2036 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
2037 # the command line window.
2038 mkdir('rtp/syntax', 'p')
2039 var export_lines =<< trim END
2040 vim9script
2041 export var That = 'yes'
2042 END
2043 writefile(export_lines, 'rtp/syntax/Xexport.vim')
2044 var import_lines =<< trim END
2045 vim9script
2046 import './Xexport.vim' as exp
2047 echo exp.That
2048 END
2049 writefile(import_lines, 'rtp/syntax/vim.vim')
2050 var save_rtp = &rtp
2051 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
2052 syntax on
2053 augroup CmdWin
2054 autocmd CmdwinEnter * g:got_there = 'yes'
2055 augroup END
2056 # this will open and also close the cmdline window
2057 feedkeys('q:', 'xt')
2058 assert_equal('yes', g:got_there)
2059
2060 augroup CmdWin
2061 au!
2062 augroup END
2063 &rtp = save_rtp
2064 delete('rtp', 'rf')
2065enddef
2066
2067def Test_import_gone_when_sourced_twice()
2068 var exportlines =<< trim END
2069 vim9script
2070 if exists('g:guard')
2071 finish
2072 endif
2073 g:guard = 1
2074 export var name = 'someName'
2075 END
2076 writefile(exportlines, 'XexportScript.vim')
2077
2078 var lines =<< trim END
2079 vim9script
2080 import './XexportScript.vim' as expo
2081 def g:GetName(): string
2082 return expo.name
2083 enddef
2084 END
2085 writefile(lines, 'XscriptImport.vim')
2086 so XscriptImport.vim
2087 assert_equal('someName', g:GetName())
2088
2089 so XexportScript.vim
2090 assert_fails('call g:GetName()', 'E1149:')
2091
2092 delfunc g:GetName
2093 delete('XexportScript.vim')
2094 delete('XscriptImport.vim')
2095 unlet g:guard
2096enddef
2097
Bram Moolenaar160aa862022-01-10 21:29:57 +00002098" test using an auto-loaded function and variable
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002099def Test_vim9_autoload_full_name()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002100 var lines =<< trim END
2101 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002102 export def Gettest(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002103 return 'test'
2104 enddef
2105 g:some#name = 'name'
2106 g:some#dict = {key: 'value'}
2107
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002108 export def Varargs(a1: string, ...l: list<string>): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002109 return a1 .. l[0] .. l[1]
2110 enddef
2111 END
2112
2113 mkdir('Xdir/autoload', 'p')
2114 writefile(lines, 'Xdir/autoload/some.vim')
2115 var save_rtp = &rtp
2116 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2117
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002118 assert_equal('test', g:some#Gettest())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002119 assert_equal('name', g:some#name)
2120 assert_equal('value', g:some#dict.key)
2121 g:some#other = 'other'
2122 assert_equal('other', g:some#other)
2123
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002124 assert_equal('abc', some#Varargs('a', 'b', 'c'))
Bram Moolenaar160aa862022-01-10 21:29:57 +00002125
2126 # upper case script name works
2127 lines =<< trim END
2128 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002129 export def GetOther(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002130 return 'other'
2131 enddef
2132 END
2133 writefile(lines, 'Xdir/autoload/Other.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002134 assert_equal('other', g:Other#GetOther())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002135
2136 delete('Xdir', 'rf')
2137 &rtp = save_rtp
2138enddef
2139
2140def Test_vim9script_autoload()
2141 mkdir('Xdir/autoload', 'p')
2142 var save_rtp = &rtp
2143 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2144
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002145 # when the path has "/autoload/" prefix is not needed
Bram Moolenaar160aa862022-01-10 21:29:57 +00002146 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002147 vim9script
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002148 g:prefixed_loaded += 1
Bram Moolenaar160aa862022-01-10 21:29:57 +00002149
2150 export def Gettest(): string
2151 return 'test'
2152 enddef
2153
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002154 export var name = 'name'
2155
2156 export func GetFunc()
2157 return Gettest() .. 'more' .. s:name
Bram Moolenaar160aa862022-01-10 21:29:57 +00002158 endfunc
2159
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002160 export def GetDef(): string
2161 return Gettest() .. 'more' .. name
2162 enddef
2163
Bram Moolenaar160aa862022-01-10 21:29:57 +00002164 export final fname = 'final'
2165 export const cname = 'const'
2166 END
2167 writefile(lines, 'Xdir/autoload/prefixed.vim')
2168
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002169 g:prefixed_loaded = 0
2170 g:expected_loaded = 0
Bram Moolenaar160aa862022-01-10 21:29:57 +00002171 lines =<< trim END
2172 vim9script
2173 import autoload 'prefixed.vim'
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002174 assert_equal(g:expected_loaded, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002175 assert_equal('test', prefixed.Gettest())
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002176 assert_equal(1, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002177
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002178 assert_equal('testmorename', prefixed.GetFunc())
2179 assert_equal('testmorename', prefixed.GetDef())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002180 assert_equal('name', prefixed.name)
2181 assert_equal('final', prefixed.fname)
2182 assert_equal('const', prefixed.cname)
2183 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002184 v9.CheckScriptSuccess(lines)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002185 # can source it again, autoload script not loaded again
2186 g:expected_loaded = 1
Bram Moolenaar62aec932022-01-29 21:45:34 +00002187 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002188
2189 # can also get the items by autoload name
2190 lines =<< trim END
2191 call assert_equal('test', prefixed#Gettest())
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002192 call assert_equal('testmorename', prefixed#GetFunc())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002193 call assert_equal('name', prefixed#name)
2194 call assert_equal('final', prefixed#fname)
2195 call assert_equal('const', prefixed#cname)
2196 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002197 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002198
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002199 unlet g:prefixed_loaded
2200 unlet g:expected_loaded
2201 delete('Xdir', 'rf')
2202 &rtp = save_rtp
2203enddef
2204
Bram Moolenaard02dce22022-01-18 17:43:04 +00002205def Test_import_autoload_not_exported()
2206 mkdir('Xdir/autoload', 'p')
2207 var save_rtp = &rtp
2208 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2209
2210 # error when using an item that is not exported from an autoload script
2211 var exportLines =<< trim END
2212 vim9script
2213 var notExported = 123
2214 def NotExport()
2215 echo 'nop'
2216 enddef
2217 END
2218 writefile(exportLines, 'Xdir/autoload/notExport1.vim')
2219
2220 var lines =<< trim END
2221 vim9script
2222 import autoload 'notExport1.vim'
2223 echo notExport1.notFound
2224 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002225 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002226
2227 lines =<< trim END
2228 vim9script
2229 import autoload 'notExport1.vim'
2230 echo notExport1.notExported
2231 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002232 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002233
2234 lines =<< trim END
2235 vim9script
2236 import autoload 'notExport1.vim'
2237 echo notExport1.NotFunc()
2238 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002239 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002240
2241 lines =<< trim END
2242 vim9script
2243 import autoload 'notExport1.vim'
2244 echo notExport1.NotExport()
2245 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002246 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002247
2248 lines =<< trim END
2249 vim9script
2250 import autoload 'notExport1.vim'
2251 echo 'text'->notExport1.NotFunc()
2252 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002253 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002254
2255 lines =<< trim END
2256 vim9script
2257 import autoload 'notExport1.vim'
2258 echo 'text'->notExport1.NotExport()
2259 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002260 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002261
2262 # using a :def function we use a different autoload script every time so that
2263 # the function is compiled without the script loaded
2264 writefile(exportLines, 'Xdir/autoload/notExport2.vim')
2265 lines =<< trim END
2266 vim9script
2267 import autoload 'notExport2.vim'
2268 def Testit()
2269 echo notExport2.notFound
2270 enddef
2271 Testit()
2272 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002273 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport2#notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002274
2275 writefile(exportLines, 'Xdir/autoload/notExport3.vim')
2276 lines =<< trim END
2277 vim9script
2278 import autoload 'notExport3.vim'
2279 def Testit()
2280 echo notExport3.notExported
2281 enddef
2282 Testit()
2283 END
2284 # don't get E1049 because it is too complicated to figure out
Bram Moolenaar62aec932022-01-29 21:45:34 +00002285 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport3#notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002286
2287 writefile(exportLines, 'Xdir/autoload/notExport4.vim')
2288 lines =<< trim END
2289 vim9script
2290 import autoload 'notExport4.vim'
2291 def Testit()
2292 echo notExport4.NotFunc()
2293 enddef
2294 Testit()
2295 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002296 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport4#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002297
2298 writefile(exportLines, 'Xdir/autoload/notExport5.vim')
2299 lines =<< trim END
2300 vim9script
2301 import autoload 'notExport5.vim'
2302 def Testit()
2303 echo notExport5.NotExport()
2304 enddef
2305 Testit()
2306 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002307 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport5#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002308
2309 writefile(exportLines, 'Xdir/autoload/notExport6.vim')
2310 lines =<< trim END
2311 vim9script
2312 import autoload 'notExport6.vim'
2313 def Testit()
2314 echo 'text'->notExport6.NotFunc()
2315 enddef
2316 Testit()
2317 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002318 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport6#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002319
2320 writefile(exportLines, 'Xdir/autoload/notExport7.vim')
2321 lines =<< trim END
2322 vim9script
2323 import autoload 'notExport7.vim'
2324 def Testit()
2325 echo 'text'->notExport7.NotExport()
2326 enddef
2327 Testit()
2328 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002329 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport7#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002330
2331 delete('Xdir', 'rf')
2332 &rtp = save_rtp
2333enddef
2334
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002335def Test_vim9script_autoload_call()
2336 mkdir('Xdir/autoload', 'p')
2337 var save_rtp = &rtp
2338 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2339
2340 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002341 vim9script
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002342
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002343 export def RetArg(arg: string): string
2344 return arg
2345 enddef
2346
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002347 export def Getother()
2348 g:result = 'other'
2349 enddef
2350 END
Bram Moolenaar5d982692022-01-12 15:15:27 +00002351 writefile(lines, 'Xdir/autoload/another.vim')
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002352
2353 lines =<< trim END
2354 vim9script
Bram Moolenaar5d982692022-01-12 15:15:27 +00002355 import autoload 'another.vim'
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002356
2357 # compile this before 'another.vim' is loaded
2358 def CallAnother()
2359 assert_equal('foo', 'foo'->another.RetArg())
2360 enddef
2361 CallAnother()
2362
Bram Moolenaar5d982692022-01-12 15:15:27 +00002363 call another.Getother()
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002364 assert_equal('other', g:result)
Bram Moolenaar3d8e25a2022-01-22 11:00:02 +00002365
2366 assert_equal('arg', call('another.RetArg', ['arg']))
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00002367
2368 verbose function another.Getother
2369 # should we disallow this?
2370 verbose function another#Getother
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002371 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002372 v9.CheckScriptSuccess(lines)
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002373
2374 unlet g:result
Bram Moolenaar160aa862022-01-10 21:29:57 +00002375 delete('Xdir', 'rf')
2376 &rtp = save_rtp
2377enddef
2378
Bram Moolenaarb697dc22022-01-22 11:27:29 +00002379def Test_vim9script_noclear_autoload()
2380 mkdir('Xdir/autoload', 'p')
2381 var save_rtp = &rtp
2382 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2383
2384 var lines =<< trim END
2385 vim9script
2386 export def Func(): string
2387 return 'called'
2388 enddef
2389 g:double_loaded = 'yes'
2390 END
2391 writefile(lines, 'Xdir/autoload/double.vim')
2392
2393 lines =<< trim END
2394 vim9script noclear
2395 if exists('g:script_loaded')
2396 finish
2397 endif
2398 g:script_loaded = true
2399
2400 import autoload 'double.vim'
2401 nnoremap <F3> <ScriptCmd>g:result = double.Func()<CR>
2402 END
2403 g:double_loaded = 'no'
2404 writefile(lines, 'Xloaddouble')
2405 source Xloaddouble
2406 assert_equal('no', g:double_loaded)
2407 assert_equal(true, g:script_loaded)
2408 source Xloaddouble
2409 feedkeys("\<F3>", 'xt')
2410 assert_equal('called', g:result)
2411 assert_equal('yes', g:double_loaded)
2412
2413 delete('Xloaddouble')
2414 unlet g:double_loaded
2415 unlet g:script_loaded
2416 unlet g:result
2417 delete('Xdir', 'rf')
2418 &rtp = save_rtp
2419enddef
2420
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002421def Test_vim9script_autoload_duplicate()
2422 mkdir('Xdir/autoload', 'p')
2423
2424 var lines =<< trim END
2425 vim9script
2426
2427 export def Func()
2428 enddef
2429
2430 def Func()
2431 enddef
2432 END
2433 writefile(lines, 'Xdir/autoload/dupfunc.vim')
2434 assert_fails('source Xdir/autoload/dupfunc.vim', 'E1073:')
2435
2436 lines =<< trim END
2437 vim9script
2438
2439 def Func()
2440 enddef
2441
2442 export def Func()
2443 enddef
2444 END
2445 writefile(lines, 'Xdir/autoload/dup2func.vim')
2446 assert_fails('source Xdir/autoload/dup2func.vim', 'E1073:')
2447
2448 lines =<< trim END
2449 vim9script
2450
2451 def Func()
2452 enddef
2453
2454 export var Func = 'asdf'
2455 END
2456 writefile(lines, 'Xdir/autoload/dup3func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002457 assert_fails('source Xdir/autoload/dup3func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002458
2459 lines =<< trim END
2460 vim9script
2461
2462 export var Func = 'asdf'
2463
2464 def Func()
2465 enddef
2466 END
2467 writefile(lines, 'Xdir/autoload/dup4func.vim')
2468 assert_fails('source Xdir/autoload/dup4func.vim', 'E707:')
2469
2470 lines =<< trim END
2471 vim9script
2472
2473 var Func = 'asdf'
2474
2475 export def Func()
2476 enddef
2477 END
2478 writefile(lines, 'Xdir/autoload/dup5func.vim')
2479 assert_fails('source Xdir/autoload/dup5func.vim', 'E707:')
2480
2481 lines =<< trim END
2482 vim9script
2483
2484 export def Func()
2485 enddef
2486
2487 var Func = 'asdf'
2488 END
2489 writefile(lines, 'Xdir/autoload/dup6func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002490 assert_fails('source Xdir/autoload/dup6func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002491
2492 delete('Xdir', 'rf')
2493enddef
2494
Bram Moolenaar2017d6f2022-01-20 19:38:46 +00002495def Test_autoload_missing_function_name()
2496 mkdir('Xdir/autoload', 'p')
2497
2498 var lines =<< trim END
2499 vim9script
2500
2501 def loadme#()
2502 enddef
2503 END
2504 writefile(lines, 'Xdir/autoload/loadme.vim')
2505 assert_fails('source Xdir/autoload/loadme.vim', 'E129:')
2506
2507 delete('Xdir', 'rf')
2508enddef
2509
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002510def Test_autoload_name_wrong()
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002511 var lines =<< trim END
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002512 def Xscriptname#Func()
2513 enddef
2514 END
2515 writefile(lines, 'Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002516 v9.CheckScriptFailure(lines, 'E746:')
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00002517 delete('Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002518
2519 mkdir('Xdir/autoload', 'p')
2520 lines =<< trim END
2521 vim9script
2522 def somescript#Func()
2523 enddef
2524 END
2525 writefile(lines, 'Xdir/autoload/somescript.vim')
2526 assert_fails('source Xdir/autoload/somescript.vim', 'E1263:')
2527
2528 delete('Xdir', 'rf')
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002529enddef
2530
Bram Moolenaard041f422022-01-12 19:54:00 +00002531def Test_import_autoload_postponed()
2532 mkdir('Xdir/autoload', 'p')
2533 var save_rtp = &rtp
2534 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2535
2536 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002537 vim9script
Bram Moolenaard041f422022-01-12 19:54:00 +00002538
2539 g:loaded_postponed = 'true'
2540 export var variable = 'bla'
2541 export def Function(): string
2542 return 'bla'
2543 enddef
2544 END
2545 writefile(lines, 'Xdir/autoload/postponed.vim')
2546
2547 lines =<< trim END
2548 vim9script
2549
2550 import autoload 'postponed.vim'
2551 def Tryit()
2552 echo postponed.variable
2553 echo postponed.Function()
2554 enddef
2555 defcompile
2556 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002557 v9.CheckScriptSuccess(lines)
Bram Moolenaard041f422022-01-12 19:54:00 +00002558 assert_false(exists('g:loaded_postponed'))
Bram Moolenaar62aec932022-01-29 21:45:34 +00002559 v9.CheckScriptSuccess(lines + ['Tryit()'])
Bram Moolenaard041f422022-01-12 19:54:00 +00002560 assert_equal('true', g:loaded_postponed)
2561
2562 unlet g:loaded_postponed
2563 delete('Xdir', 'rf')
2564 &rtp = save_rtp
2565enddef
2566
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002567def Test_import_autoload_override()
2568 mkdir('Xdir/autoload', 'p')
2569 var save_rtp = &rtp
2570 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2571 test_override('autoload', 1)
2572
2573 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002574 vim9script
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002575
2576 g:loaded_override = 'true'
2577 export var variable = 'bla'
2578 export def Function(): string
2579 return 'bla'
2580 enddef
2581 END
2582 writefile(lines, 'Xdir/autoload/override.vim')
2583
2584 lines =<< trim END
2585 vim9script
2586
2587 import autoload 'override.vim'
2588 assert_equal('true', g:loaded_override)
2589
2590 def Tryit()
2591 echo override.doesNotExist
2592 enddef
2593 defcompile
2594 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002595 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1)
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002596
2597 test_override('autoload', 0)
2598 unlet g:loaded_override
2599 delete('Xdir', 'rf')
2600 &rtp = save_rtp
2601enddef
2602
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002603def Test_autoload_mapping()
2604 mkdir('Xdir/autoload', 'p')
2605 var save_rtp = &rtp
2606 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2607
2608 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002609 vim9script
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002610
2611 g:toggle_loaded = 'yes'
2612
2613 export def Toggle(): string
2614 return ":g:toggle_called = 'yes'\<CR>"
2615 enddef
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002616 export def Doit()
2617 g:doit_called = 'yes'
2618 enddef
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002619 END
2620 writefile(lines, 'Xdir/autoload/toggle.vim')
2621
2622 lines =<< trim END
2623 vim9script
2624
2625 import autoload 'toggle.vim'
2626
2627 nnoremap <silent> <expr> tt toggle.Toggle()
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002628 nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR>
2629 nnoremap <silent> yy <Cmd>toggle.Doit()<CR>
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002630 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002631 v9.CheckScriptSuccess(lines)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002632 assert_false(exists("g:toggle_loaded"))
2633 assert_false(exists("g:toggle_called"))
Bram Moolenaar6079da72022-01-18 14:16:59 +00002634 assert_match('\d A: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002635
2636 feedkeys("tt", 'xt')
2637 assert_equal('yes', g:toggle_loaded)
2638 assert_equal('yes', g:toggle_called)
Bram Moolenaar6079da72022-01-18 14:16:59 +00002639 assert_match('\d: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002640
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002641 feedkeys("xx", 'xt')
2642 assert_equal('yes', g:doit_called)
2643
2644 assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle')
2645
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002646 nunmap tt
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002647 nunmap xx
2648 nunmap yy
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002649 unlet g:toggle_loaded
2650 unlet g:toggle_called
2651 delete('Xdir', 'rf')
2652 &rtp = save_rtp
2653enddef
2654
Bram Moolenaar160aa862022-01-10 21:29:57 +00002655def Test_vim9script_autoload_fails()
2656 var lines =<< trim END
2657 vim9script autoload
2658 var n = 0
2659 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002660 v9.CheckScriptFailure(lines, 'E475: Invalid argument: autoload')
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002661
2662 lines =<< trim END
2663 vim9script noclear noclear
2664 var n = 0
2665 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002666 v9.CheckScriptFailure(lines, 'E983: Duplicate argument: noclear')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +01002667
2668 lines =<< trim END
2669 vim9script noclears
2670 var n = 0
2671 END
2672 v9.CheckScriptFailure(lines, 'E475: Invalid argument: noclears')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002673enddef
2674
2675def Test_import_autoload_fails()
2676 var lines =<< trim END
2677 vim9script
2678 import autoload autoload 'prefixed.vim'
2679 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002680 v9.CheckScriptFailure(lines, 'E121: Undefined variable: autoload')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002681
2682 lines =<< trim END
2683 vim9script
Bram Moolenaar1836d612022-01-18 13:14:47 +00002684 import autoload './doesNotExist.vim'
Bram Moolenaar160aa862022-01-10 21:29:57 +00002685 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002686 v9.CheckScriptFailure(lines, 'E282:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002687
2688 lines =<< trim END
2689 vim9script
2690 import autoload '/dir/doesNotExist.vim'
2691 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002692 v9.CheckScriptFailure(lines, 'E282:', 2)
2693
2694 lines =<< trim END
2695 vim9script
2696 import autoload '../testdir'
2697 END
2698 v9.CheckScriptFailure(lines, 'E17:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002699
2700 lines =<< trim END
2701 vim9script
2702 import autoload 'doesNotExist.vim'
2703 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002704 v9.CheckScriptFailure(lines, 'E1053: Could not import "doesNotExist.vim"')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002705enddef
2706
2707" test disassembling an auto-loaded function starting with "debug"
2708def Test_vim9_autoload_disass()
2709 mkdir('Xdir/autoload', 'p')
2710 var save_rtp = &rtp
2711 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2712
2713 var lines =<< trim END
2714 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002715 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002716 return 'debug'
2717 enddef
2718 END
2719 writefile(lines, 'Xdir/autoload/debugit.vim')
2720
2721 lines =<< trim END
2722 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002723 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002724 return 'profile'
2725 enddef
2726 END
2727 writefile(lines, 'Xdir/autoload/profileit.vim')
2728
2729 lines =<< trim END
2730 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002731 assert_equal('debug', debugit#Test())
2732 disass debugit#Test
2733 assert_equal('profile', profileit#Test())
2734 disass profileit#Test
Bram Moolenaar160aa862022-01-10 21:29:57 +00002735 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002736 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002737
2738 delete('Xdir', 'rf')
2739 &rtp = save_rtp
2740enddef
2741
2742" test using a vim9script that is auto-loaded from an autocmd
2743def Test_vim9_aucmd_autoload()
2744 var lines =<< trim END
2745 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002746 export def Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002747 echomsg getreg('"')
2748 enddef
2749 END
2750
2751 mkdir('Xdir/autoload', 'p')
2752 writefile(lines, 'Xdir/autoload/foo.vim')
2753 var save_rtp = &rtp
2754 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2755 augroup test
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002756 autocmd TextYankPost * call foo#Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002757 augroup END
2758
2759 normal Y
2760
2761 augroup test
2762 autocmd!
2763 augroup END
2764 delete('Xdir', 'rf')
2765 &rtp = save_rtp
2766enddef
2767
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002768" test using a autoloaded file that is case sensitive
2769def Test_vim9_autoload_case_sensitive()
2770 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002771 vim9script
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002772 export def CaseSensitive(): string
2773 return 'done'
2774 enddef
2775 END
2776
2777 mkdir('Xdir/autoload', 'p')
2778 writefile(lines, 'Xdir/autoload/CaseSensitive.vim')
2779 var save_rtp = &rtp
2780 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2781
2782 lines =<< trim END
2783 vim9script
2784 import autoload 'CaseSensitive.vim'
2785 assert_equal('done', CaseSensitive.CaseSensitive())
2786 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002787 v9.CheckScriptSuccess(lines)
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002788
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002789 if !has('fname_case')
2790 lines =<< trim END
2791 vim9script
2792 import autoload 'CaseSensitive.vim'
2793 import autoload 'casesensitive.vim'
2794 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002795 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002796 endif
2797
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002798 delete('Xdir', 'rf')
2799 &rtp = save_rtp
2800enddef
2801
Bram Moolenaar160aa862022-01-10 21:29:57 +00002802" This was causing a crash because suppress_errthrow wasn't reset.
2803def Test_vim9_autoload_error()
2804 var lines =<< trim END
2805 vim9script
2806 def crash#func()
2807 try
2808 for x in List()
2809 endfor
2810 catch
2811 endtry
2812 g:ok = true
2813 enddef
2814 fu List()
2815 invalid
2816 endfu
2817 try
2818 alsoinvalid
2819 catch /wontmatch/
2820 endtry
2821 END
2822 call mkdir('Xruntime/autoload', 'p')
2823 call writefile(lines, 'Xruntime/autoload/crash.vim')
2824
2825 # run in a separate Vim to avoid the side effects of assert_fails()
2826 lines =<< trim END
2827 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
2828 call crash#func()
2829 call writefile(['ok'], 'Xdidit')
2830 qall!
2831 END
2832 writefile(lines, 'Xscript')
Bram Moolenaar62aec932022-01-29 21:45:34 +00002833 g:RunVim([], [], '-S Xscript')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002834 assert_equal(['ok'], readfile('Xdidit'))
2835
2836 delete('Xdidit')
2837 delete('Xscript')
2838 delete('Xruntime', 'rf')
2839
2840 lines =<< trim END
2841 vim9script
2842 var foo#bar = 'asdf'
2843 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002844 v9.CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002845enddef
2846
Bram Moolenaard8448622022-01-07 21:39:52 +00002847
2848" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker