blob: 652e4d36b633b55f8e1009ad90365d89e88a6b3e [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 Moolenaar779aeff2022-02-08 19:12:19 +0000503
504 lines =<< trim END
505 vim9script
506 export var item = 'hello'
507 import './Xyourself.vim'
508 END
509 writefile(lines, 'Xyourself.vim')
510 assert_fails('source Xyourself.vim', 'E1088:')
511 delete('Xyourself.vim')
512
Bram Moolenaard8448622022-01-07 21:39:52 +0000513 mkdir('Ximport')
514
515 writefile(['vim9script'], 'Ximport/.vim')
516 lines =<< trim END
517 vim9script
518 import './Ximport/.vim'
519 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000520 v9.CheckScriptFailure(lines, 'E1261: Cannot import .vim without using "as"')
Bram Moolenaard8448622022-01-07 21:39:52 +0000521 lines =<< trim END
522 vim9script
523 import './Ximport/.vim' as vim
524 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000525 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000526
527 writefile(['vim9script'], 'Ximport/.vimrc')
528 lines =<< trim END
529 vim9script
530 import './Ximport/.vimrc'
531 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000532 v9.CheckScriptFailure(lines, 'E1257: Imported script must use "as" or end in .vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000533 lines =<< trim END
534 vim9script
535 import './Ximport/.vimrc' as vimrc
536 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000537 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000538
539 delete('Ximport', 'rf')
540enddef
541
542func g:Trigger()
543 source Ximport.vim
544 return "echo 'yes'\<CR>"
545endfunc
546
547def Test_import_export_expr_map()
548 # check that :import and :export work when buffer is locked
549 var export_lines =<< trim END
550 vim9script
551 export def That(): string
552 return 'yes'
553 enddef
554 END
555 writefile(export_lines, 'Xexport_that.vim')
556
557 var import_lines =<< trim END
558 vim9script
559 import './Xexport_that.vim' as that
560 assert_equal('yes', that.That())
561 END
562 writefile(import_lines, 'Ximport.vim')
563
564 nnoremap <expr> trigger g:Trigger()
565 feedkeys('trigger', "xt")
566
567 delete('Xexport_that.vim')
568 delete('Ximport.vim')
569 nunmap trigger
570enddef
571
572def Test_import_in_filetype()
573 # check that :import works when the buffer is locked
574 mkdir('ftplugin', 'p')
575 var export_lines =<< trim END
576 vim9script
577 export var That = 'yes'
578 END
579 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
580
581 var import_lines =<< trim END
582 vim9script
583 import './Xexport_ft.vim' as ft
584 assert_equal('yes', ft.That)
585 g:did_load_mytpe = 1
586 END
587 writefile(import_lines, 'ftplugin/qf.vim')
588
589 var save_rtp = &rtp
590 &rtp = getcwd() .. ',' .. &rtp
591
592 filetype plugin on
593 copen
594 assert_equal(1, g:did_load_mytpe)
595
596 quit!
597 delete('Xexport_ft.vim')
598 delete('ftplugin', 'rf')
599 &rtp = save_rtp
600enddef
601
602def Test_use_import_in_mapping()
603 var lines =<< trim END
604 vim9script
605 export def Funcx()
606 g:result = 42
607 enddef
608 END
609 writefile(lines, 'XsomeExport.vim')
610 lines =<< trim END
611 vim9script
612 import './XsomeExport.vim' as some
613 var Funcy = some.Funcx
614 nnoremap <F3> :call <sid>Funcy()<cr>
615 END
616 writefile(lines, 'Xmapscript.vim')
617
618 source Xmapscript.vim
619 feedkeys("\<F3>", "xt")
620 assert_equal(42, g:result)
621
622 unlet g:result
623 delete('XsomeExport.vim')
624 delete('Xmapscript.vim')
625 nunmap <F3>
626enddef
627
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000628def Test_use_import_in_command_completion()
Bram Moolenaar15d16352022-01-17 20:09:08 +0000629 var lines =<< trim END
630 vim9script
631 export def Complete(..._): list<string>
632 return ['abcd']
633 enddef
634 END
635 writefile(lines, 'Xscript.vim')
636
637 lines =<< trim END
638 vim9script
639 import './Xscript.vim'
640
641 command -nargs=1 -complete=customlist,Xscript.Complete Cmd echo 'ok'
642 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
643 assert_equal('#Cmd abcd', @:)
644 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000645 v9.CheckScriptSuccess(lines)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000646
647 delcommand Cmd
648 delete('Xscript.vim')
649enddef
650
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000651def Test_use_autoload_import_in_insert_completion()
652 mkdir('Xdir/autoload', 'p')
653 var save_rtp = &rtp
654 exe 'set rtp^=' .. getcwd() .. '/Xdir'
655
656 var lines =<< trim END
657 vim9script
658 export def ThesaurusFunc(findbase: bool, _): any
659 if findbase
660 return 1
661 endif
662 return [
663 'check',
664 'experiment',
665 'test',
666 'verification'
667 ]
668 enddef
669 g:completion_loaded = 'yes'
670 END
671 writefile(lines, 'Xdir/autoload/completion.vim')
672
673 new
674 lines =<< trim END
675 vim9script
676 g:completion_loaded = 'no'
677 import autoload 'completion.vim'
678 set thesaurusfunc=completion.ThesaurusFunc
679 assert_equal('no', g:completion_loaded)
680 feedkeys("i\<C-X>\<C-T>\<C-N>\<Esc>", 'xt')
681 assert_equal('experiment', getline(1))
682 assert_equal('yes', g:completion_loaded)
683 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000684 v9.CheckScriptSuccess(lines)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000685
686 set thesaurusfunc=
687 bwipe!
688 delete('Xdir', 'rf')
689 &rtp = save_rtp
690enddef
691
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000692def Test_use_autoload_import_partial_in_opfunc()
693 mkdir('Xdir/autoload', 'p')
694 var save_rtp = &rtp
695 exe 'set rtp^=' .. getcwd() .. '/Xdir'
696
697 var lines =<< trim END
698 vim9script
699 export def Opfunc(..._)
700 g:opfunc_called = 'yes'
701 enddef
702 END
703 writefile(lines, 'Xdir/autoload/opfunc.vim')
704
705 new
706 lines =<< trim END
707 vim9script
708 import autoload 'opfunc.vim'
709 nnoremap <expr> <F3> TheFunc()
710 def TheFunc(): string
711 &operatorfunc = function('opfunc.Opfunc', [0])
712 return 'g@'
713 enddef
714 feedkeys("\<F3>l", 'xt')
715 assert_equal('yes', g:opfunc_called)
716 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000717 v9.CheckScriptSuccess(lines)
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000718
719 set opfunc=
720 bwipe!
721 delete('Xdir', 'rf')
Bram Moolenaar06b77222022-01-25 15:51:56 +0000722 nunmap <F3>
723 &rtp = save_rtp
724enddef
725
726def Test_set_opfunc_to_autoload_func_directly()
727 mkdir('Xdir/autoload', 'p')
728 var save_rtp = &rtp
729 exe 'set rtp^=' .. getcwd() .. '/Xdir'
730
731 var lines =<< trim END
732 vim9script
733 export def Opfunc(..._)
734 g:opfunc_called = 'yes'
735 enddef
736 END
737 writefile(lines, 'Xdir/autoload/opfunc.vim')
738
739 new
740 lines =<< trim END
741 vim9script
742 import autoload 'opfunc.vim'
743 nnoremap <expr> <F3> TheFunc()
744 def TheFunc(): string
745 &operatorfunc = opfunc.Opfunc
746 return 'g@'
747 enddef
748 feedkeys("\<F3>l", 'xt')
749 assert_equal('yes', g:opfunc_called)
750 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000751 v9.CheckScriptSuccess(lines)
Bram Moolenaar06b77222022-01-25 15:51:56 +0000752
753 set opfunc=
754 bwipe!
755 delete('Xdir', 'rf')
756 nunmap <F3>
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000757 &rtp = save_rtp
758enddef
759
Bram Moolenaare70dd112022-01-21 16:31:11 +0000760def Test_use_autoload_import_in_fold_expression()
761 mkdir('Xdir/autoload', 'p')
762 var save_rtp = &rtp
763 exe 'set rtp^=' .. getcwd() .. '/Xdir'
764
765 var lines =<< trim END
766 vim9script
767 export def Expr(): string
768 return getline(v:lnum) =~ '^#' ? '>1' : '1'
769 enddef
Bram Moolenaar9530b582022-01-22 13:39:08 +0000770 export def Text(): string
771 return 'fold text'
772 enddef
Bram Moolenaare70dd112022-01-21 16:31:11 +0000773 g:fold_loaded = 'yes'
774 END
775 writefile(lines, 'Xdir/autoload/fold.vim')
776
777 lines =<< trim END
778 vim9script
779 import autoload 'fold.vim'
780 &foldexpr = 'fold.Expr()'
Bram Moolenaar9530b582022-01-22 13:39:08 +0000781 &foldtext = 'fold.Text()'
Bram Moolenaare70dd112022-01-21 16:31:11 +0000782 &foldmethod = 'expr'
783 &debug = 'throw'
784 END
785 new
786 setline(1, ['# one', 'text', '# two', 'text'])
787 g:fold_loaded = 'no'
Bram Moolenaar62aec932022-01-29 21:45:34 +0000788 v9.CheckScriptSuccess(lines)
Bram Moolenaare70dd112022-01-21 16:31:11 +0000789 assert_equal('no', g:fold_loaded)
790 redraw
791 assert_equal('yes', g:fold_loaded)
792
793 # Check that script context of 'foldexpr' is copied to another buffer.
794 edit! otherfile
795 redraw
796
Bram Moolenaar9530b582022-01-22 13:39:08 +0000797 set foldexpr= foldtext& foldmethod& debug=
Bram Moolenaare70dd112022-01-21 16:31:11 +0000798 bwipe!
799 delete('Xdir', 'rf')
800 &rtp = save_rtp
801enddef
802
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000803func Test_import_in_diffexpr()
804 CheckExecutable diff
805
806 call Run_Test_import_in_diffexpr()
807endfunc
808
809def Run_Test_import_in_diffexpr()
810 var lines =<< trim END
811 vim9script
812
813 export def DiffExpr()
814 # Prepend some text to check diff type detection
815 writefile(['warning', ' message'], v:fname_out)
816 silent exe '!diff ' .. v:fname_in .. ' '
817 .. v:fname_new .. '>>' .. v:fname_out
818 enddef
819 END
820 writefile(lines, 'Xdiffexpr')
821
822 lines =<< trim END
823 vim9script
824 import './Xdiffexpr' as diff
825
826 set diffexpr=diff.DiffExpr()
827 set diffopt=foldcolumn:0
828 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000829 v9.CheckScriptSuccess(lines)
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000830
831 enew!
832 call setline(1, ['one', 'two', 'three'])
833 diffthis
834
835 botright vert new
836 call setline(1, ['one', 'two', 'three.'])
837 diffthis
838 # we only check if this does not cause errors
839 redraw
840
841 diffoff!
842 bwipe!
843 bwipe!
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +0000844 delete('Xdiffexpr')
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000845enddef
846
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000847def Test_import_in_patchexpr()
848 var lines =<< trim END
849 vim9script
850 export def TPatch()
851 call writefile(['output file'], v:fname_out)
852 enddef
853 END
854 writefile(lines, 'Xpatchexpr')
855
856 lines =<< trim END
857 vim9script
858 import './Xpatchexpr' as patch
859 set patchexpr=patch.TPatch()
860 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000861 v9.CheckScriptSuccess(lines)
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000862
863 call writefile(['input file'], 'Xinput')
864 call writefile(['diff file'], 'Xdiff')
865 :%bwipe!
866 edit Xinput
867 diffpatch Xdiff
868 call assert_equal('output file', getline(1))
869
870 call delete('Xinput')
871 call delete('Xdiff')
872 call delete('Xpatchexpr')
873 set patchexpr&
874 :%bwipe!
875enddef
876
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000877def Test_import_in_formatexpr()
878 var lines =<< trim END
879 vim9script
880 export def MyFormatExpr(): number
881 g:did_format = 'yes'
882 return 0
883 enddef
884 END
885 writefile(lines, 'Xformatter')
886
887 lines =<< trim END
888 vim9script
889 import './Xformatter' as format
890 set formatexpr=format.MyFormatExpr()
891 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000892 v9.CheckScriptSuccess(lines)
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000893
894 new
895 setline(1, ['a', 'b', 'c'])
896 normal gqG
897 assert_equal('yes', g:did_format)
898
899 bwipe!
900 delete('Xformatter')
901 unlet g:did_format
902 set formatexpr=
903enddef
904
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +0000905def Test_import_in_includeexpr()
906 writefile(['found it'], 'Xthisfile')
907 new
908
909 var lines =<< trim END
910 vim9script
911 export def DoSub(): string
912 return substitute(v:fname, 'that', 'this', '')
913 enddef
914 END
915 writefile(lines, 'Xinclude.vim')
916
917 lines =<< trim END
918 vim9script
919 import './Xinclude.vim'
920 set includeexpr=Xinclude.DoSub()
921 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000922 v9.CheckScriptSuccess(lines)
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +0000923
924 setline(1, ['Xthatfile'])
925 exe "normal \<C-W>f"
926 assert_equal('Xthisfile', expand('%'))
927
928 bwipe!
929 bwipe!
930 set includeexpr=
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +0000931 delete('Xinclude.vim')
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +0000932 delete('Xthisfile')
933enddef
934
Bram Moolenaar28e60cc2022-01-22 20:32:00 +0000935def Test_import_in_indentexpr()
936 var lines =<< trim END
937 vim9script
938 export def GetIndent(): number
939 return 5
940 enddef
941 END
942 writefile(lines, 'Xindenter')
943
944 lines =<< trim END
945 vim9script
946 import './Xindenter' as indent
947 set indentexpr=indent.GetIndent()
948 set debug=throw
949 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000950 v9.CheckScriptSuccess(lines)
Bram Moolenaar28e60cc2022-01-22 20:32:00 +0000951
952 new
953 setline(1, 'hello')
954 normal ==
955 assert_equal(' hello', getline(1))
956
957 bwipe!
958 set indentexpr= debug=
959 delete('Xindenter')
960enddef
961
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000962func Test_import_in_printexpr()
963 CheckFeature postscript
964 call Run_Test_import_in_printexpr()
965endfunc
966
967def Run_Test_import_in_printexpr()
968 var lines =<< trim END
969 vim9script
970 export def PrintFile(): bool
971 g:printed = 'yes'
972 delete('v:fname_in')
973 return false
974 enddef
975 END
976 writefile(lines, 'Xprint.vim')
977
978 lines =<< trim END
979 vim9script
980 import './Xprint.vim'
981 set printexpr=Xprint.PrintFile()
982 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000983 v9.CheckScriptSuccess(lines)
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000984
985 help
986 hardcopy dummy args
987 assert_equal('yes', g:printed)
988
989 delete('Xprint.vim')
990 set printexpr=
991enddef
992
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000993def Test_import_in_charconvert()
994 var lines =<< trim END
995 vim9script
996 export def MakeUpper(): bool
997 var data = readfile(v:fname_in)
998 map(data, 'toupper(v:val)')
999 writefile(data, v:fname_out)
1000 return false # success
1001 enddef
1002 END
1003 writefile(lines, 'Xconvert.vim')
1004
1005 lines =<< trim END
1006 vim9script
1007 import './Xconvert.vim' as conv
1008 set charconvert=conv.MakeUpper()
1009 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001010 v9.CheckScriptSuccess(lines)
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001011
1012 writefile(['one', 'two'], 'Xfile')
1013 new Xfile
1014 write ++enc=ucase Xfile1
1015 assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1016
1017 delete('Xfile')
1018 delete('Xfile1')
1019 delete('Xconvert.vim')
1020 bwipe!
1021 set charconvert&
1022enddef
1023
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001024func Test_import_in_spellsuggest_expr()
1025 CheckFeature spell
1026 call Run_Test_import_in_spellsuggest_expr()
1027endfunc
1028
1029def Run_Test_import_in_spellsuggest_expr()
1030 var lines =<< trim END
1031 vim9script
1032 export def MySuggest(): list<any>
1033 return [['Fox', 8], ['Fop', 9]]
1034 enddef
1035 END
1036 writefile(lines, 'Xsuggest.vim')
1037
1038 lines =<< trim END
1039 vim9script
1040 import './Xsuggest.vim' as sugg
1041 set spell spellsuggest=expr:sugg.MySuggest()
1042 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001043 v9.CheckScriptSuccess(lines)
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001044
1045 set verbose=1 # report errors
1046 call assert_equal(['Fox', 'Fop'], spellsuggest('Fo', 2))
1047
1048 delete('Xsuggest.vim')
1049 set nospell spellsuggest& verbose=0
1050enddef
1051
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001052def Test_export_shadows_global_function()
1053 mkdir('Xdir/autoload', 'p')
1054 var save_rtp = &rtp
1055 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1056
1057 var lines =<< trim END
1058 vim9script
1059 export def Shadow(): string
1060 return 'Shadow()'
1061 enddef
1062 END
1063 writefile(lines, 'Xdir/autoload/shadow.vim')
1064
1065 lines =<< trim END
1066 vim9script
1067
1068 def g:Shadow(): string
1069 return 'global'
1070 enddef
1071
1072 import autoload 'shadow.vim'
1073 assert_equal('Shadow()', shadow.Shadow())
1074 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001075 v9.CheckScriptSuccess(lines)
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001076
1077 delfunc g:Shadow
1078 bwipe!
1079 delete('Xdir', 'rf')
1080 &rtp = save_rtp
1081enddef
1082
Bram Moolenaard8448622022-01-07 21:39:52 +00001083def Test_export_fails()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001084 v9.CheckScriptFailure(['export var some = 123'], 'E1042:')
1085 v9.CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
1086 v9.CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001087
1088 assert_fails('export something', 'E1043:')
1089enddef
1090
1091func Test_import_fails_without_script()
1092 CheckRunVimInTerminal
1093
1094 " call indirectly to avoid compilation error for missing functions
1095 call Run_Test_import_fails_on_command_line()
1096endfunc
1097
1098def Run_Test_import_fails_on_command_line()
1099 var export =<< trim END
1100 vim9script
1101 export def Foo(): number
1102 return 0
1103 enddef
1104 END
1105 writefile(export, 'XexportCmd.vim')
1106
Bram Moolenaar62aec932022-01-29 21:45:34 +00001107 var buf = g:RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaard8448622022-01-07 21:39:52 +00001108 rows: 6, wait_for_ruler: 0})
Bram Moolenaar62aec932022-01-29 21:45:34 +00001109 g:WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaard8448622022-01-07 21:39:52 +00001110
1111 delete('XexportCmd.vim')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001112 g:StopVimInTerminal(buf)
Bram Moolenaard8448622022-01-07 21:39:52 +00001113enddef
1114
1115def Test_vim9_reload_noclear()
1116 var lines =<< trim END
1117 vim9script
1118 export var exported = 'thexport'
1119
1120 export def TheFunc(x = 0)
1121 enddef
1122 END
1123 writefile(lines, 'XExportReload')
1124 lines =<< trim END
1125 vim9script noclear
1126 g:loadCount += 1
1127 var s:reloaded = 'init'
1128 import './XExportReload' as exp
1129
1130 def Again(): string
1131 return 'again'
1132 enddef
1133
1134 exp.TheFunc()
1135
1136 if exists('s:loaded') | finish | endif
1137 var s:loaded = true
1138
1139 var s:notReloaded = 'yes'
1140 s:reloaded = 'first'
1141 def g:Values(): list<string>
1142 return [s:reloaded, s:notReloaded, Again(), Once(), exp.exported]
1143 enddef
1144
1145 def Once(): string
1146 return 'once'
1147 enddef
1148 END
1149 writefile(lines, 'XReloaded')
1150 g:loadCount = 0
1151 source XReloaded
1152 assert_equal(1, g:loadCount)
1153 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
1154 source XReloaded
1155 assert_equal(2, g:loadCount)
1156 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1157 source XReloaded
1158 assert_equal(3, g:loadCount)
1159 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1160
1161 delete('XReloaded')
1162 delete('XExportReload')
1163 delfunc g:Values
1164 unlet g:loadCount
1165
1166 lines =<< trim END
1167 vim9script
1168 def Inner()
1169 enddef
1170 END
1171 lines->writefile('XreloadScript.vim')
1172 source XreloadScript.vim
1173
1174 lines =<< trim END
1175 vim9script
1176 def Outer()
1177 def Inner()
1178 enddef
1179 enddef
1180 defcompile
1181 END
1182 lines->writefile('XreloadScript.vim')
1183 source XreloadScript.vim
1184
1185 delete('XreloadScript.vim')
1186enddef
1187
1188def Test_vim9_reload_import()
1189 var lines =<< trim END
1190 vim9script
1191 const var = ''
1192 var valone = 1234
1193 def MyFunc(arg: string)
1194 valone = 5678
1195 enddef
1196 END
1197 var morelines =<< trim END
1198 var valtwo = 222
1199 export def GetValtwo(): number
1200 return valtwo
1201 enddef
1202 END
1203 writefile(lines + morelines, 'Xreload.vim')
1204 source Xreload.vim
1205 source Xreload.vim
1206 source Xreload.vim
1207
1208 # cannot declare a var twice
1209 lines =<< trim END
1210 vim9script
1211 var valone = 1234
1212 var valone = 5678
1213 END
1214 writefile(lines, 'Xreload.vim')
1215 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
1216
1217 delete('Xreload.vim')
1218 delete('Ximport.vim')
1219enddef
1220
1221" if a script is reloaded with a script-local variable that changed its type, a
1222" compiled function using that variable must fail.
1223def Test_script_reload_change_type()
1224 var lines =<< trim END
1225 vim9script noclear
1226 var str = 'string'
1227 def g:GetStr(): string
1228 return str .. 'xxx'
1229 enddef
1230 END
1231 writefile(lines, 'Xreload.vim')
1232 source Xreload.vim
1233 echo g:GetStr()
1234
1235 lines =<< trim END
1236 vim9script noclear
1237 var str = 1234
1238 END
1239 writefile(lines, 'Xreload.vim')
1240 source Xreload.vim
1241 assert_fails('echo g:GetStr()', 'E1150:')
1242
1243 delfunc g:GetStr
1244 delete('Xreload.vim')
1245enddef
1246
1247" Define CallFunc so that the test can be compiled
1248command CallFunc echo 'nop'
1249
1250def Test_script_reload_from_function()
1251 var lines =<< trim END
1252 vim9script
1253
1254 if exists('g:loaded')
1255 finish
1256 endif
1257 g:loaded = 1
1258 delcommand CallFunc
1259 command CallFunc Func()
1260 def Func()
1261 so XreloadFunc.vim
1262 g:didTheFunc = 1
1263 enddef
1264 END
1265 writefile(lines, 'XreloadFunc.vim')
1266 source XreloadFunc.vim
1267 CallFunc
1268 assert_equal(1, g:didTheFunc)
1269
1270 delete('XreloadFunc.vim')
1271 delcommand CallFunc
1272 unlet g:loaded
1273 unlet g:didTheFunc
1274enddef
1275
1276def s:RetSome(): string
1277 return 'some'
1278enddef
1279
1280" Not exported function that is referenced needs to be accessed by the
1281" script-local name.
1282def Test_vim9_funcref()
1283 var sortlines =<< trim END
1284 vim9script
1285 def Compare(i1: number, i2: number): number
1286 return i2 - i1
1287 enddef
1288
1289 export def FastSort(): list<number>
1290 return range(5)->sort(Compare)
1291 enddef
1292
1293 export def GetString(arg: string): string
1294 return arg
1295 enddef
1296 END
1297 writefile(sortlines, 'Xsort.vim')
1298
1299 var lines =<< trim END
1300 vim9script
1301 import './Xsort.vim'
1302 def Test()
1303 g:result = Xsort.FastSort()
1304 enddef
1305 Test()
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001306 END
1307 writefile(lines, 'Xscript.vim')
1308 source Xscript.vim
1309 assert_equal([4, 3, 2, 1, 0], g:result)
1310 unlet g:result
Bram Moolenaard8448622022-01-07 21:39:52 +00001311
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001312 lines =<< trim END
1313 vim9script
Bram Moolenaard8448622022-01-07 21:39:52 +00001314 # using a function imported with "as"
1315 import './Xsort.vim' as anAlias
1316 assert_equal('yes', anAlias.GetString('yes'))
1317
1318 # using the function from a compiled function
1319 def TestMore(): string
1320 var s = s:anAlias.GetString('foo')
1321 return s .. anAlias.GetString('bar')
1322 enddef
1323 assert_equal('foobar', TestMore())
1324
1325 # error when using a function that isn't exported
1326 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
1327 END
1328 writefile(lines, 'Xscript.vim')
1329
Bram Moolenaard8448622022-01-07 21:39:52 +00001330 delete('Xsort.vim')
1331 delete('Xscript.vim')
1332
1333 var Funcref = function('s:RetSome')
1334 assert_equal('some', Funcref())
1335enddef
1336
1337" Check that when searching for "FilterFunc" it finds the import in the
1338" script where FastFilter() is called from, both as a string and as a direct
1339" function reference.
1340def Test_vim9_funcref_other_script()
1341 var filterLines =<< trim END
1342 vim9script
1343 export def FilterFunc(idx: number, val: number): bool
1344 return idx % 2 == 1
1345 enddef
1346 export def FastFilter(): list<number>
1347 return range(10)->filter('FilterFunc(v:key, v:val)')
1348 enddef
1349 export def FastFilterDirect(): list<number>
1350 return range(10)->filter(FilterFunc)
1351 enddef
1352 END
1353 writefile(filterLines, 'Xfilter.vim')
1354
1355 var lines =<< trim END
1356 vim9script
1357 import './Xfilter.vim' as filter
1358 def Test()
1359 var x: list<number> = filter.FastFilter()
1360 enddef
1361 Test()
1362 def TestDirect()
1363 var x: list<number> = filter.FastFilterDirect()
1364 enddef
1365 TestDirect()
1366 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001367 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +00001368 delete('Xfilter.vim')
1369enddef
1370
1371def Test_import_absolute()
1372 var import_lines = [
1373 'vim9script',
1374 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
1375 'def UseExported()',
1376 ' g:imported_abs = abs.exported',
1377 ' abs.exported = 8888',
1378 ' g:imported_after = abs.exported',
1379 'enddef',
1380 'UseExported()',
1381 'g:import_disassembled = execute("disass UseExported")',
1382 ]
1383 writefile(import_lines, 'Ximport_abs.vim')
1384 writefile(s:export_script_lines, 'Xexport_abs.vim')
1385
1386 source Ximport_abs.vim
1387
1388 assert_equal(9876, g:imported_abs)
1389 assert_equal(8888, g:imported_after)
1390 assert_match('<SNR>\d\+_UseExported\_s*' ..
1391 'g:imported_abs = abs.exported\_s*' ..
1392 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1393 '1 STOREG g:imported_abs\_s*' ..
1394 'abs.exported = 8888\_s*' ..
1395 '2 PUSHNR 8888\_s*' ..
1396 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1397 'g:imported_after = abs.exported\_s*' ..
1398 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1399 '5 STOREG g:imported_after',
1400 g:import_disassembled)
1401
1402 Undo_export_script_lines()
1403 unlet g:imported_abs
1404 unlet g:import_disassembled
1405
1406 delete('Ximport_abs.vim')
1407 delete('Xexport_abs.vim')
1408enddef
1409
1410def Test_import_rtp()
1411 var import_lines = [
1412 'vim9script',
1413 'import "Xexport_rtp.vim" as rtp',
1414 'g:imported_rtp = rtp.exported',
1415 ]
1416 writefile(import_lines, 'Ximport_rtp.vim')
1417 mkdir('import', 'p')
1418 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1419
1420 var save_rtp = &rtp
1421 &rtp = getcwd()
1422 source Ximport_rtp.vim
1423 &rtp = save_rtp
1424
1425 assert_equal(9876, g:imported_rtp)
1426
1427 Undo_export_script_lines()
1428 unlet g:imported_rtp
1429 delete('Ximport_rtp.vim')
1430 delete('import', 'rf')
1431enddef
1432
1433def Test_import_compile_error()
1434 var export_lines = [
1435 'vim9script',
1436 'export def ExpFunc(): string',
1437 ' return notDefined',
1438 'enddef',
1439 ]
1440 writefile(export_lines, 'Xexported.vim')
1441
1442 var import_lines = [
1443 'vim9script',
1444 'import "./Xexported.vim" as expo',
1445 'def ImpFunc()',
1446 ' echo expo.ExpFunc()',
1447 'enddef',
1448 'defcompile',
1449 ]
1450 writefile(import_lines, 'Ximport.vim')
1451
1452 try
1453 source Ximport.vim
1454 catch /E1001/
1455 # Error should be before the Xexported.vim file.
1456 assert_match('E1001: Variable not found: notDefined', v:exception)
1457 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1458 endtry
1459
1460 delete('Xexported.vim')
1461 delete('Ximport.vim')
1462enddef
1463
1464def Test_func_overrules_import_fails()
1465 var export_lines =<< trim END
1466 vim9script
1467 export def Func()
1468 echo 'imported'
1469 enddef
1470 END
1471 writefile(export_lines, 'XexportedFunc.vim')
1472
1473 var lines =<< trim END
1474 vim9script
1475 import './XexportedFunc.vim' as Func
1476 def Func()
1477 echo 'local to function'
1478 enddef
1479 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001480 v9.CheckScriptFailure(lines, 'E1213: Redefining imported item "Func"')
Bram Moolenaard8448622022-01-07 21:39:52 +00001481
1482 lines =<< trim END
1483 vim9script
1484 import './XexportedFunc.vim' as Func
1485 def Outer()
1486 def Func()
1487 echo 'local to function'
1488 enddef
1489 enddef
1490 defcompile
1491 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001492 v9.CheckScriptFailure(lines, 'E1236:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001493
1494 delete('XexportedFunc.vim')
1495enddef
1496
1497def Test_source_vim9_from_legacy()
1498 var vim9_lines =<< trim END
1499 vim9script
1500 var local = 'local'
1501 g:global = 'global'
1502 export var exported = 'exported'
1503 export def GetText(): string
1504 return 'text'
1505 enddef
1506 END
1507 writefile(vim9_lines, 'Xvim9_script.vim')
1508
1509 var legacy_lines =<< trim END
1510 source Xvim9_script.vim
1511
1512 call assert_false(exists('local'))
1513 call assert_false(exists('exported'))
1514 call assert_false(exists('s:exported'))
1515 call assert_equal('global', global)
1516 call assert_equal('global', g:global)
Bram Moolenaard8448622022-01-07 21:39:52 +00001517 END
1518 writefile(legacy_lines, 'Xlegacy_script.vim')
1519
1520 source Xlegacy_script.vim
1521 assert_equal('global', g:global)
1522 unlet g:global
1523
1524 delete('Xlegacy_script.vim')
1525 delete('Xvim9_script.vim')
1526enddef
1527
Bram Moolenaarc43e6232022-01-13 20:51:56 +00001528def Test_import_vim9_from_legacy()
1529 var vim9_lines =<< trim END
1530 vim9script
1531 var local = 'local'
1532 g:global = 'global'
1533 export var exported = 'exported'
1534 export def GetText(): string
1535 return 'text'
1536 enddef
1537 END
1538 writefile(vim9_lines, 'Xvim9_export.vim')
1539
1540 var legacy_lines =<< trim END
1541 import './Xvim9_export.vim' as vim9
1542
1543 call assert_false(exists('vim9'))
1544 call assert_false(exists('local'))
1545 call assert_false(exists('s:vim9.local'))
1546 call assert_equal('global', global)
1547 call assert_equal('global', g:global)
1548 call assert_false(exists('exported'))
1549 call assert_false(exists('s:exported'))
1550 call assert_false(exists('*GetText'))
1551
1552 " imported symbol is script-local
1553 call assert_equal('exported', s:vim9.exported)
1554 call assert_equal('text', s:vim9.GetText())
1555 END
1556 writefile(legacy_lines, 'Xlegacy_script.vim')
1557
1558 source Xlegacy_script.vim
1559 assert_equal('global', g:global)
1560 unlet g:global
1561
1562 delete('Xlegacy_script.vim')
1563 delete('Xvim9_export.vim')
1564enddef
1565
Bram Moolenaard8448622022-01-07 21:39:52 +00001566def Test_cmdline_win()
1567 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
1568 # the command line window.
1569 mkdir('rtp/syntax', 'p')
1570 var export_lines =<< trim END
1571 vim9script
1572 export var That = 'yes'
1573 END
1574 writefile(export_lines, 'rtp/syntax/Xexport.vim')
1575 var import_lines =<< trim END
1576 vim9script
1577 import './Xexport.vim' as exp
1578 echo exp.That
1579 END
1580 writefile(import_lines, 'rtp/syntax/vim.vim')
1581 var save_rtp = &rtp
1582 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
1583 syntax on
1584 augroup CmdWin
1585 autocmd CmdwinEnter * g:got_there = 'yes'
1586 augroup END
1587 # this will open and also close the cmdline window
1588 feedkeys('q:', 'xt')
1589 assert_equal('yes', g:got_there)
1590
1591 augroup CmdWin
1592 au!
1593 augroup END
1594 &rtp = save_rtp
1595 delete('rtp', 'rf')
1596enddef
1597
1598def Test_import_gone_when_sourced_twice()
1599 var exportlines =<< trim END
1600 vim9script
1601 if exists('g:guard')
1602 finish
1603 endif
1604 g:guard = 1
1605 export var name = 'someName'
1606 END
1607 writefile(exportlines, 'XexportScript.vim')
1608
1609 var lines =<< trim END
1610 vim9script
1611 import './XexportScript.vim' as expo
1612 def g:GetName(): string
1613 return expo.name
1614 enddef
1615 END
1616 writefile(lines, 'XscriptImport.vim')
1617 so XscriptImport.vim
1618 assert_equal('someName', g:GetName())
1619
1620 so XexportScript.vim
1621 assert_fails('call g:GetName()', 'E1149:')
1622
1623 delfunc g:GetName
1624 delete('XexportScript.vim')
1625 delete('XscriptImport.vim')
1626 unlet g:guard
1627enddef
1628
Bram Moolenaar160aa862022-01-10 21:29:57 +00001629" test using an auto-loaded function and variable
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001630def Test_vim9_autoload_full_name()
Bram Moolenaar160aa862022-01-10 21:29:57 +00001631 var lines =<< trim END
1632 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001633 export def Gettest(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00001634 return 'test'
1635 enddef
1636 g:some#name = 'name'
1637 g:some#dict = {key: 'value'}
1638
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001639 export def Varargs(a1: string, ...l: list<string>): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00001640 return a1 .. l[0] .. l[1]
1641 enddef
1642 END
1643
1644 mkdir('Xdir/autoload', 'p')
1645 writefile(lines, 'Xdir/autoload/some.vim')
1646 var save_rtp = &rtp
1647 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1648
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001649 assert_equal('test', g:some#Gettest())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001650 assert_equal('name', g:some#name)
1651 assert_equal('value', g:some#dict.key)
1652 g:some#other = 'other'
1653 assert_equal('other', g:some#other)
1654
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001655 assert_equal('abc', some#Varargs('a', 'b', 'c'))
Bram Moolenaar160aa862022-01-10 21:29:57 +00001656
1657 # upper case script name works
1658 lines =<< trim END
1659 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001660 export def GetOther(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00001661 return 'other'
1662 enddef
1663 END
1664 writefile(lines, 'Xdir/autoload/Other.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001665 assert_equal('other', g:Other#GetOther())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001666
1667 delete('Xdir', 'rf')
1668 &rtp = save_rtp
1669enddef
1670
1671def Test_vim9script_autoload()
1672 mkdir('Xdir/autoload', 'p')
1673 var save_rtp = &rtp
1674 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1675
Bram Moolenaarfd218c82022-01-18 16:26:24 +00001676 # when the path has "/autoload/" prefix is not needed
Bram Moolenaar160aa862022-01-10 21:29:57 +00001677 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00001678 vim9script
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001679 g:prefixed_loaded += 1
Bram Moolenaar160aa862022-01-10 21:29:57 +00001680
1681 export def Gettest(): string
1682 return 'test'
1683 enddef
1684
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001685 export var name = 'name'
1686
1687 export func GetFunc()
1688 return Gettest() .. 'more' .. s:name
Bram Moolenaar160aa862022-01-10 21:29:57 +00001689 endfunc
1690
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001691 export def GetDef(): string
1692 return Gettest() .. 'more' .. name
1693 enddef
1694
Bram Moolenaar160aa862022-01-10 21:29:57 +00001695 export final fname = 'final'
1696 export const cname = 'const'
1697 END
1698 writefile(lines, 'Xdir/autoload/prefixed.vim')
1699
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001700 g:prefixed_loaded = 0
1701 g:expected_loaded = 0
Bram Moolenaar160aa862022-01-10 21:29:57 +00001702 lines =<< trim END
1703 vim9script
1704 import autoload 'prefixed.vim'
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001705 assert_equal(g:expected_loaded, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00001706 assert_equal('test', prefixed.Gettest())
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001707 assert_equal(1, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00001708
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001709 assert_equal('testmorename', prefixed.GetFunc())
1710 assert_equal('testmorename', prefixed.GetDef())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001711 assert_equal('name', prefixed.name)
1712 assert_equal('final', prefixed.fname)
1713 assert_equal('const', prefixed.cname)
1714 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001715 v9.CheckScriptSuccess(lines)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00001716 # can source it again, autoload script not loaded again
1717 g:expected_loaded = 1
Bram Moolenaar62aec932022-01-29 21:45:34 +00001718 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00001719
1720 # can also get the items by autoload name
1721 lines =<< trim END
1722 call assert_equal('test', prefixed#Gettest())
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001723 call assert_equal('testmorename', prefixed#GetFunc())
Bram Moolenaar160aa862022-01-10 21:29:57 +00001724 call assert_equal('name', prefixed#name)
1725 call assert_equal('final', prefixed#fname)
1726 call assert_equal('const', prefixed#cname)
1727 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001728 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00001729
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001730 unlet g:prefixed_loaded
1731 unlet g:expected_loaded
1732 delete('Xdir', 'rf')
1733 &rtp = save_rtp
1734enddef
1735
Bram Moolenaard02dce22022-01-18 17:43:04 +00001736def Test_import_autoload_not_exported()
1737 mkdir('Xdir/autoload', 'p')
1738 var save_rtp = &rtp
1739 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1740
1741 # error when using an item that is not exported from an autoload script
1742 var exportLines =<< trim END
1743 vim9script
1744 var notExported = 123
1745 def NotExport()
1746 echo 'nop'
1747 enddef
1748 END
1749 writefile(exportLines, 'Xdir/autoload/notExport1.vim')
1750
1751 var lines =<< trim END
1752 vim9script
1753 import autoload 'notExport1.vim'
1754 echo notExport1.notFound
1755 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001756 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001757
1758 lines =<< trim END
1759 vim9script
1760 import autoload 'notExport1.vim'
1761 echo notExport1.notExported
1762 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001763 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001764
1765 lines =<< trim END
1766 vim9script
1767 import autoload 'notExport1.vim'
1768 echo notExport1.NotFunc()
1769 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001770 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001771
1772 lines =<< trim END
1773 vim9script
1774 import autoload 'notExport1.vim'
1775 echo notExport1.NotExport()
1776 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001777 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001778
1779 lines =<< trim END
1780 vim9script
1781 import autoload 'notExport1.vim'
1782 echo 'text'->notExport1.NotFunc()
1783 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001784 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001785
1786 lines =<< trim END
1787 vim9script
1788 import autoload 'notExport1.vim'
1789 echo 'text'->notExport1.NotExport()
1790 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001791 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001792
1793 # using a :def function we use a different autoload script every time so that
1794 # the function is compiled without the script loaded
1795 writefile(exportLines, 'Xdir/autoload/notExport2.vim')
1796 lines =<< trim END
1797 vim9script
1798 import autoload 'notExport2.vim'
1799 def Testit()
1800 echo notExport2.notFound
1801 enddef
1802 Testit()
1803 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001804 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport2#notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001805
1806 writefile(exportLines, 'Xdir/autoload/notExport3.vim')
1807 lines =<< trim END
1808 vim9script
1809 import autoload 'notExport3.vim'
1810 def Testit()
1811 echo notExport3.notExported
1812 enddef
1813 Testit()
1814 END
1815 # don't get E1049 because it is too complicated to figure out
Bram Moolenaar62aec932022-01-29 21:45:34 +00001816 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport3#notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001817
1818 writefile(exportLines, 'Xdir/autoload/notExport4.vim')
1819 lines =<< trim END
1820 vim9script
1821 import autoload 'notExport4.vim'
1822 def Testit()
1823 echo notExport4.NotFunc()
1824 enddef
1825 Testit()
1826 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001827 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport4#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001828
1829 writefile(exportLines, 'Xdir/autoload/notExport5.vim')
1830 lines =<< trim END
1831 vim9script
1832 import autoload 'notExport5.vim'
1833 def Testit()
1834 echo notExport5.NotExport()
1835 enddef
1836 Testit()
1837 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001838 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport5#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001839
1840 writefile(exportLines, 'Xdir/autoload/notExport6.vim')
1841 lines =<< trim END
1842 vim9script
1843 import autoload 'notExport6.vim'
1844 def Testit()
1845 echo 'text'->notExport6.NotFunc()
1846 enddef
1847 Testit()
1848 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001849 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport6#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001850
1851 writefile(exportLines, 'Xdir/autoload/notExport7.vim')
1852 lines =<< trim END
1853 vim9script
1854 import autoload 'notExport7.vim'
1855 def Testit()
1856 echo 'text'->notExport7.NotExport()
1857 enddef
1858 Testit()
1859 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001860 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport7#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00001861
1862 delete('Xdir', 'rf')
1863 &rtp = save_rtp
1864enddef
1865
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001866def Test_vim9script_autoload_call()
1867 mkdir('Xdir/autoload', 'p')
1868 var save_rtp = &rtp
1869 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1870
1871 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00001872 vim9script
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001873
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00001874 export def RetArg(arg: string): string
1875 return arg
1876 enddef
1877
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001878 export def Getother()
1879 g:result = 'other'
1880 enddef
1881 END
Bram Moolenaar5d982692022-01-12 15:15:27 +00001882 writefile(lines, 'Xdir/autoload/another.vim')
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001883
1884 lines =<< trim END
1885 vim9script
Bram Moolenaar5d982692022-01-12 15:15:27 +00001886 import autoload 'another.vim'
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00001887
1888 # compile this before 'another.vim' is loaded
1889 def CallAnother()
1890 assert_equal('foo', 'foo'->another.RetArg())
1891 enddef
1892 CallAnother()
1893
Bram Moolenaar5d982692022-01-12 15:15:27 +00001894 call another.Getother()
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001895 assert_equal('other', g:result)
Bram Moolenaar3d8e25a2022-01-22 11:00:02 +00001896
1897 assert_equal('arg', call('another.RetArg', ['arg']))
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00001898
1899 verbose function another.Getother
1900 # should we disallow this?
1901 verbose function another#Getother
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001902 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001903 v9.CheckScriptSuccess(lines)
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00001904
1905 unlet g:result
Bram Moolenaar160aa862022-01-10 21:29:57 +00001906 delete('Xdir', 'rf')
1907 &rtp = save_rtp
1908enddef
1909
Bram Moolenaarb697dc22022-01-22 11:27:29 +00001910def Test_vim9script_noclear_autoload()
1911 mkdir('Xdir/autoload', 'p')
1912 var save_rtp = &rtp
1913 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1914
1915 var lines =<< trim END
1916 vim9script
1917 export def Func(): string
1918 return 'called'
1919 enddef
1920 g:double_loaded = 'yes'
1921 END
1922 writefile(lines, 'Xdir/autoload/double.vim')
1923
1924 lines =<< trim END
1925 vim9script noclear
1926 if exists('g:script_loaded')
1927 finish
1928 endif
1929 g:script_loaded = true
1930
1931 import autoload 'double.vim'
1932 nnoremap <F3> <ScriptCmd>g:result = double.Func()<CR>
1933 END
1934 g:double_loaded = 'no'
1935 writefile(lines, 'Xloaddouble')
1936 source Xloaddouble
1937 assert_equal('no', g:double_loaded)
1938 assert_equal(true, g:script_loaded)
1939 source Xloaddouble
1940 feedkeys("\<F3>", 'xt')
1941 assert_equal('called', g:result)
1942 assert_equal('yes', g:double_loaded)
1943
1944 delete('Xloaddouble')
1945 unlet g:double_loaded
1946 unlet g:script_loaded
1947 unlet g:result
1948 delete('Xdir', 'rf')
1949 &rtp = save_rtp
1950enddef
1951
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00001952def Test_vim9script_autoload_duplicate()
1953 mkdir('Xdir/autoload', 'p')
1954
1955 var lines =<< trim END
1956 vim9script
1957
1958 export def Func()
1959 enddef
1960
1961 def Func()
1962 enddef
1963 END
1964 writefile(lines, 'Xdir/autoload/dupfunc.vim')
1965 assert_fails('source Xdir/autoload/dupfunc.vim', 'E1073:')
1966
1967 lines =<< trim END
1968 vim9script
1969
1970 def Func()
1971 enddef
1972
1973 export def Func()
1974 enddef
1975 END
1976 writefile(lines, 'Xdir/autoload/dup2func.vim')
1977 assert_fails('source Xdir/autoload/dup2func.vim', 'E1073:')
1978
1979 lines =<< trim END
1980 vim9script
1981
1982 def Func()
1983 enddef
1984
1985 export var Func = 'asdf'
1986 END
1987 writefile(lines, 'Xdir/autoload/dup3func.vim')
1988 assert_fails('source Xdir/autoload/dup3func.vim', 'E1041: Redefining script item Func')
1989
1990 lines =<< trim END
1991 vim9script
1992
1993 export var Func = 'asdf'
1994
1995 def Func()
1996 enddef
1997 END
1998 writefile(lines, 'Xdir/autoload/dup4func.vim')
1999 assert_fails('source Xdir/autoload/dup4func.vim', 'E707:')
2000
2001 lines =<< trim END
2002 vim9script
2003
2004 var Func = 'asdf'
2005
2006 export def Func()
2007 enddef
2008 END
2009 writefile(lines, 'Xdir/autoload/dup5func.vim')
2010 assert_fails('source Xdir/autoload/dup5func.vim', 'E707:')
2011
2012 lines =<< trim END
2013 vim9script
2014
2015 export def Func()
2016 enddef
2017
2018 var Func = 'asdf'
2019 END
2020 writefile(lines, 'Xdir/autoload/dup6func.vim')
2021 assert_fails('source Xdir/autoload/dup6func.vim', 'E1041: Redefining script item Func')
2022
2023 delete('Xdir', 'rf')
2024enddef
2025
Bram Moolenaar2017d6f2022-01-20 19:38:46 +00002026def Test_autoload_missing_function_name()
2027 mkdir('Xdir/autoload', 'p')
2028
2029 var lines =<< trim END
2030 vim9script
2031
2032 def loadme#()
2033 enddef
2034 END
2035 writefile(lines, 'Xdir/autoload/loadme.vim')
2036 assert_fails('source Xdir/autoload/loadme.vim', 'E129:')
2037
2038 delete('Xdir', 'rf')
2039enddef
2040
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002041def Test_autoload_name_wrong()
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002042 var lines =<< trim END
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002043 def Xscriptname#Func()
2044 enddef
2045 END
2046 writefile(lines, 'Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002047 v9.CheckScriptFailure(lines, 'E746:')
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00002048 delete('Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002049
2050 mkdir('Xdir/autoload', 'p')
2051 lines =<< trim END
2052 vim9script
2053 def somescript#Func()
2054 enddef
2055 END
2056 writefile(lines, 'Xdir/autoload/somescript.vim')
2057 assert_fails('source Xdir/autoload/somescript.vim', 'E1263:')
2058
2059 delete('Xdir', 'rf')
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002060enddef
2061
Bram Moolenaard041f422022-01-12 19:54:00 +00002062def Test_import_autoload_postponed()
2063 mkdir('Xdir/autoload', 'p')
2064 var save_rtp = &rtp
2065 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2066
2067 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002068 vim9script
Bram Moolenaard041f422022-01-12 19:54:00 +00002069
2070 g:loaded_postponed = 'true'
2071 export var variable = 'bla'
2072 export def Function(): string
2073 return 'bla'
2074 enddef
2075 END
2076 writefile(lines, 'Xdir/autoload/postponed.vim')
2077
2078 lines =<< trim END
2079 vim9script
2080
2081 import autoload 'postponed.vim'
2082 def Tryit()
2083 echo postponed.variable
2084 echo postponed.Function()
2085 enddef
2086 defcompile
2087 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002088 v9.CheckScriptSuccess(lines)
Bram Moolenaard041f422022-01-12 19:54:00 +00002089 assert_false(exists('g:loaded_postponed'))
Bram Moolenaar62aec932022-01-29 21:45:34 +00002090 v9.CheckScriptSuccess(lines + ['Tryit()'])
Bram Moolenaard041f422022-01-12 19:54:00 +00002091 assert_equal('true', g:loaded_postponed)
2092
2093 unlet g:loaded_postponed
2094 delete('Xdir', 'rf')
2095 &rtp = save_rtp
2096enddef
2097
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002098def Test_import_autoload_override()
2099 mkdir('Xdir/autoload', 'p')
2100 var save_rtp = &rtp
2101 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2102 test_override('autoload', 1)
2103
2104 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002105 vim9script
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002106
2107 g:loaded_override = 'true'
2108 export var variable = 'bla'
2109 export def Function(): string
2110 return 'bla'
2111 enddef
2112 END
2113 writefile(lines, 'Xdir/autoload/override.vim')
2114
2115 lines =<< trim END
2116 vim9script
2117
2118 import autoload 'override.vim'
2119 assert_equal('true', g:loaded_override)
2120
2121 def Tryit()
2122 echo override.doesNotExist
2123 enddef
2124 defcompile
2125 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002126 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1)
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002127
2128 test_override('autoload', 0)
2129 unlet g:loaded_override
2130 delete('Xdir', 'rf')
2131 &rtp = save_rtp
2132enddef
2133
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002134def Test_autoload_mapping()
2135 mkdir('Xdir/autoload', 'p')
2136 var save_rtp = &rtp
2137 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2138
2139 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002140 vim9script
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002141
2142 g:toggle_loaded = 'yes'
2143
2144 export def Toggle(): string
2145 return ":g:toggle_called = 'yes'\<CR>"
2146 enddef
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002147 export def Doit()
2148 g:doit_called = 'yes'
2149 enddef
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002150 END
2151 writefile(lines, 'Xdir/autoload/toggle.vim')
2152
2153 lines =<< trim END
2154 vim9script
2155
2156 import autoload 'toggle.vim'
2157
2158 nnoremap <silent> <expr> tt toggle.Toggle()
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002159 nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR>
2160 nnoremap <silent> yy <Cmd>toggle.Doit()<CR>
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002161 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002162 v9.CheckScriptSuccess(lines)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002163 assert_false(exists("g:toggle_loaded"))
2164 assert_false(exists("g:toggle_called"))
Bram Moolenaar6079da72022-01-18 14:16:59 +00002165 assert_match('\d A: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002166
2167 feedkeys("tt", 'xt')
2168 assert_equal('yes', g:toggle_loaded)
2169 assert_equal('yes', g:toggle_called)
Bram Moolenaar6079da72022-01-18 14:16:59 +00002170 assert_match('\d: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002171
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002172 feedkeys("xx", 'xt')
2173 assert_equal('yes', g:doit_called)
2174
2175 assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle')
2176
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002177 nunmap tt
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002178 nunmap xx
2179 nunmap yy
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002180 unlet g:toggle_loaded
2181 unlet g:toggle_called
2182 delete('Xdir', 'rf')
2183 &rtp = save_rtp
2184enddef
2185
Bram Moolenaar160aa862022-01-10 21:29:57 +00002186def Test_vim9script_autoload_fails()
2187 var lines =<< trim END
2188 vim9script autoload
2189 var n = 0
2190 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002191 v9.CheckScriptFailure(lines, 'E475: Invalid argument: autoload')
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002192
2193 lines =<< trim END
2194 vim9script noclear noclear
2195 var n = 0
2196 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002197 v9.CheckScriptFailure(lines, 'E983: Duplicate argument: noclear')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002198enddef
2199
2200def Test_import_autoload_fails()
2201 var lines =<< trim END
2202 vim9script
2203 import autoload autoload 'prefixed.vim'
2204 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002205 v9.CheckScriptFailure(lines, 'E121: Undefined variable: autoload')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002206
2207 lines =<< trim END
2208 vim9script
Bram Moolenaar1836d612022-01-18 13:14:47 +00002209 import autoload './doesNotExist.vim'
Bram Moolenaar160aa862022-01-10 21:29:57 +00002210 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002211 v9.CheckScriptFailure(lines, 'E1264:')
Bram Moolenaar1836d612022-01-18 13:14:47 +00002212
2213 lines =<< trim END
2214 vim9script
2215 import autoload '/dir/doesNotExist.vim'
2216 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002217 v9.CheckScriptFailure(lines, 'E1264:')
Bram Moolenaar1836d612022-01-18 13:14:47 +00002218
2219 lines =<< trim END
2220 vim9script
2221 import autoload 'doesNotExist.vim'
2222 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002223 v9.CheckScriptFailure(lines, 'E1053: Could not import "doesNotExist.vim"')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002224enddef
2225
2226" test disassembling an auto-loaded function starting with "debug"
2227def Test_vim9_autoload_disass()
2228 mkdir('Xdir/autoload', 'p')
2229 var save_rtp = &rtp
2230 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2231
2232 var lines =<< trim END
2233 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002234 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002235 return 'debug'
2236 enddef
2237 END
2238 writefile(lines, 'Xdir/autoload/debugit.vim')
2239
2240 lines =<< trim END
2241 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002242 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002243 return 'profile'
2244 enddef
2245 END
2246 writefile(lines, 'Xdir/autoload/profileit.vim')
2247
2248 lines =<< trim END
2249 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002250 assert_equal('debug', debugit#Test())
2251 disass debugit#Test
2252 assert_equal('profile', profileit#Test())
2253 disass profileit#Test
Bram Moolenaar160aa862022-01-10 21:29:57 +00002254 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002255 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002256
2257 delete('Xdir', 'rf')
2258 &rtp = save_rtp
2259enddef
2260
2261" test using a vim9script that is auto-loaded from an autocmd
2262def Test_vim9_aucmd_autoload()
2263 var lines =<< trim END
2264 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002265 export def Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002266 echomsg getreg('"')
2267 enddef
2268 END
2269
2270 mkdir('Xdir/autoload', 'p')
2271 writefile(lines, 'Xdir/autoload/foo.vim')
2272 var save_rtp = &rtp
2273 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2274 augroup test
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002275 autocmd TextYankPost * call foo#Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002276 augroup END
2277
2278 normal Y
2279
2280 augroup test
2281 autocmd!
2282 augroup END
2283 delete('Xdir', 'rf')
2284 &rtp = save_rtp
2285enddef
2286
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002287" test using a autoloaded file that is case sensitive
2288def Test_vim9_autoload_case_sensitive()
2289 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002290 vim9script
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002291 export def CaseSensitive(): string
2292 return 'done'
2293 enddef
2294 END
2295
2296 mkdir('Xdir/autoload', 'p')
2297 writefile(lines, 'Xdir/autoload/CaseSensitive.vim')
2298 var save_rtp = &rtp
2299 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2300
2301 lines =<< trim END
2302 vim9script
2303 import autoload 'CaseSensitive.vim'
2304 assert_equal('done', CaseSensitive.CaseSensitive())
2305 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002306 v9.CheckScriptSuccess(lines)
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002307
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002308 if !has('fname_case')
2309 lines =<< trim END
2310 vim9script
2311 import autoload 'CaseSensitive.vim'
2312 import autoload 'casesensitive.vim'
2313 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002314 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002315 endif
2316
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002317 delete('Xdir', 'rf')
2318 &rtp = save_rtp
2319enddef
2320
Bram Moolenaar160aa862022-01-10 21:29:57 +00002321" This was causing a crash because suppress_errthrow wasn't reset.
2322def Test_vim9_autoload_error()
2323 var lines =<< trim END
2324 vim9script
2325 def crash#func()
2326 try
2327 for x in List()
2328 endfor
2329 catch
2330 endtry
2331 g:ok = true
2332 enddef
2333 fu List()
2334 invalid
2335 endfu
2336 try
2337 alsoinvalid
2338 catch /wontmatch/
2339 endtry
2340 END
2341 call mkdir('Xruntime/autoload', 'p')
2342 call writefile(lines, 'Xruntime/autoload/crash.vim')
2343
2344 # run in a separate Vim to avoid the side effects of assert_fails()
2345 lines =<< trim END
2346 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
2347 call crash#func()
2348 call writefile(['ok'], 'Xdidit')
2349 qall!
2350 END
2351 writefile(lines, 'Xscript')
Bram Moolenaar62aec932022-01-29 21:45:34 +00002352 g:RunVim([], [], '-S Xscript')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002353 assert_equal(['ok'], readfile('Xdidit'))
2354
2355 delete('Xdidit')
2356 delete('Xscript')
2357 delete('Xruntime', 'rf')
2358
2359 lines =<< trim END
2360 vim9script
2361 var foo#bar = 'asdf'
2362 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002363 v9.CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002364enddef
2365
Bram Moolenaard8448622022-01-07 21:39:52 +00002366
2367" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker