summaryrefslogtreecommitdiff
path: root/unitTests.py
diff options
context:
space:
mode:
authorPaul McGuire <ptmcg@austin.rr.com>2018-09-29 13:55:54 -0500
committerPaul McGuire <ptmcg@austin.rr.com>2018-09-29 13:55:54 -0500
commit6a6c1535ea98e2f84a479cd6873dee553fee92c3 (patch)
treed0396b6ce9194b5f71b4d6ae8b3060ce94563d38 /unitTests.py
parentfe47c549f0662f068808143dee2811a69d4034c9 (diff)
downloadpyparsing-git-6a6c1535ea98e2f84a479cd6873dee553fee92c3.tar.gz
tighten up error checking in Regex.sub()
Diffstat (limited to 'unitTests.py')
-rw-r--r--unitTests.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/unitTests.py b/unitTests.py
index 56a6cf2..9966012 100644
--- a/unitTests.py
+++ b/unitTests.py
@@ -1740,12 +1740,39 @@ class RegexSubTest(ParseTestCase):
print_(result)
assert result == '<h1>This is the main heading</h1>\n<h2>This is the sub-heading</h2>', "incorrect Regex.sub result with re string"
+ print_("test sub with re string (Regex returns re.match)")
+ expr = pp.Regex(r"([Hh]\d):\s*(.*)", asMatch=True).sub(r"<\1>\2</\1>")
+ result = expr.transformString("h1: This is the main heading\nh2: This is the sub-heading")
+ print_(result)
+ assert result == '<h1>This is the main heading</h1>\n<h2>This is the sub-heading</h2>', "incorrect Regex.sub result with re string"
+
print_("test sub with callable that return str")
expr = pp.Regex(r"<(.*?)>").sub(lambda m: m.group(1).upper())
result = expr.transformString("I want this in upcase: <what? what?>")
print_(result)
assert result == 'I want this in upcase: WHAT? WHAT?', "incorrect Regex.sub result with callable"
+ try:
+ expr = pp.Regex(r"<(.*?)>", asMatch=True).sub(lambda m: m.group(1).upper())
+ except SyntaxError:
+ pass
+ else:
+ assert False, "failed to warn using a Regex.sub(callable) with asMatch=True"
+
+ try:
+ expr = pp.Regex(r"<(.*?)>", asGroupList=True).sub(lambda m: m.group(1).upper())
+ except SyntaxError:
+ pass
+ else:
+ assert False, "failed to warn using a Regex.sub() with asGroupList=True"
+
+ try:
+ expr = pp.Regex(r"<(.*?)>", asGroupList=True).sub("")
+ except SyntaxError:
+ pass
+ else:
+ assert False, "failed to warn using a Regex.sub() with asGroupList=True"
+
class CountedArrayTest(ParseTestCase):
def runTest(self):
from pyparsing import Word,nums,OneOrMore,countedArray