diff options
author | Mark Pulford <mark@kyne.com.au> | 2011-05-07 03:48:58 +0930 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2011-05-07 03:48:58 +0930 |
commit | d75e1cb7f0418615e208f49c6209b7c144591f14 (patch) | |
tree | 318bbaa76d34472b956cc21e7128e2a322210693 | |
parent | 7befa3e83bd60f1b749d80420ee5189d095f4682 (diff) | |
download | lua-cjson-d75e1cb7f0418615e208f49c6209b7c144591f14.tar.gz lua-cjson-d75e1cb7f0418615e208f49c6209b7c144591f14.tar.bz2 lua-cjson-d75e1cb7f0418615e208f49c6209b7c144591f14.zip |
Add test to compare objects after decode/encode
-rw-r--r-- | tests/common.lua | 31 | ||||
-rwxr-xr-x | tests/test.lua | 12 |
2 files changed, 40 insertions, 3 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 |
77 | end | 77 | end |
78 | 78 | ||
79 | function 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 | ||
108 | end | ||
109 | |||
79 | -- vi:ai et sw=4 ts=4: | 110 | -- vi:ai et sw=4 ts=4: |
diff --git a/tests/test.lua b/tests/test.lua index 9424e8f..3c8c404 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
@@ -122,11 +122,17 @@ local ascii2 = json.decode(json.encode(ascii)) | |||
122 | print("8bit clean encode/decode: " .. tostring(ascii1 ~= ascii2)) | 122 | print("8bit clean encode/decode: " .. tostring(ascii1 ~= ascii2)) |
123 | 123 | ||
124 | for i = 1, #arg do | 124 | for i = 1, #arg do |
125 | print(arg[i] .. " enc->dec..") | ||
126 | local obj1 = json.decode(file_load(arg[i])) | 125 | local obj1 = json.decode(file_load(arg[i])) |
127 | local obj2 = json.decode(json.encode(obj1)) | 126 | local obj2 = json.decode(json.encode(obj1)) |
128 | -- obj_compare(obj_json1, obj_json2) | 127 | if compare_values(obj, obj) then |
129 | print(".. unimplemented") | 128 | print(arg[i] .. ": PASS") |
129 | else | ||
130 | print(arg[i] .. ": FAIL") | ||
131 | print("== obj1 ==") | ||
132 | dump_value(obj1) | ||
133 | print("== obj2 ==") | ||
134 | dump_value(obj2) | ||
135 | end | ||
130 | end | 136 | end |
131 | 137 | ||
132 | -- vi:ai et sw=4 ts=4: | 138 | -- vi:ai et sw=4 ts=4: |