diff options
author | ptmcg <ptmcg@austin.rr.com> | 2020-01-04 17:43:44 -0600 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2020-01-04 17:43:44 -0600 |
commit | 062778c428a75c5b966f14bf91619c4e328261ab (patch) | |
tree | ebe5846a986fd015e5facbc76dd87db23504bc87 | |
parent | 95c5de2b0b7c04009e0f8d02f9533e59f1b16d1d (diff) | |
download | pyparsing-git-062778c428a75c5b966f14bf91619c4e328261ab.tar.gz |
Rollforward infixNotation ternary op fix from 2.4.6 branch, plus related unit test; change TestParseResultsAsserts to mixin instead of subclass; rollforward 2.4.6 CHANGES blurb from 2.4.6 branch
-rw-r--r-- | CHANGES | 28 | ||||
-rw-r--r-- | pyparsing/helpers.py | 2 | ||||
-rw-r--r-- | pyparsing/testing.py | 8 | ||||
-rw-r--r-- | tests/test_simple_unit.py | 2 | ||||
-rw-r--r-- | tests/test_unit.py | 18 |
5 files changed, 52 insertions, 6 deletions
@@ -147,6 +147,34 @@ Version 3.0.0a1 wildcards and non-Western alphabets. +Version 2.4.6 - December, 2019 +------------------------------ +- Fixed typos in White mapping of whitespace characters, to use + correct "\u" prefix instead of "u\". + +- Fix bug in left-associative ternary operators defined using + infixNotation. First reported on StackOverflow by user Jeronimo. + +- Backport of pyparsing_test namespace from 3.0.0, including + TestParseResultsAsserts mixin class defining unittest-helper + methods: + . def assertParseResultsEquals( + self, result, expected_list=None, expected_dict=None, msg=None) + . def assertParseAndCheckList( + self, expr, test_string, expected_list, msg=None, verbose=True) + . def assertParseAndCheckDict( + self, expr, test_string, expected_dict, msg=None, verbose=True) + . def assertRunTestResults( + self, run_tests_report, expected_parse_results=None, msg=None) + . def assertRaisesParseException(self, exc_type=ParseException, msg=None) + + To use the methods in this mixin class, declare your unittest classes as: + + from pyparsing import pyparsing_test as ppt + class MyParserTest(ppt.TestParseResultsAsserts, unittest.TestCase): + ... + + Version 2.4.5 - November, 2019 ------------------------------ - NOTE: final release compatible with Python 2.x. diff --git a/pyparsing/helpers.py b/pyparsing/helpers.py index 3bd5a67..c4b84d7 100644 --- a/pyparsing/helpers.py +++ b/pyparsing/helpers.py @@ -696,7 +696,7 @@ def infixNotation(baseExpr, opList, lpar=Suppress("("), rpar=Suppress(")")): elif arity == 3: matchExpr = _FB( lastExpr + opExpr1 + lastExpr + opExpr2 + lastExpr - ) + Group(lastExpr + opExpr1 + lastExpr + opExpr2 + lastExpr) + ) + Group(lastExpr + OneOrMore(opExpr1 + lastExpr + opExpr2 + lastExpr)) else: raise ValueError( "operator must be unary (1), binary (2), or ternary (3)" diff --git a/pyparsing/testing.py b/pyparsing/testing.py index b079c6d..201b6d5 100644 --- a/pyparsing/testing.py +++ b/pyparsing/testing.py @@ -35,7 +35,7 @@ class pyparsing_test: group = Group('(' + term[...] + ')') # assert that the '()' characters are not included in the parsed tokens - self.assertParseAndCheckLisst(group, "(abc 123 def)", ['abc', '123', 'def']) + self.assertParseAndCheckList(group, "(abc 123 def)", ['abc', '123', 'def']) # after exiting context manager, literals are converted to Literal expressions again """ @@ -84,7 +84,11 @@ class pyparsing_test: def __exit__(self, *args): return self.restore() - class TestParseResultsAsserts(unittest.TestCase): + class TestParseResultsAsserts: + """ + A mixin class to add parse results assertion methods to normal unittest.TestCase classes. + """ + def assertParseResultsEquals( self, result, expected_list=None, expected_dict=None, msg=None ): diff --git a/tests/test_simple_unit.py b/tests/test_simple_unit.py index 6caf7f6..41c4b25 100644 --- a/tests/test_simple_unit.py +++ b/tests/test_simple_unit.py @@ -23,7 +23,7 @@ PpTestSpec = namedtuple( PpTestSpec.__new__.__defaults__ = ("", pp.Empty(), "", "parseString", None, None, None) -class PyparsingExpressionTestCase(TestParseResultsAsserts): +class PyparsingExpressionTestCase(ppt.TestParseResultsAsserts, unittest.TestCase): """ Base pyparsing testing class to parse various pyparsing expressions against given text strings. Subclasses must define a class attribute 'tests' which diff --git a/tests/test_unit.py b/tests/test_unit.py index 070a9d1..b643262 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -19,7 +19,6 @@ from pyparsing import ParserElement from tests.json_parser_tests import test1, test2, test3, test4, test5 ppt = pp.pyparsing_test -TestParseResultsAsserts = ppt.TestParseResultsAsserts # see which Python implementation we are running CPYTHON_ENV = sys.platform == "win32" @@ -81,7 +80,7 @@ class Test1_PyparsingTestInit(TestCase): print("Python version", sys.version) -class Test2_WithoutPackrat(TestParseResultsAsserts): +class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): suite_context = None def setUp(self): @@ -6359,6 +6358,21 @@ class Test2_WithoutPackrat(TestParseResultsAsserts): ) print() + def testChainedTernaryOperator(self): + TERNARY_INFIX = pp.infixNotation( + pp.pyparsing_common.integer, [(("?", ":"), 3, pp.opAssoc.LEFT),] + ) + self.assertParseAndCheckList( + TERNARY_INFIX, "1?1:0?1:0", [[1, "?", 1, ":", 0, "?", 1, ":", 0]] + ) + + TERNARY_INFIX = pp.infixNotation( + pp.pyparsing_common.integer, [(("?", ":"), 3, pp.opAssoc.RIGHT),] + ) + self.assertParseAndCheckList( + TERNARY_INFIX, "1?1:0?1:0", [[1, "?", 1, ":", [0, "?", 1, ":", 0]]] + ) + def testMiscellaneousParserTests(self): runtests = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |