Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Bliss
python-handel
Commits
8473a1c4
Commit
8473a1c4
authored
Sep 08, 2017
by
Vincent Michel
Browse files
Add tests for the xia ini parser
parent
79ea5529
Pipeline
#942
passed with stages
in 1 minute and 17 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
handel/parser.py
View file @
8473a1c4
...
...
@@ -14,36 +14,40 @@ def parse_xia_ini_file(content):
pass
# New section
elif
line
.
startswith
(
'['
)
and
line
.
endswith
(
']'
):
if
item
is
not
None
:
msg
=
"New section within section {} item {}"
raise
ValueError
(
msg
.
format
(
section
,
item
))
item
=
None
section
=
line
[
1
:
-
1
].
strip
()
dct
[
section
]
=
[]
# New item
elif
line
.
startswith
(
'START #'
):
if
item
is
not
None
:
msg
=
"New item within section {} item {}"
raise
ValueError
(
msg
.
format
(
section
,
item
))
item
=
int
(
line
.
split
(
'#'
)[
1
])
if
item
!=
len
(
dct
[
section
]):
msg
=
'Corrupted start (section {}, {} != {})'
msg
=
msg
.
format
(
section
,
item
,
len
(
dct
[
section
]))
raise
ValueError
(
msg
)
if
section
is
None
:
msg
=
'Item {} outside of section'
raise
ValueError
(
msg
.
format
(
item
))
if
item
!=
len
(
dct
[
section
]):
msg
=
'Corrupted start (section {}, {} should be {})'
msg
=
msg
.
format
(
section
,
item
,
len
(
dct
[
section
]))
raise
ValueError
(
msg
)
dct
[
section
].
append
(
OrderedDict
())
# End item
elif
line
.
startswith
(
'END #'
):
if
item
is
None
:
msg
=
'End markup outside of item'
raise
ValueError
(
msg
)
item
=
int
(
line
.
split
(
'#'
)[
1
])
if
item
!=
len
(
dct
[
section
])
-
1
:
msg
=
'Corrupted end (section {}, {}
!=
{})'
msg
=
'Corrupted end (section {}, {}
should be
{})'
msg
=
msg
.
format
(
section
,
item
,
len
(
dct
[
section
])
-
1
)
raise
ValueError
(
msg
)
if
section
is
None
:
msg
=
'Item {} outside of section'
raise
ValueError
(
msg
.
format
(
item
))
item
=
None
# New pair
elif
'='
in
line
:
key
,
value
=
map
(
str
.
strip
,
line
.
split
(
'='
))
if
section
is
None
:
msg
=
'Key/value pair {} outside of section'
raise
ValueError
(
msg
.
format
((
key
,
value
)))
if
item
is
None
:
msg
=
'Key/value pair {} outside of item'
raise
ValueError
(
msg
.
format
((
key
,
value
)))
...
...
tests/test_parser.py
0 → 100644
View file @
8473a1c4
"""Test module for XIA INI file parsing."""
from
collections
import
OrderedDict
import
pytest
from
handel.parser
import
parse_xia_ini_file
as
parse
def
test_simple
():
content
=
"""
\
[s1]
START #0
a = 1
b = 2
END #0
START #1
c = 3
d = 4
END #1
[s2]
START #0
e = 5
f = 6
END #0
START #1
g = 7
h = 8
END #1
"""
conf
=
parse
(
content
)
expected
=
OrderedDict
([
(
's1'
,
[
OrderedDict
([
(
'a'
,
'1'
),
(
'b'
,
'2'
)]),
OrderedDict
([
(
'c'
,
'3'
),
(
'd'
,
'4'
)])]),
(
's2'
,
[
OrderedDict
([
(
'e'
,
'5'
),
(
'f'
,
'6'
)]),
OrderedDict
([
(
'g'
,
'7'
),
(
'h'
,
'8'
)])])])
assert
conf
==
expected
def
test_invalid_line
():
content
=
'hello world!'
with
pytest
.
raises
(
ValueError
)
as
ctx
:
parse
(
content
)
assert
'Line not recognized'
in
str
(
ctx
.
value
)
def
test_item_outside_of_section
():
content
=
"""
\
START #0
a = 1
END #0"""
with
pytest
.
raises
(
ValueError
)
as
ctx
:
parse
(
content
)
assert
'Item 0 outside of section'
in
str
(
ctx
.
value
)
def
test_end_markup_outside_of_item
():
content
=
"""
\
END #0"""
with
pytest
.
raises
(
ValueError
)
as
ctx
:
parse
(
content
)
assert
'End markup outside of item'
in
str
(
ctx
.
value
)
def
test_corrupted_start
():
content
=
"""
\
[s1]
START #1
a = 1
END #1"""
with
pytest
.
raises
(
ValueError
)
as
ctx
:
parse
(
content
)
assert
'Corrupted start (section s1, 1 should be 0)'
in
str
(
ctx
.
value
)
def
test_corrupted_end
():
content
=
"""
\
[s1]
START #0
a = 1
END #1"""
with
pytest
.
raises
(
ValueError
)
as
ctx
:
parse
(
content
)
assert
'Corrupted end (section s1, 1 should be 0)'
in
str
(
ctx
.
value
)
def
test_new_section_in_item
():
content
=
"""
\
[s1]
START #0
a = 1
[s2]
END #0"""
with
pytest
.
raises
(
ValueError
)
as
ctx
:
parse
(
content
)
assert
'New section within section s1 item 0'
in
str
(
ctx
.
value
)
def
test_new_item_in_item
():
content
=
"""
\
[s1]
START #0
a = 1
START #1
b = 2
END #1
END #0"""
with
pytest
.
raises
(
ValueError
)
as
ctx
:
parse
(
content
)
assert
'New item within section s1 item 0'
in
str
(
ctx
.
value
)
def
test_pair_outside_of_item
():
content
=
"""
\
[s1]
a = 1
"""
with
pytest
.
raises
(
ValueError
)
as
ctx
:
parse
(
content
)
assert
"Key/value pair ('a', '1') outside of item"
in
str
(
ctx
.
value
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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