Software /
code /
prosody
Comparison
spec/util_json_spec.lua @ 8236:4878e4159e12
Port tests to the `busted` test runner
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Fri, 15 Sep 2017 17:07:57 -0400 |
child | 8562:a6188f5d5bb5 |
comparison
equal
deleted
inserted
replaced
8235:7d9a2c200736 | 8236:4878e4159e12 |
---|---|
1 | |
2 local json = require "util.json"; | |
3 | |
4 describe("util.json", function() | |
5 describe("#encode()", function() | |
6 it("should work", function() | |
7 local function test(f, j, e) | |
8 if e then | |
9 assert.are.equal(f(j), e); | |
10 end | |
11 assert.are.equal(f(j), f(json.decode(f(j)))); | |
12 end | |
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]"); | |
18 end); | |
19 end); | |
20 | |
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); | |
27 end); | |
28 end); | |
29 | |
30 describe("testcases", function() | |
31 | |
32 local valid_data = {}; | |
33 local invalid_data = {}; | |
34 | |
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"; | |
36 | |
37 setup(function() | |
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")); | |
44 assert(f:close()); | |
45 if skip:find(name) then | |
46 -- Skip | |
47 elseif name:match("^pass") then | |
48 valid_data[name] = content; | |
49 elseif name:match("^fail") then | |
50 invalid_data[name] = content; | |
51 end | |
52 end | |
53 end | |
54 end) | |
55 | |
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)); | |
60 end | |
61 end); | |
62 | |
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)); | |
67 end | |
68 end); | |
69 end) | |
70 end); |