/
mispec.lua
134 lines (119 loc) · 3.68 KB
/
mispec.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
local moduleName = ... or 'mispec'
local M = {}
_G[moduleName] = M
-- Helpers:
function ok(expression, desc)
if expression == nil then expression = true end
desc = desc or 'expression is not ok'
if not expression then
error(desc .. '\n' .. debug.traceback())
end
end
function ko(expression, desc)
if expression == nil then expression = true end
desc = desc or 'expression is not ko'
if expression then
error(desc .. '\n' .. debug.traceback())
end
end
function eq(a, b)
if type(a) ~= type(b) then
error('type ' .. type(a) .. ' is not equal to ' .. type(b) .. '\n' .. debug.traceback())
end
if type(a) == 'function' then
return string.dump(a) == string.dump(b)
end
if a == b then return true end
if type(a) ~= 'table' then
error(tostring(a) .. ' is not equal to ' .. tostring(b) .. '\n' .. debug.traceback())
end
for k,v in pairs(a) do
if b[k] == nil or not eq(v, b[k]) then return false end
end
for k,v in pairs(b) do
if a[k] == nil or not eq(v, a[k]) then return false end
end
return true
end
local function eventuallyImpl(func, retries, delayMs)
local prevEventually = _G.eventually
_G.eventually = function() error("Cannot nest eventually/andThen.") end
local status, err = pcall(func)
_G.eventually = prevEventually
if status then
M.queuedEventuallyCount = M.queuedEventuallyCount - 1
M.runNextPending()
else
if retries > 0 then
local t = tmr.create()
t:register(delayMs, tmr.ALARM_SINGLE, M.runNextPending)
t:start()
table.insert(M.pending, 1, function() eventuallyImpl(func, retries - 1, delayMs) end)
else
M.failed = M.failed + 1
print("\n ! it failed:", err)
-- remove all pending eventuallies as spec has failed at this point
for i = 1, M.queuedEventuallyCount - 1 do
table.remove(M.pending, 1)
end
M.queuedEventuallyCount = 0
M.runNextPending()
end
end
end
function eventually(func, retries, delayMs)
retries = retries or 10
delayMs = delayMs or 300
M.queuedEventuallyCount = M.queuedEventuallyCount + 1
table.insert(M.pending, M.queuedEventuallyCount, function()
eventuallyImpl(func, retries, delayMs)
end)
end
function andThen(func)
eventually(func, 0, 0)
end
function describe(name, itshoulds)
M.name = name
M.itshoulds = itshoulds
end
-- Module:
M.runNextPending = function()
local next = table.remove(M.pending, 1)
if next then
node.task.post(next)
next = nil
else
M.succeeded = M.total - M.failed
local elapsedSeconds = (tmr.now() - M.startTime) / 1000 / 1000
print(string.format(
'\n\nCompleted in %.2f seconds.\nSuccess rate is %.1f%% (%d failed out of %d).',
elapsedSeconds, 100 * M.succeeded / M.total, M.failed, M.total))
M.pending = nil
M.queuedEventuallyCount = nil
end
end
M.run = function()
M.pending = {}
M.queuedEventuallyCount = 0
M.startTime = tmr.now()
M.total = 0
M.failed = 0
local it = {}
it.should = function(_, desc, func)
table.insert(M.pending, function()
uart.write(0, '\n * ' .. desc)
M.total = M.total + 1
local status, err = pcall(func)
if not status then
print("\n ! it failed:", err)
M.failed = M.failed + 1
end
M.runNextPending()
end)
end
M.itshoulds(it)
print('' .. M.name .. ', it should:')
M.runNextPending()
M.itshoulds = nil
M.name = nil
end