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 module("lunit-calls", lunit
.testcase
, package
.seeall
)
14 -- Ensure that the decoder is reset
15 _G
["decode"] = json
.decode
.getDecoder(false)
28 function test_identity()
29 local function testFunction(capturedName
, ...)
30 assert_equal('call', capturedName
)
38 local decode
= json
.decode
.getDecoder(strict
)
39 for i
, v
in ipairs(values
) do
40 local str
= "call(" .. encode(v
) .. ")"
41 local decoded
= decode(str
)
42 if type(decoded
) == 'table' then
43 for k2
, v2
in pairs(v
) do
44 assert_equal(v2
, decoded
[k2
])
47 assert_nil(next(decoded
))
49 assert_equal(v
, decoded
)
54 -- Test for a function that throws
55 function test_function_failure()
56 local function testFunction(...)
57 error("CANNOT CONTINUE")
64 local decode
= json
.decode
.getDecoder(strict
)
65 for i
, v
in ipairs(values
) do
66 local str
= "call(" .. encode(v
) .. ")"
67 assert_error(function()
73 -- Test for a function that is not a function
74 function test_not_a_function_fail()
76 0/0, 1/0, -1/0, 0, 1, "Hello", {}, coroutine
.create(function() end)
78 for _
, v
in ipairs(notFunction
) do
79 assert_error(function()
83 }, allowUndefined
= false }
85 json
.decode
.getDecoder(strict
)
90 function test_not_permitted_fail()
93 defs
= { call = false }
96 local decoder
= json
.decode
.getDecoder(strict
)
97 assert_error(function()
102 function test_permitted()
105 defs
= { call = true }
108 local decoder
= json
.decode
.getDecoder(strict
)
109 assert(decoder("call(1)").name
== 'call')
112 function test_not_defined_fail()
113 local decoder
= json
.decode
.getDecoder({
115 allowUndefined
= false
118 assert_error(function()
123 function test_not_defined_succeed()
124 local decoder
= json
.decode
.getDecoder({
126 allowUndefined
= true
129 assert(decoder("call(1)").name
== 'call')
132 -- Test for a name that is not a string
133 function test_name_not_string()
135 true, false, 0/0, 1/0, -1/0, 0, 1, {}, function() end, coroutine
.create(function() end)
137 for _
, v
in ipairs(notString
) do
138 assert_error(function()
143 calls
= { defs
= defs
}
145 json
.decode
.getDecoder(strict
)
150 -- Test for a name that is a string or a pattern
151 function test_name_matches_string_or_pattern()
152 local matchedValues
= {
153 ["mystring"] = "mystring",
154 [lpeg
.C(lpeg
.P("m") * (lpeg
.P("y") + lpeg
.P("Y")) * "string")] = "mystring",
155 [lpeg
.C(lpeg
.P("m") * (lpeg
.P("y") + lpeg
.P("Y")) * "string")] = "mYstring"
157 for pattern
, value
in pairs(matchedValues
) do
158 local matched
= false
159 local function mustBeCalled(capturedName
, ...)
160 assert_equal(value
, capturedName
)
166 [pattern
] = mustBeCalled
169 json
.decode
.getDecoder(strict
)(value
.. "(true)")
170 assert_true(matched
, "Value <" .. value
.. "> did not match the given pattern")