mod_admin_telnet: Identify native bidi sessions
[prosody.git] / spec / util_serialization_spec.lua
blob2af5d80395b31599a29aa3c5ca11b3c1670e411e
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));
10 end);
12 it("rejects function by default", function ()
13 assert.has_error(function ()
14 serialization.serialize(function () end)
15 end);
16 end);
18 it("makes a string in debug mode", function ()
19 assert.is_string(serialization.serialize(function () end, "debug"));
20 end);
22 it("rejects cycles", function ()
23 assert.has_error(function ()
24 local t = {}
25 t[t] = { t };
26 serialization.serialize(t)
27 end);
28 -- also with multirefs allowed
29 assert.has_error(function ()
30 local t = {}
31 t[t] = { t };
32 serialization.serialize(t, { multirefs = true })
33 end);
34 end);
36 it("rejects multiple references to same table", function ()
37 assert.has_error(function ()
38 local t1 = {};
39 local t2 = { t1, t1 };
40 serialization.serialize(t2, { multirefs = false });
41 end);
42 end);
44 it("optionally allows multiple references to same table", function ()
45 assert.has_error(function ()
46 local t1 = {};
47 local t2 = { t1, t1 };
48 serialization.serialize(t2, { multirefs = true });
49 end);
50 end);
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);
58 end
60 test({});
61 test({hello="world"});
62 test("foobar")
63 test("\0\1\2\3");
64 test("nödåtgärd");
65 test({1,2,3,4});
66 test({foo={[100]={{"bar"},{baz=1}}}});
67 test({["goto"] = {["function"]={["do"]="keywords"}}});
68 end);
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);
75 end);
77 end);
78 end);