Skip to content

Commit 7771244

Browse files
committed
use gambarina for testing
1 parent 940dee4 commit 7771244

File tree

8 files changed

+668
-556
lines changed

8 files changed

+668
-556
lines changed

test/dns_lookup_test.lua

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
local socket = require("ljsocket")
2+
local test = require("test.gambarina")
23

3-
do -- lookup google
4-
results = socket.find_address_info("www.google.com")
5-
assert(type(results) ~= nil)
6-
assert(#results ~= 0)
4+
test('DNS lookup for www.google.com', function()
5+
local results = socket.find_address_info("www.google.com")
6+
ok(type(results) ~= nil, "results should not be nil")
7+
ok(#results ~= 0, "results should not be empty")
78

89
for i, info in ipairs(results) do
9-
assert(info.host == "www.google.com")
10-
assert(info.family)
11-
assert(info.protocol)
10+
ok(info.host == "www.google.com", "host should be www.google.com")
11+
ok(info.family ~= nil, "family should be set")
12+
ok(info.protocol ~= nil, "protocol should be set")
1213
end
13-
end
14+
end)

test/gambarina.lua

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
-- Compatibility for Lua 5.1
2+
local unpack = unpack or table.unpack -- luacheck: ignore 143/table
3+
4+
local gambiarra = {
5+
_VERSION = 'gambiarra 0.4.1-0',
6+
_DESCRIPTION = 'A tiny lua unit-testing library.',
7+
_URL = 'https://codeberg.org/imo/gambiarra',
8+
_LICENSE = 'MIT',
9+
passed = 0,
10+
failed = 0,
11+
report = function(self)
12+
if self.failed == 0 then
13+
print('All ' .. self.passed .. ' tests passed.')
14+
else
15+
local total = self.passed + self.failed
16+
print(self.failed .. ' tests failed out of ' .. total)
17+
end
18+
end
19+
}
20+
21+
local function default_handler(e, test, msg)
22+
if e == 'pass' then
23+
gambiarra.passed = gambiarra.passed + 1
24+
print(('PASS %s: %s'):format(test, msg))
25+
elseif e == 'fail' then
26+
gambiarra.failed = gambiarra.failed + 1
27+
print(('FAIL %s: %s'):format(test, msg))
28+
elseif e == 'except' then
29+
gambiarra.failed = gambiarra.failed + 1
30+
print(('ECPT %s: %s'):format(test, msg))
31+
end
32+
end
33+
34+
local function deepeq(a, b)
35+
-- Different types: false
36+
if type(a) ~= type(b) then return false end
37+
-- Functions
38+
if type(a) == 'function' then
39+
return string.dump(a) == string.dump(b)
40+
end
41+
-- Primitives and equal pointers
42+
if a == b then return true end
43+
-- Only equal tables could have passed previous tests
44+
if type(a) ~= 'table' then return false end
45+
-- Compare tables size
46+
if #a ~= #b then return false end
47+
-- Compare tables field by field
48+
for k, v in pairs(a) do
49+
if b[k] == nil or not deepeq(v, b[k]) then return false end
50+
end
51+
return true
52+
end
53+
54+
-- Compatibility for Lua 5.1 and Lua 5.2
55+
local function args(...)
56+
return { n = select('#', ...), ... }
57+
end
58+
59+
local function spy(f)
60+
local s = {}
61+
s.called = {}
62+
setmetatable(s, { __call = function(_s, ...)
63+
local a = args(...)
64+
table.insert(_s.called, { ... })
65+
if f then
66+
local r = args(pcall(f, unpack(a, 1, a.n)))
67+
if not r[1] then
68+
_s.errors = _s.errors or {}
69+
_s.errors[#_s.called] = r[2]
70+
else
71+
return unpack(r, 2, r.n)
72+
end
73+
end
74+
end })
75+
return s
76+
end
77+
78+
local pendingtests = {}
79+
local env = _G
80+
local handler = default_handler
81+
82+
local function runpending()
83+
if pendingtests[1] ~= nil then pendingtests[1](runpending) end
84+
end
85+
86+
local function test_function(name, f, async)
87+
if type(name) == 'function' then
88+
handler = name
89+
env = f or _G
90+
return
91+
end
92+
93+
local function testfn(next)
94+
local prev = {
95+
ok = env.ok,
96+
spy = env.spy,
97+
eq = env.eq,
98+
}
99+
local exp, act
100+
101+
local function restore()
102+
env.ok = prev.ok
103+
env.spy = prev.spy
104+
env.eq = prev.eq
105+
handler('end', name)
106+
table.remove(pendingtests, 1)
107+
if next then next() end
108+
end
109+
110+
function env.ok(cond, msg)
111+
if not msg then
112+
local d = debug.getinfo(2, 'Sl')
113+
msg = d.short_src .. ':' .. d.currentline
114+
end
115+
if cond then
116+
handler('pass', name, msg)
117+
else
118+
handler('fail', name,
119+
('%s: Expected %q, but got %q.'):format(msg, tostring(exp), tostring(act)))
120+
end
121+
act, exp = nil, nil
122+
end
123+
124+
function env.eq(a, b)
125+
act, exp = a, b
126+
return deepeq(exp, act)
127+
end
128+
129+
env.spy = spy
130+
131+
handler('begin', name)
132+
local ok, err = pcall(f, restore)
133+
if not ok then
134+
handler('except', name, err)
135+
end
136+
137+
if not async then
138+
handler('end', name)
139+
env.ok = prev.ok
140+
env.spy = prev.spy
141+
env.eq = prev.eq
142+
end
143+
end
144+
145+
if not async then
146+
testfn()
147+
else
148+
table.insert(pendingtests, testfn)
149+
if #pendingtests == 1 then
150+
runpending()
151+
end
152+
end
153+
end
154+
155+
setmetatable(gambiarra,
156+
{ __call = function(_, name, f, async)
157+
return test_function(name, f, async)
158+
end })
159+
return gambiarra

0 commit comments

Comments
 (0)