blob: be453c654eeef30c99f1dd1c26eab98bb32f8518 [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
426def Test_import_fails()
427 writefile([], 'Xfoo.vim')
428 var lines =<< trim END
429 import './Xfoo.vim' as foo
430 foo = 'bar'
431 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000432 v9.CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use foo itself'])
Bram Moolenaard8448622022-01-07 21:39:52 +0000433 lines =<< trim END
434 vim9script
435 import './Xfoo.vim' as foo
436 var that = foo
437 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000438 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaardd5893b2022-01-20 21:32:54 +0000439 lines =<< trim END
440 vim9script
441 import './Xfoo.vim' as foo
442 var that: any
443 that += foo
444 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000445 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaardd5893b2022-01-20 21:32:54 +0000446 lines =<< trim END
447 vim9script
448 import './Xfoo.vim' as foo
449 foo += 9
450 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000451 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaard8448622022-01-07 21:39:52 +0000452
453 lines =<< trim END
454 vim9script
455 import './Xfoo.vim' as 9foo
456 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000457 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000458 lines =<< trim END
459 vim9script
460 import './Xfoo.vim' as the#foo
461 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000462 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000463 lines =<< trim END
464 vim9script
465 import './Xfoo.vim' as g:foo
466 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000467 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000468
469 delete('Xfoo.vim')
470
471 lines =<< trim END
472 vim9script
473 def TheFunc()
474 echo 'the func'
475 enddef
476 export var Ref = TheFunc
477 END
478 writefile([], 'Xthat.vim')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000479
Bram Moolenaard8448622022-01-07 21:39:52 +0000480 lines =<< trim END
481 import './Xthat.vim' as That
482 That()
483 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000484 v9.CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use That itself'])
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000485
486 lines =<< trim END
Bram Moolenaar937610b2022-01-19 17:21:29 +0000487 vim9script
488 import './Xthat.vim' as That
489 def Func()
490 echo That()
491 enddef
492 Func()
493 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000494 v9.CheckScriptFailure(lines, 'E1236: Cannot use That itself')
Bram Moolenaar937610b2022-01-19 17:21:29 +0000495
496 lines =<< trim END
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000497 import './Xthat.vim' as one
498 import './Xthat.vim' as two
499 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000500 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000501
502 delete('Xthat.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000503
504 mkdir('Ximport')
505
506 writefile(['vim9script'], 'Ximport/.vim')
507 lines =<< trim END
508 vim9script
509 import './Ximport/.vim'
510 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000511 v9.CheckScriptFailure(lines, 'E1261: Cannot import .vim without using "as"')
Bram Moolenaard8448622022-01-07 21:39:52 +0000512 lines =<< trim END
513 vim9script
514 import './Ximport/.vim' as vim
515 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000516 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000517
518 writefile(['vim9script'], 'Ximport/.vimrc')
519 lines =<< trim END
520 vim9script
521 import './Ximport/.vimrc'
522 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000523 v9.CheckScriptFailure(lines, 'E1257: Imported script must use "as" or end in .vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000524 lines =<< trim END
525 vim9script
526 import './Ximport/.vimrc' as vimrc
527 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000528 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000529
530 delete('Ximport', 'rf')
531enddef
532
533func g:Trigger()
534 source Ximport.vim
535 return "echo 'yes'\<CR>"
536endfunc
537
538def Test_import_export_expr_map()
539 # check that :import and :export work when buffer is locked
540 var export_lines =<< trim END
541 vim9script
542 export def That(): string
543 return 'yes'
544 enddef
545 END
546 writefile(export_lines, 'Xexport_that.vim')
547
548 var import_lines =<< trim END
549 vim9script
550 import './Xexport_that.vim' as that
551 assert_equal('yes', that.That())
552 END
553 writefile(import_lines, 'Ximport.vim')
554
555 nnoremap <expr> trigger g:Trigger()
556 feedkeys('trigger', "xt")
557
558 delete('Xexport_that.vim')
559 delete('Ximport.vim')
560 nunmap trigger
561enddef
562
563def Test_import_in_filetype()
564 # check that :import works when the buffer is locked
565 mkdir('ftplugin', 'p')
566 var export_lines =<< trim END
567 vim9script
568 export var That = 'yes'
569 END
570 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
571
572 var import_lines =<< trim END
573 vim9script
574 import './Xexport_ft.vim' as ft
575 assert_equal('yes', ft.That)
576 g:did_load_mytpe = 1
577 END
578 writefile(import_lines, 'ftplugin/qf.vim')
579
580 var save_rtp = &rtp
581 &rtp = getcwd() .. ',' .. &rtp
582
583 filetype plugin on
584 copen
585 assert_equal(1, g:did_load_mytpe)
586
587 quit!
588 delete('Xexport_ft.vim')
589 delete('ftplugin', 'rf')
590 &rtp = save_rtp
591enddef
592
593def Test_use_import_in_mapping()
594 var lines =<< trim END
595 vim9script
596 export def Funcx()
597 g:result = 42
598 enddef
599 END
600 writefile(lines, 'XsomeExport.vim')
601 lines =<< trim END
602 vim9script
603 import './XsomeExport.vim' as some
604 var Funcy = some.Funcx
605 nnoremap <F3> :call <sid>Funcy()<cr>
606 END
607 writefile(lines, 'Xmapscript.vim')
608
609 source Xmapscript.vim
610 feedkeys("\<F3>", "xt")
611 assert_equal(42, g:result)
612
613 unlet g:result
614 delete('XsomeExport.vim')
615 delete('Xmapscript.vim')
616 nunmap <F3>
617enddef
618
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000619def Test_use_import_in_command_completion()
Bram Moolenaar15d16352022-01-17 20:09:08 +0000620 var lines =<< trim END
621 vim9script
622 export def Complete(..._): list<string>
623 return ['abcd']
624 enddef
625 END
626 writefile(lines, 'Xscript.vim')
627
628 lines =<< trim END
629 vim9script
630 import './Xscript.vim'
631
632 command -nargs=1 -complete=customlist,Xscript.Complete Cmd echo 'ok'
633 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
634 assert_equal('#Cmd abcd', @:)
635 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000636 v9.CheckScriptSuccess(lines)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000637
638 delcommand Cmd
639 delete('Xscript.vim')
640enddef
641
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000642def Test_use_autoload_import_in_insert_completion()
643 mkdir('Xdir/autoload', 'p')
644 var save_rtp = &rtp
645 exe 'set rtp^=' .. getcwd() .. '/Xdir'
646
647 var lines =<< trim END
648 vim9script
649 export def ThesaurusFunc(findbase: bool, _): any
650 if findbase
651 return 1
652 endif
653 return [
654 'check',
655 'experiment',
656 'test',
657 'verification'
658 ]
659 enddef
660 g:completion_loaded = 'yes'
661 END
662 writefile(lines, 'Xdir/autoload/completion.vim')
663
664 new
665 lines =<< trim END
666 vim9script
667 g:completion_loaded = 'no'
668 import autoload 'completion.vim'
669 set thesaurusfunc=completion.ThesaurusFunc
670 assert_equal('no', g:completion_loaded)
671 feedkeys("i\<C-X>\<C-T>\<C-N>\<Esc>", 'xt')
672 assert_equal('experiment', getline(1))
673 assert_equal('yes', g:completion_loaded)
674 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000675 v9.CheckScriptSuccess(lines)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000676
677 set thesaurusfunc=
678 bwipe!
679 delete('Xdir', 'rf')
680 &rtp = save_rtp
681enddef
682
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000683def Test_use_autoload_import_partial_in_opfunc()
684 mkdir('Xdir/autoload', 'p')
685 var save_rtp = &rtp
686 exe 'set rtp^=' .. getcwd() .. '/Xdir'
687
688 var lines =<< trim END
689 vim9script
690 export def Opfunc(..._)
691 g:opfunc_called = 'yes'
692 enddef
693 END
694 writefile(lines, 'Xdir/autoload/opfunc.vim')
695
696 new
697 lines =<< trim END
698 vim9script
699 import autoload 'opfunc.vim'
700 nnoremap <expr> <F3> TheFunc()
701 def TheFunc(): string
702 &operatorfunc = function('opfunc.Opfunc', [0])
703 return 'g@'
704 enddef
705 feedkeys("\<F3>l", 'xt')
706 assert_equal('yes', g:opfunc_called)
707 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000708 v9.CheckScriptSuccess(lines)
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000709
710 set opfunc=
711 bwipe!
712 delete('Xdir', 'rf')
Bram Moolenaar06b77222022-01-25 15:51:56 +0000713 nunmap <F3>
714 &rtp = save_rtp
715enddef
716
717def Test_set_opfunc_to_autoload_func_directly()
718 mkdir('Xdir/autoload', 'p')
719 var save_rtp = &rtp
720 exe 'set rtp^=' .. getcwd() .. '/Xdir'
721
722 var lines =<< trim END
723 vim9script
724 export def Opfunc(..._)
725 g:opfunc_called = 'yes'
726 enddef
727 END
728 writefile(lines, 'Xdir/autoload/opfunc.vim')
729
730 new
731 lines =<< trim END
732 vim9script
733 import autoload 'opfunc.vim'
734 nnoremap <expr> <F3> TheFunc()
735 def TheFunc(): string
736 &operatorfunc = opfunc.Opfunc
737 return 'g@'
738 enddef
739 feedkeys("\<F3>l", 'xt')
740 assert_equal('yes', g:opfunc_called)
741 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000742 v9.CheckScriptSuccess(lines)
Bram Moolenaar06b77222022-01-25 15:51:56 +0000743
744 set opfunc=
745 bwipe!
746 delete('Xdir', 'rf')
747 nunmap <F3>
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000748 &rtp = save_rtp
749enddef
750
Bram Moolenaare70dd112022-01-21 16:31:11 +0000751def Test_use_autoload_import_in_fold_expression()
752 mkdir('Xdir/autoload', 'p')
753 var save_rtp = &rtp
754 exe 'set rtp^=' .. getcwd() .. '/Xdir'
755
756 var lines =<< trim END
757 vim9script
758 export def Expr(): string
759 return getline(v:lnum) =~ '^#' ? '>1' : '1'
760 enddef
Bram Moolenaar9530b582022-01-22 13:39:08 +0000761 export def Text(): string
762 return 'fold text'
763 enddef
Bram Moolenaare70dd112022-01-21 16:31:11 +0000764 g:fold_loaded = 'yes'
765 END
766 writefile(lines, 'Xdir/autoload/fold.vim')
767
768 lines =<< trim END
769 vim9script
770 import autoload 'fold.vim'
771 &foldexpr = 'fold.Expr()'
Bram Moolenaar9530b582022-01-22 13:39:08 +0000772 &foldtext = 'fold.Text()'
Bram Moolenaare70dd112022-01-21 16:31:11 +0000773 &foldmethod = 'expr'
774 &debug = 'throw'
775 END
776 new
777 setline(1, ['# one', 'text', '# two', 'text'])
778 g:fold_loaded = 'no'
Bram Moolenaar62aec932022-01-29 21:45:34 +0000779 v9.CheckScriptSuccess(lines)
Bram Moolenaare70dd112022-01-21 16:31:11 +0000780 assert_equal('no', g:fold_loaded)
781 redraw
782 assert_equal('yes', g:fold_loaded)
783
784 # Check that script context of 'foldexpr' is copied to another buffer.
785 edit! otherfile
786 redraw
787
Bram Moolenaar9530b582022-01-22 13:39:08 +0000788 set foldexpr= foldtext& foldmethod& debug=
Bram Moolenaare70dd112022-01-21 16:31:11 +0000789 bwipe!
790 delete('Xdir', 'rf')
791 &rtp = save_rtp
792enddef
793
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000794func Test_import_in_diffexpr()
795 CheckExecutable diff
796
797 call Run_Test_import_in_diffexpr()
798endfunc
799
800def Run_Test_import_in_diffexpr()
801 var lines =<< trim END
802 vim9script
803
804 export def DiffExpr()
805 # Prepend some text to check diff type detection
806 writefile(['warning', ' message'], v:fname_out)
807 silent exe '!diff ' .. v:fname_in .. ' '
808 .. v:fname_new .. '>>' .. v:fname_out
809 enddef
810 END
811 writefile(lines, 'Xdiffexpr')
812
813 lines =<< trim END
814 vim9script
815 import './Xdiffexpr' as diff
816
817 set diffexpr=diff.DiffExpr()
818 set diffopt=foldcolumn:0
819 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000820 v9.CheckScriptSuccess(lines)
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000821
822 enew!
823 call setline(1, ['one', 'two', 'three'])
824 diffthis
825
826 botright vert new
827 call setline(1, ['one', 'two', 'three.'])
828 diffthis
829 # we only check if this does not cause errors
830 redraw
831
832 diffoff!
833 bwipe!
834 bwipe!
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +0000835 delete('Xdiffexpr')
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000836enddef
837
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000838def Test_import_in_patchexpr()
839 var lines =<< trim END
840 vim9script
841 export def TPatch()
842 call writefile(['output file'], v:fname_out)
843 enddef
844 END
845 writefile(lines, 'Xpatchexpr')
846
847 lines =<< trim END
848 vim9script
849 import './Xpatchexpr' as patch
850 set patchexpr=patch.TPatch()
851 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000852 v9.CheckScriptSuccess(lines)
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000853
854 call writefile(['input file'], 'Xinput')
855 call writefile(['diff file'], 'Xdiff')
856 :%bwipe!
857 edit Xinput
858 diffpatch Xdiff
859 call assert_equal('output file', getline(1))
860
861 call delete('Xinput')
862 call delete('Xdiff')
863 call delete('Xpatchexpr')
864 set patchexpr&
865 :%bwipe!
866enddef
867
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000868def Test_import_in_formatexpr()
869 var lines =<< trim END
870 vim9script
871 export def MyFormatExpr(): number
872 g:did_format = 'yes'
873 return 0
874 enddef
875 END
876 writefile(lines, 'Xformatter')
877
878 lines =<< trim END
879 vim9script
880 import './Xformatter' as format
881 set formatexpr=format.MyFormatExpr()
882 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000883 v9.CheckScriptSuccess(lines)
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000884
885 new
886 setline(1, ['a', 'b', 'c'])
887 normal gqG
888 assert_equal('yes', g:did_format)
889
890 bwipe!
891 delete('Xformatter')
892 unlet g:did_format
893 set formatexpr=
894enddef
895
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +0000896def Test_import_in_includeexpr()
897 writefile(['found it'], 'Xthisfile')
898 new
899
900 var lines =<< trim END
901 vim9script
902 export def DoSub(): string
903 return substitute(v:fname, 'that', 'this', '')
904 enddef
905 END
906 writefile(lines, 'Xinclude.vim')
907
908 lines =<< trim END
909 vim9script
910 import './Xinclude.vim'
911 set includeexpr=Xinclude.DoSub()
912 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000913 v9.CheckScriptSuccess(lines)
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +0000914
915 setline(1, ['Xthatfile'])
916 exe "normal \<C-W>f"
917 assert_equal('Xthisfile', expand('%'))
918
919 bwipe!
920 bwipe!
921 set includeexpr=
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +0000922 delete('Xinclude.vim')
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +0000923 delete('Xthisfile')
924enddef
925
Bram Moolenaar28e60cc2022-01-22 20:32:00 +0000926def Test_import_in_indentexpr()
927 var lines =<< trim END
928 vim9script
929 export def GetIndent(): number
930 return 5
931 enddef
932 END
933 writefile(lines, 'Xindenter')
934
935 lines =<< trim END
936 vim9script
937 import './Xindenter' as indent
938 set indentexpr=indent.GetIndent()
939 set debug=throw
940 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000941 v9.CheckScriptSuccess(lines)
Bram Moolenaar28e60cc2022-01-22 20:32:00 +0000942
943 new
944 setline(1, 'hello')
945 normal ==
946 assert_equal(' hello', getline(1))
947
948 bwipe!
949 set indentexpr= debug=
950 delete('Xindenter')
951enddef
952
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000953func Test_import_in_printexpr()
954 CheckFeature postscript
955 call Run_Test_import_in_printexpr()
956endfunc
957
958def Run_Test_import_in_printexpr()
959 var lines =<< trim END
960 vim9script
961 export def PrintFile(): bool
962 g:printed = 'yes'
963 delete('v:fname_in')
964 return false
965 enddef
966 END
967 writefile(lines, 'Xprint.vim')
968
969 lines =<< trim END
970 vim9script
971 import './Xprint.vim'
972 set printexpr=Xprint.PrintFile()
973 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000974 v9.CheckScriptSuccess(lines)
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000975
976 help
977 hardcopy dummy args
978 assert_equal('yes', g:printed)
979
980 delete('Xprint.vim')
981 set printexpr=
982enddef
983
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000984def Test_import_in_charconvert()
985 var lines =<< trim END
986 vim9script
987 export def MakeUpper(): bool
988 var data = readfile(v:fname_in)
989 map(data, 'toupper(v:val)')
990 writefile(data, v:fname_out)
991 return false # success
992 enddef
993 END
994 writefile(lines, 'Xconvert.vim')
995
996 lines =<< trim END
997 vim9script
998 import './Xconvert.vim' as conv
999 set charconvert=conv.MakeUpper()
1000 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001001 v9.CheckScriptSuccess(lines)
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001002
1003 writefile(['one', 'two'], 'Xfile')
1004 new Xfile
1005 write ++enc=ucase Xfile1
1006 assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1007
1008 delete('Xfile')
1009 delete('Xfile1')
1010 delete('Xconvert.vim')
1011 bwipe!
1012 set charconvert&
1013enddef
1014
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001015func Test_import_in_spellsuggest_expr()
1016 CheckFeature spell
1017 call Run_Test_import_in_spellsuggest_expr()
1018endfunc
1019
1020def Run_Test_import_in_spellsuggest_expr()
1021 var lines =<< trim END
1022 vim9script
1023 export def MySuggest(): list<any>
1024 return [['Fox', 8], ['Fop', 9]]
1025 enddef
1026 END
1027 writefile(lines, 'Xsuggest.vim')
1028
1029 lines =<< trim END
1030 vim9script
1031 import './Xsuggest.vim' as sugg
1032 set spell spellsuggest=expr:sugg.MySuggest()
1033 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001034 v9.CheckScriptSuccess(lines)
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001035
1036 set verbose=1 # report errors
1037 call assert_equal(['Fox', 'Fop'], spellsuggest('Fo', 2))
1038
1039 delete('Xsuggest.vim')
1040 set nospell spellsuggest& verbose=0
1041enddef
1042
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001043def Test_export_shadows_global_function()
1044 mkdir('Xdir/autoload', 'p')
1045 var save_rtp = &rtp
1046 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1047
1048 var lines =<< trim END
1049 vim9script
1050 export def Shadow(): string
1051 return 'Shadow()'
1052 enddef
1053 END
1054 writefile(lines, 'Xdir/autoload/shadow.vim')
1055
1056 lines =<< trim END
1057 vim9script
1058
1059 def g:Shadow(): string
1060 return 'global'
1061 enddef
1062
1063 import autoload 'shadow.vim'
1064 assert_equal('Shadow()', shadow.Shadow())
1065 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001066 v9.CheckScriptSuccess(lines)
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001067
1068 delfunc g:Shadow
1069 bwipe!
1070 delete('Xdir', 'rf')
1071 &rtp = save_rtp
1072enddef
1073
Bram Moolenaard8448622022-01-07 21:39:52 +00001074def Test_export_fails()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001075 v9.CheckScriptFailure(['export var some = 123'], 'E1042:')
1076 v9.CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
1077 v9.CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001078
1079 assert_fails('export something', 'E1043:')
1080enddef
1081
1082func Test_import_fails_without_script()
1083 CheckRunVimInTerminal
1084
1085 " call indirectly to avoid compilation error for missing functions
1086 call Run_Test_import_fails_on_command_line()
1087endfunc
1088
1089def Run_Test_import_fails_on_command_line()
1090 var export =<< trim END
1091 vim9script
1092 export def Foo(): number
1093 return 0
1094 enddef
1095 END
1096 writefile(export, 'XexportCmd.vim')
1097
Bram Moolenaar62aec932022-01-29 21:45:34 +00001098 var buf = g:RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaard8448622022-01-07 21:39:52 +00001099 rows: 6, wait_for_ruler: 0})
Bram Moolenaar62aec932022-01-29 21:45:34 +00001100 g:WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaard8448622022-01-07 21:39:52 +00001101
1102 delete('XexportCmd.vim')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001103 g:StopVimInTerminal(buf)
Bram Moolenaard8448622022-01-07 21:39:52 +00001104enddef
1105
1106def Test_vim9_reload_noclear()
1107 var lines =<< trim END
1108 vim9script
1109 export var exported = 'thexport'
1110
1111 export def TheFunc(x = 0)
1112 enddef
1113 END
1114 writefile(lines, 'XExportReload')
1115 lines =<< trim END
1116 vim9script noclear
1117 g:loadCount += 1
1118 var s:reloaded = 'init'
1119 import './XExportReload' as exp
1120
1121 def Again(): string
1122 return 'again'
1123 enddef
1124
1125 exp.TheFunc()
1126
1127 if exists('s:loaded') | finish | endif
1128 var s:loaded = true
1129
1130 var s:notReloaded = 'yes'
1131 s:reloaded = 'first'
1132 def g:Values(): list<string>
1133 return [s:reloaded, s:notReloaded, Again(), Once(), exp.exported]
1134 enddef
1135
1136 def Once(): string
1137 return 'once'
1138 enddef
1139 END
1140 writefile(lines, 'XReloaded')
1141 g:loadCount = 0
1142 source XReloaded
1143 assert_equal(1, g:loadCount)
1144 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
1145 source XReloaded
1146 assert_equal(2, g:loadCount)
1147 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1148 source XReloaded
1149 assert_equal(3, g:loadCount)
1150 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1151
1152 delete('XReloaded')
1153 delete('XExportReload')
1154 delfunc g:Values
1155 unlet g:loadCount
1156
1157 lines =<< trim END
1158 vim9script
1159 def Inner()
1160 enddef
1161 END
1162 lines->writefile('XreloadScript.vim')
1163 source XreloadScript.vim
1164
1165 lines =<< trim END
1166 vim9script
1167 def Outer()
1168 def Inner()
1169 enddef
1170 enddef
1171 defcompile
1172 END
1173 lines->writefile('XreloadScript.vim')
1174 source XreloadScript.vim
1175
1176 delete('XreloadScript.vim')
1177enddef
1178
1179def Test_vim9_reload_import()
1180 var lines =<< trim END
1181 vim9script
1182 const var = ''
1183 var valone = 1234
1184 def MyFunc(arg: string)
1185 valone = 5678
1186 enddef
1187 END
1188 var morelines =<< trim END
1189 var valtwo = 222
1190 export def GetValtwo(): number
1191 return valtwo
1192 enddef
1193 END
1194 writefile(lines + morelines, 'Xreload.vim')
1195 source Xreload.vim
1196 source Xreload.vim
1197 source Xreload.vim
1198
1199 # cannot declare a var twice
1200 lines =<< trim END
1201 vim9script
1202 var valone = 1234
1203 var valone = 5678
1204 END
1205 writefile(lines, 'Xreload.vim')
1206 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
1207
1208 delete('Xreload.vim')
1209 delete('Ximport.vim')
1210enddef
1211
1212" if a script is reloaded with a script-local variable that changed its type, a
1213" compiled function using that variable must fail.
1214def Test_script_reload_change_type()
1215 var lines =<< trim END
1216 vim9script noclear
1217 var str = 'string'
1218 def g:GetStr(): string
1219 return str .. 'xxx'
1220 enddef
1221 END
1222 writefile(lines, 'Xreload.vim')
1223 source Xreload.vim
1224 echo g:GetStr()
1225
1226 lines =<< trim END
1227 vim9script noclear
1228 var str = 1234
1229 END
1230 writefile(lines, 'Xreload.vim')
1231 source Xreload.vim
1232 assert_fails('echo g:GetStr()', 'E1150:')
1233
1234 delfunc g:GetStr
1235 delete('Xreload.vim')
1236enddef
1237
1238" Define CallFunc so that the test can be compiled
1239command CallFunc echo 'nop'
1240
1241def Test_script_reload_from_function()
1242 var lines =<< trim END
1243 vim9script
1244
1245 if exists('g:loaded')
1246 finish
1247 endif
1248 g:loaded = 1
1249 delcommand CallFunc
1250 command CallFunc Func()
1251 def Func()
1252 so XreloadFunc.vim
1253 g:didTheFunc = 1
1254 enddef
1255 END
1256 writefile(lines, 'XreloadFunc.vim')
1257 source XreloadFunc.vim
1258 CallFunc
1259 assert_equal(1, g:didTheFunc)
1260
1261 delete('XreloadFunc.vim')
1262 delcommand CallFunc
1263 unlet g:loaded
1264 unlet g:didTheFunc
1265enddef
1266
1267def s:RetSome(): string
1268 return 'some'
1269enddef
1270
1271" Not exported function that is referenced needs to be accessed by the
1272" script-local name.
1273def Test_vim9_funcref()
1274 var sortlines =<< trim END
1275 vim9script
1276 def Compare(i1: number, i2: number): number
1277 return i2 - i1
1278 enddef
1279
1280 export def FastSort(): list<number>
1281 return range(5)->sort(Compare)
1282 enddef
1283
1284 export def GetString(arg: string): string
1285 return arg
1286 enddef
1287 END
1288 writefile(sortlines, 'Xsort.vim')
1289
1290 var lines =<< trim END
1291 vim9script
1292 import './Xsort.vim'
1293 def Test()
1294 g:result = Xsort.FastSort()
1295 enddef
1296 Test()
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001297 END
1298 writefile(lines, 'Xscript.vim')
1299 source Xscript.vim
1300 assert_equal([4, 3, 2, 1, 0], g:result)
1301 unlet g:result
Bram Moolenaard8448622022-01-07 21:39:52 +00001302
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001303 lines =<< trim END
1304 vim9script
Bram Moolenaard8448622022-01-07 21:39:52 +00001305 # using a function imported with "as"
1306 import './Xsort.vim' as anAlias
1307 assert_equal('yes', anAlias.GetString('yes'))
1308
1309 # using the function from a compiled function
1310 def TestMore(): string
1311 var s = s:anAlias.GetString('foo')
1312 return s .. anAlias.GetString('bar')
1313 enddef
1314 assert_equal('foobar', TestMore())
1315
1316 # error when using a function that isn't exported
1317 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
1318 END
1319 writefile(lines, 'Xscript.vim')
1320
Bram Moolenaard8448622022-01-07 21:39:52 +00001321 delete('Xsort.vim')
1322 delete('Xscript.vim')
1323
1324 var Funcref = function('s:RetSome')
1325 assert_equal('some', Funcref())
1326enddef
1327
1328" Check that when searching for "FilterFunc" it finds the import in the
1329" script where FastFilter() is called from, both as a string and as a direct
1330" function reference.
1331def Test_vim9_funcref_other_script()
1332 var filterLines =<< trim END
1333 vim9script
1334 export def FilterFunc(idx: number, val: number): bool
1335 return idx % 2 == 1
1336 enddef
1337 export def FastFilter(): list<number>
1338 return range(10)->filter('FilterFunc(v:key, v:val)')
1339 enddef
1340 export def FastFilterDirect(): list<number>
1341 return range(10)->filter(FilterFunc)
1342 enddef
1343 END
1344 writefile(filterLines, 'Xfilter.vim')
1345
1346 var lines =<< trim END
1347 vim9script
1348 import './Xfilter.vim' as filter
1349 def Test()
1350 var x: list<number> = filter.FastFilter()
1351 enddef
1352 Test()
1353 def TestDirect()
1354 var x: list<number> = filter.FastFilterDirect()
1355 enddef
1356 TestDirect()
1357 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001358 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +00001359 delete('Xfilter.vim')
1360enddef
1361
1362def Test_import_absolute()
1363 var import_lines = [
1364 'vim9script',
1365 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
1366 'def UseExported()',
1367 ' g:imported_abs = abs.exported',
1368 ' abs.exported = 8888',
1369 ' g:imported_after = abs.exported',
1370 'enddef',
1371 'UseExported()',
1372 'g:import_disassembled = execute("disass UseExported")',
1373 ]
1374 writefile(import_lines, 'Ximport_abs.vim')
1375 writefile(s:export_script_lines, 'Xexport_abs.vim')
1376
1377 source Ximport_abs.vim
1378
1379 assert_equal(9876, g:imported_abs)
1380 assert_equal(8888, g:imported_after)
1381 assert_match('<SNR>\d\+_UseExported\_s*' ..
1382 'g:imported_abs = abs.exported\_s*' ..
1383 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1384 '1 STOREG g:imported_abs\_s*' ..
1385 'abs.exported = 8888\_s*' ..
1386 '2 PUSHNR 8888\_s*' ..
1387 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1388 'g:imported_after = abs.exported\_s*' ..
1389 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1390 '5 STOREG g:imported_after',
1391 g:import_disassembled)
1392
1393 Undo_export_script_lines()
1394 unlet g:imported_abs
1395 unlet g:import_disassembled
1396
1397 delete('Ximport_abs.vim')
1398 delete('Xexport_abs.vim')
1399enddef
1400
1401def Test_import_rtp()
1402 var import_lines = [
1403 'vim9script',
1404 'import "Xexport_rtp.vim" as rtp',
1405 'g:imported_rtp = rtp.exported',
1406 ]
1407 writefile(import_lines, 'Ximport_rtp.vim')
1408 mkdir('import', 'p')
1409 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1410
1411 var save_rtp = &rtp
1412 &rtp = getcwd()
1413 source Ximport_rtp.vim
1414 &rtp = save_rtp
1415
1416 assert_equal(9876, g:imported_rtp)
1417
1418 Undo_export_script_lines()
1419 unlet g:imported_rtp
1420 delete('Ximport_rtp.vim')
1421 delete('import', 'rf')
1422enddef
1423
1424def Test_import_compile_error()
1425 var export_lines = [
1426 'vim9script',
1427 'export def ExpFunc(): string',
1428 ' return notDefined',
1429 'enddef',
1430 ]
1431 writefile(export_lines, 'Xexported.vim')
1432
1433 var import_lines = [
1434 'vim9script',
1435 'import "./Xexported.vim" as expo',
1436 'def ImpFunc()',
1437 ' echo expo.ExpFunc()',
1438 'enddef',
1439 'defcompile',
1440 ]
1441 writefile(import_lines, 'Ximport.vim')
1442
1443 try
1444 source Ximport.vim
1445 catch /E1001/
1446 # Error should be before the Xexported.vim file.
1447 assert_match('E1001: Variable not found: notDefined', v:exception)
1448 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1449 endtry
1450
1451 delete('Xexported.vim')
1452 delete('Ximport.vim')
1453enddef
1454
1455def Test_func_overrules_import_fails()
1456 var export_lines =<< trim END
1457 vim9script
1458 export def Func()
1459 echo 'imported'
1460 enddef
1461 END
1462 writefile(export_lines, 'XexportedFunc.vim')
1463
1464 var lines =<< trim END
1465 vim9script
1466 import './XexportedFunc.vim' as Func
1467 def Func()
1468 echo 'local to function'
1469 enddef
1470 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001471 v9.CheckScriptFailure(lines, 'E1213: Redefining imported item "Func"')
Bram Moolenaard8448622022-01-07 21:39:52 +00001472
1473 lines =<< trim END
1474 vim9script
1475 import './XexportedFunc.vim' as Func
1476 def Outer()
1477 def Func()
1478 echo 'local to function'
1479 enddef
1480 enddef
1481 defcompile
1482 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001483 v9.CheckScriptFailure(lines, 'E1236:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001484
1485 delete('XexportedFunc.vim')
1486enddef
1487
1488def Test_source_vim9_from_legacy()
1489 var vim9_lines =<< trim END
1490 vim9script
1491 var local = 'local'
1492 g:global = 'global'
1493 export var exported = 'exported'
1494 export def GetText(): string
1495 return 'text'
1496 enddef
1497 END
1498 writefile(vim9_lines, 'Xvim9_script.vim')
1499
1500 var legacy_lines =<< trim END
1501 source Xvim9_script.vim
1502
1503 call assert_false(exists('local'))
1504 call assert_false(exists('exported'))
1505 call assert_false(exists('s:exported'))
1506 call assert_equal('global', global)
1507 call assert_equal('global', g:global)
Bram Moolenaard8448622022-01-07 21:39:52 +00001508 END
1509 writefile(legacy_lines, 'Xlegacy_script.vim')
1510
1511 source Xlegacy_script.vim
1512 assert_equal('global', g:global)
1513 unlet g:global
1514
1515 delete('Xlegacy_script.vim')
1516 delete('Xvim9_script.vim')
1517enddef
1518
Bram Moolenaarc43e6232022-01-13 20:51:56 +00001519def Test_import_vim9_from_legacy()
1520 var vim9_lines =<< trim END
1521 vim9script
1522 var local = 'local'
1523 g:global = 'global'
1524 export var exported = 'exported'
1525 export def GetText(): string
1526 return 'text'
1527 enddef
1528 END
1529 writefile(vim9_lines, 'Xvim9_export.vim')
1530
1531 var legacy_lines =<< trim END
1532 import './Xvim9_export.vim' as vim9
1533
1534 call assert_false(exists('vim9'))
1535 call assert_false(exists('local'))
1536 call assert_false(exists('s:vim9.local'))
1537 call assert_equal('global', global)
1538 call assert_equal('global', g:global)
1539 call assert_false(exists('exported'))
1540 call assert_false(exists('s:exported'))
1541 call assert_false(exists('*GetText'))
1542
1543 " imported symbol is script-local
1544 call assert_equal('exported', s:vim9.exported)
1545 call assert_equal('text', s:vim9.GetText())
1546 END
1547 writefile(legacy_lines, 'Xlegacy_script.vim')
1548
1549 source Xlegacy_script.vim
1550 assert_equal('global', g:global)
1551 unlet g:global
1552
1553 delete('Xlegacy_script.vim')
1554 delete('Xvim9_export.vim')
1555enddef
1556
Bram Moolenaard8448622022-01-07 21:39:52 +00001557def Test_cmdline_win()
1558 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
1559 # the command line window.
1560 mkdir('rtp/syntax', 'p')
1561 var export_lines =<< trim END
1562 vim9script
1563 export var That = 'yes'
1564 END
1565 writefile(export_lines, 'rtp/syntax/Xexport.vim')
1566 var import_lines =<< trim END
1567 vim9script
1568 import './Xexport.vim' as exp
1569 echo exp.That
1570 END
1571 writefile(import_lines, 'rtp/syntax/vim.vim')
1572 var save_rtp = &rtp
1573 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
1574 syntax on
1575 augroup CmdWin
1576 autocmd CmdwinEnter * g:got_there = 'yes'
1577 augroup END
1578 # this will open and also close the cmdline window
1579 feedkeys('q:', 'xt')
1580 assert_equal('yes', g:got_there)
1581
1582 augroup CmdWin
1583 au!
1584 augroup END
1585 &rtp = save_rtp
1586 delete('rtp', 'rf')
1587enddef
1588
1589def Test_import_gone_when_sourced_twice()
1590 var exportlines =<< trim END
1591 vim9script
1592 if exists('g:guard')
1593 finish
1594 endif
1595 g:guard = 1
1596 export var name = 'someName'
1597 END
1598 writefile(exportlines, 'XexportScript.vim')
1599
1600 var lines =<< trim END
1601 vim9script
1602 import './XexportScript.vim' as expo
1603 def g:GetName(): string
1604 return expo.name
1605 enddef
1606 END
1607 writefile(lines, 'XscriptImport.vim')
1608 so XscriptImport.vim
1609 assert_equal('someName', g:GetName())
1610
1611 so XexportScript.vim
1612 assert_fails('call g:GetName()', 'E1149:')
1613
1614 delfunc g:GetName
1615 delete('XexportScript.vim')
1616 delete('XscriptImport.vim')
1617 unlet g:guard
1618enddef
1619
Bram Moolenaar160aa862022-01-10 21:29:57 +00001620" test using an auto-loaded function and variable
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001621def Test_vim9_autoload_full_name()
Bram Moolenaar160aa862022-01-10 21:29:57 +00001622 var lines =<< trim END
1623 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001624 export def Gettest(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00001625 return 'test'
1626 enddef
1627 g:some#name = 'name'
1628 g:some#dict = {key: 'value'}
1629
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001630 export def Varargs(a1: string, ...l: list<string>): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00001631 return a1 .. l[0] .. l[1]
1632 enddef
1633 END
1634
1635 mkdir('Xdir/autoload', 'p')
1636 writefile(lines, 'Xdir/autoload/some.vim')
1637 var save_rtp = &rtp
1638 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1639
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001640 assert_equal('test', g:some#Gettest())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001641 assert_equal('name', g:some#name)
1642 assert_equal('value', g:some#dict.key)
1643 g:some#other = 'other'
1644 assert_equal('other', g:some#other)
1645
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001646 assert_equal('abc', some#Varargs('a', 'b', 'c'))
Bram Moolenaar160aa862022-01-10 21:29:57 +00001647
1648 # upper case script name works
1649 lines =<< trim END
1650 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001651 export def GetOther(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00001652 return 'other'
1653 enddef
1654 END
1655 writefile(lines, 'Xdir/autoload/Other.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001656 assert_equal('other', g:Other#GetOther())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001657
1658 delete('Xdir', 'rf')
1659 &rtp = save_rtp
1660enddef
1661
1662def Test_vim9script_autoload()
1663 mkdir('Xdir/autoload', 'p')
1664 var save_rtp = &rtp
1665 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1666
Bram Moolenaarfd218c82022-01-18 16:26:24 +00001667 # when the path has "/autoload/" prefix is not needed
Bram Moolenaar160aa862022-01-10 21:29:57 +00001668 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00001669 vim9script
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001670 g:prefixed_loaded += 1
Bram Moolenaar160aa862022-01-10 21:29:57 +00001671
1672 export def Gettest(): string
1673 return 'test'
1674 enddef
1675
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001676 export var name = 'name'
1677
1678 export func GetFunc()
1679 return Gettest() .. 'more' .. s:name
Bram Moolenaar160aa862022-01-10 21:29:57 +00001680 endfunc
1681
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001682 export def GetDef(): string
1683 return Gettest() .. 'more' .. name
1684 enddef
1685
Bram Moolenaar160aa862022-01-10 21:29:57 +00001686 export final fname = 'final'
1687 export const cname = 'const'
1688 END
1689 writefile(lines, 'Xdir/autoload/prefixed.vim')
1690
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001691 g:prefixed_loaded = 0
1692 g:expected_loaded = 0
Bram Moolenaar160aa862022-01-10 21:29:57 +00001693 lines =<< trim END
1694 vim9script
1695 import autoload 'prefixed.vim'
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001696 assert_equal(g:expected_loaded, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00001697 assert_equal('test', prefixed.Gettest())
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001698 assert_equal(1, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00001699
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001700 assert_equal('testmorename', prefixed.GetFunc())
1701 assert_equal('testmorename', prefixed.GetDef())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001702 assert_equal('name', prefixed.name)
1703 assert_equal('final', prefixed.fname)
1704 assert_equal('const', prefixed.cname)
1705 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001706 v9.CheckScriptSuccess(lines)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001707 # can source it again, autoload script not loaded again
1708 g:expected_loaded = 1
Bram Moolenaar62aec932022-01-29 21:45:34 +00001709 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00001710
1711 # can also get the items by autoload name
1712 lines =<< trim END
1713 call assert_equal('test', prefixed#Gettest())
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001714 call assert_equal('testmorename', prefixed#GetFunc())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001715 call assert_equal('name', prefixed#name)
1716 call assert_equal('final', prefixed#fname)
1717 call assert_equal('const', prefixed#cname)
1718 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001719 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00001720
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001721 unlet g:prefixed_loaded
1722 unlet g:expected_loaded
1723 delete('Xdir', 'rf')
1724 &rtp = save_rtp
1725enddef
1726
Bram Moolenaard02dce22022-01-18 17:43:04 +00001727def Test_import_autoload_not_exported()
1728 mkdir('Xdir/autoload', 'p')
1729 var save_rtp = &rtp
1730 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1731
1732 # error when using an item that is not exported from an autoload script
1733 var exportLines =<< trim END
1734 vim9script
1735 var notExported = 123
1736 def NotExport()
1737 echo 'nop'
1738 enddef
1739 END
1740 writefile(exportLines, 'Xdir/autoload/notExport1.vim')
1741
1742 var lines =<< trim END
1743 vim9script
1744 import autoload 'notExport1.vim'
1745 echo notExport1.notFound
1746 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001747 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001748
1749 lines =<< trim END
1750 vim9script
1751 import autoload 'notExport1.vim'
1752 echo notExport1.notExported
1753 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001754 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001755
1756 lines =<< trim END
1757 vim9script
1758 import autoload 'notExport1.vim'
1759 echo notExport1.NotFunc()
1760 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001761 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001762
1763 lines =<< trim END
1764 vim9script
1765 import autoload 'notExport1.vim'
1766 echo notExport1.NotExport()
1767 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001768 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001769
1770 lines =<< trim END
1771 vim9script
1772 import autoload 'notExport1.vim'
1773 echo 'text'->notExport1.NotFunc()
1774 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001775 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001776
1777 lines =<< trim END
1778 vim9script
1779 import autoload 'notExport1.vim'
1780 echo 'text'->notExport1.NotExport()
1781 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001782 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001783
1784 # using a :def function we use a different autoload script every time so that
1785 # the function is compiled without the script loaded
1786 writefile(exportLines, 'Xdir/autoload/notExport2.vim')
1787 lines =<< trim END
1788 vim9script
1789 import autoload 'notExport2.vim'
1790 def Testit()
1791 echo notExport2.notFound
1792 enddef
1793 Testit()
1794 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001795 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport2#notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001796
1797 writefile(exportLines, 'Xdir/autoload/notExport3.vim')
1798 lines =<< trim END
1799 vim9script
1800 import autoload 'notExport3.vim'
1801 def Testit()
1802 echo notExport3.notExported
1803 enddef
1804 Testit()
1805 END
1806 # don't get E1049 because it is too complicated to figure out
Bram Moolenaar62aec932022-01-29 21:45:34 +00001807 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport3#notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001808
1809 writefile(exportLines, 'Xdir/autoload/notExport4.vim')
1810 lines =<< trim END
1811 vim9script
1812 import autoload 'notExport4.vim'
1813 def Testit()
1814 echo notExport4.NotFunc()
1815 enddef
1816 Testit()
1817 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001818 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport4#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001819
1820 writefile(exportLines, 'Xdir/autoload/notExport5.vim')
1821 lines =<< trim END
1822 vim9script
1823 import autoload 'notExport5.vim'
1824 def Testit()
1825 echo notExport5.NotExport()
1826 enddef
1827 Testit()
1828 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001829 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport5#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001830
1831 writefile(exportLines, 'Xdir/autoload/notExport6.vim')
1832 lines =<< trim END
1833 vim9script
1834 import autoload 'notExport6.vim'
1835 def Testit()
1836 echo 'text'->notExport6.NotFunc()
1837 enddef
1838 Testit()
1839 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001840 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport6#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001841
1842 writefile(exportLines, 'Xdir/autoload/notExport7.vim')
1843 lines =<< trim END
1844 vim9script
1845 import autoload 'notExport7.vim'
1846 def Testit()
1847 echo 'text'->notExport7.NotExport()
1848 enddef
1849 Testit()
1850 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001851 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport7#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001852
1853 delete('Xdir', 'rf')
1854 &rtp = save_rtp
1855enddef
1856
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001857def Test_vim9script_autoload_call()
1858 mkdir('Xdir/autoload', 'p')
1859 var save_rtp = &rtp
1860 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1861
1862 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00001863 vim9script
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001864
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00001865 export def RetArg(arg: string): string
1866 return arg
1867 enddef
1868
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001869 export def Getother()
1870 g:result = 'other'
1871 enddef
1872 END
Bram Moolenaar5d982692022-01-12 15:15:27 +00001873 writefile(lines, 'Xdir/autoload/another.vim')
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001874
1875 lines =<< trim END
1876 vim9script
Bram Moolenaar5d982692022-01-12 15:15:27 +00001877 import autoload 'another.vim'
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00001878
1879 # compile this before 'another.vim' is loaded
1880 def CallAnother()
1881 assert_equal('foo', 'foo'->another.RetArg())
1882 enddef
1883 CallAnother()
1884
Bram Moolenaar5d982692022-01-12 15:15:27 +00001885 call another.Getother()
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001886 assert_equal('other', g:result)
Bram Moolenaar3d8e25a2022-01-22 11:00:02 +00001887
1888 assert_equal('arg', call('another.RetArg', ['arg']))
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001889 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001890 v9.CheckScriptSuccess(lines)
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001891
1892 unlet g:result
Bram Moolenaar160aa862022-01-10 21:29:57 +00001893 delete('Xdir', 'rf')
1894 &rtp = save_rtp
1895enddef
1896
Bram Moolenaarb697dc22022-01-22 11:27:29 +00001897def Test_vim9script_noclear_autoload()
1898 mkdir('Xdir/autoload', 'p')
1899 var save_rtp = &rtp
1900 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1901
1902 var lines =<< trim END
1903 vim9script
1904 export def Func(): string
1905 return 'called'
1906 enddef
1907 g:double_loaded = 'yes'
1908 END
1909 writefile(lines, 'Xdir/autoload/double.vim')
1910
1911 lines =<< trim END
1912 vim9script noclear
1913 if exists('g:script_loaded')
1914 finish
1915 endif
1916 g:script_loaded = true
1917
1918 import autoload 'double.vim'
1919 nnoremap <F3> <ScriptCmd>g:result = double.Func()<CR>
1920 END
1921 g:double_loaded = 'no'
1922 writefile(lines, 'Xloaddouble')
1923 source Xloaddouble
1924 assert_equal('no', g:double_loaded)
1925 assert_equal(true, g:script_loaded)
1926 source Xloaddouble
1927 feedkeys("\<F3>", 'xt')
1928 assert_equal('called', g:result)
1929 assert_equal('yes', g:double_loaded)
1930
1931 delete('Xloaddouble')
1932 unlet g:double_loaded
1933 unlet g:script_loaded
1934 unlet g:result
1935 delete('Xdir', 'rf')
1936 &rtp = save_rtp
1937enddef
1938
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00001939def Test_vim9script_autoload_duplicate()
1940 mkdir('Xdir/autoload', 'p')
1941
1942 var lines =<< trim END
1943 vim9script
1944
1945 export def Func()
1946 enddef
1947
1948 def Func()
1949 enddef
1950 END
1951 writefile(lines, 'Xdir/autoload/dupfunc.vim')
1952 assert_fails('source Xdir/autoload/dupfunc.vim', 'E1073:')
1953
1954 lines =<< trim END
1955 vim9script
1956
1957 def Func()
1958 enddef
1959
1960 export def Func()
1961 enddef
1962 END
1963 writefile(lines, 'Xdir/autoload/dup2func.vim')
1964 assert_fails('source Xdir/autoload/dup2func.vim', 'E1073:')
1965
1966 lines =<< trim END
1967 vim9script
1968
1969 def Func()
1970 enddef
1971
1972 export var Func = 'asdf'
1973 END
1974 writefile(lines, 'Xdir/autoload/dup3func.vim')
1975 assert_fails('source Xdir/autoload/dup3func.vim', 'E1041: Redefining script item Func')
1976
1977 lines =<< trim END
1978 vim9script
1979
1980 export var Func = 'asdf'
1981
1982 def Func()
1983 enddef
1984 END
1985 writefile(lines, 'Xdir/autoload/dup4func.vim')
1986 assert_fails('source Xdir/autoload/dup4func.vim', 'E707:')
1987
1988 lines =<< trim END
1989 vim9script
1990
1991 var Func = 'asdf'
1992
1993 export def Func()
1994 enddef
1995 END
1996 writefile(lines, 'Xdir/autoload/dup5func.vim')
1997 assert_fails('source Xdir/autoload/dup5func.vim', 'E707:')
1998
1999 lines =<< trim END
2000 vim9script
2001
2002 export def Func()
2003 enddef
2004
2005 var Func = 'asdf'
2006 END
2007 writefile(lines, 'Xdir/autoload/dup6func.vim')
2008 assert_fails('source Xdir/autoload/dup6func.vim', 'E1041: Redefining script item Func')
2009
2010 delete('Xdir', 'rf')
2011enddef
2012
Bram Moolenaar2017d6f2022-01-20 19:38:46 +00002013def Test_autoload_missing_function_name()
2014 mkdir('Xdir/autoload', 'p')
2015
2016 var lines =<< trim END
2017 vim9script
2018
2019 def loadme#()
2020 enddef
2021 END
2022 writefile(lines, 'Xdir/autoload/loadme.vim')
2023 assert_fails('source Xdir/autoload/loadme.vim', 'E129:')
2024
2025 delete('Xdir', 'rf')
2026enddef
2027
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002028def Test_autoload_name_wrong()
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002029 var lines =<< trim END
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002030 def Xscriptname#Func()
2031 enddef
2032 END
2033 writefile(lines, 'Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002034 v9.CheckScriptFailure(lines, 'E746:')
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00002035 delete('Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002036
2037 mkdir('Xdir/autoload', 'p')
2038 lines =<< trim END
2039 vim9script
2040 def somescript#Func()
2041 enddef
2042 END
2043 writefile(lines, 'Xdir/autoload/somescript.vim')
2044 assert_fails('source Xdir/autoload/somescript.vim', 'E1263:')
2045
2046 delete('Xdir', 'rf')
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002047enddef
2048
Bram Moolenaard041f422022-01-12 19:54:00 +00002049def Test_import_autoload_postponed()
2050 mkdir('Xdir/autoload', 'p')
2051 var save_rtp = &rtp
2052 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2053
2054 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002055 vim9script
Bram Moolenaard041f422022-01-12 19:54:00 +00002056
2057 g:loaded_postponed = 'true'
2058 export var variable = 'bla'
2059 export def Function(): string
2060 return 'bla'
2061 enddef
2062 END
2063 writefile(lines, 'Xdir/autoload/postponed.vim')
2064
2065 lines =<< trim END
2066 vim9script
2067
2068 import autoload 'postponed.vim'
2069 def Tryit()
2070 echo postponed.variable
2071 echo postponed.Function()
2072 enddef
2073 defcompile
2074 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002075 v9.CheckScriptSuccess(lines)
Bram Moolenaard041f422022-01-12 19:54:00 +00002076 assert_false(exists('g:loaded_postponed'))
Bram Moolenaar62aec932022-01-29 21:45:34 +00002077 v9.CheckScriptSuccess(lines + ['Tryit()'])
Bram Moolenaard041f422022-01-12 19:54:00 +00002078 assert_equal('true', g:loaded_postponed)
2079
2080 unlet g:loaded_postponed
2081 delete('Xdir', 'rf')
2082 &rtp = save_rtp
2083enddef
2084
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002085def Test_import_autoload_override()
2086 mkdir('Xdir/autoload', 'p')
2087 var save_rtp = &rtp
2088 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2089 test_override('autoload', 1)
2090
2091 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002092 vim9script
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002093
2094 g:loaded_override = 'true'
2095 export var variable = 'bla'
2096 export def Function(): string
2097 return 'bla'
2098 enddef
2099 END
2100 writefile(lines, 'Xdir/autoload/override.vim')
2101
2102 lines =<< trim END
2103 vim9script
2104
2105 import autoload 'override.vim'
2106 assert_equal('true', g:loaded_override)
2107
2108 def Tryit()
2109 echo override.doesNotExist
2110 enddef
2111 defcompile
2112 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002113 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1)
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002114
2115 test_override('autoload', 0)
2116 unlet g:loaded_override
2117 delete('Xdir', 'rf')
2118 &rtp = save_rtp
2119enddef
2120
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002121def Test_autoload_mapping()
2122 mkdir('Xdir/autoload', 'p')
2123 var save_rtp = &rtp
2124 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2125
2126 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002127 vim9script
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002128
2129 g:toggle_loaded = 'yes'
2130
2131 export def Toggle(): string
2132 return ":g:toggle_called = 'yes'\<CR>"
2133 enddef
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002134 export def Doit()
2135 g:doit_called = 'yes'
2136 enddef
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002137 END
2138 writefile(lines, 'Xdir/autoload/toggle.vim')
2139
2140 lines =<< trim END
2141 vim9script
2142
2143 import autoload 'toggle.vim'
2144
2145 nnoremap <silent> <expr> tt toggle.Toggle()
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002146 nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR>
2147 nnoremap <silent> yy <Cmd>toggle.Doit()<CR>
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002148 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002149 v9.CheckScriptSuccess(lines)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002150 assert_false(exists("g:toggle_loaded"))
2151 assert_false(exists("g:toggle_called"))
Bram Moolenaar6079da72022-01-18 14:16:59 +00002152 assert_match('\d A: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002153
2154 feedkeys("tt", 'xt')
2155 assert_equal('yes', g:toggle_loaded)
2156 assert_equal('yes', g:toggle_called)
Bram Moolenaar6079da72022-01-18 14:16:59 +00002157 assert_match('\d: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002158
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002159 feedkeys("xx", 'xt')
2160 assert_equal('yes', g:doit_called)
2161
2162 assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle')
2163
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002164 nunmap tt
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002165 nunmap xx
2166 nunmap yy
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002167 unlet g:toggle_loaded
2168 unlet g:toggle_called
2169 delete('Xdir', 'rf')
2170 &rtp = save_rtp
2171enddef
2172
Bram Moolenaar160aa862022-01-10 21:29:57 +00002173def Test_vim9script_autoload_fails()
2174 var lines =<< trim END
2175 vim9script autoload
2176 var n = 0
2177 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002178 v9.CheckScriptFailure(lines, 'E475: Invalid argument: autoload')
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002179
2180 lines =<< trim END
2181 vim9script noclear noclear
2182 var n = 0
2183 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002184 v9.CheckScriptFailure(lines, 'E983: Duplicate argument: noclear')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002185enddef
2186
2187def Test_import_autoload_fails()
2188 var lines =<< trim END
2189 vim9script
2190 import autoload autoload 'prefixed.vim'
2191 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002192 v9.CheckScriptFailure(lines, 'E121: Undefined variable: autoload')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002193
2194 lines =<< trim END
2195 vim9script
Bram Moolenaar1836d612022-01-18 13:14:47 +00002196 import autoload './doesNotExist.vim'
Bram Moolenaar160aa862022-01-10 21:29:57 +00002197 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002198 v9.CheckScriptFailure(lines, 'E1264:')
Bram Moolenaar1836d612022-01-18 13:14:47 +00002199
2200 lines =<< trim END
2201 vim9script
2202 import autoload '/dir/doesNotExist.vim'
2203 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002204 v9.CheckScriptFailure(lines, 'E1264:')
Bram Moolenaar1836d612022-01-18 13:14:47 +00002205
2206 lines =<< trim END
2207 vim9script
2208 import autoload 'doesNotExist.vim'
2209 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002210 v9.CheckScriptFailure(lines, 'E1053: Could not import "doesNotExist.vim"')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002211enddef
2212
2213" test disassembling an auto-loaded function starting with "debug"
2214def Test_vim9_autoload_disass()
2215 mkdir('Xdir/autoload', 'p')
2216 var save_rtp = &rtp
2217 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2218
2219 var lines =<< trim END
2220 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002221 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002222 return 'debug'
2223 enddef
2224 END
2225 writefile(lines, 'Xdir/autoload/debugit.vim')
2226
2227 lines =<< trim END
2228 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002229 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002230 return 'profile'
2231 enddef
2232 END
2233 writefile(lines, 'Xdir/autoload/profileit.vim')
2234
2235 lines =<< trim END
2236 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002237 assert_equal('debug', debugit#Test())
2238 disass debugit#Test
2239 assert_equal('profile', profileit#Test())
2240 disass profileit#Test
Bram Moolenaar160aa862022-01-10 21:29:57 +00002241 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002242 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002243
2244 delete('Xdir', 'rf')
2245 &rtp = save_rtp
2246enddef
2247
2248" test using a vim9script that is auto-loaded from an autocmd
2249def Test_vim9_aucmd_autoload()
2250 var lines =<< trim END
2251 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002252 export def Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002253 echomsg getreg('"')
2254 enddef
2255 END
2256
2257 mkdir('Xdir/autoload', 'p')
2258 writefile(lines, 'Xdir/autoload/foo.vim')
2259 var save_rtp = &rtp
2260 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2261 augroup test
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002262 autocmd TextYankPost * call foo#Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002263 augroup END
2264
2265 normal Y
2266
2267 augroup test
2268 autocmd!
2269 augroup END
2270 delete('Xdir', 'rf')
2271 &rtp = save_rtp
2272enddef
2273
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002274" test using a autoloaded file that is case sensitive
2275def Test_vim9_autoload_case_sensitive()
2276 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002277 vim9script
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002278 export def CaseSensitive(): string
2279 return 'done'
2280 enddef
2281 END
2282
2283 mkdir('Xdir/autoload', 'p')
2284 writefile(lines, 'Xdir/autoload/CaseSensitive.vim')
2285 var save_rtp = &rtp
2286 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2287
2288 lines =<< trim END
2289 vim9script
2290 import autoload 'CaseSensitive.vim'
2291 assert_equal('done', CaseSensitive.CaseSensitive())
2292 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002293 v9.CheckScriptSuccess(lines)
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002294
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002295 if !has('fname_case')
2296 lines =<< trim END
2297 vim9script
2298 import autoload 'CaseSensitive.vim'
2299 import autoload 'casesensitive.vim'
2300 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002301 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002302 endif
2303
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002304 delete('Xdir', 'rf')
2305 &rtp = save_rtp
2306enddef
2307
Bram Moolenaar160aa862022-01-10 21:29:57 +00002308" This was causing a crash because suppress_errthrow wasn't reset.
2309def Test_vim9_autoload_error()
2310 var lines =<< trim END
2311 vim9script
2312 def crash#func()
2313 try
2314 for x in List()
2315 endfor
2316 catch
2317 endtry
2318 g:ok = true
2319 enddef
2320 fu List()
2321 invalid
2322 endfu
2323 try
2324 alsoinvalid
2325 catch /wontmatch/
2326 endtry
2327 END
2328 call mkdir('Xruntime/autoload', 'p')
2329 call writefile(lines, 'Xruntime/autoload/crash.vim')
2330
2331 # run in a separate Vim to avoid the side effects of assert_fails()
2332 lines =<< trim END
2333 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
2334 call crash#func()
2335 call writefile(['ok'], 'Xdidit')
2336 qall!
2337 END
2338 writefile(lines, 'Xscript')
Bram Moolenaar62aec932022-01-29 21:45:34 +00002339 g:RunVim([], [], '-S Xscript')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002340 assert_equal(['ok'], readfile('Xdidit'))
2341
2342 delete('Xdidit')
2343 delete('Xscript')
2344 delete('Xruntime', 'rf')
2345
2346 lines =<< trim END
2347 vim9script
2348 var foo#bar = 'asdf'
2349 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002350 v9.CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002351enddef
2352
Bram Moolenaard8448622022-01-07 21:39:52 +00002353
2354" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker