blob: ed4e9d2ffd9aa79faa4754001730bb1a910e723e [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)
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +0100744 assert_equal(999999, l[0].version)
Bram Moolenaar89445512022-04-14 12:58:23 +0100745
746 unlet g:result
Bram Moolenaar648dd882022-04-14 21:36:15 +0100747 delete('XrelautoloadExport.vim')
Bram Moolenaar89445512022-04-14 12:58:23 +0100748 delete('Xmapscript.vim')
749 nunmap <F3>
Bram Moolenaard8448622022-01-07 21:39:52 +0000750enddef
751
Bram Moolenaar5cb53b72022-05-26 19:54:05 +0100752def Test_autoload_import_var()
753 # variable name starts with "autoload"
754 var lines =<< trim END
755 vim9script
756 var autoloaded = "Xtest.vim"
757 import autoloaded
758 END
759 v9.CheckScriptFailure(lines, 'E1053: Could not import "Xtest.vim')
760enddef
761
Bram Moolenaar648dd882022-04-14 21:36:15 +0100762def Test_use_autoload_import_in_mapping()
763 var lines =<< trim END
764 vim9script
765 export def Func()
766 g:result = 49
767 enddef
768 END
769 mkdir('Xdir/autoload', 'p')
770 writefile(lines, 'Xdir/autoload/XautoloadExport.vim')
771 var save_rtp = &rtp
772 exe 'set rtp^=' .. getcwd() .. '/Xdir'
773
774 lines =<< trim END
775 vim9script
776 import autoload 'XautoloadExport.vim' as some
777 nnoremap <F3> :call <SID>some.Func()<CR>
778 END
779 writefile(lines, 'Xmapscript.vim')
780
781 source Xmapscript.vim
782 assert_match('\d\+ A: .*autoload/XautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
783 feedkeys("\<F3>", "xt")
784 assert_equal(49, g:result)
785
786 unlet g:result
787 delete('Xmapscript.vim')
788 nunmap <F3>
789 delete('Xdir', 'rf')
790 &rtp = save_rtp
791enddef
792
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000793def Test_use_import_in_command_completion()
Bram Moolenaar15d16352022-01-17 20:09:08 +0000794 var lines =<< trim END
795 vim9script
796 export def Complete(..._): list<string>
797 return ['abcd']
798 enddef
799 END
800 writefile(lines, 'Xscript.vim')
801
802 lines =<< trim END
803 vim9script
804 import './Xscript.vim'
805
806 command -nargs=1 -complete=customlist,Xscript.Complete Cmd echo 'ok'
807 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
808 assert_equal('#Cmd abcd', @:)
809 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000810 v9.CheckScriptSuccess(lines)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000811
812 delcommand Cmd
813 delete('Xscript.vim')
814enddef
815
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100816def Test_use_import_with_funcref_in_command_completion()
817 var lines =<< trim END
818 vim9script
819 export def Complete(..._): list<string>
820 return ['abcd']
821 enddef
822 END
823 writefile(lines, 'Xscript.vim')
824
825 lines =<< trim END
826 vim9script
827 import './Xscript.vim'
828
829 var Ref = Xscript.Complete
830 exe "command -nargs=1 -complete=customlist," .. expand('<SID>') .. "Ref Cmd echo 'ok'"
831 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
832 assert_equal('#Cmd abcd', @:)
833 END
834 v9.CheckScriptSuccess(lines)
835
836 delcommand Cmd
837 delete('Xscript.vim')
838enddef
839
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000840def Test_use_autoload_import_in_insert_completion()
841 mkdir('Xdir/autoload', 'p')
842 var save_rtp = &rtp
843 exe 'set rtp^=' .. getcwd() .. '/Xdir'
844
845 var lines =<< trim END
846 vim9script
847 export def ThesaurusFunc(findbase: bool, _): any
848 if findbase
849 return 1
850 endif
851 return [
852 'check',
853 'experiment',
854 'test',
855 'verification'
856 ]
857 enddef
858 g:completion_loaded = 'yes'
859 END
860 writefile(lines, 'Xdir/autoload/completion.vim')
861
862 new
863 lines =<< trim END
864 vim9script
865 g:completion_loaded = 'no'
866 import autoload 'completion.vim'
867 set thesaurusfunc=completion.ThesaurusFunc
868 assert_equal('no', g:completion_loaded)
869 feedkeys("i\<C-X>\<C-T>\<C-N>\<Esc>", 'xt')
870 assert_equal('experiment', getline(1))
871 assert_equal('yes', g:completion_loaded)
872 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000873 v9.CheckScriptSuccess(lines)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000874
875 set thesaurusfunc=
876 bwipe!
877 delete('Xdir', 'rf')
878 &rtp = save_rtp
879enddef
880
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000881def Test_use_autoload_import_partial_in_opfunc()
882 mkdir('Xdir/autoload', 'p')
883 var save_rtp = &rtp
884 exe 'set rtp^=' .. getcwd() .. '/Xdir'
885
886 var lines =<< trim END
887 vim9script
888 export def Opfunc(..._)
889 g:opfunc_called = 'yes'
890 enddef
891 END
892 writefile(lines, 'Xdir/autoload/opfunc.vim')
893
894 new
895 lines =<< trim END
896 vim9script
897 import autoload 'opfunc.vim'
898 nnoremap <expr> <F3> TheFunc()
899 def TheFunc(): string
900 &operatorfunc = function('opfunc.Opfunc', [0])
901 return 'g@'
902 enddef
903 feedkeys("\<F3>l", 'xt')
904 assert_equal('yes', g:opfunc_called)
905 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000906 v9.CheckScriptSuccess(lines)
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000907
908 set opfunc=
909 bwipe!
910 delete('Xdir', 'rf')
Bram Moolenaar06b77222022-01-25 15:51:56 +0000911 nunmap <F3>
912 &rtp = save_rtp
913enddef
914
915def Test_set_opfunc_to_autoload_func_directly()
916 mkdir('Xdir/autoload', 'p')
917 var save_rtp = &rtp
918 exe 'set rtp^=' .. getcwd() .. '/Xdir'
919
920 var lines =<< trim END
921 vim9script
922 export def Opfunc(..._)
923 g:opfunc_called = 'yes'
924 enddef
925 END
926 writefile(lines, 'Xdir/autoload/opfunc.vim')
927
928 new
929 lines =<< trim END
930 vim9script
931 import autoload 'opfunc.vim'
932 nnoremap <expr> <F3> TheFunc()
933 def TheFunc(): string
934 &operatorfunc = opfunc.Opfunc
935 return 'g@'
936 enddef
937 feedkeys("\<F3>l", 'xt')
938 assert_equal('yes', g:opfunc_called)
939 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000940 v9.CheckScriptSuccess(lines)
Bram Moolenaar06b77222022-01-25 15:51:56 +0000941
942 set opfunc=
943 bwipe!
944 delete('Xdir', 'rf')
945 nunmap <F3>
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000946 &rtp = save_rtp
947enddef
948
Bram Moolenaare70dd112022-01-21 16:31:11 +0000949def Test_use_autoload_import_in_fold_expression()
950 mkdir('Xdir/autoload', 'p')
951 var save_rtp = &rtp
952 exe 'set rtp^=' .. getcwd() .. '/Xdir'
953
954 var lines =<< trim END
955 vim9script
956 export def Expr(): string
957 return getline(v:lnum) =~ '^#' ? '>1' : '1'
958 enddef
Bram Moolenaar9530b582022-01-22 13:39:08 +0000959 export def Text(): string
960 return 'fold text'
961 enddef
Bram Moolenaare70dd112022-01-21 16:31:11 +0000962 g:fold_loaded = 'yes'
963 END
964 writefile(lines, 'Xdir/autoload/fold.vim')
965
966 lines =<< trim END
967 vim9script
968 import autoload 'fold.vim'
969 &foldexpr = 'fold.Expr()'
Bram Moolenaar9530b582022-01-22 13:39:08 +0000970 &foldtext = 'fold.Text()'
Bram Moolenaare70dd112022-01-21 16:31:11 +0000971 &foldmethod = 'expr'
972 &debug = 'throw'
973 END
974 new
975 setline(1, ['# one', 'text', '# two', 'text'])
976 g:fold_loaded = 'no'
Bram Moolenaar62aec932022-01-29 21:45:34 +0000977 v9.CheckScriptSuccess(lines)
Bram Moolenaare70dd112022-01-21 16:31:11 +0000978 assert_equal('no', g:fold_loaded)
979 redraw
980 assert_equal('yes', g:fold_loaded)
981
982 # Check that script context of 'foldexpr' is copied to another buffer.
983 edit! otherfile
984 redraw
985
Bram Moolenaar9530b582022-01-22 13:39:08 +0000986 set foldexpr= foldtext& foldmethod& debug=
Bram Moolenaare70dd112022-01-21 16:31:11 +0000987 bwipe!
988 delete('Xdir', 'rf')
989 &rtp = save_rtp
990enddef
991
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100992def Test_autoload_import_relative()
993 var lines =<< trim END
994 vim9script
995
996 g:loaded = 'yes'
997 export def RelFunc(): string
998 return 'relfunc'
999 enddef
1000 def NotExported()
1001 echo 'not'
1002 enddef
1003
1004 export var someText = 'some text'
1005 var notexp = 'bad'
1006 END
1007 writefile(lines, 'XimportRel.vim')
1008 writefile(lines, 'XimportRel2.vim')
1009 writefile(lines, 'XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +01001010 writefile(lines, 'XimportRel4.vim')
1011 writefile(lines, 'XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001012
1013 lines =<< trim END
1014 vim9script
1015 g:loaded = 'no'
1016 import autoload './XimportRel.vim'
1017 assert_equal('no', g:loaded)
1018
1019 def AFunc(): string
1020 var res = ''
1021 res ..= XimportRel.RelFunc()
1022 res ..= '/'
1023 res ..= XimportRel.someText
1024 XimportRel.someText = 'from AFunc'
1025 return res
1026 enddef
1027 # script not loaded when compiling
1028 defcompile
1029 assert_equal('no', g:loaded)
1030
1031 assert_equal('relfunc/some text', AFunc())
1032 assert_equal('yes', g:loaded)
1033 unlet g:loaded
1034
1035 assert_equal('from AFunc', XimportRel.someText)
1036 XimportRel.someText = 'from script'
1037 assert_equal('from script', XimportRel.someText)
1038 END
1039 v9.CheckScriptSuccess(lines)
1040
1041 lines =<< trim END
1042 vim9script
1043 import autoload './XimportRel.vim'
1044 echo XimportRel.NotExported()
1045 END
1046 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 3)
1047
1048 lines =<< trim END
1049 vim9script
1050 import autoload './XimportRel.vim'
1051 echo XimportRel.notexp
1052 END
1053 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
1054
1055 lines =<< trim END
1056 vim9script
1057 import autoload './XimportRel.vim'
1058 XimportRel.notexp = 'bad'
1059 END
1060 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
1061
1062 lines =<< trim END
1063 vim9script
1064 import autoload './XimportRel.vim'
1065 def Func()
1066 echo XimportRel.NotExported()
1067 enddef
1068 Func()
1069 END
1070 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 1)
1071
1072 lines =<< trim END
1073 vim9script
1074 import autoload './XimportRel.vim'
1075 def Func()
1076 echo XimportRel.notexp
1077 enddef
1078 Func()
1079 END
1080 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1081
Bram Moolenaar10611952022-04-03 21:11:34 +01001082 # Same, script not imported before
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001083 lines =<< trim END
1084 vim9script
Bram Moolenaar10611952022-04-03 21:11:34 +01001085 import autoload './XimportRel4.vim'
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001086 def Func()
Bram Moolenaar10611952022-04-03 21:11:34 +01001087 echo XimportRel4.notexp
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001088 enddef
1089 Func()
1090 END
1091 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1092
Bram Moolenaar10611952022-04-03 21:11:34 +01001093 # does not fail if the script wasn't loaded yet and only compiling
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001094 g:loaded = 'no'
1095 lines =<< trim END
1096 vim9script
1097 import autoload './XimportRel2.vim'
1098 def Func()
1099 echo XimportRel2.notexp
1100 enddef
1101 defcompile
1102 END
1103 v9.CheckScriptSuccess(lines)
1104 assert_equal('no', g:loaded)
1105
Bram Moolenaar10611952022-04-03 21:11:34 +01001106 lines =<< trim END
1107 vim9script
1108 import autoload './XimportRel.vim'
1109 def Func()
1110 XimportRel.notexp = 'bad'
1111 enddef
1112 Func()
1113 END
1114 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1115
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001116 # fails with a not loaded import
1117 lines =<< trim END
1118 vim9script
1119 import autoload './XimportRel3.vim'
1120 def Func()
1121 XimportRel3.notexp = 'bad'
1122 enddef
1123 Func()
1124 END
1125 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1126 assert_equal('yes', g:loaded)
1127 unlet g:loaded
1128
Bram Moolenaar10611952022-04-03 21:11:34 +01001129 lines =<< trim END
1130 vim9script
1131 import autoload './XimportRel5.vim'
1132 def Func()
1133 XimportRel5.nosuchvar = 'bad'
1134 enddef
1135 Func()
1136 END
1137 v9.CheckScriptFailure(lines, 'E121: Undefined variable: nosuchvar', 1)
1138 unlet g:loaded
1139
1140 # nasty: delete script after compiling function
1141 writefile(['vim9script'], 'XimportRelDel.vim')
1142 lines =<< trim END
1143 vim9script
1144
1145 import autoload './XimportRelDel.vim'
1146 def DoIt()
1147 echo XimportRelDel.var
1148 enddef
1149 defcompile
1150 delete('XimportRelDel.vim')
1151 DoIt()
1152 END
Bram Moolenaar242c1522022-04-03 21:52:51 +01001153 v9.CheckScriptFailure(lines, 'E484:')
Bram Moolenaar10611952022-04-03 21:11:34 +01001154
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001155 delete('XimportRel.vim')
1156 delete('XimportRel2.vim')
1157 delete('XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +01001158 delete('XimportRel4.vim')
1159 delete('XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001160enddef
1161
Bram Moolenaarccbfd482022-03-31 16:18:23 +01001162def Test_autoload_import_relative_autoload_dir()
1163 mkdir('autoload', 'p')
1164 var lines =<< trim END
1165 vim9script
1166 export def Bar()
1167 g:called_bar = 'yes'
1168 enddef
1169 END
1170 writefile(lines, 'autoload/script.vim')
1171
1172 lines =<< trim END
1173 vim9script
1174 import autoload './autoload/script.vim'
1175 def Foo()
1176 script.Bar()
1177 enddef
1178 Foo()
1179 assert_equal('yes', g:called_bar)
1180 END
1181 v9.CheckScriptSuccess(lines)
1182
1183 unlet g:called_bar
1184 delete('autoload', 'rf')
1185enddef
1186
Bram Moolenaaraac12da2022-04-24 21:33:20 +01001187def Test_autoload_import_deleted()
1188 var lines =<< trim END
1189 vim9script
1190 export const FOO = 1
1191 END
1192 writefile(lines, 'Xa.vim')
1193
1194 lines =<< trim END
1195 vim9script
1196 import autoload './Xa.vim'
1197
1198 delete('Xa.vim')
1199 var x = Xa.FOO
1200 END
1201 v9.CheckScriptFailure(lines, 'E484:')
1202
1203 delete('Xdir', 'rf')
1204enddef
1205
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001206func Test_import_in_diffexpr()
1207 CheckExecutable diff
1208
1209 call Run_Test_import_in_diffexpr()
1210endfunc
1211
1212def Run_Test_import_in_diffexpr()
1213 var lines =<< trim END
1214 vim9script
1215
1216 export def DiffExpr()
1217 # Prepend some text to check diff type detection
1218 writefile(['warning', ' message'], v:fname_out)
1219 silent exe '!diff ' .. v:fname_in .. ' '
1220 .. v:fname_new .. '>>' .. v:fname_out
1221 enddef
1222 END
1223 writefile(lines, 'Xdiffexpr')
1224
1225 lines =<< trim END
1226 vim9script
1227 import './Xdiffexpr' as diff
1228
1229 set diffexpr=diff.DiffExpr()
1230 set diffopt=foldcolumn:0
1231 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001232 v9.CheckScriptSuccess(lines)
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001233
1234 enew!
1235 call setline(1, ['one', 'two', 'three'])
1236 diffthis
1237
1238 botright vert new
1239 call setline(1, ['one', 'two', 'three.'])
1240 diffthis
1241 # we only check if this does not cause errors
1242 redraw
1243
1244 diffoff!
Bram Moolenaar50761872022-06-26 18:01:00 +01001245 set diffexpr=
1246 set diffopt&
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001247 bwipe!
1248 bwipe!
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001249 delete('Xdiffexpr')
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001250enddef
1251
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001252def Test_import_in_patchexpr()
1253 var lines =<< trim END
1254 vim9script
1255 export def TPatch()
1256 call writefile(['output file'], v:fname_out)
1257 enddef
1258 END
1259 writefile(lines, 'Xpatchexpr')
1260
1261 lines =<< trim END
1262 vim9script
1263 import './Xpatchexpr' as patch
1264 set patchexpr=patch.TPatch()
1265 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001266 v9.CheckScriptSuccess(lines)
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001267
1268 call writefile(['input file'], 'Xinput')
1269 call writefile(['diff file'], 'Xdiff')
1270 :%bwipe!
1271 edit Xinput
1272 diffpatch Xdiff
1273 call assert_equal('output file', getline(1))
1274
1275 call delete('Xinput')
1276 call delete('Xdiff')
1277 call delete('Xpatchexpr')
1278 set patchexpr&
1279 :%bwipe!
1280enddef
1281
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001282def Test_import_in_formatexpr()
1283 var lines =<< trim END
1284 vim9script
1285 export def MyFormatExpr(): number
1286 g:did_format = 'yes'
1287 return 0
1288 enddef
1289 END
1290 writefile(lines, 'Xformatter')
1291
1292 lines =<< trim END
1293 vim9script
1294 import './Xformatter' as format
1295 set formatexpr=format.MyFormatExpr()
1296 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001297 v9.CheckScriptSuccess(lines)
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001298
1299 new
1300 setline(1, ['a', 'b', 'c'])
1301 normal gqG
1302 assert_equal('yes', g:did_format)
1303
1304 bwipe!
1305 delete('Xformatter')
1306 unlet g:did_format
1307 set formatexpr=
1308enddef
1309
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001310def Test_import_in_includeexpr()
1311 writefile(['found it'], 'Xthisfile')
1312 new
1313
1314 var lines =<< trim END
1315 vim9script
1316 export def DoSub(): string
1317 return substitute(v:fname, 'that', 'this', '')
1318 enddef
1319 END
1320 writefile(lines, 'Xinclude.vim')
1321
1322 lines =<< trim END
1323 vim9script
1324 import './Xinclude.vim'
1325 set includeexpr=Xinclude.DoSub()
1326 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001327 v9.CheckScriptSuccess(lines)
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001328
1329 setline(1, ['Xthatfile'])
1330 exe "normal \<C-W>f"
1331 assert_equal('Xthisfile', expand('%'))
1332
1333 bwipe!
1334 bwipe!
1335 set includeexpr=
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001336 delete('Xinclude.vim')
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001337 delete('Xthisfile')
1338enddef
1339
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001340def Test_import_in_indentexpr()
1341 var lines =<< trim END
1342 vim9script
1343 export def GetIndent(): number
1344 return 5
1345 enddef
1346 END
1347 writefile(lines, 'Xindenter')
1348
1349 lines =<< trim END
1350 vim9script
1351 import './Xindenter' as indent
1352 set indentexpr=indent.GetIndent()
1353 set debug=throw
1354 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001355 v9.CheckScriptSuccess(lines)
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001356
1357 new
1358 setline(1, 'hello')
1359 normal ==
1360 assert_equal(' hello', getline(1))
1361
1362 bwipe!
1363 set indentexpr= debug=
1364 delete('Xindenter')
1365enddef
1366
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001367func Test_import_in_printexpr()
1368 CheckFeature postscript
1369 call Run_Test_import_in_printexpr()
1370endfunc
1371
1372def Run_Test_import_in_printexpr()
1373 var lines =<< trim END
1374 vim9script
1375 export def PrintFile(): bool
1376 g:printed = 'yes'
1377 delete('v:fname_in')
1378 return false
1379 enddef
1380 END
1381 writefile(lines, 'Xprint.vim')
1382
1383 lines =<< trim END
1384 vim9script
1385 import './Xprint.vim'
1386 set printexpr=Xprint.PrintFile()
1387 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001388 v9.CheckScriptSuccess(lines)
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001389
1390 help
1391 hardcopy dummy args
1392 assert_equal('yes', g:printed)
1393
1394 delete('Xprint.vim')
1395 set printexpr=
1396enddef
1397
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001398def Test_import_in_charconvert()
1399 var lines =<< trim END
1400 vim9script
1401 export def MakeUpper(): bool
1402 var data = readfile(v:fname_in)
1403 map(data, 'toupper(v:val)')
1404 writefile(data, v:fname_out)
1405 return false # success
1406 enddef
1407 END
1408 writefile(lines, 'Xconvert.vim')
1409
1410 lines =<< trim END
1411 vim9script
1412 import './Xconvert.vim' as conv
1413 set charconvert=conv.MakeUpper()
1414 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001415 v9.CheckScriptSuccess(lines)
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001416
1417 writefile(['one', 'two'], 'Xfile')
1418 new Xfile
1419 write ++enc=ucase Xfile1
1420 assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1421
1422 delete('Xfile')
1423 delete('Xfile1')
1424 delete('Xconvert.vim')
1425 bwipe!
1426 set charconvert&
1427enddef
1428
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001429func Test_import_in_spellsuggest_expr()
1430 CheckFeature spell
1431 call Run_Test_import_in_spellsuggest_expr()
1432endfunc
1433
1434def Run_Test_import_in_spellsuggest_expr()
1435 var lines =<< trim END
1436 vim9script
1437 export def MySuggest(): list<any>
1438 return [['Fox', 8], ['Fop', 9]]
1439 enddef
1440 END
1441 writefile(lines, 'Xsuggest.vim')
1442
1443 lines =<< trim END
1444 vim9script
1445 import './Xsuggest.vim' as sugg
1446 set spell spellsuggest=expr:sugg.MySuggest()
1447 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001448 v9.CheckScriptSuccess(lines)
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001449
1450 set verbose=1 # report errors
1451 call assert_equal(['Fox', 'Fop'], spellsuggest('Fo', 2))
1452
1453 delete('Xsuggest.vim')
1454 set nospell spellsuggest& verbose=0
1455enddef
1456
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001457def Test_export_shadows_global_function()
1458 mkdir('Xdir/autoload', 'p')
1459 var save_rtp = &rtp
1460 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1461
1462 var lines =<< trim END
1463 vim9script
1464 export def Shadow(): string
1465 return 'Shadow()'
1466 enddef
1467 END
1468 writefile(lines, 'Xdir/autoload/shadow.vim')
1469
1470 lines =<< trim END
1471 vim9script
1472
1473 def g:Shadow(): string
1474 return 'global'
1475 enddef
1476
1477 import autoload 'shadow.vim'
1478 assert_equal('Shadow()', shadow.Shadow())
1479 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001480 v9.CheckScriptSuccess(lines)
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001481
1482 delfunc g:Shadow
1483 bwipe!
1484 delete('Xdir', 'rf')
1485 &rtp = save_rtp
1486enddef
1487
Bram Moolenaard8448622022-01-07 21:39:52 +00001488def Test_export_fails()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001489 v9.CheckScriptFailure(['export var some = 123'], 'E1042:')
1490 v9.CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
1491 v9.CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +01001492 v9.CheckScriptFailure(['vim9script', 'export function /a1b2c3'], 'E1044:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001493
1494 assert_fails('export something', 'E1043:')
1495enddef
1496
1497func Test_import_fails_without_script()
1498 CheckRunVimInTerminal
1499
1500 " call indirectly to avoid compilation error for missing functions
1501 call Run_Test_import_fails_on_command_line()
1502endfunc
1503
1504def Run_Test_import_fails_on_command_line()
1505 var export =<< trim END
1506 vim9script
1507 export def Foo(): number
1508 return 0
1509 enddef
1510 END
1511 writefile(export, 'XexportCmd.vim')
1512
Bram Moolenaar62aec932022-01-29 21:45:34 +00001513 var buf = g:RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaard8448622022-01-07 21:39:52 +00001514 rows: 6, wait_for_ruler: 0})
Bram Moolenaar62aec932022-01-29 21:45:34 +00001515 g:WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaard8448622022-01-07 21:39:52 +00001516
1517 delete('XexportCmd.vim')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001518 g:StopVimInTerminal(buf)
Bram Moolenaard8448622022-01-07 21:39:52 +00001519enddef
1520
1521def Test_vim9_reload_noclear()
1522 var lines =<< trim END
1523 vim9script
1524 export var exported = 'thexport'
1525
1526 export def TheFunc(x = 0)
1527 enddef
1528 END
1529 writefile(lines, 'XExportReload')
1530 lines =<< trim END
1531 vim9script noclear
1532 g:loadCount += 1
Bram Moolenaara749a422022-02-12 19:52:25 +00001533 var reloaded = 'init'
Bram Moolenaard8448622022-01-07 21:39:52 +00001534 import './XExportReload' as exp
1535
1536 def Again(): string
1537 return 'again'
1538 enddef
1539
1540 exp.TheFunc()
1541
Bram Moolenaara749a422022-02-12 19:52:25 +00001542 if exists('loaded') | finish | endif
1543 var loaded = true
Bram Moolenaard8448622022-01-07 21:39:52 +00001544
Bram Moolenaara749a422022-02-12 19:52:25 +00001545 var notReloaded = 'yes'
1546 reloaded = 'first'
Bram Moolenaard8448622022-01-07 21:39:52 +00001547 def g:Values(): list<string>
Bram Moolenaara749a422022-02-12 19:52:25 +00001548 return [reloaded, notReloaded, Again(), Once(), exp.exported]
Bram Moolenaard8448622022-01-07 21:39:52 +00001549 enddef
1550
1551 def Once(): string
1552 return 'once'
1553 enddef
1554 END
1555 writefile(lines, 'XReloaded')
1556 g:loadCount = 0
1557 source XReloaded
1558 assert_equal(1, g:loadCount)
1559 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
1560 source XReloaded
1561 assert_equal(2, g:loadCount)
1562 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1563 source XReloaded
1564 assert_equal(3, g:loadCount)
1565 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1566
1567 delete('XReloaded')
1568 delete('XExportReload')
1569 delfunc g:Values
1570 unlet g:loadCount
1571
1572 lines =<< trim END
1573 vim9script
1574 def Inner()
1575 enddef
1576 END
1577 lines->writefile('XreloadScript.vim')
1578 source XreloadScript.vim
1579
1580 lines =<< trim END
1581 vim9script
1582 def Outer()
1583 def Inner()
1584 enddef
1585 enddef
1586 defcompile
1587 END
1588 lines->writefile('XreloadScript.vim')
1589 source XreloadScript.vim
1590
1591 delete('XreloadScript.vim')
1592enddef
1593
Bram Moolenaarcd1cda22022-02-16 21:48:25 +00001594def Test_vim_reload_noclear_arg_count()
1595 var lines =<< trim END
1596 vim9script noclear
1597
1598 if !exists('g:didload')
1599 def Test(a: string, b: string)
1600 echo a b
1601 enddef
1602 def Call()
1603 Test('a', 'b')
1604 enddef
1605 else
1606 # redefine with one argument less
1607 def Test(a: string)
1608 echo a
1609 enddef
1610 endif
1611 Call()
1612 g:didload = 1
1613 END
1614 lines->writefile('XreloadScript_1.vim')
1615 source XreloadScript_1.vim
1616 assert_fails('source XreloadScript_1.vim', 'E1106: One argument too many')
1617 unlet g:didload
1618
1619 lines =<< trim END
1620 vim9script noclear
1621
1622 if !exists('g:didload')
1623 def Test(a: string, b: string, c: string)
1624 echo a b
1625 enddef
1626 def Call()
1627 Test('a', 'b', 'c')
1628 enddef
1629 else
1630 # redefine with one argument less
1631 def Test(a: string)
1632 echo a
1633 enddef
1634 endif
1635 Call()
1636 g:didload = 1
1637 END
1638 lines->writefile('XreloadScript_2.vim')
1639 source XreloadScript_2.vim
1640 assert_fails('source XreloadScript_2.vim', 'E1106: 2 arguments too many')
1641 unlet g:didload
1642
1643 lines =<< trim END
1644 vim9script noclear
1645
1646 if !exists('g:didload')
1647 def Test(a: string)
1648 echo a
1649 enddef
1650 def Call()
1651 Test('a')
1652 enddef
1653 else
1654 # redefine with one argument extra
1655 def Test(a: string, b: string)
1656 echo a b
1657 enddef
1658 endif
1659 Call()
1660 g:didload = 1
1661 END
1662 lines->writefile('XreloadScript_3.vim')
1663 source XreloadScript_3.vim
1664 assert_fails('source XreloadScript_3.vim', 'E1190: One argument too few')
1665 unlet g:didload
1666
1667 lines =<< trim END
1668 vim9script noclear
1669
1670 if !exists('g:didload')
1671 def Test(a: string)
1672 echo a
1673 enddef
1674 def Call()
1675 Test('a')
1676 enddef
1677 else
1678 # redefine with two arguments extra
1679 def Test(a: string, b: string, c: string)
1680 echo a b
1681 enddef
1682 endif
1683 Call()
1684 g:didload = 1
1685 END
1686 lines->writefile('XreloadScript_4.vim')
1687 source XreloadScript_4.vim
1688 assert_fails('source XreloadScript_4.vim', 'E1190: 2 arguments too few')
1689 unlet g:didload
1690
1691 delete('XreloadScript_1.vim')
1692 delete('XreloadScript_2.vim')
1693 delete('XreloadScript_3.vim')
1694 delete('XreloadScript_4.vim')
1695enddef
1696
1697def Test_vim9_reload_noclear_error()
1698 var lines =<< trim END
1699 vim9script noclear
1700
1701 if !exists('g:didload')
1702 def Test(a: string)
1703 echo a
1704 enddef
1705 def Call()
1706 Test('a')
1707 enddef
1708 else
1709 # redefine with a compile error
1710 def Test(a: string)
1711 echo ax
1712 enddef
1713 endif
1714 Call()
1715 g:didload = 1
1716 END
1717 lines->writefile('XreloadScriptErr.vim')
1718 source XreloadScriptErr.vim
1719 assert_fails('source XreloadScriptErr.vim', 'E1001: Variable not found: ax')
1720
1721 unlet g:didload
1722 delete('XreloadScriptErr.vim')
1723enddef
1724
Bram Moolenaard8448622022-01-07 21:39:52 +00001725def Test_vim9_reload_import()
1726 var lines =<< trim END
1727 vim9script
1728 const var = ''
1729 var valone = 1234
1730 def MyFunc(arg: string)
1731 valone = 5678
1732 enddef
1733 END
1734 var morelines =<< trim END
1735 var valtwo = 222
1736 export def GetValtwo(): number
1737 return valtwo
1738 enddef
1739 END
1740 writefile(lines + morelines, 'Xreload.vim')
1741 source Xreload.vim
1742 source Xreload.vim
1743 source Xreload.vim
1744
1745 # cannot declare a var twice
1746 lines =<< trim END
1747 vim9script
1748 var valone = 1234
1749 var valone = 5678
1750 END
1751 writefile(lines, 'Xreload.vim')
1752 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
1753
1754 delete('Xreload.vim')
1755 delete('Ximport.vim')
1756enddef
1757
1758" if a script is reloaded with a script-local variable that changed its type, a
1759" compiled function using that variable must fail.
1760def Test_script_reload_change_type()
1761 var lines =<< trim END
1762 vim9script noclear
1763 var str = 'string'
1764 def g:GetStr(): string
1765 return str .. 'xxx'
1766 enddef
1767 END
1768 writefile(lines, 'Xreload.vim')
1769 source Xreload.vim
1770 echo g:GetStr()
1771
1772 lines =<< trim END
1773 vim9script noclear
1774 var str = 1234
1775 END
1776 writefile(lines, 'Xreload.vim')
1777 source Xreload.vim
1778 assert_fails('echo g:GetStr()', 'E1150:')
1779
1780 delfunc g:GetStr
1781 delete('Xreload.vim')
1782enddef
1783
1784" Define CallFunc so that the test can be compiled
1785command CallFunc echo 'nop'
1786
1787def Test_script_reload_from_function()
1788 var lines =<< trim END
1789 vim9script
1790
Bram Moolenaar10611952022-04-03 21:11:34 +01001791 if exists('g:loadedThis')
Bram Moolenaard8448622022-01-07 21:39:52 +00001792 finish
1793 endif
Bram Moolenaar10611952022-04-03 21:11:34 +01001794 g:loadedThis = 1
Bram Moolenaard8448622022-01-07 21:39:52 +00001795 delcommand CallFunc
1796 command CallFunc Func()
1797 def Func()
1798 so XreloadFunc.vim
1799 g:didTheFunc = 1
1800 enddef
1801 END
1802 writefile(lines, 'XreloadFunc.vim')
1803 source XreloadFunc.vim
1804 CallFunc
1805 assert_equal(1, g:didTheFunc)
1806
1807 delete('XreloadFunc.vim')
1808 delcommand CallFunc
Bram Moolenaar10611952022-04-03 21:11:34 +01001809 unlet g:loadedThis
Bram Moolenaard8448622022-01-07 21:39:52 +00001810 unlet g:didTheFunc
1811enddef
1812
1813def s:RetSome(): string
1814 return 'some'
1815enddef
1816
1817" Not exported function that is referenced needs to be accessed by the
1818" script-local name.
1819def Test_vim9_funcref()
1820 var sortlines =<< trim END
1821 vim9script
1822 def Compare(i1: number, i2: number): number
1823 return i2 - i1
1824 enddef
1825
1826 export def FastSort(): list<number>
1827 return range(5)->sort(Compare)
1828 enddef
1829
1830 export def GetString(arg: string): string
1831 return arg
1832 enddef
1833 END
1834 writefile(sortlines, 'Xsort.vim')
1835
1836 var lines =<< trim END
1837 vim9script
1838 import './Xsort.vim'
1839 def Test()
1840 g:result = Xsort.FastSort()
1841 enddef
1842 Test()
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001843 END
1844 writefile(lines, 'Xscript.vim')
1845 source Xscript.vim
1846 assert_equal([4, 3, 2, 1, 0], g:result)
1847 unlet g:result
Bram Moolenaard8448622022-01-07 21:39:52 +00001848
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001849 lines =<< trim END
1850 vim9script
Bram Moolenaard8448622022-01-07 21:39:52 +00001851 # using a function imported with "as"
1852 import './Xsort.vim' as anAlias
1853 assert_equal('yes', anAlias.GetString('yes'))
1854
1855 # using the function from a compiled function
1856 def TestMore(): string
1857 var s = s:anAlias.GetString('foo')
1858 return s .. anAlias.GetString('bar')
1859 enddef
1860 assert_equal('foobar', TestMore())
1861
1862 # error when using a function that isn't exported
1863 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
1864 END
1865 writefile(lines, 'Xscript.vim')
1866
Bram Moolenaard8448622022-01-07 21:39:52 +00001867 delete('Xsort.vim')
1868 delete('Xscript.vim')
1869
1870 var Funcref = function('s:RetSome')
1871 assert_equal('some', Funcref())
1872enddef
1873
1874" Check that when searching for "FilterFunc" it finds the import in the
1875" script where FastFilter() is called from, both as a string and as a direct
1876" function reference.
1877def Test_vim9_funcref_other_script()
1878 var filterLines =<< trim END
1879 vim9script
1880 export def FilterFunc(idx: number, val: number): bool
1881 return idx % 2 == 1
1882 enddef
1883 export def FastFilter(): list<number>
1884 return range(10)->filter('FilterFunc(v:key, v:val)')
1885 enddef
1886 export def FastFilterDirect(): list<number>
1887 return range(10)->filter(FilterFunc)
1888 enddef
1889 END
1890 writefile(filterLines, 'Xfilter.vim')
1891
1892 var lines =<< trim END
1893 vim9script
1894 import './Xfilter.vim' as filter
1895 def Test()
1896 var x: list<number> = filter.FastFilter()
1897 enddef
1898 Test()
1899 def TestDirect()
1900 var x: list<number> = filter.FastFilterDirect()
1901 enddef
1902 TestDirect()
1903 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001904 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +00001905 delete('Xfilter.vim')
1906enddef
1907
1908def Test_import_absolute()
1909 var import_lines = [
1910 'vim9script',
1911 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
1912 'def UseExported()',
1913 ' g:imported_abs = abs.exported',
1914 ' abs.exported = 8888',
1915 ' g:imported_after = abs.exported',
1916 'enddef',
1917 'UseExported()',
1918 'g:import_disassembled = execute("disass UseExported")',
1919 ]
1920 writefile(import_lines, 'Ximport_abs.vim')
1921 writefile(s:export_script_lines, 'Xexport_abs.vim')
1922
1923 source Ximport_abs.vim
1924
1925 assert_equal(9876, g:imported_abs)
1926 assert_equal(8888, g:imported_after)
1927 assert_match('<SNR>\d\+_UseExported\_s*' ..
1928 'g:imported_abs = abs.exported\_s*' ..
1929 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1930 '1 STOREG g:imported_abs\_s*' ..
1931 'abs.exported = 8888\_s*' ..
1932 '2 PUSHNR 8888\_s*' ..
1933 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1934 'g:imported_after = abs.exported\_s*' ..
1935 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1936 '5 STOREG g:imported_after',
1937 g:import_disassembled)
1938
1939 Undo_export_script_lines()
1940 unlet g:imported_abs
1941 unlet g:import_disassembled
1942
1943 delete('Ximport_abs.vim')
1944 delete('Xexport_abs.vim')
1945enddef
1946
1947def Test_import_rtp()
1948 var import_lines = [
1949 'vim9script',
1950 'import "Xexport_rtp.vim" as rtp',
1951 'g:imported_rtp = rtp.exported',
1952 ]
1953 writefile(import_lines, 'Ximport_rtp.vim')
1954 mkdir('import', 'p')
1955 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1956
1957 var save_rtp = &rtp
1958 &rtp = getcwd()
1959 source Ximport_rtp.vim
1960 &rtp = save_rtp
1961
1962 assert_equal(9876, g:imported_rtp)
1963
1964 Undo_export_script_lines()
1965 unlet g:imported_rtp
1966 delete('Ximport_rtp.vim')
1967 delete('import', 'rf')
1968enddef
1969
1970def Test_import_compile_error()
1971 var export_lines = [
1972 'vim9script',
1973 'export def ExpFunc(): string',
1974 ' return notDefined',
1975 'enddef',
1976 ]
1977 writefile(export_lines, 'Xexported.vim')
1978
1979 var import_lines = [
1980 'vim9script',
1981 'import "./Xexported.vim" as expo',
1982 'def ImpFunc()',
1983 ' echo expo.ExpFunc()',
1984 'enddef',
1985 'defcompile',
1986 ]
1987 writefile(import_lines, 'Ximport.vim')
1988
1989 try
1990 source Ximport.vim
1991 catch /E1001/
1992 # Error should be before the Xexported.vim file.
1993 assert_match('E1001: Variable not found: notDefined', v:exception)
1994 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1995 endtry
1996
1997 delete('Xexported.vim')
1998 delete('Ximport.vim')
1999enddef
2000
2001def Test_func_overrules_import_fails()
2002 var export_lines =<< trim END
2003 vim9script
2004 export def Func()
2005 echo 'imported'
2006 enddef
2007 END
2008 writefile(export_lines, 'XexportedFunc.vim')
2009
2010 var lines =<< trim END
2011 vim9script
2012 import './XexportedFunc.vim' as Func
2013 def Func()
2014 echo 'local to function'
2015 enddef
2016 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002017 v9.CheckScriptFailure(lines, 'E1213: Redefining imported item "Func"')
Bram Moolenaard8448622022-01-07 21:39:52 +00002018
2019 lines =<< trim END
2020 vim9script
2021 import './XexportedFunc.vim' as Func
2022 def Outer()
2023 def Func()
2024 echo 'local to function'
2025 enddef
2026 enddef
2027 defcompile
2028 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002029 v9.CheckScriptFailure(lines, 'E1236:')
Bram Moolenaard8448622022-01-07 21:39:52 +00002030
2031 delete('XexportedFunc.vim')
2032enddef
2033
2034def Test_source_vim9_from_legacy()
2035 var vim9_lines =<< trim END
2036 vim9script
2037 var local = 'local'
2038 g:global = 'global'
2039 export var exported = 'exported'
2040 export def GetText(): string
2041 return 'text'
2042 enddef
2043 END
2044 writefile(vim9_lines, 'Xvim9_script.vim')
2045
2046 var legacy_lines =<< trim END
2047 source Xvim9_script.vim
2048
2049 call assert_false(exists('local'))
2050 call assert_false(exists('exported'))
2051 call assert_false(exists('s:exported'))
2052 call assert_equal('global', global)
2053 call assert_equal('global', g:global)
Bram Moolenaard8448622022-01-07 21:39:52 +00002054 END
2055 writefile(legacy_lines, 'Xlegacy_script.vim')
2056
2057 source Xlegacy_script.vim
2058 assert_equal('global', g:global)
2059 unlet g:global
2060
2061 delete('Xlegacy_script.vim')
2062 delete('Xvim9_script.vim')
2063enddef
2064
Bram Moolenaarc43e6232022-01-13 20:51:56 +00002065def Test_import_vim9_from_legacy()
2066 var vim9_lines =<< trim END
2067 vim9script
2068 var local = 'local'
2069 g:global = 'global'
2070 export var exported = 'exported'
2071 export def GetText(): string
2072 return 'text'
2073 enddef
2074 END
2075 writefile(vim9_lines, 'Xvim9_export.vim')
2076
2077 var legacy_lines =<< trim END
2078 import './Xvim9_export.vim' as vim9
2079
2080 call assert_false(exists('vim9'))
2081 call assert_false(exists('local'))
2082 call assert_false(exists('s:vim9.local'))
2083 call assert_equal('global', global)
2084 call assert_equal('global', g:global)
2085 call assert_false(exists('exported'))
2086 call assert_false(exists('s:exported'))
2087 call assert_false(exists('*GetText'))
2088
2089 " imported symbol is script-local
2090 call assert_equal('exported', s:vim9.exported)
2091 call assert_equal('text', s:vim9.GetText())
2092 END
2093 writefile(legacy_lines, 'Xlegacy_script.vim')
2094
2095 source Xlegacy_script.vim
2096 assert_equal('global', g:global)
2097 unlet g:global
2098
2099 delete('Xlegacy_script.vim')
2100 delete('Xvim9_export.vim')
2101enddef
2102
Bram Moolenaard8448622022-01-07 21:39:52 +00002103def Test_cmdline_win()
2104 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
2105 # the command line window.
2106 mkdir('rtp/syntax', 'p')
2107 var export_lines =<< trim END
2108 vim9script
2109 export var That = 'yes'
2110 END
2111 writefile(export_lines, 'rtp/syntax/Xexport.vim')
2112 var import_lines =<< trim END
2113 vim9script
2114 import './Xexport.vim' as exp
2115 echo exp.That
2116 END
2117 writefile(import_lines, 'rtp/syntax/vim.vim')
2118 var save_rtp = &rtp
2119 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
2120 syntax on
2121 augroup CmdWin
2122 autocmd CmdwinEnter * g:got_there = 'yes'
2123 augroup END
2124 # this will open and also close the cmdline window
2125 feedkeys('q:', 'xt')
2126 assert_equal('yes', g:got_there)
2127
2128 augroup CmdWin
2129 au!
2130 augroup END
2131 &rtp = save_rtp
2132 delete('rtp', 'rf')
2133enddef
2134
2135def Test_import_gone_when_sourced_twice()
2136 var exportlines =<< trim END
2137 vim9script
2138 if exists('g:guard')
2139 finish
2140 endif
2141 g:guard = 1
2142 export var name = 'someName'
2143 END
2144 writefile(exportlines, 'XexportScript.vim')
2145
2146 var lines =<< trim END
2147 vim9script
2148 import './XexportScript.vim' as expo
2149 def g:GetName(): string
2150 return expo.name
2151 enddef
2152 END
2153 writefile(lines, 'XscriptImport.vim')
2154 so XscriptImport.vim
2155 assert_equal('someName', g:GetName())
2156
2157 so XexportScript.vim
2158 assert_fails('call g:GetName()', 'E1149:')
2159
2160 delfunc g:GetName
2161 delete('XexportScript.vim')
2162 delete('XscriptImport.vim')
2163 unlet g:guard
2164enddef
2165
Bram Moolenaar160aa862022-01-10 21:29:57 +00002166" test using an auto-loaded function and variable
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002167def Test_vim9_autoload_full_name()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002168 var lines =<< trim END
2169 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002170 export def Gettest(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002171 return 'test'
2172 enddef
2173 g:some#name = 'name'
2174 g:some#dict = {key: 'value'}
2175
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002176 export def Varargs(a1: string, ...l: list<string>): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002177 return a1 .. l[0] .. l[1]
2178 enddef
2179 END
2180
2181 mkdir('Xdir/autoload', 'p')
2182 writefile(lines, 'Xdir/autoload/some.vim')
2183 var save_rtp = &rtp
2184 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2185
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002186 assert_equal('test', g:some#Gettest())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002187 assert_equal('name', g:some#name)
2188 assert_equal('value', g:some#dict.key)
2189 g:some#other = 'other'
2190 assert_equal('other', g:some#other)
2191
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002192 assert_equal('abc', some#Varargs('a', 'b', 'c'))
Bram Moolenaar160aa862022-01-10 21:29:57 +00002193
2194 # upper case script name works
2195 lines =<< trim END
2196 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002197 export def GetOther(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002198 return 'other'
2199 enddef
2200 END
2201 writefile(lines, 'Xdir/autoload/Other.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002202 assert_equal('other', g:Other#GetOther())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002203
2204 delete('Xdir', 'rf')
2205 &rtp = save_rtp
2206enddef
2207
2208def Test_vim9script_autoload()
2209 mkdir('Xdir/autoload', 'p')
2210 var save_rtp = &rtp
2211 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2212
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002213 # when the path has "/autoload/" prefix is not needed
Bram Moolenaar160aa862022-01-10 21:29:57 +00002214 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002215 vim9script
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002216 g:prefixed_loaded += 1
Bram Moolenaar160aa862022-01-10 21:29:57 +00002217
2218 export def Gettest(): string
2219 return 'test'
2220 enddef
2221
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002222 export var name = 'name'
2223
2224 export func GetFunc()
2225 return Gettest() .. 'more' .. s:name
Bram Moolenaar160aa862022-01-10 21:29:57 +00002226 endfunc
2227
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002228 export def GetDef(): string
2229 return Gettest() .. 'more' .. name
2230 enddef
2231
Bram Moolenaar160aa862022-01-10 21:29:57 +00002232 export final fname = 'final'
2233 export const cname = 'const'
2234 END
2235 writefile(lines, 'Xdir/autoload/prefixed.vim')
2236
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002237 g:prefixed_loaded = 0
2238 g:expected_loaded = 0
Bram Moolenaar160aa862022-01-10 21:29:57 +00002239 lines =<< trim END
2240 vim9script
2241 import autoload 'prefixed.vim'
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002242 assert_equal(g:expected_loaded, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002243 assert_equal('test', prefixed.Gettest())
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002244 assert_equal(1, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002245
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002246 assert_equal('testmorename', prefixed.GetFunc())
2247 assert_equal('testmorename', prefixed.GetDef())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002248 assert_equal('name', prefixed.name)
2249 assert_equal('final', prefixed.fname)
2250 assert_equal('const', prefixed.cname)
2251 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002252 v9.CheckScriptSuccess(lines)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002253 # can source it again, autoload script not loaded again
2254 g:expected_loaded = 1
Bram Moolenaar62aec932022-01-29 21:45:34 +00002255 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002256
2257 # can also get the items by autoload name
2258 lines =<< trim END
2259 call assert_equal('test', prefixed#Gettest())
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002260 call assert_equal('testmorename', prefixed#GetFunc())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002261 call assert_equal('name', prefixed#name)
2262 call assert_equal('final', prefixed#fname)
2263 call assert_equal('const', prefixed#cname)
2264 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002265 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002266
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002267 unlet g:prefixed_loaded
2268 unlet g:expected_loaded
2269 delete('Xdir', 'rf')
2270 &rtp = save_rtp
2271enddef
2272
Bram Moolenaard02dce22022-01-18 17:43:04 +00002273def Test_import_autoload_not_exported()
2274 mkdir('Xdir/autoload', 'p')
2275 var save_rtp = &rtp
2276 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2277
2278 # error when using an item that is not exported from an autoload script
2279 var exportLines =<< trim END
2280 vim9script
2281 var notExported = 123
2282 def NotExport()
2283 echo 'nop'
2284 enddef
2285 END
2286 writefile(exportLines, 'Xdir/autoload/notExport1.vim')
2287
2288 var lines =<< trim END
2289 vim9script
2290 import autoload 'notExport1.vim'
2291 echo notExport1.notFound
2292 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002293 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002294
2295 lines =<< trim END
2296 vim9script
2297 import autoload 'notExport1.vim'
2298 echo notExport1.notExported
2299 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002300 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002301
2302 lines =<< trim END
2303 vim9script
2304 import autoload 'notExport1.vim'
2305 echo notExport1.NotFunc()
2306 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002307 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002308
2309 lines =<< trim END
2310 vim9script
2311 import autoload 'notExport1.vim'
2312 echo notExport1.NotExport()
2313 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002314 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002315
2316 lines =<< trim END
2317 vim9script
2318 import autoload 'notExport1.vim'
2319 echo 'text'->notExport1.NotFunc()
2320 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002321 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002322
2323 lines =<< trim END
2324 vim9script
2325 import autoload 'notExport1.vim'
2326 echo 'text'->notExport1.NotExport()
2327 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002328 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002329
2330 # using a :def function we use a different autoload script every time so that
2331 # the function is compiled without the script loaded
2332 writefile(exportLines, 'Xdir/autoload/notExport2.vim')
2333 lines =<< trim END
2334 vim9script
2335 import autoload 'notExport2.vim'
2336 def Testit()
2337 echo notExport2.notFound
2338 enddef
2339 Testit()
2340 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002341 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport2#notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002342
2343 writefile(exportLines, 'Xdir/autoload/notExport3.vim')
2344 lines =<< trim END
2345 vim9script
2346 import autoload 'notExport3.vim'
2347 def Testit()
2348 echo notExport3.notExported
2349 enddef
2350 Testit()
2351 END
2352 # don't get E1049 because it is too complicated to figure out
Bram Moolenaar62aec932022-01-29 21:45:34 +00002353 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport3#notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002354
2355 writefile(exportLines, 'Xdir/autoload/notExport4.vim')
2356 lines =<< trim END
2357 vim9script
2358 import autoload 'notExport4.vim'
2359 def Testit()
2360 echo notExport4.NotFunc()
2361 enddef
2362 Testit()
2363 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002364 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport4#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002365
2366 writefile(exportLines, 'Xdir/autoload/notExport5.vim')
2367 lines =<< trim END
2368 vim9script
2369 import autoload 'notExport5.vim'
2370 def Testit()
2371 echo notExport5.NotExport()
2372 enddef
2373 Testit()
2374 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002375 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport5#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002376
2377 writefile(exportLines, 'Xdir/autoload/notExport6.vim')
2378 lines =<< trim END
2379 vim9script
2380 import autoload 'notExport6.vim'
2381 def Testit()
2382 echo 'text'->notExport6.NotFunc()
2383 enddef
2384 Testit()
2385 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002386 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport6#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002387
2388 writefile(exportLines, 'Xdir/autoload/notExport7.vim')
2389 lines =<< trim END
2390 vim9script
2391 import autoload 'notExport7.vim'
2392 def Testit()
2393 echo 'text'->notExport7.NotExport()
2394 enddef
2395 Testit()
2396 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002397 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport7#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002398
2399 delete('Xdir', 'rf')
2400 &rtp = save_rtp
2401enddef
2402
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002403def Test_vim9script_autoload_call()
2404 mkdir('Xdir/autoload', 'p')
2405 var save_rtp = &rtp
2406 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2407
2408 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002409 vim9script
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002410
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002411 export def RetArg(arg: string): string
2412 return arg
2413 enddef
2414
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002415 export def Getother()
2416 g:result = 'other'
2417 enddef
2418 END
Bram Moolenaar5d982692022-01-12 15:15:27 +00002419 writefile(lines, 'Xdir/autoload/another.vim')
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002420
2421 lines =<< trim END
2422 vim9script
Bram Moolenaar5d982692022-01-12 15:15:27 +00002423 import autoload 'another.vim'
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002424
2425 # compile this before 'another.vim' is loaded
2426 def CallAnother()
2427 assert_equal('foo', 'foo'->another.RetArg())
2428 enddef
2429 CallAnother()
2430
Bram Moolenaar5d982692022-01-12 15:15:27 +00002431 call another.Getother()
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002432 assert_equal('other', g:result)
Bram Moolenaar3d8e25a2022-01-22 11:00:02 +00002433
2434 assert_equal('arg', call('another.RetArg', ['arg']))
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00002435
2436 verbose function another.Getother
2437 # should we disallow this?
2438 verbose function another#Getother
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002439 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002440 v9.CheckScriptSuccess(lines)
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002441
2442 unlet g:result
Bram Moolenaar160aa862022-01-10 21:29:57 +00002443 delete('Xdir', 'rf')
2444 &rtp = save_rtp
2445enddef
2446
Bram Moolenaarb697dc22022-01-22 11:27:29 +00002447def Test_vim9script_noclear_autoload()
2448 mkdir('Xdir/autoload', 'p')
2449 var save_rtp = &rtp
2450 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2451
2452 var lines =<< trim END
2453 vim9script
2454 export def Func(): string
2455 return 'called'
2456 enddef
2457 g:double_loaded = 'yes'
2458 END
2459 writefile(lines, 'Xdir/autoload/double.vim')
2460
2461 lines =<< trim END
2462 vim9script noclear
2463 if exists('g:script_loaded')
2464 finish
2465 endif
2466 g:script_loaded = true
2467
2468 import autoload 'double.vim'
2469 nnoremap <F3> <ScriptCmd>g:result = double.Func()<CR>
2470 END
2471 g:double_loaded = 'no'
2472 writefile(lines, 'Xloaddouble')
2473 source Xloaddouble
2474 assert_equal('no', g:double_loaded)
2475 assert_equal(true, g:script_loaded)
2476 source Xloaddouble
2477 feedkeys("\<F3>", 'xt')
2478 assert_equal('called', g:result)
2479 assert_equal('yes', g:double_loaded)
2480
2481 delete('Xloaddouble')
2482 unlet g:double_loaded
2483 unlet g:script_loaded
2484 unlet g:result
2485 delete('Xdir', 'rf')
2486 &rtp = save_rtp
2487enddef
2488
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002489def Test_vim9script_autoload_duplicate()
2490 mkdir('Xdir/autoload', 'p')
2491
2492 var lines =<< trim END
2493 vim9script
2494
2495 export def Func()
2496 enddef
2497
2498 def Func()
2499 enddef
2500 END
2501 writefile(lines, 'Xdir/autoload/dupfunc.vim')
2502 assert_fails('source Xdir/autoload/dupfunc.vim', 'E1073:')
2503
2504 lines =<< trim END
2505 vim9script
2506
2507 def Func()
2508 enddef
2509
2510 export def Func()
2511 enddef
2512 END
2513 writefile(lines, 'Xdir/autoload/dup2func.vim')
2514 assert_fails('source Xdir/autoload/dup2func.vim', 'E1073:')
2515
2516 lines =<< trim END
2517 vim9script
2518
2519 def Func()
2520 enddef
2521
2522 export var Func = 'asdf'
2523 END
2524 writefile(lines, 'Xdir/autoload/dup3func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002525 assert_fails('source Xdir/autoload/dup3func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002526
2527 lines =<< trim END
2528 vim9script
2529
2530 export var Func = 'asdf'
2531
2532 def Func()
2533 enddef
2534 END
2535 writefile(lines, 'Xdir/autoload/dup4func.vim')
2536 assert_fails('source Xdir/autoload/dup4func.vim', 'E707:')
2537
2538 lines =<< trim END
2539 vim9script
2540
2541 var Func = 'asdf'
2542
2543 export def Func()
2544 enddef
2545 END
2546 writefile(lines, 'Xdir/autoload/dup5func.vim')
2547 assert_fails('source Xdir/autoload/dup5func.vim', 'E707:')
2548
2549 lines =<< trim END
2550 vim9script
2551
2552 export def Func()
2553 enddef
2554
2555 var Func = 'asdf'
2556 END
2557 writefile(lines, 'Xdir/autoload/dup6func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002558 assert_fails('source Xdir/autoload/dup6func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002559
2560 delete('Xdir', 'rf')
2561enddef
2562
Bram Moolenaar2017d6f2022-01-20 19:38:46 +00002563def Test_autoload_missing_function_name()
2564 mkdir('Xdir/autoload', 'p')
2565
2566 var lines =<< trim END
2567 vim9script
2568
2569 def loadme#()
2570 enddef
2571 END
2572 writefile(lines, 'Xdir/autoload/loadme.vim')
2573 assert_fails('source Xdir/autoload/loadme.vim', 'E129:')
2574
2575 delete('Xdir', 'rf')
2576enddef
2577
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002578def Test_autoload_name_wrong()
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002579 var lines =<< trim END
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002580 def Xscriptname#Func()
2581 enddef
2582 END
2583 writefile(lines, 'Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002584 v9.CheckScriptFailure(lines, 'E746:')
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00002585 delete('Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002586
2587 mkdir('Xdir/autoload', 'p')
2588 lines =<< trim END
2589 vim9script
2590 def somescript#Func()
2591 enddef
2592 END
2593 writefile(lines, 'Xdir/autoload/somescript.vim')
2594 assert_fails('source Xdir/autoload/somescript.vim', 'E1263:')
2595
2596 delete('Xdir', 'rf')
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002597enddef
2598
Bram Moolenaard041f422022-01-12 19:54:00 +00002599def Test_import_autoload_postponed()
2600 mkdir('Xdir/autoload', 'p')
2601 var save_rtp = &rtp
2602 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2603
2604 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002605 vim9script
Bram Moolenaard041f422022-01-12 19:54:00 +00002606
2607 g:loaded_postponed = 'true'
2608 export var variable = 'bla'
2609 export def Function(): string
2610 return 'bla'
2611 enddef
2612 END
2613 writefile(lines, 'Xdir/autoload/postponed.vim')
2614
2615 lines =<< trim END
2616 vim9script
2617
2618 import autoload 'postponed.vim'
2619 def Tryit()
2620 echo postponed.variable
2621 echo postponed.Function()
2622 enddef
2623 defcompile
2624 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002625 v9.CheckScriptSuccess(lines)
Bram Moolenaard041f422022-01-12 19:54:00 +00002626 assert_false(exists('g:loaded_postponed'))
Bram Moolenaar62aec932022-01-29 21:45:34 +00002627 v9.CheckScriptSuccess(lines + ['Tryit()'])
Bram Moolenaard041f422022-01-12 19:54:00 +00002628 assert_equal('true', g:loaded_postponed)
2629
2630 unlet g:loaded_postponed
2631 delete('Xdir', 'rf')
2632 &rtp = save_rtp
2633enddef
2634
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002635def Test_import_autoload_override()
2636 mkdir('Xdir/autoload', 'p')
2637 var save_rtp = &rtp
2638 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2639 test_override('autoload', 1)
2640
2641 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002642 vim9script
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002643
2644 g:loaded_override = 'true'
2645 export var variable = 'bla'
2646 export def Function(): string
2647 return 'bla'
2648 enddef
2649 END
2650 writefile(lines, 'Xdir/autoload/override.vim')
2651
2652 lines =<< trim END
2653 vim9script
2654
2655 import autoload 'override.vim'
2656 assert_equal('true', g:loaded_override)
2657
2658 def Tryit()
2659 echo override.doesNotExist
2660 enddef
2661 defcompile
2662 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002663 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1)
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002664
2665 test_override('autoload', 0)
2666 unlet g:loaded_override
2667 delete('Xdir', 'rf')
2668 &rtp = save_rtp
2669enddef
2670
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002671def Test_autoload_mapping()
2672 mkdir('Xdir/autoload', 'p')
2673 var save_rtp = &rtp
2674 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2675
2676 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002677 vim9script
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002678
2679 g:toggle_loaded = 'yes'
2680
2681 export def Toggle(): string
2682 return ":g:toggle_called = 'yes'\<CR>"
2683 enddef
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002684 export def Doit()
2685 g:doit_called = 'yes'
2686 enddef
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002687 END
2688 writefile(lines, 'Xdir/autoload/toggle.vim')
2689
2690 lines =<< trim END
2691 vim9script
2692
2693 import autoload 'toggle.vim'
2694
2695 nnoremap <silent> <expr> tt toggle.Toggle()
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002696 nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR>
2697 nnoremap <silent> yy <Cmd>toggle.Doit()<CR>
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002698 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002699 v9.CheckScriptSuccess(lines)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002700 assert_false(exists("g:toggle_loaded"))
2701 assert_false(exists("g:toggle_called"))
Bram Moolenaar6079da72022-01-18 14:16:59 +00002702 assert_match('\d A: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002703
2704 feedkeys("tt", 'xt')
2705 assert_equal('yes', g:toggle_loaded)
2706 assert_equal('yes', g:toggle_called)
Bram Moolenaar6079da72022-01-18 14:16:59 +00002707 assert_match('\d: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002708
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002709 feedkeys("xx", 'xt')
2710 assert_equal('yes', g:doit_called)
2711
2712 assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle')
2713
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002714 nunmap tt
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002715 nunmap xx
2716 nunmap yy
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002717 unlet g:toggle_loaded
2718 unlet g:toggle_called
2719 delete('Xdir', 'rf')
2720 &rtp = save_rtp
2721enddef
2722
Bram Moolenaar160aa862022-01-10 21:29:57 +00002723def Test_vim9script_autoload_fails()
2724 var lines =<< trim END
2725 vim9script autoload
2726 var n = 0
2727 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002728 v9.CheckScriptFailure(lines, 'E475: Invalid argument: autoload')
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002729
2730 lines =<< trim END
2731 vim9script noclear noclear
2732 var n = 0
2733 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002734 v9.CheckScriptFailure(lines, 'E983: Duplicate argument: noclear')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +01002735
2736 lines =<< trim END
2737 vim9script noclears
2738 var n = 0
2739 END
2740 v9.CheckScriptFailure(lines, 'E475: Invalid argument: noclears')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002741enddef
2742
2743def Test_import_autoload_fails()
2744 var lines =<< trim END
2745 vim9script
2746 import autoload autoload 'prefixed.vim'
2747 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002748 v9.CheckScriptFailure(lines, 'E121: Undefined variable: autoload')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002749
2750 lines =<< trim END
2751 vim9script
Bram Moolenaar1836d612022-01-18 13:14:47 +00002752 import autoload './doesNotExist.vim'
Bram Moolenaar160aa862022-01-10 21:29:57 +00002753 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002754 v9.CheckScriptFailure(lines, 'E282:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002755
2756 lines =<< trim END
2757 vim9script
2758 import autoload '/dir/doesNotExist.vim'
2759 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002760 v9.CheckScriptFailure(lines, 'E282:', 2)
2761
2762 lines =<< trim END
2763 vim9script
2764 import autoload '../testdir'
2765 END
2766 v9.CheckScriptFailure(lines, 'E17:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002767
2768 lines =<< trim END
2769 vim9script
2770 import autoload 'doesNotExist.vim'
2771 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002772 v9.CheckScriptFailure(lines, 'E1053: Could not import "doesNotExist.vim"')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002773enddef
2774
2775" test disassembling an auto-loaded function starting with "debug"
2776def Test_vim9_autoload_disass()
2777 mkdir('Xdir/autoload', 'p')
2778 var save_rtp = &rtp
2779 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2780
2781 var lines =<< trim END
2782 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002783 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002784 return 'debug'
2785 enddef
2786 END
2787 writefile(lines, 'Xdir/autoload/debugit.vim')
2788
2789 lines =<< trim END
2790 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002791 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002792 return 'profile'
2793 enddef
2794 END
2795 writefile(lines, 'Xdir/autoload/profileit.vim')
2796
2797 lines =<< trim END
2798 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002799 assert_equal('debug', debugit#Test())
2800 disass debugit#Test
2801 assert_equal('profile', profileit#Test())
2802 disass profileit#Test
Bram Moolenaar160aa862022-01-10 21:29:57 +00002803 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002804 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002805
2806 delete('Xdir', 'rf')
2807 &rtp = save_rtp
2808enddef
2809
2810" test using a vim9script that is auto-loaded from an autocmd
2811def Test_vim9_aucmd_autoload()
2812 var lines =<< trim END
2813 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002814 export def Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002815 echomsg getreg('"')
2816 enddef
2817 END
2818
2819 mkdir('Xdir/autoload', 'p')
2820 writefile(lines, 'Xdir/autoload/foo.vim')
2821 var save_rtp = &rtp
2822 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2823 augroup test
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002824 autocmd TextYankPost * call foo#Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002825 augroup END
2826
2827 normal Y
2828
2829 augroup test
2830 autocmd!
2831 augroup END
2832 delete('Xdir', 'rf')
2833 &rtp = save_rtp
2834enddef
2835
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002836" test using a autoloaded file that is case sensitive
2837def Test_vim9_autoload_case_sensitive()
2838 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002839 vim9script
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002840 export def CaseSensitive(): string
2841 return 'done'
2842 enddef
2843 END
2844
2845 mkdir('Xdir/autoload', 'p')
2846 writefile(lines, 'Xdir/autoload/CaseSensitive.vim')
2847 var save_rtp = &rtp
2848 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2849
2850 lines =<< trim END
2851 vim9script
2852 import autoload 'CaseSensitive.vim'
2853 assert_equal('done', CaseSensitive.CaseSensitive())
2854 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002855 v9.CheckScriptSuccess(lines)
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002856
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002857 if !has('fname_case')
2858 lines =<< trim END
2859 vim9script
2860 import autoload 'CaseSensitive.vim'
2861 import autoload 'casesensitive.vim'
2862 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002863 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002864 endif
2865
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002866 delete('Xdir', 'rf')
2867 &rtp = save_rtp
2868enddef
2869
Bram Moolenaar160aa862022-01-10 21:29:57 +00002870" This was causing a crash because suppress_errthrow wasn't reset.
2871def Test_vim9_autoload_error()
2872 var lines =<< trim END
2873 vim9script
2874 def crash#func()
2875 try
2876 for x in List()
2877 endfor
2878 catch
2879 endtry
2880 g:ok = true
2881 enddef
2882 fu List()
2883 invalid
2884 endfu
2885 try
2886 alsoinvalid
2887 catch /wontmatch/
2888 endtry
2889 END
2890 call mkdir('Xruntime/autoload', 'p')
2891 call writefile(lines, 'Xruntime/autoload/crash.vim')
2892
2893 # run in a separate Vim to avoid the side effects of assert_fails()
2894 lines =<< trim END
2895 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
2896 call crash#func()
2897 call writefile(['ok'], 'Xdidit')
2898 qall!
2899 END
2900 writefile(lines, 'Xscript')
Bram Moolenaar62aec932022-01-29 21:45:34 +00002901 g:RunVim([], [], '-S Xscript')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002902 assert_equal(['ok'], readfile('Xdidit'))
2903
2904 delete('Xdidit')
2905 delete('Xscript')
2906 delete('Xruntime', 'rf')
2907
2908 lines =<< trim END
2909 vim9script
2910 var foo#bar = 'asdf'
2911 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002912 v9.CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002913enddef
2914
Bram Moolenaar753885b2022-08-24 16:30:36 +01002915def Test_vim9_import_symlink()
2916 if !has('unix')
2917 CheckUnix
2918 else
2919 mkdir('Xto/plugin', 'p')
2920 var lines =<< trim END
2921 vim9script
2922 import autoload 'bar.vim'
2923 g:resultFunc = bar.Func()
2924 g:resultValue = bar.value
2925 END
2926 writefile(lines, 'Xto/plugin/foo.vim')
2927
2928 mkdir('Xto/autoload', 'p')
2929 lines =<< trim END
2930 vim9script
2931 export def Func(): string
2932 return 'func'
2933 enddef
2934 export var value = 'val'
2935 END
2936 writefile(lines, 'Xto/autoload/bar.vim')
2937
2938 var save_rtp = &rtp
2939 &rtp = getcwd() .. '/Xfrom'
2940 system('ln -s ' .. getcwd() .. '/Xto Xfrom')
2941
2942 source Xfrom/plugin/foo.vim
2943 assert_equal('func', g:resultFunc)
2944 assert_equal('val', g:resultValue)
2945
2946 var infoTo = getscriptinfo()->filter((_, v) => v.name =~ 'Xto/autoload/bar')
2947 var infoFrom = getscriptinfo()->filter((_, v) => v.name =~ 'Xfrom/autoload/bar')
2948 assert_equal(1, len(infoTo))
2949 assert_equal(1, len(infoFrom))
2950 assert_equal(infoTo[0].sid, infoFrom[0].sourced)
Bram Moolenaar7ea9fcb2022-08-24 17:46:12 +01002951 var output: string
2952 redir => output
2953 scriptnames
2954 redir END
2955 assert_match(infoFrom[0].sid .. '->' .. infoFrom[0].sourced .. '.*Xfrom', output)
Bram Moolenaar753885b2022-08-24 16:30:36 +01002956
2957 unlet g:resultFunc
2958 unlet g:resultValue
2959 &rtp = save_rtp
2960 delete('Xto', 'rf')
2961 delete('Xfrom', 'rf')
2962 endif
2963enddef
2964
Bram Moolenaard8448622022-01-07 21:39:52 +00002965
2966" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker