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