blob: 415a7a99a45f13eab821d13488f4226041cbfe37 [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])
Bram Moolenaar89445512022-04-14 12:58:23 +0100735 feedkeys("\<F3>", "xt")
736 assert_equal(42, g:result)
737
738 unlet g:result
Bram Moolenaar648dd882022-04-14 21:36:15 +0100739 delete('XrelautoloadExport.vim')
Bram Moolenaar89445512022-04-14 12:58:23 +0100740 delete('Xmapscript.vim')
741 nunmap <F3>
Bram Moolenaard8448622022-01-07 21:39:52 +0000742enddef
743
Bram Moolenaar5cb53b72022-05-26 19:54:05 +0100744def Test_autoload_import_var()
745 # variable name starts with "autoload"
746 var lines =<< trim END
747 vim9script
748 var autoloaded = "Xtest.vim"
749 import autoloaded
750 END
751 v9.CheckScriptFailure(lines, 'E1053: Could not import "Xtest.vim')
752enddef
753
Bram Moolenaar648dd882022-04-14 21:36:15 +0100754def Test_use_autoload_import_in_mapping()
755 var lines =<< trim END
756 vim9script
757 export def Func()
758 g:result = 49
759 enddef
760 END
761 mkdir('Xdir/autoload', 'p')
762 writefile(lines, 'Xdir/autoload/XautoloadExport.vim')
763 var save_rtp = &rtp
764 exe 'set rtp^=' .. getcwd() .. '/Xdir'
765
766 lines =<< trim END
767 vim9script
768 import autoload 'XautoloadExport.vim' as some
769 nnoremap <F3> :call <SID>some.Func()<CR>
770 END
771 writefile(lines, 'Xmapscript.vim')
772
773 source Xmapscript.vim
774 assert_match('\d\+ A: .*autoload/XautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
775 feedkeys("\<F3>", "xt")
776 assert_equal(49, g:result)
777
778 unlet g:result
779 delete('Xmapscript.vim')
780 nunmap <F3>
781 delete('Xdir', 'rf')
782 &rtp = save_rtp
783enddef
784
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000785def Test_use_import_in_command_completion()
Bram Moolenaar15d16352022-01-17 20:09:08 +0000786 var lines =<< trim END
787 vim9script
788 export def Complete(..._): list<string>
789 return ['abcd']
790 enddef
791 END
792 writefile(lines, 'Xscript.vim')
793
794 lines =<< trim END
795 vim9script
796 import './Xscript.vim'
797
798 command -nargs=1 -complete=customlist,Xscript.Complete Cmd echo 'ok'
799 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
800 assert_equal('#Cmd abcd', @:)
801 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000802 v9.CheckScriptSuccess(lines)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000803
804 delcommand Cmd
805 delete('Xscript.vim')
806enddef
807
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100808def Test_use_import_with_funcref_in_command_completion()
809 var lines =<< trim END
810 vim9script
811 export def Complete(..._): list<string>
812 return ['abcd']
813 enddef
814 END
815 writefile(lines, 'Xscript.vim')
816
817 lines =<< trim END
818 vim9script
819 import './Xscript.vim'
820
821 var Ref = Xscript.Complete
822 exe "command -nargs=1 -complete=customlist," .. expand('<SID>') .. "Ref Cmd echo 'ok'"
823 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
824 assert_equal('#Cmd abcd', @:)
825 END
826 v9.CheckScriptSuccess(lines)
827
828 delcommand Cmd
829 delete('Xscript.vim')
830enddef
831
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000832def Test_use_autoload_import_in_insert_completion()
833 mkdir('Xdir/autoload', 'p')
834 var save_rtp = &rtp
835 exe 'set rtp^=' .. getcwd() .. '/Xdir'
836
837 var lines =<< trim END
838 vim9script
839 export def ThesaurusFunc(findbase: bool, _): any
840 if findbase
841 return 1
842 endif
843 return [
844 'check',
845 'experiment',
846 'test',
847 'verification'
848 ]
849 enddef
850 g:completion_loaded = 'yes'
851 END
852 writefile(lines, 'Xdir/autoload/completion.vim')
853
854 new
855 lines =<< trim END
856 vim9script
857 g:completion_loaded = 'no'
858 import autoload 'completion.vim'
859 set thesaurusfunc=completion.ThesaurusFunc
860 assert_equal('no', g:completion_loaded)
861 feedkeys("i\<C-X>\<C-T>\<C-N>\<Esc>", 'xt')
862 assert_equal('experiment', getline(1))
863 assert_equal('yes', g:completion_loaded)
864 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000865 v9.CheckScriptSuccess(lines)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000866
867 set thesaurusfunc=
868 bwipe!
869 delete('Xdir', 'rf')
870 &rtp = save_rtp
871enddef
872
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000873def Test_use_autoload_import_partial_in_opfunc()
874 mkdir('Xdir/autoload', 'p')
875 var save_rtp = &rtp
876 exe 'set rtp^=' .. getcwd() .. '/Xdir'
877
878 var lines =<< trim END
879 vim9script
880 export def Opfunc(..._)
881 g:opfunc_called = 'yes'
882 enddef
883 END
884 writefile(lines, 'Xdir/autoload/opfunc.vim')
885
886 new
887 lines =<< trim END
888 vim9script
889 import autoload 'opfunc.vim'
890 nnoremap <expr> <F3> TheFunc()
891 def TheFunc(): string
892 &operatorfunc = function('opfunc.Opfunc', [0])
893 return 'g@'
894 enddef
895 feedkeys("\<F3>l", 'xt')
896 assert_equal('yes', g:opfunc_called)
897 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000898 v9.CheckScriptSuccess(lines)
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000899
900 set opfunc=
901 bwipe!
902 delete('Xdir', 'rf')
Bram Moolenaar06b77222022-01-25 15:51:56 +0000903 nunmap <F3>
904 &rtp = save_rtp
905enddef
906
907def Test_set_opfunc_to_autoload_func_directly()
908 mkdir('Xdir/autoload', 'p')
909 var save_rtp = &rtp
910 exe 'set rtp^=' .. getcwd() .. '/Xdir'
911
912 var lines =<< trim END
913 vim9script
914 export def Opfunc(..._)
915 g:opfunc_called = 'yes'
916 enddef
917 END
918 writefile(lines, 'Xdir/autoload/opfunc.vim')
919
920 new
921 lines =<< trim END
922 vim9script
923 import autoload 'opfunc.vim'
924 nnoremap <expr> <F3> TheFunc()
925 def TheFunc(): string
926 &operatorfunc = opfunc.Opfunc
927 return 'g@'
928 enddef
929 feedkeys("\<F3>l", 'xt')
930 assert_equal('yes', g:opfunc_called)
931 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000932 v9.CheckScriptSuccess(lines)
Bram Moolenaar06b77222022-01-25 15:51:56 +0000933
934 set opfunc=
935 bwipe!
936 delete('Xdir', 'rf')
937 nunmap <F3>
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000938 &rtp = save_rtp
939enddef
940
Bram Moolenaare70dd112022-01-21 16:31:11 +0000941def Test_use_autoload_import_in_fold_expression()
942 mkdir('Xdir/autoload', 'p')
943 var save_rtp = &rtp
944 exe 'set rtp^=' .. getcwd() .. '/Xdir'
945
946 var lines =<< trim END
947 vim9script
948 export def Expr(): string
949 return getline(v:lnum) =~ '^#' ? '>1' : '1'
950 enddef
Bram Moolenaar9530b582022-01-22 13:39:08 +0000951 export def Text(): string
952 return 'fold text'
953 enddef
Bram Moolenaare70dd112022-01-21 16:31:11 +0000954 g:fold_loaded = 'yes'
955 END
956 writefile(lines, 'Xdir/autoload/fold.vim')
957
958 lines =<< trim END
959 vim9script
960 import autoload 'fold.vim'
961 &foldexpr = 'fold.Expr()'
Bram Moolenaar9530b582022-01-22 13:39:08 +0000962 &foldtext = 'fold.Text()'
Bram Moolenaare70dd112022-01-21 16:31:11 +0000963 &foldmethod = 'expr'
964 &debug = 'throw'
965 END
966 new
967 setline(1, ['# one', 'text', '# two', 'text'])
968 g:fold_loaded = 'no'
Bram Moolenaar62aec932022-01-29 21:45:34 +0000969 v9.CheckScriptSuccess(lines)
Bram Moolenaare70dd112022-01-21 16:31:11 +0000970 assert_equal('no', g:fold_loaded)
971 redraw
972 assert_equal('yes', g:fold_loaded)
973
974 # Check that script context of 'foldexpr' is copied to another buffer.
975 edit! otherfile
976 redraw
977
Bram Moolenaar9530b582022-01-22 13:39:08 +0000978 set foldexpr= foldtext& foldmethod& debug=
Bram Moolenaare70dd112022-01-21 16:31:11 +0000979 bwipe!
980 delete('Xdir', 'rf')
981 &rtp = save_rtp
982enddef
983
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100984def Test_autoload_import_relative()
985 var lines =<< trim END
986 vim9script
987
988 g:loaded = 'yes'
989 export def RelFunc(): string
990 return 'relfunc'
991 enddef
992 def NotExported()
993 echo 'not'
994 enddef
995
996 export var someText = 'some text'
997 var notexp = 'bad'
998 END
999 writefile(lines, 'XimportRel.vim')
1000 writefile(lines, 'XimportRel2.vim')
1001 writefile(lines, 'XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +01001002 writefile(lines, 'XimportRel4.vim')
1003 writefile(lines, 'XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001004
1005 lines =<< trim END
1006 vim9script
1007 g:loaded = 'no'
1008 import autoload './XimportRel.vim'
1009 assert_equal('no', g:loaded)
1010
1011 def AFunc(): string
1012 var res = ''
1013 res ..= XimportRel.RelFunc()
1014 res ..= '/'
1015 res ..= XimportRel.someText
1016 XimportRel.someText = 'from AFunc'
1017 return res
1018 enddef
1019 # script not loaded when compiling
1020 defcompile
1021 assert_equal('no', g:loaded)
1022
1023 assert_equal('relfunc/some text', AFunc())
1024 assert_equal('yes', g:loaded)
1025 unlet g:loaded
1026
1027 assert_equal('from AFunc', XimportRel.someText)
1028 XimportRel.someText = 'from script'
1029 assert_equal('from script', XimportRel.someText)
1030 END
1031 v9.CheckScriptSuccess(lines)
1032
1033 lines =<< trim END
1034 vim9script
1035 import autoload './XimportRel.vim'
1036 echo XimportRel.NotExported()
1037 END
1038 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 3)
1039
1040 lines =<< trim END
1041 vim9script
1042 import autoload './XimportRel.vim'
1043 echo XimportRel.notexp
1044 END
1045 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
1046
1047 lines =<< trim END
1048 vim9script
1049 import autoload './XimportRel.vim'
1050 XimportRel.notexp = 'bad'
1051 END
1052 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
1053
1054 lines =<< trim END
1055 vim9script
1056 import autoload './XimportRel.vim'
1057 def Func()
1058 echo XimportRel.NotExported()
1059 enddef
1060 Func()
1061 END
1062 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 1)
1063
1064 lines =<< trim END
1065 vim9script
1066 import autoload './XimportRel.vim'
1067 def Func()
1068 echo XimportRel.notexp
1069 enddef
1070 Func()
1071 END
1072 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1073
Bram Moolenaar10611952022-04-03 21:11:34 +01001074 # Same, script not imported before
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001075 lines =<< trim END
1076 vim9script
Bram Moolenaar10611952022-04-03 21:11:34 +01001077 import autoload './XimportRel4.vim'
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001078 def Func()
Bram Moolenaar10611952022-04-03 21:11:34 +01001079 echo XimportRel4.notexp
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001080 enddef
1081 Func()
1082 END
1083 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1084
Bram Moolenaar10611952022-04-03 21:11:34 +01001085 # does not fail if the script wasn't loaded yet and only compiling
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001086 g:loaded = 'no'
1087 lines =<< trim END
1088 vim9script
1089 import autoload './XimportRel2.vim'
1090 def Func()
1091 echo XimportRel2.notexp
1092 enddef
1093 defcompile
1094 END
1095 v9.CheckScriptSuccess(lines)
1096 assert_equal('no', g:loaded)
1097
Bram Moolenaar10611952022-04-03 21:11:34 +01001098 lines =<< trim END
1099 vim9script
1100 import autoload './XimportRel.vim'
1101 def Func()
1102 XimportRel.notexp = 'bad'
1103 enddef
1104 Func()
1105 END
1106 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1107
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001108 # fails with a not loaded import
1109 lines =<< trim END
1110 vim9script
1111 import autoload './XimportRel3.vim'
1112 def Func()
1113 XimportRel3.notexp = 'bad'
1114 enddef
1115 Func()
1116 END
1117 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1118 assert_equal('yes', g:loaded)
1119 unlet g:loaded
1120
Bram Moolenaar10611952022-04-03 21:11:34 +01001121 lines =<< trim END
1122 vim9script
1123 import autoload './XimportRel5.vim'
1124 def Func()
1125 XimportRel5.nosuchvar = 'bad'
1126 enddef
1127 Func()
1128 END
1129 v9.CheckScriptFailure(lines, 'E121: Undefined variable: nosuchvar', 1)
1130 unlet g:loaded
1131
1132 # nasty: delete script after compiling function
1133 writefile(['vim9script'], 'XimportRelDel.vim')
1134 lines =<< trim END
1135 vim9script
1136
1137 import autoload './XimportRelDel.vim'
1138 def DoIt()
1139 echo XimportRelDel.var
1140 enddef
1141 defcompile
1142 delete('XimportRelDel.vim')
1143 DoIt()
1144 END
Bram Moolenaar242c1522022-04-03 21:52:51 +01001145 v9.CheckScriptFailure(lines, 'E484:')
Bram Moolenaar10611952022-04-03 21:11:34 +01001146
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001147 delete('XimportRel.vim')
1148 delete('XimportRel2.vim')
1149 delete('XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +01001150 delete('XimportRel4.vim')
1151 delete('XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001152enddef
1153
Bram Moolenaarccbfd482022-03-31 16:18:23 +01001154def Test_autoload_import_relative_autoload_dir()
1155 mkdir('autoload', 'p')
1156 var lines =<< trim END
1157 vim9script
1158 export def Bar()
1159 g:called_bar = 'yes'
1160 enddef
1161 END
1162 writefile(lines, 'autoload/script.vim')
1163
1164 lines =<< trim END
1165 vim9script
1166 import autoload './autoload/script.vim'
1167 def Foo()
1168 script.Bar()
1169 enddef
1170 Foo()
1171 assert_equal('yes', g:called_bar)
1172 END
1173 v9.CheckScriptSuccess(lines)
1174
1175 unlet g:called_bar
1176 delete('autoload', 'rf')
1177enddef
1178
Bram Moolenaaraac12da2022-04-24 21:33:20 +01001179def Test_autoload_import_deleted()
1180 var lines =<< trim END
1181 vim9script
1182 export const FOO = 1
1183 END
1184 writefile(lines, 'Xa.vim')
1185
1186 lines =<< trim END
1187 vim9script
1188 import autoload './Xa.vim'
1189
1190 delete('Xa.vim')
1191 var x = Xa.FOO
1192 END
1193 v9.CheckScriptFailure(lines, 'E484:')
1194
1195 delete('Xdir', 'rf')
1196enddef
1197
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001198func Test_import_in_diffexpr()
1199 CheckExecutable diff
1200
1201 call Run_Test_import_in_diffexpr()
1202endfunc
1203
1204def Run_Test_import_in_diffexpr()
1205 var lines =<< trim END
1206 vim9script
1207
1208 export def DiffExpr()
1209 # Prepend some text to check diff type detection
1210 writefile(['warning', ' message'], v:fname_out)
1211 silent exe '!diff ' .. v:fname_in .. ' '
1212 .. v:fname_new .. '>>' .. v:fname_out
1213 enddef
1214 END
1215 writefile(lines, 'Xdiffexpr')
1216
1217 lines =<< trim END
1218 vim9script
1219 import './Xdiffexpr' as diff
1220
1221 set diffexpr=diff.DiffExpr()
1222 set diffopt=foldcolumn:0
1223 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001224 v9.CheckScriptSuccess(lines)
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001225
1226 enew!
1227 call setline(1, ['one', 'two', 'three'])
1228 diffthis
1229
1230 botright vert new
1231 call setline(1, ['one', 'two', 'three.'])
1232 diffthis
1233 # we only check if this does not cause errors
1234 redraw
1235
1236 diffoff!
1237 bwipe!
1238 bwipe!
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001239 delete('Xdiffexpr')
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001240enddef
1241
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001242def Test_import_in_patchexpr()
1243 var lines =<< trim END
1244 vim9script
1245 export def TPatch()
1246 call writefile(['output file'], v:fname_out)
1247 enddef
1248 END
1249 writefile(lines, 'Xpatchexpr')
1250
1251 lines =<< trim END
1252 vim9script
1253 import './Xpatchexpr' as patch
1254 set patchexpr=patch.TPatch()
1255 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001256 v9.CheckScriptSuccess(lines)
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001257
1258 call writefile(['input file'], 'Xinput')
1259 call writefile(['diff file'], 'Xdiff')
1260 :%bwipe!
1261 edit Xinput
1262 diffpatch Xdiff
1263 call assert_equal('output file', getline(1))
1264
1265 call delete('Xinput')
1266 call delete('Xdiff')
1267 call delete('Xpatchexpr')
1268 set patchexpr&
1269 :%bwipe!
1270enddef
1271
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001272def Test_import_in_formatexpr()
1273 var lines =<< trim END
1274 vim9script
1275 export def MyFormatExpr(): number
1276 g:did_format = 'yes'
1277 return 0
1278 enddef
1279 END
1280 writefile(lines, 'Xformatter')
1281
1282 lines =<< trim END
1283 vim9script
1284 import './Xformatter' as format
1285 set formatexpr=format.MyFormatExpr()
1286 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001287 v9.CheckScriptSuccess(lines)
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001288
1289 new
1290 setline(1, ['a', 'b', 'c'])
1291 normal gqG
1292 assert_equal('yes', g:did_format)
1293
1294 bwipe!
1295 delete('Xformatter')
1296 unlet g:did_format
1297 set formatexpr=
1298enddef
1299
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001300def Test_import_in_includeexpr()
1301 writefile(['found it'], 'Xthisfile')
1302 new
1303
1304 var lines =<< trim END
1305 vim9script
1306 export def DoSub(): string
1307 return substitute(v:fname, 'that', 'this', '')
1308 enddef
1309 END
1310 writefile(lines, 'Xinclude.vim')
1311
1312 lines =<< trim END
1313 vim9script
1314 import './Xinclude.vim'
1315 set includeexpr=Xinclude.DoSub()
1316 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001317 v9.CheckScriptSuccess(lines)
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001318
1319 setline(1, ['Xthatfile'])
1320 exe "normal \<C-W>f"
1321 assert_equal('Xthisfile', expand('%'))
1322
1323 bwipe!
1324 bwipe!
1325 set includeexpr=
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001326 delete('Xinclude.vim')
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001327 delete('Xthisfile')
1328enddef
1329
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001330def Test_import_in_indentexpr()
1331 var lines =<< trim END
1332 vim9script
1333 export def GetIndent(): number
1334 return 5
1335 enddef
1336 END
1337 writefile(lines, 'Xindenter')
1338
1339 lines =<< trim END
1340 vim9script
1341 import './Xindenter' as indent
1342 set indentexpr=indent.GetIndent()
1343 set debug=throw
1344 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001345 v9.CheckScriptSuccess(lines)
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001346
1347 new
1348 setline(1, 'hello')
1349 normal ==
1350 assert_equal(' hello', getline(1))
1351
1352 bwipe!
1353 set indentexpr= debug=
1354 delete('Xindenter')
1355enddef
1356
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001357func Test_import_in_printexpr()
1358 CheckFeature postscript
1359 call Run_Test_import_in_printexpr()
1360endfunc
1361
1362def Run_Test_import_in_printexpr()
1363 var lines =<< trim END
1364 vim9script
1365 export def PrintFile(): bool
1366 g:printed = 'yes'
1367 delete('v:fname_in')
1368 return false
1369 enddef
1370 END
1371 writefile(lines, 'Xprint.vim')
1372
1373 lines =<< trim END
1374 vim9script
1375 import './Xprint.vim'
1376 set printexpr=Xprint.PrintFile()
1377 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001378 v9.CheckScriptSuccess(lines)
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001379
1380 help
1381 hardcopy dummy args
1382 assert_equal('yes', g:printed)
1383
1384 delete('Xprint.vim')
1385 set printexpr=
1386enddef
1387
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001388def Test_import_in_charconvert()
1389 var lines =<< trim END
1390 vim9script
1391 export def MakeUpper(): bool
1392 var data = readfile(v:fname_in)
1393 map(data, 'toupper(v:val)')
1394 writefile(data, v:fname_out)
1395 return false # success
1396 enddef
1397 END
1398 writefile(lines, 'Xconvert.vim')
1399
1400 lines =<< trim END
1401 vim9script
1402 import './Xconvert.vim' as conv
1403 set charconvert=conv.MakeUpper()
1404 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001405 v9.CheckScriptSuccess(lines)
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001406
1407 writefile(['one', 'two'], 'Xfile')
1408 new Xfile
1409 write ++enc=ucase Xfile1
1410 assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1411
1412 delete('Xfile')
1413 delete('Xfile1')
1414 delete('Xconvert.vim')
1415 bwipe!
1416 set charconvert&
1417enddef
1418
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001419func Test_import_in_spellsuggest_expr()
1420 CheckFeature spell
1421 call Run_Test_import_in_spellsuggest_expr()
1422endfunc
1423
1424def Run_Test_import_in_spellsuggest_expr()
1425 var lines =<< trim END
1426 vim9script
1427 export def MySuggest(): list<any>
1428 return [['Fox', 8], ['Fop', 9]]
1429 enddef
1430 END
1431 writefile(lines, 'Xsuggest.vim')
1432
1433 lines =<< trim END
1434 vim9script
1435 import './Xsuggest.vim' as sugg
1436 set spell spellsuggest=expr:sugg.MySuggest()
1437 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001438 v9.CheckScriptSuccess(lines)
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001439
1440 set verbose=1 # report errors
1441 call assert_equal(['Fox', 'Fop'], spellsuggest('Fo', 2))
1442
1443 delete('Xsuggest.vim')
1444 set nospell spellsuggest& verbose=0
1445enddef
1446
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001447def Test_export_shadows_global_function()
1448 mkdir('Xdir/autoload', 'p')
1449 var save_rtp = &rtp
1450 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1451
1452 var lines =<< trim END
1453 vim9script
1454 export def Shadow(): string
1455 return 'Shadow()'
1456 enddef
1457 END
1458 writefile(lines, 'Xdir/autoload/shadow.vim')
1459
1460 lines =<< trim END
1461 vim9script
1462
1463 def g:Shadow(): string
1464 return 'global'
1465 enddef
1466
1467 import autoload 'shadow.vim'
1468 assert_equal('Shadow()', shadow.Shadow())
1469 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001470 v9.CheckScriptSuccess(lines)
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001471
1472 delfunc g:Shadow
1473 bwipe!
1474 delete('Xdir', 'rf')
1475 &rtp = save_rtp
1476enddef
1477
Bram Moolenaard8448622022-01-07 21:39:52 +00001478def Test_export_fails()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001479 v9.CheckScriptFailure(['export var some = 123'], 'E1042:')
1480 v9.CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
1481 v9.CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +01001482 v9.CheckScriptFailure(['vim9script', 'export function /a1b2c3'], 'E1044:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001483
1484 assert_fails('export something', 'E1043:')
1485enddef
1486
1487func Test_import_fails_without_script()
1488 CheckRunVimInTerminal
1489
1490 " call indirectly to avoid compilation error for missing functions
1491 call Run_Test_import_fails_on_command_line()
1492endfunc
1493
1494def Run_Test_import_fails_on_command_line()
1495 var export =<< trim END
1496 vim9script
1497 export def Foo(): number
1498 return 0
1499 enddef
1500 END
1501 writefile(export, 'XexportCmd.vim')
1502
Bram Moolenaar62aec932022-01-29 21:45:34 +00001503 var buf = g:RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaard8448622022-01-07 21:39:52 +00001504 rows: 6, wait_for_ruler: 0})
Bram Moolenaar62aec932022-01-29 21:45:34 +00001505 g:WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaard8448622022-01-07 21:39:52 +00001506
1507 delete('XexportCmd.vim')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001508 g:StopVimInTerminal(buf)
Bram Moolenaard8448622022-01-07 21:39:52 +00001509enddef
1510
1511def Test_vim9_reload_noclear()
1512 var lines =<< trim END
1513 vim9script
1514 export var exported = 'thexport'
1515
1516 export def TheFunc(x = 0)
1517 enddef
1518 END
1519 writefile(lines, 'XExportReload')
1520 lines =<< trim END
1521 vim9script noclear
1522 g:loadCount += 1
Bram Moolenaara749a422022-02-12 19:52:25 +00001523 var reloaded = 'init'
Bram Moolenaard8448622022-01-07 21:39:52 +00001524 import './XExportReload' as exp
1525
1526 def Again(): string
1527 return 'again'
1528 enddef
1529
1530 exp.TheFunc()
1531
Bram Moolenaara749a422022-02-12 19:52:25 +00001532 if exists('loaded') | finish | endif
1533 var loaded = true
Bram Moolenaard8448622022-01-07 21:39:52 +00001534
Bram Moolenaara749a422022-02-12 19:52:25 +00001535 var notReloaded = 'yes'
1536 reloaded = 'first'
Bram Moolenaard8448622022-01-07 21:39:52 +00001537 def g:Values(): list<string>
Bram Moolenaara749a422022-02-12 19:52:25 +00001538 return [reloaded, notReloaded, Again(), Once(), exp.exported]
Bram Moolenaard8448622022-01-07 21:39:52 +00001539 enddef
1540
1541 def Once(): string
1542 return 'once'
1543 enddef
1544 END
1545 writefile(lines, 'XReloaded')
1546 g:loadCount = 0
1547 source XReloaded
1548 assert_equal(1, g:loadCount)
1549 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
1550 source XReloaded
1551 assert_equal(2, g:loadCount)
1552 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1553 source XReloaded
1554 assert_equal(3, g:loadCount)
1555 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1556
1557 delete('XReloaded')
1558 delete('XExportReload')
1559 delfunc g:Values
1560 unlet g:loadCount
1561
1562 lines =<< trim END
1563 vim9script
1564 def Inner()
1565 enddef
1566 END
1567 lines->writefile('XreloadScript.vim')
1568 source XreloadScript.vim
1569
1570 lines =<< trim END
1571 vim9script
1572 def Outer()
1573 def Inner()
1574 enddef
1575 enddef
1576 defcompile
1577 END
1578 lines->writefile('XreloadScript.vim')
1579 source XreloadScript.vim
1580
1581 delete('XreloadScript.vim')
1582enddef
1583
Bram Moolenaarcd1cda22022-02-16 21:48:25 +00001584def Test_vim_reload_noclear_arg_count()
1585 var lines =<< trim END
1586 vim9script noclear
1587
1588 if !exists('g:didload')
1589 def Test(a: string, b: string)
1590 echo a b
1591 enddef
1592 def Call()
1593 Test('a', 'b')
1594 enddef
1595 else
1596 # redefine with one argument less
1597 def Test(a: string)
1598 echo a
1599 enddef
1600 endif
1601 Call()
1602 g:didload = 1
1603 END
1604 lines->writefile('XreloadScript_1.vim')
1605 source XreloadScript_1.vim
1606 assert_fails('source XreloadScript_1.vim', 'E1106: One argument too many')
1607 unlet g:didload
1608
1609 lines =<< trim END
1610 vim9script noclear
1611
1612 if !exists('g:didload')
1613 def Test(a: string, b: string, c: string)
1614 echo a b
1615 enddef
1616 def Call()
1617 Test('a', 'b', 'c')
1618 enddef
1619 else
1620 # redefine with one argument less
1621 def Test(a: string)
1622 echo a
1623 enddef
1624 endif
1625 Call()
1626 g:didload = 1
1627 END
1628 lines->writefile('XreloadScript_2.vim')
1629 source XreloadScript_2.vim
1630 assert_fails('source XreloadScript_2.vim', 'E1106: 2 arguments too many')
1631 unlet g:didload
1632
1633 lines =<< trim END
1634 vim9script noclear
1635
1636 if !exists('g:didload')
1637 def Test(a: string)
1638 echo a
1639 enddef
1640 def Call()
1641 Test('a')
1642 enddef
1643 else
1644 # redefine with one argument extra
1645 def Test(a: string, b: string)
1646 echo a b
1647 enddef
1648 endif
1649 Call()
1650 g:didload = 1
1651 END
1652 lines->writefile('XreloadScript_3.vim')
1653 source XreloadScript_3.vim
1654 assert_fails('source XreloadScript_3.vim', 'E1190: One argument too few')
1655 unlet g:didload
1656
1657 lines =<< trim END
1658 vim9script noclear
1659
1660 if !exists('g:didload')
1661 def Test(a: string)
1662 echo a
1663 enddef
1664 def Call()
1665 Test('a')
1666 enddef
1667 else
1668 # redefine with two arguments extra
1669 def Test(a: string, b: string, c: string)
1670 echo a b
1671 enddef
1672 endif
1673 Call()
1674 g:didload = 1
1675 END
1676 lines->writefile('XreloadScript_4.vim')
1677 source XreloadScript_4.vim
1678 assert_fails('source XreloadScript_4.vim', 'E1190: 2 arguments too few')
1679 unlet g:didload
1680
1681 delete('XreloadScript_1.vim')
1682 delete('XreloadScript_2.vim')
1683 delete('XreloadScript_3.vim')
1684 delete('XreloadScript_4.vim')
1685enddef
1686
1687def Test_vim9_reload_noclear_error()
1688 var lines =<< trim END
1689 vim9script noclear
1690
1691 if !exists('g:didload')
1692 def Test(a: string)
1693 echo a
1694 enddef
1695 def Call()
1696 Test('a')
1697 enddef
1698 else
1699 # redefine with a compile error
1700 def Test(a: string)
1701 echo ax
1702 enddef
1703 endif
1704 Call()
1705 g:didload = 1
1706 END
1707 lines->writefile('XreloadScriptErr.vim')
1708 source XreloadScriptErr.vim
1709 assert_fails('source XreloadScriptErr.vim', 'E1001: Variable not found: ax')
1710
1711 unlet g:didload
1712 delete('XreloadScriptErr.vim')
1713enddef
1714
Bram Moolenaard8448622022-01-07 21:39:52 +00001715def Test_vim9_reload_import()
1716 var lines =<< trim END
1717 vim9script
1718 const var = ''
1719 var valone = 1234
1720 def MyFunc(arg: string)
1721 valone = 5678
1722 enddef
1723 END
1724 var morelines =<< trim END
1725 var valtwo = 222
1726 export def GetValtwo(): number
1727 return valtwo
1728 enddef
1729 END
1730 writefile(lines + morelines, 'Xreload.vim')
1731 source Xreload.vim
1732 source Xreload.vim
1733 source Xreload.vim
1734
1735 # cannot declare a var twice
1736 lines =<< trim END
1737 vim9script
1738 var valone = 1234
1739 var valone = 5678
1740 END
1741 writefile(lines, 'Xreload.vim')
1742 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
1743
1744 delete('Xreload.vim')
1745 delete('Ximport.vim')
1746enddef
1747
1748" if a script is reloaded with a script-local variable that changed its type, a
1749" compiled function using that variable must fail.
1750def Test_script_reload_change_type()
1751 var lines =<< trim END
1752 vim9script noclear
1753 var str = 'string'
1754 def g:GetStr(): string
1755 return str .. 'xxx'
1756 enddef
1757 END
1758 writefile(lines, 'Xreload.vim')
1759 source Xreload.vim
1760 echo g:GetStr()
1761
1762 lines =<< trim END
1763 vim9script noclear
1764 var str = 1234
1765 END
1766 writefile(lines, 'Xreload.vim')
1767 source Xreload.vim
1768 assert_fails('echo g:GetStr()', 'E1150:')
1769
1770 delfunc g:GetStr
1771 delete('Xreload.vim')
1772enddef
1773
1774" Define CallFunc so that the test can be compiled
1775command CallFunc echo 'nop'
1776
1777def Test_script_reload_from_function()
1778 var lines =<< trim END
1779 vim9script
1780
Bram Moolenaar10611952022-04-03 21:11:34 +01001781 if exists('g:loadedThis')
Bram Moolenaard8448622022-01-07 21:39:52 +00001782 finish
1783 endif
Bram Moolenaar10611952022-04-03 21:11:34 +01001784 g:loadedThis = 1
Bram Moolenaard8448622022-01-07 21:39:52 +00001785 delcommand CallFunc
1786 command CallFunc Func()
1787 def Func()
1788 so XreloadFunc.vim
1789 g:didTheFunc = 1
1790 enddef
1791 END
1792 writefile(lines, 'XreloadFunc.vim')
1793 source XreloadFunc.vim
1794 CallFunc
1795 assert_equal(1, g:didTheFunc)
1796
1797 delete('XreloadFunc.vim')
1798 delcommand CallFunc
Bram Moolenaar10611952022-04-03 21:11:34 +01001799 unlet g:loadedThis
Bram Moolenaard8448622022-01-07 21:39:52 +00001800 unlet g:didTheFunc
1801enddef
1802
1803def s:RetSome(): string
1804 return 'some'
1805enddef
1806
1807" Not exported function that is referenced needs to be accessed by the
1808" script-local name.
1809def Test_vim9_funcref()
1810 var sortlines =<< trim END
1811 vim9script
1812 def Compare(i1: number, i2: number): number
1813 return i2 - i1
1814 enddef
1815
1816 export def FastSort(): list<number>
1817 return range(5)->sort(Compare)
1818 enddef
1819
1820 export def GetString(arg: string): string
1821 return arg
1822 enddef
1823 END
1824 writefile(sortlines, 'Xsort.vim')
1825
1826 var lines =<< trim END
1827 vim9script
1828 import './Xsort.vim'
1829 def Test()
1830 g:result = Xsort.FastSort()
1831 enddef
1832 Test()
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001833 END
1834 writefile(lines, 'Xscript.vim')
1835 source Xscript.vim
1836 assert_equal([4, 3, 2, 1, 0], g:result)
1837 unlet g:result
Bram Moolenaard8448622022-01-07 21:39:52 +00001838
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001839 lines =<< trim END
1840 vim9script
Bram Moolenaard8448622022-01-07 21:39:52 +00001841 # using a function imported with "as"
1842 import './Xsort.vim' as anAlias
1843 assert_equal('yes', anAlias.GetString('yes'))
1844
1845 # using the function from a compiled function
1846 def TestMore(): string
1847 var s = s:anAlias.GetString('foo')
1848 return s .. anAlias.GetString('bar')
1849 enddef
1850 assert_equal('foobar', TestMore())
1851
1852 # error when using a function that isn't exported
1853 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
1854 END
1855 writefile(lines, 'Xscript.vim')
1856
Bram Moolenaard8448622022-01-07 21:39:52 +00001857 delete('Xsort.vim')
1858 delete('Xscript.vim')
1859
1860 var Funcref = function('s:RetSome')
1861 assert_equal('some', Funcref())
1862enddef
1863
1864" Check that when searching for "FilterFunc" it finds the import in the
1865" script where FastFilter() is called from, both as a string and as a direct
1866" function reference.
1867def Test_vim9_funcref_other_script()
1868 var filterLines =<< trim END
1869 vim9script
1870 export def FilterFunc(idx: number, val: number): bool
1871 return idx % 2 == 1
1872 enddef
1873 export def FastFilter(): list<number>
1874 return range(10)->filter('FilterFunc(v:key, v:val)')
1875 enddef
1876 export def FastFilterDirect(): list<number>
1877 return range(10)->filter(FilterFunc)
1878 enddef
1879 END
1880 writefile(filterLines, 'Xfilter.vim')
1881
1882 var lines =<< trim END
1883 vim9script
1884 import './Xfilter.vim' as filter
1885 def Test()
1886 var x: list<number> = filter.FastFilter()
1887 enddef
1888 Test()
1889 def TestDirect()
1890 var x: list<number> = filter.FastFilterDirect()
1891 enddef
1892 TestDirect()
1893 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001894 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +00001895 delete('Xfilter.vim')
1896enddef
1897
1898def Test_import_absolute()
1899 var import_lines = [
1900 'vim9script',
1901 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
1902 'def UseExported()',
1903 ' g:imported_abs = abs.exported',
1904 ' abs.exported = 8888',
1905 ' g:imported_after = abs.exported',
1906 'enddef',
1907 'UseExported()',
1908 'g:import_disassembled = execute("disass UseExported")',
1909 ]
1910 writefile(import_lines, 'Ximport_abs.vim')
1911 writefile(s:export_script_lines, 'Xexport_abs.vim')
1912
1913 source Ximport_abs.vim
1914
1915 assert_equal(9876, g:imported_abs)
1916 assert_equal(8888, g:imported_after)
1917 assert_match('<SNR>\d\+_UseExported\_s*' ..
1918 'g:imported_abs = abs.exported\_s*' ..
1919 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1920 '1 STOREG g:imported_abs\_s*' ..
1921 'abs.exported = 8888\_s*' ..
1922 '2 PUSHNR 8888\_s*' ..
1923 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1924 'g:imported_after = abs.exported\_s*' ..
1925 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1926 '5 STOREG g:imported_after',
1927 g:import_disassembled)
1928
1929 Undo_export_script_lines()
1930 unlet g:imported_abs
1931 unlet g:import_disassembled
1932
1933 delete('Ximport_abs.vim')
1934 delete('Xexport_abs.vim')
1935enddef
1936
1937def Test_import_rtp()
1938 var import_lines = [
1939 'vim9script',
1940 'import "Xexport_rtp.vim" as rtp',
1941 'g:imported_rtp = rtp.exported',
1942 ]
1943 writefile(import_lines, 'Ximport_rtp.vim')
1944 mkdir('import', 'p')
1945 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1946
1947 var save_rtp = &rtp
1948 &rtp = getcwd()
1949 source Ximport_rtp.vim
1950 &rtp = save_rtp
1951
1952 assert_equal(9876, g:imported_rtp)
1953
1954 Undo_export_script_lines()
1955 unlet g:imported_rtp
1956 delete('Ximport_rtp.vim')
1957 delete('import', 'rf')
1958enddef
1959
1960def Test_import_compile_error()
1961 var export_lines = [
1962 'vim9script',
1963 'export def ExpFunc(): string',
1964 ' return notDefined',
1965 'enddef',
1966 ]
1967 writefile(export_lines, 'Xexported.vim')
1968
1969 var import_lines = [
1970 'vim9script',
1971 'import "./Xexported.vim" as expo',
1972 'def ImpFunc()',
1973 ' echo expo.ExpFunc()',
1974 'enddef',
1975 'defcompile',
1976 ]
1977 writefile(import_lines, 'Ximport.vim')
1978
1979 try
1980 source Ximport.vim
1981 catch /E1001/
1982 # Error should be before the Xexported.vim file.
1983 assert_match('E1001: Variable not found: notDefined', v:exception)
1984 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1985 endtry
1986
1987 delete('Xexported.vim')
1988 delete('Ximport.vim')
1989enddef
1990
1991def Test_func_overrules_import_fails()
1992 var export_lines =<< trim END
1993 vim9script
1994 export def Func()
1995 echo 'imported'
1996 enddef
1997 END
1998 writefile(export_lines, 'XexportedFunc.vim')
1999
2000 var lines =<< trim END
2001 vim9script
2002 import './XexportedFunc.vim' as Func
2003 def Func()
2004 echo 'local to function'
2005 enddef
2006 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002007 v9.CheckScriptFailure(lines, 'E1213: Redefining imported item "Func"')
Bram Moolenaard8448622022-01-07 21:39:52 +00002008
2009 lines =<< trim END
2010 vim9script
2011 import './XexportedFunc.vim' as Func
2012 def Outer()
2013 def Func()
2014 echo 'local to function'
2015 enddef
2016 enddef
2017 defcompile
2018 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002019 v9.CheckScriptFailure(lines, 'E1236:')
Bram Moolenaard8448622022-01-07 21:39:52 +00002020
2021 delete('XexportedFunc.vim')
2022enddef
2023
2024def Test_source_vim9_from_legacy()
2025 var vim9_lines =<< trim END
2026 vim9script
2027 var local = 'local'
2028 g:global = 'global'
2029 export var exported = 'exported'
2030 export def GetText(): string
2031 return 'text'
2032 enddef
2033 END
2034 writefile(vim9_lines, 'Xvim9_script.vim')
2035
2036 var legacy_lines =<< trim END
2037 source Xvim9_script.vim
2038
2039 call assert_false(exists('local'))
2040 call assert_false(exists('exported'))
2041 call assert_false(exists('s:exported'))
2042 call assert_equal('global', global)
2043 call assert_equal('global', g:global)
Bram Moolenaard8448622022-01-07 21:39:52 +00002044 END
2045 writefile(legacy_lines, 'Xlegacy_script.vim')
2046
2047 source Xlegacy_script.vim
2048 assert_equal('global', g:global)
2049 unlet g:global
2050
2051 delete('Xlegacy_script.vim')
2052 delete('Xvim9_script.vim')
2053enddef
2054
Bram Moolenaarc43e6232022-01-13 20:51:56 +00002055def Test_import_vim9_from_legacy()
2056 var vim9_lines =<< trim END
2057 vim9script
2058 var local = 'local'
2059 g:global = 'global'
2060 export var exported = 'exported'
2061 export def GetText(): string
2062 return 'text'
2063 enddef
2064 END
2065 writefile(vim9_lines, 'Xvim9_export.vim')
2066
2067 var legacy_lines =<< trim END
2068 import './Xvim9_export.vim' as vim9
2069
2070 call assert_false(exists('vim9'))
2071 call assert_false(exists('local'))
2072 call assert_false(exists('s:vim9.local'))
2073 call assert_equal('global', global)
2074 call assert_equal('global', g:global)
2075 call assert_false(exists('exported'))
2076 call assert_false(exists('s:exported'))
2077 call assert_false(exists('*GetText'))
2078
2079 " imported symbol is script-local
2080 call assert_equal('exported', s:vim9.exported)
2081 call assert_equal('text', s:vim9.GetText())
2082 END
2083 writefile(legacy_lines, 'Xlegacy_script.vim')
2084
2085 source Xlegacy_script.vim
2086 assert_equal('global', g:global)
2087 unlet g:global
2088
2089 delete('Xlegacy_script.vim')
2090 delete('Xvim9_export.vim')
2091enddef
2092
Bram Moolenaard8448622022-01-07 21:39:52 +00002093def Test_cmdline_win()
2094 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
2095 # the command line window.
2096 mkdir('rtp/syntax', 'p')
2097 var export_lines =<< trim END
2098 vim9script
2099 export var That = 'yes'
2100 END
2101 writefile(export_lines, 'rtp/syntax/Xexport.vim')
2102 var import_lines =<< trim END
2103 vim9script
2104 import './Xexport.vim' as exp
2105 echo exp.That
2106 END
2107 writefile(import_lines, 'rtp/syntax/vim.vim')
2108 var save_rtp = &rtp
2109 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
2110 syntax on
2111 augroup CmdWin
2112 autocmd CmdwinEnter * g:got_there = 'yes'
2113 augroup END
2114 # this will open and also close the cmdline window
2115 feedkeys('q:', 'xt')
2116 assert_equal('yes', g:got_there)
2117
2118 augroup CmdWin
2119 au!
2120 augroup END
2121 &rtp = save_rtp
2122 delete('rtp', 'rf')
2123enddef
2124
2125def Test_import_gone_when_sourced_twice()
2126 var exportlines =<< trim END
2127 vim9script
2128 if exists('g:guard')
2129 finish
2130 endif
2131 g:guard = 1
2132 export var name = 'someName'
2133 END
2134 writefile(exportlines, 'XexportScript.vim')
2135
2136 var lines =<< trim END
2137 vim9script
2138 import './XexportScript.vim' as expo
2139 def g:GetName(): string
2140 return expo.name
2141 enddef
2142 END
2143 writefile(lines, 'XscriptImport.vim')
2144 so XscriptImport.vim
2145 assert_equal('someName', g:GetName())
2146
2147 so XexportScript.vim
2148 assert_fails('call g:GetName()', 'E1149:')
2149
2150 delfunc g:GetName
2151 delete('XexportScript.vim')
2152 delete('XscriptImport.vim')
2153 unlet g:guard
2154enddef
2155
Bram Moolenaar160aa862022-01-10 21:29:57 +00002156" test using an auto-loaded function and variable
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002157def Test_vim9_autoload_full_name()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002158 var lines =<< trim END
2159 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002160 export def Gettest(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002161 return 'test'
2162 enddef
2163 g:some#name = 'name'
2164 g:some#dict = {key: 'value'}
2165
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002166 export def Varargs(a1: string, ...l: list<string>): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002167 return a1 .. l[0] .. l[1]
2168 enddef
2169 END
2170
2171 mkdir('Xdir/autoload', 'p')
2172 writefile(lines, 'Xdir/autoload/some.vim')
2173 var save_rtp = &rtp
2174 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2175
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002176 assert_equal('test', g:some#Gettest())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002177 assert_equal('name', g:some#name)
2178 assert_equal('value', g:some#dict.key)
2179 g:some#other = 'other'
2180 assert_equal('other', g:some#other)
2181
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002182 assert_equal('abc', some#Varargs('a', 'b', 'c'))
Bram Moolenaar160aa862022-01-10 21:29:57 +00002183
2184 # upper case script name works
2185 lines =<< trim END
2186 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002187 export def GetOther(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002188 return 'other'
2189 enddef
2190 END
2191 writefile(lines, 'Xdir/autoload/Other.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002192 assert_equal('other', g:Other#GetOther())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002193
2194 delete('Xdir', 'rf')
2195 &rtp = save_rtp
2196enddef
2197
2198def Test_vim9script_autoload()
2199 mkdir('Xdir/autoload', 'p')
2200 var save_rtp = &rtp
2201 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2202
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002203 # when the path has "/autoload/" prefix is not needed
Bram Moolenaar160aa862022-01-10 21:29:57 +00002204 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002205 vim9script
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002206 g:prefixed_loaded += 1
Bram Moolenaar160aa862022-01-10 21:29:57 +00002207
2208 export def Gettest(): string
2209 return 'test'
2210 enddef
2211
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002212 export var name = 'name'
2213
2214 export func GetFunc()
2215 return Gettest() .. 'more' .. s:name
Bram Moolenaar160aa862022-01-10 21:29:57 +00002216 endfunc
2217
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002218 export def GetDef(): string
2219 return Gettest() .. 'more' .. name
2220 enddef
2221
Bram Moolenaar160aa862022-01-10 21:29:57 +00002222 export final fname = 'final'
2223 export const cname = 'const'
2224 END
2225 writefile(lines, 'Xdir/autoload/prefixed.vim')
2226
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002227 g:prefixed_loaded = 0
2228 g:expected_loaded = 0
Bram Moolenaar160aa862022-01-10 21:29:57 +00002229 lines =<< trim END
2230 vim9script
2231 import autoload 'prefixed.vim'
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002232 assert_equal(g:expected_loaded, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002233 assert_equal('test', prefixed.Gettest())
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002234 assert_equal(1, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002235
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002236 assert_equal('testmorename', prefixed.GetFunc())
2237 assert_equal('testmorename', prefixed.GetDef())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002238 assert_equal('name', prefixed.name)
2239 assert_equal('final', prefixed.fname)
2240 assert_equal('const', prefixed.cname)
2241 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002242 v9.CheckScriptSuccess(lines)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002243 # can source it again, autoload script not loaded again
2244 g:expected_loaded = 1
Bram Moolenaar62aec932022-01-29 21:45:34 +00002245 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002246
2247 # can also get the items by autoload name
2248 lines =<< trim END
2249 call assert_equal('test', prefixed#Gettest())
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002250 call assert_equal('testmorename', prefixed#GetFunc())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002251 call assert_equal('name', prefixed#name)
2252 call assert_equal('final', prefixed#fname)
2253 call assert_equal('const', prefixed#cname)
2254 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002255 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002256
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002257 unlet g:prefixed_loaded
2258 unlet g:expected_loaded
2259 delete('Xdir', 'rf')
2260 &rtp = save_rtp
2261enddef
2262
Bram Moolenaard02dce22022-01-18 17:43:04 +00002263def Test_import_autoload_not_exported()
2264 mkdir('Xdir/autoload', 'p')
2265 var save_rtp = &rtp
2266 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2267
2268 # error when using an item that is not exported from an autoload script
2269 var exportLines =<< trim END
2270 vim9script
2271 var notExported = 123
2272 def NotExport()
2273 echo 'nop'
2274 enddef
2275 END
2276 writefile(exportLines, 'Xdir/autoload/notExport1.vim')
2277
2278 var lines =<< trim END
2279 vim9script
2280 import autoload 'notExport1.vim'
2281 echo notExport1.notFound
2282 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002283 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002284
2285 lines =<< trim END
2286 vim9script
2287 import autoload 'notExport1.vim'
2288 echo notExport1.notExported
2289 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002290 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002291
2292 lines =<< trim END
2293 vim9script
2294 import autoload 'notExport1.vim'
2295 echo notExport1.NotFunc()
2296 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002297 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002298
2299 lines =<< trim END
2300 vim9script
2301 import autoload 'notExport1.vim'
2302 echo notExport1.NotExport()
2303 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002304 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002305
2306 lines =<< trim END
2307 vim9script
2308 import autoload 'notExport1.vim'
2309 echo 'text'->notExport1.NotFunc()
2310 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002311 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002312
2313 lines =<< trim END
2314 vim9script
2315 import autoload 'notExport1.vim'
2316 echo 'text'->notExport1.NotExport()
2317 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002318 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002319
2320 # using a :def function we use a different autoload script every time so that
2321 # the function is compiled without the script loaded
2322 writefile(exportLines, 'Xdir/autoload/notExport2.vim')
2323 lines =<< trim END
2324 vim9script
2325 import autoload 'notExport2.vim'
2326 def Testit()
2327 echo notExport2.notFound
2328 enddef
2329 Testit()
2330 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002331 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport2#notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002332
2333 writefile(exportLines, 'Xdir/autoload/notExport3.vim')
2334 lines =<< trim END
2335 vim9script
2336 import autoload 'notExport3.vim'
2337 def Testit()
2338 echo notExport3.notExported
2339 enddef
2340 Testit()
2341 END
2342 # don't get E1049 because it is too complicated to figure out
Bram Moolenaar62aec932022-01-29 21:45:34 +00002343 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport3#notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002344
2345 writefile(exportLines, 'Xdir/autoload/notExport4.vim')
2346 lines =<< trim END
2347 vim9script
2348 import autoload 'notExport4.vim'
2349 def Testit()
2350 echo notExport4.NotFunc()
2351 enddef
2352 Testit()
2353 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002354 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport4#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002355
2356 writefile(exportLines, 'Xdir/autoload/notExport5.vim')
2357 lines =<< trim END
2358 vim9script
2359 import autoload 'notExport5.vim'
2360 def Testit()
2361 echo notExport5.NotExport()
2362 enddef
2363 Testit()
2364 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002365 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport5#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002366
2367 writefile(exportLines, 'Xdir/autoload/notExport6.vim')
2368 lines =<< trim END
2369 vim9script
2370 import autoload 'notExport6.vim'
2371 def Testit()
2372 echo 'text'->notExport6.NotFunc()
2373 enddef
2374 Testit()
2375 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002376 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport6#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002377
2378 writefile(exportLines, 'Xdir/autoload/notExport7.vim')
2379 lines =<< trim END
2380 vim9script
2381 import autoload 'notExport7.vim'
2382 def Testit()
2383 echo 'text'->notExport7.NotExport()
2384 enddef
2385 Testit()
2386 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002387 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport7#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002388
2389 delete('Xdir', 'rf')
2390 &rtp = save_rtp
2391enddef
2392
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002393def Test_vim9script_autoload_call()
2394 mkdir('Xdir/autoload', 'p')
2395 var save_rtp = &rtp
2396 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2397
2398 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002399 vim9script
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002400
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002401 export def RetArg(arg: string): string
2402 return arg
2403 enddef
2404
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002405 export def Getother()
2406 g:result = 'other'
2407 enddef
2408 END
Bram Moolenaar5d982692022-01-12 15:15:27 +00002409 writefile(lines, 'Xdir/autoload/another.vim')
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002410
2411 lines =<< trim END
2412 vim9script
Bram Moolenaar5d982692022-01-12 15:15:27 +00002413 import autoload 'another.vim'
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002414
2415 # compile this before 'another.vim' is loaded
2416 def CallAnother()
2417 assert_equal('foo', 'foo'->another.RetArg())
2418 enddef
2419 CallAnother()
2420
Bram Moolenaar5d982692022-01-12 15:15:27 +00002421 call another.Getother()
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002422 assert_equal('other', g:result)
Bram Moolenaar3d8e25a2022-01-22 11:00:02 +00002423
2424 assert_equal('arg', call('another.RetArg', ['arg']))
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00002425
2426 verbose function another.Getother
2427 # should we disallow this?
2428 verbose function another#Getother
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002429 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002430 v9.CheckScriptSuccess(lines)
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002431
2432 unlet g:result
Bram Moolenaar160aa862022-01-10 21:29:57 +00002433 delete('Xdir', 'rf')
2434 &rtp = save_rtp
2435enddef
2436
Bram Moolenaarb697dc22022-01-22 11:27:29 +00002437def Test_vim9script_noclear_autoload()
2438 mkdir('Xdir/autoload', 'p')
2439 var save_rtp = &rtp
2440 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2441
2442 var lines =<< trim END
2443 vim9script
2444 export def Func(): string
2445 return 'called'
2446 enddef
2447 g:double_loaded = 'yes'
2448 END
2449 writefile(lines, 'Xdir/autoload/double.vim')
2450
2451 lines =<< trim END
2452 vim9script noclear
2453 if exists('g:script_loaded')
2454 finish
2455 endif
2456 g:script_loaded = true
2457
2458 import autoload 'double.vim'
2459 nnoremap <F3> <ScriptCmd>g:result = double.Func()<CR>
2460 END
2461 g:double_loaded = 'no'
2462 writefile(lines, 'Xloaddouble')
2463 source Xloaddouble
2464 assert_equal('no', g:double_loaded)
2465 assert_equal(true, g:script_loaded)
2466 source Xloaddouble
2467 feedkeys("\<F3>", 'xt')
2468 assert_equal('called', g:result)
2469 assert_equal('yes', g:double_loaded)
2470
2471 delete('Xloaddouble')
2472 unlet g:double_loaded
2473 unlet g:script_loaded
2474 unlet g:result
2475 delete('Xdir', 'rf')
2476 &rtp = save_rtp
2477enddef
2478
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002479def Test_vim9script_autoload_duplicate()
2480 mkdir('Xdir/autoload', 'p')
2481
2482 var lines =<< trim END
2483 vim9script
2484
2485 export def Func()
2486 enddef
2487
2488 def Func()
2489 enddef
2490 END
2491 writefile(lines, 'Xdir/autoload/dupfunc.vim')
2492 assert_fails('source Xdir/autoload/dupfunc.vim', 'E1073:')
2493
2494 lines =<< trim END
2495 vim9script
2496
2497 def Func()
2498 enddef
2499
2500 export def Func()
2501 enddef
2502 END
2503 writefile(lines, 'Xdir/autoload/dup2func.vim')
2504 assert_fails('source Xdir/autoload/dup2func.vim', 'E1073:')
2505
2506 lines =<< trim END
2507 vim9script
2508
2509 def Func()
2510 enddef
2511
2512 export var Func = 'asdf'
2513 END
2514 writefile(lines, 'Xdir/autoload/dup3func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002515 assert_fails('source Xdir/autoload/dup3func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002516
2517 lines =<< trim END
2518 vim9script
2519
2520 export var Func = 'asdf'
2521
2522 def Func()
2523 enddef
2524 END
2525 writefile(lines, 'Xdir/autoload/dup4func.vim')
2526 assert_fails('source Xdir/autoload/dup4func.vim', 'E707:')
2527
2528 lines =<< trim END
2529 vim9script
2530
2531 var Func = 'asdf'
2532
2533 export def Func()
2534 enddef
2535 END
2536 writefile(lines, 'Xdir/autoload/dup5func.vim')
2537 assert_fails('source Xdir/autoload/dup5func.vim', 'E707:')
2538
2539 lines =<< trim END
2540 vim9script
2541
2542 export def Func()
2543 enddef
2544
2545 var Func = 'asdf'
2546 END
2547 writefile(lines, 'Xdir/autoload/dup6func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002548 assert_fails('source Xdir/autoload/dup6func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002549
2550 delete('Xdir', 'rf')
2551enddef
2552
Bram Moolenaar2017d6f2022-01-20 19:38:46 +00002553def Test_autoload_missing_function_name()
2554 mkdir('Xdir/autoload', 'p')
2555
2556 var lines =<< trim END
2557 vim9script
2558
2559 def loadme#()
2560 enddef
2561 END
2562 writefile(lines, 'Xdir/autoload/loadme.vim')
2563 assert_fails('source Xdir/autoload/loadme.vim', 'E129:')
2564
2565 delete('Xdir', 'rf')
2566enddef
2567
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002568def Test_autoload_name_wrong()
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002569 var lines =<< trim END
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002570 def Xscriptname#Func()
2571 enddef
2572 END
2573 writefile(lines, 'Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002574 v9.CheckScriptFailure(lines, 'E746:')
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00002575 delete('Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002576
2577 mkdir('Xdir/autoload', 'p')
2578 lines =<< trim END
2579 vim9script
2580 def somescript#Func()
2581 enddef
2582 END
2583 writefile(lines, 'Xdir/autoload/somescript.vim')
2584 assert_fails('source Xdir/autoload/somescript.vim', 'E1263:')
2585
2586 delete('Xdir', 'rf')
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002587enddef
2588
Bram Moolenaard041f422022-01-12 19:54:00 +00002589def Test_import_autoload_postponed()
2590 mkdir('Xdir/autoload', 'p')
2591 var save_rtp = &rtp
2592 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2593
2594 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002595 vim9script
Bram Moolenaard041f422022-01-12 19:54:00 +00002596
2597 g:loaded_postponed = 'true'
2598 export var variable = 'bla'
2599 export def Function(): string
2600 return 'bla'
2601 enddef
2602 END
2603 writefile(lines, 'Xdir/autoload/postponed.vim')
2604
2605 lines =<< trim END
2606 vim9script
2607
2608 import autoload 'postponed.vim'
2609 def Tryit()
2610 echo postponed.variable
2611 echo postponed.Function()
2612 enddef
2613 defcompile
2614 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002615 v9.CheckScriptSuccess(lines)
Bram Moolenaard041f422022-01-12 19:54:00 +00002616 assert_false(exists('g:loaded_postponed'))
Bram Moolenaar62aec932022-01-29 21:45:34 +00002617 v9.CheckScriptSuccess(lines + ['Tryit()'])
Bram Moolenaard041f422022-01-12 19:54:00 +00002618 assert_equal('true', g:loaded_postponed)
2619
2620 unlet g:loaded_postponed
2621 delete('Xdir', 'rf')
2622 &rtp = save_rtp
2623enddef
2624
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002625def Test_import_autoload_override()
2626 mkdir('Xdir/autoload', 'p')
2627 var save_rtp = &rtp
2628 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2629 test_override('autoload', 1)
2630
2631 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002632 vim9script
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002633
2634 g:loaded_override = 'true'
2635 export var variable = 'bla'
2636 export def Function(): string
2637 return 'bla'
2638 enddef
2639 END
2640 writefile(lines, 'Xdir/autoload/override.vim')
2641
2642 lines =<< trim END
2643 vim9script
2644
2645 import autoload 'override.vim'
2646 assert_equal('true', g:loaded_override)
2647
2648 def Tryit()
2649 echo override.doesNotExist
2650 enddef
2651 defcompile
2652 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002653 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1)
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002654
2655 test_override('autoload', 0)
2656 unlet g:loaded_override
2657 delete('Xdir', 'rf')
2658 &rtp = save_rtp
2659enddef
2660
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002661def Test_autoload_mapping()
2662 mkdir('Xdir/autoload', 'p')
2663 var save_rtp = &rtp
2664 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2665
2666 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002667 vim9script
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002668
2669 g:toggle_loaded = 'yes'
2670
2671 export def Toggle(): string
2672 return ":g:toggle_called = 'yes'\<CR>"
2673 enddef
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002674 export def Doit()
2675 g:doit_called = 'yes'
2676 enddef
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002677 END
2678 writefile(lines, 'Xdir/autoload/toggle.vim')
2679
2680 lines =<< trim END
2681 vim9script
2682
2683 import autoload 'toggle.vim'
2684
2685 nnoremap <silent> <expr> tt toggle.Toggle()
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002686 nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR>
2687 nnoremap <silent> yy <Cmd>toggle.Doit()<CR>
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002688 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002689 v9.CheckScriptSuccess(lines)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002690 assert_false(exists("g:toggle_loaded"))
2691 assert_false(exists("g:toggle_called"))
Bram Moolenaar6079da72022-01-18 14:16:59 +00002692 assert_match('\d A: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002693
2694 feedkeys("tt", 'xt')
2695 assert_equal('yes', g:toggle_loaded)
2696 assert_equal('yes', g:toggle_called)
Bram Moolenaar6079da72022-01-18 14:16:59 +00002697 assert_match('\d: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002698
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002699 feedkeys("xx", 'xt')
2700 assert_equal('yes', g:doit_called)
2701
2702 assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle')
2703
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002704 nunmap tt
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002705 nunmap xx
2706 nunmap yy
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002707 unlet g:toggle_loaded
2708 unlet g:toggle_called
2709 delete('Xdir', 'rf')
2710 &rtp = save_rtp
2711enddef
2712
Bram Moolenaar160aa862022-01-10 21:29:57 +00002713def Test_vim9script_autoload_fails()
2714 var lines =<< trim END
2715 vim9script autoload
2716 var n = 0
2717 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002718 v9.CheckScriptFailure(lines, 'E475: Invalid argument: autoload')
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002719
2720 lines =<< trim END
2721 vim9script noclear noclear
2722 var n = 0
2723 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002724 v9.CheckScriptFailure(lines, 'E983: Duplicate argument: noclear')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +01002725
2726 lines =<< trim END
2727 vim9script noclears
2728 var n = 0
2729 END
2730 v9.CheckScriptFailure(lines, 'E475: Invalid argument: noclears')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002731enddef
2732
2733def Test_import_autoload_fails()
2734 var lines =<< trim END
2735 vim9script
2736 import autoload autoload 'prefixed.vim'
2737 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002738 v9.CheckScriptFailure(lines, 'E121: Undefined variable: autoload')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002739
2740 lines =<< trim END
2741 vim9script
Bram Moolenaar1836d612022-01-18 13:14:47 +00002742 import autoload './doesNotExist.vim'
Bram Moolenaar160aa862022-01-10 21:29:57 +00002743 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002744 v9.CheckScriptFailure(lines, 'E282:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002745
2746 lines =<< trim END
2747 vim9script
2748 import autoload '/dir/doesNotExist.vim'
2749 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002750 v9.CheckScriptFailure(lines, 'E282:', 2)
2751
2752 lines =<< trim END
2753 vim9script
2754 import autoload '../testdir'
2755 END
2756 v9.CheckScriptFailure(lines, 'E17:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002757
2758 lines =<< trim END
2759 vim9script
2760 import autoload 'doesNotExist.vim'
2761 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002762 v9.CheckScriptFailure(lines, 'E1053: Could not import "doesNotExist.vim"')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002763enddef
2764
2765" test disassembling an auto-loaded function starting with "debug"
2766def Test_vim9_autoload_disass()
2767 mkdir('Xdir/autoload', 'p')
2768 var save_rtp = &rtp
2769 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2770
2771 var lines =<< trim END
2772 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002773 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002774 return 'debug'
2775 enddef
2776 END
2777 writefile(lines, 'Xdir/autoload/debugit.vim')
2778
2779 lines =<< trim END
2780 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002781 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002782 return 'profile'
2783 enddef
2784 END
2785 writefile(lines, 'Xdir/autoload/profileit.vim')
2786
2787 lines =<< trim END
2788 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002789 assert_equal('debug', debugit#Test())
2790 disass debugit#Test
2791 assert_equal('profile', profileit#Test())
2792 disass profileit#Test
Bram Moolenaar160aa862022-01-10 21:29:57 +00002793 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002794 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002795
2796 delete('Xdir', 'rf')
2797 &rtp = save_rtp
2798enddef
2799
2800" test using a vim9script that is auto-loaded from an autocmd
2801def Test_vim9_aucmd_autoload()
2802 var lines =<< trim END
2803 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002804 export def Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002805 echomsg getreg('"')
2806 enddef
2807 END
2808
2809 mkdir('Xdir/autoload', 'p')
2810 writefile(lines, 'Xdir/autoload/foo.vim')
2811 var save_rtp = &rtp
2812 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2813 augroup test
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002814 autocmd TextYankPost * call foo#Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002815 augroup END
2816
2817 normal Y
2818
2819 augroup test
2820 autocmd!
2821 augroup END
2822 delete('Xdir', 'rf')
2823 &rtp = save_rtp
2824enddef
2825
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002826" test using a autoloaded file that is case sensitive
2827def Test_vim9_autoload_case_sensitive()
2828 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002829 vim9script
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002830 export def CaseSensitive(): string
2831 return 'done'
2832 enddef
2833 END
2834
2835 mkdir('Xdir/autoload', 'p')
2836 writefile(lines, 'Xdir/autoload/CaseSensitive.vim')
2837 var save_rtp = &rtp
2838 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2839
2840 lines =<< trim END
2841 vim9script
2842 import autoload 'CaseSensitive.vim'
2843 assert_equal('done', CaseSensitive.CaseSensitive())
2844 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002845 v9.CheckScriptSuccess(lines)
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002846
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002847 if !has('fname_case')
2848 lines =<< trim END
2849 vim9script
2850 import autoload 'CaseSensitive.vim'
2851 import autoload 'casesensitive.vim'
2852 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002853 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002854 endif
2855
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002856 delete('Xdir', 'rf')
2857 &rtp = save_rtp
2858enddef
2859
Bram Moolenaar160aa862022-01-10 21:29:57 +00002860" This was causing a crash because suppress_errthrow wasn't reset.
2861def Test_vim9_autoload_error()
2862 var lines =<< trim END
2863 vim9script
2864 def crash#func()
2865 try
2866 for x in List()
2867 endfor
2868 catch
2869 endtry
2870 g:ok = true
2871 enddef
2872 fu List()
2873 invalid
2874 endfu
2875 try
2876 alsoinvalid
2877 catch /wontmatch/
2878 endtry
2879 END
2880 call mkdir('Xruntime/autoload', 'p')
2881 call writefile(lines, 'Xruntime/autoload/crash.vim')
2882
2883 # run in a separate Vim to avoid the side effects of assert_fails()
2884 lines =<< trim END
2885 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
2886 call crash#func()
2887 call writefile(['ok'], 'Xdidit')
2888 qall!
2889 END
2890 writefile(lines, 'Xscript')
Bram Moolenaar62aec932022-01-29 21:45:34 +00002891 g:RunVim([], [], '-S Xscript')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002892 assert_equal(['ok'], readfile('Xdidit'))
2893
2894 delete('Xdidit')
2895 delete('Xscript')
2896 delete('Xruntime', 'rf')
2897
2898 lines =<< trim END
2899 vim9script
2900 var foo#bar = 'asdf'
2901 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002902 v9.CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002903enddef
2904
Bram Moolenaard8448622022-01-07 21:39:52 +00002905
2906" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker