Commit d2114ebd authored by Ezio Melotti's avatar Ezio Melotti

#2650: Refactor the tests for re.escape.

parent 5cf7878f
from test.support import verbose, run_unittest from test.support import verbose, run_unittest
import re import re
from re import Scanner from re import Scanner
import sys, os, traceback import os
import sys
import string
import traceback
from weakref import proxy from weakref import proxy
# Misc tests from Tim Peters' re.doc # Misc tests from Tim Peters' re.doc
...@@ -411,31 +414,46 @@ class ReTests(unittest.TestCase): ...@@ -411,31 +414,46 @@ class ReTests(unittest.TestCase):
self.assertEqual(re.search("\s(b)", " b").group(1), "b") self.assertEqual(re.search("\s(b)", " b").group(1), "b")
self.assertEqual(re.search("a\s", "a ").group(0), "a ") self.assertEqual(re.search("a\s", "a ").group(0), "a ")
def assertMatch(self, pattern, text, match=None, span=None,
matcher=re.match):
if match is None and span is None:
# the pattern matches the whole text
match = text
span = (0, len(text))
elif match is None or span is None:
raise ValueError('If match is not None, span should be specified '
'(and vice versa).')
m = matcher(pattern, text)
self.assertTrue(m)
self.assertEqual(m.group(), match)
self.assertEqual(m.span(), span)
def test_re_escape(self): def test_re_escape(self):
p="" alnum_chars = string.ascii_letters + string.digits
self.assertEqual(re.escape(p), p) p = ''.join(chr(i) for i in range(256))
for i in range(0, 256): for c in p:
p = p + chr(i) if c in alnum_chars:
self.assertEqual(re.match(re.escape(chr(i)), chr(i)) is not None, self.assertEqual(re.escape(c), c)
True) elif c == '\x00':
self.assertEqual(re.match(re.escape(chr(i)), chr(i)).span(), (0,1)) self.assertEqual(re.escape(c), '\\000')
else:
pat=re.compile(re.escape(p)) self.assertEqual(re.escape(c), '\\' + c)
self.assertEqual(pat.match(p) is not None, True) self.assertMatch(re.escape(c), c)
self.assertEqual(pat.match(p).span(), (0,256)) self.assertMatch(re.escape(p), p)
def test_re_escape_byte(self): def test_re_escape_byte(self):
p=b"" alnum_chars = (string.ascii_letters + string.digits).encode('ascii')
self.assertEqual(re.escape(p), p) p = bytes(range(256))
for i in range(0, 256): for i in p:
b = bytes([i]) b = bytes([i])
p += b if b in alnum_chars:
self.assertEqual(re.match(re.escape(b), b) is not None, True) self.assertEqual(re.escape(b), b)
self.assertEqual(re.match(re.escape(b), b).span(), (0,1)) elif i == 0:
self.assertEqual(re.escape(b), b'\\000')
pat=re.compile(re.escape(p)) else:
self.assertEqual(pat.match(p) is not None, True) self.assertEqual(re.escape(b), b'\\' + b)
self.assertEqual(pat.match(p).span(), (0,256)) self.assertMatch(re.escape(b), b)
self.assertMatch(re.escape(p), p)
def pickle_test(self, pickle): def pickle_test(self, pickle):
oldpat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)') oldpat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment