blob: 59497ae9601c926ddf32a3ab1af67cf8de8e2552 [file] [log] [blame]
Fredrik Lundh143328b2000-09-02 11:03:34 +00001# SRE test harness for the Python regression suite
2
3# this is based on test_re.py, but uses a test function instead
4# of all those asserts
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +00005
6import sys
7sys.path=['.']+sys.path
8
Barry Warsaw04f357c2002-07-23 19:04:11 +00009from test.test_support import verbose, TestFailed, have_unicode
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000010import sre
Fredrik Lundhf2989b22001-02-18 12:05:16 +000011import sys, os, string, traceback
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000012
Fredrik Lundh143328b2000-09-02 11:03:34 +000013#
14# test support
15
16def test(expression, result, exception=None):
17 try:
18 r = eval(expression)
19 except:
20 if exception:
21 if not isinstance(sys.exc_value, exception):
22 print expression, "FAILED"
23 # display name, not actual value
24 if exception is sre.error:
25 print "expected", "sre.error"
26 else:
27 print "expected", exception.__name__
28 print "got", sys.exc_type.__name__, str(sys.exc_value)
29 else:
30 print expression, "FAILED"
31 traceback.print_exc(file=sys.stdout)
32 else:
33 if exception:
34 print expression, "FAILED"
35 if exception is sre.error:
36 print "expected", "sre.error"
37 else:
38 print "expected", exception.__name__
39 print "got result", repr(r)
40 else:
41 if r != result:
42 print expression, "FAILED"
43 print "expected", repr(result)
44 print "got result", repr(r)
45
46if verbose:
47 print 'Running tests on character literals'
48
Fredrik Lundh143328b2000-09-02 11:03:34 +000049#
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000050# Misc tests from Tim Peters' re.doc
51
52if verbose:
53 print 'Running tests on sre.search and sre.match'
54
Fredrik Lundh03dd0102000-09-03 10:43:16 +000055test(r"""sre.search(r'x*', 'axx').span(0)""", (0, 0))
56test(r"""sre.search(r'x*', 'axx').span()""", (0, 0))
57test(r"""sre.search(r'x+', 'axx').span(0)""", (1, 3))
58test(r"""sre.search(r'x+', 'axx').span()""", (1, 3))
59test(r"""sre.search(r'x', 'aaa')""", None)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000060
Fredrik Lundh03dd0102000-09-03 10:43:16 +000061test(r"""sre.match(r'a*', 'xxx').span(0)""", (0, 0))
62test(r"""sre.match(r'a*', 'xxx').span()""", (0, 0))
63test(r"""sre.match(r'x*', 'xxxa').span(0)""", (0, 3))
64test(r"""sre.match(r'x*', 'xxxa').span()""", (0, 3))
65test(r"""sre.match(r'a+', 'xxx')""", None)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000066
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000067if verbose:
68 print "Running tests on sre.findall"
69
Fredrik Lundh03dd0102000-09-03 10:43:16 +000070test(r"""sre.findall(r":+", "abc")""", [])
71test(r"""sre.findall(r":+", "a:b::c:::d")""", [":", "::", ":::"])
72test(r"""sre.findall(r"(:+)", "a:b::c:::d")""", [":", "::", ":::"])
73test(r"""sre.findall(r"(:)(:*)", "a:b::c:::d")""",
Fredrik Lundh143328b2000-09-02 11:03:34 +000074 [(":", ""), (":", ":"), (":", "::")])
Fredrik Lundh03dd0102000-09-03 10:43:16 +000075test(r"""sre.findall(r"(a)|(b)", "abc")""", [("a", ""), ("", "b")])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000076
Fredrik Lundhebc37b22000-10-28 19:30:41 +000077# bug 117612
78test(r"""sre.findall(r"(a|(b))", "aba")""", [("a", ""),("b", "b"),("a", "")])
79
Fredrik Lundhb7747e22001-10-28 20:15:40 +000080if sys.hexversion >= 0x02020000:
81 if verbose:
82 print "Running tests on sre.finditer"
83 def fixup(seq):
84 # convert iterator to list
85 if not hasattr(seq, "next") or not hasattr(seq, "__iter__"):
86 print "finditer returned", type(seq)
87 return map(lambda item: item.group(0), seq)
88 # sanity
89 test(r"""fixup(sre.finditer(r":+", "a:b::c:::d"))""", [":", "::", ":::"])
90
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000091if verbose:
92 print "Running tests on sre.match"
93
Fredrik Lundh03dd0102000-09-03 10:43:16 +000094test(r"""sre.match(r'a', 'a').groups()""", ())
95test(r"""sre.match(r'(a)', 'a').groups()""", ('a',))
96test(r"""sre.match(r'(a)', 'a').group(0)""", 'a')
97test(r"""sre.match(r'(a)', 'a').group(1)""", 'a')
98test(r"""sre.match(r'(a)', 'a').group(1, 1)""", ('a', 'a'))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000099
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000100pat = sre.compile(r'((a)|(b))(c)?')
Fredrik Lundh143328b2000-09-02 11:03:34 +0000101test(r"""pat.match('a').groups()""", ('a', 'a', None, None))
102test(r"""pat.match('b').groups()""", ('b', None, 'b', None))
103test(r"""pat.match('ac').groups()""", ('a', 'a', None, 'c'))
104test(r"""pat.match('bc').groups()""", ('b', None, 'b', 'c'))
105test(r"""pat.match('bc').groups("")""", ('b', "", 'b', 'c'))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000106
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000107pat = sre.compile(r'(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
Fredrik Lundh143328b2000-09-02 11:03:34 +0000108test(r"""pat.match('a').group(1, 2, 3)""", ('a', None, None))
109test(r"""pat.match('b').group('a1', 'b2', 'c3')""", (None, 'b', None))
110test(r"""pat.match('ac').group(1, 'b2', 3)""", ('a', None, 'c'))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000111
Fredrik Lundh397a6542001-10-18 19:30:16 +0000112# bug 448951 (similar to 429357, but with single char match)
113# (Also test greedy matches.)
114for op in '','?','*':
115 test(r"""sre.match(r'((.%s):)?z', 'z').groups()"""%op, (None, None))
116 test(r"""sre.match(r'((.%s):)?z', 'a:z').groups()"""%op, ('a:', 'a'))
117
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000118if verbose:
119 print "Running tests on sre.escape"
120
Fredrik Lundh143328b2000-09-02 11:03:34 +0000121p = ""
122for i in range(0, 256):
123 p = p + chr(i)
Fredrik Lundh538f05c2001-01-14 15:15:37 +0000124 test(r"""sre.match(sre.escape(chr(i)), chr(i)) is not None""", 1)
Fredrik Lundh143328b2000-09-02 11:03:34 +0000125 test(r"""sre.match(sre.escape(chr(i)), chr(i)).span()""", (0,1))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000126
Fredrik Lundh143328b2000-09-02 11:03:34 +0000127pat = sre.compile(sre.escape(p))
Fredrik Lundh538f05c2001-01-14 15:15:37 +0000128test(r"""pat.match(p) is not None""", 1)
Fredrik Lundh143328b2000-09-02 11:03:34 +0000129test(r"""pat.match(p).span()""", (0,256))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000130
131if verbose:
Fredrik Lundh1296a8d2001-10-21 18:04:11 +0000132 print 'Running tests on sre.Scanner'
133
134def s_ident(scanner, token): return token
135def s_operator(scanner, token): return "op%s" % token
136def s_float(scanner, token): return float(token)
137def s_int(scanner, token): return int(token)
138
139scanner = sre.Scanner([
140 (r"[a-zA-Z_]\w*", s_ident),
141 (r"\d+\.\d*", s_float),
142 (r"\d+", s_int),
143 (r"=|\+|-|\*|/", s_operator),
144 (r"\s+", None),
145 ])
146
147# sanity check
148test('scanner.scan("sum = 3*foo + 312.50 + bar")',
149 (['sum', 'op=', 3, 'op*', 'foo', 'op+', 312.5, 'op+', 'bar'], ''))
150
151if verbose:
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000152 print 'Pickling a SRE_Pattern instance'
153
154try:
155 import pickle
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000156 pat = sre.compile(r'a(?:b|(c|e){1,2}?|d)+?(.)')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000157 s = pickle.dumps(pat)
158 pat = pickle.loads(s)
159except:
Guido van Rossumbaefceb2001-12-08 05:11:15 +0000160 print TestFailed, 're module pickle'
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000161
162try:
163 import cPickle
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000164 pat = sre.compile(r'a(?:b|(c|e){1,2}?|d)+?(.)')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000165 s = cPickle.dumps(pat)
166 pat = cPickle.loads(s)
167except:
Guido van Rossumbaefceb2001-12-08 05:11:15 +0000168 print TestFailed, 're module cPickle'
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000169
Fredrik Lundh143328b2000-09-02 11:03:34 +0000170# constants
171test(r"""sre.I""", sre.IGNORECASE)
172test(r"""sre.L""", sre.LOCALE)
173test(r"""sre.M""", sre.MULTILINE)
174test(r"""sre.S""", sre.DOTALL)
175test(r"""sre.X""", sre.VERBOSE)
176test(r"""sre.T""", sre.TEMPLATE)
177test(r"""sre.U""", sre.UNICODE)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000178
179for flags in [sre.I, sre.M, sre.X, sre.S, sre.L, sre.T, sre.U]:
180 try:
181 r = sre.compile('^pattern$', flags)
182 except:
183 print 'Exception raised on flag', flags
184
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000185if verbose:
186 print 'Test engine limitations'
187
188# Try nasty case that overflows the straightforward recursive
189# implementation of repeated groups.
Fredrik Lundh015415e2001-03-22 23:48:28 +0000190test("sre.match('(x)*', 50000*'x').span()", (0, 50000), RuntimeError)
191test("sre.match(r'(x)*y', 50000*'x'+'y').span()", (0, 50001), RuntimeError)
Fredrik Lundh82b23072001-12-09 16:13:15 +0000192test("sre.match(r'(x)*?y', 50000*'x'+'y').span()", (0, 50001), RuntimeError)