blob: 94cb76033e1728a85d7281fc818ec02bef3802ed [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
672def Test_use_autoload_import_in_mapping()
673 var lines =<< trim END
674 vim9script
675 export def Func()
676 g:result = 42
677 enddef
678 END
679 writefile(lines, 'XautoloadExport.vim')
680 lines =<< trim END
681 vim9script
682 import autoload './XautoloadExport.vim' as some
683 nnoremap <F3> :call <SID>some.Func()<CR>
684 END
685 writefile(lines, 'Xmapscript.vim')
686
687 source Xmapscript.vim
688 assert_match('\d\+ A: .*XautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
689 feedkeys("\<F3>", "xt")
690 assert_equal(42, g:result)
691
692 unlet g:result
693 delete('XautoloadExport.vim')
694 delete('Xmapscript.vim')
695 nunmap <F3>
Bram Moolenaard8448622022-01-07 21:39:52 +0000696enddef
697
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000698def Test_use_import_in_command_completion()
Bram Moolenaar15d16352022-01-17 20:09:08 +0000699 var lines =<< trim END
700 vim9script
701 export def Complete(..._): list<string>
702 return ['abcd']
703 enddef
704 END
705 writefile(lines, 'Xscript.vim')
706
707 lines =<< trim END
708 vim9script
709 import './Xscript.vim'
710
711 command -nargs=1 -complete=customlist,Xscript.Complete Cmd echo 'ok'
712 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
713 assert_equal('#Cmd abcd', @:)
714 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000715 v9.CheckScriptSuccess(lines)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000716
717 delcommand Cmd
718 delete('Xscript.vim')
719enddef
720
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000721def Test_use_autoload_import_in_insert_completion()
722 mkdir('Xdir/autoload', 'p')
723 var save_rtp = &rtp
724 exe 'set rtp^=' .. getcwd() .. '/Xdir'
725
726 var lines =<< trim END
727 vim9script
728 export def ThesaurusFunc(findbase: bool, _): any
729 if findbase
730 return 1
731 endif
732 return [
733 'check',
734 'experiment',
735 'test',
736 'verification'
737 ]
738 enddef
739 g:completion_loaded = 'yes'
740 END
741 writefile(lines, 'Xdir/autoload/completion.vim')
742
743 new
744 lines =<< trim END
745 vim9script
746 g:completion_loaded = 'no'
747 import autoload 'completion.vim'
748 set thesaurusfunc=completion.ThesaurusFunc
749 assert_equal('no', g:completion_loaded)
750 feedkeys("i\<C-X>\<C-T>\<C-N>\<Esc>", 'xt')
751 assert_equal('experiment', getline(1))
752 assert_equal('yes', g:completion_loaded)
753 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000754 v9.CheckScriptSuccess(lines)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000755
756 set thesaurusfunc=
757 bwipe!
758 delete('Xdir', 'rf')
759 &rtp = save_rtp
760enddef
761
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000762def Test_use_autoload_import_partial_in_opfunc()
763 mkdir('Xdir/autoload', 'p')
764 var save_rtp = &rtp
765 exe 'set rtp^=' .. getcwd() .. '/Xdir'
766
767 var lines =<< trim END
768 vim9script
769 export def Opfunc(..._)
770 g:opfunc_called = 'yes'
771 enddef
772 END
773 writefile(lines, 'Xdir/autoload/opfunc.vim')
774
775 new
776 lines =<< trim END
777 vim9script
778 import autoload 'opfunc.vim'
779 nnoremap <expr> <F3> TheFunc()
780 def TheFunc(): string
781 &operatorfunc = function('opfunc.Opfunc', [0])
782 return 'g@'
783 enddef
784 feedkeys("\<F3>l", 'xt')
785 assert_equal('yes', g:opfunc_called)
786 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000787 v9.CheckScriptSuccess(lines)
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000788
789 set opfunc=
790 bwipe!
791 delete('Xdir', 'rf')
Bram Moolenaar06b77222022-01-25 15:51:56 +0000792 nunmap <F3>
793 &rtp = save_rtp
794enddef
795
796def Test_set_opfunc_to_autoload_func_directly()
797 mkdir('Xdir/autoload', 'p')
798 var save_rtp = &rtp
799 exe 'set rtp^=' .. getcwd() .. '/Xdir'
800
801 var lines =<< trim END
802 vim9script
803 export def Opfunc(..._)
804 g:opfunc_called = 'yes'
805 enddef
806 END
807 writefile(lines, 'Xdir/autoload/opfunc.vim')
808
809 new
810 lines =<< trim END
811 vim9script
812 import autoload 'opfunc.vim'
813 nnoremap <expr> <F3> TheFunc()
814 def TheFunc(): string
815 &operatorfunc = opfunc.Opfunc
816 return 'g@'
817 enddef
818 feedkeys("\<F3>l", 'xt')
819 assert_equal('yes', g:opfunc_called)
820 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000821 v9.CheckScriptSuccess(lines)
Bram Moolenaar06b77222022-01-25 15:51:56 +0000822
823 set opfunc=
824 bwipe!
825 delete('Xdir', 'rf')
826 nunmap <F3>
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000827 &rtp = save_rtp
828enddef
829
Bram Moolenaare70dd112022-01-21 16:31:11 +0000830def Test_use_autoload_import_in_fold_expression()
831 mkdir('Xdir/autoload', 'p')
832 var save_rtp = &rtp
833 exe 'set rtp^=' .. getcwd() .. '/Xdir'
834
835 var lines =<< trim END
836 vim9script
837 export def Expr(): string
838 return getline(v:lnum) =~ '^#' ? '>1' : '1'
839 enddef
Bram Moolenaar9530b582022-01-22 13:39:08 +0000840 export def Text(): string
841 return 'fold text'
842 enddef
Bram Moolenaare70dd112022-01-21 16:31:11 +0000843 g:fold_loaded = 'yes'
844 END
845 writefile(lines, 'Xdir/autoload/fold.vim')
846
847 lines =<< trim END
848 vim9script
849 import autoload 'fold.vim'
850 &foldexpr = 'fold.Expr()'
Bram Moolenaar9530b582022-01-22 13:39:08 +0000851 &foldtext = 'fold.Text()'
Bram Moolenaare70dd112022-01-21 16:31:11 +0000852 &foldmethod = 'expr'
853 &debug = 'throw'
854 END
855 new
856 setline(1, ['# one', 'text', '# two', 'text'])
857 g:fold_loaded = 'no'
Bram Moolenaar62aec932022-01-29 21:45:34 +0000858 v9.CheckScriptSuccess(lines)
Bram Moolenaare70dd112022-01-21 16:31:11 +0000859 assert_equal('no', g:fold_loaded)
860 redraw
861 assert_equal('yes', g:fold_loaded)
862
863 # Check that script context of 'foldexpr' is copied to another buffer.
864 edit! otherfile
865 redraw
866
Bram Moolenaar9530b582022-01-22 13:39:08 +0000867 set foldexpr= foldtext& foldmethod& debug=
Bram Moolenaare70dd112022-01-21 16:31:11 +0000868 bwipe!
869 delete('Xdir', 'rf')
870 &rtp = save_rtp
871enddef
872
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100873def Test_autoload_import_relative()
874 var lines =<< trim END
875 vim9script
876
877 g:loaded = 'yes'
878 export def RelFunc(): string
879 return 'relfunc'
880 enddef
881 def NotExported()
882 echo 'not'
883 enddef
884
885 export var someText = 'some text'
886 var notexp = 'bad'
887 END
888 writefile(lines, 'XimportRel.vim')
889 writefile(lines, 'XimportRel2.vim')
890 writefile(lines, 'XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +0100891 writefile(lines, 'XimportRel4.vim')
892 writefile(lines, 'XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100893
894 lines =<< trim END
895 vim9script
896 g:loaded = 'no'
897 import autoload './XimportRel.vim'
898 assert_equal('no', g:loaded)
899
900 def AFunc(): string
901 var res = ''
902 res ..= XimportRel.RelFunc()
903 res ..= '/'
904 res ..= XimportRel.someText
905 XimportRel.someText = 'from AFunc'
906 return res
907 enddef
908 # script not loaded when compiling
909 defcompile
910 assert_equal('no', g:loaded)
911
912 assert_equal('relfunc/some text', AFunc())
913 assert_equal('yes', g:loaded)
914 unlet g:loaded
915
916 assert_equal('from AFunc', XimportRel.someText)
917 XimportRel.someText = 'from script'
918 assert_equal('from script', XimportRel.someText)
919 END
920 v9.CheckScriptSuccess(lines)
921
922 lines =<< trim END
923 vim9script
924 import autoload './XimportRel.vim'
925 echo XimportRel.NotExported()
926 END
927 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 3)
928
929 lines =<< trim END
930 vim9script
931 import autoload './XimportRel.vim'
932 echo XimportRel.notexp
933 END
934 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
935
936 lines =<< trim END
937 vim9script
938 import autoload './XimportRel.vim'
939 XimportRel.notexp = 'bad'
940 END
941 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
942
943 lines =<< trim END
944 vim9script
945 import autoload './XimportRel.vim'
946 def Func()
947 echo XimportRel.NotExported()
948 enddef
949 Func()
950 END
951 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 1)
952
953 lines =<< trim END
954 vim9script
955 import autoload './XimportRel.vim'
956 def Func()
957 echo XimportRel.notexp
958 enddef
959 Func()
960 END
961 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
962
Bram Moolenaar10611952022-04-03 21:11:34 +0100963 # Same, script not imported before
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100964 lines =<< trim END
965 vim9script
Bram Moolenaar10611952022-04-03 21:11:34 +0100966 import autoload './XimportRel4.vim'
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100967 def Func()
Bram Moolenaar10611952022-04-03 21:11:34 +0100968 echo XimportRel4.notexp
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100969 enddef
970 Func()
971 END
972 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
973
Bram Moolenaar10611952022-04-03 21:11:34 +0100974 # does not fail if the script wasn't loaded yet and only compiling
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100975 g:loaded = 'no'
976 lines =<< trim END
977 vim9script
978 import autoload './XimportRel2.vim'
979 def Func()
980 echo XimportRel2.notexp
981 enddef
982 defcompile
983 END
984 v9.CheckScriptSuccess(lines)
985 assert_equal('no', g:loaded)
986
Bram Moolenaar10611952022-04-03 21:11:34 +0100987 lines =<< trim END
988 vim9script
989 import autoload './XimportRel.vim'
990 def Func()
991 XimportRel.notexp = 'bad'
992 enddef
993 Func()
994 END
995 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
996
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100997 # fails with a not loaded import
998 lines =<< trim END
999 vim9script
1000 import autoload './XimportRel3.vim'
1001 def Func()
1002 XimportRel3.notexp = 'bad'
1003 enddef
1004 Func()
1005 END
1006 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1007 assert_equal('yes', g:loaded)
1008 unlet g:loaded
1009
Bram Moolenaar10611952022-04-03 21:11:34 +01001010 lines =<< trim END
1011 vim9script
1012 import autoload './XimportRel5.vim'
1013 def Func()
1014 XimportRel5.nosuchvar = 'bad'
1015 enddef
1016 Func()
1017 END
1018 v9.CheckScriptFailure(lines, 'E121: Undefined variable: nosuchvar', 1)
1019 unlet g:loaded
1020
1021 # nasty: delete script after compiling function
1022 writefile(['vim9script'], 'XimportRelDel.vim')
1023 lines =<< trim END
1024 vim9script
1025
1026 import autoload './XimportRelDel.vim'
1027 def DoIt()
1028 echo XimportRelDel.var
1029 enddef
1030 defcompile
1031 delete('XimportRelDel.vim')
1032 DoIt()
1033 END
Bram Moolenaar242c1522022-04-03 21:52:51 +01001034 v9.CheckScriptFailure(lines, 'E484:')
Bram Moolenaar10611952022-04-03 21:11:34 +01001035
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001036 delete('XimportRel.vim')
1037 delete('XimportRel2.vim')
1038 delete('XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +01001039 delete('XimportRel4.vim')
1040 delete('XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001041enddef
1042
Bram Moolenaarccbfd482022-03-31 16:18:23 +01001043def Test_autoload_import_relative_autoload_dir()
1044 mkdir('autoload', 'p')
1045 var lines =<< trim END
1046 vim9script
1047 export def Bar()
1048 g:called_bar = 'yes'
1049 enddef
1050 END
1051 writefile(lines, 'autoload/script.vim')
1052
1053 lines =<< trim END
1054 vim9script
1055 import autoload './autoload/script.vim'
1056 def Foo()
1057 script.Bar()
1058 enddef
1059 Foo()
1060 assert_equal('yes', g:called_bar)
1061 END
1062 v9.CheckScriptSuccess(lines)
1063
1064 unlet g:called_bar
1065 delete('autoload', 'rf')
1066enddef
1067
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001068func Test_import_in_diffexpr()
1069 CheckExecutable diff
1070
1071 call Run_Test_import_in_diffexpr()
1072endfunc
1073
1074def Run_Test_import_in_diffexpr()
1075 var lines =<< trim END
1076 vim9script
1077
1078 export def DiffExpr()
1079 # Prepend some text to check diff type detection
1080 writefile(['warning', ' message'], v:fname_out)
1081 silent exe '!diff ' .. v:fname_in .. ' '
1082 .. v:fname_new .. '>>' .. v:fname_out
1083 enddef
1084 END
1085 writefile(lines, 'Xdiffexpr')
1086
1087 lines =<< trim END
1088 vim9script
1089 import './Xdiffexpr' as diff
1090
1091 set diffexpr=diff.DiffExpr()
1092 set diffopt=foldcolumn:0
1093 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001094 v9.CheckScriptSuccess(lines)
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001095
1096 enew!
1097 call setline(1, ['one', 'two', 'three'])
1098 diffthis
1099
1100 botright vert new
1101 call setline(1, ['one', 'two', 'three.'])
1102 diffthis
1103 # we only check if this does not cause errors
1104 redraw
1105
1106 diffoff!
1107 bwipe!
1108 bwipe!
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001109 delete('Xdiffexpr')
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001110enddef
1111
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001112def Test_import_in_patchexpr()
1113 var lines =<< trim END
1114 vim9script
1115 export def TPatch()
1116 call writefile(['output file'], v:fname_out)
1117 enddef
1118 END
1119 writefile(lines, 'Xpatchexpr')
1120
1121 lines =<< trim END
1122 vim9script
1123 import './Xpatchexpr' as patch
1124 set patchexpr=patch.TPatch()
1125 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001126 v9.CheckScriptSuccess(lines)
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001127
1128 call writefile(['input file'], 'Xinput')
1129 call writefile(['diff file'], 'Xdiff')
1130 :%bwipe!
1131 edit Xinput
1132 diffpatch Xdiff
1133 call assert_equal('output file', getline(1))
1134
1135 call delete('Xinput')
1136 call delete('Xdiff')
1137 call delete('Xpatchexpr')
1138 set patchexpr&
1139 :%bwipe!
1140enddef
1141
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001142def Test_import_in_formatexpr()
1143 var lines =<< trim END
1144 vim9script
1145 export def MyFormatExpr(): number
1146 g:did_format = 'yes'
1147 return 0
1148 enddef
1149 END
1150 writefile(lines, 'Xformatter')
1151
1152 lines =<< trim END
1153 vim9script
1154 import './Xformatter' as format
1155 set formatexpr=format.MyFormatExpr()
1156 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001157 v9.CheckScriptSuccess(lines)
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001158
1159 new
1160 setline(1, ['a', 'b', 'c'])
1161 normal gqG
1162 assert_equal('yes', g:did_format)
1163
1164 bwipe!
1165 delete('Xformatter')
1166 unlet g:did_format
1167 set formatexpr=
1168enddef
1169
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001170def Test_import_in_includeexpr()
1171 writefile(['found it'], 'Xthisfile')
1172 new
1173
1174 var lines =<< trim END
1175 vim9script
1176 export def DoSub(): string
1177 return substitute(v:fname, 'that', 'this', '')
1178 enddef
1179 END
1180 writefile(lines, 'Xinclude.vim')
1181
1182 lines =<< trim END
1183 vim9script
1184 import './Xinclude.vim'
1185 set includeexpr=Xinclude.DoSub()
1186 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001187 v9.CheckScriptSuccess(lines)
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001188
1189 setline(1, ['Xthatfile'])
1190 exe "normal \<C-W>f"
1191 assert_equal('Xthisfile', expand('%'))
1192
1193 bwipe!
1194 bwipe!
1195 set includeexpr=
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001196 delete('Xinclude.vim')
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001197 delete('Xthisfile')
1198enddef
1199
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001200def Test_import_in_indentexpr()
1201 var lines =<< trim END
1202 vim9script
1203 export def GetIndent(): number
1204 return 5
1205 enddef
1206 END
1207 writefile(lines, 'Xindenter')
1208
1209 lines =<< trim END
1210 vim9script
1211 import './Xindenter' as indent
1212 set indentexpr=indent.GetIndent()
1213 set debug=throw
1214 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001215 v9.CheckScriptSuccess(lines)
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001216
1217 new
1218 setline(1, 'hello')
1219 normal ==
1220 assert_equal(' hello', getline(1))
1221
1222 bwipe!
1223 set indentexpr= debug=
1224 delete('Xindenter')
1225enddef
1226
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001227func Test_import_in_printexpr()
1228 CheckFeature postscript
1229 call Run_Test_import_in_printexpr()
1230endfunc
1231
1232def Run_Test_import_in_printexpr()
1233 var lines =<< trim END
1234 vim9script
1235 export def PrintFile(): bool
1236 g:printed = 'yes'
1237 delete('v:fname_in')
1238 return false
1239 enddef
1240 END
1241 writefile(lines, 'Xprint.vim')
1242
1243 lines =<< trim END
1244 vim9script
1245 import './Xprint.vim'
1246 set printexpr=Xprint.PrintFile()
1247 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001248 v9.CheckScriptSuccess(lines)
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001249
1250 help
1251 hardcopy dummy args
1252 assert_equal('yes', g:printed)
1253
1254 delete('Xprint.vim')
1255 set printexpr=
1256enddef
1257
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001258def Test_import_in_charconvert()
1259 var lines =<< trim END
1260 vim9script
1261 export def MakeUpper(): bool
1262 var data = readfile(v:fname_in)
1263 map(data, 'toupper(v:val)')
1264 writefile(data, v:fname_out)
1265 return false # success
1266 enddef
1267 END
1268 writefile(lines, 'Xconvert.vim')
1269
1270 lines =<< trim END
1271 vim9script
1272 import './Xconvert.vim' as conv
1273 set charconvert=conv.MakeUpper()
1274 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001275 v9.CheckScriptSuccess(lines)
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001276
1277 writefile(['one', 'two'], 'Xfile')
1278 new Xfile
1279 write ++enc=ucase Xfile1
1280 assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1281
1282 delete('Xfile')
1283 delete('Xfile1')
1284 delete('Xconvert.vim')
1285 bwipe!
1286 set charconvert&
1287enddef
1288
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001289func Test_import_in_spellsuggest_expr()
1290 CheckFeature spell
1291 call Run_Test_import_in_spellsuggest_expr()
1292endfunc
1293
1294def Run_Test_import_in_spellsuggest_expr()
1295 var lines =<< trim END
1296 vim9script
1297 export def MySuggest(): list<any>
1298 return [['Fox', 8], ['Fop', 9]]
1299 enddef
1300 END
1301 writefile(lines, 'Xsuggest.vim')
1302
1303 lines =<< trim END
1304 vim9script
1305 import './Xsuggest.vim' as sugg
1306 set spell spellsuggest=expr:sugg.MySuggest()
1307 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001308 v9.CheckScriptSuccess(lines)
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001309
1310 set verbose=1 # report errors
1311 call assert_equal(['Fox', 'Fop'], spellsuggest('Fo', 2))
1312
1313 delete('Xsuggest.vim')
1314 set nospell spellsuggest& verbose=0
1315enddef
1316
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001317def Test_export_shadows_global_function()
1318 mkdir('Xdir/autoload', 'p')
1319 var save_rtp = &rtp
1320 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1321
1322 var lines =<< trim END
1323 vim9script
1324 export def Shadow(): string
1325 return 'Shadow()'
1326 enddef
1327 END
1328 writefile(lines, 'Xdir/autoload/shadow.vim')
1329
1330 lines =<< trim END
1331 vim9script
1332
1333 def g:Shadow(): string
1334 return 'global'
1335 enddef
1336
1337 import autoload 'shadow.vim'
1338 assert_equal('Shadow()', shadow.Shadow())
1339 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001340 v9.CheckScriptSuccess(lines)
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001341
1342 delfunc g:Shadow
1343 bwipe!
1344 delete('Xdir', 'rf')
1345 &rtp = save_rtp
1346enddef
1347
Bram Moolenaard8448622022-01-07 21:39:52 +00001348def Test_export_fails()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001349 v9.CheckScriptFailure(['export var some = 123'], 'E1042:')
1350 v9.CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
1351 v9.CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001352
1353 assert_fails('export something', 'E1043:')
1354enddef
1355
1356func Test_import_fails_without_script()
1357 CheckRunVimInTerminal
1358
1359 " call indirectly to avoid compilation error for missing functions
1360 call Run_Test_import_fails_on_command_line()
1361endfunc
1362
1363def Run_Test_import_fails_on_command_line()
1364 var export =<< trim END
1365 vim9script
1366 export def Foo(): number
1367 return 0
1368 enddef
1369 END
1370 writefile(export, 'XexportCmd.vim')
1371
Bram Moolenaar62aec932022-01-29 21:45:34 +00001372 var buf = g:RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaard8448622022-01-07 21:39:52 +00001373 rows: 6, wait_for_ruler: 0})
Bram Moolenaar62aec932022-01-29 21:45:34 +00001374 g:WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaard8448622022-01-07 21:39:52 +00001375
1376 delete('XexportCmd.vim')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001377 g:StopVimInTerminal(buf)
Bram Moolenaard8448622022-01-07 21:39:52 +00001378enddef
1379
1380def Test_vim9_reload_noclear()
1381 var lines =<< trim END
1382 vim9script
1383 export var exported = 'thexport'
1384
1385 export def TheFunc(x = 0)
1386 enddef
1387 END
1388 writefile(lines, 'XExportReload')
1389 lines =<< trim END
1390 vim9script noclear
1391 g:loadCount += 1
Bram Moolenaara749a422022-02-12 19:52:25 +00001392 var reloaded = 'init'
Bram Moolenaard8448622022-01-07 21:39:52 +00001393 import './XExportReload' as exp
1394
1395 def Again(): string
1396 return 'again'
1397 enddef
1398
1399 exp.TheFunc()
1400
Bram Moolenaara749a422022-02-12 19:52:25 +00001401 if exists('loaded') | finish | endif
1402 var loaded = true
Bram Moolenaard8448622022-01-07 21:39:52 +00001403
Bram Moolenaara749a422022-02-12 19:52:25 +00001404 var notReloaded = 'yes'
1405 reloaded = 'first'
Bram Moolenaard8448622022-01-07 21:39:52 +00001406 def g:Values(): list<string>
Bram Moolenaara749a422022-02-12 19:52:25 +00001407 return [reloaded, notReloaded, Again(), Once(), exp.exported]
Bram Moolenaard8448622022-01-07 21:39:52 +00001408 enddef
1409
1410 def Once(): string
1411 return 'once'
1412 enddef
1413 END
1414 writefile(lines, 'XReloaded')
1415 g:loadCount = 0
1416 source XReloaded
1417 assert_equal(1, g:loadCount)
1418 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
1419 source XReloaded
1420 assert_equal(2, g:loadCount)
1421 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1422 source XReloaded
1423 assert_equal(3, g:loadCount)
1424 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1425
1426 delete('XReloaded')
1427 delete('XExportReload')
1428 delfunc g:Values
1429 unlet g:loadCount
1430
1431 lines =<< trim END
1432 vim9script
1433 def Inner()
1434 enddef
1435 END
1436 lines->writefile('XreloadScript.vim')
1437 source XreloadScript.vim
1438
1439 lines =<< trim END
1440 vim9script
1441 def Outer()
1442 def Inner()
1443 enddef
1444 enddef
1445 defcompile
1446 END
1447 lines->writefile('XreloadScript.vim')
1448 source XreloadScript.vim
1449
1450 delete('XreloadScript.vim')
1451enddef
1452
Bram Moolenaarcd1cda22022-02-16 21:48:25 +00001453def Test_vim_reload_noclear_arg_count()
1454 var lines =<< trim END
1455 vim9script noclear
1456
1457 if !exists('g:didload')
1458 def Test(a: string, b: string)
1459 echo a b
1460 enddef
1461 def Call()
1462 Test('a', 'b')
1463 enddef
1464 else
1465 # redefine with one argument less
1466 def Test(a: string)
1467 echo a
1468 enddef
1469 endif
1470 Call()
1471 g:didload = 1
1472 END
1473 lines->writefile('XreloadScript_1.vim')
1474 source XreloadScript_1.vim
1475 assert_fails('source XreloadScript_1.vim', 'E1106: One argument too many')
1476 unlet g:didload
1477
1478 lines =<< trim END
1479 vim9script noclear
1480
1481 if !exists('g:didload')
1482 def Test(a: string, b: string, c: string)
1483 echo a b
1484 enddef
1485 def Call()
1486 Test('a', 'b', 'c')
1487 enddef
1488 else
1489 # redefine with one argument less
1490 def Test(a: string)
1491 echo a
1492 enddef
1493 endif
1494 Call()
1495 g:didload = 1
1496 END
1497 lines->writefile('XreloadScript_2.vim')
1498 source XreloadScript_2.vim
1499 assert_fails('source XreloadScript_2.vim', 'E1106: 2 arguments too many')
1500 unlet g:didload
1501
1502 lines =<< trim END
1503 vim9script noclear
1504
1505 if !exists('g:didload')
1506 def Test(a: string)
1507 echo a
1508 enddef
1509 def Call()
1510 Test('a')
1511 enddef
1512 else
1513 # redefine with one argument extra
1514 def Test(a: string, b: string)
1515 echo a b
1516 enddef
1517 endif
1518 Call()
1519 g:didload = 1
1520 END
1521 lines->writefile('XreloadScript_3.vim')
1522 source XreloadScript_3.vim
1523 assert_fails('source XreloadScript_3.vim', 'E1190: One argument too few')
1524 unlet g:didload
1525
1526 lines =<< trim END
1527 vim9script noclear
1528
1529 if !exists('g:didload')
1530 def Test(a: string)
1531 echo a
1532 enddef
1533 def Call()
1534 Test('a')
1535 enddef
1536 else
1537 # redefine with two arguments extra
1538 def Test(a: string, b: string, c: string)
1539 echo a b
1540 enddef
1541 endif
1542 Call()
1543 g:didload = 1
1544 END
1545 lines->writefile('XreloadScript_4.vim')
1546 source XreloadScript_4.vim
1547 assert_fails('source XreloadScript_4.vim', 'E1190: 2 arguments too few')
1548 unlet g:didload
1549
1550 delete('XreloadScript_1.vim')
1551 delete('XreloadScript_2.vim')
1552 delete('XreloadScript_3.vim')
1553 delete('XreloadScript_4.vim')
1554enddef
1555
1556def Test_vim9_reload_noclear_error()
1557 var 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 a compile error
1569 def Test(a: string)
1570 echo ax
1571 enddef
1572 endif
1573 Call()
1574 g:didload = 1
1575 END
1576 lines->writefile('XreloadScriptErr.vim')
1577 source XreloadScriptErr.vim
1578 assert_fails('source XreloadScriptErr.vim', 'E1001: Variable not found: ax')
1579
1580 unlet g:didload
1581 delete('XreloadScriptErr.vim')
1582enddef
1583
Bram Moolenaard8448622022-01-07 21:39:52 +00001584def Test_vim9_reload_import()
1585 var lines =<< trim END
1586 vim9script
1587 const var = ''
1588 var valone = 1234
1589 def MyFunc(arg: string)
1590 valone = 5678
1591 enddef
1592 END
1593 var morelines =<< trim END
1594 var valtwo = 222
1595 export def GetValtwo(): number
1596 return valtwo
1597 enddef
1598 END
1599 writefile(lines + morelines, 'Xreload.vim')
1600 source Xreload.vim
1601 source Xreload.vim
1602 source Xreload.vim
1603
1604 # cannot declare a var twice
1605 lines =<< trim END
1606 vim9script
1607 var valone = 1234
1608 var valone = 5678
1609 END
1610 writefile(lines, 'Xreload.vim')
1611 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
1612
1613 delete('Xreload.vim')
1614 delete('Ximport.vim')
1615enddef
1616
1617" if a script is reloaded with a script-local variable that changed its type, a
1618" compiled function using that variable must fail.
1619def Test_script_reload_change_type()
1620 var lines =<< trim END
1621 vim9script noclear
1622 var str = 'string'
1623 def g:GetStr(): string
1624 return str .. 'xxx'
1625 enddef
1626 END
1627 writefile(lines, 'Xreload.vim')
1628 source Xreload.vim
1629 echo g:GetStr()
1630
1631 lines =<< trim END
1632 vim9script noclear
1633 var str = 1234
1634 END
1635 writefile(lines, 'Xreload.vim')
1636 source Xreload.vim
1637 assert_fails('echo g:GetStr()', 'E1150:')
1638
1639 delfunc g:GetStr
1640 delete('Xreload.vim')
1641enddef
1642
1643" Define CallFunc so that the test can be compiled
1644command CallFunc echo 'nop'
1645
1646def Test_script_reload_from_function()
1647 var lines =<< trim END
1648 vim9script
1649
Bram Moolenaar10611952022-04-03 21:11:34 +01001650 if exists('g:loadedThis')
Bram Moolenaard8448622022-01-07 21:39:52 +00001651 finish
1652 endif
Bram Moolenaar10611952022-04-03 21:11:34 +01001653 g:loadedThis = 1
Bram Moolenaard8448622022-01-07 21:39:52 +00001654 delcommand CallFunc
1655 command CallFunc Func()
1656 def Func()
1657 so XreloadFunc.vim
1658 g:didTheFunc = 1
1659 enddef
1660 END
1661 writefile(lines, 'XreloadFunc.vim')
1662 source XreloadFunc.vim
1663 CallFunc
1664 assert_equal(1, g:didTheFunc)
1665
1666 delete('XreloadFunc.vim')
1667 delcommand CallFunc
Bram Moolenaar10611952022-04-03 21:11:34 +01001668 unlet g:loadedThis
Bram Moolenaard8448622022-01-07 21:39:52 +00001669 unlet g:didTheFunc
1670enddef
1671
1672def s:RetSome(): string
1673 return 'some'
1674enddef
1675
1676" Not exported function that is referenced needs to be accessed by the
1677" script-local name.
1678def Test_vim9_funcref()
1679 var sortlines =<< trim END
1680 vim9script
1681 def Compare(i1: number, i2: number): number
1682 return i2 - i1
1683 enddef
1684
1685 export def FastSort(): list<number>
1686 return range(5)->sort(Compare)
1687 enddef
1688
1689 export def GetString(arg: string): string
1690 return arg
1691 enddef
1692 END
1693 writefile(sortlines, 'Xsort.vim')
1694
1695 var lines =<< trim END
1696 vim9script
1697 import './Xsort.vim'
1698 def Test()
1699 g:result = Xsort.FastSort()
1700 enddef
1701 Test()
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001702 END
1703 writefile(lines, 'Xscript.vim')
1704 source Xscript.vim
1705 assert_equal([4, 3, 2, 1, 0], g:result)
1706 unlet g:result
Bram Moolenaard8448622022-01-07 21:39:52 +00001707
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001708 lines =<< trim END
1709 vim9script
Bram Moolenaard8448622022-01-07 21:39:52 +00001710 # using a function imported with "as"
1711 import './Xsort.vim' as anAlias
1712 assert_equal('yes', anAlias.GetString('yes'))
1713
1714 # using the function from a compiled function
1715 def TestMore(): string
1716 var s = s:anAlias.GetString('foo')
1717 return s .. anAlias.GetString('bar')
1718 enddef
1719 assert_equal('foobar', TestMore())
1720
1721 # error when using a function that isn't exported
1722 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
1723 END
1724 writefile(lines, 'Xscript.vim')
1725
Bram Moolenaard8448622022-01-07 21:39:52 +00001726 delete('Xsort.vim')
1727 delete('Xscript.vim')
1728
1729 var Funcref = function('s:RetSome')
1730 assert_equal('some', Funcref())
1731enddef
1732
1733" Check that when searching for "FilterFunc" it finds the import in the
1734" script where FastFilter() is called from, both as a string and as a direct
1735" function reference.
1736def Test_vim9_funcref_other_script()
1737 var filterLines =<< trim END
1738 vim9script
1739 export def FilterFunc(idx: number, val: number): bool
1740 return idx % 2 == 1
1741 enddef
1742 export def FastFilter(): list<number>
1743 return range(10)->filter('FilterFunc(v:key, v:val)')
1744 enddef
1745 export def FastFilterDirect(): list<number>
1746 return range(10)->filter(FilterFunc)
1747 enddef
1748 END
1749 writefile(filterLines, 'Xfilter.vim')
1750
1751 var lines =<< trim END
1752 vim9script
1753 import './Xfilter.vim' as filter
1754 def Test()
1755 var x: list<number> = filter.FastFilter()
1756 enddef
1757 Test()
1758 def TestDirect()
1759 var x: list<number> = filter.FastFilterDirect()
1760 enddef
1761 TestDirect()
1762 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001763 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +00001764 delete('Xfilter.vim')
1765enddef
1766
1767def Test_import_absolute()
1768 var import_lines = [
1769 'vim9script',
1770 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
1771 'def UseExported()',
1772 ' g:imported_abs = abs.exported',
1773 ' abs.exported = 8888',
1774 ' g:imported_after = abs.exported',
1775 'enddef',
1776 'UseExported()',
1777 'g:import_disassembled = execute("disass UseExported")',
1778 ]
1779 writefile(import_lines, 'Ximport_abs.vim')
1780 writefile(s:export_script_lines, 'Xexport_abs.vim')
1781
1782 source Ximport_abs.vim
1783
1784 assert_equal(9876, g:imported_abs)
1785 assert_equal(8888, g:imported_after)
1786 assert_match('<SNR>\d\+_UseExported\_s*' ..
1787 'g:imported_abs = abs.exported\_s*' ..
1788 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1789 '1 STOREG g:imported_abs\_s*' ..
1790 'abs.exported = 8888\_s*' ..
1791 '2 PUSHNR 8888\_s*' ..
1792 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1793 'g:imported_after = abs.exported\_s*' ..
1794 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1795 '5 STOREG g:imported_after',
1796 g:import_disassembled)
1797
1798 Undo_export_script_lines()
1799 unlet g:imported_abs
1800 unlet g:import_disassembled
1801
1802 delete('Ximport_abs.vim')
1803 delete('Xexport_abs.vim')
1804enddef
1805
1806def Test_import_rtp()
1807 var import_lines = [
1808 'vim9script',
1809 'import "Xexport_rtp.vim" as rtp',
1810 'g:imported_rtp = rtp.exported',
1811 ]
1812 writefile(import_lines, 'Ximport_rtp.vim')
1813 mkdir('import', 'p')
1814 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1815
1816 var save_rtp = &rtp
1817 &rtp = getcwd()
1818 source Ximport_rtp.vim
1819 &rtp = save_rtp
1820
1821 assert_equal(9876, g:imported_rtp)
1822
1823 Undo_export_script_lines()
1824 unlet g:imported_rtp
1825 delete('Ximport_rtp.vim')
1826 delete('import', 'rf')
1827enddef
1828
1829def Test_import_compile_error()
1830 var export_lines = [
1831 'vim9script',
1832 'export def ExpFunc(): string',
1833 ' return notDefined',
1834 'enddef',
1835 ]
1836 writefile(export_lines, 'Xexported.vim')
1837
1838 var import_lines = [
1839 'vim9script',
1840 'import "./Xexported.vim" as expo',
1841 'def ImpFunc()',
1842 ' echo expo.ExpFunc()',
1843 'enddef',
1844 'defcompile',
1845 ]
1846 writefile(import_lines, 'Ximport.vim')
1847
1848 try
1849 source Ximport.vim
1850 catch /E1001/
1851 # Error should be before the Xexported.vim file.
1852 assert_match('E1001: Variable not found: notDefined', v:exception)
1853 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1854 endtry
1855
1856 delete('Xexported.vim')
1857 delete('Ximport.vim')
1858enddef
1859
1860def Test_func_overrules_import_fails()
1861 var export_lines =<< trim END
1862 vim9script
1863 export def Func()
1864 echo 'imported'
1865 enddef
1866 END
1867 writefile(export_lines, 'XexportedFunc.vim')
1868
1869 var lines =<< trim END
1870 vim9script
1871 import './XexportedFunc.vim' as Func
1872 def Func()
1873 echo 'local to function'
1874 enddef
1875 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001876 v9.CheckScriptFailure(lines, 'E1213: Redefining imported item "Func"')
Bram Moolenaard8448622022-01-07 21:39:52 +00001877
1878 lines =<< trim END
1879 vim9script
1880 import './XexportedFunc.vim' as Func
1881 def Outer()
1882 def Func()
1883 echo 'local to function'
1884 enddef
1885 enddef
1886 defcompile
1887 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001888 v9.CheckScriptFailure(lines, 'E1236:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001889
1890 delete('XexportedFunc.vim')
1891enddef
1892
1893def Test_source_vim9_from_legacy()
1894 var vim9_lines =<< trim END
1895 vim9script
1896 var local = 'local'
1897 g:global = 'global'
1898 export var exported = 'exported'
1899 export def GetText(): string
1900 return 'text'
1901 enddef
1902 END
1903 writefile(vim9_lines, 'Xvim9_script.vim')
1904
1905 var legacy_lines =<< trim END
1906 source Xvim9_script.vim
1907
1908 call assert_false(exists('local'))
1909 call assert_false(exists('exported'))
1910 call assert_false(exists('s:exported'))
1911 call assert_equal('global', global)
1912 call assert_equal('global', g:global)
Bram Moolenaard8448622022-01-07 21:39:52 +00001913 END
1914 writefile(legacy_lines, 'Xlegacy_script.vim')
1915
1916 source Xlegacy_script.vim
1917 assert_equal('global', g:global)
1918 unlet g:global
1919
1920 delete('Xlegacy_script.vim')
1921 delete('Xvim9_script.vim')
1922enddef
1923
Bram Moolenaarc43e6232022-01-13 20:51:56 +00001924def Test_import_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_export.vim')
1935
1936 var legacy_lines =<< trim END
1937 import './Xvim9_export.vim' as vim9
1938
1939 call assert_false(exists('vim9'))
1940 call assert_false(exists('local'))
1941 call assert_false(exists('s:vim9.local'))
1942 call assert_equal('global', global)
1943 call assert_equal('global', g:global)
1944 call assert_false(exists('exported'))
1945 call assert_false(exists('s:exported'))
1946 call assert_false(exists('*GetText'))
1947
1948 " imported symbol is script-local
1949 call assert_equal('exported', s:vim9.exported)
1950 call assert_equal('text', s:vim9.GetText())
1951 END
1952 writefile(legacy_lines, 'Xlegacy_script.vim')
1953
1954 source Xlegacy_script.vim
1955 assert_equal('global', g:global)
1956 unlet g:global
1957
1958 delete('Xlegacy_script.vim')
1959 delete('Xvim9_export.vim')
1960enddef
1961
Bram Moolenaard8448622022-01-07 21:39:52 +00001962def Test_cmdline_win()
1963 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
1964 # the command line window.
1965 mkdir('rtp/syntax', 'p')
1966 var export_lines =<< trim END
1967 vim9script
1968 export var That = 'yes'
1969 END
1970 writefile(export_lines, 'rtp/syntax/Xexport.vim')
1971 var import_lines =<< trim END
1972 vim9script
1973 import './Xexport.vim' as exp
1974 echo exp.That
1975 END
1976 writefile(import_lines, 'rtp/syntax/vim.vim')
1977 var save_rtp = &rtp
1978 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
1979 syntax on
1980 augroup CmdWin
1981 autocmd CmdwinEnter * g:got_there = 'yes'
1982 augroup END
1983 # this will open and also close the cmdline window
1984 feedkeys('q:', 'xt')
1985 assert_equal('yes', g:got_there)
1986
1987 augroup CmdWin
1988 au!
1989 augroup END
1990 &rtp = save_rtp
1991 delete('rtp', 'rf')
1992enddef
1993
1994def Test_import_gone_when_sourced_twice()
1995 var exportlines =<< trim END
1996 vim9script
1997 if exists('g:guard')
1998 finish
1999 endif
2000 g:guard = 1
2001 export var name = 'someName'
2002 END
2003 writefile(exportlines, 'XexportScript.vim')
2004
2005 var lines =<< trim END
2006 vim9script
2007 import './XexportScript.vim' as expo
2008 def g:GetName(): string
2009 return expo.name
2010 enddef
2011 END
2012 writefile(lines, 'XscriptImport.vim')
2013 so XscriptImport.vim
2014 assert_equal('someName', g:GetName())
2015
2016 so XexportScript.vim
2017 assert_fails('call g:GetName()', 'E1149:')
2018
2019 delfunc g:GetName
2020 delete('XexportScript.vim')
2021 delete('XscriptImport.vim')
2022 unlet g:guard
2023enddef
2024
Bram Moolenaar160aa862022-01-10 21:29:57 +00002025" test using an auto-loaded function and variable
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002026def Test_vim9_autoload_full_name()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002027 var lines =<< trim END
2028 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002029 export def Gettest(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002030 return 'test'
2031 enddef
2032 g:some#name = 'name'
2033 g:some#dict = {key: 'value'}
2034
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002035 export def Varargs(a1: string, ...l: list<string>): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002036 return a1 .. l[0] .. l[1]
2037 enddef
2038 END
2039
2040 mkdir('Xdir/autoload', 'p')
2041 writefile(lines, 'Xdir/autoload/some.vim')
2042 var save_rtp = &rtp
2043 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2044
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002045 assert_equal('test', g:some#Gettest())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002046 assert_equal('name', g:some#name)
2047 assert_equal('value', g:some#dict.key)
2048 g:some#other = 'other'
2049 assert_equal('other', g:some#other)
2050
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002051 assert_equal('abc', some#Varargs('a', 'b', 'c'))
Bram Moolenaar160aa862022-01-10 21:29:57 +00002052
2053 # upper case script name works
2054 lines =<< trim END
2055 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002056 export def GetOther(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002057 return 'other'
2058 enddef
2059 END
2060 writefile(lines, 'Xdir/autoload/Other.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002061 assert_equal('other', g:Other#GetOther())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002062
2063 delete('Xdir', 'rf')
2064 &rtp = save_rtp
2065enddef
2066
2067def Test_vim9script_autoload()
2068 mkdir('Xdir/autoload', 'p')
2069 var save_rtp = &rtp
2070 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2071
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002072 # when the path has "/autoload/" prefix is not needed
Bram Moolenaar160aa862022-01-10 21:29:57 +00002073 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002074 vim9script
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002075 g:prefixed_loaded += 1
Bram Moolenaar160aa862022-01-10 21:29:57 +00002076
2077 export def Gettest(): string
2078 return 'test'
2079 enddef
2080
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002081 export var name = 'name'
2082
2083 export func GetFunc()
2084 return Gettest() .. 'more' .. s:name
Bram Moolenaar160aa862022-01-10 21:29:57 +00002085 endfunc
2086
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002087 export def GetDef(): string
2088 return Gettest() .. 'more' .. name
2089 enddef
2090
Bram Moolenaar160aa862022-01-10 21:29:57 +00002091 export final fname = 'final'
2092 export const cname = 'const'
2093 END
2094 writefile(lines, 'Xdir/autoload/prefixed.vim')
2095
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002096 g:prefixed_loaded = 0
2097 g:expected_loaded = 0
Bram Moolenaar160aa862022-01-10 21:29:57 +00002098 lines =<< trim END
2099 vim9script
2100 import autoload 'prefixed.vim'
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002101 assert_equal(g:expected_loaded, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002102 assert_equal('test', prefixed.Gettest())
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002103 assert_equal(1, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002104
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002105 assert_equal('testmorename', prefixed.GetFunc())
2106 assert_equal('testmorename', prefixed.GetDef())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002107 assert_equal('name', prefixed.name)
2108 assert_equal('final', prefixed.fname)
2109 assert_equal('const', prefixed.cname)
2110 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002111 v9.CheckScriptSuccess(lines)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002112 # can source it again, autoload script not loaded again
2113 g:expected_loaded = 1
Bram Moolenaar62aec932022-01-29 21:45:34 +00002114 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002115
2116 # can also get the items by autoload name
2117 lines =<< trim END
2118 call assert_equal('test', prefixed#Gettest())
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002119 call assert_equal('testmorename', prefixed#GetFunc())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002120 call assert_equal('name', prefixed#name)
2121 call assert_equal('final', prefixed#fname)
2122 call assert_equal('const', prefixed#cname)
2123 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002124 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002125
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002126 unlet g:prefixed_loaded
2127 unlet g:expected_loaded
2128 delete('Xdir', 'rf')
2129 &rtp = save_rtp
2130enddef
2131
Bram Moolenaard02dce22022-01-18 17:43:04 +00002132def Test_import_autoload_not_exported()
2133 mkdir('Xdir/autoload', 'p')
2134 var save_rtp = &rtp
2135 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2136
2137 # error when using an item that is not exported from an autoload script
2138 var exportLines =<< trim END
2139 vim9script
2140 var notExported = 123
2141 def NotExport()
2142 echo 'nop'
2143 enddef
2144 END
2145 writefile(exportLines, 'Xdir/autoload/notExport1.vim')
2146
2147 var lines =<< trim END
2148 vim9script
2149 import autoload 'notExport1.vim'
2150 echo notExport1.notFound
2151 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002152 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002153
2154 lines =<< trim END
2155 vim9script
2156 import autoload 'notExport1.vim'
2157 echo notExport1.notExported
2158 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002159 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002160
2161 lines =<< trim END
2162 vim9script
2163 import autoload 'notExport1.vim'
2164 echo notExport1.NotFunc()
2165 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002166 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002167
2168 lines =<< trim END
2169 vim9script
2170 import autoload 'notExport1.vim'
2171 echo notExport1.NotExport()
2172 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002173 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002174
2175 lines =<< trim END
2176 vim9script
2177 import autoload 'notExport1.vim'
2178 echo 'text'->notExport1.NotFunc()
2179 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002180 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002181
2182 lines =<< trim END
2183 vim9script
2184 import autoload 'notExport1.vim'
2185 echo 'text'->notExport1.NotExport()
2186 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002187 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002188
2189 # using a :def function we use a different autoload script every time so that
2190 # the function is compiled without the script loaded
2191 writefile(exportLines, 'Xdir/autoload/notExport2.vim')
2192 lines =<< trim END
2193 vim9script
2194 import autoload 'notExport2.vim'
2195 def Testit()
2196 echo notExport2.notFound
2197 enddef
2198 Testit()
2199 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002200 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport2#notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002201
2202 writefile(exportLines, 'Xdir/autoload/notExport3.vim')
2203 lines =<< trim END
2204 vim9script
2205 import autoload 'notExport3.vim'
2206 def Testit()
2207 echo notExport3.notExported
2208 enddef
2209 Testit()
2210 END
2211 # don't get E1049 because it is too complicated to figure out
Bram Moolenaar62aec932022-01-29 21:45:34 +00002212 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport3#notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002213
2214 writefile(exportLines, 'Xdir/autoload/notExport4.vim')
2215 lines =<< trim END
2216 vim9script
2217 import autoload 'notExport4.vim'
2218 def Testit()
2219 echo notExport4.NotFunc()
2220 enddef
2221 Testit()
2222 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002223 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport4#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002224
2225 writefile(exportLines, 'Xdir/autoload/notExport5.vim')
2226 lines =<< trim END
2227 vim9script
2228 import autoload 'notExport5.vim'
2229 def Testit()
2230 echo notExport5.NotExport()
2231 enddef
2232 Testit()
2233 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002234 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport5#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002235
2236 writefile(exportLines, 'Xdir/autoload/notExport6.vim')
2237 lines =<< trim END
2238 vim9script
2239 import autoload 'notExport6.vim'
2240 def Testit()
2241 echo 'text'->notExport6.NotFunc()
2242 enddef
2243 Testit()
2244 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002245 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport6#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002246
2247 writefile(exportLines, 'Xdir/autoload/notExport7.vim')
2248 lines =<< trim END
2249 vim9script
2250 import autoload 'notExport7.vim'
2251 def Testit()
2252 echo 'text'->notExport7.NotExport()
2253 enddef
2254 Testit()
2255 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002256 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport7#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002257
2258 delete('Xdir', 'rf')
2259 &rtp = save_rtp
2260enddef
2261
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002262def Test_vim9script_autoload_call()
2263 mkdir('Xdir/autoload', 'p')
2264 var save_rtp = &rtp
2265 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2266
2267 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002268 vim9script
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002269
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002270 export def RetArg(arg: string): string
2271 return arg
2272 enddef
2273
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002274 export def Getother()
2275 g:result = 'other'
2276 enddef
2277 END
Bram Moolenaar5d982692022-01-12 15:15:27 +00002278 writefile(lines, 'Xdir/autoload/another.vim')
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002279
2280 lines =<< trim END
2281 vim9script
Bram Moolenaar5d982692022-01-12 15:15:27 +00002282 import autoload 'another.vim'
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002283
2284 # compile this before 'another.vim' is loaded
2285 def CallAnother()
2286 assert_equal('foo', 'foo'->another.RetArg())
2287 enddef
2288 CallAnother()
2289
Bram Moolenaar5d982692022-01-12 15:15:27 +00002290 call another.Getother()
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002291 assert_equal('other', g:result)
Bram Moolenaar3d8e25a2022-01-22 11:00:02 +00002292
2293 assert_equal('arg', call('another.RetArg', ['arg']))
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00002294
2295 verbose function another.Getother
2296 # should we disallow this?
2297 verbose function another#Getother
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002298 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002299 v9.CheckScriptSuccess(lines)
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002300
2301 unlet g:result
Bram Moolenaar160aa862022-01-10 21:29:57 +00002302 delete('Xdir', 'rf')
2303 &rtp = save_rtp
2304enddef
2305
Bram Moolenaarb697dc22022-01-22 11:27:29 +00002306def Test_vim9script_noclear_autoload()
2307 mkdir('Xdir/autoload', 'p')
2308 var save_rtp = &rtp
2309 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2310
2311 var lines =<< trim END
2312 vim9script
2313 export def Func(): string
2314 return 'called'
2315 enddef
2316 g:double_loaded = 'yes'
2317 END
2318 writefile(lines, 'Xdir/autoload/double.vim')
2319
2320 lines =<< trim END
2321 vim9script noclear
2322 if exists('g:script_loaded')
2323 finish
2324 endif
2325 g:script_loaded = true
2326
2327 import autoload 'double.vim'
2328 nnoremap <F3> <ScriptCmd>g:result = double.Func()<CR>
2329 END
2330 g:double_loaded = 'no'
2331 writefile(lines, 'Xloaddouble')
2332 source Xloaddouble
2333 assert_equal('no', g:double_loaded)
2334 assert_equal(true, g:script_loaded)
2335 source Xloaddouble
2336 feedkeys("\<F3>", 'xt')
2337 assert_equal('called', g:result)
2338 assert_equal('yes', g:double_loaded)
2339
2340 delete('Xloaddouble')
2341 unlet g:double_loaded
2342 unlet g:script_loaded
2343 unlet g:result
2344 delete('Xdir', 'rf')
2345 &rtp = save_rtp
2346enddef
2347
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002348def Test_vim9script_autoload_duplicate()
2349 mkdir('Xdir/autoload', 'p')
2350
2351 var lines =<< trim END
2352 vim9script
2353
2354 export def Func()
2355 enddef
2356
2357 def Func()
2358 enddef
2359 END
2360 writefile(lines, 'Xdir/autoload/dupfunc.vim')
2361 assert_fails('source Xdir/autoload/dupfunc.vim', 'E1073:')
2362
2363 lines =<< trim END
2364 vim9script
2365
2366 def Func()
2367 enddef
2368
2369 export def Func()
2370 enddef
2371 END
2372 writefile(lines, 'Xdir/autoload/dup2func.vim')
2373 assert_fails('source Xdir/autoload/dup2func.vim', 'E1073:')
2374
2375 lines =<< trim END
2376 vim9script
2377
2378 def Func()
2379 enddef
2380
2381 export var Func = 'asdf'
2382 END
2383 writefile(lines, 'Xdir/autoload/dup3func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002384 assert_fails('source Xdir/autoload/dup3func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002385
2386 lines =<< trim END
2387 vim9script
2388
2389 export var Func = 'asdf'
2390
2391 def Func()
2392 enddef
2393 END
2394 writefile(lines, 'Xdir/autoload/dup4func.vim')
2395 assert_fails('source Xdir/autoload/dup4func.vim', 'E707:')
2396
2397 lines =<< trim END
2398 vim9script
2399
2400 var Func = 'asdf'
2401
2402 export def Func()
2403 enddef
2404 END
2405 writefile(lines, 'Xdir/autoload/dup5func.vim')
2406 assert_fails('source Xdir/autoload/dup5func.vim', 'E707:')
2407
2408 lines =<< trim END
2409 vim9script
2410
2411 export def Func()
2412 enddef
2413
2414 var Func = 'asdf'
2415 END
2416 writefile(lines, 'Xdir/autoload/dup6func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002417 assert_fails('source Xdir/autoload/dup6func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002418
2419 delete('Xdir', 'rf')
2420enddef
2421
Bram Moolenaar2017d6f2022-01-20 19:38:46 +00002422def Test_autoload_missing_function_name()
2423 mkdir('Xdir/autoload', 'p')
2424
2425 var lines =<< trim END
2426 vim9script
2427
2428 def loadme#()
2429 enddef
2430 END
2431 writefile(lines, 'Xdir/autoload/loadme.vim')
2432 assert_fails('source Xdir/autoload/loadme.vim', 'E129:')
2433
2434 delete('Xdir', 'rf')
2435enddef
2436
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002437def Test_autoload_name_wrong()
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002438 var lines =<< trim END
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002439 def Xscriptname#Func()
2440 enddef
2441 END
2442 writefile(lines, 'Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002443 v9.CheckScriptFailure(lines, 'E746:')
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00002444 delete('Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002445
2446 mkdir('Xdir/autoload', 'p')
2447 lines =<< trim END
2448 vim9script
2449 def somescript#Func()
2450 enddef
2451 END
2452 writefile(lines, 'Xdir/autoload/somescript.vim')
2453 assert_fails('source Xdir/autoload/somescript.vim', 'E1263:')
2454
2455 delete('Xdir', 'rf')
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002456enddef
2457
Bram Moolenaard041f422022-01-12 19:54:00 +00002458def Test_import_autoload_postponed()
2459 mkdir('Xdir/autoload', 'p')
2460 var save_rtp = &rtp
2461 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2462
2463 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002464 vim9script
Bram Moolenaard041f422022-01-12 19:54:00 +00002465
2466 g:loaded_postponed = 'true'
2467 export var variable = 'bla'
2468 export def Function(): string
2469 return 'bla'
2470 enddef
2471 END
2472 writefile(lines, 'Xdir/autoload/postponed.vim')
2473
2474 lines =<< trim END
2475 vim9script
2476
2477 import autoload 'postponed.vim'
2478 def Tryit()
2479 echo postponed.variable
2480 echo postponed.Function()
2481 enddef
2482 defcompile
2483 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002484 v9.CheckScriptSuccess(lines)
Bram Moolenaard041f422022-01-12 19:54:00 +00002485 assert_false(exists('g:loaded_postponed'))
Bram Moolenaar62aec932022-01-29 21:45:34 +00002486 v9.CheckScriptSuccess(lines + ['Tryit()'])
Bram Moolenaard041f422022-01-12 19:54:00 +00002487 assert_equal('true', g:loaded_postponed)
2488
2489 unlet g:loaded_postponed
2490 delete('Xdir', 'rf')
2491 &rtp = save_rtp
2492enddef
2493
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002494def Test_import_autoload_override()
2495 mkdir('Xdir/autoload', 'p')
2496 var save_rtp = &rtp
2497 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2498 test_override('autoload', 1)
2499
2500 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002501 vim9script
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002502
2503 g:loaded_override = 'true'
2504 export var variable = 'bla'
2505 export def Function(): string
2506 return 'bla'
2507 enddef
2508 END
2509 writefile(lines, 'Xdir/autoload/override.vim')
2510
2511 lines =<< trim END
2512 vim9script
2513
2514 import autoload 'override.vim'
2515 assert_equal('true', g:loaded_override)
2516
2517 def Tryit()
2518 echo override.doesNotExist
2519 enddef
2520 defcompile
2521 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002522 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1)
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002523
2524 test_override('autoload', 0)
2525 unlet g:loaded_override
2526 delete('Xdir', 'rf')
2527 &rtp = save_rtp
2528enddef
2529
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002530def Test_autoload_mapping()
2531 mkdir('Xdir/autoload', 'p')
2532 var save_rtp = &rtp
2533 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2534
2535 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002536 vim9script
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002537
2538 g:toggle_loaded = 'yes'
2539
2540 export def Toggle(): string
2541 return ":g:toggle_called = 'yes'\<CR>"
2542 enddef
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002543 export def Doit()
2544 g:doit_called = 'yes'
2545 enddef
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002546 END
2547 writefile(lines, 'Xdir/autoload/toggle.vim')
2548
2549 lines =<< trim END
2550 vim9script
2551
2552 import autoload 'toggle.vim'
2553
2554 nnoremap <silent> <expr> tt toggle.Toggle()
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002555 nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR>
2556 nnoremap <silent> yy <Cmd>toggle.Doit()<CR>
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002557 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002558 v9.CheckScriptSuccess(lines)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002559 assert_false(exists("g:toggle_loaded"))
2560 assert_false(exists("g:toggle_called"))
Bram Moolenaar6079da72022-01-18 14:16:59 +00002561 assert_match('\d A: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002562
2563 feedkeys("tt", 'xt')
2564 assert_equal('yes', g:toggle_loaded)
2565 assert_equal('yes', g:toggle_called)
Bram Moolenaar6079da72022-01-18 14:16:59 +00002566 assert_match('\d: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002567
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002568 feedkeys("xx", 'xt')
2569 assert_equal('yes', g:doit_called)
2570
2571 assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle')
2572
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002573 nunmap tt
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002574 nunmap xx
2575 nunmap yy
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002576 unlet g:toggle_loaded
2577 unlet g:toggle_called
2578 delete('Xdir', 'rf')
2579 &rtp = save_rtp
2580enddef
2581
Bram Moolenaar160aa862022-01-10 21:29:57 +00002582def Test_vim9script_autoload_fails()
2583 var lines =<< trim END
2584 vim9script autoload
2585 var n = 0
2586 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002587 v9.CheckScriptFailure(lines, 'E475: Invalid argument: autoload')
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002588
2589 lines =<< trim END
2590 vim9script noclear noclear
2591 var n = 0
2592 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002593 v9.CheckScriptFailure(lines, 'E983: Duplicate argument: noclear')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002594enddef
2595
2596def Test_import_autoload_fails()
2597 var lines =<< trim END
2598 vim9script
2599 import autoload autoload 'prefixed.vim'
2600 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002601 v9.CheckScriptFailure(lines, 'E121: Undefined variable: autoload')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002602
2603 lines =<< trim END
2604 vim9script
Bram Moolenaar1836d612022-01-18 13:14:47 +00002605 import autoload './doesNotExist.vim'
Bram Moolenaar160aa862022-01-10 21:29:57 +00002606 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002607 v9.CheckScriptFailure(lines, 'E282:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002608
2609 lines =<< trim END
2610 vim9script
2611 import autoload '/dir/doesNotExist.vim'
2612 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002613 v9.CheckScriptFailure(lines, 'E282:', 2)
2614
2615 lines =<< trim END
2616 vim9script
2617 import autoload '../testdir'
2618 END
2619 v9.CheckScriptFailure(lines, 'E17:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002620
2621 lines =<< trim END
2622 vim9script
2623 import autoload 'doesNotExist.vim'
2624 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002625 v9.CheckScriptFailure(lines, 'E1053: Could not import "doesNotExist.vim"')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002626enddef
2627
2628" test disassembling an auto-loaded function starting with "debug"
2629def Test_vim9_autoload_disass()
2630 mkdir('Xdir/autoload', 'p')
2631 var save_rtp = &rtp
2632 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2633
2634 var lines =<< trim END
2635 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002636 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002637 return 'debug'
2638 enddef
2639 END
2640 writefile(lines, 'Xdir/autoload/debugit.vim')
2641
2642 lines =<< trim END
2643 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002644 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002645 return 'profile'
2646 enddef
2647 END
2648 writefile(lines, 'Xdir/autoload/profileit.vim')
2649
2650 lines =<< trim END
2651 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002652 assert_equal('debug', debugit#Test())
2653 disass debugit#Test
2654 assert_equal('profile', profileit#Test())
2655 disass profileit#Test
Bram Moolenaar160aa862022-01-10 21:29:57 +00002656 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002657 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002658
2659 delete('Xdir', 'rf')
2660 &rtp = save_rtp
2661enddef
2662
2663" test using a vim9script that is auto-loaded from an autocmd
2664def Test_vim9_aucmd_autoload()
2665 var lines =<< trim END
2666 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002667 export def Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002668 echomsg getreg('"')
2669 enddef
2670 END
2671
2672 mkdir('Xdir/autoload', 'p')
2673 writefile(lines, 'Xdir/autoload/foo.vim')
2674 var save_rtp = &rtp
2675 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2676 augroup test
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002677 autocmd TextYankPost * call foo#Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002678 augroup END
2679
2680 normal Y
2681
2682 augroup test
2683 autocmd!
2684 augroup END
2685 delete('Xdir', 'rf')
2686 &rtp = save_rtp
2687enddef
2688
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002689" test using a autoloaded file that is case sensitive
2690def Test_vim9_autoload_case_sensitive()
2691 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002692 vim9script
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002693 export def CaseSensitive(): string
2694 return 'done'
2695 enddef
2696 END
2697
2698 mkdir('Xdir/autoload', 'p')
2699 writefile(lines, 'Xdir/autoload/CaseSensitive.vim')
2700 var save_rtp = &rtp
2701 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2702
2703 lines =<< trim END
2704 vim9script
2705 import autoload 'CaseSensitive.vim'
2706 assert_equal('done', CaseSensitive.CaseSensitive())
2707 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002708 v9.CheckScriptSuccess(lines)
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002709
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002710 if !has('fname_case')
2711 lines =<< trim END
2712 vim9script
2713 import autoload 'CaseSensitive.vim'
2714 import autoload 'casesensitive.vim'
2715 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002716 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002717 endif
2718
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002719 delete('Xdir', 'rf')
2720 &rtp = save_rtp
2721enddef
2722
Bram Moolenaar160aa862022-01-10 21:29:57 +00002723" This was causing a crash because suppress_errthrow wasn't reset.
2724def Test_vim9_autoload_error()
2725 var lines =<< trim END
2726 vim9script
2727 def crash#func()
2728 try
2729 for x in List()
2730 endfor
2731 catch
2732 endtry
2733 g:ok = true
2734 enddef
2735 fu List()
2736 invalid
2737 endfu
2738 try
2739 alsoinvalid
2740 catch /wontmatch/
2741 endtry
2742 END
2743 call mkdir('Xruntime/autoload', 'p')
2744 call writefile(lines, 'Xruntime/autoload/crash.vim')
2745
2746 # run in a separate Vim to avoid the side effects of assert_fails()
2747 lines =<< trim END
2748 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
2749 call crash#func()
2750 call writefile(['ok'], 'Xdidit')
2751 qall!
2752 END
2753 writefile(lines, 'Xscript')
Bram Moolenaar62aec932022-01-29 21:45:34 +00002754 g:RunVim([], [], '-S Xscript')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002755 assert_equal(['ok'], readfile('Xdidit'))
2756
2757 delete('Xdidit')
2758 delete('Xscript')
2759 delete('Xruntime', 'rf')
2760
2761 lines =<< trim END
2762 vim9script
2763 var foo#bar = 'asdf'
2764 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002765 v9.CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002766enddef
2767
Bram Moolenaard8448622022-01-07 21:39:52 +00002768
2769" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker