Files
integration/luprex/core/lua/ut-tablecmp.lua

146 lines
4.6 KiB
Lua
Raw Normal View History

2021-08-23 23:34:30 -04:00
-- the tdc function calculates diffs, and returns those
-- diffs as a human-readable string.
2021-08-23 23:34:30 -04:00
local tdc = table.diffcompare
-- the tda function calculates diffs, applies the diffs to the second
-- table, and then returns true if the second table equals the first.
local tda = table.diffapply
function unittests.diffcompare()
2021-09-01 18:13:30 -04:00
local mtab = nil
local rtab = nil
2021-08-23 23:34:30 -04:00
-- No differences in these simple-valued tables.
assert(tdc({}, {a=true}, {}, {a=true}) == "")
assert(tdc({}, {a=5}, {}, {a=5}) == "");
assert(tdc({}, {a="foo"}, {}, {a="foo"}) == "")
2021-08-23 23:34:30 -04:00
-- Test transmission of missing simple values.
assert(tdc({}, {a=true}, {}, {}) == "a=true;")
assert(tdc({}, {a=5}, {}, {}) == "a=5;");
assert(tdc({}, {a="foo"}, {}, {}) == "a=foo;")
2021-08-23 23:34:30 -04:00
-- Test the replacement of simple values.
assert(tdc({}, {a=true}, {}, {a=false}) == "a=true;")
assert(tdc({}, {a=5}, {}, {a=4}) == "a=5;");
assert(tdc({}, {a="foo"}, {}, {a="bar"}) == "a=foo;")
2021-08-23 23:34:30 -04:00
-- Test the clearing of values.
assert(tdc({}, {}, {}, {a=true}) == "a=nil;")
assert(tdc({}, {}, {}, {a=5}) == "a=nil;");
assert(tdc({}, {}, {}, {a="foo"}) == "a=nil;")
2021-08-23 23:34:30 -04:00
-- Try boolean keys.
assert(tdc({}, {[true]=3}, {}, {}) == "true=3;")
assert(tdc({}, {}, {}, {[true]=3}) == "true=nil;")
2021-08-23 23:34:30 -04:00
-- Try number keys.
assert(tdc({}, {[7]=3}, {}, {}) == "7=3;")
assert(tdc({}, {}, {}, {[7]=3}) == "7=nil;")
2021-08-23 23:34:30 -04:00
-- Try a table with multiple keys.
assert(tdc({}, {a=4, b=5, c=6}, {}, {b=5, c=7, d=8}) == "a=4;c=6;d=nil;")
2021-08-23 23:34:30 -04:00
-- Nonsortable keys should be ignored (no diffs).
assert(tdc({}, {[{}]=3}, {}, {}) == "")
2021-08-23 23:34:30 -04:00
-- Try a table containing a class.
assert(tdc({}, {a=deque}, {}, {}) == "a=class deque;")
2021-08-23 23:34:30 -04:00
-- Try a table containing a pointer to the global environment.
assert(tdc({}, {a=_G}, {}, {}) == "a=globals;")
2021-08-23 23:34:30 -04:00
-- GlobalDB tables should be forced to NIL.
assert(tdc({}, {a=global.table("foo")}, {}, {a=global.table("foo")}) == "a=nil;");
assert(tdc({}, {}, {}, {a=global.table("foo")}) == "a=nil;");
assert(tdc({}, {a=global.table("foo")}, {}, {}) == "");
2021-08-23 23:34:30 -04:00
-- Set up some numbered tables for tests involving such.
local mtab10 = {}
local stab10 = {}
local mtnmap = {}
local stnmap = {}
mtnmap[mtab10] = 10
stnmap[stab10] = 10
-- confirm that numbered tables are being transmitted.
assert(tdc(mtnmap, {a=mtab10}, stnmap, {}) == "a=table 10;")
assert(tdc(mtnmap, {a=mtab10}, stnmap, {a=stab10}) == "")
assert(tdc(mtnmap, {a=3}, stnmap, {a=stab10}) == "a=3;")
assert(tdc(mtnmap, {}, stnmap, {a=stab10}) == "a=nil;")
-- confirm that unnumbered tables are forced to nil.
assert(tdc(mtnmap, {a={}}, stnmap, {}) == "")
assert(tdc(mtnmap, {a={}}, stnmap, {a=3}) == "a=nil;")
2021-09-01 18:13:30 -04:00
-- transmit a correction to the metatable.
mtab={}
stab={}
setmetatable(mtab, mtab10)
setmetatable(stab, stab10)
assert(tdc(mtnmap, mtab, stnmap, {}) == "nil=table 10;")
assert(tdc(mtnmap, mtab, stnmap, stab) == "")
assert(tdc(mtnmap, {}, stnmap, stab) == "nil=nil;")
2021-08-23 23:34:30 -04:00
-- we're not going to test tangibles
-- creating tangibles here is too difficult.
end
function unittests.diffapply()
local tab10={id=tab10}
local tab11={id=tab11}
local tnmap={}
tnmap[tab10] = 10
tnmap[tab11] = 11
2021-09-01 18:13:30 -04:00
local mtab=nil
local stab=nil
-- verify some simple values.
assert(tda(tnmap, {a=1}, {}))
assert(tda(tnmap, {[true]="foo"}, {}))
assert(tda(tnmap, {[3]=false}, {}))
-- verify a table with multiple simple values.
assert(tda(tnmap, {a=1, b=2, c=3}, {}))
-- verify that it can remove or replace wrong values.
assert(tda(tnmap, {a=1,b=2}, {b=3,c=4}))
-- verify a table containing another table.
assert(tda(tnmap, {a=tab10, b=tab11}, {}))
-- verify a table containing a class.
assert(tda(tnmap, {a=deque, b=table}, {}))
-- verify a table containing the global environment.
assert(tda(tnmap, {a=_G}, {}))
-- GlobalDB tables should be forced to NIL.
rtab={a=3}
assert(not tda({}, {a=global.table("foo")}, rtab))
assert(rtab.a == nil)
-- Unnumbered tables should be forced to NIL.
rtab={a=3}
assert(not tda({}, {a={}}, rtab))
assert(rtab.a == nil)
2021-09-01 18:13:30 -04:00
-- transmit a correction to the metatable
mtab={}
rtab={}
setmetatable(mtab, tab10)
assert(tda(tnmap, mtab, rtab))
assert(getmetatable(rtab) == tab10)
-- transmit a clearing of the metatable
mtab={}
rtab={}
setmetatable(rtab, tab10)
assert(tda(tnmap, mtab, rtab))
assert(getmetatable(rtab) == nil)
-- don't test tangibles.
end