Made real pretty.

This commit is contained in:
Bruce Hill 2018-02-09 03:45:31 -08:00
parent e1677d2195
commit 1e05aa9cfd

172
test.lua
View File

@ -1,71 +1,115 @@
local immutable = require"immutable"
local bright = string.char(27).."[1m" local bright = string.char(27).."[1m"
local blue = string.char(27).."[34m" local dim = string.char(27).."[2m"
local underscore = string.char(27).."[4m"
local red = string.char(27).."[31m"
local green = string.char(27).."[32m"
local reset = string.char(27).."[0m" local reset = string.char(27).."[0m"
local function say(...) io.write(bright..blue); print(...); io.write(reset); end print(bright..underscore.."\nTesting with ".._VERSION..":"..reset)
local function repr(t)
local tmp = {}
for k,v in pairs(t) do tmp[#tmp+1] = ("%s=%s"):format(k,v) end
return "{"..table.concat(tmp, ", ").."}"
end
local Vec = immutable({"x","y"}, { local num_errors = 0
len2=function(self) local test_number = 0
return self.x*self.x + self.y*self.y local function test(description, fn)
end, test_number = test_number + 1
}, { description = (bright.."% 3d. "..reset.."%s"):format(test_number, description)
__add=function(self, other) io.write(description)
local cls = getmetatable(self) io.write(red)
return cls(self.x+other.x, self.y+other.y) local ok, err = pcall(fn)
end, if not ok then
__tostring=function(self) io.write(reset..dim.."\r...............................")
return "Vec("..tostring(self.x)..", "..tostring(self.y)..")" io.write(reset.."["..bright..red.."FAILED"..reset.."]\r")
end, io.write("\r"..description.."\n"..reset)
}) print(reset..red..(err or "")..reset)
num_errors = num_errors + 1
local v = Vec(1,3) else
say(v) io.write(reset..dim.."\r...............................")
local also_v = Vec(1,3) io.write(reset.."["..green.."PASSED"..reset.."]\r")
say(also_v) io.write(description.."\n")
-- Hash collision right now
local not_v = Vec(true,3)
say(not_v)
local also_not_v = Vec(true,3)
say(also_not_v)
say(v == also_v)
say(v + Vec(5,6))
collectgarbage()
say(Vec("hello", "world"))
say(Vec("hello", "world"))
collectgarbage()
collectgarbage()
say("Let's look at the instances")
for h,b in pairs(Vec.__instances) do
say(("Hash bucket: 0x%x"):format(h))
for k,v in pairs(b) do
local tmp = {}
for kk,vv in pairs(v) do tmp[#tmp+1] = tostring(vv) end
say(' <'..table.concat(tmp, ", ")..'>')
end end
end end
local FooVec = immutable({"x","y"}, { test("Loading module", function()
len2=function(self) immutable = require"immutable"
return self.x*self.x + self.y*self.y end)
end,
}, {
__add=function(self, other)
local cls = getmetatable(self)
return cls(self.x+other.x, self.y+other.y)
end,
__tostring=function(self)
return "Vec("..tostring(self.x)..", "..tostring(self.y)..")"
end,
})
say("FooVec: "..tostring(FooVec(1,3))) test("Creating class", function()
say("FooVec == Vec: "..tostring(FooVec(1,3) == Vec(1,3))) Vec = immutable({"x","y"}, {
say("FooVec:len2() = "..tostring(FooVec(1,3):len2())) len2=function(self)
return self.x*self.x + self.y*self.y
end,
}, {
__add=function(self, other)
local cls = getmetatable(self)
return cls(self.x+other.x, self.y+other.y)
end,
__tostring=function(self)
return "Vec("..tostring(self.x)..", "..tostring(self.y)..")"
end,
})
end)
test("Instantiating class", function()
v = assert(Vec(1,3))
end)
test("Testing method", function()
assert(v:len2() == 10)
end)
test("Testing tostring", function()
assert(tostring(v) == "Vec(1, 3)")
end)
test("Testing singletons", function()
also_v = Vec(1,3)
assert(v == also_v)
assert(tostring(v) == tostring(also_v))
-- Hash collision in the current implementation
not_v = Vec(true,3)
assert(v ~= not_v)
assert(tostring(not_v) == "Vec(true, 3)")
also_not_v = Vec(true,3)
assert(not_v == also_not_v)
end)
test("Testing __add metamethod", function()
assert(v + Vec(5,6) == Vec(6,9))
end)
collectgarbage()
test("Testing string members", function()
Vec("hello", "world")
end)
test("Testing table members", function()
Vec({}, {})
end)
test("Testing function members", function()
Vec(function() end, function() end)
end)
test("Testing immutable members", function()
Vec(v, v)
end)
collectgarbage()
collectgarbage()
test("Testing similar class", function()
local FooVec = immutable({"x","y"}, {
len2=function(self)
return self.x*self.x + self.y*self.y
end,
}, {
__add=function(self, other)
local cls = getmetatable(self)
return cls(self.x+other.x, self.y+other.y)
end,
__tostring=function(self)
return "Vec("..tostring(self.x)..", "..tostring(self.y)..")"
end,
})
assert(FooVec(1,1) ~= Vec(1,1))
end)
if num_errors == 0 then
print(green.."All tests passed!"..reset)
else
print(bright..red.."*** "..tostring(num_errors).." test"..(num_errors > 1 and "s" or "").." failed! ***"..reset)
io.write(reset)
os.exit(false, true)
end