1 local serialization
= require
"util.serialization";
3 describe("util.serialization", function ()
4 describe("serialize", function ()
5 it("makes a string", function ()
6 assert.is_string(serialization
.serialize({}));
7 assert.is_string(serialization
.serialize(nil));
8 assert.is_string(serialization
.serialize(1));
9 assert.is_string(serialization
.serialize(true));
12 it("rejects function by default", function ()
13 assert.has_error(function ()
14 serialization
.serialize(function () end)
18 it("makes a string in debug mode", function ()
19 assert.is_string(serialization
.serialize(function () end, "debug"));
22 it("rejects cycles", function ()
23 assert.has_error(function ()
26 serialization
.serialize(t
)
28 -- also with multirefs allowed
29 assert.has_error(function ()
32 serialization
.serialize(t
, { multirefs
= true })
36 it("rejects multiple references to same table", function ()
37 assert.has_error(function ()
39 local t2
= { t1
, t1
};
40 serialization
.serialize(t2
, { multirefs
= false });
44 it("optionally allows multiple references to same table", function ()
45 assert.has_error(function ()
47 local t2
= { t1
, t1
};
48 serialization
.serialize(t2
, { multirefs
= true });
52 it("roundtrips", function ()
53 local function test(data
)
54 local serialized
= serialization
.serialize(data
);
55 assert.is_string(serialized
);
56 local deserialized
, err
= serialization
.deserialize(serialized
);
57 assert.same(data
, deserialized
, err
);
61 test({hello
="world"});
66 test({foo
={[100]={{"bar"},{baz
=1}}}});
67 test({["goto"] = {["function"]={["do"]="keywords"}}});
70 it("can serialize with metatables", function ()
71 local s
= serialization
.new({ freeze
= true });
72 local t
= setmetatable({ a
= "hi" }, { __freeze
= function (t
) return { t
.a
} end });
73 local rt
= serialization
.deserialize(s(t
));
74 assert.same({"hi"}, rt
);