Skip to content

Commit

Permalink
tests/test_error_reporting.py: test for reporting rule names
Browse files Browse the repository at this point in the history
This kind of test is only present in textX but not in Arpeggio.

See `test_issue97_buildin_STRICTFLOAT_as_INT_or_FLOAT()` in textX.
  • Loading branch information
stanislaw committed Apr 9, 2023
1 parent 57dd9a6 commit d2e8fe3
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion arpeggio/tests/test_error_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from __future__ import unicode_literals
import pytest

from arpeggio import Optional, Not, ParserPython, NoMatch, EOF
from arpeggio import Optional, Not, ParserPython, NoMatch, EOF, Sequence, RegExMatch, StrMatch, OrderedChoice
from arpeggio import RegExMatch as _


Expand Down Expand Up @@ -237,3 +237,67 @@ def grammar():
_ = parser.parse('something')

assert "Expected grammar at position (1, 1)" in str(e.value)


def test_reporting_rule_names_string():
parser = ParserPython(StrMatch("String", rule_name='STRING', root=True))
with pytest.raises(NoMatch) as e:
_ = parser.parse('...')
assert (
"Expected STRING at position (1, 1) => '*...'."
) == str(e.value)

parser = ParserPython(StrMatch("String", rule_name='STRING', root=False))
with pytest.raises(NoMatch) as e:
_ = parser.parse('...')
assert (
"Expected 'String' at position (1, 1) => '*...'."
) == str(e.value)


def test_reporting_rule_names_regex():
parser = ParserPython(RegExMatch(r'[^\d\W]\w*\b', rule_name='REGEX', root=True))
with pytest.raises(NoMatch) as e:
_ = parser.parse('...')
assert (
"Expected REGEX at position (1, 1) => '*...'."
) == str(e.value)

parser = ParserPython(RegExMatch(r'[^\d\W]\w*\b', rule_name='REGEX', root=False))
with pytest.raises(NoMatch) as e:
_ = parser.parse('...')
assert (
"Expected '[^\\d\\W]\\w*\\b' at position (1, 1) => '*...'."
) == str(e.value)


def test_reporting_rule_names_sequence():
parser = ParserPython(Sequence("A", "B", "C", rule_name='SEQUENCE', root=True))
with pytest.raises(NoMatch) as e:
_ = parser.parse('...')
assert (
"Expected 'A' at position (1, 1) => '*...'."
) == str(e.value)

parser = ParserPython(Sequence("A", "B", "C", rule_name='SEQUENCE', root=False))
with pytest.raises(NoMatch) as e:
_ = parser.parse('...')
assert (
"Expected 'A' at position (1, 1) => '*...'."
) == str(e.value)


def test_reporting_rule_names_ordered_choice():
parser = ParserPython(OrderedChoice(["A", "B"], rule_name='ORDERED_CHOICE', root=True))
with pytest.raises(NoMatch) as e:
_ = parser.parse('...')
assert (
"Expected 'A' or 'B' at position (1, 1) => '*...'."
) == str(e.value)

parser = ParserPython(OrderedChoice(["A", "B"], rule_name='ORDERED_CHOICE', root=False))
with pytest.raises(NoMatch) as e:
_ = parser.parse('...')
assert (
"Expected 'A' or 'B' at position (1, 1) => '*...'."
) == str(e.value)

0 comments on commit d2e8fe3

Please sign in to comment.