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