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