aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2012-01-03 21:25:31 +1030
committerMark Pulford <mark@kyne.com.au>2012-01-03 21:25:31 +1030
commit041a32e52f694b68a881a72b7cb5f62aca449400 (patch)
treefe4022fbd560806abf28062b4185c9dd7b83fe76 /tests
parent3577e3548471da045a5198d2a9a64eba5383d8da (diff)
downloadlua-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-xtests/bench.lua5
-rw-r--r--tests/cjson-misc.lua (renamed from tests/common.lua)33
-rwxr-xr-xtests/decode.lua6
-rwxr-xr-xtests/encode.lua4
-rwxr-xr-xtests/test.lua30
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
9require "common"
10require "socket" 9require "socket"
11
12local json = require "cjson" 10local json = require "cjson"
11local misc = require "cjson-misc"
13 12
14function benchmark(tests, seconds, rep) 13function 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)
54end 53end
55 54
56function bench_file(filename) 55function 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
13function is_array(table) 13local 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
29end 29end
30 30
31function serialise_table(value, indent, depth) 31local serialise_value
32
33local 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
92end 94end
93 95
94function dump_value(value) 96local function file_load(filename)
95 print(serialise_value(value))
96end
97
98function 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
120end 118end
121 119
122function file_save(filename, data) 120local 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
137end 135end
138 136
139function compare_values(val1, val2) 137local 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
179local test_count_pass = 0 177local test_count_pass = 0
180local test_count_total = 0 178local test_count_total = 0
181 179
182function run_test_summary() 180local function run_test_summary()
183 return test_count_pass, test_count_total 181 return test_count_pass, test_count_total
184end 182end
185 183
186function run_test(testname, func, input, should_work, output) 184local 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
217end 215end
218 216
219function run_test_group(testgroup, tests) 217local 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
242end 240end
243 241
242-- Export functions
243return {
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
9require "common"
10local json = require "cjson" 9local json = require "cjson"
10local misc = require "cjson-misc"
11 11
12local json_text = file_load(arg[1]) 12local json_text = misc.file_load(arg[1])
13local t = json.decode(json_text) 13local t = json.decode(json_text)
14print(serialise_value(t)) 14print(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
9require "common"
10local json = require "cjson" 9local json = require "cjson"
10local misc = require "cjson-misc"
11 11
12function get_lua_table(file) 12function 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
9require "common"
10local json = require "cjson" 9local json = require "cjson"
10local misc = require "cjson-misc"
11 11
12local function gen_ascii() 12local function gen_ascii()
13 local chars = {} 13 local chars = {}
@@ -50,14 +50,14 @@ end
50function test_decode_cycle(filename) 50function 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)
54end 54end
55 55
56local Inf = math.huge; 56local Inf = math.huge;
57local NaN = math.huge * 0; 57local NaN = math.huge * 0;
58local octets_raw = gen_ascii() 58local octets_raw = gen_ascii()
59local octets_escaped = file_load("octets-escaped.dat") 59local octets_escaped = misc.file_load("octets-escaped.dat")
60local utf8_loaded, utf8_raw = pcall(file_load, "utf8.dat") 60local utf8_loaded, utf8_raw = pcall(misc.file_load, "utf8.dat")
61if not utf8_loaded then 61if not utf8_loaded then
62 utf8_raw = "Failed to load utf8.dat" 62 utf8_raw = "Failed to load utf8.dat"
63end 63end
@@ -224,23 +224,23 @@ local locale_tests = {
224 224
225print(string.format("Testing Lua CJSON version %s\n", json.version)) 225print(string.format("Testing Lua CJSON version %s\n", json.version))
226 226
227run_test_group("decode simple value", decode_simple_tests) 227misc.run_test_group("decode simple value", decode_simple_tests)
228run_test_group("encode simple value", encode_simple_tests) 228misc.run_test_group("encode simple value", encode_simple_tests)
229run_test_group("decode numeric", decode_numeric_tests) 229misc.run_test_group("decode numeric", decode_numeric_tests)
230run_test_group("encode table", encode_table_tests) 230misc.run_test_group("encode table", encode_table_tests)
231run_test_group("decode error", decode_error_tests) 231misc.run_test_group("decode error", decode_error_tests)
232run_test_group("encode error", encode_error_tests) 232misc.run_test_group("encode error", encode_error_tests)
233run_test_group("escape", escape_tests) 233misc.run_test_group("escape", escape_tests)
234run_test_group("locale", locale_tests) 234misc.run_test_group("locale", locale_tests)
235 235
236json.refuse_invalid_numbers(false) 236json.refuse_invalid_numbers(false)
237json.encode_max_depth(20) 237json.encode_max_depth(20)
238for i = 1, #arg do 238for 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 })
241end 241end
242 242
243local pass, total = run_test_summary() 243local pass, total = misc.run_test_summary()
244 244
245if pass == total then 245if pass == total then
246 print("==> Summary: all tests succeeded") 246 print("==> Summary: all tests succeeded")