Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 1 | " Test import/export of the Vim9 script language. |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 2 | " Also the autoload mechanism. |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 3 | |
| 4 | source check.vim |
| 5 | source term_util.vim |
| 6 | source vim9.vim |
| 7 | |
| 8 | let 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 Moolenaar | 857c8bb | 2022-01-15 21:08:19 +0000 | [diff] [blame] | 30 | export def AddSome(s: string): string |
| 31 | return s .. 'some' |
| 32 | enddef |
| 33 | export var AddRef = AddSome |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 34 | END |
| 35 | |
| 36 | def Undo_export_script_lines() |
| 37 | unlet g:result |
| 38 | unlet g:localname |
| 39 | enddef |
| 40 | |
| 41 | def 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 | |
| 70 | g:imported_name = expo.exp_name |
| 71 | expo.exp_name ..= ' Doe' |
Bram Moolenaar | 47036b6 | 2022-01-16 21:18:53 +0000 | [diff] [blame] | 72 | expo.exp_name = expo.exp_name .. ' Maar' |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 73 | g:imported_name_appended = expo.exp_name |
| 74 | g:exported_later = expo.exported |
| 75 | |
| 76 | expo.theList->add(2) |
| 77 | assert_equal([1, 2], expo.theList) |
Bram Moolenaar | 857c8bb | 2022-01-15 21:08:19 +0000 | [diff] [blame] | 78 | |
| 79 | assert_equal('andthensome', 'andthen'->expo.AddSome()) |
| 80 | assert_equal('awesome', 'awe'->expo.AddRef()) |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 81 | END |
| 82 | writefile(import_script_lines, 'Ximport.vim') |
| 83 | source Ximport.vim |
| 84 | |
| 85 | assert_equal('bobbie', g:result) |
| 86 | assert_equal('bob', g:localname) |
| 87 | assert_equal(9876, g:exported1) |
| 88 | assert_equal(9879, g:exported2) |
| 89 | assert_equal(9879, g:exported3) |
| 90 | |
| 91 | assert_equal(9884, g:exported_i1) |
| 92 | assert_equal(9884, g:exported_i2) |
| 93 | |
| 94 | assert_equal(11, g:exported_s1) |
| 95 | assert_equal(11, g:exported_s2) |
| 96 | assert_equal(11, g:exported_later) |
| 97 | |
| 98 | assert_equal('Exported', g:imported_func) |
| 99 | assert_equal('Exported', g:funcref_result) |
| 100 | assert_equal('John', g:imported_name) |
Bram Moolenaar | 47036b6 | 2022-01-16 21:18:53 +0000 | [diff] [blame] | 101 | assert_equal('John Doe Maar', g:imported_name_appended) |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 102 | assert_false(exists('g:name')) |
| 103 | |
| 104 | Undo_export_script_lines() |
| 105 | unlet g:exported1 |
| 106 | unlet g:exported2 |
| 107 | unlet g:exported3 |
| 108 | unlet g:exported_i1 |
| 109 | unlet g:exported_i2 |
| 110 | unlet g:exported_later |
| 111 | unlet g:imported_func |
| 112 | unlet g:imported_name g:imported_name_appended |
| 113 | delete('Ximport.vim') |
| 114 | |
| 115 | # similar, with line breaks |
| 116 | var import_line_break_script_lines =<< trim END |
| 117 | vim9script |
| 118 | import './Xexport.vim' |
| 119 | as expo |
| 120 | g:exported = expo.exported |
| 121 | expo.exported += 7 |
| 122 | g:exported_added = expo.exported |
| 123 | g:imported_func = expo.Exported() |
| 124 | END |
| 125 | writefile(import_line_break_script_lines, 'Ximport_lbr.vim') |
| 126 | source Ximport_lbr.vim |
| 127 | |
| 128 | assert_equal(11, g:exported) |
| 129 | assert_equal(18, g:exported_added) |
| 130 | assert_equal('Exported', g:imported_func) |
| 131 | |
| 132 | # exported script not sourced again |
| 133 | assert_false(exists('g:result')) |
| 134 | unlet g:exported |
| 135 | unlet g:exported_added |
| 136 | unlet g:imported_func |
| 137 | delete('Ximport_lbr.vim') |
| 138 | |
| 139 | var line_break_before_dot =<< trim END |
| 140 | vim9script |
| 141 | import './Xexport.vim' as expo |
| 142 | g:exported = expo |
| 143 | .exported |
| 144 | END |
| 145 | writefile(line_break_before_dot, 'Ximport_lbr_before_dot.vim') |
| 146 | assert_fails('source Ximport_lbr_before_dot.vim', 'E1060:', '', 3) |
| 147 | delete('Ximport_lbr_before_dot.vim') |
| 148 | |
| 149 | var line_break_after_dot =<< trim END |
| 150 | vim9script |
| 151 | import './Xexport.vim' as expo |
| 152 | g:exported = expo. |
| 153 | exported |
| 154 | END |
| 155 | writefile(line_break_after_dot, 'Ximport_lbr_after_dot.vim') |
| 156 | assert_fails('source Ximport_lbr_after_dot.vim', 'E1074:', '', 3) |
| 157 | delete('Ximport_lbr_after_dot.vim') |
| 158 | |
| 159 | var import_star_as_lines =<< trim END |
| 160 | vim9script |
| 161 | import './Xexport.vim' as Export |
| 162 | def UseExport() |
| 163 | g:exported_def = Export.exported |
| 164 | enddef |
| 165 | g:exported_script = Export.exported |
| 166 | assert_equal(1, exists('Export.exported')) |
| 167 | assert_equal(0, exists('Export.notexported')) |
| 168 | UseExport() |
| 169 | END |
| 170 | writefile(import_star_as_lines, 'Ximport.vim') |
| 171 | source Ximport.vim |
| 172 | |
| 173 | assert_equal(18, g:exported_def) |
| 174 | assert_equal(18, g:exported_script) |
| 175 | unlet g:exported_def |
| 176 | unlet g:exported_script |
| 177 | |
| 178 | var import_star_as_lines_no_dot =<< trim END |
| 179 | vim9script |
| 180 | import './Xexport.vim' as Export |
| 181 | def Func() |
| 182 | var dummy = 1 |
| 183 | var imported = Export + dummy |
| 184 | enddef |
| 185 | defcompile |
| 186 | END |
| 187 | writefile(import_star_as_lines_no_dot, 'Ximport.vim') |
| 188 | assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func') |
| 189 | |
| 190 | var import_star_as_lines_dot_space =<< trim END |
| 191 | vim9script |
| 192 | import './Xexport.vim' as Export |
| 193 | def Func() |
| 194 | var imported = Export . exported |
| 195 | enddef |
| 196 | defcompile |
| 197 | END |
| 198 | writefile(import_star_as_lines_dot_space, 'Ximport.vim') |
| 199 | assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func') |
| 200 | |
Bram Moolenaar | 7c24dfd | 2022-01-08 17:03:55 +0000 | [diff] [blame] | 201 | writefile(s:export_script_lines, 'Xexport2.vim') |
| 202 | var import_as_duplicated =<< trim END |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 203 | vim9script |
| 204 | import './Xexport.vim' as expo |
Bram Moolenaar | 7c24dfd | 2022-01-08 17:03:55 +0000 | [diff] [blame] | 205 | import './Xexport2.vim' as expo |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 206 | END |
Bram Moolenaar | 7c24dfd | 2022-01-08 17:03:55 +0000 | [diff] [blame] | 207 | writefile(import_as_duplicated, 'Ximport.vim') |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 208 | assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim') |
Bram Moolenaar | 7c24dfd | 2022-01-08 17:03:55 +0000 | [diff] [blame] | 209 | delete('Xexport2.vim') |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 210 | |
| 211 | var import_star_as_lines_script_no_dot =<< trim END |
| 212 | vim9script |
| 213 | import './Xexport.vim' as Export |
| 214 | g:imported_script = Export exported |
| 215 | END |
| 216 | writefile(import_star_as_lines_script_no_dot, 'Ximport.vim') |
| 217 | assert_fails('source Ximport.vim', 'E1060: Expected dot after name: Export exported') |
| 218 | |
| 219 | var import_star_as_lines_script_space_after_dot =<< trim END |
| 220 | vim9script |
| 221 | import './Xexport.vim' as Export |
| 222 | g:imported_script = Export. exported |
| 223 | END |
| 224 | writefile(import_star_as_lines_script_space_after_dot, 'Ximport.vim') |
| 225 | assert_fails('source Ximport.vim', 'E1074:') |
| 226 | |
| 227 | var import_star_as_lines_missing_name =<< trim END |
| 228 | vim9script |
| 229 | import './Xexport.vim' as Export |
| 230 | def Func() |
| 231 | var imported = Export. |
| 232 | enddef |
| 233 | defcompile |
| 234 | END |
| 235 | writefile(import_star_as_lines_missing_name, 'Ximport.vim') |
| 236 | assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func') |
| 237 | |
| 238 | var import_star_as_lbr_lines =<< trim END |
| 239 | vim9script |
| 240 | import './Xexport.vim' |
| 241 | as Export |
| 242 | def UseExport() |
| 243 | g:exported = Export.exported |
| 244 | enddef |
| 245 | UseExport() |
| 246 | END |
| 247 | writefile(import_star_as_lbr_lines, 'Ximport.vim') |
| 248 | source Ximport.vim |
| 249 | assert_equal(18, g:exported) |
| 250 | unlet g:exported |
| 251 | |
| 252 | # try to use something that exists but is not exported |
| 253 | var import_not_exported_lines =<< trim END |
| 254 | vim9script |
| 255 | import './Xexport.vim' as expo |
| 256 | echo expo.name |
| 257 | END |
| 258 | writefile(import_not_exported_lines, 'Ximport.vim') |
| 259 | assert_fails('source Ximport.vim', 'E1049:', '', 3, 'Ximport.vim') |
| 260 | |
| 261 | # try to import something that is already defined |
| 262 | var import_already_defined =<< trim END |
| 263 | vim9script |
| 264 | var exported = 'something' |
| 265 | import './Xexport.vim' as exported |
| 266 | END |
| 267 | writefile(import_already_defined, 'Ximport.vim') |
| 268 | assert_fails('source Ximport.vim', 'E1054:', '', 3, 'Ximport.vim') |
| 269 | |
| 270 | # try changing an imported const |
| 271 | var import_assign_to_const =<< trim END |
| 272 | vim9script |
| 273 | import './Xexport.vim' as expo |
| 274 | def Assign() |
| 275 | expo.CONST = 987 |
| 276 | enddef |
| 277 | defcompile |
| 278 | END |
| 279 | writefile(import_assign_to_const, 'Ximport.vim') |
| 280 | assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign') |
| 281 | |
| 282 | # try changing an imported final |
| 283 | var import_assign_to_final =<< trim END |
| 284 | vim9script |
| 285 | import './Xexport.vim' as expo |
| 286 | def Assign() |
| 287 | expo.theList = [2] |
| 288 | enddef |
| 289 | defcompile |
| 290 | END |
| 291 | writefile(import_assign_to_final, 'Ximport.vim') |
| 292 | assert_fails('source Ximport.vim', 'E46:', '', 1, '_Assign') |
| 293 | |
| 294 | var import_no_as_lines =<< trim END |
| 295 | vim9script |
| 296 | import './Xexport.vim' name |
| 297 | END |
| 298 | writefile(import_no_as_lines, 'Ximport.vim') |
| 299 | assert_fails('source Ximport.vim', 'E488:', '', 2, 'Ximport.vim') |
| 300 | |
| 301 | var import_invalid_string_lines =<< trim END |
| 302 | vim9script |
| 303 | import Xexport.vim |
| 304 | END |
| 305 | writefile(import_invalid_string_lines, 'Ximport.vim') |
| 306 | assert_fails('source Ximport.vim', 'E121:', '', 2, 'Ximport.vim') |
| 307 | |
| 308 | var import_wrong_name_lines =<< trim END |
| 309 | vim9script |
| 310 | import './XnoExport.vim' |
| 311 | END |
| 312 | writefile(import_wrong_name_lines, 'Ximport.vim') |
| 313 | assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim') |
| 314 | |
| 315 | var import_redefining_lines =<< trim END |
| 316 | vim9script |
| 317 | import './Xexport.vim' as exported |
| 318 | var exported = 5 |
| 319 | END |
| 320 | writefile(import_redefining_lines, 'Ximport.vim') |
| 321 | assert_fails('source Ximport.vim', 'E1213: Redefining imported item "exported"', '', 3) |
| 322 | |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 323 | var import_missing_dot_lines =<< trim END |
| 324 | vim9script |
| 325 | import './Xexport.vim' as expo |
| 326 | def Test() |
| 327 | expo = 9 |
| 328 | enddef |
| 329 | defcompile |
| 330 | END |
| 331 | writefile(import_missing_dot_lines, 'Ximport.vim') |
| 332 | assert_fails('source Ximport.vim', 'E1258:', '', 1) |
| 333 | |
| 334 | var import_missing_name_lines =<< trim END |
| 335 | vim9script |
| 336 | import './Xexport.vim' as expo |
| 337 | def Test() |
| 338 | expo.99 = 9 |
| 339 | enddef |
| 340 | defcompile |
| 341 | END |
| 342 | writefile(import_missing_name_lines, 'Ximport.vim') |
Bram Moolenaar | 7628382 | 2022-01-10 21:39:03 +0000 | [diff] [blame] | 343 | assert_fails('source Ximport.vim', 'E1259:', '', 1) |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 344 | |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 345 | var import_assign_wrong_type_lines =<< trim END |
| 346 | vim9script |
| 347 | import './Xexport.vim' as expo |
| 348 | expo.exported = 'xxx' |
| 349 | END |
| 350 | writefile(import_assign_wrong_type_lines, 'Ximport.vim') |
| 351 | assert_fails('source Ximport.vim', 'E1012: Type mismatch; expected number but got string', '', 3) |
| 352 | |
| 353 | var import_assign_const_lines =<< trim END |
| 354 | vim9script |
| 355 | import './Xexport.vim' as expo |
| 356 | expo.CONST = 4321 |
| 357 | END |
| 358 | writefile(import_assign_const_lines, 'Ximport.vim') |
| 359 | assert_fails('source Ximport.vim', 'E46: Cannot change read-only variable "CONST"', '', 3) |
| 360 | |
| 361 | delete('Ximport.vim') |
| 362 | delete('Ximport3.vim') |
| 363 | delete('Xexport.vim') |
| 364 | |
| 365 | # Check that in a Vim9 script 'cpo' is set to the Vim default. |
| 366 | # Flags added or removed are also applied to the restored value. |
| 367 | set cpo=abcd |
| 368 | var lines =<< trim END |
| 369 | vim9script |
| 370 | g:cpo_in_vim9script = &cpo |
| 371 | set cpo+=f |
| 372 | set cpo-=c |
| 373 | g:cpo_after_vim9script = &cpo |
| 374 | END |
| 375 | writefile(lines, 'Xvim9_script') |
| 376 | source Xvim9_script |
| 377 | assert_equal('fabd', &cpo) |
| 378 | set cpo&vim |
| 379 | assert_equal(&cpo, g:cpo_in_vim9script) |
| 380 | var newcpo = substitute(&cpo, 'c', '', '') .. 'f' |
| 381 | assert_equal(newcpo, g:cpo_after_vim9script) |
| 382 | |
| 383 | delete('Xvim9_script') |
| 384 | enddef |
| 385 | |
| 386 | def Test_import_funcref() |
| 387 | var lines =<< trim END |
| 388 | vim9script |
| 389 | export def F(): number |
| 390 | return 42 |
| 391 | enddef |
| 392 | export const G = F |
| 393 | END |
| 394 | writefile(lines, 'Xlib.vim') |
| 395 | |
| 396 | lines =<< trim END |
| 397 | vim9script |
| 398 | import './Xlib.vim' as lib |
| 399 | const Foo = lib.G() |
| 400 | assert_equal(42, Foo) |
| 401 | |
| 402 | def DoTest() |
| 403 | const Goo = lib.G() |
| 404 | assert_equal(42, Goo) |
| 405 | enddef |
| 406 | DoTest() |
| 407 | END |
| 408 | CheckScriptSuccess(lines) |
| 409 | |
| 410 | delete('Xlib.vim') |
| 411 | enddef |
| 412 | |
| 413 | def Test_import_fails() |
| 414 | writefile([], 'Xfoo.vim') |
| 415 | var lines =<< trim END |
| 416 | import './Xfoo.vim' as foo |
| 417 | foo = 'bar' |
| 418 | END |
| 419 | CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use foo itself']) |
| 420 | lines =<< trim END |
| 421 | vim9script |
| 422 | import './Xfoo.vim' as foo |
| 423 | var that = foo |
| 424 | END |
| 425 | CheckScriptFailure(lines, 'E1060: Expected dot after name: foo') |
| 426 | |
| 427 | lines =<< trim END |
| 428 | vim9script |
| 429 | import './Xfoo.vim' as 9foo |
| 430 | END |
| 431 | CheckScriptFailure(lines, 'E1047:') |
| 432 | lines =<< trim END |
| 433 | vim9script |
| 434 | import './Xfoo.vim' as the#foo |
| 435 | END |
| 436 | CheckScriptFailure(lines, 'E1047:') |
| 437 | lines =<< trim END |
| 438 | vim9script |
| 439 | import './Xfoo.vim' as g:foo |
| 440 | END |
| 441 | CheckScriptFailure(lines, 'E1047:') |
| 442 | |
| 443 | delete('Xfoo.vim') |
| 444 | |
| 445 | lines =<< trim END |
| 446 | vim9script |
| 447 | def TheFunc() |
| 448 | echo 'the func' |
| 449 | enddef |
| 450 | export var Ref = TheFunc |
| 451 | END |
| 452 | writefile([], 'Xthat.vim') |
Bram Moolenaar | 7c24dfd | 2022-01-08 17:03:55 +0000 | [diff] [blame] | 453 | |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 454 | lines =<< trim END |
| 455 | import './Xthat.vim' as That |
| 456 | That() |
| 457 | END |
| 458 | CheckDefAndScriptFailure(lines, ['E1094:', 'E1236: Cannot use That itself']) |
Bram Moolenaar | 7c24dfd | 2022-01-08 17:03:55 +0000 | [diff] [blame] | 459 | |
| 460 | lines =<< trim END |
| 461 | import './Xthat.vim' as one |
| 462 | import './Xthat.vim' as two |
| 463 | END |
| 464 | CheckScriptFailure(lines, 'E1262:') |
| 465 | |
| 466 | delete('Xthat.vim') |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 467 | |
| 468 | mkdir('Ximport') |
| 469 | |
| 470 | writefile(['vim9script'], 'Ximport/.vim') |
| 471 | lines =<< trim END |
| 472 | vim9script |
| 473 | import './Ximport/.vim' |
| 474 | END |
| 475 | CheckScriptFailure(lines, 'E1261: Cannot import .vim without using "as"') |
| 476 | lines =<< trim END |
| 477 | vim9script |
| 478 | import './Ximport/.vim' as vim |
| 479 | END |
| 480 | CheckScriptSuccess(lines) |
| 481 | |
| 482 | writefile(['vim9script'], 'Ximport/.vimrc') |
| 483 | lines =<< trim END |
| 484 | vim9script |
| 485 | import './Ximport/.vimrc' |
| 486 | END |
| 487 | CheckScriptFailure(lines, 'E1257: Imported script must use "as" or end in .vim') |
| 488 | lines =<< trim END |
| 489 | vim9script |
| 490 | import './Ximport/.vimrc' as vimrc |
| 491 | END |
| 492 | CheckScriptSuccess(lines) |
| 493 | |
| 494 | delete('Ximport', 'rf') |
| 495 | enddef |
| 496 | |
| 497 | func g:Trigger() |
| 498 | source Ximport.vim |
| 499 | return "echo 'yes'\<CR>" |
| 500 | endfunc |
| 501 | |
| 502 | def Test_import_export_expr_map() |
| 503 | # check that :import and :export work when buffer is locked |
| 504 | var export_lines =<< trim END |
| 505 | vim9script |
| 506 | export def That(): string |
| 507 | return 'yes' |
| 508 | enddef |
| 509 | END |
| 510 | writefile(export_lines, 'Xexport_that.vim') |
| 511 | |
| 512 | var import_lines =<< trim END |
| 513 | vim9script |
| 514 | import './Xexport_that.vim' as that |
| 515 | assert_equal('yes', that.That()) |
| 516 | END |
| 517 | writefile(import_lines, 'Ximport.vim') |
| 518 | |
| 519 | nnoremap <expr> trigger g:Trigger() |
| 520 | feedkeys('trigger', "xt") |
| 521 | |
| 522 | delete('Xexport_that.vim') |
| 523 | delete('Ximport.vim') |
| 524 | nunmap trigger |
| 525 | enddef |
| 526 | |
| 527 | def Test_import_in_filetype() |
| 528 | # check that :import works when the buffer is locked |
| 529 | mkdir('ftplugin', 'p') |
| 530 | var export_lines =<< trim END |
| 531 | vim9script |
| 532 | export var That = 'yes' |
| 533 | END |
| 534 | writefile(export_lines, 'ftplugin/Xexport_ft.vim') |
| 535 | |
| 536 | var import_lines =<< trim END |
| 537 | vim9script |
| 538 | import './Xexport_ft.vim' as ft |
| 539 | assert_equal('yes', ft.That) |
| 540 | g:did_load_mytpe = 1 |
| 541 | END |
| 542 | writefile(import_lines, 'ftplugin/qf.vim') |
| 543 | |
| 544 | var save_rtp = &rtp |
| 545 | &rtp = getcwd() .. ',' .. &rtp |
| 546 | |
| 547 | filetype plugin on |
| 548 | copen |
| 549 | assert_equal(1, g:did_load_mytpe) |
| 550 | |
| 551 | quit! |
| 552 | delete('Xexport_ft.vim') |
| 553 | delete('ftplugin', 'rf') |
| 554 | &rtp = save_rtp |
| 555 | enddef |
| 556 | |
| 557 | def Test_use_import_in_mapping() |
| 558 | var lines =<< trim END |
| 559 | vim9script |
| 560 | export def Funcx() |
| 561 | g:result = 42 |
| 562 | enddef |
| 563 | END |
| 564 | writefile(lines, 'XsomeExport.vim') |
| 565 | lines =<< trim END |
| 566 | vim9script |
| 567 | import './XsomeExport.vim' as some |
| 568 | var Funcy = some.Funcx |
| 569 | nnoremap <F3> :call <sid>Funcy()<cr> |
| 570 | END |
| 571 | writefile(lines, 'Xmapscript.vim') |
| 572 | |
| 573 | source Xmapscript.vim |
| 574 | feedkeys("\<F3>", "xt") |
| 575 | assert_equal(42, g:result) |
| 576 | |
| 577 | unlet g:result |
| 578 | delete('XsomeExport.vim') |
| 579 | delete('Xmapscript.vim') |
| 580 | nunmap <F3> |
| 581 | enddef |
| 582 | |
Bram Moolenaar | 15d1635 | 2022-01-17 20:09:08 +0000 | [diff] [blame] | 583 | def Test_use_import_in_completion() |
| 584 | var lines =<< trim END |
| 585 | vim9script |
| 586 | export def Complete(..._): list<string> |
| 587 | return ['abcd'] |
| 588 | enddef |
| 589 | END |
| 590 | writefile(lines, 'Xscript.vim') |
| 591 | |
| 592 | lines =<< trim END |
| 593 | vim9script |
| 594 | import './Xscript.vim' |
| 595 | |
| 596 | command -nargs=1 -complete=customlist,Xscript.Complete Cmd echo 'ok' |
| 597 | feedkeys(":Cmd ab\<Tab>\<C-B>#\<CR>", 'xnt') |
| 598 | assert_equal('#Cmd abcd', @:) |
| 599 | END |
| 600 | CheckScriptSuccess(lines) |
| 601 | |
| 602 | delcommand Cmd |
| 603 | delete('Xscript.vim') |
| 604 | enddef |
| 605 | |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 606 | def Test_export_fails() |
| 607 | CheckScriptFailure(['export var some = 123'], 'E1042:') |
| 608 | CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:') |
| 609 | CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:') |
| 610 | |
| 611 | assert_fails('export something', 'E1043:') |
| 612 | enddef |
| 613 | |
| 614 | func Test_import_fails_without_script() |
| 615 | CheckRunVimInTerminal |
| 616 | |
| 617 | " call indirectly to avoid compilation error for missing functions |
| 618 | call Run_Test_import_fails_on_command_line() |
| 619 | endfunc |
| 620 | |
| 621 | def Run_Test_import_fails_on_command_line() |
| 622 | var export =<< trim END |
| 623 | vim9script |
| 624 | export def Foo(): number |
| 625 | return 0 |
| 626 | enddef |
| 627 | END |
| 628 | writefile(export, 'XexportCmd.vim') |
| 629 | |
| 630 | var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', { |
| 631 | rows: 6, wait_for_ruler: 0}) |
| 632 | WaitForAssert(() => assert_match('^E1094:', term_getline(buf, 5))) |
| 633 | |
| 634 | delete('XexportCmd.vim') |
| 635 | StopVimInTerminal(buf) |
| 636 | enddef |
| 637 | |
| 638 | def Test_vim9_reload_noclear() |
| 639 | var lines =<< trim END |
| 640 | vim9script |
| 641 | export var exported = 'thexport' |
| 642 | |
| 643 | export def TheFunc(x = 0) |
| 644 | enddef |
| 645 | END |
| 646 | writefile(lines, 'XExportReload') |
| 647 | lines =<< trim END |
| 648 | vim9script noclear |
| 649 | g:loadCount += 1 |
| 650 | var s:reloaded = 'init' |
| 651 | import './XExportReload' as exp |
| 652 | |
| 653 | def Again(): string |
| 654 | return 'again' |
| 655 | enddef |
| 656 | |
| 657 | exp.TheFunc() |
| 658 | |
| 659 | if exists('s:loaded') | finish | endif |
| 660 | var s:loaded = true |
| 661 | |
| 662 | var s:notReloaded = 'yes' |
| 663 | s:reloaded = 'first' |
| 664 | def g:Values(): list<string> |
| 665 | return [s:reloaded, s:notReloaded, Again(), Once(), exp.exported] |
| 666 | enddef |
| 667 | |
| 668 | def Once(): string |
| 669 | return 'once' |
| 670 | enddef |
| 671 | END |
| 672 | writefile(lines, 'XReloaded') |
| 673 | g:loadCount = 0 |
| 674 | source XReloaded |
| 675 | assert_equal(1, g:loadCount) |
| 676 | assert_equal(['first', 'yes', 'again', 'once', 'thexport'], g:Values()) |
| 677 | source XReloaded |
| 678 | assert_equal(2, g:loadCount) |
| 679 | assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values()) |
| 680 | source XReloaded |
| 681 | assert_equal(3, g:loadCount) |
| 682 | assert_equal(['init', 'yes', 'again', 'once', 'thexport'], g:Values()) |
| 683 | |
| 684 | delete('XReloaded') |
| 685 | delete('XExportReload') |
| 686 | delfunc g:Values |
| 687 | unlet g:loadCount |
| 688 | |
| 689 | lines =<< trim END |
| 690 | vim9script |
| 691 | def Inner() |
| 692 | enddef |
| 693 | END |
| 694 | lines->writefile('XreloadScript.vim') |
| 695 | source XreloadScript.vim |
| 696 | |
| 697 | lines =<< trim END |
| 698 | vim9script |
| 699 | def Outer() |
| 700 | def Inner() |
| 701 | enddef |
| 702 | enddef |
| 703 | defcompile |
| 704 | END |
| 705 | lines->writefile('XreloadScript.vim') |
| 706 | source XreloadScript.vim |
| 707 | |
| 708 | delete('XreloadScript.vim') |
| 709 | enddef |
| 710 | |
| 711 | def Test_vim9_reload_import() |
| 712 | var lines =<< trim END |
| 713 | vim9script |
| 714 | const var = '' |
| 715 | var valone = 1234 |
| 716 | def MyFunc(arg: string) |
| 717 | valone = 5678 |
| 718 | enddef |
| 719 | END |
| 720 | var morelines =<< trim END |
| 721 | var valtwo = 222 |
| 722 | export def GetValtwo(): number |
| 723 | return valtwo |
| 724 | enddef |
| 725 | END |
| 726 | writefile(lines + morelines, 'Xreload.vim') |
| 727 | source Xreload.vim |
| 728 | source Xreload.vim |
| 729 | source Xreload.vim |
| 730 | |
| 731 | # cannot declare a var twice |
| 732 | lines =<< trim END |
| 733 | vim9script |
| 734 | var valone = 1234 |
| 735 | var valone = 5678 |
| 736 | END |
| 737 | writefile(lines, 'Xreload.vim') |
| 738 | assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim') |
| 739 | |
| 740 | delete('Xreload.vim') |
| 741 | delete('Ximport.vim') |
| 742 | enddef |
| 743 | |
| 744 | " if a script is reloaded with a script-local variable that changed its type, a |
| 745 | " compiled function using that variable must fail. |
| 746 | def Test_script_reload_change_type() |
| 747 | var lines =<< trim END |
| 748 | vim9script noclear |
| 749 | var str = 'string' |
| 750 | def g:GetStr(): string |
| 751 | return str .. 'xxx' |
| 752 | enddef |
| 753 | END |
| 754 | writefile(lines, 'Xreload.vim') |
| 755 | source Xreload.vim |
| 756 | echo g:GetStr() |
| 757 | |
| 758 | lines =<< trim END |
| 759 | vim9script noclear |
| 760 | var str = 1234 |
| 761 | END |
| 762 | writefile(lines, 'Xreload.vim') |
| 763 | source Xreload.vim |
| 764 | assert_fails('echo g:GetStr()', 'E1150:') |
| 765 | |
| 766 | delfunc g:GetStr |
| 767 | delete('Xreload.vim') |
| 768 | enddef |
| 769 | |
| 770 | " Define CallFunc so that the test can be compiled |
| 771 | command CallFunc echo 'nop' |
| 772 | |
| 773 | def Test_script_reload_from_function() |
| 774 | var lines =<< trim END |
| 775 | vim9script |
| 776 | |
| 777 | if exists('g:loaded') |
| 778 | finish |
| 779 | endif |
| 780 | g:loaded = 1 |
| 781 | delcommand CallFunc |
| 782 | command CallFunc Func() |
| 783 | def Func() |
| 784 | so XreloadFunc.vim |
| 785 | g:didTheFunc = 1 |
| 786 | enddef |
| 787 | END |
| 788 | writefile(lines, 'XreloadFunc.vim') |
| 789 | source XreloadFunc.vim |
| 790 | CallFunc |
| 791 | assert_equal(1, g:didTheFunc) |
| 792 | |
| 793 | delete('XreloadFunc.vim') |
| 794 | delcommand CallFunc |
| 795 | unlet g:loaded |
| 796 | unlet g:didTheFunc |
| 797 | enddef |
| 798 | |
| 799 | def s:RetSome(): string |
| 800 | return 'some' |
| 801 | enddef |
| 802 | |
| 803 | " Not exported function that is referenced needs to be accessed by the |
| 804 | " script-local name. |
| 805 | def Test_vim9_funcref() |
| 806 | var sortlines =<< trim END |
| 807 | vim9script |
| 808 | def Compare(i1: number, i2: number): number |
| 809 | return i2 - i1 |
| 810 | enddef |
| 811 | |
| 812 | export def FastSort(): list<number> |
| 813 | return range(5)->sort(Compare) |
| 814 | enddef |
| 815 | |
| 816 | export def GetString(arg: string): string |
| 817 | return arg |
| 818 | enddef |
| 819 | END |
| 820 | writefile(sortlines, 'Xsort.vim') |
| 821 | |
| 822 | var lines =<< trim END |
| 823 | vim9script |
| 824 | import './Xsort.vim' |
| 825 | def Test() |
| 826 | g:result = Xsort.FastSort() |
| 827 | enddef |
| 828 | Test() |
Bram Moolenaar | 7c24dfd | 2022-01-08 17:03:55 +0000 | [diff] [blame] | 829 | END |
| 830 | writefile(lines, 'Xscript.vim') |
| 831 | source Xscript.vim |
| 832 | assert_equal([4, 3, 2, 1, 0], g:result) |
| 833 | unlet g:result |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 834 | |
Bram Moolenaar | 7c24dfd | 2022-01-08 17:03:55 +0000 | [diff] [blame] | 835 | lines =<< trim END |
| 836 | vim9script |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 837 | # using a function imported with "as" |
| 838 | import './Xsort.vim' as anAlias |
| 839 | assert_equal('yes', anAlias.GetString('yes')) |
| 840 | |
| 841 | # using the function from a compiled function |
| 842 | def TestMore(): string |
| 843 | var s = s:anAlias.GetString('foo') |
| 844 | return s .. anAlias.GetString('bar') |
| 845 | enddef |
| 846 | assert_equal('foobar', TestMore()) |
| 847 | |
| 848 | # error when using a function that isn't exported |
| 849 | assert_fails('anAlias.Compare(1, 2)', 'E1049:') |
| 850 | END |
| 851 | writefile(lines, 'Xscript.vim') |
| 852 | |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 853 | delete('Xsort.vim') |
| 854 | delete('Xscript.vim') |
| 855 | |
| 856 | var Funcref = function('s:RetSome') |
| 857 | assert_equal('some', Funcref()) |
| 858 | enddef |
| 859 | |
| 860 | " Check that when searching for "FilterFunc" it finds the import in the |
| 861 | " script where FastFilter() is called from, both as a string and as a direct |
| 862 | " function reference. |
| 863 | def Test_vim9_funcref_other_script() |
| 864 | var filterLines =<< trim END |
| 865 | vim9script |
| 866 | export def FilterFunc(idx: number, val: number): bool |
| 867 | return idx % 2 == 1 |
| 868 | enddef |
| 869 | export def FastFilter(): list<number> |
| 870 | return range(10)->filter('FilterFunc(v:key, v:val)') |
| 871 | enddef |
| 872 | export def FastFilterDirect(): list<number> |
| 873 | return range(10)->filter(FilterFunc) |
| 874 | enddef |
| 875 | END |
| 876 | writefile(filterLines, 'Xfilter.vim') |
| 877 | |
| 878 | var lines =<< trim END |
| 879 | vim9script |
| 880 | import './Xfilter.vim' as filter |
| 881 | def Test() |
| 882 | var x: list<number> = filter.FastFilter() |
| 883 | enddef |
| 884 | Test() |
| 885 | def TestDirect() |
| 886 | var x: list<number> = filter.FastFilterDirect() |
| 887 | enddef |
| 888 | TestDirect() |
| 889 | END |
| 890 | CheckScriptSuccess(lines) |
| 891 | delete('Xfilter.vim') |
| 892 | enddef |
| 893 | |
| 894 | def Test_import_absolute() |
| 895 | var import_lines = [ |
| 896 | 'vim9script', |
| 897 | 'import "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim" as abs', |
| 898 | 'def UseExported()', |
| 899 | ' g:imported_abs = abs.exported', |
| 900 | ' abs.exported = 8888', |
| 901 | ' g:imported_after = abs.exported', |
| 902 | 'enddef', |
| 903 | 'UseExported()', |
| 904 | 'g:import_disassembled = execute("disass UseExported")', |
| 905 | ] |
| 906 | writefile(import_lines, 'Ximport_abs.vim') |
| 907 | writefile(s:export_script_lines, 'Xexport_abs.vim') |
| 908 | |
| 909 | source Ximport_abs.vim |
| 910 | |
| 911 | assert_equal(9876, g:imported_abs) |
| 912 | assert_equal(8888, g:imported_after) |
| 913 | assert_match('<SNR>\d\+_UseExported\_s*' .. |
| 914 | 'g:imported_abs = abs.exported\_s*' .. |
| 915 | '0 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' .. |
| 916 | '1 STOREG g:imported_abs\_s*' .. |
| 917 | 'abs.exported = 8888\_s*' .. |
| 918 | '2 PUSHNR 8888\_s*' .. |
| 919 | '3 STORESCRIPT exported-2 in .*Xexport_abs.vim\_s*' .. |
| 920 | 'g:imported_after = abs.exported\_s*' .. |
| 921 | '4 LOADSCRIPT exported-2 from .*Xexport_abs.vim\_s*' .. |
| 922 | '5 STOREG g:imported_after', |
| 923 | g:import_disassembled) |
| 924 | |
| 925 | Undo_export_script_lines() |
| 926 | unlet g:imported_abs |
| 927 | unlet g:import_disassembled |
| 928 | |
| 929 | delete('Ximport_abs.vim') |
| 930 | delete('Xexport_abs.vim') |
| 931 | enddef |
| 932 | |
| 933 | def Test_import_rtp() |
| 934 | var import_lines = [ |
| 935 | 'vim9script', |
| 936 | 'import "Xexport_rtp.vim" as rtp', |
| 937 | 'g:imported_rtp = rtp.exported', |
| 938 | ] |
| 939 | writefile(import_lines, 'Ximport_rtp.vim') |
| 940 | mkdir('import', 'p') |
| 941 | writefile(s:export_script_lines, 'import/Xexport_rtp.vim') |
| 942 | |
| 943 | var save_rtp = &rtp |
| 944 | &rtp = getcwd() |
| 945 | source Ximport_rtp.vim |
| 946 | &rtp = save_rtp |
| 947 | |
| 948 | assert_equal(9876, g:imported_rtp) |
| 949 | |
| 950 | Undo_export_script_lines() |
| 951 | unlet g:imported_rtp |
| 952 | delete('Ximport_rtp.vim') |
| 953 | delete('import', 'rf') |
| 954 | enddef |
| 955 | |
| 956 | def Test_import_compile_error() |
| 957 | var export_lines = [ |
| 958 | 'vim9script', |
| 959 | 'export def ExpFunc(): string', |
| 960 | ' return notDefined', |
| 961 | 'enddef', |
| 962 | ] |
| 963 | writefile(export_lines, 'Xexported.vim') |
| 964 | |
| 965 | var import_lines = [ |
| 966 | 'vim9script', |
| 967 | 'import "./Xexported.vim" as expo', |
| 968 | 'def ImpFunc()', |
| 969 | ' echo expo.ExpFunc()', |
| 970 | 'enddef', |
| 971 | 'defcompile', |
| 972 | ] |
| 973 | writefile(import_lines, 'Ximport.vim') |
| 974 | |
| 975 | try |
| 976 | source Ximport.vim |
| 977 | catch /E1001/ |
| 978 | # Error should be before the Xexported.vim file. |
| 979 | assert_match('E1001: Variable not found: notDefined', v:exception) |
| 980 | assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint) |
| 981 | endtry |
| 982 | |
| 983 | delete('Xexported.vim') |
| 984 | delete('Ximport.vim') |
| 985 | enddef |
| 986 | |
| 987 | def Test_func_overrules_import_fails() |
| 988 | var export_lines =<< trim END |
| 989 | vim9script |
| 990 | export def Func() |
| 991 | echo 'imported' |
| 992 | enddef |
| 993 | END |
| 994 | writefile(export_lines, 'XexportedFunc.vim') |
| 995 | |
| 996 | var lines =<< trim END |
| 997 | vim9script |
| 998 | import './XexportedFunc.vim' as Func |
| 999 | def Func() |
| 1000 | echo 'local to function' |
| 1001 | enddef |
| 1002 | END |
| 1003 | CheckScriptFailure(lines, 'E1236:') |
| 1004 | |
| 1005 | lines =<< trim END |
| 1006 | vim9script |
| 1007 | import './XexportedFunc.vim' as Func |
| 1008 | def Outer() |
| 1009 | def Func() |
| 1010 | echo 'local to function' |
| 1011 | enddef |
| 1012 | enddef |
| 1013 | defcompile |
| 1014 | END |
| 1015 | CheckScriptFailure(lines, 'E1236:') |
| 1016 | |
| 1017 | delete('XexportedFunc.vim') |
| 1018 | enddef |
| 1019 | |
| 1020 | def Test_source_vim9_from_legacy() |
| 1021 | var vim9_lines =<< trim END |
| 1022 | vim9script |
| 1023 | var local = 'local' |
| 1024 | g:global = 'global' |
| 1025 | export var exported = 'exported' |
| 1026 | export def GetText(): string |
| 1027 | return 'text' |
| 1028 | enddef |
| 1029 | END |
| 1030 | writefile(vim9_lines, 'Xvim9_script.vim') |
| 1031 | |
| 1032 | var legacy_lines =<< trim END |
| 1033 | source Xvim9_script.vim |
| 1034 | |
| 1035 | call assert_false(exists('local')) |
| 1036 | call assert_false(exists('exported')) |
| 1037 | call assert_false(exists('s:exported')) |
| 1038 | call assert_equal('global', global) |
| 1039 | call assert_equal('global', g:global) |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 1040 | END |
| 1041 | writefile(legacy_lines, 'Xlegacy_script.vim') |
| 1042 | |
| 1043 | source Xlegacy_script.vim |
| 1044 | assert_equal('global', g:global) |
| 1045 | unlet g:global |
| 1046 | |
| 1047 | delete('Xlegacy_script.vim') |
| 1048 | delete('Xvim9_script.vim') |
| 1049 | enddef |
| 1050 | |
Bram Moolenaar | c43e623 | 2022-01-13 20:51:56 +0000 | [diff] [blame] | 1051 | def Test_import_vim9_from_legacy() |
| 1052 | var vim9_lines =<< trim END |
| 1053 | vim9script |
| 1054 | var local = 'local' |
| 1055 | g:global = 'global' |
| 1056 | export var exported = 'exported' |
| 1057 | export def GetText(): string |
| 1058 | return 'text' |
| 1059 | enddef |
| 1060 | END |
| 1061 | writefile(vim9_lines, 'Xvim9_export.vim') |
| 1062 | |
| 1063 | var legacy_lines =<< trim END |
| 1064 | import './Xvim9_export.vim' as vim9 |
| 1065 | |
| 1066 | call assert_false(exists('vim9')) |
| 1067 | call assert_false(exists('local')) |
| 1068 | call assert_false(exists('s:vim9.local')) |
| 1069 | call assert_equal('global', global) |
| 1070 | call assert_equal('global', g:global) |
| 1071 | call assert_false(exists('exported')) |
| 1072 | call assert_false(exists('s:exported')) |
| 1073 | call assert_false(exists('*GetText')) |
| 1074 | |
| 1075 | " imported symbol is script-local |
| 1076 | call assert_equal('exported', s:vim9.exported) |
| 1077 | call assert_equal('text', s:vim9.GetText()) |
| 1078 | END |
| 1079 | writefile(legacy_lines, 'Xlegacy_script.vim') |
| 1080 | |
| 1081 | source Xlegacy_script.vim |
| 1082 | assert_equal('global', g:global) |
| 1083 | unlet g:global |
| 1084 | |
| 1085 | delete('Xlegacy_script.vim') |
| 1086 | delete('Xvim9_export.vim') |
| 1087 | enddef |
| 1088 | |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 1089 | def Test_cmdline_win() |
| 1090 | # if the Vim syntax highlighting uses Vim9 constructs they can be used from |
| 1091 | # the command line window. |
| 1092 | mkdir('rtp/syntax', 'p') |
| 1093 | var export_lines =<< trim END |
| 1094 | vim9script |
| 1095 | export var That = 'yes' |
| 1096 | END |
| 1097 | writefile(export_lines, 'rtp/syntax/Xexport.vim') |
| 1098 | var import_lines =<< trim END |
| 1099 | vim9script |
| 1100 | import './Xexport.vim' as exp |
| 1101 | echo exp.That |
| 1102 | END |
| 1103 | writefile(import_lines, 'rtp/syntax/vim.vim') |
| 1104 | var save_rtp = &rtp |
| 1105 | &rtp = getcwd() .. '/rtp' .. ',' .. &rtp |
| 1106 | syntax on |
| 1107 | augroup CmdWin |
| 1108 | autocmd CmdwinEnter * g:got_there = 'yes' |
| 1109 | augroup END |
| 1110 | # this will open and also close the cmdline window |
| 1111 | feedkeys('q:', 'xt') |
| 1112 | assert_equal('yes', g:got_there) |
| 1113 | |
| 1114 | augroup CmdWin |
| 1115 | au! |
| 1116 | augroup END |
| 1117 | &rtp = save_rtp |
| 1118 | delete('rtp', 'rf') |
| 1119 | enddef |
| 1120 | |
| 1121 | def Test_import_gone_when_sourced_twice() |
| 1122 | var exportlines =<< trim END |
| 1123 | vim9script |
| 1124 | if exists('g:guard') |
| 1125 | finish |
| 1126 | endif |
| 1127 | g:guard = 1 |
| 1128 | export var name = 'someName' |
| 1129 | END |
| 1130 | writefile(exportlines, 'XexportScript.vim') |
| 1131 | |
| 1132 | var lines =<< trim END |
| 1133 | vim9script |
| 1134 | import './XexportScript.vim' as expo |
| 1135 | def g:GetName(): string |
| 1136 | return expo.name |
| 1137 | enddef |
| 1138 | END |
| 1139 | writefile(lines, 'XscriptImport.vim') |
| 1140 | so XscriptImport.vim |
| 1141 | assert_equal('someName', g:GetName()) |
| 1142 | |
| 1143 | so XexportScript.vim |
| 1144 | assert_fails('call g:GetName()', 'E1149:') |
| 1145 | |
| 1146 | delfunc g:GetName |
| 1147 | delete('XexportScript.vim') |
| 1148 | delete('XscriptImport.vim') |
| 1149 | unlet g:guard |
| 1150 | enddef |
| 1151 | |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1152 | " test using an auto-loaded function and variable |
Bram Moolenaar | 0e3e7ba | 2022-01-13 20:18:56 +0000 | [diff] [blame] | 1153 | def Test_vim9_autoload_full_name() |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1154 | var lines =<< trim END |
| 1155 | vim9script |
| 1156 | def some#gettest(): string |
| 1157 | return 'test' |
| 1158 | enddef |
| 1159 | g:some#name = 'name' |
| 1160 | g:some#dict = {key: 'value'} |
| 1161 | |
| 1162 | def some#varargs(a1: string, ...l: list<string>): string |
| 1163 | return a1 .. l[0] .. l[1] |
| 1164 | enddef |
| 1165 | END |
| 1166 | |
| 1167 | mkdir('Xdir/autoload', 'p') |
| 1168 | writefile(lines, 'Xdir/autoload/some.vim') |
| 1169 | var save_rtp = &rtp |
| 1170 | exe 'set rtp^=' .. getcwd() .. '/Xdir' |
| 1171 | |
| 1172 | assert_equal('test', g:some#gettest()) |
| 1173 | assert_equal('name', g:some#name) |
| 1174 | assert_equal('value', g:some#dict.key) |
| 1175 | g:some#other = 'other' |
| 1176 | assert_equal('other', g:some#other) |
| 1177 | |
| 1178 | assert_equal('abc', some#varargs('a', 'b', 'c')) |
| 1179 | |
| 1180 | # upper case script name works |
| 1181 | lines =<< trim END |
| 1182 | vim9script |
| 1183 | def Other#getOther(): string |
| 1184 | return 'other' |
| 1185 | enddef |
| 1186 | END |
| 1187 | writefile(lines, 'Xdir/autoload/Other.vim') |
| 1188 | assert_equal('other', g:Other#getOther()) |
| 1189 | |
| 1190 | delete('Xdir', 'rf') |
| 1191 | &rtp = save_rtp |
| 1192 | enddef |
| 1193 | |
| 1194 | def Test_vim9script_autoload() |
| 1195 | mkdir('Xdir/autoload', 'p') |
| 1196 | var save_rtp = &rtp |
| 1197 | exe 'set rtp^=' .. getcwd() .. '/Xdir' |
| 1198 | |
Bram Moolenaar | fd218c8 | 2022-01-18 16:26:24 +0000 | [diff] [blame] | 1199 | # when the path has "/autoload/" prefix is not needed |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1200 | var lines =<< trim END |
Bram Moolenaar | fd218c8 | 2022-01-18 16:26:24 +0000 | [diff] [blame] | 1201 | vim9script |
Bram Moolenaar | 17d36cb | 2022-01-12 11:46:40 +0000 | [diff] [blame] | 1202 | g:prefixed_loaded += 1 |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1203 | |
| 1204 | export def Gettest(): string |
| 1205 | return 'test' |
| 1206 | enddef |
| 1207 | |
Bram Moolenaar | 0e3e7ba | 2022-01-13 20:18:56 +0000 | [diff] [blame] | 1208 | export var name = 'name' |
| 1209 | |
| 1210 | export func GetFunc() |
| 1211 | return Gettest() .. 'more' .. s:name |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1212 | endfunc |
| 1213 | |
Bram Moolenaar | 0e3e7ba | 2022-01-13 20:18:56 +0000 | [diff] [blame] | 1214 | export def GetDef(): string |
| 1215 | return Gettest() .. 'more' .. name |
| 1216 | enddef |
| 1217 | |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1218 | export final fname = 'final' |
| 1219 | export const cname = 'const' |
| 1220 | END |
| 1221 | writefile(lines, 'Xdir/autoload/prefixed.vim') |
| 1222 | |
Bram Moolenaar | 17d36cb | 2022-01-12 11:46:40 +0000 | [diff] [blame] | 1223 | g:prefixed_loaded = 0 |
| 1224 | g:expected_loaded = 0 |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1225 | lines =<< trim END |
| 1226 | vim9script |
| 1227 | import autoload 'prefixed.vim' |
Bram Moolenaar | 17d36cb | 2022-01-12 11:46:40 +0000 | [diff] [blame] | 1228 | assert_equal(g:expected_loaded, g:prefixed_loaded) |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1229 | assert_equal('test', prefixed.Gettest()) |
Bram Moolenaar | 17d36cb | 2022-01-12 11:46:40 +0000 | [diff] [blame] | 1230 | assert_equal(1, g:prefixed_loaded) |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1231 | |
Bram Moolenaar | 0e3e7ba | 2022-01-13 20:18:56 +0000 | [diff] [blame] | 1232 | assert_equal('testmorename', prefixed.GetFunc()) |
| 1233 | assert_equal('testmorename', prefixed.GetDef()) |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1234 | assert_equal('name', prefixed.name) |
| 1235 | assert_equal('final', prefixed.fname) |
| 1236 | assert_equal('const', prefixed.cname) |
| 1237 | END |
| 1238 | CheckScriptSuccess(lines) |
Bram Moolenaar | 17d36cb | 2022-01-12 11:46:40 +0000 | [diff] [blame] | 1239 | # can source it again, autoload script not loaded again |
| 1240 | g:expected_loaded = 1 |
| 1241 | CheckScriptSuccess(lines) |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1242 | |
| 1243 | # can also get the items by autoload name |
| 1244 | lines =<< trim END |
| 1245 | call assert_equal('test', prefixed#Gettest()) |
Bram Moolenaar | 0e3e7ba | 2022-01-13 20:18:56 +0000 | [diff] [blame] | 1246 | call assert_equal('testmorename', prefixed#GetFunc()) |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1247 | call assert_equal('name', prefixed#name) |
| 1248 | call assert_equal('final', prefixed#fname) |
| 1249 | call assert_equal('const', prefixed#cname) |
| 1250 | END |
| 1251 | CheckScriptSuccess(lines) |
| 1252 | |
Bram Moolenaar | f111cdf | 2022-01-12 12:48:17 +0000 | [diff] [blame] | 1253 | unlet g:prefixed_loaded |
| 1254 | unlet g:expected_loaded |
| 1255 | delete('Xdir', 'rf') |
| 1256 | &rtp = save_rtp |
| 1257 | enddef |
| 1258 | |
Bram Moolenaar | d02dce2 | 2022-01-18 17:43:04 +0000 | [diff] [blame] | 1259 | def Test_import_autoload_not_exported() |
| 1260 | mkdir('Xdir/autoload', 'p') |
| 1261 | var save_rtp = &rtp |
| 1262 | exe 'set rtp^=' .. getcwd() .. '/Xdir' |
| 1263 | |
| 1264 | # error when using an item that is not exported from an autoload script |
| 1265 | var exportLines =<< trim END |
| 1266 | vim9script |
| 1267 | var notExported = 123 |
| 1268 | def NotExport() |
| 1269 | echo 'nop' |
| 1270 | enddef |
| 1271 | END |
| 1272 | writefile(exportLines, 'Xdir/autoload/notExport1.vim') |
| 1273 | |
| 1274 | var lines =<< trim END |
| 1275 | vim9script |
| 1276 | import autoload 'notExport1.vim' |
| 1277 | echo notExport1.notFound |
| 1278 | END |
| 1279 | CheckScriptFailure(lines, 'E1048: Item not found in script: notFound') |
| 1280 | |
| 1281 | lines =<< trim END |
| 1282 | vim9script |
| 1283 | import autoload 'notExport1.vim' |
| 1284 | echo notExport1.notExported |
| 1285 | END |
| 1286 | CheckScriptFailure(lines, 'E1049: Item not exported in script: notExported') |
| 1287 | |
| 1288 | lines =<< trim END |
| 1289 | vim9script |
| 1290 | import autoload 'notExport1.vim' |
| 1291 | echo notExport1.NotFunc() |
| 1292 | END |
| 1293 | CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc') |
| 1294 | |
| 1295 | lines =<< trim END |
| 1296 | vim9script |
| 1297 | import autoload 'notExport1.vim' |
| 1298 | echo notExport1.NotExport() |
| 1299 | END |
| 1300 | CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport') |
| 1301 | |
| 1302 | lines =<< trim END |
| 1303 | vim9script |
| 1304 | import autoload 'notExport1.vim' |
| 1305 | echo 'text'->notExport1.NotFunc() |
| 1306 | END |
| 1307 | CheckScriptFailure(lines, 'E1048: Item not found in script: NotFunc') |
| 1308 | |
| 1309 | lines =<< trim END |
| 1310 | vim9script |
| 1311 | import autoload 'notExport1.vim' |
| 1312 | echo 'text'->notExport1.NotExport() |
| 1313 | END |
| 1314 | CheckScriptFailure(lines, 'E1049: Item not exported in script: NotExport') |
| 1315 | |
| 1316 | # using a :def function we use a different autoload script every time so that |
| 1317 | # the function is compiled without the script loaded |
| 1318 | writefile(exportLines, 'Xdir/autoload/notExport2.vim') |
| 1319 | lines =<< trim END |
| 1320 | vim9script |
| 1321 | import autoload 'notExport2.vim' |
| 1322 | def Testit() |
| 1323 | echo notExport2.notFound |
| 1324 | enddef |
| 1325 | Testit() |
| 1326 | END |
| 1327 | CheckScriptFailure(lines, 'E1048: Item not found in script: notExport2#notFound') |
| 1328 | |
| 1329 | writefile(exportLines, 'Xdir/autoload/notExport3.vim') |
| 1330 | lines =<< trim END |
| 1331 | vim9script |
| 1332 | import autoload 'notExport3.vim' |
| 1333 | def Testit() |
| 1334 | echo notExport3.notExported |
| 1335 | enddef |
| 1336 | Testit() |
| 1337 | END |
| 1338 | # don't get E1049 because it is too complicated to figure out |
| 1339 | CheckScriptFailure(lines, 'E1048: Item not found in script: notExport3#notExported') |
| 1340 | |
| 1341 | writefile(exportLines, 'Xdir/autoload/notExport4.vim') |
| 1342 | lines =<< trim END |
| 1343 | vim9script |
| 1344 | import autoload 'notExport4.vim' |
| 1345 | def Testit() |
| 1346 | echo notExport4.NotFunc() |
| 1347 | enddef |
| 1348 | Testit() |
| 1349 | END |
| 1350 | CheckScriptFailure(lines, 'E117: Unknown function: notExport4#NotFunc') |
| 1351 | |
| 1352 | writefile(exportLines, 'Xdir/autoload/notExport5.vim') |
| 1353 | lines =<< trim END |
| 1354 | vim9script |
| 1355 | import autoload 'notExport5.vim' |
| 1356 | def Testit() |
| 1357 | echo notExport5.NotExport() |
| 1358 | enddef |
| 1359 | Testit() |
| 1360 | END |
| 1361 | CheckScriptFailure(lines, 'E117: Unknown function: notExport5#NotExport') |
| 1362 | |
| 1363 | writefile(exportLines, 'Xdir/autoload/notExport6.vim') |
| 1364 | lines =<< trim END |
| 1365 | vim9script |
| 1366 | import autoload 'notExport6.vim' |
| 1367 | def Testit() |
| 1368 | echo 'text'->notExport6.NotFunc() |
| 1369 | enddef |
| 1370 | Testit() |
| 1371 | END |
| 1372 | CheckScriptFailure(lines, 'E117: Unknown function: notExport6#NotFunc') |
| 1373 | |
| 1374 | writefile(exportLines, 'Xdir/autoload/notExport7.vim') |
| 1375 | lines =<< trim END |
| 1376 | vim9script |
| 1377 | import autoload 'notExport7.vim' |
| 1378 | def Testit() |
| 1379 | echo 'text'->notExport7.NotExport() |
| 1380 | enddef |
| 1381 | Testit() |
| 1382 | END |
| 1383 | CheckScriptFailure(lines, 'E117: Unknown function: notExport7#NotExport') |
| 1384 | |
| 1385 | delete('Xdir', 'rf') |
| 1386 | &rtp = save_rtp |
| 1387 | enddef |
| 1388 | |
Bram Moolenaar | f111cdf | 2022-01-12 12:48:17 +0000 | [diff] [blame] | 1389 | def Test_vim9script_autoload_call() |
| 1390 | mkdir('Xdir/autoload', 'p') |
| 1391 | var save_rtp = &rtp |
| 1392 | exe 'set rtp^=' .. getcwd() .. '/Xdir' |
| 1393 | |
| 1394 | var lines =<< trim END |
Bram Moolenaar | fd218c8 | 2022-01-18 16:26:24 +0000 | [diff] [blame] | 1395 | vim9script |
Bram Moolenaar | f111cdf | 2022-01-12 12:48:17 +0000 | [diff] [blame] | 1396 | |
Bram Moolenaar | cbbc48f | 2022-01-18 12:58:28 +0000 | [diff] [blame] | 1397 | export def RetArg(arg: string): string |
| 1398 | return arg |
| 1399 | enddef |
| 1400 | |
Bram Moolenaar | f111cdf | 2022-01-12 12:48:17 +0000 | [diff] [blame] | 1401 | export def Getother() |
| 1402 | g:result = 'other' |
| 1403 | enddef |
| 1404 | END |
Bram Moolenaar | 5d98269 | 2022-01-12 15:15:27 +0000 | [diff] [blame] | 1405 | writefile(lines, 'Xdir/autoload/another.vim') |
Bram Moolenaar | f111cdf | 2022-01-12 12:48:17 +0000 | [diff] [blame] | 1406 | |
| 1407 | lines =<< trim END |
| 1408 | vim9script |
Bram Moolenaar | 5d98269 | 2022-01-12 15:15:27 +0000 | [diff] [blame] | 1409 | import autoload 'another.vim' |
Bram Moolenaar | cbbc48f | 2022-01-18 12:58:28 +0000 | [diff] [blame] | 1410 | |
| 1411 | # compile this before 'another.vim' is loaded |
| 1412 | def CallAnother() |
| 1413 | assert_equal('foo', 'foo'->another.RetArg()) |
| 1414 | enddef |
| 1415 | CallAnother() |
| 1416 | |
Bram Moolenaar | 5d98269 | 2022-01-12 15:15:27 +0000 | [diff] [blame] | 1417 | call another.Getother() |
Bram Moolenaar | f111cdf | 2022-01-12 12:48:17 +0000 | [diff] [blame] | 1418 | assert_equal('other', g:result) |
| 1419 | END |
| 1420 | CheckScriptSuccess(lines) |
| 1421 | |
| 1422 | unlet g:result |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1423 | delete('Xdir', 'rf') |
| 1424 | &rtp = save_rtp |
| 1425 | enddef |
| 1426 | |
Bram Moolenaar | d041f42 | 2022-01-12 19:54:00 +0000 | [diff] [blame] | 1427 | def Test_import_autoload_postponed() |
| 1428 | mkdir('Xdir/autoload', 'p') |
| 1429 | var save_rtp = &rtp |
| 1430 | exe 'set rtp^=' .. getcwd() .. '/Xdir' |
| 1431 | |
| 1432 | var lines =<< trim END |
Bram Moolenaar | fd218c8 | 2022-01-18 16:26:24 +0000 | [diff] [blame] | 1433 | vim9script |
Bram Moolenaar | d041f42 | 2022-01-12 19:54:00 +0000 | [diff] [blame] | 1434 | |
| 1435 | g:loaded_postponed = 'true' |
| 1436 | export var variable = 'bla' |
| 1437 | export def Function(): string |
| 1438 | return 'bla' |
| 1439 | enddef |
| 1440 | END |
| 1441 | writefile(lines, 'Xdir/autoload/postponed.vim') |
| 1442 | |
| 1443 | lines =<< trim END |
| 1444 | vim9script |
| 1445 | |
| 1446 | import autoload 'postponed.vim' |
| 1447 | def Tryit() |
| 1448 | echo postponed.variable |
| 1449 | echo postponed.Function() |
| 1450 | enddef |
| 1451 | defcompile |
| 1452 | END |
| 1453 | CheckScriptSuccess(lines) |
| 1454 | assert_false(exists('g:loaded_postponed')) |
| 1455 | CheckScriptSuccess(lines + ['Tryit()']) |
| 1456 | assert_equal('true', g:loaded_postponed) |
| 1457 | |
| 1458 | unlet g:loaded_postponed |
| 1459 | delete('Xdir', 'rf') |
| 1460 | &rtp = save_rtp |
| 1461 | enddef |
| 1462 | |
Bram Moolenaar | 3e4fa3d | 2022-01-13 22:05:09 +0000 | [diff] [blame] | 1463 | def Test_import_autoload_override() |
| 1464 | mkdir('Xdir/autoload', 'p') |
| 1465 | var save_rtp = &rtp |
| 1466 | exe 'set rtp^=' .. getcwd() .. '/Xdir' |
| 1467 | test_override('autoload', 1) |
| 1468 | |
| 1469 | var lines =<< trim END |
Bram Moolenaar | fd218c8 | 2022-01-18 16:26:24 +0000 | [diff] [blame] | 1470 | vim9script |
Bram Moolenaar | 3e4fa3d | 2022-01-13 22:05:09 +0000 | [diff] [blame] | 1471 | |
| 1472 | g:loaded_override = 'true' |
| 1473 | export var variable = 'bla' |
| 1474 | export def Function(): string |
| 1475 | return 'bla' |
| 1476 | enddef |
| 1477 | END |
| 1478 | writefile(lines, 'Xdir/autoload/override.vim') |
| 1479 | |
| 1480 | lines =<< trim END |
| 1481 | vim9script |
| 1482 | |
| 1483 | import autoload 'override.vim' |
| 1484 | assert_equal('true', g:loaded_override) |
| 1485 | |
| 1486 | def Tryit() |
| 1487 | echo override.doesNotExist |
| 1488 | enddef |
| 1489 | defcompile |
| 1490 | END |
| 1491 | CheckScriptFailure(lines, 'E1048: Item not found in script: doesNotExist', 1) |
| 1492 | |
| 1493 | test_override('autoload', 0) |
| 1494 | unlet g:loaded_override |
| 1495 | delete('Xdir', 'rf') |
| 1496 | &rtp = save_rtp |
| 1497 | enddef |
| 1498 | |
Bram Moolenaar | 19db9e6 | 2022-01-11 11:58:19 +0000 | [diff] [blame] | 1499 | def Test_autoload_mapping() |
| 1500 | mkdir('Xdir/autoload', 'p') |
| 1501 | var save_rtp = &rtp |
| 1502 | exe 'set rtp^=' .. getcwd() .. '/Xdir' |
| 1503 | |
| 1504 | var lines =<< trim END |
Bram Moolenaar | fd218c8 | 2022-01-18 16:26:24 +0000 | [diff] [blame] | 1505 | vim9script |
Bram Moolenaar | 19db9e6 | 2022-01-11 11:58:19 +0000 | [diff] [blame] | 1506 | |
| 1507 | g:toggle_loaded = 'yes' |
| 1508 | |
| 1509 | export def Toggle(): string |
| 1510 | return ":g:toggle_called = 'yes'\<CR>" |
| 1511 | enddef |
Bram Moolenaar | e32c3c4 | 2022-01-15 18:26:04 +0000 | [diff] [blame] | 1512 | export def Doit() |
| 1513 | g:doit_called = 'yes' |
| 1514 | enddef |
Bram Moolenaar | 19db9e6 | 2022-01-11 11:58:19 +0000 | [diff] [blame] | 1515 | END |
| 1516 | writefile(lines, 'Xdir/autoload/toggle.vim') |
| 1517 | |
| 1518 | lines =<< trim END |
| 1519 | vim9script |
| 1520 | |
| 1521 | import autoload 'toggle.vim' |
| 1522 | |
| 1523 | nnoremap <silent> <expr> tt toggle.Toggle() |
Bram Moolenaar | e32c3c4 | 2022-01-15 18:26:04 +0000 | [diff] [blame] | 1524 | nnoremap <silent> xx <ScriptCmd>toggle.Doit()<CR> |
| 1525 | nnoremap <silent> yy <Cmd>toggle.Doit()<CR> |
Bram Moolenaar | 19db9e6 | 2022-01-11 11:58:19 +0000 | [diff] [blame] | 1526 | END |
| 1527 | CheckScriptSuccess(lines) |
| 1528 | assert_false(exists("g:toggle_loaded")) |
| 1529 | assert_false(exists("g:toggle_called")) |
Bram Moolenaar | 6079da7 | 2022-01-18 14:16:59 +0000 | [diff] [blame] | 1530 | assert_match('\d A: \f*[/\\]toggle.vim', execute('scriptnames')) |
Bram Moolenaar | 19db9e6 | 2022-01-11 11:58:19 +0000 | [diff] [blame] | 1531 | |
| 1532 | feedkeys("tt", 'xt') |
| 1533 | assert_equal('yes', g:toggle_loaded) |
| 1534 | assert_equal('yes', g:toggle_called) |
Bram Moolenaar | 6079da7 | 2022-01-18 14:16:59 +0000 | [diff] [blame] | 1535 | assert_match('\d: \f*[/\\]toggle.vim', execute('scriptnames')) |
Bram Moolenaar | 19db9e6 | 2022-01-11 11:58:19 +0000 | [diff] [blame] | 1536 | |
Bram Moolenaar | e32c3c4 | 2022-01-15 18:26:04 +0000 | [diff] [blame] | 1537 | feedkeys("xx", 'xt') |
| 1538 | assert_equal('yes', g:doit_called) |
| 1539 | |
| 1540 | assert_fails('call feedkeys("yy", "xt")', 'E121: Undefined variable: toggle') |
| 1541 | |
Bram Moolenaar | 19db9e6 | 2022-01-11 11:58:19 +0000 | [diff] [blame] | 1542 | nunmap tt |
Bram Moolenaar | e32c3c4 | 2022-01-15 18:26:04 +0000 | [diff] [blame] | 1543 | nunmap xx |
| 1544 | nunmap yy |
Bram Moolenaar | 19db9e6 | 2022-01-11 11:58:19 +0000 | [diff] [blame] | 1545 | unlet g:toggle_loaded |
| 1546 | unlet g:toggle_called |
| 1547 | delete('Xdir', 'rf') |
| 1548 | &rtp = save_rtp |
| 1549 | enddef |
| 1550 | |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1551 | def Test_vim9script_autoload_fails() |
| 1552 | var lines =<< trim END |
| 1553 | vim9script autoload |
| 1554 | var n = 0 |
| 1555 | END |
Bram Moolenaar | fd218c8 | 2022-01-18 16:26:24 +0000 | [diff] [blame] | 1556 | CheckScriptFailure(lines, 'E475: Invalid argument: autoload') |
| 1557 | |
| 1558 | lines =<< trim END |
| 1559 | vim9script noclear noclear |
| 1560 | var n = 0 |
| 1561 | END |
| 1562 | CheckScriptFailure(lines, 'E983: Duplicate argument: noclear') |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1563 | enddef |
| 1564 | |
| 1565 | def Test_import_autoload_fails() |
| 1566 | var lines =<< trim END |
| 1567 | vim9script |
| 1568 | import autoload autoload 'prefixed.vim' |
| 1569 | END |
| 1570 | CheckScriptFailure(lines, 'E121: Undefined variable: autoload') |
| 1571 | |
| 1572 | lines =<< trim END |
| 1573 | vim9script |
Bram Moolenaar | 1836d61 | 2022-01-18 13:14:47 +0000 | [diff] [blame] | 1574 | import autoload './doesNotExist.vim' |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1575 | END |
| 1576 | CheckScriptFailure(lines, 'E1264:') |
Bram Moolenaar | 1836d61 | 2022-01-18 13:14:47 +0000 | [diff] [blame] | 1577 | |
| 1578 | lines =<< trim END |
| 1579 | vim9script |
| 1580 | import autoload '/dir/doesNotExist.vim' |
| 1581 | END |
| 1582 | CheckScriptFailure(lines, 'E1264:') |
| 1583 | |
| 1584 | lines =<< trim END |
| 1585 | vim9script |
| 1586 | import autoload 'doesNotExist.vim' |
| 1587 | END |
| 1588 | CheckScriptFailure(lines, 'E1053: Could not import "doesNotExist.vim"') |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1589 | enddef |
| 1590 | |
| 1591 | " test disassembling an auto-loaded function starting with "debug" |
| 1592 | def Test_vim9_autoload_disass() |
| 1593 | mkdir('Xdir/autoload', 'p') |
| 1594 | var save_rtp = &rtp |
| 1595 | exe 'set rtp^=' .. getcwd() .. '/Xdir' |
| 1596 | |
| 1597 | var lines =<< trim END |
| 1598 | vim9script |
| 1599 | def debugit#test(): string |
| 1600 | return 'debug' |
| 1601 | enddef |
| 1602 | END |
| 1603 | writefile(lines, 'Xdir/autoload/debugit.vim') |
| 1604 | |
| 1605 | lines =<< trim END |
| 1606 | vim9script |
| 1607 | def profileit#test(): string |
| 1608 | return 'profile' |
| 1609 | enddef |
| 1610 | END |
| 1611 | writefile(lines, 'Xdir/autoload/profileit.vim') |
| 1612 | |
| 1613 | lines =<< trim END |
| 1614 | vim9script |
| 1615 | assert_equal('debug', debugit#test()) |
| 1616 | disass debugit#test |
| 1617 | assert_equal('profile', profileit#test()) |
| 1618 | disass profileit#test |
| 1619 | END |
| 1620 | CheckScriptSuccess(lines) |
| 1621 | |
| 1622 | delete('Xdir', 'rf') |
| 1623 | &rtp = save_rtp |
| 1624 | enddef |
| 1625 | |
| 1626 | " test using a vim9script that is auto-loaded from an autocmd |
| 1627 | def Test_vim9_aucmd_autoload() |
| 1628 | var lines =<< trim END |
| 1629 | vim9script |
| 1630 | def foo#test() |
| 1631 | echomsg getreg('"') |
| 1632 | enddef |
| 1633 | END |
| 1634 | |
| 1635 | mkdir('Xdir/autoload', 'p') |
| 1636 | writefile(lines, 'Xdir/autoload/foo.vim') |
| 1637 | var save_rtp = &rtp |
| 1638 | exe 'set rtp^=' .. getcwd() .. '/Xdir' |
| 1639 | augroup test |
| 1640 | autocmd TextYankPost * call foo#test() |
| 1641 | augroup END |
| 1642 | |
| 1643 | normal Y |
| 1644 | |
| 1645 | augroup test |
| 1646 | autocmd! |
| 1647 | augroup END |
| 1648 | delete('Xdir', 'rf') |
| 1649 | &rtp = save_rtp |
| 1650 | enddef |
| 1651 | |
Bram Moolenaar | 3049fcf | 2022-01-13 19:25:50 +0000 | [diff] [blame] | 1652 | " test using a autoloaded file that is case sensitive |
| 1653 | def Test_vim9_autoload_case_sensitive() |
| 1654 | var lines =<< trim END |
Bram Moolenaar | fd218c8 | 2022-01-18 16:26:24 +0000 | [diff] [blame] | 1655 | vim9script |
Bram Moolenaar | 3049fcf | 2022-01-13 19:25:50 +0000 | [diff] [blame] | 1656 | export def CaseSensitive(): string |
| 1657 | return 'done' |
| 1658 | enddef |
| 1659 | END |
| 1660 | |
| 1661 | mkdir('Xdir/autoload', 'p') |
| 1662 | writefile(lines, 'Xdir/autoload/CaseSensitive.vim') |
| 1663 | var save_rtp = &rtp |
| 1664 | exe 'set rtp^=' .. getcwd() .. '/Xdir' |
| 1665 | |
| 1666 | lines =<< trim END |
| 1667 | vim9script |
| 1668 | import autoload 'CaseSensitive.vim' |
| 1669 | assert_equal('done', CaseSensitive.CaseSensitive()) |
| 1670 | END |
| 1671 | CheckScriptSuccess(lines) |
| 1672 | |
Bram Moolenaar | bfac409 | 2022-01-16 11:12:12 +0000 | [diff] [blame] | 1673 | if !has('fname_case') |
| 1674 | lines =<< trim END |
| 1675 | vim9script |
| 1676 | import autoload 'CaseSensitive.vim' |
| 1677 | import autoload 'casesensitive.vim' |
| 1678 | END |
| 1679 | CheckScriptFailure(lines, 'E1262:') |
| 1680 | endif |
| 1681 | |
Bram Moolenaar | 3049fcf | 2022-01-13 19:25:50 +0000 | [diff] [blame] | 1682 | delete('Xdir', 'rf') |
| 1683 | &rtp = save_rtp |
| 1684 | enddef |
| 1685 | |
Bram Moolenaar | 160aa86 | 2022-01-10 21:29:57 +0000 | [diff] [blame] | 1686 | " This was causing a crash because suppress_errthrow wasn't reset. |
| 1687 | def Test_vim9_autoload_error() |
| 1688 | var lines =<< trim END |
| 1689 | vim9script |
| 1690 | def crash#func() |
| 1691 | try |
| 1692 | for x in List() |
| 1693 | endfor |
| 1694 | catch |
| 1695 | endtry |
| 1696 | g:ok = true |
| 1697 | enddef |
| 1698 | fu List() |
| 1699 | invalid |
| 1700 | endfu |
| 1701 | try |
| 1702 | alsoinvalid |
| 1703 | catch /wontmatch/ |
| 1704 | endtry |
| 1705 | END |
| 1706 | call mkdir('Xruntime/autoload', 'p') |
| 1707 | call writefile(lines, 'Xruntime/autoload/crash.vim') |
| 1708 | |
| 1709 | # run in a separate Vim to avoid the side effects of assert_fails() |
| 1710 | lines =<< trim END |
| 1711 | exe 'set rtp^=' .. getcwd() .. '/Xruntime' |
| 1712 | call crash#func() |
| 1713 | call writefile(['ok'], 'Xdidit') |
| 1714 | qall! |
| 1715 | END |
| 1716 | writefile(lines, 'Xscript') |
| 1717 | RunVim([], [], '-S Xscript') |
| 1718 | assert_equal(['ok'], readfile('Xdidit')) |
| 1719 | |
| 1720 | delete('Xdidit') |
| 1721 | delete('Xscript') |
| 1722 | delete('Xruntime', 'rf') |
| 1723 | |
| 1724 | lines =<< trim END |
| 1725 | vim9script |
| 1726 | var foo#bar = 'asdf' |
| 1727 | END |
| 1728 | CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2) |
| 1729 | enddef |
| 1730 | |
Bram Moolenaar | d844862 | 2022-01-07 21:39:52 +0000 | [diff] [blame] | 1731 | |
| 1732 | " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker |