Skip to content

Commit

Permalink
Add tests for parser impl.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Mar 16, 2023
1 parent 93e6ac5 commit 509f625
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/antsibull_docs/markup/_parser_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def parse_parameters_escaped(text: str, index: int, parameter_count: int,
) -> t.Tuple[int, t.List[str], t.Optional[str]]:
) -> t.Tuple[t.List[str], int, t.Optional[str]]:
result: t.List[str] = []
parameters_left = parameter_count
while parameters_left > 1:
Expand Down Expand Up @@ -54,7 +54,7 @@ def parse_parameters_escaped(text: str, index: int, parameter_count: int,


def parse_parameters_unescaped(text: str, index: int, parameter_count: int,
) -> t.Tuple[int, t.List[str], t.Optional[str]]:
) -> t.Tuple[t.List[str], int, t.Optional[str]]:
result: t.List[str] = []
first = True
parameters_left = parameter_count
Expand Down
4 changes: 2 additions & 2 deletions src/antsibull_docs/markup/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ def parse_text(text: str, commands: CommandSet) -> t.List[Part]:
continue
index += 1
if command.escaped_content:
index, parameters, error = parse_parameters_escaped(
parameters, index, error = parse_parameters_escaped(
text, index, command.parameter_count)
else:
index, parameters, error = parse_parameters_unescaped(
parameters, index, error = parse_parameters_unescaped(
text, index, command.parameter_count)
if error is not None:
raise ParsingException(
Expand Down
65 changes: 65 additions & 0 deletions tests/units/markup/test_parser_impl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Author: Felix Fontein <felix@fontein.de>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2022, Ansible Project

import typing as t

import pytest

from antsibull_docs.markup._parser_impl import parse_parameters_escaped, parse_parameters_unescaped


ESCAPED_TESTS = [
['(a)', 1, 1, ['a'], 3, None],
['(a,b)', 1, 1, ['a,b'], 5, None],
['(a,b,c)', 1, 1, ['a,b,c'], 7, None],
['(a,b)', 1, 2, ['a', 'b'], 5, None],
['(a,b,c)', 1, 2, ['a', 'b,c'], 7, None],
['(a,b,c)', 1, 3, ['a', 'b', 'c'], 7, None],
['(a\\,,b\\,\\),c\\))', 1, 3, ['a,', 'b,)', 'c)'], 15, None],
['(a', 1, 1, [''], 2, 'Cannot find closing ")" after last parameter'],
['(a', 1, 2, [''], 2, 'Cannot find comma separating parameter 1 from the next one'],
['(a,b', 1, 2, ['a', ''], 4, 'Cannot find closing ")" after last parameter'],
]


@pytest.mark.parametrize(
'text, index, parameter_count, expected_index, expected_result, expected_error',
ESCAPED_TESTS,
)
def test_parse_parameters_escaped(text: str, index: int, parameter_count: int,
expected_index: int, expected_result: t.List[str],
expected_error: t.Optional[str]) -> None:
end_index, result, error = parse_parameters_escaped(text, index, parameter_count)
print(end_index, result, error)
assert index == expected_index
assert result == expected_result
assert error == expected_error


UNESCAPED_TESTS = [
['(a)', 1, 1, ['a'], 3, None],
['(a,b)', 1, 1, ['a,b'], 5, None],
['(a,b,c)', 1, 1, ['a,b,c'], 7, None],
['(a,b)', 1, 2, ['a', 'b'], 5, None],
['(a,b,c)', 1, 2, ['a', 'b,c'], 7, None],
['(a,b,c)', 1, 3, ['a', 'b', 'c'], 7, None],
['(a', 1, 1, [], 2, 'Cannot find closing ")" after last parameter'],
['(a', 1, 2, [], 2, 'Cannot find comma separating parameter 1 from the next one'],
['(a,b', 1, 2, ['a'], 4, 'Cannot find closing ")" after last parameter'],
]


@pytest.mark.parametrize(
'text, index, parameter_count, expected_index, expected_result, expected_error',
UNESCAPED_TESTS,
)
def test_parse_parameters_unescaped(text: str, index: int, parameter_count: int,
expected_index: int, expected_result: t.List[str],
expected_error: t.Optional[str]) -> None:
end_index, result, error = parse_parameters_unescaped(text, index, parameter_count)
print(end_index, result, error)
assert index == expected_index
assert result == expected_result
assert error == expected_error

0 comments on commit 509f625

Please sign in to comment.