Switch from lunit to u-test
lunit is unmaintained since ~2011. Switch to u-test [1] instead which seems to be more actively maintained. [1]: https://github.com/IUdalov/u-test
This commit is contained in:
parent
0d852d7aac
commit
4a5349be96
20 changed files with 701 additions and 1335 deletions
23
tests/support/component.lua
Normal file
23
tests/support/component.lua
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
local network = require("network")
|
||||
|
||||
local modem = {}
|
||||
|
||||
function modem.open(port)
|
||||
-- Nothing
|
||||
end
|
||||
|
||||
function modem.send(addr, port, msg)
|
||||
network.send(addr, port, msg)
|
||||
end
|
||||
|
||||
function modem.close(port)
|
||||
-- Nothing
|
||||
end
|
||||
|
||||
function modem.broadcast(port, msg)
|
||||
network.broadcast(port, msg)
|
||||
end
|
||||
|
||||
return {
|
||||
modem = modem,
|
||||
}
|
||||
10
tests/support/computer.lua
Normal file
10
tests/support/computer.lua
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
local socket = require("socket")
|
||||
local computer = {}
|
||||
|
||||
local start = socket.gettime()
|
||||
|
||||
function computer.uptime()
|
||||
return socket.gettime() - start
|
||||
end
|
||||
|
||||
return computer
|
||||
39
tests/support/event.lua
Normal file
39
tests/support/event.lua
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
local network = require("network")
|
||||
local component = require("component")
|
||||
|
||||
local event = {}
|
||||
local addr_num = 0
|
||||
|
||||
function event.listen(event, callback)
|
||||
if event ~= "modem_message" then
|
||||
error("Event '"..event"' is not supported!")
|
||||
end
|
||||
addr = "A"..addr_num
|
||||
addr_num = addr_num + 1
|
||||
function ev_callback(ev, local_addr, ...)
|
||||
-- Inject local address so the check passes
|
||||
component.modem.address = local_addr
|
||||
callback(ev, local_addr, ...)
|
||||
end
|
||||
network.register(addr, ev_callback)
|
||||
end
|
||||
|
||||
function event.ignore(event, callback)
|
||||
if event ~= "modem_message" then
|
||||
error("Event '"..event"' is not supported!")
|
||||
end
|
||||
network.deregister(network.get_scene())
|
||||
end
|
||||
|
||||
function event.pullFiltered(timeout, filter)
|
||||
while true do
|
||||
local msg = table.pack(network.pull())
|
||||
if msg[1] == nil then
|
||||
return nil
|
||||
elseif filter(table.unpack(msg)) then
|
||||
return table.unpack(msg)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return event
|
||||
113
tests/support/network.lua
Normal file
113
tests/support/network.lua
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
local network = {
|
||||
allow_blackhole = false,
|
||||
}
|
||||
|
||||
local nodes = {}
|
||||
local active_node = {}
|
||||
local latest_message = {}
|
||||
local inject_message = {}
|
||||
|
||||
function network.register(addr, callback)
|
||||
if nodes[addr] ~= nil then
|
||||
error("Address "..addr.." already registered!")
|
||||
end
|
||||
nodes[addr] = callback
|
||||
|
||||
active_node = {}
|
||||
table.insert(active_node, addr)
|
||||
end
|
||||
|
||||
function network.deregister(addr)
|
||||
nodes[addr] = nil
|
||||
end
|
||||
|
||||
function network.get_callback(addr)
|
||||
return nodes[addr]
|
||||
end
|
||||
|
||||
function network.send(addr, port, msg)
|
||||
local callback = nodes[addr]
|
||||
if callback == nil then
|
||||
if not network.allow_blackhole then
|
||||
error("Send message to offline node: "..tostring(addr))
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local current_node = active_node[#active_node]
|
||||
-- Push target onto node-stack
|
||||
table.insert(active_node, addr)
|
||||
|
||||
-- print("Sending from "..current_node.." to "..addr..": "..msg)
|
||||
-- print(require("inspect").inspect(active_node))
|
||||
|
||||
latest_message[addr] = {
|
||||
from=current_node,
|
||||
port=port,
|
||||
msg=msg,
|
||||
}
|
||||
callback("modem_message", addr, current_node, port, 0, msg)
|
||||
|
||||
-- Remove again
|
||||
active_node[#active_node] = nil
|
||||
end
|
||||
|
||||
function network.broadcast(port, msg)
|
||||
local current_node = active_node[#active_node]
|
||||
-- print("Broadcasting from "..current_node)
|
||||
for addr in pairs(nodes) do
|
||||
if addr ~= current_node then
|
||||
network.send(addr, port, msg)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function network.pull()
|
||||
local current_node = active_node[#active_node]
|
||||
|
||||
if inject_message[current_node] ~= nil and #inject_message[current_node] > 0 then
|
||||
local message = inject_message[current_node][1]
|
||||
table.remove(inject_message[current_node], 1)
|
||||
|
||||
-- Actually send the injected message now
|
||||
-- but save the last latest_message
|
||||
local latest = latest_message[current_node]
|
||||
table.insert(active_node, message.from)
|
||||
network.send(current_node, message.port, message.msg)
|
||||
active_node[#active_node] = nil
|
||||
latest_message[current_node] = latest
|
||||
|
||||
return "modem_message", current_node, message.from, message.port, 0, message.msg
|
||||
end
|
||||
|
||||
if latest_message[current_node] == nil then
|
||||
return nil
|
||||
end
|
||||
local message = latest_message[current_node]
|
||||
|
||||
-- Delete message
|
||||
latest_message[current_node] = nil
|
||||
return "modem_message", current_node, message.from, message.port, 0, message.msg
|
||||
end
|
||||
|
||||
function network.set_scene(addr)
|
||||
active_node = {}
|
||||
table.insert(active_node, addr)
|
||||
end
|
||||
|
||||
function network.get_scene(addr)
|
||||
return active_node[#active_node]
|
||||
end
|
||||
|
||||
function network.inject(addr, from, port, msg)
|
||||
if inject_message[addr] == nil then
|
||||
inject_message[addr] = {}
|
||||
end
|
||||
table.insert(inject_message[addr], {
|
||||
from=from,
|
||||
port=port,
|
||||
msg=msg,
|
||||
})
|
||||
end
|
||||
|
||||
return network
|
||||
140
tests/support/serialization.lua
Normal file
140
tests/support/serialization.lua
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
local serialization = {}
|
||||
|
||||
-- delay loaded tables fail to deserialize cross [C] boundaries (such as when having to read files that cause yields)
|
||||
local local_pairs = function(tbl)
|
||||
local mt = getmetatable(tbl)
|
||||
return (mt and mt.__pairs or pairs)(tbl)
|
||||
end
|
||||
|
||||
-- Important: pretty formatting will allow presenting non-serializable values
|
||||
-- but may generate output that cannot be unserialized back.
|
||||
function serialization.serialize(value, pretty)
|
||||
local kw = {["and"]=true, ["break"]=true, ["do"]=true, ["else"]=true,
|
||||
["elseif"]=true, ["end"]=true, ["false"]=true, ["for"]=true,
|
||||
["function"]=true, ["goto"]=true, ["if"]=true, ["in"]=true,
|
||||
["local"]=true, ["nil"]=true, ["not"]=true, ["or"]=true,
|
||||
["repeat"]=true, ["return"]=true, ["then"]=true, ["true"]=true,
|
||||
["until"]=true, ["while"]=true}
|
||||
local id = "^[%a_][%w_]*$"
|
||||
local ts = {}
|
||||
local function s(v, l)
|
||||
local t = type(v)
|
||||
if t == "nil" then
|
||||
return "nil"
|
||||
elseif t == "boolean" then
|
||||
return v and "true" or "false"
|
||||
elseif t == "number" then
|
||||
if v ~= v then
|
||||
return "0/0"
|
||||
elseif v == math.huge then
|
||||
return "math.huge"
|
||||
elseif v == -math.huge then
|
||||
return "-math.huge"
|
||||
else
|
||||
return tostring(v)
|
||||
end
|
||||
elseif t == "string" then
|
||||
return string.format("%q", v):gsub("\\\n","\\n")
|
||||
elseif t == "table" and pretty and getmetatable(v) and getmetatable(v).__tostring then
|
||||
return tostring(v)
|
||||
elseif t == "table" then
|
||||
if ts[v] then
|
||||
if pretty then
|
||||
return "recursion"
|
||||
else
|
||||
error("tables with cycles are not supported")
|
||||
end
|
||||
end
|
||||
ts[v] = true
|
||||
local i, r = 1, nil
|
||||
local f
|
||||
if pretty then
|
||||
local ks, sks, oks = {}, {}, {}
|
||||
for k in local_pairs(v) do
|
||||
if type(k) == "number" then
|
||||
table.insert(ks, k)
|
||||
elseif type(k) == "string" then
|
||||
table.insert(sks, k)
|
||||
else
|
||||
table.insert(oks, k)
|
||||
end
|
||||
end
|
||||
table.sort(ks)
|
||||
table.sort(sks)
|
||||
for _, k in ipairs(sks) do
|
||||
table.insert(ks, k)
|
||||
end
|
||||
for _, k in ipairs(oks) do
|
||||
table.insert(ks, k)
|
||||
end
|
||||
local n = 0
|
||||
f = table.pack(function()
|
||||
n = n + 1
|
||||
local k = ks[n]
|
||||
if k ~= nil then
|
||||
return k, v[k]
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end)
|
||||
else
|
||||
f = table.pack(local_pairs(v))
|
||||
end
|
||||
for k, v in table.unpack(f) do
|
||||
if r then
|
||||
r = r .. "," .. (pretty and ("\n" .. string.rep(" ", l)) or "")
|
||||
else
|
||||
r = "{"
|
||||
end
|
||||
local tk = type(k)
|
||||
if tk == "number" and k == i then
|
||||
i = i + 1
|
||||
r = r .. s(v, l + 1)
|
||||
else
|
||||
if tk == "string" and not kw[k] and string.match(k, id) then
|
||||
r = r .. k
|
||||
else
|
||||
r = r .. "[" .. s(k, l + 1) .. "]"
|
||||
end
|
||||
r = r .. "=" .. s(v, l + 1)
|
||||
end
|
||||
end
|
||||
ts[v] = nil -- allow writing same table more than once
|
||||
return (r or "{") .. "}"
|
||||
else
|
||||
if pretty then
|
||||
return tostring(v)
|
||||
else
|
||||
error("unsupported type: " .. t)
|
||||
end
|
||||
end
|
||||
end
|
||||
local result = s(value, 1)
|
||||
local limit = type(pretty) == "number" and pretty or 10
|
||||
if pretty then
|
||||
local truncate = 0
|
||||
while limit > 0 and truncate do
|
||||
truncate = string.find(result, "\n", truncate + 1, true)
|
||||
limit = limit - 1
|
||||
end
|
||||
if truncate then
|
||||
return result:sub(1, truncate) .. "..."
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
function serialization.unserialize(data)
|
||||
-- checkArg(1, data, "string")
|
||||
local result, reason = load("return " .. data, "=data", _, {math={huge=math.huge}})
|
||||
if not result then
|
||||
return nil, reason
|
||||
end
|
||||
local ok, output = pcall(result)
|
||||
if not ok then
|
||||
return nil, output
|
||||
end
|
||||
return output
|
||||
end
|
||||
|
||||
return serialization
|
||||
30
tests/support/uuid.lua
Normal file
30
tests/support/uuid.lua
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
local bit32 = require("bit32")
|
||||
local uuid = {}
|
||||
|
||||
function uuid.next()
|
||||
-- e.g. 3c44c8a9-0613-46a2-ad33-97b6ba2e9d9a
|
||||
-- 8-4-4-4-12 (halved sizes because bytes make hex pairs)
|
||||
local sets = {4, 2, 2, 2, 6}
|
||||
local result = ""
|
||||
local pos = 0
|
||||
|
||||
for _,set in ipairs(sets) do
|
||||
if result:len() > 0 then
|
||||
result = result .. "-"
|
||||
end
|
||||
for _ = 1,set do
|
||||
local byte = math.random(0, 255)
|
||||
if pos == 6 then
|
||||
byte = bit32.bor(bit32.band(byte, 0x0F), 0x40)
|
||||
elseif pos == 8 then
|
||||
byte = bit32.bor(bit32.band(byte, 0x3F), 0x80)
|
||||
end
|
||||
result = result .. string.format("%02x", byte)
|
||||
pos = pos + 1
|
||||
end
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
return uuid
|
||||
Loading…
Add table
Add a link
Reference in a new issue