aboutsummaryrefslogtreecommitdiff
path: root/tests/common.lua
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/common.lua
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/common.lua')
-rw-r--r--tests/common.lua244
1 files changed, 0 insertions, 244 deletions
diff --git a/tests/common.lua b/tests/common.lua
deleted file mode 100644
index 7472a10..0000000
--- a/tests/common.lua
+++ /dev/null
@@ -1,244 +0,0 @@
1local json = require "cjson"
2
3-- Misc routines to assist with CJSON testing
4--
5-- Mark Pulford <mark@kyne.com.au>
6
7-- Determine with a Lua table can be treated as an array.
8-- Explicitly returns "not an array" for very sparse arrays.
9-- Returns:
10-- -1 Not an array
11-- 0 Empty table
12-- >0 Highest index in the array
13function is_array(table)
14 local max = 0
15 local count = 0
16 for k, v in pairs(table) do
17 if type(k) == "number" then
18 if k > max then max = k end
19 count = count + 1
20 else
21 return -1
22 end
23 end
24 if max > count * 2 then
25 return -1
26 end
27
28 return max
29end
30
31function serialise_table(value, indent, depth)
32 local spacing, spacing2, indent2
33 if indent then
34 spacing = "\n" .. indent
35 spacing2 = spacing .. " "
36 indent2 = indent .. " "
37 else
38 spacing, spacing2, indent2 = " ", " ", false
39 end
40 depth = depth + 1
41 if depth > 50 then
42 return "Cannot serialise any further: too many nested tables"
43 end
44
45 local max = is_array(value)
46
47 local comma = false
48 local fragment = { "{" .. spacing2 }
49 if max > 0 then
50 -- Serialise array
51 for i = 1, max do
52 if comma then
53 table.insert(fragment, "," .. spacing2)
54 end
55 table.insert(fragment, serialise_value(value[i], indent2, depth))
56 comma = true
57 end
58 elseif max < 0 then
59 -- Serialise table
60 for k, v in pairs(value) do
61 if comma then
62 table.insert(fragment, "," .. spacing2)
63 end
64 table.insert(fragment, string.format(
65 "[%s] = %s", serialise_value(k, indent2, depth),
66 serialise_value(v, indent2, depth))
67 )
68 comma = true
69 end
70 end
71 table.insert(fragment, spacing .. "}")
72
73 return table.concat(fragment)
74end
75
76function serialise_value(value, indent, depth)
77 if indent == nil then indent = "" end
78 if depth == nil then depth = 0 end
79
80 if value == json.null then
81 return "json.null"
82 elseif type(value) == "string" then
83 return string.format("%q", value)
84 elseif type(value) == "nil" or type(value) == "number" or
85 type(value) == "boolean" then
86 return tostring(value)
87 elseif type(value) == "table" then
88 return serialise_table(value, indent, depth)
89 else
90 return "\"<" .. type(value) .. ">\""
91 end
92end
93
94function dump_value(value)
95 print(serialise_value(value))
96end
97
98function file_load(filename)
99 local file
100 if filename == nil then
101 file = io.stdin
102 else
103 local err
104 file, err = io.open(filename)
105 if file == nil then
106 error(string.format("Unable to read '%s': %s", filename, err))
107 end
108 end
109 local data = file:read("*a")
110
111 if filename ~= nil then
112 file:close()
113 end
114
115 if data == nil then
116 error("Failed to read " .. filename)
117 end
118
119 return data
120end
121
122function file_save(filename, data)
123 local file
124 if filename == nil then
125 file = io.stdout
126 else
127 local err
128 file, err = io.open(filename, "w")
129 if file == nil then
130 error(string.format("Unable to write '%s': %s", filename, err))
131 end
132 end
133 file:write(data)
134 if filename ~= nil then
135 file:close()
136 end
137end
138
139function compare_values(val1, val2)
140 local type1 = type(val1)
141 local type2 = type(val2)
142 if type1 ~= type2 then
143 return false
144 end
145
146 -- Check for NaN
147 if type1 == "number" and val1 ~= val1 and val2 ~= val2 then
148 return true
149 end
150
151 if type1 ~= "table" then
152 return val1 == val2
153 end
154
155 -- check_keys stores all the keys that must be checked in val2
156 local check_keys = {}
157 for k, _ in pairs(val1) do
158 check_keys[k] = true
159 end
160
161 for k, v in pairs(val2) do
162 if not check_keys[k] then
163 return false
164 end
165
166 if not compare_values(val1[k], val2[k]) then
167 return false
168 end
169
170 check_keys[k] = nil
171 end
172 for k, _ in pairs(check_keys) do
173 -- Not the same if any keys from val1 were not found in val2
174 return false
175 end
176 return true
177end
178
179local test_count_pass = 0
180local test_count_total = 0
181
182function run_test_summary()
183 return test_count_pass, test_count_total
184end
185
186function run_test(testname, func, input, should_work, output)
187 local function status_line(name, status, value)
188 local statusmap = { [true] = ":success", [false] = ":error" }
189 if status ~= nil then
190 name = name .. statusmap[status]
191 end
192 print(string.format("[%s] %s", name, serialise_value(value, false)))
193 end
194
195 local result = { pcall(func, unpack(input)) }
196 local success = table.remove(result, 1)
197
198 local correct = false
199 if success == should_work and compare_values(result, output) then
200 correct = true
201 test_count_pass = test_count_pass + 1
202 end
203 test_count_total = test_count_total + 1
204
205 local teststatus = { [true] = "PASS", [false] = "FAIL" }
206 print(string.format("==> Test[%d] / %s: %s",
207 test_count_total, testname, teststatus[correct]))
208
209 status_line("Input", nil, input)
210 if not correct then
211 status_line("Expected", should_work, output)
212 end
213 status_line("Received", success, result)
214 print()
215
216 return correct, result
217end
218
219function run_test_group(testgroup, tests)
220 local function run_config(configname, func)
221 local success, msg = pcall(func)
222 if msg then
223 print(string.format("==> Config %s: %s", configname, msg))
224 end
225 print()
226 end
227
228 local function test_id(group, id)
229 return string.format("%s[%d]", group, id)
230 end
231
232 for k, v in ipairs(tests) do
233 if type(v) == "function" then
234 -- Useful for changing configuration during a batch
235 run_config(test_id(testgroup, k), v)
236 elseif type(v) == "table" then
237 run_test(test_id(testgroup, k), unpack(v))
238 else
239 error("Testgroup can only contain functions and tables")
240 end
241 end
242end
243
244-- vi:ai et sw=4 ts=4: