From 890369090e64379ac1c175ff3ae8eeec99435f90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20P=C3=A9rez-Cerezo?= Date: Wed, 10 Nov 2021 11:39:54 +0100 Subject: Activate serialize_lib unit tests --- .build.yml | 2 + serialize_lib/spec/serialize_spec.lua | 123 +++++++++++++++++++++++++++++++++ serialize_lib/tests/serialize_spec.lua | 123 --------------------------------- 3 files changed, 125 insertions(+), 123 deletions(-) create mode 100644 serialize_lib/spec/serialize_spec.lua delete mode 100644 serialize_lib/tests/serialize_spec.lua diff --git a/.build.yml b/.build.yml index 9627b14..303348f 100644 --- a/.build.yml +++ b/.build.yml @@ -33,6 +33,8 @@ tasks: busted cd ../advtrains_interlocking busted + cd ../serialize_lib + busted - activate_test_env: | cd advtrains git merge --no-commit origin/luaatcdebug diff --git a/serialize_lib/spec/serialize_spec.lua b/serialize_lib/spec/serialize_spec.lua new file mode 100644 index 0000000..016634e --- /dev/null +++ b/serialize_lib/spec/serialize_spec.lua @@ -0,0 +1,123 @@ +-- test the serialization function + + +package.path = "../?.lua;" .. package.path + + +ser = require("serialize") + + +local mock_file = {} +_G.mock_file = mock_file +function mock_file:read(arg) + if arg == "*l" or arg== "*line" then + local l = self.lines[self.pointer or 1] + self.pointer = (self.pointer or 1) + 1 + return l + end +end + +function mock_file:close() + return nil +end + +function mock_file:write(text) + self.content = self.content..text +end + +function mock_file:create(lines) + local f = {} + setmetatable(f, mock_file) + f.lines = lines or {} + f.write = self.write + f.close = self.close + f.read = self.read + f.content = "" + return f +end + + +local testtable = { + key = "value", + [1] = "eins", + [true] = { + a = "b", + c = false, + }, + ["es:cape1"] = "foo:bar", + ["es&ca\npe2"] = "baz&bam\nbim", + ["es&&ca&\npe3"] = "baz&&bam&\nbim", + ["es&:cape4"] = "foo\n:bar" +} +local testser = [[LUA_SER v=2 +B1:T +Sa:Sb +Sc:B0 +E +Skey:Svalue +Ses&&&&ca&&&npe3:Sbaz&&&&bam&&&nbim +N1:Seins +Ses&&&:cape4:Sfoo&n&:bar +Ses&&ca&npe2:Sbaz&&bam&nbim +Ses&:cape1:Sfoo&:bar +E +END_SER +]] + +local function check_write(tb, conf) + f = mock_file:create() + ser.write_to_fd(tb, f, conf or {}) + return f.content +end + +function string:split() + local fields = {} + self:gsub("[^\n]+", function(c) fields[#fields+1] = c end) + return fields +end + +local function check_read(text) + f = mock_file:create(text:split()) + return ser.read_from_fd(f) +end + +local noskip = [[LUA_SER v=2 +N1:T +E +E +END_SER +]] +local skip = [[LUA_SER v=2 +E +END_SER +]] + +describe("write_to_fd", function() + it("serializes a table correctly", function() + assert.equals(check_write(testtable), testser) + end) + it("does not skip empty tables", function() + assert.equals(check_write({{}}),noskip) + end) + it("skips empty tables when needed", function() + + assert.equals(check_write({{}},{skip_empty_tables=true}),skip) + end) +end) + +describe("read_from_fd", function () + it("reads a table correctly", function() + assert.same(check_read(testser),testtable) + end) + it("handles some edge cases correctly", function() + assert.same(check_read(noskip), {{}}) + assert.same(check_read(skip), {}) + end) + it("Read back table", function() + local tb = {} + for k=1,262 do + tb[k] = { "Foo", "bar", k} + end + assert.same(check_read(check_write(tb)), tb) + end) +end) diff --git a/serialize_lib/tests/serialize_spec.lua b/serialize_lib/tests/serialize_spec.lua deleted file mode 100644 index ccc3a67..0000000 --- a/serialize_lib/tests/serialize_spec.lua +++ /dev/null @@ -1,123 +0,0 @@ --- test the serialization function - - -package.path = "../?.lua;" .. package.path - - -ser = require("serialize") - - -local mock_file = {} -_G.mock_file = mock_file -function mock_file:read(arg) - if arg == "*l" then - local l = self.lines[self.pointer or 1] - self.pointer = (self.pointer or 1) + 1 - return l - end -end - -function mock_file:close() - return nil -end - -function mock_file:write(text) - self.content = self.content..text -end - -function mock_file:create(lines) - local f = {} - setmetatable(f, mock_file) - f.lines = lines or {} - f.write = self.write - f.close = self.close - f.read = self.read - f.content = "" - return f -end - - -local testtable = { - key = "value", - [1] = "eins", - [true] = { - a = "b", - c = false, - }, - ["es:cape1"] = "foo:bar", - ["es&ca\npe2"] = "baz&bam\nbim", - ["es&&ca&\npe3"] = "baz&&bam&\nbim", - ["es&:cape4"] = "foo\n:bar" -} -local testser = [[LUA_SER v=1 -B1:T -Sa:Sb -Sc:B0 -E -Skey:Svalue -Ses&&&&ca&&&npe3:Sbaz&&&&bam&&&nbim -N1:Seins -Ses&&&:cape4:Sfoo&n&:bar -Ses&&ca&npe2:Sbaz&&bam&nbim -Ses&:cape1:Sfoo&:bar -E -END_SER -]] - -local function check_write(tb, conf) - f = mock_file:create() - ser.write_to_fd(tb, f, conf or {}) - return f.content -end - -function string:split() - local fields = {} - self:gsub("[^\n]+", function(c) fields[#fields+1] = c end) - return fields -end - -local function check_read(text) - f = mock_file:create(text:split()) - return ser.read_from_fd(f) -end - -local noskip = [[LUA_SER v=1 -N1:T -E -E -END_SER -]] -local skip = [[LUA_SER v=1 -E -END_SER -]] - -describe("write_to_fd", function() - it("serializes a table correctly", function() - assert.equals(check_write(testtable), testser) - end) - it("does not skip empty tables", function() - assert.equals(check_write({{}}),noskip) - end) - it("skips empty tables when needed", function() - - assert.equals(check_write({{}},{skip_empty_tables=true}),skip) - end) -end) - -describe("read_from_fd", function () - it("reads a table correctly", function() - assert.same(check_read(testser),testtable) - end) - it("handles some edge cases correctly", function() - assert.same(check_read(noskip), {{}}) - assert.same(check_read(skip), {}) - end) - it("Read back table", function() - local tb = {} - for k=1,262 do - tb[k] = { "Foo", "bar", k} - end - assert.same(check_read(check_write(tb)), tb) - end) -end) -- cgit v1.2.3