This repository has been archived by the owner on Sep 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assertion_factory.lua
81 lines (58 loc) · 1.8 KB
/
assertion_factory.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
local assertionFactory = {};
local assertionMeta = {};
function assertionFactory.new(callback)
local assertion = {};
local subAssertions = {};
setmetatable(assertion, assertionMeta);
local function getCallback()
return callback;
end;
function assertion.compose(joiningAssertion)
local composedAssertion = assertionFactory.new(function(...)
local originalStatus, originalType, originalMessage = assertion.run(...);
if (not originalStatus) then
return originalStatus, originalType, originalMessage;
end;
return joiningAssertion.run(...);
end);
local originalSubAssertions = assertion.getSubAssertions();
for index, callback in pairs(originalSubAssertions) do
composedAssertion.addSubAssertion(index, callback);
end;
local joiningSubAssertions = joiningAssertion.getSubAssertions();
for index, callback in pairs(joiningSubAssertions) do
composedAssertion.addSubAssertion(index, callback);
end;
return composedAssertion;
end;
function assertion.run(...)
local callback = getCallback();
return callback(...);
end;
function assertion.addSubAssertion(index, subAssertion)
if (type(subAssertion) == "function") then
subAssertion = flow.assertionFactory.new(subAssertion);
end;
subAssertions[index] = subAssertion;
end;
function assertion.removeSubAssertion(index)
subAssertions[index] = nil;
end;
function assertion.getSubAssertions()
return subAssertions;
end;
return assertion;
end;
function assertionMeta:__index(index, key)
local subAssertions = self.getSubAssertions();
if (subAssertions[index]) then
return self.compose(subAssertions[index]);
end;
end;
function assertionMeta:__call(...)
local status, exceptionType, message = self.run(...);
if (not status) then
flow.throw(exceptionType, message, 2);
end;
end;
return assertionFactory;