blob: 404b15c0d378402f38be50fe4b531f461bafcad4 [file] [log] [blame]
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001" Tests for the exists() function
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002
Yegappan Lakshmanana2ebb6e2024-02-25 08:40:10 +01003import './vim9.vim' as v9
4
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02005func Test_exists()
6 augroup myagroup
7 autocmd! BufEnter *.my echo "myfile edited"
8 autocmd! FuncUndefined UndefFun exec "fu UndefFun()\nendfu"
9 augroup END
10 set rtp+=./sautest
11
12 " valid autocmd group
13 call assert_equal(1, exists('#myagroup'))
14 " valid autocmd group with garbage
15 call assert_equal(0, exists('#myagroup+b'))
16 " Valid autocmd group and event
17 call assert_equal(1, exists('#myagroup#BufEnter'))
18 " Valid autocmd group, event and pattern
19 call assert_equal(1, exists('#myagroup#BufEnter#*.my'))
20 " Valid autocmd event
21 call assert_equal(1, exists('#BufEnter'))
22 " Valid autocmd event and pattern
23 call assert_equal(1, exists('#BufEnter#*.my'))
24 " Non-existing autocmd group or event
25 call assert_equal(0, exists('#xyzagroup'))
26 " Non-existing autocmd group and valid autocmd event
27 call assert_equal(0, exists('#xyzagroup#BufEnter'))
28 " Valid autocmd group and event with no matching pattern
29 call assert_equal(0, exists('#myagroup#CmdwinEnter'))
30 " Valid autocmd group and non-existing autocmd event
31 call assert_equal(0, exists('#myagroup#xyzacmd'))
32 " Valid autocmd group and event and non-matching pattern
33 call assert_equal(0, exists('#myagroup#BufEnter#xyzpat'))
34 " Valid autocmd event and non-matching pattern
35 call assert_equal(0, exists('#BufEnter#xyzpat'))
36 " Empty autocmd group, event and pattern
37 call assert_equal(0, exists('###'))
38 " Empty autocmd group and event or empty event and pattern
39 call assert_equal(0, exists('##'))
40 " Valid autocmd event
41 call assert_equal(1, exists('##FileReadCmd'))
42 " Non-existing autocmd event
43 call assert_equal(0, exists('##MySpecialCmd'))
44
45 " Existing and working option (long form)
46 call assert_equal(1, exists('&textwidth'))
47 " Existing and working option (short form)
48 call assert_equal(1, exists('&tw'))
49 " Existing and working option with garbage
50 call assert_equal(0, exists('&tw-'))
51 " Global option
52 call assert_equal(1, exists('&g:errorformat'))
53 " Local option
54 call assert_equal(1, exists('&l:errorformat'))
55 " Negative form of existing and working option (long form)
56 call assert_equal(0, exists('&nojoinspaces'))
57 " Negative form of existing and working option (short form)
58 call assert_equal(0, exists('&nojs'))
59 " Non-existing option
60 call assert_equal(0, exists('&myxyzoption'))
61
62 " Existing and working option (long form)
63 call assert_equal(1, exists('+incsearch'))
64 " Existing and working option with garbage
65 call assert_equal(0, exists('+incsearch!1'))
66 " Existing and working option (short form)
67 call assert_equal(1, exists('+is'))
68 " Existing option that is hidden.
69 call assert_equal(0, exists('+autoprint'))
70
71 " Existing environment variable
72 let $EDITOR_NAME = 'Vim Editor'
73 call assert_equal(1, exists('$EDITOR_NAME'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010074 if has('unix')
75 " ${name} environment variables are supported only on Unix-like systems
76 call assert_equal(1, exists('${VIM}'))
77 endif
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +020078 " Non-existing environment variable
79 call assert_equal(0, exists('$NON_ENV_VAR'))
80
81 " Valid internal function
82 call assert_equal(1, exists('*bufnr'))
83 " Valid internal function with ()
84 call assert_equal(1, exists('*bufnr()'))
85 " Non-existing internal function
86 call assert_equal(0, exists('*myxyzfunc'))
87 " Valid internal function with garbage
88 call assert_equal(0, exists('*bufnr&6'))
89 " Valid user defined function
90 call assert_equal(1, exists('*Test_exists'))
91 " Non-existing user defined function
92 call assert_equal(0, exists('*MyxyzFunc'))
93 " Function that may be created by FuncUndefined event
94 call assert_equal(0, exists('*UndefFun'))
95 " Function that may be created by script autoloading
96 call assert_equal(0, exists('*footest#F'))
97
Bram Moolenaar15c47602020-03-26 22:16:48 +010098 call assert_equal(has('float'), exists('*acos'))
99 call assert_equal(1, exists('?acos'))
100 call assert_equal(has('win32'), exists('*debugbreak'))
101 call assert_equal(1, exists('?debugbreak'))
102
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200103 " Valid internal command (full match)
104 call assert_equal(2, exists(':edit'))
105 " Valid internal command (full match) with garbage
106 call assert_equal(0, exists(':edit/a'))
107 " Valid internal command (partial match)
108 call assert_equal(1, exists(':q'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100109 " Valid internal command with a digit
110 call assert_equal(2, exists(':2match'))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200111 " Non-existing internal command
112 call assert_equal(0, exists(':invalidcmd'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100113 " Internal command with a count
114 call assert_equal(0, exists(':3buffer'))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200115
Doug Kearnsea842022024-09-29 17:17:41 +0200116 " Valid internal command (full match)
117 call assert_equal(2, exists(':k'))
118 " Non-existing internal command (':k' with arg 'e')
119 call assert_equal(0, exists(':ke'))
120 " Valid internal command (partial match)
121 call assert_equal(1, exists(':kee'))
122
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200123 " User defined command (full match)
124 command! MyCmd :echo 'My command'
125 call assert_equal(2, exists(':MyCmd'))
126 " User defined command (partial match)
127 command! MyOtherCmd :echo 'Another command'
128 call assert_equal(3, exists(':My'))
129
130 " Command modifier
131 call assert_equal(2, exists(':rightbelow'))
132
133 " Non-existing user defined command (full match)
134 delcommand MyCmd
135 call assert_equal(0, exists(':MyCmd'))
136
137 " Non-existing user defined command (partial match)
138 delcommand MyOtherCmd
139 call assert_equal(0, exists(':My'))
140
141 " Valid local variable
142 let local_var = 1
143 call assert_equal(1, exists('local_var'))
144 " Valid local variable with garbage
145 call assert_equal(0, exists('local_var%n'))
146 " Non-existing local variable
147 unlet local_var
148 call assert_equal(0, exists('local_var'))
149
150 " Non-existing autoload variable that may be autoloaded
151 call assert_equal(0, exists('footest#x'))
152
153 " Valid local list
154 let local_list = ["blue", "orange"]
155 call assert_equal(1, exists('local_list'))
156 " Valid local list item
157 call assert_equal(1, exists('local_list[1]'))
158 " Valid local list item with garbage
159 call assert_equal(0, exists('local_list[1]+5'))
160 " Invalid local list item
161 call assert_equal(0, exists('local_list[2]'))
162 " Non-existing local list
163 unlet local_list
164 call assert_equal(0, exists('local_list'))
165 " Valid local dictionary
166 let local_dict = {"xcord":100, "ycord":2}
167 call assert_equal(1, exists('local_dict'))
168 " Non-existing local dictionary
169 unlet local_dict
170 call assert_equal(0, exists('local_dict'))
171 " Existing local curly-brace variable
172 let str = "local"
173 let curly_{str}_var = 1
174 call assert_equal(1, exists('curly_{str}_var'))
175 " Non-existing local curly-brace variable
176 unlet curly_{str}_var
177 call assert_equal(0, exists('curly_{str}_var'))
178
179 " Existing global variable
180 let g:global_var = 1
181 call assert_equal(1, exists('g:global_var'))
182 " Existing global variable with garbage
183 call assert_equal(0, exists('g:global_var-n'))
184 " Non-existing global variable
185 unlet g:global_var
186 call assert_equal(0, exists('g:global_var'))
187 " Existing global list
188 let g:global_list = ["blue", "orange"]
189 call assert_equal(1, exists('g:global_list'))
190 " Non-existing global list
191 unlet g:global_list
192 call assert_equal(0, exists('g:global_list'))
193 " Existing global dictionary
194 let g:global_dict = {"xcord":100, "ycord":2}
195 call assert_equal(1, exists('g:global_dict'))
196 " Non-existing global dictionary
197 unlet g:global_dict
198 call assert_equal(0, exists('g:global_dict'))
199 " Existing global curly-brace variable
200 let str = "global"
201 let g:curly_{str}_var = 1
202 call assert_equal(1, exists('g:curly_{str}_var'))
203 " Non-existing global curly-brace variable
204 unlet g:curly_{str}_var
205 call assert_equal(0, exists('g:curly_{str}_var'))
206
207 " Existing window variable
208 let w:window_var = 1
209 call assert_equal(1, exists('w:window_var'))
210 " Non-existing window variable
211 unlet w:window_var
212 call assert_equal(0, exists('w:window_var'))
213 " Existing window list
214 let w:window_list = ["blue", "orange"]
215 call assert_equal(1, exists('w:window_list'))
216 " Non-existing window list
217 unlet w:window_list
218 call assert_equal(0, exists('w:window_list'))
219 " Existing window dictionary
220 let w:window_dict = {"xcord":100, "ycord":2}
221 call assert_equal(1, exists('w:window_dict'))
222 " Non-existing window dictionary
223 unlet w:window_dict
224 call assert_equal(0, exists('w:window_dict'))
225 " Existing window curly-brace variable
226 let str = "window"
227 let w:curly_{str}_var = 1
228 call assert_equal(1, exists('w:curly_{str}_var'))
229 " Non-existing window curly-brace variable
230 unlet w:curly_{str}_var
231 call assert_equal(0, exists('w:curly_{str}_var'))
232
233 " Existing tab variable
234 let t:tab_var = 1
235 call assert_equal(1, exists('t:tab_var'))
236 " Non-existing tab variable
237 unlet t:tab_var
238 call assert_equal(0, exists('t:tab_var'))
239 " Existing tab list
240 let t:tab_list = ["blue", "orange"]
241 call assert_equal(1, exists('t:tab_list'))
242 " Non-existing tab list
243 unlet t:tab_list
244 call assert_equal(0, exists('t:tab_list'))
245 " Existing tab dictionary
246 let t:tab_dict = {"xcord":100, "ycord":2}
247 call assert_equal(1, exists('t:tab_dict'))
248 " Non-existing tab dictionary
249 unlet t:tab_dict
250 call assert_equal(0, exists('t:tab_dict'))
251 " Existing tab curly-brace variable
252 let str = "tab"
253 let t:curly_{str}_var = 1
254 call assert_equal(1, exists('t:curly_{str}_var'))
255 " Non-existing tab curly-brace variable
256 unlet t:curly_{str}_var
257 call assert_equal(0, exists('t:curly_{str}_var'))
258
259 " Existing buffer variable
260 let b:buffer_var = 1
261 call assert_equal(1, exists('b:buffer_var'))
262 " Non-existing buffer variable
263 unlet b:buffer_var
264 call assert_equal(0, exists('b:buffer_var'))
265 " Existing buffer list
266 let b:buffer_list = ["blue", "orange"]
267 call assert_equal(1, exists('b:buffer_list'))
268 " Non-existing buffer list
269 unlet b:buffer_list
270 call assert_equal(0, exists('b:buffer_list'))
271 " Existing buffer dictionary
272 let b:buffer_dict = {"xcord":100, "ycord":2}
273 call assert_equal(1, exists('b:buffer_dict'))
274 " Non-existing buffer dictionary
275 unlet b:buffer_dict
276 call assert_equal(0, exists('b:buffer_dict'))
277 " Existing buffer curly-brace variable
278 let str = "buffer"
279 let b:curly_{str}_var = 1
280 call assert_equal(1, exists('b:curly_{str}_var'))
281 " Non-existing buffer curly-brace variable
282 unlet b:curly_{str}_var
283 call assert_equal(0, exists('b:curly_{str}_var'))
284
285 " Existing Vim internal variable
286 call assert_equal(1, exists('v:version'))
287 " Non-existing Vim internal variable
288 call assert_equal(0, exists('v:non_exists_var'))
289
290 " Existing script-local variable
291 let s:script_var = 1
292 call assert_equal(1, exists('s:script_var'))
293 " Non-existing script-local variable
294 unlet s:script_var
295 call assert_equal(0, exists('s:script_var'))
296 " Existing script-local list
297 let s:script_list = ["blue", "orange"]
298 call assert_equal(1, exists('s:script_list'))
299 " Non-existing script-local list
300 unlet s:script_list
301 call assert_equal(0, exists('s:script_list'))
302 " Existing script-local dictionary
303 let s:script_dict = {"xcord":100, "ycord":2}
304 call assert_equal(1, exists('s:script_dict'))
305 " Non-existing script-local dictionary
306 unlet s:script_dict
307 call assert_equal(0, exists('s:script_dict'))
308 " Existing script curly-brace variable
309 let str = "script"
310 let s:curly_{str}_var = 1
311 call assert_equal(1, exists('s:curly_{str}_var'))
312 " Non-existing script-local curly-brace variable
313 unlet s:curly_{str}_var
314 call assert_equal(0, exists('s:curly_{str}_var'))
315
316 " Existing script-local function
317 function! s:my_script_func()
318 endfunction
319
320 echo '*s:my_script_func: 1'
321 call assert_equal(1, exists('*s:my_script_func'))
322
323 " Non-existing script-local function
324 delfunction s:my_script_func
325
326 call assert_equal(0, exists('*s:my_script_func'))
327 unlet str
328
329 call assert_equal(1, g:footest#x)
330 call assert_equal(0, footest#F())
331 call assert_equal(0, UndefFun())
332endfunc
333
334" exists() test for Function arguments
335func FuncArg_Tests(func_arg, ...)
336 call assert_equal(1, exists('a:func_arg'))
337 call assert_equal(0, exists('a:non_exists_arg'))
338 call assert_equal(1, exists('a:1'))
339 call assert_equal(0, exists('a:2'))
340endfunc
341
342func Test_exists_funcarg()
343 call FuncArg_Tests("arg1", "arg2")
344endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100345
Yegappan Lakshmanana2ebb6e2024-02-25 08:40:10 +0100346" Test for using exists() with class and object variables and methods.
347func Test_exists_class_object()
348 let lines =<< trim END
349 vim9script
350 class A
351 var var1: number = 10
352 static var var2: number = 10
353 static def Foo()
354 enddef
355 def Bar()
356 enddef
357 endclass
358
359 assert_equal(1, exists("A"))
360 var a = A.new()
361 assert_equal(1, exists("a"))
362
363 assert_equal(1, exists("a.var1"))
364 assert_fails('exists("a.var2")', 'E1375: Class variable "var2" accessible only using class "A"')
365 assert_fails('exists("a.var3")', 'E1326: Variable "var3" not found in object "A"')
366 assert_equal(1, exists("A.var2"))
367 assert_fails('exists("A.var1")', 'E1376: Object variable "var1" accessible only using class "A" object')
368 assert_fails('exists("A.var3")', 'E1337: Class variable "var3" not found in class "A"')
369
370 assert_equal(1, exists("a.Bar"))
371 assert_fails('exists("a.Barz")', 'E1326: Variable "Barz" not found in object "A"')
372 assert_fails('exists("a.Foo")', 'E1326: Variable "Foo" not found in object "A"')
373 assert_equal(1, exists("A.Foo"))
374 assert_fails('exists("A.Bar")', 'E1337: Class variable "Bar" not found in class "A"')
375 assert_fails('exists("A.Barz")', 'E1337: Class variable "Barz" not found in class "A"')
376
377 def Baz()
378 assert_equal(1, exists("A"))
379 var aa = A.new()
380 assert_equal(1, exists("A.var2"))
381 assert_fails('exists("A.var1")', 'E1376: Object variable "var1" accessible only using class "A" object')
382 assert_fails('exists("A.var3")', 'E1337: Class variable "var3" not found in class "A"')
383
384 assert_equal(1, exists("A.Foo"))
385 assert_fails('exists("A.Bar")', 'E1337: Class variable "Bar" not found in class "A"')
386 assert_fails('exists("A.Barz")', 'E1337: Class variable "Barz" not found in class "A"')
387 enddef
388 Baz()
389 END
390 call v9.CheckSourceSuccess(lines)
391endfunc
392
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100393" vim: shiftwidth=2 sts=2 expandtab