aboutsummaryrefslogtreecommitdiffstats
path: root/spec/util_serialization_spec.lua
blob: 52075bc2ae21b88f6180c5587d93f54427ad9a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
local serialization = require "util.serialization";

describe("util.serialization", function ()
	describe("serialize", function ()
		it("makes a string", function ()
			assert.is_string(serialization.serialize({}));
			assert.is_string(serialization.serialize(nil));
			assert.is_string(serialization.serialize(1));
			assert.is_string(serialization.serialize(true));
		end);

		it("rejects function by default", function ()
			assert.has_error(function ()
				serialization.serialize(function () end)
			end);
		end);

		it("makes a string in debug mode", function ()
			assert.is_string(serialization.serialize(function () end, "debug"));
		end);

		it("rejects cycles", function ()
			assert.has_error(function ()
				local t = {}
				t[t] = { t };
				serialization.serialize(t)
			end);
		end);

		it("roundtrips", function ()
			local function test(data)
				local serialized = serialization.serialize(data);
				assert.is_string(serialized);
				local deserialized, err = serialization.deserialize(serialized);
				assert.same(data, deserialized, err);
			end

			test({});
			test({hello="world"});
			test("foobar")
			test("\0\1\2\3");
			test("nödåtgärd");
			test({1,2,3,4});
			test({foo={[100]={{"bar"},{baz=1}}}});
			test({["goto"] = {["function"]={["do"]="keywords"}}});
		end);
	end);
end);