robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
b2828af5b5
master
1" Tests for the exists() function 2func Test_exists () 3augroup myagroup 4autocmd ! BufEnter * .my echo "myfile edited" 5autocmd ! FuncUndefined UndefFun exec "fu UndefFun()\nendfu" 6augroup END 7set rtp += ./sautest 8 9" valid autocmd group 10call assert_equal ( 1 , exists ( '#myagroup' )) 11" valid autocmd group with garbage 12call assert_equal ( 0 , exists ( '#myagroup+b' )) 13" Valid autocmd group and event 14call assert_equal ( 1 , exists ( '#myagroup#BufEnter' )) 15" Valid autocmd group, event and pattern 16call assert_equal ( 1 , exists ( '#myagroup#BufEnter#*.my' )) 17" Valid autocmd event 18call assert_equal ( 1 , exists ( '#BufEnter' )) 19" Valid autocmd event and pattern 20call assert_equal ( 1 , exists ( '#BufEnter#*.my' )) 21" Non-existing autocmd group or event 22call assert_equal ( 0 , exists ( '#xyzagroup' )) 23" Non-existing autocmd group and valid autocmd event 24call assert_equal ( 0 , exists ( '#xyzagroup#BufEnter' )) 25" Valid autocmd group and event with no matching pattern 26call assert_equal ( 0 , exists ( '#myagroup#CmdwinEnter' )) 27" Valid autocmd group and non-existing autocmd event 28call assert_equal ( 0 , exists ( '#myagroup#xyzacmd' )) 29" Valid autocmd group and event and non-matching pattern 30call assert_equal ( 0 , exists ( '#myagroup#BufEnter#xyzpat' )) 31" Valid autocmd event and non-matching pattern 32call assert_equal ( 0 , exists ( '#BufEnter#xyzpat' )) 33" Empty autocmd group, event and pattern 34call assert_equal ( 0 , exists ( '###' )) 35" Empty autocmd group and event or empty event and pattern 36call assert_equal ( 0 , exists ( '##' )) 37" Valid autocmd event 38call assert_equal ( 1 , exists ( '##FileReadCmd' )) 39" Non-existing autocmd event 40call assert_equal ( 0 , exists ( '##MySpecialCmd' )) 41 42" Existing and working option (long form) 43call assert_equal ( 1 , exists ( '&textwidth' )) 44" Existing and working option (short form) 45call assert_equal ( 1 , exists ( '&tw' )) 46" Existing and working option with garbage 47call assert_equal ( 0 , exists ( '&tw-' )) 48" Global option 49call assert_equal ( 1 , exists ( '&g:errorformat' )) 50" Local option 51call assert_equal ( 1 , exists ( '&l:errorformat' )) 52" Negative form of existing and working option (long form) 53call assert_equal ( 0 , exists ( '&nojoinspaces' )) 54" Negative form of existing and working option (short form) 55call assert_equal ( 0 , exists ( '&nojs' )) 56" Non-existing option 57call assert_equal ( 0 , exists ( '&myxyzoption' )) 58 59" Existing and working option (long form) 60call assert_equal ( 1 , exists ( '+incsearch' )) 61" Existing and working option with garbage 62call assert_equal ( 0 , exists ( '+incsearch!1' )) 63" Existing and working option (short form) 64call assert_equal ( 1 , exists ( '+is' )) 65" Existing option that is hidden. 66call assert_equal ( 0 , exists ( '+autoprint' )) 67 68" Existing environment variable 69let $ EDITOR_NAME = 'Vim Editor' 70call assert_equal ( 1 , exists ( '$EDITOR_NAME' )) 71if has ( 'unix' ) 72" ${name} environment variables are supported only on Unix-like systems 73call assert_equal ( 1 , exists ( '${VIM}' )) 74endif 75" Non-existing environment variable 76call assert_equal ( 0 , exists ( '$NON_ENV_VAR' )) 77 78" Valid internal function 79call assert_equal ( 1 , exists ( '*bufnr' )) 80" Valid internal function with () 81call assert_equal ( 1 , exists ( '*bufnr()' )) 82" Non-existing internal function 83call assert_equal ( 0 , exists ( '*myxyzfunc' )) 84" Valid internal function with garbage 85call assert_equal ( 0 , exists ( '*bufnr&6' )) 86" Valid user defined function 87call assert_equal ( 1 , exists ( '*Test_exists' )) 88" Non-existing user defined function 89call assert_equal ( 0 , exists ( '*MyxyzFunc' )) 90" Function that may be created by FuncUndefined event 91call assert_equal ( 0 , exists ( '*UndefFun' )) 92" Function that may be created by script autoloading 93call assert_equal ( 0 , exists ( '*footest#F' )) 94 95" Valid internal command (full match) 96call assert_equal ( 2 , exists ( ':edit' )) 97" Valid internal command (full match) with garbage 98call assert_equal ( 0 , exists ( ':edit/a' )) 99" Valid internal command (partial match) 100call assert_equal ( 1 , exists ( ':q' )) 101" Valid internal command with a digit 102call assert_equal ( 2 , exists ( ':2match' )) 103" Non-existing internal command 104call assert_equal ( 0 , exists ( ':invalidcmd' )) 105" Internal command with a count 106call assert_equal ( 0 , exists ( ':3buffer' )) 107 108" Valid internal command (full match) 109call assert_equal ( 2 , exists ( ':k' )) 110" Non-existing internal command (':k' with arg 'e') 111call assert_equal ( 0 , exists ( ':ke' )) 112" Valid internal command (partial match) 113call assert_equal ( 1 , exists ( ':kee' )) 114 115" User defined command (full match) 116command ! MyCmd :echo 'My command' 117call assert_equal ( 2 , exists ( ':MyCmd' )) 118" User defined command (partial match) 119command ! MyOtherCmd :echo 'Another command' 120call assert_equal ( 3 , exists ( ':My' )) 121 122" Command modifier 123call assert_equal ( 2 , exists ( ':rightbelow' )) 124 125" Non-existing user defined command (full match) 126delcommand MyCmd 127call assert_equal ( 0 , exists ( ':MyCmd' )) 128 129" Non-existing user defined command (partial match) 130delcommand MyOtherCmd 131call assert_equal ( 0 , exists ( ':My' )) 132 133" Valid local variable 134let local_var = 1 135call assert_equal ( 1 , exists ( 'local_var' )) 136" Valid local variable with garbage 137call assert_equal ( 0 , exists ( 'local_var%n' )) 138" Non-existing local variable 139unlet local_var 140call assert_equal ( 0 , exists ( 'local_var' )) 141 142" Non-existing autoload variable that may be autoloaded 143call assert_equal ( 0 , exists ( 'footest#x' )) 144 145" Valid local list 146let local_list = [ "blue" , "orange" ] 147call assert_equal ( 1 , exists ( 'local_list' )) 148" Valid local list item 149call assert_equal ( 1 , exists ( 'local_list[1]' )) 150" Valid local list item with garbage 151call assert_equal ( 0 , exists ( 'local_list[1]+5' )) 152" Invalid local list item 153call assert_equal ( 0 , exists ( 'local_list[2]' )) 154" Non-existing local list 155unlet local_list 156call assert_equal ( 0 , exists ( 'local_list' )) 157" Valid local dictionary 158let local_dict = { "xcord" : 100 , "ycord" : 2 } 159call assert_equal ( 1 , exists ( 'local_dict' )) 160" Non-existing local dictionary 161unlet local_dict 162call assert_equal ( 0 , exists ( 'local_dict' )) 163" Existing local curly-brace variable 164let str = "local" 165let curly_ { str } _var = 1 166call assert_equal ( 1 , exists ( 'curly_{str}_var' )) 167" Non-existing local curly-brace variable 168unlet curly_ { str } _var 169call assert_equal ( 0 , exists ( 'curly_{str}_var' )) 170 171" Existing global variable 172let g: global_var = 1 173call assert_equal ( 1 , exists ( 'g:global_var' )) 174" Existing global variable with garbage 175call assert_equal ( 0 , exists ( 'g:global_var-n' )) 176" Non-existing global variable 177unlet g: global_var 178call assert_equal ( 0 , exists ( 'g:global_var' )) 179" Existing global list 180let g: global_list = [ "blue" , "orange" ] 181call assert_equal ( 1 , exists ( 'g:global_list' )) 182" Non-existing global list 183unlet g: global_list 184call assert_equal ( 0 , exists ( 'g:global_list' )) 185" Existing global dictionary 186let g: global_dict = { "xcord" : 100 , "ycord" : 2 } 187call assert_equal ( 1 , exists ( 'g:global_dict' )) 188" Non-existing global dictionary 189unlet g: global_dict 190call assert_equal ( 0 , exists ( 'g:global_dict' )) 191" Existing global curly-brace variable 192let str = "global" 193let g: curly_ { str } _var = 1 194call assert_equal ( 1 , exists ( 'g:curly_{str}_var' )) 195" Non-existing global curly-brace variable 196unlet g: curly_ { str } _var 197call assert_equal ( 0 , exists ( 'g:curly_{str}_var' )) 198 199" Existing window variable 200let w: window_var = 1 201call assert_equal ( 1 , exists ( 'w:window_var' )) 202" Non-existing window variable 203unlet w: window_var 204call assert_equal ( 0 , exists ( 'w:window_var' )) 205" Existing window list 206let w: window_list = [ "blue" , "orange" ] 207call assert_equal ( 1 , exists ( 'w:window_list' )) 208" Non-existing window list 209unlet w: window_list 210call assert_equal ( 0 , exists ( 'w:window_list' )) 211" Existing window dictionary 212let w: window_dict = { "xcord" : 100 , "ycord" : 2 } 213call assert_equal ( 1 , exists ( 'w:window_dict' )) 214" Non-existing window dictionary 215unlet w: window_dict 216call assert_equal ( 0 , exists ( 'w:window_dict' )) 217" Existing window curly-brace variable 218let str = "window" 219let w: curly_ { str } _var = 1 220call assert_equal ( 1 , exists ( 'w:curly_{str}_var' )) 221" Non-existing window curly-brace variable 222unlet w: curly_ { str } _var 223call assert_equal ( 0 , exists ( 'w:curly_{str}_var' )) 224 225" Existing tab variable 226let t: tab_var = 1 227call assert_equal ( 1 , exists ( 't:tab_var' )) 228" Non-existing tab variable 229unlet t: tab_var 230call assert_equal ( 0 , exists ( 't:tab_var' )) 231" Existing tab list 232let t: tab_list = [ "blue" , "orange" ] 233call assert_equal ( 1 , exists ( 't:tab_list' )) 234" Non-existing tab list 235unlet t: tab_list 236call assert_equal ( 0 , exists ( 't:tab_list' )) 237" Existing tab dictionary 238let t: tab_dict = { "xcord" : 100 , "ycord" : 2 } 239call assert_equal ( 1 , exists ( 't:tab_dict' )) 240" Non-existing tab dictionary 241unlet t: tab_dict 242call assert_equal ( 0 , exists ( 't:tab_dict' )) 243" Existing tab curly-brace variable 244let str = "tab" 245let t: curly_ { str } _var = 1 246call assert_equal ( 1 , exists ( 't:curly_{str}_var' )) 247" Non-existing tab curly-brace variable 248unlet t: curly_ { str } _var 249call assert_equal ( 0 , exists ( 't:curly_{str}_var' )) 250 251" Existing buffer variable 252let b: buffer_var = 1 253call assert_equal ( 1 , exists ( 'b:buffer_var' )) 254" Non-existing buffer variable 255unlet b: buffer_var 256call assert_equal ( 0 , exists ( 'b:buffer_var' )) 257" Existing buffer list 258let b: buffer_list = [ "blue" , "orange" ] 259call assert_equal ( 1 , exists ( 'b:buffer_list' )) 260" Non-existing buffer list 261unlet b: buffer_list 262call assert_equal ( 0 , exists ( 'b:buffer_list' )) 263" Existing buffer dictionary 264let b: buffer_dict = { "xcord" : 100 , "ycord" : 2 } 265call assert_equal ( 1 , exists ( 'b:buffer_dict' )) 266" Non-existing buffer dictionary 267unlet b: buffer_dict 268call assert_equal ( 0 , exists ( 'b:buffer_dict' )) 269" Existing buffer curly-brace variable 270let str = "buffer" 271let b: curly_ { str } _var = 1 272call assert_equal ( 1 , exists ( 'b:curly_{str}_var' )) 273" Non-existing buffer curly-brace variable 274unlet b: curly_ { str } _var 275call assert_equal ( 0 , exists ( 'b:curly_{str}_var' )) 276 277" Existing Vim internal variable 278call assert_equal ( 1 , exists ( 'v:version' )) 279" Non-existing Vim internal variable 280call assert_equal ( 0 , exists ( 'v:non_exists_var' )) 281 282" Existing script-local variable 283let s: script_var = 1 284call assert_equal ( 1 , exists ( 's:script_var' )) 285" Non-existing script-local variable 286unlet s: script_var 287call assert_equal ( 0 , exists ( 's:script_var' )) 288" Existing script-local list 289let s: script_list = [ "blue" , "orange" ] 290call assert_equal ( 1 , exists ( 's:script_list' )) 291" Non-existing script-local list 292unlet s: script_list 293call assert_equal ( 0 , exists ( 's:script_list' )) 294" Existing script-local dictionary 295let s: script_dict = { "xcord" : 100 , "ycord" : 2 } 296call assert_equal ( 1 , exists ( 's:script_dict' )) 297" Non-existing script-local dictionary 298unlet s: script_dict 299call assert_equal ( 0 , exists ( 's:script_dict' )) 300" Existing script curly-brace variable 301let str = "script" 302let s: curly_ { str } _var = 1 303call assert_equal ( 1 , exists ( 's:curly_{str}_var' )) 304" Non-existing script-local curly-brace variable 305unlet s: curly_ { str } _var 306call assert_equal ( 0 , exists ( 's:curly_{str}_var' )) 307 308" Existing script-local function 309function ! s: my_script_func () 310endfunction 311 312echo '*s:my_script_func: 1' 313call assert_equal ( 1 , exists ( '*s:my_script_func' )) 314 315" Non-existing script-local function 316delfunction s:my_script_func 317 318call assert_equal ( 0 , exists ( '*s:my_script_func' )) 319unlet str 320 321call assert_equal ( 1 , g: footest#x ) 322call assert_equal ( 0 , footest#F ()) 323call assert_equal ( 0 , UndefFun ()) 324endfunc 325 326" exists() test for Function arguments 327func FuncArg_Tests ( func_arg , ...) 328call assert_equal ( 1 , exists ( 'a:func_arg' )) 329call assert_equal ( 0 , exists ( 'a:non_exists_arg' )) 330call assert_equal ( 1 , exists ( 'a:1' )) 331call assert_equal ( 0 , exists ( 'a:2' )) 332endfunc 333 334func Test_exists_funcarg () 335call FuncArg_Tests ( "arg1" , "arg2" ) 336endfunc 337 338" vim: shiftwidth=2 sts=2 expandtab