From f063c7e9a657044d46367f76476353bc18fdcd9a Mon Sep 17 00:00:00 2001
From: "Dmitry E. Oboukhov" <unera@debian.org>
Date: Mon, 18 Aug 2014 15:55:10 +0400
Subject: [PATCH] isdeeply into tap.lua

---
 src/lua/tap.lua       | 46 +++++++++++++++++++++++++++++++++++++++++++
 test/app/tap.result   | 28 ++++++++++++++++++++++++--
 test/app/tap.test.lua | 18 ++++++++++++++++-
 3 files changed, 89 insertions(+), 3 deletions(-)

diff --git a/src/lua/tap.lua b/src/lua/tap.lua
index 83356030be..b8042e7d7a 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 052e3ad667..a26757e5bf 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 243d1cbdc9..cc2d9bf382 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)
+
+
-- 
GitLab