Merge pull request #49 from jokajak/bugfix/lpeg_version_check
[luajson.git] / tests / lunit-calls.lua
blobb8450f47256ec0a759843235794d3d9c15d32590
1 local lpeg = require("lpeg")
2 local json = require("json")
3 local lunit = require("lunit")
4 local math = require("math")
5 local testutil = require("testutil")
7 local encode = json.encode
8 -- DECODE NOT 'local' due to requirement for testutil to access it
9 decode = json.decode.getDecoder(false)
11 if not module then
12 _ENV = lunit.module("lunit-calls", 'seeall')
13 else
14 module("lunit-calls", lunit.testcase, package.seeall)
15 end
17 function setup()
18 -- Ensure that the decoder is reset
19 _G["decode"] = json.decode.getDecoder(false)
20 end
22 local values = {
25 0.2,
26 "Hello",
27 true,
28 {hi=true},
29 {1,2}
32 function test_identity()
33 local function testFunction(capturedName, ...)
34 assert_equal('call', capturedName)
35 return (...)
36 end
37 local strict = {
38 calls = { defs = {
39 call = testFunction
40 } }
42 local decode = json.decode.getDecoder(strict)
43 for i, v in ipairs(values) do
44 local str = "call(" .. encode(v) .. ")"
45 local decoded = decode(str)
46 if type(decoded) == 'table' then
47 for k2, v2 in pairs(v) do
48 assert_equal(v2, decoded[k2])
49 decoded[k2] = nil
50 end
51 assert_nil(next(decoded))
52 else
53 assert_equal(v, decoded)
54 end
55 end
56 end
58 -- Test for a function that throws
59 function test_function_failure()
60 local function testFunction(...)
61 error("CANNOT CONTINUE")
62 end
63 local strict = {
64 calls = { defs = {
65 call = testFunction
66 } }
68 local decode = json.decode.getDecoder(strict)
69 for i, v in ipairs(values) do
70 local str = "call(" .. encode(v) .. ")"
71 assert_error(function()
72 decode(str)
73 end)
74 end
75 end
77 -- Test for a function that is not a function
78 function test_not_a_function_fail()
79 local notFunction = {
80 0/0, 1/0, -1/0, 0, 1, "Hello", {}, coroutine.create(function() end)
82 for _, v in ipairs(notFunction) do
83 assert_error(function()
84 local strict = {
85 calls = { defs = {
86 call = v
87 }, allowUndefined = false }
89 json.decode.getDecoder(strict)
90 end)
91 end
92 end
94 function test_not_permitted_fail()
95 local strict = {
96 calls = {
97 defs = { call = false }
100 local decoder = json.decode.getDecoder(strict)
101 assert_error(function()
102 decoder("call(1)")
103 end)
106 function test_permitted()
107 local strict = {
108 calls = {
109 defs = { call = true, other = true }
112 local decoder = json.decode.getDecoder(strict)
113 assert(decoder("call(1)").name == 'call')
114 assert(decoder("other(1)").name == 'other')
117 function test_permitted_trailing()
118 local strict = {
119 calls = {
120 defs = { call = true, other = true }
123 local decoder = json.decode.getDecoder(strict)
124 assert(decoder("call(1,)").name == 'call')
125 assert(decoder("other(1,)").name == 'other')
127 function test_permitted_no_trailing()
128 local strict = {
129 calls = {
130 defs = { call = true, other = true },
131 trailingComma = false
134 local decoder = json.decode.getDecoder(strict)
135 assert_error(function()
136 decoder("call(1,)")
137 end)
138 assert_error(function()
139 decoder("other(1,)")
140 end)
142 function test_permitted_nested()
143 local strict = {
144 calls = {
145 defs = { call = true, other = true }
148 local decoder = json.decode.getDecoder(strict)
149 assert(decoder("call(call(1))").name == 'call')
150 assert(decoder("other(call(1))").name == 'other')
153 function test_not_defined_fail()
154 local decoder = json.decode.getDecoder({
155 calls = {
156 allowUndefined = false
159 assert_error(function()
160 decoder("call(1)")
161 end)
164 function test_not_defined_succeed()
165 local decoder = json.decode.getDecoder({
166 calls = {
167 allowUndefined = true
170 assert(decoder("call(1)").name == 'call')
173 -- Test for a name that is not a string
174 function test_name_not_string()
175 local notString = {
176 true, false, 0/0, 1/0, -1/0, 0, 1, {}, function() end, coroutine.create(function() end)
178 for _, v in ipairs(notString) do
179 assert_error(function()
180 local defs = {
181 [v] = function() end
183 local strict = {
184 calls = { defs = defs }
186 json.decode.getDecoder(strict)
187 end)
191 -- Test for a name that is a string or a pattern
192 function test_name_matches_string_or_pattern()
193 local matchedValues = {
194 ["mystring"] = "mystring",
195 [lpeg.C(lpeg.P("m") * (lpeg.P("y") + lpeg.P("Y")) * "string")] = "mystring",
196 [lpeg.C(lpeg.P("m") * (lpeg.P("y") + lpeg.P("Y")) * "string")] = "mYstring"
198 for pattern, value in pairs(matchedValues) do
199 local matched = false
200 local function mustBeCalled(capturedName, ...)
201 assert_equal(value, capturedName)
202 matched = true
204 matched = false
205 local strict = {
206 calls = { defs = {
207 [pattern] = mustBeCalled
210 json.decode.getDecoder(strict)(value .. "(true)")
211 assert_true(matched, "Value <" .. value .. "> did not match the given pattern")