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)
12 _ENV
= lunit
.module("lunit-calls", 'seeall')
14 module("lunit-calls", lunit
.testcase
, package
.seeall
)
18 -- Ensure that the decoder is reset
19 _G
["decode"] = json
.decode
.getDecoder(false)
32 function test_identity()
33 local function testFunction(capturedName
, ...)
34 assert_equal('call', capturedName
)
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
])
51 assert_nil(next(decoded
))
53 assert_equal(v
, decoded
)
58 -- Test for a function that throws
59 function test_function_failure()
60 local function testFunction(...)
61 error("CANNOT CONTINUE")
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()
77 -- Test for a function that is not a function
78 function test_not_a_function_fail()
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()
87 }, allowUndefined
= false }
89 json
.decode
.getDecoder(strict
)
94 function test_not_permitted_fail()
97 defs
= { call = false }
100 local decoder
= json
.decode
.getDecoder(strict
)
101 assert_error(function()
106 function test_permitted()
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_nested()
120 defs
= { call = true, other
= true }
123 local decoder
= json
.decode
.getDecoder(strict
)
124 assert(decoder("call(call(1))").name
== 'call')
125 assert(decoder("other(call(1))").name
== 'other')
128 function test_not_defined_fail()
129 local decoder
= json
.decode
.getDecoder({
131 allowUndefined
= false
134 assert_error(function()
139 function test_not_defined_succeed()
140 local decoder
= json
.decode
.getDecoder({
142 allowUndefined
= true
145 assert(decoder("call(1)").name
== 'call')
148 -- Test for a name that is not a string
149 function test_name_not_string()
151 true, false, 0/0, 1/0, -1/0, 0, 1, {}, function() end, coroutine
.create(function() end)
153 for _
, v
in ipairs(notString
) do
154 assert_error(function()
159 calls
= { defs
= defs
}
161 json
.decode
.getDecoder(strict
)
166 -- Test for a name that is a string or a pattern
167 function test_name_matches_string_or_pattern()
168 local matchedValues
= {
169 ["mystring"] = "mystring",
170 [lpeg
.C(lpeg
.P("m") * (lpeg
.P("y") + lpeg
.P("Y")) * "string")] = "mystring",
171 [lpeg
.C(lpeg
.P("m") * (lpeg
.P("y") + lpeg
.P("Y")) * "string")] = "mYstring"
173 for pattern
, value
in pairs(matchedValues
) do
174 local matched
= false
175 local function mustBeCalled(capturedName
, ...)
176 assert_equal(value
, capturedName
)
182 [pattern
] = mustBeCalled
185 json
.decode
.getDecoder(strict
)(value
.. "(true)")
186 assert_true(matched
, "Value <" .. value
.. "> did not match the given pattern")