assert_spec.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local nvim, call = helpers.meths, helpers.call
  3. local clear, eq = helpers.clear, helpers.eq
  4. local source, command = helpers.source, helpers.command
  5. local exc_exec = helpers.exc_exec
  6. local eval = helpers.eval
  7. local function expected_errors(errors)
  8. eq(errors, nvim.get_vvar('errors'))
  9. end
  10. local function expected_empty()
  11. eq({}, nvim.get_vvar('errors'))
  12. end
  13. describe('assert function:', function()
  14. before_each(function()
  15. clear()
  16. end)
  17. describe('assert_beeps', function()
  18. it('works', function()
  19. call('assert_beeps', 'normal h')
  20. expected_empty()
  21. call('assert_beeps', 'normal 0')
  22. expected_errors({'command did not beep: normal 0'})
  23. end)
  24. end)
  25. -- assert_equal({expected}, {actual}, [, {msg}])
  26. describe('assert_equal', function()
  27. it('should not change v:errors when expected is equal to actual', function()
  28. source([[
  29. let s = 'foo'
  30. call assert_equal('foo', s)
  31. let n = 4
  32. call assert_equal(4, n)
  33. let l = [1, 2, 3]
  34. call assert_equal([1, 2, 3], l)
  35. call assert_equal(v:_null_list, v:_null_list)
  36. call assert_equal(v:_null_list, [])
  37. call assert_equal([], v:_null_list)
  38. fu Func()
  39. endfu
  40. let F1 = function('Func')
  41. let F2 = function('Func')
  42. call assert_equal(F1, F2)
  43. ]])
  44. expected_empty()
  45. end)
  46. it('should not change v:errors when expected is equal to actual', function()
  47. eq(0, call('assert_equal', '', ''))
  48. eq(0, call('assert_equal', 'string', 'string'))
  49. expected_empty()
  50. end)
  51. it('should change v:errors when expected is not equal to actual', function()
  52. eq(1, call('assert_equal', 0, {0}))
  53. expected_errors({'Expected 0 but got [0]'})
  54. end)
  55. it('should change v:errors when expected is not equal to actual', function()
  56. eq(1, call('assert_equal', 0, "0"))
  57. expected_errors({"Expected 0 but got '0'"})
  58. end)
  59. it('should change v:errors when expected is not equal to actual', function()
  60. -- Lua does not tell integer from float.
  61. command('call assert_equal(1, 1.0)')
  62. expected_errors({'Expected 1 but got 1.0'})
  63. end)
  64. it('should change v:errors when expected is not equal to actual', function()
  65. call('assert_equal', 'true', 'false')
  66. expected_errors({"Expected 'true' but got 'false'"})
  67. end)
  68. it('should change v:errors when expected is not equal to actual', function()
  69. source([[
  70. function CheckAssert()
  71. let s:v = {}
  72. let s:x = {"a": s:v}
  73. let s:v["b"] = s:x
  74. let s:w = {"c": s:x, "d": ''}
  75. call assert_equal(s:w, '')
  76. endfunction
  77. ]])
  78. eq('Vim(call):E724: unable to correctly dump variable with self-referencing container',
  79. exc_exec('call CheckAssert()'))
  80. end)
  81. it('can specify a message and get a message about what failed', function()
  82. call('assert_equal', 'foo', 'bar', 'testing')
  83. expected_errors({"testing: Expected 'foo' but got 'bar'"})
  84. end)
  85. it('should shorten a long message', function()
  86. call ('assert_equal', 'XxxxxxxxxxxxxxxxxxxxxxX', 'XyyyyyyyyyyyyyyyyyyyyyyyyyX')
  87. expected_errors({"Expected 'X\\[x occurs 21 times]X' but got 'X\\[y occurs 25 times]X'"})
  88. end)
  89. end)
  90. -- assert_notequal({expected}, {actual}[, {msg}])
  91. describe('assert_notequal', function()
  92. it('should not change v:errors when expected differs from actual', function()
  93. eq(0, call('assert_notequal', 'foo', 4))
  94. eq(0, call('assert_notequal', {1, 2, 3}, 'foo'))
  95. expected_empty()
  96. end)
  97. it('should change v:errors when expected is equal to actual', function()
  98. eq(1, call('assert_notequal', 'foo', 'foo'))
  99. expected_errors({"Expected not equal to 'foo'"})
  100. end)
  101. end)
  102. -- assert_false({actual}, [, {msg}])
  103. describe('assert_false', function()
  104. it('should not change v:errors when actual is false', function()
  105. eq(0, call('assert_false', 0))
  106. eq(0, call('assert_false', false))
  107. expected_empty()
  108. end)
  109. it('should change v:errors when actual is not false', function()
  110. eq(1, call('assert_false', 1))
  111. expected_errors({'Expected False but got 1'})
  112. end)
  113. it('should change v:errors when actual is not false', function()
  114. call('assert_false', {})
  115. expected_errors({'Expected False but got []'})
  116. end)
  117. end)
  118. -- assert_true({actual}, [, {msg}])
  119. describe('assert_true', function()
  120. it('should not change v:errors when actual is true', function()
  121. eq(0, call('assert_true', 1))
  122. eq(0, call('assert_true', -1)) -- In Vim script, non-zero Numbers are TRUE.
  123. eq(0, call('assert_true', true))
  124. expected_empty()
  125. end)
  126. it('should change v:errors when actual is not true', function()
  127. eq(1, call('assert_true', 1.5))
  128. expected_errors({'Expected True but got 1.5'})
  129. end)
  130. end)
  131. describe('v:errors', function()
  132. it('should be initialized at startup', function()
  133. expected_empty()
  134. end)
  135. it('should have function names and relative line numbers', function()
  136. source([[
  137. fu Func_one()
  138. call assert_equal([0], {'0' : 0})
  139. call assert_false('False')
  140. call assert_true("True")
  141. endfu
  142. fu Func_two()
  143. " for shifting a line number
  144. call assert_true('line two')
  145. endfu
  146. ]])
  147. call('Func_one')
  148. call('Func_two')
  149. expected_errors({
  150. "function Func_one line 1: Expected [0] but got {'0': 0}",
  151. "function Func_one line 2: Expected False but got 'False'",
  152. "function Func_one line 3: Expected True but got 'True'",
  153. "function Func_two line 2: Expected True but got 'line two'",
  154. })
  155. end)
  156. it('should have file names and passed messages', function()
  157. local tmpname_one = source([[
  158. call assert_equal(1, 100, 'equal assertion failed')
  159. call assert_false('true', 'true assertion failed')
  160. call assert_true('false', 'false assertion failed')
  161. ]])
  162. local tmpname_two = source([[
  163. call assert_true('', 'file two')
  164. ]])
  165. expected_errors({
  166. tmpname_one .. " line 1: equal assertion failed: Expected 1 but got 100",
  167. tmpname_one .. " line 2: true assertion failed: Expected False but got 'true'",
  168. tmpname_one .. " line 3: false assertion failed: Expected True but got 'false'",
  169. tmpname_two .. " line 1: file two: Expected True but got ''",
  170. })
  171. end)
  172. it('is reset to a list by assert functions', function()
  173. source([[
  174. let save_verrors = v:errors
  175. let v:['errors'] = {'foo': 3}
  176. call assert_equal('yes', 'no')
  177. let verrors = v:errors
  178. let v:errors = save_verrors
  179. call assert_equal(type([]), type(verrors))
  180. ]])
  181. expected_empty()
  182. end)
  183. end)
  184. -- assert_match({pat}, {text}[, {msg}])
  185. describe('assert_match', function()
  186. it('should not change v:errors when pat matches text', function()
  187. call('assert_match', '^f.*b.*r$', 'foobar')
  188. expected_empty()
  189. end)
  190. it('should change v:errors when pat does not match text', function()
  191. call('assert_match', 'bar.*foo', 'foobar')
  192. expected_errors({"Pattern 'bar.*foo' does not match 'foobar'"})
  193. end)
  194. it('should set v:errors to msg when given and match fails', function()
  195. call('assert_match', 'bar.*foo', 'foobar', 'wrong')
  196. expected_errors({"wrong: Pattern 'bar.*foo' does not match 'foobar'"})
  197. end)
  198. end)
  199. -- assert_notmatch({pat}, {text}[, {msg}])
  200. describe('assert_notmatch', function()
  201. it('should not change v:errors when pat does not match text', function()
  202. call('assert_notmatch', 'foo', 'bar')
  203. call('assert_notmatch', '^foobar$', 'foobars')
  204. expected_empty()
  205. end)
  206. it('should change v:errors when pat matches text', function()
  207. call('assert_notmatch', 'foo', 'foobar')
  208. expected_errors({"Pattern 'foo' does match 'foobar'"})
  209. end)
  210. end)
  211. -- assert_fails({cmd}, [, {error}])
  212. describe('assert_fails', function()
  213. it('should change v:errors when error does not match v:errmsg', function()
  214. eq(1, eval([[assert_fails('xxx', 'E12345')]]))
  215. command([[call assert_match("Expected 'E12345' but got 'E492:", v:errors[0])]])
  216. expected_errors({"Expected 'E12345' but got 'E492: Not an editor command: xxx': xxx"})
  217. end)
  218. it('should not change v:errors when cmd errors', function()
  219. eq(0, eval([[assert_fails('NonexistentCmd')]]))
  220. expected_empty()
  221. end)
  222. it('should change v:errors when cmd succeeds', function()
  223. eq(1, eval([[assert_fails('call empty("")', '')]]))
  224. expected_errors({'command did not fail: call empty("")'})
  225. end)
  226. it('can specify and get a message about what failed', function()
  227. eq(1, eval([[assert_fails('xxx', 'E9876', 'stupid')]]))
  228. command([[call assert_match("stupid: Expected 'E9876' but got 'E492:", v:errors[0])]])
  229. expected_errors({"stupid: Expected 'E9876' but got 'E492: Not an editor command: xxx': stupid"})
  230. end)
  231. it('can specify and get a message even when cmd succeeds', function()
  232. eq(1, eval([[assert_fails('echo', '', 'echo command')]]))
  233. expected_errors({'command did not fail: echo command'})
  234. end)
  235. end)
  236. -- assert_inrange({lower}, {upper}, {actual}[, {msg}])
  237. describe('assert_inrange()', function()
  238. it('should not change v:errors when actual is in range', function()
  239. call('assert_inrange', 7, 7, 7)
  240. call('assert_inrange', 5, 7, 5)
  241. call('assert_inrange', 5, 7, 6)
  242. call('assert_inrange', 5, 7, 7)
  243. expected_empty()
  244. end)
  245. it('should change v:errors when actual is not in range', function()
  246. call('assert_inrange', 5, 7, 4)
  247. call('assert_inrange', 5, 7, 8)
  248. expected_errors({
  249. "Expected range 5 - 7, but got 4",
  250. "Expected range 5 - 7, but got 8",
  251. })
  252. end)
  253. it('assert_inrange(1, 1) returns E119', function()
  254. eq('Vim(call):E119: Not enough arguments for function: assert_inrange',
  255. exc_exec("call assert_inrange(1, 1)"))
  256. end)
  257. end)
  258. -- assert_report({msg})
  259. describe('assert_report()', function()
  260. it('should add a message to v:errors', function()
  261. eq(1, call('assert_report', 'something is wrong'))
  262. command("call assert_match('something is wrong', v:errors[0])")
  263. command('call remove(v:errors, 0)')
  264. expected_empty()
  265. end)
  266. end)
  267. -- assert_exception({cmd}, [, {error}])
  268. describe('assert_exception()', function()
  269. it('should assert thrown exceptions properly', function()
  270. source([[
  271. try
  272. nocommand
  273. catch
  274. call assert_equal(0, assert_exception('E492'))
  275. endtry
  276. ]])
  277. expected_empty()
  278. end)
  279. it('should work properly when nested', function()
  280. source([[
  281. try
  282. nocommand
  283. catch
  284. try
  285. " illegal argument, get NULL for error
  286. call assert_equal(1, assert_exception([]))
  287. catch
  288. call assert_equal(0, assert_exception('E730'))
  289. endtry
  290. endtry
  291. ]])
  292. expected_empty()
  293. end)
  294. end)
  295. end)