1 Tests for the exists() function. vim: set ft=vim :
5 :function! RunTest(str, result)
6 if exists(a:str) == a:result
9 echo "FAILED: Checking for " . a:str
12 :function! TestExists()
14 autocmd! BufEnter *.my echo 'myfile edited'
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]]
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]]
48 let test_cases += [['##FileReadCmd', 1]]
49 " Non-existing autocmd event
50 let test_cases += [['##MySpecialCmd', 0]]
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]]
59 let test_cases += [['&g:errorformat', 1]]
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]]
67 let test_cases += [['&myxyzoption', 0]]
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]]
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]]
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]]
93 " Valid user defined function
94 let test_cases += [['*TestExists', 1]]
95 " Non-existing user defined function
96 let test_cases += [['*MyxyzFunc', 0]]
100 for [test_case, result] in test_cases
101 echo test_case . ": " . result
102 call RunTest(test_case, result)
105 " Valid internal command (full match)
107 if exists(':edit') == 2
113 " Valid internal command (full match) with garbage
115 if exists(':edit/a') == 0
121 " Valid internal command (partial match)
129 " Non-existing internal command
130 echo ':invalidcmd: 0'
131 if !exists(':invalidcmd')
137 " User defined command (full match)
138 command! MyCmd :echo 'My command'
140 if exists(':MyCmd') == 2
146 " User defined command (partial match)
147 command! MyOtherCmd :echo 'Another command'
149 if exists(':My') == 3
156 echo ':rightbelow: 2'
157 if exists(':rightbelow') == 2
163 " Non-existing user defined command (full match)
173 " Non-existing user defined command (partial match)
174 delcommand MyOtherCmd
183 " Valid local variable
186 if exists('local_var')
192 " Valid local variable with garbage
194 echo 'local_var%n: 0'
195 if !exists('local_var%n')
201 " Non-existing local variable
204 if !exists('local_var')
211 let local_list = ["blue", "orange"]
213 if exists('local_list')
219 " Valid local list item
220 echo 'local_list[1]: 1'
221 if exists('local_list[1]')
227 " Valid local list item with garbage
228 echo 'local_list[1]+5: 0'
229 if !exists('local_list[1]+5')
235 " Invalid local list item
236 echo 'local_list[2]: 0'
237 if !exists('local_list[2]')
243 " Non-existing local list
246 if !exists('local_list')
252 " Valid local dictionary
253 let local_dict = {"xcord":100, "ycord":2}
255 if exists('local_dict')
261 " Non-existing local dictionary
264 if !exists('local_dict')
270 " Existing local curly-brace variable
272 let curly_{str}_var = 1
273 echo 'curly_' . str . '_var: 1'
274 if exists('curly_{str}_var')
280 " Non-existing local curly-brace variable
281 unlet curly_{str}_var
282 echo 'curly_' . str . '_var: 0'
283 if !exists('curly_{str}_var')
290 " Existing global variable
292 echo 'g:global_var: 1'
293 if exists('g:global_var')
299 " Existing global variable with garbage
300 echo 'g:global_var-n: 1'
301 if !exists('g:global_var-n')
307 " Non-existing global variable
309 echo 'g:global_var: 0'
310 if !exists('g:global_var')
316 " Existing global list
317 let g:global_list = ["blue", "orange"]
318 echo 'g:global_list: 1'
319 if exists('g:global_list')
325 " Non-existing global list
327 echo 'g:global_list: 0'
328 if !exists('g:global_list')
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')
343 " Non-existing global dictionary
345 echo 'g:global_dict: 0'
346 if !exists('g:global_dict')
352 " Existing global curly-brace variable
354 let g:curly_{str}_var = 1
355 echo 'g:curly_' . str . '_var: 1'
356 if exists('g:curly_{str}_var')
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')
371 " Existing window variable
372 echo 'w:window_var: 1'
374 if exists('w:window_var')
380 " Non-existing window variable
382 echo 'w:window_var: 0'
383 if !exists('w:window_var')
389 " Existing window list
390 let w:window_list = ["blue", "orange"]
391 echo 'w:window_list: 1'
392 if exists('w:window_list')
398 " Non-existing window list
400 echo 'w:window_list: 0'
401 if !exists('w:window_list')
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')
416 " Non-existing window dictionary
418 echo 'w:window_dict: 0'
419 if !exists('w:window_dict')
425 " Existing window curly-brace variable
427 let w:curly_{str}_var = 1
428 echo 'w:curly_' . str . '_var: 1'
429 if exists('w:curly_{str}_var')
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')
444 " Existing buffer variable
445 echo 'b:buffer_var: 1'
447 if exists('b:buffer_var')
453 " Non-existing buffer variable
455 echo 'b:buffer_var: 0'
456 if !exists('b:buffer_var')
462 " Existing buffer list
463 let b:buffer_list = ["blue", "orange"]
464 echo 'b:buffer_list: 1'
465 if exists('b:buffer_list')
471 " Non-existing buffer list
473 echo 'b:buffer_list: 0'
474 if !exists('b:buffer_list')
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')
489 " Non-existing buffer dictionary
491 echo 'b:buffer_dict: 0'
492 if !exists('b:buffer_dict')
498 " Existing buffer curly-brace variable
500 let b:curly_{str}_var = 1
501 echo 'b:curly_' . str . '_var: 1'
502 if exists('b:curly_{str}_var')
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')
520 " Existing Vim internal variable
522 if exists('v:version')
528 " Non-existing Vim internal variable
529 echo 'v:non_exists_var: 0'
530 if !exists('v:non_exists_var')
537 function TestFuncArg(func_arg, ...)
539 if exists('a:func_arg')
545 echo 'a:non_exists_arg: 0'
546 if !exists('a:non_exists_arg')
567 call TestFuncArg("arg1", "arg2")