summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@austin.rr.com>2020-07-07 00:10:12 -0500
committerptmcg <ptmcg@austin.rr.com>2020-07-07 00:10:12 -0500
commitc104123d984ae1f7ac81046f78e2bf6421e30e56 (patch)
tree0794e7091afd2e646d8a3b64f57d66ed06d02e17
parent6e91e87cabe1e49a836103d3431f1d94dcacec33 (diff)
downloadpyparsing-git-c104123d984ae1f7ac81046f78e2bf6421e30e56.tar.gz
infixNotation unit tests to address missing coverage and features; rename infixNotation tests to meaningful names
-rw-r--r--tests/test_unit.py54
1 files changed, 50 insertions, 4 deletions
diff --git a/tests/test_unit.py b/tests/test_unit.py
index 0ea4cf9..907d942 100644
--- a/tests/test_unit.py
+++ b/tests/test_unit.py
@@ -1972,7 +1972,7 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
self.assertParseResultsEquals(testVal, expected_list=expected)
- def testInfixNotationGrammarTest1(self):
+ def testInfixNotationBasicArithEval(self):
from pyparsing import Word, nums, alphas, Literal, oneOf, infixNotation, opAssoc
import ast
@@ -2027,7 +2027,7 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
for test_str, exp_list in zip(test, expected):
self.assertParseAndCheckList(expr, test_str, exp_list, verbose=True)
- def testInfixNotationGrammarTest2(self):
+ def testInfixNotationEvalBoolExprUsingAstClasses(self):
from pyparsing import infixNotation, Word, alphas, oneOf, opAssoc
@@ -2119,7 +2119,7 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
expected, bool(res[0]), "failed boolean eval test {}".format(t)
)
- def testInfixNotationGrammarTest3(self):
+ def testInfixNotationMinimalParseActionCalls(self):
from pyparsing import infixNotation, Word, alphas, oneOf, opAssoc, nums, Literal
@@ -2160,7 +2160,7 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
print("%r => %s (count=%d)" % (t, expr.parseString(t), count))
self.assertEqual(1, count, "count evaluated too many times!")
- def testInfixNotationGrammarTest4(self):
+ def testInfixNotationWithParseActions(self):
word = pp.Word(pp.alphas)
@@ -2270,6 +2270,52 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
),
)
+ def testInfixNotationExceptions(self):
+ num = pp.Word(pp.nums)
+
+ # arity 3 with None opExpr - should raise ValueError
+ with self.assertRaises(ValueError):
+ expr = pp.infixNotation(num, [(None, 3, pp.opAssoc.LEFT),])
+
+ # arity 3 with invalid tuple - should raise ValueError
+ with self.assertRaises(ValueError):
+ expr = pp.infixNotation(num, [(("+", "-", "*"), 3, pp.opAssoc.LEFT)])
+
+ # left arity > 3 - should raise ValueError
+ with self.assertRaises(ValueError):
+ expr = pp.infixNotation(num, [("*", 4, pp.opAssoc.LEFT)])
+
+ # right arity > 3 - should raise ValueError
+ with self.assertRaises(ValueError):
+ expr = pp.infixNotation(num, [("*", 4, pp.opAssoc.RIGHT)])
+
+ # assoc not from opAssoc - should raise ValueError
+ with self.assertRaises(ValueError):
+ expr = pp.infixNotation(num, [("*", 2, "LEFT")])
+
+ def testInfixNotationWithNonOperators(self):
+ # left arity 2 with None expr
+ # right arity 2 with None expr
+ num = pp.Word(pp.nums).addParseAction(pp.tokenMap(int))
+ ident = ppc.identifier()
+ for assoc in (pp.opAssoc.LEFT, pp.opAssoc.RIGHT):
+ expr = pp.infixNotation(
+ num | ident, [(None, 2, assoc), ("+", 2, pp.opAssoc.LEFT)]
+ )
+ self.assertParseAndCheckList(expr, "3x+2", [[[3, "x"], "+", 2]])
+
+ def testInfixNotationTernaryOperator(self):
+ # left arity 3
+ # right arity 3
+ num = pp.Word(pp.nums).addParseAction(pp.tokenMap(int))
+ for assoc in (pp.opAssoc.LEFT, pp.opAssoc.RIGHT):
+ expr = pp.infixNotation(
+ num, [("+", 2, pp.opAssoc.LEFT), (("?", ":"), 3, assoc),]
+ )
+ self.assertParseAndCheckList(
+ expr, "3 + 2? 12: 13", [[[3, "+", 2], "?", 12, ":", 13]]
+ )
+
def testParseResultsPickle(self):
import pickle