Commit e6be4d89 authored by Craig Barnes's avatar Craig Barnes

Improve error messages produced by test/compare.lua

parent e8232844
Pipeline #7618539 passed with stage
in 51 seconds
local ltcn = require "ltcn"
local type, pairs, assert = type, pairs, assert
local open, stderr, exit = io.open, io.stderr, os.exit
local setmetatable, concat = setmetatable, table.concat
local load = loadstring or load
local verbose = os.getenv "VERBOSE"
local filename = assert(arg[1], "arg[1] is nil; expected filename")
local _ENV = nil
local Stack = {}
Stack.__index = Stack
function Stack.new()
return setmetatable({length = 0}, Stack)
end
function Stack:push(v)
local n = self.length + 1
self.length = n
self[n] = v
end
function Stack:pop()
local n = self.length
self[n] = nil
self.length = n - 1
end
function Stack:tostring(k)
local n = self.length
if 0 >= n then
return ("[%q]"):format(k)
end
local buf = {}
for i = 1, n do
buf[i] = ("[%q]"):format(self[i])
end
buf[n+1] = ("[%q]"):format(k)
return concat(buf)
end
local keys = Stack.new()
local seen = {}
local function compare(t1, t2)
for k, v1 in pairs(t1) do
local v2 = t2[k]
if type(v1) == "table" then
assert(type(v2) == "table", "Not a table")
keys:push(k)
compare(v1, v2)
keys:pop()
elseif v1 ~= v2 then
local s = ("(%s, %s)"):format(v1, v2)
stderr:write(filename, ": Error: values not equal: ", s, "\n")
exit(1)
local index = keys:tostring(k)
if not seen[index] then
local msg = "%s: values at index %s not equal: (%s, %s)\n"
stderr:write(msg:format(filename, index, v1, v2))
seen[index] = true
end
elseif verbose then
stderr:write(("OK: %-15s %s\n"):format(v1, v2))
local index = keys:tostring(k)
stderr:write(("OK: %s\n"):format(index))
end
end
return true
......
......@@ -4,4 +4,8 @@
["\a\b\n\\'\""] = '"\n\t\r\\\r\n',
[0xff] = 0Xff, locals = 0x55, falsey = .5,
--[=[ foo=xyz ]] ]==] ]=] etc = 100, bla = [==[ ]=] ]==],
foo = {bar = {lol = {baz = {qux = {x = 1337}}}}},
etc = {{{{['"""[]][]'] = false}}}},
a = {b = {c = {d = {e = {{{1, 2, 3, true}}}}}}}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment