blob: c6a820654a0f49142b7aae2780c9f2e03fbd9d81 [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
645 export def Funcx()
646 g:result = 42
647 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
654 nnoremap <F3> :call <sid>Funcy()<cr>
655 END
656 writefile(lines, 'Xmapscript.vim')
657
658 source Xmapscript.vim
659 feedkeys("\<F3>", "xt")
660 assert_equal(42, g:result)
661
662 unlet g:result
663 delete('XsomeExport.vim')
664 delete('Xmapscript.vim')
665 nunmap <F3>
666enddef
667
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000668def Test_use_import_in_command_completion()
Bram Moolenaar15d16352022-01-17 20:09:08 +0000669 var lines =<< trim END
670 vim9script
671 export def Complete(..._): list<string>
672 return ['abcd']
673 enddef
674 END
675 writefile(lines, 'Xscript.vim')
676
677 lines =<< trim END
678 vim9script
679 import './Xscript.vim'
680
681 command -nargs=1 -complete=customlist,Xscript.Complete Cmd echo 'ok'
682 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
683 assert_equal('#Cmd abcd', @:)
684 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000685 v9.CheckScriptSuccess(lines)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000686
687 delcommand Cmd
688 delete('Xscript.vim')
689enddef
690
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000691def Test_use_autoload_import_in_insert_completion()
692 mkdir('Xdir/autoload', 'p')
693 var save_rtp = &rtp
694 exe 'set rtp^=' .. getcwd() .. '/Xdir'
695
696 var lines =<< trim END
697 vim9script
698 export def ThesaurusFunc(findbase: bool, _): any
699 if findbase
700 return 1
701 endif
702 return [
703 'check',
704 'experiment',
705 'test',
706 'verification'
707 ]
708 enddef
709 g:completion_loaded = 'yes'
710 END
711 writefile(lines, 'Xdir/autoload/completion.vim')
712
713 new
714 lines =<< trim END
715 vim9script
716 g:completion_loaded = 'no'
717 import autoload 'completion.vim'
718 set thesaurusfunc=completion.ThesaurusFunc
719 assert_equal('no', g:completion_loaded)
720 feedkeys("i\<C-X>\<C-T>\<C-N>\<Esc>", 'xt')
721 assert_equal('experiment', getline(1))
722 assert_equal('yes', g:completion_loaded)
723 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000724 v9.CheckScriptSuccess(lines)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000725
726 set thesaurusfunc=
727 bwipe!
728 delete('Xdir', 'rf')
729 &rtp = save_rtp
730enddef
731
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000732def Test_use_autoload_import_partial_in_opfunc()
733 mkdir('Xdir/autoload', 'p')
734 var save_rtp = &rtp
735 exe 'set rtp^=' .. getcwd() .. '/Xdir'
736
737 var lines =<< trim END
738 vim9script
739 export def Opfunc(..._)
740 g:opfunc_called = 'yes'
741 enddef
742 END
743 writefile(lines, 'Xdir/autoload/opfunc.vim')
744
745 new
746 lines =<< trim END
747 vim9script
748 import autoload 'opfunc.vim'
749 nnoremap <expr> <F3> TheFunc()
750 def TheFunc(): string
751 &operatorfunc = function('opfunc.Opfunc', [0])
752 return 'g@'
753 enddef
754 feedkeys("\<F3>l", 'xt')
755 assert_equal('yes', g:opfunc_called)
756 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000757 v9.CheckScriptSuccess(lines)
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000758
759 set opfunc=
760 bwipe!
761 delete('Xdir', 'rf')
Bram Moolenaar06b77222022-01-25 15:51:56 +0000762 nunmap <F3>
763 &rtp = save_rtp
764enddef
765
766def Test_set_opfunc_to_autoload_func_directly()
767 mkdir('Xdir/autoload', 'p')
768 var save_rtp = &rtp
769 exe 'set rtp^=' .. getcwd() .. '/Xdir'
770
771 var lines =<< trim END
772 vim9script
773 export def Opfunc(..._)
774 g:opfunc_called = 'yes'
775 enddef
776 END
777 writefile(lines, 'Xdir/autoload/opfunc.vim')
778
779 new
780 lines =<< trim END
781 vim9script
782 import autoload 'opfunc.vim'
783 nnoremap <expr> <F3> TheFunc()
784 def TheFunc(): string
785 &operatorfunc = opfunc.Opfunc
786 return 'g@'
787 enddef
788 feedkeys("\<F3>l", 'xt')
789 assert_equal('yes', g:opfunc_called)
790 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000791 v9.CheckScriptSuccess(lines)
Bram Moolenaar06b77222022-01-25 15:51:56 +0000792
793 set opfunc=
794 bwipe!
795 delete('Xdir', 'rf')
796 nunmap <F3>
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000797 &rtp = save_rtp
798enddef
799
Bram Moolenaare70dd112022-01-21 16:31:11 +0000800def Test_use_autoload_import_in_fold_expression()
801 mkdir('Xdir/autoload', 'p')
802 var save_rtp = &rtp
803 exe 'set rtp^=' .. getcwd() .. '/Xdir'
804
805 var lines =<< trim END
806 vim9script
807 export def Expr(): string
808 return getline(v:lnum) =~ '^#' ? '>1' : '1'
809 enddef
Bram Moolenaar9530b582022-01-22 13:39:08 +0000810 export def Text(): string
811 return 'fold text'
812 enddef
Bram Moolenaare70dd112022-01-21 16:31:11 +0000813 g:fold_loaded = 'yes'
814 END
815 writefile(lines, 'Xdir/autoload/fold.vim')
816
817 lines =<< trim END
818 vim9script
819 import autoload 'fold.vim'
820 &foldexpr = 'fold.Expr()'
Bram Moolenaar9530b582022-01-22 13:39:08 +0000821 &foldtext = 'fold.Text()'
Bram Moolenaare70dd112022-01-21 16:31:11 +0000822 &foldmethod = 'expr'
823 &debug = 'throw'
824 END
825 new
826 setline(1, ['# one', 'text', '# two', 'text'])
827 g:fold_loaded = 'no'
Bram Moolenaar62aec932022-01-29 21:45:34 +0000828 v9.CheckScriptSuccess(lines)
Bram Moolenaare70dd112022-01-21 16:31:11 +0000829 assert_equal('no', g:fold_loaded)
830 redraw
831 assert_equal('yes', g:fold_loaded)
832
833 # Check that script context of 'foldexpr' is copied to another buffer.
834 edit! otherfile
835 redraw
836
Bram Moolenaar9530b582022-01-22 13:39:08 +0000837 set foldexpr= foldtext& foldmethod& debug=
Bram Moolenaare70dd112022-01-21 16:31:11 +0000838 bwipe!
839 delete('Xdir', 'rf')
840 &rtp = save_rtp
841enddef
842
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100843def Test_autoload_import_relative()
844 var lines =<< trim END
845 vim9script
846
847 g:loaded = 'yes'
848 export def RelFunc(): string
849 return 'relfunc'
850 enddef
851 def NotExported()
852 echo 'not'
853 enddef
854
855 export var someText = 'some text'
856 var notexp = 'bad'
857 END
858 writefile(lines, 'XimportRel.vim')
859 writefile(lines, 'XimportRel2.vim')
860 writefile(lines, 'XimportRel3.vim')
861
862 lines =<< trim END
863 vim9script
864 g:loaded = 'no'
865 import autoload './XimportRel.vim'
866 assert_equal('no', g:loaded)
867
868 def AFunc(): string
869 var res = ''
870 res ..= XimportRel.RelFunc()
871 res ..= '/'
872 res ..= XimportRel.someText
873 XimportRel.someText = 'from AFunc'
874 return res
875 enddef
876 # script not loaded when compiling
877 defcompile
878 assert_equal('no', g:loaded)
879
880 assert_equal('relfunc/some text', AFunc())
881 assert_equal('yes', g:loaded)
882 unlet g:loaded
883
884 assert_equal('from AFunc', XimportRel.someText)
885 XimportRel.someText = 'from script'
886 assert_equal('from script', XimportRel.someText)
887 END
888 v9.CheckScriptSuccess(lines)
889
890 lines =<< trim END
891 vim9script
892 import autoload './XimportRel.vim'
893 echo XimportRel.NotExported()
894 END
895 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 3)
896
897 lines =<< trim END
898 vim9script
899 import autoload './XimportRel.vim'
900 echo XimportRel.notexp
901 END
902 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
903
904 lines =<< trim END
905 vim9script
906 import autoload './XimportRel.vim'
907 XimportRel.notexp = 'bad'
908 END
909 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
910
911 lines =<< trim END
912 vim9script
913 import autoload './XimportRel.vim'
914 def Func()
915 echo XimportRel.NotExported()
916 enddef
917 Func()
918 END
919 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 1)
920
921 lines =<< trim END
922 vim9script
923 import autoload './XimportRel.vim'
924 def Func()
925 echo XimportRel.notexp
926 enddef
927 Func()
928 END
929 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
930
931 lines =<< trim END
932 vim9script
933 import autoload './XimportRel.vim'
934 def Func()
935 XimportRel.notexp = 'bad'
936 enddef
937 Func()
938 END
939 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
940
941 # does not fail if the script wasn't loaded yet
942 g:loaded = 'no'
943 lines =<< trim END
944 vim9script
945 import autoload './XimportRel2.vim'
946 def Func()
947 echo XimportRel2.notexp
948 enddef
949 defcompile
950 END
951 v9.CheckScriptSuccess(lines)
952 assert_equal('no', g:loaded)
953
954 # fails with a not loaded import
955 lines =<< trim END
956 vim9script
957 import autoload './XimportRel3.vim'
958 def Func()
959 XimportRel3.notexp = 'bad'
960 enddef
961 Func()
962 END
963 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
964 assert_equal('yes', g:loaded)
965 unlet g:loaded
966
967 delete('XimportRel.vim')
968 delete('XimportRel2.vim')
969 delete('XimportRel3.vim')
970enddef
971
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000972func Test_import_in_diffexpr()
973 CheckExecutable diff
974
975 call Run_Test_import_in_diffexpr()
976endfunc
977
978def Run_Test_import_in_diffexpr()
979 var lines =<< trim END
980 vim9script
981
982 export def DiffExpr()
983 # Prepend some text to check diff type detection
984 writefile(['warning', ' message'], v:fname_out)
985 silent exe '!diff ' .. v:fname_in .. ' '
986 .. v:fname_new .. '>>' .. v:fname_out
987 enddef
988 END
989 writefile(lines, 'Xdiffexpr')
990
991 lines =<< trim END
992 vim9script
993 import './Xdiffexpr' as diff
994
995 set diffexpr=diff.DiffExpr()
996 set diffopt=foldcolumn:0
997 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000998 v9.CheckScriptSuccess(lines)
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000999
1000 enew!
1001 call setline(1, ['one', 'two', 'three'])
1002 diffthis
1003
1004 botright vert new
1005 call setline(1, ['one', 'two', 'three.'])
1006 diffthis
1007 # we only check if this does not cause errors
1008 redraw
1009
1010 diffoff!
1011 bwipe!
1012 bwipe!
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001013 delete('Xdiffexpr')
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001014enddef
1015
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001016def Test_import_in_patchexpr()
1017 var lines =<< trim END
1018 vim9script
1019 export def TPatch()
1020 call writefile(['output file'], v:fname_out)
1021 enddef
1022 END
1023 writefile(lines, 'Xpatchexpr')
1024
1025 lines =<< trim END
1026 vim9script
1027 import './Xpatchexpr' as patch
1028 set patchexpr=patch.TPatch()
1029 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001030 v9.CheckScriptSuccess(lines)
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001031
1032 call writefile(['input file'], 'Xinput')
1033 call writefile(['diff file'], 'Xdiff')
1034 :%bwipe!
1035 edit Xinput
1036 diffpatch Xdiff
1037 call assert_equal('output file', getline(1))
1038
1039 call delete('Xinput')
1040 call delete('Xdiff')
1041 call delete('Xpatchexpr')
1042 set patchexpr&
1043 :%bwipe!
1044enddef
1045
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001046def Test_import_in_formatexpr()
1047 var lines =<< trim END
1048 vim9script
1049 export def MyFormatExpr(): number
1050 g:did_format = 'yes'
1051 return 0
1052 enddef
1053 END
1054 writefile(lines, 'Xformatter')
1055
1056 lines =<< trim END
1057 vim9script
1058 import './Xformatter' as format
1059 set formatexpr=format.MyFormatExpr()
1060 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001061 v9.CheckScriptSuccess(lines)
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001062
1063 new
1064 setline(1, ['a', 'b', 'c'])
1065 normal gqG
1066 assert_equal('yes', g:did_format)
1067
1068 bwipe!
1069 delete('Xformatter')
1070 unlet g:did_format
1071 set formatexpr=
1072enddef
1073
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001074def Test_import_in_includeexpr()
1075 writefile(['found it'], 'Xthisfile')
1076 new
1077
1078 var lines =<< trim END
1079 vim9script
1080 export def DoSub(): string
1081 return substitute(v:fname, 'that', 'this', '')
1082 enddef
1083 END
1084 writefile(lines, 'Xinclude.vim')
1085
1086 lines =<< trim END
1087 vim9script
1088 import './Xinclude.vim'
1089 set includeexpr=Xinclude.DoSub()
1090 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001091 v9.CheckScriptSuccess(lines)
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001092
1093 setline(1, ['Xthatfile'])
1094 exe "normal \<C-W>f"
1095 assert_equal('Xthisfile', expand('%'))
1096
1097 bwipe!
1098 bwipe!
1099 set includeexpr=
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001100 delete('Xinclude.vim')
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001101 delete('Xthisfile')
1102enddef
1103
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001104def Test_import_in_indentexpr()
1105 var lines =<< trim END
1106 vim9script
1107 export def GetIndent(): number
1108 return 5
1109 enddef
1110 END
1111 writefile(lines, 'Xindenter')
1112
1113 lines =<< trim END
1114 vim9script
1115 import './Xindenter' as indent
1116 set indentexpr=indent.GetIndent()
1117 set debug=throw
1118 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001119 v9.CheckScriptSuccess(lines)
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001120
1121 new
1122 setline(1, 'hello')
1123 normal ==
1124 assert_equal(' hello', getline(1))
1125
1126 bwipe!
1127 set indentexpr= debug=
1128 delete('Xindenter')
1129enddef
1130
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001131func Test_import_in_printexpr()
1132 CheckFeature postscript
1133 call Run_Test_import_in_printexpr()
1134endfunc
1135
1136def Run_Test_import_in_printexpr()
1137 var lines =<< trim END
1138 vim9script
1139 export def PrintFile(): bool
1140 g:printed = 'yes'
1141 delete('v:fname_in')
1142 return false
1143 enddef
1144 END
1145 writefile(lines, 'Xprint.vim')
1146
1147 lines =<< trim END
1148 vim9script
1149 import './Xprint.vim'
1150 set printexpr=Xprint.PrintFile()
1151 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001152 v9.CheckScriptSuccess(lines)
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001153
1154 help
1155 hardcopy dummy args
1156 assert_equal('yes', g:printed)
1157
1158 delete('Xprint.vim')
1159 set printexpr=
1160enddef
1161
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001162def Test_import_in_charconvert()
1163 var lines =<< trim END
1164 vim9script
1165 export def MakeUpper(): bool
1166 var data = readfile(v:fname_in)
1167 map(data, 'toupper(v:val)')
1168 writefile(data, v:fname_out)
1169 return false # success
1170 enddef
1171 END
1172 writefile(lines, 'Xconvert.vim')
1173
1174 lines =<< trim END
1175 vim9script
1176 import './Xconvert.vim' as conv
1177 set charconvert=conv.MakeUpper()
1178 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001179 v9.CheckScriptSuccess(lines)
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001180
1181 writefile(['one', 'two'], 'Xfile')
1182 new Xfile
1183 write ++enc=ucase Xfile1
1184 assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1185
1186 delete('Xfile')
1187 delete('Xfile1')
1188 delete('Xconvert.vim')
1189 bwipe!
1190 set charconvert&
1191enddef
1192
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001193func Test_import_in_spellsuggest_expr()
1194 CheckFeature spell
1195 call Run_Test_import_in_spellsuggest_expr()
1196endfunc
1197
1198def Run_Test_import_in_spellsuggest_expr()
1199 var lines =<< trim END
1200 vim9script
1201 export def MySuggest(): list<any>
1202 return [['Fox', 8], ['Fop', 9]]
1203 enddef
1204 END
1205 writefile(lines, 'Xsuggest.vim')
1206
1207 lines =<< trim END
1208 vim9script
1209 import './Xsuggest.vim' as sugg
1210 set spell spellsuggest=expr:sugg.MySuggest()
1211 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001212 v9.CheckScriptSuccess(lines)
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001213
1214 set verbose=1 # report errors
1215 call assert_equal(['Fox', 'Fop'], spellsuggest('Fo', 2))
1216
1217 delete('Xsuggest.vim')
1218 set nospell spellsuggest& verbose=0
1219enddef
1220
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001221def Test_export_shadows_global_function()
1222 mkdir('Xdir/autoload', 'p')
1223 var save_rtp = &rtp
1224 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1225
1226 var lines =<< trim END
1227 vim9script
1228 export def Shadow(): string
1229 return 'Shadow()'
1230 enddef
1231 END
1232 writefile(lines, 'Xdir/autoload/shadow.vim')
1233
1234 lines =<< trim END
1235 vim9script
1236
1237 def g:Shadow(): string
1238 return 'global'
1239 enddef
1240
1241 import autoload 'shadow.vim'
1242 assert_equal('Shadow()', shadow.Shadow())
1243 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001244 v9.CheckScriptSuccess(lines)
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001245
1246 delfunc g:Shadow
1247 bwipe!
1248 delete('Xdir', 'rf')
1249 &rtp = save_rtp
1250enddef
1251
Bram Moolenaard8448622022-01-07 21:39:52 +00001252def Test_export_fails()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001253 v9.CheckScriptFailure(['export var some = 123'], 'E1042:')
1254 v9.CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
1255 v9.CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001256
1257 assert_fails('export something', 'E1043:')
1258enddef
1259
1260func Test_import_fails_without_script()
1261 CheckRunVimInTerminal
1262
1263 " call indirectly to avoid compilation error for missing functions
1264 call Run_Test_import_fails_on_command_line()
1265endfunc
1266
1267def Run_Test_import_fails_on_command_line()
1268 var export =<< trim END
1269 vim9script
1270 export def Foo(): number
1271 return 0
1272 enddef
1273 END
1274 writefile(export, 'XexportCmd.vim')
1275
Bram Moolenaar62aec932022-01-29 21:45:34 +00001276 var buf = g:RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaard8448622022-01-07 21:39:52 +00001277 rows: 6, wait_for_ruler: 0})
Bram Moolenaar62aec932022-01-29 21:45:34 +00001278 g:WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaard8448622022-01-07 21:39:52 +00001279
1280 delete('XexportCmd.vim')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001281 g:StopVimInTerminal(buf)
Bram Moolenaard8448622022-01-07 21:39:52 +00001282enddef
1283
1284def Test_vim9_reload_noclear()
1285 var lines =<< trim END
1286 vim9script
1287 export var exported = 'thexport'
1288
1289 export def TheFunc(x = 0)
1290 enddef
1291 END
1292 writefile(lines, 'XExportReload')
1293 lines =<< trim END
1294 vim9script noclear
1295 g:loadCount += 1
Bram Moolenaara749a422022-02-12 19:52:25 +00001296 var reloaded = 'init'
Bram Moolenaard8448622022-01-07 21:39:52 +00001297 import './XExportReload' as exp
1298
1299 def Again(): string
1300 return 'again'
1301 enddef
1302
1303 exp.TheFunc()
1304
Bram Moolenaara749a422022-02-12 19:52:25 +00001305 if exists('loaded') | finish | endif
1306 var loaded = true
Bram Moolenaard8448622022-01-07 21:39:52 +00001307
Bram Moolenaara749a422022-02-12 19:52:25 +00001308 var notReloaded = 'yes'
1309 reloaded = 'first'
Bram Moolenaard8448622022-01-07 21:39:52 +00001310 def g:Values(): list<string>
Bram Moolenaara749a422022-02-12 19:52:25 +00001311 return [reloaded, notReloaded, Again(), Once(), exp.exported]
Bram Moolenaard8448622022-01-07 21:39:52 +00001312 enddef
1313
1314 def Once(): string
1315 return 'once'
1316 enddef
1317 END
1318 writefile(lines, 'XReloaded')
1319 g:loadCount = 0
1320 source XReloaded
1321 assert_equal(1, g:loadCount)
1322 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
1323 source XReloaded
1324 assert_equal(2, g:loadCount)
1325 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1326 source XReloaded
1327 assert_equal(3, g:loadCount)
1328 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1329
1330 delete('XReloaded')
1331 delete('XExportReload')
1332 delfunc g:Values
1333 unlet g:loadCount
1334
1335 lines =<< trim END
1336 vim9script
1337 def Inner()
1338 enddef
1339 END
1340 lines->writefile('XreloadScript.vim')
1341 source XreloadScript.vim
1342
1343 lines =<< trim END
1344 vim9script
1345 def Outer()
1346 def Inner()
1347 enddef
1348 enddef
1349 defcompile
1350 END
1351 lines->writefile('XreloadScript.vim')
1352 source XreloadScript.vim
1353
1354 delete('XreloadScript.vim')
1355enddef
1356
Bram Moolenaarcd1cda22022-02-16 21:48:25 +00001357def Test_vim_reload_noclear_arg_count()
1358 var lines =<< trim END
1359 vim9script noclear
1360
1361 if !exists('g:didload')
1362 def Test(a: string, b: string)
1363 echo a b
1364 enddef
1365 def Call()
1366 Test('a', 'b')
1367 enddef
1368 else
1369 # redefine with one argument less
1370 def Test(a: string)
1371 echo a
1372 enddef
1373 endif
1374 Call()
1375 g:didload = 1
1376 END
1377 lines->writefile('XreloadScript_1.vim')
1378 source XreloadScript_1.vim
1379 assert_fails('source XreloadScript_1.vim', 'E1106: One argument too many')
1380 unlet g:didload
1381
1382 lines =<< trim END
1383 vim9script noclear
1384
1385 if !exists('g:didload')
1386 def Test(a: string, b: string, c: string)
1387 echo a b
1388 enddef
1389 def Call()
1390 Test('a', 'b', 'c')
1391 enddef
1392 else
1393 # redefine with one argument less
1394 def Test(a: string)
1395 echo a
1396 enddef
1397 endif
1398 Call()
1399 g:didload = 1
1400 END
1401 lines->writefile('XreloadScript_2.vim')
1402 source XreloadScript_2.vim
1403 assert_fails('source XreloadScript_2.vim', 'E1106: 2 arguments too many')
1404 unlet g:didload
1405
1406 lines =<< trim END
1407 vim9script noclear
1408
1409 if !exists('g:didload')
1410 def Test(a: string)
1411 echo a
1412 enddef
1413 def Call()
1414 Test('a')
1415 enddef
1416 else
1417 # redefine with one argument extra
1418 def Test(a: string, b: string)
1419 echo a b
1420 enddef
1421 endif
1422 Call()
1423 g:didload = 1
1424 END
1425 lines->writefile('XreloadScript_3.vim')
1426 source XreloadScript_3.vim
1427 assert_fails('source XreloadScript_3.vim', 'E1190: One argument too few')
1428 unlet g:didload
1429
1430 lines =<< trim END
1431 vim9script noclear
1432
1433 if !exists('g:didload')
1434 def Test(a: string)
1435 echo a
1436 enddef
1437 def Call()
1438 Test('a')
1439 enddef
1440 else
1441 # redefine with two arguments extra
1442 def Test(a: string, b: string, c: string)
1443 echo a b
1444 enddef
1445 endif
1446 Call()
1447 g:didload = 1
1448 END
1449 lines->writefile('XreloadScript_4.vim')
1450 source XreloadScript_4.vim
1451 assert_fails('source XreloadScript_4.vim', 'E1190: 2 arguments too few')
1452 unlet g:didload
1453
1454 delete('XreloadScript_1.vim')
1455 delete('XreloadScript_2.vim')
1456 delete('XreloadScript_3.vim')
1457 delete('XreloadScript_4.vim')
1458enddef
1459
1460def Test_vim9_reload_noclear_error()
1461 var lines =<< trim END
1462 vim9script noclear
1463
1464 if !exists('g:didload')
1465 def Test(a: string)
1466 echo a
1467 enddef
1468 def Call()
1469 Test('a')
1470 enddef
1471 else
1472 # redefine with a compile error
1473 def Test(a: string)
1474 echo ax
1475 enddef
1476 endif
1477 Call()
1478 g:didload = 1
1479 END
1480 lines->writefile('XreloadScriptErr.vim')
1481 source XreloadScriptErr.vim
1482 assert_fails('source XreloadScriptErr.vim', 'E1001: Variable not found: ax')
1483
1484 unlet g:didload
1485 delete('XreloadScriptErr.vim')
1486enddef
1487
Bram Moolenaard8448622022-01-07 21:39:52 +00001488def Test_vim9_reload_import()
1489 var lines =<< trim END
1490 vim9script
1491 const var = ''
1492 var valone = 1234
1493 def MyFunc(arg: string)
1494 valone = 5678
1495 enddef
1496 END
1497 var morelines =<< trim END
1498 var valtwo = 222
1499 export def GetValtwo(): number
1500 return valtwo
1501 enddef
1502 END
1503 writefile(lines + morelines, 'Xreload.vim')
1504 source Xreload.vim
1505 source Xreload.vim
1506 source Xreload.vim
1507
1508 # cannot declare a var twice
1509 lines =<< trim END
1510 vim9script
1511 var valone = 1234
1512 var valone = 5678
1513 END
1514 writefile(lines, 'Xreload.vim')
1515 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
1516
1517 delete('Xreload.vim')
1518 delete('Ximport.vim')
1519enddef
1520
1521" if a script is reloaded with a script-local variable that changed its type, a
1522" compiled function using that variable must fail.
1523def Test_script_reload_change_type()
1524 var lines =<< trim END
1525 vim9script noclear
1526 var str = 'string'
1527 def g:GetStr(): string
1528 return str .. 'xxx'
1529 enddef
1530 END
1531 writefile(lines, 'Xreload.vim')
1532 source Xreload.vim
1533 echo g:GetStr()
1534
1535 lines =<< trim END
1536 vim9script noclear
1537 var str = 1234
1538 END
1539 writefile(lines, 'Xreload.vim')
1540 source Xreload.vim
1541 assert_fails('echo g:GetStr()', 'E1150:')
1542
1543 delfunc g:GetStr
1544 delete('Xreload.vim')
1545enddef
1546
1547" Define CallFunc so that the test can be compiled
1548command CallFunc echo 'nop'
1549
1550def Test_script_reload_from_function()
1551 var lines =<< trim END
1552 vim9script
1553
1554 if exists('g:loaded')
1555 finish
1556 endif
1557 g:loaded = 1
1558 delcommand CallFunc
1559 command CallFunc Func()
1560 def Func()
1561 so XreloadFunc.vim
1562 g:didTheFunc = 1
1563 enddef
1564 END
1565 writefile(lines, 'XreloadFunc.vim')
1566 source XreloadFunc.vim
1567 CallFunc
1568 assert_equal(1, g:didTheFunc)
1569
1570 delete('XreloadFunc.vim')
1571 delcommand CallFunc
1572 unlet g:loaded
1573 unlet g:didTheFunc
1574enddef
1575
1576def s:RetSome(): string
1577 return 'some'
1578enddef
1579
1580" Not exported function that is referenced needs to be accessed by the
1581" script-local name.
1582def Test_vim9_funcref()
1583 var sortlines =<< trim END
1584 vim9script
1585 def Compare(i1: number, i2: number): number
1586 return i2 - i1
1587 enddef
1588
1589 export def FastSort(): list<number>
1590 return range(5)->sort(Compare)
1591 enddef
1592
1593 export def GetString(arg: string): string
1594 return arg
1595 enddef
1596 END
1597 writefile(sortlines, 'Xsort.vim')
1598
1599 var lines =<< trim END
1600 vim9script
1601 import './Xsort.vim'
1602 def Test()
1603 g:result = Xsort.FastSort()
1604 enddef
1605 Test()
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001606 END
1607 writefile(lines, 'Xscript.vim')
1608 source Xscript.vim
1609 assert_equal([4, 3, 2, 1, 0], g:result)
1610 unlet g:result
Bram Moolenaard8448622022-01-07 21:39:52 +00001611
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001612 lines =<< trim END
1613 vim9script
Bram Moolenaard8448622022-01-07 21:39:52 +00001614 # using a function imported with "as"
1615 import './Xsort.vim' as anAlias
1616 assert_equal('yes', anAlias.GetString('yes'))
1617
1618 # using the function from a compiled function
1619 def TestMore(): string
1620 var s = s:anAlias.GetString('foo')
1621 return s .. anAlias.GetString('bar')
1622 enddef
1623 assert_equal('foobar', TestMore())
1624
1625 # error when using a function that isn't exported
1626 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
1627 END
1628 writefile(lines, 'Xscript.vim')
1629
Bram Moolenaard8448622022-01-07 21:39:52 +00001630 delete('Xsort.vim')
1631 delete('Xscript.vim')
1632
1633 var Funcref = function('s:RetSome')
1634 assert_equal('some', Funcref())
1635enddef
1636
1637" Check that when searching for "FilterFunc" it finds the import in the
1638" script where FastFilter() is called from, both as a string and as a direct
1639" function reference.
1640def Test_vim9_funcref_other_script()
1641 var filterLines =<< trim END
1642 vim9script
1643 export def FilterFunc(idx: number, val: number): bool
1644 return idx % 2 == 1
1645 enddef
1646 export def FastFilter(): list<number>
1647 return range(10)->filter('FilterFunc(v:key, v:val)')
1648 enddef
1649 export def FastFilterDirect(): list<number>
1650 return range(10)->filter(FilterFunc)
1651 enddef
1652 END
1653 writefile(filterLines, 'Xfilter.vim')
1654
1655 var lines =<< trim END
1656 vim9script
1657 import './Xfilter.vim' as filter
1658 def Test()
1659 var x: list<number> = filter.FastFilter()
1660 enddef
1661 Test()
1662 def TestDirect()
1663 var x: list<number> = filter.FastFilterDirect()
1664 enddef
1665 TestDirect()
1666 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001667 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +00001668 delete('Xfilter.vim')
1669enddef
1670
1671def Test_import_absolute()
1672 var import_lines = [
1673 'vim9script',
1674 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
1675 'def UseExported()',
1676 ' g:imported_abs = abs.exported',
1677 ' abs.exported = 8888',
1678 ' g:imported_after = abs.exported',
1679 'enddef',
1680 'UseExported()',
1681 'g:import_disassembled = execute("disass UseExported")',
1682 ]
1683 writefile(import_lines, 'Ximport_abs.vim')
1684 writefile(s:export_script_lines, 'Xexport_abs.vim')
1685
1686 source Ximport_abs.vim
1687
1688 assert_equal(9876, g:imported_abs)
1689 assert_equal(8888, g:imported_after)
1690 assert_match('<SNR>\d\+_UseExported\_s*' ..
1691 'g:imported_abs = abs.exported\_s*' ..
1692 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1693 '1 STOREG g:imported_abs\_s*' ..
1694 'abs.exported = 8888\_s*' ..
1695 '2 PUSHNR 8888\_s*' ..
1696 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1697 'g:imported_after = abs.exported\_s*' ..
1698 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1699 '5 STOREG g:imported_after',
1700 g:import_disassembled)
1701
1702 Undo_export_script_lines()
1703 unlet g:imported_abs
1704 unlet g:import_disassembled
1705
1706 delete('Ximport_abs.vim')
1707 delete('Xexport_abs.vim')
1708enddef
1709
1710def Test_import_rtp()
1711 var import_lines = [
1712 'vim9script',
1713 'import "Xexport_rtp.vim" as rtp',
1714 'g:imported_rtp = rtp.exported',
1715 ]
1716 writefile(import_lines, 'Ximport_rtp.vim')
1717 mkdir('import', 'p')
1718 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1719
1720 var save_rtp = &rtp
1721 &rtp = getcwd()
1722 source Ximport_rtp.vim
1723 &rtp = save_rtp
1724
1725 assert_equal(9876, g:imported_rtp)
1726
1727 Undo_export_script_lines()
1728 unlet g:imported_rtp
1729 delete('Ximport_rtp.vim')
1730 delete('import', 'rf')
1731enddef
1732
1733def Test_import_compile_error()
1734 var export_lines = [
1735 'vim9script',
1736 'export def ExpFunc(): string',
1737 ' return notDefined',
1738 'enddef',
1739 ]
1740 writefile(export_lines, 'Xexported.vim')
1741
1742 var import_lines = [
1743 'vim9script',
1744 'import "./Xexported.vim" as expo',
1745 'def ImpFunc()',
1746 ' echo expo.ExpFunc()',
1747 'enddef',
1748 'defcompile',
1749 ]
1750 writefile(import_lines, 'Ximport.vim')
1751
1752 try
1753 source Ximport.vim
1754 catch /E1001/
1755 # Error should be before the Xexported.vim file.
1756 assert_match('E1001: Variable not found: notDefined', v:exception)
1757 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1758 endtry
1759
1760 delete('Xexported.vim')
1761 delete('Ximport.vim')
1762enddef
1763
1764def Test_func_overrules_import_fails()
1765 var export_lines =<< trim END
1766 vim9script
1767 export def Func()
1768 echo 'imported'
1769 enddef
1770 END
1771 writefile(export_lines, 'XexportedFunc.vim')
1772
1773 var lines =<< trim END
1774 vim9script
1775 import './XexportedFunc.vim' as Func
1776 def Func()
1777 echo 'local to function'
1778 enddef
1779 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001780 v9.CheckScriptFailure(lines, 'E1213: Redefining imported item "Func"')
Bram Moolenaard8448622022-01-07 21:39:52 +00001781
1782 lines =<< trim END
1783 vim9script
1784 import './XexportedFunc.vim' as Func
1785 def Outer()
1786 def Func()
1787 echo 'local to function'
1788 enddef
1789 enddef
1790 defcompile
1791 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001792 v9.CheckScriptFailure(lines, 'E1236:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001793
1794 delete('XexportedFunc.vim')
1795enddef
1796
1797def Test_source_vim9_from_legacy()
1798 var vim9_lines =<< trim END
1799 vim9script
1800 var local = 'local'
1801 g:global = 'global'
1802 export var exported = 'exported'
1803 export def GetText(): string
1804 return 'text'
1805 enddef
1806 END
1807 writefile(vim9_lines, 'Xvim9_script.vim')
1808
1809 var legacy_lines =<< trim END
1810 source Xvim9_script.vim
1811
1812 call assert_false(exists('local'))
1813 call assert_false(exists('exported'))
1814 call assert_false(exists('s:exported'))
1815 call assert_equal('global', global)
1816 call assert_equal('global', g:global)
Bram Moolenaard8448622022-01-07 21:39:52 +00001817 END
1818 writefile(legacy_lines, 'Xlegacy_script.vim')
1819
1820 source Xlegacy_script.vim
1821 assert_equal('global', g:global)
1822 unlet g:global
1823
1824 delete('Xlegacy_script.vim')
1825 delete('Xvim9_script.vim')
1826enddef
1827
Bram Moolenaarc43e6232022-01-13 20:51:56 +00001828def Test_import_vim9_from_legacy()
1829 var vim9_lines =<< trim END
1830 vim9script
1831 var local = 'local'
1832 g:global = 'global'
1833 export var exported = 'exported'
1834 export def GetText(): string
1835 return 'text'
1836 enddef
1837 END
1838 writefile(vim9_lines, 'Xvim9_export.vim')
1839
1840 var legacy_lines =<< trim END
1841 import './Xvim9_export.vim' as vim9
1842
1843 call assert_false(exists('vim9'))
1844 call assert_false(exists('local'))
1845 call assert_false(exists('s:vim9.local'))
1846 call assert_equal('global', global)
1847 call assert_equal('global', g:global)
1848 call assert_false(exists('exported'))
1849 call assert_false(exists('s:exported'))
1850 call assert_false(exists('*GetText'))
1851
1852 " imported symbol is script-local
1853 call assert_equal('exported', s:vim9.exported)
1854 call assert_equal('text', s:vim9.GetText())
1855 END
1856 writefile(legacy_lines, 'Xlegacy_script.vim')
1857
1858 source Xlegacy_script.vim
1859 assert_equal('global', g:global)
1860 unlet g:global
1861
1862 delete('Xlegacy_script.vim')
1863 delete('Xvim9_export.vim')
1864enddef
1865
Bram Moolenaard8448622022-01-07 21:39:52 +00001866def Test_cmdline_win()
1867 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
1868 # the command line window.
1869 mkdir('rtp/syntax', 'p')
1870 var export_lines =<< trim END
1871 vim9script
1872 export var That = 'yes'
1873 END
1874 writefile(export_lines, 'rtp/syntax/Xexport.vim')
1875 var import_lines =<< trim END
1876 vim9script
1877 import './Xexport.vim' as exp
1878 echo exp.That
1879 END
1880 writefile(import_lines, 'rtp/syntax/vim.vim')
1881 var save_rtp = &rtp
1882 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
1883 syntax on
1884 augroup CmdWin
1885 autocmd CmdwinEnter * g:got_there = 'yes'
1886 augroup END
1887 # this will open and also close the cmdline window
1888 feedkeys('q:', 'xt')
1889 assert_equal('yes', g:got_there)
1890
1891 augroup CmdWin
1892 au!
1893 augroup END
1894 &rtp = save_rtp
1895 delete('rtp', 'rf')
1896enddef
1897
1898def Test_import_gone_when_sourced_twice()
1899 var exportlines =<< trim END
1900 vim9script
1901 if exists('g:guard')
1902 finish
1903 endif
1904 g:guard = 1
1905 export var name = 'someName'
1906 END
1907 writefile(exportlines, 'XexportScript.vim')
1908
1909 var lines =<< trim END
1910 vim9script
1911 import './XexportScript.vim' as expo
1912 def g:GetName(): string
1913 return expo.name
1914 enddef
1915 END
1916 writefile(lines, 'XscriptImport.vim')
1917 so XscriptImport.vim
1918 assert_equal('someName', g:GetName())
1919
1920 so XexportScript.vim
1921 assert_fails('call g:GetName()', 'E1149:')
1922
1923 delfunc g:GetName
1924 delete('XexportScript.vim')
1925 delete('XscriptImport.vim')
1926 unlet g:guard
1927enddef
1928
Bram Moolenaar160aa862022-01-10 21:29:57 +00001929" test using an auto-loaded function and variable
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001930def Test_vim9_autoload_full_name()
Bram Moolenaar160aa862022-01-10 21:29:57 +00001931 var lines =<< trim END
1932 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001933 export def Gettest(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00001934 return 'test'
1935 enddef
1936 g:some#name = 'name'
1937 g:some#dict = {key: 'value'}
1938
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001939 export def Varargs(a1: string, ...l: list<string>): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00001940 return a1 .. l[0] .. l[1]
1941 enddef
1942 END
1943
1944 mkdir('Xdir/autoload', 'p')
1945 writefile(lines, 'Xdir/autoload/some.vim')
1946 var save_rtp = &rtp
1947 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1948
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001949 assert_equal('test', g:some#Gettest())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001950 assert_equal('name', g:some#name)
1951 assert_equal('value', g:some#dict.key)
1952 g:some#other = 'other'
1953 assert_equal('other', g:some#other)
1954
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001955 assert_equal('abc', some#Varargs('a', 'b', 'c'))
Bram Moolenaar160aa862022-01-10 21:29:57 +00001956
1957 # upper case script name works
1958 lines =<< trim END
1959 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001960 export def GetOther(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00001961 return 'other'
1962 enddef
1963 END
1964 writefile(lines, 'Xdir/autoload/Other.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001965 assert_equal('other', g:Other#GetOther())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001966
1967 delete('Xdir', 'rf')
1968 &rtp = save_rtp
1969enddef
1970
1971def Test_vim9script_autoload()
1972 mkdir('Xdir/autoload', 'p')
1973 var save_rtp = &rtp
1974 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1975
Bram Moolenaarfd218c82022-01-18 16:26:24 +00001976 # when the path has "/autoload/" prefix is not needed
Bram Moolenaar160aa862022-01-10 21:29:57 +00001977 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00001978 vim9script
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001979 g:prefixed_loaded += 1
Bram Moolenaar160aa862022-01-10 21:29:57 +00001980
1981 export def Gettest(): string
1982 return 'test'
1983 enddef
1984
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001985 export var name = 'name'
1986
1987 export func GetFunc()
1988 return Gettest() .. 'more' .. s:name
Bram Moolenaar160aa862022-01-10 21:29:57 +00001989 endfunc
1990
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001991 export def GetDef(): string
1992 return Gettest() .. 'more' .. name
1993 enddef
1994
Bram Moolenaar160aa862022-01-10 21:29:57 +00001995 export final fname = 'final'
1996 export const cname = 'const'
1997 END
1998 writefile(lines, 'Xdir/autoload/prefixed.vim')
1999
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002000 g:prefixed_loaded = 0
2001 g:expected_loaded = 0
Bram Moolenaar160aa862022-01-10 21:29:57 +00002002 lines =<< trim END
2003 vim9script
2004 import autoload 'prefixed.vim'
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002005 assert_equal(g:expected_loaded, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002006 assert_equal('test', prefixed.Gettest())
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002007 assert_equal(1, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002008
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002009 assert_equal('testmorename', prefixed.GetFunc())
2010 assert_equal('testmorename', prefixed.GetDef())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002011 assert_equal('name', prefixed.name)
2012 assert_equal('final', prefixed.fname)
2013 assert_equal('const', prefixed.cname)
2014 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002015 v9.CheckScriptSuccess(lines)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002016 # can source it again, autoload script not loaded again
2017 g:expected_loaded = 1
Bram Moolenaar62aec932022-01-29 21:45:34 +00002018 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002019
2020 # can also get the items by autoload name
2021 lines =<< trim END
2022 call assert_equal('test', prefixed#Gettest())
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002023 call assert_equal('testmorename', prefixed#GetFunc())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002024 call assert_equal('name', prefixed#name)
2025 call assert_equal('final', prefixed#fname)
2026 call assert_equal('const', prefixed#cname)
2027 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002028 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002029
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002030 unlet g:prefixed_loaded
2031 unlet g:expected_loaded
2032 delete('Xdir', 'rf')
2033 &rtp = save_rtp
2034enddef
2035
Bram Moolenaard02dce22022-01-18 17:43:04 +00002036def Test_import_autoload_not_exported()
2037 mkdir('Xdir/autoload', 'p')
2038 var save_rtp = &rtp
2039 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2040
2041 # error when using an item that is not exported from an autoload script
2042 var exportLines =<< trim END
2043 vim9script
2044 var notExported = 123
2045 def NotExport()
2046 echo 'nop'
2047 enddef
2048 END
2049 writefile(exportLines, 'Xdir/autoload/notExport1.vim')
2050
2051 var lines =<< trim END
2052 vim9script
2053 import autoload 'notExport1.vim'
2054 echo notExport1.notFound
2055 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002056 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002057
2058 lines =<< trim END
2059 vim9script
2060 import autoload 'notExport1.vim'
2061 echo notExport1.notExported
2062 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002063 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002064
2065 lines =<< trim END
2066 vim9script
2067 import autoload 'notExport1.vim'
2068 echo notExport1.NotFunc()
2069 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002070 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002071
2072 lines =<< trim END
2073 vim9script
2074 import autoload 'notExport1.vim'
2075 echo notExport1.NotExport()
2076 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002077 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002078
2079 lines =<< trim END
2080 vim9script
2081 import autoload 'notExport1.vim'
2082 echo 'text'->notExport1.NotFunc()
2083 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002084 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002085
2086 lines =<< trim END
2087 vim9script
2088 import autoload 'notExport1.vim'
2089 echo 'text'->notExport1.NotExport()
2090 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002091 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002092
2093 # using a :def function we use a different autoload script every time so that
2094 # the function is compiled without the script loaded
2095 writefile(exportLines, 'Xdir/autoload/notExport2.vim')
2096 lines =<< trim END
2097 vim9script
2098 import autoload 'notExport2.vim'
2099 def Testit()
2100 echo notExport2.notFound
2101 enddef
2102 Testit()
2103 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002104 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport2#notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002105
2106 writefile(exportLines, 'Xdir/autoload/notExport3.vim')
2107 lines =<< trim END
2108 vim9script
2109 import autoload 'notExport3.vim'
2110 def Testit()
2111 echo notExport3.notExported
2112 enddef
2113 Testit()
2114 END
2115 # don't get E1049 because it is too complicated to figure out
Bram Moolenaar62aec932022-01-29 21:45:34 +00002116 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport3#notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002117
2118 writefile(exportLines, 'Xdir/autoload/notExport4.vim')
2119 lines =<< trim END
2120 vim9script
2121 import autoload 'notExport4.vim'
2122 def Testit()
2123 echo notExport4.NotFunc()
2124 enddef
2125 Testit()
2126 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002127 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport4#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002128
2129 writefile(exportLines, 'Xdir/autoload/notExport5.vim')
2130 lines =<< trim END
2131 vim9script
2132 import autoload 'notExport5.vim'
2133 def Testit()
2134 echo notExport5.NotExport()
2135 enddef
2136 Testit()
2137 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002138 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport5#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002139
2140 writefile(exportLines, 'Xdir/autoload/notExport6.vim')
2141 lines =<< trim END
2142 vim9script
2143 import autoload 'notExport6.vim'
2144 def Testit()
2145 echo 'text'->notExport6.NotFunc()
2146 enddef
2147 Testit()
2148 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002149 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport6#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002150
2151 writefile(exportLines, 'Xdir/autoload/notExport7.vim')
2152 lines =<< trim END
2153 vim9script
2154 import autoload 'notExport7.vim'
2155 def Testit()
2156 echo 'text'->notExport7.NotExport()
2157 enddef
2158 Testit()
2159 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002160 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport7#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002161
2162 delete('Xdir', 'rf')
2163 &rtp = save_rtp
2164enddef
2165
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002166def Test_vim9script_autoload_call()
2167 mkdir('Xdir/autoload', 'p')
2168 var save_rtp = &rtp
2169 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2170
2171 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002172 vim9script
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002173
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002174 export def RetArg(arg: string): string
2175 return arg
2176 enddef
2177
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002178 export def Getother()
2179 g:result = 'other'
2180 enddef
2181 END
Bram Moolenaar5d982692022-01-12 15:15:27 +00002182 writefile(lines, 'Xdir/autoload/another.vim')
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002183
2184 lines =<< trim END
2185 vim9script
Bram Moolenaar5d982692022-01-12 15:15:27 +00002186 import autoload 'another.vim'
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002187
2188 # compile this before 'another.vim' is loaded
2189 def CallAnother()
2190 assert_equal('foo', 'foo'->another.RetArg())
2191 enddef
2192 CallAnother()
2193
Bram Moolenaar5d982692022-01-12 15:15:27 +00002194 call another.Getother()
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002195 assert_equal('other', g:result)
Bram Moolenaar3d8e25a2022-01-22 11:00:02 +00002196
2197 assert_equal('arg', call('another.RetArg', ['arg']))
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00002198
2199 verbose function another.Getother
2200 # should we disallow this?
2201 verbose function another#Getother
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002202 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002203 v9.CheckScriptSuccess(lines)
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002204
2205 unlet g:result
Bram Moolenaar160aa862022-01-10 21:29:57 +00002206 delete('Xdir', 'rf')
2207 &rtp = save_rtp
2208enddef
2209
Bram Moolenaarb697dc22022-01-22 11:27:29 +00002210def Test_vim9script_noclear_autoload()
2211 mkdir('Xdir/autoload', 'p')
2212 var save_rtp = &rtp
2213 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2214
2215 var lines =<< trim END
2216 vim9script
2217 export def Func(): string
2218 return 'called'
2219 enddef
2220 g:double_loaded = 'yes'
2221 END
2222 writefile(lines, 'Xdir/autoload/double.vim')
2223
2224 lines =<< trim END
2225 vim9script noclear
2226 if exists('g:script_loaded')
2227 finish
2228 endif
2229 g:script_loaded = true
2230
2231 import autoload 'double.vim'
2232 nnoremap <F3> <ScriptCmd>g:result = double.Func()<CR>
2233 END
2234 g:double_loaded = 'no'
2235 writefile(lines, 'Xloaddouble')
2236 source Xloaddouble
2237 assert_equal('no', g:double_loaded)
2238 assert_equal(true, g:script_loaded)
2239 source Xloaddouble
2240 feedkeys("\<F3>", 'xt')
2241 assert_equal('called', g:result)
2242 assert_equal('yes', g:double_loaded)
2243
2244 delete('Xloaddouble')
2245 unlet g:double_loaded
2246 unlet g:script_loaded
2247 unlet g:result
2248 delete('Xdir', 'rf')
2249 &rtp = save_rtp
2250enddef
2251
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002252def Test_vim9script_autoload_duplicate()
2253 mkdir('Xdir/autoload', 'p')
2254
2255 var lines =<< trim END
2256 vim9script
2257
2258 export def Func()
2259 enddef
2260
2261 def Func()
2262 enddef
2263 END
2264 writefile(lines, 'Xdir/autoload/dupfunc.vim')
2265 assert_fails('source Xdir/autoload/dupfunc.vim', 'E1073:')
2266
2267 lines =<< trim END
2268 vim9script
2269
2270 def Func()
2271 enddef
2272
2273 export def Func()
2274 enddef
2275 END
2276 writefile(lines, 'Xdir/autoload/dup2func.vim')
2277 assert_fails('source Xdir/autoload/dup2func.vim', 'E1073:')
2278
2279 lines =<< trim END
2280 vim9script
2281
2282 def Func()
2283 enddef
2284
2285 export var Func = 'asdf'
2286 END
2287 writefile(lines, 'Xdir/autoload/dup3func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002288 assert_fails('source Xdir/autoload/dup3func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002289
2290 lines =<< trim END
2291 vim9script
2292
2293 export var Func = 'asdf'
2294
2295 def Func()
2296 enddef
2297 END
2298 writefile(lines, 'Xdir/autoload/dup4func.vim')
2299 assert_fails('source Xdir/autoload/dup4func.vim', 'E707:')
2300
2301 lines =<< trim END
2302 vim9script
2303
2304 var Func = 'asdf'
2305
2306 export def Func()
2307 enddef
2308 END
2309 writefile(lines, 'Xdir/autoload/dup5func.vim')
2310 assert_fails('source Xdir/autoload/dup5func.vim', 'E707:')
2311
2312 lines =<< trim END
2313 vim9script
2314
2315 export def Func()
2316 enddef
2317
2318 var Func = 'asdf'
2319 END
2320 writefile(lines, 'Xdir/autoload/dup6func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002321 assert_fails('source Xdir/autoload/dup6func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002322
2323 delete('Xdir', 'rf')
2324enddef
2325
Bram Moolenaar2017d6f2022-01-20 19:38:46 +00002326def Test_autoload_missing_function_name()
2327 mkdir('Xdir/autoload', 'p')
2328
2329 var lines =<< trim END
2330 vim9script
2331
2332 def loadme#()
2333 enddef
2334 END
2335 writefile(lines, 'Xdir/autoload/loadme.vim')
2336 assert_fails('source Xdir/autoload/loadme.vim', 'E129:')
2337
2338 delete('Xdir', 'rf')
2339enddef
2340
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002341def Test_autoload_name_wrong()
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002342 var lines =<< trim END
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002343 def Xscriptname#Func()
2344 enddef
2345 END
2346 writefile(lines, 'Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002347 v9.CheckScriptFailure(lines, 'E746:')
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00002348 delete('Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002349
2350 mkdir('Xdir/autoload', 'p')
2351 lines =<< trim END
2352 vim9script
2353 def somescript#Func()
2354 enddef
2355 END
2356 writefile(lines, 'Xdir/autoload/somescript.vim')
2357 assert_fails('source Xdir/autoload/somescript.vim', 'E1263:')
2358
2359 delete('Xdir', 'rf')
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002360enddef
2361
Bram Moolenaard041f422022-01-12 19:54:00 +00002362def Test_import_autoload_postponed()
2363 mkdir('Xdir/autoload', 'p')
2364 var save_rtp = &rtp
2365 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2366
2367 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002368 vim9script
Bram Moolenaard041f422022-01-12 19:54:00 +00002369
2370 g:loaded_postponed = 'true'
2371 export var variable = 'bla'
2372 export def Function(): string
2373 return 'bla'
2374 enddef
2375 END
2376 writefile(lines, 'Xdir/autoload/postponed.vim')
2377
2378 lines =<< trim END
2379 vim9script
2380
2381 import autoload 'postponed.vim'
2382 def Tryit()
2383 echo postponed.variable
2384 echo postponed.Function()
2385 enddef
2386 defcompile
2387 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002388 v9.CheckScriptSuccess(lines)
Bram Moolenaard041f422022-01-12 19:54:00 +00002389 assert_false(exists('g:loaded_postponed'))
Bram Moolenaar62aec932022-01-29 21:45:34 +00002390 v9.CheckScriptSuccess(lines + ['Tryit()'])
Bram Moolenaard041f422022-01-12 19:54:00 +00002391 assert_equal('true', g:loaded_postponed)
2392
2393 unlet g:loaded_postponed
2394 delete('Xdir', 'rf')
2395 &rtp = save_rtp
2396enddef
2397
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002398def Test_import_autoload_override()
2399 mkdir('Xdir/autoload', 'p')
2400 var save_rtp = &rtp
2401 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2402 test_override('autoload', 1)
2403
2404 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002405 vim9script
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002406
2407 g:loaded_override = 'true'
2408 export var variable = 'bla'
2409 export def Function(): string
2410 return 'bla'
2411 enddef
2412 END
2413 writefile(lines, 'Xdir/autoload/override.vim')
2414
2415 lines =<< trim END
2416 vim9script
2417
2418 import autoload 'override.vim'
2419 assert_equal('true', g:loaded_override)
2420
2421 def Tryit()
2422 echo override.doesNotExist
2423 enddef
2424 defcompile
2425 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002426 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1)
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002427
2428 test_override('autoload', 0)
2429 unlet g:loaded_override
2430 delete('Xdir', 'rf')
2431 &rtp = save_rtp
2432enddef
2433
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002434def Test_autoload_mapping()
2435 mkdir('Xdir/autoload', 'p')
2436 var save_rtp = &rtp
2437 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2438
2439 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002440 vim9script
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002441
2442 g:toggle_loaded = 'yes'
2443
2444 export def Toggle(): string
2445 return ":g:toggle_called = 'yes'\<CR>"
2446 enddef
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002447 export def Doit()
2448 g:doit_called = 'yes'
2449 enddef
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002450 END
2451 writefile(lines, 'Xdir/autoload/toggle.vim')
2452
2453 lines =<< trim END
2454 vim9script
2455
2456 import autoload 'toggle.vim'
2457
2458 nnoremap <silent> <expr> tt toggle.Toggle()
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002459 nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR>
2460 nnoremap <silent> yy <Cmd>toggle.Doit()<CR>
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002461 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002462 v9.CheckScriptSuccess(lines)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002463 assert_false(exists("g:toggle_loaded"))
2464 assert_false(exists("g:toggle_called"))
Bram Moolenaar6079da72022-01-18 14:16:59 +00002465 assert_match('\d A: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002466
2467 feedkeys("tt", 'xt')
2468 assert_equal('yes', g:toggle_loaded)
2469 assert_equal('yes', g:toggle_called)
Bram Moolenaar6079da72022-01-18 14:16:59 +00002470 assert_match('\d: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002471
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002472 feedkeys("xx", 'xt')
2473 assert_equal('yes', g:doit_called)
2474
2475 assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle')
2476
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002477 nunmap tt
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002478 nunmap xx
2479 nunmap yy
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002480 unlet g:toggle_loaded
2481 unlet g:toggle_called
2482 delete('Xdir', 'rf')
2483 &rtp = save_rtp
2484enddef
2485
Bram Moolenaar160aa862022-01-10 21:29:57 +00002486def Test_vim9script_autoload_fails()
2487 var lines =<< trim END
2488 vim9script autoload
2489 var n = 0
2490 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002491 v9.CheckScriptFailure(lines, 'E475: Invalid argument: autoload')
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002492
2493 lines =<< trim END
2494 vim9script noclear noclear
2495 var n = 0
2496 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002497 v9.CheckScriptFailure(lines, 'E983: Duplicate argument: noclear')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002498enddef
2499
2500def Test_import_autoload_fails()
2501 var lines =<< trim END
2502 vim9script
2503 import autoload autoload 'prefixed.vim'
2504 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002505 v9.CheckScriptFailure(lines, 'E121: Undefined variable: autoload')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002506
2507 lines =<< trim END
2508 vim9script
Bram Moolenaar1836d612022-01-18 13:14:47 +00002509 import autoload './doesNotExist.vim'
Bram Moolenaar160aa862022-01-10 21:29:57 +00002510 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002511 v9.CheckScriptFailure(lines, 'E282:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002512
2513 lines =<< trim END
2514 vim9script
2515 import autoload '/dir/doesNotExist.vim'
2516 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002517 v9.CheckScriptFailure(lines, 'E282:', 2)
2518
2519 lines =<< trim END
2520 vim9script
2521 import autoload '../testdir'
2522 END
2523 v9.CheckScriptFailure(lines, 'E17:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002524
2525 lines =<< trim END
2526 vim9script
2527 import autoload 'doesNotExist.vim'
2528 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002529 v9.CheckScriptFailure(lines, 'E1053: Could not import "doesNotExist.vim"')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002530enddef
2531
2532" test disassembling an auto-loaded function starting with "debug"
2533def Test_vim9_autoload_disass()
2534 mkdir('Xdir/autoload', 'p')
2535 var save_rtp = &rtp
2536 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2537
2538 var lines =<< trim END
2539 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002540 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002541 return 'debug'
2542 enddef
2543 END
2544 writefile(lines, 'Xdir/autoload/debugit.vim')
2545
2546 lines =<< trim END
2547 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002548 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002549 return 'profile'
2550 enddef
2551 END
2552 writefile(lines, 'Xdir/autoload/profileit.vim')
2553
2554 lines =<< trim END
2555 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002556 assert_equal('debug', debugit#Test())
2557 disass debugit#Test
2558 assert_equal('profile', profileit#Test())
2559 disass profileit#Test
Bram Moolenaar160aa862022-01-10 21:29:57 +00002560 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002561 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002562
2563 delete('Xdir', 'rf')
2564 &rtp = save_rtp
2565enddef
2566
2567" test using a vim9script that is auto-loaded from an autocmd
2568def Test_vim9_aucmd_autoload()
2569 var lines =<< trim END
2570 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002571 export def Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002572 echomsg getreg('"')
2573 enddef
2574 END
2575
2576 mkdir('Xdir/autoload', 'p')
2577 writefile(lines, 'Xdir/autoload/foo.vim')
2578 var save_rtp = &rtp
2579 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2580 augroup test
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002581 autocmd TextYankPost * call foo#Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002582 augroup END
2583
2584 normal Y
2585
2586 augroup test
2587 autocmd!
2588 augroup END
2589 delete('Xdir', 'rf')
2590 &rtp = save_rtp
2591enddef
2592
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002593" test using a autoloaded file that is case sensitive
2594def Test_vim9_autoload_case_sensitive()
2595 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002596 vim9script
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002597 export def CaseSensitive(): string
2598 return 'done'
2599 enddef
2600 END
2601
2602 mkdir('Xdir/autoload', 'p')
2603 writefile(lines, 'Xdir/autoload/CaseSensitive.vim')
2604 var save_rtp = &rtp
2605 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2606
2607 lines =<< trim END
2608 vim9script
2609 import autoload 'CaseSensitive.vim'
2610 assert_equal('done', CaseSensitive.CaseSensitive())
2611 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002612 v9.CheckScriptSuccess(lines)
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002613
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002614 if !has('fname_case')
2615 lines =<< trim END
2616 vim9script
2617 import autoload 'CaseSensitive.vim'
2618 import autoload 'casesensitive.vim'
2619 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002620 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002621 endif
2622
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002623 delete('Xdir', 'rf')
2624 &rtp = save_rtp
2625enddef
2626
Bram Moolenaar160aa862022-01-10 21:29:57 +00002627" This was causing a crash because suppress_errthrow wasn't reset.
2628def Test_vim9_autoload_error()
2629 var lines =<< trim END
2630 vim9script
2631 def crash#func()
2632 try
2633 for x in List()
2634 endfor
2635 catch
2636 endtry
2637 g:ok = true
2638 enddef
2639 fu List()
2640 invalid
2641 endfu
2642 try
2643 alsoinvalid
2644 catch /wontmatch/
2645 endtry
2646 END
2647 call mkdir('Xruntime/autoload', 'p')
2648 call writefile(lines, 'Xruntime/autoload/crash.vim')
2649
2650 # run in a separate Vim to avoid the side effects of assert_fails()
2651 lines =<< trim END
2652 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
2653 call crash#func()
2654 call writefile(['ok'], 'Xdidit')
2655 qall!
2656 END
2657 writefile(lines, 'Xscript')
Bram Moolenaar62aec932022-01-29 21:45:34 +00002658 g:RunVim([], [], '-S Xscript')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002659 assert_equal(['ok'], readfile('Xdidit'))
2660
2661 delete('Xdidit')
2662 delete('Xscript')
2663 delete('Xruntime', 'rf')
2664
2665 lines =<< trim END
2666 vim9script
2667 var foo#bar = 'asdf'
2668 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002669 v9.CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002670enddef
2671
Bram Moolenaard8448622022-01-07 21:39:52 +00002672
2673" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker