blob: ebd875ed9530611095dc1343d6d0faff47ed2495 [file] [log] [blame]
Bram Moolenaard8448622022-01-07 21:39:52 +00001" Test import/export of the Vim9 script language.
Bram Moolenaar160aa862022-01-10 21:29:57 +00002" Also the autoload mechanism.
Bram Moolenaard8448622022-01-07 21:39:52 +00003
4source check.vim
5source term_util.vim
Bram Moolenaar62aec932022-01-29 21:45:34 +00006import './vim9.vim' as v9
Bram Moolenaard8448622022-01-07 21:39:52 +00007
8let s:export_script_lines =<< trim END
9 vim9script
10 var name: string = 'bob'
11 def Concat(arg: string): string
12 return name .. arg
13 enddef
14 g:result = Concat('bie')
15 g:localname = name
16
17 export const CONST = 1234
18 export var exported = 9876
19 export var exp_name = 'John'
20 export def Exported(): string
21 return 'Exported'
22 enddef
23 export def ExportedValue(): number
24 return exported
25 enddef
26 export def ExportedInc()
27 exported += 5
28 enddef
29 export final theList = [1]
Bram Moolenaar857c8bb2022-01-15 21:08:19 +000030 export def AddSome(s: string): string
31 return s .. 'some'
32 enddef
33 export var AddRef = AddSome
Bram Moolenaard8448622022-01-07 21:39:52 +000034END
35
Bram Moolenaar62aec932022-01-29 21:45:34 +000036def s:Undo_export_script_lines()
Bram Moolenaard8448622022-01-07 21:39:52 +000037 unlet g:result
38 unlet g:localname
39enddef
40
41def Test_vim9_import_export()
42 writefile(s:export_script_lines, 'Xexport.vim')
43 var import_script_lines =<< trim END
44 vim9script
45 var dir = './'
46 var ext = ".vim"
47 import dir .. 'Xexport' .. ext as expo
48
49 g:exported1 = expo.exported
50 expo.exported += 3
51 g:exported2 = expo.exported
52 g:exported3 = expo.ExportedValue()
53
54 expo.ExportedInc()
55 g:exported_i1 = expo.exported
56 g:exported_i2 = expo.ExportedValue()
57
58 expo.exported = 11
59 g:exported_s1 = expo.exported
60 g:exported_s2 = expo.ExportedValue()
61
62 g:imported_func = expo.Exported()
63
64 def GetExported(): string
65 var local_dict = {ref: expo.Exported}
66 return local_dict.ref()
67 enddef
68 g:funcref_result = GetExported()
69
Bram Moolenaar21f0d6c2022-01-20 17:35:49 +000070 def GetName(): string
71 return expo.exp_name .. 'son'
72 enddef
73 g:long_name = GetName()
74
Bram Moolenaard8448622022-01-07 21:39:52 +000075 g:imported_name = expo.exp_name
76 expo.exp_name ..= ' Doe'
Bram Moolenaar47036b62022-01-16 21:18:53 +000077 expo.exp_name = expo.exp_name .. ' Maar'
Bram Moolenaard8448622022-01-07 21:39:52 +000078 g:imported_name_appended = expo.exp_name
79 g:exported_later = expo.exported
80
81 expo.theList->add(2)
82 assert_equal([1, 2], expo.theList)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +000083
84 assert_equal('andthensome', 'andthen'->expo.AddSome())
85 assert_equal('awesome', 'awe'->expo.AddRef())
Bram Moolenaard8448622022-01-07 21:39:52 +000086 END
87 writefile(import_script_lines, 'Ximport.vim')
88 source Ximport.vim
89
90 assert_equal('bobbie', g:result)
91 assert_equal('bob', g:localname)
92 assert_equal(9876, g:exported1)
93 assert_equal(9879, g:exported2)
94 assert_equal(9879, g:exported3)
95
96 assert_equal(9884, g:exported_i1)
97 assert_equal(9884, g:exported_i2)
98
99 assert_equal(11, g:exported_s1)
100 assert_equal(11, g:exported_s2)
101 assert_equal(11, g:exported_later)
102
103 assert_equal('Exported', g:imported_func)
104 assert_equal('Exported', g:funcref_result)
105 assert_equal('John', g:imported_name)
Bram Moolenaar21f0d6c2022-01-20 17:35:49 +0000106 assert_equal('Johnson', g:long_name)
Bram Moolenaar47036b62022-01-16 21:18:53 +0000107 assert_equal('John Doe Maar', g:imported_name_appended)
Bram Moolenaard8448622022-01-07 21:39:52 +0000108 assert_false(exists('g:name'))
109
110 Undo_export_script_lines()
111 unlet g:exported1
112 unlet g:exported2
113 unlet g:exported3
114 unlet g:exported_i1
115 unlet g:exported_i2
116 unlet g:exported_later
117 unlet g:imported_func
Bram Moolenaar21f0d6c2022-01-20 17:35:49 +0000118 unlet g:imported_name g:long_name g:imported_name_appended
Bram Moolenaard8448622022-01-07 21:39:52 +0000119 delete('Ximport.vim')
120
121 # similar, with line breaks
122 var import_line_break_script_lines =<< trim END
123 vim9script
124 import './Xexport.vim'
125 as expo
126 g:exported = expo.exported
127 expo.exported += 7
128 g:exported_added = expo.exported
129 g:imported_func = expo.Exported()
130 END
131 writefile(import_line_break_script_lines, 'Ximport_lbr.vim')
132 source Ximport_lbr.vim
133
134 assert_equal(11, g:exported)
135 assert_equal(18, g:exported_added)
136 assert_equal('Exported', g:imported_func)
137
138 # exported script not sourced again
139 assert_false(exists('g:result'))
140 unlet g:exported
141 unlet g:exported_added
142 unlet g:imported_func
143 delete('Ximport_lbr.vim')
144
Bram Moolenaar68854a82022-01-31 18:59:13 +0000145 var import_shadows_cmdmod_lines =<< trim END
146 vim9script
147 import './Xexport.vim' as vim9
148 vim9.exp_name = 'Shadow'
149 assert_equal('Shadow', vim9.exp_name)
150 END
151 v9.CheckScriptSuccess(import_shadows_cmdmod_lines)
152
Bram Moolenaard8448622022-01-07 21:39:52 +0000153 var line_break_before_dot =<< trim END
154 vim9script
155 import './Xexport.vim' as expo
156 g:exported = expo
157 .exported
158 END
159 writefile(line_break_before_dot, 'Ximport_lbr_before_dot.vim')
160 assert_fails('source Ximport_lbr_before_dot.vim', 'E1060:', '', 3)
161 delete('Ximport_lbr_before_dot.vim')
162
163 var line_break_after_dot =<< trim END
164 vim9script
165 import './Xexport.vim' as expo
166 g:exported = expo.
167 exported
168 END
169 writefile(line_break_after_dot, 'Ximport_lbr_after_dot.vim')
170 assert_fails('source Ximport_lbr_after_dot.vim', 'E1074:', '', 3)
171 delete('Ximport_lbr_after_dot.vim')
172
173 var import_star_as_lines =<< trim END
174 vim9script
175 import './Xexport.vim' as Export
176 def UseExport()
177 g:exported_def = Export.exported
178 enddef
179 g:exported_script = Export.exported
180 assert_equal(1, exists('Export.exported'))
181 assert_equal(0, exists('Export.notexported'))
182 UseExport()
183 END
184 writefile(import_star_as_lines, 'Ximport.vim')
185 source Ximport.vim
186
187 assert_equal(18, g:exported_def)
188 assert_equal(18, g:exported_script)
189 unlet g:exported_def
190 unlet g:exported_script
191
192 var import_star_as_lines_no_dot =<< trim END
193 vim9script
194 import './Xexport.vim' as Export
195 def Func()
196 var dummy = 1
197 var imported = Export + dummy
198 enddef
199 defcompile
200 END
201 writefile(import_star_as_lines_no_dot, 'Ximport.vim')
202 assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
203
204 var import_star_as_lines_dot_space =<< trim END
205 vim9script
206 import './Xexport.vim' as Export
207 def Func()
208 var imported = Export . exported
209 enddef
210 defcompile
211 END
212 writefile(import_star_as_lines_dot_space, 'Ximport.vim')
213 assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
214
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000215 writefile(s:export_script_lines, 'Xexport2.vim')
216 var import_as_duplicated =<< trim END
Bram Moolenaard8448622022-01-07 21:39:52 +0000217 vim9script
218 import './Xexport.vim' as expo
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000219 import './Xexport2.vim' as expo
Bram Moolenaard8448622022-01-07 21:39:52 +0000220 END
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000221 writefile(import_as_duplicated, 'Ximport.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000222 assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000223 delete('Xexport2.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000224
225 var import_star_as_lines_script_no_dot =<< trim END
226 vim9script
227 import './Xexport.vim' as Export
228 g:imported_script = Export exported
229 END
230 writefile(import_star_as_lines_script_no_dot, 'Ximport.vim')
231 assert_fails('source Ximport.vim', 'E1060: Expected dot after name: Export exported')
232
233 var import_star_as_lines_script_space_after_dot =<< trim END
234 vim9script
235 import './Xexport.vim' as Export
236 g:imported_script = Export. exported
237 END
238 writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim')
239 assert_fails('source Ximport.vim', 'E1074:')
240
241 var import_star_as_lines_missing_name =<< trim END
242 vim9script
243 import './Xexport.vim' as Export
244 def Func()
245 var imported = Export.
246 enddef
247 defcompile
248 END
249 writefile(import_star_as_lines_missing_name, 'Ximport.vim')
250 assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
251
252 var import_star_as_lbr_lines =<< trim END
253 vim9script
254 import './Xexport.vim'
255 as Export
256 def UseExport()
257 g:exported = Export.exported
258 enddef
259 UseExport()
260 END
261 writefile(import_star_as_lbr_lines, 'Ximport.vim')
262 source Ximport.vim
263 assert_equal(18, g:exported)
264 unlet g:exported
265
266 # try to use something that exists but is not exported
267 var import_not_exported_lines =<< trim END
268 vim9script
269 import './Xexport.vim' as expo
270 echo expo.name
271 END
272 writefile(import_not_exported_lines, 'Ximport.vim')
273 assert_fails('source Ximport.vim', 'E1049:', '', 3, 'Ximport.vim')
274
275 # try to import something that is already defined
276 var import_already_defined =<< trim END
277 vim9script
278 var exported = 'something'
279 import './Xexport.vim' as exported
280 END
281 writefile(import_already_defined, 'Ximport.vim')
282 assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim')
283
284 # try changing an imported const
285 var import_assign_to_const =<< trim END
286 vim9script
287 import './Xexport.vim' as expo
288 def Assign()
289 expo.CONST = 987
290 enddef
291 defcompile
292 END
293 writefile(import_assign_to_const, 'Ximport.vim')
294 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
295
296 # try changing an imported final
297 var import_assign_to_final =<< trim END
298 vim9script
299 import './Xexport.vim' as expo
300 def Assign()
301 expo.theList = [2]
302 enddef
303 defcompile
304 END
305 writefile(import_assign_to_final, 'Ximport.vim')
306 assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign')
307
308 var import_no_as_lines =<< trim END
309 vim9script
310 import './Xexport.vim' name
311 END
312 writefile(import_no_as_lines, 'Ximport.vim')
313 assert_fails('source Ximport.vim', 'E488:', '', 2, 'Ximport.vim')
314
315 var import_invalid_string_lines =<< trim END
316 vim9script
317 import Xexport.vim
318 END
319 writefile(import_invalid_string_lines, 'Ximport.vim')
320 assert_fails('source Ximport.vim', 'E121:', '', 2, 'Ximport.vim')
321
322 var import_wrong_name_lines =<< trim END
323 vim9script
324 import './XnoExport.vim'
325 END
326 writefile(import_wrong_name_lines, 'Ximport.vim')
327 assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
328
329 var import_redefining_lines =<< trim END
330 vim9script
331 import './Xexport.vim' as exported
332 var exported = 5
333 END
334 writefile(import_redefining_lines, 'Ximport.vim')
335 assert_fails('source Ximport.vim', 'E1213: Redefining imported item "exported"', '', 3)
336
Bram Moolenaar160aa862022-01-10 21:29:57 +0000337 var import_missing_dot_lines =<< trim END
338 vim9script
339 import './Xexport.vim' as expo
340 def Test()
341 expo = 9
342 enddef
343 defcompile
344 END
345 writefile(import_missing_dot_lines, 'Ximport.vim')
346 assert_fails('source Ximport.vim', 'E1258:', '', 1)
347
348 var import_missing_name_lines =<< trim END
349 vim9script
350 import './Xexport.vim' as expo
351 def Test()
352 expo.99 = 9
353 enddef
354 defcompile
355 END
356 writefile(import_missing_name_lines, 'Ximport.vim')
Bram Moolenaar76283822022-01-10 21:39:03 +0000357 assert_fails('source Ximport.vim', 'E1259:', '', 1)
Bram Moolenaar160aa862022-01-10 21:29:57 +0000358
Bram Moolenaard8448622022-01-07 21:39:52 +0000359 var import_assign_wrong_type_lines =<< trim END
360 vim9script
361 import './Xexport.vim' as expo
362 expo.exported = 'xxx'
363 END
364 writefile(import_assign_wrong_type_lines, 'Ximport.vim')
365 assert_fails('source Ximport.vim', 'E1012: Type mismatch; expected number but got string', '', 3)
366
367 var import_assign_const_lines =<< trim END
368 vim9script
369 import './Xexport.vim' as expo
370 expo.CONST = 4321
371 END
372 writefile(import_assign_const_lines, 'Ximport.vim')
373 assert_fails('source Ximport.vim', 'E46: Cannot change read-only variable "CONST"', '', 3)
374
375 delete('Ximport.vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000376 delete('Xexport.vim')
377
378 # Check that in a Vim9 script 'cpo' is set to the Vim default.
379 # Flags added or removed are also applied to the restored value.
380 set cpo=abcd
381 var lines =<< trim END
382 vim9script
383 g:cpo_in_vim9script = &cpo
384 set cpo+=f
385 set cpo-=c
386 g:cpo_after_vim9script = &cpo
387 END
388 writefile(lines, 'Xvim9_script')
389 source Xvim9_script
390 assert_equal('fabd', &cpo)
391 set cpo&vim
392 assert_equal(&cpo, g:cpo_in_vim9script)
393 var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
394 assert_equal(newcpo, g:cpo_after_vim9script)
395
396 delete('Xvim9_script')
397enddef
398
399def Test_import_funcref()
400 var lines =<< trim END
401 vim9script
402 export def F(): number
403 return 42
404 enddef
405 export const G = F
406 END
407 writefile(lines, 'Xlib.vim')
408
409 lines =<< trim END
410 vim9script
411 import './Xlib.vim' as lib
412 const Foo = lib.G()
413 assert_equal(42, Foo)
414
415 def DoTest()
416 const Goo = lib.G()
417 assert_equal(42, Goo)
418 enddef
419 DoTest()
420 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000421 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000422
423 delete('Xlib.vim')
424enddef
425
Bram Moolenaarc2f17f72022-02-21 13:13:50 +0000426def Test_import_duplicate_function()
427 # Function Hover() exists in both scripts, partial should refer to the right
428 # one.
429 var lines =<< trim END
430 vim9script
431
432 def Hover(d: dict<any>): string
433 return 'found it'
434 enddef
435
436 export def NewLspServer(): dict<any>
437 var d: dict<any> = {}
438 d->extend({hover: function('Hover', [d])})
439 return d
440 enddef
441
442 NewLspServer()
443 END
444 writefile(lines, 'Xserver.vim')
445
446 lines =<< trim END
447 vim9script
448
449 import './Xserver.vim' as server
450
451 export def Hover()
452 enddef
453
454 def AddServer()
455 var d: dict<any> = server.NewLspServer()
456 assert_equal('found it', d.hover())
457 enddef
458 AddServer()
459 END
460 v9.CheckScriptSuccess(lines)
461
462 delete('Xserver.vim')
463enddef
464
465
Bram Moolenaard8448622022-01-07 21:39:52 +0000466def Test_import_fails()
467 writefile([], 'Xfoo.vim')
468 var lines =<< trim END
469 import './Xfoo.vim' as foo
470 foo = 'bar'
471 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000472 v9.CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use foo itself'])
Bram Moolenaard8448622022-01-07 21:39:52 +0000473 lines =<< trim END
474 vim9script
475 import './Xfoo.vim' as foo
476 var that = foo
477 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000478 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaardd5893b2022-01-20 21:32:54 +0000479 lines =<< trim END
480 vim9script
481 import './Xfoo.vim' as foo
482 var that: any
483 that += foo
484 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000485 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaardd5893b2022-01-20 21:32:54 +0000486 lines =<< trim END
487 vim9script
488 import './Xfoo.vim' as foo
489 foo += 9
490 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000491 v9.CheckScriptFailure(lines, 'E1060: Expected dot after name: foo')
Bram Moolenaard8448622022-01-07 21:39:52 +0000492
493 lines =<< trim END
494 vim9script
495 import './Xfoo.vim' as 9foo
496 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000497 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000498 lines =<< trim END
499 vim9script
500 import './Xfoo.vim' as the#foo
501 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000502 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000503 lines =<< trim END
504 vim9script
505 import './Xfoo.vim' as g:foo
506 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000507 v9.CheckScriptFailure(lines, 'E1047:')
Bram Moolenaard8448622022-01-07 21:39:52 +0000508
509 delete('Xfoo.vim')
510
511 lines =<< trim END
512 vim9script
513 def TheFunc()
514 echo 'the func'
515 enddef
516 export var Ref = TheFunc
517 END
518 writefile([], 'Xthat.vim')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000519
Bram Moolenaard8448622022-01-07 21:39:52 +0000520 lines =<< trim END
521 import './Xthat.vim' as That
522 That()
523 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000524 v9.CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use That itself'])
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000525
526 lines =<< trim END
Bram Moolenaar937610b2022-01-19 17:21:29 +0000527 vim9script
528 import './Xthat.vim' as That
529 def Func()
530 echo That()
531 enddef
532 Func()
533 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000534 v9.CheckScriptFailure(lines, 'E1236: Cannot use That itself')
Bram Moolenaar937610b2022-01-19 17:21:29 +0000535
536 lines =<< trim END
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000537 import './Xthat.vim' as one
538 import './Xthat.vim' as two
539 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000540 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000541
542 delete('Xthat.vim')
Bram Moolenaar779aeff2022-02-08 19:12:19 +0000543
544 lines =<< trim END
545 vim9script
546 export var item = 'hello'
547 import './Xyourself.vim'
548 END
549 writefile(lines, 'Xyourself.vim')
550 assert_fails('source Xyourself.vim', 'E1088:')
551 delete('Xyourself.vim')
552
Bram Moolenaard8448622022-01-07 21:39:52 +0000553 mkdir('Ximport')
554
555 writefile(['vim9script'], 'Ximport/.vim')
556 lines =<< trim END
557 vim9script
558 import './Ximport/.vim'
559 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000560 v9.CheckScriptFailure(lines, 'E1261: Cannot import .vim without using "as"')
Bram Moolenaard8448622022-01-07 21:39:52 +0000561 lines =<< trim END
562 vim9script
563 import './Ximport/.vim' as vim
564 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000565 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000566
567 writefile(['vim9script'], 'Ximport/.vimrc')
568 lines =<< trim END
569 vim9script
570 import './Ximport/.vimrc'
571 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000572 v9.CheckScriptFailure(lines, 'E1257: Imported script must use "as" or end in .vim')
Bram Moolenaard8448622022-01-07 21:39:52 +0000573 lines =<< trim END
574 vim9script
575 import './Ximport/.vimrc' as vimrc
576 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000577 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +0000578
579 delete('Ximport', 'rf')
580enddef
581
582func g:Trigger()
583 source Ximport.vim
584 return "echo 'yes'\<CR>"
585endfunc
586
587def Test_import_export_expr_map()
588 # check that :import and :export work when buffer is locked
589 var export_lines =<< trim END
590 vim9script
591 export def That(): string
592 return 'yes'
593 enddef
594 END
595 writefile(export_lines, 'Xexport_that.vim')
596
597 var import_lines =<< trim END
598 vim9script
599 import './Xexport_that.vim' as that
600 assert_equal('yes', that.That())
601 END
602 writefile(import_lines, 'Ximport.vim')
603
604 nnoremap <expr> trigger g:Trigger()
605 feedkeys('trigger', "xt")
606
607 delete('Xexport_that.vim')
608 delete('Ximport.vim')
609 nunmap trigger
610enddef
611
612def Test_import_in_filetype()
613 # check that :import works when the buffer is locked
614 mkdir('ftplugin', 'p')
615 var export_lines =<< trim END
616 vim9script
617 export var That = 'yes'
618 END
619 writefile(export_lines, 'ftplugin/Xexport_ft.vim')
620
621 var import_lines =<< trim END
622 vim9script
623 import './Xexport_ft.vim' as ft
624 assert_equal('yes', ft.That)
625 g:did_load_mytpe = 1
626 END
627 writefile(import_lines, 'ftplugin/qf.vim')
628
629 var save_rtp = &rtp
630 &rtp = getcwd() .. ',' .. &rtp
631
632 filetype plugin on
633 copen
634 assert_equal(1, g:did_load_mytpe)
635
636 quit!
637 delete('Xexport_ft.vim')
638 delete('ftplugin', 'rf')
639 &rtp = save_rtp
640enddef
641
642def Test_use_import_in_mapping()
643 var lines =<< trim END
644 vim9script
Bram Moolenaar89445512022-04-14 12:58:23 +0100645 export def Funcx(nr: number)
646 g:result = nr
Bram Moolenaard8448622022-01-07 21:39:52 +0000647 enddef
648 END
649 writefile(lines, 'XsomeExport.vim')
650 lines =<< trim END
651 vim9script
652 import './XsomeExport.vim' as some
653 var Funcy = some.Funcx
Bram Moolenaar89445512022-04-14 12:58:23 +0100654 nnoremap <F3> :call <sid>Funcy(42)<cr>
655 nnoremap <F4> :call <sid>some.Funcx(44)<cr>
Bram Moolenaard8448622022-01-07 21:39:52 +0000656 END
657 writefile(lines, 'Xmapscript.vim')
658
659 source Xmapscript.vim
660 feedkeys("\<F3>", "xt")
661 assert_equal(42, g:result)
Bram Moolenaar89445512022-04-14 12:58:23 +0100662 feedkeys("\<F4>", "xt")
663 assert_equal(44, g:result)
Bram Moolenaard8448622022-01-07 21:39:52 +0000664
665 unlet g:result
666 delete('XsomeExport.vim')
667 delete('Xmapscript.vim')
668 nunmap <F3>
Bram Moolenaar89445512022-04-14 12:58:23 +0100669 nunmap <F4>
670enddef
671
Bram Moolenaar648dd882022-04-14 21:36:15 +0100672def Test_use_relative_autoload_import_in_mapping()
Bram Moolenaar89445512022-04-14 12:58:23 +0100673 var lines =<< trim END
674 vim9script
675 export def Func()
676 g:result = 42
677 enddef
678 END
Bram Moolenaar648dd882022-04-14 21:36:15 +0100679 writefile(lines, 'XrelautoloadExport.vim')
Bram Moolenaar89445512022-04-14 12:58:23 +0100680 lines =<< trim END
681 vim9script
Bram Moolenaar648dd882022-04-14 21:36:15 +0100682 import autoload './XrelautoloadExport.vim' as some
Bram Moolenaar89445512022-04-14 12:58:23 +0100683 nnoremap <F3> :call <SID>some.Func()<CR>
684 END
685 writefile(lines, 'Xmapscript.vim')
686
687 source Xmapscript.vim
Bram Moolenaar648dd882022-04-14 21:36:15 +0100688 assert_match('\d\+ A: .*XrelautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
Bram Moolenaar89445512022-04-14 12:58:23 +0100689 feedkeys("\<F3>", "xt")
690 assert_equal(42, g:result)
691
692 unlet g:result
Bram Moolenaar648dd882022-04-14 21:36:15 +0100693 delete('XrelautoloadExport.vim')
Bram Moolenaar89445512022-04-14 12:58:23 +0100694 delete('Xmapscript.vim')
695 nunmap <F3>
Bram Moolenaard8448622022-01-07 21:39:52 +0000696enddef
697
Bram Moolenaar648dd882022-04-14 21:36:15 +0100698def Test_use_autoload_import_in_mapping()
699 var lines =<< trim END
700 vim9script
701 export def Func()
702 g:result = 49
703 enddef
704 END
705 mkdir('Xdir/autoload', 'p')
706 writefile(lines, 'Xdir/autoload/XautoloadExport.vim')
707 var save_rtp = &rtp
708 exe 'set rtp^=' .. getcwd() .. '/Xdir'
709
710 lines =<< trim END
711 vim9script
712 import autoload 'XautoloadExport.vim' as some
713 nnoremap <F3> :call <SID>some.Func()<CR>
714 END
715 writefile(lines, 'Xmapscript.vim')
716
717 source Xmapscript.vim
718 assert_match('\d\+ A: .*autoload/XautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
719 feedkeys("\<F3>", "xt")
720 assert_equal(49, g:result)
721
722 unlet g:result
723 delete('Xmapscript.vim')
724 nunmap <F3>
725 delete('Xdir', 'rf')
726 &rtp = save_rtp
727enddef
728
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000729def Test_use_import_in_command_completion()
Bram Moolenaar15d16352022-01-17 20:09:08 +0000730 var lines =<< trim END
731 vim9script
732 export def Complete(..._): list<string>
733 return ['abcd']
734 enddef
735 END
736 writefile(lines, 'Xscript.vim')
737
738 lines =<< trim END
739 vim9script
740 import './Xscript.vim'
741
742 command -nargs=1 -complete=customlist,Xscript.Complete Cmd echo 'ok'
743 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
744 assert_equal('#Cmd abcd', @:)
745 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000746 v9.CheckScriptSuccess(lines)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000747
748 delcommand Cmd
749 delete('Xscript.vim')
750enddef
751
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100752def Test_use_import_with_funcref_in_command_completion()
753 var lines =<< trim END
754 vim9script
755 export def Complete(..._): list<string>
756 return ['abcd']
757 enddef
758 END
759 writefile(lines, 'Xscript.vim')
760
761 lines =<< trim END
762 vim9script
763 import './Xscript.vim'
764
765 var Ref = Xscript.Complete
766 exe "command -nargs=1 -complete=customlist," .. expand('<SID>') .. "Ref Cmd echo 'ok'"
767 feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt')
768 assert_equal('#Cmd abcd', @:)
769 END
770 v9.CheckScriptSuccess(lines)
771
772 delcommand Cmd
773 delete('Xscript.vim')
774enddef
775
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000776def Test_use_autoload_import_in_insert_completion()
777 mkdir('Xdir/autoload', 'p')
778 var save_rtp = &rtp
779 exe 'set rtp^=' .. getcwd() .. '/Xdir'
780
781 var lines =<< trim END
782 vim9script
783 export def ThesaurusFunc(findbase: bool, _): any
784 if findbase
785 return 1
786 endif
787 return [
788 'check',
789 'experiment',
790 'test',
791 'verification'
792 ]
793 enddef
794 g:completion_loaded = 'yes'
795 END
796 writefile(lines, 'Xdir/autoload/completion.vim')
797
798 new
799 lines =<< trim END
800 vim9script
801 g:completion_loaded = 'no'
802 import autoload 'completion.vim'
803 set thesaurusfunc=completion.ThesaurusFunc
804 assert_equal('no', g:completion_loaded)
805 feedkeys("i\<C-X>\<C-T>\<C-N>\<Esc>", 'xt')
806 assert_equal('experiment', getline(1))
807 assert_equal('yes', g:completion_loaded)
808 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000809 v9.CheckScriptSuccess(lines)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +0000810
811 set thesaurusfunc=
812 bwipe!
813 delete('Xdir', 'rf')
814 &rtp = save_rtp
815enddef
816
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000817def Test_use_autoload_import_partial_in_opfunc()
818 mkdir('Xdir/autoload', 'p')
819 var save_rtp = &rtp
820 exe 'set rtp^=' .. getcwd() .. '/Xdir'
821
822 var lines =<< trim END
823 vim9script
824 export def Opfunc(..._)
825 g:opfunc_called = 'yes'
826 enddef
827 END
828 writefile(lines, 'Xdir/autoload/opfunc.vim')
829
830 new
831 lines =<< trim END
832 vim9script
833 import autoload 'opfunc.vim'
834 nnoremap <expr> <F3> TheFunc()
835 def TheFunc(): string
836 &operatorfunc = function('opfunc.Opfunc', [0])
837 return 'g@'
838 enddef
839 feedkeys("\<F3>l", 'xt')
840 assert_equal('yes', g:opfunc_called)
841 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000842 v9.CheckScriptSuccess(lines)
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000843
844 set opfunc=
845 bwipe!
846 delete('Xdir', 'rf')
Bram Moolenaar06b77222022-01-25 15:51:56 +0000847 nunmap <F3>
848 &rtp = save_rtp
849enddef
850
851def Test_set_opfunc_to_autoload_func_directly()
852 mkdir('Xdir/autoload', 'p')
853 var save_rtp = &rtp
854 exe 'set rtp^=' .. getcwd() .. '/Xdir'
855
856 var lines =<< trim END
857 vim9script
858 export def Opfunc(..._)
859 g:opfunc_called = 'yes'
860 enddef
861 END
862 writefile(lines, 'Xdir/autoload/opfunc.vim')
863
864 new
865 lines =<< trim END
866 vim9script
867 import autoload 'opfunc.vim'
868 nnoremap <expr> <F3> TheFunc()
869 def TheFunc(): string
870 &operatorfunc = opfunc.Opfunc
871 return 'g@'
872 enddef
873 feedkeys("\<F3>l", 'xt')
874 assert_equal('yes', g:opfunc_called)
875 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000876 v9.CheckScriptSuccess(lines)
Bram Moolenaar06b77222022-01-25 15:51:56 +0000877
878 set opfunc=
879 bwipe!
880 delete('Xdir', 'rf')
881 nunmap <F3>
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +0000882 &rtp = save_rtp
883enddef
884
Bram Moolenaare70dd112022-01-21 16:31:11 +0000885def Test_use_autoload_import_in_fold_expression()
886 mkdir('Xdir/autoload', 'p')
887 var save_rtp = &rtp
888 exe 'set rtp^=' .. getcwd() .. '/Xdir'
889
890 var lines =<< trim END
891 vim9script
892 export def Expr(): string
893 return getline(v:lnum) =~ '^#' ? '>1' : '1'
894 enddef
Bram Moolenaar9530b582022-01-22 13:39:08 +0000895 export def Text(): string
896 return 'fold text'
897 enddef
Bram Moolenaare70dd112022-01-21 16:31:11 +0000898 g:fold_loaded = 'yes'
899 END
900 writefile(lines, 'Xdir/autoload/fold.vim')
901
902 lines =<< trim END
903 vim9script
904 import autoload 'fold.vim'
905 &foldexpr = 'fold.Expr()'
Bram Moolenaar9530b582022-01-22 13:39:08 +0000906 &foldtext = 'fold.Text()'
Bram Moolenaare70dd112022-01-21 16:31:11 +0000907 &foldmethod = 'expr'
908 &debug = 'throw'
909 END
910 new
911 setline(1, ['# one', 'text', '# two', 'text'])
912 g:fold_loaded = 'no'
Bram Moolenaar62aec932022-01-29 21:45:34 +0000913 v9.CheckScriptSuccess(lines)
Bram Moolenaare70dd112022-01-21 16:31:11 +0000914 assert_equal('no', g:fold_loaded)
915 redraw
916 assert_equal('yes', g:fold_loaded)
917
918 # Check that script context of 'foldexpr' is copied to another buffer.
919 edit! otherfile
920 redraw
921
Bram Moolenaar9530b582022-01-22 13:39:08 +0000922 set foldexpr= foldtext& foldmethod& debug=
Bram Moolenaare70dd112022-01-21 16:31:11 +0000923 bwipe!
924 delete('Xdir', 'rf')
925 &rtp = save_rtp
926enddef
927
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100928def Test_autoload_import_relative()
929 var lines =<< trim END
930 vim9script
931
932 g:loaded = 'yes'
933 export def RelFunc(): string
934 return 'relfunc'
935 enddef
936 def NotExported()
937 echo 'not'
938 enddef
939
940 export var someText = 'some text'
941 var notexp = 'bad'
942 END
943 writefile(lines, 'XimportRel.vim')
944 writefile(lines, 'XimportRel2.vim')
945 writefile(lines, 'XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +0100946 writefile(lines, 'XimportRel4.vim')
947 writefile(lines, 'XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100948
949 lines =<< trim END
950 vim9script
951 g:loaded = 'no'
952 import autoload './XimportRel.vim'
953 assert_equal('no', g:loaded)
954
955 def AFunc(): string
956 var res = ''
957 res ..= XimportRel.RelFunc()
958 res ..= '/'
959 res ..= XimportRel.someText
960 XimportRel.someText = 'from AFunc'
961 return res
962 enddef
963 # script not loaded when compiling
964 defcompile
965 assert_equal('no', g:loaded)
966
967 assert_equal('relfunc/some text', AFunc())
968 assert_equal('yes', g:loaded)
969 unlet g:loaded
970
971 assert_equal('from AFunc', XimportRel.someText)
972 XimportRel.someText = 'from script'
973 assert_equal('from script', XimportRel.someText)
974 END
975 v9.CheckScriptSuccess(lines)
976
977 lines =<< trim END
978 vim9script
979 import autoload './XimportRel.vim'
980 echo XimportRel.NotExported()
981 END
982 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 3)
983
984 lines =<< trim END
985 vim9script
986 import autoload './XimportRel.vim'
987 echo XimportRel.notexp
988 END
989 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
990
991 lines =<< trim END
992 vim9script
993 import autoload './XimportRel.vim'
994 XimportRel.notexp = 'bad'
995 END
996 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 3)
997
998 lines =<< trim END
999 vim9script
1000 import autoload './XimportRel.vim'
1001 def Func()
1002 echo XimportRel.NotExported()
1003 enddef
1004 Func()
1005 END
1006 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExported', 1)
1007
1008 lines =<< trim END
1009 vim9script
1010 import autoload './XimportRel.vim'
1011 def Func()
1012 echo XimportRel.notexp
1013 enddef
1014 Func()
1015 END
1016 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1017
Bram Moolenaar10611952022-04-03 21:11:34 +01001018 # Same, script not imported before
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001019 lines =<< trim END
1020 vim9script
Bram Moolenaar10611952022-04-03 21:11:34 +01001021 import autoload './XimportRel4.vim'
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001022 def Func()
Bram Moolenaar10611952022-04-03 21:11:34 +01001023 echo XimportRel4.notexp
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001024 enddef
1025 Func()
1026 END
1027 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1028
Bram Moolenaar10611952022-04-03 21:11:34 +01001029 # does not fail if the script wasn't loaded yet and only compiling
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001030 g:loaded = 'no'
1031 lines =<< trim END
1032 vim9script
1033 import autoload './XimportRel2.vim'
1034 def Func()
1035 echo XimportRel2.notexp
1036 enddef
1037 defcompile
1038 END
1039 v9.CheckScriptSuccess(lines)
1040 assert_equal('no', g:loaded)
1041
Bram Moolenaar10611952022-04-03 21:11:34 +01001042 lines =<< trim END
1043 vim9script
1044 import autoload './XimportRel.vim'
1045 def Func()
1046 XimportRel.notexp = 'bad'
1047 enddef
1048 Func()
1049 END
1050 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1051
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001052 # fails with a not loaded import
1053 lines =<< trim END
1054 vim9script
1055 import autoload './XimportRel3.vim'
1056 def Func()
1057 XimportRel3.notexp = 'bad'
1058 enddef
1059 Func()
1060 END
1061 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notexp', 1)
1062 assert_equal('yes', g:loaded)
1063 unlet g:loaded
1064
Bram Moolenaar10611952022-04-03 21:11:34 +01001065 lines =<< trim END
1066 vim9script
1067 import autoload './XimportRel5.vim'
1068 def Func()
1069 XimportRel5.nosuchvar = 'bad'
1070 enddef
1071 Func()
1072 END
1073 v9.CheckScriptFailure(lines, 'E121: Undefined variable: nosuchvar', 1)
1074 unlet g:loaded
1075
1076 # nasty: delete script after compiling function
1077 writefile(['vim9script'], 'XimportRelDel.vim')
1078 lines =<< trim END
1079 vim9script
1080
1081 import autoload './XimportRelDel.vim'
1082 def DoIt()
1083 echo XimportRelDel.var
1084 enddef
1085 defcompile
1086 delete('XimportRelDel.vim')
1087 DoIt()
1088 END
Bram Moolenaar242c1522022-04-03 21:52:51 +01001089 v9.CheckScriptFailure(lines, 'E484:')
Bram Moolenaar10611952022-04-03 21:11:34 +01001090
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001091 delete('XimportRel.vim')
1092 delete('XimportRel2.vim')
1093 delete('XimportRel3.vim')
Bram Moolenaar10611952022-04-03 21:11:34 +01001094 delete('XimportRel4.vim')
1095 delete('XimportRel5.vim')
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001096enddef
1097
Bram Moolenaarccbfd482022-03-31 16:18:23 +01001098def Test_autoload_import_relative_autoload_dir()
1099 mkdir('autoload', 'p')
1100 var lines =<< trim END
1101 vim9script
1102 export def Bar()
1103 g:called_bar = 'yes'
1104 enddef
1105 END
1106 writefile(lines, 'autoload/script.vim')
1107
1108 lines =<< trim END
1109 vim9script
1110 import autoload './autoload/script.vim'
1111 def Foo()
1112 script.Bar()
1113 enddef
1114 Foo()
1115 assert_equal('yes', g:called_bar)
1116 END
1117 v9.CheckScriptSuccess(lines)
1118
1119 unlet g:called_bar
1120 delete('autoload', 'rf')
1121enddef
1122
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001123func Test_import_in_diffexpr()
1124 CheckExecutable diff
1125
1126 call Run_Test_import_in_diffexpr()
1127endfunc
1128
1129def Run_Test_import_in_diffexpr()
1130 var lines =<< trim END
1131 vim9script
1132
1133 export def DiffExpr()
1134 # Prepend some text to check diff type detection
1135 writefile(['warning', ' message'], v:fname_out)
1136 silent exe '!diff ' .. v:fname_in .. ' '
1137 .. v:fname_new .. '>>' .. v:fname_out
1138 enddef
1139 END
1140 writefile(lines, 'Xdiffexpr')
1141
1142 lines =<< trim END
1143 vim9script
1144 import './Xdiffexpr' as diff
1145
1146 set diffexpr=diff.DiffExpr()
1147 set diffopt=foldcolumn:0
1148 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001149 v9.CheckScriptSuccess(lines)
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001150
1151 enew!
1152 call setline(1, ['one', 'two', 'three'])
1153 diffthis
1154
1155 botright vert new
1156 call setline(1, ['one', 'two', 'three.'])
1157 diffthis
1158 # we only check if this does not cause errors
1159 redraw
1160
1161 diffoff!
1162 bwipe!
1163 bwipe!
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001164 delete('Xdiffexpr')
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +00001165enddef
1166
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001167def Test_import_in_patchexpr()
1168 var lines =<< trim END
1169 vim9script
1170 export def TPatch()
1171 call writefile(['output file'], v:fname_out)
1172 enddef
1173 END
1174 writefile(lines, 'Xpatchexpr')
1175
1176 lines =<< trim END
1177 vim9script
1178 import './Xpatchexpr' as patch
1179 set patchexpr=patch.TPatch()
1180 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001181 v9.CheckScriptSuccess(lines)
Bram Moolenaar36c2add2022-01-22 20:55:30 +00001182
1183 call writefile(['input file'], 'Xinput')
1184 call writefile(['diff file'], 'Xdiff')
1185 :%bwipe!
1186 edit Xinput
1187 diffpatch Xdiff
1188 call assert_equal('output file', getline(1))
1189
1190 call delete('Xinput')
1191 call delete('Xdiff')
1192 call delete('Xpatchexpr')
1193 set patchexpr&
1194 :%bwipe!
1195enddef
1196
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001197def Test_import_in_formatexpr()
1198 var lines =<< trim END
1199 vim9script
1200 export def MyFormatExpr(): number
1201 g:did_format = 'yes'
1202 return 0
1203 enddef
1204 END
1205 writefile(lines, 'Xformatter')
1206
1207 lines =<< trim END
1208 vim9script
1209 import './Xformatter' as format
1210 set formatexpr=format.MyFormatExpr()
1211 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001212 v9.CheckScriptSuccess(lines)
Bram Moolenaar3ba685e2022-01-22 19:17:31 +00001213
1214 new
1215 setline(1, ['a', 'b', 'c'])
1216 normal gqG
1217 assert_equal('yes', g:did_format)
1218
1219 bwipe!
1220 delete('Xformatter')
1221 unlet g:did_format
1222 set formatexpr=
1223enddef
1224
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001225def Test_import_in_includeexpr()
1226 writefile(['found it'], 'Xthisfile')
1227 new
1228
1229 var lines =<< trim END
1230 vim9script
1231 export def DoSub(): string
1232 return substitute(v:fname, 'that', 'this', '')
1233 enddef
1234 END
1235 writefile(lines, 'Xinclude.vim')
1236
1237 lines =<< trim END
1238 vim9script
1239 import './Xinclude.vim'
1240 set includeexpr=Xinclude.DoSub()
1241 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001242 v9.CheckScriptSuccess(lines)
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001243
1244 setline(1, ['Xthatfile'])
1245 exe "normal \<C-W>f"
1246 assert_equal('Xthisfile', expand('%'))
1247
1248 bwipe!
1249 bwipe!
1250 set includeexpr=
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00001251 delete('Xinclude.vim')
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001252 delete('Xthisfile')
1253enddef
1254
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001255def Test_import_in_indentexpr()
1256 var lines =<< trim END
1257 vim9script
1258 export def GetIndent(): number
1259 return 5
1260 enddef
1261 END
1262 writefile(lines, 'Xindenter')
1263
1264 lines =<< trim END
1265 vim9script
1266 import './Xindenter' as indent
1267 set indentexpr=indent.GetIndent()
1268 set debug=throw
1269 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001270 v9.CheckScriptSuccess(lines)
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001271
1272 new
1273 setline(1, 'hello')
1274 normal ==
1275 assert_equal(' hello', getline(1))
1276
1277 bwipe!
1278 set indentexpr= debug=
1279 delete('Xindenter')
1280enddef
1281
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001282func Test_import_in_printexpr()
1283 CheckFeature postscript
1284 call Run_Test_import_in_printexpr()
1285endfunc
1286
1287def Run_Test_import_in_printexpr()
1288 var lines =<< trim END
1289 vim9script
1290 export def PrintFile(): bool
1291 g:printed = 'yes'
1292 delete('v:fname_in')
1293 return false
1294 enddef
1295 END
1296 writefile(lines, 'Xprint.vim')
1297
1298 lines =<< trim END
1299 vim9script
1300 import './Xprint.vim'
1301 set printexpr=Xprint.PrintFile()
1302 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001303 v9.CheckScriptSuccess(lines)
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +00001304
1305 help
1306 hardcopy dummy args
1307 assert_equal('yes', g:printed)
1308
1309 delete('Xprint.vim')
1310 set printexpr=
1311enddef
1312
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001313def Test_import_in_charconvert()
1314 var lines =<< trim END
1315 vim9script
1316 export def MakeUpper(): bool
1317 var data = readfile(v:fname_in)
1318 map(data, 'toupper(v:val)')
1319 writefile(data, v:fname_out)
1320 return false # success
1321 enddef
1322 END
1323 writefile(lines, 'Xconvert.vim')
1324
1325 lines =<< trim END
1326 vim9script
1327 import './Xconvert.vim' as conv
1328 set charconvert=conv.MakeUpper()
1329 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001330 v9.CheckScriptSuccess(lines)
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00001331
1332 writefile(['one', 'two'], 'Xfile')
1333 new Xfile
1334 write ++enc=ucase Xfile1
1335 assert_equal(['ONE', 'TWO'], readfile('Xfile1'))
1336
1337 delete('Xfile')
1338 delete('Xfile1')
1339 delete('Xconvert.vim')
1340 bwipe!
1341 set charconvert&
1342enddef
1343
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001344func Test_import_in_spellsuggest_expr()
1345 CheckFeature spell
1346 call Run_Test_import_in_spellsuggest_expr()
1347endfunc
1348
1349def Run_Test_import_in_spellsuggest_expr()
1350 var lines =<< trim END
1351 vim9script
1352 export def MySuggest(): list<any>
1353 return [['Fox', 8], ['Fop', 9]]
1354 enddef
1355 END
1356 writefile(lines, 'Xsuggest.vim')
1357
1358 lines =<< trim END
1359 vim9script
1360 import './Xsuggest.vim' as sugg
1361 set spell spellsuggest=expr:sugg.MySuggest()
1362 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001363 v9.CheckScriptSuccess(lines)
Bram Moolenaar2a7aa832022-01-23 17:59:06 +00001364
1365 set verbose=1 # report errors
1366 call assert_equal(['Fox', 'Fop'], spellsuggest('Fo', 2))
1367
1368 delete('Xsuggest.vim')
1369 set nospell spellsuggest& verbose=0
1370enddef
1371
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001372def Test_export_shadows_global_function()
1373 mkdir('Xdir/autoload', 'p')
1374 var save_rtp = &rtp
1375 exe 'set rtp^=' .. getcwd() .. '/Xdir'
1376
1377 var lines =<< trim END
1378 vim9script
1379 export def Shadow(): string
1380 return 'Shadow()'
1381 enddef
1382 END
1383 writefile(lines, 'Xdir/autoload/shadow.vim')
1384
1385 lines =<< trim END
1386 vim9script
1387
1388 def g:Shadow(): string
1389 return 'global'
1390 enddef
1391
1392 import autoload 'shadow.vim'
1393 assert_equal('Shadow()', shadow.Shadow())
1394 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001395 v9.CheckScriptSuccess(lines)
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001396
1397 delfunc g:Shadow
1398 bwipe!
1399 delete('Xdir', 'rf')
1400 &rtp = save_rtp
1401enddef
1402
Bram Moolenaard8448622022-01-07 21:39:52 +00001403def Test_export_fails()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001404 v9.CheckScriptFailure(['export var some = 123'], 'E1042:')
1405 v9.CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
1406 v9.CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001407
1408 assert_fails('export something', 'E1043:')
1409enddef
1410
1411func Test_import_fails_without_script()
1412 CheckRunVimInTerminal
1413
1414 " call indirectly to avoid compilation error for missing functions
1415 call Run_Test_import_fails_on_command_line()
1416endfunc
1417
1418def Run_Test_import_fails_on_command_line()
1419 var export =<< trim END
1420 vim9script
1421 export def Foo(): number
1422 return 0
1423 enddef
1424 END
1425 writefile(export, 'XexportCmd.vim')
1426
Bram Moolenaar62aec932022-01-29 21:45:34 +00001427 var buf = g:RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
Bram Moolenaard8448622022-01-07 21:39:52 +00001428 rows: 6, wait_for_ruler: 0})
Bram Moolenaar62aec932022-01-29 21:45:34 +00001429 g:WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5)))
Bram Moolenaard8448622022-01-07 21:39:52 +00001430
1431 delete('XexportCmd.vim')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001432 g:StopVimInTerminal(buf)
Bram Moolenaard8448622022-01-07 21:39:52 +00001433enddef
1434
1435def Test_vim9_reload_noclear()
1436 var lines =<< trim END
1437 vim9script
1438 export var exported = 'thexport'
1439
1440 export def TheFunc(x = 0)
1441 enddef
1442 END
1443 writefile(lines, 'XExportReload')
1444 lines =<< trim END
1445 vim9script noclear
1446 g:loadCount += 1
Bram Moolenaara749a422022-02-12 19:52:25 +00001447 var reloaded = 'init'
Bram Moolenaard8448622022-01-07 21:39:52 +00001448 import './XExportReload' as exp
1449
1450 def Again(): string
1451 return 'again'
1452 enddef
1453
1454 exp.TheFunc()
1455
Bram Moolenaara749a422022-02-12 19:52:25 +00001456 if exists('loaded') | finish | endif
1457 var loaded = true
Bram Moolenaard8448622022-01-07 21:39:52 +00001458
Bram Moolenaara749a422022-02-12 19:52:25 +00001459 var notReloaded = 'yes'
1460 reloaded = 'first'
Bram Moolenaard8448622022-01-07 21:39:52 +00001461 def g:Values(): list<string>
Bram Moolenaara749a422022-02-12 19:52:25 +00001462 return [reloaded, notReloaded, Again(), Once(), exp.exported]
Bram Moolenaard8448622022-01-07 21:39:52 +00001463 enddef
1464
1465 def Once(): string
1466 return 'once'
1467 enddef
1468 END
1469 writefile(lines, 'XReloaded')
1470 g:loadCount = 0
1471 source XReloaded
1472 assert_equal(1, g:loadCount)
1473 assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values())
1474 source XReloaded
1475 assert_equal(2, g:loadCount)
1476 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1477 source XReloaded
1478 assert_equal(3, g:loadCount)
1479 assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values())
1480
1481 delete('XReloaded')
1482 delete('XExportReload')
1483 delfunc g:Values
1484 unlet g:loadCount
1485
1486 lines =<< trim END
1487 vim9script
1488 def Inner()
1489 enddef
1490 END
1491 lines->writefile('XreloadScript.vim')
1492 source XreloadScript.vim
1493
1494 lines =<< trim END
1495 vim9script
1496 def Outer()
1497 def Inner()
1498 enddef
1499 enddef
1500 defcompile
1501 END
1502 lines->writefile('XreloadScript.vim')
1503 source XreloadScript.vim
1504
1505 delete('XreloadScript.vim')
1506enddef
1507
Bram Moolenaarcd1cda22022-02-16 21:48:25 +00001508def Test_vim_reload_noclear_arg_count()
1509 var lines =<< trim END
1510 vim9script noclear
1511
1512 if !exists('g:didload')
1513 def Test(a: string, b: string)
1514 echo a b
1515 enddef
1516 def Call()
1517 Test('a', 'b')
1518 enddef
1519 else
1520 # redefine with one argument less
1521 def Test(a: string)
1522 echo a
1523 enddef
1524 endif
1525 Call()
1526 g:didload = 1
1527 END
1528 lines->writefile('XreloadScript_1.vim')
1529 source XreloadScript_1.vim
1530 assert_fails('source XreloadScript_1.vim', 'E1106: One argument too many')
1531 unlet g:didload
1532
1533 lines =<< trim END
1534 vim9script noclear
1535
1536 if !exists('g:didload')
1537 def Test(a: string, b: string, c: string)
1538 echo a b
1539 enddef
1540 def Call()
1541 Test('a', 'b', 'c')
1542 enddef
1543 else
1544 # redefine with one argument less
1545 def Test(a: string)
1546 echo a
1547 enddef
1548 endif
1549 Call()
1550 g:didload = 1
1551 END
1552 lines->writefile('XreloadScript_2.vim')
1553 source XreloadScript_2.vim
1554 assert_fails('source XreloadScript_2.vim', 'E1106: 2 arguments too many')
1555 unlet g:didload
1556
1557 lines =<< trim END
1558 vim9script noclear
1559
1560 if !exists('g:didload')
1561 def Test(a: string)
1562 echo a
1563 enddef
1564 def Call()
1565 Test('a')
1566 enddef
1567 else
1568 # redefine with one argument extra
1569 def Test(a: string, b: string)
1570 echo a b
1571 enddef
1572 endif
1573 Call()
1574 g:didload = 1
1575 END
1576 lines->writefile('XreloadScript_3.vim')
1577 source XreloadScript_3.vim
1578 assert_fails('source XreloadScript_3.vim', 'E1190: One argument too few')
1579 unlet g:didload
1580
1581 lines =<< trim END
1582 vim9script noclear
1583
1584 if !exists('g:didload')
1585 def Test(a: string)
1586 echo a
1587 enddef
1588 def Call()
1589 Test('a')
1590 enddef
1591 else
1592 # redefine with two arguments extra
1593 def Test(a: string, b: string, c: string)
1594 echo a b
1595 enddef
1596 endif
1597 Call()
1598 g:didload = 1
1599 END
1600 lines->writefile('XreloadScript_4.vim')
1601 source XreloadScript_4.vim
1602 assert_fails('source XreloadScript_4.vim', 'E1190: 2 arguments too few')
1603 unlet g:didload
1604
1605 delete('XreloadScript_1.vim')
1606 delete('XreloadScript_2.vim')
1607 delete('XreloadScript_3.vim')
1608 delete('XreloadScript_4.vim')
1609enddef
1610
1611def Test_vim9_reload_noclear_error()
1612 var lines =<< trim END
1613 vim9script noclear
1614
1615 if !exists('g:didload')
1616 def Test(a: string)
1617 echo a
1618 enddef
1619 def Call()
1620 Test('a')
1621 enddef
1622 else
1623 # redefine with a compile error
1624 def Test(a: string)
1625 echo ax
1626 enddef
1627 endif
1628 Call()
1629 g:didload = 1
1630 END
1631 lines->writefile('XreloadScriptErr.vim')
1632 source XreloadScriptErr.vim
1633 assert_fails('source XreloadScriptErr.vim', 'E1001: Variable not found: ax')
1634
1635 unlet g:didload
1636 delete('XreloadScriptErr.vim')
1637enddef
1638
Bram Moolenaard8448622022-01-07 21:39:52 +00001639def Test_vim9_reload_import()
1640 var lines =<< trim END
1641 vim9script
1642 const var = ''
1643 var valone = 1234
1644 def MyFunc(arg: string)
1645 valone = 5678
1646 enddef
1647 END
1648 var morelines =<< trim END
1649 var valtwo = 222
1650 export def GetValtwo(): number
1651 return valtwo
1652 enddef
1653 END
1654 writefile(lines + morelines, 'Xreload.vim')
1655 source Xreload.vim
1656 source Xreload.vim
1657 source Xreload.vim
1658
1659 # cannot declare a var twice
1660 lines =<< trim END
1661 vim9script
1662 var valone = 1234
1663 var valone = 5678
1664 END
1665 writefile(lines, 'Xreload.vim')
1666 assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
1667
1668 delete('Xreload.vim')
1669 delete('Ximport.vim')
1670enddef
1671
1672" if a script is reloaded with a script-local variable that changed its type, a
1673" compiled function using that variable must fail.
1674def Test_script_reload_change_type()
1675 var lines =<< trim END
1676 vim9script noclear
1677 var str = 'string'
1678 def g:GetStr(): string
1679 return str .. 'xxx'
1680 enddef
1681 END
1682 writefile(lines, 'Xreload.vim')
1683 source Xreload.vim
1684 echo g:GetStr()
1685
1686 lines =<< trim END
1687 vim9script noclear
1688 var str = 1234
1689 END
1690 writefile(lines, 'Xreload.vim')
1691 source Xreload.vim
1692 assert_fails('echo g:GetStr()', 'E1150:')
1693
1694 delfunc g:GetStr
1695 delete('Xreload.vim')
1696enddef
1697
1698" Define CallFunc so that the test can be compiled
1699command CallFunc echo 'nop'
1700
1701def Test_script_reload_from_function()
1702 var lines =<< trim END
1703 vim9script
1704
Bram Moolenaar10611952022-04-03 21:11:34 +01001705 if exists('g:loadedThis')
Bram Moolenaard8448622022-01-07 21:39:52 +00001706 finish
1707 endif
Bram Moolenaar10611952022-04-03 21:11:34 +01001708 g:loadedThis = 1
Bram Moolenaard8448622022-01-07 21:39:52 +00001709 delcommand CallFunc
1710 command CallFunc Func()
1711 def Func()
1712 so XreloadFunc.vim
1713 g:didTheFunc = 1
1714 enddef
1715 END
1716 writefile(lines, 'XreloadFunc.vim')
1717 source XreloadFunc.vim
1718 CallFunc
1719 assert_equal(1, g:didTheFunc)
1720
1721 delete('XreloadFunc.vim')
1722 delcommand CallFunc
Bram Moolenaar10611952022-04-03 21:11:34 +01001723 unlet g:loadedThis
Bram Moolenaard8448622022-01-07 21:39:52 +00001724 unlet g:didTheFunc
1725enddef
1726
1727def s:RetSome(): string
1728 return 'some'
1729enddef
1730
1731" Not exported function that is referenced needs to be accessed by the
1732" script-local name.
1733def Test_vim9_funcref()
1734 var sortlines =<< trim END
1735 vim9script
1736 def Compare(i1: number, i2: number): number
1737 return i2 - i1
1738 enddef
1739
1740 export def FastSort(): list<number>
1741 return range(5)->sort(Compare)
1742 enddef
1743
1744 export def GetString(arg: string): string
1745 return arg
1746 enddef
1747 END
1748 writefile(sortlines, 'Xsort.vim')
1749
1750 var lines =<< trim END
1751 vim9script
1752 import './Xsort.vim'
1753 def Test()
1754 g:result = Xsort.FastSort()
1755 enddef
1756 Test()
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001757 END
1758 writefile(lines, 'Xscript.vim')
1759 source Xscript.vim
1760 assert_equal([4, 3, 2, 1, 0], g:result)
1761 unlet g:result
Bram Moolenaard8448622022-01-07 21:39:52 +00001762
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +00001763 lines =<< trim END
1764 vim9script
Bram Moolenaard8448622022-01-07 21:39:52 +00001765 # using a function imported with "as"
1766 import './Xsort.vim' as anAlias
1767 assert_equal('yes', anAlias.GetString('yes'))
1768
1769 # using the function from a compiled function
1770 def TestMore(): string
1771 var s = s:anAlias.GetString('foo')
1772 return s .. anAlias.GetString('bar')
1773 enddef
1774 assert_equal('foobar', TestMore())
1775
1776 # error when using a function that isn't exported
1777 assert_fails('anAlias.Compare(1, 2)', 'E1049:')
1778 END
1779 writefile(lines, 'Xscript.vim')
1780
Bram Moolenaard8448622022-01-07 21:39:52 +00001781 delete('Xsort.vim')
1782 delete('Xscript.vim')
1783
1784 var Funcref = function('s:RetSome')
1785 assert_equal('some', Funcref())
1786enddef
1787
1788" Check that when searching for "FilterFunc" it finds the import in the
1789" script where FastFilter() is called from, both as a string and as a direct
1790" function reference.
1791def Test_vim9_funcref_other_script()
1792 var filterLines =<< trim END
1793 vim9script
1794 export def FilterFunc(idx: number, val: number): bool
1795 return idx % 2 == 1
1796 enddef
1797 export def FastFilter(): list<number>
1798 return range(10)->filter('FilterFunc(v:key, v:val)')
1799 enddef
1800 export def FastFilterDirect(): list<number>
1801 return range(10)->filter(FilterFunc)
1802 enddef
1803 END
1804 writefile(filterLines, 'Xfilter.vim')
1805
1806 var lines =<< trim END
1807 vim9script
1808 import './Xfilter.vim' as filter
1809 def Test()
1810 var x: list<number> = filter.FastFilter()
1811 enddef
1812 Test()
1813 def TestDirect()
1814 var x: list<number> = filter.FastFilterDirect()
1815 enddef
1816 TestDirect()
1817 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001818 v9.CheckScriptSuccess(lines)
Bram Moolenaard8448622022-01-07 21:39:52 +00001819 delete('Xfilter.vim')
1820enddef
1821
1822def Test_import_absolute()
1823 var import_lines = [
1824 'vim9script',
1825 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs',
1826 'def UseExported()',
1827 ' g:imported_abs = abs.exported',
1828 ' abs.exported = 8888',
1829 ' g:imported_after = abs.exported',
1830 'enddef',
1831 'UseExported()',
1832 'g:import_disassembled = execute("disass UseExported")',
1833 ]
1834 writefile(import_lines, 'Ximport_abs.vim')
1835 writefile(s:export_script_lines, 'Xexport_abs.vim')
1836
1837 source Ximport_abs.vim
1838
1839 assert_equal(9876, g:imported_abs)
1840 assert_equal(8888, g:imported_after)
1841 assert_match('<SNR>\d\+_UseExported\_s*' ..
1842 'g:imported_abs = abs.exported\_s*' ..
1843 '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1844 '1 STOREG g:imported_abs\_s*' ..
1845 'abs.exported = 8888\_s*' ..
1846 '2 PUSHNR 8888\_s*' ..
1847 '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' ..
1848 'g:imported_after = abs.exported\_s*' ..
1849 '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' ..
1850 '5 STOREG g:imported_after',
1851 g:import_disassembled)
1852
1853 Undo_export_script_lines()
1854 unlet g:imported_abs
1855 unlet g:import_disassembled
1856
1857 delete('Ximport_abs.vim')
1858 delete('Xexport_abs.vim')
1859enddef
1860
1861def Test_import_rtp()
1862 var import_lines = [
1863 'vim9script',
1864 'import "Xexport_rtp.vim" as rtp',
1865 'g:imported_rtp = rtp.exported',
1866 ]
1867 writefile(import_lines, 'Ximport_rtp.vim')
1868 mkdir('import', 'p')
1869 writefile(s:export_script_lines, 'import/Xexport_rtp.vim')
1870
1871 var save_rtp = &rtp
1872 &rtp = getcwd()
1873 source Ximport_rtp.vim
1874 &rtp = save_rtp
1875
1876 assert_equal(9876, g:imported_rtp)
1877
1878 Undo_export_script_lines()
1879 unlet g:imported_rtp
1880 delete('Ximport_rtp.vim')
1881 delete('import', 'rf')
1882enddef
1883
1884def Test_import_compile_error()
1885 var export_lines = [
1886 'vim9script',
1887 'export def ExpFunc(): string',
1888 ' return notDefined',
1889 'enddef',
1890 ]
1891 writefile(export_lines, 'Xexported.vim')
1892
1893 var import_lines = [
1894 'vim9script',
1895 'import "./Xexported.vim" as expo',
1896 'def ImpFunc()',
1897 ' echo expo.ExpFunc()',
1898 'enddef',
1899 'defcompile',
1900 ]
1901 writefile(import_lines, 'Ximport.vim')
1902
1903 try
1904 source Ximport.vim
1905 catch /E1001/
1906 # Error should be before the Xexported.vim file.
1907 assert_match('E1001: Variable not found: notDefined', v:exception)
1908 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint)
1909 endtry
1910
1911 delete('Xexported.vim')
1912 delete('Ximport.vim')
1913enddef
1914
1915def Test_func_overrules_import_fails()
1916 var export_lines =<< trim END
1917 vim9script
1918 export def Func()
1919 echo 'imported'
1920 enddef
1921 END
1922 writefile(export_lines, 'XexportedFunc.vim')
1923
1924 var lines =<< trim END
1925 vim9script
1926 import './XexportedFunc.vim' as Func
1927 def Func()
1928 echo 'local to function'
1929 enddef
1930 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001931 v9.CheckScriptFailure(lines, 'E1213: Redefining imported item "Func"')
Bram Moolenaard8448622022-01-07 21:39:52 +00001932
1933 lines =<< trim END
1934 vim9script
1935 import './XexportedFunc.vim' as Func
1936 def Outer()
1937 def Func()
1938 echo 'local to function'
1939 enddef
1940 enddef
1941 defcompile
1942 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001943 v9.CheckScriptFailure(lines, 'E1236:')
Bram Moolenaard8448622022-01-07 21:39:52 +00001944
1945 delete('XexportedFunc.vim')
1946enddef
1947
1948def Test_source_vim9_from_legacy()
1949 var vim9_lines =<< trim END
1950 vim9script
1951 var local = 'local'
1952 g:global = 'global'
1953 export var exported = 'exported'
1954 export def GetText(): string
1955 return 'text'
1956 enddef
1957 END
1958 writefile(vim9_lines, 'Xvim9_script.vim')
1959
1960 var legacy_lines =<< trim END
1961 source Xvim9_script.vim
1962
1963 call assert_false(exists('local'))
1964 call assert_false(exists('exported'))
1965 call assert_false(exists('s:exported'))
1966 call assert_equal('global', global)
1967 call assert_equal('global', g:global)
Bram Moolenaard8448622022-01-07 21:39:52 +00001968 END
1969 writefile(legacy_lines, 'Xlegacy_script.vim')
1970
1971 source Xlegacy_script.vim
1972 assert_equal('global', g:global)
1973 unlet g:global
1974
1975 delete('Xlegacy_script.vim')
1976 delete('Xvim9_script.vim')
1977enddef
1978
Bram Moolenaarc43e6232022-01-13 20:51:56 +00001979def Test_import_vim9_from_legacy()
1980 var vim9_lines =<< trim END
1981 vim9script
1982 var local = 'local'
1983 g:global = 'global'
1984 export var exported = 'exported'
1985 export def GetText(): string
1986 return 'text'
1987 enddef
1988 END
1989 writefile(vim9_lines, 'Xvim9_export.vim')
1990
1991 var legacy_lines =<< trim END
1992 import './Xvim9_export.vim' as vim9
1993
1994 call assert_false(exists('vim9'))
1995 call assert_false(exists('local'))
1996 call assert_false(exists('s:vim9.local'))
1997 call assert_equal('global', global)
1998 call assert_equal('global', g:global)
1999 call assert_false(exists('exported'))
2000 call assert_false(exists('s:exported'))
2001 call assert_false(exists('*GetText'))
2002
2003 " imported symbol is script-local
2004 call assert_equal('exported', s:vim9.exported)
2005 call assert_equal('text', s:vim9.GetText())
2006 END
2007 writefile(legacy_lines, 'Xlegacy_script.vim')
2008
2009 source Xlegacy_script.vim
2010 assert_equal('global', g:global)
2011 unlet g:global
2012
2013 delete('Xlegacy_script.vim')
2014 delete('Xvim9_export.vim')
2015enddef
2016
Bram Moolenaard8448622022-01-07 21:39:52 +00002017def Test_cmdline_win()
2018 # if the Vim syntax highlighting uses Vim9 constructs they can be used from
2019 # the command line window.
2020 mkdir('rtp/syntax', 'p')
2021 var export_lines =<< trim END
2022 vim9script
2023 export var That = 'yes'
2024 END
2025 writefile(export_lines, 'rtp/syntax/Xexport.vim')
2026 var import_lines =<< trim END
2027 vim9script
2028 import './Xexport.vim' as exp
2029 echo exp.That
2030 END
2031 writefile(import_lines, 'rtp/syntax/vim.vim')
2032 var save_rtp = &rtp
2033 &rtp = getcwd() .. '/rtp' .. ',' .. &rtp
2034 syntax on
2035 augroup CmdWin
2036 autocmd CmdwinEnter * g:got_there = 'yes'
2037 augroup END
2038 # this will open and also close the cmdline window
2039 feedkeys('q:', 'xt')
2040 assert_equal('yes', g:got_there)
2041
2042 augroup CmdWin
2043 au!
2044 augroup END
2045 &rtp = save_rtp
2046 delete('rtp', 'rf')
2047enddef
2048
2049def Test_import_gone_when_sourced_twice()
2050 var exportlines =<< trim END
2051 vim9script
2052 if exists('g:guard')
2053 finish
2054 endif
2055 g:guard = 1
2056 export var name = 'someName'
2057 END
2058 writefile(exportlines, 'XexportScript.vim')
2059
2060 var lines =<< trim END
2061 vim9script
2062 import './XexportScript.vim' as expo
2063 def g:GetName(): string
2064 return expo.name
2065 enddef
2066 END
2067 writefile(lines, 'XscriptImport.vim')
2068 so XscriptImport.vim
2069 assert_equal('someName', g:GetName())
2070
2071 so XexportScript.vim
2072 assert_fails('call g:GetName()', 'E1149:')
2073
2074 delfunc g:GetName
2075 delete('XexportScript.vim')
2076 delete('XscriptImport.vim')
2077 unlet g:guard
2078enddef
2079
Bram Moolenaar160aa862022-01-10 21:29:57 +00002080" test using an auto-loaded function and variable
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002081def Test_vim9_autoload_full_name()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002082 var lines =<< trim END
2083 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002084 export def Gettest(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002085 return 'test'
2086 enddef
2087 g:some#name = 'name'
2088 g:some#dict = {key: 'value'}
2089
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002090 export def Varargs(a1: string, ...l: list<string>): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002091 return a1 .. l[0] .. l[1]
2092 enddef
2093 END
2094
2095 mkdir('Xdir/autoload', 'p')
2096 writefile(lines, 'Xdir/autoload/some.vim')
2097 var save_rtp = &rtp
2098 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2099
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002100 assert_equal('test', g:some#Gettest())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002101 assert_equal('name', g:some#name)
2102 assert_equal('value', g:some#dict.key)
2103 g:some#other = 'other'
2104 assert_equal('other', g:some#other)
2105
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002106 assert_equal('abc', some#Varargs('a', 'b', 'c'))
Bram Moolenaar160aa862022-01-10 21:29:57 +00002107
2108 # upper case script name works
2109 lines =<< trim END
2110 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002111 export def GetOther(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002112 return 'other'
2113 enddef
2114 END
2115 writefile(lines, 'Xdir/autoload/Other.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002116 assert_equal('other', g:Other#GetOther())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002117
2118 delete('Xdir', 'rf')
2119 &rtp = save_rtp
2120enddef
2121
2122def Test_vim9script_autoload()
2123 mkdir('Xdir/autoload', 'p')
2124 var save_rtp = &rtp
2125 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2126
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002127 # when the path has "/autoload/" prefix is not needed
Bram Moolenaar160aa862022-01-10 21:29:57 +00002128 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002129 vim9script
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002130 g:prefixed_loaded += 1
Bram Moolenaar160aa862022-01-10 21:29:57 +00002131
2132 export def Gettest(): string
2133 return 'test'
2134 enddef
2135
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002136 export var name = 'name'
2137
2138 export func GetFunc()
2139 return Gettest() .. 'more' .. s:name
Bram Moolenaar160aa862022-01-10 21:29:57 +00002140 endfunc
2141
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002142 export def GetDef(): string
2143 return Gettest() .. 'more' .. name
2144 enddef
2145
Bram Moolenaar160aa862022-01-10 21:29:57 +00002146 export final fname = 'final'
2147 export const cname = 'const'
2148 END
2149 writefile(lines, 'Xdir/autoload/prefixed.vim')
2150
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002151 g:prefixed_loaded = 0
2152 g:expected_loaded = 0
Bram Moolenaar160aa862022-01-10 21:29:57 +00002153 lines =<< trim END
2154 vim9script
2155 import autoload 'prefixed.vim'
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002156 assert_equal(g:expected_loaded, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002157 assert_equal('test', prefixed.Gettest())
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002158 assert_equal(1, g:prefixed_loaded)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002159
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002160 assert_equal('testmorename', prefixed.GetFunc())
2161 assert_equal('testmorename', prefixed.GetDef())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002162 assert_equal('name', prefixed.name)
2163 assert_equal('final', prefixed.fname)
2164 assert_equal('const', prefixed.cname)
2165 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002166 v9.CheckScriptSuccess(lines)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +00002167 # can source it again, autoload script not loaded again
2168 g:expected_loaded = 1
Bram Moolenaar62aec932022-01-29 21:45:34 +00002169 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002170
2171 # can also get the items by autoload name
2172 lines =<< trim END
2173 call assert_equal('test', prefixed#Gettest())
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002174 call assert_equal('testmorename', prefixed#GetFunc())
Bram Moolenaar160aa862022-01-10 21:29:57 +00002175 call assert_equal('name', prefixed#name)
2176 call assert_equal('final', prefixed#fname)
2177 call assert_equal('const', prefixed#cname)
2178 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002179 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002180
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002181 unlet g:prefixed_loaded
2182 unlet g:expected_loaded
2183 delete('Xdir', 'rf')
2184 &rtp = save_rtp
2185enddef
2186
Bram Moolenaard02dce22022-01-18 17:43:04 +00002187def Test_import_autoload_not_exported()
2188 mkdir('Xdir/autoload', 'p')
2189 var save_rtp = &rtp
2190 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2191
2192 # error when using an item that is not exported from an autoload script
2193 var exportLines =<< trim END
2194 vim9script
2195 var notExported = 123
2196 def NotExport()
2197 echo 'nop'
2198 enddef
2199 END
2200 writefile(exportLines, 'Xdir/autoload/notExport1.vim')
2201
2202 var lines =<< trim END
2203 vim9script
2204 import autoload 'notExport1.vim'
2205 echo notExport1.notFound
2206 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002207 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002208
2209 lines =<< trim END
2210 vim9script
2211 import autoload 'notExport1.vim'
2212 echo notExport1.notExported
2213 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002214 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002215
2216 lines =<< trim END
2217 vim9script
2218 import autoload 'notExport1.vim'
2219 echo notExport1.NotFunc()
2220 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002221 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002222
2223 lines =<< trim END
2224 vim9script
2225 import autoload 'notExport1.vim'
2226 echo notExport1.NotExport()
2227 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002228 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002229
2230 lines =<< trim END
2231 vim9script
2232 import autoload 'notExport1.vim'
2233 echo 'text'->notExport1.NotFunc()
2234 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002235 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002236
2237 lines =<< trim END
2238 vim9script
2239 import autoload 'notExport1.vim'
2240 echo 'text'->notExport1.NotExport()
2241 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002242 v9.CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002243
2244 # using a :def function we use a different autoload script every time so that
2245 # the function is compiled without the script loaded
2246 writefile(exportLines, 'Xdir/autoload/notExport2.vim')
2247 lines =<< trim END
2248 vim9script
2249 import autoload 'notExport2.vim'
2250 def Testit()
2251 echo notExport2.notFound
2252 enddef
2253 Testit()
2254 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002255 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport2#notFound')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002256
2257 writefile(exportLines, 'Xdir/autoload/notExport3.vim')
2258 lines =<< trim END
2259 vim9script
2260 import autoload 'notExport3.vim'
2261 def Testit()
2262 echo notExport3.notExported
2263 enddef
2264 Testit()
2265 END
2266 # don't get E1049 because it is too complicated to figure out
Bram Moolenaar62aec932022-01-29 21:45:34 +00002267 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: notExport3#notExported')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002268
2269 writefile(exportLines, 'Xdir/autoload/notExport4.vim')
2270 lines =<< trim END
2271 vim9script
2272 import autoload 'notExport4.vim'
2273 def Testit()
2274 echo notExport4.NotFunc()
2275 enddef
2276 Testit()
2277 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002278 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport4#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002279
2280 writefile(exportLines, 'Xdir/autoload/notExport5.vim')
2281 lines =<< trim END
2282 vim9script
2283 import autoload 'notExport5.vim'
2284 def Testit()
2285 echo notExport5.NotExport()
2286 enddef
2287 Testit()
2288 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002289 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport5#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002290
2291 writefile(exportLines, 'Xdir/autoload/notExport6.vim')
2292 lines =<< trim END
2293 vim9script
2294 import autoload 'notExport6.vim'
2295 def Testit()
2296 echo 'text'->notExport6.NotFunc()
2297 enddef
2298 Testit()
2299 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002300 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport6#NotFunc')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002301
2302 writefile(exportLines, 'Xdir/autoload/notExport7.vim')
2303 lines =<< trim END
2304 vim9script
2305 import autoload 'notExport7.vim'
2306 def Testit()
2307 echo 'text'->notExport7.NotExport()
2308 enddef
2309 Testit()
2310 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002311 v9.CheckScriptFailure(lines, 'E117: Unknown function: notExport7#NotExport')
Bram Moolenaard02dce22022-01-18 17:43:04 +00002312
2313 delete('Xdir', 'rf')
2314 &rtp = save_rtp
2315enddef
2316
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002317def Test_vim9script_autoload_call()
2318 mkdir('Xdir/autoload', 'p')
2319 var save_rtp = &rtp
2320 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2321
2322 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002323 vim9script
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002324
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002325 export def RetArg(arg: string): string
2326 return arg
2327 enddef
2328
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002329 export def Getother()
2330 g:result = 'other'
2331 enddef
2332 END
Bram Moolenaar5d982692022-01-12 15:15:27 +00002333 writefile(lines, 'Xdir/autoload/another.vim')
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002334
2335 lines =<< trim END
2336 vim9script
Bram Moolenaar5d982692022-01-12 15:15:27 +00002337 import autoload 'another.vim'
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002338
2339 # compile this before 'another.vim' is loaded
2340 def CallAnother()
2341 assert_equal('foo', 'foo'->another.RetArg())
2342 enddef
2343 CallAnother()
2344
Bram Moolenaar5d982692022-01-12 15:15:27 +00002345 call another.Getother()
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002346 assert_equal('other', g:result)
Bram Moolenaar3d8e25a2022-01-22 11:00:02 +00002347
2348 assert_equal('arg', call('another.RetArg', ['arg']))
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00002349
2350 verbose function another.Getother
2351 # should we disallow this?
2352 verbose function another#Getother
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002353 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002354 v9.CheckScriptSuccess(lines)
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00002355
2356 unlet g:result
Bram Moolenaar160aa862022-01-10 21:29:57 +00002357 delete('Xdir', 'rf')
2358 &rtp = save_rtp
2359enddef
2360
Bram Moolenaarb697dc22022-01-22 11:27:29 +00002361def Test_vim9script_noclear_autoload()
2362 mkdir('Xdir/autoload', 'p')
2363 var save_rtp = &rtp
2364 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2365
2366 var lines =<< trim END
2367 vim9script
2368 export def Func(): string
2369 return 'called'
2370 enddef
2371 g:double_loaded = 'yes'
2372 END
2373 writefile(lines, 'Xdir/autoload/double.vim')
2374
2375 lines =<< trim END
2376 vim9script noclear
2377 if exists('g:script_loaded')
2378 finish
2379 endif
2380 g:script_loaded = true
2381
2382 import autoload 'double.vim'
2383 nnoremap <F3> <ScriptCmd>g:result = double.Func()<CR>
2384 END
2385 g:double_loaded = 'no'
2386 writefile(lines, 'Xloaddouble')
2387 source Xloaddouble
2388 assert_equal('no', g:double_loaded)
2389 assert_equal(true, g:script_loaded)
2390 source Xloaddouble
2391 feedkeys("\<F3>", 'xt')
2392 assert_equal('called', g:result)
2393 assert_equal('yes', g:double_loaded)
2394
2395 delete('Xloaddouble')
2396 unlet g:double_loaded
2397 unlet g:script_loaded
2398 unlet g:result
2399 delete('Xdir', 'rf')
2400 &rtp = save_rtp
2401enddef
2402
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002403def Test_vim9script_autoload_duplicate()
2404 mkdir('Xdir/autoload', 'p')
2405
2406 var lines =<< trim END
2407 vim9script
2408
2409 export def Func()
2410 enddef
2411
2412 def Func()
2413 enddef
2414 END
2415 writefile(lines, 'Xdir/autoload/dupfunc.vim')
2416 assert_fails('source Xdir/autoload/dupfunc.vim', 'E1073:')
2417
2418 lines =<< trim END
2419 vim9script
2420
2421 def Func()
2422 enddef
2423
2424 export def Func()
2425 enddef
2426 END
2427 writefile(lines, 'Xdir/autoload/dup2func.vim')
2428 assert_fails('source Xdir/autoload/dup2func.vim', 'E1073:')
2429
2430 lines =<< trim END
2431 vim9script
2432
2433 def Func()
2434 enddef
2435
2436 export var Func = 'asdf'
2437 END
2438 writefile(lines, 'Xdir/autoload/dup3func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002439 assert_fails('source Xdir/autoload/dup3func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002440
2441 lines =<< trim END
2442 vim9script
2443
2444 export var Func = 'asdf'
2445
2446 def Func()
2447 enddef
2448 END
2449 writefile(lines, 'Xdir/autoload/dup4func.vim')
2450 assert_fails('source Xdir/autoload/dup4func.vim', 'E707:')
2451
2452 lines =<< trim END
2453 vim9script
2454
2455 var Func = 'asdf'
2456
2457 export def Func()
2458 enddef
2459 END
2460 writefile(lines, 'Xdir/autoload/dup5func.vim')
2461 assert_fails('source Xdir/autoload/dup5func.vim', 'E707:')
2462
2463 lines =<< trim END
2464 vim9script
2465
2466 export def Func()
2467 enddef
2468
2469 var Func = 'asdf'
2470 END
2471 writefile(lines, 'Xdir/autoload/dup6func.vim')
Bram Moolenaare18acb02022-03-21 20:40:35 +00002472 assert_fails('source Xdir/autoload/dup6func.vim', 'E1041: Redefining script item: "Func"')
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002473
2474 delete('Xdir', 'rf')
2475enddef
2476
Bram Moolenaar2017d6f2022-01-20 19:38:46 +00002477def Test_autoload_missing_function_name()
2478 mkdir('Xdir/autoload', 'p')
2479
2480 var lines =<< trim END
2481 vim9script
2482
2483 def loadme#()
2484 enddef
2485 END
2486 writefile(lines, 'Xdir/autoload/loadme.vim')
2487 assert_fails('source Xdir/autoload/loadme.vim', 'E129:')
2488
2489 delete('Xdir', 'rf')
2490enddef
2491
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002492def Test_autoload_name_wrong()
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002493 var lines =<< trim END
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002494 def Xscriptname#Func()
2495 enddef
2496 END
2497 writefile(lines, 'Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002498 v9.CheckScriptFailure(lines, 'E746:')
Yegappan Lakshmanan7e765a32022-01-24 11:40:37 +00002499 delete('Xscriptname.vim')
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002500
2501 mkdir('Xdir/autoload', 'p')
2502 lines =<< trim END
2503 vim9script
2504 def somescript#Func()
2505 enddef
2506 END
2507 writefile(lines, 'Xdir/autoload/somescript.vim')
2508 assert_fails('source Xdir/autoload/somescript.vim', 'E1263:')
2509
2510 delete('Xdir', 'rf')
Bram Moolenaar19e69a62022-01-21 20:37:05 +00002511enddef
2512
Bram Moolenaard041f422022-01-12 19:54:00 +00002513def Test_import_autoload_postponed()
2514 mkdir('Xdir/autoload', 'p')
2515 var save_rtp = &rtp
2516 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2517
2518 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002519 vim9script
Bram Moolenaard041f422022-01-12 19:54:00 +00002520
2521 g:loaded_postponed = 'true'
2522 export var variable = 'bla'
2523 export def Function(): string
2524 return 'bla'
2525 enddef
2526 END
2527 writefile(lines, 'Xdir/autoload/postponed.vim')
2528
2529 lines =<< trim END
2530 vim9script
2531
2532 import autoload 'postponed.vim'
2533 def Tryit()
2534 echo postponed.variable
2535 echo postponed.Function()
2536 enddef
2537 defcompile
2538 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002539 v9.CheckScriptSuccess(lines)
Bram Moolenaard041f422022-01-12 19:54:00 +00002540 assert_false(exists('g:loaded_postponed'))
Bram Moolenaar62aec932022-01-29 21:45:34 +00002541 v9.CheckScriptSuccess(lines + ['Tryit()'])
Bram Moolenaard041f422022-01-12 19:54:00 +00002542 assert_equal('true', g:loaded_postponed)
2543
2544 unlet g:loaded_postponed
2545 delete('Xdir', 'rf')
2546 &rtp = save_rtp
2547enddef
2548
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002549def Test_import_autoload_override()
2550 mkdir('Xdir/autoload', 'p')
2551 var save_rtp = &rtp
2552 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2553 test_override('autoload', 1)
2554
2555 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002556 vim9script
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002557
2558 g:loaded_override = 'true'
2559 export var variable = 'bla'
2560 export def Function(): string
2561 return 'bla'
2562 enddef
2563 END
2564 writefile(lines, 'Xdir/autoload/override.vim')
2565
2566 lines =<< trim END
2567 vim9script
2568
2569 import autoload 'override.vim'
2570 assert_equal('true', g:loaded_override)
2571
2572 def Tryit()
2573 echo override.doesNotExist
2574 enddef
2575 defcompile
2576 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002577 v9.CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1)
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +00002578
2579 test_override('autoload', 0)
2580 unlet g:loaded_override
2581 delete('Xdir', 'rf')
2582 &rtp = save_rtp
2583enddef
2584
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002585def Test_autoload_mapping()
2586 mkdir('Xdir/autoload', 'p')
2587 var save_rtp = &rtp
2588 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2589
2590 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002591 vim9script
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002592
2593 g:toggle_loaded = 'yes'
2594
2595 export def Toggle(): string
2596 return ":g:toggle_called = 'yes'\<CR>"
2597 enddef
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002598 export def Doit()
2599 g:doit_called = 'yes'
2600 enddef
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002601 END
2602 writefile(lines, 'Xdir/autoload/toggle.vim')
2603
2604 lines =<< trim END
2605 vim9script
2606
2607 import autoload 'toggle.vim'
2608
2609 nnoremap <silent> <expr> tt toggle.Toggle()
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002610 nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR>
2611 nnoremap <silent> yy <Cmd>toggle.Doit()<CR>
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002612 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002613 v9.CheckScriptSuccess(lines)
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002614 assert_false(exists("g:toggle_loaded"))
2615 assert_false(exists("g:toggle_called"))
Bram Moolenaar6079da72022-01-18 14:16:59 +00002616 assert_match('\d A: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002617
2618 feedkeys("tt", 'xt')
2619 assert_equal('yes', g:toggle_loaded)
2620 assert_equal('yes', g:toggle_called)
Bram Moolenaar6079da72022-01-18 14:16:59 +00002621 assert_match('\d: \f*[/\\]toggle.vim', execute('scriptnames'))
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002622
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002623 feedkeys("xx", 'xt')
2624 assert_equal('yes', g:doit_called)
2625
2626 assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle')
2627
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002628 nunmap tt
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002629 nunmap xx
2630 nunmap yy
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002631 unlet g:toggle_loaded
2632 unlet g:toggle_called
2633 delete('Xdir', 'rf')
2634 &rtp = save_rtp
2635enddef
2636
Bram Moolenaar160aa862022-01-10 21:29:57 +00002637def Test_vim9script_autoload_fails()
2638 var lines =<< trim END
2639 vim9script autoload
2640 var n = 0
2641 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002642 v9.CheckScriptFailure(lines, 'E475: Invalid argument: autoload')
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002643
2644 lines =<< trim END
2645 vim9script noclear noclear
2646 var n = 0
2647 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002648 v9.CheckScriptFailure(lines, 'E983: Duplicate argument: noclear')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002649enddef
2650
2651def Test_import_autoload_fails()
2652 var lines =<< trim END
2653 vim9script
2654 import autoload autoload 'prefixed.vim'
2655 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002656 v9.CheckScriptFailure(lines, 'E121: Undefined variable: autoload')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002657
2658 lines =<< trim END
2659 vim9script
Bram Moolenaar1836d612022-01-18 13:14:47 +00002660 import autoload './doesNotExist.vim'
Bram Moolenaar160aa862022-01-10 21:29:57 +00002661 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002662 v9.CheckScriptFailure(lines, 'E282:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002663
2664 lines =<< trim END
2665 vim9script
2666 import autoload '/dir/doesNotExist.vim'
2667 END
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01002668 v9.CheckScriptFailure(lines, 'E282:', 2)
2669
2670 lines =<< trim END
2671 vim9script
2672 import autoload '../testdir'
2673 END
2674 v9.CheckScriptFailure(lines, 'E17:', 2)
Bram Moolenaar1836d612022-01-18 13:14:47 +00002675
2676 lines =<< trim END
2677 vim9script
2678 import autoload 'doesNotExist.vim'
2679 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002680 v9.CheckScriptFailure(lines, 'E1053: Could not import "doesNotExist.vim"')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002681enddef
2682
2683" test disassembling an auto-loaded function starting with "debug"
2684def Test_vim9_autoload_disass()
2685 mkdir('Xdir/autoload', 'p')
2686 var save_rtp = &rtp
2687 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2688
2689 var lines =<< trim END
2690 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002691 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002692 return 'debug'
2693 enddef
2694 END
2695 writefile(lines, 'Xdir/autoload/debugit.vim')
2696
2697 lines =<< trim END
2698 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002699 export def Test(): string
Bram Moolenaar160aa862022-01-10 21:29:57 +00002700 return 'profile'
2701 enddef
2702 END
2703 writefile(lines, 'Xdir/autoload/profileit.vim')
2704
2705 lines =<< trim END
2706 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002707 assert_equal('debug', debugit#Test())
2708 disass debugit#Test
2709 assert_equal('profile', profileit#Test())
2710 disass profileit#Test
Bram Moolenaar160aa862022-01-10 21:29:57 +00002711 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002712 v9.CheckScriptSuccess(lines)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002713
2714 delete('Xdir', 'rf')
2715 &rtp = save_rtp
2716enddef
2717
2718" test using a vim9script that is auto-loaded from an autocmd
2719def Test_vim9_aucmd_autoload()
2720 var lines =<< trim END
2721 vim9script
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002722 export def Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002723 echomsg getreg('"')
2724 enddef
2725 END
2726
2727 mkdir('Xdir/autoload', 'p')
2728 writefile(lines, 'Xdir/autoload/foo.vim')
2729 var save_rtp = &rtp
2730 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2731 augroup test
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00002732 autocmd TextYankPost * call foo#Test()
Bram Moolenaar160aa862022-01-10 21:29:57 +00002733 augroup END
2734
2735 normal Y
2736
2737 augroup test
2738 autocmd!
2739 augroup END
2740 delete('Xdir', 'rf')
2741 &rtp = save_rtp
2742enddef
2743
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002744" test using a autoloaded file that is case sensitive
2745def Test_vim9_autoload_case_sensitive()
2746 var lines =<< trim END
Bram Moolenaarfd218c82022-01-18 16:26:24 +00002747 vim9script
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002748 export def CaseSensitive(): string
2749 return 'done'
2750 enddef
2751 END
2752
2753 mkdir('Xdir/autoload', 'p')
2754 writefile(lines, 'Xdir/autoload/CaseSensitive.vim')
2755 var save_rtp = &rtp
2756 exe 'set rtp^=' .. getcwd() .. '/Xdir'
2757
2758 lines =<< trim END
2759 vim9script
2760 import autoload 'CaseSensitive.vim'
2761 assert_equal('done', CaseSensitive.CaseSensitive())
2762 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002763 v9.CheckScriptSuccess(lines)
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002764
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002765 if !has('fname_case')
2766 lines =<< trim END
2767 vim9script
2768 import autoload 'CaseSensitive.vim'
2769 import autoload 'casesensitive.vim'
2770 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002771 v9.CheckScriptFailure(lines, 'E1262:')
Bram Moolenaarbfac4092022-01-16 11:12:12 +00002772 endif
2773
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002774 delete('Xdir', 'rf')
2775 &rtp = save_rtp
2776enddef
2777
Bram Moolenaar160aa862022-01-10 21:29:57 +00002778" This was causing a crash because suppress_errthrow wasn't reset.
2779def Test_vim9_autoload_error()
2780 var lines =<< trim END
2781 vim9script
2782 def crash#func()
2783 try
2784 for x in List()
2785 endfor
2786 catch
2787 endtry
2788 g:ok = true
2789 enddef
2790 fu List()
2791 invalid
2792 endfu
2793 try
2794 alsoinvalid
2795 catch /wontmatch/
2796 endtry
2797 END
2798 call mkdir('Xruntime/autoload', 'p')
2799 call writefile(lines, 'Xruntime/autoload/crash.vim')
2800
2801 # run in a separate Vim to avoid the side effects of assert_fails()
2802 lines =<< trim END
2803 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
2804 call crash#func()
2805 call writefile(['ok'], 'Xdidit')
2806 qall!
2807 END
2808 writefile(lines, 'Xscript')
Bram Moolenaar62aec932022-01-29 21:45:34 +00002809 g:RunVim([], [], '-S Xscript')
Bram Moolenaar160aa862022-01-10 21:29:57 +00002810 assert_equal(['ok'], readfile('Xdidit'))
2811
2812 delete('Xdidit')
2813 delete('Xscript')
2814 delete('Xruntime', 'rf')
2815
2816 lines =<< trim END
2817 vim9script
2818 var foo#bar = 'asdf'
2819 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002820 v9.CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
Bram Moolenaar160aa862022-01-10 21:29:57 +00002821enddef
2822
Bram Moolenaard8448622022-01-07 21:39:52 +00002823
2824" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker