Added 'list_only' option (and modified 'run()' to respect it).
[python/dscho.git] / Lib / test / test_regex.py
blob6a7f01b266900cd6bc28b0f387e86658afcb49d6
1 from test_support import verbose
2 import regex
3 from regex_syntax import *
5 re = 'a+b+c+'
6 print 'no match:', regex.match(re, 'hello aaaabcccc world')
7 print 'successful search:', regex.search(re, 'hello aaaabcccc world')
8 try:
9 cre = regex.compile('\(' + re)
10 except regex.error:
11 print 'caught expected exception'
12 else:
13 print 'expected regex.error not raised'
15 print 'failed awk syntax:', regex.search('(a+)|(b+)', 'cdb')
16 prev = regex.set_syntax(RE_SYNTAX_AWK)
17 print 'successful awk syntax:', regex.search('(a+)|(b+)', 'cdb')
18 regex.set_syntax(prev)
19 print 'failed awk syntax:', regex.search('(a+)|(b+)', 'cdb')
21 re = '\(<one>[0-9]+\) *\(<two>[0-9]+\)'
22 print 'matching with group names and compile()'
23 cre = regex.compile(re)
24 print cre.match('801 999')
25 try:
26 print cre.group('one')
27 except regex.error:
28 print 'caught expected exception'
29 else:
30 print 'expected regex.error not raised'
32 print 'matching with group names and symcomp()'
33 cre = regex.symcomp(re)
34 print cre.match('801 999')
35 print cre.group(0)
36 print cre.group('one')
37 print cre.group(1, 2)
38 print cre.group('one', 'two')
39 print 'realpat:', cre.realpat
40 print 'groupindex:', cre.groupindex
42 re = 'world'
43 cre = regex.compile(re)
44 print 'not case folded search:', cre.search('HELLO WORLD')
45 cre = regex.compile(re, regex.casefold)
46 print 'case folded search:', cre.search('HELLO WORLD')
48 print '__members__:', cre.__members__
49 print 'regs:', cre.regs
50 print 'last:', cre.last
51 print 'translate:', len(cre.translate)
52 print 'givenpat:', cre.givenpat
54 print 'match with pos:', cre.match('hello world', 7)
55 print 'search with pos:', cre.search('hello world there world', 7)
56 print 'bogus group:', cre.group(0, 1, 3)
57 try:
58 print 'no name:', cre.group('one')
59 except regex.error:
60 print 'caught expected exception'
61 else:
62 print 'expected regex.error not raised'
64 from regex_tests import *
65 if verbose: print 'Running regex_tests test suite'
67 for t in tests:
68 pattern=s=outcome=repl=expected=None
69 if len(t)==5:
70 pattern, s, outcome, repl, expected = t
71 elif len(t)==3:
72 pattern, s, outcome = t
73 else:
74 raise ValueError, ('Test tuples should have 3 or 5 fields',t)
76 try:
77 obj=regex.compile(pattern)
78 except regex.error:
79 if outcome==SYNTAX_ERROR: pass # Expected a syntax error
80 else:
81 # Regex syntax errors aren't yet reported, so for
82 # the official test suite they'll be quietly ignored.
83 pass
84 #print '=== Syntax error:', t
85 else:
86 try:
87 result=obj.search(s)
88 except regex.error, msg:
89 print '=== Unexpected exception', t, repr(msg)
90 if outcome==SYNTAX_ERROR:
91 # This should have been a syntax error; forget it.
92 pass
93 elif outcome==FAIL:
94 if result==-1: pass # No match, as expected
95 else: print '=== Succeeded incorrectly', t
96 elif outcome==SUCCEED:
97 if result!=-1:
98 # Matched, as expected, so now we compute the
99 # result string and compare it to our expected result.
100 start, end = obj.regs[0]
101 found=s[start:end]
102 groups=obj.group(1,2,3,4,5,6,7,8,9,10)
103 vardict=vars()
104 for i in range(len(groups)):
105 vardict['g'+str(i+1)]=str(groups[i])
106 repl=eval(repl)
107 if repl!=expected:
108 print '=== grouping error', t, repr(repl)+' should be '+repr(expected)
109 else:
110 print '=== Failed incorrectly', t