2021-01-13 10:45:00 +00:00
|
|
|
describe("Persist module", function()
|
|
|
|
local Persist
|
|
|
|
local sample
|
Logger: Use serpent instead of dump (#9588)
* Persist: support serpent, and use by default over dump (as we assume consistency > readability in Persist).
* Logger/Dbg: Use serpent instead of dump to dump tables (it's slightly more compact, honors __tostring, and will tag tables with their ref, which can come in handy when debugging).
* Dbg: Don't duplicate Logger's log function, just use it directly.
* Fontlist/ConfigDialog: Use serpent for the debug dump.
* Call `os.setlocale(C, "numeric")` on startup instead of peppering it around dump calls. It's process-wide, so it didn't make much sense.
* Trapper: Use LuaJIT's serde facilities instead of dump. They're more reliable in the face of funky input, much faster, and in this case, the data never makes it to human eyes, so a human-readable format didn't gain us anything.
2022-10-06 00:21:03 +00:00
|
|
|
local bitserInstance, luajitInstance, zstdInstance, dumpInstance, serpentInstance
|
2021-01-13 10:45:00 +00:00
|
|
|
local fail = { a = function() end, }
|
|
|
|
|
|
|
|
local function arrayOf(n)
|
|
|
|
assert(type(n) == "number", "wrong type (expected number)")
|
|
|
|
local t = {}
|
|
|
|
for i = 1, n do
|
|
|
|
table.insert(t, i, {
|
|
|
|
a = "sample " .. tostring(i),
|
|
|
|
b = true,
|
|
|
|
c = nil,
|
|
|
|
d = i,
|
|
|
|
e = {
|
|
|
|
f = {
|
|
|
|
g = nil,
|
|
|
|
h = false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
setup(function()
|
|
|
|
require("commonrequire")
|
|
|
|
Persist = require("persist")
|
|
|
|
bitserInstance = Persist:new{ path = "test.dat", codec = "bitser" }
|
2021-04-13 16:11:39 +00:00
|
|
|
luajitInstance = Persist:new{ path = "testj.dat", codec = "luajit" }
|
Logger: Use serpent instead of dump (#9588)
* Persist: support serpent, and use by default over dump (as we assume consistency > readability in Persist).
* Logger/Dbg: Use serpent instead of dump to dump tables (it's slightly more compact, honors __tostring, and will tag tables with their ref, which can come in handy when debugging).
* Dbg: Don't duplicate Logger's log function, just use it directly.
* Fontlist/ConfigDialog: Use serpent for the debug dump.
* Call `os.setlocale(C, "numeric")` on startup instead of peppering it around dump calls. It's process-wide, so it didn't make much sense.
* Trapper: Use LuaJIT's serde facilities instead of dump. They're more reliable in the face of funky input, much faster, and in this case, the data never makes it to human eyes, so a human-readable format didn't gain us anything.
2022-10-06 00:21:03 +00:00
|
|
|
zstdInstance = Persist:new{ path = "test.zst", codec = "zstd" }
|
|
|
|
dumpInstance = Persist:new{ path = "test.lua", codec = "dump" }
|
|
|
|
serpentInstance = Persist:new{ path = "tests.lua", codec = "serpent" }
|
2021-01-13 10:45:00 +00:00
|
|
|
sample = arrayOf(1000)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should save a table to file", function()
|
|
|
|
assert.is_true(bitserInstance:save(sample))
|
2021-04-13 16:11:39 +00:00
|
|
|
assert.is_true(luajitInstance:save(sample))
|
Logger: Use serpent instead of dump (#9588)
* Persist: support serpent, and use by default over dump (as we assume consistency > readability in Persist).
* Logger/Dbg: Use serpent instead of dump to dump tables (it's slightly more compact, honors __tostring, and will tag tables with their ref, which can come in handy when debugging).
* Dbg: Don't duplicate Logger's log function, just use it directly.
* Fontlist/ConfigDialog: Use serpent for the debug dump.
* Call `os.setlocale(C, "numeric")` on startup instead of peppering it around dump calls. It's process-wide, so it didn't make much sense.
* Trapper: Use LuaJIT's serde facilities instead of dump. They're more reliable in the face of funky input, much faster, and in this case, the data never makes it to human eyes, so a human-readable format didn't gain us anything.
2022-10-06 00:21:03 +00:00
|
|
|
assert.is_true(zstdInstance:save(sample))
|
2021-01-13 10:45:00 +00:00
|
|
|
assert.is_true(dumpInstance:save(sample))
|
Logger: Use serpent instead of dump (#9588)
* Persist: support serpent, and use by default over dump (as we assume consistency > readability in Persist).
* Logger/Dbg: Use serpent instead of dump to dump tables (it's slightly more compact, honors __tostring, and will tag tables with their ref, which can come in handy when debugging).
* Dbg: Don't duplicate Logger's log function, just use it directly.
* Fontlist/ConfigDialog: Use serpent for the debug dump.
* Call `os.setlocale(C, "numeric")` on startup instead of peppering it around dump calls. It's process-wide, so it didn't make much sense.
* Trapper: Use LuaJIT's serde facilities instead of dump. They're more reliable in the face of funky input, much faster, and in this case, the data never makes it to human eyes, so a human-readable format didn't gain us anything.
2022-10-06 00:21:03 +00:00
|
|
|
assert.is_true(serpentInstance:save(sample))
|
2021-01-13 10:45:00 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("should generate a valid file", function()
|
|
|
|
assert.is_true(bitserInstance:exists())
|
|
|
|
assert.is_true(bitserInstance:size() > 0)
|
|
|
|
assert.is_true(type(bitserInstance:timestamp()) == "number")
|
2021-04-13 16:11:39 +00:00
|
|
|
|
|
|
|
assert.is_true(luajitInstance:exists())
|
|
|
|
assert.is_true(luajitInstance:size() > 0)
|
|
|
|
assert.is_true(type(luajitInstance:timestamp()) == "number")
|
Logger: Use serpent instead of dump (#9588)
* Persist: support serpent, and use by default over dump (as we assume consistency > readability in Persist).
* Logger/Dbg: Use serpent instead of dump to dump tables (it's slightly more compact, honors __tostring, and will tag tables with their ref, which can come in handy when debugging).
* Dbg: Don't duplicate Logger's log function, just use it directly.
* Fontlist/ConfigDialog: Use serpent for the debug dump.
* Call `os.setlocale(C, "numeric")` on startup instead of peppering it around dump calls. It's process-wide, so it didn't make much sense.
* Trapper: Use LuaJIT's serde facilities instead of dump. They're more reliable in the face of funky input, much faster, and in this case, the data never makes it to human eyes, so a human-readable format didn't gain us anything.
2022-10-06 00:21:03 +00:00
|
|
|
|
|
|
|
assert.is_true(zstdInstance:exists())
|
|
|
|
assert.is_true(zstdInstance:size() > 0)
|
|
|
|
assert.is_true(type(zstdInstance:timestamp()) == "number")
|
2021-01-13 10:45:00 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("should load a table from file", function()
|
|
|
|
assert.are.same(sample, bitserInstance:load())
|
2021-04-13 16:11:39 +00:00
|
|
|
assert.are.same(sample, luajitInstance:load())
|
Logger: Use serpent instead of dump (#9588)
* Persist: support serpent, and use by default over dump (as we assume consistency > readability in Persist).
* Logger/Dbg: Use serpent instead of dump to dump tables (it's slightly more compact, honors __tostring, and will tag tables with their ref, which can come in handy when debugging).
* Dbg: Don't duplicate Logger's log function, just use it directly.
* Fontlist/ConfigDialog: Use serpent for the debug dump.
* Call `os.setlocale(C, "numeric")` on startup instead of peppering it around dump calls. It's process-wide, so it didn't make much sense.
* Trapper: Use LuaJIT's serde facilities instead of dump. They're more reliable in the face of funky input, much faster, and in this case, the data never makes it to human eyes, so a human-readable format didn't gain us anything.
2022-10-06 00:21:03 +00:00
|
|
|
assert.are.same(sample, zstdInstance:load())
|
2021-01-13 10:45:00 +00:00
|
|
|
assert.are.same(sample, dumpInstance:load())
|
Logger: Use serpent instead of dump (#9588)
* Persist: support serpent, and use by default over dump (as we assume consistency > readability in Persist).
* Logger/Dbg: Use serpent instead of dump to dump tables (it's slightly more compact, honors __tostring, and will tag tables with their ref, which can come in handy when debugging).
* Dbg: Don't duplicate Logger's log function, just use it directly.
* Fontlist/ConfigDialog: Use serpent for the debug dump.
* Call `os.setlocale(C, "numeric")` on startup instead of peppering it around dump calls. It's process-wide, so it didn't make much sense.
* Trapper: Use LuaJIT's serde facilities instead of dump. They're more reliable in the face of funky input, much faster, and in this case, the data never makes it to human eyes, so a human-readable format didn't gain us anything.
2022-10-06 00:21:03 +00:00
|
|
|
assert.are.same(sample, serpentInstance:load())
|
2021-01-13 10:45:00 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("should delete the file", function()
|
|
|
|
bitserInstance:delete()
|
2021-04-13 16:11:39 +00:00
|
|
|
luajitInstance:delete()
|
Logger: Use serpent instead of dump (#9588)
* Persist: support serpent, and use by default over dump (as we assume consistency > readability in Persist).
* Logger/Dbg: Use serpent instead of dump to dump tables (it's slightly more compact, honors __tostring, and will tag tables with their ref, which can come in handy when debugging).
* Dbg: Don't duplicate Logger's log function, just use it directly.
* Fontlist/ConfigDialog: Use serpent for the debug dump.
* Call `os.setlocale(C, "numeric")` on startup instead of peppering it around dump calls. It's process-wide, so it didn't make much sense.
* Trapper: Use LuaJIT's serde facilities instead of dump. They're more reliable in the face of funky input, much faster, and in this case, the data never makes it to human eyes, so a human-readable format didn't gain us anything.
2022-10-06 00:21:03 +00:00
|
|
|
zstdInstance:delete()
|
2021-01-13 10:45:00 +00:00
|
|
|
dumpInstance:delete()
|
Logger: Use serpent instead of dump (#9588)
* Persist: support serpent, and use by default over dump (as we assume consistency > readability in Persist).
* Logger/Dbg: Use serpent instead of dump to dump tables (it's slightly more compact, honors __tostring, and will tag tables with their ref, which can come in handy when debugging).
* Dbg: Don't duplicate Logger's log function, just use it directly.
* Fontlist/ConfigDialog: Use serpent for the debug dump.
* Call `os.setlocale(C, "numeric")` on startup instead of peppering it around dump calls. It's process-wide, so it didn't make much sense.
* Trapper: Use LuaJIT's serde facilities instead of dump. They're more reliable in the face of funky input, much faster, and in this case, the data never makes it to human eyes, so a human-readable format didn't gain us anything.
2022-10-06 00:21:03 +00:00
|
|
|
serpentInstance:delete()
|
2021-01-13 10:45:00 +00:00
|
|
|
assert.is_nil(bitserInstance:exists())
|
2021-04-13 16:11:39 +00:00
|
|
|
assert.is_nil(luajitInstance:exists())
|
Logger: Use serpent instead of dump (#9588)
* Persist: support serpent, and use by default over dump (as we assume consistency > readability in Persist).
* Logger/Dbg: Use serpent instead of dump to dump tables (it's slightly more compact, honors __tostring, and will tag tables with their ref, which can come in handy when debugging).
* Dbg: Don't duplicate Logger's log function, just use it directly.
* Fontlist/ConfigDialog: Use serpent for the debug dump.
* Call `os.setlocale(C, "numeric")` on startup instead of peppering it around dump calls. It's process-wide, so it didn't make much sense.
* Trapper: Use LuaJIT's serde facilities instead of dump. They're more reliable in the face of funky input, much faster, and in this case, the data never makes it to human eyes, so a human-readable format didn't gain us anything.
2022-10-06 00:21:03 +00:00
|
|
|
assert.is_nil(zstdInstance:exists())
|
2021-01-13 10:45:00 +00:00
|
|
|
assert.is_nil(dumpInstance:exists())
|
Logger: Use serpent instead of dump (#9588)
* Persist: support serpent, and use by default over dump (as we assume consistency > readability in Persist).
* Logger/Dbg: Use serpent instead of dump to dump tables (it's slightly more compact, honors __tostring, and will tag tables with their ref, which can come in handy when debugging).
* Dbg: Don't duplicate Logger's log function, just use it directly.
* Fontlist/ConfigDialog: Use serpent for the debug dump.
* Call `os.setlocale(C, "numeric")` on startup instead of peppering it around dump calls. It's process-wide, so it didn't make much sense.
* Trapper: Use LuaJIT's serde facilities instead of dump. They're more reliable in the face of funky input, much faster, and in this case, the data never makes it to human eyes, so a human-readable format didn't gain us anything.
2022-10-06 00:21:03 +00:00
|
|
|
assert.is_nil(serpentInstance:exists())
|
2021-01-13 10:45:00 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("should return standalone serializers/deserializers", function()
|
2022-10-06 03:56:12 +00:00
|
|
|
local tab = sample
|
|
|
|
-- NOTE: zstd only deser from a *file*, not a string.
|
|
|
|
for _, codec in ipairs({"dump", "serpent", "bitser", "luajit"}) do
|
2021-01-13 10:45:00 +00:00
|
|
|
assert.is_true(Persist.getCodec(codec).id == codec)
|
2022-10-06 03:56:12 +00:00
|
|
|
local ser = Persist.getCodec(codec).serialize
|
|
|
|
local deser = Persist.getCodec(codec).deserialize
|
|
|
|
local str = ser(tab)
|
|
|
|
local t, err = deser(str)
|
|
|
|
if not t then
|
|
|
|
print(codec, "deser failed:", err)
|
|
|
|
end
|
|
|
|
assert.are.same(t, tab)
|
2021-01-13 10:45:00 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should work with huge tables", function()
|
2022-10-06 03:56:12 +00:00
|
|
|
local tab = arrayOf(100000)
|
|
|
|
for _, codec in ipairs({"bitser", "luajit"}) do
|
|
|
|
local ser = Persist.getCodec(codec).serialize
|
|
|
|
local deser = Persist.getCodec(codec).deserialize
|
|
|
|
local str = ser(tab)
|
2021-04-13 16:11:39 +00:00
|
|
|
assert.are.same(deser(str), tab)
|
|
|
|
end
|
2021-01-13 10:45:00 +00:00
|
|
|
end)
|
|
|
|
|
Logger: Use serpent instead of dump (#9588)
* Persist: support serpent, and use by default over dump (as we assume consistency > readability in Persist).
* Logger/Dbg: Use serpent instead of dump to dump tables (it's slightly more compact, honors __tostring, and will tag tables with their ref, which can come in handy when debugging).
* Dbg: Don't duplicate Logger's log function, just use it directly.
* Fontlist/ConfigDialog: Use serpent for the debug dump.
* Call `os.setlocale(C, "numeric")` on startup instead of peppering it around dump calls. It's process-wide, so it didn't make much sense.
* Trapper: Use LuaJIT's serde facilities instead of dump. They're more reliable in the face of funky input, much faster, and in this case, the data never makes it to human eyes, so a human-readable format didn't gain us anything.
2022-10-06 00:21:03 +00:00
|
|
|
it("should fail to serialize functions", function()
|
|
|
|
for _, codec in ipairs({"dump", "bitser", "luajit", "zstd"}) do
|
|
|
|
assert.is_true(Persist.getCodec(codec).id == codec)
|
2022-10-06 03:56:12 +00:00
|
|
|
local ser = Persist.getCodec(codec).serialize
|
|
|
|
local deser = Persist.getCodec(codec).deserialize
|
|
|
|
local str = ser(fail)
|
Logger: Use serpent instead of dump (#9588)
* Persist: support serpent, and use by default over dump (as we assume consistency > readability in Persist).
* Logger/Dbg: Use serpent instead of dump to dump tables (it's slightly more compact, honors __tostring, and will tag tables with their ref, which can come in handy when debugging).
* Dbg: Don't duplicate Logger's log function, just use it directly.
* Fontlist/ConfigDialog: Use serpent for the debug dump.
* Call `os.setlocale(C, "numeric")` on startup instead of peppering it around dump calls. It's process-wide, so it didn't make much sense.
* Trapper: Use LuaJIT's serde facilities instead of dump. They're more reliable in the face of funky input, much faster, and in this case, the data never makes it to human eyes, so a human-readable format didn't gain us anything.
2022-10-06 00:21:03 +00:00
|
|
|
assert.are_not.same(deser(str), fail)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should successfully serialize functions", function()
|
|
|
|
for _, codec in ipairs({"serpent"}) do
|
2021-01-13 10:45:00 +00:00
|
|
|
assert.is_true(Persist.getCodec(codec).id == codec)
|
2022-10-06 03:56:12 +00:00
|
|
|
local ser = Persist.getCodec(codec).serialize
|
|
|
|
local deser = Persist.getCodec(codec).deserialize
|
|
|
|
local str = ser(fail)
|
2021-01-13 10:45:00 +00:00
|
|
|
assert.are_not.same(deser(str), fail)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|