test_exists.vim 11 KB

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