060_exists_and_has_functions_spec.lua (22720B)
1 -- Tests for the exists() and has() functions. 2 3 local t = require('test.testutil') 4 local n = require('test.functional.testnvim')() 5 6 local source = n.source 7 local clear, expect = n.clear, n.expect 8 local write_file = t.write_file 9 10 describe('exists() and has() functions', function() 11 setup(function() 12 clear() 13 -- Create a temporary script needed for the test. 14 write_file( 15 'test60.vim', 16 [[ 17 " Vim script for exists() function test 18 " Script-local variables are checked here 19 20 " Existing script-local variable 21 let s:script_var = 1 22 echo 's:script_var: 1' 23 if exists('s:script_var') 24 echo "OK" 25 else 26 echo "FAILED" 27 endif 28 29 " Non-existing script-local variable 30 unlet s:script_var 31 echo 's:script_var: 0' 32 if !exists('s:script_var') 33 echo "OK" 34 else 35 echo "FAILED" 36 endif 37 38 " Existing script-local list 39 let s:script_list = ["blue", "orange"] 40 echo 's:script_list: 1' 41 if exists('s:script_list') 42 echo "OK" 43 else 44 echo "FAILED" 45 endif 46 47 " Non-existing script-local list 48 unlet s:script_list 49 echo 's:script_list: 0' 50 if !exists('s:script_list') 51 echo "OK" 52 else 53 echo "FAILED" 54 endif 55 56 " Existing script-local dictionary 57 let s:script_dict = {"xcord":100, "ycord":2} 58 echo 's:script_dict: 1' 59 if exists('s:script_dict') 60 echo "OK" 61 else 62 echo "FAILED" 63 endif 64 65 " Non-existing script-local dictionary 66 unlet s:script_dict 67 echo 's:script_dict: 0' 68 if !exists('s:script_dict') 69 echo "OK" 70 else 71 echo "FAILED" 72 endif 73 74 " Existing script curly-brace variable 75 let str = "script" 76 let s:curly_{str}_var = 1 77 echo 's:curly_' . str . '_var: 1' 78 if exists('s:curly_{str}_var') 79 echo "OK" 80 else 81 echo "FAILED" 82 endif 83 84 " Non-existing script-local curly-brace variable 85 unlet s:curly_{str}_var 86 echo 's:curly_' . str . '_var: 0' 87 if !exists('s:curly_{str}_var') 88 echo "OK" 89 else 90 echo "FAILED" 91 endif 92 93 " Existing script-local function 94 function! s:my_script_func() 95 endfunction 96 97 echo '*s:my_script_func: 1' 98 if exists('*s:my_script_func') 99 echo "OK" 100 else 101 echo "FAILED" 102 endif 103 104 " Non-existing script-local function 105 delfunction s:my_script_func 106 107 echo '*s:my_script_func: 0' 108 if !exists('*s:my_script_func') 109 echo "OK" 110 else 111 echo "FAILED" 112 endif 113 unlet str 114 ]] 115 ) 116 end) 117 teardown(function() 118 os.remove('test.out') 119 os.remove('test60.vim') 120 end) 121 122 it('is working', function() 123 source([=[ 124 " Add the special directory with test scripts to &rtp 125 set rtp+=test/functional/fixtures 126 set wildchar=^E 127 function! RunTest(str, result) 128 if exists(a:str) == a:result 129 echo "OK" 130 else 131 echo "FAILED: Checking for " . a:str 132 endif 133 endfunction 134 function! TestExists() 135 augroup myagroup 136 autocmd! BufEnter *.my echo "myfile edited" 137 autocmd! FuncUndefined UndefFun exec "fu UndefFun()\nendfu" 138 augroup END 139 set rtp+=./sautest 140 let test_cases = [] 141 " Valid autocmd group. 142 let test_cases += [['#myagroup', 1]] 143 " Valid autocmd group with garbage. 144 let test_cases += [['#myagroup+b', 0]] 145 " Valid autocmd group and event. 146 let test_cases += [['#myagroup#BufEnter', 1]] 147 " Valid autocmd group, event and pattern. 148 let test_cases += [['#myagroup#BufEnter#*.my', 1]] 149 " Valid autocmd event. 150 let test_cases += [['#BufEnter', 1]] 151 " Valid autocmd event and pattern. 152 let test_cases += [['#BufEnter#*.my', 1]] 153 " Non-existing autocmd group or event. 154 let test_cases += [['#xyzagroup', 0]] 155 " Non-existing autocmd group and valid autocmd event. 156 let test_cases += [['#xyzagroup#BufEnter', 0]] 157 " Valid autocmd group and event with no matching pattern. 158 let test_cases += [['#myagroup#CmdwinEnter', 0]] 159 " Valid autocmd group and non-existing autocmd event. 160 let test_cases += [['#myagroup#xyzacmd', 0]] 161 " Valid autocmd group and event and non-matching pattern. 162 let test_cases += [['#myagroup#BufEnter#xyzpat', 0]] 163 " Valid autocmd event and non-matching pattern. 164 let test_cases += [['#BufEnter#xyzpat', 0]] 165 " Empty autocmd group, event and pattern. 166 let test_cases += [['###', 0]] 167 " Empty autocmd group and event or empty event and pattern. 168 let test_cases += [['##', 0]] 169 " Valid autocmd event. 170 let test_cases += [['##FileReadCmd', 1]] 171 " Non-existing autocmd event. 172 let test_cases += [['##MySpecialCmd', 0]] 173 " Existing and working option (long form). 174 let test_cases += [['&textwidth', 1]] 175 " Existing and working option (short form). 176 let test_cases += [['&tw', 1]] 177 " Existing and working option with garbage. 178 let test_cases += [['&tw-', 0]] 179 " Global option. 180 let test_cases += [['&g:errorformat', 1]] 181 " Local option. 182 let test_cases += [['&l:errorformat', 1]] 183 " Negative form of existing and working option (long form). 184 let test_cases += [['&nojoinspaces', 0]] 185 " Negative form of existing and working option (short form). 186 let test_cases += [['&nojs', 0]] 187 " Non-existing option. 188 let test_cases += [['&myxyzoption', 0]] 189 " Existing and working option (long form). 190 let test_cases += [['+incsearch', 1]] 191 " Existing and working option with garbage. 192 let test_cases += [['+incsearch!1', 0]] 193 " Existing and working option (short form). 194 let test_cases += [['+is', 1]] 195 " Existing option that is hidden. 196 let test_cases += [['+mouseshape', 0]] 197 " Existing environment variable. 198 let $EDITOR_NAME = 'Vim Editor' 199 let test_cases += [['$EDITOR_NAME', 1]] 200 " Non-existing environment variable. 201 let test_cases += [['$NON_ENV_VAR', 0]] 202 " Valid internal function. 203 let test_cases += [['*bufnr', 1]] 204 " Valid internal function with (). 205 let test_cases += [['*bufnr()', 1]] 206 " Non-existing internal function. 207 let test_cases += [['*myxyzfunc', 0]] 208 " Valid internal function with garbage. 209 let test_cases += [['*bufnr&6', 0]] 210 " Valid user defined function. 211 let test_cases += [['*TestExists', 1]] 212 " Non-existing user defined function. 213 let test_cases += [['*MyxyzFunc', 0]] 214 " Function that may be created by FuncUndefined event. 215 let test_cases += [['*UndefFun', 0]] 216 " Function that may be created by script autoloading. 217 let test_cases += [['*footest#F', 0]] 218 redir! > test.out 219 for [test_case, result] in test_cases 220 echo test_case . ": " . result 221 call RunTest(test_case, result) 222 endfor 223 " Valid internal command (full match). 224 echo ':edit: 2' 225 if exists(':edit') == 2 226 echo "OK" 227 else 228 echo "FAILED" 229 endif 230 " Valid internal command (full match) with garbage. 231 echo ':edit/a: 0' 232 if exists(':edit/a') == 0 233 echo "OK" 234 else 235 echo "FAILED" 236 endif 237 " Valid internal command (partial match). 238 echo ':q: 1' 239 if exists(':q') == 1 240 echo "OK" 241 else 242 echo "FAILED" 243 endif 244 " Non-existing internal command. 245 echo ':invalidcmd: 0' 246 if !exists(':invalidcmd') 247 echo "OK" 248 else 249 echo "FAILED" 250 endif 251 " User defined command (full match). 252 command! MyCmd :echo 'My command' 253 echo ':MyCmd: 2' 254 if exists(':MyCmd') == 2 255 echo "OK" 256 else 257 echo "FAILED" 258 endif 259 " User defined command (partial match). 260 command! MyOtherCmd :echo 'Another command' 261 echo ':My: 3' 262 if exists(':My') == 3 263 echo "OK" 264 else 265 echo "FAILED" 266 endif 267 " Command modifier. 268 echo ':rightbelow: 2' 269 if exists(':rightbelow') == 2 270 echo "OK" 271 else 272 echo "FAILED" 273 endif 274 " Non-existing user defined command (full match). 275 delcommand MyCmd 276 echo ':MyCmd: 0' 277 if !exists(':MyCmd') 278 echo "OK" 279 else 280 echo "FAILED" 281 endif 282 " Non-existing user defined command (partial match). 283 delcommand MyOtherCmd 284 echo ':My: 0' 285 if !exists(':My') 286 echo "OK" 287 else 288 echo "FAILED" 289 endif 290 " Valid local variable. 291 let local_var = 1 292 echo 'local_var: 1' 293 if exists('local_var') 294 echo "OK" 295 else 296 echo "FAILED" 297 endif 298 " Valid local variable with garbage. 299 let local_var = 1 300 echo 'local_var%n: 0' 301 if !exists('local_var%n') 302 echo "OK" 303 else 304 echo "FAILED" 305 endif 306 " Non-existing local variable. 307 unlet local_var 308 echo 'local_var: 0' 309 if !exists('local_var') 310 echo "OK" 311 else 312 echo "FAILED" 313 endif 314 " Non-existing autoload variable that may be autoloaded. 315 echo 'footest#x: 0' 316 if !exists('footest#x') 317 echo "OK" 318 else 319 echo "FAILED" 320 endif 321 " Valid local list. 322 let local_list = ["blue", "orange"] 323 echo 'local_list: 1' 324 if exists('local_list') 325 echo "OK" 326 else 327 echo "FAILED" 328 endif 329 " Valid local list item. 330 echo 'local_list[1]: 1' 331 if exists('local_list[1]') 332 echo "OK" 333 else 334 echo "FAILED" 335 endif 336 " Valid local list item with garbage. 337 echo 'local_list[1]+5: 0' 338 if !exists('local_list[1]+5') 339 echo "OK" 340 else 341 echo "FAILED" 342 endif 343 " Invalid local list item. 344 echo 'local_list[2]: 0' 345 if !exists('local_list[2]') 346 echo "OK" 347 else 348 echo "FAILED" 349 endif 350 " Non-existing local list. 351 unlet local_list 352 echo 'local_list: 0' 353 if !exists('local_list') 354 echo "OK" 355 else 356 echo "FAILED" 357 endif 358 " Valid local dictionary. 359 let local_dict = {"xcord":100, "ycord":2} 360 echo 'local_dict: 1' 361 if exists('local_dict') 362 echo "OK" 363 else 364 echo "FAILED" 365 endif 366 " Non-existing local dictionary. 367 unlet local_dict 368 echo 'local_dict: 0' 369 if !exists('local_dict') 370 echo "OK" 371 else 372 echo "FAILED" 373 endif 374 " Existing local curly-brace variable. 375 let str = "local" 376 let curly_{str}_var = 1 377 echo 'curly_' . str . '_var: 1' 378 if exists('curly_{str}_var') 379 echo "OK" 380 else 381 echo "FAILED" 382 endif 383 " Non-existing local curly-brace variable. 384 unlet curly_{str}_var 385 echo 'curly_' . str . '_var: 0' 386 if !exists('curly_{str}_var') 387 echo "OK" 388 else 389 echo "FAILED" 390 endif 391 " Existing global variable. 392 let g:global_var = 1 393 echo 'g:global_var: 1' 394 if exists('g:global_var') 395 echo "OK" 396 else 397 echo "FAILED" 398 endif 399 " Existing global variable with garbage. 400 echo 'g:global_var-n: 1' 401 if !exists('g:global_var-n') 402 echo "OK" 403 else 404 echo "FAILED" 405 endif 406 " Non-existing global variable. 407 unlet g:global_var 408 echo 'g:global_var: 0' 409 if !exists('g:global_var') 410 echo "OK" 411 else 412 echo "FAILED" 413 endif 414 " Existing global list. 415 let g:global_list = ["blue", "orange"] 416 echo 'g:global_list: 1' 417 if exists('g:global_list') 418 echo "OK" 419 else 420 echo "FAILED" 421 endif 422 " Non-existing global list. 423 unlet g:global_list 424 echo 'g:global_list: 0' 425 if !exists('g:global_list') 426 echo "OK" 427 else 428 echo "FAILED" 429 endif 430 " Existing global dictionary. 431 let g:global_dict = {"xcord":100, "ycord":2} 432 echo 'g:global_dict: 1' 433 if exists('g:global_dict') 434 echo "OK" 435 else 436 echo "FAILED" 437 endif 438 " Non-existing global dictionary. 439 unlet g:global_dict 440 echo 'g:global_dict: 0' 441 if !exists('g:global_dict') 442 echo "OK" 443 else 444 echo "FAILED" 445 endif 446 " Existing global curly-brace variable. 447 let str = "global" 448 let g:curly_{str}_var = 1 449 echo 'g:curly_' . str . '_var: 1' 450 if exists('g:curly_{str}_var') 451 echo "OK" 452 else 453 echo "FAILED" 454 endif 455 " Non-existing global curly-brace variable. 456 unlet g:curly_{str}_var 457 echo 'g:curly_' . str . '_var: 0' 458 if !exists('g:curly_{str}_var') 459 echo "OK" 460 else 461 echo "FAILED" 462 endif 463 " Existing window variable. 464 echo 'w:window_var: 1' 465 let w:window_var = 1 466 if exists('w:window_var') 467 echo "OK" 468 else 469 echo "FAILED" 470 endif 471 " Non-existing window variable. 472 unlet w:window_var 473 echo 'w:window_var: 0' 474 if !exists('w:window_var') 475 echo "OK" 476 else 477 echo "FAILED" 478 endif 479 " Existing window list. 480 let w:window_list = ["blue", "orange"] 481 echo 'w:window_list: 1' 482 if exists('w:window_list') 483 echo "OK" 484 else 485 echo "FAILED" 486 endif 487 " Non-existing window list. 488 unlet w:window_list 489 echo 'w:window_list: 0' 490 if !exists('w:window_list') 491 echo "OK" 492 else 493 echo "FAILED" 494 endif 495 " Existing window dictionary. 496 let w:window_dict = {"xcord":100, "ycord":2} 497 echo 'w:window_dict: 1' 498 if exists('w:window_dict') 499 echo "OK" 500 else 501 echo "FAILED" 502 endif 503 " Non-existing window dictionary. 504 unlet w:window_dict 505 echo 'w:window_dict: 0' 506 if !exists('w:window_dict') 507 echo "OK" 508 else 509 echo "FAILED" 510 endif 511 " Existing window curly-brace variable. 512 let str = "window" 513 let w:curly_{str}_var = 1 514 echo 'w:curly_' . str . '_var: 1' 515 if exists('w:curly_{str}_var') 516 echo "OK" 517 else 518 echo "FAILED" 519 endif 520 " Non-existing window curly-brace variable. 521 unlet w:curly_{str}_var 522 echo 'w:curly_' . str . '_var: 0' 523 if !exists('w:curly_{str}_var') 524 echo "OK" 525 else 526 echo "FAILED" 527 endif 528 " Existing buffer variable. 529 echo 'b:buffer_var: 1' 530 let b:buffer_var = 1 531 if exists('b:buffer_var') 532 echo "OK" 533 else 534 echo "FAILED" 535 endif 536 " Non-existing buffer variable. 537 unlet b:buffer_var 538 echo 'b:buffer_var: 0' 539 if !exists('b:buffer_var') 540 echo "OK" 541 else 542 echo "FAILED" 543 endif 544 " Existing buffer list. 545 let b:buffer_list = ["blue", "orange"] 546 echo 'b:buffer_list: 1' 547 if exists('b:buffer_list') 548 echo "OK" 549 else 550 echo "FAILED" 551 endif 552 " Non-existing buffer list. 553 unlet b:buffer_list 554 echo 'b:buffer_list: 0' 555 if !exists('b:buffer_list') 556 echo "OK" 557 else 558 echo "FAILED" 559 endif 560 " Existing buffer dictionary. 561 let b:buffer_dict = {"xcord":100, "ycord":2} 562 echo 'b:buffer_dict: 1' 563 if exists('b:buffer_dict') 564 echo "OK" 565 else 566 echo "FAILED" 567 endif 568 " Non-existing buffer dictionary. 569 unlet b:buffer_dict 570 echo 'b:buffer_dict: 0' 571 if !exists('b:buffer_dict') 572 echo "OK" 573 else 574 echo "FAILED" 575 endif 576 " Existing buffer curly-brace variable. 577 let str = "buffer" 578 let b:curly_{str}_var = 1 579 echo 'b:curly_' . str . '_var: 1' 580 if exists('b:curly_{str}_var') 581 echo "OK" 582 else 583 echo "FAILED" 584 endif 585 " Non-existing buffer curly-brace variable. 586 unlet b:curly_{str}_var 587 echo 'b:curly_' . str . '_var: 0' 588 if !exists('b:curly_{str}_var') 589 echo "OK" 590 else 591 echo "FAILED" 592 endif 593 " Script-local tests. 594 source test60.vim 595 " Existing Vim internal variable. 596 echo 'v:version: 1' 597 if exists('v:version') 598 echo "OK" 599 else 600 echo "FAILED" 601 endif 602 " Non-existing Vim internal variable. 603 echo 'v:non_exists_var: 0' 604 if !exists('v:non_exists_var') 605 echo "OK" 606 else 607 echo "FAILED" 608 endif 609 " Function arguments. 610 function TestFuncArg(func_arg, ...) 611 echo 'a:func_arg: 1' 612 if exists('a:func_arg') 613 echo "OK" 614 else 615 echo "FAILED" 616 endif 617 echo 'a:non_exists_arg: 0' 618 if !exists('a:non_exists_arg') 619 echo "OK" 620 else 621 echo "FAILED" 622 endif 623 echo 'a:1: 1' 624 if exists('a:1') 625 echo "OK" 626 else 627 echo "FAILED" 628 endif 629 echo 'a:2: 0' 630 if !exists('a:2') 631 echo "OK" 632 else 633 echo "FAILED" 634 endif 635 endfunction 636 call TestFuncArg("arg1", "arg2") 637 echo ' g:footest#x =' g:footest#x 638 echo ' footest#F()' footest#F() 639 echo 'UndefFun()' UndefFun() 640 redir END 641 endfunction 642 643 call TestExists() 644 645 edit! test.out 646 set ff=unix 647 ]=]) 648 649 -- Assert buffer contents. 650 expect([[ 651 652 #myagroup: 1 653 OK 654 #myagroup+b: 0 655 OK 656 #myagroup#BufEnter: 1 657 OK 658 #myagroup#BufEnter#*.my: 1 659 OK 660 #BufEnter: 1 661 OK 662 #BufEnter#*.my: 1 663 OK 664 #xyzagroup: 0 665 OK 666 #xyzagroup#BufEnter: 0 667 OK 668 #myagroup#CmdwinEnter: 0 669 OK 670 #myagroup#xyzacmd: 0 671 OK 672 #myagroup#BufEnter#xyzpat: 0 673 OK 674 #BufEnter#xyzpat: 0 675 OK 676 ###: 0 677 OK 678 ##: 0 679 OK 680 ##FileReadCmd: 1 681 OK 682 ##MySpecialCmd: 0 683 OK 684 &textwidth: 1 685 OK 686 &tw: 1 687 OK 688 &tw-: 0 689 OK 690 &g:errorformat: 1 691 OK 692 &l:errorformat: 1 693 OK 694 &nojoinspaces: 0 695 OK 696 &nojs: 0 697 OK 698 &myxyzoption: 0 699 OK 700 +incsearch: 1 701 OK 702 +incsearch!1: 0 703 OK 704 +is: 1 705 OK 706 +mouseshape: 0 707 OK 708 $EDITOR_NAME: 1 709 OK 710 $NON_ENV_VAR: 0 711 OK 712 *bufnr: 1 713 OK 714 *bufnr(): 1 715 OK 716 *myxyzfunc: 0 717 OK 718 *bufnr&6: 0 719 OK 720 *TestExists: 1 721 OK 722 *MyxyzFunc: 0 723 OK 724 *UndefFun: 0 725 OK 726 *footest#F: 0 727 OK 728 :edit: 2 729 OK 730 :edit/a: 0 731 OK 732 :q: 1 733 OK 734 :invalidcmd: 0 735 OK 736 :MyCmd: 2 737 OK 738 :My: 3 739 OK 740 :rightbelow: 2 741 OK 742 :MyCmd: 0 743 OK 744 :My: 0 745 OK 746 local_var: 1 747 OK 748 local_var%n: 0 749 OK 750 local_var: 0 751 OK 752 footest#x: 0 753 OK 754 local_list: 1 755 OK 756 local_list[1]: 1 757 OK 758 local_list[1]+5: 0 759 OK 760 local_list[2]: 0 761 OK 762 local_list: 0 763 OK 764 local_dict: 1 765 OK 766 local_dict: 0 767 OK 768 curly_local_var: 1 769 OK 770 curly_local_var: 0 771 OK 772 g:global_var: 1 773 OK 774 g:global_var-n: 1 775 OK 776 g:global_var: 0 777 OK 778 g:global_list: 1 779 OK 780 g:global_list: 0 781 OK 782 g:global_dict: 1 783 OK 784 g:global_dict: 0 785 OK 786 g:curly_global_var: 1 787 OK 788 g:curly_global_var: 0 789 OK 790 w:window_var: 1 791 OK 792 w:window_var: 0 793 OK 794 w:window_list: 1 795 OK 796 w:window_list: 0 797 OK 798 w:window_dict: 1 799 OK 800 w:window_dict: 0 801 OK 802 w:curly_window_var: 1 803 OK 804 w:curly_window_var: 0 805 OK 806 b:buffer_var: 1 807 OK 808 b:buffer_var: 0 809 OK 810 b:buffer_list: 1 811 OK 812 b:buffer_list: 0 813 OK 814 b:buffer_dict: 1 815 OK 816 b:buffer_dict: 0 817 OK 818 b:curly_buffer_var: 1 819 OK 820 b:curly_buffer_var: 0 821 OK 822 s:script_var: 1 823 OK 824 s:script_var: 0 825 OK 826 s:script_list: 1 827 OK 828 s:script_list: 0 829 OK 830 s:script_dict: 1 831 OK 832 s:script_dict: 0 833 OK 834 s:curly_script_var: 1 835 OK 836 s:curly_script_var: 0 837 OK 838 *s:my_script_func: 1 839 OK 840 *s:my_script_func: 0 841 OK 842 v:version: 1 843 OK 844 v:non_exists_var: 0 845 OK 846 a:func_arg: 1 847 OK 848 a:non_exists_arg: 0 849 OK 850 a:1: 1 851 OK 852 a:2: 0 853 OK 854 g:footest#x = 1 855 footest#F() 0 856 UndefFun() 0]]) 857 end) 858 end)