aboutsummaryrefslogtreecommitdiff
path: root/tests/cjson-misc.lua
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/cjson-misc.lua (renamed from tests/common.lua)33
1 files changed, 21 insertions, 12 deletions
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: