aboutsummaryrefslogtreecommitdiff
path: root/tests/common.lua
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2011-05-07 03:48:58 +0930
committerMark Pulford <mark@kyne.com.au>2011-05-07 03:48:58 +0930
commitd75e1cb7f0418615e208f49c6209b7c144591f14 (patch)
tree318bbaa76d34472b956cc21e7128e2a322210693 /tests/common.lua
parent7befa3e83bd60f1b749d80420ee5189d095f4682 (diff)
downloadlua-cjson-d75e1cb7f0418615e208f49c6209b7c144591f14.tar.gz
lua-cjson-d75e1cb7f0418615e208f49c6209b7c144591f14.tar.bz2
lua-cjson-d75e1cb7f0418615e208f49c6209b7c144591f14.zip
Add test to compare objects after decode/encode
Diffstat (limited to 'tests/common.lua')
-rw-r--r--tests/common.lua31
1 files changed, 31 insertions, 0 deletions
diff --git a/tests/common.lua b/tests/common.lua
index e935032..219de3f 100644
--- a/tests/common.lua
+++ b/tests/common.lua
@@ -76,4 +76,35 @@ function benchmark(tests, iter, rep)
76 return test_results 76 return test_results
77end 77end
78 78
79function compare_values(val1, val2)
80 local type1 = type(val1)
81 local type2 = type(val2)
82 if type1 ~= type2 then
83 return false
84 end
85 if type1 ~= "table" then
86 return val1 == val2
87 end
88 local val1_keys = {}
89 -- Note all the keys in val1 need to be checked
90 for k, _ in pairs(val1) do
91 check_keys[k] = true
92 end
93 for k, v in pairs(val2) do
94 if not check_keys[k] then
95 -- Key didn't exist in val1
96 return false
97 end
98 if not compare_value(val1[k], val2[k]) then
99 return false
100 end
101 check_keys[k] = nil
102 end
103 for k, _ in pairs(check_keys) do
104 -- Not the same if any keys left to check
105 return false
106 end
107 return true
108end
109
79-- vi:ai et sw=4 ts=4: 110-- vi:ai et sw=4 ts=4: