summaryrefslogtreecommitdiff
path: root/src/unitTests.py
diff options
context:
space:
mode:
authorPaul McGuire <ptmcg@austin.rr.com>2016-05-13 18:51:50 +0000
committerPaul McGuire <ptmcg@austin.rr.com>2016-05-13 18:51:50 +0000
commit764de3d328e7255a465cb422342d8fa7bf24749f (patch)
tree2a0e9a8ac3509bc1da1343bf7bad2d05c412e8f9 /src/unitTests.py
parentecbd19fa3f3ec3f4d580b0949df52800dcd5c921 (diff)
downloadpyparsing-git-764de3d328e7255a465cb422342d8fa7bf24749f.tar.gz
Added 'fatal' option to addCondition; enhancements to runTests, and added unit test for runTests
Diffstat (limited to 'src/unitTests.py')
-rw-r--r--src/unitTests.py58
1 files changed, 57 insertions, 1 deletions
diff --git a/src/unitTests.py b/src/unitTests.py
index 9e3bcd2..b545af4 100644
--- a/src/unitTests.py
+++ b/src/unitTests.py
@@ -2362,7 +2362,7 @@ class PopTest(ParseTestCase):
class AddConditionTest(ParseTestCase):
def runTest(self):
- from pyparsing import Word, alphas, nums
+ from pyparsing import Word, alphas, nums, Suppress, ParseFatalException
numParser = Word(nums)
numParser.addParseAction(lambda s,l,t: int(t[0]))
@@ -2373,6 +2373,26 @@ class AddConditionTest(ParseTestCase):
print_(result.asList())
assert result.asList() == [[7],[9]], "failed to properly process conditions"
+ numParser = Word(nums)
+ numParser.addParseAction(lambda s,l,t: int(t[0]))
+ rangeParser = (numParser("from_") + Suppress('-') + numParser("to"))
+
+ result = rangeParser.searchString("1-4 2-4 4-3 5 6 7 8 9 10")
+ print_(result.asList())
+ assert result.asList() == [[1, 4], [2, 4], [4, 3]], "failed to properly process conditions"
+
+ rangeParser.addCondition(lambda t: t.to > t.from_, message="from must be <= to", fatal=False)
+ result = rangeParser.searchString("1-4 2-4 4-3 5 6 7 8 9 10")
+ print_(result.asList())
+ assert result.asList() == [[1, 4], [2, 4]], "failed to properly process conditions"
+
+ rangeParser = (numParser("from_") + Suppress('-') + numParser("to"))
+ rangeParser.addCondition(lambda t: t.to > t.from_, message="from must be <= to", fatal=True)
+ try:
+ result = rangeParser.searchString("1-4 2-4 4-3 5 6 7 8 9 10")
+ assert False, "failed to interrupt parsing on fatal condition failure"
+ except ParseFatalException:
+ print_("detected fatal condition")
class PatientOrTest(ParseTestCase):
def runTest(self):
@@ -2570,6 +2590,42 @@ class TraceParseActionDecoratorTest(ParseTestCase):
integer.addParseAction(traceParseAction(Z()))
integer.parseString("132")
+class RunTestsTest(ParseTestCase):
+ def runTest(self):
+ from pyparsing import Word, nums, delimitedList
+
+ integer = Word(nums).setParseAction(lambda t : int(t[0]))
+ intrange = integer("start") + '-' + integer("end")
+ intrange.addCondition(lambda t: t.end > t.start, message="invalid range, start must be <= end", fatal=True)
+ intrange.addParseAction(lambda t: list(range(t.start, t.end+1)))
+
+ indices = delimitedList(intrange | integer)
+ indices.addParseAction(lambda t: sorted(set(t)))
+
+ tests = """\
+ # normal data
+ 1-3,2-4,6,8-10,16
+
+ # invalid range
+ 1-2, 3-1, 4-6, 7, 12
+
+ # lone integer
+ 11"""
+ results = indices.runTests(tests, printResults=False)[1]
+ #~ import pprint
+ #~ pprint.pprint(results)
+
+ expectedResults = [
+ ['# normal data', '1-3,2-4,6,8-10,16', '[1, 2, 3, 4, 6, 8, 9, 10, 16]'],
+ ['# invalid range',
+ '1-2, 3-1, 4-6, 7, 12',
+ ' ^(FATAL)',
+ 'FAIL: invalid range, start must be <= end (at char 5), (line:1, col:6)'],
+ ['# lone integer', '11', '[11]']]
+
+ for res,expected in zip(results, expectedResults):
+ assert res == expected, "failed test: " + expected[0][2:]
+
class MiscellaneousParserTests(ParseTestCase):
def runTest(self):
import pyparsing