peachy/tests/test-csexp.lua

47 lines
1.2 KiB
Lua
Raw Normal View History

2024-07-18 06:18:18 +00:00
local csexp = require "csexp"
local serialize, parse = csexp.serialize, csexp.parse
local describe, it, expect = lester.describe, lester.it, lester.expect
describe("csexp", function()
2024-07-18 08:24:41 +00:00
describe("serialize", function()
it("handles empty string", function()
expect.equal(serialize"", "0:")
end)
it("handles nonempty string", function()
expect.equal(serialize"abcd", "4:abcd")
end)
it("handles empty list", function()
expect.equal(serialize{}, "()")
end)
it("converts non-string values", function()
expect.equal(serialize(12345), "5:12345")
end)
it("inverts parse", function()
local original = "(10:abcdefghij(5:hello(5:world)7:goodbye))"
local obj = parse(original)
local serialized = serialize(obj)
expect.equal(serialized, original)
end)
end)
2024-07-18 06:18:18 +00:00
2024-07-18 08:24:41 +00:00
describe("parse", function()
local rejects = {
["malformed lists"] = "(5:hello5:world",
["early eof in strings"] = "5:1234",
["extra chars in strings"] = "5:123456",
["extra chars after list"] = "()0:"
}
for description, value in pairs(rejects) do
it("rejects "..description, function()
expect.fail(function()
parse(value)
end)
end)
end
end)
2024-07-18 06:18:18 +00:00
end)