lust.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. -- lust v0.2.0 - Lua test framework
  2. -- https://github.com/bjornbytes/lust
  3. -- MIT LICENSE
  4. local lust = {}
  5. lust.level = 0
  6. lust.passes = 0
  7. lust.errors = 0
  8. lust.befores = {}
  9. lust.afters = {}
  10. local red = string.char(27) .. '[31m'
  11. local green = string.char(27) .. '[32m'
  12. local normal = string.char(27) .. '[0m'
  13. local function indent(level) return string.rep('\t', level or lust.level) end
  14. function lust.nocolor()
  15. red, green, normal = '', '', ''
  16. return lust
  17. end
  18. function lust.describe(name, fn)
  19. print(indent() .. name)
  20. lust.level = lust.level + 1
  21. fn()
  22. lust.befores[lust.level] = {}
  23. lust.afters[lust.level] = {}
  24. lust.level = lust.level - 1
  25. end
  26. function lust.it(name, fn)
  27. for level = 1, lust.level do
  28. if lust.befores[level] then
  29. for i = 1, #lust.befores[level] do
  30. lust.befores[level][i](name)
  31. end
  32. end
  33. end
  34. local success, err = pcall(fn)
  35. if success then lust.passes = lust.passes + 1
  36. else lust.errors = lust.errors + 1 end
  37. local color = success and green or red
  38. local label = success and 'PASS' or 'FAIL'
  39. print(indent() .. color .. label .. normal .. ' ' .. name)
  40. if err then
  41. print(indent(lust.level + 1) .. red .. tostring(err) .. normal)
  42. end
  43. for level = 1, lust.level do
  44. if lust.afters[level] then
  45. for i = 1, #lust.afters[level] do
  46. lust.afters[level][i](name)
  47. end
  48. end
  49. end
  50. end
  51. function lust.before(fn)
  52. lust.befores[lust.level] = lust.befores[lust.level] or {}
  53. table.insert(lust.befores[lust.level], fn)
  54. end
  55. function lust.after(fn)
  56. lust.afters[lust.level] = lust.afters[lust.level] or {}
  57. table.insert(lust.afters[lust.level], fn)
  58. end
  59. -- Assertions
  60. local function isa(v, x)
  61. if type(x) == 'string' then
  62. return type(v) == x,
  63. 'expected ' .. tostring(v) .. ' to be a ' .. x,
  64. 'expected ' .. tostring(v) .. ' to not be a ' .. x
  65. elseif type(x) == 'table' then
  66. if type(v) ~= 'table' then
  67. return false,
  68. 'expected ' .. tostring(v) .. ' to be a ' .. tostring(x),
  69. 'expected ' .. tostring(v) .. ' to not be a ' .. tostring(x)
  70. end
  71. local seen = {}
  72. local meta = v
  73. while meta and not seen[meta] do
  74. if meta == x then return true end
  75. seen[meta] = true
  76. meta = getmetatable(meta) and getmetatable(meta).__index
  77. end
  78. return false,
  79. 'expected ' .. tostring(v) .. ' to be a ' .. tostring(x),
  80. 'expected ' .. tostring(v) .. ' to not be a ' .. tostring(x)
  81. end
  82. error('invalid type ' .. tostring(x))
  83. end
  84. local function has(t, x)
  85. for k, v in pairs(t) do
  86. if v == x then return true end
  87. end
  88. return false
  89. end
  90. local function strict_eq(t1, t2)
  91. if type(t1) ~= type(t2) then return false end
  92. if type(t1) ~= 'table' then return t1 == t2 end
  93. for k, _ in pairs(t1) do
  94. if not strict_eq(t1[k], t2[k]) then return false end
  95. end
  96. for k, _ in pairs(t2) do
  97. if not strict_eq(t2[k], t1[k]) then return false end
  98. end
  99. return true
  100. end
  101. local paths = {
  102. [''] = { 'to', 'to_not' },
  103. to = { 'have', 'equal', 'be', 'exist', 'fail', 'match' },
  104. to_not = { 'have', 'equal', 'be', 'exist', 'fail', 'match', chain = function(a) a.negate = not a.negate end },
  105. a = { test = isa },
  106. an = { test = isa },
  107. be = { 'a', 'an', 'truthy',
  108. test = function(v, x)
  109. return v == x,
  110. 'expected ' .. tostring(v) .. ' and ' .. tostring(x) .. ' to be equal',
  111. 'expected ' .. tostring(v) .. ' and ' .. tostring(x) .. ' to not be equal'
  112. end
  113. },
  114. exist = {
  115. test = function(v)
  116. return v ~= nil,
  117. 'expected ' .. tostring(v) .. ' to exist',
  118. 'expected ' .. tostring(v) .. ' to not exist'
  119. end
  120. },
  121. truthy = {
  122. test = function(v)
  123. return v,
  124. 'expected ' .. tostring(v) .. ' to be truthy',
  125. 'expected ' .. tostring(v) .. ' to not be truthy'
  126. end
  127. },
  128. equal = {
  129. test = function(v, x)
  130. return strict_eq(v, x),
  131. 'expected ' .. tostring(v) .. ' and ' .. tostring(x) .. ' to be exactly equal',
  132. 'expected ' .. tostring(v) .. ' and ' .. tostring(x) .. ' to not be exactly equal'
  133. end
  134. },
  135. have = {
  136. test = function(v, x)
  137. if type(v) ~= 'table' then
  138. error('expected ' .. tostring(v) .. ' to be a table')
  139. end
  140. return has(v, x),
  141. 'expected ' .. tostring(v) .. ' to contain ' .. tostring(x),
  142. 'expected ' .. tostring(v) .. ' to not contain ' .. tostring(x)
  143. end
  144. },
  145. fail = {
  146. test = function(v)
  147. return not pcall(v),
  148. 'expected ' .. tostring(v) .. ' to fail',
  149. 'expected ' .. tostring(v) .. ' to not fail'
  150. end
  151. },
  152. match = {
  153. test = function(v, p)
  154. if type(v) ~= 'string' then v = tostring(v) end
  155. local result = string.find(v, p)
  156. return result ~= nil,
  157. 'expected ' .. v .. ' to match pattern [[' .. p .. ']]',
  158. 'expected ' .. v .. ' to not match pattern [[' .. p .. ']]'
  159. end
  160. },
  161. }
  162. function lust.expect(v)
  163. local assertion = {}
  164. assertion.val = v
  165. assertion.action = ''
  166. assertion.negate = false
  167. setmetatable(assertion, {
  168. __index = function(t, k)
  169. if has(paths[rawget(t, 'action')], k) then
  170. rawset(t, 'action', k)
  171. local chain = paths[rawget(t, 'action')].chain
  172. if chain then chain(t) end
  173. return t
  174. end
  175. return rawget(t, k)
  176. end,
  177. __call = function(t, ...)
  178. if paths[t.action].test then
  179. local res, err, nerr = paths[t.action].test(t.val, ...)
  180. if assertion.negate then
  181. res = not res
  182. err = nerr or err
  183. end
  184. if not res then
  185. error(err or 'unknown failure', 2)
  186. end
  187. end
  188. end
  189. })
  190. return assertion
  191. end
  192. function lust.spy(target, name, run)
  193. local spy = {}
  194. local subject
  195. local function capture(...)
  196. table.insert(spy, {...})
  197. return subject(...)
  198. end
  199. if type(target) == 'table' then
  200. subject = target[name]
  201. target[name] = capture
  202. else
  203. run = name
  204. subject = target or function() end
  205. end
  206. setmetatable(spy, {__call = function(_, ...) return capture(...) end})
  207. if run then run() end
  208. return spy
  209. end
  210. lust.test = lust.it
  211. lust.paths = paths
  212. return lust