1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +00:00

Revamp dump

This commit is contained in:
Lars Mueller 2025-04-24 21:12:12 +02:00 committed by Lars Müller
parent 98b2edeb11
commit 9ad23e4384
3 changed files with 240 additions and 55 deletions

View file

@ -232,8 +232,122 @@ describe("math", function()
end)
describe("dump", function()
it("avoids misleading rounding of floating point numbers", function()
assert.equal("0.3", dump(0.3))
assert.equal("0.30000000000000004", dump(0.1 + 0.2))
local function test_expression(expr)
local chunk = assert(loadstring("return " .. expr))
local refs = {}
setfenv(chunk, {
setref = function(id)
refs[id] = {}
return function(fields)
for k, v in pairs(fields) do
refs[id][k] = v
end
return refs[id]
end
end,
getref = function(id)
return assert(refs[id])
end,
})
assert.equal(expr, dump(chunk()))
end
it("nil", function()
test_expression("nil")
end)
it("booleans", function()
test_expression("false")
test_expression("true")
end)
describe("numbers", function()
it("formats integers nicely", function()
test_expression("42")
end)
it("avoids misleading rounding", function()
test_expression("0.3")
assert.equal("0.30000000000000004", dump(0.1 + 0.2))
end)
end)
it("strings", function()
test_expression('"hello world"')
test_expression([["hello \"world\""]])
end)
describe("tables", function()
it("empty", function()
test_expression("{}")
end)
it("lists", function()
test_expression([[
{
false,
true,
"foo",
1,
2,
}]])
end)
it("number keys", function()
test_expression([[
{
[0.5] = false,
[1.5] = true,
[2.5] = "foo",
}]])
end)
it("dicts", function()
test_expression([[{
a = 1,
b = 2,
c = 3,
}]])
end)
it("mixed", function()
test_expression([[{
a = 1,
b = 2,
c = 3,
["d e"] = true,
"foo",
"bar",
}]])
end)
it("nested", function()
test_expression([[{
a = {
1,
{},
},
b = "foo",
c = {
[0.5] = 0.1,
[1.5] = 0.2,
},
}]])
end)
it("circular references", function()
test_expression([[setref(1){
child = {
parent = getref(1),
},
other_child = {
parent = getref(1),
},
}]])
end)
it("supports variable indent", function()
assert.equal('{1,2,3,{foo = "bar",},}', dump({1, 2, 3, {foo = "bar"}}, ""))
assert.equal('{\n "x",\n "y",\n}', dump({"x", "y"}, " "))
end)
end)
end)