blob: 6a7f01b266900cd6bc28b0f387e86658afcb49d6 [file] [log] [blame]
Barry Warsaw4b722781996-12-20 22:00:21 +00001from test_support import verbose
2import regex
3from regex_syntax import *
4
5re = 'a+b+c+'
6print 'no match:', regex.match(re, 'hello aaaabcccc world')
7print 'successful search:', regex.search(re, 'hello aaaabcccc world')
8try:
9 cre = regex.compile('\(' + re)
10except regex.error:
11 print 'caught expected exception'
12else:
13 print 'expected regex.error not raised'
14
15print 'failed awk syntax:', regex.search('(a+)|(b+)', 'cdb')
16prev = regex.set_syntax(RE_SYNTAX_AWK)
17print 'successful awk syntax:', regex.search('(a+)|(b+)', 'cdb')
18regex.set_syntax(prev)
19print 'failed awk syntax:', regex.search('(a+)|(b+)', 'cdb')
20
21re = '\(<one>[0-9]+\) *\(<two>[0-9]+\)'
22print 'matching with group names and compile()'
23cre = regex.compile(re)
24print cre.match('801 999')
25try:
26 print cre.group('one')
27except regex.error:
28 print 'caught expected exception'
29else:
30 print 'expected regex.error not raised'
31
32print 'matching with group names and symcomp()'
33cre = regex.symcomp(re)
34print cre.match('801 999')
35print cre.group(0)
36print cre.group('one')
37print cre.group(1, 2)
38print cre.group('one', 'two')
39print 'realpat:', cre.realpat
40print 'groupindex:', cre.groupindex
41
42re = 'world'
43cre = regex.compile(re)
44print 'not case folded search:', cre.search('HELLO WORLD')
45cre = regex.compile(re, regex.casefold)
46print 'case folded search:', cre.search('HELLO WORLD')
47
48print '__members__:', cre.__members__
49print 'regs:', cre.regs
50print 'last:', cre.last
Guido van Rossum3f11da01997-05-16 13:51:48 +000051print 'translate:', len(cre.translate)
Barry Warsaw4b722781996-12-20 22:00:21 +000052print 'givenpat:', cre.givenpat
53
54print 'match with pos:', cre.match('hello world', 7)
55print 'search with pos:', cre.search('hello world there world', 7)
56print 'bogus group:', cre.group(0, 1, 3)
57try:
58 print 'no name:', cre.group('one')
59except regex.error:
60 print 'caught expected exception'
61else:
62 print 'expected regex.error not raised'
Guido van Rossum876736c1997-06-03 18:07:49 +000063
64from regex_tests import *
65if verbose: print 'Running regex_tests test suite'
66
67for t in tests:
68 pattern=s=outcome=repl=expected=None
69 if len(t)==5:
Guido van Rossum41360a41998-03-26 19:42:58 +000070 pattern, s, outcome, repl, expected = t
Guido van Rossum876736c1997-06-03 18:07:49 +000071 elif len(t)==3:
Guido van Rossum41360a41998-03-26 19:42:58 +000072 pattern, s, outcome = t
Guido van Rossum876736c1997-06-03 18:07:49 +000073 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000074 raise ValueError, ('Test tuples should have 3 or 5 fields',t)
Guido van Rossum876736c1997-06-03 18:07:49 +000075
76 try:
Guido van Rossum41360a41998-03-26 19:42:58 +000077 obj=regex.compile(pattern)
Guido van Rossum876736c1997-06-03 18:07:49 +000078 except regex.error:
Guido van Rossum41360a41998-03-26 19:42:58 +000079 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
Guido van Rossum876736c1997-06-03 18:07:49 +000085 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000086 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