blob: dd9f5668db347e41766f2c70398c227358c49f92 [file] [log] [blame]
Bram Moolenaard8448622022-01-07 21:39:52 +00001" Test import/export of the Vim9 script language.
Bram Moolenaar160aa862022-01-10 21:29:57 +00002" Also the autoload mechanism.
Bram Moolenaard8448622022-01-07 21:39:52 +00003
4source check.vim
5source term_util.vim
Bram Moolenaar62aec932022-01-29 21:45:34 +00006import './vim9.vim' as v9
Bram Moolenaard8448622022-01-07 21:39:52 +00007
8let s:export_script_lines =<< trim END
9 vim9script
10 var name: string = 'bob'
11 def Concat(arg: string): string
12 return name .. arg
13 enddef
14 g:result = Concat('bie')
15 g:localname = name
16
17 export const CONST = 1234
18 export var exported = 9876
19 export var exp_name = 'John'
20 export def Exported(): string
21 return 'Exported'
22 enddef
23 export def ExportedValue(): number
24 return exported
25 enddef
26 export def ExportedInc()
27 exported += 5
28 enddef
29 export final theList = [1]
Bram Moolenaar857c8bb2022-01-15 21:08:19 +000030 export def AddSome(s: string): string
31 return s .. 'some'
32 enddef
33 export var AddRef = AddSome
Bram Moolenaard8448622022-01-07 21:39:52 +000034END
35
Bram Moolenaar62aec932022-01-29 21:45:34 +000036def s:Undo_export_script_lines()
Bram Moolenaard8448622022-01-07 21:39:52 +000037 unlet g:result
38 unlet g:localname
39enddef
40
41def Test_vim9_import_export()
42 writefile(s:export_script_lines, 'Xexport.vim')
43 var import_script_lines =<< trim END
44 vim9script
45 var dir = './'
46 var ext = ".vim"
47 import dir .. 'Xexport' .. ext as expo
48
49 g:exported1 = expo.exported
50 expo.exported += 3
51 g:exported2 = expo.exported
52 g:exported3 = expo.ExportedValue()
53
54 expo.ExportedInc()
55 g:exported_i1 = expo.exported
56 g:exported_i2 = expo.ExportedValue()
57
58 expo.exported = 11
59 g:exported_s1 = expo.exported
60 g:exported_s2 = expo.ExportedValue()
61
62 g:imported_func = expo.Exported()
63
64 def GetExported(): string
65 var local_dict = {ref: expo.Exported}
66 return local_dict.ref()
67 enddef
68 g:funcref_result = GetExported()
69
Bram Moolenaar21f0d6c2022-01-20 17:35:49 +000070 def GetName(): string
71 return expo.exp_name .. 'son'
72 enddef
73 g:long_name = GetName()
74
Bram Moolenaard8448622022-01-07 21:39:52 +000075 g:imported_name = expo.exp_name
76 expo.exp_name ..= ' Doe'
Bram Moolenaar47036b62022-01-16 21:18:53 +000077 expo.exp_name = expo.exp_name .. ' Maar'
Bram Moolenaard8448622022-01-07 21:39:52 +000078 g:imported_name_appended = expo.exp_name
79 g:exported_later = expo.exported
80
81 expo.theList->add(2)
82 assert_equal([1, 2], expo.theList)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +000083
84 assert_equal('andthensome', 'andthen'->expo.AddSome())
85 assert_equal('awesome', 'awe'->expo.AddRef())
Bram Moolenaard8448622022-01-07 21:39:52 +000086 END
87 writefile(import_script_lines, 'Ximport.vim')
88 source Ximport.vim
89
90 assert_equal('bobbie', g:result)
91 assert_equal('bob', g:localname)
92 assert_equal(9876, g:exported1)
93 assert_equal(9879, g:exported2)
94 assert_equal(9879, g:exported3)
95
96 assert_equal(9884, g:exported_i1)
97 assert_equal(9884, g:exported_i2)
98
99 assert_equal(11, g:exported_s1)
100 assert_equal(11, g:exported_s2)
101 assert_equal(11, g:exported_later)
102
103 assert_equal('Exported', g:imported_func)
104 assert_equal('Exported', g:funcref_result)
105 assert_equal('John', g:imported_name)
Bram Moolenaar21f0d6c2022-01-20 17:35:49 +0000106 assert_equal('Johnson', g:long_name)
Bram Moolenaar47036b62022-01-16 21:18:53 +0000107 assert_equal('John Doe Maar', g:imported_name_appended)
Bram Moolenaard8448622022-01-07 21:39:52 +0000108 assert_false(exists('g:name'))
109
110 Undo_export_script_lines()
111 unlet g:exported1
112 unlet g:exported2
113 unlet g:exported3
114 unlet g:exported_i1
115 unlet g:exported_i2
116 unlet g:exported_later
117 unlet g:imported_func
Bram Moolenaar21f0d6c2022-01-20 17:35:49 +0000118 unlet g:imported_name g:long_name g:imported_name_appended
Bram Moolenaard8448622022-01-07 21:39:52 +0000119 delete('Ximport.vim')
120
121 # similar, with line breaks
122 var import_line_break_script_lines =<< trim END
123 vim9script
124 import './Xexport.vim'
125 as expo
126 g:exported = expo.exported
127 expo.exported += 7
128 g:exported_added = expo.exported
129 g:imported_func = expo.Exported()
130 END
131 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
132 source Ximport_lbr.vim
133
134 assert_equal(11, g:exported)
135 assert_equal(18, g:exported_added)
136 assert_equal('Exported', g:imported_func)
137
138 # exported script not sourced again
139 assert_false(exists('g:result'))
140 unlet g:exported
141 unlet g:exported_added
142 unlet g:imported_func
143 delete('Ximport_lbr.vim')
144
Bram Moolenaar68854a82022-01-31 18:59:13 +0000145 var import_shadows_cmdmod_lines =<< trim END
146 vim9script
147 import './Xexport.vim' as vim9
148 vim9.exp_name = 'Shadow'
149 assert_equal('Shadow', vim9.exp_name)
150 END
151 v9.CheckScriptSuccess(import_shadows_cmdmod_lines)
152
Bram Moolenaard8448622022-01-07 21:39:52 +0000153 var line_break_before_dot =<< trim END
154 vim9script
155 import './Xexport.vim' as expo
156 g:exported = expo
157 .exported
158 END
159 writefile(line_break_before_dot, 'Ximport_lbr_before_dot.vim')
160 assert_fails('source Ximport_lbr_before_dot.vim', 'E1060:', '', 3)
161 delete('Ximport_lbr_before_dot.vim')
162
163 var line_break_after_dot =<< trim END
164 vim9script
165 import './Xexport.vim' as expo
166 g:exported = expo.
167 exported
168 END
169 writefile(line_break_after_dot, 'Ximport_lbr_after_dot.vim')
170 assert_fails('source Ximport_lbr_after_dot.vim', 'E1074:', '', 3)
171 delete('Ximport_lbr_after_dot.vim')
172
173 var import_star_as_lines =<< trim END
174 vim9script
175 import './Xexport.vim' as Export
176 def UseExport()
177 g:exported_def = Export.exported
178 enddef
179 g:exported_script = Export.exported
180 assert_equal(1, exists('Export.exported'))
181 assert_equal(0, exists('Export.notexported'))
182 UseExport()
183 END
184 writefile(import_star_as_lines, 'Ximport.vim')
185 source Ximport.vim
186
187 assert_equal(18, g:exported_def)
188 assert_equal(18, g:exported_script)
189 unlet g:exported_def
190 unlet g:exported_script
191
192 var import_star_as_lines_no_dot =<< trim END
193 vim9script
194 import './Xexport.vim' as Export
195 def Func()
196 var dummy = 1
197 var imported = Export + dummy
198 enddef
199 defcompile
200 END
201 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
202 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
203
204 var import_star_as_lines_dot_space =<< trim END
205 vim9script
206 import './Xexport.vim' as Export
207 def Func()
208 var imported = Export . exported
209 enddef
210 defcompile
211 END
212 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
213 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
214
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000215 writefile(s:export_script_lines, 'Xexport2.vim')
216 var import_as_duplicated =<< trim END
Bram Moolenaard8448622022-01-07 21:39:52 +0000217 vim9script
218 import './Xexport.vim' as expo
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000219 import './Xexport2.vim' as expo
Bram Moolenaard8448622022-01-07 21:39:52 +0000220 END
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000221 writefile(import_as_duplicated, 'Ximport.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000222 assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000223 delete('Xexport2.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000224
225 var import_star_as_lines_script_no_dot =<< trim END
226 vim9script
227 import './Xexport.vim' as Export
228 g:imported_script = Export exported
229 END
230 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
231 assert_fails('source Ximport.vim', 'E1060: Expected dot after name: Export exported')
232
233 var import_star_as_lines_script_space_after_dot =<< trim END
234 vim9script
235 import './Xexport.vim' as Export
236 g:imported_script = Export. exported
237 END
238 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
239 assert_fails('source Ximport.vim', 'E1074:')
240
241 var import_star_as_lines_missing_name =<< trim END
242 vim9script
243 import './Xexport.vim' as Export
244 def Func()
245 var imported = Export.
246 enddef
247 defcompile
248 END
249 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
250 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
251
252 var import_star_as_lbr_lines =<< trim END
253 vim9script
254 import './Xexport.vim'
255 as Export
256 def UseExport()
257 g:exported = Export.exported
258 enddef
259 UseExport()
260 END
261 writefile(import_star_as_lbr_lines, 'Ximport.vim')
262 source Ximport.vim
263 assert_equal(18, g:exported)
264 unlet g:exported
265
266 # try to use something that exists but is not exported
267 var import_not_exported_lines =<< trim END
268 vim9script
269 import './Xexport.vim' as expo
270 echo expo.name
271 END
272 writefile(import_not_exported_lines, 'Ximport.vim')
273 assert_fails('source Ximport.vim', 'E1049:', '', 3, 'Ximport.vim')
274
275 # try to import something that is already defined
276 var import_already_defined =<< trim END
277 vim9script
278 var exported = 'something'
279 import './Xexport.vim' as exported
280 END
281 writefile(import_already_defined, 'Ximport.vim')
282 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
283
284 # try changing an imported const
285 var import_assign_to_const =<< trim END
286 vim9script
287 import './Xexport.vim' as expo
288 def Assign()
289 expo.CONST = 987
290 enddef
291 defcompile
292 END
293 writefile(import_assign_to_const, 'Ximport.vim')
294 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
295
296 # try changing an imported final
297 var import_assign_to_final =<< trim END
298 vim9script
299 import './Xexport.vim' as expo
300 def Assign()
301 expo.theList = [2]
302 enddef
303 defcompile
304 END
305 writefile(import_assign_to_final, 'Ximport.vim')
306 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
307
308 var import_no_as_lines =<< trim END
309 vim9script
310 import './Xexport.vim' name
311 END
312 writefile(import_no_as_lines, 'Ximport.vim')
313 assert_fails('source Ximport.vim', 'E488:', '', 2, 'Ximport.vim')
314
315 var import_invalid_string_lines =<< trim END
316 vim9script
317 import Xexport.vim
318 END
319 writefile(import_invalid_string_lines, 'Ximport.vim')
320 assert_fails('source Ximport.vim', 'E121:', '', 2, 'Ximport.vim')
321
322 var import_wrong_name_lines =<< trim END
323 vim9script
324 import './XnoExport.vim'
325 END
326 writefile(import_wrong_name_lines, 'Ximport.vim')
327 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
328
329 var import_redefining_lines =<< trim END
330 vim9script
331 import './Xexport.vim' as exported
332 var exported = 5
333 END
334 writefile(import_redefining_lines, 'Ximport.vim')
335 assert_fails('source Ximport.vim', 'E1213: Redefining imported item "exported"', '', 3)
336
Bram Moolenaar160aa862022-01-10 21:29:57 +0000337 var import_missing_dot_lines =<< trim END
338 vim9script
339 import './Xexport.vim' as expo
340 def Test()
341 expo = 9
342 enddef
343 defcompile
344 END
345 writefile(import_missing_dot_lines, 'Ximport.vim')
346 assert_fails('source Ximport.vim', 'E1258:', '', 1)
347
348 var import_missing_name_lines =<< trim END
349 vim9script
350 import './Xexport.vim' as expo
351 def Test()
352 expo.99 = 9
353 enddef
354 defcompile
355 END
356 writefile(import_missing_name_lines, 'Ximport.vim')
Bram Moolenaar76283822022-01-10 21:39:03 +0000357 assert_fails('source Ximport.vim', 'E1259:', '', 1)
Bram Moolenaar160aa862022-01-10 21:29:57 +0000358
Bram Moolenaard8448622022-01-07 21:39:52 +0000359 var import_assign_wrong_type_lines =<< trim END
360 vim9script
361 import './Xexport.vim' as expo
362 expo.exported = 'xxx'
363 END
364 writefile(import_assign_wrong_type_lines, 'Ximport.vim')
365 assert_fails('source Ximport.vim', 'E1012: Type mismatch; expected number but got string', '', 3)
366
367 var import_assign_const_lines =<< trim END
368 vim9script
369 import './Xexport.vim' as expo
370 expo.CONST = 4321
371 END
372 writefile(import_assign_const_lines, 'Ximport.vim')
373 assert_fails('source Ximport.vim', 'E46: Cannot change read-only variable "CONST"', '', 3)
374
375 delete('Ximport.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000376 delete('Xexport.vim')
377
378 # Check that in a Vim9 script 'cpo' is set to the Vim default.
379 # Flags added or removed are also applied to the restored value.
380 set cpo=abcd
381 var lines =<< trim END
382 vim9script
383 g:cpo_in_vim9script = &cpo
384 set cpo+=f
385 set cpo-=c
386 g:cpo_after_vim9script = &cpo
387 END
388 writefile(lines, 'Xvim9_script')
389 source Xvim9_script
390 assert_equal('fabd', &cpo)
391 set cpo&vim
392 assert_equal(&cpo, g:cpo_in_vim9script)
393 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
394 assert_equal(newcpo, g:cpo_after_vim9script)
395
396 delete('Xvim9_script')
397enddef
398
399def Test_import_funcref()
400 var lines =<< trim END
401 vim9script
402 export def F(): number
403 return 42
404 enddef
405 export const G = F
406 END
407 writefile(lines, 'Xlib.vim')
408
409 lines =<< trim END
410 vim9script
411 import './Xlib.vim' as lib
412 const Foo = lib.G()
413 assert_equal(42, Foo)
414
415 def DoTest()
416 const Goo = lib.G()
417 assert_equal(42, Goo)
418 enddef
419 DoTest()
420 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000421 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000422
423 delete('Xlib.vim')
424enddef
425
Bram Moolenaarc2f17f72022-02-21 13:13:50 +0000426def Test_import_duplicate_function()
427 # Function Hover() exists in both scripts, partial should refer to the right
428 # one.
429 var lines =<< trim END
430 vim9script
431
432 def Hover(d: dict<any>): string
433 return 'found it'
434 enddef
435
436 export def NewLspServer(): dict<any>
437 var d: dict<any> = {}
438 d->extend({hover: function('Hover', [d])})
439 return d
440 enddef
441
442 NewLspServer()
443 END
444 writefile(lines, 'Xserver.vim')
445
446 lines =<< trim END
447 vim9script
448
449 import './Xserver.vim' as server
450
451 export def Hover()
452 enddef
453
454 def AddServer()
455 var d: dict<any> = server.NewLspServer()
456 assert_equal('found it', d.hover())
457 enddef
458 AddServer()
459 END
460 v9.CheckScriptSuccess(lines)
461
462 delete('Xserver.vim')
463enddef
464
465
Bram Moolenaard8448622022-01-07 21:39:52 +0000466def Test_import_fails()
467 writefile([], 'Xfoo.vim')
468 var lines =<< trim END
469 import './Xfoo.vim' as foo
470 foo = 'bar'
471 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000472 v9.CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use foo itself'])
Bram Moolenaard8448622022-01-07 21:39:52 +0000473 lines =<< trim END
474 vim9script
475 import './Xfoo.vim' as foo
476 var that = foo
477 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000478 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaardd5893b2022-01-20 21:32:54 +0000479 lines =<< trim END
480 vim9script
481 import './Xfoo.vim' as foo
482 var that: any
483 that += foo
484 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000485 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaardd5893b2022-01-20 21:32:54 +0000486 lines =<< trim END
487 vim9script
488 import './Xfoo.vim' as foo
489 foo += 9
490 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000491 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaard8448622022-01-07 21:39:52 +0000492
493 lines =<< trim END
494 vim9script
495 import './Xfoo.vim' as 9foo
496 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000497 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000498 lines =<< trim END
499 vim9script
500 import './Xfoo.vim' as the#foo
501 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000502 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000503 lines =<< trim END
504 vim9script
505 import './Xfoo.vim' as g:foo
506 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000507 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000508
509 delete('Xfoo.vim')
510
511 lines =<< trim END
512 vim9script
513 def TheFunc()
514 echo 'the func'
515 enddef
516 export var Ref = TheFunc
517 END
518 writefile([], 'Xthat.vim')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000519
Bram Moolenaard8448622022-01-07 21:39:52 +0000520 lines =<< trim END
521 import './Xthat.vim' as That
522 That()
523 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000524 v9.CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use That itself'])
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000525
526 lines =<< trim END
Bram Moolenaar937610b2022-01-19 17:21:29 +0000527 vim9script
528 import './Xthat.vim' as That
529 def Func()
530 echo That()
531 enddef
532 Func()
533 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000534 v9.CheckScriptFailure(lines, 'E1236: Cannot use That itself')
Bram Moolenaar937610b2022-01-19 17:21:29 +0000535
536 lines =<< trim END
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000537 import './Xthat.vim' as one
538 import './Xthat.vim' as two
539 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000540 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000541
542 delete('Xthat.vim')
Bram Moolenaar779aeff2022-02-08 19:12:19 +0000543
544 lines =<< trim END
545 vim9script
546 export var item = 'hello'
547 import './Xyourself.vim'
548 END
549 writefile(lines, 'Xyourself.vim')
550 assert_fails('source Xyourself.vim', 'E1088:')
551 delete('Xyourself.vim')
552
Bram Moolenaard8448622022-01-07 21:39:52 +0000553 mkdir('Ximport')
554
555 writefile(['vim9script'], 'Ximport/.vim')
556 lines =<< trim END
557 vim9script
558 import './Ximport/.vim'
559 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000560 v9.CheckScriptFailure(lines, 'E1261: Cannot import .vim without using "as"')
Bram Moolenaard8448622022-01-07 21:39:52 +0000561 lines =<< trim END
562 vim9script
563 import './Ximport/.vim' as vim
564 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000565 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000566
567 writefile(['vim9script'], 'Ximport/.vimrc')
568 lines =<< trim END
569 vim9script
570 import './Ximport/.vimrc'
571 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000572 v9.CheckScriptFailure(lines, 'E1257: Imported script must use "as" or end in .vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000573 lines =<< trim END
574 vim9script
575 import './Ximport/.vimrc' as vimrc
576 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000577 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000578
579 delete('Ximport', 'rf')
580enddef
581
582func g:Trigger()
583 source Ximport.vim
584 return "echo 'yes'\<CR>"
585endfunc
586
587def Test_import_export_expr_map()
588 # check that :import and :export work when buffer is locked
589 var export_lines =<< trim END
590 vim9script
591 export def That(): string
592 return 'yes'
593 enddef
594 END
595 writefile(export_lines, 'Xexport_that.vim')
596
597 var import_lines =<< trim END
598 vim9script
599 import './Xexport_that.vim' as that
600 assert_equal('yes', that.That())
601 END
602 writefile(import_lines, 'Ximport.vim')
603
604 nnoremap <expr> trigger g:Trigger()
605 feedkeys('trigger', "xt")
606
607 delete('Xexport_that.vim')
608 delete('Ximport.vim')
609 nunmap trigger
610enddef
611
612def Test_import_in_filetype()
613 # check that :import works when the buffer is locked
614 mkdir('ftplugin', 'p')
615 var export_lines =<< trim END
616 vim9script
617 export var That = 'yes'
618 END
619 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
620
621 var import_lines =<< trim END
622 vim9script
623 import './Xexport_ft.vim' as ft
624 assert_equal('yes', ft.That)
625 g:did_load_mytpe = 1
626 END
627 writefile(import_lines, 'ftplugin/qf.vim')
628
629 var save_rtp = &rtp
630 &rtp = getcwd() .. ',' .. &rtp
631
632 filetype plugin on
633 copen
634 assert_equal(1, g:did_load_mytpe)
635
636 quit!
637 delete('Xexport_ft.vim')
638 delete('ftplugin', 'rf')
639 &rtp = save_rtp
640enddef
641
642def Test_use_import_in_mapping()
643 var lines =<< trim END
644 vim9script
Bram Moolenaar89445512022-04-14 12:58:23 +0100645 export def Funcx(nr: number)
646 g:result = nr
Bram Moolenaard8448622022-01-07 21:39:52 +0000647 enddef
648 END
649 writefile(lines, 'XsomeExport.vim')
650 lines =<< trim END
651 vim9script
652 import './XsomeExport.vim' as some
653 var Funcy = some.Funcx
Bram Moolenaar89445512022-04-14 12:58:23 +0100654 nnoremap <F3> :call <sid>Funcy(42)<cr>
655 nnoremap <F4> :call <sid>some.Funcx(44)<cr>
Bram Moolenaard8448622022-01-07 21:39:52 +0000656 END
657 writefile(lines, 'Xmapscript.vim')
658
659 source Xmapscript.vim
660 feedkeys("\<F3>", "xt")
661 assert_equal(42, g:result)
Bram Moolenaar89445512022-04-14 12:58:23 +0100662 feedkeys("\<F4>", "xt")
663 assert_equal(44, g:result)
Bram Moolenaard8448622022-01-07 21:39:52 +0000664
665 unlet g:result
666 delete('XsomeExport.vim')
667 delete('Xmapscript.vim')
668 nunmap <F3>
Bram Moolenaar89445512022-04-14 12:58:23 +0100669 nunmap <F4>
670enddef
671
Bram Moolenaar648dd882022-04-14 21:36:15 +0100672def Test_use_relative_autoload_import_in_mapping()
Bram Moolenaar89445512022-04-14 12:58:23 +0100673 var lines =<< trim END
674 vim9script
675 export def Func()
676 g:result = 42
677 enddef
678 END
Bram Moolenaar648dd882022-04-14 21:36:15 +0100679 writefile(lines, 'XrelautoloadExport.vim')
Bram Moolenaar89445512022-04-14 12:58:23 +0100680 lines =<< trim END
681 vim9script
Bram Moolenaar648dd882022-04-14 21:36:15 +0100682 import autoload './XrelautoloadExport.vim' as some
Bram Moolenaar89445512022-04-14 12:58:23 +0100683 nnoremap <F3> :call <SID>some.Func()<CR>
684 END
685 writefile(lines, 'Xmapscript.vim')
686
687 source Xmapscript.vim
Bram Moolenaar648dd882022-04-14 21:36:15 +0100688 assert_match('\d\+ A: .*XrelautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
Bram Moolenaar89445512022-04-14 12:58:23 +0100689 feedkeys("\<F3>", "xt")
690 assert_equal(42, g:result)
691
692 unlet g:result
Bram Moolenaar648dd882022-04-14 21:36:15 +0100693 delete('XrelautoloadExport.vim')
Bram Moolenaar89445512022-04-14 12:58:23 +0100694 delete('Xmapscript.vim')
695 nunmap <F3>
Bram Moolenaard8448622022-01-07 21:39:52 +0000696enddef
697
Bram Moolenaar648dd882022-04-14 21:36:15 +0100698def Test_use_autoload_import_in_mapping()
699 var lines =<< trim END
700 vim9script
701 export def Func()
702 g:result = 49
703 enddef
704 END
705 mkdir('Xdir/autoload', 'p')
706 writefile(lines, 'Xdir/autoload/XautoloadExport.vim')
707 var save_rtp = &rtp
708 exe 'set rtp^=' .. getcwd() .. '/Xdir'
709
710 lines =<< trim END
711 vim9script
712 import autoload 'XautoloadExport.vim' as some
713 nnoremap <F3> :call <SID>some.Func()<CR>
714 END
715 writefile(lines, 'Xmapscript.vim')
716
717 source Xmapscript.vim
718 assert_match('\d\+ A: .*autoload/XautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
719 feedkeys("\<F3>", "xt")
720 assert_equal(49, g:result)
721
722 unlet g:result
723 delete('Xmapscript.vim')
724 nunmap <F3>
725 delete('Xdir', 'rf')
726 &rtp = save_rtp
727enddef
728
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000729def Test_use_import_in_command_completion()
Bram Moolenaar15d16352022-01-17 20:09:08 +0000730 var lines =<< trim END
731 vim9script
732 export def Complete(..._): list<string>
733 return ['abcd']
734 enddef
735 END
736 writefile(lines, 'Xscript.vim')
737
738 lines =<< trim END
739 vim9script
740 import './Xscript.vim'
741
742 command -nargs=1 -complete=customlist,Xscript.Complete Cmd echo 'ok'
743 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
744 assert_equal('#Cmd abcd', @:)
745 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000746 v9.CheckScriptSuccess(lines)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000747
748 delcommand Cmd
749 delete('Xscript.vim')
750enddef
751
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000752def Test_use_autoload_import_in_insert_completion()
753 mkdir('Xdir/autoload', 'p')
754 var save_rtp = &rtp
755 exe 'set rtp^=' .. getcwd() .. '/Xdir'
756
757 var lines =<< trim END
758 vim9script
759 export def ThesaurusFunc(findbase: bool, _): any
760 if findbase
761 return 1
762 endif
763 return [
764 'check',
765 'experiment',
766 'test',
767 'verification'
768 ]
769 enddef
770 g:completion_loaded = 'yes'
771 END
772 writefile(lines, 'Xdir/autoload/completion.vim')
773
774 new
775 lines =<< trim END
776 vim9script
777 g:completion_loaded = 'no'
778 import autoload 'completion.vim'
779 set thesaurusfunc=completion.ThesaurusFunc
780 assert_equal('no', g:completion_loaded)
781 feedkeys("i\<C-X>\<C-T>\<C-N>\<Esc>", 'xt')
782 assert_equal('experiment', getline(1))
783 assert_equal('yes', g:completion_loaded)
784 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000785 v9.CheckScriptSuccess(lines)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000786
787 set thesaurusfunc=
788 bwipe!
789 delete('Xdir', 'rf')
790 &rtp = save_rtp
791enddef
792
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000793def Test_use_autoload_import_partial_in_opfunc()
794 mkdir('Xdir/autoload', 'p')
795 var save_rtp = &rtp
796 exe 'set rtp^=' .. getcwd() .. '/Xdir'
797
798 var lines =<< trim END
799 vim9script
800 export def Opfunc(..._)
801 g:opfunc_called = 'yes'
802 enddef
803 END
804 writefile(lines, 'Xdir/autoload/opfunc.vim')
805
806 new
807 lines =<< trim END
808 vim9script
809 import autoload 'opfunc.vim'
810 nnoremap <expr> <F3> TheFunc()
811 def TheFunc(): string
812 &operatorfunc = function('opfunc.Opfunc', [0])
813 return 'g@'
814 enddef
815 feedkeys("\<F3>l", 'xt')
816 assert_equal('yes', g:opfunc_called)
817 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000818 v9.CheckScriptSuccess(lines)
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000819
820 set opfunc=
821 bwipe!
822 delete('Xdir', 'rf')
Bram Moolenaar06b77222022-01-25 15:51:56 +0000823 nunmap <F3>
824 &rtp = save_rtp
825enddef
826
827def Test_set_opfunc_to_autoload_func_directly()
828 mkdir('Xdir/autoload', 'p')
829 var save_rtp = &rtp
830 exe 'set rtp^=' .. getcwd() .. '/Xdir'
831
832 var lines =<< trim END
833 vim9script
834 export def Opfunc(..._)
835 g:opfunc_called = 'yes'
836 enddef
837 END
838 writefile(lines, 'Xdir/autoload/opfunc.vim')
839
840 new
841 lines =<< trim END
842 vim9script
843 import autoload 'opfunc.vim'
844 nnoremap <expr> <F3> TheFunc()
845 def TheFunc(): string
846 &operatorfunc = opfunc.Opfunc
847 return 'g@'
848 enddef
849 feedkeys("\<F3>l", 'xt')
850 assert_equal('yes', g:opfunc_called)
851 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000852 v9.CheckScriptSuccess(lines)
Bram Moolenaar06b77222022-01-25 15:51:56 +0000853
854 set opfunc=
855 bwipe!
856 delete('Xdir', 'rf')
857 nunmap <F3>
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000858 &rtp = save_rtp
859enddef
860
Bram Moolenaare70dd112022-01-21 16:31:11 +0000861def Test_use_autoload_import_in_fold_expression()
862 mkdir('Xdir/autoload', 'p')
863 var save_rtp = &rtp
864 exe 'set rtp^=' .. getcwd() .. '/Xdir'
865
866 var lines =<< trim END
867 vim9script
868 export def Expr(): string
869 return getline(v:lnum) =~ '^#' ? '>1' : '1'
870 enddef
Bram Moolenaar9530b582022-01-22 13:39:08 +0000871 export def Text(): string
872 return 'fold text'
873 enddef
Bram Moolenaare70dd112022-01-21 16:31:11 +0000874 g:fold_loaded = 'yes'
875 END
876 writefile(lines, 'Xdir/autoload/fold.vim')
877
878 lines =<< trim END
879 vim9script
880 import autoload 'fold.vim'
881 &foldexpr = 'fold.Expr()'
Bram Moolenaar9530b582022-01-22 13:39:08 +0000882 &foldtext = 'fold.Text()'
Bram Moolenaare70dd112022-01-21 16:31:11 +0000883 &foldmethod = 'expr'
884 &debug = 'throw'
885 END
886 new
887 setline(1, ['# one', 'text', '# two', 'text'])
888 g:fold_loaded = 'no'
Bram Moolenaar62aec932022-01-29 21:45:34 +0000889 v9.CheckScriptSuccess(lines)
Bram Moolenaare70dd112022-01-21 16:31:11 +0000890 assert_equal('no', g:fold_loaded)
891 redraw
892 assert_equal('yes', g:fold_loaded)
893
894 # Check that script context of 'foldexpr' is copied to another buffer.
895 edit! otherfile
896 redraw
897
Bram Moolenaar9530b582022-01-22 13:39:08 +0000898 set foldexpr= foldtext& foldmethod& debug=
Bram Moolenaare70dd112022-01-21 16:31:11 +0000899 bwipe!
900 delete('Xdir', 'rf')
901 &rtp = save_rtp
902enddef
903
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100904def Test_autoload_import_relative()
905 var lines =<< trim END
906 vim9script
907
908 g:loaded = 'yes'
909 export def RelFunc(): string
910 return 'relfunc'
911 enddef
912 def NotExported()
913 echo 'not'
914 enddef
915
916 export var someText = 'some text'
917 var notexp = 'bad'
918 END
919 writefile(lines, 'XimportRel.vim')
920 writefile(lines, 'XimportRel2.vim')
921 writefile(lines, 'XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +0100922 writefile(lines, 'XimportRel4.vim')
923 writefile(lines, 'XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100924
925 lines =<< trim END
926 vim9script
927 g:loaded = 'no'
928 import autoload './XimportRel.vim'
929 assert_equal('no', g:loaded)
930
931 def AFunc(): string
932 var res = ''
933 res ..= XimportRel.RelFunc()
934 res ..= '/'
935 res ..= XimportRel.someText
936 XimportRel.someText = 'from AFunc'
937 return res
938 enddef
939 # script not loaded when compiling
940 defcompile
941 assert_equal('no', g:loaded)
942
943 assert_equal('relfunc/some text', AFunc())
944 assert_equal('yes', g:loaded)
945 unlet g:loaded
946
947 assert_equal('from AFunc', XimportRel.someText)
948 XimportRel.someText = 'from script'
949 assert_equal('from script', XimportRel.someText)
950 END
951 v9.CheckScriptSuccess(lines)
952
953 lines =<< trim END
954 vim9script
955 import autoload './XimportRel.vim'
956 echo XimportRel.NotExported()
957 END
958 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 3)
959
960 lines =<< trim END
961 vim9script
962 import autoload './XimportRel.vim'
963 echo XimportRel.notexp
964 END
965 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
966
967 lines =<< trim END
968 vim9script
969 import autoload './XimportRel.vim'
970 XimportRel.notexp = 'bad'
971 END
972 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
973
974 lines =<< trim END
975 vim9script
976 import autoload './XimportRel.vim'
977 def Func()
978 echo XimportRel.NotExported()
979 enddef
980 Func()
981 END
982 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 1)
983
984 lines =<< trim END
985 vim9script
986 import autoload './XimportRel.vim'
987 def Func()
988 echo XimportRel.notexp
989 enddef
990 Func()
991 END
992 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
993
Bram Moolenaar10611952022-04-03 21:11:34 +0100994 # Same, script not imported before
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100995 lines =<< trim END
996 vim9script
Bram Moolenaar10611952022-04-03 21:11:34 +0100997 import autoload './XimportRel4.vim'
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100998 def Func()
Bram Moolenaar10611952022-04-03 21:11:34 +0100999 echo XimportRel4.notexp
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001000 enddef
1001 Func()
1002 END
1003 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1004
Bram Moolenaar10611952022-04-03 21:11:34 +01001005 # does not fail if the script wasn't loaded yet and only compiling
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001006 g:loaded = 'no'
1007 lines =<< trim END
1008 vim9script
1009 import autoload './XimportRel2.vim'
1010 def Func()
1011 echo XimportRel2.notexp
1012 enddef
1013 defcompile
1014 END
1015 v9.CheckScriptSuccess(lines)
1016 assert_equal('no', g:loaded)
1017
Bram Moolenaar10611952022-04-03 21:11:34 +01001018 lines =<< trim END
1019 vim9script
1020 import autoload './XimportRel.vim'
1021 def Func()
1022 XimportRel.notexp = 'bad'
1023 enddef
1024 Func()
1025 END
1026 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1027
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001028 # fails with a not loaded import
1029 lines =<< trim END
1030 vim9script
1031 import autoload './XimportRel3.vim'
1032 def Func()
1033 XimportRel3.notexp = 'bad'
1034 enddef
1035 Func()
1036 END
1037 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1038 assert_equal('yes', g:loaded)
1039 unlet g:loaded
1040
Bram Moolenaar10611952022-04-03 21:11:34 +01001041 lines =<< trim END
1042 vim9script
1043 import autoload './XimportRel5.vim'
1044 def Func()
1045 XimportRel5.nosuchvar = 'bad'
1046 enddef
1047 Func()
1048 END
1049 v9.CheckScriptFailure(lines, 'E121: Undefined variable: nosuchvar', 1)
1050 unlet g:loaded
1051
1052 # nasty: delete script after compiling function
1053 writefile(['vim9script'], 'XimportRelDel.vim')
1054 lines =<< trim END
1055 vim9script
1056
1057 import autoload './XimportRelDel.vim'
1058 def DoIt()
1059 echo XimportRelDel.var
1060 enddef
1061 defcompile
1062 delete('XimportRelDel.vim')
1063 DoIt()
1064 END
Bram Moolenaar242c1522022-04-03 21:52:51 +01001065 v9.CheckScriptFailure(lines, 'E484:')
Bram Moolenaar10611952022-04-03 21:11:34 +01001066
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001067 delete('XimportRel.vim')
1068 delete('XimportRel2.vim')
1069 delete('XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +01001070 delete('XimportRel4.vim')
1071 delete('XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001072enddef
1073
Bram Moolenaarccbfd482022-03-31 16:18:23 +01001074def Test_autoload_import_relative_autoload_dir()
1075 mkdir('autoload', 'p')
1076 var lines =<< trim END
1077 vim9script
1078 export def Bar()
1079 g:called_bar = 'yes'
1080 enddef
1081 END
1082 writefile(lines, 'autoload/script.vim')
1083
1084 lines =<< trim END
1085 vim9script
1086 import autoload './autoload/script.vim'
1087 def Foo()
1088 script.Bar()
1089 enddef
1090 Foo()
1091 assert_equal('yes', g:called_bar)
1092 END
1093 v9.CheckScriptSuccess(lines)
1094
1095 unlet g:called_bar
1096 delete('autoload', 'rf')
1097enddef
1098
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001099func Test_import_in_diffexpr()
1100 CheckExecutable diff
1101
1102 call Run_Test_import_in_diffexpr()
1103endfunc
1104
1105def Run_Test_import_in_diffexpr()
1106 var lines =<< trim END
1107 vim9script
1108
1109 export def DiffExpr()
1110 # Prepend some text to check diff type detection
1111 writefile(['warning', ' message'], v:fname_out)
1112 silent exe '!diff ' .. v:fname_in .. ' '
1113 .. v:fname_new .. '>>' .. v:fname_out
1114 enddef
1115 END
1116 writefile(lines, 'Xdiffexpr')
1117
1118 lines =<< trim END
1119 vim9script
1120 import './Xdiffexpr' as diff
1121
1122 set diffexpr=diff.DiffExpr()
1123 set diffopt=foldcolumn:0
1124 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001125 v9.CheckScriptSuccess(lines)
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001126
1127 enew!
1128 call setline(1, ['one', 'two', 'three'])
1129 diffthis
1130
1131 botright vert new
1132 call setline(1, ['one', 'two', 'three.'])
1133 diffthis
1134 # we only check if this does not cause errors
1135 redraw
1136
1137 diffoff!
1138 bwipe!
1139 bwipe!
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001140 delete('Xdiffexpr')
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001141enddef
1142
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001143def Test_import_in_patchexpr()
1144 var lines =<< trim END
1145 vim9script
1146 export def TPatch()
1147 call writefile(['output file'], v:fname_out)
1148 enddef
1149 END
1150 writefile(lines, 'Xpatchexpr')
1151
1152 lines =<< trim END
1153 vim9script
1154 import './Xpatchexpr' as patch
1155 set patchexpr=patch.TPatch()
1156 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001157 v9.CheckScriptSuccess(lines)
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001158
1159 call writefile(['input file'], 'Xinput')
1160 call writefile(['diff file'], 'Xdiff')
1161 :%bwipe!
1162 edit Xinput
1163 diffpatch Xdiff
1164 call assert_equal('output file', getline(1))
1165
1166 call delete('Xinput')
1167 call delete('Xdiff')
1168 call delete('Xpatchexpr')
1169 set patchexpr&
1170 :%bwipe!
1171enddef
1172
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001173def Test_import_in_formatexpr()
1174 var lines =<< trim END
1175 vim9script
1176 export def MyFormatExpr(): number
1177 g:did_format = 'yes'
1178 return 0
1179 enddef
1180 END
1181 writefile(lines, 'Xformatter')
1182
1183 lines =<< trim END
1184 vim9script
1185 import './Xformatter' as format
1186 set formatexpr=format.MyFormatExpr()
1187 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001188 v9.CheckScriptSuccess(lines)
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001189
1190 new
1191 setline(1, ['a', 'b', 'c'])
1192 normal gqG
1193 assert_equal('yes', g:did_format)
1194
1195 bwipe!
1196 delete('Xformatter')
1197 unlet g:did_format
1198 set formatexpr=
1199enddef
1200
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001201def Test_import_in_includeexpr()
1202 writefile(['found it'], 'Xthisfile')
1203 new
1204
1205 var lines =<< trim END
1206 vim9script
1207 export def DoSub(): string
1208 return substitute(v:fname, 'that', 'this', '')
1209 enddef
1210 END
1211 writefile(lines, 'Xinclude.vim')
1212
1213 lines =<< trim END
1214 vim9script
1215 import './Xinclude.vim'
1216 set includeexpr=Xinclude.DoSub()
1217 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001218 v9.CheckScriptSuccess(lines)
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001219
1220 setline(1, ['Xthatfile'])
1221 exe "normal \<C-W>f"
1222 assert_equal('Xthisfile', expand('%'))
1223
1224 bwipe!
1225 bwipe!
1226 set includeexpr=
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001227 delete('Xinclude.vim')
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001228 delete('Xthisfile')
1229enddef
1230
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001231def Test_import_in_indentexpr()
1232 var lines =<< trim END
1233 vim9script
1234 export def GetIndent(): number
1235 return 5
1236 enddef
1237 END
1238 writefile(lines, 'Xindenter')
1239
1240 lines =<< trim END
1241 vim9script
1242 import './Xindenter' as indent
1243 set indentexpr=indent.GetIndent()
1244 set debug=throw
1245 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001246 v9.CheckScriptSuccess(lines)
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001247
1248 new
1249 setline(1, 'hello')
1250 normal ==
1251 assert_equal(' hello', getline(1))
1252
1253 bwipe!
1254 set indentexpr= debug=
1255 delete('Xindenter')
1256enddef
1257
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001258func Test_import_in_printexpr()
1259 CheckFeature postscript
1260 call Run_Test_import_in_printexpr()
1261endfunc
1262
1263def Run_Test_import_in_printexpr()
1264 var lines =<< trim END
1265 vim9script
1266 export def PrintFile(): bool
1267 g:printed = 'yes'
1268 delete('v:fname_in')
1269 return false
1270 enddef
1271 END
1272 writefile(lines, 'Xprint.vim')
1273
1274 lines =<< trim END
1275 vim9script
1276 import './Xprint.vim'
1277 set printexpr=Xprint.PrintFile()
1278 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001279 v9.CheckScriptSuccess(lines)
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001280
1281 help
1282 hardcopy dummy args
1283 assert_equal('yes', g:printed)
1284
1285 delete('Xprint.vim')
1286 set printexpr=
1287enddef
1288
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001289def Test_import_in_charconvert()
1290 var lines =<< trim END
1291 vim9script
1292 export def MakeUpper(): bool
1293 var data = readfile(v:fname_in)
1294 map(data, 'toupper(v:val)')
1295 writefile(data, v:fname_out)
1296 return false # success
1297 enddef
1298 END
1299 writefile(lines, 'Xconvert.vim')
1300
1301 lines =<< trim END
1302 vim9script
1303 import './Xconvert.vim' as conv
1304 set charconvert=conv.MakeUpper()
1305 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001306 v9.CheckScriptSuccess(lines)
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001307
1308 writefile(['one', 'two'], 'Xfile')
1309 new Xfile
1310 write ++enc=ucase Xfile1
1311 assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1312
1313 delete('Xfile')
1314 delete('Xfile1')
1315 delete('Xconvert.vim')
1316 bwipe!
1317 set charconvert&
1318enddef
1319
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001320func Test_import_in_spellsuggest_expr()
1321 CheckFeature spell
1322 call Run_Test_import_in_spellsuggest_expr()
1323endfunc
1324
1325def Run_Test_import_in_spellsuggest_expr()
1326 var lines =<< trim END
1327 vim9script
1328 export def MySuggest(): list<any>
1329 return [['Fox', 8], ['Fop', 9]]
1330 enddef
1331 END
1332 writefile(lines, 'Xsuggest.vim')
1333
1334 lines =<< trim END
1335 vim9script
1336 import './Xsuggest.vim' as sugg
1337 set spell spellsuggest=expr:sugg.MySuggest()
1338 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001339 v9.CheckScriptSuccess(lines)
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001340
1341 set verbose=1 # report errors
1342 call assert_equal(['Fox', 'Fop'], spellsuggest('Fo', 2))
1343
1344 delete('Xsuggest.vim')
1345 set nospell spellsuggest& verbose=0
1346enddef
1347
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001348def Test_export_shadows_global_function()
1349 mkdir('Xdir/autoload', 'p')
1350 var save_rtp = &rtp
1351 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1352
1353 var lines =<< trim END
1354 vim9script
1355 export def Shadow(): string
1356 return 'Shadow()'
1357 enddef
1358 END
1359 writefile(lines, 'Xdir/autoload/shadow.vim')
1360
1361 lines =<< trim END
1362 vim9script
1363
1364 def g:Shadow(): string
1365 return 'global'
1366 enddef
1367
1368 import autoload 'shadow.vim'
1369 assert_equal('Shadow()', shadow.Shadow())
1370 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001371 v9.CheckScriptSuccess(lines)
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001372
1373 delfunc g:Shadow
1374 bwipe!
1375 delete('Xdir', 'rf')
1376 &rtp = save_rtp
1377enddef
1378
Bram Moolenaard8448622022-01-07 21:39:52 +00001379def Test_export_fails()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001380 v9.CheckScriptFailure(['export var some = 123'], 'E1042:')
1381 v9.CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
1382 v9.CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001383
1384 assert_fails('export something', 'E1043:')
1385enddef
1386
1387func Test_import_fails_without_script()
1388 CheckRunVimInTerminal
1389
1390 " call indirectly to avoid compilation error for missing functions
1391 call Run_Test_import_fails_on_command_line()
1392endfunc
1393
1394def Run_Test_import_fails_on_command_line()
1395 var export =<< trim END
1396 vim9script
1397 export def Foo(): number
1398 return 0
1399 enddef
1400 END
1401 writefile(export, 'XexportCmd.vim')
1402
Bram Moolenaar62aec932022-01-29 21:45:34 +00001403 var buf = g:RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaard8448622022-01-07 21:39:52 +00001404 rows: 6, wait_for_ruler: 0})
Bram Moolenaar62aec932022-01-29 21:45:34 +00001405 g:WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaard8448622022-01-07 21:39:52 +00001406
1407 delete('XexportCmd.vim')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001408 g:StopVimInTerminal(buf)
Bram Moolenaard8448622022-01-07 21:39:52 +00001409enddef
1410
1411def Test_vim9_reload_noclear()
1412 var lines =<< trim END
1413 vim9script
1414 export var exported = 'thexport'
1415
1416 export def TheFunc(x = 0)
1417 enddef
1418 END
1419 writefile(lines, 'XExportReload')
1420 lines =<< trim END
1421 vim9script noclear
1422 g:loadCount += 1
Bram Moolenaara749a422022-02-12 19:52:25 +00001423 var reloaded = 'init'
Bram Moolenaard8448622022-01-07 21:39:52 +00001424 import './XExportReload' as exp
1425
1426 def Again(): string
1427 return 'again'
1428 enddef
1429
1430 exp.TheFunc()
1431
Bram Moolenaara749a422022-02-12 19:52:25 +00001432 if exists('loaded') | finish | endif
1433 var loaded = true
Bram Moolenaard8448622022-01-07 21:39:52 +00001434
Bram Moolenaara749a422022-02-12 19:52:25 +00001435 var notReloaded = 'yes'
1436 reloaded = 'first'
Bram Moolenaard8448622022-01-07 21:39:52 +00001437 def g:Values(): list<string>
Bram Moolenaara749a422022-02-12 19:52:25 +00001438 return [reloaded, notReloaded, Again(), Once(), exp.exported]
Bram Moolenaard8448622022-01-07 21:39:52 +00001439 enddef
1440
1441 def Once(): string
1442 return 'once'
1443 enddef
1444 END
1445 writefile(lines, 'XReloaded')
1446 g:loadCount = 0
1447 source XReloaded
1448 assert_equal(1, g:loadCount)
1449 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
1450 source XReloaded
1451 assert_equal(2, g:loadCount)
1452 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1453 source XReloaded
1454 assert_equal(3, g:loadCount)
1455 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1456
1457 delete('XReloaded')
1458 delete('XExportReload')
1459 delfunc g:Values
1460 unlet g:loadCount
1461
1462 lines =<< trim END
1463 vim9script
1464 def Inner()
1465 enddef
1466 END
1467 lines->writefile('XreloadScript.vim')
1468 source XreloadScript.vim
1469
1470 lines =<< trim END
1471 vim9script
1472 def Outer()
1473 def Inner()
1474 enddef
1475 enddef
1476 defcompile
1477 END
1478 lines->writefile('XreloadScript.vim')
1479 source XreloadScript.vim
1480
1481 delete('XreloadScript.vim')
1482enddef
1483
Bram Moolenaarcd1cda22022-02-16 21:48:25 +00001484def Test_vim_reload_noclear_arg_count()
1485 var lines =<< trim END
1486 vim9script noclear
1487
1488 if !exists('g:didload')
1489 def Test(a: string, b: string)
1490 echo a b
1491 enddef
1492 def Call()
1493 Test('a', 'b')
1494 enddef
1495 else
1496 # redefine with one argument less
1497 def Test(a: string)
1498 echo a
1499 enddef
1500 endif
1501 Call()
1502 g:didload = 1
1503 END
1504 lines->writefile('XreloadScript_1.vim')
1505 source XreloadScript_1.vim
1506 assert_fails('source XreloadScript_1.vim', 'E1106: One argument too many')
1507 unlet g:didload
1508
1509 lines =<< trim END
1510 vim9script noclear
1511
1512 if !exists('g:didload')
1513 def Test(a: string, b: string, c: string)
1514 echo a b
1515 enddef
1516 def Call()
1517 Test('a', 'b', 'c')
1518 enddef
1519 else
1520 # redefine with one argument less
1521 def Test(a: string)
1522 echo a
1523 enddef
1524 endif
1525 Call()
1526 g:didload = 1
1527 END
1528 lines->writefile('XreloadScript_2.vim')
1529 source XreloadScript_2.vim
1530 assert_fails('source XreloadScript_2.vim', 'E1106: 2 arguments too many')
1531 unlet g:didload
1532
1533 lines =<< trim END
1534 vim9script noclear
1535
1536 if !exists('g:didload')
1537 def Test(a: string)
1538 echo a
1539 enddef
1540 def Call()
1541 Test('a')
1542 enddef
1543 else
1544 # redefine with one argument extra
1545 def Test(a: string, b: string)
1546 echo a b
1547 enddef
1548 endif
1549 Call()
1550 g:didload = 1
1551 END
1552 lines->writefile('XreloadScript_3.vim')
1553 source XreloadScript_3.vim
1554 assert_fails('source XreloadScript_3.vim', 'E1190: One argument too few')
1555 unlet g:didload
1556
1557 lines =<< trim END
1558 vim9script noclear
1559
1560 if !exists('g:didload')
1561 def Test(a: string)
1562 echo a
1563 enddef
1564 def Call()
1565 Test('a')
1566 enddef
1567 else
1568 # redefine with two arguments extra
1569 def Test(a: string, b: string, c: string)
1570 echo a b
1571 enddef
1572 endif
1573 Call()
1574 g:didload = 1
1575 END
1576 lines->writefile('XreloadScript_4.vim')
1577 source XreloadScript_4.vim
1578 assert_fails('source XreloadScript_4.vim', 'E1190: 2 arguments too few')
1579 unlet g:didload
1580
1581 delete('XreloadScript_1.vim')
1582 delete('XreloadScript_2.vim')
1583 delete('XreloadScript_3.vim')
1584 delete('XreloadScript_4.vim')
1585enddef
1586
1587def Test_vim9_reload_noclear_error()
1588 var lines =<< trim END
1589 vim9script noclear
1590
1591 if !exists('g:didload')
1592 def Test(a: string)
1593 echo a
1594 enddef
1595 def Call()
1596 Test('a')
1597 enddef
1598 else
1599 # redefine with a compile error
1600 def Test(a: string)
1601 echo ax
1602 enddef
1603 endif
1604 Call()
1605 g:didload = 1
1606 END
1607 lines->writefile('XreloadScriptErr.vim')
1608 source XreloadScriptErr.vim
1609 assert_fails('source XreloadScriptErr.vim', 'E1001: Variable not found: ax')
1610
1611 unlet g:didload
1612 delete('XreloadScriptErr.vim')
1613enddef
1614
Bram Moolenaard8448622022-01-07 21:39:52 +00001615def Test_vim9_reload_import()
1616 var lines =<< trim END
1617 vim9script
1618 const var = ''
1619 var valone = 1234
1620 def MyFunc(arg: string)
1621 valone = 5678
1622 enddef
1623 END
1624 var morelines =<< trim END
1625 var valtwo = 222
1626 export def GetValtwo(): number
1627 return valtwo
1628 enddef
1629 END
1630 writefile(lines + morelines, 'Xreload.vim')
1631 source Xreload.vim
1632 source Xreload.vim
1633 source Xreload.vim
1634
1635 # cannot declare a var twice
1636 lines =<< trim END
1637 vim9script
1638 var valone = 1234
1639 var valone = 5678
1640 END
1641 writefile(lines, 'Xreload.vim')
1642 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
1643
1644 delete('Xreload.vim')
1645 delete('Ximport.vim')
1646enddef
1647
1648" if a script is reloaded with a script-local variable that changed its type, a
1649" compiled function using that variable must fail.
1650def Test_script_reload_change_type()
1651 var lines =<< trim END
1652 vim9script noclear
1653 var str = 'string'
1654 def g:GetStr(): string
1655 return str .. 'xxx'
1656 enddef
1657 END
1658 writefile(lines, 'Xreload.vim')
1659 source Xreload.vim
1660 echo g:GetStr()
1661
1662 lines =<< trim END
1663 vim9script noclear
1664 var str = 1234
1665 END
1666 writefile(lines, 'Xreload.vim')
1667 source Xreload.vim
1668 assert_fails('echo g:GetStr()', 'E1150:')
1669
1670 delfunc g:GetStr
1671 delete('Xreload.vim')
1672enddef
1673
1674" Define CallFunc so that the test can be compiled
1675command CallFunc echo 'nop'
1676
1677def Test_script_reload_from_function()
1678 var lines =<< trim END
1679 vim9script
1680
Bram Moolenaar10611952022-04-03 21:11:34 +01001681 if exists('g:loadedThis')
Bram Moolenaard8448622022-01-07 21:39:52 +00001682 finish
1683 endif
Bram Moolenaar10611952022-04-03 21:11:34 +01001684 g:loadedThis = 1
Bram Moolenaard8448622022-01-07 21:39:52 +00001685 delcommand CallFunc
1686 command CallFunc Func()
1687 def Func()
1688 so XreloadFunc.vim
1689 g:didTheFunc = 1
1690 enddef
1691 END
1692 writefile(lines, 'XreloadFunc.vim')
1693 source XreloadFunc.vim
1694 CallFunc
1695 assert_equal(1, g:didTheFunc)
1696
1697 delete('XreloadFunc.vim')
1698 delcommand CallFunc
Bram Moolenaar10611952022-04-03 21:11:34 +01001699 unlet g:loadedThis
Bram Moolenaard8448622022-01-07 21:39:52 +00001700 unlet g:didTheFunc
1701enddef
1702
1703def s:RetSome(): string
1704 return 'some'
1705enddef
1706
1707" Not exported function that is referenced needs to be accessed by the
1708" script-local name.
1709def Test_vim9_funcref()
1710 var sortlines =<< trim END
1711 vim9script
1712 def Compare(i1: number, i2: number): number
1713 return i2 - i1
1714 enddef
1715
1716 export def FastSort(): list<number>
1717 return range(5)->sort(Compare)
1718 enddef
1719
1720 export def GetString(arg: string): string
1721 return arg
1722 enddef
1723 END
1724 writefile(sortlines, 'Xsort.vim')
1725
1726 var lines =<< trim END
1727 vim9script
1728 import './Xsort.vim'
1729 def Test()
1730 g:result = Xsort.FastSort()
1731 enddef
1732 Test()
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001733 END
1734 writefile(lines, 'Xscript.vim')
1735 source Xscript.vim
1736 assert_equal([4, 3, 2, 1, 0], g:result)
1737 unlet g:result
Bram Moolenaard8448622022-01-07 21:39:52 +00001738
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001739 lines =<< trim END
1740 vim9script
Bram Moolenaard8448622022-01-07 21:39:52 +00001741 # using a function imported with "as"
1742 import './Xsort.vim' as anAlias
1743 assert_equal('yes', anAlias.GetString('yes'))
1744
1745 # using the function from a compiled function
1746 def TestMore(): string
1747 var s = s:anAlias.GetString('foo')
1748 return s .. anAlias.GetString('bar')
1749 enddef
1750 assert_equal('foobar', TestMore())
1751
1752 # error when using a function that isn't exported
1753 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
1754 END
1755 writefile(lines, 'Xscript.vim')
1756
Bram Moolenaard8448622022-01-07 21:39:52 +00001757 delete('Xsort.vim')
1758 delete('Xscript.vim')
1759
1760 var Funcref = function('s:RetSome')
1761 assert_equal('some', Funcref())
1762enddef
1763
1764" Check that when searching for "FilterFunc" it finds the import in the
1765" script where FastFilter() is called from, both as a string and as a direct
1766" function reference.
1767def Test_vim9_funcref_other_script()
1768 var filterLines =<< trim END
1769 vim9script
1770 export def FilterFunc(idx: number, val: number): bool
1771 return idx % 2 == 1
1772 enddef
1773 export def FastFilter(): list<number>
1774 return range(10)->filter('FilterFunc(v:key, v:val)')
1775 enddef
1776 export def FastFilterDirect(): list<number>
1777 return range(10)->filter(FilterFunc)
1778 enddef
1779 END
1780 writefile(filterLines, 'Xfilter.vim')
1781
1782 var lines =<< trim END
1783 vim9script
1784 import './Xfilter.vim' as filter
1785 def Test()
1786 var x: list<number> = filter.FastFilter()
1787 enddef
1788 Test()
1789 def TestDirect()
1790 var x: list<number> = filter.FastFilterDirect()
1791 enddef
1792 TestDirect()
1793 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001794 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +00001795 delete('Xfilter.vim')
1796enddef
1797
1798def Test_import_absolute()
1799 var import_lines = [
1800 'vim9script',
1801 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
1802 'def UseExported()',
1803 ' g:imported_abs = abs.exported',
1804 ' abs.exported = 8888',
1805 ' g:imported_after = abs.exported',
1806 'enddef',
1807 'UseExported()',
1808 'g:import_disassembled = execute("disass UseExported")',
1809 ]
1810 writefile(import_lines, 'Ximport_abs.vim')
1811 writefile(s:export_script_lines, 'Xexport_abs.vim')
1812
1813 source Ximport_abs.vim
1814
1815 assert_equal(9876, g:imported_abs)
1816 assert_equal(8888, g:imported_after)
1817 assert_match('<SNR>\d\+_UseExported\_s*' ..
1818 'g:imported_abs = abs.exported\_s*' ..
1819 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1820 '1 STOREG g:imported_abs\_s*' ..
1821 'abs.exported = 8888\_s*' ..
1822 '2 PUSHNR 8888\_s*' ..
1823 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1824 'g:imported_after = abs.exported\_s*' ..
1825 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1826 '5 STOREG g:imported_after',
1827 g:import_disassembled)
1828
1829 Undo_export_script_lines()
1830 unlet g:imported_abs
1831 unlet g:import_disassembled
1832
1833 delete('Ximport_abs.vim')
1834 delete('Xexport_abs.vim')
1835enddef
1836
1837def Test_import_rtp()
1838 var import_lines = [
1839 'vim9script',
1840 'import "Xexport_rtp.vim" as rtp',
1841 'g:imported_rtp = rtp.exported',
1842 ]
1843 writefile(import_lines, 'Ximport_rtp.vim')
1844 mkdir('import', 'p')
1845 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1846
1847 var save_rtp = &rtp
1848 &rtp = getcwd()
1849 source Ximport_rtp.vim
1850 &rtp = save_rtp
1851
1852 assert_equal(9876, g:imported_rtp)
1853
1854 Undo_export_script_lines()
1855 unlet g:imported_rtp
1856 delete('Ximport_rtp.vim')
1857 delete('import', 'rf')
1858enddef
1859
1860def Test_import_compile_error()
1861 var export_lines = [
1862 'vim9script',
1863 'export def ExpFunc(): string',
1864 ' return notDefined',
1865 'enddef',
1866 ]
1867 writefile(export_lines, 'Xexported.vim')
1868
1869 var import_lines = [
1870 'vim9script',
1871 'import "./Xexported.vim" as expo',
1872 'def ImpFunc()',
1873 ' echo expo.ExpFunc()',
1874 'enddef',
1875 'defcompile',
1876 ]
1877 writefile(import_lines, 'Ximport.vim')
1878
1879 try
1880 source Ximport.vim
1881 catch /E1001/
1882 # Error should be before the Xexported.vim file.
1883 assert_match('E1001: Variable not found: notDefined', v:exception)
1884 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1885 endtry
1886
1887 delete('Xexported.vim')
1888 delete('Ximport.vim')
1889enddef
1890
1891def Test_func_overrules_import_fails()
1892 var export_lines =<< trim END
1893 vim9script
1894 export def Func()
1895 echo 'imported'
1896 enddef
1897 END
1898 writefile(export_lines, 'XexportedFunc.vim')
1899
1900 var lines =<< trim END
1901 vim9script
1902 import './XexportedFunc.vim' as Func
1903 def Func()
1904 echo 'local to function'
1905 enddef
1906 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001907 v9.CheckScriptFailure(lines, 'E1213: Redefining imported item "Func"')
Bram Moolenaard8448622022-01-07 21:39:52 +00001908
1909 lines =<< trim END
1910 vim9script
1911 import './XexportedFunc.vim' as Func
1912 def Outer()
1913 def Func()
1914 echo 'local to function'
1915 enddef
1916 enddef
1917 defcompile
1918 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001919 v9.CheckScriptFailure(lines, 'E1236:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001920
1921 delete('XexportedFunc.vim')
1922enddef
1923
1924def Test_source_vim9_from_legacy()
1925 var vim9_lines =<< trim END
1926 vim9script
1927 var local = 'local'
1928 g:global = 'global'
1929 export var exported = 'exported'
1930 export def GetText(): string
1931 return 'text'
1932 enddef
1933 END
1934 writefile(vim9_lines, 'Xvim9_script.vim')
1935
1936 var legacy_lines =<< trim END
1937 source Xvim9_script.vim
1938
1939 call assert_false(exists('local'))
1940 call assert_false(exists('exported'))
1941 call assert_false(exists('s:exported'))
1942 call assert_equal('global', global)
1943 call assert_equal('global', g:global)
Bram Moolenaard8448622022-01-07 21:39:52 +00001944 END
1945 writefile(legacy_lines, 'Xlegacy_script.vim')
1946
1947 source Xlegacy_script.vim
1948 assert_equal('global', g:global)
1949 unlet g:global
1950
1951 delete('Xlegacy_script.vim')
1952 delete('Xvim9_script.vim')
1953enddef
1954
Bram Moolenaarc43e6232022-01-13 20:51:56 +00001955def Test_import_vim9_from_legacy()
1956 var vim9_lines =<< trim END
1957 vim9script
1958 var local = 'local'
1959 g:global = 'global'
1960 export var exported = 'exported'
1961 export def GetText(): string
1962 return 'text'
1963 enddef
1964 END
1965 writefile(vim9_lines, 'Xvim9_export.vim')
1966
1967 var legacy_lines =<< trim END
1968 import './Xvim9_export.vim' as vim9
1969
1970 call assert_false(exists('vim9'))
1971 call assert_false(exists('local'))
1972 call assert_false(exists('s:vim9.local'))
1973 call assert_equal('global', global)
1974 call assert_equal('global', g:global)
1975 call assert_false(exists('exported'))
1976 call assert_false(exists('s:exported'))
1977 call assert_false(exists('*GetText'))
1978
1979 " imported symbol is script-local
1980 call assert_equal('exported', s:vim9.exported)
1981 call assert_equal('text', s:vim9.GetText())
1982 END
1983 writefile(legacy_lines, 'Xlegacy_script.vim')
1984
1985 source Xlegacy_script.vim
1986 assert_equal('global', g:global)
1987 unlet g:global
1988
1989 delete('Xlegacy_script.vim')
1990 delete('Xvim9_export.vim')
1991enddef
1992
Bram Moolenaard8448622022-01-07 21:39:52 +00001993def Test_cmdline_win()
1994 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
1995 # the command line window.
1996 mkdir('rtp/syntax', 'p')
1997 var export_lines =<< trim END
1998 vim9script
1999 export var That = 'yes'
2000 END
2001 writefile(export_lines, 'rtp/syntax/Xexport.vim')
2002 var import_lines =<< trim END
2003 vim9script
2004 import './Xexport.vim' as exp
2005 echo exp.That
2006 END
2007 writefile(import_lines, 'rtp/syntax/vim.vim')
2008 var save_rtp = &rtp
2009 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
2010 syntax on
2011 augroup CmdWin
2012 autocmd CmdwinEnter * g:got_there = 'yes'
2013 augroup END
2014 # this will open and also close the cmdline window
2015 feedkeys('q:', 'xt')
2016 assert_equal('yes', g:got_there)
2017
2018 augroup CmdWin
2019 au!
2020 augroup END
2021 &rtp = save_rtp
2022 delete('rtp', 'rf')
2023enddef
2024
2025def Test_import_gone_when_sourced_twice()
2026 var exportlines =<< trim END
2027 vim9script
2028 if exists('g:guard')
2029 finish
2030 endif
2031 g:guard = 1
2032 export var name = 'someName'
2033 END
2034 writefile(exportlines, 'XexportScript.vim')
2035
2036 var lines =<< trim END
2037 vim9script
2038 import './XexportScript.vim' as expo
2039 def g:GetName(): string
2040 return expo.name
2041 enddef
2042 END
2043 writefile(lines, 'XscriptImport.vim')
2044 so XscriptImport.vim
2045 assert_equal('someName', g:GetName())
2046
2047 so XexportScript.vim
2048 assert_fails('call g:GetName()', 'E1149:')
2049
2050 delfunc g:GetName
2051 delete('XexportScript.vim')
2052 delete('XscriptImport.vim')
2053 unlet g:guard
2054enddef
2055
Bram Moolenaar160aa862022-01-10 21:29:57 +00002056" test using an auto-loaded function and variable
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002057def Test_vim9_autoload_full_name()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002058 var lines =<< trim END
2059 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002060 export def Gettest(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002061 return 'test'
2062 enddef
2063 g:some#name = 'name'
2064 g:some#dict = {key: 'value'}
2065
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002066 export def Varargs(a1: string, ...l: list<string>): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002067 return a1 .. l[0] .. l[1]
2068 enddef
2069 END
2070
2071 mkdir('Xdir/autoload', 'p')
2072 writefile(lines, 'Xdir/autoload/some.vim')
2073 var save_rtp = &rtp
2074 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2075
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002076 assert_equal('test', g:some#Gettest())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002077 assert_equal('name', g:some#name)
2078 assert_equal('value', g:some#dict.key)
2079 g:some#other = 'other'
2080 assert_equal('other', g:some#other)
2081
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002082 assert_equal('abc', some#Varargs('a', 'b', 'c'))
Bram Moolenaar160aa862022-01-10 21:29:57 +00002083
2084 # upper case script name works
2085 lines =<< trim END
2086 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002087 export def GetOther(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002088 return 'other'
2089 enddef
2090 END
2091 writefile(lines, 'Xdir/autoload/Other.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002092 assert_equal('other', g:Other#GetOther())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002093
2094 delete('Xdir', 'rf')
2095 &rtp = save_rtp
2096enddef
2097
2098def Test_vim9script_autoload()
2099 mkdir('Xdir/autoload', 'p')
2100 var save_rtp = &rtp
2101 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2102
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002103 # when the path has "/autoload/" prefix is not needed
Bram Moolenaar160aa862022-01-10 21:29:57 +00002104 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002105 vim9script
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002106 g:prefixed_loaded += 1
Bram Moolenaar160aa862022-01-10 21:29:57 +00002107
2108 export def Gettest(): string
2109 return 'test'
2110 enddef
2111
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002112 export var name = 'name'
2113
2114 export func GetFunc()
2115 return Gettest() .. 'more' .. s:name
Bram Moolenaar160aa862022-01-10 21:29:57 +00002116 endfunc
2117
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002118 export def GetDef(): string
2119 return Gettest() .. 'more' .. name
2120 enddef
2121
Bram Moolenaar160aa862022-01-10 21:29:57 +00002122 export final fname = 'final'
2123 export const cname = 'const'
2124 END
2125 writefile(lines, 'Xdir/autoload/prefixed.vim')
2126
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002127 g:prefixed_loaded = 0
2128 g:expected_loaded = 0
Bram Moolenaar160aa862022-01-10 21:29:57 +00002129 lines =<< trim END
2130 vim9script
2131 import autoload 'prefixed.vim'
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002132 assert_equal(g:expected_loaded, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002133 assert_equal('test', prefixed.Gettest())
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002134 assert_equal(1, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002135
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002136 assert_equal('testmorename', prefixed.GetFunc())
2137 assert_equal('testmorename', prefixed.GetDef())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002138 assert_equal('name', prefixed.name)
2139 assert_equal('final', prefixed.fname)
2140 assert_equal('const', prefixed.cname)
2141 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002142 v9.CheckScriptSuccess(lines)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002143 # can source it again, autoload script not loaded again
2144 g:expected_loaded = 1
Bram Moolenaar62aec932022-01-29 21:45:34 +00002145 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002146
2147 # can also get the items by autoload name
2148 lines =<< trim END
2149 call assert_equal('test', prefixed#Gettest())
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002150 call assert_equal('testmorename', prefixed#GetFunc())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002151 call assert_equal('name', prefixed#name)
2152 call assert_equal('final', prefixed#fname)
2153 call assert_equal('const', prefixed#cname)
2154 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002155 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002156
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002157 unlet g:prefixed_loaded
2158 unlet g:expected_loaded
2159 delete('Xdir', 'rf')
2160 &rtp = save_rtp
2161enddef
2162
Bram Moolenaard02dce22022-01-18 17:43:04 +00002163def Test_import_autoload_not_exported()
2164 mkdir('Xdir/autoload', 'p')
2165 var save_rtp = &rtp
2166 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2167
2168 # error when using an item that is not exported from an autoload script
2169 var exportLines =<< trim END
2170 vim9script
2171 var notExported = 123
2172 def NotExport()
2173 echo 'nop'
2174 enddef
2175 END
2176 writefile(exportLines, 'Xdir/autoload/notExport1.vim')
2177
2178 var lines =<< trim END
2179 vim9script
2180 import autoload 'notExport1.vim'
2181 echo notExport1.notFound
2182 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002183 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002184
2185 lines =<< trim END
2186 vim9script
2187 import autoload 'notExport1.vim'
2188 echo notExport1.notExported
2189 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002190 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002191
2192 lines =<< trim END
2193 vim9script
2194 import autoload 'notExport1.vim'
2195 echo notExport1.NotFunc()
2196 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002197 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002198
2199 lines =<< trim END
2200 vim9script
2201 import autoload 'notExport1.vim'
2202 echo notExport1.NotExport()
2203 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002204 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002205
2206 lines =<< trim END
2207 vim9script
2208 import autoload 'notExport1.vim'
2209 echo 'text'->notExport1.NotFunc()
2210 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002211 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002212
2213 lines =<< trim END
2214 vim9script
2215 import autoload 'notExport1.vim'
2216 echo 'text'->notExport1.NotExport()
2217 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002218 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002219
2220 # using a :def function we use a different autoload script every time so that
2221 # the function is compiled without the script loaded
2222 writefile(exportLines, 'Xdir/autoload/notExport2.vim')
2223 lines =<< trim END
2224 vim9script
2225 import autoload 'notExport2.vim'
2226 def Testit()
2227 echo notExport2.notFound
2228 enddef
2229 Testit()
2230 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002231 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport2#notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002232
2233 writefile(exportLines, 'Xdir/autoload/notExport3.vim')
2234 lines =<< trim END
2235 vim9script
2236 import autoload 'notExport3.vim'
2237 def Testit()
2238 echo notExport3.notExported
2239 enddef
2240 Testit()
2241 END
2242 # don't get E1049 because it is too complicated to figure out
Bram Moolenaar62aec932022-01-29 21:45:34 +00002243 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport3#notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002244
2245 writefile(exportLines, 'Xdir/autoload/notExport4.vim')
2246 lines =<< trim END
2247 vim9script
2248 import autoload 'notExport4.vim'
2249 def Testit()
2250 echo notExport4.NotFunc()
2251 enddef
2252 Testit()
2253 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002254 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport4#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002255
2256 writefile(exportLines, 'Xdir/autoload/notExport5.vim')
2257 lines =<< trim END
2258 vim9script
2259 import autoload 'notExport5.vim'
2260 def Testit()
2261 echo notExport5.NotExport()
2262 enddef
2263 Testit()
2264 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002265 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport5#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002266
2267 writefile(exportLines, 'Xdir/autoload/notExport6.vim')
2268 lines =<< trim END
2269 vim9script
2270 import autoload 'notExport6.vim'
2271 def Testit()
2272 echo 'text'->notExport6.NotFunc()
2273 enddef
2274 Testit()
2275 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002276 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport6#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002277
2278 writefile(exportLines, 'Xdir/autoload/notExport7.vim')
2279 lines =<< trim END
2280 vim9script
2281 import autoload 'notExport7.vim'
2282 def Testit()
2283 echo 'text'->notExport7.NotExport()
2284 enddef
2285 Testit()
2286 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002287 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport7#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002288
2289 delete('Xdir', 'rf')
2290 &rtp = save_rtp
2291enddef
2292
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002293def Test_vim9script_autoload_call()
2294 mkdir('Xdir/autoload', 'p')
2295 var save_rtp = &rtp
2296 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2297
2298 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002299 vim9script
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002300
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002301 export def RetArg(arg: string): string
2302 return arg
2303 enddef
2304
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002305 export def Getother()
2306 g:result = 'other'
2307 enddef
2308 END
Bram Moolenaar5d982692022-01-12 15:15:27 +00002309 writefile(lines, 'Xdir/autoload/another.vim')
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002310
2311 lines =<< trim END
2312 vim9script
Bram Moolenaar5d982692022-01-12 15:15:27 +00002313 import autoload 'another.vim'
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002314
2315 # compile this before 'another.vim' is loaded
2316 def CallAnother()
2317 assert_equal('foo', 'foo'->another.RetArg())
2318 enddef
2319 CallAnother()
2320
Bram Moolenaar5d982692022-01-12 15:15:27 +00002321 call another.Getother()
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002322 assert_equal('other', g:result)
Bram Moolenaar3d8e25a2022-01-22 11:00:02 +00002323
2324 assert_equal('arg', call('another.RetArg', ['arg']))
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00002325
2326 verbose function another.Getother
2327 # should we disallow this?
2328 verbose function another#Getother
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002329 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002330 v9.CheckScriptSuccess(lines)
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002331
2332 unlet g:result
Bram Moolenaar160aa862022-01-10 21:29:57 +00002333 delete('Xdir', 'rf')
2334 &rtp = save_rtp
2335enddef
2336
Bram Moolenaarb697dc22022-01-22 11:27:29 +00002337def Test_vim9script_noclear_autoload()
2338 mkdir('Xdir/autoload', 'p')
2339 var save_rtp = &rtp
2340 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2341
2342 var lines =<< trim END
2343 vim9script
2344 export def Func(): string
2345 return 'called'
2346 enddef
2347 g:double_loaded = 'yes'
2348 END
2349 writefile(lines, 'Xdir/autoload/double.vim')
2350
2351 lines =<< trim END
2352 vim9script noclear
2353 if exists('g:script_loaded')
2354 finish
2355 endif
2356 g:script_loaded = true
2357
2358 import autoload 'double.vim'
2359 nnoremap <F3> <ScriptCmd>g:result = double.Func()<CR>
2360 END
2361 g:double_loaded = 'no'
2362 writefile(lines, 'Xloaddouble')
2363 source Xloaddouble
2364 assert_equal('no', g:double_loaded)
2365 assert_equal(true, g:script_loaded)
2366 source Xloaddouble
2367 feedkeys("\<F3>", 'xt')
2368 assert_equal('called', g:result)
2369 assert_equal('yes', g:double_loaded)
2370
2371 delete('Xloaddouble')
2372 unlet g:double_loaded
2373 unlet g:script_loaded
2374 unlet g:result
2375 delete('Xdir', 'rf')
2376 &rtp = save_rtp
2377enddef
2378
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002379def Test_vim9script_autoload_duplicate()
2380 mkdir('Xdir/autoload', 'p')
2381
2382 var lines =<< trim END
2383 vim9script
2384
2385 export def Func()
2386 enddef
2387
2388 def Func()
2389 enddef
2390 END
2391 writefile(lines, 'Xdir/autoload/dupfunc.vim')
2392 assert_fails('source Xdir/autoload/dupfunc.vim', 'E1073:')
2393
2394 lines =<< trim END
2395 vim9script
2396
2397 def Func()
2398 enddef
2399
2400 export def Func()
2401 enddef
2402 END
2403 writefile(lines, 'Xdir/autoload/dup2func.vim')
2404 assert_fails('source Xdir/autoload/dup2func.vim', 'E1073:')
2405
2406 lines =<< trim END
2407 vim9script
2408
2409 def Func()
2410 enddef
2411
2412 export var Func = 'asdf'
2413 END
2414 writefile(lines, 'Xdir/autoload/dup3func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002415 assert_fails('source Xdir/autoload/dup3func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002416
2417 lines =<< trim END
2418 vim9script
2419
2420 export var Func = 'asdf'
2421
2422 def Func()
2423 enddef
2424 END
2425 writefile(lines, 'Xdir/autoload/dup4func.vim')
2426 assert_fails('source Xdir/autoload/dup4func.vim', 'E707:')
2427
2428 lines =<< trim END
2429 vim9script
2430
2431 var Func = 'asdf'
2432
2433 export def Func()
2434 enddef
2435 END
2436 writefile(lines, 'Xdir/autoload/dup5func.vim')
2437 assert_fails('source Xdir/autoload/dup5func.vim', 'E707:')
2438
2439 lines =<< trim END
2440 vim9script
2441
2442 export def Func()
2443 enddef
2444
2445 var Func = 'asdf'
2446 END
2447 writefile(lines, 'Xdir/autoload/dup6func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002448 assert_fails('source Xdir/autoload/dup6func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002449
2450 delete('Xdir', 'rf')
2451enddef
2452
Bram Moolenaar2017d6f2022-01-20 19:38:46 +00002453def Test_autoload_missing_function_name()
2454 mkdir('Xdir/autoload', 'p')
2455
2456 var lines =<< trim END
2457 vim9script
2458
2459 def loadme#()
2460 enddef
2461 END
2462 writefile(lines, 'Xdir/autoload/loadme.vim')
2463 assert_fails('source Xdir/autoload/loadme.vim', 'E129:')
2464
2465 delete('Xdir', 'rf')
2466enddef
2467
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002468def Test_autoload_name_wrong()
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002469 var lines =<< trim END
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002470 def Xscriptname#Func()
2471 enddef
2472 END
2473 writefile(lines, 'Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002474 v9.CheckScriptFailure(lines, 'E746:')
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00002475 delete('Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002476
2477 mkdir('Xdir/autoload', 'p')
2478 lines =<< trim END
2479 vim9script
2480 def somescript#Func()
2481 enddef
2482 END
2483 writefile(lines, 'Xdir/autoload/somescript.vim')
2484 assert_fails('source Xdir/autoload/somescript.vim', 'E1263:')
2485
2486 delete('Xdir', 'rf')
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002487enddef
2488
Bram Moolenaard041f422022-01-12 19:54:00 +00002489def Test_import_autoload_postponed()
2490 mkdir('Xdir/autoload', 'p')
2491 var save_rtp = &rtp
2492 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2493
2494 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002495 vim9script
Bram Moolenaard041f422022-01-12 19:54:00 +00002496
2497 g:loaded_postponed = 'true'
2498 export var variable = 'bla'
2499 export def Function(): string
2500 return 'bla'
2501 enddef
2502 END
2503 writefile(lines, 'Xdir/autoload/postponed.vim')
2504
2505 lines =<< trim END
2506 vim9script
2507
2508 import autoload 'postponed.vim'
2509 def Tryit()
2510 echo postponed.variable
2511 echo postponed.Function()
2512 enddef
2513 defcompile
2514 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002515 v9.CheckScriptSuccess(lines)
Bram Moolenaard041f422022-01-12 19:54:00 +00002516 assert_false(exists('g:loaded_postponed'))
Bram Moolenaar62aec932022-01-29 21:45:34 +00002517 v9.CheckScriptSuccess(lines + ['Tryit()'])
Bram Moolenaard041f422022-01-12 19:54:00 +00002518 assert_equal('true', g:loaded_postponed)
2519
2520 unlet g:loaded_postponed
2521 delete('Xdir', 'rf')
2522 &rtp = save_rtp
2523enddef
2524
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002525def Test_import_autoload_override()
2526 mkdir('Xdir/autoload', 'p')
2527 var save_rtp = &rtp
2528 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2529 test_override('autoload', 1)
2530
2531 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002532 vim9script
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002533
2534 g:loaded_override = 'true'
2535 export var variable = 'bla'
2536 export def Function(): string
2537 return 'bla'
2538 enddef
2539 END
2540 writefile(lines, 'Xdir/autoload/override.vim')
2541
2542 lines =<< trim END
2543 vim9script
2544
2545 import autoload 'override.vim'
2546 assert_equal('true', g:loaded_override)
2547
2548 def Tryit()
2549 echo override.doesNotExist
2550 enddef
2551 defcompile
2552 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002553 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1)
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002554
2555 test_override('autoload', 0)
2556 unlet g:loaded_override
2557 delete('Xdir', 'rf')
2558 &rtp = save_rtp
2559enddef
2560
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002561def Test_autoload_mapping()
2562 mkdir('Xdir/autoload', 'p')
2563 var save_rtp = &rtp
2564 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2565
2566 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002567 vim9script
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002568
2569 g:toggle_loaded = 'yes'
2570
2571 export def Toggle(): string
2572 return ":g:toggle_called = 'yes'\<CR>"
2573 enddef
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002574 export def Doit()
2575 g:doit_called = 'yes'
2576 enddef
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002577 END
2578 writefile(lines, 'Xdir/autoload/toggle.vim')
2579
2580 lines =<< trim END
2581 vim9script
2582
2583 import autoload 'toggle.vim'
2584
2585 nnoremap <silent> <expr> tt toggle.Toggle()
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002586 nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR>
2587 nnoremap <silent> yy <Cmd>toggle.Doit()<CR>
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002588 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002589 v9.CheckScriptSuccess(lines)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002590 assert_false(exists("g:toggle_loaded"))
2591 assert_false(exists("g:toggle_called"))
Bram Moolenaar6079da72022-01-18 14:16:59 +00002592 assert_match('\d A: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002593
2594 feedkeys("tt", 'xt')
2595 assert_equal('yes', g:toggle_loaded)
2596 assert_equal('yes', g:toggle_called)
Bram Moolenaar6079da72022-01-18 14:16:59 +00002597 assert_match('\d: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002598
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002599 feedkeys("xx", 'xt')
2600 assert_equal('yes', g:doit_called)
2601
2602 assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle')
2603
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002604 nunmap tt
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002605 nunmap xx
2606 nunmap yy
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002607 unlet g:toggle_loaded
2608 unlet g:toggle_called
2609 delete('Xdir', 'rf')
2610 &rtp = save_rtp
2611enddef
2612
Bram Moolenaar160aa862022-01-10 21:29:57 +00002613def Test_vim9script_autoload_fails()
2614 var lines =<< trim END
2615 vim9script autoload
2616 var n = 0
2617 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002618 v9.CheckScriptFailure(lines, 'E475: Invalid argument: autoload')
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002619
2620 lines =<< trim END
2621 vim9script noclear noclear
2622 var n = 0
2623 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002624 v9.CheckScriptFailure(lines, 'E983: Duplicate argument: noclear')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002625enddef
2626
2627def Test_import_autoload_fails()
2628 var lines =<< trim END
2629 vim9script
2630 import autoload autoload 'prefixed.vim'
2631 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002632 v9.CheckScriptFailure(lines, 'E121: Undefined variable: autoload')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002633
2634 lines =<< trim END
2635 vim9script
Bram Moolenaar1836d612022-01-18 13:14:47 +00002636 import autoload './doesNotExist.vim'
Bram Moolenaar160aa862022-01-10 21:29:57 +00002637 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002638 v9.CheckScriptFailure(lines, 'E282:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002639
2640 lines =<< trim END
2641 vim9script
2642 import autoload '/dir/doesNotExist.vim'
2643 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002644 v9.CheckScriptFailure(lines, 'E282:', 2)
2645
2646 lines =<< trim END
2647 vim9script
2648 import autoload '../testdir'
2649 END
2650 v9.CheckScriptFailure(lines, 'E17:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002651
2652 lines =<< trim END
2653 vim9script
2654 import autoload 'doesNotExist.vim'
2655 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002656 v9.CheckScriptFailure(lines, 'E1053: Could not import "doesNotExist.vim"')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002657enddef
2658
2659" test disassembling an auto-loaded function starting with "debug"
2660def Test_vim9_autoload_disass()
2661 mkdir('Xdir/autoload', 'p')
2662 var save_rtp = &rtp
2663 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2664
2665 var lines =<< trim END
2666 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002667 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002668 return 'debug'
2669 enddef
2670 END
2671 writefile(lines, 'Xdir/autoload/debugit.vim')
2672
2673 lines =<< trim END
2674 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002675 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002676 return 'profile'
2677 enddef
2678 END
2679 writefile(lines, 'Xdir/autoload/profileit.vim')
2680
2681 lines =<< trim END
2682 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002683 assert_equal('debug', debugit#Test())
2684 disass debugit#Test
2685 assert_equal('profile', profileit#Test())
2686 disass profileit#Test
Bram Moolenaar160aa862022-01-10 21:29:57 +00002687 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002688 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002689
2690 delete('Xdir', 'rf')
2691 &rtp = save_rtp
2692enddef
2693
2694" test using a vim9script that is auto-loaded from an autocmd
2695def Test_vim9_aucmd_autoload()
2696 var lines =<< trim END
2697 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002698 export def Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002699 echomsg getreg('"')
2700 enddef
2701 END
2702
2703 mkdir('Xdir/autoload', 'p')
2704 writefile(lines, 'Xdir/autoload/foo.vim')
2705 var save_rtp = &rtp
2706 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2707 augroup test
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002708 autocmd TextYankPost * call foo#Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002709 augroup END
2710
2711 normal Y
2712
2713 augroup test
2714 autocmd!
2715 augroup END
2716 delete('Xdir', 'rf')
2717 &rtp = save_rtp
2718enddef
2719
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002720" test using a autoloaded file that is case sensitive
2721def Test_vim9_autoload_case_sensitive()
2722 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002723 vim9script
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002724 export def CaseSensitive(): string
2725 return 'done'
2726 enddef
2727 END
2728
2729 mkdir('Xdir/autoload', 'p')
2730 writefile(lines, 'Xdir/autoload/CaseSensitive.vim')
2731 var save_rtp = &rtp
2732 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2733
2734 lines =<< trim END
2735 vim9script
2736 import autoload 'CaseSensitive.vim'
2737 assert_equal('done', CaseSensitive.CaseSensitive())
2738 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002739 v9.CheckScriptSuccess(lines)
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002740
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002741 if !has('fname_case')
2742 lines =<< trim END
2743 vim9script
2744 import autoload 'CaseSensitive.vim'
2745 import autoload 'casesensitive.vim'
2746 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002747 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002748 endif
2749
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002750 delete('Xdir', 'rf')
2751 &rtp = save_rtp
2752enddef
2753
Bram Moolenaar160aa862022-01-10 21:29:57 +00002754" This was causing a crash because suppress_errthrow wasn't reset.
2755def Test_vim9_autoload_error()
2756 var lines =<< trim END
2757 vim9script
2758 def crash#func()
2759 try
2760 for x in List()
2761 endfor
2762 catch
2763 endtry
2764 g:ok = true
2765 enddef
2766 fu List()
2767 invalid
2768 endfu
2769 try
2770 alsoinvalid
2771 catch /wontmatch/
2772 endtry
2773 END
2774 call mkdir('Xruntime/autoload', 'p')
2775 call writefile(lines, 'Xruntime/autoload/crash.vim')
2776
2777 # run in a separate Vim to avoid the side effects of assert_fails()
2778 lines =<< trim END
2779 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
2780 call crash#func()
2781 call writefile(['ok'], 'Xdidit')
2782 qall!
2783 END
2784 writefile(lines, 'Xscript')
Bram Moolenaar62aec932022-01-29 21:45:34 +00002785 g:RunVim([], [], '-S Xscript')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002786 assert_equal(['ok'], readfile('Xdidit'))
2787
2788 delete('Xdidit')
2789 delete('Xscript')
2790 delete('Xruntime', 'rf')
2791
2792 lines =<< trim END
2793 vim9script
2794 var foo#bar = 'asdf'
2795 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002796 v9.CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002797enddef
2798
Bram Moolenaard8448622022-01-07 21:39:52 +00002799
2800" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker