Verified Commit d6fa02fb authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner
Browse files

parser: Parse single options block

parent 149773f7
--[[
Copyright 2016, CZ.NIC z.s.p.o. (http://www.nic.cz/)
This file is part of the turris updater.
Updater is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Updater is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Updater. If not, see <http://www.gnu.org/licenses/>.
]]--
local error = error
module "backend"
--[[
Parse a single block of mail-header-like records.
Return as a table.
]]--
function parse_block(block)
local result = {}
local name
local value
local function store()
if name then
result[name] = value
name = nil
value = nil
end
end
for line in block:gmatch("[^\n]+") do
local n, v = line:match('^(%S+):%s*(.*)')
if n then
-- The beginning of the field
store()
name = n
value = v
elseif line:match('^%s') then
-- The continuation of a field
if not name then
error("Continuation at the beginning of block: " .. line)
end
value = value .. "\n" .. line
else
error("Malformed line: " .. line)
end
end
store()
return result
end
return _M
......@@ -16,7 +16,8 @@ C_TESTS := \
events \
interpreter
LUA_TESTS := # No tests yet, but they'll come. The machinery has been tested with uncommited „empty“ test case.
LUA_TESTS := \
backend
define DO_C_TEST
......
--[[
Copyright 2016, CZ.NIC z.s.p.o. (http://www.nic.cz/)
This file is part of the turris updater.
Updater is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Updater is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Updater. If not, see <http://www.gnu.org/licenses/>.
]]--
require 'lunit'
local B = require 'backend'
module("backend-tests", package.seeall, lunit.testcase)
-- Tests for the parse_block function
function test_parse_block()
-- Simple case
assert_table_equal({
val1 = "value 1",
val2 = "value 2",
val3 = "value 3"
}, B.parse_block([[val1: value 1
val2: value 2
val3: value 3]]))
-- Continuations of fields
assert_table_equal({
val1 = [[value 1
line 2
line 3]],
val2 = "value 2"
}, B.parse_block([[val1: value 1
line 2
line 3
val2: value 2]]))
-- Continuation on the first line, several ways
assert_error(function() B.parse_block(" x") end)
assert_error(function() B.parse_block(" x: y") end)
-- Some other strange lines
assert_error(function() B.parse_block("xyz") end)
assert_error(function() B.parse_block(" ") end)
end
......@@ -4,3 +4,13 @@ function launch(test)
local stats = lunit.main({test})
return stats.errors, stats.failed
end
function assert_table_equal(t1, t2)
local function cmp(t1, t2, name)
for k, v in pairs(t1) do
lunit.assert_equal(v, t2[k], "Values for key '" .. k .. "' differ: '" .. v .. "' vs '" .. t2[k] .. "'" .. name)
end
end
cmp(t1, t2, " pass 1")
cmp(t2, t1, " pass 2")
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment