1" Test various aspects of the Vim9 script language. 2 3source check.vim 4source term_util.vim 5source view_util.vim 6source vim9.vim 7 8def Test_syntax() 9 let var = 234 10 let other: list<string> = ['asdf'] 11enddef 12 13let s:appendToMe = 'xxx' 14let s:addToMe = 111 15let g:existing = 'yes' 16let g:inc_counter = 1 17let $SOME_ENV_VAR = 'some' 18let g:alist = [7] 19let g:astring = 'text' 20 21def Test_assignment() 22 let bool1: bool = true 23 assert_equal(v:true, bool1) 24 let bool2: bool = false 25 assert_equal(v:false, bool2) 26 27 call CheckDefFailure(['let x:string'], 'E1069:') 28 call CheckDefFailure(['let x:string = "x"'], 'E1069:') 29 call CheckDefFailure(['let a:string = "x"'], 'E1069:') 30 31 let a: number = 6 32 assert_equal(6, a) 33 34 if has('channel') 35 let chan1: channel 36 let job1: job 37 let job2: job = job_start('willfail') 38 endif 39 if has('float') 40 let float1: float = 3.4 41 endif 42 let Funky1: func 43 let Funky2: func = function('len') 44 let Party2: func = funcref('g:Test_syntax') 45 46 g:newvar = 'new' 47 assert_equal('new', g:newvar) 48 49 assert_equal('yes', g:existing) 50 g:existing = 'no' 51 assert_equal('no', g:existing) 52 53 v:char = 'abc' 54 assert_equal('abc', v:char) 55 56 $ENVVAR = 'foobar' 57 assert_equal('foobar', $ENVVAR) 58 $ENVVAR = '' 59 60 s:appendToMe ..= 'yyy' 61 assert_equal('xxxyyy', s:appendToMe) 62 s:addToMe += 222 63 assert_equal(333, s:addToMe) 64 s:newVar = 'new' 65 assert_equal('new', s:newVar) 66 67 set ts=7 68 &ts += 1 69 assert_equal(8, &ts) 70 &ts -= 3 71 assert_equal(5, &ts) 72 &ts *= 2 73 assert_equal(10, &ts) 74 &ts /= 3 75 assert_equal(3, &ts) 76 set ts=10 77 &ts %= 4 78 assert_equal(2, &ts) 79 call CheckDefFailure(['¬ex += 3'], 'E113:') 80 call CheckDefFailure(['&ts ..= "xxx"'], 'E1019:') 81 call CheckDefFailure(['&ts = [7]'], 'E1013:') 82 call CheckDefExecFailure(['&ts = g:alist'], 'E1029: Expected number but got list') 83 call CheckDefFailure(['&ts = "xx"'], 'E1013:') 84 call CheckDefExecFailure(['&ts = g:astring'], 'E1029: Expected number but got string') 85 call CheckDefFailure(['&path += 3'], 'E1013:') 86 call CheckDefExecFailure(['&bs = "asdf"'], 'E474:') 87 # test freeing ISN_STOREOPT 88 call CheckDefFailure(['&ts = 3', 'let asdf'], 'E1022:') 89 &ts = 8 90 91 g:inc_counter += 1 92 assert_equal(2, g:inc_counter) 93 94 $SOME_ENV_VAR ..= 'more' 95 assert_equal('somemore', $SOME_ENV_VAR) 96 call CheckDefFailure(['$SOME_ENV_VAR += "more"'], 'E1013:') 97 call CheckDefFailure(['$SOME_ENV_VAR += 123'], 'E1013:') 98 99 @a = 'areg' 100 @a ..= 'add' 101 assert_equal('aregadd', @a) 102 call CheckDefFailure(['@a += "more"'], 'E1013:') 103 call CheckDefFailure(['@a += 123'], 'E1013:') 104 105 v:errmsg = 'none' 106 v:errmsg ..= 'again' 107 assert_equal('noneagain', v:errmsg) 108 call CheckDefFailure(['v:errmsg += "more"'], 'E1013:') 109 call CheckDefFailure(['v:errmsg += 123'], 'E1013:') 110enddef 111 112def Test_assignment_list() 113 let list1: list<bool> = [false, true, false] 114 let list2: list<number> = [1, 2, 3] 115 let list3: list<string> = ['sdf', 'asdf'] 116 let list4: list<any> = ['yes', true, 1234] 117 let list5: list<blob> = [0z01, 0z02] 118 119 let listS: list<string> = [] 120 let listN: list<number> = [] 121 122 assert_equal([1, 2, 3], list2) 123 list2[-1] = 99 124 assert_equal([1, 2, 99], list2) 125 list2[-2] = 88 126 assert_equal([1, 88, 99], list2) 127 list2[-3] = 77 128 assert_equal([77, 88, 99], list2) 129 call CheckDefExecFailure(['let ll = [1, 2, 3]', 'll[-4] = 6'], 'E684:') 130 call CheckDefExecFailure(['let [v1, v2] = [1, 2]'], 'E1092:') 131 132 # type becomes list<any> 133 let somelist = rand() > 0 ? [1, 2, 3] : ['a', 'b', 'c'] 134enddef 135 136def Test_assignment_dict() 137 let dict1: dict<bool> = #{one: false, two: true} 138 let dict2: dict<number> = #{one: 1, two: 2} 139 let dict3: dict<string> = #{key: 'value'} 140 let dict4: dict<any> = #{one: 1, two: '2'} 141 let dict5: dict<blob> = #{one: 0z01, two: 0z02} 142 143 call CheckDefExecFailure(['let dd = {}', 'dd[""] = 6'], 'E713:') 144 145 # type becomes dict<any> 146 let somedict = rand() > 0 ? #{a: 1, b: 2} : #{a: 'a', b: 'b'} 147enddef 148 149def Test_assignment_local() 150 " Test in a separated file in order not to the current buffer/window/tab is 151 " changed. 152 let script_lines: list<string> =<< trim END 153 let b:existing = 'yes' 154 let w:existing = 'yes' 155 let t:existing = 'yes' 156 157 def Test_assignment_local_internal() 158 b:newvar = 'new' 159 assert_equal('new', b:newvar) 160 assert_equal('yes', b:existing) 161 b:existing = 'no' 162 assert_equal('no', b:existing) 163 b:existing ..= 'NO' 164 assert_equal('noNO', b:existing) 165 166 w:newvar = 'new' 167 assert_equal('new', w:newvar) 168 assert_equal('yes', w:existing) 169 w:existing = 'no' 170 assert_equal('no', w:existing) 171 w:existing ..= 'NO' 172 assert_equal('noNO', w:existing) 173 174 t:newvar = 'new' 175 assert_equal('new', t:newvar) 176 assert_equal('yes', t:existing) 177 t:existing = 'no' 178 assert_equal('no', t:existing) 179 t:existing ..= 'NO' 180 assert_equal('noNO', t:existing) 181 enddef 182 call Test_assignment_local_internal() 183 END 184 call CheckScriptSuccess(script_lines) 185enddef 186 187def Test_assignment_default() 188 189 # Test default values. 190 let thebool: bool 191 assert_equal(v:false, thebool) 192 193 let thenumber: number 194 assert_equal(0, thenumber) 195 196 if has('float') 197 let thefloat: float 198 assert_equal(0.0, thefloat) 199 endif 200 201 let thestring: string 202 assert_equal('', thestring) 203 204 let theblob: blob 205 assert_equal(0z, theblob) 206 207 let Thefunc: func 208 assert_equal(test_null_function(), Thefunc) 209 210 let thelist: list<any> 211 assert_equal([], thelist) 212 213 let thedict: dict<any> 214 assert_equal({}, thedict) 215 216 if has('channel') 217 let thejob: job 218 assert_equal(test_null_job(), thejob) 219 220 let thechannel: channel 221 assert_equal(test_null_channel(), thechannel) 222 endif 223 224 let nr = 1234 | nr = 5678 225 assert_equal(5678, nr) 226enddef 227 228def Test_assignment_var_list() 229 let v1: string 230 let v2: string 231 let vrem: list<string> 232 [v1] = ['aaa'] 233 assert_equal('aaa', v1) 234 235 [v1, v2] = ['one', 'two'] 236 assert_equal('one', v1) 237 assert_equal('two', v2) 238 239 [v1, v2; vrem] = ['one', 'two'] 240 assert_equal('one', v1) 241 assert_equal('two', v2) 242 assert_equal([], vrem) 243 244 [v1, v2; vrem] = ['one', 'two', 'three'] 245 assert_equal('one', v1) 246 assert_equal('two', v2) 247 assert_equal(['three'], vrem) 248enddef 249 250def Mess(): string 251 v:foldstart = 123 252 return 'xxx' 253enddef 254 255def Test_assignment_failure() 256 call CheckDefFailure(['let var=234'], 'E1004:') 257 call CheckDefFailure(['let var =234'], 'E1004:') 258 call CheckDefFailure(['let var= 234'], 'E1004:') 259 260 call CheckDefFailure(['let true = 1'], 'E1034:') 261 call CheckDefFailure(['let false = 1'], 'E1034:') 262 263 call CheckDefFailure(['[a; b; c] = g:list'], 'E452:') 264 call CheckDefExecFailure(['let a: number', 265 '[a] = test_null_list()'], 'E1093:') 266 call CheckDefExecFailure(['let a: number', 267 '[a] = []'], 'E1093:') 268 call CheckDefExecFailure(['let x: number', 269 'let y: number', 270 '[x, y] = [1]'], 'E1093:') 271 call CheckDefExecFailure(['let x: number', 272 'let y: number', 273 'let z: list<number>', 274 '[x, y; z] = [1]'], 'E1093:') 275 276 call CheckDefFailure(['let somevar'], "E1022:") 277 call CheckDefFailure(['let &option'], 'E1052:') 278 call CheckDefFailure(['&g:option = 5'], 'E113:') 279 280 call CheckDefFailure(['let $VAR = 5'], 'E1065:') 281 282 call CheckDefFailure(['let @~ = 5'], 'E354:') 283 call CheckDefFailure(['let @a = 5'], 'E1066:') 284 285 call CheckDefFailure(['let g:var = 5'], 'E1016:') 286 call CheckDefFailure(['let w:var = 5'], 'E1079:') 287 call CheckDefFailure(['let b:var = 5'], 'E1078:') 288 call CheckDefFailure(['let t:var = 5'], 'E1080:') 289 290 call CheckDefFailure(['let anr = 4', 'anr ..= "text"'], 'E1019:') 291 call CheckDefFailure(['let xnr += 4'], 'E1020:') 292 293 call CheckScriptFailure(['vim9script', 'def Func()', 'let dummy = s:notfound', 'enddef', 'defcompile'], 'E1050:') 294 295 call CheckDefFailure(['let var: list<string> = [123]'], 'expected list<string> but got list<number>') 296 call CheckDefFailure(['let var: list<number> = ["xx"]'], 'expected list<number> but got list<string>') 297 298 call CheckDefFailure(['let var: dict<string> = #{key: 123}'], 'expected dict<string> but got dict<number>') 299 call CheckDefFailure(['let var: dict<number> = #{key: "xx"}'], 'expected dict<number> but got dict<string>') 300 301 call CheckDefFailure(['let var = feedkeys("0")'], 'E1031:') 302 call CheckDefFailure(['let var: number = feedkeys("0")'], 'expected number but got void') 303 304 call CheckDefFailure(['let var: dict <number>'], 'E1068:') 305 call CheckDefFailure(['let var: dict<number'], 'E1009:') 306 307 call assert_fails('s/^/\=Mess()/n', 'E794:') 308 call CheckDefFailure(['let var: dict<number'], 'E1009:') 309enddef 310 311def Test_unlet() 312 g:somevar = 'yes' 313 assert_true(exists('g:somevar')) 314 unlet g:somevar 315 assert_false(exists('g:somevar')) 316 unlet! g:somevar 317 318 call CheckScriptFailure([ 319 'vim9script', 320 'let svar = 123', 321 'unlet svar', 322 ], 'E1081:') 323 call CheckScriptFailure([ 324 'vim9script', 325 'let svar = 123', 326 'unlet s:svar', 327 ], 'E1081:') 328 call CheckScriptFailure([ 329 'vim9script', 330 'let svar = 123', 331 'def Func()', 332 ' unlet svar', 333 'enddef', 334 'defcompile', 335 ], 'E1081:') 336 call CheckScriptFailure([ 337 'vim9script', 338 'let svar = 123', 339 'def Func()', 340 ' unlet s:svar', 341 'enddef', 342 'defcompile', 343 ], 'E1081:') 344 345 $ENVVAR = 'foobar' 346 assert_equal('foobar', $ENVVAR) 347 unlet $ENVVAR 348 assert_equal('', $ENVVAR) 349enddef 350 351def Test_delfunction() 352 " Check function is defined in script namespace 353 CheckScriptSuccess([ 354 'vim9script', 355 'func CheckMe()', 356 ' return 123', 357 'endfunc', 358 'assert_equal(123, s:CheckMe())', 359 ]) 360 361 " Check function in script namespace cannot be deleted 362 CheckScriptFailure([ 363 'vim9script', 364 'func DeleteMe1()', 365 'endfunc', 366 'delfunction DeleteMe1', 367 ], 'E1084:') 368 CheckScriptFailure([ 369 'vim9script', 370 'func DeleteMe2()', 371 'endfunc', 372 'def DoThat()', 373 ' delfunction DeleteMe2', 374 'enddef', 375 'DoThat()', 376 ], 'E1084:') 377 CheckScriptFailure([ 378 'vim9script', 379 'def DeleteMe3()', 380 'enddef', 381 'delfunction DeleteMe3', 382 ], 'E1084:') 383 CheckScriptFailure([ 384 'vim9script', 385 'def DeleteMe4()', 386 'enddef', 387 'def DoThat()', 388 ' delfunction DeleteMe4', 389 'enddef', 390 'DoThat()', 391 ], 'E1084:') 392enddef 393 394func Test_wrong_type() 395 call CheckDefFailure(['let var: list<nothing>'], 'E1010:') 396 call CheckDefFailure(['let var: list<list<nothing>>'], 'E1010:') 397 call CheckDefFailure(['let var: dict<nothing>'], 'E1010:') 398 call CheckDefFailure(['let var: dict<dict<nothing>>'], 'E1010:') 399 400 call CheckDefFailure(['let var: dict<number'], 'E1009:') 401 call CheckDefFailure(['let var: dict<list<number>'], 'E1009:') 402 403 call CheckDefFailure(['let var: ally'], 'E1010:') 404 call CheckDefFailure(['let var: bram'], 'E1010:') 405 call CheckDefFailure(['let var: cathy'], 'E1010:') 406 call CheckDefFailure(['let var: dom'], 'E1010:') 407 call CheckDefFailure(['let var: freddy'], 'E1010:') 408 call CheckDefFailure(['let var: john'], 'E1010:') 409 call CheckDefFailure(['let var: larry'], 'E1010:') 410 call CheckDefFailure(['let var: ned'], 'E1010:') 411 call CheckDefFailure(['let var: pam'], 'E1010:') 412 call CheckDefFailure(['let var: sam'], 'E1010:') 413 call CheckDefFailure(['let var: vim'], 'E1010:') 414 415 call CheckDefFailure(['let Ref: number', 'Ref()'], 'E1085:') 416 call CheckDefFailure(['let Ref: string', 'let res = Ref()'], 'E1085:') 417endfunc 418 419func Test_const() 420 call CheckDefFailure(['const var = 234', 'var = 99'], 'E1018:') 421 call CheckDefFailure(['const one = 234', 'let one = 99'], 'E1017:') 422 call CheckDefFailure(['const two'], 'E1021:') 423 call CheckDefFailure(['const &option'], 'E996:') 424endfunc 425 426def Test_block() 427 let outer = 1 428 { 429 let inner = 2 430 assert_equal(1, outer) 431 assert_equal(2, inner) 432 } 433 assert_equal(1, outer) 434enddef 435 436func Test_block_failure() 437 call CheckDefFailure(['{', 'let inner = 1', '}', 'echo inner'], 'E1001:') 438 call CheckDefFailure(['}'], 'E1025:') 439 call CheckDefFailure(['{', 'echo 1'], 'E1026:') 440endfunc 441 442def Test_cmd_modifier() 443 tab echo '0' 444 call CheckDefFailure(['5tab echo 3'], 'E16:') 445enddef 446 447def Test_try_catch() 448 let l = [] 449 try # comment 450 add(l, '1') 451 throw 'wrong' 452 add(l, '2') 453 catch # comment 454 add(l, v:exception) 455 finally # comment 456 add(l, '3') 457 endtry # comment 458 assert_equal(['1', 'wrong', '3'], l) 459enddef 460 461def ThrowFromDef() 462 throw "getout" # comment 463enddef 464 465func CatchInFunc() 466 try 467 call ThrowFromDef() 468 catch 469 let g:thrown_func = v:exception 470 endtry 471endfunc 472 473def CatchInDef() 474 try 475 ThrowFromDef() 476 catch 477 g:thrown_def = v:exception 478 endtry 479enddef 480 481def ReturnFinally(): string 482 try 483 return 'intry' 484 finally 485 g:in_finally = 'finally' 486 endtry 487 return 'end' 488enddef 489 490def Test_try_catch_nested() 491 CatchInFunc() 492 assert_equal('getout', g:thrown_func) 493 494 CatchInDef() 495 assert_equal('getout', g:thrown_def) 496 497 assert_equal('intry', ReturnFinally()) 498 assert_equal('finally', g:in_finally) 499enddef 500 501def Test_try_catch_match() 502 let seq = 'a' 503 try 504 throw 'something' 505 catch /nothing/ 506 seq ..= 'x' 507 catch /some/ 508 seq ..= 'b' 509 catch /asdf/ 510 seq ..= 'x' 511 catch ?a\?sdf? 512 seq ..= 'y' 513 finally 514 seq ..= 'c' 515 endtry 516 assert_equal('abc', seq) 517enddef 518 519def Test_try_catch_fails() 520 call CheckDefFailure(['catch'], 'E603:') 521 call CheckDefFailure(['try', 'echo 0', 'catch','catch'], 'E1033:') 522 call CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:') 523 call CheckDefFailure(['finally'], 'E606:') 524 call CheckDefFailure(['try', 'echo 0', 'finally', 'echo 1', 'finally'], 'E607:') 525 call CheckDefFailure(['endtry'], 'E602:') 526 call CheckDefFailure(['while 1', 'endtry'], 'E170:') 527 call CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:') 528 call CheckDefFailure(['if 2', 'endtry'], 'E171:') 529 call CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:') 530 531 call CheckDefFailure(['throw'], 'E1015:') 532 call CheckDefFailure(['throw xxx'], 'E1001:') 533enddef 534 535if has('channel') 536 let someJob = test_null_job() 537 538 def FuncWithError() 539 echomsg g:someJob 540 enddef 541 542 func Test_convert_emsg_to_exception() 543 try 544 call FuncWithError() 545 catch 546 call assert_match('Vim:E908:', v:exception) 547 endtry 548 endfunc 549endif 550 551let s:export_script_lines =<< trim END 552 vim9script 553 let name: string = 'bob' 554 def Concat(arg: string): string 555 return name .. arg 556 enddef 557 g:result = Concat('bie') 558 g:localname = name 559 560 export const CONST = 1234 561 export let exported = 9876 562 export let exp_name = 'John' 563 export def Exported(): string 564 return 'Exported' 565 enddef 566END 567 568def Test_vim9_import_export() 569 let import_script_lines =<< trim END 570 vim9script 571 import {exported, Exported} from './Xexport.vim' 572 g:imported = exported 573 exported += 3 574 g:imported_added = exported 575 g:imported_func = Exported() 576 577 import {exp_name} from './Xexport.vim' 578 g:imported_name = exp_name 579 exp_name ..= ' Doe' 580 g:imported_name_appended = exp_name 581 g:imported_later = exported 582 END 583 584 writefile(import_script_lines, 'Ximport.vim') 585 writefile(s:export_script_lines, 'Xexport.vim') 586 587 source Ximport.vim 588 589 assert_equal('bobbie', g:result) 590 assert_equal('bob', g:localname) 591 assert_equal(9876, g:imported) 592 assert_equal(9879, g:imported_added) 593 assert_equal(9879, g:imported_later) 594 assert_equal('Exported', g:imported_func) 595 assert_equal('John', g:imported_name) 596 assert_equal('John Doe', g:imported_name_appended) 597 assert_false(exists('g:name')) 598 599 unlet g:result 600 unlet g:localname 601 unlet g:imported 602 unlet g:imported_added 603 unlet g:imported_later 604 unlet g:imported_func 605 unlet g:imported_name g:imported_name_appended 606 delete('Ximport.vim') 607 608 let import_in_def_lines =<< trim END 609 vim9script 610 def ImportInDef() 611 import exported from './Xexport.vim' 612 g:imported = exported 613 exported += 7 614 g:imported_added = exported 615 enddef 616 ImportInDef() 617 END 618 writefile(import_in_def_lines, 'Ximport2.vim') 619 source Ximport2.vim 620 " TODO: this should be 9879 621 assert_equal(9876, g:imported) 622 assert_equal(9883, g:imported_added) 623 unlet g:imported 624 unlet g:imported_added 625 delete('Ximport2.vim') 626 627 let import_star_as_lines =<< trim END 628 vim9script 629 import * as Export from './Xexport.vim' 630 def UseExport() 631 g:imported = Export.exported 632 enddef 633 UseExport() 634 END 635 writefile(import_star_as_lines, 'Ximport.vim') 636 source Ximport.vim 637 assert_equal(9883, g:imported) 638 639 let import_star_as_lines_no_dot =<< trim END 640 vim9script 641 import * as Export from './Xexport.vim' 642 def Func() 643 let dummy = 1 644 let imported = Export + dummy 645 enddef 646 defcompile 647 END 648 writefile(import_star_as_lines_no_dot, 'Ximport.vim') 649 assert_fails('source Ximport.vim', 'E1060:') 650 651 let import_star_as_lines_dot_space =<< trim END 652 vim9script 653 import * as Export from './Xexport.vim' 654 def Func() 655 let imported = Export . exported 656 enddef 657 defcompile 658 END 659 writefile(import_star_as_lines_dot_space, 'Ximport.vim') 660 assert_fails('source Ximport.vim', 'E1074:') 661 662 let import_star_as_lines_missing_name =<< trim END 663 vim9script 664 import * as Export from './Xexport.vim' 665 def Func() 666 let imported = Export. 667 enddef 668 defcompile 669 END 670 writefile(import_star_as_lines_missing_name, 'Ximport.vim') 671 assert_fails('source Ximport.vim', 'E1048:') 672 673 let import_star_lines =<< trim END 674 vim9script 675 import * from './Xexport.vim' 676 END 677 writefile(import_star_lines, 'Ximport.vim') 678 assert_fails('source Ximport.vim', 'E1045:') 679 680 " try to import something that exists but is not exported 681 let import_not_exported_lines =<< trim END 682 vim9script 683 import name from './Xexport.vim' 684 END 685 writefile(import_not_exported_lines, 'Ximport.vim') 686 assert_fails('source Ximport.vim', 'E1049:') 687 688 " try to import something that is already defined 689 let import_already_defined =<< trim END 690 vim9script 691 let exported = 'something' 692 import exported from './Xexport.vim' 693 END 694 writefile(import_already_defined, 'Ximport.vim') 695 assert_fails('source Ximport.vim', 'E1073:') 696 697 " try to import something that is already defined 698 import_already_defined =<< trim END 699 vim9script 700 let exported = 'something' 701 import * as exported from './Xexport.vim' 702 END 703 writefile(import_already_defined, 'Ximport.vim') 704 assert_fails('source Ximport.vim', 'E1073:') 705 706 " try to import something that is already defined 707 import_already_defined =<< trim END 708 vim9script 709 let exported = 'something' 710 import {exported} from './Xexport.vim' 711 END 712 writefile(import_already_defined, 'Ximport.vim') 713 assert_fails('source Ximport.vim', 'E1073:') 714 715 " import a very long name, requires making a copy 716 let import_long_name_lines =<< trim END 717 vim9script 718 import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim' 719 END 720 writefile(import_long_name_lines, 'Ximport.vim') 721 assert_fails('source Ximport.vim', 'E1048:') 722 723 let import_no_from_lines =<< trim END 724 vim9script 725 import name './Xexport.vim' 726 END 727 writefile(import_no_from_lines, 'Ximport.vim') 728 assert_fails('source Ximport.vim', 'E1070:') 729 730 let import_invalid_string_lines =<< trim END 731 vim9script 732 import name from Xexport.vim 733 END 734 writefile(import_invalid_string_lines, 'Ximport.vim') 735 assert_fails('source Ximport.vim', 'E1071:') 736 737 let import_wrong_name_lines =<< trim END 738 vim9script 739 import name from './XnoExport.vim' 740 END 741 writefile(import_wrong_name_lines, 'Ximport.vim') 742 assert_fails('source Ximport.vim', 'E1053:') 743 744 let import_missing_comma_lines =<< trim END 745 vim9script 746 import {exported name} from './Xexport.vim' 747 END 748 writefile(import_missing_comma_lines, 'Ximport3.vim') 749 assert_fails('source Ximport3.vim', 'E1046:') 750 751 delete('Ximport.vim') 752 delete('Ximport3.vim') 753 delete('Xexport.vim') 754 755 " Check that in a Vim9 script 'cpo' is set to the Vim default. 756 set cpo&vi 757 let cpo_before = &cpo 758 let lines =<< trim END 759 vim9script 760 g:cpo_in_vim9script = &cpo 761 END 762 writefile(lines, 'Xvim9_script') 763 source Xvim9_script 764 assert_equal(cpo_before, &cpo) 765 set cpo&vim 766 assert_equal(&cpo, g:cpo_in_vim9script) 767 delete('Xvim9_script') 768enddef 769 770def Test_vim9script_fails() 771 CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:') 772 CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:') 773 CheckScriptFailure(['export let some = 123'], 'E1042:') 774 CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:') 775 CheckScriptFailure(['vim9script', 'export let g:some'], 'E1044:') 776 CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:') 777 778 assert_fails('vim9script', 'E1038') 779 assert_fails('export something', 'E1043') 780enddef 781 782func Test_import_fails_without_script() 783 CheckRunVimInTerminal 784 785 " call indirectly to avoid compilation error for missing functions 786 call Run_Test_import_fails_without_script() 787endfunc 788 789def Run_Test_import_fails_without_script() 790 let export =<< trim END 791 vim9script 792 export def Foo(): number 793 return 0 794 enddef 795 END 796 writefile(export, 'Xexport.vim') 797 798 let buf = RunVimInTerminal('-c "import Foo from ''./Xexport.vim''"', #{ 799 rows: 6, wait_for_ruler: 0}) 800 WaitForAssert({-> assert_match('^E1094:', term_getline(buf, 5))}) 801 802 delete('Xexport.vim') 803 StopVimInTerminal(buf) 804enddef 805 806def Test_vim9script_reload_import() 807 let lines =<< trim END 808 vim9script 809 const var = '' 810 let valone = 1234 811 def MyFunc(arg: string) 812 valone = 5678 813 enddef 814 END 815 let morelines =<< trim END 816 let valtwo = 222 817 export def GetValtwo(): number 818 return valtwo 819 enddef 820 END 821 writefile(lines + morelines, 'Xreload.vim') 822 source Xreload.vim 823 source Xreload.vim 824 source Xreload.vim 825 826 let testlines =<< trim END 827 vim9script 828 def TheFunc() 829 import GetValtwo from './Xreload.vim' 830 assert_equal(222, GetValtwo()) 831 enddef 832 TheFunc() 833 END 834 writefile(testlines, 'Ximport.vim') 835 source Ximport.vim 836 837 " Test that when not using "morelines" GetValtwo() and valtwo are still 838 " defined, because import doesn't reload a script. 839 writefile(lines, 'Xreload.vim') 840 source Ximport.vim 841 842 " cannot declare a var twice 843 lines =<< trim END 844 vim9script 845 let valone = 1234 846 let valone = 5678 847 END 848 writefile(lines, 'Xreload.vim') 849 assert_fails('source Xreload.vim', 'E1041:') 850 851 delete('Xreload.vim') 852 delete('Ximport.vim') 853enddef 854 855def Test_vim9script_reload_delfunc() 856 let first_lines =<< trim END 857 vim9script 858 def FuncYes(): string 859 return 'yes' 860 enddef 861 END 862 let withno_lines =<< trim END 863 def FuncNo(): string 864 return 'no' 865 enddef 866 def g:DoCheck(no_exists: bool) 867 assert_equal('yes', FuncYes()) 868 assert_equal('no', FuncNo()) 869 enddef 870 END 871 let nono_lines =<< trim END 872 def g:DoCheck(no_exists: bool) 873 assert_equal('yes', FuncYes()) 874 assert_fails('call FuncNo()', 'E117:') 875 enddef 876 END 877 878 # FuncNo() is defined 879 writefile(first_lines + withno_lines, 'Xreloaded.vim') 880 source Xreloaded.vim 881 g:DoCheck(true) 882 883 # FuncNo() is not redefined 884 writefile(first_lines + nono_lines, 'Xreloaded.vim') 885 source Xreloaded.vim 886 g:DoCheck() 887 888 # FuncNo() is back 889 writefile(first_lines + withno_lines, 'Xreloaded.vim') 890 source Xreloaded.vim 891 g:DoCheck() 892 893 delete('Xreloaded.vim') 894enddef 895 896def Test_vim9script_reload_delvar() 897 # write the script with a script-local variable 898 let lines =<< trim END 899 vim9script 900 let var = 'string' 901 END 902 writefile(lines, 'XreloadVar.vim') 903 source XreloadVar.vim 904 905 # now write the script using the same variable locally - works 906 lines =<< trim END 907 vim9script 908 def Func() 909 let var = 'string' 910 enddef 911 END 912 writefile(lines, 'XreloadVar.vim') 913 source XreloadVar.vim 914 915 delete('XreloadVar.vim') 916enddef 917 918def Test_import_absolute() 919 let import_lines = [ 920 'vim9script', 921 'import exported from "' .. escape(getcwd(), '\') .. '/Xexport_abs.vim"', 922 'def UseExported()', 923 ' g:imported_abs = exported', 924 ' exported = 8888', 925 ' g:imported_after = exported', 926 'enddef', 927 'UseExported()', 928 'g:import_disassembled = execute("disass UseExported")', 929 ] 930 writefile(import_lines, 'Ximport_abs.vim') 931 writefile(s:export_script_lines, 'Xexport_abs.vim') 932 933 source Ximport_abs.vim 934 935 assert_equal(9876, g:imported_abs) 936 assert_equal(8888, g:imported_after) 937 assert_match('<SNR>\d\+_UseExported.*' .. 938 'g:imported_abs = exported.*' .. 939 '0 LOADSCRIPT exported from .*Xexport_abs.vim.*' .. 940 '1 STOREG g:imported_abs.*' .. 941 'exported = 8888.*' .. 942 '3 STORESCRIPT exported in .*Xexport_abs.vim.*' .. 943 'g:imported_after = exported.*' .. 944 '4 LOADSCRIPT exported from .*Xexport_abs.vim.*' .. 945 '5 STOREG g:imported_after.*', 946 g:import_disassembled) 947 unlet g:imported_abs 948 unlet g:import_disassembled 949 950 delete('Ximport_abs.vim') 951 delete('Xexport_abs.vim') 952enddef 953 954def Test_import_rtp() 955 let import_lines = [ 956 'vim9script', 957 'import exported from "Xexport_rtp.vim"', 958 'g:imported_rtp = exported', 959 ] 960 writefile(import_lines, 'Ximport_rtp.vim') 961 mkdir('import') 962 writefile(s:export_script_lines, 'import/Xexport_rtp.vim') 963 964 let save_rtp = &rtp 965 &rtp = getcwd() 966 source Ximport_rtp.vim 967 &rtp = save_rtp 968 969 assert_equal(9876, g:imported_rtp) 970 unlet g:imported_rtp 971 972 delete('Ximport_rtp.vim') 973 delete('import', 'rf') 974enddef 975 976def Test_import_compile_error() 977 let export_lines = [ 978 'vim9script', 979 'export def ExpFunc(): string', 980 ' return notDefined', 981 'enddef', 982 ] 983 writefile(export_lines, 'Xexported.vim') 984 985 let import_lines = [ 986 'vim9script', 987 'import ExpFunc from "./Xexported.vim"', 988 'def ImpFunc()', 989 ' echo ExpFunc()', 990 'enddef', 991 'defcompile', 992 ] 993 writefile(import_lines, 'Ximport.vim') 994 995 try 996 source Ximport.vim 997 catch /E1001/ 998 " Error should be fore the Xexported.vim file. 999 assert_match('E1001: variable not found: notDefined', v:exception) 1000 assert_match('function <SNR>\d\+_ImpFunc\[1\]..<SNR>\d\+_ExpFunc, line 1', v:throwpoint) 1001 endtry 1002 1003 delete('Xexported.vim') 1004 delete('Ximport.vim') 1005enddef 1006 1007def Test_fixed_size_list() 1008 " will be allocated as one piece of memory, check that changes work 1009 let l = [1, 2, 3, 4] 1010 l->remove(0) 1011 l->add(5) 1012 l->insert(99, 1) 1013 assert_equal([2, 99, 3, 4, 5], l) 1014enddef 1015 1016def IfElse(what: number): string 1017 let res = '' 1018 if what == 1 1019 res = "one" 1020 elseif what == 2 1021 res = "two" 1022 else 1023 res = "three" 1024 endif 1025 return res 1026enddef 1027 1028def Test_if_elseif_else() 1029 assert_equal('one', IfElse(1)) 1030 assert_equal('two', IfElse(2)) 1031 assert_equal('three', IfElse(3)) 1032enddef 1033 1034def Test_if_elseif_else_fails() 1035 call CheckDefFailure(['elseif true'], 'E582:') 1036 call CheckDefFailure(['else'], 'E581:') 1037 call CheckDefFailure(['endif'], 'E580:') 1038 call CheckDefFailure(['if true', 'elseif xxx'], 'E1001:') 1039 call CheckDefFailure(['if true', 'echo 1'], 'E171:') 1040enddef 1041 1042let g:bool_true = v:true 1043let g:bool_false = v:false 1044 1045def Test_if_const_expr() 1046 let res = false 1047 if true ? true : false 1048 res = true 1049 endif 1050 assert_equal(true, res) 1051 1052 g:glob = 2 1053 if false 1054 execute('let g:glob = 3') 1055 endif 1056 assert_equal(2, g:glob) 1057 if true 1058 execute('let g:glob = 3') 1059 endif 1060 assert_equal(3, g:glob) 1061 1062 res = false 1063 if g:bool_true ? true : false 1064 res = true 1065 endif 1066 assert_equal(true, res) 1067 1068 res = false 1069 if true ? g:bool_true : false 1070 res = true 1071 endif 1072 assert_equal(true, res) 1073 1074 res = false 1075 if true ? true : g:bool_false 1076 res = true 1077 endif 1078 assert_equal(true, res) 1079 1080 res = false 1081 if true ? false : true 1082 res = true 1083 endif 1084 assert_equal(false, res) 1085 1086 res = false 1087 if false ? false : true 1088 res = true 1089 endif 1090 assert_equal(true, res) 1091 1092 res = false 1093 if false ? true : false 1094 res = true 1095 endif 1096 assert_equal(false, res) 1097 1098 res = false 1099 if has('xyz') ? true : false 1100 res = true 1101 endif 1102 assert_equal(false, res) 1103 1104 res = false 1105 if true && true 1106 res = true 1107 endif 1108 assert_equal(true, res) 1109 1110 res = false 1111 if true && false 1112 res = true 1113 endif 1114 assert_equal(false, res) 1115 1116 res = false 1117 if g:bool_true && false 1118 res = true 1119 endif 1120 assert_equal(false, res) 1121 1122 res = false 1123 if true && g:bool_false 1124 res = true 1125 endif 1126 assert_equal(false, res) 1127 1128 res = false 1129 if false && false 1130 res = true 1131 endif 1132 assert_equal(false, res) 1133 1134 res = false 1135 if true || false 1136 res = true 1137 endif 1138 assert_equal(true, res) 1139 1140 res = false 1141 if g:bool_true || false 1142 res = true 1143 endif 1144 assert_equal(true, res) 1145 1146 res = false 1147 if true || g:bool_false 1148 res = true 1149 endif 1150 assert_equal(true, res) 1151 1152 res = false 1153 if false || false 1154 res = true 1155 endif 1156 assert_equal(false, res) 1157enddef 1158 1159def Test_if_const_expr_fails() 1160 call CheckDefFailure(['if "aaa" == "bbb'], 'E114:') 1161 call CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:') 1162 call CheckDefFailure(["if has('aaa'"], 'E110:') 1163 call CheckDefFailure(["if has('aaa') ? true false"], 'E109:') 1164enddef 1165 1166def RunNested(i: number): number 1167 let x: number = 0 1168 if i % 2 1169 if 1 1170 " comment 1171 else 1172 " comment 1173 endif 1174 x += 1 1175 else 1176 x += 1000 1177 endif 1178 return x 1179enddef 1180 1181def Test_nested_if() 1182 assert_equal(1, RunNested(1)) 1183 assert_equal(1000, RunNested(2)) 1184enddef 1185 1186def Test_execute_cmd() 1187 new 1188 setline(1, 'default') 1189 execute 'call setline(1, "execute-string")' 1190 assert_equal('execute-string', getline(1)) 1191 1192 execute "call setline(1, 'execute-string')" 1193 assert_equal('execute-string', getline(1)) 1194 1195 let cmd1 = 'call setline(1,' 1196 let cmd2 = '"execute-var")' 1197 execute cmd1 cmd2 # comment 1198 assert_equal('execute-var', getline(1)) 1199 1200 execute cmd1 cmd2 '|call setline(1, "execute-var-string")' 1201 assert_equal('execute-var-string', getline(1)) 1202 1203 let cmd_first = 'call ' 1204 let cmd_last = 'setline(1, "execute-var-var")' 1205 execute cmd_first .. cmd_last 1206 assert_equal('execute-var-var', getline(1)) 1207 bwipe! 1208 1209 call CheckDefFailure(['execute xxx'], 'E1001:') 1210 call CheckDefFailure(['execute "cmd"# comment'], 'E488:') 1211enddef 1212 1213def Test_echo_cmd() 1214 echo 'some' # comment 1215 echon 'thing' 1216 assert_match('^something$', Screenline(&lines)) 1217 1218 echo "some" # comment 1219 echon "thing" 1220 assert_match('^something$', Screenline(&lines)) 1221 1222 let str1 = 'some' 1223 let str2 = 'more' 1224 echo str1 str2 1225 assert_match('^some more$', Screenline(&lines)) 1226 1227 call CheckDefFailure(['echo "xxx"# comment'], 'E488:') 1228enddef 1229 1230def Test_echomsg_cmd() 1231 echomsg 'some' 'more' # comment 1232 assert_match('^some more$', Screenline(&lines)) 1233 echo 'clear' 1234 1messages 1235 assert_match('^some more$', Screenline(&lines)) 1236 1237 call CheckDefFailure(['echomsg "xxx"# comment'], 'E488:') 1238enddef 1239 1240def Test_echoerr_cmd() 1241 try 1242 echoerr 'something' 'wrong' # comment 1243 catch 1244 assert_match('something wrong', v:exception) 1245 endtry 1246enddef 1247 1248def Test_for_outside_of_function() 1249 let lines =<< trim END 1250 vim9script 1251 new 1252 for var in range(0, 3) 1253 append(line('$'), var) 1254 endfor 1255 assert_equal(['', '0', '1', '2', '3'], getline(1, '$')) 1256 bwipe! 1257 END 1258 writefile(lines, 'Xvim9for.vim') 1259 source Xvim9for.vim 1260 delete('Xvim9for.vim') 1261enddef 1262 1263def Test_for_loop() 1264 let result = '' 1265 for cnt in range(7) 1266 if cnt == 4 1267 break 1268 endif 1269 if cnt == 2 1270 continue 1271 endif 1272 result ..= cnt .. '_' 1273 endfor 1274 assert_equal('0_1_3_', result) 1275enddef 1276 1277def Test_for_loop_fails() 1278 CheckDefFailure(['for # in range(5)'], 'E690:') 1279 CheckDefFailure(['for i In range(5)'], 'E690:') 1280 CheckDefFailure(['let x = 5', 'for x in range(5)'], 'E1023:') 1281 CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef', 'defcompile'], 'E1006:') 1282 CheckDefFailure(['for i in "text"'], 'E1024:') 1283 CheckDefFailure(['for i in xxx'], 'E1001:') 1284 CheckDefFailure(['endfor'], 'E588:') 1285 CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:') 1286enddef 1287 1288def Test_while_loop() 1289 let result = '' 1290 let cnt = 0 1291 while cnt < 555 1292 if cnt == 3 1293 break 1294 endif 1295 cnt += 1 1296 if cnt == 2 1297 continue 1298 endif 1299 result ..= cnt .. '_' 1300 endwhile 1301 assert_equal('1_3_', result) 1302enddef 1303 1304def Test_while_loop_fails() 1305 CheckDefFailure(['while xxx'], 'E1001:') 1306 CheckDefFailure(['endwhile'], 'E588:') 1307 CheckDefFailure(['continue'], 'E586:') 1308 CheckDefFailure(['if true', 'continue'], 'E586:') 1309 CheckDefFailure(['break'], 'E587:') 1310 CheckDefFailure(['if true', 'break'], 'E587:') 1311 CheckDefFailure(['while 1', 'echo 3'], 'E170:') 1312enddef 1313 1314def Test_interrupt_loop() 1315 let caught = false 1316 let x = 0 1317 try 1318 while 1 1319 x += 1 1320 if x == 100 1321 feedkeys("\<C-C>", 'Lt') 1322 endif 1323 endwhile 1324 catch 1325 caught = true 1326 assert_equal(100, x) 1327 endtry 1328 assert_true(caught, 'should have caught an exception') 1329enddef 1330 1331def Test_automatic_line_continuation() 1332 let mylist = [ 1333 'one', 1334 'two', 1335 'three', 1336 ] " comment 1337 assert_equal(['one', 'two', 'three'], mylist) 1338 1339 let mydict = { 1340 'one': 1, 1341 'two': 2, 1342 'three': 1343 3, 1344 } " comment 1345 assert_equal({'one': 1, 'two': 2, 'three': 3}, mydict) 1346 mydict = #{ 1347 one: 1, # comment 1348 two: # comment 1349 2, # comment 1350 three: 3 # comment 1351 } 1352 assert_equal(#{one: 1, two: 2, three: 3}, mydict) 1353 mydict = #{ 1354 one: 1, 1355 two: 1356 2, 1357 three: 3 1358 } 1359 assert_equal(#{one: 1, two: 2, three: 3}, mydict) 1360 1361 assert_equal( 1362 ['one', 'two', 'three'], 1363 split('one two three') 1364 ) 1365enddef 1366 1367def Test_vim9_comment() 1368 CheckScriptSuccess([ 1369 'vim9script', 1370 '# something', 1371 ]) 1372 CheckScriptFailure([ 1373 'vim9script', 1374 ':# something', 1375 ], 'E488:') 1376 CheckScriptFailure([ 1377 '# something', 1378 ], 'E488:') 1379 CheckScriptFailure([ 1380 ':# something', 1381 ], 'E488:') 1382 1383 { # block start 1384 } # block end 1385 CheckDefFailure([ 1386 '{# comment', 1387 ], 'E488:') 1388 CheckDefFailure([ 1389 '{', 1390 '}# comment', 1391 ], 'E488:') 1392 1393 echo "yes" # comment 1394 CheckDefFailure([ 1395 'echo "yes"# comment', 1396 ], 'E488:') 1397 CheckScriptSuccess([ 1398 'vim9script', 1399 'echo "yes" # something', 1400 ]) 1401 CheckScriptFailure([ 1402 'vim9script', 1403 'echo "yes"# something', 1404 ], 'E121:') 1405 CheckScriptFailure([ 1406 'vim9script', 1407 'echo# something', 1408 ], 'E121:') 1409 CheckScriptFailure([ 1410 'echo "yes" # something', 1411 ], 'E121:') 1412 1413 exe "echo" # comment 1414 CheckDefFailure([ 1415 'exe "echo"# comment', 1416 ], 'E488:') 1417 CheckScriptSuccess([ 1418 'vim9script', 1419 'exe "echo" # something', 1420 ]) 1421 CheckScriptFailure([ 1422 'vim9script', 1423 'exe "echo"# something', 1424 ], 'E121:') 1425 CheckDefFailure([ 1426 'exe # comment', 1427 ], 'E1015:') 1428 CheckScriptFailure([ 1429 'vim9script', 1430 'exe# something', 1431 ], 'E121:') 1432 CheckScriptFailure([ 1433 'exe "echo" # something', 1434 ], 'E121:') 1435 1436 CheckDefFailure([ 1437 'try# comment', 1438 ' echo "yes"', 1439 'catch', 1440 'endtry', 1441 ], 'E488:') 1442 CheckScriptFailure([ 1443 'vim9script', 1444 'try# comment', 1445 'echo "yes"', 1446 ], 'E488:') 1447 CheckDefFailure([ 1448 'try', 1449 ' throw#comment', 1450 'catch', 1451 'endtry', 1452 ], 'E1015:') 1453 CheckDefFailure([ 1454 'try', 1455 ' throw "yes"#comment', 1456 'catch', 1457 'endtry', 1458 ], 'E488:') 1459 CheckDefFailure([ 1460 'try', 1461 ' echo "yes"', 1462 'catch# comment', 1463 'endtry', 1464 ], 'E488:') 1465 CheckScriptFailure([ 1466 'vim9script', 1467 'try', 1468 ' echo "yes"', 1469 'catch# comment', 1470 'endtry', 1471 ], 'E654:') 1472 CheckDefFailure([ 1473 'try', 1474 ' echo "yes"', 1475 'catch /pat/# comment', 1476 'endtry', 1477 ], 'E488:') 1478 CheckDefFailure([ 1479 'try', 1480 'echo "yes"', 1481 'catch', 1482 'endtry# comment', 1483 ], 'E488:') 1484 CheckScriptFailure([ 1485 'vim9script', 1486 'try', 1487 ' echo "yes"', 1488 'catch', 1489 'endtry# comment', 1490 ], 'E600:') 1491 1492 CheckScriptSuccess([ 1493 'vim9script', 1494 'hi # comment', 1495 ]) 1496 CheckScriptFailure([ 1497 'vim9script', 1498 'hi# comment', 1499 ], 'E416:') 1500 CheckScriptSuccess([ 1501 'vim9script', 1502 'hi Search # comment', 1503 ]) 1504 CheckScriptFailure([ 1505 'vim9script', 1506 'hi Search# comment', 1507 ], 'E416:') 1508 CheckScriptSuccess([ 1509 'vim9script', 1510 'hi link This Search # comment', 1511 ]) 1512 CheckScriptFailure([ 1513 'vim9script', 1514 'hi link This That# comment', 1515 ], 'E413:') 1516 CheckScriptSuccess([ 1517 'vim9script', 1518 'hi clear This # comment', 1519 'hi clear # comment', 1520 ]) 1521 " not tested, because it doesn't give an error but a warning: 1522 " hi clear This# comment', 1523 CheckScriptFailure([ 1524 'vim9script', 1525 'hi clear# comment', 1526 ], 'E416:') 1527 1528 CheckScriptSuccess([ 1529 'vim9script', 1530 'hi Group term=bold', 1531 'match Group /todo/ # comment', 1532 ]) 1533 CheckScriptFailure([ 1534 'vim9script', 1535 'hi Group term=bold', 1536 'match Group /todo/# comment', 1537 ], 'E488:') 1538 CheckScriptSuccess([ 1539 'vim9script', 1540 'match # comment', 1541 ]) 1542 CheckScriptFailure([ 1543 'vim9script', 1544 'match# comment', 1545 ], 'E475:') 1546 CheckScriptSuccess([ 1547 'vim9script', 1548 'match none # comment', 1549 ]) 1550 CheckScriptFailure([ 1551 'vim9script', 1552 'match none# comment', 1553 ], 'E475:') 1554 1555 CheckScriptSuccess([ 1556 'vim9script', 1557 'menutrans clear # comment', 1558 ]) 1559 CheckScriptFailure([ 1560 'vim9script', 1561 'menutrans clear# comment text', 1562 ], 'E474:') 1563 1564 CheckScriptSuccess([ 1565 'vim9script', 1566 'syntax clear # comment', 1567 ]) 1568 CheckScriptFailure([ 1569 'vim9script', 1570 'syntax clear# comment text', 1571 ], 'E28:') 1572 CheckScriptSuccess([ 1573 'vim9script', 1574 'syntax keyword Word some', 1575 'syntax clear Word # comment', 1576 ]) 1577 CheckScriptFailure([ 1578 'vim9script', 1579 'syntax keyword Word some', 1580 'syntax clear Word# comment text', 1581 ], 'E28:') 1582 1583 CheckScriptSuccess([ 1584 'vim9script', 1585 'syntax list # comment', 1586 ]) 1587 CheckScriptFailure([ 1588 'vim9script', 1589 'syntax list# comment text', 1590 ], 'E28:') 1591 1592 CheckScriptSuccess([ 1593 'vim9script', 1594 'syntax match Word /pat/ oneline # comment', 1595 ]) 1596 CheckScriptFailure([ 1597 'vim9script', 1598 'syntax match Word /pat/ oneline# comment', 1599 ], 'E475:') 1600 1601 CheckScriptSuccess([ 1602 'vim9script', 1603 'syntax keyword Word word # comm[ent', 1604 ]) 1605 CheckScriptFailure([ 1606 'vim9script', 1607 'syntax keyword Word word# comm[ent', 1608 ], 'E789:') 1609 1610 CheckScriptSuccess([ 1611 'vim9script', 1612 'syntax match Word /pat/ # comment', 1613 ]) 1614 CheckScriptFailure([ 1615 'vim9script', 1616 'syntax match Word /pat/# comment', 1617 ], 'E402:') 1618 1619 CheckScriptSuccess([ 1620 'vim9script', 1621 'syntax match Word /pat/ contains=Something # comment', 1622 ]) 1623 CheckScriptFailure([ 1624 'vim9script', 1625 'syntax match Word /pat/ contains=Something# comment', 1626 ], 'E475:') 1627 CheckScriptFailure([ 1628 'vim9script', 1629 'syntax match Word /pat/ contains= # comment', 1630 ], 'E406:') 1631 CheckScriptFailure([ 1632 'vim9script', 1633 'syntax match Word /pat/ contains=# comment', 1634 ], 'E475:') 1635 1636 CheckScriptSuccess([ 1637 'vim9script', 1638 'syntax region Word start=/pat/ end=/pat/ # comment', 1639 ]) 1640 CheckScriptFailure([ 1641 'vim9script', 1642 'syntax region Word start=/pat/ end=/pat/# comment', 1643 ], 'E475:') 1644 1645 CheckScriptSuccess([ 1646 'vim9script', 1647 'syntax sync # comment', 1648 ]) 1649 CheckScriptFailure([ 1650 'vim9script', 1651 'syntax sync# comment', 1652 ], 'E404:') 1653 CheckScriptSuccess([ 1654 'vim9script', 1655 'syntax sync ccomment # comment', 1656 ]) 1657 CheckScriptFailure([ 1658 'vim9script', 1659 'syntax sync ccomment# comment', 1660 ], 'E404:') 1661 1662 CheckScriptSuccess([ 1663 'vim9script', 1664 'syntax cluster Some contains=Word # comment', 1665 ]) 1666 CheckScriptFailure([ 1667 'vim9script', 1668 'syntax cluster Some contains=Word# comment', 1669 ], 'E475:') 1670 1671 CheckScriptSuccess([ 1672 'vim9script', 1673 'command Echo echo # comment', 1674 'command Echo # comment', 1675 ]) 1676 CheckScriptFailure([ 1677 'vim9script', 1678 'command Echo echo# comment', 1679 'Echo', 1680 ], 'E121:') 1681 CheckScriptFailure([ 1682 'vim9script', 1683 'command Echo# comment', 1684 ], 'E182:') 1685 CheckScriptFailure([ 1686 'vim9script', 1687 'command Echo echo', 1688 'command Echo# comment', 1689 ], 'E182:') 1690 1691 CheckScriptSuccess([ 1692 'vim9script', 1693 'function # comment', 1694 ]) 1695 CheckScriptFailure([ 1696 'vim9script', 1697 'function# comment', 1698 ], 'E129:') 1699 CheckScriptSuccess([ 1700 'vim9script', 1701 'function CheckScriptSuccess # comment', 1702 ]) 1703 CheckScriptFailure([ 1704 'vim9script', 1705 'function CheckScriptSuccess# comment', 1706 ], 'E488:') 1707 1708 CheckScriptSuccess([ 1709 'vim9script', 1710 'func g:DeleteMeA()', 1711 'endfunc', 1712 'delfunction g:DeleteMeA # comment', 1713 ]) 1714 CheckScriptFailure([ 1715 'vim9script', 1716 'func g:DeleteMeB()', 1717 'endfunc', 1718 'delfunction g:DeleteMeB# comment', 1719 ], 'E488:') 1720 1721 CheckScriptSuccess([ 1722 'vim9script', 1723 'call execute("ls") # comment', 1724 ]) 1725 CheckScriptFailure([ 1726 'vim9script', 1727 'call execute("ls")# comment', 1728 ], 'E488:') 1729enddef 1730 1731def Test_vim9_comment_gui() 1732 CheckCanRunGui 1733 1734 CheckScriptFailure([ 1735 'vim9script', 1736 'gui#comment' 1737 ], 'E499:') 1738 CheckScriptFailure([ 1739 'vim9script', 1740 'gui -f#comment' 1741 ], 'E499:') 1742enddef 1743 1744def Test_vim9_comment_not_compiled() 1745 au TabEnter *.vim let g:entered = 1 1746 au TabEnter *.x let g:entered = 2 1747 1748 edit test.vim 1749 doautocmd TabEnter #comment 1750 assert_equal(1, g:entered) 1751 1752 doautocmd TabEnter f.x 1753 assert_equal(2, g:entered) 1754 1755 g:entered = 0 1756 doautocmd TabEnter f.x #comment 1757 assert_equal(2, g:entered) 1758 1759 assert_fails('doautocmd Syntax#comment', 'E216:') 1760 1761 au! TabEnter 1762 unlet g:entered 1763 1764 CheckScriptSuccess([ 1765 'vim9script', 1766 'let g:var = 123', 1767 'let w:var = 777', 1768 'unlet g:var w:var # something', 1769 ]) 1770 1771 CheckScriptFailure([ 1772 'vim9script', 1773 'let g:var = 123', 1774 'unlet g:var# comment1', 1775 ], 'E108:') 1776 1777 CheckScriptFailure([ 1778 'let g:var = 123', 1779 'unlet g:var # something', 1780 ], 'E488:') 1781 1782 CheckScriptSuccess([ 1783 'vim9script', 1784 'if 1 # comment2', 1785 ' echo "yes"', 1786 'elseif 2 #comment', 1787 ' echo "no"', 1788 'endif', 1789 ]) 1790 1791 CheckScriptFailure([ 1792 'vim9script', 1793 'if 1# comment3', 1794 ' echo "yes"', 1795 'endif', 1796 ], 'E15:') 1797 1798 CheckScriptFailure([ 1799 'vim9script', 1800 'if 0 # comment4', 1801 ' echo "yes"', 1802 'elseif 2#comment', 1803 ' echo "no"', 1804 'endif', 1805 ], 'E15:') 1806 1807 CheckScriptSuccess([ 1808 'vim9script', 1809 'let v = 1 # comment5', 1810 ]) 1811 1812 CheckScriptFailure([ 1813 'vim9script', 1814 'let v = 1# comment6', 1815 ], 'E15:') 1816 1817 CheckScriptSuccess([ 1818 'vim9script', 1819 'new' 1820 'call setline(1, ["# define pat", "last"])', 1821 '$', 1822 'dsearch /pat/ #comment', 1823 'bwipe!', 1824 ]) 1825 1826 CheckScriptFailure([ 1827 'vim9script', 1828 'new' 1829 'call setline(1, ["# define pat", "last"])', 1830 '$', 1831 'dsearch /pat/#comment', 1832 'bwipe!', 1833 ], 'E488:') 1834 1835 CheckScriptFailure([ 1836 'vim9script', 1837 'func! SomeFunc()', 1838 ], 'E477:') 1839enddef 1840 1841def Test_finish() 1842 let lines =<< trim END 1843 vim9script 1844 let g:res = 'one' 1845 if v:false | finish | endif 1846 let g:res = 'two' 1847 finish 1848 let g:res = 'three' 1849 END 1850 writefile(lines, 'Xfinished') 1851 source Xfinished 1852 assert_equal('two', g:res) 1853 1854 unlet g:res 1855 delete('Xfinished') 1856enddef 1857 1858def Test_let_func_call() 1859 let lines =<< trim END 1860 vim9script 1861 func GetValue() 1862 if exists('g:count') 1863 let g:count += 1 1864 else 1865 let g:count = 1 1866 endif 1867 return 'this' 1868 endfunc 1869 let val: string = GetValue() 1870 " env var is always a string 1871 let env = $TERM 1872 END 1873 writefile(lines, 'Xfinished') 1874 source Xfinished 1875 " GetValue() is not called during discovery phase 1876 assert_equal(1, g:count) 1877 1878 unlet g:count 1879 delete('Xfinished') 1880enddef 1881 1882def Test_let_missing_type() 1883 let lines =<< trim END 1884 vim9script 1885 let var = g:unknown 1886 END 1887 CheckScriptFailure(lines, 'E121:') 1888 1889 lines =<< trim END 1890 vim9script 1891 let nr: number = 123 1892 let var = nr 1893 END 1894 CheckScriptSuccess(lines) 1895enddef 1896 1897def Test_let_declaration() 1898 let lines =<< trim END 1899 vim9script 1900 let var: string 1901 g:var_uninit = var 1902 var = 'text' 1903 g:var_test = var 1904 " prefixing s: is optional 1905 s:var = 'prefixed' 1906 g:var_prefixed = s:var 1907 1908 let s:other: number 1909 other = 1234 1910 g:other_var = other 1911 END 1912 CheckScriptSuccess(lines) 1913 assert_equal('', g:var_uninit) 1914 assert_equal('text', g:var_test) 1915 assert_equal('prefixed', g:var_prefixed) 1916 assert_equal(1234, g:other_var) 1917 1918 unlet g:var_uninit 1919 unlet g:var_test 1920 unlet g:var_prefixed 1921 unlet g:other_var 1922enddef 1923 1924def Test_let_declaration_fails() 1925 let lines =<< trim END 1926 vim9script 1927 const var: string 1928 END 1929 CheckScriptFailure(lines, 'E1021:') 1930 1931 lines =<< trim END 1932 vim9script 1933 let 9var: string 1934 END 1935 CheckScriptFailure(lines, 'E475:') 1936enddef 1937 1938def Test_let_type_check() 1939 let lines =<< trim END 1940 vim9script 1941 let var: string 1942 var = 1234 1943 END 1944 CheckScriptFailure(lines, 'E1013:') 1945 1946 lines =<< trim END 1947 vim9script 1948 let var:string 1949 END 1950 CheckScriptFailure(lines, 'E1069:') 1951 1952 lines =<< trim END 1953 vim9script 1954 let var: asdf 1955 END 1956 CheckScriptFailure(lines, 'E1010:') 1957enddef 1958 1959def Test_forward_declaration() 1960 let lines =<< trim END 1961 vim9script 1962 def GetValue(): string 1963 return theVal 1964 enddef 1965 let theVal = 'something' 1966 g:initVal = GetValue() 1967 theVal = 'else' 1968 g:laterVal = GetValue() 1969 END 1970 writefile(lines, 'Xforward') 1971 source Xforward 1972 assert_equal('something', g:initVal) 1973 assert_equal('else', g:laterVal) 1974 1975 unlet g:initVal 1976 unlet g:laterVal 1977 delete('Xforward') 1978enddef 1979 1980def Test_source_vim9_from_legacy() 1981 let legacy_lines =<< trim END 1982 source Xvim9_script.vim 1983 1984 call assert_false(exists('local')) 1985 call assert_false(exists('exported')) 1986 call assert_false(exists('s:exported')) 1987 call assert_equal('global', global) 1988 call assert_equal('global', g:global) 1989 1990 " imported variable becomes script-local 1991 import exported from './Xvim9_script.vim' 1992 call assert_equal('exported', s:exported) 1993 call assert_false(exists('exported')) 1994 1995 " imported function becomes script-local 1996 import GetText from './Xvim9_script.vim' 1997 call assert_equal('text', s:GetText()) 1998 call assert_false(exists('*GetText')) 1999 END 2000 writefile(legacy_lines, 'Xlegacy_script.vim') 2001 2002 let vim9_lines =<< trim END 2003 vim9script 2004 let local = 'local' 2005 g:global = 'global' 2006 export let exported = 'exported' 2007 export def GetText(): string 2008 return 'text' 2009 enddef 2010 END 2011 writefile(vim9_lines, 'Xvim9_script.vim') 2012 2013 source Xlegacy_script.vim 2014 2015 assert_equal('global', g:global) 2016" unlet g:global 2017 2018 delete('Xlegacy_script.vim') 2019 delete('Xvim9_script.vim') 2020enddef 2021 2022" Keep this last, it messes up highlighting. 2023def Test_substitute_cmd() 2024 new 2025 setline(1, 'something') 2026 :substitute(some(other( 2027 assert_equal('otherthing', getline(1)) 2028 bwipe! 2029 2030 " also when the context is Vim9 script 2031 let lines =<< trim END 2032 vim9script 2033 new 2034 setline(1, 'something') 2035 :substitute(some(other( 2036 assert_equal('otherthing', getline(1)) 2037 bwipe! 2038 END 2039 writefile(lines, 'Xvim9lines') 2040 source Xvim9lines 2041 2042 delete('Xvim9lines') 2043enddef 2044 2045" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker 2046