99
1010# Misc tests from Tim Peters' re.doc
1111
12- try :
12+ if verbose :
13+ print 'Running tests on re.sub'
1314
15+ try :
1416 assert re .sub ("(?i)b+" , "x" , "bbbb BBBB" ) == 'x x'
15-
17+
1618 def bump_num (matchobj ):
1719 int_value = int (matchobj .group (0 ))
1820 return str (int_value + 1 )
1921
20- assert re .sub (r" \d+" , bump_num , '08.2 -2 23x99y' ) == '9.3 -3 24x100y'
21-
22+ assert re .sub (r' \d+' , bump_num , '08.2 -2 23x99y' ) == '9.3 -3 24x100y'
23+
2224 assert re .sub ('.' , lambda m : r"\n" , 'x' ) == '\\ n'
2325 assert re .sub ('.' , r"\n" , 'x' ) == '\n '
24-
26+
2527 s = r"\1\1"
2628 assert re .sub ('(.)' , s , 'x' ) == 'xx'
2729 assert re .sub ('(.)' , re .escape (s ), 'x' ) == s
2830 assert re .sub ('(.)' , lambda m : s , 'x' ) == s
2931
32+ assert re .sub ('(?P<a>x)' , '\g<a>\g<a>' , 'xx' ) == 'xxxx'
33+
3034except AssertionError :
3135 raise TestFailed , "re.sub"
3236
37+ if verbose :
38+ print 'Running tests on symbolic references'
39+
40+ try :
41+ re .sub ('(?P<a>x)' , '\g<a' , 'xx' )
42+ except re .error , reason :
43+ pass
44+ else :
45+ raise TestFailed , "symbolic reference"
46+
47+ try :
48+ re .sub ('(?P<a>x)' , '\g<' , 'xx' )
49+ except re .error , reason :
50+ pass
51+ else :
52+ raise TestFailed , "symbolic reference"
53+
54+ try :
55+ re .sub ('(?P<a>x)' , '\g' , 'xx' )
56+ except re .error , reason :
57+ pass
58+ else :
59+ raise TestFailed , "symbolic reference"
60+
61+ try :
62+ re .sub ('(?P<a>x)' , '\g<a a>' , 'xx' )
63+ except re .error , reason :
64+ pass
65+ else :
66+ raise TestFailed , "symbolic reference"
67+
68+ try :
69+ re .sub ('(?P<a>x)' , '\g<ab>' , 'xx' )
70+ except IndexError , reason :
71+ pass
72+ else :
73+ raise TestFailed , "symbolic reference"
74+
75+ try :
76+ re .sub ('(?P<a>x)|(?P<b>y)' , '\g<b>' , 'xx' )
77+ except re .error , reason :
78+ pass
79+ else :
80+ raise TestFailed , "symbolic reference"
81+
82+ try :
83+ re .sub ('(?P<a>x)|(?P<b>y)' , '\\ 2' , 'xx' )
84+ except re .error , reason :
85+ pass
86+ else :
87+ raise TestFailed , "symbolic reference"
88+
89+ if verbose :
90+ print 'Running tests on re.subn'
91+
3392try :
3493 assert re .subn ("(?i)b+" , "x" , "bbbb BBBB" ) == ('x x' , 2 )
3594 assert re .subn ("b+" , "x" , "bbbb BBBB" ) == ('x BBBB' , 1 )
3695 assert re .subn ("b+" , "x" , "xyz" ) == ('xyz' , 0 )
3796 assert re .subn ("b*" , "x" , "xyz" ) == ('xxxyxzx' , 4 )
38-
97+
3998except AssertionError :
4099 raise TestFailed , "re.subn"
41100
101+ if verbose :
102+ print 'Running tests on re.split'
103+
42104try :
43105 assert re .split (":" , ":a:b::c" ) == ['' , 'a' , 'b' , '' , 'c' ]
44106 assert re .split (":*" , ":a:b::c" ) == ['' , 'a' , 'b' , 'c' ]
@@ -47,14 +109,15 @@ def bump_num(matchobj):
47109 assert re .split ("(:)*" , ":a:b::c" ) == ['' , ':' , 'a' , ':' , 'b' , ':' , 'c' ]
48110 assert re .split ("([b:]+)" , ":a:b::c" ) == ['' , ':' , 'a' , ':b::' , 'c' ]
49111 assert re .split ("(b)|(:+)" , ":a:b::c" ) == \
50- ['' , None , ':' , 'a' , None , ':' , '' , 'b' , None , '' , None , '::' , 'c' ]
112+ ['' , None , ':' , 'a' , None , ':' , '' , 'b' , None , '' , None , '::' , 'c' ]
51113 assert re .split ("(?:b)|(?::+)" , ":a:b::c" ) == ['' , 'a' , '' , '' , 'c' ]
52-
114+
53115except AssertionError :
54116 raise TestFailed , "re.split"
55117
56118from re_tests import *
57- if verbose : print 'Running re_tests test suite'
119+ if verbose :
120+ print 'Running re_tests test suite'
58121
59122for t in tests :
60123 print t
@@ -73,8 +136,6 @@ def bump_num(matchobj):
73136 if outcome == SYNTAX_ERROR : pass # Expected a syntax error
74137 else :
75138 print '=== Syntax error:' , t
76- except KeyboardInterrupt :
77- raise KeyboardInterrupt
78139 except :
79140 print '*** Unexpected error ***'
80141 if verbose :
0 commit comments