summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES5
-rw-r--r--pyparsing.py22
-rw-r--r--unitTests.py18
3 files changed, 37 insertions, 8 deletions
diff --git a/CHANGES b/CHANGES
index 911e4de..e577840 100644
--- a/CHANGES
+++ b/CHANGES
@@ -21,6 +21,11 @@ Version 2.3.1 -
- Expanded the whitespace characters recognized by the White class to include
all unicode defined spaces. Suggested in Issue #51 by rtkjbillo.
+- Added optional post_parse argument to ParserElement.runTests() to add a
+ custom callback to be called for test strings that parse successfully. Useful
+ for running tests that do additional validation or processing on the parsed
+ results.
+
Version 2.3.0 - October, 2018
-----------------------------
diff --git a/pyparsing.py b/pyparsing.py
index eab4c9f..ed1b748 100644
--- a/pyparsing.py
+++ b/pyparsing.py
@@ -94,7 +94,7 @@ classes inherit from. Use the docstrings for examples of how to:
"""
__version__ = "2.3.1"
-__versionTime__ = "13 Dec 2018 06:25 UTC"
+__versionTime__ = "21 Dec 2018 06:14 UTC"
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
import string
@@ -2340,7 +2340,8 @@ class ParserElement(object):
except ParseBaseException:
return False
- def runTests(self, tests, parseAll=True, comment='#', fullDump=True, printResults=True, failureTests=False):
+ def runTests(self, tests, parseAll=True, comment='#',
+ fullDump=True, printResults=True, failureTests=False, postParse=None):
"""
Execute the parse expression on a series of test strings, showing each
test, the parsed results or where the parse failed. Quick and easy way to
@@ -2348,13 +2349,15 @@ class ParserElement(object):
Parameters:
- tests - a list of separate test strings, or a multiline string of test strings
- - parseAll - (default= ``True`` ) - flag to pass to :class:`parseString` when running tests
- - comment - (default= ``'#'`` ) - expression for indicating embedded comments in the test
+ - parseAll - (default= ``True``) - flag to pass to :class:`parseString` when running tests
+ - comment - (default= ``'#'``) - expression for indicating embedded comments in the test
string; pass None to disable comment filtering
- - fullDump - (default= ``True`` ) - dump results as list followed by results names in nested outline;
+ - fullDump - (default= ``True``) - dump results as list followed by results names in nested outline;
if False, only dump nested list
- - printResults - (default= ``True`` ) prints test output to stdout
- - failureTests - (default= ``False`` ) indicates if these tests are expected to fail parsing
+ - printResults - (default= ``True``) prints test output to stdout
+ - failureTests - (default= ``False``) indicates if these tests are expected to fail parsing
+ - postParse - (default= ``None``) optional callback for successful parse results; called as
+ `fn(test_string, parse_results)` and returns a string to be added to the test output
Returns: a (success, results) tuple, where success indicates that all tests succeeded
(or failed if ``failureTests`` is True), and the results contain a list of lines of each
@@ -2450,6 +2453,11 @@ class ParserElement(object):
result = self.parseString(t, parseAll=parseAll)
out.append(result.dump(full=fullDump))
success = success and not failureTests
+ if post_parse is not None:
+ try:
+ out.append(post_parse(t, result))
+ except Exception as e:
+ out.append("{} failed: {}: {}".format(post_parse.__name__, type(e).__name__, e))
except ParseBaseException as pe:
fatal = "(FATAL)" if isinstance(pe, ParseFatalException) else ""
if '\n' in t:
diff --git a/unitTests.py b/unitTests.py
index adf433f..c831fc4 100644
--- a/unitTests.py
+++ b/unitTests.py
@@ -103,7 +103,6 @@ class ParseTestCase(TestCase):
print_(buffered_stdout.getvalue())
raise
-
def runTest(self):
pass
@@ -3058,6 +3057,23 @@ class RunTestsTest(ParseTestCase):
success = indices.runTests(tests, printResults=False, failureTests=True)[0]
assert success, "failed to raise exception on improper range test"
+class RunTestsPostParseTest(ParseTestCase):
+ def runTest(self):
+ import pyparsing as pp
+
+ integer = pp.pyparsing_common.integer
+ fraction = integer('numerator') + '/' + integer('denominator')
+
+ def eval_fraction(test, result):
+ return "eval: {}".format(result.numerator / result.denominator)
+
+ success = fraction.runTests("""\
+ 1/2
+ 1/0
+ """, postParse=eval_fraction)
+
+ self.assertTrue(success, "failed to parse fractions in RunTestsPostParse")
+
class CommonExpressionsTest(ParseTestCase):
def runTest(self):
from pyparsing import pyparsing_common