Skip to content

Commit abc357a

Browse files
committed
Initial test cases
1 parent 50245e7 commit abc357a

File tree

6 files changed

+155
-0
lines changed

6 files changed

+155
-0
lines changed

.github/workflows/lest.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- name: Run Lest
2+
uses: TAServers/lest@v1.0.6

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ qjson.lua : 7e-07s [x1] (min: 0s, max 1e-06s)
2424
local json = require "qjson"
2525
print(json.encode {
2626
hello = "world!",
27+
qjson = { "fast", "simple", "tiny" }
2728
})
2829

2930
--[[

lest.config.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
return {
2+
testMatch = { "tests/.+%.test%.lua" },
3+
}

tests/decode.test.lua

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
local qjson = require "qjson"
2+
3+
it("should parse an array of numbers", function()
4+
expect(qjson.decode("[1, 2, 3, 4]")).toEqual({ 1, 2, 3, 4 })
5+
end)
6+
7+
it("should parse an array of strings", function()
8+
expect(qjson.decode("[\"Hello, \", \"world!\"]")).toEqual({ "Hello, ", "world!" })
9+
end)
10+
11+
it("should parse an array of booleans", function()
12+
expect(qjson.decode("[true, false]")).toEqual({ "true", "false" })
13+
end)
14+
15+
it("should parse an array of booleans into strings", function()
16+
expect(qjson.decode("[true, false]")).toEqual({ "true", "false" })
17+
end)
18+
19+
it("should parse an array of null into strings", function()
20+
expect(qjson.decode("[null, null]")).toEqual({ "null", "null" })
21+
end)
22+
23+
it("should parse an empty array", function()
24+
expect(qjson.decode("[]")).toEqual({})
25+
end)
26+
27+
it("should not care about whitespace", function()
28+
expect(qjson.decode(" [ 1 , null ] ")).toEqual({ 1, "null" })
29+
end)
30+
31+
it("should parse an object holding a string", function()
32+
expect(qjson.decode([[{"qux": "baz"}]])).toEqual({ qux = "baz" })
33+
end)
34+
35+
it("should parse an object holding an integer", function()
36+
expect(qjson.decode([[{ "hmm": 5 }]])).toEqual({ hmm = 5 })
37+
end)
38+
39+
it("should parse an object holding a decimal number", function()
40+
expect(qjson.decode([[{ "hmm": 5.2 }]])).toEqual({ hmm = 5.2 })
41+
end)
42+
43+
it("should parse an object holding a negative integer", function()
44+
expect(qjson.decode([[{ "hmm": -5 }]])).toEqual({ hmm = -5 })
45+
end)
46+
47+
it("should parse an object holding a negative decimal number", function()
48+
expect(qjson.decode([[{ "hmm": -5.2 }]])).toEqual({ hmm = -5.2 })
49+
end)
50+
51+
it("should parse an object holding a negative decimal number with exponents", function()
52+
expect(qjson.decode([[{ "hmm": -5.2e2 }]])).toEqual({ hmm = -5.2e2 })
53+
end)
54+
55+
it("should parse an object holding a positine decimal number with exponents 2", function()
56+
expect(qjson.decode([[{ "hmm": 5.2E+2 }]])).toEqual({ hmm = 5.2E+2 })
57+
end)
58+
59+
it("should parse an object holding an array of numbers", function()
60+
expect(qjson.decode([[{ "hmm": [ 1, 2, 3 ] }]])).toEqual({ hmm = { 1, 2, 3 } })
61+
end)
62+
63+
it("should parse an object holding an empty object", function()
64+
expect(qjson.decode([[{ "hmm": {} }]])).toEqual({ hmm = {} })
65+
end)
66+
67+
it("should parse an object holding a list of empty objects", function()
68+
expect(qjson.decode([[{ "hmm": [ {}, {}, {} ] }]])).toEqual({ hmm = { {}, {}, {} } })
69+
end)
70+
71+
it("should parse an object holding a list of random values", function()
72+
expect(qjson.decode([[{ "hmm": [ 1, {}, "test", true ] }]])).toEqual({ hmm = { 1, {}, "test", "true" } })
73+
end)
74+
75+
it("should parse an object holding objects to five levels of depth", function()
76+
local json = [[
77+
{
78+
"foo": {
79+
"bar": {
80+
"baz": {
81+
"qux": {}
82+
}
83+
}
84+
}
85+
}
86+
]]
87+
88+
expect(qjson.decode(json)).toEqual({
89+
foo = {
90+
bar = {
91+
baz = {
92+
qux = {}
93+
}
94+
}
95+
}
96+
})
97+
end)

tests/encode.test.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local qjson = require "qjson"
2+
3+
it("should encode an empty table as an array", function()
4+
expect(qjson.encode({})).toBe("[]")
5+
end)
6+
7+
it("should encode a sequential table as an array", function()
8+
expect(qjson.encode({1, 2, 3})).toBe("[1,2,3]")
9+
end)
10+
11+
it("should encode a list of booleans", function()
12+
expect(qjson.encode({true, false, false})).toBe("[true,false,false]")
13+
end)
14+
15+
it("should encode a list of strings", function()
16+
expect(qjson.encode({"Hello", "world!"})).toBe('["Hello","world!"]')
17+
end)
18+
19+
it("should encode a list of strings with escapes", function()
20+
expect(qjson.encode({"Hello", "world\"!"})).toBe('["Hello","world\\"!"]')
21+
end)
22+
23+
it("should encode a lookup table", function()
24+
local encoded = qjson.encode({ foo = "bar", [1] = "qux" })
25+
26+
-- Todo: Better approach for this.
27+
local equality =
28+
encoded == [[{"foo": "bar","1": "qux"}]] or
29+
encoded == [[{"1": "qux","foo": "bar"}]]
30+
31+
expect(equality).toBeTruthy()
32+
end)

tests/readme.test.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local qjson = require "qjson"
2+
3+
test("readme encoding should work", function()
4+
local encoded = qjson.encode({ hello = "world!", qjson = { "fast", "simple", "tiny" } })
5+
6+
-- Todo: Better approach for this.
7+
local equality =
8+
encoded == [[{"qjson": ["fast","simple","tiny"],"hello": "world!"}]] or
9+
encoded == [[{"hello": "world!","qjson": ["fast","simple","tiny"]}]]
10+
11+
expect(equality).toBeTruthy()
12+
end)
13+
14+
test("readme decoding should work", function()
15+
local decoded = qjson.decode([[
16+
{ "foo": "bar" }
17+
]])
18+
19+
expect(decoded).toEqual({ foo = "bar" })
20+
end)

0 commit comments

Comments
 (0)