blob: d9b40d69702404d62a171d9227098bc10666e6f4 [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
Bram Moolenaar5cb53b72022-05-26 19:54:05 +0100315 # trailing starts with "as"
316 var import_bad_as_lines =<< trim END
317 vim9script
318 import './Xexport.vim' asname
319 END
320 writefile(import_no_as_lines, 'Ximport.vim')
321 assert_fails('source Ximport.vim', 'E488:', '', 2, 'Ximport.vim')
322
Bram Moolenaard8448622022-01-07 21:39:52 +0000323 var import_invalid_string_lines =<< trim END
324 vim9script
325 import Xexport.vim
326 END
327 writefile(import_invalid_string_lines, 'Ximport.vim')
328 assert_fails('source Ximport.vim', 'E121:', '', 2, 'Ximport.vim')
329
330 var import_wrong_name_lines =<< trim END
331 vim9script
332 import './XnoExport.vim'
333 END
334 writefile(import_wrong_name_lines, 'Ximport.vim')
335 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
336
337 var import_redefining_lines =<< trim END
338 vim9script
339 import './Xexport.vim' as exported
340 var exported = 5
341 END
342 writefile(import_redefining_lines, 'Ximport.vim')
343 assert_fails('source Ximport.vim', 'E1213: Redefining imported item "exported"', '', 3)
344
Bram Moolenaar160aa862022-01-10 21:29:57 +0000345 var import_missing_dot_lines =<< trim END
346 vim9script
347 import './Xexport.vim' as expo
348 def Test()
349 expo = 9
350 enddef
351 defcompile
352 END
353 writefile(import_missing_dot_lines, 'Ximport.vim')
354 assert_fails('source Ximport.vim', 'E1258:', '', 1)
355
356 var import_missing_name_lines =<< trim END
357 vim9script
358 import './Xexport.vim' as expo
359 def Test()
360 expo.99 = 9
361 enddef
362 defcompile
363 END
364 writefile(import_missing_name_lines, 'Ximport.vim')
Bram Moolenaar76283822022-01-10 21:39:03 +0000365 assert_fails('source Ximport.vim', 'E1259:', '', 1)
Bram Moolenaar160aa862022-01-10 21:29:57 +0000366
Bram Moolenaard8448622022-01-07 21:39:52 +0000367 var import_assign_wrong_type_lines =<< trim END
368 vim9script
369 import './Xexport.vim' as expo
370 expo.exported = 'xxx'
371 END
372 writefile(import_assign_wrong_type_lines, 'Ximport.vim')
373 assert_fails('source Ximport.vim', 'E1012: Type mismatch; expected number but got string', '', 3)
374
375 var import_assign_const_lines =<< trim END
376 vim9script
377 import './Xexport.vim' as expo
378 expo.CONST = 4321
379 END
380 writefile(import_assign_const_lines, 'Ximport.vim')
381 assert_fails('source Ximport.vim', 'E46: Cannot change read-only variable "CONST"', '', 3)
382
383 delete('Ximport.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000384 delete('Xexport.vim')
385
386 # Check that in a Vim9 script 'cpo' is set to the Vim default.
387 # Flags added or removed are also applied to the restored value.
388 set cpo=abcd
389 var lines =<< trim END
390 vim9script
391 g:cpo_in_vim9script = &cpo
392 set cpo+=f
393 set cpo-=c
394 g:cpo_after_vim9script = &cpo
395 END
396 writefile(lines, 'Xvim9_script')
397 source Xvim9_script
398 assert_equal('fabd', &cpo)
399 set cpo&vim
400 assert_equal(&cpo, g:cpo_in_vim9script)
401 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
402 assert_equal(newcpo, g:cpo_after_vim9script)
403
404 delete('Xvim9_script')
405enddef
406
Bram Moolenaar5cb53b72022-05-26 19:54:05 +0100407def Test_import_very_long_name()
408 var lines =<< trim END
409 vim9script
410
411 export var verylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongname = 'asdf'
412 END
413 writefile(lines, 'Xverylong.vim')
414
415 lines =<< trim END
416 vim9script
417 import './Xverylong.vim'
418
419 g:result = Xverylong.verylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongname
420 END
421 v9.CheckScriptSuccess(lines)
422 assert_equal('asdf', g:result)
423
424 delete('Xverylong.vim')
425 unlet g:result
426enddef
427
Bram Moolenaard8448622022-01-07 21:39:52 +0000428def Test_import_funcref()
429 var lines =<< trim END
430 vim9script
431 export def F(): number
432 return 42
433 enddef
434 export const G = F
435 END
436 writefile(lines, 'Xlib.vim')
437
438 lines =<< trim END
439 vim9script
440 import './Xlib.vim' as lib
441 const Foo = lib.G()
442 assert_equal(42, Foo)
443
444 def DoTest()
445 const Goo = lib.G()
446 assert_equal(42, Goo)
447 enddef
448 DoTest()
449 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000450 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000451
452 delete('Xlib.vim')
453enddef
454
Bram Moolenaarc2f17f72022-02-21 13:13:50 +0000455def Test_import_duplicate_function()
456 # Function Hover() exists in both scripts, partial should refer to the right
457 # one.
458 var lines =<< trim END
459 vim9script
460
461 def Hover(d: dict<any>): string
462 return 'found it'
463 enddef
464
465 export def NewLspServer(): dict<any>
466 var d: dict<any> = {}
467 d->extend({hover: function('Hover', [d])})
468 return d
469 enddef
470
471 NewLspServer()
472 END
473 writefile(lines, 'Xserver.vim')
474
475 lines =<< trim END
476 vim9script
477
478 import './Xserver.vim' as server
479
480 export def Hover()
481 enddef
482
483 def AddServer()
484 var d: dict<any> = server.NewLspServer()
485 assert_equal('found it', d.hover())
486 enddef
487 AddServer()
488 END
489 v9.CheckScriptSuccess(lines)
490
491 delete('Xserver.vim')
492enddef
493
494
Bram Moolenaard8448622022-01-07 21:39:52 +0000495def Test_import_fails()
496 writefile([], 'Xfoo.vim')
497 var lines =<< trim END
498 import './Xfoo.vim' as foo
499 foo = 'bar'
500 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000501 v9.CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use foo itself'])
Bram Moolenaard8448622022-01-07 21:39:52 +0000502 lines =<< trim END
503 vim9script
504 import './Xfoo.vim' as foo
505 var that = foo
506 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000507 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaardd5893b2022-01-20 21:32:54 +0000508 lines =<< trim END
509 vim9script
510 import './Xfoo.vim' as foo
511 var that: any
512 that += foo
513 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000514 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaardd5893b2022-01-20 21:32:54 +0000515 lines =<< trim END
516 vim9script
517 import './Xfoo.vim' as foo
518 foo += 9
519 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000520 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaard8448622022-01-07 21:39:52 +0000521
522 lines =<< trim END
523 vim9script
524 import './Xfoo.vim' as 9foo
525 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000526 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000527 lines =<< trim END
528 vim9script
529 import './Xfoo.vim' as the#foo
530 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000531 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000532 lines =<< trim END
533 vim9script
534 import './Xfoo.vim' as g:foo
535 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000536 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000537
Bram Moolenaard8448622022-01-07 21:39:52 +0000538 lines =<< trim END
539 vim9script
540 def TheFunc()
541 echo 'the func'
542 enddef
543 export var Ref = TheFunc
544 END
545 writefile([], 'Xthat.vim')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000546
Bram Moolenaard8448622022-01-07 21:39:52 +0000547 lines =<< trim END
548 import './Xthat.vim' as That
549 That()
550 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000551 v9.CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use That itself'])
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000552
553 lines =<< trim END
Bram Moolenaar937610b2022-01-19 17:21:29 +0000554 vim9script
555 import './Xthat.vim' as That
556 def Func()
557 echo That()
558 enddef
559 Func()
560 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000561 v9.CheckScriptFailure(lines, 'E1236: Cannot use That itself')
Bram Moolenaar937610b2022-01-19 17:21:29 +0000562
563 lines =<< trim END
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000564 import './Xthat.vim' as one
565 import './Xthat.vim' as two
566 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000567 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000568
569 delete('Xthat.vim')
Bram Moolenaar779aeff2022-02-08 19:12:19 +0000570
571 lines =<< trim END
572 vim9script
573 export var item = 'hello'
574 import './Xyourself.vim'
575 END
576 writefile(lines, 'Xyourself.vim')
577 assert_fails('source Xyourself.vim', 'E1088:')
578 delete('Xyourself.vim')
579
Bram Moolenaard8448622022-01-07 21:39:52 +0000580 mkdir('Ximport')
581
582 writefile(['vim9script'], 'Ximport/.vim')
583 lines =<< trim END
584 vim9script
585 import './Ximport/.vim'
586 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000587 v9.CheckScriptFailure(lines, 'E1261: Cannot import .vim without using "as"')
Bram Moolenaard8448622022-01-07 21:39:52 +0000588 lines =<< trim END
589 vim9script
590 import './Ximport/.vim' as vim
591 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000592 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000593
594 writefile(['vim9script'], 'Ximport/.vimrc')
595 lines =<< trim END
596 vim9script
597 import './Ximport/.vimrc'
598 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000599 v9.CheckScriptFailure(lines, 'E1257: Imported script must use "as" or end in .vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000600 lines =<< trim END
601 vim9script
602 import './Ximport/.vimrc' as vimrc
603 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000604 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000605
Yegappan Lakshmanan885de442022-04-23 10:51:14 +0100606 new
607 setline(1, ['vim9script', 'import "" as abc'])
608 assert_fails('source', 'E1071: Invalid string for :import: "" as abc')
609 setline(2, 'import [] as abc')
610 assert_fails('source', 'E1071: Invalid string for :import: [] as abc')
611 setline(2, 'import test_null_string() as abc')
612 assert_fails('source', 'E1071: Invalid string for :import: test_null_string() as abc')
613 bw!
614 call writefile(['vim9script', "import './Xfoo.vim' ask expo"], 'Xbar.vim')
615 assert_fails('source Xbar.vim', 'E488: Trailing characters: ask expo')
616 writefile([], 'Xtemp')
617 call writefile(['vim9script', "import './Xtemp'"], 'Xbar.vim')
618 assert_fails('source Xbar.vim', 'E1257: Imported script must use "as" or end in .vim: Xtemp')
619 delete('Xtemp')
620 call writefile(['vim9script', "import './Xfoo.vim' as abc | foobar"], 'Xbar.vim')
621 assert_fails('source Xbar.vim', 'E492: Not an editor command: foobar')
622 call delete('Xbar.vim')
623
Bram Moolenaard8448622022-01-07 21:39:52 +0000624 delete('Ximport', 'rf')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +0100625 delete('Xfoo.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000626enddef
627
628func g:Trigger()
629 source Ximport.vim
630 return "echo 'yes'\<CR>"
631endfunc
632
633def Test_import_export_expr_map()
634 # check that :import and :export work when buffer is locked
635 var export_lines =<< trim END
636 vim9script
637 export def That(): string
638 return 'yes'
639 enddef
640 END
641 writefile(export_lines, 'Xexport_that.vim')
642
643 var import_lines =<< trim END
644 vim9script
645 import './Xexport_that.vim' as that
646 assert_equal('yes', that.That())
647 END
648 writefile(import_lines, 'Ximport.vim')
649
650 nnoremap <expr> trigger g:Trigger()
651 feedkeys('trigger', "xt")
652
653 delete('Xexport_that.vim')
654 delete('Ximport.vim')
655 nunmap trigger
656enddef
657
658def Test_import_in_filetype()
659 # check that :import works when the buffer is locked
660 mkdir('ftplugin', 'p')
661 var export_lines =<< trim END
662 vim9script
663 export var That = 'yes'
664 END
665 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
666
667 var import_lines =<< trim END
668 vim9script
669 import './Xexport_ft.vim' as ft
670 assert_equal('yes', ft.That)
671 g:did_load_mytpe = 1
672 END
673 writefile(import_lines, 'ftplugin/qf.vim')
674
675 var save_rtp = &rtp
676 &rtp = getcwd() .. ',' .. &rtp
677
678 filetype plugin on
679 copen
680 assert_equal(1, g:did_load_mytpe)
681
682 quit!
683 delete('Xexport_ft.vim')
684 delete('ftplugin', 'rf')
685 &rtp = save_rtp
686enddef
687
688def Test_use_import_in_mapping()
689 var lines =<< trim END
690 vim9script
Bram Moolenaar89445512022-04-14 12:58:23 +0100691 export def Funcx(nr: number)
692 g:result = nr
Bram Moolenaard8448622022-01-07 21:39:52 +0000693 enddef
694 END
695 writefile(lines, 'XsomeExport.vim')
696 lines =<< trim END
697 vim9script
698 import './XsomeExport.vim' as some
699 var Funcy = some.Funcx
Bram Moolenaar89445512022-04-14 12:58:23 +0100700 nnoremap <F3> :call <sid>Funcy(42)<cr>
701 nnoremap <F4> :call <sid>some.Funcx(44)<cr>
Bram Moolenaard8448622022-01-07 21:39:52 +0000702 END
703 writefile(lines, 'Xmapscript.vim')
704
705 source Xmapscript.vim
706 feedkeys("\<F3>", "xt")
707 assert_equal(42, g:result)
Bram Moolenaar89445512022-04-14 12:58:23 +0100708 feedkeys("\<F4>", "xt")
709 assert_equal(44, g:result)
Bram Moolenaard8448622022-01-07 21:39:52 +0000710
711 unlet g:result
712 delete('XsomeExport.vim')
713 delete('Xmapscript.vim')
714 nunmap <F3>
Bram Moolenaar89445512022-04-14 12:58:23 +0100715 nunmap <F4>
716enddef
717
Bram Moolenaar648dd882022-04-14 21:36:15 +0100718def Test_use_relative_autoload_import_in_mapping()
Bram Moolenaar89445512022-04-14 12:58:23 +0100719 var lines =<< trim END
720 vim9script
721 export def Func()
722 g:result = 42
723 enddef
724 END
Bram Moolenaar648dd882022-04-14 21:36:15 +0100725 writefile(lines, 'XrelautoloadExport.vim')
Bram Moolenaar89445512022-04-14 12:58:23 +0100726 lines =<< trim END
727 vim9script
Bram Moolenaar648dd882022-04-14 21:36:15 +0100728 import autoload './XrelautoloadExport.vim' as some
Bram Moolenaar89445512022-04-14 12:58:23 +0100729 nnoremap <F3> :call <SID>some.Func()<CR>
730 END
731 writefile(lines, 'Xmapscript.vim')
732
733 source Xmapscript.vim
Bram Moolenaar648dd882022-04-14 21:36:15 +0100734 assert_match('\d\+ A: .*XrelautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +0100735 assert_match('XrelautoloadExport.vim$', getscriptinfo()[-1].name)
736 assert_true(getscriptinfo()[-1].autoload)
Bram Moolenaar89445512022-04-14 12:58:23 +0100737 feedkeys("\<F3>", "xt")
738 assert_equal(42, g:result)
739
740 unlet g:result
Bram Moolenaar648dd882022-04-14 21:36:15 +0100741 delete('XrelautoloadExport.vim')
Bram Moolenaar89445512022-04-14 12:58:23 +0100742 delete('Xmapscript.vim')
743 nunmap <F3>
Bram Moolenaard8448622022-01-07 21:39:52 +0000744enddef
745
Bram Moolenaar5cb53b72022-05-26 19:54:05 +0100746def Test_autoload_import_var()
747 # variable name starts with "autoload"
748 var lines =<< trim END
749 vim9script
750 var autoloaded = "Xtest.vim"
751 import autoloaded
752 END
753 v9.CheckScriptFailure(lines, 'E1053: Could not import "Xtest.vim')
754enddef
755
Bram Moolenaar648dd882022-04-14 21:36:15 +0100756def Test_use_autoload_import_in_mapping()
757 var lines =<< trim END
758 vim9script
759 export def Func()
760 g:result = 49
761 enddef
762 END
763 mkdir('Xdir/autoload', 'p')
764 writefile(lines, 'Xdir/autoload/XautoloadExport.vim')
765 var save_rtp = &rtp
766 exe 'set rtp^=' .. getcwd() .. '/Xdir'
767
768 lines =<< trim END
769 vim9script
770 import autoload 'XautoloadExport.vim' as some
771 nnoremap <F3> :call <SID>some.Func()<CR>
772 END
773 writefile(lines, 'Xmapscript.vim')
774
775 source Xmapscript.vim
776 assert_match('\d\+ A: .*autoload/XautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
777 feedkeys("\<F3>", "xt")
778 assert_equal(49, g:result)
779
780 unlet g:result
781 delete('Xmapscript.vim')
782 nunmap <F3>
783 delete('Xdir', 'rf')
784 &rtp = save_rtp
785enddef
786
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000787def Test_use_import_in_command_completion()
Bram Moolenaar15d16352022-01-17 20:09:08 +0000788 var lines =<< trim END
789 vim9script
790 export def Complete(..._): list<string>
791 return ['abcd']
792 enddef
793 END
794 writefile(lines, 'Xscript.vim')
795
796 lines =<< trim END
797 vim9script
798 import './Xscript.vim'
799
800 command -nargs=1 -complete=customlist,Xscript.Complete Cmd echo 'ok'
801 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
802 assert_equal('#Cmd abcd', @:)
803 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000804 v9.CheckScriptSuccess(lines)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000805
806 delcommand Cmd
807 delete('Xscript.vim')
808enddef
809
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100810def Test_use_import_with_funcref_in_command_completion()
811 var lines =<< trim END
812 vim9script
813 export def Complete(..._): list<string>
814 return ['abcd']
815 enddef
816 END
817 writefile(lines, 'Xscript.vim')
818
819 lines =<< trim END
820 vim9script
821 import './Xscript.vim'
822
823 var Ref = Xscript.Complete
824 exe "command -nargs=1 -complete=customlist," .. expand('<SID>') .. "Ref Cmd echo 'ok'"
825 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
826 assert_equal('#Cmd abcd', @:)
827 END
828 v9.CheckScriptSuccess(lines)
829
830 delcommand Cmd
831 delete('Xscript.vim')
832enddef
833
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000834def Test_use_autoload_import_in_insert_completion()
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 ThesaurusFunc(findbase: bool, _): any
842 if findbase
843 return 1
844 endif
845 return [
846 'check',
847 'experiment',
848 'test',
849 'verification'
850 ]
851 enddef
852 g:completion_loaded = 'yes'
853 END
854 writefile(lines, 'Xdir/autoload/completion.vim')
855
856 new
857 lines =<< trim END
858 vim9script
859 g:completion_loaded = 'no'
860 import autoload 'completion.vim'
861 set thesaurusfunc=completion.ThesaurusFunc
862 assert_equal('no', g:completion_loaded)
863 feedkeys("i\<C-X>\<C-T>\<C-N>\<Esc>", 'xt')
864 assert_equal('experiment', getline(1))
865 assert_equal('yes', g:completion_loaded)
866 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000867 v9.CheckScriptSuccess(lines)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000868
869 set thesaurusfunc=
870 bwipe!
871 delete('Xdir', 'rf')
872 &rtp = save_rtp
873enddef
874
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000875def Test_use_autoload_import_partial_in_opfunc()
876 mkdir('Xdir/autoload', 'p')
877 var save_rtp = &rtp
878 exe 'set rtp^=' .. getcwd() .. '/Xdir'
879
880 var lines =<< trim END
881 vim9script
882 export def Opfunc(..._)
883 g:opfunc_called = 'yes'
884 enddef
885 END
886 writefile(lines, 'Xdir/autoload/opfunc.vim')
887
888 new
889 lines =<< trim END
890 vim9script
891 import autoload 'opfunc.vim'
892 nnoremap <expr> <F3> TheFunc()
893 def TheFunc(): string
894 &operatorfunc = function('opfunc.Opfunc', [0])
895 return 'g@'
896 enddef
897 feedkeys("\<F3>l", 'xt')
898 assert_equal('yes', g:opfunc_called)
899 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000900 v9.CheckScriptSuccess(lines)
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000901
902 set opfunc=
903 bwipe!
904 delete('Xdir', 'rf')
Bram Moolenaar06b77222022-01-25 15:51:56 +0000905 nunmap <F3>
906 &rtp = save_rtp
907enddef
908
909def Test_set_opfunc_to_autoload_func_directly()
910 mkdir('Xdir/autoload', 'p')
911 var save_rtp = &rtp
912 exe 'set rtp^=' .. getcwd() .. '/Xdir'
913
914 var lines =<< trim END
915 vim9script
916 export def Opfunc(..._)
917 g:opfunc_called = 'yes'
918 enddef
919 END
920 writefile(lines, 'Xdir/autoload/opfunc.vim')
921
922 new
923 lines =<< trim END
924 vim9script
925 import autoload 'opfunc.vim'
926 nnoremap <expr> <F3> TheFunc()
927 def TheFunc(): string
928 &operatorfunc = opfunc.Opfunc
929 return 'g@'
930 enddef
931 feedkeys("\<F3>l", 'xt')
932 assert_equal('yes', g:opfunc_called)
933 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000934 v9.CheckScriptSuccess(lines)
Bram Moolenaar06b77222022-01-25 15:51:56 +0000935
936 set opfunc=
937 bwipe!
938 delete('Xdir', 'rf')
939 nunmap <F3>
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000940 &rtp = save_rtp
941enddef
942
Bram Moolenaare70dd112022-01-21 16:31:11 +0000943def Test_use_autoload_import_in_fold_expression()
944 mkdir('Xdir/autoload', 'p')
945 var save_rtp = &rtp
946 exe 'set rtp^=' .. getcwd() .. '/Xdir'
947
948 var lines =<< trim END
949 vim9script
950 export def Expr(): string
951 return getline(v:lnum) =~ '^#' ? '>1' : '1'
952 enddef
Bram Moolenaar9530b582022-01-22 13:39:08 +0000953 export def Text(): string
954 return 'fold text'
955 enddef
Bram Moolenaare70dd112022-01-21 16:31:11 +0000956 g:fold_loaded = 'yes'
957 END
958 writefile(lines, 'Xdir/autoload/fold.vim')
959
960 lines =<< trim END
961 vim9script
962 import autoload 'fold.vim'
963 &foldexpr = 'fold.Expr()'
Bram Moolenaar9530b582022-01-22 13:39:08 +0000964 &foldtext = 'fold.Text()'
Bram Moolenaare70dd112022-01-21 16:31:11 +0000965 &foldmethod = 'expr'
966 &debug = 'throw'
967 END
968 new
969 setline(1, ['# one', 'text', '# two', 'text'])
970 g:fold_loaded = 'no'
Bram Moolenaar62aec932022-01-29 21:45:34 +0000971 v9.CheckScriptSuccess(lines)
Bram Moolenaare70dd112022-01-21 16:31:11 +0000972 assert_equal('no', g:fold_loaded)
973 redraw
974 assert_equal('yes', g:fold_loaded)
975
976 # Check that script context of 'foldexpr' is copied to another buffer.
977 edit! otherfile
978 redraw
979
Bram Moolenaar9530b582022-01-22 13:39:08 +0000980 set foldexpr= foldtext& foldmethod& debug=
Bram Moolenaare70dd112022-01-21 16:31:11 +0000981 bwipe!
982 delete('Xdir', 'rf')
983 &rtp = save_rtp
984enddef
985
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100986def Test_autoload_import_relative()
987 var lines =<< trim END
988 vim9script
989
990 g:loaded = 'yes'
991 export def RelFunc(): string
992 return 'relfunc'
993 enddef
994 def NotExported()
995 echo 'not'
996 enddef
997
998 export var someText = 'some text'
999 var notexp = 'bad'
1000 END
1001 writefile(lines, 'XimportRel.vim')
1002 writefile(lines, 'XimportRel2.vim')
1003 writefile(lines, 'XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +01001004 writefile(lines, 'XimportRel4.vim')
1005 writefile(lines, 'XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001006
1007 lines =<< trim END
1008 vim9script
1009 g:loaded = 'no'
1010 import autoload './XimportRel.vim'
1011 assert_equal('no', g:loaded)
1012
1013 def AFunc(): string
1014 var res = ''
1015 res ..= XimportRel.RelFunc()
1016 res ..= '/'
1017 res ..= XimportRel.someText
1018 XimportRel.someText = 'from AFunc'
1019 return res
1020 enddef
1021 # script not loaded when compiling
1022 defcompile
1023 assert_equal('no', g:loaded)
1024
1025 assert_equal('relfunc/some text', AFunc())
1026 assert_equal('yes', g:loaded)
1027 unlet g:loaded
1028
1029 assert_equal('from AFunc', XimportRel.someText)
1030 XimportRel.someText = 'from script'
1031 assert_equal('from script', XimportRel.someText)
1032 END
1033 v9.CheckScriptSuccess(lines)
1034
1035 lines =<< trim END
1036 vim9script
1037 import autoload './XimportRel.vim'
1038 echo XimportRel.NotExported()
1039 END
1040 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 3)
1041
1042 lines =<< trim END
1043 vim9script
1044 import autoload './XimportRel.vim'
1045 echo XimportRel.notexp
1046 END
1047 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
1048
1049 lines =<< trim END
1050 vim9script
1051 import autoload './XimportRel.vim'
1052 XimportRel.notexp = 'bad'
1053 END
1054 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
1055
1056 lines =<< trim END
1057 vim9script
1058 import autoload './XimportRel.vim'
1059 def Func()
1060 echo XimportRel.NotExported()
1061 enddef
1062 Func()
1063 END
1064 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 1)
1065
1066 lines =<< trim END
1067 vim9script
1068 import autoload './XimportRel.vim'
1069 def Func()
1070 echo XimportRel.notexp
1071 enddef
1072 Func()
1073 END
1074 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1075
Bram Moolenaar10611952022-04-03 21:11:34 +01001076 # Same, script not imported before
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001077 lines =<< trim END
1078 vim9script
Bram Moolenaar10611952022-04-03 21:11:34 +01001079 import autoload './XimportRel4.vim'
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001080 def Func()
Bram Moolenaar10611952022-04-03 21:11:34 +01001081 echo XimportRel4.notexp
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001082 enddef
1083 Func()
1084 END
1085 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1086
Bram Moolenaar10611952022-04-03 21:11:34 +01001087 # does not fail if the script wasn't loaded yet and only compiling
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001088 g:loaded = 'no'
1089 lines =<< trim END
1090 vim9script
1091 import autoload './XimportRel2.vim'
1092 def Func()
1093 echo XimportRel2.notexp
1094 enddef
1095 defcompile
1096 END
1097 v9.CheckScriptSuccess(lines)
1098 assert_equal('no', g:loaded)
1099
Bram Moolenaar10611952022-04-03 21:11:34 +01001100 lines =<< trim END
1101 vim9script
1102 import autoload './XimportRel.vim'
1103 def Func()
1104 XimportRel.notexp = 'bad'
1105 enddef
1106 Func()
1107 END
1108 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1109
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001110 # fails with a not loaded import
1111 lines =<< trim END
1112 vim9script
1113 import autoload './XimportRel3.vim'
1114 def Func()
1115 XimportRel3.notexp = 'bad'
1116 enddef
1117 Func()
1118 END
1119 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1120 assert_equal('yes', g:loaded)
1121 unlet g:loaded
1122
Bram Moolenaar10611952022-04-03 21:11:34 +01001123 lines =<< trim END
1124 vim9script
1125 import autoload './XimportRel5.vim'
1126 def Func()
1127 XimportRel5.nosuchvar = 'bad'
1128 enddef
1129 Func()
1130 END
1131 v9.CheckScriptFailure(lines, 'E121: Undefined variable: nosuchvar', 1)
1132 unlet g:loaded
1133
1134 # nasty: delete script after compiling function
1135 writefile(['vim9script'], 'XimportRelDel.vim')
1136 lines =<< trim END
1137 vim9script
1138
1139 import autoload './XimportRelDel.vim'
1140 def DoIt()
1141 echo XimportRelDel.var
1142 enddef
1143 defcompile
1144 delete('XimportRelDel.vim')
1145 DoIt()
1146 END
Bram Moolenaar242c1522022-04-03 21:52:51 +01001147 v9.CheckScriptFailure(lines, 'E484:')
Bram Moolenaar10611952022-04-03 21:11:34 +01001148
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001149 delete('XimportRel.vim')
1150 delete('XimportRel2.vim')
1151 delete('XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +01001152 delete('XimportRel4.vim')
1153 delete('XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001154enddef
1155
Bram Moolenaarccbfd482022-03-31 16:18:23 +01001156def Test_autoload_import_relative_autoload_dir()
1157 mkdir('autoload', 'p')
1158 var lines =<< trim END
1159 vim9script
1160 export def Bar()
1161 g:called_bar = 'yes'
1162 enddef
1163 END
1164 writefile(lines, 'autoload/script.vim')
1165
1166 lines =<< trim END
1167 vim9script
1168 import autoload './autoload/script.vim'
1169 def Foo()
1170 script.Bar()
1171 enddef
1172 Foo()
1173 assert_equal('yes', g:called_bar)
1174 END
1175 v9.CheckScriptSuccess(lines)
1176
1177 unlet g:called_bar
1178 delete('autoload', 'rf')
1179enddef
1180
Bram Moolenaaraac12da2022-04-24 21:33:20 +01001181def Test_autoload_import_deleted()
1182 var lines =<< trim END
1183 vim9script
1184 export const FOO = 1
1185 END
1186 writefile(lines, 'Xa.vim')
1187
1188 lines =<< trim END
1189 vim9script
1190 import autoload './Xa.vim'
1191
1192 delete('Xa.vim')
1193 var x = Xa.FOO
1194 END
1195 v9.CheckScriptFailure(lines, 'E484:')
1196
1197 delete('Xdir', 'rf')
1198enddef
1199
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001200func Test_import_in_diffexpr()
1201 CheckExecutable diff
1202
1203 call Run_Test_import_in_diffexpr()
1204endfunc
1205
1206def Run_Test_import_in_diffexpr()
1207 var lines =<< trim END
1208 vim9script
1209
1210 export def DiffExpr()
1211 # Prepend some text to check diff type detection
1212 writefile(['warning', ' message'], v:fname_out)
1213 silent exe '!diff ' .. v:fname_in .. ' '
1214 .. v:fname_new .. '>>' .. v:fname_out
1215 enddef
1216 END
1217 writefile(lines, 'Xdiffexpr')
1218
1219 lines =<< trim END
1220 vim9script
1221 import './Xdiffexpr' as diff
1222
1223 set diffexpr=diff.DiffExpr()
1224 set diffopt=foldcolumn:0
1225 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001226 v9.CheckScriptSuccess(lines)
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001227
1228 enew!
1229 call setline(1, ['one', 'two', 'three'])
1230 diffthis
1231
1232 botright vert new
1233 call setline(1, ['one', 'two', 'three.'])
1234 diffthis
1235 # we only check if this does not cause errors
1236 redraw
1237
1238 diffoff!
Bram Moolenaar50761872022-06-26 18:01:00 +01001239 set diffexpr=
1240 set diffopt&
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001241 bwipe!
1242 bwipe!
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001243 delete('Xdiffexpr')
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001244enddef
1245
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001246def Test_import_in_patchexpr()
1247 var lines =<< trim END
1248 vim9script
1249 export def TPatch()
1250 call writefile(['output file'], v:fname_out)
1251 enddef
1252 END
1253 writefile(lines, 'Xpatchexpr')
1254
1255 lines =<< trim END
1256 vim9script
1257 import './Xpatchexpr' as patch
1258 set patchexpr=patch.TPatch()
1259 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001260 v9.CheckScriptSuccess(lines)
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001261
1262 call writefile(['input file'], 'Xinput')
1263 call writefile(['diff file'], 'Xdiff')
1264 :%bwipe!
1265 edit Xinput
1266 diffpatch Xdiff
1267 call assert_equal('output file', getline(1))
1268
1269 call delete('Xinput')
1270 call delete('Xdiff')
1271 call delete('Xpatchexpr')
1272 set patchexpr&
1273 :%bwipe!
1274enddef
1275
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001276def Test_import_in_formatexpr()
1277 var lines =<< trim END
1278 vim9script
1279 export def MyFormatExpr(): number
1280 g:did_format = 'yes'
1281 return 0
1282 enddef
1283 END
1284 writefile(lines, 'Xformatter')
1285
1286 lines =<< trim END
1287 vim9script
1288 import './Xformatter' as format
1289 set formatexpr=format.MyFormatExpr()
1290 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001291 v9.CheckScriptSuccess(lines)
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001292
1293 new
1294 setline(1, ['a', 'b', 'c'])
1295 normal gqG
1296 assert_equal('yes', g:did_format)
1297
1298 bwipe!
1299 delete('Xformatter')
1300 unlet g:did_format
1301 set formatexpr=
1302enddef
1303
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001304def Test_import_in_includeexpr()
1305 writefile(['found it'], 'Xthisfile')
1306 new
1307
1308 var lines =<< trim END
1309 vim9script
1310 export def DoSub(): string
1311 return substitute(v:fname, 'that', 'this', '')
1312 enddef
1313 END
1314 writefile(lines, 'Xinclude.vim')
1315
1316 lines =<< trim END
1317 vim9script
1318 import './Xinclude.vim'
1319 set includeexpr=Xinclude.DoSub()
1320 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001321 v9.CheckScriptSuccess(lines)
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001322
1323 setline(1, ['Xthatfile'])
1324 exe "normal \<C-W>f"
1325 assert_equal('Xthisfile', expand('%'))
1326
1327 bwipe!
1328 bwipe!
1329 set includeexpr=
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001330 delete('Xinclude.vim')
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001331 delete('Xthisfile')
1332enddef
1333
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001334def Test_import_in_indentexpr()
1335 var lines =<< trim END
1336 vim9script
1337 export def GetIndent(): number
1338 return 5
1339 enddef
1340 END
1341 writefile(lines, 'Xindenter')
1342
1343 lines =<< trim END
1344 vim9script
1345 import './Xindenter' as indent
1346 set indentexpr=indent.GetIndent()
1347 set debug=throw
1348 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001349 v9.CheckScriptSuccess(lines)
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001350
1351 new
1352 setline(1, 'hello')
1353 normal ==
1354 assert_equal(' hello', getline(1))
1355
1356 bwipe!
1357 set indentexpr= debug=
1358 delete('Xindenter')
1359enddef
1360
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001361func Test_import_in_printexpr()
1362 CheckFeature postscript
1363 call Run_Test_import_in_printexpr()
1364endfunc
1365
1366def Run_Test_import_in_printexpr()
1367 var lines =<< trim END
1368 vim9script
1369 export def PrintFile(): bool
1370 g:printed = 'yes'
1371 delete('v:fname_in')
1372 return false
1373 enddef
1374 END
1375 writefile(lines, 'Xprint.vim')
1376
1377 lines =<< trim END
1378 vim9script
1379 import './Xprint.vim'
1380 set printexpr=Xprint.PrintFile()
1381 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001382 v9.CheckScriptSuccess(lines)
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001383
1384 help
1385 hardcopy dummy args
1386 assert_equal('yes', g:printed)
1387
1388 delete('Xprint.vim')
1389 set printexpr=
1390enddef
1391
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001392def Test_import_in_charconvert()
1393 var lines =<< trim END
1394 vim9script
1395 export def MakeUpper(): bool
1396 var data = readfile(v:fname_in)
1397 map(data, 'toupper(v:val)')
1398 writefile(data, v:fname_out)
1399 return false # success
1400 enddef
1401 END
1402 writefile(lines, 'Xconvert.vim')
1403
1404 lines =<< trim END
1405 vim9script
1406 import './Xconvert.vim' as conv
1407 set charconvert=conv.MakeUpper()
1408 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001409 v9.CheckScriptSuccess(lines)
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001410
1411 writefile(['one', 'two'], 'Xfile')
1412 new Xfile
1413 write ++enc=ucase Xfile1
1414 assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1415
1416 delete('Xfile')
1417 delete('Xfile1')
1418 delete('Xconvert.vim')
1419 bwipe!
1420 set charconvert&
1421enddef
1422
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001423func Test_import_in_spellsuggest_expr()
1424 CheckFeature spell
1425 call Run_Test_import_in_spellsuggest_expr()
1426endfunc
1427
1428def Run_Test_import_in_spellsuggest_expr()
1429 var lines =<< trim END
1430 vim9script
1431 export def MySuggest(): list<any>
1432 return [['Fox', 8], ['Fop', 9]]
1433 enddef
1434 END
1435 writefile(lines, 'Xsuggest.vim')
1436
1437 lines =<< trim END
1438 vim9script
1439 import './Xsuggest.vim' as sugg
1440 set spell spellsuggest=expr:sugg.MySuggest()
1441 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001442 v9.CheckScriptSuccess(lines)
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001443
1444 set verbose=1 # report errors
1445 call assert_equal(['Fox', 'Fop'], spellsuggest('Fo', 2))
1446
1447 delete('Xsuggest.vim')
1448 set nospell spellsuggest& verbose=0
1449enddef
1450
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001451def Test_export_shadows_global_function()
1452 mkdir('Xdir/autoload', 'p')
1453 var save_rtp = &rtp
1454 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1455
1456 var lines =<< trim END
1457 vim9script
1458 export def Shadow(): string
1459 return 'Shadow()'
1460 enddef
1461 END
1462 writefile(lines, 'Xdir/autoload/shadow.vim')
1463
1464 lines =<< trim END
1465 vim9script
1466
1467 def g:Shadow(): string
1468 return 'global'
1469 enddef
1470
1471 import autoload 'shadow.vim'
1472 assert_equal('Shadow()', shadow.Shadow())
1473 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001474 v9.CheckScriptSuccess(lines)
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001475
1476 delfunc g:Shadow
1477 bwipe!
1478 delete('Xdir', 'rf')
1479 &rtp = save_rtp
1480enddef
1481
Bram Moolenaard8448622022-01-07 21:39:52 +00001482def Test_export_fails()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001483 v9.CheckScriptFailure(['export var some = 123'], 'E1042:')
1484 v9.CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
1485 v9.CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +01001486 v9.CheckScriptFailure(['vim9script', 'export function /a1b2c3'], 'E1044:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001487
1488 assert_fails('export something', 'E1043:')
1489enddef
1490
1491func Test_import_fails_without_script()
1492 CheckRunVimInTerminal
1493
1494 " call indirectly to avoid compilation error for missing functions
1495 call Run_Test_import_fails_on_command_line()
1496endfunc
1497
1498def Run_Test_import_fails_on_command_line()
1499 var export =<< trim END
1500 vim9script
1501 export def Foo(): number
1502 return 0
1503 enddef
1504 END
1505 writefile(export, 'XexportCmd.vim')
1506
Bram Moolenaar62aec932022-01-29 21:45:34 +00001507 var buf = g:RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaard8448622022-01-07 21:39:52 +00001508 rows: 6, wait_for_ruler: 0})
Bram Moolenaar62aec932022-01-29 21:45:34 +00001509 g:WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaard8448622022-01-07 21:39:52 +00001510
1511 delete('XexportCmd.vim')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001512 g:StopVimInTerminal(buf)
Bram Moolenaard8448622022-01-07 21:39:52 +00001513enddef
1514
1515def Test_vim9_reload_noclear()
1516 var lines =<< trim END
1517 vim9script
1518 export var exported = 'thexport'
1519
1520 export def TheFunc(x = 0)
1521 enddef
1522 END
1523 writefile(lines, 'XExportReload')
1524 lines =<< trim END
1525 vim9script noclear
1526 g:loadCount += 1
Bram Moolenaara749a422022-02-12 19:52:25 +00001527 var reloaded = 'init'
Bram Moolenaard8448622022-01-07 21:39:52 +00001528 import './XExportReload' as exp
1529
1530 def Again(): string
1531 return 'again'
1532 enddef
1533
1534 exp.TheFunc()
1535
Bram Moolenaara749a422022-02-12 19:52:25 +00001536 if exists('loaded') | finish | endif
1537 var loaded = true
Bram Moolenaard8448622022-01-07 21:39:52 +00001538
Bram Moolenaara749a422022-02-12 19:52:25 +00001539 var notReloaded = 'yes'
1540 reloaded = 'first'
Bram Moolenaard8448622022-01-07 21:39:52 +00001541 def g:Values(): list<string>
Bram Moolenaara749a422022-02-12 19:52:25 +00001542 return [reloaded, notReloaded, Again(), Once(), exp.exported]
Bram Moolenaard8448622022-01-07 21:39:52 +00001543 enddef
1544
1545 def Once(): string
1546 return 'once'
1547 enddef
1548 END
1549 writefile(lines, 'XReloaded')
1550 g:loadCount = 0
1551 source XReloaded
1552 assert_equal(1, g:loadCount)
1553 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
1554 source XReloaded
1555 assert_equal(2, g:loadCount)
1556 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1557 source XReloaded
1558 assert_equal(3, g:loadCount)
1559 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1560
1561 delete('XReloaded')
1562 delete('XExportReload')
1563 delfunc g:Values
1564 unlet g:loadCount
1565
1566 lines =<< trim END
1567 vim9script
1568 def Inner()
1569 enddef
1570 END
1571 lines->writefile('XreloadScript.vim')
1572 source XreloadScript.vim
1573
1574 lines =<< trim END
1575 vim9script
1576 def Outer()
1577 def Inner()
1578 enddef
1579 enddef
1580 defcompile
1581 END
1582 lines->writefile('XreloadScript.vim')
1583 source XreloadScript.vim
1584
1585 delete('XreloadScript.vim')
1586enddef
1587
Bram Moolenaarcd1cda22022-02-16 21:48:25 +00001588def Test_vim_reload_noclear_arg_count()
1589 var lines =<< trim END
1590 vim9script noclear
1591
1592 if !exists('g:didload')
1593 def Test(a: string, b: string)
1594 echo a b
1595 enddef
1596 def Call()
1597 Test('a', 'b')
1598 enddef
1599 else
1600 # redefine with one argument less
1601 def Test(a: string)
1602 echo a
1603 enddef
1604 endif
1605 Call()
1606 g:didload = 1
1607 END
1608 lines->writefile('XreloadScript_1.vim')
1609 source XreloadScript_1.vim
1610 assert_fails('source XreloadScript_1.vim', 'E1106: One argument too many')
1611 unlet g:didload
1612
1613 lines =<< trim END
1614 vim9script noclear
1615
1616 if !exists('g:didload')
1617 def Test(a: string, b: string, c: string)
1618 echo a b
1619 enddef
1620 def Call()
1621 Test('a', 'b', 'c')
1622 enddef
1623 else
1624 # redefine with one argument less
1625 def Test(a: string)
1626 echo a
1627 enddef
1628 endif
1629 Call()
1630 g:didload = 1
1631 END
1632 lines->writefile('XreloadScript_2.vim')
1633 source XreloadScript_2.vim
1634 assert_fails('source XreloadScript_2.vim', 'E1106: 2 arguments too many')
1635 unlet g:didload
1636
1637 lines =<< trim END
1638 vim9script noclear
1639
1640 if !exists('g:didload')
1641 def Test(a: string)
1642 echo a
1643 enddef
1644 def Call()
1645 Test('a')
1646 enddef
1647 else
1648 # redefine with one argument extra
1649 def Test(a: string, b: string)
1650 echo a b
1651 enddef
1652 endif
1653 Call()
1654 g:didload = 1
1655 END
1656 lines->writefile('XreloadScript_3.vim')
1657 source XreloadScript_3.vim
1658 assert_fails('source XreloadScript_3.vim', 'E1190: One argument too few')
1659 unlet g:didload
1660
1661 lines =<< trim END
1662 vim9script noclear
1663
1664 if !exists('g:didload')
1665 def Test(a: string)
1666 echo a
1667 enddef
1668 def Call()
1669 Test('a')
1670 enddef
1671 else
1672 # redefine with two arguments extra
1673 def Test(a: string, b: string, c: string)
1674 echo a b
1675 enddef
1676 endif
1677 Call()
1678 g:didload = 1
1679 END
1680 lines->writefile('XreloadScript_4.vim')
1681 source XreloadScript_4.vim
1682 assert_fails('source XreloadScript_4.vim', 'E1190: 2 arguments too few')
1683 unlet g:didload
1684
1685 delete('XreloadScript_1.vim')
1686 delete('XreloadScript_2.vim')
1687 delete('XreloadScript_3.vim')
1688 delete('XreloadScript_4.vim')
1689enddef
1690
1691def Test_vim9_reload_noclear_error()
1692 var lines =<< trim END
1693 vim9script noclear
1694
1695 if !exists('g:didload')
1696 def Test(a: string)
1697 echo a
1698 enddef
1699 def Call()
1700 Test('a')
1701 enddef
1702 else
1703 # redefine with a compile error
1704 def Test(a: string)
1705 echo ax
1706 enddef
1707 endif
1708 Call()
1709 g:didload = 1
1710 END
1711 lines->writefile('XreloadScriptErr.vim')
1712 source XreloadScriptErr.vim
1713 assert_fails('source XreloadScriptErr.vim', 'E1001: Variable not found: ax')
1714
1715 unlet g:didload
1716 delete('XreloadScriptErr.vim')
1717enddef
1718
Bram Moolenaard8448622022-01-07 21:39:52 +00001719def Test_vim9_reload_import()
1720 var lines =<< trim END
1721 vim9script
1722 const var = ''
1723 var valone = 1234
1724 def MyFunc(arg: string)
1725 valone = 5678
1726 enddef
1727 END
1728 var morelines =<< trim END
1729 var valtwo = 222
1730 export def GetValtwo(): number
1731 return valtwo
1732 enddef
1733 END
1734 writefile(lines + morelines, 'Xreload.vim')
1735 source Xreload.vim
1736 source Xreload.vim
1737 source Xreload.vim
1738
1739 # cannot declare a var twice
1740 lines =<< trim END
1741 vim9script
1742 var valone = 1234
1743 var valone = 5678
1744 END
1745 writefile(lines, 'Xreload.vim')
1746 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
1747
1748 delete('Xreload.vim')
1749 delete('Ximport.vim')
1750enddef
1751
1752" if a script is reloaded with a script-local variable that changed its type, a
1753" compiled function using that variable must fail.
1754def Test_script_reload_change_type()
1755 var lines =<< trim END
1756 vim9script noclear
1757 var str = 'string'
1758 def g:GetStr(): string
1759 return str .. 'xxx'
1760 enddef
1761 END
1762 writefile(lines, 'Xreload.vim')
1763 source Xreload.vim
1764 echo g:GetStr()
1765
1766 lines =<< trim END
1767 vim9script noclear
1768 var str = 1234
1769 END
1770 writefile(lines, 'Xreload.vim')
1771 source Xreload.vim
1772 assert_fails('echo g:GetStr()', 'E1150:')
1773
1774 delfunc g:GetStr
1775 delete('Xreload.vim')
1776enddef
1777
1778" Define CallFunc so that the test can be compiled
1779command CallFunc echo 'nop'
1780
1781def Test_script_reload_from_function()
1782 var lines =<< trim END
1783 vim9script
1784
Bram Moolenaar10611952022-04-03 21:11:34 +01001785 if exists('g:loadedThis')
Bram Moolenaard8448622022-01-07 21:39:52 +00001786 finish
1787 endif
Bram Moolenaar10611952022-04-03 21:11:34 +01001788 g:loadedThis = 1
Bram Moolenaard8448622022-01-07 21:39:52 +00001789 delcommand CallFunc
1790 command CallFunc Func()
1791 def Func()
1792 so XreloadFunc.vim
1793 g:didTheFunc = 1
1794 enddef
1795 END
1796 writefile(lines, 'XreloadFunc.vim')
1797 source XreloadFunc.vim
1798 CallFunc
1799 assert_equal(1, g:didTheFunc)
1800
1801 delete('XreloadFunc.vim')
1802 delcommand CallFunc
Bram Moolenaar10611952022-04-03 21:11:34 +01001803 unlet g:loadedThis
Bram Moolenaard8448622022-01-07 21:39:52 +00001804 unlet g:didTheFunc
1805enddef
1806
1807def s:RetSome(): string
1808 return 'some'
1809enddef
1810
1811" Not exported function that is referenced needs to be accessed by the
1812" script-local name.
1813def Test_vim9_funcref()
1814 var sortlines =<< trim END
1815 vim9script
1816 def Compare(i1: number, i2: number): number
1817 return i2 - i1
1818 enddef
1819
1820 export def FastSort(): list<number>
1821 return range(5)->sort(Compare)
1822 enddef
1823
1824 export def GetString(arg: string): string
1825 return arg
1826 enddef
1827 END
1828 writefile(sortlines, 'Xsort.vim')
1829
1830 var lines =<< trim END
1831 vim9script
1832 import './Xsort.vim'
1833 def Test()
1834 g:result = Xsort.FastSort()
1835 enddef
1836 Test()
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001837 END
1838 writefile(lines, 'Xscript.vim')
1839 source Xscript.vim
1840 assert_equal([4, 3, 2, 1, 0], g:result)
1841 unlet g:result
Bram Moolenaard8448622022-01-07 21:39:52 +00001842
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001843 lines =<< trim END
1844 vim9script
Bram Moolenaard8448622022-01-07 21:39:52 +00001845 # using a function imported with "as"
1846 import './Xsort.vim' as anAlias
1847 assert_equal('yes', anAlias.GetString('yes'))
1848
1849 # using the function from a compiled function
1850 def TestMore(): string
1851 var s = s:anAlias.GetString('foo')
1852 return s .. anAlias.GetString('bar')
1853 enddef
1854 assert_equal('foobar', TestMore())
1855
1856 # error when using a function that isn't exported
1857 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
1858 END
1859 writefile(lines, 'Xscript.vim')
1860
Bram Moolenaard8448622022-01-07 21:39:52 +00001861 delete('Xsort.vim')
1862 delete('Xscript.vim')
1863
1864 var Funcref = function('s:RetSome')
1865 assert_equal('some', Funcref())
1866enddef
1867
1868" Check that when searching for "FilterFunc" it finds the import in the
1869" script where FastFilter() is called from, both as a string and as a direct
1870" function reference.
1871def Test_vim9_funcref_other_script()
1872 var filterLines =<< trim END
1873 vim9script
1874 export def FilterFunc(idx: number, val: number): bool
1875 return idx % 2 == 1
1876 enddef
1877 export def FastFilter(): list<number>
1878 return range(10)->filter('FilterFunc(v:key, v:val)')
1879 enddef
1880 export def FastFilterDirect(): list<number>
1881 return range(10)->filter(FilterFunc)
1882 enddef
1883 END
1884 writefile(filterLines, 'Xfilter.vim')
1885
1886 var lines =<< trim END
1887 vim9script
1888 import './Xfilter.vim' as filter
1889 def Test()
1890 var x: list<number> = filter.FastFilter()
1891 enddef
1892 Test()
1893 def TestDirect()
1894 var x: list<number> = filter.FastFilterDirect()
1895 enddef
1896 TestDirect()
1897 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001898 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +00001899 delete('Xfilter.vim')
1900enddef
1901
1902def Test_import_absolute()
1903 var import_lines = [
1904 'vim9script',
1905 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
1906 'def UseExported()',
1907 ' g:imported_abs = abs.exported',
1908 ' abs.exported = 8888',
1909 ' g:imported_after = abs.exported',
1910 'enddef',
1911 'UseExported()',
1912 'g:import_disassembled = execute("disass UseExported")',
1913 ]
1914 writefile(import_lines, 'Ximport_abs.vim')
1915 writefile(s:export_script_lines, 'Xexport_abs.vim')
1916
1917 source Ximport_abs.vim
1918
1919 assert_equal(9876, g:imported_abs)
1920 assert_equal(8888, g:imported_after)
1921 assert_match('<SNR>\d\+_UseExported\_s*' ..
1922 'g:imported_abs = abs.exported\_s*' ..
1923 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1924 '1 STOREG g:imported_abs\_s*' ..
1925 'abs.exported = 8888\_s*' ..
1926 '2 PUSHNR 8888\_s*' ..
1927 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1928 'g:imported_after = abs.exported\_s*' ..
1929 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1930 '5 STOREG g:imported_after',
1931 g:import_disassembled)
1932
1933 Undo_export_script_lines()
1934 unlet g:imported_abs
1935 unlet g:import_disassembled
1936
1937 delete('Ximport_abs.vim')
1938 delete('Xexport_abs.vim')
1939enddef
1940
1941def Test_import_rtp()
1942 var import_lines = [
1943 'vim9script',
1944 'import "Xexport_rtp.vim" as rtp',
1945 'g:imported_rtp = rtp.exported',
1946 ]
1947 writefile(import_lines, 'Ximport_rtp.vim')
1948 mkdir('import', 'p')
1949 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1950
1951 var save_rtp = &rtp
1952 &rtp = getcwd()
1953 source Ximport_rtp.vim
1954 &rtp = save_rtp
1955
1956 assert_equal(9876, g:imported_rtp)
1957
1958 Undo_export_script_lines()
1959 unlet g:imported_rtp
1960 delete('Ximport_rtp.vim')
1961 delete('import', 'rf')
1962enddef
1963
1964def Test_import_compile_error()
1965 var export_lines = [
1966 'vim9script',
1967 'export def ExpFunc(): string',
1968 ' return notDefined',
1969 'enddef',
1970 ]
1971 writefile(export_lines, 'Xexported.vim')
1972
1973 var import_lines = [
1974 'vim9script',
1975 'import "./Xexported.vim" as expo',
1976 'def ImpFunc()',
1977 ' echo expo.ExpFunc()',
1978 'enddef',
1979 'defcompile',
1980 ]
1981 writefile(import_lines, 'Ximport.vim')
1982
1983 try
1984 source Ximport.vim
1985 catch /E1001/
1986 # Error should be before the Xexported.vim file.
1987 assert_match('E1001: Variable not found: notDefined', v:exception)
1988 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1989 endtry
1990
1991 delete('Xexported.vim')
1992 delete('Ximport.vim')
1993enddef
1994
1995def Test_func_overrules_import_fails()
1996 var export_lines =<< trim END
1997 vim9script
1998 export def Func()
1999 echo 'imported'
2000 enddef
2001 END
2002 writefile(export_lines, 'XexportedFunc.vim')
2003
2004 var lines =<< trim END
2005 vim9script
2006 import './XexportedFunc.vim' as Func
2007 def Func()
2008 echo 'local to function'
2009 enddef
2010 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002011 v9.CheckScriptFailure(lines, 'E1213: Redefining imported item "Func"')
Bram Moolenaard8448622022-01-07 21:39:52 +00002012
2013 lines =<< trim END
2014 vim9script
2015 import './XexportedFunc.vim' as Func
2016 def Outer()
2017 def Func()
2018 echo 'local to function'
2019 enddef
2020 enddef
2021 defcompile
2022 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002023 v9.CheckScriptFailure(lines, 'E1236:')
Bram Moolenaard8448622022-01-07 21:39:52 +00002024
2025 delete('XexportedFunc.vim')
2026enddef
2027
2028def Test_source_vim9_from_legacy()
2029 var vim9_lines =<< trim END
2030 vim9script
2031 var local = 'local'
2032 g:global = 'global'
2033 export var exported = 'exported'
2034 export def GetText(): string
2035 return 'text'
2036 enddef
2037 END
2038 writefile(vim9_lines, 'Xvim9_script.vim')
2039
2040 var legacy_lines =<< trim END
2041 source Xvim9_script.vim
2042
2043 call assert_false(exists('local'))
2044 call assert_false(exists('exported'))
2045 call assert_false(exists('s:exported'))
2046 call assert_equal('global', global)
2047 call assert_equal('global', g:global)
Bram Moolenaard8448622022-01-07 21:39:52 +00002048 END
2049 writefile(legacy_lines, 'Xlegacy_script.vim')
2050
2051 source Xlegacy_script.vim
2052 assert_equal('global', g:global)
2053 unlet g:global
2054
2055 delete('Xlegacy_script.vim')
2056 delete('Xvim9_script.vim')
2057enddef
2058
Bram Moolenaarc43e6232022-01-13 20:51:56 +00002059def Test_import_vim9_from_legacy()
2060 var vim9_lines =<< trim END
2061 vim9script
2062 var local = 'local'
2063 g:global = 'global'
2064 export var exported = 'exported'
2065 export def GetText(): string
2066 return 'text'
2067 enddef
2068 END
2069 writefile(vim9_lines, 'Xvim9_export.vim')
2070
2071 var legacy_lines =<< trim END
2072 import './Xvim9_export.vim' as vim9
2073
2074 call assert_false(exists('vim9'))
2075 call assert_false(exists('local'))
2076 call assert_false(exists('s:vim9.local'))
2077 call assert_equal('global', global)
2078 call assert_equal('global', g:global)
2079 call assert_false(exists('exported'))
2080 call assert_false(exists('s:exported'))
2081 call assert_false(exists('*GetText'))
2082
2083 " imported symbol is script-local
2084 call assert_equal('exported', s:vim9.exported)
2085 call assert_equal('text', s:vim9.GetText())
2086 END
2087 writefile(legacy_lines, 'Xlegacy_script.vim')
2088
2089 source Xlegacy_script.vim
2090 assert_equal('global', g:global)
2091 unlet g:global
2092
2093 delete('Xlegacy_script.vim')
2094 delete('Xvim9_export.vim')
2095enddef
2096
Bram Moolenaard8448622022-01-07 21:39:52 +00002097def Test_cmdline_win()
2098 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
2099 # the command line window.
2100 mkdir('rtp/syntax', 'p')
2101 var export_lines =<< trim END
2102 vim9script
2103 export var That = 'yes'
2104 END
2105 writefile(export_lines, 'rtp/syntax/Xexport.vim')
2106 var import_lines =<< trim END
2107 vim9script
2108 import './Xexport.vim' as exp
2109 echo exp.That
2110 END
2111 writefile(import_lines, 'rtp/syntax/vim.vim')
2112 var save_rtp = &rtp
2113 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
2114 syntax on
2115 augroup CmdWin
2116 autocmd CmdwinEnter * g:got_there = 'yes'
2117 augroup END
2118 # this will open and also close the cmdline window
2119 feedkeys('q:', 'xt')
2120 assert_equal('yes', g:got_there)
2121
2122 augroup CmdWin
2123 au!
2124 augroup END
2125 &rtp = save_rtp
2126 delete('rtp', 'rf')
2127enddef
2128
2129def Test_import_gone_when_sourced_twice()
2130 var exportlines =<< trim END
2131 vim9script
2132 if exists('g:guard')
2133 finish
2134 endif
2135 g:guard = 1
2136 export var name = 'someName'
2137 END
2138 writefile(exportlines, 'XexportScript.vim')
2139
2140 var lines =<< trim END
2141 vim9script
2142 import './XexportScript.vim' as expo
2143 def g:GetName(): string
2144 return expo.name
2145 enddef
2146 END
2147 writefile(lines, 'XscriptImport.vim')
2148 so XscriptImport.vim
2149 assert_equal('someName', g:GetName())
2150
2151 so XexportScript.vim
2152 assert_fails('call g:GetName()', 'E1149:')
2153
2154 delfunc g:GetName
2155 delete('XexportScript.vim')
2156 delete('XscriptImport.vim')
2157 unlet g:guard
2158enddef
2159
Bram Moolenaar160aa862022-01-10 21:29:57 +00002160" test using an auto-loaded function and variable
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002161def Test_vim9_autoload_full_name()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002162 var lines =<< trim END
2163 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002164 export def Gettest(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002165 return 'test'
2166 enddef
2167 g:some#name = 'name'
2168 g:some#dict = {key: 'value'}
2169
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002170 export def Varargs(a1: string, ...l: list<string>): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002171 return a1 .. l[0] .. l[1]
2172 enddef
2173 END
2174
2175 mkdir('Xdir/autoload', 'p')
2176 writefile(lines, 'Xdir/autoload/some.vim')
2177 var save_rtp = &rtp
2178 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2179
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002180 assert_equal('test', g:some#Gettest())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002181 assert_equal('name', g:some#name)
2182 assert_equal('value', g:some#dict.key)
2183 g:some#other = 'other'
2184 assert_equal('other', g:some#other)
2185
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002186 assert_equal('abc', some#Varargs('a', 'b', 'c'))
Bram Moolenaar160aa862022-01-10 21:29:57 +00002187
2188 # upper case script name works
2189 lines =<< trim END
2190 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002191 export def GetOther(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002192 return 'other'
2193 enddef
2194 END
2195 writefile(lines, 'Xdir/autoload/Other.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002196 assert_equal('other', g:Other#GetOther())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002197
2198 delete('Xdir', 'rf')
2199 &rtp = save_rtp
2200enddef
2201
2202def Test_vim9script_autoload()
2203 mkdir('Xdir/autoload', 'p')
2204 var save_rtp = &rtp
2205 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2206
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002207 # when the path has "/autoload/" prefix is not needed
Bram Moolenaar160aa862022-01-10 21:29:57 +00002208 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002209 vim9script
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002210 g:prefixed_loaded += 1
Bram Moolenaar160aa862022-01-10 21:29:57 +00002211
2212 export def Gettest(): string
2213 return 'test'
2214 enddef
2215
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002216 export var name = 'name'
2217
2218 export func GetFunc()
2219 return Gettest() .. 'more' .. s:name
Bram Moolenaar160aa862022-01-10 21:29:57 +00002220 endfunc
2221
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002222 export def GetDef(): string
2223 return Gettest() .. 'more' .. name
2224 enddef
2225
Bram Moolenaar160aa862022-01-10 21:29:57 +00002226 export final fname = 'final'
2227 export const cname = 'const'
2228 END
2229 writefile(lines, 'Xdir/autoload/prefixed.vim')
2230
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002231 g:prefixed_loaded = 0
2232 g:expected_loaded = 0
Bram Moolenaar160aa862022-01-10 21:29:57 +00002233 lines =<< trim END
2234 vim9script
2235 import autoload 'prefixed.vim'
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002236 assert_equal(g:expected_loaded, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002237 assert_equal('test', prefixed.Gettest())
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002238 assert_equal(1, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002239
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002240 assert_equal('testmorename', prefixed.GetFunc())
2241 assert_equal('testmorename', prefixed.GetDef())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002242 assert_equal('name', prefixed.name)
2243 assert_equal('final', prefixed.fname)
2244 assert_equal('const', prefixed.cname)
2245 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002246 v9.CheckScriptSuccess(lines)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002247 # can source it again, autoload script not loaded again
2248 g:expected_loaded = 1
Bram Moolenaar62aec932022-01-29 21:45:34 +00002249 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002250
2251 # can also get the items by autoload name
2252 lines =<< trim END
2253 call assert_equal('test', prefixed#Gettest())
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002254 call assert_equal('testmorename', prefixed#GetFunc())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002255 call assert_equal('name', prefixed#name)
2256 call assert_equal('final', prefixed#fname)
2257 call assert_equal('const', prefixed#cname)
2258 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002259 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002260
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002261 unlet g:prefixed_loaded
2262 unlet g:expected_loaded
2263 delete('Xdir', 'rf')
2264 &rtp = save_rtp
2265enddef
2266
Bram Moolenaard02dce22022-01-18 17:43:04 +00002267def Test_import_autoload_not_exported()
2268 mkdir('Xdir/autoload', 'p')
2269 var save_rtp = &rtp
2270 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2271
2272 # error when using an item that is not exported from an autoload script
2273 var exportLines =<< trim END
2274 vim9script
2275 var notExported = 123
2276 def NotExport()
2277 echo 'nop'
2278 enddef
2279 END
2280 writefile(exportLines, 'Xdir/autoload/notExport1.vim')
2281
2282 var lines =<< trim END
2283 vim9script
2284 import autoload 'notExport1.vim'
2285 echo notExport1.notFound
2286 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002287 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002288
2289 lines =<< trim END
2290 vim9script
2291 import autoload 'notExport1.vim'
2292 echo notExport1.notExported
2293 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002294 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002295
2296 lines =<< trim END
2297 vim9script
2298 import autoload 'notExport1.vim'
2299 echo notExport1.NotFunc()
2300 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002301 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002302
2303 lines =<< trim END
2304 vim9script
2305 import autoload 'notExport1.vim'
2306 echo notExport1.NotExport()
2307 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002308 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002309
2310 lines =<< trim END
2311 vim9script
2312 import autoload 'notExport1.vim'
2313 echo 'text'->notExport1.NotFunc()
2314 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002315 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002316
2317 lines =<< trim END
2318 vim9script
2319 import autoload 'notExport1.vim'
2320 echo 'text'->notExport1.NotExport()
2321 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002322 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002323
2324 # using a :def function we use a different autoload script every time so that
2325 # the function is compiled without the script loaded
2326 writefile(exportLines, 'Xdir/autoload/notExport2.vim')
2327 lines =<< trim END
2328 vim9script
2329 import autoload 'notExport2.vim'
2330 def Testit()
2331 echo notExport2.notFound
2332 enddef
2333 Testit()
2334 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002335 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport2#notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002336
2337 writefile(exportLines, 'Xdir/autoload/notExport3.vim')
2338 lines =<< trim END
2339 vim9script
2340 import autoload 'notExport3.vim'
2341 def Testit()
2342 echo notExport3.notExported
2343 enddef
2344 Testit()
2345 END
2346 # don't get E1049 because it is too complicated to figure out
Bram Moolenaar62aec932022-01-29 21:45:34 +00002347 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport3#notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002348
2349 writefile(exportLines, 'Xdir/autoload/notExport4.vim')
2350 lines =<< trim END
2351 vim9script
2352 import autoload 'notExport4.vim'
2353 def Testit()
2354 echo notExport4.NotFunc()
2355 enddef
2356 Testit()
2357 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002358 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport4#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002359
2360 writefile(exportLines, 'Xdir/autoload/notExport5.vim')
2361 lines =<< trim END
2362 vim9script
2363 import autoload 'notExport5.vim'
2364 def Testit()
2365 echo notExport5.NotExport()
2366 enddef
2367 Testit()
2368 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002369 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport5#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002370
2371 writefile(exportLines, 'Xdir/autoload/notExport6.vim')
2372 lines =<< trim END
2373 vim9script
2374 import autoload 'notExport6.vim'
2375 def Testit()
2376 echo 'text'->notExport6.NotFunc()
2377 enddef
2378 Testit()
2379 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002380 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport6#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002381
2382 writefile(exportLines, 'Xdir/autoload/notExport7.vim')
2383 lines =<< trim END
2384 vim9script
2385 import autoload 'notExport7.vim'
2386 def Testit()
2387 echo 'text'->notExport7.NotExport()
2388 enddef
2389 Testit()
2390 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002391 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport7#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002392
2393 delete('Xdir', 'rf')
2394 &rtp = save_rtp
2395enddef
2396
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002397def Test_vim9script_autoload_call()
2398 mkdir('Xdir/autoload', 'p')
2399 var save_rtp = &rtp
2400 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2401
2402 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002403 vim9script
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002404
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002405 export def RetArg(arg: string): string
2406 return arg
2407 enddef
2408
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002409 export def Getother()
2410 g:result = 'other'
2411 enddef
2412 END
Bram Moolenaar5d982692022-01-12 15:15:27 +00002413 writefile(lines, 'Xdir/autoload/another.vim')
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002414
2415 lines =<< trim END
2416 vim9script
Bram Moolenaar5d982692022-01-12 15:15:27 +00002417 import autoload 'another.vim'
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002418
2419 # compile this before 'another.vim' is loaded
2420 def CallAnother()
2421 assert_equal('foo', 'foo'->another.RetArg())
2422 enddef
2423 CallAnother()
2424
Bram Moolenaar5d982692022-01-12 15:15:27 +00002425 call another.Getother()
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002426 assert_equal('other', g:result)
Bram Moolenaar3d8e25a2022-01-22 11:00:02 +00002427
2428 assert_equal('arg', call('another.RetArg', ['arg']))
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00002429
2430 verbose function another.Getother
2431 # should we disallow this?
2432 verbose function another#Getother
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002433 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002434 v9.CheckScriptSuccess(lines)
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002435
2436 unlet g:result
Bram Moolenaar160aa862022-01-10 21:29:57 +00002437 delete('Xdir', 'rf')
2438 &rtp = save_rtp
2439enddef
2440
Bram Moolenaarb697dc22022-01-22 11:27:29 +00002441def Test_vim9script_noclear_autoload()
2442 mkdir('Xdir/autoload', 'p')
2443 var save_rtp = &rtp
2444 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2445
2446 var lines =<< trim END
2447 vim9script
2448 export def Func(): string
2449 return 'called'
2450 enddef
2451 g:double_loaded = 'yes'
2452 END
2453 writefile(lines, 'Xdir/autoload/double.vim')
2454
2455 lines =<< trim END
2456 vim9script noclear
2457 if exists('g:script_loaded')
2458 finish
2459 endif
2460 g:script_loaded = true
2461
2462 import autoload 'double.vim'
2463 nnoremap <F3> <ScriptCmd>g:result = double.Func()<CR>
2464 END
2465 g:double_loaded = 'no'
2466 writefile(lines, 'Xloaddouble')
2467 source Xloaddouble
2468 assert_equal('no', g:double_loaded)
2469 assert_equal(true, g:script_loaded)
2470 source Xloaddouble
2471 feedkeys("\<F3>", 'xt')
2472 assert_equal('called', g:result)
2473 assert_equal('yes', g:double_loaded)
2474
2475 delete('Xloaddouble')
2476 unlet g:double_loaded
2477 unlet g:script_loaded
2478 unlet g:result
2479 delete('Xdir', 'rf')
2480 &rtp = save_rtp
2481enddef
2482
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002483def Test_vim9script_autoload_duplicate()
2484 mkdir('Xdir/autoload', 'p')
2485
2486 var lines =<< trim END
2487 vim9script
2488
2489 export def Func()
2490 enddef
2491
2492 def Func()
2493 enddef
2494 END
2495 writefile(lines, 'Xdir/autoload/dupfunc.vim')
2496 assert_fails('source Xdir/autoload/dupfunc.vim', 'E1073:')
2497
2498 lines =<< trim END
2499 vim9script
2500
2501 def Func()
2502 enddef
2503
2504 export def Func()
2505 enddef
2506 END
2507 writefile(lines, 'Xdir/autoload/dup2func.vim')
2508 assert_fails('source Xdir/autoload/dup2func.vim', 'E1073:')
2509
2510 lines =<< trim END
2511 vim9script
2512
2513 def Func()
2514 enddef
2515
2516 export var Func = 'asdf'
2517 END
2518 writefile(lines, 'Xdir/autoload/dup3func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002519 assert_fails('source Xdir/autoload/dup3func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002520
2521 lines =<< trim END
2522 vim9script
2523
2524 export var Func = 'asdf'
2525
2526 def Func()
2527 enddef
2528 END
2529 writefile(lines, 'Xdir/autoload/dup4func.vim')
2530 assert_fails('source Xdir/autoload/dup4func.vim', 'E707:')
2531
2532 lines =<< trim END
2533 vim9script
2534
2535 var Func = 'asdf'
2536
2537 export def Func()
2538 enddef
2539 END
2540 writefile(lines, 'Xdir/autoload/dup5func.vim')
2541 assert_fails('source Xdir/autoload/dup5func.vim', 'E707:')
2542
2543 lines =<< trim END
2544 vim9script
2545
2546 export def Func()
2547 enddef
2548
2549 var Func = 'asdf'
2550 END
2551 writefile(lines, 'Xdir/autoload/dup6func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002552 assert_fails('source Xdir/autoload/dup6func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002553
2554 delete('Xdir', 'rf')
2555enddef
2556
Bram Moolenaar2017d6f2022-01-20 19:38:46 +00002557def Test_autoload_missing_function_name()
2558 mkdir('Xdir/autoload', 'p')
2559
2560 var lines =<< trim END
2561 vim9script
2562
2563 def loadme#()
2564 enddef
2565 END
2566 writefile(lines, 'Xdir/autoload/loadme.vim')
2567 assert_fails('source Xdir/autoload/loadme.vim', 'E129:')
2568
2569 delete('Xdir', 'rf')
2570enddef
2571
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002572def Test_autoload_name_wrong()
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002573 var lines =<< trim END
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002574 def Xscriptname#Func()
2575 enddef
2576 END
2577 writefile(lines, 'Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002578 v9.CheckScriptFailure(lines, 'E746:')
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00002579 delete('Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002580
2581 mkdir('Xdir/autoload', 'p')
2582 lines =<< trim END
2583 vim9script
2584 def somescript#Func()
2585 enddef
2586 END
2587 writefile(lines, 'Xdir/autoload/somescript.vim')
2588 assert_fails('source Xdir/autoload/somescript.vim', 'E1263:')
2589
2590 delete('Xdir', 'rf')
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002591enddef
2592
Bram Moolenaard041f422022-01-12 19:54:00 +00002593def Test_import_autoload_postponed()
2594 mkdir('Xdir/autoload', 'p')
2595 var save_rtp = &rtp
2596 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2597
2598 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002599 vim9script
Bram Moolenaard041f422022-01-12 19:54:00 +00002600
2601 g:loaded_postponed = 'true'
2602 export var variable = 'bla'
2603 export def Function(): string
2604 return 'bla'
2605 enddef
2606 END
2607 writefile(lines, 'Xdir/autoload/postponed.vim')
2608
2609 lines =<< trim END
2610 vim9script
2611
2612 import autoload 'postponed.vim'
2613 def Tryit()
2614 echo postponed.variable
2615 echo postponed.Function()
2616 enddef
2617 defcompile
2618 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002619 v9.CheckScriptSuccess(lines)
Bram Moolenaard041f422022-01-12 19:54:00 +00002620 assert_false(exists('g:loaded_postponed'))
Bram Moolenaar62aec932022-01-29 21:45:34 +00002621 v9.CheckScriptSuccess(lines + ['Tryit()'])
Bram Moolenaard041f422022-01-12 19:54:00 +00002622 assert_equal('true', g:loaded_postponed)
2623
2624 unlet g:loaded_postponed
2625 delete('Xdir', 'rf')
2626 &rtp = save_rtp
2627enddef
2628
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002629def Test_import_autoload_override()
2630 mkdir('Xdir/autoload', 'p')
2631 var save_rtp = &rtp
2632 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2633 test_override('autoload', 1)
2634
2635 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002636 vim9script
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002637
2638 g:loaded_override = 'true'
2639 export var variable = 'bla'
2640 export def Function(): string
2641 return 'bla'
2642 enddef
2643 END
2644 writefile(lines, 'Xdir/autoload/override.vim')
2645
2646 lines =<< trim END
2647 vim9script
2648
2649 import autoload 'override.vim'
2650 assert_equal('true', g:loaded_override)
2651
2652 def Tryit()
2653 echo override.doesNotExist
2654 enddef
2655 defcompile
2656 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002657 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1)
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002658
2659 test_override('autoload', 0)
2660 unlet g:loaded_override
2661 delete('Xdir', 'rf')
2662 &rtp = save_rtp
2663enddef
2664
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002665def Test_autoload_mapping()
2666 mkdir('Xdir/autoload', 'p')
2667 var save_rtp = &rtp
2668 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2669
2670 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002671 vim9script
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002672
2673 g:toggle_loaded = 'yes'
2674
2675 export def Toggle(): string
2676 return ":g:toggle_called = 'yes'\<CR>"
2677 enddef
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002678 export def Doit()
2679 g:doit_called = 'yes'
2680 enddef
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002681 END
2682 writefile(lines, 'Xdir/autoload/toggle.vim')
2683
2684 lines =<< trim END
2685 vim9script
2686
2687 import autoload 'toggle.vim'
2688
2689 nnoremap <silent> <expr> tt toggle.Toggle()
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002690 nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR>
2691 nnoremap <silent> yy <Cmd>toggle.Doit()<CR>
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002692 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002693 v9.CheckScriptSuccess(lines)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002694 assert_false(exists("g:toggle_loaded"))
2695 assert_false(exists("g:toggle_called"))
Bram Moolenaar6079da72022-01-18 14:16:59 +00002696 assert_match('\d A: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002697
2698 feedkeys("tt", 'xt')
2699 assert_equal('yes', g:toggle_loaded)
2700 assert_equal('yes', g:toggle_called)
Bram Moolenaar6079da72022-01-18 14:16:59 +00002701 assert_match('\d: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002702
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002703 feedkeys("xx", 'xt')
2704 assert_equal('yes', g:doit_called)
2705
2706 assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle')
2707
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002708 nunmap tt
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002709 nunmap xx
2710 nunmap yy
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002711 unlet g:toggle_loaded
2712 unlet g:toggle_called
2713 delete('Xdir', 'rf')
2714 &rtp = save_rtp
2715enddef
2716
Bram Moolenaar160aa862022-01-10 21:29:57 +00002717def Test_vim9script_autoload_fails()
2718 var lines =<< trim END
2719 vim9script autoload
2720 var n = 0
2721 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002722 v9.CheckScriptFailure(lines, 'E475: Invalid argument: autoload')
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002723
2724 lines =<< trim END
2725 vim9script noclear noclear
2726 var n = 0
2727 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002728 v9.CheckScriptFailure(lines, 'E983: Duplicate argument: noclear')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +01002729
2730 lines =<< trim END
2731 vim9script noclears
2732 var n = 0
2733 END
2734 v9.CheckScriptFailure(lines, 'E475: Invalid argument: noclears')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002735enddef
2736
2737def Test_import_autoload_fails()
2738 var lines =<< trim END
2739 vim9script
2740 import autoload autoload 'prefixed.vim'
2741 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002742 v9.CheckScriptFailure(lines, 'E121: Undefined variable: autoload')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002743
2744 lines =<< trim END
2745 vim9script
Bram Moolenaar1836d612022-01-18 13:14:47 +00002746 import autoload './doesNotExist.vim'
Bram Moolenaar160aa862022-01-10 21:29:57 +00002747 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002748 v9.CheckScriptFailure(lines, 'E282:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002749
2750 lines =<< trim END
2751 vim9script
2752 import autoload '/dir/doesNotExist.vim'
2753 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002754 v9.CheckScriptFailure(lines, 'E282:', 2)
2755
2756 lines =<< trim END
2757 vim9script
2758 import autoload '../testdir'
2759 END
2760 v9.CheckScriptFailure(lines, 'E17:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002761
2762 lines =<< trim END
2763 vim9script
2764 import autoload 'doesNotExist.vim'
2765 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002766 v9.CheckScriptFailure(lines, 'E1053: Could not import "doesNotExist.vim"')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002767enddef
2768
2769" test disassembling an auto-loaded function starting with "debug"
2770def Test_vim9_autoload_disass()
2771 mkdir('Xdir/autoload', 'p')
2772 var save_rtp = &rtp
2773 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2774
2775 var lines =<< trim END
2776 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002777 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002778 return 'debug'
2779 enddef
2780 END
2781 writefile(lines, 'Xdir/autoload/debugit.vim')
2782
2783 lines =<< trim END
2784 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002785 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002786 return 'profile'
2787 enddef
2788 END
2789 writefile(lines, 'Xdir/autoload/profileit.vim')
2790
2791 lines =<< trim END
2792 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002793 assert_equal('debug', debugit#Test())
2794 disass debugit#Test
2795 assert_equal('profile', profileit#Test())
2796 disass profileit#Test
Bram Moolenaar160aa862022-01-10 21:29:57 +00002797 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002798 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002799
2800 delete('Xdir', 'rf')
2801 &rtp = save_rtp
2802enddef
2803
2804" test using a vim9script that is auto-loaded from an autocmd
2805def Test_vim9_aucmd_autoload()
2806 var lines =<< trim END
2807 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002808 export def Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002809 echomsg getreg('"')
2810 enddef
2811 END
2812
2813 mkdir('Xdir/autoload', 'p')
2814 writefile(lines, 'Xdir/autoload/foo.vim')
2815 var save_rtp = &rtp
2816 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2817 augroup test
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002818 autocmd TextYankPost * call foo#Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002819 augroup END
2820
2821 normal Y
2822
2823 augroup test
2824 autocmd!
2825 augroup END
2826 delete('Xdir', 'rf')
2827 &rtp = save_rtp
2828enddef
2829
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002830" test using a autoloaded file that is case sensitive
2831def Test_vim9_autoload_case_sensitive()
2832 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002833 vim9script
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002834 export def CaseSensitive(): string
2835 return 'done'
2836 enddef
2837 END
2838
2839 mkdir('Xdir/autoload', 'p')
2840 writefile(lines, 'Xdir/autoload/CaseSensitive.vim')
2841 var save_rtp = &rtp
2842 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2843
2844 lines =<< trim END
2845 vim9script
2846 import autoload 'CaseSensitive.vim'
2847 assert_equal('done', CaseSensitive.CaseSensitive())
2848 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002849 v9.CheckScriptSuccess(lines)
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002850
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002851 if !has('fname_case')
2852 lines =<< trim END
2853 vim9script
2854 import autoload 'CaseSensitive.vim'
2855 import autoload 'casesensitive.vim'
2856 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002857 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002858 endif
2859
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002860 delete('Xdir', 'rf')
2861 &rtp = save_rtp
2862enddef
2863
Bram Moolenaar160aa862022-01-10 21:29:57 +00002864" This was causing a crash because suppress_errthrow wasn't reset.
2865def Test_vim9_autoload_error()
2866 var lines =<< trim END
2867 vim9script
2868 def crash#func()
2869 try
2870 for x in List()
2871 endfor
2872 catch
2873 endtry
2874 g:ok = true
2875 enddef
2876 fu List()
2877 invalid
2878 endfu
2879 try
2880 alsoinvalid
2881 catch /wontmatch/
2882 endtry
2883 END
2884 call mkdir('Xruntime/autoload', 'p')
2885 call writefile(lines, 'Xruntime/autoload/crash.vim')
2886
2887 # run in a separate Vim to avoid the side effects of assert_fails()
2888 lines =<< trim END
2889 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
2890 call crash#func()
2891 call writefile(['ok'], 'Xdidit')
2892 qall!
2893 END
2894 writefile(lines, 'Xscript')
Bram Moolenaar62aec932022-01-29 21:45:34 +00002895 g:RunVim([], [], '-S Xscript')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002896 assert_equal(['ok'], readfile('Xdidit'))
2897
2898 delete('Xdidit')
2899 delete('Xscript')
2900 delete('Xruntime', 'rf')
2901
2902 lines =<< trim END
2903 vim9script
2904 var foo#bar = 'asdf'
2905 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002906 v9.CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002907enddef
2908
Bram Moolenaar753885b2022-08-24 16:30:36 +01002909def Test_vim9_import_symlink()
2910 if !has('unix')
2911 CheckUnix
2912 else
2913 mkdir('Xto/plugin', 'p')
2914 var lines =<< trim END
2915 vim9script
2916 import autoload 'bar.vim'
2917 g:resultFunc = bar.Func()
2918 g:resultValue = bar.value
2919 END
2920 writefile(lines, 'Xto/plugin/foo.vim')
2921
2922 mkdir('Xto/autoload', 'p')
2923 lines =<< trim END
2924 vim9script
2925 export def Func(): string
2926 return 'func'
2927 enddef
2928 export var value = 'val'
2929 END
2930 writefile(lines, 'Xto/autoload/bar.vim')
2931
2932 var save_rtp = &rtp
2933 &rtp = getcwd() .. '/Xfrom'
2934 system('ln -s ' .. getcwd() .. '/Xto Xfrom')
2935
2936 source Xfrom/plugin/foo.vim
2937 assert_equal('func', g:resultFunc)
2938 assert_equal('val', g:resultValue)
2939
2940 var infoTo = getscriptinfo()->filter((_, v) => v.name =~ 'Xto/autoload/bar')
2941 var infoFrom = getscriptinfo()->filter((_, v) => v.name =~ 'Xfrom/autoload/bar')
2942 assert_equal(1, len(infoTo))
2943 assert_equal(1, len(infoFrom))
2944 assert_equal(infoTo[0].sid, infoFrom[0].sourced)
Bram Moolenaar7ea9fcb2022-08-24 17:46:12 +01002945 var output: string
2946 redir => output
2947 scriptnames
2948 redir END
2949 assert_match(infoFrom[0].sid .. '->' .. infoFrom[0].sourced .. '.*Xfrom', output)
Bram Moolenaar753885b2022-08-24 16:30:36 +01002950
2951 unlet g:resultFunc
2952 unlet g:resultValue
2953 &rtp = save_rtp
2954 delete('Xto', 'rf')
2955 delete('Xfrom', 'rf')
2956 endif
2957enddef
2958
Bram Moolenaard8448622022-01-07 21:39:52 +00002959
2960" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker