Commit 8473a1c4 authored by Vincent Michel's avatar Vincent Michel
Browse files

Add tests for the xia ini parser

parent 79ea5529
Pipeline #942 passed with stages
in 1 minute and 17 seconds
......@@ -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)))
......
"""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)
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