Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 1 | from test_support import verbose |
| 2 | import regex |
| 3 | from regex_syntax import * |
| 4 | |
| 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' |
| 14 | |
| 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') |
| 20 | |
| 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' |
| 31 | |
| 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 |
| 41 | |
| 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') |
| 47 | |
| 48 | print '__members__:', cre.__members__ |
| 49 | print 'regs:', cre.regs |
| 50 | print 'last:', cre.last |
Guido van Rossum | f1e6354 | 1997-05-22 20:48:03 +0000 | [diff] [blame] | 51 | print 'translate:', len(cre.translate) |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 52 | print 'givenpat:', cre.givenpat |
| 53 | |
| 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' |
Guido van Rossum | ff712aa | 1997-08-15 00:45:26 +0000 | [diff] [blame^] | 63 | |
| 64 | from regex_tests import * |
| 65 | if verbose: print 'Running regex_tests test suite' |
| 66 | |
| 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) |
| 75 | |
| 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 |