forked from Garoze/testLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.lua
More file actions
86 lines (79 loc) · 3.49 KB
/
interface.lua
File metadata and controls
86 lines (79 loc) · 3.49 KB
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
-- function name(arg) *1 -> name {} -> f({}) --
local GlobalInterfaces = {}
local interfaceCallMeta = { __call = function(self, ...) return self:validateInterface(self,...) end }
function interfaceExists(name)
return GlobalInterfaces[name] ~= nil or false
end
function isInterfaceObject(interface)
return interface.__type == 'interfaceObject' or false
end
function getInterfaceObject(name)
if interfaceExists(name) == true then
return GlobalInterfaces[name]
end
return false
end
function isValidInterface(interfaceTable)
local errors = {}
for field, v in pairs(interfaceTable) do
if type(field) ~= 'string' or type(v) ~= 'string' then
table.insert(errors, field)
end
end
return #errors == 0 or false
end
function Interface(name, interfaceTable)
assert(type(name) == 'string', '[createInterface] - Expected a \'string\' on #1 argument and got '..type(name))
assert(type(interfaceTable) == 'table', '[createInterface] - Expected a \'table\' on #2 argument and got '..type(interfaceTable))
if interfaceExists(name) == false then
assert(isValidInterface(interfaceTable) == true, '[Interface] - Invalid interface construction, please make sure that every field has a \'string\' value.')
GlobalInterfaces[name] = {
__name = name,
__type = 'interfaceObject',
interface = interfaceTable,
validateInterface = function(self, validateTable)
assert(type(validateTable) == 'table', '[validateInterface] - Expected a \'table\' on #1 argument and got a '..type(validateTable))
if self:hasSameMethods(validateTable) == true then
return self:hasSameTypes(validateTable) == true or self:getDiffTypes(validateTable)
else self:getMissingFields(validateTable) end
end,
hasSameMethods = function(self, validateTable)
for field, v in pairs(self.interface) do
if validateTable[field] == nil then
return false
end
end
return true
end,
hasSameTypes = function(self, validateTable)
for field, v in pairs(self.interface) do
if type(validateTable[field]) ~= v then
return false
end
end
return true
end,
getDiffMethods = function(self, validateTable)
for field, v in pairs(self.interface) do
if validateTable[field] == nil then
print('[Interface] - Missing Field: '..field)
end
end
end,
getDiffTypes = function(self, validateTable)
for field, v in pairs(self.interface) do
if type(validateTable[field]) ~= v then
print('[getDiffTypes] - Field: '..field..' expected \''..v..'\' and got ' ..type(validateTable[field]))
end
end
end,
getMissingFields = function(self, validateTable)
for field, v in pairs(self.interface) do
if validateTable[field] == nil then
print('[getMissingFields] - Missing field: '..field)
end
end
end,
}
else return getInterfaceObject(name):validateInterface(interfaceTable) end
end