summaryrefslogtreecommitdiff
path: root/unitTests.py
diff options
context:
space:
mode:
authorPaul McGuire <ptmcg@austin.rr.com>2019-03-30 02:28:19 -0500
committerPaul McGuire <ptmcg@austin.rr.com>2019-03-30 02:28:19 -0500
commit5a2fd3bd482d7c3c61d054601c4a17a1111e7747 (patch)
tree412cc2641682f168bd3c9ec7f917161307025283 /unitTests.py
parent897696352f4b0a03152058c01403a42b07be650c (diff)
parentfed0f3da669a9a81829651080b6dfe32ce44c1b0 (diff)
downloadpyparsing-git-5a2fd3bd482d7c3c61d054601c4a17a1111e7747.tar.gz
Merge remote-tracking branch 'origin/master'
# Conflicts: # pyparsing.py
Diffstat (limited to 'unitTests.py')
-rw-r--r--unitTests.py87
1 files changed, 84 insertions, 3 deletions
diff --git a/unitTests.py b/unitTests.py
index caf538d..9a44193 100644
--- a/unitTests.py
+++ b/unitTests.py
@@ -2891,7 +2891,8 @@ class UnicodeExpressionTest(ParseTestCase):
class SetNameTest(ParseTestCase):
def runTest(self):
from pyparsing import (oneOf,infixNotation,Word,nums,opAssoc,delimitedList,countedArray,
- nestedExpr,makeHTMLTags,anyOpenTag,anyCloseTag,commonHTMLEntity,replaceHTMLEntity)
+ nestedExpr,makeHTMLTags,anyOpenTag,anyCloseTag,commonHTMLEntity,replaceHTMLEntity,
+ Forward,ZeroOrMore)
a = oneOf("a b c")
b = oneOf("d e f")
@@ -2904,6 +2905,8 @@ class SetNameTest(ParseTestCase):
[
(('?',':'),3,opAssoc.LEFT),
])
+ recursive = Forward()
+ recursive <<= a + ZeroOrMore(b + recursive)
tests = [
a,
@@ -2913,6 +2916,7 @@ class SetNameTest(ParseTestCase):
arith_expr.expr,
arith_expr2,
arith_expr2.expr,
+ recursive,
delimitedList(Word(nums).setName("int")),
countedArray(Word(nums).setName("int")),
nestedExpr(),
@@ -2926,10 +2930,11 @@ class SetNameTest(ParseTestCase):
a | b | c
d | e | f
{a | b | c | d | e | f}
- Forward: ...
+ Forward: + | - term
+ | - term
- Forward: ...
+ Forward: ?: term
?: term
+ Forward: {a | b | c [{d | e | f Forward: ...}]...}
int [, int]...
(len) int...
nested () expression
@@ -3842,6 +3847,82 @@ class IndentedBlockTest(ParseTestCase):
self.assertEqual(result.c.c1, 200, "invalid indented block result")
self.assertEqual(result.c.c2.c21, 999, "invalid indented block result")
+
+class IndentedBlockScanTest(ParseTestCase):
+ def get_parser(self):
+ """
+ A valid statement is the word "block:", followed by an indent, followed by the letter A only, or another block
+ """
+ stack = [1]
+ block = pp.Forward()
+ body = pp.indentedBlock(pp.Literal('A') ^ block, indentStack=stack, indent=True)
+ block <<= pp.Literal('block:') + body
+ return block
+
+ def runTest(self):
+ from textwrap import dedent
+
+ # This input string is a perfect match for the parser, so a single match is found
+ p1 = self.get_parser()
+ r1 = list(p1.scanString(dedent("""\
+ block:
+ A
+ """)))
+ self.assertEqual(len(r1), 1)
+
+ # This input string is a perfect match for the parser, except for the letter B instead of A, so this will fail (and should)
+ p2 = self.get_parser()
+ r2 = list(p2.scanString(dedent("""\
+ block:
+ B
+ """)))
+ self.assertEqual(len(r2), 0)
+
+ # This input string contains both string A and string B, and it finds one match (as it should)
+ p3 = self.get_parser()
+ r3 = list(p3.scanString(dedent("""\
+ block:
+ A
+ block:
+ B
+ """)))
+ self.assertEqual(len(r3), 1)
+
+ # This input string contains both string A and string B, but in a different order.
+ p4 = self.get_parser()
+ r4 = list(p4.scanString(dedent("""\
+ block:
+ B
+ block:
+ A
+ """)))
+ self.assertEqual(len(r4), 1)
+
+ # This is the same as case 3, but with nesting
+ p5 = self.get_parser()
+ r5 = list(p5.scanString(dedent("""\
+ block:
+ block:
+ A
+ block:
+ block:
+ B
+ """)))
+ self.assertEqual(len(r5), 1)
+
+ # This is the same as case 4, but with nesting
+ p6 = self.get_parser()
+ r6 = list(p6.scanString(dedent("""\
+ block:
+ block:
+ B
+ block:
+ block:
+ A
+ """)))
+ self.assertEqual(len(r6), 1)
+
+
class ParseResultsWithNameMatchFirst(ParseTestCase):
def runTest(self):
import pyparsing as pp