-
Notifications
You must be signed in to change notification settings - Fork 16
/
Debug.lua
executable file
·118 lines (109 loc) · 3.19 KB
/
Debug.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
local PitBull4 = select(2, ...)
PitBull4.DEBUG = true
local function is_list(t)
local n = #t
for k in pairs(t) do
if type(k) ~= "number" or k < 1 or k > n or math.floor(k) ~= k then
return false
end
end
return true
end
local function simple_pretty_tostring(value)
if type(value) == "string" then
return ("%q"):format(value)
else
return tostring(value)
end
end
local function pretty_tostring(value)
if type(value) ~= "table" then
return simple_pretty_tostring(value)
end
local t = {}
if is_list(value) then
for _, v in ipairs(value) do
t[#t+1] = simple_pretty_tostring(v)
end
else
for k, v in pairs(value) do
t[#t+1] = "[" .. simple_pretty_tostring(k) .. "] = " .. simple_pretty_tostring(v)
end
end
return "{" .. table.concat(t, ", ") .. "}"
end
local conditions = {}
local function helper(alpha, ...)
for i = 1, select('#', ...) do
if alpha == select(i, ...) then
return true
end
end
return false
end
conditions['inset'] = function(alpha, bravo)
if type(bravo) == "table" then
return bravo[alpha] ~= nil
elseif type(bravo) == "string" then
return helper(alpha, (";"):split(bravo))
else
error(("Bad argument #3 to `expect'. Expected %q or %q, got %q"):format("table", "string", type(bravo)))
end
end
conditions['typeof'] = function(alpha, bravo)
local type_alpha = type(alpha)
if type_alpha == "table" and type(rawget(alpha, 0)) == "userdata" and type(alpha.IsObjectType) == "function" then
type_alpha = 'frame'
end
return conditions['inset'](type_alpha, bravo)
end
conditions['frametype'] = function(alpha, bravo)
if type(bravo) ~= "string" then
error(("Bad argument #3 to `expect'. Expected %q, got %q"):format("string", type(bravo)), 3)
end
return type(alpha) == "table" and type(rawget(alpha, 0)) == "userdata" and type(alpha.IsObjectType) == "function" and alpha:IsObjectType(bravo)
end
conditions['match'] = function(alpha, bravo)
if type(alpha) ~= "string" then
error(("Bad argument #1 to `expect'. Expected %q, got %q"):format("string", type(alpha)), 3)
end
if type(bravo) ~= "string" then
error(("Bad argument #3 to `expect'. Expected %q, got %q"):format("string", type(bravo)), 3)
end
return alpha:match(bravo)
end
conditions['=='] = function(alpha, bravo)
return alpha == bravo
end
conditions['~='] = function(alpha, bravo)
return alpha ~= bravo
end
conditions['>'] = function(alpha, bravo)
return type(alpha) == type(bravo) and alpha > bravo
end
conditions['>='] = function(alpha, bravo)
return type(alpha) == type(bravo) and alpha >= bravo
end
conditions['<'] = function(alpha, bravo)
return type(alpha) == type(bravo) and alpha < bravo
end
conditions['<='] = function(alpha, bravo)
return type(alpha) == type(bravo) and alpha <= bravo
end
local t = {}
for k, v in pairs(conditions) do
t[#t+1] = k
end
for _, k in ipairs(t) do
conditions["not_" .. k] = function(alpha, bravo)
return not conditions[k](alpha, bravo)
end
end
function PitBull4.expect(alpha, condition, bravo)
if not conditions[condition] then
error(("Unknown condition %s"):format(pretty_tostring(condition)), 2)
end
if not conditions[condition](alpha, bravo) then
error(("Expectation failed: %s %s %s"):format(pretty_tostring(alpha), condition, pretty_tostring(bravo)), 2)
end
end