lust.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. -- lust - Lua test framework
  2. -- https://github.com/bjornbytes/lust
  3. -- License - MIT, see LICENSE for details.
  4. local lust = {}
  5. lust.level = 0
  6. lust.passes = 0
  7. lust.errors = 0
  8. local red = string.char(27) .. '[31m'
  9. local green = string.char(27) .. '[32m'
  10. local normal = string.char(27) .. '[0m'
  11. function lust.describe(name, fn)
  12. print(string.rep('\t', lust.level) .. name)
  13. lust.level = lust.level + 1
  14. fn()
  15. lust.level = lust.level - 1
  16. end
  17. function lust.it(name, fn)
  18. if type(lust.onbefore) == 'function' then lust.onbefore(name) end
  19. local success, err = pcall(fn)
  20. if success then lust.passes = lust.passes + 1
  21. else lust.errors = lust.errors + 1 end
  22. local color = success and green or red
  23. local label = success and 'PASS' or 'FAIL'
  24. print(string.rep('\t', lust.level) .. color .. label .. normal .. ' ' .. name)
  25. if type(lust.onafter) == 'function' then lust.onafter(name) end
  26. end
  27. function lust.before(fn)
  28. assert(fn == nil or type(fn) == 'function', 'Must pass nil or a function to lust.before')
  29. lust.onbefore = fn
  30. end
  31. function lust.after(fn)
  32. assert(fn == nil or type(fn) == 'function', 'Must pass nil or a function to lust.after')
  33. lust.onafter = fn
  34. end
  35. -- Assertions
  36. local function isa(v, x)
  37. if type(x) == 'string' then return type(v) == x, tostring(v) .. ' is not a ' .. x
  38. elseif type(x) == 'table' then
  39. if type(v) ~= 'table' then return false, tostring(v) .. ' is not a ' .. tostring(x) end
  40. local seen = {}
  41. local meta = v
  42. while meta and not seen[meta] do
  43. if meta == x then return true end
  44. seen[meta] = true
  45. meta = getmetatable(meta) and getmetatable(meta).__index
  46. end
  47. return false, tostring(v) .. ' is not a ' .. tostring(x)
  48. end
  49. return false, 'invalid type ' .. tostring(x)
  50. end
  51. local function has(t, x)
  52. for k, v in pairs(t) do
  53. if v == x then return true end
  54. end
  55. return false
  56. end
  57. local function strict_eq(t1, t2)
  58. if type(t1) ~= type(t2) then return false end
  59. if type(t1) ~= 'table' then return t1 == t2 end
  60. if #t1 ~= #t2 then return false end
  61. for k, _ in pairs(t1) do
  62. if not strict_eq(t1[k], t2[k]) then return false end
  63. end
  64. for k, _ in pairs(t2) do
  65. if not strict_eq(t2[k], t1[k]) then return false end
  66. end
  67. return true
  68. end
  69. local paths = {
  70. [''] = {'to', 'to_not'},
  71. to = {'have', 'equal', 'be', 'exist', 'fail'},
  72. to_not = {'have', 'equal', 'be', 'exist', 'fail', chain = function(a) a.negate = not a.negate end},
  73. be = {'a', 'an', 'truthy', 'falsy', f = function(v, x)
  74. return v == x, tostring(v) .. ' and ' .. tostring(x) .. ' are not equal'
  75. end},
  76. a = {f = isa},
  77. an = {f = isa},
  78. exist = {f = function(v) return v ~= nil, tostring(v) .. ' is nil' end},
  79. truthy = {f = function(v) return v, tostring(v) .. ' is not truthy' end},
  80. falsy = {f = function(v) return not v, tostring(v) .. ' is not falsy' end},
  81. equal = {f = function(v, x) return strict_eq(v, x), tostring(v) .. ' and ' .. tostring(x) .. ' are not strictly equal' end},
  82. have = {
  83. f = function(v, x)
  84. if type(v) ~= 'table' then return false, 'table "' .. tostring(v) .. '" is not a table' end
  85. return has(v, x), 'table "' .. tostring(v) .. '" does not have ' .. tostring(x)
  86. end
  87. },
  88. fail = {f = function(v) return not pcall(v), tostring(v) .. ' did not fail' end}
  89. }
  90. function lust.expect(v)
  91. local assertion = {}
  92. assertion.val = v
  93. assertion.action = ''
  94. assertion.negate = false
  95. setmetatable(assertion, {
  96. __index = function(t, k)
  97. if has(paths[rawget(t, 'action')], k) then
  98. rawset(t, 'action', k)
  99. local chain = paths[rawget(t, 'action')].chain
  100. if chain then chain(t) end
  101. return t
  102. end
  103. return rawget(t, k)
  104. end,
  105. __call = function(t, ...)
  106. if paths[t.action].f then
  107. local res, err = paths[t.action].f(t.val, ...)
  108. if assertion.negate then res = not res end
  109. if not res then
  110. error(err or 'unknown failure', 2)
  111. end
  112. end
  113. end
  114. })
  115. return assertion
  116. end
  117. function lust.spy(subject, name, run)
  118. local spy = {}
  119. local original = subject[name]
  120. subject[name] = function(...)
  121. table.insert(spy, {...})
  122. return original(...)
  123. end
  124. setmetatable(spy, {__call = function(_, fn) fn() return spy end})
  125. if run then run() end
  126. return spy
  127. end
  128. lust.test = lust.it
  129. lust.paths = paths
  130. return lust