assert_spec.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local api, call = n.api, n.call
  4. local clear, eq = n.clear, t.eq
  5. local source, command = n.source, n.command
  6. local exc_exec = n.exc_exec
  7. local eval = n.eval
  8. local function expected_errors(errors)
  9. eq(errors, api.nvim_get_vvar('errors'))
  10. end
  11. local function expected_empty()
  12. eq({}, api.nvim_get_vvar('errors'))
  13. end
  14. describe('assert function:', function()
  15. before_each(function()
  16. clear()
  17. end)
  18. -- assert_equal({expected}, {actual}, [, {msg}])
  19. describe('assert_equal', function()
  20. it('should not change v:errors when expected is equal to actual', function()
  21. source([[
  22. fu Func()
  23. endfu
  24. let F1 = function('Func')
  25. let F2 = function('Func')
  26. call assert_equal(F1, F2)
  27. ]])
  28. expected_empty()
  29. end)
  30. it('should not change v:errors when expected is equal to actual', function()
  31. eq(0, call('assert_equal', '', ''))
  32. eq(0, call('assert_equal', 'string', 'string'))
  33. expected_empty()
  34. end)
  35. it('should change v:errors when expected is not equal to actual', function()
  36. eq(1, call('assert_equal', 0, { 0 }))
  37. expected_errors({ 'Expected 0 but got [0]' })
  38. end)
  39. it('should change v:errors when expected is not equal to actual', function()
  40. eq(1, call('assert_equal', 0, '0'))
  41. expected_errors({ "Expected 0 but got '0'" })
  42. end)
  43. it('should change v:errors when expected is not equal to actual', function()
  44. -- Lua does not tell integer from float.
  45. command('call assert_equal(1, 1.0)')
  46. expected_errors({ 'Expected 1 but got 1.0' })
  47. end)
  48. it('should change v:errors when expected is not equal to actual', function()
  49. call('assert_equal', 'true', 'false')
  50. expected_errors({ "Expected 'true' but got 'false'" })
  51. end)
  52. it('should change v:errors when expected is not equal to actual', function()
  53. source([[
  54. function CheckAssert()
  55. let s:v = {}
  56. let s:x = {"a": s:v}
  57. let s:v["b"] = s:x
  58. let s:w = {"c": s:x, "d": ''}
  59. call assert_equal(s:w, '')
  60. endfunction
  61. ]])
  62. eq(
  63. 'Vim(call):E724: unable to correctly dump variable with self-referencing container',
  64. exc_exec('call CheckAssert()')
  65. )
  66. end)
  67. end)
  68. -- assert_false({actual}, [, {msg}])
  69. describe('assert_false', function()
  70. it('should not change v:errors when actual is false', function()
  71. eq(0, call('assert_false', 0))
  72. eq(0, call('assert_false', false))
  73. expected_empty()
  74. end)
  75. it('should change v:errors when actual is not false', function()
  76. eq(1, call('assert_false', 1))
  77. expected_errors({ 'Expected False but got 1' })
  78. end)
  79. it('should change v:errors when actual is not false', function()
  80. call('assert_false', {})
  81. expected_errors({ 'Expected False but got []' })
  82. end)
  83. end)
  84. -- assert_true({actual}, [, {msg}])
  85. describe('assert_true', function()
  86. it('should not change v:errors when actual is true', function()
  87. eq(0, call('assert_true', 1))
  88. eq(0, call('assert_true', -1)) -- In Vim script, non-zero Numbers are TRUE.
  89. eq(0, call('assert_true', true))
  90. expected_empty()
  91. end)
  92. it('should change v:errors when actual is not true', function()
  93. eq(1, call('assert_true', 1.5))
  94. expected_errors({ 'Expected True but got 1.5' })
  95. end)
  96. end)
  97. describe('v:errors', function()
  98. it('should be initialized at startup', function()
  99. expected_empty()
  100. end)
  101. it('should have function names and relative line numbers', function()
  102. source([[
  103. fu Func_one()
  104. call assert_equal([0], {'0' : 0})
  105. call assert_false('False')
  106. call assert_true("True")
  107. endfu
  108. fu Func_two()
  109. " for shifting a line number
  110. call assert_true('line two')
  111. endfu
  112. ]])
  113. call('Func_one')
  114. call('Func_two')
  115. expected_errors({
  116. "function Func_one line 1: Expected [0] but got {'0': 0}",
  117. "function Func_one line 2: Expected False but got 'False'",
  118. "function Func_one line 3: Expected True but got 'True'",
  119. "function Func_two line 2: Expected True but got 'line two'",
  120. })
  121. end)
  122. it('should have file names and passed messages', function()
  123. source([[
  124. call assert_equal(1, 100, 'equal assertion failed')
  125. call assert_false('true', 'true assertion failed')
  126. call assert_true('false', 'false assertion failed')
  127. ]])
  128. source([[
  129. call assert_true('', 'file two')
  130. ]])
  131. expected_errors({
  132. 'nvim_exec2(): equal assertion failed: Expected 1 but got 100',
  133. "nvim_exec2(): true assertion failed: Expected False but got 'true'",
  134. "nvim_exec2(): false assertion failed: Expected True but got 'false'",
  135. "nvim_exec2(): file two: Expected True but got ''",
  136. })
  137. end)
  138. end)
  139. -- assert_fails({cmd}, [, {error}])
  140. describe('assert_fails', function()
  141. it('should not change v:errors when cmd errors', function()
  142. eq(0, eval([[assert_fails('NonexistentCmd')]]))
  143. expected_empty()
  144. end)
  145. it('should change v:errors when cmd succeeds', function()
  146. eq(1, eval([[assert_fails('call empty("")', '')]]))
  147. expected_errors({ 'command did not fail: call empty("")' })
  148. end)
  149. end)
  150. end)