base: drops obsolute SCM rockspecs
[luajson.git] / tests / lunit-calls.lua
blobcaa57e961393d1321ce79457114887a5b521a427
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)
13 function setup()
14 -- Ensure that the decoder is reset
15 _G["decode"] = json.decode.getDecoder(false)
16 end
18 local values = {
21 0.2,
22 "Hello",
23 true,
24 {hi=true},
25 {1,2}
28 function test_identity()
29 local function testFunction(capturedName, ...)
30 assert_equal('call', capturedName)
31 return (...)
32 end
33 local strict = {
34 calls = { defs = {
35 call = testFunction
36 } }
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])
45 decoded[k2] = nil
46 end
47 assert_nil(next(decoded))
48 else
49 assert_equal(v, decoded)
50 end
51 end
52 end
54 -- Test for a function that throws
55 function test_function_failure()
56 local function testFunction(...)
57 error("CANNOT CONTINUE")
58 end
59 local strict = {
60 calls = { defs = {
61 call = testFunction
62 } }
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()
68 decode(str)
69 end)
70 end
71 end
73 -- Test for a function that is not a function
74 function test_not_a_function_fail()
75 local notFunction = {
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()
80 local strict = {
81 calls = { defs = {
82 call = v
83 }, allowUndefined = false }
85 json.decode.getDecoder(strict)
86 end)
87 end
88 end
90 function test_not_permitted_fail()
91 local strict = {
92 calls = {
93 defs = { call = false }
96 local decoder = json.decode.getDecoder(strict)
97 assert_error(function()
98 decoder("call(1)")
99 end)
102 function test_permitted()
103 local strict = {
104 calls = {
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({
114 calls = {
115 allowUndefined = false
118 assert_error(function()
119 decoder("call(1)")
120 end)
123 function test_not_defined_succeed()
124 local decoder = json.decode.getDecoder({
125 calls = {
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()
134 local notString = {
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()
139 local defs = {
140 [v] = function() end
142 local strict = {
143 calls = { defs = defs }
145 json.decode.getDecoder(strict)
146 end)
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)
161 matched = true
163 matched = false
164 local strict = {
165 calls = { defs = {
166 [pattern] = mustBeCalled
169 json.decode.getDecoder(strict)(value .. "(true)")
170 assert_true(matched, "Value <" .. value .. "> did not match the given pattern")