blob: 9cdb3fb5c0b330b8c3019aab3ff69a25920aeb44 [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
116 " User defined command (full match)
117 command! MyCmd :echo 'My command'
118 call assert_equal(2, exists(':MyCmd'))
119 " User defined command (partial match)
120 command! MyOtherCmd :echo 'Another command'
121 call assert_equal(3, exists(':My'))
122
123 " Command modifier
124 call assert_equal(2, exists(':rightbelow'))
125
126 " Non-existing user defined command (full match)
127 delcommand MyCmd
128 call assert_equal(0, exists(':MyCmd'))
129
130 " Non-existing user defined command (partial match)
131 delcommand MyOtherCmd
132 call assert_equal(0, exists(':My'))
133
134 " Valid local variable
135 let local_var = 1
136 call assert_equal(1, exists('local_var'))
137 " Valid local variable with garbage
138 call assert_equal(0, exists('local_var%n'))
139 " Non-existing local variable
140 unlet local_var
141 call assert_equal(0, exists('local_var'))
142
143 " Non-existing autoload variable that may be autoloaded
144 call assert_equal(0, exists('footest#x'))
145
146 " Valid local list
147 let local_list = ["blue", "orange"]
148 call assert_equal(1, exists('local_list'))
149 " Valid local list item
150 call assert_equal(1, exists('local_list[1]'))
151 " Valid local list item with garbage
152 call assert_equal(0, exists('local_list[1]+5'))
153 " Invalid local list item
154 call assert_equal(0, exists('local_list[2]'))
155 " Non-existing local list
156 unlet local_list
157 call assert_equal(0, exists('local_list'))
158 " Valid local dictionary
159 let local_dict = {"xcord":100, "ycord":2}
160 call assert_equal(1, exists('local_dict'))
161 " Non-existing local dictionary
162 unlet local_dict
163 call assert_equal(0, exists('local_dict'))
164 " Existing local curly-brace variable
165 let str = "local"
166 let curly_{str}_var = 1
167 call assert_equal(1, exists('curly_{str}_var'))
168 " Non-existing local curly-brace variable
169 unlet curly_{str}_var
170 call assert_equal(0, exists('curly_{str}_var'))
171
172 " Existing global variable
173 let g:global_var = 1
174 call assert_equal(1, exists('g:global_var'))
175 " Existing global variable with garbage
176 call assert_equal(0, exists('g:global_var-n'))
177 " Non-existing global variable
178 unlet g:global_var
179 call assert_equal(0, exists('g:global_var'))
180 " Existing global list
181 let g:global_list = ["blue", "orange"]
182 call assert_equal(1, exists('g:global_list'))
183 " Non-existing global list
184 unlet g:global_list
185 call assert_equal(0, exists('g:global_list'))
186 " Existing global dictionary
187 let g:global_dict = {"xcord":100, "ycord":2}
188 call assert_equal(1, exists('g:global_dict'))
189 " Non-existing global dictionary
190 unlet g:global_dict
191 call assert_equal(0, exists('g:global_dict'))
192 " Existing global curly-brace variable
193 let str = "global"
194 let g:curly_{str}_var = 1
195 call assert_equal(1, exists('g:curly_{str}_var'))
196 " Non-existing global curly-brace variable
197 unlet g:curly_{str}_var
198 call assert_equal(0, exists('g:curly_{str}_var'))
199
200 " Existing window variable
201 let w:window_var = 1
202 call assert_equal(1, exists('w:window_var'))
203 " Non-existing window variable
204 unlet w:window_var
205 call assert_equal(0, exists('w:window_var'))
206 " Existing window list
207 let w:window_list = ["blue", "orange"]
208 call assert_equal(1, exists('w:window_list'))
209 " Non-existing window list
210 unlet w:window_list
211 call assert_equal(0, exists('w:window_list'))
212 " Existing window dictionary
213 let w:window_dict = {"xcord":100, "ycord":2}
214 call assert_equal(1, exists('w:window_dict'))
215 " Non-existing window dictionary
216 unlet w:window_dict
217 call assert_equal(0, exists('w:window_dict'))
218 " Existing window curly-brace variable
219 let str = "window"
220 let w:curly_{str}_var = 1
221 call assert_equal(1, exists('w:curly_{str}_var'))
222 " Non-existing window curly-brace variable
223 unlet w:curly_{str}_var
224 call assert_equal(0, exists('w:curly_{str}_var'))
225
226 " Existing tab variable
227 let t:tab_var = 1
228 call assert_equal(1, exists('t:tab_var'))
229 " Non-existing tab variable
230 unlet t:tab_var
231 call assert_equal(0, exists('t:tab_var'))
232 " Existing tab list
233 let t:tab_list = ["blue", "orange"]
234 call assert_equal(1, exists('t:tab_list'))
235 " Non-existing tab list
236 unlet t:tab_list
237 call assert_equal(0, exists('t:tab_list'))
238 " Existing tab dictionary
239 let t:tab_dict = {"xcord":100, "ycord":2}
240 call assert_equal(1, exists('t:tab_dict'))
241 " Non-existing tab dictionary
242 unlet t:tab_dict
243 call assert_equal(0, exists('t:tab_dict'))
244 " Existing tab curly-brace variable
245 let str = "tab"
246 let t:curly_{str}_var = 1
247 call assert_equal(1, exists('t:curly_{str}_var'))
248 " Non-existing tab curly-brace variable
249 unlet t:curly_{str}_var
250 call assert_equal(0, exists('t:curly_{str}_var'))
251
252 " Existing buffer variable
253 let b:buffer_var = 1
254 call assert_equal(1, exists('b:buffer_var'))
255 " Non-existing buffer variable
256 unlet b:buffer_var
257 call assert_equal(0, exists('b:buffer_var'))
258 " Existing buffer list
259 let b:buffer_list = ["blue", "orange"]
260 call assert_equal(1, exists('b:buffer_list'))
261 " Non-existing buffer list
262 unlet b:buffer_list
263 call assert_equal(0, exists('b:buffer_list'))
264 " Existing buffer dictionary
265 let b:buffer_dict = {"xcord":100, "ycord":2}
266 call assert_equal(1, exists('b:buffer_dict'))
267 " Non-existing buffer dictionary
268 unlet b:buffer_dict
269 call assert_equal(0, exists('b:buffer_dict'))
270 " Existing buffer curly-brace variable
271 let str = "buffer"
272 let b:curly_{str}_var = 1
273 call assert_equal(1, exists('b:curly_{str}_var'))
274 " Non-existing buffer curly-brace variable
275 unlet b:curly_{str}_var
276 call assert_equal(0, exists('b:curly_{str}_var'))
277
278 " Existing Vim internal variable
279 call assert_equal(1, exists('v:version'))
280 " Non-existing Vim internal variable
281 call assert_equal(0, exists('v:non_exists_var'))
282
283 " Existing script-local variable
284 let s:script_var = 1
285 call assert_equal(1, exists('s:script_var'))
286 " Non-existing script-local variable
287 unlet s:script_var
288 call assert_equal(0, exists('s:script_var'))
289 " Existing script-local list
290 let s:script_list = ["blue", "orange"]
291 call assert_equal(1, exists('s:script_list'))
292 " Non-existing script-local list
293 unlet s:script_list
294 call assert_equal(0, exists('s:script_list'))
295 " Existing script-local dictionary
296 let s:script_dict = {"xcord":100, "ycord":2}
297 call assert_equal(1, exists('s:script_dict'))
298 " Non-existing script-local dictionary
299 unlet s:script_dict
300 call assert_equal(0, exists('s:script_dict'))
301 " Existing script curly-brace variable
302 let str = "script"
303 let s:curly_{str}_var = 1
304 call assert_equal(1, exists('s:curly_{str}_var'))
305 " Non-existing script-local curly-brace variable
306 unlet s:curly_{str}_var
307 call assert_equal(0, exists('s:curly_{str}_var'))
308
309 " Existing script-local function
310 function! s:my_script_func()
311 endfunction
312
313 echo '*s:my_script_func: 1'
314 call assert_equal(1, exists('*s:my_script_func'))
315
316 " Non-existing script-local function
317 delfunction s:my_script_func
318
319 call assert_equal(0, exists('*s:my_script_func'))
320 unlet str
321
322 call assert_equal(1, g:footest#x)
323 call assert_equal(0, footest#F())
324 call assert_equal(0, UndefFun())
325endfunc
326
327" exists() test for Function arguments
328func FuncArg_Tests(func_arg, ...)
329 call assert_equal(1, exists('a:func_arg'))
330 call assert_equal(0, exists('a:non_exists_arg'))
331 call assert_equal(1, exists('a:1'))
332 call assert_equal(0, exists('a:2'))
333endfunc
334
335func Test_exists_funcarg()
336 call FuncArg_Tests("arg1", "arg2")
337endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100338
Yegappan Lakshmanana2ebb6e2024-02-25 08:40:10 +0100339" Test for using exists() with class and object variables and methods.
340func Test_exists_class_object()
341 let lines =<< trim END
342 vim9script
343 class A
344 var var1: number = 10
345 static var var2: number = 10
346 static def Foo()
347 enddef
348 def Bar()
349 enddef
350 endclass
351
352 assert_equal(1, exists("A"))
353 var a = A.new()
354 assert_equal(1, exists("a"))
355
356 assert_equal(1, exists("a.var1"))
357 assert_fails('exists("a.var2")', 'E1375: Class variable "var2" accessible only using class "A"')
358 assert_fails('exists("a.var3")', 'E1326: Variable "var3" not found in object "A"')
359 assert_equal(1, exists("A.var2"))
360 assert_fails('exists("A.var1")', 'E1376: Object variable "var1" accessible only using class "A" object')
361 assert_fails('exists("A.var3")', 'E1337: Class variable "var3" not found in class "A"')
362
363 assert_equal(1, exists("a.Bar"))
364 assert_fails('exists("a.Barz")', 'E1326: Variable "Barz" not found in object "A"')
365 assert_fails('exists("a.Foo")', 'E1326: Variable "Foo" not found in object "A"')
366 assert_equal(1, exists("A.Foo"))
367 assert_fails('exists("A.Bar")', 'E1337: Class variable "Bar" not found in class "A"')
368 assert_fails('exists("A.Barz")', 'E1337: Class variable "Barz" not found in class "A"')
369
370 def Baz()
371 assert_equal(1, exists("A"))
372 var aa = A.new()
373 assert_equal(1, exists("A.var2"))
374 assert_fails('exists("A.var1")', 'E1376: Object variable "var1" accessible only using class "A" object')
375 assert_fails('exists("A.var3")', 'E1337: Class variable "var3" not found in class "A"')
376
377 assert_equal(1, exists("A.Foo"))
378 assert_fails('exists("A.Bar")', 'E1337: Class variable "Bar" not found in class "A"')
379 assert_fails('exists("A.Barz")', 'E1337: Class variable "Barz" not found in class "A"')
380 enddef
381 Baz()
382 END
383 call v9.CheckSourceSuccess(lines)
384endfunc
385
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100386" vim: shiftwidth=2 sts=2 expandtab