-- the tdc function calculates diffs, and returns those -- diffs as a human-readable string. 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() local mtab = nil local rtab = nil -- 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"}) == "") -- Test transmission of missing simple values. assert(tdc({}, {a=true}, {}, {}) == "a=true;") assert(tdc({}, {a=5}, {}, {}) == "a=5;"); assert(tdc({}, {a="foo"}, {}, {}) == "a=foo;") -- 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;") -- Test the clearing of values. assert(tdc({}, {}, {}, {a=true}) == "a=nil;") assert(tdc({}, {}, {}, {a=5}) == "a=nil;"); assert(tdc({}, {}, {}, {a="foo"}) == "a=nil;") -- Try boolean keys. assert(tdc({}, {[true]=3}, {}, {}) == "true=3;") assert(tdc({}, {}, {}, {[true]=3}) == "true=nil;") -- Try number keys. assert(tdc({}, {[7]=3}, {}, {}) == "7=3;") assert(tdc({}, {}, {}, {[7]=3}) == "7=nil;") -- 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;") -- Nonsortable keys should be ignored (no diffs). assert(tdc({}, {[{}]=3}, {}, {}) == "") -- Try a table containing a class. assert(tdc({}, {a=deque}, {}, {}) == "a=class deque;") -- Try a table containing a pointer to the global environment. assert(tdc({}, {a=_G}, {}, {}) == "a=globals;") -- GlobalDB tables should be forced to NIL. assert(tdc({}, {a=global("foo")}, {}, {a=global("foo")}) == "a=nil;"); assert(tdc({}, {}, {}, {a=global("foo")}) == "a=nil;"); assert(tdc({}, {a=global("foo")}, {}, {}) == ""); -- 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;") -- 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;") -- 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 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("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) -- 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