2 local json
= require
"util.json";
4 describe("util.json", function()
5 describe("#encode()", function()
6 it("should work", function()
7 local function test(f
, j
, e
)
9 assert.are
.equal(f(j
), e
);
11 assert.are
.equal(f(j
), f(json
.decode(f(j
))));
13 test(json
.encode
, json
.null
, "null")
14 test(json
.encode
, {}, "{}")
15 test(json
.encode
, {a
=1});
16 test(json
.encode
, {a
={1,2,3}});
17 test(json
.encode
, {1}, "[1]");
21 describe("#decode()", function()
22 it("should work", function()
23 local empty_array
= json
.decode("[]");
24 assert.are
.equal(type(empty_array
), "table");
25 assert.are
.equal(#empty_array
, 0);
26 assert.are
.equal(next(empty_array
), nil);
30 describe("testcases", function()
32 local valid_data
= {};
33 local invalid_data
= {};
35 local skip
= "fail1.json fail9.json fail18.json fail15.json fail13.json fail25.json fail26.json fail27.json fail28.json fail17.json pass1.json";
38 local lfs
= require
"lfs";
39 local path
= "spec/json";
40 for name
in lfs
.dir(path
) do
41 if name
:match("%.json$") then
42 local f
= assert(io
.open(path
.."/"..name
));
43 local content
= assert(f
:read("*a"));
45 if skip
:find(name
) then --luacheck: ignore 542
47 elseif name
:match("^pass") then
48 valid_data
[name
] = content
;
49 elseif name
:match("^fail") then
50 invalid_data
[name
] = content
;
56 it("should pass valid testcases", function()
57 for name
, content
in pairs(valid_data
) do
58 local parsed
, err
= json
.decode(content
);
59 assert(parsed
, name
..": "..tostring(err
));
63 it("should fail invalid testcases", function()
64 for name
, content
in pairs(invalid_data
) do
65 local parsed
, err
= json
.decode(content
);
66 assert(not parsed
, name
..": "..tostring(err
));