Skip to content

Commit

Permalink
tests/test_error_reporting: more specific assert statements
Browse files Browse the repository at this point in the history
Setting the exact string match expectations helps to visualize what exactly
changes with any attempts to refactor the error reporting functionality.
  • Loading branch information
stanislaw authored and igordejanovic committed Apr 6, 2023
1 parent 73d52a6 commit 299e6cc
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions arpeggio/tests/test_error_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def grammar():

with pytest.raises(NoMatch) as e:
parser.parse('c')
assert "Expected 'a' or 'b'" in str(e.value)
assert (
"Expected 'a' or 'b' at position (1, 1) => '*c'."
) == str(e.value)
assert (e.value.line, e.value.col) == (1, 1)

# This grammar always succeeds due to the optional match
Expand All @@ -51,7 +53,9 @@ def second(): return 'one', 'two', 'three', 'four', 'five'
with pytest.raises(NoMatch) as e:
parser.parse('one two three four 5')

assert "Expected 'five'" in str(e.value)
assert (
"Expected 'five' at position (1, 20) => 'hree four *5'."
) == str(e.value)
assert (e.value.line, e.value.col) == (1, 20)


Expand All @@ -68,7 +72,9 @@ def grammar():

with pytest.raises(NoMatch) as e:
parser.parse(' three ident')
assert "Expected 'one' or 'two'" in str(e.value)
assert (
"Expected 'one' or 'two' at position (1, 4) => ' *three iden'."
) == str(e.value)
assert (e.value.line, e.value.col) == (1, 4)


Expand All @@ -83,7 +89,9 @@ def grammar(): return Optional('a'), 'b', EOF

with pytest.raises(NoMatch) as e:
parser.parse("\n\n a c", file_name="test_file.peg")
assert "Expected 'b' at position test_file.peg:(3, 6)" in str(e.value)
assert (
"Expected 'b' at position test_file.peg:(3, 6) => ' a *c'."
) == str(e.value)
assert (e.value.line, e.value.col) == (3, 6)


Expand All @@ -99,7 +107,9 @@ def comments(): return _(r'//.*$')

with pytest.raises(NoMatch) as e:
parser.parse('\n\n a // This is a comment \n c')
assert "Expected 'b' at position (4, 2)" in str(e.value)
assert (
"Expected 'b' at position (4, 2) => 'comment *c'."
) == str(e.value)
assert (e.value.line, e.value.col) == (4, 2)


Expand All @@ -116,7 +126,11 @@ def grammar():

with pytest.raises(NoMatch) as e:
parser.parse(' one ident')
assert "Not expected input" in str(e.value)
# FIXME: It would be great to have the error reported at (1, 4) because the
# whitespace is consumed.
assert (
"Not expected input at position (1, 1) => '* one ide'."
) == str(e.value)


def test_not_match_as_alternative():
Expand All @@ -132,7 +146,9 @@ def grammar():

with pytest.raises(NoMatch) as e:
parser.parse(' two ident')
assert "Expected 'one' at " in str(e.value)
assert (
"Expected 'one' at position (1, 4) => ' *two ident'."
) == str(e.value)


def test_sequence_of_nots():
Expand All @@ -147,7 +163,9 @@ def grammar():

with pytest.raises(NoMatch) as e:
parser.parse(' two ident')
assert "Not expected input" in str(e.value)
assert (
"Not expected input at position (1, 4) => ' *two ident'."
) == str(e.value)


def test_compound_not_match():
Expand All @@ -161,7 +179,9 @@ def grammar():

with pytest.raises(NoMatch) as e:
parser.parse(' three ident')
assert "Expected 'one' or 'two' at" in str(e.value)
assert (
"Expected 'one' or 'two' at position (1, 4) => ' *three iden'."
) == str(e.value)

parser.parse(' four ident')

Expand Down Expand Up @@ -191,7 +211,9 @@ def grammar():
with pytest.raises(NoMatch) as e:
_ = parser.parse('first')

assert "Expected '\\n' at position (1, 6)" in str(e.value)
assert (
"Expected '\\n' at position (1, 6) => 'first*'."
) == str(e.value)

# A case when regex match has newline
from arpeggio import RegExMatch
Expand Down

0 comments on commit 299e6cc

Please sign in to comment.