diff options
author | leaf corcoran <leafot@gmail.com> | 2019-09-21 19:09:00 -0700 |
---|---|---|
committer | leaf <leafot@gmail.com> | 2020-04-25 14:24:18 -0700 |
commit | 3882fac81a08c24c948faafe980dd181a906fa00 (patch) | |
tree | 7804d668dd532cf3e167402f001123910928d57d | |
parent | 0d0c4063d00bc33549832a8d58a00c9836c04df8 (diff) | |
download | lua-cjson-3882fac81a08c24c948faafe980dd181a906fa00.tar.gz lua-cjson-3882fac81a08c24c948faafe980dd181a906fa00.tar.bz2 lua-cjson-3882fac81a08c24c948faafe980dd181a906fa00.zip |
add one_of testing to avoid hash table ordering causing test to fail
-rw-r--r-- | lua/cjson/util.lua | 29 | ||||
-rwxr-xr-x | tests/test.lua | 7 |
2 files changed, 33 insertions, 3 deletions
diff --git a/lua/cjson/util.lua b/lua/cjson/util.lua index 2405257..7cf66be 100644 --- a/lua/cjson/util.lua +++ b/lua/cjson/util.lua | |||
@@ -12,6 +12,17 @@ local maxn = table.maxn or function(t) | |||
12 | return max | 12 | return max |
13 | end | 13 | end |
14 | 14 | ||
15 | local _one_of_mt = {} | ||
16 | |||
17 | local function one_of(t) | ||
18 | setmetatable(t, _one_of_mt) | ||
19 | return t | ||
20 | end | ||
21 | |||
22 | local function is_one_of(t) | ||
23 | return type(t) == "table" and getmetatable(t) == _one_of_mt | ||
24 | end | ||
25 | |||
15 | -- Various common routines used by the Lua CJSON package | 26 | -- Various common routines used by the Lua CJSON package |
16 | -- | 27 | -- |
17 | -- Mark Pulford <mark@kyne.com.au> | 28 | -- Mark Pulford <mark@kyne.com.au> |
@@ -59,7 +70,11 @@ local function serialise_table(value, indent, depth) | |||
59 | local max = is_array(value) | 70 | local max = is_array(value) |
60 | 71 | ||
61 | local comma = false | 72 | local comma = false |
62 | local fragment = { "{" .. spacing2 } | 73 | local prefix = "{" |
74 | if is_one_of(value) then | ||
75 | prefix = "ONE_OF{" | ||
76 | end | ||
77 | local fragment = { prefix .. spacing2 } | ||
63 | if max > 0 then | 78 | if max > 0 then |
64 | -- Serialise array | 79 | -- Serialise array |
65 | for i = 1, max do | 80 | for i = 1, max do |
@@ -146,6 +161,15 @@ local function file_save(filename, data) | |||
146 | end | 161 | end |
147 | 162 | ||
148 | local function compare_values(val1, val2) | 163 | local function compare_values(val1, val2) |
164 | if is_one_of(val2) then | ||
165 | for _, option in ipairs(val2) do | ||
166 | if compare_values(val1, option) then | ||
167 | return true | ||
168 | end | ||
169 | end | ||
170 | return false | ||
171 | end | ||
172 | |||
149 | local type1 = type(val1) | 173 | local type1 = type(val1) |
150 | local type2 = type(val2) | 174 | local type2 = type(val2) |
151 | if type1 ~= type2 then | 175 | if type1 ~= type2 then |
@@ -281,7 +305,8 @@ return { | |||
281 | run_test_summary = run_test_summary, | 305 | run_test_summary = run_test_summary, |
282 | run_test = run_test, | 306 | run_test = run_test, |
283 | run_test_group = run_test_group, | 307 | run_test_group = run_test_group, |
284 | run_script = run_script | 308 | run_script = run_script, |
309 | one_of = one_of | ||
285 | } | 310 | } |
286 | 311 | ||
287 | -- vi:ai et sw=4 ts=4: | 312 | -- vi:ai et sw=4 ts=4: |
diff --git a/tests/test.lua b/tests/test.lua index d1bf7e4..b2162b9 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
@@ -293,7 +293,12 @@ local cjson_tests = { | |||
293 | true, { '["one",null,null,"sparse test"]' } }, | 293 | true, { '["one",null,null,"sparse test"]' } }, |
294 | { "Encode sparse array as object", | 294 | { "Encode sparse array as object", |
295 | json.encode, { { [1] = "one", [5] = "sparse test" } }, | 295 | json.encode, { { [1] = "one", [5] = "sparse test" } }, |
296 | true, { '{"5":"sparse test","1":"one"}' } }, | 296 | true, { |
297 | util.one_of{ | ||
298 | '{"5":"sparse test","1":"one"}', | ||
299 | '{"1":"one","5":"sparse test"}' | ||
300 | } | ||
301 | } }, | ||
297 | { "Encode table with numeric string key as object", | 302 | { "Encode table with numeric string key as object", |
298 | json.encode, { { ["2"] = "numeric string key test" } }, | 303 | json.encode, { { ["2"] = "numeric string key test" } }, |
299 | true, { '{"2":"numeric string key test"}' } }, | 304 | true, { '{"2":"numeric string key test"}' } }, |