-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstringify.test.lua
More file actions
325 lines (292 loc) · 7.62 KB
/
Copy pathstringify.test.lua
File metadata and controls
325 lines (292 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
local mods = require "mods"
local kw = mods.keyword
local is_luajit = mods.runtime.is_luajit
local stringify = mods.stringify
local fmt = string.format
describe("mods.stringify", function()
local nan = tostring(0 / 0)
it("renders primitives", function()
local fn = function() end
local co = coroutine.create(fn)
-- stylua: ignore
local tests = {
{ nil , "nil" },
{ true , "true" },
{ false , "false" },
{ 42 , "42" },
{ 0/0 , nan },
{ 1/0 , "inf" },
{ -1/0 , "-inf" },
{ 'He said "hi"' , [['He said "hi"']] },
{ fn , tostring(fn) },
{ co , tostring(co) },
}
for i = 1, #tests do
local input, expected = unpack(tests[i] --[[@as {[1]:any, [2]:string}]], 1, 2)
assert.are.equal(expected, stringify(input))
end
end)
it("renders arrays as implicit entries by default", function()
assert.are.equal('{\n "a",\n "b",\n "c"\n}', stringify({ "a", "b", "c" }))
assert.are.equal('{"a","b","c"}', stringify({ "a", "b", "c" }, { newline = "" }))
end)
it("optionally keeps contiguous array keys explicit", function()
local opts = { omit_array_keys = false }
assert.are.equal('{\n [1] = "a",\n [2] = "b",\n [3] = "c"\n}', stringify({ "a", "b", "c" }, opts))
assert.are.equal(
'{[1]="a",[2]="b",[3]="c"}',
stringify({ "a", "b", "c" }, { omit_array_keys = false, newline = "" })
)
end)
it("renders empty arrays as empty braces", function()
assert.are.equal("{}", stringify({}))
end)
it("renders dictionaries with indentation by default", function()
local input = { hello = "world", answer = 42 }
local expected = '{\n answer = 42,\n hello = "world"\n}'
assert.are.equal(expected, stringify(input))
end)
it("renders non-identifier keys with brackets", function()
local input = {
["not-valid"] = true,
["with space"] = "x",
[false] = "no",
}
local expected = [[
{
["not-valid"] = true,
["with space"] = "x",
[false] = "no"
}]]
assert.are.equal(expected, stringify(input))
end)
it("renders mixed tables as keyed tables", function()
local input = {
"a",
"b",
name = "Ada",
nested = {
"x",
role = "Scientist",
},
}
local expected = [[
{
"a",
"b",
name = "Ada",
nested = {
"x",
role = "Scientist"
}
}]]
assert.are.equal(expected, stringify(input))
end)
it("renders sparse numeric tables without nil placeholders", function()
local input = {
[1] = "a",
[3] = "c",
name = "Ada",
}
local expected = [[
{
"a",
[3] = "c",
name = "Ada"
}]]
assert.are.equal(expected, stringify(input))
end)
it("renders arrays with gaps as keyed tables", function()
local input = { "a", nil, "b" }
local expected = '{\n "a",\n [3] = "b"\n}'
assert.are.equal(expected, stringify(input))
end)
it("keeps integer keys after gaps when omitting array keys", function()
local input = {
"a",
nil,
"b",
nested = { "x", "y", nil, "z" },
}
local expected = [[
{
"a",
[3] = "b",
nested = {
"x",
"y",
[4] = "z"
}
}]]
assert.are.equal(expected, stringify(input))
end)
it("renders sparse mixed tables as keyed tables", function()
local input = {
"a",
nil,
"b",
role = "Scientist",
nested = { "x", nil, "y" },
}
local expected = [[
{
"a",
[3] = "b",
nested = {
"x",
[3] = "y"
},
role = "Scientist"
}]]
assert.are.equal(expected, stringify(input))
end)
it("renders empty tables and nested tables", function()
local input = {
empty = {},
list = { "x", "y" },
user = { name = "Ada" },
}
local expected = [[
{
empty = {},
list = {
"x",
"y"
},
user = {
name = "Ada"
}
}]]
assert.are.equal(expected, stringify(input))
end)
it("ignores metatables when rendering tables", function()
local input = setmetatable({
__tostring = "",
name = "Ada",
nested = setmetatable({
__key = "nested",
ok = true,
}, { __index = { hidden = "no" } }),
}, { __index = { ignored = true } })
local expected = [[
{
__tostring = "",
name = "Ada",
nested = {
__key = "nested",
ok = true
}
}]]
assert.are.equal(expected, stringify(input))
end)
it("errors on non-function replacer", function()
assert.has_error(function()
stringify({}, { replacer = true })
end, "stringify.opts.replacer: function expected, got boolean")
end)
it("supports replacer functions for filtering", function()
local input = {
[1] = "one",
[2] = "two",
[9] = "nine",
[false] = "no",
[true] = "yes",
drop = "hidden",
name = "Ada",
role = "Engineer",
nested = {
name = "Grace",
role = "Scientist",
[2] = "two",
team = "Compiler",
drop = "hidden",
},
}
local function replacer(k, v)
if k == "name" then
return v:upper()
end
if k ~= "drop" then
return v
end
end
local expected = [[
{
"one",
"two",
[9] = "nine",
name = "ADA",
nested = {
[2] = "two",
name = "GRACE",
role = "Scientist",
team = "Compiler"
},
role = "Engineer",
[false] = "no",
[true] = "yes"
}]]
assert.are.equal(expected, stringify(input, { replacer = replacer }))
end)
it("supports custom indentation", function()
local input = { outer = { inner = true } }
local expected = [[
{
..outer = {
....inner = true
..}
}]]
assert.are.equal(expected, stringify(input, { indent = ".." }))
end)
it("errors on invalid indentation types", function()
assert.has_error(function()
stringify({}, { indent = true })
end, "stringify.opts.indent: string expected, got boolean")
end)
it("supports custom newlines", function()
local input = { outer = { inner = true } }
local expected = "{\r\n outer = {\r\n inner = true\r\n }\r\n}"
assert.are.equal(expected, stringify(input, { newline = "\r\n" }))
end)
it("errors on invalid newline types", function()
assert.has_error(function()
stringify({}, { newline = true })
end, "stringify.opts.newline: string expected, got boolean")
end)
it("errors on invalid option types", function()
assert.has_error(function()
stringify({}, true)
end, "bad argument #2 (table expected, got boolean)")
assert.has_error(function()
stringify({}, { omit_array_keys = "yes" })
end, "stringify.opts.omit_array_keys: boolean expected, got string")
end)
it("supports inline formatting", function()
local input = { key = "value", key2 = { nested = { inner = true } }, ["not-valid"] = true }
local expected = '{key="value",key2={nested={inner=true}},["not-valid"]=true}'
assert.are.equal(expected, stringify(input, { newline = "" }))
end)
it("renders circular references without crashing", function()
local root = { title = "root" }
local child = { name = "child" }
root.child = child
root.self = root
child.parent = root
local expected = [[
{
child = {
name = "child",
parent = <cycle>
},
self = <cycle>,
title = "root"
}]]
assert.are.equal(expected, stringify(root))
end)
for _, v in ipairs(kw.kwlist()) do
it(fmt("brackets reserved keys for %q", v), function()
local expected = '{\n ["' .. v .. '"] = true\n}'
assert.are.equal(expected, stringify({ [v] = true }))
end)
end
end)