summaryrefslogtreecommitdiff
path: root/unitTests.py
diff options
context:
space:
mode:
authorPaul McGuire <ptmcg@austin.rr.com>2019-06-29 00:19:43 -0500
committerPaul McGuire <ptmcg@austin.rr.com>2019-06-29 00:19:43 -0500
commit450fd02154cfb0ba0cdbec09fc34e8e66a092f11 (patch)
treedacc3d8e1d014fb2af890eed967a8907adfe7f35 /unitTests.py
parent459f1d5d9f6f200f94bb70e96493fa1bf82dbba8 (diff)
downloadpyparsing-git-450fd02154cfb0ba0cdbec09fc34e8e66a092f11.tar.gz
Fix issue #87, regression in indentedBlock
Diffstat (limited to 'unitTests.py')
-rw-r--r--unitTests.py123
1 files changed, 120 insertions, 3 deletions
diff --git a/unitTests.py b/unitTests.py
index 5db9ffe..e14016a 100644
--- a/unitTests.py
+++ b/unitTests.py
@@ -3817,12 +3817,78 @@ class UnicodeTests(ParseTestCase):
self.assertEqual(result.asDict(), {u'şehir': u'İzmir', u'ülke': u'Türkiye', u'nüfus': 4279677},
"Failed to parse Turkish key-value pairs")
+
+class IndentedBlockExampleTest(ParseTestCase):
+ # Make sure example in indentedBlock docstring actually works!
+ def runTest(self):
+ from textwrap import dedent
+ from pyparsing import (Word, alphas, alphanums, indentedBlock, Optional, delimitedList, Group, Forward,
+ nums, OneOrMore)
+ data = dedent('''
+ def A(z):
+ A1
+ B = 100
+ G = A2
+ A2
+ A3
+ B
+ def BB(a,b,c):
+ BB1
+ def BBA():
+ bba1
+ bba2
+ bba3
+ C
+ D
+ def spam(x,y):
+ def eggs(z):
+ pass
+ ''')
+
+ indentStack = [1]
+ stmt = Forward()
+
+ identifier = Word(alphas, alphanums)
+ funcDecl = ("def" + identifier + Group("(" + Optional(delimitedList(identifier)) + ")") + ":")
+ func_body = indentedBlock(stmt, indentStack)
+ funcDef = Group(funcDecl + func_body)
+
+ rvalue = Forward()
+ funcCall = Group(identifier + "(" + Optional(delimitedList(rvalue)) + ")")
+ rvalue << (funcCall | identifier | Word(nums))
+ assignment = Group(identifier + "=" + rvalue)
+ stmt << (funcDef | assignment | identifier)
+
+ module_body = OneOrMore(stmt)
+
+ parseTree = module_body.parseString(data)
+ parseTree.pprint()
+ self.assertEqual(parseTree.asList(),
+ [['def',
+ 'A',
+ ['(', 'z', ')'],
+ ':',
+ [['A1'], [['B', '=', '100']], [['G', '=', 'A2']], ['A2'], ['A3']]],
+ 'B',
+ ['def',
+ 'BB',
+ ['(', 'a', 'b', 'c', ')'],
+ ':',
+ [['BB1'], [['def', 'BBA', ['(', ')'], ':', [['bba1'], ['bba2'], ['bba3']]]]]],
+ 'C',
+ 'D',
+ ['def',
+ 'spam',
+ ['(', 'x', 'y', ')'],
+ ':',
+ [[['def', 'eggs', ['(', 'z', ')'], ':', [['pass']]]]]]],
+ "Failed indentedBlock example"
+ )
+
+
class IndentedBlockTest(ParseTestCase):
# parse pseudo-yaml indented text
def runTest(self):
- if pp.ParserElement.packrat_cache:
- print_("cannot test indentedBlock with packrat enabled")
- return
import textwrap
EQ = pp.Suppress('=')
@@ -3854,6 +3920,57 @@ class IndentedBlockTest(ParseTestCase):
self.assertEqual(result.c.c2.c21, 999, "invalid indented block result")
+class IndentedBlockTest2(ParseTestCase):
+ # exercise indentedBlock with example posted in issue #87
+ def runTest(self):
+ from textwrap import dedent
+ from pyparsing import Word, alphas, alphanums, Suppress, Forward, indentedBlock, Literal, OneOrMore
+
+ indent_stack = [1]
+
+ key = Word(alphas, alphanums) + Suppress(":")
+ stmt = Forward()
+
+ suite = indentedBlock(stmt, indent_stack)
+ body = key + suite
+
+ pattern = (Word(alphas) + Suppress("(") + Word(alphas) + Suppress(")"))
+ stmt << pattern
+
+ def key_parse_action(toks):
+ print_("Parsing '%s'..." % toks[0])
+
+ key.setParseAction(key_parse_action)
+ header = Suppress("[") + Literal("test") + Suppress("]")
+ content = (header + OneOrMore(indentedBlock(body, indent_stack, False)))
+
+ contents = Forward()
+ suites = indentedBlock(content, indent_stack)
+
+ extra = Literal("extra") + Suppress(":") + suites
+ contents << (content | extra)
+
+ parser = OneOrMore(contents)
+
+ sample = dedent("""\
+ extra:
+ [test]
+ one0:
+ two (three)
+ four0:
+ five (seven)
+ extra:
+ [test]
+ one1:
+ two (three)
+ four1:
+ five (seven)
+ """)
+
+ success, _ = parser.runTests([sample])
+ self.assertTrue(success, "Failed indentedBlock test for issue #87")
+
+
class IndentedBlockScanTest(ParseTestCase):
def get_parser(self):
"""