blob: 5729d73e579a298a6bbae717a428e2a0c9df6a85 [file] [log] [blame]
Bram Moolenaard8448622022-01-07 21:39:52 +00001" Test import/export of the Vim9 script language.
Bram Moolenaar160aa862022-01-10 21:29:57 +00002" Also the autoload mechanism.
Bram Moolenaard8448622022-01-07 21:39:52 +00003
4source check.vim
5source term_util.vim
Bram Moolenaar62aec932022-01-29 21:45:34 +00006import './vim9.vim' as v9
Bram Moolenaard8448622022-01-07 21:39:52 +00007
8let s:export_script_lines =<< trim END
9 vim9script
10 var name: string = 'bob'
11 def Concat(arg: string): string
12 return name .. arg
13 enddef
14 g:result = Concat('bie')
15 g:localname = name
16
17 export const CONST = 1234
18 export var exported = 9876
19 export var exp_name = 'John'
20 export def Exported(): string
21 return 'Exported'
22 enddef
23 export def ExportedValue(): number
24 return exported
25 enddef
26 export def ExportedInc()
27 exported += 5
28 enddef
29 export final theList = [1]
Bram Moolenaar857c8bb2022-01-15 21:08:19 +000030 export def AddSome(s: string): string
31 return s .. 'some'
32 enddef
33 export var AddRef = AddSome
Bram Moolenaard8448622022-01-07 21:39:52 +000034END
35
Bram Moolenaar62aec932022-01-29 21:45:34 +000036def s:Undo_export_script_lines()
Bram Moolenaard8448622022-01-07 21:39:52 +000037 unlet g:result
38 unlet g:localname
39enddef
40
41def Test_vim9_import_export()
42 writefile(s:export_script_lines, 'Xexport.vim')
43 var import_script_lines =<< trim END
44 vim9script
45 var dir = './'
46 var ext = ".vim"
47 import dir .. 'Xexport' .. ext as expo
48
49 g:exported1 = expo.exported
50 expo.exported += 3
51 g:exported2 = expo.exported
52 g:exported3 = expo.ExportedValue()
53
54 expo.ExportedInc()
55 g:exported_i1 = expo.exported
56 g:exported_i2 = expo.ExportedValue()
57
58 expo.exported = 11
59 g:exported_s1 = expo.exported
60 g:exported_s2 = expo.ExportedValue()
61
62 g:imported_func = expo.Exported()
63
64 def GetExported(): string
65 var local_dict = {ref: expo.Exported}
66 return local_dict.ref()
67 enddef
68 g:funcref_result = GetExported()
69
Bram Moolenaar21f0d6c2022-01-20 17:35:49 +000070 def GetName(): string
71 return expo.exp_name .. 'son'
72 enddef
73 g:long_name = GetName()
74
Bram Moolenaard8448622022-01-07 21:39:52 +000075 g:imported_name = expo.exp_name
76 expo.exp_name ..= ' Doe'
Bram Moolenaar47036b62022-01-16 21:18:53 +000077 expo.exp_name = expo.exp_name .. ' Maar'
Bram Moolenaard8448622022-01-07 21:39:52 +000078 g:imported_name_appended = expo.exp_name
79 g:exported_later = expo.exported
80
81 expo.theList->add(2)
82 assert_equal([1, 2], expo.theList)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +000083
84 assert_equal('andthensome', 'andthen'->expo.AddSome())
85 assert_equal('awesome', 'awe'->expo.AddRef())
Bram Moolenaard8448622022-01-07 21:39:52 +000086 END
87 writefile(import_script_lines, 'Ximport.vim')
88 source Ximport.vim
89
90 assert_equal('bobbie', g:result)
91 assert_equal('bob', g:localname)
92 assert_equal(9876, g:exported1)
93 assert_equal(9879, g:exported2)
94 assert_equal(9879, g:exported3)
95
96 assert_equal(9884, g:exported_i1)
97 assert_equal(9884, g:exported_i2)
98
99 assert_equal(11, g:exported_s1)
100 assert_equal(11, g:exported_s2)
101 assert_equal(11, g:exported_later)
102
103 assert_equal('Exported', g:imported_func)
104 assert_equal('Exported', g:funcref_result)
105 assert_equal('John', g:imported_name)
Bram Moolenaar21f0d6c2022-01-20 17:35:49 +0000106 assert_equal('Johnson', g:long_name)
Bram Moolenaar47036b62022-01-16 21:18:53 +0000107 assert_equal('John Doe Maar', g:imported_name_appended)
Bram Moolenaard8448622022-01-07 21:39:52 +0000108 assert_false(exists('g:name'))
109
110 Undo_export_script_lines()
111 unlet g:exported1
112 unlet g:exported2
113 unlet g:exported3
114 unlet g:exported_i1
115 unlet g:exported_i2
116 unlet g:exported_later
117 unlet g:imported_func
Bram Moolenaar21f0d6c2022-01-20 17:35:49 +0000118 unlet g:imported_name g:long_name g:imported_name_appended
Bram Moolenaard8448622022-01-07 21:39:52 +0000119 delete('Ximport.vim')
120
121 # similar, with line breaks
122 var import_line_break_script_lines =<< trim END
123 vim9script
124 import './Xexport.vim'
125 as expo
126 g:exported = expo.exported
127 expo.exported += 7
128 g:exported_added = expo.exported
129 g:imported_func = expo.Exported()
130 END
131 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
132 source Ximport_lbr.vim
133
134 assert_equal(11, g:exported)
135 assert_equal(18, g:exported_added)
136 assert_equal('Exported', g:imported_func)
137
138 # exported script not sourced again
139 assert_false(exists('g:result'))
140 unlet g:exported
141 unlet g:exported_added
142 unlet g:imported_func
143 delete('Ximport_lbr.vim')
144
Bram Moolenaar68854a82022-01-31 18:59:13 +0000145 var import_shadows_cmdmod_lines =<< trim END
146 vim9script
147 import './Xexport.vim' as vim9
148 vim9.exp_name = 'Shadow'
149 assert_equal('Shadow', vim9.exp_name)
150 END
151 v9.CheckScriptSuccess(import_shadows_cmdmod_lines)
152
Bram Moolenaard8448622022-01-07 21:39:52 +0000153 var line_break_before_dot =<< trim END
154 vim9script
155 import './Xexport.vim' as expo
156 g:exported = expo
157 .exported
158 END
159 writefile(line_break_before_dot, 'Ximport_lbr_before_dot.vim')
160 assert_fails('source Ximport_lbr_before_dot.vim', 'E1060:', '', 3)
161 delete('Ximport_lbr_before_dot.vim')
162
163 var line_break_after_dot =<< trim END
164 vim9script
165 import './Xexport.vim' as expo
166 g:exported = expo.
167 exported
168 END
169 writefile(line_break_after_dot, 'Ximport_lbr_after_dot.vim')
170 assert_fails('source Ximport_lbr_after_dot.vim', 'E1074:', '', 3)
171 delete('Ximport_lbr_after_dot.vim')
172
173 var import_star_as_lines =<< trim END
174 vim9script
175 import './Xexport.vim' as Export
176 def UseExport()
177 g:exported_def = Export.exported
178 enddef
179 g:exported_script = Export.exported
180 assert_equal(1, exists('Export.exported'))
181 assert_equal(0, exists('Export.notexported'))
182 UseExport()
183 END
184 writefile(import_star_as_lines, 'Ximport.vim')
185 source Ximport.vim
186
187 assert_equal(18, g:exported_def)
188 assert_equal(18, g:exported_script)
189 unlet g:exported_def
190 unlet g:exported_script
191
192 var import_star_as_lines_no_dot =<< trim END
193 vim9script
194 import './Xexport.vim' as Export
195 def Func()
196 var dummy = 1
197 var imported = Export + dummy
198 enddef
199 defcompile
200 END
201 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
202 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
203
204 var import_star_as_lines_dot_space =<< trim END
205 vim9script
206 import './Xexport.vim' as Export
207 def Func()
208 var imported = Export . exported
209 enddef
210 defcompile
211 END
212 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
213 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
214
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000215 writefile(s:export_script_lines, 'Xexport2.vim')
216 var import_as_duplicated =<< trim END
Bram Moolenaard8448622022-01-07 21:39:52 +0000217 vim9script
218 import './Xexport.vim' as expo
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000219 import './Xexport2.vim' as expo
Bram Moolenaard8448622022-01-07 21:39:52 +0000220 END
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000221 writefile(import_as_duplicated, 'Ximport.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000222 assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000223 delete('Xexport2.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000224
225 var import_star_as_lines_script_no_dot =<< trim END
226 vim9script
227 import './Xexport.vim' as Export
228 g:imported_script = Export exported
229 END
230 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
231 assert_fails('source Ximport.vim', 'E1060: Expected dot after name: Export exported')
232
233 var import_star_as_lines_script_space_after_dot =<< trim END
234 vim9script
235 import './Xexport.vim' as Export
236 g:imported_script = Export. exported
237 END
238 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
239 assert_fails('source Ximport.vim', 'E1074:')
240
241 var import_star_as_lines_missing_name =<< trim END
242 vim9script
243 import './Xexport.vim' as Export
244 def Func()
245 var imported = Export.
246 enddef
247 defcompile
248 END
249 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
250 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
251
252 var import_star_as_lbr_lines =<< trim END
253 vim9script
254 import './Xexport.vim'
255 as Export
256 def UseExport()
257 g:exported = Export.exported
258 enddef
259 UseExport()
260 END
261 writefile(import_star_as_lbr_lines, 'Ximport.vim')
262 source Ximport.vim
263 assert_equal(18, g:exported)
264 unlet g:exported
265
266 # try to use something that exists but is not exported
267 var import_not_exported_lines =<< trim END
268 vim9script
269 import './Xexport.vim' as expo
270 echo expo.name
271 END
272 writefile(import_not_exported_lines, 'Ximport.vim')
273 assert_fails('source Ximport.vim', 'E1049:', '', 3, 'Ximport.vim')
274
275 # try to import something that is already defined
276 var import_already_defined =<< trim END
277 vim9script
278 var exported = 'something'
279 import './Xexport.vim' as exported
280 END
281 writefile(import_already_defined, 'Ximport.vim')
282 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
283
284 # try changing an imported const
285 var import_assign_to_const =<< trim END
286 vim9script
287 import './Xexport.vim' as expo
288 def Assign()
289 expo.CONST = 987
290 enddef
291 defcompile
292 END
293 writefile(import_assign_to_const, 'Ximport.vim')
294 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
295
296 # try changing an imported final
297 var import_assign_to_final =<< trim END
298 vim9script
299 import './Xexport.vim' as expo
300 def Assign()
301 expo.theList = [2]
302 enddef
303 defcompile
304 END
305 writefile(import_assign_to_final, 'Ximport.vim')
306 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
307
308 var import_no_as_lines =<< trim END
309 vim9script
310 import './Xexport.vim' name
311 END
312 writefile(import_no_as_lines, 'Ximport.vim')
313 assert_fails('source Ximport.vim', 'E488:', '', 2, 'Ximport.vim')
314
315 var import_invalid_string_lines =<< trim END
316 vim9script
317 import Xexport.vim
318 END
319 writefile(import_invalid_string_lines, 'Ximport.vim')
320 assert_fails('source Ximport.vim', 'E121:', '', 2, 'Ximport.vim')
321
322 var import_wrong_name_lines =<< trim END
323 vim9script
324 import './XnoExport.vim'
325 END
326 writefile(import_wrong_name_lines, 'Ximport.vim')
327 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
328
329 var import_redefining_lines =<< trim END
330 vim9script
331 import './Xexport.vim' as exported
332 var exported = 5
333 END
334 writefile(import_redefining_lines, 'Ximport.vim')
335 assert_fails('source Ximport.vim', 'E1213: Redefining imported item "exported"', '', 3)
336
Bram Moolenaar160aa862022-01-10 21:29:57 +0000337 var import_missing_dot_lines =<< trim END
338 vim9script
339 import './Xexport.vim' as expo
340 def Test()
341 expo = 9
342 enddef
343 defcompile
344 END
345 writefile(import_missing_dot_lines, 'Ximport.vim')
346 assert_fails('source Ximport.vim', 'E1258:', '', 1)
347
348 var import_missing_name_lines =<< trim END
349 vim9script
350 import './Xexport.vim' as expo
351 def Test()
352 expo.99 = 9
353 enddef
354 defcompile
355 END
356 writefile(import_missing_name_lines, 'Ximport.vim')
Bram Moolenaar76283822022-01-10 21:39:03 +0000357 assert_fails('source Ximport.vim', 'E1259:', '', 1)
Bram Moolenaar160aa862022-01-10 21:29:57 +0000358
Bram Moolenaard8448622022-01-07 21:39:52 +0000359 var import_assign_wrong_type_lines =<< trim END
360 vim9script
361 import './Xexport.vim' as expo
362 expo.exported = 'xxx'
363 END
364 writefile(import_assign_wrong_type_lines, 'Ximport.vim')
365 assert_fails('source Ximport.vim', 'E1012: Type mismatch; expected number but got string', '', 3)
366
367 var import_assign_const_lines =<< trim END
368 vim9script
369 import './Xexport.vim' as expo
370 expo.CONST = 4321
371 END
372 writefile(import_assign_const_lines, 'Ximport.vim')
373 assert_fails('source Ximport.vim', 'E46: Cannot change read-only variable "CONST"', '', 3)
374
375 delete('Ximport.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000376 delete('Xexport.vim')
377
378 # Check that in a Vim9 script 'cpo' is set to the Vim default.
379 # Flags added or removed are also applied to the restored value.
380 set cpo=abcd
381 var lines =<< trim END
382 vim9script
383 g:cpo_in_vim9script = &cpo
384 set cpo+=f
385 set cpo-=c
386 g:cpo_after_vim9script = &cpo
387 END
388 writefile(lines, 'Xvim9_script')
389 source Xvim9_script
390 assert_equal('fabd', &cpo)
391 set cpo&vim
392 assert_equal(&cpo, g:cpo_in_vim9script)
393 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
394 assert_equal(newcpo, g:cpo_after_vim9script)
395
396 delete('Xvim9_script')
397enddef
398
399def Test_import_funcref()
400 var lines =<< trim END
401 vim9script
402 export def F(): number
403 return 42
404 enddef
405 export const G = F
406 END
407 writefile(lines, 'Xlib.vim')
408
409 lines =<< trim END
410 vim9script
411 import './Xlib.vim' as lib
412 const Foo = lib.G()
413 assert_equal(42, Foo)
414
415 def DoTest()
416 const Goo = lib.G()
417 assert_equal(42, Goo)
418 enddef
419 DoTest()
420 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000421 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000422
423 delete('Xlib.vim')
424enddef
425
Bram Moolenaarc2f17f72022-02-21 13:13:50 +0000426def Test_import_duplicate_function()
427 # Function Hover() exists in both scripts, partial should refer to the right
428 # one.
429 var lines =<< trim END
430 vim9script
431
432 def Hover(d: dict<any>): string
433 return 'found it'
434 enddef
435
436 export def NewLspServer(): dict<any>
437 var d: dict<any> = {}
438 d->extend({hover: function('Hover', [d])})
439 return d
440 enddef
441
442 NewLspServer()
443 END
444 writefile(lines, 'Xserver.vim')
445
446 lines =<< trim END
447 vim9script
448
449 import './Xserver.vim' as server
450
451 export def Hover()
452 enddef
453
454 def AddServer()
455 var d: dict<any> = server.NewLspServer()
456 assert_equal('found it', d.hover())
457 enddef
458 AddServer()
459 END
460 v9.CheckScriptSuccess(lines)
461
462 delete('Xserver.vim')
463enddef
464
465
Bram Moolenaard8448622022-01-07 21:39:52 +0000466def Test_import_fails()
467 writefile([], 'Xfoo.vim')
468 var lines =<< trim END
469 import './Xfoo.vim' as foo
470 foo = 'bar'
471 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000472 v9.CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use foo itself'])
Bram Moolenaard8448622022-01-07 21:39:52 +0000473 lines =<< trim END
474 vim9script
475 import './Xfoo.vim' as foo
476 var that = foo
477 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000478 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaardd5893b2022-01-20 21:32:54 +0000479 lines =<< trim END
480 vim9script
481 import './Xfoo.vim' as foo
482 var that: any
483 that += foo
484 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000485 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaardd5893b2022-01-20 21:32:54 +0000486 lines =<< trim END
487 vim9script
488 import './Xfoo.vim' as foo
489 foo += 9
490 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000491 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaard8448622022-01-07 21:39:52 +0000492
493 lines =<< trim END
494 vim9script
495 import './Xfoo.vim' as 9foo
496 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000497 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000498 lines =<< trim END
499 vim9script
500 import './Xfoo.vim' as the#foo
501 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000502 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000503 lines =<< trim END
504 vim9script
505 import './Xfoo.vim' as g:foo
506 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000507 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000508
509 delete('Xfoo.vim')
510
511 lines =<< trim END
512 vim9script
513 def TheFunc()
514 echo 'the func'
515 enddef
516 export var Ref = TheFunc
517 END
518 writefile([], 'Xthat.vim')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000519
Bram Moolenaard8448622022-01-07 21:39:52 +0000520 lines =<< trim END
521 import './Xthat.vim' as That
522 That()
523 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000524 v9.CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use That itself'])
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000525
526 lines =<< trim END
Bram Moolenaar937610b2022-01-19 17:21:29 +0000527 vim9script
528 import './Xthat.vim' as That
529 def Func()
530 echo That()
531 enddef
532 Func()
533 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000534 v9.CheckScriptFailure(lines, 'E1236: Cannot use That itself')
Bram Moolenaar937610b2022-01-19 17:21:29 +0000535
536 lines =<< trim END
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000537 import './Xthat.vim' as one
538 import './Xthat.vim' as two
539 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000540 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000541
542 delete('Xthat.vim')
Bram Moolenaar779aeff2022-02-08 19:12:19 +0000543
544 lines =<< trim END
545 vim9script
546 export var item = 'hello'
547 import './Xyourself.vim'
548 END
549 writefile(lines, 'Xyourself.vim')
550 assert_fails('source Xyourself.vim', 'E1088:')
551 delete('Xyourself.vim')
552
Bram Moolenaard8448622022-01-07 21:39:52 +0000553 mkdir('Ximport')
554
555 writefile(['vim9script'], 'Ximport/.vim')
556 lines =<< trim END
557 vim9script
558 import './Ximport/.vim'
559 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000560 v9.CheckScriptFailure(lines, 'E1261: Cannot import .vim without using "as"')
Bram Moolenaard8448622022-01-07 21:39:52 +0000561 lines =<< trim END
562 vim9script
563 import './Ximport/.vim' as vim
564 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000565 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000566
567 writefile(['vim9script'], 'Ximport/.vimrc')
568 lines =<< trim END
569 vim9script
570 import './Ximport/.vimrc'
571 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000572 v9.CheckScriptFailure(lines, 'E1257: Imported script must use "as" or end in .vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000573 lines =<< trim END
574 vim9script
575 import './Ximport/.vimrc' as vimrc
576 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000577 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000578
579 delete('Ximport', 'rf')
580enddef
581
582func g:Trigger()
583 source Ximport.vim
584 return "echo 'yes'\<CR>"
585endfunc
586
587def Test_import_export_expr_map()
588 # check that :import and :export work when buffer is locked
589 var export_lines =<< trim END
590 vim9script
591 export def That(): string
592 return 'yes'
593 enddef
594 END
595 writefile(export_lines, 'Xexport_that.vim')
596
597 var import_lines =<< trim END
598 vim9script
599 import './Xexport_that.vim' as that
600 assert_equal('yes', that.That())
601 END
602 writefile(import_lines, 'Ximport.vim')
603
604 nnoremap <expr> trigger g:Trigger()
605 feedkeys('trigger', "xt")
606
607 delete('Xexport_that.vim')
608 delete('Ximport.vim')
609 nunmap trigger
610enddef
611
612def Test_import_in_filetype()
613 # check that :import works when the buffer is locked
614 mkdir('ftplugin', 'p')
615 var export_lines =<< trim END
616 vim9script
617 export var That = 'yes'
618 END
619 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
620
621 var import_lines =<< trim END
622 vim9script
623 import './Xexport_ft.vim' as ft
624 assert_equal('yes', ft.That)
625 g:did_load_mytpe = 1
626 END
627 writefile(import_lines, 'ftplugin/qf.vim')
628
629 var save_rtp = &rtp
630 &rtp = getcwd() .. ',' .. &rtp
631
632 filetype plugin on
633 copen
634 assert_equal(1, g:did_load_mytpe)
635
636 quit!
637 delete('Xexport_ft.vim')
638 delete('ftplugin', 'rf')
639 &rtp = save_rtp
640enddef
641
642def Test_use_import_in_mapping()
643 var lines =<< trim END
644 vim9script
645 export def Funcx()
646 g:result = 42
647 enddef
648 END
649 writefile(lines, 'XsomeExport.vim')
650 lines =<< trim END
651 vim9script
652 import './XsomeExport.vim' as some
653 var Funcy = some.Funcx
654 nnoremap <F3> :call <sid>Funcy()<cr>
655 END
656 writefile(lines, 'Xmapscript.vim')
657
658 source Xmapscript.vim
659 feedkeys("\<F3>", "xt")
660 assert_equal(42, g:result)
661
662 unlet g:result
663 delete('XsomeExport.vim')
664 delete('Xmapscript.vim')
665 nunmap <F3>
666enddef
667
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000668def Test_use_import_in_command_completion()
Bram Moolenaar15d16352022-01-17 20:09:08 +0000669 var lines =<< trim END
670 vim9script
671 export def Complete(..._): list<string>
672 return ['abcd']
673 enddef
674 END
675 writefile(lines, 'Xscript.vim')
676
677 lines =<< trim END
678 vim9script
679 import './Xscript.vim'
680
681 command -nargs=1 -complete=customlist,Xscript.Complete Cmd echo 'ok'
682 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
683 assert_equal('#Cmd abcd', @:)
684 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000685 v9.CheckScriptSuccess(lines)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000686
687 delcommand Cmd
688 delete('Xscript.vim')
689enddef
690
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000691def Test_use_autoload_import_in_insert_completion()
692 mkdir('Xdir/autoload', 'p')
693 var save_rtp = &rtp
694 exe 'set rtp^=' .. getcwd() .. '/Xdir'
695
696 var lines =<< trim END
697 vim9script
698 export def ThesaurusFunc(findbase: bool, _): any
699 if findbase
700 return 1
701 endif
702 return [
703 'check',
704 'experiment',
705 'test',
706 'verification'
707 ]
708 enddef
709 g:completion_loaded = 'yes'
710 END
711 writefile(lines, 'Xdir/autoload/completion.vim')
712
713 new
714 lines =<< trim END
715 vim9script
716 g:completion_loaded = 'no'
717 import autoload 'completion.vim'
718 set thesaurusfunc=completion.ThesaurusFunc
719 assert_equal('no', g:completion_loaded)
720 feedkeys("i\<C-X>\<C-T>\<C-N>\<Esc>", 'xt')
721 assert_equal('experiment', getline(1))
722 assert_equal('yes', g:completion_loaded)
723 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000724 v9.CheckScriptSuccess(lines)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000725
726 set thesaurusfunc=
727 bwipe!
728 delete('Xdir', 'rf')
729 &rtp = save_rtp
730enddef
731
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000732def Test_use_autoload_import_partial_in_opfunc()
733 mkdir('Xdir/autoload', 'p')
734 var save_rtp = &rtp
735 exe 'set rtp^=' .. getcwd() .. '/Xdir'
736
737 var lines =<< trim END
738 vim9script
739 export def Opfunc(..._)
740 g:opfunc_called = 'yes'
741 enddef
742 END
743 writefile(lines, 'Xdir/autoload/opfunc.vim')
744
745 new
746 lines =<< trim END
747 vim9script
748 import autoload 'opfunc.vim'
749 nnoremap <expr> <F3> TheFunc()
750 def TheFunc(): string
751 &operatorfunc = function('opfunc.Opfunc', [0])
752 return 'g@'
753 enddef
754 feedkeys("\<F3>l", 'xt')
755 assert_equal('yes', g:opfunc_called)
756 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000757 v9.CheckScriptSuccess(lines)
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000758
759 set opfunc=
760 bwipe!
761 delete('Xdir', 'rf')
Bram Moolenaar06b77222022-01-25 15:51:56 +0000762 nunmap <F3>
763 &rtp = save_rtp
764enddef
765
766def Test_set_opfunc_to_autoload_func_directly()
767 mkdir('Xdir/autoload', 'p')
768 var save_rtp = &rtp
769 exe 'set rtp^=' .. getcwd() .. '/Xdir'
770
771 var lines =<< trim END
772 vim9script
773 export def Opfunc(..._)
774 g:opfunc_called = 'yes'
775 enddef
776 END
777 writefile(lines, 'Xdir/autoload/opfunc.vim')
778
779 new
780 lines =<< trim END
781 vim9script
782 import autoload 'opfunc.vim'
783 nnoremap <expr> <F3> TheFunc()
784 def TheFunc(): string
785 &operatorfunc = opfunc.Opfunc
786 return 'g@'
787 enddef
788 feedkeys("\<F3>l", 'xt')
789 assert_equal('yes', g:opfunc_called)
790 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000791 v9.CheckScriptSuccess(lines)
Bram Moolenaar06b77222022-01-25 15:51:56 +0000792
793 set opfunc=
794 bwipe!
795 delete('Xdir', 'rf')
796 nunmap <F3>
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000797 &rtp = save_rtp
798enddef
799
Bram Moolenaare70dd112022-01-21 16:31:11 +0000800def Test_use_autoload_import_in_fold_expression()
801 mkdir('Xdir/autoload', 'p')
802 var save_rtp = &rtp
803 exe 'set rtp^=' .. getcwd() .. '/Xdir'
804
805 var lines =<< trim END
806 vim9script
807 export def Expr(): string
808 return getline(v:lnum) =~ '^#' ? '>1' : '1'
809 enddef
Bram Moolenaar9530b582022-01-22 13:39:08 +0000810 export def Text(): string
811 return 'fold text'
812 enddef
Bram Moolenaare70dd112022-01-21 16:31:11 +0000813 g:fold_loaded = 'yes'
814 END
815 writefile(lines, 'Xdir/autoload/fold.vim')
816
817 lines =<< trim END
818 vim9script
819 import autoload 'fold.vim'
820 &foldexpr = 'fold.Expr()'
Bram Moolenaar9530b582022-01-22 13:39:08 +0000821 &foldtext = 'fold.Text()'
Bram Moolenaare70dd112022-01-21 16:31:11 +0000822 &foldmethod = 'expr'
823 &debug = 'throw'
824 END
825 new
826 setline(1, ['# one', 'text', '# two', 'text'])
827 g:fold_loaded = 'no'
Bram Moolenaar62aec932022-01-29 21:45:34 +0000828 v9.CheckScriptSuccess(lines)
Bram Moolenaare70dd112022-01-21 16:31:11 +0000829 assert_equal('no', g:fold_loaded)
830 redraw
831 assert_equal('yes', g:fold_loaded)
832
833 # Check that script context of 'foldexpr' is copied to another buffer.
834 edit! otherfile
835 redraw
836
Bram Moolenaar9530b582022-01-22 13:39:08 +0000837 set foldexpr= foldtext& foldmethod& debug=
Bram Moolenaare70dd112022-01-21 16:31:11 +0000838 bwipe!
839 delete('Xdir', 'rf')
840 &rtp = save_rtp
841enddef
842
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100843def Test_autoload_import_relative()
844 var lines =<< trim END
845 vim9script
846
847 g:loaded = 'yes'
848 export def RelFunc(): string
849 return 'relfunc'
850 enddef
851 def NotExported()
852 echo 'not'
853 enddef
854
855 export var someText = 'some text'
856 var notexp = 'bad'
857 END
858 writefile(lines, 'XimportRel.vim')
859 writefile(lines, 'XimportRel2.vim')
860 writefile(lines, 'XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +0100861 writefile(lines, 'XimportRel4.vim')
862 writefile(lines, 'XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100863
864 lines =<< trim END
865 vim9script
866 g:loaded = 'no'
867 import autoload './XimportRel.vim'
868 assert_equal('no', g:loaded)
869
870 def AFunc(): string
871 var res = ''
872 res ..= XimportRel.RelFunc()
873 res ..= '/'
874 res ..= XimportRel.someText
875 XimportRel.someText = 'from AFunc'
876 return res
877 enddef
878 # script not loaded when compiling
879 defcompile
880 assert_equal('no', g:loaded)
881
882 assert_equal('relfunc/some text', AFunc())
883 assert_equal('yes', g:loaded)
884 unlet g:loaded
885
886 assert_equal('from AFunc', XimportRel.someText)
887 XimportRel.someText = 'from script'
888 assert_equal('from script', XimportRel.someText)
889 END
890 v9.CheckScriptSuccess(lines)
891
892 lines =<< trim END
893 vim9script
894 import autoload './XimportRel.vim'
895 echo XimportRel.NotExported()
896 END
897 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 3)
898
899 lines =<< trim END
900 vim9script
901 import autoload './XimportRel.vim'
902 echo XimportRel.notexp
903 END
904 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
905
906 lines =<< trim END
907 vim9script
908 import autoload './XimportRel.vim'
909 XimportRel.notexp = 'bad'
910 END
911 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
912
913 lines =<< trim END
914 vim9script
915 import autoload './XimportRel.vim'
916 def Func()
917 echo XimportRel.NotExported()
918 enddef
919 Func()
920 END
921 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 1)
922
923 lines =<< trim END
924 vim9script
925 import autoload './XimportRel.vim'
926 def Func()
927 echo XimportRel.notexp
928 enddef
929 Func()
930 END
931 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
932
Bram Moolenaar10611952022-04-03 21:11:34 +0100933 # Same, script not imported before
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100934 lines =<< trim END
935 vim9script
Bram Moolenaar10611952022-04-03 21:11:34 +0100936 import autoload './XimportRel4.vim'
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100937 def Func()
Bram Moolenaar10611952022-04-03 21:11:34 +0100938 echo XimportRel4.notexp
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100939 enddef
940 Func()
941 END
942 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
943
Bram Moolenaar10611952022-04-03 21:11:34 +0100944 # does not fail if the script wasn't loaded yet and only compiling
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100945 g:loaded = 'no'
946 lines =<< trim END
947 vim9script
948 import autoload './XimportRel2.vim'
949 def Func()
950 echo XimportRel2.notexp
951 enddef
952 defcompile
953 END
954 v9.CheckScriptSuccess(lines)
955 assert_equal('no', g:loaded)
956
Bram Moolenaar10611952022-04-03 21:11:34 +0100957 lines =<< trim END
958 vim9script
959 import autoload './XimportRel.vim'
960 def Func()
961 XimportRel.notexp = 'bad'
962 enddef
963 Func()
964 END
965 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
966
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100967 # fails with a not loaded import
968 lines =<< trim END
969 vim9script
970 import autoload './XimportRel3.vim'
971 def Func()
972 XimportRel3.notexp = 'bad'
973 enddef
974 Func()
975 END
976 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
977 assert_equal('yes', g:loaded)
978 unlet g:loaded
979
Bram Moolenaar10611952022-04-03 21:11:34 +0100980 lines =<< trim END
981 vim9script
982 import autoload './XimportRel5.vim'
983 def Func()
984 XimportRel5.nosuchvar = 'bad'
985 enddef
986 Func()
987 END
988 v9.CheckScriptFailure(lines, 'E121: Undefined variable: nosuchvar', 1)
989 unlet g:loaded
990
991 # nasty: delete script after compiling function
992 writefile(['vim9script'], 'XimportRelDel.vim')
993 lines =<< trim END
994 vim9script
995
996 import autoload './XimportRelDel.vim'
997 def DoIt()
998 echo XimportRelDel.var
999 enddef
1000 defcompile
1001 delete('XimportRelDel.vim')
1002 DoIt()
1003 END
1004 v9.CheckScriptFailure(lines, 'E456:')
1005
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001006 delete('XimportRel.vim')
1007 delete('XimportRel2.vim')
1008 delete('XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +01001009 delete('XimportRel4.vim')
1010 delete('XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001011enddef
1012
Bram Moolenaarccbfd482022-03-31 16:18:23 +01001013def Test_autoload_import_relative_autoload_dir()
1014 mkdir('autoload', 'p')
1015 var lines =<< trim END
1016 vim9script
1017 export def Bar()
1018 g:called_bar = 'yes'
1019 enddef
1020 END
1021 writefile(lines, 'autoload/script.vim')
1022
1023 lines =<< trim END
1024 vim9script
1025 import autoload './autoload/script.vim'
1026 def Foo()
1027 script.Bar()
1028 enddef
1029 Foo()
1030 assert_equal('yes', g:called_bar)
1031 END
1032 v9.CheckScriptSuccess(lines)
1033
1034 unlet g:called_bar
1035 delete('autoload', 'rf')
1036enddef
1037
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001038func Test_import_in_diffexpr()
1039 CheckExecutable diff
1040
1041 call Run_Test_import_in_diffexpr()
1042endfunc
1043
1044def Run_Test_import_in_diffexpr()
1045 var lines =<< trim END
1046 vim9script
1047
1048 export def DiffExpr()
1049 # Prepend some text to check diff type detection
1050 writefile(['warning', ' message'], v:fname_out)
1051 silent exe '!diff ' .. v:fname_in .. ' '
1052 .. v:fname_new .. '>>' .. v:fname_out
1053 enddef
1054 END
1055 writefile(lines, 'Xdiffexpr')
1056
1057 lines =<< trim END
1058 vim9script
1059 import './Xdiffexpr' as diff
1060
1061 set diffexpr=diff.DiffExpr()
1062 set diffopt=foldcolumn:0
1063 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001064 v9.CheckScriptSuccess(lines)
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001065
1066 enew!
1067 call setline(1, ['one', 'two', 'three'])
1068 diffthis
1069
1070 botright vert new
1071 call setline(1, ['one', 'two', 'three.'])
1072 diffthis
1073 # we only check if this does not cause errors
1074 redraw
1075
1076 diffoff!
1077 bwipe!
1078 bwipe!
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001079 delete('Xdiffexpr')
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001080enddef
1081
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001082def Test_import_in_patchexpr()
1083 var lines =<< trim END
1084 vim9script
1085 export def TPatch()
1086 call writefile(['output file'], v:fname_out)
1087 enddef
1088 END
1089 writefile(lines, 'Xpatchexpr')
1090
1091 lines =<< trim END
1092 vim9script
1093 import './Xpatchexpr' as patch
1094 set patchexpr=patch.TPatch()
1095 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001096 v9.CheckScriptSuccess(lines)
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001097
1098 call writefile(['input file'], 'Xinput')
1099 call writefile(['diff file'], 'Xdiff')
1100 :%bwipe!
1101 edit Xinput
1102 diffpatch Xdiff
1103 call assert_equal('output file', getline(1))
1104
1105 call delete('Xinput')
1106 call delete('Xdiff')
1107 call delete('Xpatchexpr')
1108 set patchexpr&
1109 :%bwipe!
1110enddef
1111
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001112def Test_import_in_formatexpr()
1113 var lines =<< trim END
1114 vim9script
1115 export def MyFormatExpr(): number
1116 g:did_format = 'yes'
1117 return 0
1118 enddef
1119 END
1120 writefile(lines, 'Xformatter')
1121
1122 lines =<< trim END
1123 vim9script
1124 import './Xformatter' as format
1125 set formatexpr=format.MyFormatExpr()
1126 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001127 v9.CheckScriptSuccess(lines)
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001128
1129 new
1130 setline(1, ['a', 'b', 'c'])
1131 normal gqG
1132 assert_equal('yes', g:did_format)
1133
1134 bwipe!
1135 delete('Xformatter')
1136 unlet g:did_format
1137 set formatexpr=
1138enddef
1139
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001140def Test_import_in_includeexpr()
1141 writefile(['found it'], 'Xthisfile')
1142 new
1143
1144 var lines =<< trim END
1145 vim9script
1146 export def DoSub(): string
1147 return substitute(v:fname, 'that', 'this', '')
1148 enddef
1149 END
1150 writefile(lines, 'Xinclude.vim')
1151
1152 lines =<< trim END
1153 vim9script
1154 import './Xinclude.vim'
1155 set includeexpr=Xinclude.DoSub()
1156 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001157 v9.CheckScriptSuccess(lines)
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001158
1159 setline(1, ['Xthatfile'])
1160 exe "normal \<C-W>f"
1161 assert_equal('Xthisfile', expand('%'))
1162
1163 bwipe!
1164 bwipe!
1165 set includeexpr=
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001166 delete('Xinclude.vim')
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001167 delete('Xthisfile')
1168enddef
1169
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001170def Test_import_in_indentexpr()
1171 var lines =<< trim END
1172 vim9script
1173 export def GetIndent(): number
1174 return 5
1175 enddef
1176 END
1177 writefile(lines, 'Xindenter')
1178
1179 lines =<< trim END
1180 vim9script
1181 import './Xindenter' as indent
1182 set indentexpr=indent.GetIndent()
1183 set debug=throw
1184 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001185 v9.CheckScriptSuccess(lines)
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001186
1187 new
1188 setline(1, 'hello')
1189 normal ==
1190 assert_equal(' hello', getline(1))
1191
1192 bwipe!
1193 set indentexpr= debug=
1194 delete('Xindenter')
1195enddef
1196
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001197func Test_import_in_printexpr()
1198 CheckFeature postscript
1199 call Run_Test_import_in_printexpr()
1200endfunc
1201
1202def Run_Test_import_in_printexpr()
1203 var lines =<< trim END
1204 vim9script
1205 export def PrintFile(): bool
1206 g:printed = 'yes'
1207 delete('v:fname_in')
1208 return false
1209 enddef
1210 END
1211 writefile(lines, 'Xprint.vim')
1212
1213 lines =<< trim END
1214 vim9script
1215 import './Xprint.vim'
1216 set printexpr=Xprint.PrintFile()
1217 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001218 v9.CheckScriptSuccess(lines)
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001219
1220 help
1221 hardcopy dummy args
1222 assert_equal('yes', g:printed)
1223
1224 delete('Xprint.vim')
1225 set printexpr=
1226enddef
1227
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001228def Test_import_in_charconvert()
1229 var lines =<< trim END
1230 vim9script
1231 export def MakeUpper(): bool
1232 var data = readfile(v:fname_in)
1233 map(data, 'toupper(v:val)')
1234 writefile(data, v:fname_out)
1235 return false # success
1236 enddef
1237 END
1238 writefile(lines, 'Xconvert.vim')
1239
1240 lines =<< trim END
1241 vim9script
1242 import './Xconvert.vim' as conv
1243 set charconvert=conv.MakeUpper()
1244 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001245 v9.CheckScriptSuccess(lines)
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001246
1247 writefile(['one', 'two'], 'Xfile')
1248 new Xfile
1249 write ++enc=ucase Xfile1
1250 assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1251
1252 delete('Xfile')
1253 delete('Xfile1')
1254 delete('Xconvert.vim')
1255 bwipe!
1256 set charconvert&
1257enddef
1258
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001259func Test_import_in_spellsuggest_expr()
1260 CheckFeature spell
1261 call Run_Test_import_in_spellsuggest_expr()
1262endfunc
1263
1264def Run_Test_import_in_spellsuggest_expr()
1265 var lines =<< trim END
1266 vim9script
1267 export def MySuggest(): list<any>
1268 return [['Fox', 8], ['Fop', 9]]
1269 enddef
1270 END
1271 writefile(lines, 'Xsuggest.vim')
1272
1273 lines =<< trim END
1274 vim9script
1275 import './Xsuggest.vim' as sugg
1276 set spell spellsuggest=expr:sugg.MySuggest()
1277 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001278 v9.CheckScriptSuccess(lines)
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001279
1280 set verbose=1 # report errors
1281 call assert_equal(['Fox', 'Fop'], spellsuggest('Fo', 2))
1282
1283 delete('Xsuggest.vim')
1284 set nospell spellsuggest& verbose=0
1285enddef
1286
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001287def Test_export_shadows_global_function()
1288 mkdir('Xdir/autoload', 'p')
1289 var save_rtp = &rtp
1290 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1291
1292 var lines =<< trim END
1293 vim9script
1294 export def Shadow(): string
1295 return 'Shadow()'
1296 enddef
1297 END
1298 writefile(lines, 'Xdir/autoload/shadow.vim')
1299
1300 lines =<< trim END
1301 vim9script
1302
1303 def g:Shadow(): string
1304 return 'global'
1305 enddef
1306
1307 import autoload 'shadow.vim'
1308 assert_equal('Shadow()', shadow.Shadow())
1309 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001310 v9.CheckScriptSuccess(lines)
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001311
1312 delfunc g:Shadow
1313 bwipe!
1314 delete('Xdir', 'rf')
1315 &rtp = save_rtp
1316enddef
1317
Bram Moolenaard8448622022-01-07 21:39:52 +00001318def Test_export_fails()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001319 v9.CheckScriptFailure(['export var some = 123'], 'E1042:')
1320 v9.CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
1321 v9.CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001322
1323 assert_fails('export something', 'E1043:')
1324enddef
1325
1326func Test_import_fails_without_script()
1327 CheckRunVimInTerminal
1328
1329 " call indirectly to avoid compilation error for missing functions
1330 call Run_Test_import_fails_on_command_line()
1331endfunc
1332
1333def Run_Test_import_fails_on_command_line()
1334 var export =<< trim END
1335 vim9script
1336 export def Foo(): number
1337 return 0
1338 enddef
1339 END
1340 writefile(export, 'XexportCmd.vim')
1341
Bram Moolenaar62aec932022-01-29 21:45:34 +00001342 var buf = g:RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaard8448622022-01-07 21:39:52 +00001343 rows: 6, wait_for_ruler: 0})
Bram Moolenaar62aec932022-01-29 21:45:34 +00001344 g:WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaard8448622022-01-07 21:39:52 +00001345
1346 delete('XexportCmd.vim')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001347 g:StopVimInTerminal(buf)
Bram Moolenaard8448622022-01-07 21:39:52 +00001348enddef
1349
1350def Test_vim9_reload_noclear()
1351 var lines =<< trim END
1352 vim9script
1353 export var exported = 'thexport'
1354
1355 export def TheFunc(x = 0)
1356 enddef
1357 END
1358 writefile(lines, 'XExportReload')
1359 lines =<< trim END
1360 vim9script noclear
1361 g:loadCount += 1
Bram Moolenaara749a422022-02-12 19:52:25 +00001362 var reloaded = 'init'
Bram Moolenaard8448622022-01-07 21:39:52 +00001363 import './XExportReload' as exp
1364
1365 def Again(): string
1366 return 'again'
1367 enddef
1368
1369 exp.TheFunc()
1370
Bram Moolenaara749a422022-02-12 19:52:25 +00001371 if exists('loaded') | finish | endif
1372 var loaded = true
Bram Moolenaard8448622022-01-07 21:39:52 +00001373
Bram Moolenaara749a422022-02-12 19:52:25 +00001374 var notReloaded = 'yes'
1375 reloaded = 'first'
Bram Moolenaard8448622022-01-07 21:39:52 +00001376 def g:Values(): list<string>
Bram Moolenaara749a422022-02-12 19:52:25 +00001377 return [reloaded, notReloaded, Again(), Once(), exp.exported]
Bram Moolenaard8448622022-01-07 21:39:52 +00001378 enddef
1379
1380 def Once(): string
1381 return 'once'
1382 enddef
1383 END
1384 writefile(lines, 'XReloaded')
1385 g:loadCount = 0
1386 source XReloaded
1387 assert_equal(1, g:loadCount)
1388 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
1389 source XReloaded
1390 assert_equal(2, g:loadCount)
1391 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1392 source XReloaded
1393 assert_equal(3, g:loadCount)
1394 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1395
1396 delete('XReloaded')
1397 delete('XExportReload')
1398 delfunc g:Values
1399 unlet g:loadCount
1400
1401 lines =<< trim END
1402 vim9script
1403 def Inner()
1404 enddef
1405 END
1406 lines->writefile('XreloadScript.vim')
1407 source XreloadScript.vim
1408
1409 lines =<< trim END
1410 vim9script
1411 def Outer()
1412 def Inner()
1413 enddef
1414 enddef
1415 defcompile
1416 END
1417 lines->writefile('XreloadScript.vim')
1418 source XreloadScript.vim
1419
1420 delete('XreloadScript.vim')
1421enddef
1422
Bram Moolenaarcd1cda22022-02-16 21:48:25 +00001423def Test_vim_reload_noclear_arg_count()
1424 var lines =<< trim END
1425 vim9script noclear
1426
1427 if !exists('g:didload')
1428 def Test(a: string, b: string)
1429 echo a b
1430 enddef
1431 def Call()
1432 Test('a', 'b')
1433 enddef
1434 else
1435 # redefine with one argument less
1436 def Test(a: string)
1437 echo a
1438 enddef
1439 endif
1440 Call()
1441 g:didload = 1
1442 END
1443 lines->writefile('XreloadScript_1.vim')
1444 source XreloadScript_1.vim
1445 assert_fails('source XreloadScript_1.vim', 'E1106: One argument too many')
1446 unlet g:didload
1447
1448 lines =<< trim END
1449 vim9script noclear
1450
1451 if !exists('g:didload')
1452 def Test(a: string, b: string, c: string)
1453 echo a b
1454 enddef
1455 def Call()
1456 Test('a', 'b', 'c')
1457 enddef
1458 else
1459 # redefine with one argument less
1460 def Test(a: string)
1461 echo a
1462 enddef
1463 endif
1464 Call()
1465 g:didload = 1
1466 END
1467 lines->writefile('XreloadScript_2.vim')
1468 source XreloadScript_2.vim
1469 assert_fails('source XreloadScript_2.vim', 'E1106: 2 arguments too many')
1470 unlet g:didload
1471
1472 lines =<< trim END
1473 vim9script noclear
1474
1475 if !exists('g:didload')
1476 def Test(a: string)
1477 echo a
1478 enddef
1479 def Call()
1480 Test('a')
1481 enddef
1482 else
1483 # redefine with one argument extra
1484 def Test(a: string, b: string)
1485 echo a b
1486 enddef
1487 endif
1488 Call()
1489 g:didload = 1
1490 END
1491 lines->writefile('XreloadScript_3.vim')
1492 source XreloadScript_3.vim
1493 assert_fails('source XreloadScript_3.vim', 'E1190: One argument too few')
1494 unlet g:didload
1495
1496 lines =<< trim END
1497 vim9script noclear
1498
1499 if !exists('g:didload')
1500 def Test(a: string)
1501 echo a
1502 enddef
1503 def Call()
1504 Test('a')
1505 enddef
1506 else
1507 # redefine with two arguments extra
1508 def Test(a: string, b: string, c: string)
1509 echo a b
1510 enddef
1511 endif
1512 Call()
1513 g:didload = 1
1514 END
1515 lines->writefile('XreloadScript_4.vim')
1516 source XreloadScript_4.vim
1517 assert_fails('source XreloadScript_4.vim', 'E1190: 2 arguments too few')
1518 unlet g:didload
1519
1520 delete('XreloadScript_1.vim')
1521 delete('XreloadScript_2.vim')
1522 delete('XreloadScript_3.vim')
1523 delete('XreloadScript_4.vim')
1524enddef
1525
1526def Test_vim9_reload_noclear_error()
1527 var lines =<< trim END
1528 vim9script noclear
1529
1530 if !exists('g:didload')
1531 def Test(a: string)
1532 echo a
1533 enddef
1534 def Call()
1535 Test('a')
1536 enddef
1537 else
1538 # redefine with a compile error
1539 def Test(a: string)
1540 echo ax
1541 enddef
1542 endif
1543 Call()
1544 g:didload = 1
1545 END
1546 lines->writefile('XreloadScriptErr.vim')
1547 source XreloadScriptErr.vim
1548 assert_fails('source XreloadScriptErr.vim', 'E1001: Variable not found: ax')
1549
1550 unlet g:didload
1551 delete('XreloadScriptErr.vim')
1552enddef
1553
Bram Moolenaard8448622022-01-07 21:39:52 +00001554def Test_vim9_reload_import()
1555 var lines =<< trim END
1556 vim9script
1557 const var = ''
1558 var valone = 1234
1559 def MyFunc(arg: string)
1560 valone = 5678
1561 enddef
1562 END
1563 var morelines =<< trim END
1564 var valtwo = 222
1565 export def GetValtwo(): number
1566 return valtwo
1567 enddef
1568 END
1569 writefile(lines + morelines, 'Xreload.vim')
1570 source Xreload.vim
1571 source Xreload.vim
1572 source Xreload.vim
1573
1574 # cannot declare a var twice
1575 lines =<< trim END
1576 vim9script
1577 var valone = 1234
1578 var valone = 5678
1579 END
1580 writefile(lines, 'Xreload.vim')
1581 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
1582
1583 delete('Xreload.vim')
1584 delete('Ximport.vim')
1585enddef
1586
1587" if a script is reloaded with a script-local variable that changed its type, a
1588" compiled function using that variable must fail.
1589def Test_script_reload_change_type()
1590 var lines =<< trim END
1591 vim9script noclear
1592 var str = 'string'
1593 def g:GetStr(): string
1594 return str .. 'xxx'
1595 enddef
1596 END
1597 writefile(lines, 'Xreload.vim')
1598 source Xreload.vim
1599 echo g:GetStr()
1600
1601 lines =<< trim END
1602 vim9script noclear
1603 var str = 1234
1604 END
1605 writefile(lines, 'Xreload.vim')
1606 source Xreload.vim
1607 assert_fails('echo g:GetStr()', 'E1150:')
1608
1609 delfunc g:GetStr
1610 delete('Xreload.vim')
1611enddef
1612
1613" Define CallFunc so that the test can be compiled
1614command CallFunc echo 'nop'
1615
1616def Test_script_reload_from_function()
1617 var lines =<< trim END
1618 vim9script
1619
Bram Moolenaar10611952022-04-03 21:11:34 +01001620 if exists('g:loadedThis')
Bram Moolenaard8448622022-01-07 21:39:52 +00001621 finish
1622 endif
Bram Moolenaar10611952022-04-03 21:11:34 +01001623 g:loadedThis = 1
Bram Moolenaard8448622022-01-07 21:39:52 +00001624 delcommand CallFunc
1625 command CallFunc Func()
1626 def Func()
1627 so XreloadFunc.vim
1628 g:didTheFunc = 1
1629 enddef
1630 END
1631 writefile(lines, 'XreloadFunc.vim')
1632 source XreloadFunc.vim
1633 CallFunc
1634 assert_equal(1, g:didTheFunc)
1635
1636 delete('XreloadFunc.vim')
1637 delcommand CallFunc
Bram Moolenaar10611952022-04-03 21:11:34 +01001638 unlet g:loadedThis
Bram Moolenaard8448622022-01-07 21:39:52 +00001639 unlet g:didTheFunc
1640enddef
1641
1642def s:RetSome(): string
1643 return 'some'
1644enddef
1645
1646" Not exported function that is referenced needs to be accessed by the
1647" script-local name.
1648def Test_vim9_funcref()
1649 var sortlines =<< trim END
1650 vim9script
1651 def Compare(i1: number, i2: number): number
1652 return i2 - i1
1653 enddef
1654
1655 export def FastSort(): list<number>
1656 return range(5)->sort(Compare)
1657 enddef
1658
1659 export def GetString(arg: string): string
1660 return arg
1661 enddef
1662 END
1663 writefile(sortlines, 'Xsort.vim')
1664
1665 var lines =<< trim END
1666 vim9script
1667 import './Xsort.vim'
1668 def Test()
1669 g:result = Xsort.FastSort()
1670 enddef
1671 Test()
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001672 END
1673 writefile(lines, 'Xscript.vim')
1674 source Xscript.vim
1675 assert_equal([4, 3, 2, 1, 0], g:result)
1676 unlet g:result
Bram Moolenaard8448622022-01-07 21:39:52 +00001677
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001678 lines =<< trim END
1679 vim9script
Bram Moolenaard8448622022-01-07 21:39:52 +00001680 # using a function imported with "as"
1681 import './Xsort.vim' as anAlias
1682 assert_equal('yes', anAlias.GetString('yes'))
1683
1684 # using the function from a compiled function
1685 def TestMore(): string
1686 var s = s:anAlias.GetString('foo')
1687 return s .. anAlias.GetString('bar')
1688 enddef
1689 assert_equal('foobar', TestMore())
1690
1691 # error when using a function that isn't exported
1692 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
1693 END
1694 writefile(lines, 'Xscript.vim')
1695
Bram Moolenaard8448622022-01-07 21:39:52 +00001696 delete('Xsort.vim')
1697 delete('Xscript.vim')
1698
1699 var Funcref = function('s:RetSome')
1700 assert_equal('some', Funcref())
1701enddef
1702
1703" Check that when searching for "FilterFunc" it finds the import in the
1704" script where FastFilter() is called from, both as a string and as a direct
1705" function reference.
1706def Test_vim9_funcref_other_script()
1707 var filterLines =<< trim END
1708 vim9script
1709 export def FilterFunc(idx: number, val: number): bool
1710 return idx % 2 == 1
1711 enddef
1712 export def FastFilter(): list<number>
1713 return range(10)->filter('FilterFunc(v:key, v:val)')
1714 enddef
1715 export def FastFilterDirect(): list<number>
1716 return range(10)->filter(FilterFunc)
1717 enddef
1718 END
1719 writefile(filterLines, 'Xfilter.vim')
1720
1721 var lines =<< trim END
1722 vim9script
1723 import './Xfilter.vim' as filter
1724 def Test()
1725 var x: list<number> = filter.FastFilter()
1726 enddef
1727 Test()
1728 def TestDirect()
1729 var x: list<number> = filter.FastFilterDirect()
1730 enddef
1731 TestDirect()
1732 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001733 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +00001734 delete('Xfilter.vim')
1735enddef
1736
1737def Test_import_absolute()
1738 var import_lines = [
1739 'vim9script',
1740 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
1741 'def UseExported()',
1742 ' g:imported_abs = abs.exported',
1743 ' abs.exported = 8888',
1744 ' g:imported_after = abs.exported',
1745 'enddef',
1746 'UseExported()',
1747 'g:import_disassembled = execute("disass UseExported")',
1748 ]
1749 writefile(import_lines, 'Ximport_abs.vim')
1750 writefile(s:export_script_lines, 'Xexport_abs.vim')
1751
1752 source Ximport_abs.vim
1753
1754 assert_equal(9876, g:imported_abs)
1755 assert_equal(8888, g:imported_after)
1756 assert_match('<SNR>\d\+_UseExported\_s*' ..
1757 'g:imported_abs = abs.exported\_s*' ..
1758 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1759 '1 STOREG g:imported_abs\_s*' ..
1760 'abs.exported = 8888\_s*' ..
1761 '2 PUSHNR 8888\_s*' ..
1762 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1763 'g:imported_after = abs.exported\_s*' ..
1764 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1765 '5 STOREG g:imported_after',
1766 g:import_disassembled)
1767
1768 Undo_export_script_lines()
1769 unlet g:imported_abs
1770 unlet g:import_disassembled
1771
1772 delete('Ximport_abs.vim')
1773 delete('Xexport_abs.vim')
1774enddef
1775
1776def Test_import_rtp()
1777 var import_lines = [
1778 'vim9script',
1779 'import "Xexport_rtp.vim" as rtp',
1780 'g:imported_rtp = rtp.exported',
1781 ]
1782 writefile(import_lines, 'Ximport_rtp.vim')
1783 mkdir('import', 'p')
1784 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1785
1786 var save_rtp = &rtp
1787 &rtp = getcwd()
1788 source Ximport_rtp.vim
1789 &rtp = save_rtp
1790
1791 assert_equal(9876, g:imported_rtp)
1792
1793 Undo_export_script_lines()
1794 unlet g:imported_rtp
1795 delete('Ximport_rtp.vim')
1796 delete('import', 'rf')
1797enddef
1798
1799def Test_import_compile_error()
1800 var export_lines = [
1801 'vim9script',
1802 'export def ExpFunc(): string',
1803 ' return notDefined',
1804 'enddef',
1805 ]
1806 writefile(export_lines, 'Xexported.vim')
1807
1808 var import_lines = [
1809 'vim9script',
1810 'import "./Xexported.vim" as expo',
1811 'def ImpFunc()',
1812 ' echo expo.ExpFunc()',
1813 'enddef',
1814 'defcompile',
1815 ]
1816 writefile(import_lines, 'Ximport.vim')
1817
1818 try
1819 source Ximport.vim
1820 catch /E1001/
1821 # Error should be before the Xexported.vim file.
1822 assert_match('E1001: Variable not found: notDefined', v:exception)
1823 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1824 endtry
1825
1826 delete('Xexported.vim')
1827 delete('Ximport.vim')
1828enddef
1829
1830def Test_func_overrules_import_fails()
1831 var export_lines =<< trim END
1832 vim9script
1833 export def Func()
1834 echo 'imported'
1835 enddef
1836 END
1837 writefile(export_lines, 'XexportedFunc.vim')
1838
1839 var lines =<< trim END
1840 vim9script
1841 import './XexportedFunc.vim' as Func
1842 def Func()
1843 echo 'local to function'
1844 enddef
1845 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001846 v9.CheckScriptFailure(lines, 'E1213: Redefining imported item "Func"')
Bram Moolenaard8448622022-01-07 21:39:52 +00001847
1848 lines =<< trim END
1849 vim9script
1850 import './XexportedFunc.vim' as Func
1851 def Outer()
1852 def Func()
1853 echo 'local to function'
1854 enddef
1855 enddef
1856 defcompile
1857 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001858 v9.CheckScriptFailure(lines, 'E1236:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001859
1860 delete('XexportedFunc.vim')
1861enddef
1862
1863def Test_source_vim9_from_legacy()
1864 var vim9_lines =<< trim END
1865 vim9script
1866 var local = 'local'
1867 g:global = 'global'
1868 export var exported = 'exported'
1869 export def GetText(): string
1870 return 'text'
1871 enddef
1872 END
1873 writefile(vim9_lines, 'Xvim9_script.vim')
1874
1875 var legacy_lines =<< trim END
1876 source Xvim9_script.vim
1877
1878 call assert_false(exists('local'))
1879 call assert_false(exists('exported'))
1880 call assert_false(exists('s:exported'))
1881 call assert_equal('global', global)
1882 call assert_equal('global', g:global)
Bram Moolenaard8448622022-01-07 21:39:52 +00001883 END
1884 writefile(legacy_lines, 'Xlegacy_script.vim')
1885
1886 source Xlegacy_script.vim
1887 assert_equal('global', g:global)
1888 unlet g:global
1889
1890 delete('Xlegacy_script.vim')
1891 delete('Xvim9_script.vim')
1892enddef
1893
Bram Moolenaarc43e6232022-01-13 20:51:56 +00001894def Test_import_vim9_from_legacy()
1895 var vim9_lines =<< trim END
1896 vim9script
1897 var local = 'local'
1898 g:global = 'global'
1899 export var exported = 'exported'
1900 export def GetText(): string
1901 return 'text'
1902 enddef
1903 END
1904 writefile(vim9_lines, 'Xvim9_export.vim')
1905
1906 var legacy_lines =<< trim END
1907 import './Xvim9_export.vim' as vim9
1908
1909 call assert_false(exists('vim9'))
1910 call assert_false(exists('local'))
1911 call assert_false(exists('s:vim9.local'))
1912 call assert_equal('global', global)
1913 call assert_equal('global', g:global)
1914 call assert_false(exists('exported'))
1915 call assert_false(exists('s:exported'))
1916 call assert_false(exists('*GetText'))
1917
1918 " imported symbol is script-local
1919 call assert_equal('exported', s:vim9.exported)
1920 call assert_equal('text', s:vim9.GetText())
1921 END
1922 writefile(legacy_lines, 'Xlegacy_script.vim')
1923
1924 source Xlegacy_script.vim
1925 assert_equal('global', g:global)
1926 unlet g:global
1927
1928 delete('Xlegacy_script.vim')
1929 delete('Xvim9_export.vim')
1930enddef
1931
Bram Moolenaard8448622022-01-07 21:39:52 +00001932def Test_cmdline_win()
1933 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
1934 # the command line window.
1935 mkdir('rtp/syntax', 'p')
1936 var export_lines =<< trim END
1937 vim9script
1938 export var That = 'yes'
1939 END
1940 writefile(export_lines, 'rtp/syntax/Xexport.vim')
1941 var import_lines =<< trim END
1942 vim9script
1943 import './Xexport.vim' as exp
1944 echo exp.That
1945 END
1946 writefile(import_lines, 'rtp/syntax/vim.vim')
1947 var save_rtp = &rtp
1948 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
1949 syntax on
1950 augroup CmdWin
1951 autocmd CmdwinEnter * g:got_there = 'yes'
1952 augroup END
1953 # this will open and also close the cmdline window
1954 feedkeys('q:', 'xt')
1955 assert_equal('yes', g:got_there)
1956
1957 augroup CmdWin
1958 au!
1959 augroup END
1960 &rtp = save_rtp
1961 delete('rtp', 'rf')
1962enddef
1963
1964def Test_import_gone_when_sourced_twice()
1965 var exportlines =<< trim END
1966 vim9script
1967 if exists('g:guard')
1968 finish
1969 endif
1970 g:guard = 1
1971 export var name = 'someName'
1972 END
1973 writefile(exportlines, 'XexportScript.vim')
1974
1975 var lines =<< trim END
1976 vim9script
1977 import './XexportScript.vim' as expo
1978 def g:GetName(): string
1979 return expo.name
1980 enddef
1981 END
1982 writefile(lines, 'XscriptImport.vim')
1983 so XscriptImport.vim
1984 assert_equal('someName', g:GetName())
1985
1986 so XexportScript.vim
1987 assert_fails('call g:GetName()', 'E1149:')
1988
1989 delfunc g:GetName
1990 delete('XexportScript.vim')
1991 delete('XscriptImport.vim')
1992 unlet g:guard
1993enddef
1994
Bram Moolenaar160aa862022-01-10 21:29:57 +00001995" test using an auto-loaded function and variable
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00001996def Test_vim9_autoload_full_name()
Bram Moolenaar160aa862022-01-10 21:29:57 +00001997 var lines =<< trim END
1998 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00001999 export def Gettest(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002000 return 'test'
2001 enddef
2002 g:some#name = 'name'
2003 g:some#dict = {key: 'value'}
2004
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002005 export def Varargs(a1: string, ...l: list<string>): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002006 return a1 .. l[0] .. l[1]
2007 enddef
2008 END
2009
2010 mkdir('Xdir/autoload', 'p')
2011 writefile(lines, 'Xdir/autoload/some.vim')
2012 var save_rtp = &rtp
2013 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2014
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002015 assert_equal('test', g:some#Gettest())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002016 assert_equal('name', g:some#name)
2017 assert_equal('value', g:some#dict.key)
2018 g:some#other = 'other'
2019 assert_equal('other', g:some#other)
2020
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002021 assert_equal('abc', some#Varargs('a', 'b', 'c'))
Bram Moolenaar160aa862022-01-10 21:29:57 +00002022
2023 # upper case script name works
2024 lines =<< trim END
2025 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002026 export def GetOther(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002027 return 'other'
2028 enddef
2029 END
2030 writefile(lines, 'Xdir/autoload/Other.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002031 assert_equal('other', g:Other#GetOther())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002032
2033 delete('Xdir', 'rf')
2034 &rtp = save_rtp
2035enddef
2036
2037def Test_vim9script_autoload()
2038 mkdir('Xdir/autoload', 'p')
2039 var save_rtp = &rtp
2040 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2041
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002042 # when the path has "/autoload/" prefix is not needed
Bram Moolenaar160aa862022-01-10 21:29:57 +00002043 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002044 vim9script
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002045 g:prefixed_loaded += 1
Bram Moolenaar160aa862022-01-10 21:29:57 +00002046
2047 export def Gettest(): string
2048 return 'test'
2049 enddef
2050
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002051 export var name = 'name'
2052
2053 export func GetFunc()
2054 return Gettest() .. 'more' .. s:name
Bram Moolenaar160aa862022-01-10 21:29:57 +00002055 endfunc
2056
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002057 export def GetDef(): string
2058 return Gettest() .. 'more' .. name
2059 enddef
2060
Bram Moolenaar160aa862022-01-10 21:29:57 +00002061 export final fname = 'final'
2062 export const cname = 'const'
2063 END
2064 writefile(lines, 'Xdir/autoload/prefixed.vim')
2065
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002066 g:prefixed_loaded = 0
2067 g:expected_loaded = 0
Bram Moolenaar160aa862022-01-10 21:29:57 +00002068 lines =<< trim END
2069 vim9script
2070 import autoload 'prefixed.vim'
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002071 assert_equal(g:expected_loaded, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002072 assert_equal('test', prefixed.Gettest())
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002073 assert_equal(1, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002074
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002075 assert_equal('testmorename', prefixed.GetFunc())
2076 assert_equal('testmorename', prefixed.GetDef())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002077 assert_equal('name', prefixed.name)
2078 assert_equal('final', prefixed.fname)
2079 assert_equal('const', prefixed.cname)
2080 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002081 v9.CheckScriptSuccess(lines)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002082 # can source it again, autoload script not loaded again
2083 g:expected_loaded = 1
Bram Moolenaar62aec932022-01-29 21:45:34 +00002084 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002085
2086 # can also get the items by autoload name
2087 lines =<< trim END
2088 call assert_equal('test', prefixed#Gettest())
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002089 call assert_equal('testmorename', prefixed#GetFunc())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002090 call assert_equal('name', prefixed#name)
2091 call assert_equal('final', prefixed#fname)
2092 call assert_equal('const', prefixed#cname)
2093 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002094 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002095
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002096 unlet g:prefixed_loaded
2097 unlet g:expected_loaded
2098 delete('Xdir', 'rf')
2099 &rtp = save_rtp
2100enddef
2101
Bram Moolenaard02dce22022-01-18 17:43:04 +00002102def Test_import_autoload_not_exported()
2103 mkdir('Xdir/autoload', 'p')
2104 var save_rtp = &rtp
2105 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2106
2107 # error when using an item that is not exported from an autoload script
2108 var exportLines =<< trim END
2109 vim9script
2110 var notExported = 123
2111 def NotExport()
2112 echo 'nop'
2113 enddef
2114 END
2115 writefile(exportLines, 'Xdir/autoload/notExport1.vim')
2116
2117 var lines =<< trim END
2118 vim9script
2119 import autoload 'notExport1.vim'
2120 echo notExport1.notFound
2121 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002122 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002123
2124 lines =<< trim END
2125 vim9script
2126 import autoload 'notExport1.vim'
2127 echo notExport1.notExported
2128 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002129 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002130
2131 lines =<< trim END
2132 vim9script
2133 import autoload 'notExport1.vim'
2134 echo notExport1.NotFunc()
2135 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002136 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002137
2138 lines =<< trim END
2139 vim9script
2140 import autoload 'notExport1.vim'
2141 echo notExport1.NotExport()
2142 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002143 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002144
2145 lines =<< trim END
2146 vim9script
2147 import autoload 'notExport1.vim'
2148 echo 'text'->notExport1.NotFunc()
2149 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002150 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002151
2152 lines =<< trim END
2153 vim9script
2154 import autoload 'notExport1.vim'
2155 echo 'text'->notExport1.NotExport()
2156 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002157 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002158
2159 # using a :def function we use a different autoload script every time so that
2160 # the function is compiled without the script loaded
2161 writefile(exportLines, 'Xdir/autoload/notExport2.vim')
2162 lines =<< trim END
2163 vim9script
2164 import autoload 'notExport2.vim'
2165 def Testit()
2166 echo notExport2.notFound
2167 enddef
2168 Testit()
2169 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002170 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport2#notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002171
2172 writefile(exportLines, 'Xdir/autoload/notExport3.vim')
2173 lines =<< trim END
2174 vim9script
2175 import autoload 'notExport3.vim'
2176 def Testit()
2177 echo notExport3.notExported
2178 enddef
2179 Testit()
2180 END
2181 # don't get E1049 because it is too complicated to figure out
Bram Moolenaar62aec932022-01-29 21:45:34 +00002182 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport3#notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002183
2184 writefile(exportLines, 'Xdir/autoload/notExport4.vim')
2185 lines =<< trim END
2186 vim9script
2187 import autoload 'notExport4.vim'
2188 def Testit()
2189 echo notExport4.NotFunc()
2190 enddef
2191 Testit()
2192 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002193 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport4#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002194
2195 writefile(exportLines, 'Xdir/autoload/notExport5.vim')
2196 lines =<< trim END
2197 vim9script
2198 import autoload 'notExport5.vim'
2199 def Testit()
2200 echo notExport5.NotExport()
2201 enddef
2202 Testit()
2203 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002204 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport5#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002205
2206 writefile(exportLines, 'Xdir/autoload/notExport6.vim')
2207 lines =<< trim END
2208 vim9script
2209 import autoload 'notExport6.vim'
2210 def Testit()
2211 echo 'text'->notExport6.NotFunc()
2212 enddef
2213 Testit()
2214 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002215 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport6#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002216
2217 writefile(exportLines, 'Xdir/autoload/notExport7.vim')
2218 lines =<< trim END
2219 vim9script
2220 import autoload 'notExport7.vim'
2221 def Testit()
2222 echo 'text'->notExport7.NotExport()
2223 enddef
2224 Testit()
2225 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002226 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport7#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002227
2228 delete('Xdir', 'rf')
2229 &rtp = save_rtp
2230enddef
2231
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002232def Test_vim9script_autoload_call()
2233 mkdir('Xdir/autoload', 'p')
2234 var save_rtp = &rtp
2235 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2236
2237 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002238 vim9script
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002239
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002240 export def RetArg(arg: string): string
2241 return arg
2242 enddef
2243
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002244 export def Getother()
2245 g:result = 'other'
2246 enddef
2247 END
Bram Moolenaar5d982692022-01-12 15:15:27 +00002248 writefile(lines, 'Xdir/autoload/another.vim')
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002249
2250 lines =<< trim END
2251 vim9script
Bram Moolenaar5d982692022-01-12 15:15:27 +00002252 import autoload 'another.vim'
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002253
2254 # compile this before 'another.vim' is loaded
2255 def CallAnother()
2256 assert_equal('foo', 'foo'->another.RetArg())
2257 enddef
2258 CallAnother()
2259
Bram Moolenaar5d982692022-01-12 15:15:27 +00002260 call another.Getother()
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002261 assert_equal('other', g:result)
Bram Moolenaar3d8e25a2022-01-22 11:00:02 +00002262
2263 assert_equal('arg', call('another.RetArg', ['arg']))
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00002264
2265 verbose function another.Getother
2266 # should we disallow this?
2267 verbose function another#Getother
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002268 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002269 v9.CheckScriptSuccess(lines)
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002270
2271 unlet g:result
Bram Moolenaar160aa862022-01-10 21:29:57 +00002272 delete('Xdir', 'rf')
2273 &rtp = save_rtp
2274enddef
2275
Bram Moolenaarb697dc22022-01-22 11:27:29 +00002276def Test_vim9script_noclear_autoload()
2277 mkdir('Xdir/autoload', 'p')
2278 var save_rtp = &rtp
2279 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2280
2281 var lines =<< trim END
2282 vim9script
2283 export def Func(): string
2284 return 'called'
2285 enddef
2286 g:double_loaded = 'yes'
2287 END
2288 writefile(lines, 'Xdir/autoload/double.vim')
2289
2290 lines =<< trim END
2291 vim9script noclear
2292 if exists('g:script_loaded')
2293 finish
2294 endif
2295 g:script_loaded = true
2296
2297 import autoload 'double.vim'
2298 nnoremap <F3> <ScriptCmd>g:result = double.Func()<CR>
2299 END
2300 g:double_loaded = 'no'
2301 writefile(lines, 'Xloaddouble')
2302 source Xloaddouble
2303 assert_equal('no', g:double_loaded)
2304 assert_equal(true, g:script_loaded)
2305 source Xloaddouble
2306 feedkeys("\<F3>", 'xt')
2307 assert_equal('called', g:result)
2308 assert_equal('yes', g:double_loaded)
2309
2310 delete('Xloaddouble')
2311 unlet g:double_loaded
2312 unlet g:script_loaded
2313 unlet g:result
2314 delete('Xdir', 'rf')
2315 &rtp = save_rtp
2316enddef
2317
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002318def Test_vim9script_autoload_duplicate()
2319 mkdir('Xdir/autoload', 'p')
2320
2321 var lines =<< trim END
2322 vim9script
2323
2324 export def Func()
2325 enddef
2326
2327 def Func()
2328 enddef
2329 END
2330 writefile(lines, 'Xdir/autoload/dupfunc.vim')
2331 assert_fails('source Xdir/autoload/dupfunc.vim', 'E1073:')
2332
2333 lines =<< trim END
2334 vim9script
2335
2336 def Func()
2337 enddef
2338
2339 export def Func()
2340 enddef
2341 END
2342 writefile(lines, 'Xdir/autoload/dup2func.vim')
2343 assert_fails('source Xdir/autoload/dup2func.vim', 'E1073:')
2344
2345 lines =<< trim END
2346 vim9script
2347
2348 def Func()
2349 enddef
2350
2351 export var Func = 'asdf'
2352 END
2353 writefile(lines, 'Xdir/autoload/dup3func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002354 assert_fails('source Xdir/autoload/dup3func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002355
2356 lines =<< trim END
2357 vim9script
2358
2359 export var Func = 'asdf'
2360
2361 def Func()
2362 enddef
2363 END
2364 writefile(lines, 'Xdir/autoload/dup4func.vim')
2365 assert_fails('source Xdir/autoload/dup4func.vim', 'E707:')
2366
2367 lines =<< trim END
2368 vim9script
2369
2370 var Func = 'asdf'
2371
2372 export def Func()
2373 enddef
2374 END
2375 writefile(lines, 'Xdir/autoload/dup5func.vim')
2376 assert_fails('source Xdir/autoload/dup5func.vim', 'E707:')
2377
2378 lines =<< trim END
2379 vim9script
2380
2381 export def Func()
2382 enddef
2383
2384 var Func = 'asdf'
2385 END
2386 writefile(lines, 'Xdir/autoload/dup6func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002387 assert_fails('source Xdir/autoload/dup6func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002388
2389 delete('Xdir', 'rf')
2390enddef
2391
Bram Moolenaar2017d6f2022-01-20 19:38:46 +00002392def Test_autoload_missing_function_name()
2393 mkdir('Xdir/autoload', 'p')
2394
2395 var lines =<< trim END
2396 vim9script
2397
2398 def loadme#()
2399 enddef
2400 END
2401 writefile(lines, 'Xdir/autoload/loadme.vim')
2402 assert_fails('source Xdir/autoload/loadme.vim', 'E129:')
2403
2404 delete('Xdir', 'rf')
2405enddef
2406
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002407def Test_autoload_name_wrong()
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002408 var lines =<< trim END
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002409 def Xscriptname#Func()
2410 enddef
2411 END
2412 writefile(lines, 'Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002413 v9.CheckScriptFailure(lines, 'E746:')
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00002414 delete('Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002415
2416 mkdir('Xdir/autoload', 'p')
2417 lines =<< trim END
2418 vim9script
2419 def somescript#Func()
2420 enddef
2421 END
2422 writefile(lines, 'Xdir/autoload/somescript.vim')
2423 assert_fails('source Xdir/autoload/somescript.vim', 'E1263:')
2424
2425 delete('Xdir', 'rf')
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002426enddef
2427
Bram Moolenaard041f422022-01-12 19:54:00 +00002428def Test_import_autoload_postponed()
2429 mkdir('Xdir/autoload', 'p')
2430 var save_rtp = &rtp
2431 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2432
2433 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002434 vim9script
Bram Moolenaard041f422022-01-12 19:54:00 +00002435
2436 g:loaded_postponed = 'true'
2437 export var variable = 'bla'
2438 export def Function(): string
2439 return 'bla'
2440 enddef
2441 END
2442 writefile(lines, 'Xdir/autoload/postponed.vim')
2443
2444 lines =<< trim END
2445 vim9script
2446
2447 import autoload 'postponed.vim'
2448 def Tryit()
2449 echo postponed.variable
2450 echo postponed.Function()
2451 enddef
2452 defcompile
2453 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002454 v9.CheckScriptSuccess(lines)
Bram Moolenaard041f422022-01-12 19:54:00 +00002455 assert_false(exists('g:loaded_postponed'))
Bram Moolenaar62aec932022-01-29 21:45:34 +00002456 v9.CheckScriptSuccess(lines + ['Tryit()'])
Bram Moolenaard041f422022-01-12 19:54:00 +00002457 assert_equal('true', g:loaded_postponed)
2458
2459 unlet g:loaded_postponed
2460 delete('Xdir', 'rf')
2461 &rtp = save_rtp
2462enddef
2463
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002464def Test_import_autoload_override()
2465 mkdir('Xdir/autoload', 'p')
2466 var save_rtp = &rtp
2467 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2468 test_override('autoload', 1)
2469
2470 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002471 vim9script
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002472
2473 g:loaded_override = 'true'
2474 export var variable = 'bla'
2475 export def Function(): string
2476 return 'bla'
2477 enddef
2478 END
2479 writefile(lines, 'Xdir/autoload/override.vim')
2480
2481 lines =<< trim END
2482 vim9script
2483
2484 import autoload 'override.vim'
2485 assert_equal('true', g:loaded_override)
2486
2487 def Tryit()
2488 echo override.doesNotExist
2489 enddef
2490 defcompile
2491 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002492 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1)
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002493
2494 test_override('autoload', 0)
2495 unlet g:loaded_override
2496 delete('Xdir', 'rf')
2497 &rtp = save_rtp
2498enddef
2499
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002500def Test_autoload_mapping()
2501 mkdir('Xdir/autoload', 'p')
2502 var save_rtp = &rtp
2503 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2504
2505 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002506 vim9script
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002507
2508 g:toggle_loaded = 'yes'
2509
2510 export def Toggle(): string
2511 return ":g:toggle_called = 'yes'\<CR>"
2512 enddef
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002513 export def Doit()
2514 g:doit_called = 'yes'
2515 enddef
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002516 END
2517 writefile(lines, 'Xdir/autoload/toggle.vim')
2518
2519 lines =<< trim END
2520 vim9script
2521
2522 import autoload 'toggle.vim'
2523
2524 nnoremap <silent> <expr> tt toggle.Toggle()
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002525 nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR>
2526 nnoremap <silent> yy <Cmd>toggle.Doit()<CR>
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002527 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002528 v9.CheckScriptSuccess(lines)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002529 assert_false(exists("g:toggle_loaded"))
2530 assert_false(exists("g:toggle_called"))
Bram Moolenaar6079da72022-01-18 14:16:59 +00002531 assert_match('\d A: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002532
2533 feedkeys("tt", 'xt')
2534 assert_equal('yes', g:toggle_loaded)
2535 assert_equal('yes', g:toggle_called)
Bram Moolenaar6079da72022-01-18 14:16:59 +00002536 assert_match('\d: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002537
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002538 feedkeys("xx", 'xt')
2539 assert_equal('yes', g:doit_called)
2540
2541 assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle')
2542
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002543 nunmap tt
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002544 nunmap xx
2545 nunmap yy
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002546 unlet g:toggle_loaded
2547 unlet g:toggle_called
2548 delete('Xdir', 'rf')
2549 &rtp = save_rtp
2550enddef
2551
Bram Moolenaar160aa862022-01-10 21:29:57 +00002552def Test_vim9script_autoload_fails()
2553 var lines =<< trim END
2554 vim9script autoload
2555 var n = 0
2556 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002557 v9.CheckScriptFailure(lines, 'E475: Invalid argument: autoload')
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002558
2559 lines =<< trim END
2560 vim9script noclear noclear
2561 var n = 0
2562 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002563 v9.CheckScriptFailure(lines, 'E983: Duplicate argument: noclear')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002564enddef
2565
2566def Test_import_autoload_fails()
2567 var lines =<< trim END
2568 vim9script
2569 import autoload autoload 'prefixed.vim'
2570 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002571 v9.CheckScriptFailure(lines, 'E121: Undefined variable: autoload')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002572
2573 lines =<< trim END
2574 vim9script
Bram Moolenaar1836d612022-01-18 13:14:47 +00002575 import autoload './doesNotExist.vim'
Bram Moolenaar160aa862022-01-10 21:29:57 +00002576 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002577 v9.CheckScriptFailure(lines, 'E282:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002578
2579 lines =<< trim END
2580 vim9script
2581 import autoload '/dir/doesNotExist.vim'
2582 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002583 v9.CheckScriptFailure(lines, 'E282:', 2)
2584
2585 lines =<< trim END
2586 vim9script
2587 import autoload '../testdir'
2588 END
2589 v9.CheckScriptFailure(lines, 'E17:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002590
2591 lines =<< trim END
2592 vim9script
2593 import autoload 'doesNotExist.vim'
2594 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002595 v9.CheckScriptFailure(lines, 'E1053: Could not import "doesNotExist.vim"')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002596enddef
2597
2598" test disassembling an auto-loaded function starting with "debug"
2599def Test_vim9_autoload_disass()
2600 mkdir('Xdir/autoload', 'p')
2601 var save_rtp = &rtp
2602 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2603
2604 var lines =<< trim END
2605 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002606 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002607 return 'debug'
2608 enddef
2609 END
2610 writefile(lines, 'Xdir/autoload/debugit.vim')
2611
2612 lines =<< trim END
2613 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002614 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002615 return 'profile'
2616 enddef
2617 END
2618 writefile(lines, 'Xdir/autoload/profileit.vim')
2619
2620 lines =<< trim END
2621 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002622 assert_equal('debug', debugit#Test())
2623 disass debugit#Test
2624 assert_equal('profile', profileit#Test())
2625 disass profileit#Test
Bram Moolenaar160aa862022-01-10 21:29:57 +00002626 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002627 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002628
2629 delete('Xdir', 'rf')
2630 &rtp = save_rtp
2631enddef
2632
2633" test using a vim9script that is auto-loaded from an autocmd
2634def Test_vim9_aucmd_autoload()
2635 var lines =<< trim END
2636 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002637 export def Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002638 echomsg getreg('"')
2639 enddef
2640 END
2641
2642 mkdir('Xdir/autoload', 'p')
2643 writefile(lines, 'Xdir/autoload/foo.vim')
2644 var save_rtp = &rtp
2645 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2646 augroup test
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002647 autocmd TextYankPost * call foo#Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002648 augroup END
2649
2650 normal Y
2651
2652 augroup test
2653 autocmd!
2654 augroup END
2655 delete('Xdir', 'rf')
2656 &rtp = save_rtp
2657enddef
2658
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002659" test using a autoloaded file that is case sensitive
2660def Test_vim9_autoload_case_sensitive()
2661 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002662 vim9script
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002663 export def CaseSensitive(): string
2664 return 'done'
2665 enddef
2666 END
2667
2668 mkdir('Xdir/autoload', 'p')
2669 writefile(lines, 'Xdir/autoload/CaseSensitive.vim')
2670 var save_rtp = &rtp
2671 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2672
2673 lines =<< trim END
2674 vim9script
2675 import autoload 'CaseSensitive.vim'
2676 assert_equal('done', CaseSensitive.CaseSensitive())
2677 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002678 v9.CheckScriptSuccess(lines)
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002679
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002680 if !has('fname_case')
2681 lines =<< trim END
2682 vim9script
2683 import autoload 'CaseSensitive.vim'
2684 import autoload 'casesensitive.vim'
2685 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002686 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002687 endif
2688
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002689 delete('Xdir', 'rf')
2690 &rtp = save_rtp
2691enddef
2692
Bram Moolenaar160aa862022-01-10 21:29:57 +00002693" This was causing a crash because suppress_errthrow wasn't reset.
2694def Test_vim9_autoload_error()
2695 var lines =<< trim END
2696 vim9script
2697 def crash#func()
2698 try
2699 for x in List()
2700 endfor
2701 catch
2702 endtry
2703 g:ok = true
2704 enddef
2705 fu List()
2706 invalid
2707 endfu
2708 try
2709 alsoinvalid
2710 catch /wontmatch/
2711 endtry
2712 END
2713 call mkdir('Xruntime/autoload', 'p')
2714 call writefile(lines, 'Xruntime/autoload/crash.vim')
2715
2716 # run in a separate Vim to avoid the side effects of assert_fails()
2717 lines =<< trim END
2718 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
2719 call crash#func()
2720 call writefile(['ok'], 'Xdidit')
2721 qall!
2722 END
2723 writefile(lines, 'Xscript')
Bram Moolenaar62aec932022-01-29 21:45:34 +00002724 g:RunVim([], [], '-S Xscript')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002725 assert_equal(['ok'], readfile('Xdidit'))
2726
2727 delete('Xdidit')
2728 delete('Xscript')
2729 delete('Xruntime', 'rf')
2730
2731 lines =<< trim END
2732 vim9script
2733 var foo#bar = 'asdf'
2734 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002735 v9.CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002736enddef
2737
Bram Moolenaard8448622022-01-07 21:39:52 +00002738
2739" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker