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