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