22# -*- mode: python -*-
33# $Id$
44
5+ import sys
6+ sys .path = ['.' ]+ sys .path
7+
58from test_support import verbose , TestFailed
69import re
710import sys , os , string , traceback
811
912# Misc tests from Tim Peters' re.doc
1013
14+ if verbose :
15+ print 'Running tests on re.search and re.match'
16+
17+ try :
18+ assert re .search ('x*' , 'axx' ).span (0 ) == (0 , 0 )
19+ assert re .search ('x*' , 'axx' ).span () == (0 , 0 )
20+ assert re .search ('x+' , 'axx' ).span (0 ) == (1 , 3 )
21+ assert re .search ('x+' , 'axx' ).span () == (1 , 3 )
22+ assert re .search ('x' , 'aaa' ) == None
23+ except :
24+ raise TestFailed , "re.search"
25+
26+ try :
27+ assert re .match ('a*' , 'xxx' ).span (0 ) == (0 , 0 )
28+ assert re .match ('a*' , 'xxx' ).span () == (0 , 0 )
29+ assert re .match ('x*' , 'xxxa' ).span (0 ) == (0 , 3 )
30+ assert re .match ('x*' , 'xxxa' ).span () == (0 , 3 )
31+ assert re .match ('a+' , 'xxx' ) == None
32+ except :
33+ raise TestFailed , "re.search"
34+
1135if verbose :
1236 print 'Running tests on re.sub'
1337
@@ -19,25 +43,30 @@ def bump_num(matchobj):
1943 return str (int_value + 1 )
2044
2145 assert re .sub (r'\d+' , bump_num , '08.2 -2 23x99y' ) == '9.3 -3 24x100y'
46+ assert re .sub (r'\d+' , bump_num , '08.2 -2 23x99y' , 3 ) == '9.3 -3 23x99y'
2247
2348 assert re .sub ('.' , lambda m : r"\n" , 'x' ) == '\\ n'
2449 assert re .sub ('.' , r"\n" , 'x' ) == '\n '
2550
2651 s = r"\1\1"
2752 assert re .sub ('(.)' , s , 'x' ) == 'xx'
28- assert re .sub ('(.)' , re .escape (s ), 'x' ) == s
53+ assert re .sub ('(.)' , re .escape (s ), 'x' ) == s
2954 assert re .sub ('(.)' , lambda m : s , 'x' ) == s
3055
3156 assert re .sub ('(?P<a>x)' , '\g<a>\g<a>' , 'xx' ) == 'xxxx'
57+ assert re .sub ('(?P<a>x)' , '\g<a>\g<1>' , 'xx' ) == 'xxxx'
3258 assert re .sub ('(?P<unk>x)' , '\g<unk>\g<unk>' , 'xx' ) == 'xxxx'
59+ assert re .sub ('(?P<unk>x)' , '\g<1>\g<1>' , 'xx' ) == 'xxxx'
3360
34- assert re .sub ('a' , r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D' , 'a' ) == '\t \n \v \r \f \a \b BZ \a AwWsSdD '
61+ assert re .sub ('a' , r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D' , 'a' ) == '\t \n \v \r \f \a \b \\ B \\ Z \a \\ A \\ w \\ W \\ s \\ S \\ d \\ D '
3562 assert re .sub ('a' , '\t \n \v \r \f \a ' , 'a' ) == '\t \n \v \r \f \a '
3663 assert re .sub ('a' , '\t \n \v \r \f \a ' , 'a' ) == (chr (9 )+ chr (10 )+ chr (11 )+ chr (13 )+ chr (12 )+ chr (7 ))
3764
65+ assert re .sub ('^\s*' , 'X' , 'test' ) == 'Xtest'
3866except AssertionError :
3967 raise TestFailed , "re.sub"
4068
69+
4170try :
4271 assert re .sub ('a' , 'b' , 'aaaaa' ) == 'bbbbb'
4372 assert re .sub ('a' , 'b' , 'aaaaa' , 1 ) == 'baaaa'
@@ -75,6 +104,13 @@ def bump_num(matchobj):
75104else :
76105 raise TestFailed , "symbolic reference"
77106
107+ try :
108+ re .sub ('(?P<a>x)' , '\g<1a1>' , 'xx' )
109+ except re .error , reason :
110+ pass
111+ else :
112+ raise TestFailed , "symbolic reference"
113+
78114try :
79115 re .sub ('(?P<a>x)' , '\g<ab>' , 'xx' )
80116except IndexError , reason :
@@ -104,9 +140,13 @@ def bump_num(matchobj):
104140 assert re .subn ("b+" , "x" , "bbbb BBBB" ) == ('x BBBB' , 1 )
105141 assert re .subn ("b+" , "x" , "xyz" ) == ('xyz' , 0 )
106142 assert re .subn ("b*" , "x" , "xyz" ) == ('xxxyxzx' , 4 )
143+ assert re .subn ("b*" , "x" , "xyz" , 2 ) == ('xxxyz' , 2 )
107144except AssertionError :
108145 raise TestFailed , "re.subn"
109146
147+ if verbose :
148+ print 'Running tests on re.split'
149+
110150try :
111151 assert re .split (":" , ":a:b::c" ) == ['' , 'a' , 'b' , '' , 'c' ]
112152 assert re .split (":*" , ":a:b::c" ) == ['' , 'a' , 'b' , 'c' ]
@@ -117,7 +157,6 @@ def bump_num(matchobj):
117157 assert re .split ("(b)|(:+)" , ":a:b::c" ) == \
118158 ['' , None , ':' , 'a' , None , ':' , '' , 'b' , None , '' , None , '::' , 'c' ]
119159 assert re .split ("(?:b)|(?::+)" , ":a:b::c" ) == ['' , 'a' , '' , '' , 'c' ]
120-
121160except AssertionError :
122161 raise TestFailed , "re.split"
123162
@@ -130,16 +169,55 @@ def bump_num(matchobj):
130169except AssertionError :
131170 raise TestFailed , "qualified re.split"
132171
172+ try :
173+ # No groups at all
174+ m = re .match ('a' , 'a' ) ; assert m .groups () == ()
175+ # A single group
176+ m = re .match ('(a)' , 'a' ) ; assert m .groups () == ('a' ,)
177+
178+ pat = re .compile ('((a)|(b))(c)?' )
179+ assert pat .match ('a' ).groups () == ('a' , 'a' , None , None )
180+ assert pat .match ('b' ).groups () == ('b' , None , 'b' , None )
181+ assert pat .match ('ac' ).groups () == ('a' , 'a' , None , 'c' )
182+ assert pat .match ('bc' ).groups () == ('b' , None , 'b' , 'c' )
183+ except AssertionError :
184+ raise TestFailed , "match .groups() method"
185+
186+ try :
187+ # A single group
188+ m = re .match ('(a)' , 'a' )
189+ assert m .group (0 ) == 'a' ; assert m .group (0 ) == 'a'
190+ assert m .group (1 ) == 'a' ; assert m .group (1 , 1 ) == ('a' , 'a' )
191+
192+ pat = re .compile ('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?' )
193+ assert pat .match ('a' ).group (1 , 2 , 3 ) == ('a' , None , None )
194+ assert pat .match ('b' ).group ('a1' , 'b2' , 'c3' ) == (None , 'b' , None )
195+ assert pat .match ('ac' ).group (1 , 'b2' , 3 ) == ('a' , None , 'c' )
196+ except AssertionError :
197+ raise TestFailed , "match .group() method"
198+
199+ try :
200+ p = ""
201+ for i in range (0 , 256 ):
202+ p = p + chr (i )
203+ assert re .match (re .escape (chr (i )), chr (i )) != None
204+ assert re .match (re .escape (chr (i )), chr (i )).span () == (0 ,1 )
205+
206+ pat = re .compile ( re .escape (p ) )
207+ assert pat .match (p ) != None
208+ assert pat .match (p ).span () == (0 ,256 )
209+ except AssertionError :
210+ raise TestFailed , "re.escape"
211+
212+
133213if verbose :
134214 print 'Pickling a RegexObject instance'
135- import pickle
136- pat = re .compile ('a(?:b|(c|e){1,2}?|d)+?(.)' )
137- s = pickle .dumps (pat )
138- pat = pickle .loads (s )
139215
140- if verbose :
141- print 'Running tests on re.split'
142-
216+ import pickle
217+ pat = re .compile ('a(?:b|(c|e){1,2}?|d)+?(.)' )
218+ s = pickle .dumps (pat )
219+ pat = pickle .loads (s )
220+
143221try :
144222 assert re .I == re .IGNORECASE
145223 assert re .L == re .LOCALE
@@ -156,11 +234,13 @@ def bump_num(matchobj):
156234 print 'Exception raised on flag' , flags
157235
158236from re_tests import *
237+
159238if verbose :
160239 print 'Running re_tests test suite'
161240else :
162241 # To save time, only run the first and last 10 tests
163- pass #tests = tests[:10] + tests[-10:]
242+ #tests = tests[:10] + tests[-10:]
243+ pass
164244
165245for t in tests :
166246 sys .stdout .flush ()
@@ -180,7 +260,7 @@ def bump_num(matchobj):
180260 print '=== Syntax error:' , t
181261 except KeyboardInterrupt : raise KeyboardInterrupt
182262 except :
183- print '*** Unexpected error ***'
263+ print '*** Unexpected error ***' , t
184264 if verbose :
185265 traceback .print_exc (file = sys .stdout )
186266 else :
@@ -250,4 +330,3 @@ def bump_num(matchobj):
250330 result = obj .search (s )
251331 if result == None :
252332 print '=== Fails on locale-sensitive match' , t
253-
0 commit comments