diff options
author | ptmcg <ptmcg@austin.rr.com> | 2021-11-12 10:11:53 -0600 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2021-11-12 10:11:53 -0600 |
commit | 16b766b97c9c144be8c3fad4fec00417728abfa6 (patch) | |
tree | 4a49acfc67c667d9527d2c467ce8243ad7059c94 /tests/test_unit.py | |
parent | b429eb6cda915fb89620cc103913d0faa1b8ef16 (diff) | |
download | pyparsing-git-pyparsing_3.0.6.tar.gz |
Add warning suppression detection for all diagnostic warningspyparsing_3.0.6
Diffstat (limited to 'tests/test_unit.py')
-rw-r--r-- | tests/test_unit.py | 61 |
1 files changed, 50 insertions, 11 deletions
diff --git a/tests/test_unit.py b/tests/test_unit.py index a3c9c07..4c41e7f 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -114,13 +114,13 @@ class TestCase(unittest.TestCase): @contextlib.contextmanager def assertDoesNotWarn(self, warning_type: type = UserWarning, msg: str = None): - if msg is None: - msg = "unexpected warning raised" with warnings.catch_warnings(record=True) as w: warnings.simplefilter("error") try: yield except Exception as e: + if msg is None: + msg = "unexpected warning {} raised".format(e) if isinstance(e, warning_type): self.fail("{}: {}".format(msg, e)) else: @@ -180,7 +180,7 @@ class Test01b_PyparsingUnitTestUtilitiesTests(TestCase): # test assertDoesNotWarn raises an AssertionError with self.assertRaises(AssertionError): with self.assertDoesNotWarn( - msg="failed to warn when naming an empty Forward expression", + msg="warned when parsing with an empty Forward expression warning was suppressed", ): base = pp.Forward() try: @@ -7409,6 +7409,11 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): ): expr = (expr_a | expr_b)("rexp") + with self.assertDoesNotWarn( + UserWarning, msg="warned when And within alternation warning was suppressed" + ): + expr = (expr_a | expr_b).suppress_warning(pp.Diagnostics.warn_multiple_tokens_in_named_alternation)("rexp") + success, report = expr.runTests( """ not the bird @@ -7470,6 +7475,11 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): ): expr = (expr_a ^ expr_b)("rexp") + with self.assertDoesNotWarn( + UserWarning, msg="warned when And within alternation warning was suppressed" + ): + expr = (expr_a ^ expr_b).suppress_warning(pp.Diagnostics.warn_multiple_tokens_in_named_alternation)("rexp") + expr.runTests( """\ not the bird @@ -7641,17 +7651,19 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): ): path = coord[...].setResultsName("path") - def testDontWarnUngroupedNamedTokensIfUngroupedNamesStartWithNOWARN(self): - """ - - warn_ungrouped_named_tokens_in_collection - flag to enable warnings when a results - name is defined on a containing expression with ungrouped subexpressions that also - have results names (default=True) - """ + with self.assertDoesNotWarn( + UserWarning, + msg="warned when named repetition of" + " ungrouped named expressions warning was suppressed", + ): + path = coord[...].suppress_warning(pp.Diagnostics.warn_ungrouped_named_tokens_in_collection).setResultsName("path") + + def testDontWarnUngroupedNamedTokensIfWarningSuppressed(self): with ppt.reset_pyparsing_context(): pp.enable_diag(pp.Diagnostics.warn_ungrouped_named_tokens_in_collection) with self.assertDoesNotWarn( - msg="raised {} warning inner names start with '_NOWARN'".format( + msg="raised {} warning when warn on ungrouped named tokens was suppressed (original_text_for)".format( pp.Diagnostics.warn_ungrouped_named_tokens_in_collection ) ): @@ -7681,6 +7693,12 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): ): base("x") + with self.assertDoesNotWarn( + UserWarning, + msg="warned when naming an empty Forward expression warning was suppressed", + ): + base.suppress_warning(pp.Diagnostics.warn_name_set_on_empty_Forward)("x") + def testWarnParsingEmptyForward(self): """ - warn_on_parse_using_empty_Forward - flag to enable warnings when a Forward @@ -7705,13 +7723,23 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): with self.assertWarns( UserWarning, - msg="failed to warn when naming an empty Forward expression", + msg="failed to warn when parsing using an empty Forward expression", ): try: print(base.parseString("x")) except ParseException as pe: pass + with self.assertDoesNotWarn( + UserWarning, + msg="warned when parsing using an empty Forward expression warning was suppressed", + ): + base.suppress_warning(pp.Diagnostics.warn_on_parse_using_empty_Forward) + try: + print(base.parseString("x")) + except ParseException as pe: + pass + def testWarnIncorrectAssignmentToForward(self): """ - warn_on_parse_using_empty_Forward - flag to enable warnings when a Forward @@ -7741,6 +7769,17 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): ): a_method() + def a_method(): + base = pp.Forward().suppress_warning(pp.Diagnostics.warn_on_assignment_to_Forward) + base = pp.Word(pp.alphas)[...] | "(" + base + ")" + + with self.assertDoesNotWarn( + UserWarning, + msg="warned when using '=' to assign expression to a Forward warning was suppressed", + ): + a_method() + + def testWarnOnMultipleStringArgsToOneOf(self): """ - warn_on_multiple_string_args_to_oneof - flag to enable warnings when oneOf is |