615
|
1 Tests for the exists() function. vim: set ft=vim :
|
|
2
|
|
3 STARTTEST
|
|
4 :so small.vim
|
|
5 :function! RunTest(str, result)
|
|
6 if exists(a:str) == a:result
|
|
7 echo "OK"
|
|
8 else
|
|
9 echo "FAILED: Checking for " . a:str
|
|
10 endif
|
|
11 endfunction
|
|
12 :function! TestExists()
|
|
13 augroup myagroup
|
|
14 autocmd! BufEnter *.my echo 'myfile edited'
|
|
15 augroup END
|
617
|
16
|
|
17 let test_cases = []
|
615
|
18
|
|
19 " valid autocmd group
|
617
|
20 let test_cases += [['#myagroup', 1]]
|
615
|
21 " Valid autocmd group and event
|
617
|
22 let test_cases += [['#myagroup#BufEnter', 1]]
|
615
|
23 " Valid autocmd group, event and pattern
|
617
|
24 let test_cases += [['#myagroup#BufEnter#*.my', 1]]
|
615
|
25 " Valid autocmd event
|
617
|
26 let test_cases += [['#BufEnter', 1]]
|
615
|
27 " Valid autocmd event and pattern
|
617
|
28 let test_cases += [['#BufEnter#*.my', 1]]
|
615
|
29 " Non-existing autocmd group or event
|
617
|
30 let test_cases += [['#xyzagroup', 0]]
|
615
|
31 " Non-existing autocmd group and valid autocmd event
|
617
|
32 let test_cases += [['#xyzagroup#BufEnter', 0]]
|
|
33 " Valid autocmd group and event with no matching pattern
|
|
34 let test_cases += [['#myagroup#CmdwinEnter', 0]]
|
|
35 " Valid autocmd group and non-existing autocmd event
|
|
36 let test_cases += [['#myagroup#xyzacmd', 0]]
|
|
37 " Valid autocmd group and event and non-matching pattern
|
|
38 let test_cases += [['#myagroup#BufEnter#xyzpat', 0]]
|
|
39 " Valid autocmd event and non-matching pattern
|
|
40 let test_cases += [['#BufEnter#xyzpat', 0]]
|
|
41 " Empty autocmd group, event and pattern
|
|
42 let test_cases += [['###', 0]]
|
|
43 " Empty autocmd group and event or empty event and pattern
|
|
44 let test_cases += [['##', 0]]
|
|
45 " Valid autocmd event
|
|
46 let test_cases += [['##FileReadCmd', 1]]
|
|
47 " Non-existing autocmd event
|
|
48 let test_cases += [['##MySpecialCmd', 0]]
|
615
|
49
|
620
|
50 " Existing and working option (long form)
|
|
51 let test_cases += [['&textwidth', 1]]
|
|
52 " Existing and working option (short form)
|
|
53 let test_cases += [['&tw', 1]]
|
|
54 " Negative form of existing and working option (long form)
|
|
55 let test_cases += [['&nojoinspaces', 0]]
|
|
56 " Negative form of existing and working option (short form)
|
|
57 let test_cases += [['&nojs', 0]]
|
|
58 " Non-existing option
|
|
59 let test_cases += [['&myxyzoption', 0]]
|
|
60
|
|
61 " Existing and working option (long form)
|
|
62 let test_cases += [['+incsearch', 1]]
|
|
63 " Existing and working option (short form)
|
|
64 let test_cases += [['+is', 1]]
|
|
65 " Existing option that is hidden.
|
|
66 let test_cases += [['+autoprint', 0]]
|
|
67
|
|
68 " Existing environment variable
|
|
69 let $EDITOR_NAME = 'Vim Editor'
|
|
70 let test_cases += [['$EDITOR_NAME', 1]]
|
|
71 " Non-existing environment variable
|
|
72 let test_cases += [['$NON_ENV_VAR', 0]]
|
|
73
|
|
74 " Valid internal function
|
|
75 let test_cases += [['*bufnr', 1]]
|
|
76 " Non-existing internal function
|
|
77 let test_cases += [['*myxyzfunc', 0]]
|
|
78
|
|
79 " Valid user defined function
|
|
80 let test_cases += [['*TestExists', 1]]
|
|
81 " Non-existing user defined function
|
|
82 let test_cases += [['*MyxyzFunc', 0]]
|
|
83
|
617
|
84 redir! > test.out
|
615
|
85
|
617
|
86 for [test_case, result] in test_cases
|
|
87 echo test_case . ": " . result
|
|
88 call RunTest(test_case, result)
|
|
89 endfor
|
615
|
90
|
620
|
91 " Valid internal command (full match)
|
|
92 echo ':edit: 2'
|
|
93 if exists(':edit') == 2
|
|
94 echo "OK"
|
|
95 else
|
|
96 echo "FAILED"
|
|
97 endif
|
|
98
|
|
99 " Valid internal command (partial match)
|
|
100 echo ':q: 1'
|
|
101 if exists(':q') == 1
|
|
102 echo "OK"
|
|
103 else
|
|
104 echo "FAILED"
|
|
105 endif
|
|
106
|
|
107 " Non-existing internal command
|
|
108 echo ':invalidcmd: 0'
|
|
109 if !exists(':invalidcmd')
|
|
110 echo "OK"
|
|
111 else
|
|
112 echo "FAILED"
|
|
113 endif
|
|
114
|
|
115 " User defined command (full match)
|
|
116 command! MyCmd :echo 'My command'
|
|
117 echo ':MyCmd: 2'
|
|
118 if exists(':MyCmd') == 2
|
|
119 echo "OK"
|
|
120 else
|
|
121 echo "FAILED"
|
|
122 endif
|
|
123
|
|
124 " User defined command (partial match)
|
|
125 command! MyOtherCmd :echo 'Another command'
|
|
126 echo ':My: 3'
|
|
127 if exists(':My') == 3
|
|
128 echo "OK"
|
|
129 else
|
|
130 echo "FAILED"
|
|
131 endif
|
|
132
|
|
133 " Command modifier
|
|
134 echo ':rightbelow: 2'
|
|
135 if exists(':rightbelow') == 2
|
|
136 echo "OK"
|
|
137 else
|
|
138 echo "FAILED"
|
|
139 endif
|
|
140
|
|
141 " Non-existing user defined command (full match)
|
|
142 delcommand MyCmd
|
|
143
|
|
144 echo ':MyCmd: 0'
|
|
145 if !exists(':MyCmd')
|
|
146 echo "OK"
|
|
147 else
|
|
148 echo "FAILED"
|
|
149 endif
|
|
150
|
|
151 " Non-existing user defined command (partial match)
|
|
152 delcommand MyOtherCmd
|
|
153
|
|
154 echo ':My: 0'
|
|
155 if !exists(':My')
|
|
156 echo "OK"
|
|
157 else
|
|
158 echo "FAILED"
|
|
159 endif
|
|
160
|
|
161 " Valid local variable
|
|
162 let local_var = 1
|
|
163 echo 'local_var: 1'
|
|
164 if exists('local_var')
|
|
165 echo "OK"
|
|
166 else
|
|
167 echo "FAILED"
|
|
168 endif
|
|
169
|
|
170 " Non-existing local variable
|
|
171 unlet local_var
|
|
172 echo 'local_var: 0'
|
|
173 if !exists('local_var')
|
|
174 echo "OK"
|
|
175 else
|
|
176 echo "FAILED"
|
|
177 endif
|
|
178
|
|
179 " Valid local list
|
|
180 let local_list = ["blue", "orange"]
|
|
181 echo 'local_list: 1'
|
|
182 if exists('local_list')
|
|
183 echo "OK"
|
|
184 else
|
|
185 echo "FAILED"
|
|
186 endif
|
|
187
|
|
188 " Non-existing local list
|
|
189 unlet local_list
|
|
190 echo 'local_list: 0'
|
|
191 if !exists('local_list')
|
|
192 echo "OK"
|
|
193 else
|
|
194 echo "FAILED"
|
|
195 endif
|
|
196
|
|
197 " Valid local dictionary
|
|
198 let local_dict = {"xcord":100, "ycord":2}
|
|
199 echo 'local_dict: 1'
|
|
200 if exists('local_dict')
|
|
201 echo "OK"
|
|
202 else
|
|
203 echo "FAILED"
|
|
204 endif
|
|
205
|
|
206 " Non-existing local dictionary
|
|
207 unlet local_dict
|
|
208 echo 'local_dict: 0'
|
|
209 if !exists('local_dict')
|
|
210 echo "OK"
|
|
211 else
|
|
212 echo "FAILED"
|
|
213 endif
|
|
214
|
|
215 " Existing global variable
|
|
216 let g:global_var = 1
|
|
217 echo 'g:global_var: 1'
|
|
218 if exists('g:global_var')
|
|
219 echo "OK"
|
|
220 else
|
|
221 echo "FAILED"
|
|
222 endif
|
|
223
|
|
224 " Non-existing global variable
|
|
225 unlet g:global_var
|
|
226 echo 'g:global_var: 0'
|
|
227 if !exists('g:global_var')
|
|
228 echo "OK"
|
|
229 else
|
|
230 echo "FAILED"
|
|
231 endif
|
|
232
|
|
233 " Existing local curly-brace variable
|
|
234 let curly_local_var = 1
|
|
235 let str = "local"
|
|
236 echo 'curly_{str}_var: 1'
|
|
237 if exists('curly_{str}_var')
|
|
238 echo "OK"
|
|
239 else
|
|
240 echo "FAILED"
|
|
241 endif
|
|
242
|
|
243 " Non-existing local curly-brace variable
|
|
244 unlet curly_local_var
|
|
245 echo 'curly_{str}_var: 0'
|
|
246 if !exists('curly_{str}_var')
|
|
247 echo "OK"
|
|
248 else
|
|
249 echo "FAILED"
|
|
250 endif
|
|
251
|
|
252 " Existing global curly-brace variable
|
|
253 let g:curly_global_var = 1
|
|
254 let str = "global"
|
|
255 echo 'g:curly_{str}_var: 1'
|
|
256 if exists('g:curly_{str}_var')
|
|
257 echo "OK"
|
|
258 else
|
|
259 echo "FAILED"
|
|
260 endif
|
|
261
|
|
262 " Non-existing global curly-brace variable
|
|
263 unlet g:curly_global_var
|
|
264 echo 'g:curly_{str}_var: 0'
|
|
265 if !exists('g:curly_{str}_var')
|
|
266 echo "OK"
|
|
267 else
|
|
268 echo "FAILED"
|
|
269 endif
|
|
270
|
|
271 " Script-local tests
|
|
272 source test60.vim
|
|
273
|
615
|
274 redir END
|
|
275 endfunction
|
|
276 :call TestExists()
|
|
277 :edit! test.out
|
|
278 :set ff=unix
|
|
279 :w
|
|
280 :qa!
|
|
281 ENDTEST
|
|
282
|