diff options
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 |
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: |