blob: 832fad8d74a52b1ac0329c4045d7f8017b3ac4ac [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 Lakshmanan520f6ef2022-08-25 17:40:40 +0100735 var l = getscriptinfo()
736 assert_match('XrelautoloadExport.vim$', l[-1].name)
737 assert_true(l[-1].autoload)
Bram Moolenaar89445512022-04-14 12:58:23 +0100738 feedkeys("\<F3>", "xt")
739 assert_equal(42, g:result)
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +0100740 l = getscriptinfo({name: 'XrelautoloadExport'})
741 assert_true(len(l) == 1)
742 assert_match('XrelautoloadExport.vim$', l[0].name)
743 assert_false(l[0].autoload)
Bram Moolenaar89445512022-04-14 12:58:23 +0100744
745 unlet g:result
Bram Moolenaar648dd882022-04-14 21:36:15 +0100746 delete('XrelautoloadExport.vim')
Bram Moolenaar89445512022-04-14 12:58:23 +0100747 delete('Xmapscript.vim')
748 nunmap <F3>
Bram Moolenaard8448622022-01-07 21:39:52 +0000749enddef
750
Bram Moolenaar5cb53b72022-05-26 19:54:05 +0100751def Test_autoload_import_var()
752 # variable name starts with "autoload"
753 var lines =<< trim END
754 vim9script
755 var autoloaded = "Xtest.vim"
756 import autoloaded
757 END
758 v9.CheckScriptFailure(lines, 'E1053: Could not import "Xtest.vim')
759enddef
760
Bram Moolenaar648dd882022-04-14 21:36:15 +0100761def Test_use_autoload_import_in_mapping()
762 var lines =<< trim END
763 vim9script
764 export def Func()
765 g:result = 49
766 enddef
767 END
768 mkdir('Xdir/autoload', 'p')
769 writefile(lines, 'Xdir/autoload/XautoloadExport.vim')
770 var save_rtp = &rtp
771 exe 'set rtp^=' .. getcwd() .. '/Xdir'
772
773 lines =<< trim END
774 vim9script
775 import autoload 'XautoloadExport.vim' as some
776 nnoremap <F3> :call <SID>some.Func()<CR>
777 END
778 writefile(lines, 'Xmapscript.vim')
779
780 source Xmapscript.vim
781 assert_match('\d\+ A: .*autoload/XautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
782 feedkeys("\<F3>", "xt")
783 assert_equal(49, g:result)
784
785 unlet g:result
786 delete('Xmapscript.vim')
787 nunmap <F3>
788 delete('Xdir', 'rf')
789 &rtp = save_rtp
790enddef
791
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000792def Test_use_import_in_command_completion()
Bram Moolenaar15d16352022-01-17 20:09:08 +0000793 var lines =<< trim END
794 vim9script
795 export def Complete(..._): list<string>
796 return ['abcd']
797 enddef
798 END
799 writefile(lines, 'Xscript.vim')
800
801 lines =<< trim END
802 vim9script
803 import './Xscript.vim'
804
805 command -nargs=1 -complete=customlist,Xscript.Complete Cmd echo 'ok'
806 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
807 assert_equal('#Cmd abcd', @:)
808 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000809 v9.CheckScriptSuccess(lines)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000810
811 delcommand Cmd
812 delete('Xscript.vim')
813enddef
814
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100815def Test_use_import_with_funcref_in_command_completion()
816 var lines =<< trim END
817 vim9script
818 export def Complete(..._): list<string>
819 return ['abcd']
820 enddef
821 END
822 writefile(lines, 'Xscript.vim')
823
824 lines =<< trim END
825 vim9script
826 import './Xscript.vim'
827
828 var Ref = Xscript.Complete
829 exe "command -nargs=1 -complete=customlist," .. expand('<SID>') .. "Ref Cmd echo 'ok'"
830 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
831 assert_equal('#Cmd abcd', @:)
832 END
833 v9.CheckScriptSuccess(lines)
834
835 delcommand Cmd
836 delete('Xscript.vim')
837enddef
838
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000839def Test_use_autoload_import_in_insert_completion()
840 mkdir('Xdir/autoload', 'p')
841 var save_rtp = &rtp
842 exe 'set rtp^=' .. getcwd() .. '/Xdir'
843
844 var lines =<< trim END
845 vim9script
846 export def ThesaurusFunc(findbase: bool, _): any
847 if findbase
848 return 1
849 endif
850 return [
851 'check',
852 'experiment',
853 'test',
854 'verification'
855 ]
856 enddef
857 g:completion_loaded = 'yes'
858 END
859 writefile(lines, 'Xdir/autoload/completion.vim')
860
861 new
862 lines =<< trim END
863 vim9script
864 g:completion_loaded = 'no'
865 import autoload 'completion.vim'
866 set thesaurusfunc=completion.ThesaurusFunc
867 assert_equal('no', g:completion_loaded)
868 feedkeys("i\<C-X>\<C-T>\<C-N>\<Esc>", 'xt')
869 assert_equal('experiment', getline(1))
870 assert_equal('yes', g:completion_loaded)
871 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000872 v9.CheckScriptSuccess(lines)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000873
874 set thesaurusfunc=
875 bwipe!
876 delete('Xdir', 'rf')
877 &rtp = save_rtp
878enddef
879
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000880def Test_use_autoload_import_partial_in_opfunc()
881 mkdir('Xdir/autoload', 'p')
882 var save_rtp = &rtp
883 exe 'set rtp^=' .. getcwd() .. '/Xdir'
884
885 var lines =<< trim END
886 vim9script
887 export def Opfunc(..._)
888 g:opfunc_called = 'yes'
889 enddef
890 END
891 writefile(lines, 'Xdir/autoload/opfunc.vim')
892
893 new
894 lines =<< trim END
895 vim9script
896 import autoload 'opfunc.vim'
897 nnoremap <expr> <F3> TheFunc()
898 def TheFunc(): string
899 &operatorfunc = function('opfunc.Opfunc', [0])
900 return 'g@'
901 enddef
902 feedkeys("\<F3>l", 'xt')
903 assert_equal('yes', g:opfunc_called)
904 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000905 v9.CheckScriptSuccess(lines)
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000906
907 set opfunc=
908 bwipe!
909 delete('Xdir', 'rf')
Bram Moolenaar06b77222022-01-25 15:51:56 +0000910 nunmap <F3>
911 &rtp = save_rtp
912enddef
913
914def Test_set_opfunc_to_autoload_func_directly()
915 mkdir('Xdir/autoload', 'p')
916 var save_rtp = &rtp
917 exe 'set rtp^=' .. getcwd() .. '/Xdir'
918
919 var lines =<< trim END
920 vim9script
921 export def Opfunc(..._)
922 g:opfunc_called = 'yes'
923 enddef
924 END
925 writefile(lines, 'Xdir/autoload/opfunc.vim')
926
927 new
928 lines =<< trim END
929 vim9script
930 import autoload 'opfunc.vim'
931 nnoremap <expr> <F3> TheFunc()
932 def TheFunc(): string
933 &operatorfunc = opfunc.Opfunc
934 return 'g@'
935 enddef
936 feedkeys("\<F3>l", 'xt')
937 assert_equal('yes', g:opfunc_called)
938 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000939 v9.CheckScriptSuccess(lines)
Bram Moolenaar06b77222022-01-25 15:51:56 +0000940
941 set opfunc=
942 bwipe!
943 delete('Xdir', 'rf')
944 nunmap <F3>
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000945 &rtp = save_rtp
946enddef
947
Bram Moolenaare70dd112022-01-21 16:31:11 +0000948def Test_use_autoload_import_in_fold_expression()
949 mkdir('Xdir/autoload', 'p')
950 var save_rtp = &rtp
951 exe 'set rtp^=' .. getcwd() .. '/Xdir'
952
953 var lines =<< trim END
954 vim9script
955 export def Expr(): string
956 return getline(v:lnum) =~ '^#' ? '>1' : '1'
957 enddef
Bram Moolenaar9530b582022-01-22 13:39:08 +0000958 export def Text(): string
959 return 'fold text'
960 enddef
Bram Moolenaare70dd112022-01-21 16:31:11 +0000961 g:fold_loaded = 'yes'
962 END
963 writefile(lines, 'Xdir/autoload/fold.vim')
964
965 lines =<< trim END
966 vim9script
967 import autoload 'fold.vim'
968 &foldexpr = 'fold.Expr()'
Bram Moolenaar9530b582022-01-22 13:39:08 +0000969 &foldtext = 'fold.Text()'
Bram Moolenaare70dd112022-01-21 16:31:11 +0000970 &foldmethod = 'expr'
971 &debug = 'throw'
972 END
973 new
974 setline(1, ['# one', 'text', '# two', 'text'])
975 g:fold_loaded = 'no'
Bram Moolenaar62aec932022-01-29 21:45:34 +0000976 v9.CheckScriptSuccess(lines)
Bram Moolenaare70dd112022-01-21 16:31:11 +0000977 assert_equal('no', g:fold_loaded)
978 redraw
979 assert_equal('yes', g:fold_loaded)
980
981 # Check that script context of 'foldexpr' is copied to another buffer.
982 edit! otherfile
983 redraw
984
Bram Moolenaar9530b582022-01-22 13:39:08 +0000985 set foldexpr= foldtext& foldmethod& debug=
Bram Moolenaare70dd112022-01-21 16:31:11 +0000986 bwipe!
987 delete('Xdir', 'rf')
988 &rtp = save_rtp
989enddef
990
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100991def Test_autoload_import_relative()
992 var lines =<< trim END
993 vim9script
994
995 g:loaded = 'yes'
996 export def RelFunc(): string
997 return 'relfunc'
998 enddef
999 def NotExported()
1000 echo 'not'
1001 enddef
1002
1003 export var someText = 'some text'
1004 var notexp = 'bad'
1005 END
1006 writefile(lines, 'XimportRel.vim')
1007 writefile(lines, 'XimportRel2.vim')
1008 writefile(lines, 'XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +01001009 writefile(lines, 'XimportRel4.vim')
1010 writefile(lines, 'XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001011
1012 lines =<< trim END
1013 vim9script
1014 g:loaded = 'no'
1015 import autoload './XimportRel.vim'
1016 assert_equal('no', g:loaded)
1017
1018 def AFunc(): string
1019 var res = ''
1020 res ..= XimportRel.RelFunc()
1021 res ..= '/'
1022 res ..= XimportRel.someText
1023 XimportRel.someText = 'from AFunc'
1024 return res
1025 enddef
1026 # script not loaded when compiling
1027 defcompile
1028 assert_equal('no', g:loaded)
1029
1030 assert_equal('relfunc/some text', AFunc())
1031 assert_equal('yes', g:loaded)
1032 unlet g:loaded
1033
1034 assert_equal('from AFunc', XimportRel.someText)
1035 XimportRel.someText = 'from script'
1036 assert_equal('from script', XimportRel.someText)
1037 END
1038 v9.CheckScriptSuccess(lines)
1039
1040 lines =<< trim END
1041 vim9script
1042 import autoload './XimportRel.vim'
1043 echo XimportRel.NotExported()
1044 END
1045 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 3)
1046
1047 lines =<< trim END
1048 vim9script
1049 import autoload './XimportRel.vim'
1050 echo XimportRel.notexp
1051 END
1052 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
1053
1054 lines =<< trim END
1055 vim9script
1056 import autoload './XimportRel.vim'
1057 XimportRel.notexp = 'bad'
1058 END
1059 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
1060
1061 lines =<< trim END
1062 vim9script
1063 import autoload './XimportRel.vim'
1064 def Func()
1065 echo XimportRel.NotExported()
1066 enddef
1067 Func()
1068 END
1069 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 1)
1070
1071 lines =<< trim END
1072 vim9script
1073 import autoload './XimportRel.vim'
1074 def Func()
1075 echo XimportRel.notexp
1076 enddef
1077 Func()
1078 END
1079 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1080
Bram Moolenaar10611952022-04-03 21:11:34 +01001081 # Same, script not imported before
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001082 lines =<< trim END
1083 vim9script
Bram Moolenaar10611952022-04-03 21:11:34 +01001084 import autoload './XimportRel4.vim'
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001085 def Func()
Bram Moolenaar10611952022-04-03 21:11:34 +01001086 echo XimportRel4.notexp
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001087 enddef
1088 Func()
1089 END
1090 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1091
Bram Moolenaar10611952022-04-03 21:11:34 +01001092 # does not fail if the script wasn't loaded yet and only compiling
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001093 g:loaded = 'no'
1094 lines =<< trim END
1095 vim9script
1096 import autoload './XimportRel2.vim'
1097 def Func()
1098 echo XimportRel2.notexp
1099 enddef
1100 defcompile
1101 END
1102 v9.CheckScriptSuccess(lines)
1103 assert_equal('no', g:loaded)
1104
Bram Moolenaar10611952022-04-03 21:11:34 +01001105 lines =<< trim END
1106 vim9script
1107 import autoload './XimportRel.vim'
1108 def Func()
1109 XimportRel.notexp = 'bad'
1110 enddef
1111 Func()
1112 END
1113 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1114
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001115 # fails with a not loaded import
1116 lines =<< trim END
1117 vim9script
1118 import autoload './XimportRel3.vim'
1119 def Func()
1120 XimportRel3.notexp = 'bad'
1121 enddef
1122 Func()
1123 END
1124 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1125 assert_equal('yes', g:loaded)
1126 unlet g:loaded
1127
Bram Moolenaar10611952022-04-03 21:11:34 +01001128 lines =<< trim END
1129 vim9script
1130 import autoload './XimportRel5.vim'
1131 def Func()
1132 XimportRel5.nosuchvar = 'bad'
1133 enddef
1134 Func()
1135 END
1136 v9.CheckScriptFailure(lines, 'E121: Undefined variable: nosuchvar', 1)
1137 unlet g:loaded
1138
1139 # nasty: delete script after compiling function
1140 writefile(['vim9script'], 'XimportRelDel.vim')
1141 lines =<< trim END
1142 vim9script
1143
1144 import autoload './XimportRelDel.vim'
1145 def DoIt()
1146 echo XimportRelDel.var
1147 enddef
1148 defcompile
1149 delete('XimportRelDel.vim')
1150 DoIt()
1151 END
Bram Moolenaar242c1522022-04-03 21:52:51 +01001152 v9.CheckScriptFailure(lines, 'E484:')
Bram Moolenaar10611952022-04-03 21:11:34 +01001153
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001154 delete('XimportRel.vim')
1155 delete('XimportRel2.vim')
1156 delete('XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +01001157 delete('XimportRel4.vim')
1158 delete('XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001159enddef
1160
Bram Moolenaarccbfd482022-03-31 16:18:23 +01001161def Test_autoload_import_relative_autoload_dir()
1162 mkdir('autoload', 'p')
1163 var lines =<< trim END
1164 vim9script
1165 export def Bar()
1166 g:called_bar = 'yes'
1167 enddef
1168 END
1169 writefile(lines, 'autoload/script.vim')
1170
1171 lines =<< trim END
1172 vim9script
1173 import autoload './autoload/script.vim'
1174 def Foo()
1175 script.Bar()
1176 enddef
1177 Foo()
1178 assert_equal('yes', g:called_bar)
1179 END
1180 v9.CheckScriptSuccess(lines)
1181
1182 unlet g:called_bar
1183 delete('autoload', 'rf')
1184enddef
1185
Bram Moolenaaraac12da2022-04-24 21:33:20 +01001186def Test_autoload_import_deleted()
1187 var lines =<< trim END
1188 vim9script
1189 export const FOO = 1
1190 END
1191 writefile(lines, 'Xa.vim')
1192
1193 lines =<< trim END
1194 vim9script
1195 import autoload './Xa.vim'
1196
1197 delete('Xa.vim')
1198 var x = Xa.FOO
1199 END
1200 v9.CheckScriptFailure(lines, 'E484:')
1201
1202 delete('Xdir', 'rf')
1203enddef
1204
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001205func Test_import_in_diffexpr()
1206 CheckExecutable diff
1207
1208 call Run_Test_import_in_diffexpr()
1209endfunc
1210
1211def Run_Test_import_in_diffexpr()
1212 var lines =<< trim END
1213 vim9script
1214
1215 export def DiffExpr()
1216 # Prepend some text to check diff type detection
1217 writefile(['warning', ' message'], v:fname_out)
1218 silent exe '!diff ' .. v:fname_in .. ' '
1219 .. v:fname_new .. '>>' .. v:fname_out
1220 enddef
1221 END
1222 writefile(lines, 'Xdiffexpr')
1223
1224 lines =<< trim END
1225 vim9script
1226 import './Xdiffexpr' as diff
1227
1228 set diffexpr=diff.DiffExpr()
1229 set diffopt=foldcolumn:0
1230 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001231 v9.CheckScriptSuccess(lines)
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001232
1233 enew!
1234 call setline(1, ['one', 'two', 'three'])
1235 diffthis
1236
1237 botright vert new
1238 call setline(1, ['one', 'two', 'three.'])
1239 diffthis
1240 # we only check if this does not cause errors
1241 redraw
1242
1243 diffoff!
Bram Moolenaar50761872022-06-26 18:01:00 +01001244 set diffexpr=
1245 set diffopt&
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001246 bwipe!
1247 bwipe!
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001248 delete('Xdiffexpr')
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001249enddef
1250
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001251def Test_import_in_patchexpr()
1252 var lines =<< trim END
1253 vim9script
1254 export def TPatch()
1255 call writefile(['output file'], v:fname_out)
1256 enddef
1257 END
1258 writefile(lines, 'Xpatchexpr')
1259
1260 lines =<< trim END
1261 vim9script
1262 import './Xpatchexpr' as patch
1263 set patchexpr=patch.TPatch()
1264 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001265 v9.CheckScriptSuccess(lines)
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001266
1267 call writefile(['input file'], 'Xinput')
1268 call writefile(['diff file'], 'Xdiff')
1269 :%bwipe!
1270 edit Xinput
1271 diffpatch Xdiff
1272 call assert_equal('output file', getline(1))
1273
1274 call delete('Xinput')
1275 call delete('Xdiff')
1276 call delete('Xpatchexpr')
1277 set patchexpr&
1278 :%bwipe!
1279enddef
1280
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001281def Test_import_in_formatexpr()
1282 var lines =<< trim END
1283 vim9script
1284 export def MyFormatExpr(): number
1285 g:did_format = 'yes'
1286 return 0
1287 enddef
1288 END
1289 writefile(lines, 'Xformatter')
1290
1291 lines =<< trim END
1292 vim9script
1293 import './Xformatter' as format
1294 set formatexpr=format.MyFormatExpr()
1295 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001296 v9.CheckScriptSuccess(lines)
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001297
1298 new
1299 setline(1, ['a', 'b', 'c'])
1300 normal gqG
1301 assert_equal('yes', g:did_format)
1302
1303 bwipe!
1304 delete('Xformatter')
1305 unlet g:did_format
1306 set formatexpr=
1307enddef
1308
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001309def Test_import_in_includeexpr()
1310 writefile(['found it'], 'Xthisfile')
1311 new
1312
1313 var lines =<< trim END
1314 vim9script
1315 export def DoSub(): string
1316 return substitute(v:fname, 'that', 'this', '')
1317 enddef
1318 END
1319 writefile(lines, 'Xinclude.vim')
1320
1321 lines =<< trim END
1322 vim9script
1323 import './Xinclude.vim'
1324 set includeexpr=Xinclude.DoSub()
1325 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001326 v9.CheckScriptSuccess(lines)
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001327
1328 setline(1, ['Xthatfile'])
1329 exe "normal \<C-W>f"
1330 assert_equal('Xthisfile', expand('%'))
1331
1332 bwipe!
1333 bwipe!
1334 set includeexpr=
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001335 delete('Xinclude.vim')
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001336 delete('Xthisfile')
1337enddef
1338
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001339def Test_import_in_indentexpr()
1340 var lines =<< trim END
1341 vim9script
1342 export def GetIndent(): number
1343 return 5
1344 enddef
1345 END
1346 writefile(lines, 'Xindenter')
1347
1348 lines =<< trim END
1349 vim9script
1350 import './Xindenter' as indent
1351 set indentexpr=indent.GetIndent()
1352 set debug=throw
1353 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001354 v9.CheckScriptSuccess(lines)
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001355
1356 new
1357 setline(1, 'hello')
1358 normal ==
1359 assert_equal(' hello', getline(1))
1360
1361 bwipe!
1362 set indentexpr= debug=
1363 delete('Xindenter')
1364enddef
1365
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001366func Test_import_in_printexpr()
1367 CheckFeature postscript
1368 call Run_Test_import_in_printexpr()
1369endfunc
1370
1371def Run_Test_import_in_printexpr()
1372 var lines =<< trim END
1373 vim9script
1374 export def PrintFile(): bool
1375 g:printed = 'yes'
1376 delete('v:fname_in')
1377 return false
1378 enddef
1379 END
1380 writefile(lines, 'Xprint.vim')
1381
1382 lines =<< trim END
1383 vim9script
1384 import './Xprint.vim'
1385 set printexpr=Xprint.PrintFile()
1386 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001387 v9.CheckScriptSuccess(lines)
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001388
1389 help
1390 hardcopy dummy args
1391 assert_equal('yes', g:printed)
1392
1393 delete('Xprint.vim')
1394 set printexpr=
1395enddef
1396
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001397def Test_import_in_charconvert()
1398 var lines =<< trim END
1399 vim9script
1400 export def MakeUpper(): bool
1401 var data = readfile(v:fname_in)
1402 map(data, 'toupper(v:val)')
1403 writefile(data, v:fname_out)
1404 return false # success
1405 enddef
1406 END
1407 writefile(lines, 'Xconvert.vim')
1408
1409 lines =<< trim END
1410 vim9script
1411 import './Xconvert.vim' as conv
1412 set charconvert=conv.MakeUpper()
1413 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001414 v9.CheckScriptSuccess(lines)
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001415
1416 writefile(['one', 'two'], 'Xfile')
1417 new Xfile
1418 write ++enc=ucase Xfile1
1419 assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1420
1421 delete('Xfile')
1422 delete('Xfile1')
1423 delete('Xconvert.vim')
1424 bwipe!
1425 set charconvert&
1426enddef
1427
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001428func Test_import_in_spellsuggest_expr()
1429 CheckFeature spell
1430 call Run_Test_import_in_spellsuggest_expr()
1431endfunc
1432
1433def Run_Test_import_in_spellsuggest_expr()
1434 var lines =<< trim END
1435 vim9script
1436 export def MySuggest(): list<any>
1437 return [['Fox', 8], ['Fop', 9]]
1438 enddef
1439 END
1440 writefile(lines, 'Xsuggest.vim')
1441
1442 lines =<< trim END
1443 vim9script
1444 import './Xsuggest.vim' as sugg
1445 set spell spellsuggest=expr:sugg.MySuggest()
1446 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001447 v9.CheckScriptSuccess(lines)
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001448
1449 set verbose=1 # report errors
1450 call assert_equal(['Fox', 'Fop'], spellsuggest('Fo', 2))
1451
1452 delete('Xsuggest.vim')
1453 set nospell spellsuggest& verbose=0
1454enddef
1455
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001456def Test_export_shadows_global_function()
1457 mkdir('Xdir/autoload', 'p')
1458 var save_rtp = &rtp
1459 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1460
1461 var lines =<< trim END
1462 vim9script
1463 export def Shadow(): string
1464 return 'Shadow()'
1465 enddef
1466 END
1467 writefile(lines, 'Xdir/autoload/shadow.vim')
1468
1469 lines =<< trim END
1470 vim9script
1471
1472 def g:Shadow(): string
1473 return 'global'
1474 enddef
1475
1476 import autoload 'shadow.vim'
1477 assert_equal('Shadow()', shadow.Shadow())
1478 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001479 v9.CheckScriptSuccess(lines)
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001480
1481 delfunc g:Shadow
1482 bwipe!
1483 delete('Xdir', 'rf')
1484 &rtp = save_rtp
1485enddef
1486
Bram Moolenaard8448622022-01-07 21:39:52 +00001487def Test_export_fails()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001488 v9.CheckScriptFailure(['export var some = 123'], 'E1042:')
1489 v9.CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
1490 v9.CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +01001491 v9.CheckScriptFailure(['vim9script', 'export function /a1b2c3'], 'E1044:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001492
1493 assert_fails('export something', 'E1043:')
1494enddef
1495
1496func Test_import_fails_without_script()
1497 CheckRunVimInTerminal
1498
1499 " call indirectly to avoid compilation error for missing functions
1500 call Run_Test_import_fails_on_command_line()
1501endfunc
1502
1503def Run_Test_import_fails_on_command_line()
1504 var export =<< trim END
1505 vim9script
1506 export def Foo(): number
1507 return 0
1508 enddef
1509 END
1510 writefile(export, 'XexportCmd.vim')
1511
Bram Moolenaar62aec932022-01-29 21:45:34 +00001512 var buf = g:RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaard8448622022-01-07 21:39:52 +00001513 rows: 6, wait_for_ruler: 0})
Bram Moolenaar62aec932022-01-29 21:45:34 +00001514 g:WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaard8448622022-01-07 21:39:52 +00001515
1516 delete('XexportCmd.vim')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001517 g:StopVimInTerminal(buf)
Bram Moolenaard8448622022-01-07 21:39:52 +00001518enddef
1519
1520def Test_vim9_reload_noclear()
1521 var lines =<< trim END
1522 vim9script
1523 export var exported = 'thexport'
1524
1525 export def TheFunc(x = 0)
1526 enddef
1527 END
1528 writefile(lines, 'XExportReload')
1529 lines =<< trim END
1530 vim9script noclear
1531 g:loadCount += 1
Bram Moolenaara749a422022-02-12 19:52:25 +00001532 var reloaded = 'init'
Bram Moolenaard8448622022-01-07 21:39:52 +00001533 import './XExportReload' as exp
1534
1535 def Again(): string
1536 return 'again'
1537 enddef
1538
1539 exp.TheFunc()
1540
Bram Moolenaara749a422022-02-12 19:52:25 +00001541 if exists('loaded') | finish | endif
1542 var loaded = true
Bram Moolenaard8448622022-01-07 21:39:52 +00001543
Bram Moolenaara749a422022-02-12 19:52:25 +00001544 var notReloaded = 'yes'
1545 reloaded = 'first'
Bram Moolenaard8448622022-01-07 21:39:52 +00001546 def g:Values(): list<string>
Bram Moolenaara749a422022-02-12 19:52:25 +00001547 return [reloaded, notReloaded, Again(), Once(), exp.exported]
Bram Moolenaard8448622022-01-07 21:39:52 +00001548 enddef
1549
1550 def Once(): string
1551 return 'once'
1552 enddef
1553 END
1554 writefile(lines, 'XReloaded')
1555 g:loadCount = 0
1556 source XReloaded
1557 assert_equal(1, g:loadCount)
1558 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
1559 source XReloaded
1560 assert_equal(2, g:loadCount)
1561 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1562 source XReloaded
1563 assert_equal(3, g:loadCount)
1564 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1565
1566 delete('XReloaded')
1567 delete('XExportReload')
1568 delfunc g:Values
1569 unlet g:loadCount
1570
1571 lines =<< trim END
1572 vim9script
1573 def Inner()
1574 enddef
1575 END
1576 lines->writefile('XreloadScript.vim')
1577 source XreloadScript.vim
1578
1579 lines =<< trim END
1580 vim9script
1581 def Outer()
1582 def Inner()
1583 enddef
1584 enddef
1585 defcompile
1586 END
1587 lines->writefile('XreloadScript.vim')
1588 source XreloadScript.vim
1589
1590 delete('XreloadScript.vim')
1591enddef
1592
Bram Moolenaarcd1cda22022-02-16 21:48:25 +00001593def Test_vim_reload_noclear_arg_count()
1594 var lines =<< trim END
1595 vim9script noclear
1596
1597 if !exists('g:didload')
1598 def Test(a: string, b: string)
1599 echo a b
1600 enddef
1601 def Call()
1602 Test('a', 'b')
1603 enddef
1604 else
1605 # redefine with one argument less
1606 def Test(a: string)
1607 echo a
1608 enddef
1609 endif
1610 Call()
1611 g:didload = 1
1612 END
1613 lines->writefile('XreloadScript_1.vim')
1614 source XreloadScript_1.vim
1615 assert_fails('source XreloadScript_1.vim', 'E1106: One argument too many')
1616 unlet g:didload
1617
1618 lines =<< trim END
1619 vim9script noclear
1620
1621 if !exists('g:didload')
1622 def Test(a: string, b: string, c: string)
1623 echo a b
1624 enddef
1625 def Call()
1626 Test('a', 'b', 'c')
1627 enddef
1628 else
1629 # redefine with one argument less
1630 def Test(a: string)
1631 echo a
1632 enddef
1633 endif
1634 Call()
1635 g:didload = 1
1636 END
1637 lines->writefile('XreloadScript_2.vim')
1638 source XreloadScript_2.vim
1639 assert_fails('source XreloadScript_2.vim', 'E1106: 2 arguments too many')
1640 unlet g:didload
1641
1642 lines =<< trim END
1643 vim9script noclear
1644
1645 if !exists('g:didload')
1646 def Test(a: string)
1647 echo a
1648 enddef
1649 def Call()
1650 Test('a')
1651 enddef
1652 else
1653 # redefine with one argument extra
1654 def Test(a: string, b: string)
1655 echo a b
1656 enddef
1657 endif
1658 Call()
1659 g:didload = 1
1660 END
1661 lines->writefile('XreloadScript_3.vim')
1662 source XreloadScript_3.vim
1663 assert_fails('source XreloadScript_3.vim', 'E1190: One argument too few')
1664 unlet g:didload
1665
1666 lines =<< trim END
1667 vim9script noclear
1668
1669 if !exists('g:didload')
1670 def Test(a: string)
1671 echo a
1672 enddef
1673 def Call()
1674 Test('a')
1675 enddef
1676 else
1677 # redefine with two arguments extra
1678 def Test(a: string, b: string, c: string)
1679 echo a b
1680 enddef
1681 endif
1682 Call()
1683 g:didload = 1
1684 END
1685 lines->writefile('XreloadScript_4.vim')
1686 source XreloadScript_4.vim
1687 assert_fails('source XreloadScript_4.vim', 'E1190: 2 arguments too few')
1688 unlet g:didload
1689
1690 delete('XreloadScript_1.vim')
1691 delete('XreloadScript_2.vim')
1692 delete('XreloadScript_3.vim')
1693 delete('XreloadScript_4.vim')
1694enddef
1695
1696def Test_vim9_reload_noclear_error()
1697 var lines =<< trim END
1698 vim9script noclear
1699
1700 if !exists('g:didload')
1701 def Test(a: string)
1702 echo a
1703 enddef
1704 def Call()
1705 Test('a')
1706 enddef
1707 else
1708 # redefine with a compile error
1709 def Test(a: string)
1710 echo ax
1711 enddef
1712 endif
1713 Call()
1714 g:didload = 1
1715 END
1716 lines->writefile('XreloadScriptErr.vim')
1717 source XreloadScriptErr.vim
1718 assert_fails('source XreloadScriptErr.vim', 'E1001: Variable not found: ax')
1719
1720 unlet g:didload
1721 delete('XreloadScriptErr.vim')
1722enddef
1723
Bram Moolenaard8448622022-01-07 21:39:52 +00001724def Test_vim9_reload_import()
1725 var lines =<< trim END
1726 vim9script
1727 const var = ''
1728 var valone = 1234
1729 def MyFunc(arg: string)
1730 valone = 5678
1731 enddef
1732 END
1733 var morelines =<< trim END
1734 var valtwo = 222
1735 export def GetValtwo(): number
1736 return valtwo
1737 enddef
1738 END
1739 writefile(lines + morelines, 'Xreload.vim')
1740 source Xreload.vim
1741 source Xreload.vim
1742 source Xreload.vim
1743
1744 # cannot declare a var twice
1745 lines =<< trim END
1746 vim9script
1747 var valone = 1234
1748 var valone = 5678
1749 END
1750 writefile(lines, 'Xreload.vim')
1751 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
1752
1753 delete('Xreload.vim')
1754 delete('Ximport.vim')
1755enddef
1756
1757" if a script is reloaded with a script-local variable that changed its type, a
1758" compiled function using that variable must fail.
1759def Test_script_reload_change_type()
1760 var lines =<< trim END
1761 vim9script noclear
1762 var str = 'string'
1763 def g:GetStr(): string
1764 return str .. 'xxx'
1765 enddef
1766 END
1767 writefile(lines, 'Xreload.vim')
1768 source Xreload.vim
1769 echo g:GetStr()
1770
1771 lines =<< trim END
1772 vim9script noclear
1773 var str = 1234
1774 END
1775 writefile(lines, 'Xreload.vim')
1776 source Xreload.vim
1777 assert_fails('echo g:GetStr()', 'E1150:')
1778
1779 delfunc g:GetStr
1780 delete('Xreload.vim')
1781enddef
1782
1783" Define CallFunc so that the test can be compiled
1784command CallFunc echo 'nop'
1785
1786def Test_script_reload_from_function()
1787 var lines =<< trim END
1788 vim9script
1789
Bram Moolenaar10611952022-04-03 21:11:34 +01001790 if exists('g:loadedThis')
Bram Moolenaard8448622022-01-07 21:39:52 +00001791 finish
1792 endif
Bram Moolenaar10611952022-04-03 21:11:34 +01001793 g:loadedThis = 1
Bram Moolenaard8448622022-01-07 21:39:52 +00001794 delcommand CallFunc
1795 command CallFunc Func()
1796 def Func()
1797 so XreloadFunc.vim
1798 g:didTheFunc = 1
1799 enddef
1800 END
1801 writefile(lines, 'XreloadFunc.vim')
1802 source XreloadFunc.vim
1803 CallFunc
1804 assert_equal(1, g:didTheFunc)
1805
1806 delete('XreloadFunc.vim')
1807 delcommand CallFunc
Bram Moolenaar10611952022-04-03 21:11:34 +01001808 unlet g:loadedThis
Bram Moolenaard8448622022-01-07 21:39:52 +00001809 unlet g:didTheFunc
1810enddef
1811
1812def s:RetSome(): string
1813 return 'some'
1814enddef
1815
1816" Not exported function that is referenced needs to be accessed by the
1817" script-local name.
1818def Test_vim9_funcref()
1819 var sortlines =<< trim END
1820 vim9script
1821 def Compare(i1: number, i2: number): number
1822 return i2 - i1
1823 enddef
1824
1825 export def FastSort(): list<number>
1826 return range(5)->sort(Compare)
1827 enddef
1828
1829 export def GetString(arg: string): string
1830 return arg
1831 enddef
1832 END
1833 writefile(sortlines, 'Xsort.vim')
1834
1835 var lines =<< trim END
1836 vim9script
1837 import './Xsort.vim'
1838 def Test()
1839 g:result = Xsort.FastSort()
1840 enddef
1841 Test()
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001842 END
1843 writefile(lines, 'Xscript.vim')
1844 source Xscript.vim
1845 assert_equal([4, 3, 2, 1, 0], g:result)
1846 unlet g:result
Bram Moolenaard8448622022-01-07 21:39:52 +00001847
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001848 lines =<< trim END
1849 vim9script
Bram Moolenaard8448622022-01-07 21:39:52 +00001850 # using a function imported with "as"
1851 import './Xsort.vim' as anAlias
1852 assert_equal('yes', anAlias.GetString('yes'))
1853
1854 # using the function from a compiled function
1855 def TestMore(): string
1856 var s = s:anAlias.GetString('foo')
1857 return s .. anAlias.GetString('bar')
1858 enddef
1859 assert_equal('foobar', TestMore())
1860
1861 # error when using a function that isn't exported
1862 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
1863 END
1864 writefile(lines, 'Xscript.vim')
1865
Bram Moolenaard8448622022-01-07 21:39:52 +00001866 delete('Xsort.vim')
1867 delete('Xscript.vim')
1868
1869 var Funcref = function('s:RetSome')
1870 assert_equal('some', Funcref())
1871enddef
1872
1873" Check that when searching for "FilterFunc" it finds the import in the
1874" script where FastFilter() is called from, both as a string and as a direct
1875" function reference.
1876def Test_vim9_funcref_other_script()
1877 var filterLines =<< trim END
1878 vim9script
1879 export def FilterFunc(idx: number, val: number): bool
1880 return idx % 2 == 1
1881 enddef
1882 export def FastFilter(): list<number>
1883 return range(10)->filter('FilterFunc(v:key, v:val)')
1884 enddef
1885 export def FastFilterDirect(): list<number>
1886 return range(10)->filter(FilterFunc)
1887 enddef
1888 END
1889 writefile(filterLines, 'Xfilter.vim')
1890
1891 var lines =<< trim END
1892 vim9script
1893 import './Xfilter.vim' as filter
1894 def Test()
1895 var x: list<number> = filter.FastFilter()
1896 enddef
1897 Test()
1898 def TestDirect()
1899 var x: list<number> = filter.FastFilterDirect()
1900 enddef
1901 TestDirect()
1902 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001903 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +00001904 delete('Xfilter.vim')
1905enddef
1906
1907def Test_import_absolute()
1908 var import_lines = [
1909 'vim9script',
1910 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
1911 'def UseExported()',
1912 ' g:imported_abs = abs.exported',
1913 ' abs.exported = 8888',
1914 ' g:imported_after = abs.exported',
1915 'enddef',
1916 'UseExported()',
1917 'g:import_disassembled = execute("disass UseExported")',
1918 ]
1919 writefile(import_lines, 'Ximport_abs.vim')
1920 writefile(s:export_script_lines, 'Xexport_abs.vim')
1921
1922 source Ximport_abs.vim
1923
1924 assert_equal(9876, g:imported_abs)
1925 assert_equal(8888, g:imported_after)
1926 assert_match('<SNR>\d\+_UseExported\_s*' ..
1927 'g:imported_abs = abs.exported\_s*' ..
1928 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1929 '1 STOREG g:imported_abs\_s*' ..
1930 'abs.exported = 8888\_s*' ..
1931 '2 PUSHNR 8888\_s*' ..
1932 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1933 'g:imported_after = abs.exported\_s*' ..
1934 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1935 '5 STOREG g:imported_after',
1936 g:import_disassembled)
1937
1938 Undo_export_script_lines()
1939 unlet g:imported_abs
1940 unlet g:import_disassembled
1941
1942 delete('Ximport_abs.vim')
1943 delete('Xexport_abs.vim')
1944enddef
1945
1946def Test_import_rtp()
1947 var import_lines = [
1948 'vim9script',
1949 'import "Xexport_rtp.vim" as rtp',
1950 'g:imported_rtp = rtp.exported',
1951 ]
1952 writefile(import_lines, 'Ximport_rtp.vim')
1953 mkdir('import', 'p')
1954 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1955
1956 var save_rtp = &rtp
1957 &rtp = getcwd()
1958 source Ximport_rtp.vim
1959 &rtp = save_rtp
1960
1961 assert_equal(9876, g:imported_rtp)
1962
1963 Undo_export_script_lines()
1964 unlet g:imported_rtp
1965 delete('Ximport_rtp.vim')
1966 delete('import', 'rf')
1967enddef
1968
1969def Test_import_compile_error()
1970 var export_lines = [
1971 'vim9script',
1972 'export def ExpFunc(): string',
1973 ' return notDefined',
1974 'enddef',
1975 ]
1976 writefile(export_lines, 'Xexported.vim')
1977
1978 var import_lines = [
1979 'vim9script',
1980 'import "./Xexported.vim" as expo',
1981 'def ImpFunc()',
1982 ' echo expo.ExpFunc()',
1983 'enddef',
1984 'defcompile',
1985 ]
1986 writefile(import_lines, 'Ximport.vim')
1987
1988 try
1989 source Ximport.vim
1990 catch /E1001/
1991 # Error should be before the Xexported.vim file.
1992 assert_match('E1001: Variable not found: notDefined', v:exception)
1993 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1994 endtry
1995
1996 delete('Xexported.vim')
1997 delete('Ximport.vim')
1998enddef
1999
2000def Test_func_overrules_import_fails()
2001 var export_lines =<< trim END
2002 vim9script
2003 export def Func()
2004 echo 'imported'
2005 enddef
2006 END
2007 writefile(export_lines, 'XexportedFunc.vim')
2008
2009 var lines =<< trim END
2010 vim9script
2011 import './XexportedFunc.vim' as Func
2012 def Func()
2013 echo 'local to function'
2014 enddef
2015 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002016 v9.CheckScriptFailure(lines, 'E1213: Redefining imported item "Func"')
Bram Moolenaard8448622022-01-07 21:39:52 +00002017
2018 lines =<< trim END
2019 vim9script
2020 import './XexportedFunc.vim' as Func
2021 def Outer()
2022 def Func()
2023 echo 'local to function'
2024 enddef
2025 enddef
2026 defcompile
2027 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002028 v9.CheckScriptFailure(lines, 'E1236:')
Bram Moolenaard8448622022-01-07 21:39:52 +00002029
2030 delete('XexportedFunc.vim')
2031enddef
2032
2033def Test_source_vim9_from_legacy()
2034 var vim9_lines =<< trim END
2035 vim9script
2036 var local = 'local'
2037 g:global = 'global'
2038 export var exported = 'exported'
2039 export def GetText(): string
2040 return 'text'
2041 enddef
2042 END
2043 writefile(vim9_lines, 'Xvim9_script.vim')
2044
2045 var legacy_lines =<< trim END
2046 source Xvim9_script.vim
2047
2048 call assert_false(exists('local'))
2049 call assert_false(exists('exported'))
2050 call assert_false(exists('s:exported'))
2051 call assert_equal('global', global)
2052 call assert_equal('global', g:global)
Bram Moolenaard8448622022-01-07 21:39:52 +00002053 END
2054 writefile(legacy_lines, 'Xlegacy_script.vim')
2055
2056 source Xlegacy_script.vim
2057 assert_equal('global', g:global)
2058 unlet g:global
2059
2060 delete('Xlegacy_script.vim')
2061 delete('Xvim9_script.vim')
2062enddef
2063
Bram Moolenaarc43e6232022-01-13 20:51:56 +00002064def Test_import_vim9_from_legacy()
2065 var vim9_lines =<< trim END
2066 vim9script
2067 var local = 'local'
2068 g:global = 'global'
2069 export var exported = 'exported'
2070 export def GetText(): string
2071 return 'text'
2072 enddef
2073 END
2074 writefile(vim9_lines, 'Xvim9_export.vim')
2075
2076 var legacy_lines =<< trim END
2077 import './Xvim9_export.vim' as vim9
2078
2079 call assert_false(exists('vim9'))
2080 call assert_false(exists('local'))
2081 call assert_false(exists('s:vim9.local'))
2082 call assert_equal('global', global)
2083 call assert_equal('global', g:global)
2084 call assert_false(exists('exported'))
2085 call assert_false(exists('s:exported'))
2086 call assert_false(exists('*GetText'))
2087
2088 " imported symbol is script-local
2089 call assert_equal('exported', s:vim9.exported)
2090 call assert_equal('text', s:vim9.GetText())
2091 END
2092 writefile(legacy_lines, 'Xlegacy_script.vim')
2093
2094 source Xlegacy_script.vim
2095 assert_equal('global', g:global)
2096 unlet g:global
2097
2098 delete('Xlegacy_script.vim')
2099 delete('Xvim9_export.vim')
2100enddef
2101
Bram Moolenaard8448622022-01-07 21:39:52 +00002102def Test_cmdline_win()
2103 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
2104 # the command line window.
2105 mkdir('rtp/syntax', 'p')
2106 var export_lines =<< trim END
2107 vim9script
2108 export var That = 'yes'
2109 END
2110 writefile(export_lines, 'rtp/syntax/Xexport.vim')
2111 var import_lines =<< trim END
2112 vim9script
2113 import './Xexport.vim' as exp
2114 echo exp.That
2115 END
2116 writefile(import_lines, 'rtp/syntax/vim.vim')
2117 var save_rtp = &rtp
2118 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
2119 syntax on
2120 augroup CmdWin
2121 autocmd CmdwinEnter * g:got_there = 'yes'
2122 augroup END
2123 # this will open and also close the cmdline window
2124 feedkeys('q:', 'xt')
2125 assert_equal('yes', g:got_there)
2126
2127 augroup CmdWin
2128 au!
2129 augroup END
2130 &rtp = save_rtp
2131 delete('rtp', 'rf')
2132enddef
2133
2134def Test_import_gone_when_sourced_twice()
2135 var exportlines =<< trim END
2136 vim9script
2137 if exists('g:guard')
2138 finish
2139 endif
2140 g:guard = 1
2141 export var name = 'someName'
2142 END
2143 writefile(exportlines, 'XexportScript.vim')
2144
2145 var lines =<< trim END
2146 vim9script
2147 import './XexportScript.vim' as expo
2148 def g:GetName(): string
2149 return expo.name
2150 enddef
2151 END
2152 writefile(lines, 'XscriptImport.vim')
2153 so XscriptImport.vim
2154 assert_equal('someName', g:GetName())
2155
2156 so XexportScript.vim
2157 assert_fails('call g:GetName()', 'E1149:')
2158
2159 delfunc g:GetName
2160 delete('XexportScript.vim')
2161 delete('XscriptImport.vim')
2162 unlet g:guard
2163enddef
2164
Bram Moolenaar160aa862022-01-10 21:29:57 +00002165" test using an auto-loaded function and variable
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002166def Test_vim9_autoload_full_name()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002167 var lines =<< trim END
2168 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002169 export def Gettest(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002170 return 'test'
2171 enddef
2172 g:some#name = 'name'
2173 g:some#dict = {key: 'value'}
2174
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002175 export def Varargs(a1: string, ...l: list<string>): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002176 return a1 .. l[0] .. l[1]
2177 enddef
2178 END
2179
2180 mkdir('Xdir/autoload', 'p')
2181 writefile(lines, 'Xdir/autoload/some.vim')
2182 var save_rtp = &rtp
2183 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2184
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002185 assert_equal('test', g:some#Gettest())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002186 assert_equal('name', g:some#name)
2187 assert_equal('value', g:some#dict.key)
2188 g:some#other = 'other'
2189 assert_equal('other', g:some#other)
2190
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002191 assert_equal('abc', some#Varargs('a', 'b', 'c'))
Bram Moolenaar160aa862022-01-10 21:29:57 +00002192
2193 # upper case script name works
2194 lines =<< trim END
2195 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002196 export def GetOther(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002197 return 'other'
2198 enddef
2199 END
2200 writefile(lines, 'Xdir/autoload/Other.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002201 assert_equal('other', g:Other#GetOther())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002202
2203 delete('Xdir', 'rf')
2204 &rtp = save_rtp
2205enddef
2206
2207def Test_vim9script_autoload()
2208 mkdir('Xdir/autoload', 'p')
2209 var save_rtp = &rtp
2210 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2211
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002212 # when the path has "/autoload/" prefix is not needed
Bram Moolenaar160aa862022-01-10 21:29:57 +00002213 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002214 vim9script
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002215 g:prefixed_loaded += 1
Bram Moolenaar160aa862022-01-10 21:29:57 +00002216
2217 export def Gettest(): string
2218 return 'test'
2219 enddef
2220
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002221 export var name = 'name'
2222
2223 export func GetFunc()
2224 return Gettest() .. 'more' .. s:name
Bram Moolenaar160aa862022-01-10 21:29:57 +00002225 endfunc
2226
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002227 export def GetDef(): string
2228 return Gettest() .. 'more' .. name
2229 enddef
2230
Bram Moolenaar160aa862022-01-10 21:29:57 +00002231 export final fname = 'final'
2232 export const cname = 'const'
2233 END
2234 writefile(lines, 'Xdir/autoload/prefixed.vim')
2235
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002236 g:prefixed_loaded = 0
2237 g:expected_loaded = 0
Bram Moolenaar160aa862022-01-10 21:29:57 +00002238 lines =<< trim END
2239 vim9script
2240 import autoload 'prefixed.vim'
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002241 assert_equal(g:expected_loaded, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002242 assert_equal('test', prefixed.Gettest())
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002243 assert_equal(1, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002244
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002245 assert_equal('testmorename', prefixed.GetFunc())
2246 assert_equal('testmorename', prefixed.GetDef())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002247 assert_equal('name', prefixed.name)
2248 assert_equal('final', prefixed.fname)
2249 assert_equal('const', prefixed.cname)
2250 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002251 v9.CheckScriptSuccess(lines)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002252 # can source it again, autoload script not loaded again
2253 g:expected_loaded = 1
Bram Moolenaar62aec932022-01-29 21:45:34 +00002254 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002255
2256 # can also get the items by autoload name
2257 lines =<< trim END
2258 call assert_equal('test', prefixed#Gettest())
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002259 call assert_equal('testmorename', prefixed#GetFunc())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002260 call assert_equal('name', prefixed#name)
2261 call assert_equal('final', prefixed#fname)
2262 call assert_equal('const', prefixed#cname)
2263 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002264 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002265
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002266 unlet g:prefixed_loaded
2267 unlet g:expected_loaded
2268 delete('Xdir', 'rf')
2269 &rtp = save_rtp
2270enddef
2271
Bram Moolenaard02dce22022-01-18 17:43:04 +00002272def Test_import_autoload_not_exported()
2273 mkdir('Xdir/autoload', 'p')
2274 var save_rtp = &rtp
2275 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2276
2277 # error when using an item that is not exported from an autoload script
2278 var exportLines =<< trim END
2279 vim9script
2280 var notExported = 123
2281 def NotExport()
2282 echo 'nop'
2283 enddef
2284 END
2285 writefile(exportLines, 'Xdir/autoload/notExport1.vim')
2286
2287 var lines =<< trim END
2288 vim9script
2289 import autoload 'notExport1.vim'
2290 echo notExport1.notFound
2291 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002292 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002293
2294 lines =<< trim END
2295 vim9script
2296 import autoload 'notExport1.vim'
2297 echo notExport1.notExported
2298 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002299 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002300
2301 lines =<< trim END
2302 vim9script
2303 import autoload 'notExport1.vim'
2304 echo notExport1.NotFunc()
2305 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002306 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002307
2308 lines =<< trim END
2309 vim9script
2310 import autoload 'notExport1.vim'
2311 echo notExport1.NotExport()
2312 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002313 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002314
2315 lines =<< trim END
2316 vim9script
2317 import autoload 'notExport1.vim'
2318 echo 'text'->notExport1.NotFunc()
2319 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002320 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002321
2322 lines =<< trim END
2323 vim9script
2324 import autoload 'notExport1.vim'
2325 echo 'text'->notExport1.NotExport()
2326 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002327 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002328
2329 # using a :def function we use a different autoload script every time so that
2330 # the function is compiled without the script loaded
2331 writefile(exportLines, 'Xdir/autoload/notExport2.vim')
2332 lines =<< trim END
2333 vim9script
2334 import autoload 'notExport2.vim'
2335 def Testit()
2336 echo notExport2.notFound
2337 enddef
2338 Testit()
2339 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002340 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport2#notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002341
2342 writefile(exportLines, 'Xdir/autoload/notExport3.vim')
2343 lines =<< trim END
2344 vim9script
2345 import autoload 'notExport3.vim'
2346 def Testit()
2347 echo notExport3.notExported
2348 enddef
2349 Testit()
2350 END
2351 # don't get E1049 because it is too complicated to figure out
Bram Moolenaar62aec932022-01-29 21:45:34 +00002352 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport3#notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002353
2354 writefile(exportLines, 'Xdir/autoload/notExport4.vim')
2355 lines =<< trim END
2356 vim9script
2357 import autoload 'notExport4.vim'
2358 def Testit()
2359 echo notExport4.NotFunc()
2360 enddef
2361 Testit()
2362 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002363 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport4#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002364
2365 writefile(exportLines, 'Xdir/autoload/notExport5.vim')
2366 lines =<< trim END
2367 vim9script
2368 import autoload 'notExport5.vim'
2369 def Testit()
2370 echo notExport5.NotExport()
2371 enddef
2372 Testit()
2373 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002374 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport5#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002375
2376 writefile(exportLines, 'Xdir/autoload/notExport6.vim')
2377 lines =<< trim END
2378 vim9script
2379 import autoload 'notExport6.vim'
2380 def Testit()
2381 echo 'text'->notExport6.NotFunc()
2382 enddef
2383 Testit()
2384 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002385 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport6#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002386
2387 writefile(exportLines, 'Xdir/autoload/notExport7.vim')
2388 lines =<< trim END
2389 vim9script
2390 import autoload 'notExport7.vim'
2391 def Testit()
2392 echo 'text'->notExport7.NotExport()
2393 enddef
2394 Testit()
2395 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002396 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport7#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002397
2398 delete('Xdir', 'rf')
2399 &rtp = save_rtp
2400enddef
2401
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002402def Test_vim9script_autoload_call()
2403 mkdir('Xdir/autoload', 'p')
2404 var save_rtp = &rtp
2405 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2406
2407 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002408 vim9script
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002409
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002410 export def RetArg(arg: string): string
2411 return arg
2412 enddef
2413
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002414 export def Getother()
2415 g:result = 'other'
2416 enddef
2417 END
Bram Moolenaar5d982692022-01-12 15:15:27 +00002418 writefile(lines, 'Xdir/autoload/another.vim')
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002419
2420 lines =<< trim END
2421 vim9script
Bram Moolenaar5d982692022-01-12 15:15:27 +00002422 import autoload 'another.vim'
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002423
2424 # compile this before 'another.vim' is loaded
2425 def CallAnother()
2426 assert_equal('foo', 'foo'->another.RetArg())
2427 enddef
2428 CallAnother()
2429
Bram Moolenaar5d982692022-01-12 15:15:27 +00002430 call another.Getother()
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002431 assert_equal('other', g:result)
Bram Moolenaar3d8e25a2022-01-22 11:00:02 +00002432
2433 assert_equal('arg', call('another.RetArg', ['arg']))
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00002434
2435 verbose function another.Getother
2436 # should we disallow this?
2437 verbose function another#Getother
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002438 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002439 v9.CheckScriptSuccess(lines)
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002440
2441 unlet g:result
Bram Moolenaar160aa862022-01-10 21:29:57 +00002442 delete('Xdir', 'rf')
2443 &rtp = save_rtp
2444enddef
2445
Bram Moolenaarb697dc22022-01-22 11:27:29 +00002446def Test_vim9script_noclear_autoload()
2447 mkdir('Xdir/autoload', 'p')
2448 var save_rtp = &rtp
2449 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2450
2451 var lines =<< trim END
2452 vim9script
2453 export def Func(): string
2454 return 'called'
2455 enddef
2456 g:double_loaded = 'yes'
2457 END
2458 writefile(lines, 'Xdir/autoload/double.vim')
2459
2460 lines =<< trim END
2461 vim9script noclear
2462 if exists('g:script_loaded')
2463 finish
2464 endif
2465 g:script_loaded = true
2466
2467 import autoload 'double.vim'
2468 nnoremap <F3> <ScriptCmd>g:result = double.Func()<CR>
2469 END
2470 g:double_loaded = 'no'
2471 writefile(lines, 'Xloaddouble')
2472 source Xloaddouble
2473 assert_equal('no', g:double_loaded)
2474 assert_equal(true, g:script_loaded)
2475 source Xloaddouble
2476 feedkeys("\<F3>", 'xt')
2477 assert_equal('called', g:result)
2478 assert_equal('yes', g:double_loaded)
2479
2480 delete('Xloaddouble')
2481 unlet g:double_loaded
2482 unlet g:script_loaded
2483 unlet g:result
2484 delete('Xdir', 'rf')
2485 &rtp = save_rtp
2486enddef
2487
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002488def Test_vim9script_autoload_duplicate()
2489 mkdir('Xdir/autoload', 'p')
2490
2491 var lines =<< trim END
2492 vim9script
2493
2494 export def Func()
2495 enddef
2496
2497 def Func()
2498 enddef
2499 END
2500 writefile(lines, 'Xdir/autoload/dupfunc.vim')
2501 assert_fails('source Xdir/autoload/dupfunc.vim', 'E1073:')
2502
2503 lines =<< trim END
2504 vim9script
2505
2506 def Func()
2507 enddef
2508
2509 export def Func()
2510 enddef
2511 END
2512 writefile(lines, 'Xdir/autoload/dup2func.vim')
2513 assert_fails('source Xdir/autoload/dup2func.vim', 'E1073:')
2514
2515 lines =<< trim END
2516 vim9script
2517
2518 def Func()
2519 enddef
2520
2521 export var Func = 'asdf'
2522 END
2523 writefile(lines, 'Xdir/autoload/dup3func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002524 assert_fails('source Xdir/autoload/dup3func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002525
2526 lines =<< trim END
2527 vim9script
2528
2529 export var Func = 'asdf'
2530
2531 def Func()
2532 enddef
2533 END
2534 writefile(lines, 'Xdir/autoload/dup4func.vim')
2535 assert_fails('source Xdir/autoload/dup4func.vim', 'E707:')
2536
2537 lines =<< trim END
2538 vim9script
2539
2540 var Func = 'asdf'
2541
2542 export def Func()
2543 enddef
2544 END
2545 writefile(lines, 'Xdir/autoload/dup5func.vim')
2546 assert_fails('source Xdir/autoload/dup5func.vim', 'E707:')
2547
2548 lines =<< trim END
2549 vim9script
2550
2551 export def Func()
2552 enddef
2553
2554 var Func = 'asdf'
2555 END
2556 writefile(lines, 'Xdir/autoload/dup6func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002557 assert_fails('source Xdir/autoload/dup6func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002558
2559 delete('Xdir', 'rf')
2560enddef
2561
Bram Moolenaar2017d6f2022-01-20 19:38:46 +00002562def Test_autoload_missing_function_name()
2563 mkdir('Xdir/autoload', 'p')
2564
2565 var lines =<< trim END
2566 vim9script
2567
2568 def loadme#()
2569 enddef
2570 END
2571 writefile(lines, 'Xdir/autoload/loadme.vim')
2572 assert_fails('source Xdir/autoload/loadme.vim', 'E129:')
2573
2574 delete('Xdir', 'rf')
2575enddef
2576
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002577def Test_autoload_name_wrong()
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002578 var lines =<< trim END
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002579 def Xscriptname#Func()
2580 enddef
2581 END
2582 writefile(lines, 'Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002583 v9.CheckScriptFailure(lines, 'E746:')
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00002584 delete('Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002585
2586 mkdir('Xdir/autoload', 'p')
2587 lines =<< trim END
2588 vim9script
2589 def somescript#Func()
2590 enddef
2591 END
2592 writefile(lines, 'Xdir/autoload/somescript.vim')
2593 assert_fails('source Xdir/autoload/somescript.vim', 'E1263:')
2594
2595 delete('Xdir', 'rf')
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002596enddef
2597
Bram Moolenaard041f422022-01-12 19:54:00 +00002598def Test_import_autoload_postponed()
2599 mkdir('Xdir/autoload', 'p')
2600 var save_rtp = &rtp
2601 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2602
2603 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002604 vim9script
Bram Moolenaard041f422022-01-12 19:54:00 +00002605
2606 g:loaded_postponed = 'true'
2607 export var variable = 'bla'
2608 export def Function(): string
2609 return 'bla'
2610 enddef
2611 END
2612 writefile(lines, 'Xdir/autoload/postponed.vim')
2613
2614 lines =<< trim END
2615 vim9script
2616
2617 import autoload 'postponed.vim'
2618 def Tryit()
2619 echo postponed.variable
2620 echo postponed.Function()
2621 enddef
2622 defcompile
2623 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002624 v9.CheckScriptSuccess(lines)
Bram Moolenaard041f422022-01-12 19:54:00 +00002625 assert_false(exists('g:loaded_postponed'))
Bram Moolenaar62aec932022-01-29 21:45:34 +00002626 v9.CheckScriptSuccess(lines + ['Tryit()'])
Bram Moolenaard041f422022-01-12 19:54:00 +00002627 assert_equal('true', g:loaded_postponed)
2628
2629 unlet g:loaded_postponed
2630 delete('Xdir', 'rf')
2631 &rtp = save_rtp
2632enddef
2633
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002634def Test_import_autoload_override()
2635 mkdir('Xdir/autoload', 'p')
2636 var save_rtp = &rtp
2637 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2638 test_override('autoload', 1)
2639
2640 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002641 vim9script
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002642
2643 g:loaded_override = 'true'
2644 export var variable = 'bla'
2645 export def Function(): string
2646 return 'bla'
2647 enddef
2648 END
2649 writefile(lines, 'Xdir/autoload/override.vim')
2650
2651 lines =<< trim END
2652 vim9script
2653
2654 import autoload 'override.vim'
2655 assert_equal('true', g:loaded_override)
2656
2657 def Tryit()
2658 echo override.doesNotExist
2659 enddef
2660 defcompile
2661 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002662 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1)
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002663
2664 test_override('autoload', 0)
2665 unlet g:loaded_override
2666 delete('Xdir', 'rf')
2667 &rtp = save_rtp
2668enddef
2669
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002670def Test_autoload_mapping()
2671 mkdir('Xdir/autoload', 'p')
2672 var save_rtp = &rtp
2673 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2674
2675 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002676 vim9script
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002677
2678 g:toggle_loaded = 'yes'
2679
2680 export def Toggle(): string
2681 return ":g:toggle_called = 'yes'\<CR>"
2682 enddef
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002683 export def Doit()
2684 g:doit_called = 'yes'
2685 enddef
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002686 END
2687 writefile(lines, 'Xdir/autoload/toggle.vim')
2688
2689 lines =<< trim END
2690 vim9script
2691
2692 import autoload 'toggle.vim'
2693
2694 nnoremap <silent> <expr> tt toggle.Toggle()
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002695 nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR>
2696 nnoremap <silent> yy <Cmd>toggle.Doit()<CR>
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002697 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002698 v9.CheckScriptSuccess(lines)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002699 assert_false(exists("g:toggle_loaded"))
2700 assert_false(exists("g:toggle_called"))
Bram Moolenaar6079da72022-01-18 14:16:59 +00002701 assert_match('\d A: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002702
2703 feedkeys("tt", 'xt')
2704 assert_equal('yes', g:toggle_loaded)
2705 assert_equal('yes', g:toggle_called)
Bram Moolenaar6079da72022-01-18 14:16:59 +00002706 assert_match('\d: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002707
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002708 feedkeys("xx", 'xt')
2709 assert_equal('yes', g:doit_called)
2710
2711 assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle')
2712
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002713 nunmap tt
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002714 nunmap xx
2715 nunmap yy
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002716 unlet g:toggle_loaded
2717 unlet g:toggle_called
2718 delete('Xdir', 'rf')
2719 &rtp = save_rtp
2720enddef
2721
Bram Moolenaar160aa862022-01-10 21:29:57 +00002722def Test_vim9script_autoload_fails()
2723 var lines =<< trim END
2724 vim9script autoload
2725 var n = 0
2726 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002727 v9.CheckScriptFailure(lines, 'E475: Invalid argument: autoload')
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002728
2729 lines =<< trim END
2730 vim9script noclear noclear
2731 var n = 0
2732 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002733 v9.CheckScriptFailure(lines, 'E983: Duplicate argument: noclear')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +01002734
2735 lines =<< trim END
2736 vim9script noclears
2737 var n = 0
2738 END
2739 v9.CheckScriptFailure(lines, 'E475: Invalid argument: noclears')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002740enddef
2741
2742def Test_import_autoload_fails()
2743 var lines =<< trim END
2744 vim9script
2745 import autoload autoload 'prefixed.vim'
2746 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002747 v9.CheckScriptFailure(lines, 'E121: Undefined variable: autoload')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002748
2749 lines =<< trim END
2750 vim9script
Bram Moolenaar1836d612022-01-18 13:14:47 +00002751 import autoload './doesNotExist.vim'
Bram Moolenaar160aa862022-01-10 21:29:57 +00002752 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002753 v9.CheckScriptFailure(lines, 'E282:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002754
2755 lines =<< trim END
2756 vim9script
2757 import autoload '/dir/doesNotExist.vim'
2758 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002759 v9.CheckScriptFailure(lines, 'E282:', 2)
2760
2761 lines =<< trim END
2762 vim9script
2763 import autoload '../testdir'
2764 END
2765 v9.CheckScriptFailure(lines, 'E17:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002766
2767 lines =<< trim END
2768 vim9script
2769 import autoload 'doesNotExist.vim'
2770 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002771 v9.CheckScriptFailure(lines, 'E1053: Could not import "doesNotExist.vim"')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002772enddef
2773
2774" test disassembling an auto-loaded function starting with "debug"
2775def Test_vim9_autoload_disass()
2776 mkdir('Xdir/autoload', 'p')
2777 var save_rtp = &rtp
2778 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2779
2780 var lines =<< trim END
2781 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002782 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002783 return 'debug'
2784 enddef
2785 END
2786 writefile(lines, 'Xdir/autoload/debugit.vim')
2787
2788 lines =<< trim END
2789 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002790 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002791 return 'profile'
2792 enddef
2793 END
2794 writefile(lines, 'Xdir/autoload/profileit.vim')
2795
2796 lines =<< trim END
2797 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002798 assert_equal('debug', debugit#Test())
2799 disass debugit#Test
2800 assert_equal('profile', profileit#Test())
2801 disass profileit#Test
Bram Moolenaar160aa862022-01-10 21:29:57 +00002802 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002803 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002804
2805 delete('Xdir', 'rf')
2806 &rtp = save_rtp
2807enddef
2808
2809" test using a vim9script that is auto-loaded from an autocmd
2810def Test_vim9_aucmd_autoload()
2811 var lines =<< trim END
2812 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002813 export def Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002814 echomsg getreg('"')
2815 enddef
2816 END
2817
2818 mkdir('Xdir/autoload', 'p')
2819 writefile(lines, 'Xdir/autoload/foo.vim')
2820 var save_rtp = &rtp
2821 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2822 augroup test
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002823 autocmd TextYankPost * call foo#Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002824 augroup END
2825
2826 normal Y
2827
2828 augroup test
2829 autocmd!
2830 augroup END
2831 delete('Xdir', 'rf')
2832 &rtp = save_rtp
2833enddef
2834
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002835" test using a autoloaded file that is case sensitive
2836def Test_vim9_autoload_case_sensitive()
2837 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002838 vim9script
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002839 export def CaseSensitive(): string
2840 return 'done'
2841 enddef
2842 END
2843
2844 mkdir('Xdir/autoload', 'p')
2845 writefile(lines, 'Xdir/autoload/CaseSensitive.vim')
2846 var save_rtp = &rtp
2847 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2848
2849 lines =<< trim END
2850 vim9script
2851 import autoload 'CaseSensitive.vim'
2852 assert_equal('done', CaseSensitive.CaseSensitive())
2853 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002854 v9.CheckScriptSuccess(lines)
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002855
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002856 if !has('fname_case')
2857 lines =<< trim END
2858 vim9script
2859 import autoload 'CaseSensitive.vim'
2860 import autoload 'casesensitive.vim'
2861 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002862 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002863 endif
2864
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002865 delete('Xdir', 'rf')
2866 &rtp = save_rtp
2867enddef
2868
Bram Moolenaar160aa862022-01-10 21:29:57 +00002869" This was causing a crash because suppress_errthrow wasn't reset.
2870def Test_vim9_autoload_error()
2871 var lines =<< trim END
2872 vim9script
2873 def crash#func()
2874 try
2875 for x in List()
2876 endfor
2877 catch
2878 endtry
2879 g:ok = true
2880 enddef
2881 fu List()
2882 invalid
2883 endfu
2884 try
2885 alsoinvalid
2886 catch /wontmatch/
2887 endtry
2888 END
2889 call mkdir('Xruntime/autoload', 'p')
2890 call writefile(lines, 'Xruntime/autoload/crash.vim')
2891
2892 # run in a separate Vim to avoid the side effects of assert_fails()
2893 lines =<< trim END
2894 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
2895 call crash#func()
2896 call writefile(['ok'], 'Xdidit')
2897 qall!
2898 END
2899 writefile(lines, 'Xscript')
Bram Moolenaar62aec932022-01-29 21:45:34 +00002900 g:RunVim([], [], '-S Xscript')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002901 assert_equal(['ok'], readfile('Xdidit'))
2902
2903 delete('Xdidit')
2904 delete('Xscript')
2905 delete('Xruntime', 'rf')
2906
2907 lines =<< trim END
2908 vim9script
2909 var foo#bar = 'asdf'
2910 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002911 v9.CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002912enddef
2913
Bram Moolenaar753885b2022-08-24 16:30:36 +01002914def Test_vim9_import_symlink()
2915 if !has('unix')
2916 CheckUnix
2917 else
2918 mkdir('Xto/plugin', 'p')
2919 var lines =<< trim END
2920 vim9script
2921 import autoload 'bar.vim'
2922 g:resultFunc = bar.Func()
2923 g:resultValue = bar.value
2924 END
2925 writefile(lines, 'Xto/plugin/foo.vim')
2926
2927 mkdir('Xto/autoload', 'p')
2928 lines =<< trim END
2929 vim9script
2930 export def Func(): string
2931 return 'func'
2932 enddef
2933 export var value = 'val'
2934 END
2935 writefile(lines, 'Xto/autoload/bar.vim')
2936
2937 var save_rtp = &rtp
2938 &rtp = getcwd() .. '/Xfrom'
2939 system('ln -s ' .. getcwd() .. '/Xto Xfrom')
2940
2941 source Xfrom/plugin/foo.vim
2942 assert_equal('func', g:resultFunc)
2943 assert_equal('val', g:resultValue)
2944
2945 var infoTo = getscriptinfo()->filter((_, v) => v.name =~ 'Xto/autoload/bar')
2946 var infoFrom = getscriptinfo()->filter((_, v) => v.name =~ 'Xfrom/autoload/bar')
2947 assert_equal(1, len(infoTo))
2948 assert_equal(1, len(infoFrom))
2949 assert_equal(infoTo[0].sid, infoFrom[0].sourced)
Bram Moolenaar7ea9fcb2022-08-24 17:46:12 +01002950 var output: string
2951 redir => output
2952 scriptnames
2953 redir END
2954 assert_match(infoFrom[0].sid .. '->' .. infoFrom[0].sourced .. '.*Xfrom', output)
Bram Moolenaar753885b2022-08-24 16:30:36 +01002955
2956 unlet g:resultFunc
2957 unlet g:resultValue
2958 &rtp = save_rtp
2959 delete('Xto', 'rf')
2960 delete('Xfrom', 'rf')
2961 endif
2962enddef
2963
Bram Moolenaard8448622022-01-07 21:39:52 +00002964
2965" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker