home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / test / test_regex.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  3.3 KB  |  116 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. verbose
  5. *
  6. print 'no match:', regex.match(re, 'hello aaaabcccc world')
  7. print 'successful search:', regex.search(re, 'hello aaaabcccc world')
  8.  
  9. try:
  10.     cre = regex.compile('\\(' + re)
  11. except regex.error:
  12.     print 'caught expected exception'
  13.  
  14. print 'expected regex.error not raised'
  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. re = '\\(<one>[0-9]+\\) *\\(<two>[0-9]+\\)'
  21. print 'matching with group names and compile()'
  22. cre = regex.compile(re)
  23. print cre.match('801 999')
  24.  
  25. try:
  26.     print cre.group('one')
  27. except regex.error:
  28.     print 'caught expected exception'
  29.  
  30. print 'expected regex.error not raised'
  31. print 'matching with group names and symcomp()'
  32. cre = regex.symcomp(re)
  33. print cre.match('801 999')
  34. print cre.group(0)
  35. print cre.group('one')
  36. print cre.group(1, 2)
  37. print cre.group('one', 'two')
  38. print 'realpat:', cre.realpat
  39. print 'groupindex:', cre.groupindex
  40. re = 'world'
  41. cre = regex.compile(re)
  42. print 'not case folded search:', cre.search('HELLO WORLD')
  43. cre = regex.compile(re, regex.casefold)
  44. print 'case folded search:', cre.search('HELLO WORLD')
  45. print '__members__:', cre.__members__
  46. print 'regs:', cre.regs
  47. print 'last:', cre.last
  48. print 'translate:', len(cre.translate)
  49. print 'givenpat:', cre.givenpat
  50. print 'match with pos:', cre.match('hello world', 7)
  51. print 'search with pos:', cre.search('hello world there world', 7)
  52. print 'bogus group:', cre.group(0, 1, 3)
  53.  
  54. try:
  55.     print 'no name:', cre.group('one')
  56. except regex.error:
  57.     print 'caught expected exception'
  58.  
  59. print 'expected regex.error not raised'
  60. *
  61. for t in tests:
  62.     if len(t) == 5:
  63.         (pattern, s, outcome, repl, expected) = t
  64.     elif len(t) == 3:
  65.         (pattern, s, outcome) = t
  66.     else:
  67.         raise ValueError, ('Test tuples should have 3 or 5 fields', t)
  68.     
  69.     try:
  70.         obj = regex.compile(pattern)
  71.     except regex.error:
  72.         tests
  73.         tests
  74.         None if verbose else import regex_tests
  75.         if outcome == SYNTAX_ERROR:
  76.             pass
  77.         
  78.     except:
  79.         outcome == SYNTAX_ERROR
  80.  
  81.     
  82.     try:
  83.         result = obj.search(s)
  84.     except regex.error:
  85.         tests
  86.         msg = tests
  87.         None if verbose else import regex_tests
  88.         print '=== Unexpected exception', t, repr(msg)
  89.     except:
  90.         tests
  91.  
  92.     if outcome == SYNTAX_ERROR:
  93.         pass
  94.     elif outcome == FAIL:
  95.         if result == -1:
  96.             pass
  97.         else:
  98.             print '=== Succeeded incorrectly', t
  99.     elif outcome == SUCCEED:
  100.         if result != -1:
  101.             (start, end) = obj.regs[0]
  102.             found = s[start:end]
  103.             groups = obj.group(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  104.             vardict = vars()
  105.             for i in range(len(groups)):
  106.                 vardict['g' + str(i + 1)] = str(groups[i])
  107.             
  108.             repl = eval(repl)
  109.             if repl != expected:
  110.                 print '=== grouping error', t, repr(repl) + ' should be ' + repr(expected)
  111.             
  112.         else:
  113.             print '=== Failed incorrectly', t
  114.     
  115.  
  116.