Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
U
updater
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Michal Čihař
updater
Commits
d6fa02fb
Verified
Commit
d6fa02fb
authored
Feb 12, 2016
by
Michal 'vorner' Vaner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
parser: Parse single options block
parent
149773f7
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
123 additions
and
1 deletion
+123
-1
src/lib/autoload/a_04_backend.lua
src/lib/autoload/a_04_backend.lua
+60
-0
tests/Makefile.dir
tests/Makefile.dir
+2
-1
tests/backend.lua
tests/backend.lua
+51
-0
tests/lunit-launch/launch.lua
tests/lunit-launch/launch.lua
+10
-0
No files found.
src/lib/autoload/a_04_backend.lua
0 → 100644
View file @
d6fa02fb
--[[
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
tests/Makefile.dir
View file @
d6fa02fb
...
...
@@ -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
...
...
tests/backend.lua
0 → 100644
View file @
d6fa02fb
--[[
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
tests/lunit-launch/launch.lua
View file @
d6fa02fb
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment