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