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 /lua | |
| 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
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/cjson/util.lua | 29 |
1 files changed, 27 insertions, 2 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: |
