[pypy-svn] r5182 - pypy/branch/src-new-utest/pypy/tool
lac at codespeak.net
lac at codespeak.net
Sun Jun 20 15:46:26 CEST 2004
Author: lac
Date: Sun Jun 20 15:46:25 2004
New Revision: 5182
Modified:
pypy/branch/src-new-utest/pypy/tool/utestconvert.py
Log:
fixed a bug where I forgot that self.assertEquals and friends could
have messages too ...
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 15:46:25 2004
@@ -4,15 +4,16 @@
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
+# d['change'] is one of the following functions
# namechange_only e.g. assertRaises becomes raises
# fail_special e.g. fail() becomes raise AssertionError
# strip_parens e.g. assert_(expr) becomes assert expr
# comma_to_op e.g. assertEquals(l, r) becomes assert l == r
# rounding e.g. assertAlmostEqual(l, r) becomes
-# assert round(l - r, 7) == 0
+# assert round(l - r, 7) == 0
# Finally, 'op' is the operator you will substitute, if applicable.
# Got to define the dispatch functions first ....
@@ -20,12 +21,6 @@
'''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):
'''change fail function to raise AssertionError. dictionary dispatched. '''
indent, expr, trailer = common_setup(old, block)
@@ -34,10 +29,6 @@
return indent + new + trailer
else: # fail('Problem') --> raise AssertionError, 'Problem'
return indent + new + ', ' + expr + trailer
-
-d['fail'] = {'new': 'raise AssertionError',
- 'change type': fail_special,
- 'op': None}
def comma_to_op(old, new, block, op):
'''change comma to appropriate op. dictionary dispatched. '''
@@ -54,36 +45,32 @@
try:
parser.expr(right.lstrip()) # that paren came off easily
except SyntaxError:
- right = re.sub(linesep, '\\'+linesep, right)
+ # paste continuation backslashes on our multiline constructs
+ try:
+ # is the input expr, expr, string?
+ # so right is expr, string?
+
+ expr, string = get_expr(right, ',')
+ expr = re.sub(linesep, '\\'+linesep, expr)
+ # since the right1 is a string, assume it can take care
+ # of itself even if multiline.
+
+ if expr.startswith(linesep):# that needs a slash too ...
+ between = ',\\'
+ else:
+ between = ','
+ right = expr + between + string
+ except SyntaxError: # just a regular old multiline expression
+ right = re.sub(linesep, '\\'+linesep, right)
if right.startswith(linesep):
op = op + '\\'
return indent + new + left + op + right + trailer
-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': '=='}
-
def strip_parens(old, new, block, op):
'''remove one set of parens. dictionary dispatched. '''
indent, expr, trailer = common_setup(old, block)
new = new + ' '
-
try:
parser.expr(expr) # the parens came off easily
return indent + new + expr + trailer
@@ -105,62 +92,60 @@
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}
+def rounding():
+ pass
+
+# Now the dictionary of unittests. There sure are enough of them!
+
+d['assertRaises'] = {'new': 'raises', 'change': namechange_only, 'op': None}
+d['failUnlessRaises'] = d['assertRaises']
+
+d['fail'] = {'new': 'raise AssertionError', 'change': fail_special, 'op': None}
+
+d['assertEqual'] = {'new': 'assert', 'change': comma_to_op, 'op': '=='}
+d['assertEquals'] = d['assertEqual']
+
+d['assertNotEqual'] = {'new': 'assert', 'change':comma_to_op, 'op': '!='}
+d['assertNotEquals'] = d['assertNotEqual']
+
+d['failUnlessEqual'] = {'new': 'assert not', 'change': comma_to_op, 'op': '!='}
+
+d['failIfEqual'] = {'new': 'assert not', 'change': comma_to_op, 'op': '=='}
+
+d['assert_'] = {'new': 'assert','change': strip_parens, 'op': None}
d['failUnless'] = d['assert_']
-d['failIf'] = {'new': 'assert not',
- 'change type': strip_parens,
- 'op': None}
-"""
-
-d['assertNotAlmostEqual'] = {'old': 'assertNotAlmostEqual',
- 'new': 'assert round',
- 'change type': 'rounding',
- 'op': '!='}
-
-d['assertNotAlmostEquals'] = {'old': 'assertNotAlmostEquals',
- 'new': 'assert round',
- 'change type': 'rounding',
- 'op': '!='}
-
-d['failUnlessAlmostEqual'] = {'old': 'failUnlessAlmostEqual',
- 'new': 'assert not round',
- 'change type': 'rounding',
- 'op': '=='}
-
-d['assertUnlessAlmostEquals'] = {'old': 'assertUnlessAlmostEquals',
- 'new': 'assert round',
- 'change type': 'rounding',
- 'op': '=='}
-
-d['assertAlmostEqual'] = {'old': 'assertAlmostEqual',
- 'new': 'assert round',
- 'change type': 'rounding',
- 'op': '=='}
-"""
-leading_spaces = re.compile(r'^(\s*)')
+d['failIf'] = {'new': 'assert not', 'change': strip_parens, 'op': None}
+
+d['assertAlmostEqual'] = {'new': 'assert round', 'change': rounding, 'op':'=='}
+d['assertAlmostEquals'] = d['assertAlmostEqual']
+
+d['assertNotAlmostEqual'] = {'new':'assert round','change':rounding, 'op':'!='}
+d['assertNotAlmostEquals'] = d['assertNotAlmostEqual']
+
+d['failIfAlmostEqual'] = {'new': 'assert not round',
+ 'change': rounding, 'op': '=='}
+d['failUnlessAlmostEquals'] = {'new': 'assert not round',
+ 'change': rounding, 'op': '!='}
+
+leading_spaces = re.compile(r'^(\s*)') # this never fails
pat = ''
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
+old_names = re.compile(pat[1:])
linesep=os.linesep
def blocksplitter(filename):
-
+ '''split a file into blocks that are headed by functions to rename'''
fp = file(filename, 'r')
blocklist = []
blockstring = ''
for line in fp:
-
interesting = old_names.match(line)
-
if interesting :
if blockstring:
blocklist.append(blockstring)
@@ -171,13 +156,13 @@
blocklist.append(blockstring)
return blocklist
-def process_block(s):
+def dispatch(s):
+ '''do a dictionary dispatch based on the change key in the dict d '''
f = old_names.match(s)
if f:
key = f.group(0).lstrip()[5:-1] # '\tself.blah(' -> 'blah'
- # now do the dictionary dispatch.
- return d[key]['change type'](key, d[key]['new'], s, d[key] ['op'])
- else:
+ return d[key]['change'](key, d[key]['new'], s, d[key] ['op'])
+ else: # just copy uninteresting lines
return s
def common_setup(old, block):
@@ -189,11 +174,12 @@
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)
+ '''split a string into an expression, and the rest of the string'''
+ pos=[]
+ for i in range(len(s)):
+ if s[i] == char:
+ pos.append(i)
if pos == []:
raise SyntaxError # we didn't find the expected char. Ick.
@@ -207,27 +193,18 @@
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"),
+ self.assertEquals(dispatch("badger badger badger"),
"badger badger badger")
- self.assertEquals(process_block(
+ self.assertEquals(dispatch(
"self.assertRaises(excClass, callableObj, *args, **kwargs)"
),
"raises(excClass, callableObj, *args, **kwargs)"
)
- self.assertEquals(process_block(
+ self.assertEquals(dispatch(
"""
self.failUnlessRaises(TypeError, func, 42, **{'arg1': 23})
"""
@@ -236,7 +213,7 @@
raises(TypeError, func, 42, **{'arg1': 23})
"""
)
- self.assertEquals(process_block(
+ self.assertEquals(dispatch(
"""
self.assertRaises(TypeError,
func,
@@ -249,14 +226,14 @@
mushroom)
"""
)
- self.assertEquals(process_block("self.fail()"), "raise AssertionError")
- self.assertEquals(process_block("self.fail('mushroom, mushroom')"),
+ self.assertEquals(dispatch("self.fail()"), "raise AssertionError")
+ self.assertEquals(dispatch("self.fail('mushroom, mushroom')"),
"raise AssertionError, 'mushroom, mushroom'")
- self.assertEquals(process_block("self.assert_(x)"), "assert x")
- self.assertEquals(process_block("self.failUnless(func(x)) # XXX"),
+ self.assertEquals(dispatch("self.assert_(x)"), "assert x")
+ self.assertEquals(dispatch("self.failUnless(func(x)) # XXX"),
"assert func(x) # XXX")
- self.assertEquals(process_block(
+ self.assertEquals(dispatch(
"""
self.assert_(1 + f(y)
+ z) # multiline, add continuation backslash
@@ -268,10 +245,10 @@
"""
)
- self.assertEquals(process_block("self.assert_(0, 'badger badger')"),
+ self.assertEquals(dispatch("self.assert_(0, 'badger badger')"),
"assert 0, 'badger badger'")
- self.assertEquals(process_block(
+ self.assertEquals(dispatch(
r"""
self.assert_(0,
'Meet the badger.\n')
@@ -283,7 +260,7 @@
"""
)
- self.assertEquals(process_block(
+ self.assertEquals(dispatch(
r"""
self.failIf(0 + 0
+ len('badger\n')
@@ -303,10 +280,10 @@
"""
)
- self.assertEquals(process_block("self.assertEquals(0, 0)"),
+ self.assertEquals(dispatch("self.assertEquals(0, 0)"),
"assert 0 == 0")
- self.assertEquals(process_block(
+ self.assertEquals(dispatch(
r"""
self.assertEquals(0,
'Run away from the snake.\n')
@@ -318,7 +295,7 @@
"""
)
- self.assertEquals(process_block(
+ self.assertEquals(dispatch(
r"""
self.assertEquals(badger + 0
+ mushroom
@@ -332,7 +309,7 @@
"""
)
- self.assertEquals(process_block(
+ self.assertEquals(dispatch(
r"""
self.assertNotEquals(badger + 0
+ mushroom
@@ -350,7 +327,7 @@
"""
)
- self.assertEqual(process_block(
+ self.assertEqual(dispatch(
r"""
self.assertEquals(badger(),
mushroom()
@@ -365,13 +342,13 @@
- badger()
"""
)
- self.assertEquals(process_block("self.failIfEqual(0, 0)"),
+ self.assertEquals(dispatch("self.failIfEqual(0, 0)"),
"assert not 0 == 0")
- self.assertEquals(process_block("self.failUnlessEqual(0, 0)"),
+ self.assertEquals(dispatch("self.failUnlessEqual(0, 0)"),
"assert not 0 != 0")
- self.assertEquals(process_block(
+ self.assertEquals(dispatch(
r"""
self.failUnlessEqual(mushroom()
+ mushroom()
@@ -391,7 +368,52 @@
"""
)
+ self.assertEquals(dispatch(
+ r"""
+ self.assertEquals(badger(),
+ snake(), 'BAD BADGER')
+ """
+ ),
+ r"""
+ assert badger() ==\
+ snake(), 'BAD BADGER'
+ """
+ )
+ self.assertEquals(dispatch(
+ r"""
+ self.assertEquals(badger(),
+ snake(), '''BAD BADGER
+ BAD BADGER
+ BAD BADGER'''
+ )
+ """
+ ),
+ r"""
+ assert badger() ==\
+ snake(), '''BAD BADGER
+ BAD BADGER
+ BAD BADGER'''
+
+ """
+ )
+ self.assertEquals(dispatch(
+ r"""
+ self.assertNotEquals(badger(),
+ snake()+
+ snake(), 'POISONOUS MUSHROOM!\
+ Ai! I ate a POISONOUS MUSHROOM!!')
+ """
+ ),
+ r"""
+ assert badger() !=\
+ snake()+\
+ snake(), 'POISONOUS MUSHROOM!\
+ Ai! I ate a POISONOUS MUSHROOM!!'
+ """
+ )
+
if __name__ == '__main__':
unittest.main()
- #for block in blocksplitter('xxx.py'): print process_block(block)
+ #for block in blocksplitter('xxx.py'): print dispatch(block)
+
More information about the Pypy-commit
mailing list