xref: /vim-8.2.3635/src/testdir/test_exists.vim (revision ceb56ddb)
1" Tests for the exists() function
2func Test_exists()
3  augroup myagroup
4      autocmd! BufEnter       *.my     echo "myfile edited"
5      autocmd! FuncUndefined  UndefFun exec "fu UndefFun()\nendfu"
6  augroup END
7  set rtp+=./sautest
8
9  " valid autocmd group
10  call assert_equal(1, exists('#myagroup'))
11  " valid autocmd group with garbage
12  call assert_equal(0, exists('#myagroup+b'))
13  " Valid autocmd group and event
14  call assert_equal(1, exists('#myagroup#BufEnter'))
15  " Valid autocmd group, event and pattern
16  call assert_equal(1, exists('#myagroup#BufEnter#*.my'))
17  " Valid autocmd event
18  call assert_equal(1, exists('#BufEnter'))
19  " Valid autocmd event and pattern
20  call assert_equal(1, exists('#BufEnter#*.my'))
21  " Non-existing autocmd group or event
22  call assert_equal(0, exists('#xyzagroup'))
23  " Non-existing autocmd group and valid autocmd event
24  call assert_equal(0, exists('#xyzagroup#BufEnter'))
25  " Valid autocmd group and event with no matching pattern
26  call assert_equal(0, exists('#myagroup#CmdwinEnter'))
27  " Valid autocmd group and non-existing autocmd event
28  call assert_equal(0, exists('#myagroup#xyzacmd'))
29  " Valid autocmd group and event and non-matching pattern
30  call assert_equal(0, exists('#myagroup#BufEnter#xyzpat'))
31  " Valid autocmd event and non-matching pattern
32  call assert_equal(0, exists('#BufEnter#xyzpat'))
33  " Empty autocmd group, event and pattern
34  call assert_equal(0, exists('###'))
35  " Empty autocmd group and event or empty event and pattern
36  call assert_equal(0, exists('##'))
37  " Valid autocmd event
38  call assert_equal(1, exists('##FileReadCmd'))
39  " Non-existing autocmd event
40  call assert_equal(0, exists('##MySpecialCmd'))
41
42  " Existing and working option (long form)
43  call assert_equal(1, exists('&textwidth'))
44  " Existing and working option (short form)
45  call assert_equal(1, exists('&tw'))
46  " Existing and working option with garbage
47  call assert_equal(0, exists('&tw-'))
48  " Global option
49  call assert_equal(1, exists('&g:errorformat'))
50  " Local option
51  call assert_equal(1, exists('&l:errorformat'))
52  " Negative form of existing and working option (long form)
53  call assert_equal(0, exists('&nojoinspaces'))
54  " Negative form of existing and working option (short form)
55  call assert_equal(0, exists('&nojs'))
56  " Non-existing option
57  call assert_equal(0, exists('&myxyzoption'))
58
59  " Existing and working option (long form)
60  call assert_equal(1, exists('+incsearch'))
61  " Existing and working option with garbage
62  call assert_equal(0, exists('+incsearch!1'))
63  " Existing and working option (short form)
64  call assert_equal(1, exists('+is'))
65  " Existing option that is hidden.
66  call assert_equal(0, exists('+autoprint'))
67
68  " Existing environment variable
69  let $EDITOR_NAME = 'Vim Editor'
70  call assert_equal(1, exists('$EDITOR_NAME'))
71  if has('unix')
72    " ${name} environment variables are supported only on Unix-like systems
73    call assert_equal(1, exists('${VIM}'))
74  endif
75  " Non-existing environment variable
76  call assert_equal(0, exists('$NON_ENV_VAR'))
77
78  " Valid internal function
79  call assert_equal(1, exists('*bufnr'))
80  " Valid internal function with ()
81  call assert_equal(1, exists('*bufnr()'))
82  " Non-existing internal function
83  call assert_equal(0, exists('*myxyzfunc'))
84  " Valid internal function with garbage
85  call assert_equal(0, exists('*bufnr&6'))
86  " Valid user defined function
87  call assert_equal(1, exists('*Test_exists'))
88  " Non-existing user defined function
89  call assert_equal(0, exists('*MyxyzFunc'))
90  " Function that may be created by FuncUndefined event
91  call assert_equal(0, exists('*UndefFun'))
92  " Function that may be created by script autoloading
93  call assert_equal(0, exists('*footest#F'))
94
95  call assert_equal(has('float'), exists('*acos'))
96  call assert_equal(1, exists('?acos'))
97  call assert_equal(has('win32'), exists('*debugbreak'))
98  call assert_equal(1, exists('?debugbreak'))
99
100  " Valid internal command (full match)
101  call assert_equal(2, exists(':edit'))
102  " Valid internal command (full match) with garbage
103  call assert_equal(0, exists(':edit/a'))
104  " Valid internal command (partial match)
105  call assert_equal(1, exists(':q'))
106  " Valid internal command with a digit
107  call assert_equal(2, exists(':2match'))
108  " Non-existing internal command
109  call assert_equal(0, exists(':invalidcmd'))
110  " Internal command with a count
111  call assert_equal(0, exists(':3buffer'))
112
113  " User defined command (full match)
114  command! MyCmd :echo 'My command'
115  call assert_equal(2, exists(':MyCmd'))
116  " User defined command (partial match)
117  command! MyOtherCmd :echo 'Another command'
118  call assert_equal(3, exists(':My'))
119
120  " Command modifier
121  call assert_equal(2, exists(':rightbelow'))
122
123  " Non-existing user defined command (full match)
124  delcommand MyCmd
125  call assert_equal(0, exists(':MyCmd'))
126
127  " Non-existing user defined command (partial match)
128  delcommand MyOtherCmd
129  call assert_equal(0, exists(':My'))
130
131  " Valid local variable
132  let local_var = 1
133  call assert_equal(1, exists('local_var'))
134  " Valid local variable with garbage
135  call assert_equal(0, exists('local_var%n'))
136  " Non-existing local variable
137  unlet local_var
138  call assert_equal(0, exists('local_var'))
139
140  " Non-existing autoload variable that may be autoloaded
141  call assert_equal(0, exists('footest#x'))
142
143  " Valid local list
144  let local_list = ["blue", "orange"]
145  call assert_equal(1, exists('local_list'))
146  " Valid local list item
147  call assert_equal(1, exists('local_list[1]'))
148  " Valid local list item with garbage
149  call assert_equal(0, exists('local_list[1]+5'))
150  " Invalid local list item
151  call assert_equal(0, exists('local_list[2]'))
152  " Non-existing local list
153  unlet local_list
154  call assert_equal(0, exists('local_list'))
155  " Valid local dictionary
156  let local_dict = {"xcord":100, "ycord":2}
157  call assert_equal(1, exists('local_dict'))
158  " Non-existing local dictionary
159  unlet local_dict
160  call assert_equal(0, exists('local_dict'))
161  " Existing local curly-brace variable
162  let str = "local"
163  let curly_{str}_var = 1
164  call assert_equal(1, exists('curly_{str}_var'))
165  " Non-existing local curly-brace variable
166  unlet curly_{str}_var
167  call assert_equal(0, exists('curly_{str}_var'))
168
169  " Existing global variable
170  let g:global_var = 1
171  call assert_equal(1, exists('g:global_var'))
172  " Existing global variable with garbage
173  call assert_equal(0, exists('g:global_var-n'))
174  " Non-existing global variable
175  unlet g:global_var
176  call assert_equal(0, exists('g:global_var'))
177  " Existing global list
178  let g:global_list = ["blue", "orange"]
179  call assert_equal(1, exists('g:global_list'))
180  " Non-existing global list
181  unlet g:global_list
182  call assert_equal(0, exists('g:global_list'))
183  " Existing global dictionary
184  let g:global_dict = {"xcord":100, "ycord":2}
185  call assert_equal(1, exists('g:global_dict'))
186  " Non-existing global dictionary
187  unlet g:global_dict
188  call assert_equal(0, exists('g:global_dict'))
189  " Existing global curly-brace variable
190  let str = "global"
191  let g:curly_{str}_var = 1
192  call assert_equal(1, exists('g:curly_{str}_var'))
193  " Non-existing global curly-brace variable
194  unlet g:curly_{str}_var
195  call assert_equal(0, exists('g:curly_{str}_var'))
196
197  " Existing window variable
198  let w:window_var = 1
199  call assert_equal(1, exists('w:window_var'))
200  " Non-existing window variable
201  unlet w:window_var
202  call assert_equal(0, exists('w:window_var'))
203  " Existing window list
204  let w:window_list = ["blue", "orange"]
205  call assert_equal(1, exists('w:window_list'))
206  " Non-existing window list
207  unlet w:window_list
208  call assert_equal(0, exists('w:window_list'))
209  " Existing window dictionary
210  let w:window_dict = {"xcord":100, "ycord":2}
211  call assert_equal(1, exists('w:window_dict'))
212  " Non-existing window dictionary
213  unlet w:window_dict
214  call assert_equal(0, exists('w:window_dict'))
215  " Existing window curly-brace variable
216  let str = "window"
217  let w:curly_{str}_var = 1
218  call assert_equal(1, exists('w:curly_{str}_var'))
219  " Non-existing window curly-brace variable
220  unlet w:curly_{str}_var
221  call assert_equal(0, exists('w:curly_{str}_var'))
222
223  " Existing tab variable
224  let t:tab_var = 1
225  call assert_equal(1, exists('t:tab_var'))
226  " Non-existing tab variable
227  unlet t:tab_var
228  call assert_equal(0, exists('t:tab_var'))
229  " Existing tab list
230  let t:tab_list = ["blue", "orange"]
231  call assert_equal(1, exists('t:tab_list'))
232  " Non-existing tab list
233  unlet t:tab_list
234  call assert_equal(0, exists('t:tab_list'))
235  " Existing tab dictionary
236  let t:tab_dict = {"xcord":100, "ycord":2}
237  call assert_equal(1, exists('t:tab_dict'))
238  " Non-existing tab dictionary
239  unlet t:tab_dict
240  call assert_equal(0, exists('t:tab_dict'))
241  " Existing tab curly-brace variable
242  let str = "tab"
243  let t:curly_{str}_var = 1
244  call assert_equal(1, exists('t:curly_{str}_var'))
245  " Non-existing tab curly-brace variable
246  unlet t:curly_{str}_var
247  call assert_equal(0, exists('t:curly_{str}_var'))
248
249  " Existing buffer variable
250  let b:buffer_var = 1
251  call assert_equal(1, exists('b:buffer_var'))
252  " Non-existing buffer variable
253  unlet b:buffer_var
254  call assert_equal(0, exists('b:buffer_var'))
255  " Existing buffer list
256  let b:buffer_list = ["blue", "orange"]
257  call assert_equal(1, exists('b:buffer_list'))
258  " Non-existing buffer list
259  unlet b:buffer_list
260  call assert_equal(0, exists('b:buffer_list'))
261  " Existing buffer dictionary
262  let b:buffer_dict = {"xcord":100, "ycord":2}
263  call assert_equal(1, exists('b:buffer_dict'))
264  " Non-existing buffer dictionary
265  unlet b:buffer_dict
266  call assert_equal(0, exists('b:buffer_dict'))
267  " Existing buffer curly-brace variable
268  let str = "buffer"
269  let b:curly_{str}_var = 1
270  call assert_equal(1, exists('b:curly_{str}_var'))
271  " Non-existing buffer curly-brace variable
272  unlet b:curly_{str}_var
273  call assert_equal(0, exists('b:curly_{str}_var'))
274
275  " Existing Vim internal variable
276  call assert_equal(1, exists('v:version'))
277  " Non-existing Vim internal variable
278  call assert_equal(0, exists('v:non_exists_var'))
279
280  " Existing script-local variable
281  let s:script_var = 1
282  call assert_equal(1, exists('s:script_var'))
283  " Non-existing script-local variable
284  unlet s:script_var
285  call assert_equal(0, exists('s:script_var'))
286  " Existing script-local list
287  let s:script_list = ["blue", "orange"]
288  call assert_equal(1, exists('s:script_list'))
289  " Non-existing script-local list
290  unlet s:script_list
291  call assert_equal(0, exists('s:script_list'))
292  " Existing script-local dictionary
293  let s:script_dict = {"xcord":100, "ycord":2}
294  call assert_equal(1, exists('s:script_dict'))
295  " Non-existing script-local dictionary
296  unlet s:script_dict
297  call assert_equal(0, exists('s:script_dict'))
298  " Existing script curly-brace variable
299  let str = "script"
300  let s:curly_{str}_var = 1
301  call assert_equal(1, exists('s:curly_{str}_var'))
302  " Non-existing script-local curly-brace variable
303  unlet s:curly_{str}_var
304  call assert_equal(0, exists('s:curly_{str}_var'))
305
306  " Existing script-local function
307  function! s:my_script_func()
308  endfunction
309
310  echo '*s:my_script_func: 1'
311  call assert_equal(1, exists('*s:my_script_func'))
312
313  " Non-existing script-local function
314  delfunction s:my_script_func
315
316  call assert_equal(0, exists('*s:my_script_func'))
317  unlet str
318
319  call assert_equal(1, g:footest#x)
320  call assert_equal(0, footest#F())
321  call assert_equal(0, UndefFun())
322endfunc
323
324" exists() test for Function arguments
325func FuncArg_Tests(func_arg, ...)
326  call assert_equal(1, exists('a:func_arg'))
327  call assert_equal(0, exists('a:non_exists_arg'))
328  call assert_equal(1, exists('a:1'))
329  call assert_equal(0, exists('a:2'))
330endfunc
331
332func Test_exists_funcarg()
333  call FuncArg_Tests("arg1", "arg2")
334endfunc
335
336" vim: shiftwidth=2 sts=2 expandtab
337