diff options
author | ptmcg <ptmcg@austin.rr.com> | 2021-09-05 10:13:03 -0500 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2021-09-08 09:08:55 -0500 |
commit | e26b74e750b1444379a6881284ded51f8187ef43 (patch) | |
tree | e703dcace14aefee583ebed56edf295204410418 /tests | |
parent | e7fde50c5c7099af9b350c97933dfff20d59da79 (diff) | |
download | pyparsing-git-e26b74e750b1444379a6881284ded51f8187ef43.tar.gz |
Add test for optimized Word with max>0
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_unit.py | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/tests/test_unit.py b/tests/test_unit.py index 57322ec..2dd4a1d 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -3,7 +3,7 @@ # # Unit tests for pyparsing module # -# Copyright 2002-2020, Paul McGuire +# Copyright 2002-2021, Paul McGuire # # @@ -11,6 +11,7 @@ import contextlib import datetime import re import sys +from types import SimpleNamespace from io import StringIO from textwrap import dedent from unittest import TestCase @@ -34,6 +35,7 @@ PYPY_ENV = python_impl == "PyPy" # get full stack traces during testing pp.ParserElement.verbose_stacktrace = True + # simple utility for flattening nested lists def flatten(nested_list): if not isinstance(nested_list, list): @@ -74,6 +76,17 @@ def find_all_re_matches(patt, s): return ret +def current_method_name(level=2): + import traceback + + stack = traceback.extract_stack(limit=level) + return stack[0].name + + +def __(): + return current_method_name(3) + ": " + + class Test01_PyparsingTestInit(TestCase): def runTest(self): from pyparsing import ( @@ -1954,7 +1967,7 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): v = bool(self.arg) return not v - boolOperand = pp.Word(pp.alphas, max=1) | pp.oneOf("True False") + boolOperand = pp.Word(pp.alphas, max=1, asKeyword=True) | pp.oneOf("True False") boolExpr = pp.infixNotation( boolOperand, [ @@ -4163,6 +4176,39 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): with self.assertRaises(ValueError, msg="expected min 0 to error"): expr = pp.Word(pp.nums, min=0, max=10) + @staticmethod + def setup_testWordMaxGreaterThanZeroAndAsKeyword(): + # fmt: off + bool_operand = ( + pp.Word(pp.alphas, max=1, asKeyword=True) + | pp.one_of("True False") + ) + test_string = "p q r False" + return SimpleNamespace(**locals()) + # fmt: on + + def testWordMaxGreaterThanZeroAndAsKeyword1(self): + """test a Word with max>0 and asKeyword=True""" + setup = self.setup_testWordMaxGreaterThanZeroAndAsKeyword() + + result = setup.bool_operand[...].parseString(setup.test_string) + self.assertParseAndCheckList( + setup.bool_operand[...], + setup.test_string, + setup.test_string.split(), + msg=__() + "Failed to parse Word(max=1, asKeyword=True)", + verbose=True, + ) + + def testWordMaxGreaterThanZeroAndAsKeyword2(self): + """test a Word with max>0 and asKeyword=True""" + setup = self.setup_testWordMaxGreaterThanZeroAndAsKeyword() + + with self.assertRaisesParseException( + msg=__() + "failed to detect Word with max > 0 and asKeyword=True" + ): + setup.bool_operand.parseString("abc") + def testCharAsKeyword(self): """test a Char with asKeyword=True""" |