Skip to content

Commit

Permalink
Add unit tests to test for exception message contents; enhanced pypar…
Browse files Browse the repository at this point in the history
…sing.testing.assertRaisesParseException to accept an expected exception message
  • Loading branch information
ptmcg committed Feb 13, 2024
1 parent f02e401 commit 5d54550
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGES
Expand Up @@ -18,6 +18,11 @@ Version 3.1.2 - in development
- Updated pep8 synonym wrappers for better type checking compatibility. PR submitted
by Ricardo Coccioli.

- Fixed empty error message bug, PR submitted by InSync (#534).

- Added unit tests to test for exception message contents, with enhancement to
pyparsing.testing.assertRaisesParseException to accept an expected exception message.

- Some code refactoring to reduce code nesting, PRs submitted by InSync.


Expand Down
15 changes: 12 additions & 3 deletions pyparsing/testing.py
@@ -1,8 +1,10 @@
# testing.py

from contextlib import contextmanager
import re
import typing


from .core import (
ParserElement,
ParseException,
Expand Down Expand Up @@ -233,9 +235,16 @@ def assertRunTestResults(
)

@contextmanager
def assertRaisesParseException(self, exc_type=ParseException, msg=None):
with self.assertRaises(exc_type, msg=msg):
yield
def assertRaisesParseException(self, exc_type=ParseException, expected_msg=None, msg=None):
if expected_msg is not None:
if isinstance(expected_msg, str):
expected_msg = re.escape(expected_msg)
with self.assertRaisesRegex(exc_type, expected_msg, msg=msg):
yield

else:
with self.assertRaises(exc_type, msg=msg):
yield

@staticmethod
def with_line_numbers(
Expand Down
46 changes: 46 additions & 0 deletions tests/test_unit.py
Expand Up @@ -10131,6 +10131,52 @@ def testForwardsDoProperStreamlining(self):
self.assertEqual(len(ff.expr.exprs), 4)
self.assertEqual(len(w3.exprs), 3)

test_exception_messages_tests = (
(
pp.Word(pp.alphas),
"123",
"Expected W:(A-Za-z), found '123'"
),
(
pp.Word(pp.alphas).set_name("word"),
"123",
"Expected word, found '123'"
),
(
pp.Group(pp.Word(pp.alphas).set_name("word")),
"123",
"Expected word, found '123'"
),
(
pp.OneOrMore(pp.Word(pp.alphas).set_name("word")),
"123",
"Expected word, found '123'"
),
(
pp.DelimitedList(pp.Word(pp.alphas).set_name("word")),
"123",
"Expected word, found '123'"
),
(
pp.Suppress(pp.Word(pp.alphas).set_name("word")),
"123",
"Expected word, found '123'"
),
(
pp.Forward() << pp.Word(pp.alphas).set_name("word"),
"123",
"Expected word, found '123'"
),
)

def test_exception_messages(self, tests=test_exception_messages_tests):
for expr, input_str, expected_msg in tests:
with self.subTest(expr=expr, input_str=input_str):
with self.assertRaisesParseException(
expected_msg=expected_msg
):
expr.parse_string(input_str)


class Test03_EnablePackratParsing(TestCase):
def runTest(self):
Expand Down

0 comments on commit 5d54550

Please sign in to comment.