[pypy-svn] r5181 - pypy/branch/src-new-utest/pypy/tool

lac at codespeak.net lac at codespeak.net
Sun Jun 20 13:18:37 CEST 2004


Author: lac
Date: Sun Jun 20 13:18:36 2004
New Revision: 5181

Modified:
   pypy/branch/src-new-utest/pypy/tool/utestconvert.py
Log:
Reorder the function some,  to aid in comprehension.  Change it to handle
other os's line separation character.  Still untested on anything but 
linux.


Modified: pypy/branch/src-new-utest/pypy/tool/utestconvert.py
==============================================================================
--- pypy/branch/src-new-utest/pypy/tool/utestconvert.py	(original)
+++ pypy/branch/src-new-utest/pypy/tool/utestconvert.py	Sun Jun 20 13:18:36 2004
@@ -1,8 +1,9 @@
 import re
 import unittest
 import parser
+import os
 
-
+d={}
 #  d is the dictionary of unittest changes, keyed to the old name
 #  used by unittest.  d['new'] is the new replacement function, and
 #  d['change type'] is one of the following functions
@@ -13,146 +14,98 @@
 #           rounding          e.g.  assertAlmostEqual(l, r) becomes
 #                                     assert round(l - r, 7) == 0
 #  Finally, 'op' is the operator you will substitute, if applicable.
-
-# First define the functions you want to dispatch
+#  Got to define the dispatch functions first ....
 
 def namechange_only(old, new, block, op):
-    # dictionary dispatch function.
-    # this is the simplest of changes.
+    '''rename a function.  dictionary dispatched.'''
     return re.sub('self.'+old, new, block)
 
+d['assertRaises'] = {'new': 'raises',
+                     'change type': namechange_only,
+                     'op': None}
+
+d['failUnlessRaises'] = d['assertRaises']
+
 def fail_special(old, new, block, op):
-    # dictionary dispatch function.
+    '''change fail function to raise AssertionError. dictionary dispatched. '''
     indent, expr, trailer = common_setup(old, block)
     
     if expr == '':  # fail()  --> raise AssertionError
          return indent + new + trailer
     else:   # fail('Problem')  --> raise AssertionError, 'Problem'
          return indent + new + ', ' + expr + trailer
-
-def strip_parens(old, new, block, op):
-    # dictionary dispatch function.
-    indent, expr, trailer = common_setup(old, block)
-    new = new + ' '
-
-    try:
-        parser.expr(expr) # the parens came off easily
-        return indent + new + expr + trailer
-
-    except SyntaxError:
-        # paste continuation backslashes on our multiline constructs.
-        try:
-            # is the input expr, string?
-            left, right = get_expr(expr, ',')
-            # ok, paste continuation backslashes on our left,
-            # but not on our right hand side, since a multiline
-            # string is not using the parens to avoid SyntaxError,
-            # and must already have a working mechanism, existing
-            # backslashes, or triple quotes ....
-
-            left = re.sub(r'\n', r'\\\n', left)
-            
-            if right[0] == '\n':  # that needs a slash too ...
-                                  # do we handle non-unix correctly?
-                between = ',\\'
-            else:
-                between = ','
-            return indent + new + left + between + right + trailer
-
-        except SyntaxError:
-            # we couldn't find a 'expr, string' so it is
-            # probably just a regular old multiline expression
-            # e.g   self.assertx(0
-            #                    +f(x)
-            #                    +g(x))
-
-            expr = re.sub(r'\n', r'\\\n', expr)
-
-            return indent + new + expr + trailer
+     
+d['fail'] = {'new': 'raise AssertionError',
+             'change type': fail_special,
+             'op': None}
 
 def comma_to_op(old, new, block, op):
-    # dictionary dispatch function.  parser.expr does all the work.
-
+    '''change comma to appropriate op. dictionary dispatched. '''
     indent, expr, trailer = common_setup(old, block)
     new = new + ' '
     op = ' ' + op
     left, right = get_expr(expr, ',')
 
-    print 'left is <%s>, right is <%s>' % (left, right)
     try:
         parser.expr(left)  # that paren came off easily
     except SyntaxError:
-        left  = re.sub(r'\n', r'\\\n', left)
+        left  = re.sub(linesep, '\\'+linesep, left)
         
     try:
         parser.expr(right.lstrip())  # that paren came off easily
-
     except SyntaxError:
-        right = re.sub(r'\n', r'\\\n', right)
+        right = re.sub(linesep, '\\'+linesep, right)
 
-    if right[0] == '\n':
+    if right.startswith(linesep):
         op = op + '\\'
     return indent + new + left + op + right + trailer
 
-def right_finder(s):
-    
-    for i in range(len(s)):
-        if s[i] != ' ' and s[i] != '\t':
-            print i
-            break
-
-    if s[i] == '\n':
-        return True
-    else:
-        return False
-
-def common_setup(old, block):
-    """split the block into component parts"""
-    indent = re.search(r'^(\s*)', block).group()
-    pat = re.search('self.' + old + r'\(', block)
-    expr, trailer = get_expr(block[pat.end():], ')')
-    return indent, expr, trailer
-
-def get_expr(s, char):
-    # the trick.  how to get an expression without really trying :-)
-    # read from the beginning of the string until you get an expression.
-    # return it, and the stuff left over, minus the char you separated on
-    pos = pos_finder(s, char)
+d['assertEqual'] = {'new': 'assert',
+                    'change type': comma_to_op,
+                    'op': '=='}
 
-    if pos == []:
-        raise SyntaxError # we didn't find the expected char.  Ick.
-     
-    for p in pos:
-        # make the python parser do the hard work of deciding which comma
-        # splits the string into two expressions
-        try:
-            parser.expr('(' + s[:p] + ')')
-            return s[:p], s[p+1:]
-        except SyntaxError: # It's not an expression yet
-            pass
-    raise SyntaxError       # We never found anything that worked.
+d['assertEquals'] = d['assertEqual']
 
-def pos_finder(s, char=','):
-    # used by find_expr
-    # returns the list of string positions where the char 'char' was found
-    pos=[]
-    for i in range(len(s)):
-        if s[i] == char:
-            pos.append(i)
-    return pos
+d['assertNotEqual'] = {'new': 'assert',
+                       'change type':comma_to_op,
+                       'op': '!='}
 
-d={}
+d['assertNotEquals'] = d['assertNotEqual']
 
-d['assertRaises'] = {'new': 'raises',
-                     'change type': namechange_only,
-                     'op': None}
+d['failUnlessEqual'] = {'new': 'assert not',
+                        'change type': comma_to_op,
+                        'op': '!='}
+d['failIfEqual'] = {'new': 'assert not',
+                    'change type': comma_to_op,
+                    'op': '=='}
 
-d['failUnlessRaises'] = d['assertRaises']
+def strip_parens(old, new, block, op):
+    '''remove one set of parens. dictionary dispatched. '''
+    indent, expr, trailer = common_setup(old, block)
+    new = new + ' '
 
-d['fail'] = {'new': 'raise AssertionError',
-             'change type': fail_special,
-             'op': None}
+    try:
+        parser.expr(expr) # the parens came off easily
+        return indent + new + expr + trailer
+    except SyntaxError:
+        # paste continuation backslashes on our multiline constructs.
+        try:
+            # is the input expr, string?
+            left, right = get_expr(expr, ',')
+            left = re.sub(linesep, '\\'+linesep, left)
+            # since the right is a string, assume it can take care
+            # of itself even if multiline.
+            
+            if right.startswith(linesep):# that needs a slash too ...
+                between = ',\\'
+            else:
+                between = ','
+            return indent + new + left + between + right + trailer
 
+        except SyntaxError: # just a regular old multiline expression
+            expr = re.sub(linesep, '\\'+linesep, expr)
+            return indent + new + expr + trailer
+        
 d['assert_'] = {'new': 'assert',
                 'change type': strip_parens,
                 'op': None}
@@ -162,27 +115,6 @@
 d['failIf'] = {'new': 'assert not',
                'change type': strip_parens,
                'op': None}
-
-d['assertEqual'] = {'new': 'assert',
-                     'change type': comma_to_op,
-                     'op': '=='}
-
-d['assertEquals'] = d['assertEqual']
-
-
-d['assertNotEqual'] = {'new': 'assert',
-                        'change type':comma_to_op,
-                        'op': '!='}
-
-d['assertNotEquals'] = d['assertNotEqual']
-
-d['failUnlessEqual'] = {'new': 'assert not',
-                        'change type': comma_to_op,
-                        'op': '!='}
-d['failIfEqual'] = {'new': 'assert not',
-                    'change type': comma_to_op,
-                    'op': '=='}
-
 """
 
 d['assertNotAlmostEqual'] = {'old': 'assertNotAlmostEqual',
@@ -213,10 +145,11 @@
 leading_spaces = re.compile(r'^(\s*)')
 
 pat = ''
-for k in d.keys():
+for k in d.keys():  # this complicated pattern to match all unittests
     pat += '|' + r'^(\s*)' + 'self.' + k + r'\(' # \tself.whatever(
 
 old_names = re.compile(pat[1:])  # strip the extra '|' from front
+linesep=os.linesep
 
 def blocksplitter(filename):
 
@@ -247,6 +180,42 @@
     else:
         return s
 
+def common_setup(old, block):
+    '''split the block into component parts'''
+
+    indent = re.search(r'^(\s*)', block).group()
+    pat = re.search('self.' + old + r'\(', block)
+    expr, trailer = get_expr(block[pat.end():], ')')
+    return indent, expr, trailer
+
+def get_expr(s, char):
+    # the trick.  how to get an expression without really trying :-)
+    # read from the beginning of the string until you get an expression.
+    # return it, and the stuff left over, minus the char you separated on
+    pos = pos_finder(s, char)
+
+    if pos == []:
+        raise SyntaxError # we didn't find the expected char.  Ick.
+     
+    for p in pos:
+        # make the python parser do the hard work of deciding which comma
+        # splits the string into two expressions
+        try:
+            parser.expr('(' + s[:p] + ')')
+            return s[:p], s[p+1:]
+        except SyntaxError: # It's not an expression yet
+            pass
+    raise SyntaxError       # We never found anything that worked.
+
+def pos_finder(s, char=','):
+    # used by find_expr
+    # returns the list of string positions where the char 'char' was found
+    pos=[]
+    for i in range(len(s)):
+        if s[i] == char:
+            pos.append(i)
+    return pos
+
 class Testit(unittest.TestCase):
     def test(self):
         self.assertEquals(process_block("badger badger badger"),



More information about the Pypy-commit mailing list