Debian Codegraph @

CorsixTH/Luatest/spec/utility_spec.lua

raw
1 require("utility")
2 if _VERSION ~= "Lua 5.1" then
3 loadstring = load -- luacheck: ignore 121
4 end
5
6 describe("serialize", function()
7 it("serializes a basic string", function()
8 local res = serialize('hello')
9 assert.equal('[[hello]]', res)
10 end)
11
12 it("serializes a string that includes ]]", function()
13 local res = serialize('he]]o')
14 assert.equal('[=[he]]o]=]', res)
15 end)
16
17 it("serializes an array of primitives", function()
18 local res = serialize({'hello', 'happy', 'people', 'everywhere', 42, false})
19 assert.equal('{[[hello]],[[happy]],[[people]],[[everywhere]],42,false}', res)
20 end)
21
22 it("serializes a pretty array", function()
23 local res = serialize({'hello', 'happy', 'people', 'everywhere', 42, false}, {pretty = true})
24 assert.equal([=[{
25 [[hello]],
26 [[happy]],
27 [[people]],
28 [[everywhere]],
29 42,
30 false
31 }]=], res)
32 end)
33
34 it("serializes a simple key value table", function()
35 local test_tbl = {foo='bar', [true]='candy', bar=42}
36 local res = serialize(test_tbl)
37 local res_tbl = loadstring_envcall("return " .. res)({})
38 assert.same(test_tbl, res_tbl)
39 end)
40
41 it("serializes a nested table", function()
42 local test_tbl = {foo='bar', bar={hello='world'}}
43 local res = serialize(test_tbl)
44 local res_tbl = loadstring_envcall("return " .. res)({})
45 assert.same(test_tbl, res_tbl)
46 end)
47
48 it("serializes a table with nested keys", function()
49 local test_tbl = {[{hello='world'}] = true, [1] = 'tree'}
50 local res = serialize(test_tbl)
51 local res_tbl = loadstring_envcall("return " .. res)({})
52
53 local found_tbl = false
54 for k, _ in pairs(res_tbl) do
55 if type(k) == 'table' then
56 assert.same({hello='world'}, k)
57 found_tbl = true
58 break
59 end
60 end
61
62 if not found_tbl then
63 assert("table key not found")
64 end
65
66 assert.equals(test_tbl[1], res_tbl[1])
67 end)
68
69 it("respects max_depth if set", function()
70 local test_tbl = {[{hello='world'}] = true, [1] = 'tree'}
71 local res = serialize(test_tbl, {max_depth=1})
72
73 assert.has.match('{...}', res, nil, true)
74
75 res = serialize(test_tbl, {max_depth=2})
76 assert.has_no.match('{...}', res, nil, true)
77 end)
78
79 it("detects cycles", function()
80 local test_tbl = {[{hello='world'}] = true, [1] = 'tree'}
81 test_tbl[2] = test_tbl
82
83 local res = serialize(test_tbl, {detect_cycles=true})
84 assert.has.match('<reference loop>', res, nil, true)
85 end)
86
87 it("doesn't report cycles with only max_depth", function()
88 local test_tbl = {[{hello='world'}] = true, [1] = 'tree'}
89 test_tbl[2] = test_tbl
90
91 local res = serialize(test_tbl, {max_depth=4})
92 assert.has_no.match('<reference loop>', res, nil, true)
93 assert.has.match('{...}', res, nil, true)
94 end)
95
96 it("serializes nil", function()
97 local res = serialize(nil)
98 assert.equals('nil', res)
99 end)
100 end)
101
102 describe("array_join", function()
103 it("joins a basic array of numbers to a comma separated string", function()
104 local res = array_join({5,3,1,0})
105 assert.equals("5,3,1,0", res)
106 end)
107
108 it("joins a string array with custom separator", function()
109 local res = array_join({"a"," long"," hard","able"}," very")
110 assert.equals("a very long very hard veryable", res)
111 end)
112
113 it("converts a non-table to a string", function()
114 local res = array_join(1)
115 assert.equals("1", res)
116 end)
117 end)