blob: 62c66192ef6c6a2e4a197d4bee9a2c4b13313ff4 [file] [log] [blame]
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001" 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'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010071 if has('unix')
72 " ${name} environment variables are supported only on Unix-like systems
73 call assert_equal(1, exists('${VIM}'))
74 endif
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +020075 " 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 " Valid internal command (full match)
96 call assert_equal(2, exists(':edit'))
97 " Valid internal command (full match) with garbage
98 call assert_equal(0, exists(':edit/a'))
99 " Valid internal command (partial match)
100 call assert_equal(1, exists(':q'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100101 " Valid internal command with a digit
102 call assert_equal(2, exists(':2match'))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200103 " Non-existing internal command
104 call assert_equal(0, exists(':invalidcmd'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100105 " Internal command with a count
106 call assert_equal(0, exists(':3buffer'))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200107
108 " User defined command (full match)
109 command! MyCmd :echo 'My command'
110 call assert_equal(2, exists(':MyCmd'))
111 " User defined command (partial match)
112 command! MyOtherCmd :echo 'Another command'
113 call assert_equal(3, exists(':My'))
114
115 " Command modifier
116 call assert_equal(2, exists(':rightbelow'))
117
118 " Non-existing user defined command (full match)
119 delcommand MyCmd
120 call assert_equal(0, exists(':MyCmd'))
121
122 " Non-existing user defined command (partial match)
123 delcommand MyOtherCmd
124 call assert_equal(0, exists(':My'))
125
126 " Valid local variable
127 let local_var = 1
128 call assert_equal(1, exists('local_var'))
129 " Valid local variable with garbage
130 call assert_equal(0, exists('local_var%n'))
131 " Non-existing local variable
132 unlet local_var
133 call assert_equal(0, exists('local_var'))
134
135 " Non-existing autoload variable that may be autoloaded
136 call assert_equal(0, exists('footest#x'))
137
138 " Valid local list
139 let local_list = ["blue", "orange"]
140 call assert_equal(1, exists('local_list'))
141 " Valid local list item
142 call assert_equal(1, exists('local_list[1]'))
143 " Valid local list item with garbage
144 call assert_equal(0, exists('local_list[1]+5'))
145 " Invalid local list item
146 call assert_equal(0, exists('local_list[2]'))
147 " Non-existing local list
148 unlet local_list
149 call assert_equal(0, exists('local_list'))
150 " Valid local dictionary
151 let local_dict = {"xcord":100, "ycord":2}
152 call assert_equal(1, exists('local_dict'))
153 " Non-existing local dictionary
154 unlet local_dict
155 call assert_equal(0, exists('local_dict'))
156 " Existing local curly-brace variable
157 let str = "local"
158 let curly_{str}_var = 1
159 call assert_equal(1, exists('curly_{str}_var'))
160 " Non-existing local curly-brace variable
161 unlet curly_{str}_var
162 call assert_equal(0, exists('curly_{str}_var'))
163
164 " Existing global variable
165 let g:global_var = 1
166 call assert_equal(1, exists('g:global_var'))
167 " Existing global variable with garbage
168 call assert_equal(0, exists('g:global_var-n'))
169 " Non-existing global variable
170 unlet g:global_var
171 call assert_equal(0, exists('g:global_var'))
172 " Existing global list
173 let g:global_list = ["blue", "orange"]
174 call assert_equal(1, exists('g:global_list'))
175 " Non-existing global list
176 unlet g:global_list
177 call assert_equal(0, exists('g:global_list'))
178 " Existing global dictionary
179 let g:global_dict = {"xcord":100, "ycord":2}
180 call assert_equal(1, exists('g:global_dict'))
181 " Non-existing global dictionary
182 unlet g:global_dict
183 call assert_equal(0, exists('g:global_dict'))
184 " Existing global curly-brace variable
185 let str = "global"
186 let g:curly_{str}_var = 1
187 call assert_equal(1, exists('g:curly_{str}_var'))
188 " Non-existing global curly-brace variable
189 unlet g:curly_{str}_var
190 call assert_equal(0, exists('g:curly_{str}_var'))
191
192 " Existing window variable
193 let w:window_var = 1
194 call assert_equal(1, exists('w:window_var'))
195 " Non-existing window variable
196 unlet w:window_var
197 call assert_equal(0, exists('w:window_var'))
198 " Existing window list
199 let w:window_list = ["blue", "orange"]
200 call assert_equal(1, exists('w:window_list'))
201 " Non-existing window list
202 unlet w:window_list
203 call assert_equal(0, exists('w:window_list'))
204 " Existing window dictionary
205 let w:window_dict = {"xcord":100, "ycord":2}
206 call assert_equal(1, exists('w:window_dict'))
207 " Non-existing window dictionary
208 unlet w:window_dict
209 call assert_equal(0, exists('w:window_dict'))
210 " Existing window curly-brace variable
211 let str = "window"
212 let w:curly_{str}_var = 1
213 call assert_equal(1, exists('w:curly_{str}_var'))
214 " Non-existing window curly-brace variable
215 unlet w:curly_{str}_var
216 call assert_equal(0, exists('w:curly_{str}_var'))
217
218 " Existing tab variable
219 let t:tab_var = 1
220 call assert_equal(1, exists('t:tab_var'))
221 " Non-existing tab variable
222 unlet t:tab_var
223 call assert_equal(0, exists('t:tab_var'))
224 " Existing tab list
225 let t:tab_list = ["blue", "orange"]
226 call assert_equal(1, exists('t:tab_list'))
227 " Non-existing tab list
228 unlet t:tab_list
229 call assert_equal(0, exists('t:tab_list'))
230 " Existing tab dictionary
231 let t:tab_dict = {"xcord":100, "ycord":2}
232 call assert_equal(1, exists('t:tab_dict'))
233 " Non-existing tab dictionary
234 unlet t:tab_dict
235 call assert_equal(0, exists('t:tab_dict'))
236 " Existing tab curly-brace variable
237 let str = "tab"
238 let t:curly_{str}_var = 1
239 call assert_equal(1, exists('t:curly_{str}_var'))
240 " Non-existing tab curly-brace variable
241 unlet t:curly_{str}_var
242 call assert_equal(0, exists('t:curly_{str}_var'))
243
244 " Existing buffer variable
245 let b:buffer_var = 1
246 call assert_equal(1, exists('b:buffer_var'))
247 " Non-existing buffer variable
248 unlet b:buffer_var
249 call assert_equal(0, exists('b:buffer_var'))
250 " Existing buffer list
251 let b:buffer_list = ["blue", "orange"]
252 call assert_equal(1, exists('b:buffer_list'))
253 " Non-existing buffer list
254 unlet b:buffer_list
255 call assert_equal(0, exists('b:buffer_list'))
256 " Existing buffer dictionary
257 let b:buffer_dict = {"xcord":100, "ycord":2}
258 call assert_equal(1, exists('b:buffer_dict'))
259 " Non-existing buffer dictionary
260 unlet b:buffer_dict
261 call assert_equal(0, exists('b:buffer_dict'))
262 " Existing buffer curly-brace variable
263 let str = "buffer"
264 let b:curly_{str}_var = 1
265 call assert_equal(1, exists('b:curly_{str}_var'))
266 " Non-existing buffer curly-brace variable
267 unlet b:curly_{str}_var
268 call assert_equal(0, exists('b:curly_{str}_var'))
269
270 " Existing Vim internal variable
271 call assert_equal(1, exists('v:version'))
272 " Non-existing Vim internal variable
273 call assert_equal(0, exists('v:non_exists_var'))
274
275 " Existing script-local variable
276 let s:script_var = 1
277 call assert_equal(1, exists('s:script_var'))
278 " Non-existing script-local variable
279 unlet s:script_var
280 call assert_equal(0, exists('s:script_var'))
281 " Existing script-local list
282 let s:script_list = ["blue", "orange"]
283 call assert_equal(1, exists('s:script_list'))
284 " Non-existing script-local list
285 unlet s:script_list
286 call assert_equal(0, exists('s:script_list'))
287 " Existing script-local dictionary
288 let s:script_dict = {"xcord":100, "ycord":2}
289 call assert_equal(1, exists('s:script_dict'))
290 " Non-existing script-local dictionary
291 unlet s:script_dict
292 call assert_equal(0, exists('s:script_dict'))
293 " Existing script curly-brace variable
294 let str = "script"
295 let s:curly_{str}_var = 1
296 call assert_equal(1, exists('s:curly_{str}_var'))
297 " Non-existing script-local curly-brace variable
298 unlet s:curly_{str}_var
299 call assert_equal(0, exists('s:curly_{str}_var'))
300
301 " Existing script-local function
302 function! s:my_script_func()
303 endfunction
304
305 echo '*s:my_script_func: 1'
306 call assert_equal(1, exists('*s:my_script_func'))
307
308 " Non-existing script-local function
309 delfunction s:my_script_func
310
311 call assert_equal(0, exists('*s:my_script_func'))
312 unlet str
313
314 call assert_equal(1, g:footest#x)
315 call assert_equal(0, footest#F())
316 call assert_equal(0, UndefFun())
317endfunc
318
319" exists() test for Function arguments
320func FuncArg_Tests(func_arg, ...)
321 call assert_equal(1, exists('a:func_arg'))
322 call assert_equal(0, exists('a:non_exists_arg'))
323 call assert_equal(1, exists('a:1'))
324 call assert_equal(0, exists('a:2'))
325endfunc
326
327func Test_exists_funcarg()
328 call FuncArg_Tests("arg1", "arg2")
329endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100330
331" vim: shiftwidth=2 sts=2 expandtab