1" Test using builtin functions in the Vim9 script language. 2 3source check.vim 4source vim9.vim 5 6" Test for passing too many or too few arguments to builtin functions 7func Test_internalfunc_arg_error() 8 let l =<< trim END 9 def! FArgErr(): float 10 return ceil(1.1, 2) 11 enddef 12 defcompile 13 END 14 call writefile(l, 'Xinvalidarg') 15 call assert_fails('so Xinvalidarg', 'E118:', '', 1, 'FArgErr') 16 let l =<< trim END 17 def! FArgErr(): float 18 return ceil() 19 enddef 20 defcompile 21 END 22 call writefile(l, 'Xinvalidarg') 23 call assert_fails('so Xinvalidarg', 'E119:', '', 1, 'FArgErr') 24 call delete('Xinvalidarg') 25endfunc 26 27" Test for builtin functions returning different types 28func Test_InternalFuncRetType() 29 let lines =<< trim END 30 def RetFloat(): float 31 return ceil(1.456) 32 enddef 33 34 def RetListAny(): list<any> 35 return items({k: 'v'}) 36 enddef 37 38 def RetListString(): list<string> 39 return split('a:b:c', ':') 40 enddef 41 42 def RetListDictAny(): list<dict<any>> 43 return getbufinfo() 44 enddef 45 46 def RetDictNumber(): dict<number> 47 return wordcount() 48 enddef 49 50 def RetDictString(): dict<string> 51 return environ() 52 enddef 53 END 54 call writefile(lines, 'Xscript') 55 source Xscript 56 57 call RetFloat()->assert_equal(2.0) 58 call RetListAny()->assert_equal([['k', 'v']]) 59 call RetListString()->assert_equal(['a', 'b', 'c']) 60 call RetListDictAny()->assert_notequal([]) 61 call RetDictNumber()->assert_notequal({}) 62 call RetDictString()->assert_notequal({}) 63 call delete('Xscript') 64endfunc 65 66def Test_abs() 67 assert_equal(0, abs(0)) 68 assert_equal(2, abs(-2)) 69 assert_equal(3, abs(3)) 70 CheckDefFailure(['abs("text")'], 'E1013: Argument 1: type mismatch, expected number but got string', 1) 71 if has('float') 72 assert_equal(0, abs(0)) 73 assert_equal(2.0, abs(-2.0)) 74 assert_equal(3.0, abs(3.0)) 75 endif 76enddef 77 78def Test_add_list() 79 var l: list<number> # defaults to empty list 80 add(l, 9) 81 assert_equal([9], l) 82 83 var lines =<< trim END 84 var l: list<number> 85 add(l, "x") 86 END 87 CheckDefFailure(lines, 'E1012:', 2) 88 89 lines =<< trim END 90 add(test_null_list(), 123) 91 END 92 CheckDefExecAndScriptFailure(lines, 'E1130:', 1) 93 94 lines =<< trim END 95 var l: list<number> = test_null_list() 96 add(l, 123) 97 END 98 CheckDefExecFailure(lines, 'E1130:', 2) 99 100 # Getting variable with NULL list allocates a new list at script level 101 lines =<< trim END 102 vim9script 103 var l: list<number> = test_null_list() 104 add(l, 123) 105 END 106 CheckScriptSuccess(lines) 107enddef 108 109def Test_add_blob() 110 var b1: blob = 0z12 111 add(b1, 0x34) 112 assert_equal(0z1234, b1) 113 114 var b2: blob # defaults to empty blob 115 add(b2, 0x67) 116 assert_equal(0z67, b2) 117 118 var lines =<< trim END 119 var b: blob 120 add(b, "x") 121 END 122 CheckDefFailure(lines, 'E1012:', 2) 123 124 lines =<< trim END 125 add(test_null_blob(), 123) 126 END 127 CheckDefExecAndScriptFailure(lines, 'E1131:', 1) 128 129 lines =<< trim END 130 var b: blob = test_null_blob() 131 add(b, 123) 132 END 133 CheckDefExecFailure(lines, 'E1131:', 2) 134 135 # Getting variable with NULL blob allocates a new blob at script level 136 lines =<< trim END 137 vim9script 138 var b: blob = test_null_blob() 139 add(b, 123) 140 END 141 CheckScriptSuccess(lines) 142enddef 143 144def Test_append() 145 new 146 setline(1, range(3)) 147 var res1: number = append(1, 'one') 148 assert_equal(0, res1) 149 var res2: bool = append(3, 'two') 150 assert_equal(false, res2) 151 assert_equal(['0', 'one', '1', 'two', '2'], getline(1, 6)) 152 153 append(0, 'zero') 154 assert_equal('zero', getline(1)) 155 bwipe! 156enddef 157 158def Test_balloon_show() 159 CheckGui 160 CheckFeature balloon_eval 161 162 assert_fails('balloon_show(true)', 'E1174:') 163enddef 164 165def Test_balloon_split() 166 CheckFeature balloon_eval_term 167 168 assert_fails('balloon_split(true)', 'E1174:') 169enddef 170 171def Test_browse() 172 CheckFeature browse 173 174 var lines =<< trim END 175 browse(1, 2, 3, 4) 176 END 177 CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 2') 178 lines =<< trim END 179 browse(1, 'title', 3, 4) 180 END 181 CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 3') 182 lines =<< trim END 183 browse(1, 'title', 'dir', 4) 184 END 185 CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 4') 186enddef 187 188def Test_bufexists() 189 assert_fails('bufexists(true)', 'E1174') 190enddef 191 192def Test_buflisted() 193 var res: bool = buflisted('asdf') 194 assert_equal(false, res) 195 assert_fails('buflisted(true)', 'E1174') 196enddef 197 198def Test_bufname() 199 split SomeFile 200 bufname('%')->assert_equal('SomeFile') 201 edit OtherFile 202 bufname('#')->assert_equal('SomeFile') 203 close 204enddef 205 206def Test_bufnr() 207 var buf = bufnr() 208 bufnr('%')->assert_equal(buf) 209 210 buf = bufnr('Xdummy', true) 211 buf->assert_notequal(-1) 212 exe 'bwipe! ' .. buf 213enddef 214 215def Test_bufwinid() 216 var origwin = win_getid() 217 below split SomeFile 218 var SomeFileID = win_getid() 219 below split OtherFile 220 below split SomeFile 221 bufwinid('SomeFile')->assert_equal(SomeFileID) 222 223 win_gotoid(origwin) 224 only 225 bwipe SomeFile 226 bwipe OtherFile 227 228 assert_fails('bufwinid(true)', 'E1138') 229enddef 230 231def Test_call_call() 232 var l = [3, 2, 1] 233 call('reverse', [l]) 234 l->assert_equal([1, 2, 3]) 235enddef 236 237def Test_ch_logfile() 238 if !has('channel') 239 CheckFeature channel 240 endif 241 assert_fails('ch_logfile(true)', 'E1174') 242 assert_fails('ch_logfile("foo", true)', 'E1174') 243enddef 244 245def Test_char2nr() 246 char2nr('あ', true)->assert_equal(12354) 247 248 assert_fails('char2nr(true)', 'E1174') 249enddef 250 251def Test_charclass() 252 assert_fails('charclass(true)', 'E1174') 253enddef 254 255def Test_chdir() 256 assert_fails('chdir(true)', 'E1174') 257enddef 258 259def Test_col() 260 new 261 setline(1, 'asdf') 262 col([1, '$'])->assert_equal(5) 263 264 assert_fails('col(true)', 'E1174') 265enddef 266 267def Test_confirm() 268 if !has('dialog_con') && !has('dialog_gui') 269 CheckFeature dialog_con 270 endif 271 272 assert_fails('confirm(true)', 'E1174') 273 assert_fails('confirm("yes", true)', 'E1174') 274 assert_fails('confirm("yes", "maybe", 2, true)', 'E1174') 275enddef 276 277def Test_copy_return_type() 278 var l = copy([1, 2, 3]) 279 var res = 0 280 for n in l 281 res += n 282 endfor 283 res->assert_equal(6) 284 285 var dl = deepcopy([1, 2, 3]) 286 res = 0 287 for n in dl 288 res += n 289 endfor 290 res->assert_equal(6) 291 292 dl = deepcopy([1, 2, 3], true) 293enddef 294 295def Test_count() 296 count('ABC ABC ABC', 'b', true)->assert_equal(3) 297 count('ABC ABC ABC', 'b', false)->assert_equal(0) 298enddef 299 300def Test_cursor() 301 new 302 setline(1, range(4)) 303 cursor(2, 1) 304 assert_equal(2, getcurpos()[1]) 305 cursor('$', 1) 306 assert_equal(4, getcurpos()[1]) 307 308 var lines =<< trim END 309 cursor('2', 1) 310 END 311 CheckDefExecAndScriptFailure(lines, 'E475:') 312enddef 313 314def Test_delete() 315 var res: bool = delete('doesnotexist') 316 assert_equal(true, res) 317enddef 318 319def Test_executable() 320 assert_false(executable("")) 321 assert_false(executable(test_null_string())) 322 323 CheckDefExecFailure(['echo executable(123)'], 'E1174:') 324 CheckDefExecFailure(['echo executable(true)'], 'E1174:') 325enddef 326 327def Test_execute() 328 var res = execute("echo 'hello'") 329 assert_equal("\nhello", res) 330 res = execute(["echo 'here'", "echo 'there'"]) 331 assert_equal("\nhere\nthere", res) 332 333 CheckDefFailure(['echo execute(123)'], 'E1013: Argument 1: type mismatch, expected string but got number') 334 CheckDefFailure(['echo execute([123])'], 'E1013: Argument 1: type mismatch, expected list<string> but got list<number>') 335 CheckDefExecFailure(['echo execute(["xx", 123])'], 'E492') 336 CheckDefFailure(['echo execute("xx", 123)'], 'E1013: Argument 2: type mismatch, expected string but got number') 337enddef 338 339def Test_exepath() 340 CheckDefExecFailure(['echo exepath(true)'], 'E1174:') 341 CheckDefExecFailure(['echo exepath(v:null)'], 'E1174:') 342 CheckDefExecFailure(['echo exepath("")'], 'E1175:') 343enddef 344 345def Test_expand() 346 split SomeFile 347 expand('%', true, true)->assert_equal(['SomeFile']) 348 close 349enddef 350 351def Test_expandcmd() 352 $FOO = "blue" 353 assert_equal("blue sky", expandcmd("`=$FOO .. ' sky'`")) 354 355 assert_equal("yes", expandcmd("`={a: 'yes'}['a']`")) 356enddef 357 358def Test_extend_arg_types() 359 g:number_one = 1 360 g:string_keep = 'keep' 361 var lines =<< trim END 362 assert_equal([1, 2, 3], extend([1, 2], [3])) 363 assert_equal([3, 1, 2], extend([1, 2], [3], 0)) 364 assert_equal([1, 3, 2], extend([1, 2], [3], 1)) 365 assert_equal([1, 3, 2], extend([1, 2], [3], g:number_one)) 366 367 assert_equal({a: 1, b: 2, c: 3}, extend({a: 1, b: 2}, {c: 3})) 368 assert_equal({a: 1, b: 4}, extend({a: 1, b: 2}, {b: 4})) 369 assert_equal({a: 1, b: 2}, extend({a: 1, b: 2}, {b: 4}, 'keep')) 370 assert_equal({a: 1, b: 2}, extend({a: 1, b: 2}, {b: 4}, g:string_keep)) 371 372 var res: list<dict<any>> 373 extend(res, mapnew([1, 2], (_, v) => ({}))) 374 assert_equal([{}, {}], res) 375 END 376 CheckDefAndScriptSuccess(lines) 377 378 CheckDefFailure(['extend("a", 1)'], 'E1013: Argument 1: type mismatch, expected list<any> but got string') 379 CheckDefFailure(['extend([1, 2], 3)'], 'E1013: Argument 2: type mismatch, expected list<number> but got number') 380 CheckDefFailure(['extend([1, 2], ["x"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>') 381 CheckDefFailure(['extend([1, 2], [3], "x")'], 'E1013: Argument 3: type mismatch, expected number but got string') 382 383 CheckDefFailure(['extend({a: 1}, 42)'], 'E1013: Argument 2: type mismatch, expected dict<number> but got number') 384 CheckDefFailure(['extend({a: 1}, {b: "x"})'], 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>') 385 CheckDefFailure(['extend({a: 1}, {b: 2}, 1)'], 'E1013: Argument 3: type mismatch, expected string but got number') 386 387 CheckDefFailure(['extend([1], ["b"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>') 388 CheckDefExecFailure(['extend([1], ["b", 1])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<any>') 389enddef 390 391def Test_extendnew() 392 assert_equal([1, 2, 'a'], extendnew([1, 2], ['a'])) 393 assert_equal({one: 1, two: 'a'}, extendnew({one: 1}, {two: 'a'})) 394 395 CheckDefFailure(['extendnew({a: 1}, 42)'], 'E1013: Argument 2: type mismatch, expected dict<number> but got number') 396 CheckDefFailure(['extendnew({a: 1}, [42])'], 'E1013: Argument 2: type mismatch, expected dict<number> but got list<number>') 397 CheckDefFailure(['extendnew([1, 2], "x")'], 'E1013: Argument 2: type mismatch, expected list<number> but got string') 398 CheckDefFailure(['extendnew([1, 2], {x: 1})'], 'E1013: Argument 2: type mismatch, expected list<number> but got dict<number>') 399enddef 400 401def Test_extend_return_type() 402 var l = extend([1, 2], [3]) 403 var res = 0 404 for n in l 405 res += n 406 endfor 407 res->assert_equal(6) 408enddef 409 410func g:ExtendDict(d) 411 call extend(a:d, #{xx: 'x'}) 412endfunc 413 414def Test_extend_dict_item_type() 415 var lines =<< trim END 416 var d: dict<number> = {a: 1} 417 extend(d, {b: 2}) 418 END 419 CheckDefAndScriptSuccess(lines) 420 421 lines =<< trim END 422 var d: dict<number> = {a: 1} 423 extend(d, {b: 'x'}) 424 END 425 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>', 2) 426 427 lines =<< trim END 428 var d: dict<number> = {a: 1} 429 g:ExtendDict(d) 430 END 431 CheckDefExecFailure(lines, 'E1012: Type mismatch; expected number but got string', 0) 432 CheckScriptFailure(['vim9script'] + lines, 'E1012:', 1) 433enddef 434 435func g:ExtendList(l) 436 call extend(a:l, ['x']) 437endfunc 438 439def Test_extend_list_item_type() 440 var lines =<< trim END 441 var l: list<number> = [1] 442 extend(l, [2]) 443 END 444 CheckDefAndScriptSuccess(lines) 445 446 lines =<< trim END 447 var l: list<number> = [1] 448 extend(l, ['x']) 449 END 450 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>', 2) 451 452 lines =<< trim END 453 var l: list<number> = [1] 454 g:ExtendList(l) 455 END 456 CheckDefExecFailure(lines, 'E1012: Type mismatch; expected number but got string', 0) 457 CheckScriptFailure(['vim9script'] + lines, 'E1012:', 1) 458enddef 459 460def Test_extend_with_error_function() 461 var lines =<< trim END 462 vim9script 463 def F() 464 { 465 var m = 10 466 } 467 echo m 468 enddef 469 470 def Test() 471 var d: dict<any> = {} 472 d->extend({A: 10, Func: function('F', [])}) 473 enddef 474 475 Test() 476 END 477 CheckScriptFailure(lines, 'E1001: Variable not found: m') 478enddef 479 480def Test_job_info_return_type() 481 if has('job') 482 job_start(&shell) 483 var jobs = job_info() 484 assert_equal('list<job>', typename(jobs)) 485 assert_equal('dict<any>', typename(job_info(jobs[0]))) 486 job_stop(jobs[0]) 487 endif 488enddef 489 490def Test_filereadable() 491 assert_false(filereadable("")) 492 assert_false(filereadable(test_null_string())) 493 494 CheckDefExecFailure(['echo filereadable(123)'], 'E1174:') 495 CheckDefExecFailure(['echo filereadable(true)'], 'E1174:') 496enddef 497 498def Test_filewritable() 499 assert_false(filewritable("")) 500 assert_false(filewritable(test_null_string())) 501 502 CheckDefExecFailure(['echo filewritable(123)'], 'E1174:') 503 CheckDefExecFailure(['echo filewritable(true)'], 'E1174:') 504enddef 505 506def Test_finddir() 507 CheckDefExecFailure(['echo finddir(true)'], 'E1174:') 508 CheckDefExecFailure(['echo finddir(v:null)'], 'E1174:') 509 CheckDefExecFailure(['echo finddir("")'], 'E1175:') 510enddef 511 512def Test_findfile() 513 CheckDefExecFailure(['echo findfile(true)'], 'E1174:') 514 CheckDefExecFailure(['echo findfile(v:null)'], 'E1174:') 515 CheckDefExecFailure(['echo findfile("")'], 'E1175:') 516enddef 517 518def Test_flattennew() 519 var lines =<< trim END 520 var l = [1, [2, [3, 4]], 5] 521 call assert_equal([1, 2, 3, 4, 5], flattennew(l)) 522 call assert_equal([1, [2, [3, 4]], 5], l) 523 524 call assert_equal([1, 2, [3, 4], 5], flattennew(l, 1)) 525 call assert_equal([1, [2, [3, 4]], 5], l) 526 END 527 CheckDefAndScriptSuccess(lines) 528 529 lines =<< trim END 530 echo flatten([1, 2, 3]) 531 END 532 CheckDefAndScriptFailure(lines, 'E1158:') 533enddef 534 535def Test_fnamemodify() 536 CheckDefSuccess(['echo fnamemodify(test_null_string(), ":p")']) 537 CheckDefSuccess(['echo fnamemodify("", ":p")']) 538 CheckDefSuccess(['echo fnamemodify("file", test_null_string())']) 539 CheckDefSuccess(['echo fnamemodify("file", "")']) 540 541 CheckDefExecFailure(['echo fnamemodify(true, ":p")'], 'E1174: String required for argument 1') 542 CheckDefExecFailure(['echo fnamemodify(v:null, ":p")'], 'E1174: String required for argument 1') 543 CheckDefExecFailure(['echo fnamemodify("file", true)'], 'E1174: String required for argument 2') 544enddef 545 546def Wrong_dict_key_type(items: list<number>): list<number> 547 return filter(items, (_, val) => get({[val]: 1}, 'x')) 548enddef 549 550def Test_filter_wrong_dict_key_type() 551 assert_fails('Wrong_dict_key_type([1, v:null, 3])', 'E1013:') 552enddef 553 554def Test_filter_return_type() 555 var l = filter([1, 2, 3], (_, _) => 1) 556 var res = 0 557 for n in l 558 res += n 559 endfor 560 res->assert_equal(6) 561enddef 562 563def Test_filter_missing_argument() 564 var dict = {aa: [1], ab: [2], ac: [3], de: [4]} 565 var res = dict->filter((k, _) => k =~ 'a' && k !~ 'b') 566 res->assert_equal({aa: [1], ac: [3]}) 567enddef 568 569def Test_fullcommand() 570 assert_equal('next', fullcommand('n')) 571 assert_equal('noremap', fullcommand('no')) 572 assert_equal('noremap', fullcommand('nor')) 573 assert_equal('normal', fullcommand('norm')) 574 575 assert_equal('', fullcommand('k')) 576 assert_equal('keepmarks', fullcommand('ke')) 577 assert_equal('keepmarks', fullcommand('kee')) 578 assert_equal('keepmarks', fullcommand('keep')) 579 assert_equal('keepjumps', fullcommand('keepj')) 580 581 assert_equal('dlist', fullcommand('dl')) 582 assert_equal('', fullcommand('dp')) 583 assert_equal('delete', fullcommand('del')) 584 assert_equal('', fullcommand('dell')) 585 assert_equal('', fullcommand('delp')) 586 587 assert_equal('srewind', fullcommand('sre')) 588 assert_equal('scriptnames', fullcommand('scr')) 589 assert_equal('', fullcommand('scg')) 590enddef 591 592def Test_garbagecollect() 593 garbagecollect(true) 594enddef 595 596def Test_getbufinfo() 597 var bufinfo = getbufinfo(bufnr()) 598 getbufinfo('%')->assert_equal(bufinfo) 599 600 edit Xtestfile1 601 hide edit Xtestfile2 602 hide enew 603 getbufinfo({bufloaded: true, buflisted: true, bufmodified: false}) 604 ->len()->assert_equal(3) 605 bwipe Xtestfile1 Xtestfile2 606enddef 607 608def Test_getbufline() 609 e SomeFile 610 var buf = bufnr() 611 e # 612 var lines = ['aaa', 'bbb', 'ccc'] 613 setbufline(buf, 1, lines) 614 getbufline('#', 1, '$')->assert_equal(lines) 615 getbufline(-1, '$', '$')->assert_equal([]) 616 getbufline(-1, 1, '$')->assert_equal([]) 617 618 bwipe! 619enddef 620 621def Test_getchangelist() 622 new 623 setline(1, 'some text') 624 var changelist = bufnr()->getchangelist() 625 getchangelist('%')->assert_equal(changelist) 626 bwipe! 627enddef 628 629def Test_getchar() 630 while getchar(0) 631 endwhile 632 getchar(true)->assert_equal(0) 633enddef 634 635def Test_getenv() 636 if getenv('does-not_exist') == '' 637 assert_report('getenv() should return null') 638 endif 639 if getenv('does-not_exist') == null 640 else 641 assert_report('getenv() should return null') 642 endif 643 $SOMEENVVAR = 'some' 644 assert_equal('some', getenv('SOMEENVVAR')) 645 unlet $SOMEENVVAR 646enddef 647 648def Test_getcompletion() 649 set wildignore=*.vim,*~ 650 var l = getcompletion('run', 'file', true) 651 l->assert_equal([]) 652 set wildignore& 653enddef 654 655def Test_getloclist_return_type() 656 var l = getloclist(1) 657 l->assert_equal([]) 658 659 var d = getloclist(1, {items: 0}) 660 d->assert_equal({items: []}) 661enddef 662 663def Test_getfperm() 664 assert_equal('', getfperm("")) 665 assert_equal('', getfperm(test_null_string())) 666 667 CheckDefExecFailure(['echo getfperm(true)'], 'E1174:') 668 CheckDefExecFailure(['echo getfperm(v:null)'], 'E1174:') 669enddef 670 671def Test_getfsize() 672 assert_equal(-1, getfsize("")) 673 assert_equal(-1, getfsize(test_null_string())) 674 675 CheckDefExecFailure(['echo getfsize(true)'], 'E1174:') 676 CheckDefExecFailure(['echo getfsize(v:null)'], 'E1174:') 677enddef 678 679def Test_getftime() 680 assert_equal(-1, getftime("")) 681 assert_equal(-1, getftime(test_null_string())) 682 683 CheckDefExecFailure(['echo getftime(true)'], 'E1174:') 684 CheckDefExecFailure(['echo getftime(v:null)'], 'E1174:') 685enddef 686 687def Test_getftype() 688 assert_equal('', getftype("")) 689 assert_equal('', getftype(test_null_string())) 690 691 CheckDefExecFailure(['echo getftype(true)'], 'E1174:') 692 CheckDefExecFailure(['echo getftype(v:null)'], 'E1174:') 693enddef 694 695def Test_getqflist_return_type() 696 var l = getqflist() 697 l->assert_equal([]) 698 699 var d = getqflist({items: 0}) 700 d->assert_equal({items: []}) 701enddef 702 703def Test_getreg() 704 var lines = ['aaa', 'bbb', 'ccc'] 705 setreg('a', lines) 706 getreg('a', true, true)->assert_equal(lines) 707 assert_fails('getreg("ab")', 'E1162:') 708enddef 709 710def Test_getreg_return_type() 711 var s1: string = getreg('"') 712 var s2: string = getreg('"', 1) 713 var s3: list<string> = getreg('"', 1, 1) 714enddef 715 716def Test_getreginfo() 717 var text = 'abc' 718 setreg('a', text) 719 getreginfo('a')->assert_equal({regcontents: [text], regtype: 'v', isunnamed: false}) 720 assert_fails('getreginfo("ab")', 'E1162:') 721enddef 722 723def Test_getregtype() 724 var lines = ['aaa', 'bbb', 'ccc'] 725 setreg('a', lines) 726 getregtype('a')->assert_equal('V') 727 assert_fails('getregtype("ab")', 'E1162:') 728enddef 729 730def Test_glob() 731 glob('runtest.vim', true, true, true)->assert_equal(['runtest.vim']) 732enddef 733 734def Test_globpath() 735 globpath('.', 'runtest.vim', true, true, true)->assert_equal(['./runtest.vim']) 736enddef 737 738def Test_has() 739 has('eval', true)->assert_equal(1) 740enddef 741 742def Test_hasmapto() 743 hasmapto('foobar', 'i', true)->assert_equal(0) 744 iabbrev foo foobar 745 hasmapto('foobar', 'i', true)->assert_equal(1) 746 iunabbrev foo 747enddef 748 749def Test_index() 750 index(['a', 'b', 'a', 'B'], 'b', 2, true)->assert_equal(3) 751enddef 752 753let s:number_one = 1 754let s:number_two = 2 755let s:string_keep = 'keep' 756 757def Test_insert() 758 var l = insert([2, 1], 3) 759 var res = 0 760 for n in l 761 res += n 762 endfor 763 res->assert_equal(6) 764 765 var m: any = [] 766 insert(m, 4) 767 call assert_equal([4], m) 768 extend(m, [6], 0) 769 call assert_equal([6, 4], m) 770 771 var lines =<< trim END 772 insert(test_null_list(), 123) 773 END 774 CheckDefExecAndScriptFailure(lines, 'E1130:', 1) 775 776 lines =<< trim END 777 insert(test_null_blob(), 123) 778 END 779 CheckDefExecAndScriptFailure(lines, 'E1131:', 1) 780 781 assert_equal([1, 2, 3], insert([2, 3], 1)) 782 assert_equal([1, 2, 3], insert([2, 3], s:number_one)) 783 assert_equal([1, 2, 3], insert([1, 2], 3, 2)) 784 assert_equal([1, 2, 3], insert([1, 2], 3, s:number_two)) 785 assert_equal(['a', 'b', 'c'], insert(['b', 'c'], 'a')) 786 assert_equal(0z1234, insert(0z34, 0x12)) 787 788 CheckDefFailure(['insert("a", 1)'], 'E1013: Argument 1: type mismatch, expected list<any> but got string', 1) 789 CheckDefFailure(['insert([2, 3], "a")'], 'E1013: Argument 2: type mismatch, expected number but got string', 1) 790 CheckDefFailure(['insert([2, 3], 1, "x")'], 'E1013: Argument 3: type mismatch, expected number but got string', 1) 791enddef 792 793def Test_keys_return_type() 794 const var: list<string> = {a: 1, b: 2}->keys() 795 var->assert_equal(['a', 'b']) 796enddef 797 798def Test_line() 799 assert_fails('line(true)', 'E1174') 800enddef 801 802def Test_list2str_str2list_utf8() 803 var s = "\u3042\u3044" 804 var l = [0x3042, 0x3044] 805 str2list(s, true)->assert_equal(l) 806 list2str(l, true)->assert_equal(s) 807enddef 808 809def SID(): number 810 return expand('<SID>') 811 ->matchstr('<SNR>\zs\d\+\ze_$') 812 ->str2nr() 813enddef 814 815def Test_map_function_arg() 816 var lines =<< trim END 817 def MapOne(i: number, v: string): string 818 return i .. ':' .. v 819 enddef 820 var l = ['a', 'b', 'c'] 821 map(l, MapOne) 822 assert_equal(['0:a', '1:b', '2:c'], l) 823 END 824 CheckDefAndScriptSuccess(lines) 825 826 lines =<< trim END 827 range(3)->map((a, b, c) => a + b + c) 828 END 829 CheckDefExecAndScriptFailure(lines, 'E1190: One argument too few') 830 lines =<< trim END 831 range(3)->map((a, b, c, d) => a + b + c + d) 832 END 833 CheckDefExecAndScriptFailure(lines, 'E1190: 2 arguments too few') 834enddef 835 836def Test_map_item_type() 837 var lines =<< trim END 838 var l = ['a', 'b', 'c'] 839 map(l, (k, v) => k .. '/' .. v ) 840 assert_equal(['0/a', '1/b', '2/c'], l) 841 END 842 CheckDefAndScriptSuccess(lines) 843 844 lines =<< trim END 845 var l: list<number> = [0] 846 echo map(l, (_, v) => []) 847 END 848 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2) 849 850 lines =<< trim END 851 var l: list<number> = range(2) 852 echo map(l, (_, v) => []) 853 END 854 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2) 855 856 lines =<< trim END 857 var d: dict<number> = {key: 0} 858 echo map(d, (_, v) => []) 859 END 860 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2) 861enddef 862 863def Test_maparg() 864 var lnum = str2nr(expand('<sflnum>')) 865 map foo bar 866 maparg('foo', '', false, true)->assert_equal({ 867 lnum: lnum + 1, 868 script: 0, 869 mode: ' ', 870 silent: 0, 871 noremap: 0, 872 lhs: 'foo', 873 lhsraw: 'foo', 874 nowait: 0, 875 expr: 0, 876 sid: SID(), 877 rhs: 'bar', 878 buffer: 0}) 879 unmap foo 880enddef 881 882def Test_mapcheck() 883 iabbrev foo foobar 884 mapcheck('foo', 'i', true)->assert_equal('foobar') 885 iunabbrev foo 886enddef 887 888def Test_maparg_mapset() 889 nnoremap <F3> :echo "hit F3"<CR> 890 var mapsave = maparg('<F3>', 'n', false, true) 891 mapset('n', false, mapsave) 892 893 nunmap <F3> 894enddef 895 896def Test_map_failure() 897 CheckFeature job 898 899 var lines =<< trim END 900 vim9script 901 writefile([], 'Xtmpfile') 902 silent e Xtmpfile 903 var d = {[bufnr('%')]: {a: 0}} 904 au BufReadPost * Func() 905 def Func() 906 if d->has_key('') 907 endif 908 eval d[expand('<abuf>')]->mapnew((_, v: dict<job>) => 0) 909 enddef 910 e 911 END 912 CheckScriptFailure(lines, 'E1013:') 913 au! BufReadPost 914 delete('Xtmpfile') 915enddef 916 917def Test_max() 918 g:flag = true 919 var l1: list<number> = g:flag 920 ? [1, max([2, 3])] 921 : [4, 5] 922 assert_equal([1, 3], l1) 923 924 g:flag = false 925 var l2: list<number> = g:flag 926 ? [1, max([2, 3])] 927 : [4, 5] 928 assert_equal([4, 5], l2) 929enddef 930 931def Test_min() 932 g:flag = true 933 var l1: list<number> = g:flag 934 ? [1, min([2, 3])] 935 : [4, 5] 936 assert_equal([1, 2], l1) 937 938 g:flag = false 939 var l2: list<number> = g:flag 940 ? [1, min([2, 3])] 941 : [4, 5] 942 assert_equal([4, 5], l2) 943enddef 944 945def Test_nr2char() 946 nr2char(97, true)->assert_equal('a') 947enddef 948 949def Test_readdir() 950 eval expand('sautest')->readdir((e) => e[0] !=# '.') 951 eval expand('sautest')->readdirex((e) => e.name[0] !=# '.') 952enddef 953 954def Test_readblob() 955 var blob = 0z12341234 956 writefile(blob, 'Xreadblob') 957 var read: blob = readblob('Xreadblob') 958 assert_equal(blob, read) 959 960 var lines =<< trim END 961 var read: list<string> = readblob('Xreadblob') 962 END 963 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected list<string> but got blob', 1) 964 delete('Xreadblob') 965enddef 966 967def Test_readfile() 968 var text = ['aaa', 'bbb', 'ccc'] 969 writefile(text, 'Xreadfile') 970 var read: list<string> = readfile('Xreadfile') 971 assert_equal(text, read) 972 973 var lines =<< trim END 974 var read: dict<string> = readfile('Xreadfile') 975 END 976 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected dict<string> but got list<string>', 1) 977 delete('Xreadfile') 978enddef 979 980def Test_remove_return_type() 981 var l = remove({one: [1, 2], two: [3, 4]}, 'one') 982 var res = 0 983 for n in l 984 res += n 985 endfor 986 res->assert_equal(3) 987enddef 988 989def Test_reverse_return_type() 990 var l = reverse([1, 2, 3]) 991 var res = 0 992 for n in l 993 res += n 994 endfor 995 res->assert_equal(6) 996enddef 997 998def Test_search() 999 new 1000 setline(1, ['foo', 'bar']) 1001 var val = 0 1002 # skip expr returns boolean 1003 search('bar', 'W', 0, 0, () => val == 1)->assert_equal(2) 1004 :1 1005 search('bar', 'W', 0, 0, () => val == 0)->assert_equal(0) 1006 # skip expr returns number, only 0 and 1 are accepted 1007 :1 1008 search('bar', 'W', 0, 0, () => 0)->assert_equal(2) 1009 :1 1010 search('bar', 'W', 0, 0, () => 1)->assert_equal(0) 1011 assert_fails("search('bar', '', 0, 0, () => -1)", 'E1023:') 1012 assert_fails("search('bar', '', 0, 0, () => -1)", 'E1023:') 1013 1014 setline(1, "find this word") 1015 normal gg 1016 var col = 7 1017 assert_equal(1, search('this', '', 0, 0, 'col(".") > col')) 1018 normal 0 1019 assert_equal([1, 6], searchpos('this', '', 0, 0, 'col(".") > col')) 1020 1021 col = 5 1022 normal 0 1023 assert_equal(0, search('this', '', 0, 0, 'col(".") > col')) 1024 normal 0 1025 assert_equal([0, 0], searchpos('this', '', 0, 0, 'col(".") > col')) 1026 bwipe! 1027enddef 1028 1029def Test_searchcount() 1030 new 1031 setline(1, "foo bar") 1032 :/foo 1033 searchcount({recompute: true}) 1034 ->assert_equal({ 1035 exact_match: 1, 1036 current: 1, 1037 total: 1, 1038 maxcount: 99, 1039 incomplete: 0}) 1040 bwipe! 1041enddef 1042 1043def Test_searchpair() 1044 new 1045 setline(1, "here { and } there") 1046 1047 normal f{ 1048 var col = 15 1049 assert_equal(1, searchpair('{', '', '}', '', 'col(".") > col')) 1050 assert_equal(12, col('.')) 1051 normal 0f{ 1052 assert_equal([1, 12], searchpairpos('{', '', '}', '', 'col(".") > col')) 1053 1054 col = 8 1055 normal 0f{ 1056 assert_equal(0, searchpair('{', '', '}', '', 'col(".") > col')) 1057 assert_equal(6, col('.')) 1058 normal 0f{ 1059 assert_equal([0, 0], searchpairpos('{', '', '}', '', 'col(".") > col')) 1060 1061 var lines =<< trim END 1062 vim9script 1063 setline(1, '()') 1064 normal gg 1065 def Fail() 1066 try 1067 searchpairpos('(', '', ')', 'nW', '[0]->map("")') 1068 catch 1069 g:caught = 'yes' 1070 endtry 1071 enddef 1072 Fail() 1073 END 1074 CheckScriptSuccess(lines) 1075 assert_equal('yes', g:caught) 1076 1077 unlet g:caught 1078 bwipe! 1079enddef 1080 1081def Test_set_get_bufline() 1082 # similar to Test_setbufline_getbufline() 1083 var lines =<< trim END 1084 new 1085 var b = bufnr('%') 1086 hide 1087 assert_equal(0, setbufline(b, 1, ['foo', 'bar'])) 1088 assert_equal(['foo'], getbufline(b, 1)) 1089 assert_equal(['bar'], getbufline(b, '$')) 1090 assert_equal(['foo', 'bar'], getbufline(b, 1, 2)) 1091 exe "bd!" b 1092 assert_equal([], getbufline(b, 1, 2)) 1093 1094 split Xtest 1095 setline(1, ['a', 'b', 'c']) 1096 b = bufnr('%') 1097 wincmd w 1098 1099 assert_equal(1, setbufline(b, 5, 'x')) 1100 assert_equal(1, setbufline(b, 5, ['x'])) 1101 assert_equal(1, setbufline(b, 5, [])) 1102 assert_equal(1, setbufline(b, 5, test_null_list())) 1103 1104 assert_equal(1, 'x'->setbufline(bufnr('$') + 1, 1)) 1105 assert_equal(1, ['x']->setbufline(bufnr('$') + 1, 1)) 1106 assert_equal(1, []->setbufline(bufnr('$') + 1, 1)) 1107 assert_equal(1, test_null_list()->setbufline(bufnr('$') + 1, 1)) 1108 1109 assert_equal(['a', 'b', 'c'], getbufline(b, 1, '$')) 1110 1111 assert_equal(0, setbufline(b, 4, ['d', 'e'])) 1112 assert_equal(['c'], b->getbufline(3)) 1113 assert_equal(['d'], getbufline(b, 4)) 1114 assert_equal(['e'], getbufline(b, 5)) 1115 assert_equal([], getbufline(b, 6)) 1116 assert_equal([], getbufline(b, 2, 1)) 1117 1118 if has('job') 1119 setbufline(b, 2, [function('eval'), {key: 123}, string(test_null_job())]) 1120 assert_equal(["function('eval')", 1121 "{'key': 123}", 1122 "no process"], 1123 getbufline(b, 2, 4)) 1124 endif 1125 1126 exe 'bwipe! ' .. b 1127 END 1128 CheckDefAndScriptSuccess(lines) 1129enddef 1130 1131def Test_searchdecl() 1132 searchdecl('blah', true, true)->assert_equal(1) 1133enddef 1134 1135def Test_setbufvar() 1136 setbufvar(bufnr('%'), '&syntax', 'vim') 1137 &syntax->assert_equal('vim') 1138 setbufvar(bufnr('%'), '&ts', 16) 1139 &ts->assert_equal(16) 1140 setbufvar(bufnr('%'), '&ai', true) 1141 &ai->assert_equal(true) 1142 setbufvar(bufnr('%'), '&ft', 'filetype') 1143 &ft->assert_equal('filetype') 1144 1145 settabwinvar(1, 1, '&syntax', 'vam') 1146 &syntax->assert_equal('vam') 1147 settabwinvar(1, 1, '&ts', 15) 1148 &ts->assert_equal(15) 1149 setlocal ts=8 1150 settabwinvar(1, 1, '&list', false) 1151 &list->assert_equal(false) 1152 settabwinvar(1, 1, '&list', true) 1153 &list->assert_equal(true) 1154 setlocal list& 1155 1156 setbufvar('%', 'myvar', 123) 1157 getbufvar('%', 'myvar')->assert_equal(123) 1158enddef 1159 1160def Test_setloclist() 1161 var items = [{filename: '/tmp/file', lnum: 1, valid: true}] 1162 var what = {items: items} 1163 setqflist([], ' ', what) 1164 setloclist(0, [], ' ', what) 1165enddef 1166 1167def Test_setreg() 1168 setreg('a', ['aaa', 'bbb', 'ccc']) 1169 var reginfo = getreginfo('a') 1170 setreg('a', reginfo) 1171 getreginfo('a')->assert_equal(reginfo) 1172 assert_fails('setreg("ab", 0)', 'E1162:') 1173enddef 1174 1175def Test_slice() 1176 assert_equal('12345', slice('012345', 1)) 1177 assert_equal('123', slice('012345', 1, 4)) 1178 assert_equal('1234', slice('012345', 1, -1)) 1179 assert_equal('1', slice('012345', 1, -4)) 1180 assert_equal('', slice('012345', 1, -5)) 1181 assert_equal('', slice('012345', 1, -6)) 1182 1183 assert_equal([1, 2, 3, 4, 5], slice(range(6), 1)) 1184 assert_equal([1, 2, 3], slice(range(6), 1, 4)) 1185 assert_equal([1, 2, 3, 4], slice(range(6), 1, -1)) 1186 assert_equal([1], slice(range(6), 1, -4)) 1187 assert_equal([], slice(range(6), 1, -5)) 1188 assert_equal([], slice(range(6), 1, -6)) 1189 1190 assert_equal(0z1122334455, slice(0z001122334455, 1)) 1191 assert_equal(0z112233, slice(0z001122334455, 1, 4)) 1192 assert_equal(0z11223344, slice(0z001122334455, 1, -1)) 1193 assert_equal(0z11, slice(0z001122334455, 1, -4)) 1194 assert_equal(0z, slice(0z001122334455, 1, -5)) 1195 assert_equal(0z, slice(0z001122334455, 1, -6)) 1196enddef 1197 1198def Test_spellsuggest() 1199 if !has('spell') 1200 MissingFeature 'spell' 1201 else 1202 spellsuggest('marrch', 1, true)->assert_equal(['March']) 1203 endif 1204enddef 1205 1206def Test_sort_return_type() 1207 var res: list<number> 1208 res = [1, 2, 3]->sort() 1209enddef 1210 1211def Test_sort_argument() 1212 var lines =<< trim END 1213 var res = ['b', 'a', 'c']->sort('i') 1214 res->assert_equal(['a', 'b', 'c']) 1215 1216 def Compare(a: number, b: number): number 1217 return a - b 1218 enddef 1219 var l = [3, 6, 7, 1, 8, 2, 4, 5] 1220 sort(l, Compare) 1221 assert_equal([1, 2, 3, 4, 5, 6, 7, 8], l) 1222 END 1223 CheckDefAndScriptSuccess(lines) 1224enddef 1225 1226def Test_split() 1227 split(' aa bb ', '\W\+', true)->assert_equal(['', 'aa', 'bb', '']) 1228enddef 1229 1230def Run_str2float() 1231 if !has('float') 1232 MissingFeature 'float' 1233 endif 1234 str2float("1.00")->assert_equal(1.00) 1235 str2float("2e-2")->assert_equal(0.02) 1236 1237 CheckDefFailure(['echo str2float(123)'], 'E1013:') 1238 CheckScriptFailure(['vim9script', 'echo str2float(123)'], 'E1024:') 1239 endif 1240enddef 1241 1242def Test_str2nr() 1243 str2nr("1'000'000", 10, true)->assert_equal(1000000) 1244 1245 CheckDefFailure(['echo str2nr(123)'], 'E1013:') 1246 CheckScriptFailure(['vim9script', 'echo str2nr(123)'], 'E1024:') 1247 CheckDefFailure(['echo str2nr("123", "x")'], 'E1013:') 1248 CheckScriptFailure(['vim9script', 'echo str2nr("123", "x")'], 'E1030:') 1249 CheckDefFailure(['echo str2nr("123", 10, "x")'], 'E1013:') 1250 CheckScriptFailure(['vim9script', 'echo str2nr("123", 10, "x")'], 'E1135:') 1251enddef 1252 1253def Test_strchars() 1254 strchars("A\u20dd", true)->assert_equal(1) 1255enddef 1256 1257def Test_submatch() 1258 var pat = 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)' 1259 var Rep = () => range(10)->mapnew((_, v) => submatch(v, true))->string() 1260 var actual = substitute('A123456789', pat, Rep, '') 1261 var expected = "[['A123456789'], ['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9']]" 1262 actual->assert_equal(expected) 1263enddef 1264 1265def Test_substitute() 1266 var res = substitute('A1234', '\d', 'X', '') 1267 assert_equal('AX234', res) 1268 1269 if has('job') 1270 assert_fails('"text"->substitute(".*", () => job_start(":"), "")', 'E908: using an invalid value as a String: job') 1271 assert_fails('"text"->substitute(".*", () => job_start(":")->job_getchannel(), "")', 'E908: using an invalid value as a String: channel') 1272 endif 1273enddef 1274 1275def Test_synID() 1276 new 1277 setline(1, "text") 1278 synID(1, 1, true)->assert_equal(0) 1279 bwipe! 1280enddef 1281 1282def Test_term_gettty() 1283 if !has('terminal') 1284 MissingFeature 'terminal' 1285 else 1286 var buf = Run_shell_in_terminal({}) 1287 term_gettty(buf, true)->assert_notequal('') 1288 StopShellInTerminal(buf) 1289 endif 1290enddef 1291 1292def Test_term_start() 1293 if !has('terminal') 1294 MissingFeature 'terminal' 1295 else 1296 botright new 1297 var winnr = winnr() 1298 term_start(&shell, {curwin: true}) 1299 winnr()->assert_equal(winnr) 1300 bwipe! 1301 endif 1302enddef 1303 1304def Test_timer_paused() 1305 var id = timer_start(50, () => 0) 1306 timer_pause(id, true) 1307 var info = timer_info(id) 1308 info[0]['paused']->assert_equal(1) 1309 timer_stop(id) 1310enddef 1311 1312def Test_win_execute() 1313 assert_equal("\n" .. winnr(), win_execute(win_getid(), 'echo winnr()')) 1314 assert_equal('', win_execute(342343, 'echo winnr()')) 1315enddef 1316 1317def Test_win_splitmove() 1318 split 1319 win_splitmove(1, 2, {vertical: true, rightbelow: true}) 1320 close 1321enddef 1322 1323def Test_winrestcmd() 1324 split 1325 var cmd = winrestcmd() 1326 wincmd _ 1327 exe cmd 1328 assert_equal(cmd, winrestcmd()) 1329 close 1330enddef 1331 1332def Test_winsaveview() 1333 var view: dict<number> = winsaveview() 1334 1335 var lines =<< trim END 1336 var view: list<number> = winsaveview() 1337 END 1338 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected list<number> but got dict<number>', 1) 1339enddef 1340 1341 1342 1343 1344" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker 1345