summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_re.py
blob: 1581856131cf89c5e0e7c3f5055f8d5f968502bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/local/bin/python
# -*- mode: python -*-
# $Id$

from test_support import verbose, TestFailed
import re
import reop
import sys, os, string, traceback

# Misc tests from Tim Peters' re.doc

if verbose:
    print 'Running tests on re.sub'

try:
    assert re.sub("(?i)b+", "x", "bbbb BBBB") == 'x x'
    
    def bump_num(matchobj):
	int_value = int(matchobj.group(0))
	return str(int_value + 1)

    assert re.sub(r'\d+', bump_num, '08.2 -2 23x99y') == '9.3 -3 24x100y'
    
    assert re.sub('.', lambda m: r"\n", 'x') == '\\n'
    assert re.sub('.', r"\n", 'x') == '\n'
    
    s = r"\1\1"
    assert re.sub('(.)', s, 'x') == 'xx'
    assert re.sub('(.)', re.escape(s), 'x') == s
    assert re.sub('(.)', lambda m: s, 'x') == s

    assert re.sub('(?P<a>x)', '\g<a>\g<a>', 'xx') == 'xxxx'

except AssertionError:
    raise TestFailed, "re.sub"

if verbose:
    print 'Running tests on symbolic references'

try:
    re.sub('(?P<a>x)', '\g<a', 'xx')
except re.error, reason:
    pass
else:
    raise TestFailed, "symbolic reference"

try:
    re.sub('(?P<a>x)', '\g<', 'xx')
except re.error, reason:
    pass
else:
    raise TestFailed, "symbolic reference"

try:
    re.sub('(?P<a>x)', '\g', 'xx')
except re.error, reason:
    pass
else:
    raise TestFailed, "symbolic reference"

try:
    re.sub('(?P<a>x)', '\g<a a>', 'xx')
except re.error, reason:
    pass
else:
    raise TestFailed, "symbolic reference"

try:
    re.sub('(?P<a>x)', '\g<ab>', 'xx')
except IndexError, reason:
    pass
else:
    raise TestFailed, "symbolic reference"

try:
    re.sub('(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')
except re.error, reason:
    pass
else:
    raise TestFailed, "symbolic reference"

try:
    re.sub('(?P<a>x)|(?P<b>y)', '\\2', 'xx')
except re.error, reason:
    pass
else:
    raise TestFailed, "symbolic reference"

if verbose:
    print 'Running tests on re.subn'

try:
    assert re.subn("(?i)b+", "x", "bbbb BBBB") == ('x x', 2)
    assert re.subn("b+", "x", "bbbb BBBB") == ('x BBBB', 1)
    assert re.subn("b+", "x", "xyz") == ('xyz', 0)
    assert re.subn("b*", "x", "xyz") == ('xxxyxzx', 4)
    
except AssertionError:
    raise TestFailed, "re.subn"

if verbose:
    print 'Running tests on re.split'

try:
    assert re.split(":", ":a:b::c") == ['', 'a', 'b', '', 'c']
    assert re.split(":*", ":a:b::c") == ['', 'a', 'b', 'c']
    assert re.split("(:*)", ":a:b::c") == ['', ':', 'a', ':', 'b', '::', 'c']
    assert re.split("(?::*)", ":a:b::c") == ['', 'a', 'b', 'c']
    assert re.split("(:)*", ":a:b::c") == ['', ':', 'a', ':', 'b', ':', 'c']
    assert re.split("([b:]+)", ":a:b::c") == ['', ':', 'a', ':b::', 'c']
    assert re.split("(b)|(:+)", ":a:b::c") == \
           ['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c']
    assert re.split("(?:b)|(?::+)", ":a:b::c") == ['', 'a', '', '', 'c']
    
except AssertionError:
    raise TestFailed, "re.split"

from re_tests import *
if verbose:
    print 'Running re_tests test suite'

for t in tests:
    print t
    sys.stdout.flush()
    pattern=s=outcome=repl=expected=None
    if len(t)==5:
	pattern, s, outcome, repl, expected = t
    elif len(t)==3:
	pattern, s, outcome = t 
    else:
	raise ValueError, ('Test tuples should have 3 or 5 fields',t)

    try:
	obj=re.compile(pattern)
    except re.error:
	if outcome==SYNTAX_ERROR: pass	# Expected a syntax error
	else: 
	    print '=== Syntax error:', t
    except:
	print '*** Unexpected error ***'
	if verbose:
	    traceback.print_exc(file=sys.stdout)
    else:
	try:
	    result=obj.search(s)
	except (re.error, reop.error), msg:
	    print '=== Unexpected exception', t, repr(msg)
	if outcome==SYNTAX_ERROR:
	    # This should have been a syntax error; forget it.
	    pass
	elif outcome==FAIL:
	    if result is None: pass   # No match, as expected
	    else: print '=== Succeeded incorrectly', t
	elif outcome==SUCCEED:
	    if result is not None:
		# Matched, as expected, so now we compute the
		# result string and compare it to our expected result.
		start, end = result.span(0)
		vardict={'found': result.group(0),
			 'groups': result.group(),
			 'flags': result.re.flags}
		for i in range(1, 100):
		    try:
			gi = result.group(i)
			# Special hack because else the string concat fails:
			if gi is None:
			    gi = "None"
		    except IndexError:
			gi = "Error"
		    vardict['g%d' % i] = gi
		for i in result.re.groupindex.keys():
		    try:
			gi = result.group(i)
			if gi is None:
			    gi = "None"
		    except IndexError:
			gi = "Error"
		    vardict[i] = gi
		repl=eval(repl, vardict)
		if repl!=expected:
		    print '=== grouping error', t,
		    print repr(repl)+' should be '+repr(expected)
	    else:
		print '=== Failed incorrectly', t