| 1 |
-- vim: et:ts=2:sw=2 |
| 2 |
-- Helper variables and functions |
| 3 |
local get_info = debug.getinfo |
| 4 |
local pcall = pcall |
| 5 |
local slice = string.sub |
| 6 |
local sprintf = string.format |
| 7 |
local str_find = string.find |
| 8 |
local tonumber = tonumber |
| 9 |
-- Lua 5.3 moved unpack to table.unpack |
| 10 |
local unpack = unpack or table.unpack |
| 11 |
local write = io.write |
| 12 |
local rawget = rawget |
| 13 |
local getmetatable = getmetatable |
| 14 |
local exit = os.exit |
| 15 |
local krprint = require("krprint") |
| 16 |
|
| 17 |
---- Helper methods |
| 18 |
|
| 19 |
--- C-like printf method |
| 20 |
local printf = function(fmt, ...) |
| 21 |
write(sprintf(fmt, ...)) |
| 22 |
end |
| 23 |
|
| 24 |
local printdiff = function(func_name, actual, expected) |
| 25 |
printf("Assertion %s() failed for test below (marked 'not ok'):\n", func_name) |
| 26 |
printf("Expected: %s\n", krprint.pprint(expected)) |
| 27 |
printf("Got: %s\n", krprint.pprint(actual)) |
| 28 |
end |
| 29 |
|
| 30 |
--- Compare potentially complex tables or objects |
| 31 |
-- |
| 32 |
-- Ideas here are taken from [Penlight][p], [Underscore][u], [cwtest][cw], and |
| 33 |
-- [luassert][l]. |
| 34 |
-- [p]: https://github.com/stevedonovan/Penlight |
| 35 |
-- [u]: https://github.com/mirven/underscore.lua |
| 36 |
-- [cw]: https://github.com/catwell/cwtest |
| 37 |
-- [l]: https://github.com/Olivine-Labs/luassert |
| 38 |
-- |
| 39 |
-- Predeclare both function names |
| 40 |
local keyvaluesame |
| 41 |
local deepsame |
| 42 |
-- |
| 43 |
--- keyvaluesame(table, table) => true or false |
| 44 |
-- Helper method to compare all the keys and values of a table |
| 45 |
keyvaluesame = function (t1, t2) |
| 46 |
for k1, v1 in pairs(t1) do |
| 47 |
local v2 = t2[k1] |
| 48 |
if v2 == nil or not deepsame(v1, v2) then return false end |
| 49 |
end |
| 50 |
|
| 51 |
-- Check for any keys present in t2 but not t1 |
| 52 |
for k2, _ in pairs(t2) do |
| 53 |
if t1[k2] == nil then return false end |
| 54 |
end |
| 55 |
|
| 56 |
return true |
| 57 |
end |
| 58 |
-- |
| 59 |
--- deepsame(item, item) => true or false |
| 60 |
-- Compare two items of any type for identity |
| 61 |
deepsame = function (t1, t2) |
| 62 |
local ty1, ty2 = type(t1), type(t2) |
| 63 |
if ty1 ~= ty2 then return false end |
| 64 |
if ty1 ~= 'table' then return t1 == t2 end |
| 65 |
|
| 66 |
-- If ._eq is found, use == and end quickly. |
| 67 |
-- As of Lua 5.3 == only cares if **one** of the two items has a __eq |
| 68 |
-- metamethod. Penlight, underscore and cwtest take the same approach, |
| 69 |
-- so I will as well. |
| 70 |
local eq = rawget(getmetatable(t1) or {}, '__eq') |
| 71 |
if (type(eq) == 'function') then |
| 72 |
return not not eq(t1, t2) |
| 73 |
else |
| 74 |
return keyvaluesame(t1, t2) |
| 75 |
end |
| 76 |
end |
| 77 |
|
| 78 |
---- tapered test suite |
| 79 |
|
| 80 |
local exit_status = 0 |
| 81 |
local test_count = 0 |
| 82 |
local debug_level = 3 |
| 83 |
|
| 84 |
-- All other tests are defined in terms of this primitive, which is |
| 85 |
-- kept private. |
| 86 |
local _test = function (exp, msg) |
| 87 |
test_count = test_count + 1 |
| 88 |
|
| 89 |
if msg then |
| 90 |
msg = sprintf(" - %s", msg) |
| 91 |
else |
| 92 |
msg = '' |
| 93 |
end |
| 94 |
|
| 95 |
if exp then |
| 96 |
printf("ok %s%s\n", test_count, msg) |
| 97 |
else |
| 98 |
exit_status = 1 + exit_status |
| 99 |
printf("not ok %s%s\n", test_count, msg) |
| 100 |
local info = get_info(debug_level) |
| 101 |
printf("# Trouble in %s around line %s\n", |
| 102 |
slice(info.source, 2), info.currentline) |
| 103 |
end |
| 104 |
end |
| 105 |
|
| 106 |
local ok = function (expression, msg) |
| 107 |
_test(expression, msg) |
| 108 |
end |
| 109 |
|
| 110 |
local nok = function (expression, msg) |
| 111 |
_test(not expression, msg) |
| 112 |
end |
| 113 |
|
| 114 |
local is = function (actual, expected, msg) |
| 115 |
local result = actual == expected; |
| 116 |
if not result then |
| 117 |
printdiff("is", actual, expected) |
| 118 |
end |
| 119 |
_test(result, msg) |
| 120 |
end |
| 121 |
|
| 122 |
local isnt = function (actual, expected, msg) |
| 123 |
_test(actual ~= expected, msg) |
| 124 |
end |
| 125 |
|
| 126 |
local same = function (actual, expected, msg) |
| 127 |
local result = deepsame(actual, expected) |
| 128 |
if not result then |
| 129 |
printdiff("same", actual, expected) |
| 130 |
end |
| 131 |
_test(result, msg) |
| 132 |
end |
| 133 |
|
| 134 |
local like = function (str, pattern, msg) |
| 135 |
_test(str_find(str, pattern), msg) |
| 136 |
end |
| 137 |
|
| 138 |
local unlike = function (str, pattern, msg) |
| 139 |
_test(not str_find(str, pattern), msg) |
| 140 |
end |
| 141 |
|
| 142 |
local pass = function (msg) |
| 143 |
_test(true, msg) |
| 144 |
end |
| 145 |
|
| 146 |
local fail = function (msg) |
| 147 |
_test(false, msg) |
| 148 |
end |
| 149 |
|
| 150 |
local boom = function (func, args, msg) |
| 151 |
local err, errmsg |
| 152 |
err, errmsg = pcall(func, unpack(args)) |
| 153 |
_test(not err, msg) |
| 154 |
if not err and type(errmsg) == 'string' then |
| 155 |
printf('# Got this error: "%s"\n', errmsg) |
| 156 |
end |
| 157 |
end |
| 158 |
|
| 159 |
local done = function (n) |
| 160 |
local expected = tonumber(n) |
| 161 |
if not expected or test_count == expected then |
| 162 |
printf('1..%d\n', test_count) |
| 163 |
elseif expected ~= test_count then |
| 164 |
exit_status = 1 + exit_status |
| 165 |
local s |
| 166 |
if expected == 1 then s = '' else s = 's' end |
| 167 |
printf("# Bad plan. You planned %d test%s but ran %d\n", |
| 168 |
expected, s, test_count) |
| 169 |
end |
| 170 |
exit(exit_status) |
| 171 |
end |
| 172 |
|
| 173 |
local version = function () |
| 174 |
return "2.3.0" |
| 175 |
end |
| 176 |
|
| 177 |
local author = function () |
| 178 |
return "Peter Aronoff" |
| 179 |
end |
| 180 |
|
| 181 |
local url = function () |
| 182 |
return "https://github.com/telemachus/tapered" |
| 183 |
end |
| 184 |
|
| 185 |
local license = function () |
| 186 |
return "BSD 3-Clause" |
| 187 |
end |
| 188 |
|
| 189 |
return { |
| 190 |
ok = ok, |
| 191 |
nok = nok, |
| 192 |
is = is, |
| 193 |
isnt = isnt, |
| 194 |
same = same, |
| 195 |
like = like, |
| 196 |
unlike = unlike, |
| 197 |
pass = pass, |
| 198 |
fail = fail, |
| 199 |
boom = boom, |
| 200 |
done = done, |
| 201 |
version = version, |
| 202 |
author = author, |
| 203 |
url = url, |
| 204 |
license = license |
| 205 |
} |