Skip to content

Commit

Permalink
python: Add unit tests for ListParser.
Browse files Browse the repository at this point in the history
Add unit tests for ListParser class.

Acked-by: Eelco Chaudron <[email protected]>
Signed-off-by: Adrian Moreno <[email protected]>
Signed-off-by: Ilya Maximets <[email protected]>
  • Loading branch information
amorenoz authored and igsilya committed Jul 15, 2022
1 parent 445dceb commit 3425d01
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
3 changes: 2 additions & 1 deletion python/automake.mk
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ ovs_pyfiles = \
python/ovs/winutils.py

ovs_pytests = \
python/ovs/tests/test_kv.py
python/ovs/tests/test_kv.py \
python/ovs/tests/test_list.py

# These python files are used at build time but not runtime,
# so they are not installed.
Expand Down
66 changes: 66 additions & 0 deletions python/ovs/tests/test_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import pytest

from ovs.flow.list import ListParser, ListDecoders
from ovs.flow.kv import KeyValue


@pytest.mark.parametrize(
"input_data,expected",
[
(
("field1,field2,3,nested:value", None, [","]),
[
KeyValue("elem_0", "field1"),
KeyValue("elem_1", "field2"),
KeyValue("elem_2", 3),
KeyValue("elem_3", "nested:value"),
],
),
(
(
"field1,field2,3,nested:value",
ListDecoders(
[
("key1", str),
("key2", str),
("key3", int),
("key4", lambda x: x.split(":"), [","]),
]
),
[","],
),
[
KeyValue("key1", "field1"),
KeyValue("key2", "field2"),
KeyValue("key3", 3),
KeyValue("key4", ["nested", "value"]),
],
),
(
("field1:field2:3", None, [":"]),
[
KeyValue("elem_0", "field1"),
KeyValue("elem_1", "field2"),
KeyValue("elem_2", 3),
],
),
],
)
def test_kv_parser(input_data, expected):
input_string = input_data[0]
decoders = input_data[1]
delims = input_data[2]
tparser = ListParser(input_string, decoders, delims)
tparser.parse()
result = tparser.kv()
assert len(expected) == len(result)
for i in range(0, len(result)):
assert result[i].key == expected[i].key
assert result[i].value == expected[i].value
kpos = result[i].meta.kpos
kstr = result[i].meta.kstring
vpos = result[i].meta.vpos
vstr = result[i].meta.vstring
assert input_string[kpos : kpos + len(kstr)] == kstr
if vpos != -1:
assert input_string[vpos : vpos + len(vstr)] == vstr

0 comments on commit 3425d01

Please sign in to comment.