Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_regex.py @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | 55dff8169cf5 |
| children | aedd2d0dcb31 |
| rev | line source |
|---|---|
| 4810 | 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 | |
|
5539
8f871a030b8d
Changes to make these tests work on the Mac.
Guido van Rossum <guido@python.org>
parents:
4810
diff
changeset
|
51 print 'translate:', len(cre.translate) |
| 4810 | 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' | |
|
5718
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
63 |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
64 from regex_tests import * |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
65 if verbose: print 'Running regex_tests test suite' |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
66 |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
67 for t in tests: |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
68 pattern=s=outcome=repl=expected=None |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
69 if len(t)==5: |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
70 pattern, s, outcome, repl, expected = t |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
71 elif len(t)==3: |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
72 pattern, s, outcome = t |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
73 else: |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
74 raise ValueError, ('Test tuples should have 3 or 5 fields',t) |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
75 |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
76 try: |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
77 obj=regex.compile(pattern) |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
78 except regex.error: |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
79 if outcome==SYNTAX_ERROR: pass # Expected a syntax error |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
80 else: |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
81 # Regex syntax errors aren't yet reported, so for |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
82 # the official test suite they'll be quietly ignored. |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
83 pass |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
84 #print '=== Syntax error:', t |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
85 else: |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
86 try: |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
87 result=obj.search(s) |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
88 except regex.error, msg: |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
89 print '=== Unexpected exception', t, repr(msg) |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
90 if outcome==SYNTAX_ERROR: |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
91 # This should have been a syntax error; forget it. |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
92 pass |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
93 elif outcome==FAIL: |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
94 if result==-1: pass # No match, as expected |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
95 else: print '=== Succeeded incorrectly', t |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
96 elif outcome==SUCCEED: |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
97 if result!=-1: |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
98 # Matched, as expected, so now we compute the |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
99 # result string and compare it to our expected result. |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
100 start, end = obj.regs[0] |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
101 found=s[start:end] |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
102 groups=obj.group(1,2,3,4,5,6,7,8,9,10) |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
103 vardict=vars() |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
104 for i in range(len(groups)): |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
105 vardict['g'+str(i+1)]=str(groups[i]) |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
106 repl=eval(repl) |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
107 if repl!=expected: |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
108 print '=== grouping error', t, repr(repl)+' should be '+repr(expected) |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
109 else: |
|
55dff8169cf5
AMK's regex test suite
Guido van Rossum <guido@python.org>
parents:
5539
diff
changeset
|
110 print '=== Failed incorrectly', t |
