diff options
| author | Mark Pulford <mark@kyne.com.au> | 2012-01-03 21:25:31 +1030 |
|---|---|---|
| committer | Mark Pulford <mark@kyne.com.au> | 2012-01-03 21:25:31 +1030 |
| commit | 041a32e52f694b68a881a72b7cb5f62aca449400 (patch) | |
| tree | fe4022fbd560806abf28062b4185c9dd7b83fe76 /tests | |
| parent | 3577e3548471da045a5198d2a9a64eba5383d8da (diff) | |
| download | lua-cjson-041a32e52f694b68a881a72b7cb5f62aca449400.tar.gz lua-cjson-041a32e52f694b68a881a72b7cb5f62aca449400.tar.bz2 lua-cjson-041a32e52f694b68a881a72b7cb5f62aca449400.zip | |
Convert common.lua into cjson-misc module
Diffstat (limited to 'tests')
| -rwxr-xr-x | tests/bench.lua | 5 | ||||
| -rw-r--r-- | tests/cjson-misc.lua (renamed from tests/common.lua) | 33 | ||||
| -rwxr-xr-x | tests/decode.lua | 6 | ||||
| -rwxr-xr-x | tests/encode.lua | 4 | ||||
| -rwxr-xr-x | tests/test.lua | 30 |
5 files changed, 43 insertions, 35 deletions
diff --git a/tests/bench.lua b/tests/bench.lua index c81213d..2b5177b 100755 --- a/tests/bench.lua +++ b/tests/bench.lua | |||
| @@ -6,10 +6,9 @@ | |||
| 6 | -- | 6 | -- |
| 7 | -- Mark Pulford <mark@kyne.com.au> | 7 | -- Mark Pulford <mark@kyne.com.au> |
| 8 | 8 | ||
| 9 | require "common" | ||
| 10 | require "socket" | 9 | require "socket" |
| 11 | |||
| 12 | local json = require "cjson" | 10 | local json = require "cjson" |
| 11 | local misc = require "cjson-misc" | ||
| 13 | 12 | ||
| 14 | function benchmark(tests, seconds, rep) | 13 | function benchmark(tests, seconds, rep) |
| 15 | local function bench(func, iter) | 14 | local function bench(func, iter) |
| @@ -54,7 +53,7 @@ function benchmark(tests, seconds, rep) | |||
| 54 | end | 53 | end |
| 55 | 54 | ||
| 56 | function bench_file(filename) | 55 | function bench_file(filename) |
| 57 | local data_json = file_load(filename) | 56 | local data_json = misc.file_load(filename) |
| 58 | local data_obj = json.decode(data_json) | 57 | local data_obj = json.decode(data_json) |
| 59 | 58 | ||
| 60 | local function test_encode () | 59 | local function test_encode () |
diff --git a/tests/common.lua b/tests/cjson-misc.lua index 7472a10..d8bfe5e 100644 --- a/tests/common.lua +++ b/tests/cjson-misc.lua | |||
| @@ -10,7 +10,7 @@ local json = require "cjson" | |||
| 10 | -- -1 Not an array | 10 | -- -1 Not an array |
| 11 | -- 0 Empty table | 11 | -- 0 Empty table |
| 12 | -- >0 Highest index in the array | 12 | -- >0 Highest index in the array |
| 13 | function is_array(table) | 13 | local function is_array(table) |
| 14 | local max = 0 | 14 | local max = 0 |
| 15 | local count = 0 | 15 | local count = 0 |
| 16 | for k, v in pairs(table) do | 16 | for k, v in pairs(table) do |
| @@ -28,7 +28,9 @@ function is_array(table) | |||
| 28 | return max | 28 | return max |
| 29 | end | 29 | end |
| 30 | 30 | ||
| 31 | function serialise_table(value, indent, depth) | 31 | local serialise_value |
| 32 | |||
| 33 | local function serialise_table(value, indent, depth) | ||
| 32 | local spacing, spacing2, indent2 | 34 | local spacing, spacing2, indent2 |
| 33 | if indent then | 35 | if indent then |
| 34 | spacing = "\n" .. indent | 36 | spacing = "\n" .. indent |
| @@ -91,11 +93,7 @@ function serialise_value(value, indent, depth) | |||
| 91 | end | 93 | end |
| 92 | end | 94 | end |
| 93 | 95 | ||
| 94 | function dump_value(value) | 96 | local function file_load(filename) |
| 95 | print(serialise_value(value)) | ||
| 96 | end | ||
| 97 | |||
| 98 | function file_load(filename) | ||
| 99 | local file | 97 | local file |
| 100 | if filename == nil then | 98 | if filename == nil then |
| 101 | file = io.stdin | 99 | file = io.stdin |
| @@ -119,7 +117,7 @@ function file_load(filename) | |||
| 119 | return data | 117 | return data |
| 120 | end | 118 | end |
| 121 | 119 | ||
| 122 | function file_save(filename, data) | 120 | local function file_save(filename, data) |
| 123 | local file | 121 | local file |
| 124 | if filename == nil then | 122 | if filename == nil then |
| 125 | file = io.stdout | 123 | file = io.stdout |
| @@ -136,7 +134,7 @@ function file_save(filename, data) | |||
| 136 | end | 134 | end |
| 137 | end | 135 | end |
| 138 | 136 | ||
| 139 | function compare_values(val1, val2) | 137 | local function compare_values(val1, val2) |
| 140 | local type1 = type(val1) | 138 | local type1 = type(val1) |
| 141 | local type2 = type(val2) | 139 | local type2 = type(val2) |
| 142 | if type1 ~= type2 then | 140 | if type1 ~= type2 then |
| @@ -179,11 +177,11 @@ end | |||
| 179 | local test_count_pass = 0 | 177 | local test_count_pass = 0 |
| 180 | local test_count_total = 0 | 178 | local test_count_total = 0 |
| 181 | 179 | ||
| 182 | function run_test_summary() | 180 | local function run_test_summary() |
| 183 | return test_count_pass, test_count_total | 181 | return test_count_pass, test_count_total |
| 184 | end | 182 | end |
| 185 | 183 | ||
| 186 | function run_test(testname, func, input, should_work, output) | 184 | local function run_test(testname, func, input, should_work, output) |
| 187 | local function status_line(name, status, value) | 185 | local function status_line(name, status, value) |
| 188 | local statusmap = { [true] = ":success", [false] = ":error" } | 186 | local statusmap = { [true] = ":success", [false] = ":error" } |
| 189 | if status ~= nil then | 187 | if status ~= nil then |
| @@ -216,7 +214,7 @@ function run_test(testname, func, input, should_work, output) | |||
| 216 | return correct, result | 214 | return correct, result |
| 217 | end | 215 | end |
| 218 | 216 | ||
| 219 | function run_test_group(testgroup, tests) | 217 | local function run_test_group(testgroup, tests) |
| 220 | local function run_config(configname, func) | 218 | local function run_config(configname, func) |
| 221 | local success, msg = pcall(func) | 219 | local success, msg = pcall(func) |
| 222 | if msg then | 220 | if msg then |
| @@ -241,4 +239,15 @@ function run_test_group(testgroup, tests) | |||
| 241 | end | 239 | end |
| 242 | end | 240 | end |
| 243 | 241 | ||
| 242 | -- Export functions | ||
| 243 | return { | ||
| 244 | serialise_value = serialise_value, | ||
| 245 | file_load = file_load, | ||
| 246 | file_save = file_save, | ||
| 247 | compare_values = compare_values, | ||
| 248 | run_test_summary = run_test_summary, | ||
| 249 | run_test = run_test, | ||
| 250 | run_test_group = run_test_group | ||
| 251 | } | ||
| 252 | |||
| 244 | -- vi:ai et sw=4 ts=4: | 253 | -- vi:ai et sw=4 ts=4: |
diff --git a/tests/decode.lua b/tests/decode.lua index 89354cd..18f3fc5 100755 --- a/tests/decode.lua +++ b/tests/decode.lua | |||
| @@ -6,9 +6,9 @@ | |||
| 6 | -- echo '[ "testing" ]' | ./decode.lua | 6 | -- echo '[ "testing" ]' | ./decode.lua |
| 7 | -- ./decode.lua test.json | 7 | -- ./decode.lua test.json |
| 8 | 8 | ||
| 9 | require "common" | ||
| 10 | local json = require "cjson" | 9 | local json = require "cjson" |
| 10 | local misc = require "cjson-misc" | ||
| 11 | 11 | ||
| 12 | local json_text = file_load(arg[1]) | 12 | local json_text = misc.file_load(arg[1]) |
| 13 | local t = json.decode(json_text) | 13 | local t = json.decode(json_text) |
| 14 | print(serialise_value(t)) | 14 | print(misc.serialise_value(t)) |
diff --git a/tests/encode.lua b/tests/encode.lua index a8d749a..6b767cb 100755 --- a/tests/encode.lua +++ b/tests/encode.lua | |||
| @@ -6,11 +6,11 @@ | |||
| 6 | -- echo '{ "testing" }' | ./encode.lua | 6 | -- echo '{ "testing" }' | ./encode.lua |
| 7 | -- ./encode.lua lua_data.lua | 7 | -- ./encode.lua lua_data.lua |
| 8 | 8 | ||
| 9 | require "common" | ||
| 10 | local json = require "cjson" | 9 | local json = require "cjson" |
| 10 | local misc = require "cjson-misc" | ||
| 11 | 11 | ||
| 12 | function get_lua_table(file) | 12 | function get_lua_table(file) |
| 13 | local func = loadstring("data = " .. file_load(file)) | 13 | local func = loadstring("data = " .. misc.file_load(file)) |
| 14 | if func == nil then | 14 | if func == nil then |
| 15 | error("Invalid syntax? Lua table required.") | 15 | error("Invalid syntax? Lua table required.") |
| 16 | end | 16 | end |
diff --git a/tests/test.lua b/tests/test.lua index f471289..c860878 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
| @@ -6,8 +6,8 @@ | |||
| 6 | -- | 6 | -- |
| 7 | -- Note: The output of this script is easier to read with "less -S" | 7 | -- Note: The output of this script is easier to read with "less -S" |
| 8 | 8 | ||
| 9 | require "common" | ||
| 10 | local json = require "cjson" | 9 | local json = require "cjson" |
| 10 | local misc = require "cjson-misc" | ||
| 11 | 11 | ||
| 12 | local function gen_ascii() | 12 | local function gen_ascii() |
| 13 | local chars = {} | 13 | local chars = {} |
| @@ -50,14 +50,14 @@ end | |||
| 50 | function test_decode_cycle(filename) | 50 | function test_decode_cycle(filename) |
| 51 | local obj1 = json.decode(file_load(filename)) | 51 | local obj1 = json.decode(file_load(filename)) |
| 52 | local obj2 = json.decode(json.encode(obj1)) | 52 | local obj2 = json.decode(json.encode(obj1)) |
| 53 | return compare_values(obj1, obj2) | 53 | return misc.compare_values(obj1, obj2) |
| 54 | end | 54 | end |
| 55 | 55 | ||
| 56 | local Inf = math.huge; | 56 | local Inf = math.huge; |
| 57 | local NaN = math.huge * 0; | 57 | local NaN = math.huge * 0; |
| 58 | local octets_raw = gen_ascii() | 58 | local octets_raw = gen_ascii() |
| 59 | local octets_escaped = file_load("octets-escaped.dat") | 59 | local octets_escaped = misc.file_load("octets-escaped.dat") |
| 60 | local utf8_loaded, utf8_raw = pcall(file_load, "utf8.dat") | 60 | local utf8_loaded, utf8_raw = pcall(misc.file_load, "utf8.dat") |
| 61 | if not utf8_loaded then | 61 | if not utf8_loaded then |
| 62 | utf8_raw = "Failed to load utf8.dat" | 62 | utf8_raw = "Failed to load utf8.dat" |
| 63 | end | 63 | end |
| @@ -224,23 +224,23 @@ local locale_tests = { | |||
| 224 | 224 | ||
| 225 | print(string.format("Testing Lua CJSON version %s\n", json.version)) | 225 | print(string.format("Testing Lua CJSON version %s\n", json.version)) |
| 226 | 226 | ||
| 227 | run_test_group("decode simple value", decode_simple_tests) | 227 | misc.run_test_group("decode simple value", decode_simple_tests) |
| 228 | run_test_group("encode simple value", encode_simple_tests) | 228 | misc.run_test_group("encode simple value", encode_simple_tests) |
| 229 | run_test_group("decode numeric", decode_numeric_tests) | 229 | misc.run_test_group("decode numeric", decode_numeric_tests) |
| 230 | run_test_group("encode table", encode_table_tests) | 230 | misc.run_test_group("encode table", encode_table_tests) |
| 231 | run_test_group("decode error", decode_error_tests) | 231 | misc.run_test_group("decode error", decode_error_tests) |
| 232 | run_test_group("encode error", encode_error_tests) | 232 | misc.run_test_group("encode error", encode_error_tests) |
| 233 | run_test_group("escape", escape_tests) | 233 | misc.run_test_group("escape", escape_tests) |
| 234 | run_test_group("locale", locale_tests) | 234 | misc.run_test_group("locale", locale_tests) |
| 235 | 235 | ||
| 236 | json.refuse_invalid_numbers(false) | 236 | json.refuse_invalid_numbers(false) |
| 237 | json.encode_max_depth(20) | 237 | json.encode_max_depth(20) |
| 238 | for i = 1, #arg do | 238 | for i = 1, #arg do |
| 239 | run_test("decode cycle " .. arg[i], test_decode_cycle, { arg[i] }, | 239 | misc.run_test("decode cycle " .. arg[i], test_decode_cycle, { arg[i] }, |
| 240 | true, { true }) | 240 | true, { true }) |
| 241 | end | 241 | end |
| 242 | 242 | ||
| 243 | local pass, total = run_test_summary() | 243 | local pass, total = misc.run_test_summary() |
| 244 | 244 | ||
| 245 | if pass == total then | 245 | if pass == total then |
| 246 | print("==> Summary: all tests succeeded") | 246 | print("==> Summary: all tests succeeded") |
