summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMatt Carmody <33763384+mattcarmody@users.noreply.github.com>2020-03-31 12:06:49 +0000
committerGitHub <noreply@github.com>2020-03-31 07:06:49 -0500
commit90f6fa2703e3117723864d08bccc310638b1d645 (patch)
tree6faedcd265c3bff03f34216a855b5b4db9da19f0 /tests
parent44f9d453604dada57a9756d160fd6628b18651db (diff)
downloadpyparsing-git-90f6fa2703e3117723864d08bccc310638b1d645.tar.gz
Add OnlyOnce docstring, error message and unit test (#193)
Diffstat (limited to 'tests')
-rw-r--r--tests/test_unit.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/tests/test_unit.py b/tests/test_unit.py
index d7bb77d..a2a5580 100644
--- a/tests/test_unit.py
+++ b/tests/test_unit.py
@@ -7087,6 +7087,37 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
exprWithInt, "rucksack 49", {"item": "rucksack", "qty": 49}
)
+ def testOnlyOnce(self):
+ """test class OnlyOnce and its reset method"""
+
+ # use a parse action to compute the sum of the parsed integers,
+ # and add it to the end
+ def append_sum(tokens):
+ tokens.append(sum(map(int, tokens)))
+
+ pa = pp.OnlyOnce(append_sum)
+ expr = pp.OneOrMore(pp.Word(pp.nums)).addParseAction(pa)
+
+ result = expr.parseString("0 123 321")
+ print(result.dump())
+ expected = ["0", "123", "321", 444]
+ self.assertParseResultsEquals(
+ result, expected, msg="issue with OnlyOnce first call"
+ )
+
+ with self.assertRaisesParseException(
+ msg="failed to raise exception calling OnlyOnce more than once"
+ ):
+ result2 = expr.parseString("1 2 3 4 5")
+
+ pa.reset()
+ result = expr.parseString("100 200 300")
+ print(result.dump())
+ expected = ["100", "200", "300", 600]
+ self.assertParseResultsEquals(
+ result, expected, msg="issue with OnlyOnce after reset"
+ )
+
class Test3_EnablePackratParsing(TestCase):
def runTest(self):