Bram Moolenaar | 94738d8 | 2020-10-21 14:25:07 +0200 | [diff] [blame] | 1 | " Test using builtin functions in the Vim9 script language. |
| 2 | |
| 3 | source check.vim |
| 4 | source vim9.vim |
| 5 | |
| 6 | " Test for passing too many or too few arguments to builtin functions |
| 7 | func 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') |
| 25 | endfunc |
| 26 | |
| 27 | " Test for builtin functions returning different types |
| 28 | func 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') |
| 64 | endfunc |
| 65 | |
| 66 | def 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 |
| 76 | enddef |
| 77 | |
| 78 | def 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 | var l: list<number> = test_null_list() |
| 91 | add(l, 123) |
| 92 | END |
| 93 | CheckDefExecFailure(lines, 'E1130:', 2) |
| 94 | enddef |
| 95 | |
| 96 | def Test_add_blob() |
| 97 | var b1: blob = 0z12 |
| 98 | add(b1, 0x34) |
| 99 | assert_equal(0z1234, b1) |
| 100 | |
| 101 | var b2: blob # defaults to empty blob |
| 102 | add(b2, 0x67) |
| 103 | assert_equal(0z67, b2) |
| 104 | |
| 105 | var lines =<< trim END |
| 106 | var b: blob |
| 107 | add(b, "x") |
| 108 | END |
| 109 | CheckDefFailure(lines, 'E1012:', 2) |
| 110 | |
| 111 | lines =<< trim END |
| 112 | var b: blob = test_null_blob() |
| 113 | add(b, 123) |
| 114 | END |
| 115 | CheckDefExecFailure(lines, 'E1131:', 2) |
| 116 | enddef |
| 117 | |
| 118 | def Test_bufname() |
| 119 | split SomeFile |
| 120 | bufname('%')->assert_equal('SomeFile') |
| 121 | edit OtherFile |
| 122 | bufname('#')->assert_equal('SomeFile') |
| 123 | close |
| 124 | enddef |
| 125 | |
| 126 | def Test_bufnr() |
| 127 | var buf = bufnr() |
| 128 | bufnr('%')->assert_equal(buf) |
| 129 | |
| 130 | buf = bufnr('Xdummy', true) |
| 131 | buf->assert_notequal(-1) |
| 132 | exe 'bwipe! ' .. buf |
| 133 | enddef |
| 134 | |
| 135 | def Test_bufwinid() |
| 136 | var origwin = win_getid() |
| 137 | below split SomeFile |
| 138 | var SomeFileID = win_getid() |
| 139 | below split OtherFile |
| 140 | below split SomeFile |
| 141 | bufwinid('SomeFile')->assert_equal(SomeFileID) |
| 142 | |
| 143 | win_gotoid(origwin) |
| 144 | only |
| 145 | bwipe SomeFile |
| 146 | bwipe OtherFile |
| 147 | enddef |
| 148 | |
| 149 | def Test_call_call() |
| 150 | var l = [3, 2, 1] |
| 151 | call('reverse', [l]) |
| 152 | l->assert_equal([1, 2, 3]) |
| 153 | enddef |
| 154 | |
| 155 | def Test_char2nr() |
| 156 | char2nr('あ', true)->assert_equal(12354) |
| 157 | enddef |
| 158 | |
| 159 | def Test_col() |
| 160 | new |
| 161 | setline(1, 'asdf') |
| 162 | col([1, '$'])->assert_equal(5) |
| 163 | enddef |
| 164 | |
| 165 | def Test_copy_return_type() |
| 166 | var l = copy([1, 2, 3]) |
| 167 | var res = 0 |
| 168 | for n in l |
| 169 | res += n |
| 170 | endfor |
| 171 | res->assert_equal(6) |
| 172 | |
| 173 | var dl = deepcopy([1, 2, 3]) |
| 174 | res = 0 |
| 175 | for n in dl |
| 176 | res += n |
| 177 | endfor |
| 178 | res->assert_equal(6) |
| 179 | |
| 180 | dl = deepcopy([1, 2, 3], true) |
| 181 | enddef |
| 182 | |
| 183 | def Test_count() |
| 184 | count('ABC ABC ABC', 'b', true)->assert_equal(3) |
| 185 | count('ABC ABC ABC', 'b', false)->assert_equal(0) |
| 186 | enddef |
| 187 | |
| 188 | def Test_expand() |
| 189 | split SomeFile |
| 190 | expand('%', true, true)->assert_equal(['SomeFile']) |
| 191 | close |
| 192 | enddef |
| 193 | |
| 194 | def Test_extend_return_type() |
| 195 | var l = extend([1, 2], [3]) |
| 196 | var res = 0 |
| 197 | for n in l |
| 198 | res += n |
| 199 | endfor |
| 200 | res->assert_equal(6) |
| 201 | enddef |
| 202 | |
| 203 | |
| 204 | def Wrong_dict_key_type(items: list<number>): list<number> |
| 205 | return filter(items, {_, val -> get({val: 1}, 'x')}) |
| 206 | enddef |
| 207 | |
| 208 | def Test_filter_wrong_dict_key_type() |
| 209 | assert_fails('Wrong_dict_key_type([1, 2, 3])', 'E1012:') |
| 210 | enddef |
| 211 | |
| 212 | def Test_filter_return_type() |
| 213 | var l = filter([1, 2, 3], {-> 1}) |
| 214 | var res = 0 |
| 215 | for n in l |
| 216 | res += n |
| 217 | endfor |
| 218 | res->assert_equal(6) |
| 219 | enddef |
| 220 | |
| 221 | |
| 222 | def Test_garbagecollect() |
| 223 | garbagecollect(true) |
| 224 | enddef |
| 225 | |
| 226 | def Test_getbufinfo() |
| 227 | var bufinfo = getbufinfo(bufnr()) |
| 228 | getbufinfo('%')->assert_equal(bufinfo) |
| 229 | |
| 230 | edit Xtestfile1 |
| 231 | hide edit Xtestfile2 |
| 232 | hide enew |
| 233 | getbufinfo(#{bufloaded: true, buflisted: true, bufmodified: false}) |
| 234 | ->len()->assert_equal(3) |
| 235 | bwipe Xtestfile1 Xtestfile2 |
| 236 | enddef |
| 237 | |
| 238 | def Test_getbufline() |
| 239 | e SomeFile |
| 240 | var buf = bufnr() |
| 241 | e # |
| 242 | var lines = ['aaa', 'bbb', 'ccc'] |
| 243 | setbufline(buf, 1, lines) |
| 244 | getbufline('#', 1, '$')->assert_equal(lines) |
| 245 | |
| 246 | bwipe! |
| 247 | enddef |
| 248 | |
| 249 | def Test_getchangelist() |
| 250 | new |
| 251 | setline(1, 'some text') |
| 252 | var changelist = bufnr()->getchangelist() |
| 253 | getchangelist('%')->assert_equal(changelist) |
| 254 | bwipe! |
| 255 | enddef |
| 256 | |
| 257 | def Test_getchar() |
| 258 | while getchar(0) |
| 259 | endwhile |
| 260 | getchar(true)->assert_equal(0) |
| 261 | enddef |
| 262 | |
| 263 | def Test_getcompletion() |
| 264 | set wildignore=*.vim,*~ |
| 265 | var l = getcompletion('run', 'file', true) |
| 266 | l->assert_equal([]) |
| 267 | set wildignore& |
| 268 | enddef |
| 269 | |
| 270 | def Test_getloclist_return_type() |
| 271 | var l = getloclist(1) |
| 272 | l->assert_equal([]) |
| 273 | |
| 274 | var d = getloclist(1, #{items: 0}) |
| 275 | d->assert_equal(#{items: []}) |
| 276 | enddef |
| 277 | |
| 278 | def Test_getqflist_return_type() |
| 279 | var l = getqflist() |
| 280 | l->assert_equal([]) |
| 281 | |
| 282 | var d = getqflist(#{items: 0}) |
| 283 | d->assert_equal(#{items: []}) |
| 284 | enddef |
| 285 | |
| 286 | def Test_getreg() |
| 287 | var lines = ['aaa', 'bbb', 'ccc'] |
| 288 | setreg('a', lines) |
| 289 | getreg('a', true, true)->assert_equal(lines) |
| 290 | enddef |
| 291 | |
| 292 | def Test_getreg_return_type() |
| 293 | var s1: string = getreg('"') |
| 294 | var s2: string = getreg('"', 1) |
| 295 | var s3: list<string> = getreg('"', 1, 1) |
| 296 | enddef |
| 297 | |
| 298 | def Test_glob() |
| 299 | glob('runtest.vim', true, true, true)->assert_equal(['runtest.vim']) |
| 300 | enddef |
| 301 | |
| 302 | def Test_globpath() |
| 303 | globpath('.', 'runtest.vim', true, true, true)->assert_equal(['./runtest.vim']) |
| 304 | enddef |
| 305 | |
| 306 | def Test_has() |
| 307 | has('eval', true)->assert_equal(1) |
| 308 | enddef |
| 309 | |
| 310 | def Test_hasmapto() |
| 311 | hasmapto('foobar', 'i', true)->assert_equal(0) |
| 312 | iabbrev foo foobar |
| 313 | hasmapto('foobar', 'i', true)->assert_equal(1) |
| 314 | iunabbrev foo |
| 315 | enddef |
| 316 | |
| 317 | def Test_index() |
| 318 | index(['a', 'b', 'a', 'B'], 'b', 2, true)->assert_equal(3) |
| 319 | enddef |
| 320 | |
Bram Moolenaar | ca17453 | 2020-10-21 16:42:22 +0200 | [diff] [blame] | 321 | def Test_insert() |
Bram Moolenaar | 94738d8 | 2020-10-21 14:25:07 +0200 | [diff] [blame] | 322 | var l = insert([2, 1], 3) |
| 323 | var res = 0 |
| 324 | for n in l |
| 325 | res += n |
| 326 | endfor |
| 327 | res->assert_equal(6) |
Bram Moolenaar | ca17453 | 2020-10-21 16:42:22 +0200 | [diff] [blame] | 328 | |
| 329 | assert_equal([1, 2, 3], insert([2, 3], 1)) |
| 330 | assert_equal([1, 2, 3], insert([1, 2], 3, 2)) |
| 331 | assert_equal(['a', 'b', 'c'], insert(['b', 'c'], 'a')) |
| 332 | assert_equal(0z1234, insert(0z34, 0x12)) |
| 333 | CheckDefFailure(['insert([2, 3], "a")'], 'E1013: Argument 2: type mismatch, expected number but got string', 1) |
| 334 | CheckDefFailure(['insert([2, 3], 1, "x")'], 'E1013: Argument 3: type mismatch, expected number but got string', 1) |
Bram Moolenaar | 94738d8 | 2020-10-21 14:25:07 +0200 | [diff] [blame] | 335 | enddef |
| 336 | |
| 337 | def Test_keys_return_type() |
| 338 | const var: list<string> = #{a: 1, b: 2}->keys() |
| 339 | var->assert_equal(['a', 'b']) |
| 340 | enddef |
| 341 | |
| 342 | def Test_list2str_str2list_utf8() |
| 343 | var s = "\u3042\u3044" |
| 344 | var l = [0x3042, 0x3044] |
| 345 | str2list(s, true)->assert_equal(l) |
| 346 | list2str(l, true)->assert_equal(s) |
| 347 | enddef |
| 348 | |
| 349 | def SID(): number |
| 350 | return expand('<SID>') |
| 351 | ->matchstr('<SNR>\zs\d\+\ze_$') |
| 352 | ->str2nr() |
| 353 | enddef |
| 354 | |
| 355 | def Test_maparg() |
| 356 | var lnum = str2nr(expand('<sflnum>')) |
| 357 | map foo bar |
| 358 | maparg('foo', '', false, true)->assert_equal(#{ |
| 359 | lnum: lnum + 1, |
| 360 | script: 0, |
| 361 | mode: ' ', |
| 362 | silent: 0, |
| 363 | noremap: 0, |
| 364 | lhs: 'foo', |
| 365 | lhsraw: 'foo', |
| 366 | nowait: 0, |
| 367 | expr: 0, |
| 368 | sid: SID(), |
| 369 | rhs: 'bar', |
| 370 | buffer: 0}) |
| 371 | unmap foo |
| 372 | enddef |
| 373 | |
| 374 | def Test_mapcheck() |
| 375 | iabbrev foo foobar |
| 376 | mapcheck('foo', 'i', true)->assert_equal('foobar') |
| 377 | iunabbrev foo |
| 378 | enddef |
| 379 | |
| 380 | def Test_maparg_mapset() |
| 381 | nnoremap <F3> :echo "hit F3"<CR> |
| 382 | var mapsave = maparg('<F3>', 'n', false, true) |
| 383 | mapset('n', false, mapsave) |
| 384 | |
| 385 | nunmap <F3> |
| 386 | enddef |
| 387 | |
| 388 | def Test_nr2char() |
| 389 | nr2char(97, true)->assert_equal('a') |
| 390 | enddef |
| 391 | |
| 392 | def Test_readdir() |
| 393 | eval expand('sautest')->readdir({e -> e[0] !=# '.'}) |
| 394 | eval expand('sautest')->readdirex({e -> e.name[0] !=# '.'}) |
| 395 | enddef |
| 396 | |
| 397 | def Test_remove_return_type() |
| 398 | var l = remove(#{one: [1, 2], two: [3, 4]}, 'one') |
| 399 | var res = 0 |
| 400 | for n in l |
| 401 | res += n |
| 402 | endfor |
| 403 | res->assert_equal(3) |
| 404 | enddef |
| 405 | |
| 406 | def Test_reverse_return_type() |
| 407 | var l = reverse([1, 2, 3]) |
| 408 | var res = 0 |
| 409 | for n in l |
| 410 | res += n |
| 411 | endfor |
| 412 | res->assert_equal(6) |
| 413 | enddef |
| 414 | |
| 415 | def Test_search() |
| 416 | new |
| 417 | setline(1, ['foo', 'bar']) |
| 418 | var val = 0 |
| 419 | # skip expr returns boolean |
| 420 | search('bar', 'W', 0, 0, {-> val == 1})->assert_equal(2) |
| 421 | :1 |
| 422 | search('bar', 'W', 0, 0, {-> val == 0})->assert_equal(0) |
| 423 | # skip expr returns number, only 0 and 1 are accepted |
| 424 | :1 |
| 425 | search('bar', 'W', 0, 0, {-> 0})->assert_equal(2) |
| 426 | :1 |
| 427 | search('bar', 'W', 0, 0, {-> 1})->assert_equal(0) |
| 428 | assert_fails("search('bar', '', 0, 0, {-> -1})", 'E1023:') |
| 429 | assert_fails("search('bar', '', 0, 0, {-> -1})", 'E1023:') |
| 430 | enddef |
| 431 | |
| 432 | def Test_searchcount() |
| 433 | new |
| 434 | setline(1, "foo bar") |
| 435 | :/foo |
| 436 | searchcount(#{recompute: true}) |
| 437 | ->assert_equal(#{ |
| 438 | exact_match: 1, |
| 439 | current: 1, |
| 440 | total: 1, |
| 441 | maxcount: 99, |
| 442 | incomplete: 0}) |
| 443 | bwipe! |
| 444 | enddef |
| 445 | |
| 446 | def Test_searchdecl() |
| 447 | searchdecl('blah', true, true)->assert_equal(1) |
| 448 | enddef |
| 449 | |
| 450 | def Test_setbufvar() |
| 451 | setbufvar(bufnr('%'), '&syntax', 'vim') |
| 452 | &syntax->assert_equal('vim') |
| 453 | setbufvar(bufnr('%'), '&ts', 16) |
| 454 | &ts->assert_equal(16) |
| 455 | settabwinvar(1, 1, '&syntax', 'vam') |
| 456 | &syntax->assert_equal('vam') |
| 457 | settabwinvar(1, 1, '&ts', 15) |
| 458 | &ts->assert_equal(15) |
| 459 | setlocal ts=8 |
| 460 | |
| 461 | setbufvar('%', 'myvar', 123) |
| 462 | getbufvar('%', 'myvar')->assert_equal(123) |
| 463 | enddef |
| 464 | |
| 465 | def Test_setloclist() |
| 466 | var items = [#{filename: '/tmp/file', lnum: 1, valid: true}] |
| 467 | var what = #{items: items} |
| 468 | setqflist([], ' ', what) |
| 469 | setloclist(0, [], ' ', what) |
| 470 | enddef |
| 471 | |
| 472 | def Test_setreg() |
| 473 | setreg('a', ['aaa', 'bbb', 'ccc']) |
| 474 | var reginfo = getreginfo('a') |
| 475 | setreg('a', reginfo) |
| 476 | getreginfo('a')->assert_equal(reginfo) |
| 477 | enddef |
| 478 | |
| 479 | def Test_spellsuggest() |
| 480 | if !has('spell') |
| 481 | MissingFeature 'spell' |
| 482 | else |
| 483 | spellsuggest('marrch', 1, true)->assert_equal(['March']) |
| 484 | endif |
| 485 | enddef |
| 486 | |
| 487 | def Test_sort_return_type() |
| 488 | var res: list<number> |
| 489 | res = [1, 2, 3]->sort() |
| 490 | enddef |
| 491 | |
| 492 | def Test_sort_argument() |
| 493 | var res = ['b', 'a', 'c']->sort('i') |
| 494 | res->assert_equal(['a', 'b', 'c']) |
| 495 | enddef |
| 496 | |
| 497 | def Test_split() |
| 498 | split(' aa bb ', '\W\+', true)->assert_equal(['', 'aa', 'bb', '']) |
| 499 | enddef |
| 500 | |
| 501 | def Test_str2nr() |
| 502 | str2nr("1'000'000", 10, true)->assert_equal(1000000) |
| 503 | enddef |
| 504 | |
| 505 | def Test_strchars() |
| 506 | strchars("A\u20dd", true)->assert_equal(1) |
| 507 | enddef |
| 508 | |
| 509 | def Test_submatch() |
| 510 | var pat = 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)' |
| 511 | var Rep = {-> range(10)->map({_, v -> submatch(v, true)})->string()} |
| 512 | var actual = substitute('A123456789', pat, Rep, '') |
| 513 | var expected = "[['A123456789'], ['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9']]" |
| 514 | actual->assert_equal(expected) |
| 515 | enddef |
| 516 | |
| 517 | def Test_synID() |
| 518 | new |
| 519 | setline(1, "text") |
| 520 | synID(1, 1, true)->assert_equal(0) |
| 521 | bwipe! |
| 522 | enddef |
| 523 | |
| 524 | def Test_term_gettty() |
| 525 | if !has('terminal') |
| 526 | MissingFeature 'terminal' |
| 527 | else |
| 528 | var buf = Run_shell_in_terminal({}) |
| 529 | term_gettty(buf, true)->assert_notequal('') |
| 530 | StopShellInTerminal(buf) |
| 531 | endif |
| 532 | enddef |
| 533 | |
| 534 | def Test_term_start() |
| 535 | if !has('terminal') |
| 536 | MissingFeature 'terminal' |
| 537 | else |
| 538 | botright new |
| 539 | var winnr = winnr() |
| 540 | term_start(&shell, #{curwin: true}) |
| 541 | winnr()->assert_equal(winnr) |
| 542 | bwipe! |
| 543 | endif |
| 544 | enddef |
| 545 | |
| 546 | def Test_timer_paused() |
| 547 | var id = timer_start(50, {-> 0}) |
| 548 | timer_pause(id, true) |
| 549 | var info = timer_info(id) |
| 550 | info[0]['paused']->assert_equal(1) |
| 551 | timer_stop(id) |
| 552 | enddef |
| 553 | |
| 554 | def Test_win_splitmove() |
| 555 | split |
| 556 | win_splitmove(1, 2, #{vertical: true, rightbelow: true}) |
| 557 | close |
| 558 | enddef |
| 559 | |
| 560 | |
| 561 | " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker |