Persist: unbreak serialize for serpent

reviewable/pr9603/r1
NiLuJe 2 years ago
parent 39e0f84959
commit b1bcafa833

@ -174,7 +174,7 @@ local codecs = {
writes_to_file = false, writes_to_file = false,
serialize = function(t) serialize = function(t)
local ok, str = serpent.dump(t) local ok, str = pcall(serpent.dump, t)
if not ok then if not ok then
return nil, "cannot serialize " .. tostring(t) .. " (" .. str .. ")" return nil, "cannot serialize " .. tostring(t) .. " (" .. str .. ")"
end end

@ -2,7 +2,6 @@ describe("Persist module", function()
local Persist local Persist
local sample local sample
local bitserInstance, luajitInstance, zstdInstance, dumpInstance, serpentInstance local bitserInstance, luajitInstance, zstdInstance, dumpInstance, serpentInstance
local ser, deser, str, tab
local fail = { a = function() end, } local fail = { a = function() end, }
local function arrayOf(n) local function arrayOf(n)
@ -80,23 +79,27 @@ describe("Persist module", function()
end) end)
it("should return standalone serializers/deserializers", function() it("should return standalone serializers/deserializers", function()
tab = sample local tab = sample
for _, codec in ipairs({"dump", "serpent", "bitser", "luajit", "zstd"}) do -- NOTE: zstd only deser from a *file*, not a string.
for _, codec in ipairs({"dump", "serpent", "bitser", "luajit"}) do
assert.is_true(Persist.getCodec(codec).id == codec) assert.is_true(Persist.getCodec(codec).id == codec)
ser = Persist.getCodec(codec).serialize local ser = Persist.getCodec(codec).serialize
deser = Persist.getCodec(codec).deserialize local deser = Persist.getCodec(codec).deserialize
str = ser(tab) local str = ser(tab)
assert.are.same(deser(str), tab) local t, err = deser(str)
str, ser, deser = nil, nil, nil if not t then
print(codec, "deser failed:", err)
end
assert.are.same(t, tab)
end end
end) end)
it("should work with huge tables", function() it("should work with huge tables", function()
for _, codec in ipairs({"bitser", "luajit", "zstd"}) do local tab = arrayOf(100000)
tab = arrayOf(100000) for _, codec in ipairs({"bitser", "luajit"}) do
ser = Persist.getCodec(codec).serialize local ser = Persist.getCodec(codec).serialize
deser = Persist.getCodec(codec).deserialize local deser = Persist.getCodec(codec).deserialize
str = ser(tab) local str = ser(tab)
assert.are.same(deser(str), tab) assert.are.same(deser(str), tab)
end end
end) end)
@ -104,9 +107,9 @@ describe("Persist module", function()
it("should fail to serialize functions", function() it("should fail to serialize functions", function()
for _, codec in ipairs({"dump", "bitser", "luajit", "zstd"}) do for _, codec in ipairs({"dump", "bitser", "luajit", "zstd"}) do
assert.is_true(Persist.getCodec(codec).id == codec) assert.is_true(Persist.getCodec(codec).id == codec)
ser = Persist.getCodec(codec).serialize local ser = Persist.getCodec(codec).serialize
deser = Persist.getCodec(codec).deserialize local deser = Persist.getCodec(codec).deserialize
str = ser(fail) local str = ser(fail)
assert.are_not.same(deser(str), fail) assert.are_not.same(deser(str), fail)
end end
end) end)
@ -114,9 +117,9 @@ describe("Persist module", function()
it("should successfully serialize functions", function() it("should successfully serialize functions", function()
for _, codec in ipairs({"serpent"}) do for _, codec in ipairs({"serpent"}) do
assert.is_true(Persist.getCodec(codec).id == codec) assert.is_true(Persist.getCodec(codec).id == codec)
ser = Persist.getCodec(codec).serialize local ser = Persist.getCodec(codec).serialize
deser = Persist.getCodec(codec).deserialize local deser = Persist.getCodec(codec).deserialize
str = ser(fail) local str = ser(fail)
assert.are_not.same(deser(str), fail) assert.are_not.same(deser(str), fail)
end end
end) end)

Loading…
Cancel
Save