Diff

spec/util_serialization_spec.lua @ 9342:83e4596c2824

util.serialization: Add brief initial tests
author Kim Alvefur <zash@zash.se>
date Sun, 23 Sep 2018 16:30:35 +0200
child 9343:e767da06399d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spec/util_serialization_spec.lua	Sun Sep 23 16:30:35 2018 +0200
@@ -0,0 +1,33 @@
+local serialization = require "util.serialization";
+local envload = require "util.envload";
+
+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));
+			assert.is_string(serialization.serialize(function () end));
+		end);
+
+		it("roundtrips", function ()
+			local function test(data)
+				local serialized = serialization.serialize(data);
+				assert.is_string(serialized);
+				local chunk, err = envload.envload("return ("..serialized..");");
+				assert.is_function(chunk, err);
+				assert.same(data, chunk());
+			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}}}});
+		end);
+	end);
+end);
+