function_spec.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local eq = helpers.eq
  3. local clear = helpers.clear
  4. local dedent = helpers.dedent
  5. local redir_exec = helpers.redir_exec
  6. before_each(clear)
  7. local function check_func(fname, body, indent)
  8. if type(body) == 'number' then
  9. body = ('return %i'):format(body)
  10. end
  11. eq(dedent(([[
  12. function %s()%s
  13. endfunction]]
  14. ), 3):format(
  15. fname,
  16. body and ('\n1' .. (' '):rep(2 + (indent or 8)) .. body) or ''),
  17. redir_exec('function ' .. fname))
  18. end
  19. describe(':endfunction', function()
  20. it('accepts bang', function()
  21. eq('', redir_exec([[
  22. function F()
  23. endfunction!
  24. ]]))
  25. check_func('F')
  26. eq('', redir_exec([[
  27. function! F()
  28. return 1
  29. endfunction!
  30. ]]))
  31. check_func('F', 1)
  32. end)
  33. it('accepts comments', function()
  34. eq('', redir_exec([[
  35. function F1()
  36. endfunction " Comment
  37. ]]))
  38. check_func('F1')
  39. eq('', redir_exec([[
  40. function F2()
  41. endfunction " }}}
  42. ]]))
  43. check_func('F2')
  44. eq('', redir_exec([[
  45. function F3()
  46. endfunction " F3
  47. ]]))
  48. check_func('F3')
  49. eq('', redir_exec([[
  50. function F4()
  51. endfunction! " F4
  52. ]]))
  53. check_func('F4')
  54. eq('', redir_exec([[
  55. function! F4()
  56. return 2
  57. endfunction! " F4
  58. ]]))
  59. check_func('F4', 2)
  60. end)
  61. it('accepts function name', function()
  62. eq('', redir_exec([[
  63. function F0()
  64. endfunction F0
  65. ]]))
  66. check_func('F0')
  67. eq('', redir_exec([[
  68. function F1()
  69. endfunction! F1
  70. ]]))
  71. check_func('F1')
  72. eq('', redir_exec([[
  73. function! F2()
  74. endfunction! F2
  75. ]]))
  76. check_func('F2')
  77. eq('', redir_exec([[
  78. function! F2()
  79. return 3
  80. endfunction! F2
  81. ]]))
  82. check_func('F2', 3)
  83. end)
  84. it('accepts weird characters', function()
  85. eq('', redir_exec([[
  86. function F1()
  87. endfunction: }}}
  88. ]]))
  89. check_func('F1')
  90. -- From accurev
  91. eq('', redir_exec([[
  92. function F2()
  93. endfunction :}}}
  94. ]]))
  95. check_func('F2')
  96. -- From cream-vimabbrev
  97. eq('', redir_exec([[
  98. function F3()
  99. endfunction 1}}}
  100. ]]))
  101. check_func('F3')
  102. -- From pyunit
  103. eq('', redir_exec([[
  104. function F4()
  105. endfunction # }}}
  106. ]]))
  107. check_func('F4')
  108. -- From vim-lldb
  109. eq('', redir_exec([[
  110. function F5()
  111. endfunction()
  112. ]]))
  113. check_func('F5')
  114. -- From vim-mail
  115. eq('', redir_exec([[
  116. function F6()
  117. endfunction;
  118. ]]))
  119. check_func('F6')
  120. end)
  121. it('accepts commented bar', function()
  122. eq('', redir_exec([[
  123. function F1()
  124. endfunction " F1 | echo 42
  125. ]]))
  126. check_func('F1')
  127. eq('', redir_exec([[
  128. function! F1()
  129. return 42
  130. endfunction! " F1 | echo 42
  131. ]]))
  132. check_func('F1', 42)
  133. end)
  134. it('accepts uncommented bar', function()
  135. eq('\n42', redir_exec([[
  136. function F1()
  137. endfunction | echo 42
  138. ]]))
  139. check_func('F1')
  140. end)
  141. it('allows running multiple commands', function()
  142. eq('\n2', redir_exec([[
  143. function F1()
  144. echo 2
  145. endfunction
  146. call F1()
  147. ]]))
  148. check_func('F1', 'echo 2')
  149. eq('\n2\n3\n4', redir_exec([[
  150. function F2()
  151. echo 2
  152. endfunction F2
  153. function F3()
  154. echo 3
  155. endfunction " F3
  156. function! F4()
  157. echo 4
  158. endfunction!
  159. call F2()
  160. call F3()
  161. call F4()
  162. ]]))
  163. check_func('F2', 'echo 2')
  164. check_func('F3', 'echo 3')
  165. check_func('F4', 'echo 4')
  166. end)
  167. it('allows running multiple commands with only one character in between',
  168. function()
  169. eq('\n3', redir_exec(dedent([[
  170. function! F1()
  171. echo 3
  172. endfunction!
  173. call F1()]])))
  174. check_func('F1', 'echo 3', 2)
  175. eq('\n4', redir_exec(dedent([[
  176. function F5()
  177. echo 4
  178. endfunction
  179. call F5()]])))
  180. check_func('F5', 'echo 4', 2)
  181. eq('\n5', redir_exec(dedent([[
  182. function F6()
  183. echo 5
  184. endfunction " TEST
  185. call F6()]])))
  186. check_func('F6', 'echo 5', 2)
  187. eq('\n6', redir_exec(dedent([[
  188. function F7()
  189. echo 6
  190. endfunction F7
  191. call F7()]])))
  192. check_func('F7', 'echo 6', 2)
  193. eq('\n2\n3\n4', redir_exec(dedent([[
  194. function F2()
  195. echo 2
  196. endfunction F2
  197. function F3()
  198. echo 3
  199. endfunction " F3
  200. function! F4()
  201. echo 4
  202. endfunction!
  203. call F2()
  204. call F3()
  205. call F4()]])))
  206. check_func('F2', 'echo 2', 2)
  207. check_func('F3', 'echo 3', 2)
  208. check_func('F4', 'echo 4', 2)
  209. end)
  210. end)
  211. -- vim: foldmarker=▶,▲