diff --git a/src/lua/tap.lua b/src/lua/tap.lua index 83356030be80a6053c915e7d6650a71e2ea4a379..b8042e7d7af99f2f55efc8623d728e6d96bafd6e 100644 --- a/src/lua/tap.lua +++ b/src/lua/tap.lua @@ -68,6 +68,43 @@ local function skip(test, message, extra) ok(test, true, message.." # skip", extra) end + + +local function cmpdeeply(got, expected, extra) + + if type(got) ~= type(expected) then + extra.got = type(got) + extra.expected = type(expected) + return false + end + + if type(got) ~= 'table' then + extra.got = got + extra.expected = expected + return got == expected + end + + local path = extra.path or '/' + + for i, v in pairs(got) do + extra.path = path .. '/' .. i + if not cmpdeeply(v, expected[i], extra) then + return false + end + end + + for i, v in pairs(expected) do + extra.path = path .. '/' .. i + if not cmpdeeply(got[i], v, extra) then + return false + end + end + + extra.path = path + + return true +end + local function is(test, got, expected, message, extra) extra = extra or {} extra.got = got @@ -82,6 +119,14 @@ local function isnt(test, got, unexpected, message, extra) return ok(test, got ~= unexpected, message, extra) end + +local function isdeeply(test, got, expected, message, extra) + extra = extra or {} + extra.got = got + extra.expected = expected + return ok(test, cmpdeeply(got, expected, extra), message, extra) +end + local function isnil(test, v, message, extra) return is(test, not v and 'nil' or v, 'nil', message, extra) end @@ -202,6 +247,7 @@ test_mt = { isboolean = isboolean; isudata = isudata; iscdata = iscdata; + isdeeply = isdeeply; } } diff --git a/test/app/tap.result b/test/app/tap.result index 052e3ad667e5a3812a36a102ce4a7d0051eb16cf..a26757e5bf0de39ea0266d114785af85f9dafc64 100644 --- a/test/app/tap.result +++ b/test/app/tap.result @@ -1,5 +1,5 @@ TAP version 13 -1..30 +1..31 ok - true ok - extra information is not printed on success not ok - extra printed using yaml only on failure @@ -118,4 +118,28 @@ not ok - failed subtests planned: 1 failed: 1 ... -# failed subtest: 14 + # isdeeply + 1..6 + ok - 1 and 1 + ok - abc and abc + ok - empty tables + ok - {1} and {1} + not ok - {1} and {2} + --- + path: //1 + expected: 2 + got: 1 + ... + not ok - {1,2,{3,4}} and {1,2,{3,5}} + --- + path: //3/2 + expected: 5 + got: 4 + ... + # isdeeply: end +not ok - failed subtests + --- + planned: 6 + failed: 2 + ... +# failed subtest: 15 diff --git a/test/app/tap.test.lua b/test/app/tap.test.lua index 243d1cbdc90f92e20f4c4a7f1af920f78c60c60e..cc2d9bf3828395259b466ecc27e284bfd6cdaa62 100755 --- a/test/app/tap.test.lua +++ b/test/app/tap.test.lua @@ -20,7 +20,7 @@ test.trace = false -- ok, fail and skip predicates -- -test:plan(30) -- plan to run 3 test +test:plan(31) -- plan to run 3 test test:ok(true, 'true') -- basic function local extra = { state = 'some userful information to debug on failure', details = 'a table argument formatted using yaml.encode()' } @@ -115,9 +115,25 @@ test:test("failed subtest", function(t) t:fail("failed subtest") end) + + +test:test('isdeeply', function(t) + t:plan(6) + + t:isdeeply(1, 1, '1 and 1') + t:isdeeply('abc', 'abc', 'abc and abc') + t:isdeeply({}, {}, 'empty tables') + t:isdeeply({1}, {1}, '{1} and {1}') + t:isdeeply({1}, {2}, '{1} and {2}') + t:isdeeply({1, 2, { 3, 4 }}, {1, 2, { 3, 5 }}, '{1,2,{3,4}} and {1,2,{3,5}}') + +end) + -- -- Finish root test. Since we used non-callback variant, we have to -- call check explicitly. -- test:check() -- call check() explicitly os.exit(0) + +