Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 1 | from test.test_support import verbose, sortdict |
Fred Drake | 9a1a7dd | 2000-12-23 22:08:27 +0000 | [diff] [blame] | 2 | import warnings |
| 3 | warnings.filterwarnings("ignore", "the regex module is deprecated", |
Fred Drake | 1e146e7 | 2002-10-17 22:13:28 +0000 | [diff] [blame] | 4 | DeprecationWarning, __name__) |
Barry Warsaw | 4b72278 | 1996-12-20 22:00:21 +0000 | [diff] [blame] | 5 | import regex |
| 6 | from regex_syntax import * |
| 7 | |
| 8 | re = 'a+b+c+' |
| 9 | print 'no match:', regex.match(re, 'hello aaaabcccc world') |
| 10 | print 'successful search:', regex.search(re, 'hello aaaabcccc world') |
| 11 | try: |
| 12 | cre = regex.compile('\(' + re) |
| 13 | except regex.error: |
| 14 | print 'caught expected exception' |
| 15 | else: |
| 16 | print 'expected regex.error not raised' |
| 17 | |
| 18 | print 'failed awk syntax:', regex.search('(a+)|(b+)', 'cdb') |
| 19 | prev = regex.set_syntax(RE_SYNTAX_AWK) |
| 20 | print 'successful awk syntax:', regex.search('(a+)|(b+)', 'cdb') |
| 21 | regex.set_syntax(prev) |
| 22 | print 'failed awk syntax:', regex.search('(a+)|(b+)', 'cdb') |
| 23 | |
| 24 | re = '\(<one>[0-9]+\) *\(<two>[0-9]+\)' |
| 25 | print 'matching with group names and compile()' |
| 26 | cre = regex.compile(re) |
| 27 | print cre.match('801 999') |
| 28 | try: |
| 29 | print cre.group('one') |
| 30 | except regex.error: |
| 31 | print 'caught expected exception' |
| 32 | else: |
| 33 | print 'expected regex.error not raised' |
| 34 | |
| 35 | print 'matching with group names and symcomp()' |
| 36 | cre = regex.symcomp(re) |
| 37 | print cre.match('801 999') |
| 38 | print cre.group(0) |
| 39 | print cre.group('one') |
| 40 | print cre.group(1, 2) |
| 41 | print cre.group('one', 'two') |
| 42 | print 'realpat:', cre.realpat |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 43 | print 'groupindex:', sortdict(cre.groupindex) |
Barry Warsaw | 4b72278 | 1996-12-20 22:00:21 +0000 | [diff] [blame] | 44 | |
| 45 | re = 'world' |
| 46 | cre = regex.compile(re) |
| 47 | print 'not case folded search:', cre.search('HELLO WORLD') |
| 48 | cre = regex.compile(re, regex.casefold) |
| 49 | print 'case folded search:', cre.search('HELLO WORLD') |
| 50 | |
| 51 | print '__members__:', cre.__members__ |
| 52 | print 'regs:', cre.regs |
| 53 | print 'last:', cre.last |
Guido van Rossum | 3f11da0 | 1997-05-16 13:51:48 +0000 | [diff] [blame] | 54 | print 'translate:', len(cre.translate) |
Barry Warsaw | 4b72278 | 1996-12-20 22:00:21 +0000 | [diff] [blame] | 55 | print 'givenpat:', cre.givenpat |
| 56 | |
| 57 | print 'match with pos:', cre.match('hello world', 7) |
| 58 | print 'search with pos:', cre.search('hello world there world', 7) |
| 59 | print 'bogus group:', cre.group(0, 1, 3) |
| 60 | try: |
| 61 | print 'no name:', cre.group('one') |
| 62 | except regex.error: |
| 63 | print 'caught expected exception' |
| 64 | else: |
| 65 | print 'expected regex.error not raised' |
Guido van Rossum | 876736c | 1997-06-03 18:07:49 +0000 | [diff] [blame] | 66 | |
| 67 | from regex_tests import * |
| 68 | if verbose: print 'Running regex_tests test suite' |
| 69 | |
| 70 | for t in tests: |
| 71 | pattern=s=outcome=repl=expected=None |
| 72 | if len(t)==5: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 73 | pattern, s, outcome, repl, expected = t |
Guido van Rossum | 876736c | 1997-06-03 18:07:49 +0000 | [diff] [blame] | 74 | elif len(t)==3: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 75 | pattern, s, outcome = t |
Guido van Rossum | 876736c | 1997-06-03 18:07:49 +0000 | [diff] [blame] | 76 | else: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 77 | raise ValueError, ('Test tuples should have 3 or 5 fields',t) |
Guido van Rossum | 876736c | 1997-06-03 18:07:49 +0000 | [diff] [blame] | 78 | |
| 79 | try: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 80 | obj=regex.compile(pattern) |
Guido van Rossum | 876736c | 1997-06-03 18:07:49 +0000 | [diff] [blame] | 81 | except regex.error: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 82 | if outcome==SYNTAX_ERROR: pass # Expected a syntax error |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 83 | else: |
| 84 | # Regex syntax errors aren't yet reported, so for |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 85 | # the official test suite they'll be quietly ignored. |
| 86 | pass |
| 87 | #print '=== Syntax error:', t |
Guido van Rossum | 876736c | 1997-06-03 18:07:49 +0000 | [diff] [blame] | 88 | else: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 89 | try: |
| 90 | result=obj.search(s) |
| 91 | except regex.error, msg: |
| 92 | print '=== Unexpected exception', t, repr(msg) |
| 93 | if outcome==SYNTAX_ERROR: |
| 94 | # This should have been a syntax error; forget it. |
| 95 | pass |
| 96 | elif outcome==FAIL: |
| 97 | if result==-1: pass # No match, as expected |
| 98 | else: print '=== Succeeded incorrectly', t |
| 99 | elif outcome==SUCCEED: |
| 100 | if result!=-1: |
| 101 | # Matched, as expected, so now we compute the |
| 102 | # result string and compare it to our expected result. |
| 103 | start, end = obj.regs[0] |
| 104 | found=s[start:end] |
| 105 | groups=obj.group(1,2,3,4,5,6,7,8,9,10) |
| 106 | vardict=vars() |
| 107 | for i in range(len(groups)): |
| 108 | vardict['g'+str(i+1)]=str(groups[i]) |
| 109 | repl=eval(repl) |
| 110 | if repl!=expected: |
| 111 | print '=== grouping error', t, repr(repl)+' should be '+repr(expected) |
| 112 | else: |
| 113 | print '=== Failed incorrectly', t |