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_and() 145 CheckDefFailure(['echo and("x", 0x2)'], 'E1013: Argument 1: type mismatch, expected number but got string') 146 CheckDefFailure(['echo and(0x1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string') 147enddef 148 149def Test_append() 150 new 151 setline(1, range(3)) 152 var res1: number = append(1, 'one') 153 assert_equal(0, res1) 154 var res2: bool = append(3, 'two') 155 assert_equal(false, res2) 156 assert_equal(['0', 'one', '1', 'two', '2'], getline(1, 6)) 157 158 append(0, 'zero') 159 assert_equal('zero', getline(1)) 160 bwipe! 161enddef 162 163def Test_argc() 164 CheckDefFailure(['echo argc("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 165enddef 166 167def Test_arglistid() 168 CheckDefFailure(['echo arglistid("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 169 CheckDefFailure(['echo arglistid(1, "y")'], 'E1013: Argument 2: type mismatch, expected number but got string') 170 CheckDefFailure(['echo arglistid("x", "y")'], 'E1013: Argument 1: type mismatch, expected number but got string') 171enddef 172 173def Test_argv() 174 CheckDefFailure(['echo argv("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 175 CheckDefFailure(['echo argv(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string') 176 CheckDefFailure(['echo argv("x", "y")'], 'E1013: Argument 1: type mismatch, expected number but got string') 177enddef 178 179def Test_balloon_show() 180 CheckGui 181 CheckFeature balloon_eval 182 183 assert_fails('balloon_show(true)', 'E1174:') 184enddef 185 186def Test_balloon_split() 187 CheckFeature balloon_eval_term 188 189 assert_fails('balloon_split(true)', 'E1174:') 190enddef 191 192def Test_browse() 193 CheckFeature browse 194 195 var lines =<< trim END 196 browse(1, 2, 3, 4) 197 END 198 CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 2') 199 lines =<< trim END 200 browse(1, 'title', 3, 4) 201 END 202 CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 3') 203 lines =<< trim END 204 browse(1, 'title', 'dir', 4) 205 END 206 CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 4') 207enddef 208 209def Test_bufexists() 210 assert_fails('bufexists(true)', 'E1174') 211enddef 212 213def Test_buflisted() 214 var res: bool = buflisted('asdf') 215 assert_equal(false, res) 216 assert_fails('buflisted(true)', 'E1174') 217enddef 218 219def Test_bufname() 220 split SomeFile 221 bufname('%')->assert_equal('SomeFile') 222 edit OtherFile 223 bufname('#')->assert_equal('SomeFile') 224 close 225enddef 226 227def Test_bufnr() 228 var buf = bufnr() 229 bufnr('%')->assert_equal(buf) 230 231 buf = bufnr('Xdummy', true) 232 buf->assert_notequal(-1) 233 exe 'bwipe! ' .. buf 234enddef 235 236def Test_bufwinid() 237 var origwin = win_getid() 238 below split SomeFile 239 var SomeFileID = win_getid() 240 below split OtherFile 241 below split SomeFile 242 bufwinid('SomeFile')->assert_equal(SomeFileID) 243 244 win_gotoid(origwin) 245 only 246 bwipe SomeFile 247 bwipe OtherFile 248 249 assert_fails('bufwinid(true)', 'E1138') 250enddef 251 252def Test_call_call() 253 var l = [3, 2, 1] 254 call('reverse', [l]) 255 l->assert_equal([1, 2, 3]) 256enddef 257 258def Test_ch_logfile() 259 if !has('channel') 260 CheckFeature channel 261 endif 262 assert_fails('ch_logfile(true)', 'E1174') 263 assert_fails('ch_logfile("foo", true)', 'E1174') 264enddef 265 266def Test_char2nr() 267 char2nr('あ', true)->assert_equal(12354) 268 269 assert_fails('char2nr(true)', 'E1174') 270enddef 271 272def Test_charclass() 273 assert_fails('charclass(true)', 'E1174') 274enddef 275 276def Test_chdir() 277 assert_fails('chdir(true)', 'E1174') 278enddef 279 280def Test_clearmatches() 281 CheckDefFailure(['echo clearmatches("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 282enddef 283 284def Test_col() 285 new 286 setline(1, 'asdf') 287 col([1, '$'])->assert_equal(5) 288 289 assert_fails('col(true)', 'E1174') 290enddef 291 292def Test_confirm() 293 if !has('dialog_con') && !has('dialog_gui') 294 CheckFeature dialog_con 295 endif 296 297 assert_fails('confirm(true)', 'E1174') 298 assert_fails('confirm("yes", true)', 'E1174') 299 assert_fails('confirm("yes", "maybe", 2, true)', 'E1174') 300enddef 301 302def Test_copy_return_type() 303 var l = copy([1, 2, 3]) 304 var res = 0 305 for n in l 306 res += n 307 endfor 308 res->assert_equal(6) 309 310 var dl = deepcopy([1, 2, 3]) 311 res = 0 312 for n in dl 313 res += n 314 endfor 315 res->assert_equal(6) 316 317 dl = deepcopy([1, 2, 3], true) 318enddef 319 320def Test_count() 321 count('ABC ABC ABC', 'b', true)->assert_equal(3) 322 count('ABC ABC ABC', 'b', false)->assert_equal(0) 323enddef 324 325def Test_cursor() 326 new 327 setline(1, range(4)) 328 cursor(2, 1) 329 assert_equal(2, getcurpos()[1]) 330 cursor('$', 1) 331 assert_equal(4, getcurpos()[1]) 332 333 var lines =<< trim END 334 cursor('2', 1) 335 END 336 CheckDefExecAndScriptFailure(lines, 'E475:') 337enddef 338 339def Test_debugbreak() 340 CheckMSWindows 341 CheckDefFailure(['echo debugbreak("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 342enddef 343 344def Test_delete() 345 var res: bool = delete('doesnotexist') 346 assert_equal(true, res) 347enddef 348 349def Test_executable() 350 assert_false(executable("")) 351 assert_false(executable(test_null_string())) 352 353 CheckDefExecFailure(['echo executable(123)'], 'E1174:') 354 CheckDefExecFailure(['echo executable(true)'], 'E1174:') 355enddef 356 357def Test_execute() 358 var res = execute("echo 'hello'") 359 assert_equal("\nhello", res) 360 res = execute(["echo 'here'", "echo 'there'"]) 361 assert_equal("\nhere\nthere", res) 362 363 CheckDefFailure(['echo execute(123)'], 'E1013: Argument 1: type mismatch, expected string but got number') 364 CheckDefFailure(['echo execute([123])'], 'E1013: Argument 1: type mismatch, expected list<string> but got list<number>') 365 CheckDefExecFailure(['echo execute(["xx", 123])'], 'E492') 366 CheckDefFailure(['echo execute("xx", 123)'], 'E1013: Argument 2: type mismatch, expected string but got number') 367enddef 368 369def Test_exepath() 370 CheckDefExecFailure(['echo exepath(true)'], 'E1174:') 371 CheckDefExecFailure(['echo exepath(v:null)'], 'E1174:') 372 CheckDefExecFailure(['echo exepath("")'], 'E1175:') 373enddef 374 375def Test_expand() 376 split SomeFile 377 expand('%', true, true)->assert_equal(['SomeFile']) 378 close 379enddef 380 381def Test_expandcmd() 382 $FOO = "blue" 383 assert_equal("blue sky", expandcmd("`=$FOO .. ' sky'`")) 384 385 assert_equal("yes", expandcmd("`={a: 'yes'}['a']`")) 386enddef 387 388def Test_extend_arg_types() 389 g:number_one = 1 390 g:string_keep = 'keep' 391 var lines =<< trim END 392 assert_equal([1, 2, 3], extend([1, 2], [3])) 393 assert_equal([3, 1, 2], extend([1, 2], [3], 0)) 394 assert_equal([1, 3, 2], extend([1, 2], [3], 1)) 395 assert_equal([1, 3, 2], extend([1, 2], [3], g:number_one)) 396 397 assert_equal({a: 1, b: 2, c: 3}, extend({a: 1, b: 2}, {c: 3})) 398 assert_equal({a: 1, b: 4}, extend({a: 1, b: 2}, {b: 4})) 399 assert_equal({a: 1, b: 2}, extend({a: 1, b: 2}, {b: 4}, 'keep')) 400 assert_equal({a: 1, b: 2}, extend({a: 1, b: 2}, {b: 4}, g:string_keep)) 401 402 var res: list<dict<any>> 403 extend(res, mapnew([1, 2], (_, v) => ({}))) 404 assert_equal([{}, {}], res) 405 END 406 CheckDefAndScriptSuccess(lines) 407 408 CheckDefFailure(['extend("a", 1)'], 'E1013: Argument 1: type mismatch, expected list<any> but got string') 409 CheckDefFailure(['extend([1, 2], 3)'], 'E1013: Argument 2: type mismatch, expected list<number> but got number') 410 CheckDefFailure(['extend([1, 2], ["x"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>') 411 CheckDefFailure(['extend([1, 2], [3], "x")'], 'E1013: Argument 3: type mismatch, expected number but got string') 412 413 CheckDefFailure(['extend({a: 1}, 42)'], 'E1013: Argument 2: type mismatch, expected dict<number> but got number') 414 CheckDefFailure(['extend({a: 1}, {b: "x"})'], 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>') 415 CheckDefFailure(['extend({a: 1}, {b: 2}, 1)'], 'E1013: Argument 3: type mismatch, expected string but got number') 416 417 CheckDefFailure(['extend([1], ["b"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>') 418 CheckDefExecFailure(['extend([1], ["b", 1])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<any>') 419enddef 420 421def Test_extendnew() 422 assert_equal([1, 2, 'a'], extendnew([1, 2], ['a'])) 423 assert_equal({one: 1, two: 'a'}, extendnew({one: 1}, {two: 'a'})) 424 425 CheckDefFailure(['extendnew({a: 1}, 42)'], 'E1013: Argument 2: type mismatch, expected dict<number> but got number') 426 CheckDefFailure(['extendnew({a: 1}, [42])'], 'E1013: Argument 2: type mismatch, expected dict<number> but got list<number>') 427 CheckDefFailure(['extendnew([1, 2], "x")'], 'E1013: Argument 2: type mismatch, expected list<number> but got string') 428 CheckDefFailure(['extendnew([1, 2], {x: 1})'], 'E1013: Argument 2: type mismatch, expected list<number> but got dict<number>') 429enddef 430 431def Test_extend_return_type() 432 var l = extend([1, 2], [3]) 433 var res = 0 434 for n in l 435 res += n 436 endfor 437 res->assert_equal(6) 438enddef 439 440func g:ExtendDict(d) 441 call extend(a:d, #{xx: 'x'}) 442endfunc 443 444def Test_extend_dict_item_type() 445 var lines =<< trim END 446 var d: dict<number> = {a: 1} 447 extend(d, {b: 2}) 448 END 449 CheckDefAndScriptSuccess(lines) 450 451 lines =<< trim END 452 var d: dict<number> = {a: 1} 453 extend(d, {b: 'x'}) 454 END 455 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>', 2) 456 457 lines =<< trim END 458 var d: dict<number> = {a: 1} 459 g:ExtendDict(d) 460 END 461 CheckDefExecFailure(lines, 'E1012: Type mismatch; expected number but got string', 0) 462 CheckScriptFailure(['vim9script'] + lines, 'E1012:', 1) 463enddef 464 465func g:ExtendList(l) 466 call extend(a:l, ['x']) 467endfunc 468 469def Test_extend_list_item_type() 470 var lines =<< trim END 471 var l: list<number> = [1] 472 extend(l, [2]) 473 END 474 CheckDefAndScriptSuccess(lines) 475 476 lines =<< trim END 477 var l: list<number> = [1] 478 extend(l, ['x']) 479 END 480 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>', 2) 481 482 lines =<< trim END 483 var l: list<number> = [1] 484 g:ExtendList(l) 485 END 486 CheckDefExecFailure(lines, 'E1012: Type mismatch; expected number but got string', 0) 487 CheckScriptFailure(['vim9script'] + lines, 'E1012:', 1) 488enddef 489 490def Test_extend_with_error_function() 491 var lines =<< trim END 492 vim9script 493 def F() 494 { 495 var m = 10 496 } 497 echo m 498 enddef 499 500 def Test() 501 var d: dict<any> = {} 502 d->extend({A: 10, Func: function('F', [])}) 503 enddef 504 505 Test() 506 END 507 CheckScriptFailure(lines, 'E1001: Variable not found: m') 508enddef 509 510def Test_job_info_return_type() 511 if has('job') 512 job_start(&shell) 513 var jobs = job_info() 514 assert_equal('list<job>', typename(jobs)) 515 assert_equal('dict<any>', typename(job_info(jobs[0]))) 516 job_stop(jobs[0]) 517 endif 518enddef 519 520def Test_filereadable() 521 assert_false(filereadable("")) 522 assert_false(filereadable(test_null_string())) 523 524 CheckDefExecFailure(['echo filereadable(123)'], 'E1174:') 525 CheckDefExecFailure(['echo filereadable(true)'], 'E1174:') 526enddef 527 528def Test_filewritable() 529 assert_false(filewritable("")) 530 assert_false(filewritable(test_null_string())) 531 532 CheckDefExecFailure(['echo filewritable(123)'], 'E1174:') 533 CheckDefExecFailure(['echo filewritable(true)'], 'E1174:') 534enddef 535 536def Test_finddir() 537 CheckDefExecFailure(['echo finddir(true)'], 'E1174:') 538 CheckDefExecFailure(['echo finddir(v:null)'], 'E1174:') 539 CheckDefExecFailure(['echo finddir("")'], 'E1175:') 540enddef 541 542def Test_findfile() 543 CheckDefExecFailure(['echo findfile(true)'], 'E1174:') 544 CheckDefExecFailure(['echo findfile(v:null)'], 'E1174:') 545 CheckDefExecFailure(['echo findfile("")'], 'E1175:') 546enddef 547 548def Test_flattennew() 549 var lines =<< trim END 550 var l = [1, [2, [3, 4]], 5] 551 call assert_equal([1, 2, 3, 4, 5], flattennew(l)) 552 call assert_equal([1, [2, [3, 4]], 5], l) 553 554 call assert_equal([1, 2, [3, 4], 5], flattennew(l, 1)) 555 call assert_equal([1, [2, [3, 4]], 5], l) 556 END 557 CheckDefAndScriptSuccess(lines) 558 559 lines =<< trim END 560 echo flatten([1, 2, 3]) 561 END 562 CheckDefAndScriptFailure(lines, 'E1158:') 563enddef 564 565" Test for float functions argument type 566def Test_float_funcs_args() 567 CheckFeature float 568 569 # acos() 570 CheckDefFailure(['echo acos("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 571 # asin() 572 CheckDefFailure(['echo asin("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 573 # atan() 574 CheckDefFailure(['echo atan("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 575 # atan2() 576 CheckDefFailure(['echo atan2("a", 1.1)'], 'E1013: Argument 1: type mismatch, expected number but got string') 577 CheckDefFailure(['echo atan2(1.2, "a")'], 'E1013: Argument 2: type mismatch, expected number but got string') 578 CheckDefFailure(['echo atan2(1.2)'], 'E119:') 579 # ceil() 580 CheckDefFailure(['echo ceil("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 581 # cos() 582 CheckDefFailure(['echo cos("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 583 # cosh() 584 CheckDefFailure(['echo cosh("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 585 # exp() 586 CheckDefFailure(['echo exp("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 587 # float2nr() 588 CheckDefFailure(['echo float2nr("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 589 # floor() 590 CheckDefFailure(['echo floor("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 591 # fmod() 592 CheckDefFailure(['echo fmod(1.1, "a")'], 'E1013: Argument 2: type mismatch, expected number but got string') 593 CheckDefFailure(['echo fmod("a", 1.1)'], 'E1013: Argument 1: type mismatch, expected number but got string') 594 CheckDefFailure(['echo fmod(1.1)'], 'E119:') 595 # isinf() 596 CheckDefFailure(['echo isinf("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 597 # isnan() 598 CheckDefFailure(['echo isnan("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 599 # log() 600 CheckDefFailure(['echo log("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 601 # log10() 602 CheckDefFailure(['echo log10("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 603 # pow() 604 CheckDefFailure(['echo pow("a", 1.1)'], 'E1013: Argument 1: type mismatch, expected number but got string') 605 CheckDefFailure(['echo pow(1.1, "a")'], 'E1013: Argument 2: type mismatch, expected number but got string') 606 CheckDefFailure(['echo pow(1.1)'], 'E119:') 607 # round() 608 CheckDefFailure(['echo round("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 609 # sin() 610 CheckDefFailure(['echo sin("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 611 # sinh() 612 CheckDefFailure(['echo sinh("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 613 # sqrt() 614 CheckDefFailure(['echo sqrt("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 615 # tan() 616 CheckDefFailure(['echo tan("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 617 # tanh() 618 CheckDefFailure(['echo tanh("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 619 # trunc() 620 CheckDefFailure(['echo trunc("a")'], 'E1013: Argument 1: type mismatch, expected number but got string') 621enddef 622 623def Test_fnamemodify() 624 CheckDefSuccess(['echo fnamemodify(test_null_string(), ":p")']) 625 CheckDefSuccess(['echo fnamemodify("", ":p")']) 626 CheckDefSuccess(['echo fnamemodify("file", test_null_string())']) 627 CheckDefSuccess(['echo fnamemodify("file", "")']) 628 629 CheckDefExecFailure(['echo fnamemodify(true, ":p")'], 'E1174: String required for argument 1') 630 CheckDefExecFailure(['echo fnamemodify(v:null, ":p")'], 'E1174: String required for argument 1') 631 CheckDefExecFailure(['echo fnamemodify("file", true)'], 'E1174: String required for argument 2') 632enddef 633 634def Wrong_dict_key_type(items: list<number>): list<number> 635 return filter(items, (_, val) => get({[val]: 1}, 'x')) 636enddef 637 638def Test_filter_wrong_dict_key_type() 639 assert_fails('Wrong_dict_key_type([1, v:null, 3])', 'E1013:') 640enddef 641 642def Test_filter_return_type() 643 var l = filter([1, 2, 3], (_, _) => 1) 644 var res = 0 645 for n in l 646 res += n 647 endfor 648 res->assert_equal(6) 649enddef 650 651def Test_filter_missing_argument() 652 var dict = {aa: [1], ab: [2], ac: [3], de: [4]} 653 var res = dict->filter((k, _) => k =~ 'a' && k !~ 'b') 654 res->assert_equal({aa: [1], ac: [3]}) 655enddef 656 657def Test_fullcommand() 658 assert_equal('next', fullcommand('n')) 659 assert_equal('noremap', fullcommand('no')) 660 assert_equal('noremap', fullcommand('nor')) 661 assert_equal('normal', fullcommand('norm')) 662 663 assert_equal('', fullcommand('k')) 664 assert_equal('keepmarks', fullcommand('ke')) 665 assert_equal('keepmarks', fullcommand('kee')) 666 assert_equal('keepmarks', fullcommand('keep')) 667 assert_equal('keepjumps', fullcommand('keepj')) 668 669 assert_equal('dlist', fullcommand('dl')) 670 assert_equal('', fullcommand('dp')) 671 assert_equal('delete', fullcommand('del')) 672 assert_equal('', fullcommand('dell')) 673 assert_equal('', fullcommand('delp')) 674 675 assert_equal('srewind', fullcommand('sre')) 676 assert_equal('scriptnames', fullcommand('scr')) 677 assert_equal('', fullcommand('scg')) 678enddef 679 680def Test_garbagecollect() 681 garbagecollect(true) 682enddef 683 684def Test_getbufinfo() 685 var bufinfo = getbufinfo(bufnr()) 686 getbufinfo('%')->assert_equal(bufinfo) 687 688 edit Xtestfile1 689 hide edit Xtestfile2 690 hide enew 691 getbufinfo({bufloaded: true, buflisted: true, bufmodified: false}) 692 ->len()->assert_equal(3) 693 bwipe Xtestfile1 Xtestfile2 694enddef 695 696def Test_getbufline() 697 e SomeFile 698 var buf = bufnr() 699 e # 700 var lines = ['aaa', 'bbb', 'ccc'] 701 setbufline(buf, 1, lines) 702 getbufline('#', 1, '$')->assert_equal(lines) 703 getbufline(-1, '$', '$')->assert_equal([]) 704 getbufline(-1, 1, '$')->assert_equal([]) 705 706 bwipe! 707enddef 708 709def Test_getchangelist() 710 new 711 setline(1, 'some text') 712 var changelist = bufnr()->getchangelist() 713 getchangelist('%')->assert_equal(changelist) 714 bwipe! 715enddef 716 717def Test_getchar() 718 while getchar(0) 719 endwhile 720 getchar(true)->assert_equal(0) 721enddef 722 723def Test_getenv() 724 if getenv('does-not_exist') == '' 725 assert_report('getenv() should return null') 726 endif 727 if getenv('does-not_exist') == null 728 else 729 assert_report('getenv() should return null') 730 endif 731 $SOMEENVVAR = 'some' 732 assert_equal('some', getenv('SOMEENVVAR')) 733 unlet $SOMEENVVAR 734enddef 735 736def Test_getcompletion() 737 set wildignore=*.vim,*~ 738 var l = getcompletion('run', 'file', true) 739 l->assert_equal([]) 740 set wildignore& 741enddef 742 743def Test_getcurpos() 744 CheckDefFailure(['echo getcursorcharpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 745enddef 746 747def Test_getcursorcharpos() 748 CheckDefFailure(['echo getcursorcharpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 749enddef 750 751def Test_getcwd() 752 CheckDefFailure(['echo getcwd("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 753 CheckDefFailure(['echo getcwd("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string') 754 CheckDefFailure(['echo getcwd(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string') 755enddef 756 757def Test_getloclist_return_type() 758 var l = getloclist(1) 759 l->assert_equal([]) 760 761 var d = getloclist(1, {items: 0}) 762 d->assert_equal({items: []}) 763enddef 764 765def Test_getfperm() 766 assert_equal('', getfperm("")) 767 assert_equal('', getfperm(test_null_string())) 768 769 CheckDefExecFailure(['echo getfperm(true)'], 'E1174:') 770 CheckDefExecFailure(['echo getfperm(v:null)'], 'E1174:') 771enddef 772 773def Test_getfsize() 774 assert_equal(-1, getfsize("")) 775 assert_equal(-1, getfsize(test_null_string())) 776 777 CheckDefExecFailure(['echo getfsize(true)'], 'E1174:') 778 CheckDefExecFailure(['echo getfsize(v:null)'], 'E1174:') 779enddef 780 781def Test_getftime() 782 assert_equal(-1, getftime("")) 783 assert_equal(-1, getftime(test_null_string())) 784 785 CheckDefExecFailure(['echo getftime(true)'], 'E1174:') 786 CheckDefExecFailure(['echo getftime(v:null)'], 'E1174:') 787enddef 788 789def Test_getftype() 790 assert_equal('', getftype("")) 791 assert_equal('', getftype(test_null_string())) 792 793 CheckDefExecFailure(['echo getftype(true)'], 'E1174:') 794 CheckDefExecFailure(['echo getftype(v:null)'], 'E1174:') 795enddef 796 797def Test_getjumplist() 798 CheckDefFailure(['echo getjumplist("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 799 CheckDefFailure(['echo getjumplist("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string') 800 CheckDefFailure(['echo getjumplist(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string') 801enddef 802 803def Test_getmatches() 804 CheckDefFailure(['echo getmatches("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 805enddef 806 807def Test_getqflist_return_type() 808 var l = getqflist() 809 l->assert_equal([]) 810 811 var d = getqflist({items: 0}) 812 d->assert_equal({items: []}) 813enddef 814 815def Test_getreg() 816 var lines = ['aaa', 'bbb', 'ccc'] 817 setreg('a', lines) 818 getreg('a', true, true)->assert_equal(lines) 819 assert_fails('getreg("ab")', 'E1162:') 820enddef 821 822def Test_getreg_return_type() 823 var s1: string = getreg('"') 824 var s2: string = getreg('"', 1) 825 var s3: list<string> = getreg('"', 1, 1) 826enddef 827 828def Test_getreginfo() 829 var text = 'abc' 830 setreg('a', text) 831 getreginfo('a')->assert_equal({regcontents: [text], regtype: 'v', isunnamed: false}) 832 assert_fails('getreginfo("ab")', 'E1162:') 833enddef 834 835def Test_getregtype() 836 var lines = ['aaa', 'bbb', 'ccc'] 837 setreg('a', lines) 838 getregtype('a')->assert_equal('V') 839 assert_fails('getregtype("ab")', 'E1162:') 840enddef 841 842def Test_gettabinfo() 843 CheckDefFailure(['echo gettabinfo("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 844enddef 845 846def Test_gettagstack() 847 CheckDefFailure(['echo gettagstack("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 848enddef 849 850def Test_getwininfo() 851 CheckDefFailure(['echo getwininfo("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 852enddef 853 854def Test_getwinpos() 855 CheckDefFailure(['echo getwinpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 856enddef 857 858def Test_glob() 859 glob('runtest.vim', true, true, true)->assert_equal(['runtest.vim']) 860enddef 861 862def Test_globpath() 863 globpath('.', 'runtest.vim', true, true, true)->assert_equal(['./runtest.vim']) 864enddef 865 866def Test_has() 867 has('eval', true)->assert_equal(1) 868enddef 869 870def Test_haslocaldir() 871 CheckDefFailure(['echo haslocaldir("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 872 CheckDefFailure(['echo haslocaldir("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string') 873 CheckDefFailure(['echo haslocaldir(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string') 874enddef 875 876def Test_hasmapto() 877 hasmapto('foobar', 'i', true)->assert_equal(0) 878 iabbrev foo foobar 879 hasmapto('foobar', 'i', true)->assert_equal(1) 880 iunabbrev foo 881enddef 882 883def Test_index() 884 index(['a', 'b', 'a', 'B'], 'b', 2, true)->assert_equal(3) 885enddef 886 887let s:number_one = 1 888let s:number_two = 2 889let s:string_keep = 'keep' 890 891def Test_insert() 892 var l = insert([2, 1], 3) 893 var res = 0 894 for n in l 895 res += n 896 endfor 897 res->assert_equal(6) 898 899 var m: any = [] 900 insert(m, 4) 901 call assert_equal([4], m) 902 extend(m, [6], 0) 903 call assert_equal([6, 4], m) 904 905 var lines =<< trim END 906 insert(test_null_list(), 123) 907 END 908 CheckDefExecAndScriptFailure(lines, 'E1130:', 1) 909 910 lines =<< trim END 911 insert(test_null_blob(), 123) 912 END 913 CheckDefExecAndScriptFailure(lines, 'E1131:', 1) 914 915 assert_equal([1, 2, 3], insert([2, 3], 1)) 916 assert_equal([1, 2, 3], insert([2, 3], s:number_one)) 917 assert_equal([1, 2, 3], insert([1, 2], 3, 2)) 918 assert_equal([1, 2, 3], insert([1, 2], 3, s:number_two)) 919 assert_equal(['a', 'b', 'c'], insert(['b', 'c'], 'a')) 920 assert_equal(0z1234, insert(0z34, 0x12)) 921 922 CheckDefFailure(['insert("a", 1)'], 'E1013: Argument 1: type mismatch, expected list<any> but got string', 1) 923 CheckDefFailure(['insert([2, 3], "a")'], 'E1013: Argument 2: type mismatch, expected number but got string', 1) 924 CheckDefFailure(['insert([2, 3], 1, "x")'], 'E1013: Argument 3: type mismatch, expected number but got string', 1) 925enddef 926 927def Test_invert() 928 CheckDefFailure(['echo invert("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 929enddef 930 931def Test_keys_return_type() 932 const var: list<string> = {a: 1, b: 2}->keys() 933 var->assert_equal(['a', 'b']) 934enddef 935 936def Test_line() 937 assert_fails('line(true)', 'E1174') 938enddef 939 940def Test_list2str_str2list_utf8() 941 var s = "\u3042\u3044" 942 var l = [0x3042, 0x3044] 943 str2list(s, true)->assert_equal(l) 944 list2str(l, true)->assert_equal(s) 945enddef 946 947def SID(): number 948 return expand('<SID>') 949 ->matchstr('<SNR>\zs\d\+\ze_$') 950 ->str2nr() 951enddef 952 953def Test_listener_remove() 954 CheckDefFailure(['echo listener_remove("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 955enddef 956 957def Test_map_function_arg() 958 var lines =<< trim END 959 def MapOne(i: number, v: string): string 960 return i .. ':' .. v 961 enddef 962 var l = ['a', 'b', 'c'] 963 map(l, MapOne) 964 assert_equal(['0:a', '1:b', '2:c'], l) 965 END 966 CheckDefAndScriptSuccess(lines) 967 968 lines =<< trim END 969 range(3)->map((a, b, c) => a + b + c) 970 END 971 CheckDefExecAndScriptFailure(lines, 'E1190: One argument too few') 972 lines =<< trim END 973 range(3)->map((a, b, c, d) => a + b + c + d) 974 END 975 CheckDefExecAndScriptFailure(lines, 'E1190: 2 arguments too few') 976enddef 977 978def Test_map_item_type() 979 var lines =<< trim END 980 var l = ['a', 'b', 'c'] 981 map(l, (k, v) => k .. '/' .. v ) 982 assert_equal(['0/a', '1/b', '2/c'], l) 983 END 984 CheckDefAndScriptSuccess(lines) 985 986 lines =<< trim END 987 var l: list<number> = [0] 988 echo map(l, (_, v) => []) 989 END 990 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2) 991 992 lines =<< trim END 993 var l: list<number> = range(2) 994 echo map(l, (_, v) => []) 995 END 996 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2) 997 998 lines =<< trim END 999 var d: dict<number> = {key: 0} 1000 echo map(d, (_, v) => []) 1001 END 1002 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2) 1003enddef 1004 1005def Test_maparg() 1006 var lnum = str2nr(expand('<sflnum>')) 1007 map foo bar 1008 maparg('foo', '', false, true)->assert_equal({ 1009 lnum: lnum + 1, 1010 script: 0, 1011 mode: ' ', 1012 silent: 0, 1013 noremap: 0, 1014 lhs: 'foo', 1015 lhsraw: 'foo', 1016 nowait: 0, 1017 expr: 0, 1018 sid: SID(), 1019 rhs: 'bar', 1020 buffer: 0}) 1021 unmap foo 1022enddef 1023 1024def Test_mapcheck() 1025 iabbrev foo foobar 1026 mapcheck('foo', 'i', true)->assert_equal('foobar') 1027 iunabbrev foo 1028enddef 1029 1030def Test_maparg_mapset() 1031 nnoremap <F3> :echo "hit F3"<CR> 1032 var mapsave = maparg('<F3>', 'n', false, true) 1033 mapset('n', false, mapsave) 1034 1035 nunmap <F3> 1036enddef 1037 1038def Test_map_failure() 1039 CheckFeature job 1040 1041 var lines =<< trim END 1042 vim9script 1043 writefile([], 'Xtmpfile') 1044 silent e Xtmpfile 1045 var d = {[bufnr('%')]: {a: 0}} 1046 au BufReadPost * Func() 1047 def Func() 1048 if d->has_key('') 1049 endif 1050 eval d[expand('<abuf>')]->mapnew((_, v: dict<job>) => 0) 1051 enddef 1052 e 1053 END 1054 CheckScriptFailure(lines, 'E1013:') 1055 au! BufReadPost 1056 delete('Xtmpfile') 1057enddef 1058 1059def Test_matcharg() 1060 CheckDefFailure(['echo matcharg("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 1061enddef 1062 1063def Test_matchdelete() 1064 CheckDefFailure(['echo matchdelete("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 1065 CheckDefFailure(['echo matchdelete("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string') 1066 CheckDefFailure(['echo matchdelete(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string') 1067enddef 1068 1069def Test_max() 1070 g:flag = true 1071 var l1: list<number> = g:flag 1072 ? [1, max([2, 3])] 1073 : [4, 5] 1074 assert_equal([1, 3], l1) 1075 1076 g:flag = false 1077 var l2: list<number> = g:flag 1078 ? [1, max([2, 3])] 1079 : [4, 5] 1080 assert_equal([4, 5], l2) 1081enddef 1082 1083def Test_min() 1084 g:flag = true 1085 var l1: list<number> = g:flag 1086 ? [1, min([2, 3])] 1087 : [4, 5] 1088 assert_equal([1, 2], l1) 1089 1090 g:flag = false 1091 var l2: list<number> = g:flag 1092 ? [1, min([2, 3])] 1093 : [4, 5] 1094 assert_equal([4, 5], l2) 1095enddef 1096 1097def Test_nr2char() 1098 nr2char(97, true)->assert_equal('a') 1099enddef 1100 1101def Test_or() 1102 CheckDefFailure(['echo or("x", 0x2)'], 'E1013: Argument 1: type mismatch, expected number but got string') 1103 CheckDefFailure(['echo or(0x1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string') 1104enddef 1105 1106def Test_readdir() 1107 eval expand('sautest')->readdir((e) => e[0] !=# '.') 1108 eval expand('sautest')->readdirex((e) => e.name[0] !=# '.') 1109enddef 1110 1111def Test_readblob() 1112 var blob = 0z12341234 1113 writefile(blob, 'Xreadblob') 1114 var read: blob = readblob('Xreadblob') 1115 assert_equal(blob, read) 1116 1117 var lines =<< trim END 1118 var read: list<string> = readblob('Xreadblob') 1119 END 1120 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected list<string> but got blob', 1) 1121 delete('Xreadblob') 1122enddef 1123 1124def Test_readfile() 1125 var text = ['aaa', 'bbb', 'ccc'] 1126 writefile(text, 'Xreadfile') 1127 var read: list<string> = readfile('Xreadfile') 1128 assert_equal(text, read) 1129 1130 var lines =<< trim END 1131 var read: dict<string> = readfile('Xreadfile') 1132 END 1133 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected dict<string> but got list<string>', 1) 1134 delete('Xreadfile') 1135enddef 1136 1137def Test_remove_return_type() 1138 var l = remove({one: [1, 2], two: [3, 4]}, 'one') 1139 var res = 0 1140 for n in l 1141 res += n 1142 endfor 1143 res->assert_equal(3) 1144enddef 1145 1146def Test_reverse_return_type() 1147 var l = reverse([1, 2, 3]) 1148 var res = 0 1149 for n in l 1150 res += n 1151 endfor 1152 res->assert_equal(6) 1153enddef 1154 1155def Test_screenattr() 1156 CheckDefFailure(['echo screenattr("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string') 1157 CheckDefFailure(['echo screenattr(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string') 1158enddef 1159 1160def Test_screenchar() 1161 CheckDefFailure(['echo screenchar("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string') 1162 CheckDefFailure(['echo screenchar(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string') 1163enddef 1164 1165def Test_screenchars() 1166 CheckDefFailure(['echo screenchars("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string') 1167 CheckDefFailure(['echo screenchars(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string') 1168enddef 1169 1170def Test_screenstring() 1171 CheckDefFailure(['echo screenstring("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string') 1172 CheckDefFailure(['echo screenstring(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string') 1173enddef 1174 1175def Test_search() 1176 new 1177 setline(1, ['foo', 'bar']) 1178 var val = 0 1179 # skip expr returns boolean 1180 search('bar', 'W', 0, 0, () => val == 1)->assert_equal(2) 1181 :1 1182 search('bar', 'W', 0, 0, () => val == 0)->assert_equal(0) 1183 # skip expr returns number, only 0 and 1 are accepted 1184 :1 1185 search('bar', 'W', 0, 0, () => 0)->assert_equal(2) 1186 :1 1187 search('bar', 'W', 0, 0, () => 1)->assert_equal(0) 1188 assert_fails("search('bar', '', 0, 0, () => -1)", 'E1023:') 1189 assert_fails("search('bar', '', 0, 0, () => -1)", 'E1023:') 1190 1191 setline(1, "find this word") 1192 normal gg 1193 var col = 7 1194 assert_equal(1, search('this', '', 0, 0, 'col(".") > col')) 1195 normal 0 1196 assert_equal([1, 6], searchpos('this', '', 0, 0, 'col(".") > col')) 1197 1198 col = 5 1199 normal 0 1200 assert_equal(0, search('this', '', 0, 0, 'col(".") > col')) 1201 normal 0 1202 assert_equal([0, 0], searchpos('this', '', 0, 0, 'col(".") > col')) 1203 bwipe! 1204enddef 1205 1206def Test_searchcount() 1207 new 1208 setline(1, "foo bar") 1209 :/foo 1210 searchcount({recompute: true}) 1211 ->assert_equal({ 1212 exact_match: 1, 1213 current: 1, 1214 total: 1, 1215 maxcount: 99, 1216 incomplete: 0}) 1217 bwipe! 1218enddef 1219 1220def Test_searchpair() 1221 new 1222 setline(1, "here { and } there") 1223 1224 normal f{ 1225 var col = 15 1226 assert_equal(1, searchpair('{', '', '}', '', 'col(".") > col')) 1227 assert_equal(12, col('.')) 1228 normal 0f{ 1229 assert_equal([1, 12], searchpairpos('{', '', '}', '', 'col(".") > col')) 1230 1231 col = 8 1232 normal 0f{ 1233 assert_equal(0, searchpair('{', '', '}', '', 'col(".") > col')) 1234 assert_equal(6, col('.')) 1235 normal 0f{ 1236 assert_equal([0, 0], searchpairpos('{', '', '}', '', 'col(".") > col')) 1237 1238 var lines =<< trim END 1239 vim9script 1240 setline(1, '()') 1241 normal gg 1242 def Fail() 1243 try 1244 searchpairpos('(', '', ')', 'nW', '[0]->map("")') 1245 catch 1246 g:caught = 'yes' 1247 endtry 1248 enddef 1249 Fail() 1250 END 1251 CheckScriptSuccess(lines) 1252 assert_equal('yes', g:caught) 1253 1254 unlet g:caught 1255 bwipe! 1256enddef 1257 1258def Test_set_get_bufline() 1259 # similar to Test_setbufline_getbufline() 1260 var lines =<< trim END 1261 new 1262 var b = bufnr('%') 1263 hide 1264 assert_equal(0, setbufline(b, 1, ['foo', 'bar'])) 1265 assert_equal(['foo'], getbufline(b, 1)) 1266 assert_equal(['bar'], getbufline(b, '$')) 1267 assert_equal(['foo', 'bar'], getbufline(b, 1, 2)) 1268 exe "bd!" b 1269 assert_equal([], getbufline(b, 1, 2)) 1270 1271 split Xtest 1272 setline(1, ['a', 'b', 'c']) 1273 b = bufnr('%') 1274 wincmd w 1275 1276 assert_equal(1, setbufline(b, 5, 'x')) 1277 assert_equal(1, setbufline(b, 5, ['x'])) 1278 assert_equal(1, setbufline(b, 5, [])) 1279 assert_equal(1, setbufline(b, 5, test_null_list())) 1280 1281 assert_equal(1, 'x'->setbufline(bufnr('$') + 1, 1)) 1282 assert_equal(1, ['x']->setbufline(bufnr('$') + 1, 1)) 1283 assert_equal(1, []->setbufline(bufnr('$') + 1, 1)) 1284 assert_equal(1, test_null_list()->setbufline(bufnr('$') + 1, 1)) 1285 1286 assert_equal(['a', 'b', 'c'], getbufline(b, 1, '$')) 1287 1288 assert_equal(0, setbufline(b, 4, ['d', 'e'])) 1289 assert_equal(['c'], b->getbufline(3)) 1290 assert_equal(['d'], getbufline(b, 4)) 1291 assert_equal(['e'], getbufline(b, 5)) 1292 assert_equal([], getbufline(b, 6)) 1293 assert_equal([], getbufline(b, 2, 1)) 1294 1295 if has('job') 1296 setbufline(b, 2, [function('eval'), {key: 123}, string(test_null_job())]) 1297 assert_equal(["function('eval')", 1298 "{'key': 123}", 1299 "no process"], 1300 getbufline(b, 2, 4)) 1301 endif 1302 1303 exe 'bwipe! ' .. b 1304 END 1305 CheckDefAndScriptSuccess(lines) 1306enddef 1307 1308def Test_searchdecl() 1309 searchdecl('blah', true, true)->assert_equal(1) 1310enddef 1311 1312def Test_setbufvar() 1313 setbufvar(bufnr('%'), '&syntax', 'vim') 1314 &syntax->assert_equal('vim') 1315 setbufvar(bufnr('%'), '&ts', 16) 1316 &ts->assert_equal(16) 1317 setbufvar(bufnr('%'), '&ai', true) 1318 &ai->assert_equal(true) 1319 setbufvar(bufnr('%'), '&ft', 'filetype') 1320 &ft->assert_equal('filetype') 1321 1322 settabwinvar(1, 1, '&syntax', 'vam') 1323 &syntax->assert_equal('vam') 1324 settabwinvar(1, 1, '&ts', 15) 1325 &ts->assert_equal(15) 1326 setlocal ts=8 1327 settabwinvar(1, 1, '&list', false) 1328 &list->assert_equal(false) 1329 settabwinvar(1, 1, '&list', true) 1330 &list->assert_equal(true) 1331 setlocal list& 1332 1333 setbufvar('%', 'myvar', 123) 1334 getbufvar('%', 'myvar')->assert_equal(123) 1335enddef 1336 1337def Test_setcmdpos() 1338 CheckDefFailure(['echo setcmdpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 1339enddef 1340 1341def Test_setloclist() 1342 var items = [{filename: '/tmp/file', lnum: 1, valid: true}] 1343 var what = {items: items} 1344 setqflist([], ' ', what) 1345 setloclist(0, [], ' ', what) 1346enddef 1347 1348def Test_setreg() 1349 setreg('a', ['aaa', 'bbb', 'ccc']) 1350 var reginfo = getreginfo('a') 1351 setreg('a', reginfo) 1352 getreginfo('a')->assert_equal(reginfo) 1353 assert_fails('setreg("ab", 0)', 'E1162:') 1354enddef 1355 1356def Test_shiftwidth() 1357 CheckDefFailure(['echo shiftwidth("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 1358enddef 1359 1360def Test_slice() 1361 assert_equal('12345', slice('012345', 1)) 1362 assert_equal('123', slice('012345', 1, 4)) 1363 assert_equal('1234', slice('012345', 1, -1)) 1364 assert_equal('1', slice('012345', 1, -4)) 1365 assert_equal('', slice('012345', 1, -5)) 1366 assert_equal('', slice('012345', 1, -6)) 1367 1368 assert_equal([1, 2, 3, 4, 5], slice(range(6), 1)) 1369 assert_equal([1, 2, 3], slice(range(6), 1, 4)) 1370 assert_equal([1, 2, 3, 4], slice(range(6), 1, -1)) 1371 assert_equal([1], slice(range(6), 1, -4)) 1372 assert_equal([], slice(range(6), 1, -5)) 1373 assert_equal([], slice(range(6), 1, -6)) 1374 1375 assert_equal(0z1122334455, slice(0z001122334455, 1)) 1376 assert_equal(0z112233, slice(0z001122334455, 1, 4)) 1377 assert_equal(0z11223344, slice(0z001122334455, 1, -1)) 1378 assert_equal(0z11, slice(0z001122334455, 1, -4)) 1379 assert_equal(0z, slice(0z001122334455, 1, -5)) 1380 assert_equal(0z, slice(0z001122334455, 1, -6)) 1381enddef 1382 1383def Test_spellsuggest() 1384 if !has('spell') 1385 MissingFeature 'spell' 1386 else 1387 spellsuggest('marrch', 1, true)->assert_equal(['March']) 1388 endif 1389enddef 1390 1391def Test_sort_return_type() 1392 var res: list<number> 1393 res = [1, 2, 3]->sort() 1394enddef 1395 1396def Test_sort_argument() 1397 var lines =<< trim END 1398 var res = ['b', 'a', 'c']->sort('i') 1399 res->assert_equal(['a', 'b', 'c']) 1400 1401 def Compare(a: number, b: number): number 1402 return a - b 1403 enddef 1404 var l = [3, 6, 7, 1, 8, 2, 4, 5] 1405 sort(l, Compare) 1406 assert_equal([1, 2, 3, 4, 5, 6, 7, 8], l) 1407 END 1408 CheckDefAndScriptSuccess(lines) 1409enddef 1410 1411def Test_split() 1412 split(' aa bb ', '\W\+', true)->assert_equal(['', 'aa', 'bb', '']) 1413enddef 1414 1415def Run_str2float() 1416 if !has('float') 1417 MissingFeature 'float' 1418 endif 1419 str2float("1.00")->assert_equal(1.00) 1420 str2float("2e-2")->assert_equal(0.02) 1421 1422 CheckDefFailure(['echo str2float(123)'], 'E1013:') 1423 CheckScriptFailure(['vim9script', 'echo str2float(123)'], 'E1024:') 1424 endif 1425enddef 1426 1427def Test_str2nr() 1428 str2nr("1'000'000", 10, true)->assert_equal(1000000) 1429 1430 CheckDefFailure(['echo str2nr(123)'], 'E1013:') 1431 CheckScriptFailure(['vim9script', 'echo str2nr(123)'], 'E1024:') 1432 CheckDefFailure(['echo str2nr("123", "x")'], 'E1013:') 1433 CheckScriptFailure(['vim9script', 'echo str2nr("123", "x")'], 'E1030:') 1434 CheckDefFailure(['echo str2nr("123", 10, "x")'], 'E1013:') 1435 CheckScriptFailure(['vim9script', 'echo str2nr("123", 10, "x")'], 'E1135:') 1436enddef 1437 1438def Test_strchars() 1439 strchars("A\u20dd", true)->assert_equal(1) 1440enddef 1441 1442def Test_submatch() 1443 var pat = 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)' 1444 var Rep = () => range(10)->mapnew((_, v) => submatch(v, true))->string() 1445 var actual = substitute('A123456789', pat, Rep, '') 1446 var expected = "[['A123456789'], ['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9']]" 1447 actual->assert_equal(expected) 1448enddef 1449 1450def Test_substitute() 1451 var res = substitute('A1234', '\d', 'X', '') 1452 assert_equal('AX234', res) 1453 1454 if has('job') 1455 assert_fails('"text"->substitute(".*", () => job_start(":"), "")', 'E908: using an invalid value as a String: job') 1456 assert_fails('"text"->substitute(".*", () => job_start(":")->job_getchannel(), "")', 'E908: using an invalid value as a String: channel') 1457 endif 1458enddef 1459 1460def Test_synID() 1461 new 1462 setline(1, "text") 1463 synID(1, 1, true)->assert_equal(0) 1464 bwipe! 1465enddef 1466 1467def Test_term_gettty() 1468 if !has('terminal') 1469 MissingFeature 'terminal' 1470 else 1471 var buf = Run_shell_in_terminal({}) 1472 term_gettty(buf, true)->assert_notequal('') 1473 StopShellInTerminal(buf) 1474 endif 1475enddef 1476 1477def Test_term_start() 1478 if !has('terminal') 1479 MissingFeature 'terminal' 1480 else 1481 botright new 1482 var winnr = winnr() 1483 term_start(&shell, {curwin: true}) 1484 winnr()->assert_equal(winnr) 1485 bwipe! 1486 endif 1487enddef 1488 1489def Test_timer_paused() 1490 var id = timer_start(50, () => 0) 1491 timer_pause(id, true) 1492 var info = timer_info(id) 1493 info[0]['paused']->assert_equal(1) 1494 timer_stop(id) 1495enddef 1496 1497def Test_tolower() 1498 CheckDefFailure(['echo tolower(1)'], 'E1013: Argument 1: type mismatch, expected string but got number') 1499enddef 1500 1501def Test_toupper() 1502 CheckDefFailure(['echo toupper(1)'], 'E1013: Argument 1: type mismatch, expected string but got number') 1503enddef 1504 1505def Test_tr() 1506 CheckDefFailure(['echo tr(1, "a", "b")'], 'E1013: Argument 1: type mismatch, expected string but got number') 1507 CheckDefFailure(['echo tr("a", 1, "b")'], 'E1013: Argument 2: type mismatch, expected string but got number') 1508 CheckDefFailure(['echo tr("a", "a", 1)'], 'E1013: Argument 3: type mismatch, expected string but got number') 1509enddef 1510 1511def Test_win_execute() 1512 assert_equal("\n" .. winnr(), win_execute(win_getid(), 'echo winnr()')) 1513 assert_equal('', win_execute(342343, 'echo winnr()')) 1514enddef 1515 1516def Test_win_splitmove() 1517 split 1518 win_splitmove(1, 2, {vertical: true, rightbelow: true}) 1519 close 1520enddef 1521 1522def Test_winrestcmd() 1523 split 1524 var cmd = winrestcmd() 1525 wincmd _ 1526 exe cmd 1527 assert_equal(cmd, winrestcmd()) 1528 close 1529enddef 1530 1531def Test_winsaveview() 1532 var view: dict<number> = winsaveview() 1533 1534 var lines =<< trim END 1535 var view: list<number> = winsaveview() 1536 END 1537 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected list<number> but got dict<number>', 1) 1538enddef 1539 1540def Test_win_gettype() 1541 CheckDefFailure(['echo win_gettype("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 1542enddef 1543 1544def Test_win_gotoid() 1545 CheckDefFailure(['echo win_gotoid("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 1546enddef 1547 1548def Test_win_id2tabwin() 1549 CheckDefFailure(['echo win_id2tabwin("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 1550enddef 1551 1552def Test_win_id2win() 1553 CheckDefFailure(['echo win_id2win("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 1554enddef 1555 1556def Test_win_screenpos() 1557 CheckDefFailure(['echo win_screenpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 1558enddef 1559 1560def Test_winbufnr() 1561 CheckDefFailure(['echo winbufnr("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 1562enddef 1563 1564def Test_winheight() 1565 CheckDefFailure(['echo winheight("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 1566enddef 1567 1568def Test_winlayout() 1569 CheckDefFailure(['echo winlayout("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 1570enddef 1571 1572def Test_winwidth() 1573 CheckDefFailure(['echo winwidth("x")'], 'E1013: Argument 1: type mismatch, expected number but got string') 1574enddef 1575 1576def Test_xor() 1577 CheckDefFailure(['echo xor("x", 0x2)'], 'E1013: Argument 1: type mismatch, expected number but got string') 1578 CheckDefFailure(['echo xor(0x1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string') 1579enddef 1580 1581" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker 1582