diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2019-09-26 23:16:59 -0500 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2019-09-26 23:16:59 -0500 |
commit | f75feff314812f3821f408cd7134524e2e56293f (patch) | |
tree | 47bbe3786207a81adae4fe9de84d138fe83b04cf | |
parent | 30a41524ed347662e7e050cc4fd0a873932c4feb (diff) | |
download | pyparsing-git-f75feff314812f3821f408cd7134524e2e56293f.tar.gz |
Add convenience assert methods (to parse and check list or dict contents in one go)unittest_integration
-rw-r--r-- | CHANGES | 6 | ||||
-rw-r--r-- | pyparsing.py | 20 | ||||
-rw-r--r-- | unitTests.py | 20 |
3 files changed, 37 insertions, 9 deletions
@@ -74,7 +74,11 @@ Version 3.0.0a1 pp.__diag__.enable_all_warnings() - New namespace, assert methods and classes added to support writing unit tests. - (MORE TBD) + - assertParseResultsEquals + - assertParseAndCheckList + - assertParseAndCheckDict + - assertRunTestResults + - assertRaisesParseException - Fixed handling of ParseSyntaxExceptions raised as part of Each expressions, when sub-expressions contain '-' backtrack diff --git a/pyparsing.py b/pyparsing.py index 4f0c829..47d50d6 100644 --- a/pyparsing.py +++ b/pyparsing.py @@ -6727,6 +6727,26 @@ class pyparsing_test: if expected_dict is not None: self.assertEqual(expected_dict, result.asDict(), msg=msg) + def assertParseAndCheckList(self, expr, test_string, expected_list, msg=None, verbose=True): + """ + Convenience wrapper assert to test a parser element and input string, and assert that + the resulting ParseResults.asList() is equal to the expected_list. + """ + result = expr.parseString(test_string, parseAll=True) + if verbose: + print(result.dump()) + self.assertParseResultsEquals(result, expected_list=expected_list, msg=msg) + + def assertParseAndCheckDict(self, expr, test_string, expected_dict, msg=None, verbose=True): + """ + Convenience wrapper assert to test a parser element and input string, and assert that + the resulting ParseResults.asDict() is equal to the expected_dict. + """ + result = expr.parseString(test_string, parseAll=True) + if verbose: + print(result.dump()) + self.assertParseResultsEquals(result, expected_dict=expected_dict, msg=msg) + def assertRunTestResults(self, run_tests_report, expected_parse_results=None, msg=None): """ Unit test assertion to evaluate output of ParserElement.runTests(). If a list of diff --git a/unitTests.py b/unitTests.py index 6fb8fdd..61b00d4 100644 --- a/unitTests.py +++ b/unitTests.py @@ -3908,35 +3908,39 @@ class InlineLiteralsUsingTest(ParseTestCase): self.assertEqual(len(result), 4, "inlineLiteralsUsing(Literal) failed!") ParserElement.inlineLiteralsUsing(CaselessKeyword) - result = ("SELECT" + wd + "FROM" + wd).parseString("select color from colors") # WAS: + # result = ("SELECT" + wd + "FROM" + wd).parseString("select color from colors") # self.assertEqual(result.asList(), "SELECT color FROM colors".split(), # "inlineLiteralsUsing(CaselessKeyword) failed!") - self.assertParseResultsEquals(result, "SELECT color FROM colors".split(), + self.assertParseAndCheckList("SELECT" + wd + "FROM" + wd, + "select color from colors", + expected_list=['SELECT', 'color', 'FROM', 'colors'], msg="inlineLiteralsUsing(CaselessKeyword) failed!") ParserElement.inlineLiteralsUsing(CaselessLiteral) - result = ("SELECT" + wd + "FROM" + wd).parseString("select color from colors") + # result = ("SELECT" + wd + "FROM" + wd).parseString("select color from colors") # self.assertEqual(result.asList(), "SELECT color FROM colors".split(), # "inlineLiteralsUsing(CaselessLiteral) failed!") - self.assertParseResultsEquals(result, "SELECT color FROM colors".split(), + self.assertParseAndCheckList("SELECT" + wd + "FROM" + wd, + "select color from colors", + expected_list=['SELECT', 'color', 'FROM', 'colors'], msg="inlineLiteralsUsing(CaselessLiteral) failed!") integer = Word(nums) ParserElement.inlineLiteralsUsing(Literal) date_str = integer("year") + '/' + integer("month") + '/' + integer("day") - result = date_str.parseString("1999/12/31") + # result = date_str.parseString("1999/12/31") # self.assertEqual(result.asList(), ['1999', '/', '12', '/', '31'], "inlineLiteralsUsing(example 1) failed!") - self.assertParseResultsEquals(result, expected_list=['1999', '/', '12', '/', '31'], + self.assertParseAndCheckList(date_str, "1999/12/31", expected_list=['1999', '/', '12', '/', '31'], msg="inlineLiteralsUsing(example 1) failed!") # change to Suppress ParserElement.inlineLiteralsUsing(Suppress) date_str = integer("year") + '/' + integer("month") + '/' + integer("day") - result = date_str.parseString("1999/12/31") # -> ['1999', '12', '31'] + # result = date_str.parseString("1999/12/31") # -> ['1999', '12', '31'] # self.assertEqual(result.asList(), ['1999', '12', '31'], "inlineLiteralsUsing(example 2) failed!") - self.assertParseResultsEquals(result, expected_list=['1999', '12', '31'], + self.assertParseAndCheckList(date_str, "1999/12/31", expected_list=['1999', '12', '31'], msg="inlineLiteralsUsing(example 2) failed!") class CloseMatchTest(ParseTestCase): |