1Tests for the exists() function. vim: set ft=vim : 2 3STARTTEST 4:so small.vim 5:function! RunTest(str, result) 6 if exists(a:str) == a:result 7 echo "OK" 8 else 9 echo "FAILED: Checking for " . a:str 10 endif 11endfunction 12:function! TestExists() 13 augroup myagroup 14 autocmd! BufEnter *.my echo 'myfile edited' 15 augroup END 16 17 let test_cases = [] 18 19 " valid autocmd group 20 let test_cases += [['#myagroup', 1]] 21 " valid autocmd group with garbage 22 let test_cases += [['#myagroup+b', 0]] 23 " Valid autocmd group and event 24 let test_cases += [['#myagroup#BufEnter', 1]] 25 " Valid autocmd group, event and pattern 26 let test_cases += [['#myagroup#BufEnter#*.my', 1]] 27 " Valid autocmd event 28 let test_cases += [['#BufEnter', 1]] 29 " Valid autocmd event and pattern 30 let test_cases += [['#BufEnter#*.my', 1]] 31 " Non-existing autocmd group or event 32 let test_cases += [['#xyzagroup', 0]] 33 " Non-existing autocmd group and valid autocmd event 34 let test_cases += [['#xyzagroup#BufEnter', 0]] 35 " Valid autocmd group and event with no matching pattern 36 let test_cases += [['#myagroup#CmdwinEnter', 0]] 37 " Valid autocmd group and non-existing autocmd event 38 let test_cases += [['#myagroup#xyzacmd', 0]] 39 " Valid autocmd group and event and non-matching pattern 40 let test_cases += [['#myagroup#BufEnter#xyzpat', 0]] 41 " Valid autocmd event and non-matching pattern 42 let test_cases += [['#BufEnter#xyzpat', 0]] 43 " Empty autocmd group, event and pattern 44 let test_cases += [['###', 0]] 45 " Empty autocmd group and event or empty event and pattern 46 let test_cases += [['##', 0]] 47 " Valid autocmd event 48 let test_cases += [['##FileReadCmd', 1]] 49 " Non-existing autocmd event 50 let test_cases += [['##MySpecialCmd', 0]] 51 52 " Existing and working option (long form) 53 let test_cases += [['&textwidth', 1]] 54 " Existing and working option (short form) 55 let test_cases += [['&tw', 1]] 56 " Existing and working option with garbage 57 let test_cases += [['&tw-', 0]] 58 " Global option 59 let test_cases += [['&g:errorformat', 1]] 60 " Local option 61 let test_cases += [['&l:errorformat', 1]] 62 " Negative form of existing and working option (long form) 63 let test_cases += [['&nojoinspaces', 0]] 64 " Negative form of existing and working option (short form) 65 let test_cases += [['&nojs', 0]] 66 " Non-existing option 67 let test_cases += [['&myxyzoption', 0]] 68 69 " Existing and working option (long form) 70 let test_cases += [['+incsearch', 1]] 71 " Existing and working option with garbage 72 let test_cases += [['+incsearch!1', 0]] 73 " Existing and working option (short form) 74 let test_cases += [['+is', 1]] 75 " Existing option that is hidden. 76 let test_cases += [['+autoprint', 0]] 77 78 " Existing environment variable 79 let $EDITOR_NAME = 'Vim Editor' 80 let test_cases += [['$EDITOR_NAME', 1]] 81 " Non-existing environment variable 82 let test_cases += [['$NON_ENV_VAR', 0]] 83 84 " Valid internal function 85 let test_cases += [['*bufnr', 1]] 86 " Valid internal function with () 87 let test_cases += [['*bufnr()', 1]] 88 " Non-existing internal function 89 let test_cases += [['*myxyzfunc', 0]] 90 " Valid internal function with garbage 91 let test_cases += [['*bufnr&6', 0]] 92 93 " Valid user defined function 94 let test_cases += [['*TestExists', 1]] 95 " Non-existing user defined function 96 let test_cases += [['*MyxyzFunc', 0]] 97 98 redir! > test.out 99 100 for [test_case, result] in test_cases 101 echo test_case . ": " . result 102 call RunTest(test_case, result) 103 endfor 104 105 " Valid internal command (full match) 106 echo ':edit: 2' 107 if exists(':edit') == 2 108 echo "OK" 109 else 110 echo "FAILED" 111 endif 112 113 " Valid internal command (full match) with garbage 114 echo ':edit/a: 0' 115 if exists(':edit/a') == 0 116 echo "OK" 117 else 118 echo "FAILED" 119 endif 120 121 " Valid internal command (partial match) 122 echo ':q: 1' 123 if exists(':q') == 1 124 echo "OK" 125 else 126 echo "FAILED" 127 endif 128 129 " Non-existing internal command 130 echo ':invalidcmd: 0' 131 if !exists(':invalidcmd') 132 echo "OK" 133 else 134 echo "FAILED" 135 endif 136 137 " User defined command (full match) 138 command! MyCmd :echo 'My command' 139 echo ':MyCmd: 2' 140 if exists(':MyCmd') == 2 141 echo "OK" 142 else 143 echo "FAILED" 144 endif 145 146 " User defined command (partial match) 147 command! MyOtherCmd :echo 'Another command' 148 echo ':My: 3' 149 if exists(':My') == 3 150 echo "OK" 151 else 152 echo "FAILED" 153 endif 154 155 " Command modifier 156 echo ':rightbelow: 2' 157 if exists(':rightbelow') == 2 158 echo "OK" 159 else 160 echo "FAILED" 161 endif 162 163 " Non-existing user defined command (full match) 164 delcommand MyCmd 165 166 echo ':MyCmd: 0' 167 if !exists(':MyCmd') 168 echo "OK" 169 else 170 echo "FAILED" 171 endif 172 173 " Non-existing user defined command (partial match) 174 delcommand MyOtherCmd 175 176 echo ':My: 0' 177 if !exists(':My') 178 echo "OK" 179 else 180 echo "FAILED" 181 endif 182 183 " Valid local variable 184 let local_var = 1 185 echo 'local_var: 1' 186 if exists('local_var') 187 echo "OK" 188 else 189 echo "FAILED" 190 endif 191 192 " Valid local variable with garbage 193 let local_var = 1 194 echo 'local_var%n: 0' 195 if !exists('local_var%n') 196 echo "OK" 197 else 198 echo "FAILED" 199 endif 200 201 " Non-existing local variable 202 unlet local_var 203 echo 'local_var: 0' 204 if !exists('local_var') 205 echo "OK" 206 else 207 echo "FAILED" 208 endif 209 210 " Valid local list 211 let local_list = ["blue", "orange"] 212 echo 'local_list: 1' 213 if exists('local_list') 214 echo "OK" 215 else 216 echo "FAILED" 217 endif 218 219 " Valid local list item 220 echo 'local_list[1]: 1' 221 if exists('local_list[1]') 222 echo "OK" 223 else 224 echo "FAILED" 225 endif 226 227 " Valid local list item with garbage 228 echo 'local_list[1]+5: 0' 229 if !exists('local_list[1]+5') 230 echo "OK" 231 else 232 echo "FAILED" 233 endif 234 235 " Invalid local list item 236 echo 'local_list[2]: 0' 237 if !exists('local_list[2]') 238 echo "OK" 239 else 240 echo "FAILED" 241 endif 242 243 " Non-existing local list 244 unlet local_list 245 echo 'local_list: 0' 246 if !exists('local_list') 247 echo "OK" 248 else 249 echo "FAILED" 250 endif 251 252 " Valid local dictionary 253 let local_dict = {"xcord":100, "ycord":2} 254 echo 'local_dict: 1' 255 if exists('local_dict') 256 echo "OK" 257 else 258 echo "FAILED" 259 endif 260 261 " Non-existing local dictionary 262 unlet local_dict 263 echo 'local_dict: 0' 264 if !exists('local_dict') 265 echo "OK" 266 else 267 echo "FAILED" 268 endif 269 270 " Existing local curly-brace variable 271 let str = "local" 272 let curly_{str}_var = 1 273 echo 'curly_' . str . '_var: 1' 274 if exists('curly_{str}_var') 275 echo "OK" 276 else 277 echo "FAILED" 278 endif 279 280 " Non-existing local curly-brace variable 281 unlet curly_{str}_var 282 echo 'curly_' . str . '_var: 0' 283 if !exists('curly_{str}_var') 284 echo "OK" 285 else 286 echo "FAILED" 287 endif 288 289 290 " Existing global variable 291 let g:global_var = 1 292 echo 'g:global_var: 1' 293 if exists('g:global_var') 294 echo "OK" 295 else 296 echo "FAILED" 297 endif 298 299 " Existing global variable with garbage 300 echo 'g:global_var-n: 1' 301 if !exists('g:global_var-n') 302 echo "OK" 303 else 304 echo "FAILED" 305 endif 306 307 " Non-existing global variable 308 unlet g:global_var 309 echo 'g:global_var: 0' 310 if !exists('g:global_var') 311 echo "OK" 312 else 313 echo "FAILED" 314 endif 315 316 " Existing global list 317 let g:global_list = ["blue", "orange"] 318 echo 'g:global_list: 1' 319 if exists('g:global_list') 320 echo "OK" 321 else 322 echo "FAILED" 323 endif 324 325 " Non-existing global list 326 unlet g:global_list 327 echo 'g:global_list: 0' 328 if !exists('g:global_list') 329 echo "OK" 330 else 331 echo "FAILED" 332 endif 333 334 " Existing global dictionary 335 let g:global_dict = {"xcord":100, "ycord":2} 336 echo 'g:global_dict: 1' 337 if exists('g:global_dict') 338 echo "OK" 339 else 340 echo "FAILED" 341 endif 342 343 " Non-existing global dictionary 344 unlet g:global_dict 345 echo 'g:global_dict: 0' 346 if !exists('g:global_dict') 347 echo "OK" 348 else 349 echo "FAILED" 350 endif 351 352 " Existing global curly-brace variable 353 let str = "global" 354 let g:curly_{str}_var = 1 355 echo 'g:curly_' . str . '_var: 1' 356 if exists('g:curly_{str}_var') 357 echo "OK" 358 else 359 echo "FAILED" 360 endif 361 362 " Non-existing global curly-brace variable 363 unlet g:curly_{str}_var 364 echo 'g:curly_' . str . '_var: 0' 365 if !exists('g:curly_{str}_var') 366 echo "OK" 367 else 368 echo "FAILED" 369 endif 370 371 " Existing window variable 372 echo 'w:window_var: 1' 373 let w:window_var = 1 374 if exists('w:window_var') 375 echo "OK" 376 else 377 echo "FAILED" 378 endif 379 380 " Non-existing window variable 381 unlet w:window_var 382 echo 'w:window_var: 0' 383 if !exists('w:window_var') 384 echo "OK" 385 else 386 echo "FAILED" 387 endif 388 389 " Existing window list 390 let w:window_list = ["blue", "orange"] 391 echo 'w:window_list: 1' 392 if exists('w:window_list') 393 echo "OK" 394 else 395 echo "FAILED" 396 endif 397 398 " Non-existing window list 399 unlet w:window_list 400 echo 'w:window_list: 0' 401 if !exists('w:window_list') 402 echo "OK" 403 else 404 echo "FAILED" 405 endif 406 407 " Existing window dictionary 408 let w:window_dict = {"xcord":100, "ycord":2} 409 echo 'w:window_dict: 1' 410 if exists('w:window_dict') 411 echo "OK" 412 else 413 echo "FAILED" 414 endif 415 416 " Non-existing window dictionary 417 unlet w:window_dict 418 echo 'w:window_dict: 0' 419 if !exists('w:window_dict') 420 echo "OK" 421 else 422 echo "FAILED" 423 endif 424 425 " Existing window curly-brace variable 426 let str = "window" 427 let w:curly_{str}_var = 1 428 echo 'w:curly_' . str . '_var: 1' 429 if exists('w:curly_{str}_var') 430 echo "OK" 431 else 432 echo "FAILED" 433 endif 434 435 " Non-existing window curly-brace variable 436 unlet w:curly_{str}_var 437 echo 'w:curly_' . str . '_var: 0' 438 if !exists('w:curly_{str}_var') 439 echo "OK" 440 else 441 echo "FAILED" 442 endif 443 444 " Existing buffer variable 445 echo 'b:buffer_var: 1' 446 let b:buffer_var = 1 447 if exists('b:buffer_var') 448 echo "OK" 449 else 450 echo "FAILED" 451 endif 452 453 " Non-existing buffer variable 454 unlet b:buffer_var 455 echo 'b:buffer_var: 0' 456 if !exists('b:buffer_var') 457 echo "OK" 458 else 459 echo "FAILED" 460 endif 461 462 " Existing buffer list 463 let b:buffer_list = ["blue", "orange"] 464 echo 'b:buffer_list: 1' 465 if exists('b:buffer_list') 466 echo "OK" 467 else 468 echo "FAILED" 469 endif 470 471 " Non-existing buffer list 472 unlet b:buffer_list 473 echo 'b:buffer_list: 0' 474 if !exists('b:buffer_list') 475 echo "OK" 476 else 477 echo "FAILED" 478 endif 479 480 " Existing buffer dictionary 481 let b:buffer_dict = {"xcord":100, "ycord":2} 482 echo 'b:buffer_dict: 1' 483 if exists('b:buffer_dict') 484 echo "OK" 485 else 486 echo "FAILED" 487 endif 488 489 " Non-existing buffer dictionary 490 unlet b:buffer_dict 491 echo 'b:buffer_dict: 0' 492 if !exists('b:buffer_dict') 493 echo "OK" 494 else 495 echo "FAILED" 496 endif 497 498 " Existing buffer curly-brace variable 499 let str = "buffer" 500 let b:curly_{str}_var = 1 501 echo 'b:curly_' . str . '_var: 1' 502 if exists('b:curly_{str}_var') 503 echo "OK" 504 else 505 echo "FAILED" 506 endif 507 508 " Non-existing buffer curly-brace variable 509 unlet b:curly_{str}_var 510 echo 'b:curly_' . str . '_var: 0' 511 if !exists('b:curly_{str}_var') 512 echo "OK" 513 else 514 echo "FAILED" 515 endif 516 517 " Script-local tests 518 source test60.vim 519 520 " Existing Vim internal variable 521 echo 'v:version: 1' 522 if exists('v:version') 523 echo "OK" 524 else 525 echo "FAILED" 526 endif 527 528 " Non-existing Vim internal variable 529 echo 'v:non_exists_var: 0' 530 if !exists('v:non_exists_var') 531 echo "OK" 532 else 533 echo "FAILED" 534 endif 535 536 " Function arguments 537 function TestFuncArg(func_arg, ...) 538 echo 'a:func_arg: 1' 539 if exists('a:func_arg') 540 echo "OK" 541 else 542 echo "FAILED" 543 endif 544 545 echo 'a:non_exists_arg: 0' 546 if !exists('a:non_exists_arg') 547 echo "OK" 548 else 549 echo "FAILED" 550 endif 551 552 echo 'a:1: 1' 553 if exists('a:1') 554 echo "OK" 555 else 556 echo "FAILED" 557 endif 558 559 echo 'a:2: 0' 560 if !exists('a:2') 561 echo "OK" 562 else 563 echo "FAILED" 564 endif 565 endfunction 566 567 call TestFuncArg("arg1", "arg2") 568 569 redir END 570endfunction 571:call TestExists() 572:delfunc TestExists 573:delfunc RunTest 574:delfunc TestFuncArg 575:edit! test.out 576:set ff=unix 577:w 578:qa! 579ENDTEST 580 581