home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / AACD / Programming / Python / Lib / Python1.5 / test / test_re.py < prev    next >
Encoding:
Python Source  |  1998-07-17  |  10.9 KB  |  349 lines

  1. import sys
  2. sys.path=['.']+sys.path
  3.  
  4. from test_support import verbose, TestFailed
  5. import re
  6. import sys, os, string, traceback
  7.  
  8. # Misc tests from Tim Peters' re.doc
  9.  
  10. if verbose:
  11.     print 'Running tests on re.search and re.match'
  12.  
  13. try:
  14.     assert re.search('x*', 'axx').span(0) == (0, 0)
  15.     assert re.search('x*', 'axx').span() == (0, 0)
  16.     assert re.search('x+', 'axx').span(0) == (1, 3)
  17.     assert re.search('x+', 'axx').span() == (1, 3)
  18.     assert re.search('x', 'aaa') == None
  19. except:
  20.     raise TestFailed, "re.search"
  21.  
  22. try:
  23.     assert re.match('a*', 'xxx').span(0) == (0, 0)
  24.     assert re.match('a*', 'xxx').span() == (0, 0)
  25.     assert re.match('x*', 'xxxa').span(0) == (0, 3)
  26.     assert re.match('x*', 'xxxa').span() == (0, 3)
  27.     assert re.match('a+', 'xxx') == None
  28. except:
  29.     raise TestFailed, "re.search"
  30.  
  31. if verbose:
  32.     print 'Running tests on re.sub'
  33.  
  34. try:
  35.     assert re.sub("(?i)b+", "x", "bbbb BBBB") == 'x x'
  36.     
  37.     def bump_num(matchobj):
  38.         int_value = int(matchobj.group(0))
  39.         return str(int_value + 1)
  40.  
  41.     assert re.sub(r'\d+', bump_num, '08.2 -2 23x99y') == '9.3 -3 24x100y'
  42.     assert re.sub(r'\d+', bump_num, '08.2 -2 23x99y', 3) == '9.3 -3 23x99y'
  43.     
  44.     assert re.sub('.', lambda m: r"\n", 'x') == '\\n'
  45.     assert re.sub('.', r"\n", 'x') == '\n'
  46.  
  47.     s = r"\1\1"
  48.     assert re.sub('(.)', s, 'x') == 'xx'
  49.     assert re.sub('(.)', re.escape(s), 'x') == s 
  50.     assert re.sub('(.)', lambda m: s, 'x') == s
  51.  
  52.     assert re.sub('(?P<a>x)', '\g<a>\g<a>', 'xx') == 'xxxx'
  53.     assert re.sub('(?P<a>x)', '\g<a>\g<1>', 'xx') == 'xxxx'
  54.     assert re.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx') == 'xxxx'
  55.     assert re.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx') == 'xxxx'
  56.  
  57.     assert re.sub('a', r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D', 'a') == '\t\n\v\r\f\a\b\\B\\Z\a\\A\\w\\W\\s\\S\\d\\D'
  58.     assert re.sub('a', '\t\n\v\r\f\a', 'a') == '\t\n\v\r\f\a'
  59.     assert re.sub('a', '\t\n\v\r\f\a', 'a') == (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7))
  60.  
  61.     assert re.sub('^\s*', 'X', 'test') == 'Xtest'
  62. except AssertionError:
  63.     raise TestFailed, "re.sub"
  64.  
  65.  
  66. try:
  67.     assert re.sub('a', 'b', 'aaaaa') == 'bbbbb'
  68.     assert re.sub('a', 'b', 'aaaaa', 1) == 'baaaa'
  69. except AssertionError:
  70.     raise TestFailed, "qualified re.sub"
  71.  
  72. if verbose:
  73.     print 'Running tests on symbolic references'
  74.  
  75. try:
  76.     re.sub('(?P<a>x)', '\g<a', 'xx')
  77. except re.error, reason:
  78.     pass
  79. else:
  80.     raise TestFailed, "symbolic reference"
  81.  
  82. try:
  83.     re.sub('(?P<a>x)', '\g<', 'xx')
  84. except re.error, reason:
  85.     pass
  86. else:
  87.     raise TestFailed, "symbolic reference"
  88.  
  89. try:
  90.     re.sub('(?P<a>x)', '\g', 'xx')
  91. except re.error, reason:
  92.     pass
  93. else:
  94.     raise TestFailed, "symbolic reference"
  95.  
  96. try:
  97.     re.sub('(?P<a>x)', '\g<a a>', 'xx')
  98. except re.error, reason:
  99.     pass
  100. else:
  101.     raise TestFailed, "symbolic reference"
  102.  
  103. try:
  104.     re.sub('(?P<a>x)', '\g<1a1>', 'xx')
  105. except re.error, reason:
  106.     pass
  107. else:
  108.     raise TestFailed, "symbolic reference"
  109.  
  110. try:
  111.     re.sub('(?P<a>x)', '\g<ab>', 'xx')
  112. except IndexError, reason:
  113.     pass
  114. else:
  115.     raise TestFailed, "symbolic reference"
  116.  
  117. try:
  118.     re.sub('(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')
  119. except re.error, reason:
  120.     pass
  121. else:
  122.     raise TestFailed, "symbolic reference"
  123.  
  124. try:
  125.     re.sub('(?P<a>x)|(?P<b>y)', '\\2', 'xx')
  126. except re.error, reason:
  127.     pass
  128. else:
  129.     raise TestFailed, "symbolic reference"
  130.  
  131. if verbose:
  132.     print 'Running tests on re.subn'
  133.  
  134. try:
  135.     assert re.subn("(?i)b+", "x", "bbbb BBBB") == ('x x', 2)
  136.     assert re.subn("b+", "x", "bbbb BBBB") == ('x BBBB', 1)
  137.     assert re.subn("b+", "x", "xyz") == ('xyz', 0)
  138.     assert re.subn("b*", "x", "xyz") == ('xxxyxzx', 4)
  139.     assert re.subn("b*", "x", "xyz", 2) == ('xxxyz', 2)
  140. except AssertionError:
  141.     raise TestFailed, "re.subn"
  142.  
  143. if verbose:
  144.     print 'Running tests on re.split'
  145.     
  146. try:
  147.     assert re.split(":", ":a:b::c") == ['', 'a', 'b', '', 'c']
  148.     assert re.split(":*", ":a:b::c") == ['', 'a', 'b', 'c']
  149.     assert re.split("(:*)", ":a:b::c") == ['', ':', 'a', ':', 'b', '::', 'c']
  150.     assert re.split("(?::*)", ":a:b::c") == ['', 'a', 'b', 'c']
  151.     assert re.split("(:)*", ":a:b::c") == ['', ':', 'a', ':', 'b', ':', 'c']
  152.     assert re.split("([b:]+)", ":a:b::c") == ['', ':', 'a', ':b::', 'c']
  153.     assert re.split("(b)|(:+)", ":a:b::c") == \
  154.            ['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c']
  155.     assert re.split("(?:b)|(?::+)", ":a:b::c") == ['', 'a', '', '', 'c']
  156. except AssertionError:
  157.     raise TestFailed, "re.split"
  158.  
  159. try:
  160.     assert re.split(":", ":a:b::c", 2) == ['', 'a', 'b::c']
  161.     assert re.split(':', 'a:b:c:d', 2) == ['a', 'b', 'c:d']
  162.  
  163.     assert re.split("(:)", ":a:b::c", 2) == ['', ':', 'a', ':', 'b::c']
  164.     assert re.split("(:*)", ":a:b::c", 2) == ['', ':', 'a', ':', 'b::c']    
  165. except AssertionError:
  166.     raise TestFailed, "qualified re.split"
  167.  
  168. if verbose:
  169.     print "Running tests on re.findall"
  170.  
  171. try:
  172.     assert re.findall(":+", "abc") == []
  173.     assert re.findall(":+", "a:b::c:::d") == [":", "::", ":::"]
  174.     assert re.findall("(:+)", "a:b::c:::d") == [":", "::", ":::"]
  175.     assert re.findall("(:)(:*)", "a:b::c:::d") == [(":", ""),
  176.                                                    (":", ":"),
  177.                                                    (":", "::")]
  178. except AssertionError:
  179.     raise TestFailed, "re.findall"
  180.  
  181. if verbose:
  182.     print "Running tests on re.match"
  183.  
  184. try:
  185.     # No groups at all
  186.     m = re.match('a', 'a') ; assert m.groups() == ()    
  187.     # A single group
  188.     m = re.match('(a)', 'a') ; assert m.groups() == ('a',)      
  189.  
  190.     pat = re.compile('((a)|(b))(c)?')
  191.     assert pat.match('a').groups() == ('a', 'a', None, None)    
  192.     assert pat.match('b').groups() == ('b', None, 'b', None)    
  193.     assert pat.match('ac').groups() == ('a', 'a', None, 'c')    
  194.     assert pat.match('bc').groups() == ('b', None, 'b', 'c')    
  195.     assert pat.match('bc').groups("") == ('b', "", 'b', 'c')    
  196. except AssertionError:
  197.     raise TestFailed, "match .groups() method"
  198.  
  199. try:
  200.     # A single group
  201.     m = re.match('(a)', 'a') 
  202.     assert m.group(0) == 'a' ; assert m.group(0) == 'a' 
  203.     assert m.group(1) == 'a' ; assert m.group(1, 1) == ('a', 'a')
  204.  
  205.     pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
  206.     assert pat.match('a').group(1, 2, 3) == ('a', None, None)   
  207.     assert pat.match('b').group('a1', 'b2', 'c3') == (None, 'b', None)  
  208.     assert pat.match('ac').group(1, 'b2', 3) == ('a', None, 'c')        
  209. except AssertionError:
  210.     raise TestFailed, "match .group() method"
  211.  
  212. if verbose:
  213.     print "Running tests on re.escape"
  214.  
  215. try:
  216.     p=""
  217.     for i in range(0, 256):
  218.         p = p + chr(i)
  219.         assert re.match(re.escape(chr(i)), chr(i)) != None
  220.         assert re.match(re.escape(chr(i)), chr(i)).span() == (0,1)
  221.  
  222.     pat=re.compile( re.escape(p) )
  223.     assert pat.match(p) != None
  224.     assert pat.match(p).span() == (0,256)
  225. except AssertionError:
  226.     raise TestFailed, "re.escape"
  227.  
  228.  
  229. if verbose:
  230.     print 'Pickling a RegexObject instance'
  231.  
  232. import pickle
  233. pat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
  234. s = pickle.dumps(pat)
  235. pat = pickle.loads(s)
  236.  
  237. try:
  238.     assert re.I == re.IGNORECASE
  239.     assert re.L == re.LOCALE
  240.     assert re.M == re.MULTILINE
  241.     assert re.S == re.DOTALL 
  242.     assert re.X == re.VERBOSE 
  243. except AssertionError:
  244.     raise TestFailed, 're module constants'
  245.  
  246. for flags in [re.I, re.M, re.X, re.S, re.L]:
  247.     try:
  248.         r = re.compile('^pattern$', flags)
  249.     except:
  250.         print 'Exception raised on flag', flags
  251.  
  252. from re_tests import *
  253.  
  254. if verbose:
  255.     print 'Running re_tests test suite'
  256. else:
  257.     # To save time, only run the first and last 10 tests
  258.     #tests = tests[:10] + tests[-10:]
  259.     pass 
  260.  
  261. for t in tests:
  262.     sys.stdout.flush()
  263.     pattern=s=outcome=repl=expected=None
  264.     if len(t)==5:
  265.         pattern, s, outcome, repl, expected = t
  266.     elif len(t)==3:
  267.         pattern, s, outcome = t 
  268.     else:
  269.         raise ValueError, ('Test tuples should have 3 or 5 fields',t)
  270.  
  271.     try:
  272.         obj=re.compile(pattern)
  273.     except re.error:
  274.         if outcome==SYNTAX_ERROR: pass  # Expected a syntax error
  275.         else: 
  276.             print '=== Syntax error:', t
  277.     except KeyboardInterrupt: raise KeyboardInterrupt
  278.     except:
  279.         print '*** Unexpected error ***', t
  280.         if verbose:
  281.             traceback.print_exc(file=sys.stdout)
  282.     else:
  283.         try:
  284.             result=obj.search(s)
  285.         except (re.error), msg:
  286.             print '=== Unexpected exception', t, repr(msg)
  287.         if outcome==SYNTAX_ERROR:
  288.             # This should have been a syntax error; forget it.
  289.             pass
  290.         elif outcome==FAIL:
  291.             if result is None: pass   # No match, as expected
  292.             else: print '=== Succeeded incorrectly', t
  293.         elif outcome==SUCCEED:
  294.             if result is not None:
  295.                 # Matched, as expected, so now we compute the
  296.                 # result string and compare it to our expected result.
  297.                 start, end = result.span(0)
  298.                 vardict={'found': result.group(0),
  299.                          'groups': result.group(),
  300.                          'flags': result.re.flags}
  301.                 for i in range(1, 100):
  302.                     try:
  303.                         gi = result.group(i)
  304.                         # Special hack because else the string concat fails:
  305.                         if gi is None:
  306.                             gi = "None"
  307.                     except IndexError:
  308.                         gi = "Error"
  309.                     vardict['g%d' % i] = gi
  310.                 for i in result.re.groupindex.keys():
  311.                     try:
  312.                         gi = result.group(i)
  313.                         if gi is None:
  314.                             gi = "None"
  315.                     except IndexError:
  316.                         gi = "Error"
  317.                     vardict[i] = gi
  318.                 repl=eval(repl, vardict)
  319.                 if repl!=expected:
  320.                     print '=== grouping error', t,
  321.                     print repr(repl)+' should be '+repr(expected)
  322.             else:
  323.                 print '=== Failed incorrectly', t
  324.  
  325.             # Try the match with the search area limited to the extent
  326.             # of the match and see if it still succeeds.  \B will
  327.             # break (because it won't match at the end or start of a
  328.             # string), so we'll ignore patterns that feature it.
  329.             
  330.             if pattern[:2]!='\\B' and pattern[-2:]!='\\B':
  331.                 obj=re.compile(pattern)
  332.                 result=obj.search(s, pos=result.start(0), endpos=result.end(0)+1)
  333.                 if result==None:
  334.                     print '=== Failed on range-limited match', t
  335.  
  336.             # Try the match with IGNORECASE enabled, and check that it
  337.             # still succeeds.
  338.             obj=re.compile(pattern, re.IGNORECASE)
  339.             result=obj.search(s)
  340.             if result==None:
  341.                 print '=== Fails on case-insensitive match', t
  342.  
  343.             # Try the match with LOCALE enabled, and check that it
  344.             # still succeeds.
  345.             obj=re.compile(pattern, re.LOCALE)
  346.             result=obj.search(s)
  347.             if result==None:
  348.                 print '=== Fails on locale-sensitive match', t
  349.