[Python-checkins] r54007 - in sandbox/trunk/2to3: README fixer_tests.py test.py tests tests.py tests/__init__.py tests/support.py tests/test_fixers.py tests/test_pytree.py
collin.winter
python-checkins at python.org
Wed Feb 28 00:45:52 CET 2007
Author: collin.winter
Date: Wed Feb 28 00:45:47 2007
New Revision: 54007
Added:
sandbox/trunk/2to3/test.py (contents, props changed)
sandbox/trunk/2to3/tests/ (props changed)
sandbox/trunk/2to3/tests/__init__.py (contents, props changed)
sandbox/trunk/2to3/tests/support.py (contents, props changed)
sandbox/trunk/2to3/tests/test_fixers.py
- copied, changed from r53996, sandbox/trunk/2to3/fixer_tests.py
sandbox/trunk/2to3/tests/test_pytree.py
- copied, changed from r53996, sandbox/trunk/2to3/tests.py
Removed:
sandbox/trunk/2to3/fixer_tests.py
sandbox/trunk/2to3/tests.py
Modified:
sandbox/trunk/2to3/README
Log:
Move tests.py and fixer_tests.py into tests/, add testing infrastructure.
Modified: sandbox/trunk/2to3/README
==============================================================================
--- sandbox/trunk/2to3/README (original)
+++ sandbox/trunk/2to3/README Wed Feb 28 00:45:47 2007
@@ -5,8 +5,8 @@
Files:
README - this file
+test.py - runs all unittests for 2to3
play.py - program to exercise the idempotency of pytree nodes
-tests.py - unit tests for pytree.py
patcomp.py - pattern compiler
pytree.py - parse tree nodes (not specific to Python, despite the name!)
pygram.py - code specific to the Python grammar
@@ -17,6 +17,7 @@
PatternGrammar.pickle - pickled pattern grammar tables (generated file)
pgen2/ - Parser generator and driver (1) (2)
fixes/ - Individual transformations
+tests/ - Test files for pytree, fixers, grammar, etc
Notes:
Deleted: /sandbox/trunk/2to3/fixer_tests.py
==============================================================================
--- /sandbox/trunk/2to3/fixer_tests.py Wed Feb 28 00:45:47 2007
+++ (empty file)
@@ -1,1126 +0,0 @@
-#!/usr/bin/env python2.5
-""" Test suite for the fixer modules """
-# Author: Collin Winter
-
-# Python imports
-from StringIO import StringIO
-import re
-import unittest
-import logging
-
-# Local imports
-import pytree
-import refactor
-
-skip_whitespace = re.compile(r"""\S""")
-
-def reformat(string):
- indent = re.search(skip_whitespace, string).start()
- if indent == 0:
- code = string
- else:
- code = "\n".join(line[indent-1:] for line in string.split("\n")[1:])
- return code + "\n\n"
-
-# We wrap the RefactoringTool's fixer objects so we can intercept
-# the call to set_filename() and so modify the fixers' logging objects.
-# This allows us to make sure that certain code chunks produce certain
-# warnings.
-class Fixer(object):
- def __init__(self, fixer, handler):
- self.fixer = fixer
- self.handler = handler
-
- def __getattr__(self, attr):
- return getattr(self.fixer, attr)
-
- def set_filename(self, filename):
- self.fixer.set_filename(filename)
- self.fixer.logger.addHandler(self.handler)
-
-class Options:
- def __init__(self, **kwargs):
- for k, v in kwargs.items():
- setattr(self, k, v)
-
- self.verbose = False
-
-class FixerTestCase(unittest.TestCase):
- def setUp(self):
- options = Options(fix=[self.fixer], print_function=False)
- self.refactor = refactor.RefactoringTool(options)
-
- self.logging_stream = StringIO()
- sh = logging.StreamHandler(self.logging_stream)
- sh.setFormatter(logging.Formatter("%(message)s"))
- self.refactor.fixers = [Fixer(f, sh) for f in self.refactor.fixers]
-
- def check(self, before, after):
- before = reformat(before)
- after = reformat(after)
- refactored = self.refactor_stream("<string>", StringIO(before))
- self.failUnlessEqual(after, refactored)
-
- def warns(self, before, after, message):
- self.check(before, after)
-
- self.failUnless(message in self.logging_stream.getvalue())
-
- def refactor_stream(self, stream_name, stream):
- try:
- tree = self.refactor.driver.parse_stream(stream)
- except Exception, err:
- raise
- self.log_error("Can't parse %s: %s: %s",
- filename, err.__class__.__name__, err)
- return
- self.refactor.refactor_tree(tree, stream_name)
- return str(tree)
-
-
-class Test_ne(FixerTestCase):
- fixer = "ne"
-
- def test_1(self):
- b = """if x <> y:
- pass"""
-
- a = """if x != y:
- pass"""
- self.check(b, a)
-
- def test_2(self):
- b = """if x<>y:
- pass"""
-
- a = """if x!=y:
- pass"""
- self.check(b, a)
-
- def test_3(self):
- b = """if x<>y<>z:
- pass"""
-
- a = """if x!=y!=z:
- pass"""
- self.check(b, a)
-
-class Test_has_key(FixerTestCase):
- fixer = "has_key"
-
- def test_1(self):
- b = """x = d.has_key("x") or d.has_key("y")"""
- a = """x = "x" in d or "y" in d"""
- self.check(b, a)
-
- def test_2(self):
- b = """x = a.b.c.d.has_key("x") ** 3"""
- a = """x = ("x" in a.b.c.d) ** 3"""
- self.check(b, a)
-
- def test_3(self):
- b = """x = a.b.has_key(1 + 2).__repr__()"""
- a = """x = (1 + 2 in a.b).__repr__()"""
- self.check(b, a)
-
- def test_4(self):
- b = """x = a.b.has_key(1 + 2).__repr__() ** -3 ** 4"""
- a = """x = (1 + 2 in a.b).__repr__() ** -3 ** 4"""
- self.check(b, a)
-
- def test_5(self):
- b = """x = a.has_key(f or g)"""
- a = """x = (f or g) in a"""
- self.check(b, a)
-
- def test_6(self):
- b = """x = a + b.has_key(c)"""
- a = """x = a + (c in b)"""
- self.check(b, a)
-
- def test_7(self):
- b = """x = a.has_key(lambda: 12)"""
- a = """x = (lambda: 12) in a"""
- self.check(b, a)
-
- def test_8(self):
- b = """x = a.has_key(a for a in b)"""
- a = """x = (a for a in b) in a"""
- self.check(b, a)
-
- def test_9(self):
- b = """if not a.has_key(b): pass"""
- a = """if b not in a: pass"""
- self.check(b, a)
-
- def test_10(self):
- b = """if not a.has_key(b).__repr__(): pass"""
- a = """if not (b in a).__repr__(): pass"""
- self.check(b, a)
-
- def test_11(self):
- b = """if not a.has_key(b) ** 2: pass"""
- a = """if not (b in a) ** 2: pass"""
- self.check(b, a)
-
-class Test_apply(FixerTestCase):
- fixer = "apply"
-
- def test_1(self):
- b = """x = apply(f, g + h)"""
- a = """x = f(*g + h)"""
- self.check(b, a)
-
- def test_2(self):
- b = """y = apply(f, g, h)"""
- a = """y = f(*g, **h)"""
- self.check(b, a)
-
- def test_3(self):
- b = """z = apply(fs[0], g or h, h or g)"""
- a = """z = fs[0](*g or h, **h or g)"""
- self.check(b, a)
-
- def test_4(self):
- b = """apply(f, (x, y) + t)"""
- a = """f(*(x, y) + t)"""
- self.check(b, a)
-
- def test_5(self):
- b = """apply(f, args,)"""
- a = """f(*args)"""
- self.check(b, a)
-
- def test_6(self):
- b = """apply(f, args, kwds,)"""
- a = """f(*args, **kwds)"""
- self.check(b, a)
-
- # Test that complex functions are parenthesized
-
- def test_7(self):
- b = """x = apply(f+g, args)"""
- a = """x = (f+g)(*args)"""
- self.check(b, a)
-
- def test_8(self):
- b = """x = apply(f*g, args)"""
- a = """x = (f*g)(*args)"""
- self.check(b, a)
-
- def test_9(self):
- b = """x = apply(f**g, args)"""
- a = """x = (f**g)(*args)"""
- self.check(b, a)
-
- # But dotted names etc. not
-
- def test_10(self):
- b = """x = apply(f.g, args)"""
- a = """x = f.g(*args)"""
- self.check(b, a)
-
- def test_11(self):
- b = """x = apply(f[x], args)"""
- a = """x = f[x](*args)"""
- self.check(b, a)
-
- def test_12(self):
- b = """x = apply(f(), args)"""
- a = """x = f()(*args)"""
- self.check(b, a)
-
- # Extreme case
- def test_13(self):
- b = """x = apply(a.b.c.d.e.f, args, kwds)"""
- a = """x = a.b.c.d.e.f(*args, **kwds)"""
- self.check(b, a)
-
- # XXX Comments in weird places still get lost
- def test_14(self):
- b = """apply( # foo
- f, # bar
- args)"""
- a = """f(*args)"""
- self.check(b, a)
-
- # These should *not* be touched
-
- def test_15(self):
- b = """apply()"""
- a = """apply()"""
- self.check(b, a)
-
- def test_16(self):
- b = """apply(f)"""
- a = """apply(f)"""
- self.check(b, a)
-
- def test_17(self):
- b = """apply(f,)"""
- a = """apply(f,)"""
- self.check(b, a)
-
- def test_18(self):
- b = """apply(f, args, kwds, extras)"""
- a = """apply(f, args, kwds, extras)"""
- self.check(b, a)
-
- def test_19(self):
- b = """apply(f, *args, **kwds)"""
- a = """apply(f, *args, **kwds)"""
- self.check(b, a)
-
- def test_20(self):
- b = """apply(f, *args)"""
- a = """apply(f, *args)"""
- self.check(b, a)
-
- def test_21(self):
- b = """apply(func=f, args=args, kwds=kwds)"""
- a = """apply(func=f, args=args, kwds=kwds)"""
- self.check(b, a)
-
- def test_22(self):
- b = """apply(f, args=args, kwds=kwds)"""
- a = """apply(f, args=args, kwds=kwds)"""
- self.check(b, a)
-
- def test_23(self):
- b = """apply(f, args, kwds=kwds)"""
- a = """apply(f, args, kwds=kwds)"""
- self.check(b, a)
-
-
-class Test_intern(FixerTestCase):
- fixer = "intern"
-
- def test_1(self):
- b = """x = intern(a)"""
- a = """x = sys.intern(a)"""
- self.check(b, a)
-
- def test_2(self):
- b = """y = intern("b" # test
- )"""
- a = """y = sys.intern("b" # test
- )"""
- self.check(b, a)
-
- def test_3(self):
- b = """z = intern(a+b+c.d,)"""
- a = """z = sys.intern(a+b+c.d,)"""
- self.check(b, a)
-
- def test_4(self):
- b = """intern("y%s" % 5).replace("y", "")"""
- a = """sys.intern("y%s" % 5).replace("y", "")"""
- self.check(b, a)
-
- # These should not be refactored
-
- def test_5(self):
- b = """intern(a=1)"""
- a = """intern(a=1)"""
- self.check(b, a)
-
- def test_6(self):
- b = """intern(f, g)"""
- a = """intern(f, g)"""
- self.check(b, a)
-
- def test_7(self):
- b = """intern(*h)"""
- a = """intern(*h)"""
- self.check(b, a)
-
- def test_8(self):
- b = """intern(**i)"""
- a = """intern(**i)"""
- self.check(b, a)
-
-class Test_print(FixerTestCase):
- fixer = "print"
-
- def test_1(self):
- b = """print 1, 1+1, 1+1+1"""
- a = """print(1, 1+1, 1+1+1)"""
- self.check(b, a)
-
- def test_2(self):
- b = """print 1, 2"""
- a = """print(1, 2)"""
- self.check(b, a)
-
- def test_3(self):
- b = """print"""
- a = """print()"""
- self.check(b, a)
-
- # trailing commas
-
- def test_4(self):
- b = """print 1, 2, 3,"""
- a = """print(1, 2, 3, end=' ')"""
- self.check(b, a)
-
- def test_5(self):
- b = """print 1, 2,"""
- a = """print(1, 2, end=' ')"""
- self.check(b, a)
-
- def test_6(self):
- b = """print 1,"""
- a = """print(1, end=' ')"""
- self.check(b, a)
-
- # >> stuff
-
- # no trailing comma
- def test_7(self):
- b = """print >>sys.stderr, 1, 2, 3"""
- a = """print(1, 2, 3, file=sys.stderr)"""
- self.check(b, a)
-
- # trailing comma
- def test_8(self):
- b = """print >>sys.stderr, 1, 2,"""
- a = """print(1, 2, end=' ', file=sys.stderr)"""
- self.check(b, a)
-
- # no trailing comma
- def test_9(self):
- b = """print >>sys.stderr, 1+1"""
- a = """print(1+1, file=sys.stderr)"""
- self.check(b, a)
-
- # spaces before sys.stderr
- def test_10(self):
- b = """print >> sys.stderr"""
- a = """print(file=sys.stderr)"""
- self.check(b, a)
-
-
-class Test_exec(FixerTestCase):
- fixer = "exec"
-
- def test_1(self):
- b = """exec code"""
- a = """exec(code)"""
- self.check(b, a)
-
- def test_2(self):
- b = """exec code in ns"""
- a = """exec(code, ns)"""
- self.check(b, a)
-
- def test_3(self):
- b = """exec code in ns1, ns2"""
- a = """exec(code, ns1, ns2)"""
- self.check(b, a)
-
- def test_4(self):
- b = """exec (a.b()) in ns"""
- a = """exec((a.b()), ns)"""
- self.check(b, a)
-
- def test_5(self):
- b = """exec a.b() + c in ns"""
- a = """exec(a.b() + c, ns)"""
- self.check(b, a)
-
- # These should not be touched
-
- def test_6(self):
- b = """exec(code)"""
- a = """exec(code)"""
- self.check(b, a)
-
- def test_7(self):
- b = """exec (code)"""
- a = """exec (code)"""
- self.check(b, a)
-
- def test_8(self):
- b = """exec(code, ns)"""
- a = """exec(code, ns)"""
- self.check(b, a)
-
- def test_9(self):
- b = """exec(code, ns1, ns2)"""
- a = """exec(code, ns1, ns2)"""
- self.check(b, a)
-
-
-class Test_repr(FixerTestCase):
- fixer = "repr"
-
- def test_1(self):
- b = """x = `1 + 2`"""
- a = """x = repr(1 + 2)"""
- self.check(b, a)
-
- def test_2(self):
- b = """y = `x`"""
- a = """y = repr(x)"""
- self.check(b, a)
-
- def test_3(self):
- b = """z = `y`.__repr__()"""
- a = """z = repr(y).__repr__()"""
- self.check(b, a)
-
- def test_4(self):
- b = """x = `1, 2, 3`"""
- a = """x = repr((1, 2, 3))"""
- self.check(b, a)
-
- def test_5(self):
- b = """x = `1 + `2``"""
- a = """x = repr(1 + repr(2))"""
- self.check(b, a)
-
- def test_6(self):
- b = """x = `1, 2 + `3, 4``"""
- a = """x = repr((1, 2 + repr((3, 4))))"""
- self.check(b, a)
-
-class Test_except(FixerTestCase):
- fixer = "except"
-
- def test_1(self):
- b = """
- def foo():
- try:
- pass
- except Exception, (f, e):
- pass
- except ImportError, e:
- pass"""
-
- a = """
- def foo():
- try:
- pass
- except Exception as xxx_todo_changeme:
- (f, e) = xxx_todo_changeme.message
- pass
- except ImportError as e:
- pass"""
- self.check(b, a)
-
- def test_2(self):
- b = """
- try:
- pass
- except (RuntimeError, ImportError), e:
- pass"""
-
- a = """
- try:
- pass
- except (RuntimeError, ImportError) as e:
- pass"""
- self.check(b, a)
-
- def test_3(self):
- b = """
- try:
- pass
- except Exception, (a, b):
- pass"""
-
- a = """
- try:
- pass
- except Exception as xxx_todo_changeme1:
- (a, b) = xxx_todo_changeme1.message
- pass"""
- self.check(b, a)
-
- def test_4(self):
- b = """
- try:
- pass
- except Exception, d[5]:
- pass"""
-
- a = """
- try:
- pass
- except Exception as xxx_todo_changeme2:
- d[5] = xxx_todo_changeme2
- pass"""
- self.check(b, a)
-
- def test_5(self):
- b = """
- try:
- pass
- except Exception, a.foo:
- pass"""
-
- a = """
- try:
- pass
- except Exception as xxx_todo_changeme3:
- a.foo = xxx_todo_changeme3
- pass"""
- self.check(b, a)
-
- def test_6(self):
- b = """
- try:
- pass
- except Exception, a().foo:
- pass"""
-
- a = """
- try:
- pass
- except Exception as xxx_todo_changeme4:
- a().foo = xxx_todo_changeme4
- pass"""
- self.check(b, a)
-
- # These should not be touched:
-
- def test_7(self):
- b = """
- try:
- pass
- except:
- pass"""
-
- a = """
- try:
- pass
- except:
- pass"""
- self.check(b, a)
-
- def test_8(self):
- b = """
- try:
- pass
- except Exception:
- pass"""
-
- a = """
- try:
- pass
- except Exception:
- pass"""
- self.check(b, a)
-
- def test_9(self):
- b = """
- try:
- pass
- except (Exception, SystemExit):
- pass"""
-
- a = """
- try:
- pass
- except (Exception, SystemExit):
- pass"""
- self.check(b, a)
-
-
-class Test_raise(FixerTestCase):
- fixer = "raise"
-
- def test_1(self):
- b = """raise Exception, 5"""
- a = """raise Exception(5)"""
- self.check(b, a)
-
- def test_2(self):
- b = """raise Exception,5"""
- a = """raise Exception(5)"""
- self.check(b, a)
-
- def test_3(self):
- b = """raise Exception, (5, 6, 7)"""
- a = """raise Exception(5, 6, 7)"""
- self.check(b, a)
-
- def test_4(self):
- b = """raise E, (5, 6) % (a, b)"""
- a = """raise E((5, 6) % (a, b))"""
- self.check(b, a)
-
- def test_5(self):
- b = """raise (((E1, E2), E3), E4), V"""
- a = """raise E1(V)"""
- self.check(b, a)
-
- def test_6(self):
- b = """raise (E1, (E2, E3), E4), V"""
- a = """raise E1(V)"""
- self.check(b, a)
-
- # These should produce a warning
-
- def test_warn_1(self):
- s = """raise 'foo'"""
- self.warns(s, s, "Python 3 does not support string exceptions")
-
- def test_warn_2(self):
- s = """raise "foo", 5"""
- self.warns(s, s, "Python 3 does not support string exceptions")
-
- def test_warn_3(self):
- s = """raise "foo", 5, 6"""
- self.warns(s, s, "Python 3 does not support string exceptions")
-
- # These should result in traceback-assignment
-
- def test_tb_1(self):
- b = """def foo():
- raise Exception, 5, 6"""
- a = """def foo():
- raise Exception(5).with_traceback(6)"""
- self.check(b, a)
-
- def test_tb_2(self):
- b = """def foo():
- a = 5
- raise Exception, 5, 6
- b = 6"""
- a = """def foo():
- a = 5
- raise Exception(5).with_traceback(6)
- b = 6"""
- self.check(b, a)
-
- def test_tb_3(self):
- b = """def foo():
- raise Exception,5,6"""
- a = """def foo():
- raise Exception(5).with_traceback(6)"""
- self.check(b, a)
-
- def test_tb_4(self):
- b = """def foo():
- a = 5
- raise Exception,5,6
- b = 6"""
- a = """def foo():
- a = 5
- raise Exception(5).with_traceback(6)
- b = 6"""
- self.check(b, a)
-
- def test_tb_5(self):
- b = """def foo():
- raise Exception, (5, 6, 7), 6"""
- a = """def foo():
- raise Exception(5, 6, 7).with_traceback(6)"""
- self.check(b, a)
-
- def test_tb_6(self):
- b = """def foo():
- a = 5
- raise Exception, (5, 6, 7), 6
- b = 6"""
- a = """def foo():
- a = 5
- raise Exception(5, 6, 7).with_traceback(6)
- b = 6"""
- self.check(b, a)
-
-
-class Test_throw(FixerTestCase):
- fixer = "throw"
-
- def test_1(self):
- b = """g.throw(Exception, 5)"""
- a = """g.throw(Exception(5))"""
- self.check(b, a)
-
- def test_2(self):
- b = """g.throw(Exception,5)"""
- a = """g.throw(Exception(5))"""
- self.check(b, a)
-
- def test_3(self):
- b = """g.throw(Exception, (5, 6, 7))"""
- a = """g.throw(Exception(5, 6, 7))"""
- self.check(b, a)
-
- def test_4(self):
- b = """5 + g.throw(Exception, 5)"""
- a = """5 + g.throw(Exception(5))"""
- self.check(b, a)
-
- # These should produce warnings
-
- def test_warn_1(self):
- s = """g.throw("foo")"""
- self.warns(s, s, "Python 3 does not support string exceptions")
-
- def test_warn_2(self):
- s = """g.throw("foo", 5)"""
- self.warns(s, s, "Python 3 does not support string exceptions")
-
- def test_warn_3(self):
- s = """g.throw("foo", 5, 6)"""
- self.warns(s, s, "Python 3 does not support string exceptions")
-
- # These should not be touched
-
- def test_untouched_1(self):
- b = """g.throw(Exception)"""
- a = """g.throw(Exception)"""
- self.check(b, a)
-
- def test_untouched_2(self):
- b = """g.throw(Exception(5, 6))"""
- a = """g.throw(Exception(5, 6))"""
- self.check(b, a)
-
- def test_untouched_3(self):
- b = """5 + g.throw(Exception(5, 6))"""
- a = """5 + g.throw(Exception(5, 6))"""
- self.check(b, a)
-
- # These should result in traceback-assignment
-
- def test_tb_1(self):
- b = """def foo():
- g.throw(Exception, 5, 6)"""
- a = """def foo():
- g.throw(Exception(5).with_traceback(6))"""
- self.check(b, a)
-
- def test_tb_2(self):
- b = """def foo():
- a = 5
- g.throw(Exception, 5, 6)
- b = 6"""
- a = """def foo():
- a = 5
- g.throw(Exception(5).with_traceback(6))
- b = 6"""
- self.check(b, a)
-
- def test_tb_3(self):
- b = """def foo():
- g.throw(Exception,5,6)"""
- a = """def foo():
- g.throw(Exception(5).with_traceback(6))"""
- self.check(b, a)
-
- def test_tb_4(self):
- b = """def foo():
- a = 5
- g.throw(Exception,5,6)
- b = 6"""
- a = """def foo():
- a = 5
- g.throw(Exception(5).with_traceback(6))
- b = 6"""
- self.check(b, a)
-
- def test_tb_5(self):
- b = """def foo():
- g.throw(Exception, (5, 6, 7), 6)"""
- a = """def foo():
- g.throw(Exception(5, 6, 7).with_traceback(6))"""
- self.check(b, a)
-
- def test_tb_6(self):
- b = """def foo():
- a = 5
- g.throw(Exception, (5, 6, 7), 6)
- b = 6"""
- a = """def foo():
- a = 5
- g.throw(Exception(5, 6, 7).with_traceback(6))
- b = 6"""
- self.check(b, a)
-
- def test_tb_7(self):
- b = """def foo():
- a + g.throw(Exception, 5, 6)"""
- a = """def foo():
- a + g.throw(Exception(5).with_traceback(6))"""
- self.check(b, a)
-
- def test_tb_8(self):
- b = """def foo():
- a = 5
- a + g.throw(Exception, 5, 6)
- b = 6"""
- a = """def foo():
- a = 5
- a + g.throw(Exception(5).with_traceback(6))
- b = 6"""
- self.check(b, a)
-
-
-class Test_long(FixerTestCase):
- fixer = "long"
-
- def test_1(self):
- b = """x = long(x)"""
- a = """x = int(x)"""
- self.check(b, a)
-
- def test_2(self):
- b = """y = isinstance(x, long)"""
- a = """y = isinstance(x, int)"""
- self.check(b, a)
-
- def test_3(self):
- b = """z = type(x) in (int, long)"""
- a = """z = type(x) in (int, int)"""
- self.check(b, a)
-
- def test_4(self):
- b = """a = 12L"""
- a = """a = 12"""
- self.check(b, a)
-
- def test_5(self):
- b = """b = 0x12l"""
- a = """b = 0x12"""
- self.check(b, a)
-
- # These should not be touched
-
- def test_6(self):
- b = """a = 12"""
- a = """a = 12"""
- self.check(b, a)
-
- def test_7(self):
- b = """b = 0x12"""
- a = """b = 0x12"""
- self.check(b, a)
-
- def test_8(self):
- b = """c = 3.14"""
- a = """c = 3.14"""
- self.check(b, a)
-
-
-class Test_sysexcinfo(FixerTestCase):
- fixer = "sysexcinfo"
-
- def test_1(self):
- s = """sys.exc_info()"""
- self.warns(s, s, "This function is going away")
-
- def test_2(self):
- s = """if sys.exc_info()[1] == 1:
- pass"""
-
- self.warns(s, s, "This function is going away")
-
- def test_3(self):
- s = """f = sys.exc_info"""
- self.warns(s, s, "This function is going away")
-
- def test_4(self):
- s = """f = sys.exc_type + ":" + sys.exc_value"""
- self.warns(s, s, "This attribute is going away")
-
-
-class Test_dict(FixerTestCase):
- fixer = "dict"
-
- def test_01(self):
- b = "d.keys()"
- a = "list(d.keys())"
- self.check(b, a)
-
- def test_01a(self):
- b = "a[0].foo().keys()"
- a = "list(a[0].foo().keys())"
- self.check(b, a)
-
- def test_02(self):
- b = "d.items()"
- a = "list(d.items())"
- self.check(b, a)
-
- def test_03(self):
- b = "d.values()"
- a = "list(d.values())"
- self.check(b, a)
-
- def test_04(self):
- b = "d.iterkeys()"
- a = "iter(d.keys())"
- self.check(b, a)
-
- def test_05(self):
- b = "d.iteritems()"
- a = "iter(d.items())"
- self.check(b, a)
-
- def test_06(self):
- b = "d.itervalues()"
- a = "iter(d.values())"
- self.check(b, a)
-
- def test_07(self):
- b = "list(d.keys())"
- a = b
- self.check(b, a)
-
- def test_08(self):
- b = "sorted(d.keys())"
- a = b
- self.check(b, a)
-
- def test_09(self):
- b = "iter(d.keys())"
- a = "iter(list(d.keys()))"
- self.check(b, a)
-
- def test_10(self):
- b = "foo(d.keys())"
- a = "foo(list(d.keys()))"
- self.check(b, a)
-
- def test_11(self):
- b = "for i in d.keys(): print i"
- a = "for i in list(d.keys()): print i"
- self.check(b, a)
-
- def test_12(self):
- b = "for i in d.iterkeys(): print i"
- a = "for i in d.keys(): print i"
- self.check(b, a)
-
- def test_13(self):
- b = "[i for i in d.keys()]"
- a = "[i for i in list(d.keys())]"
- self.check(b, a)
-
- def test_14(self):
- b = "[i for i in d.iterkeys()]"
- a = "[i for i in d.keys()]"
- self.check(b, a)
-
- def test_15(self):
- b = "(i for i in d.keys())"
- a = "(i for i in list(d.keys()))"
- self.check(b, a)
-
- def test_16(self):
- b = "(i for i in d.iterkeys())"
- a = "(i for i in d.keys())"
- self.check(b, a)
-
- def test_17(self):
- b = "iter(d.iterkeys())"
- a = "iter(d.keys())"
- self.check(b, a)
-
- def test_18(self):
- b = "list(d.iterkeys())"
- a = "list(d.keys())"
- self.check(b, a)
-
- def test_19(self):
- b = "sorted(d.iterkeys())"
- a = "sorted(d.keys())"
- self.check(b, a)
-
- def test_20(self):
- b = "foo(d.iterkeys())"
- a = "foo(iter(d.keys()))"
- self.check(b, a)
-
- def test_21(self):
- b = "print h.iterkeys().next()"
- a = "print iter(h.keys()).next()"
- self.check(b, a)
-
- def test_22(self):
- b = "print h.keys()[0]"
- a = "print list(h.keys())[0]"
- self.check(b, a)
-
- def test_23(self):
- b = "print list(h.iterkeys().next())"
- a = "print list(iter(h.keys()).next())"
- self.check(b, a)
-
- def test_24(self):
- b = "for x in h.keys()[0]: print x"
- a = "for x in list(h.keys())[0]: print x"
- self.check(b, a)
-
-class Test_xrange(FixerTestCase):
- fixer = "xrange"
-
- def test_1(self):
- b = """x = xrange(10)"""
- a = """x = range(10)"""
- self.check(b, a)
-
- def test_2(self):
- b = """x = xrange(1, 10)"""
- a = """x = range(1, 10)"""
- self.check(b, a)
-
- def test_3(self):
- b = """x = xrange(0, 10, 2)"""
- a = """x = range(0, 10, 2)"""
- self.check(b, a)
-
- def test_4(self):
- b = """for i in xrange(10):\n j=i"""
- a = """for i in range(10):\n j=i"""
- self.check(b, a)
-
-
-class Test_raw_input(FixerTestCase):
- fixer = "raw_input"
-
- def test_1(self):
- b = """x = raw_input()"""
- a = """x = input()"""
- self.check(b, a)
-
- def test_2(self):
- b = """x = raw_input('')"""
- a = """x = input('')"""
- self.check(b, a)
-
- def test_3(self):
- b = """x = raw_input('prompt')"""
- a = """x = input('prompt')"""
- self.check(b, a)
-
-
-class Test_input(FixerTestCase):
- fixer = "input"
-
- def test_1(self):
- b = """x = input()"""
- a = """x = eval(input())"""
- self.check(b, a)
-
- def test_2(self):
- b = """x = input('')"""
- a = """x = eval(input(''))"""
- self.check(b, a)
-
- def test_3(self):
- b = """x = input('prompt')"""
- a = """x = eval(input('prompt'))"""
- self.check(b, a)
-
-
-if __name__ == "__main__":
- import sys
- if not sys.argv[1:]:
- sys.argv.append("-v")
- unittest.main()
Added: sandbox/trunk/2to3/test.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/test.py Wed Feb 28 00:45:47 2007
@@ -0,0 +1,8 @@
+"""Main test file for 2to3. Running "python test.py" will run all
+tests in tests/test_*.py."""
+# Author: Collin Winter
+
+import tests
+import tests.support
+
+tests.support.run_all_tests(tests=tests.all_tests)
Deleted: /sandbox/trunk/2to3/tests.py
==============================================================================
--- /sandbox/trunk/2to3/tests.py Wed Feb 28 00:45:47 2007
+++ (empty file)
@@ -1,239 +0,0 @@
-#!/usr/bin/env python2.5
-# Copyright 2006 Google, Inc. All Rights Reserved.
-# Licensed to PSF under a Contributor Agreement.
-
-"""Unit tests for pytree.py.
-
-NOTE: Please *don't* add doc strings to individual test methods!
-In verbose mode, printing of the module, class and method name is much
-more helpful than printing of (the first line of) the docstring,
-especially when debugging a test.
-"""
-
-# Python imports
-import unittest
-
-# Local imports (XXX should become a package)
-import pytree
-
-
-class TestNodes(unittest.TestCase):
-
- """Unit tests for nodes (Base, Leaf, Node)."""
-
- def testBaseCantConstruct(self):
- if __debug__:
- # Test that instantiating Base() raises an AssertionError
- self.assertRaises(AssertionError, pytree.Base)
-
- def testLeaf(self):
- l1 = pytree.Leaf(100, "foo")
- self.assertEqual(l1.type, 100)
- self.assertEqual(l1.value, "foo")
-
- def testLeafRepr(self):
- l1 = pytree.Leaf(100, "foo")
- self.assertEqual(repr(l1), "Leaf(100, 'foo')")
-
- def testLeafStr(self):
- l1 = pytree.Leaf(100, "foo")
- self.assertEqual(str(l1), "foo")
- l2 = pytree.Leaf(100, "foo", context=(" ", (10, 1)))
- self.assertEqual(str(l2), " foo")
-
- def testLeafEq(self):
- l1 = pytree.Leaf(100, "foo")
- l2 = pytree.Leaf(100, "foo", context=(" ", (1, 0)))
- self.assertEqual(l1, l2)
- l3 = pytree.Leaf(101, "foo")
- l4 = pytree.Leaf(100, "bar")
- self.assertNotEqual(l1, l3)
- self.assertNotEqual(l1, l4)
-
- def testLeafPrefix(self):
- l1 = pytree.Leaf(100, "foo")
- self.assertEqual(l1.get_prefix(), "")
- l1.set_prefix(" ##\n\n")
- self.assertEqual(l1.get_prefix(), " ##\n\n")
-
- def testNode(self):
- l1 = pytree.Leaf(100, "foo")
- l2 = pytree.Leaf(200, "bar")
- n1 = pytree.Node(1000, [l1, l2])
- self.assertEqual(n1.type, 1000)
- self.assertEqual(n1.children, (l1, l2))
-
- def testNodeRepr(self):
- l1 = pytree.Leaf(100, "foo")
- l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0)))
- n1 = pytree.Node(1000, [l1, l2])
- self.assertEqual(repr(n1),
- "Node(1000, (%s, %s))" % (repr(l1), repr(l2)))
-
- def testNodeStr(self):
- l1 = pytree.Leaf(100, "foo")
- l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0)))
- n1 = pytree.Node(1000, [l1, l2])
- self.assertEqual(str(n1), "foo bar")
-
- def testNodePrefix(self):
- l1 = pytree.Leaf(100, "foo")
- self.assertEqual(l1.get_prefix(), "")
- n1 = pytree.Node(1000, [l1])
- self.assertEqual(n1.get_prefix(), "")
- n1.set_prefix(" ")
- self.assertEqual(n1.get_prefix(), " ")
- self.assertEqual(l1.get_prefix(), " ")
-
- def testNodeEq(self):
- n1 = pytree.Node(1000, ())
- n2 = pytree.Node(1000, [], context=(" ", (1, 0)))
- self.assertEqual(n1, n2)
- n3 = pytree.Node(1001, ())
- self.assertNotEqual(n1, n3)
-
- def testNodeEqRecursive(self):
- l1 = pytree.Leaf(100, "foo")
- l2 = pytree.Leaf(100, "foo")
- n1 = pytree.Node(1000, [l1])
- n2 = pytree.Node(1000, [l2])
- self.assertEqual(n1, n2)
- l3 = pytree.Leaf(100, "bar")
- n3 = pytree.Node(1000, [l3])
- self.assertNotEqual(n1, n3)
-
- def testReplace(self):
- l1 = pytree.Leaf(100, "foo")
- l2 = pytree.Leaf(100, "+")
- l3 = pytree.Leaf(100, "bar")
- n1 = pytree.Node(1000, [l1, l2, l3])
- self.assertEqual(n1.children, (l1, l2, l3))
- l2new = pytree.Leaf(100, "-")
- l2.replace(l2new)
- self.assertEqual(n1.children, (l1, l2new, l3))
-
- def testConvert(self):
- # XXX
- pass
-
-
-class TestPatterns(unittest.TestCase):
-
- """Unit tests for tree matching patterns."""
-
- def testBasicPatterns(self):
- # Build a tree
- l1 = pytree.Leaf(100, "foo")
- l2 = pytree.Leaf(100, "bar")
- l3 = pytree.Leaf(100, "foo")
- n1 = pytree.Node(1000, [l1, l2])
- n2 = pytree.Node(1000, [l3])
- root = pytree.Node(1000, [n1, n2])
- # Build a pattern matching a leaf
- pl = pytree.LeafPattern(100, "foo", name="pl")
- r = {}
- self.assertEqual(pl.match(root, results=r), False)
- self.assertEqual(r, {})
- self.assertEqual(pl.match(n1, results=r), False)
- self.assertEqual(r, {})
- self.assertEqual(pl.match(n2, results=r), False)
- self.assertEqual(r, {})
- self.assertEqual(pl.match(l1, results=r), True)
- self.assertEqual(r, {"pl": l1})
- r = {}
- self.assertEqual(pl.match(l2, results=r), False)
- self.assertEqual(r, {})
- # Build a pattern matching a node
- pn = pytree.NodePattern(1000, [pl], name="pn")
- self.assertEqual(pn.match(root, results=r), False)
- self.assertEqual(r, {})
- self.assertEqual(pn.match(n1, results=r), False)
- self.assertEqual(r, {})
- self.assertEqual(pn.match(n2, results=r), True)
- self.assertEqual(r, {"pn": n2, "pl": l3})
- r = {}
- self.assertEqual(pn.match(l1, results=r), False)
- self.assertEqual(r, {})
- self.assertEqual(pn.match(l2, results=r), False)
- self.assertEqual(r, {})
-
- def testWildcardPatterns(self):
- # Build a tree for testing
- l1 = pytree.Leaf(100, "foo")
- l2 = pytree.Leaf(100, "bar")
- l3 = pytree.Leaf(100, "foo")
- n1 = pytree.Node(1000, [l1, l2])
- n2 = pytree.Node(1000, [l3])
- root = pytree.Node(1000, [n1, n2])
- # Build a pattern
- pl = pytree.LeafPattern(100, "foo", name="pl")
- pn = pytree.NodePattern(1000, [pl], name="pn")
- pw = pytree.WildcardPattern([[pn], [pl, pl]], name="pw")
- r = {}
- self.assertEqual(pw.match_seq([root], r), False)
- self.assertEqual(r, {})
- self.assertEqual(pw.match_seq([n1], r), False)
- self.assertEqual(r, {})
- self.assertEqual(pw.match_seq([n2], r), True)
- # These are easier to debug
- self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"])
- self.assertEqual(r["pl"], l1)
- self.assertEqual(r["pn"], n2)
- self.assertEqual(r["pw"], (n2,))
- # But this is equivalent
- self.assertEqual(r, {"pl": l1, "pn": n2, "pw": (n2,)})
- r = {}
- self.assertEqual(pw.match_seq([l1, l3], r), True)
- self.assertEqual(r, {"pl": l3, "pw": (l1, l3)})
- self.assert_(r["pl"] is l3)
- r = {}
-
- def testGenerateMatches(self):
- la = pytree.Leaf(1, "a")
- lb = pytree.Leaf(1, "b")
- lc = pytree.Leaf(1, "c")
- ld = pytree.Leaf(1, "d")
- le = pytree.Leaf(1, "e")
- lf = pytree.Leaf(1, "f")
- leaves = [la, lb, lc, ld, le, lf]
- root = pytree.Node(1000, leaves)
- pa = pytree.LeafPattern(1, "a", "pa")
- pb = pytree.LeafPattern(1, "b", "pb")
- pc = pytree.LeafPattern(1, "c", "pc")
- pd = pytree.LeafPattern(1, "d", "pd")
- pe = pytree.LeafPattern(1, "e", "pe")
- pf = pytree.LeafPattern(1, "f", "pf")
- pw = pytree.WildcardPattern([[pa, pb, pc], [pd, pe],
- [pa, pb], [pc, pd], [pe, pf]],
- min=1, max=4, name="pw")
- self.assertEqual([x[0] for x in pw.generate_matches(leaves)],
- [3, 5, 2, 4, 6])
- pr = pytree.NodePattern(type=1000, content=[pw], name="pr")
- matches = list(pytree.generate_matches([pr], [root]))
- self.assertEqual(len(matches), 1)
- c, r = matches[0]
- self.assertEqual(c, 1)
- self.assertEqual(str(r["pr"]), "abcdef")
- self.assertEqual(r["pw"], (la, lb, lc, ld, le, lf))
- for c in "abcdef":
- self.assertEqual(r["p" + c], pytree.Leaf(1, c))
-
- def testHasKeyExample(self):
- pattern = pytree.NodePattern(331,
- (pytree.LeafPattern(7),
- pytree.WildcardPattern(name="args"),
- pytree.LeafPattern(8)))
- l1 = pytree.Leaf(7, "(")
- l2 = pytree.Leaf(3, "x")
- l3 = pytree.Leaf(8, ")")
- node = pytree.Node(331, (l1, l2, l3))
- r = {}
- self.assert_(pattern.match(node, r))
- self.assertEqual(r["args"], (l2,))
-
-
-if __name__ == "__main__":
- import sys
- if not sys.argv[1:]:
- sys.argv.append("-v")
- unittest.main()
Added: sandbox/trunk/2to3/tests/__init__.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/tests/__init__.py Wed Feb 28 00:45:47 2007
@@ -0,0 +1,23 @@
+"""Make tests/ into a package. This allows us to "import tests" and
+have tests.all_tests be a TestSuite representing all test cases
+from all test_*.py files in tests/."""
+# Author: Collin Winter
+
+import os
+import os.path
+import unittest
+import types
+
+import support
+
+all_tests = unittest.TestSuite()
+
+tests_dir = os.path.join(os.getcwd(), 'tests')
+tests = [t[0:-3] for t in os.listdir(tests_dir)
+ if t.startswith('test_') and t.endswith('.py')]
+
+loader = unittest.TestLoader()
+for t in tests:
+ __import__('tests.' + t)
+ mod = globals()[t]
+ all_tests.addTests(loader.loadTestsFromModule(mod))
Added: sandbox/trunk/2to3/tests/support.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/tests/support.py Wed Feb 28 00:45:47 2007
@@ -0,0 +1,17 @@
+"""Support code for test_*.py files"""
+# Author: Collin Winter
+
+import unittest
+import sys
+import os.path
+
+def run_all_tests(test_mod=None, tests=None):
+ if tests is None:
+ tests = unittest.TestLoader().loadTestsFromModule(test_mod)
+ unittest.TextTestRunner(verbosity=2).run(tests)
+
+def adjust_path():
+ parent_dir = os.path.split(sys.path[0])[0]
+ sys.path = [parent_dir] + sys.path
+
+TestCase = unittest.TestCase
Copied: sandbox/trunk/2to3/tests/test_fixers.py (from r53996, sandbox/trunk/2to3/fixer_tests.py)
==============================================================================
--- sandbox/trunk/2to3/fixer_tests.py (original)
+++ sandbox/trunk/2to3/tests/test_fixers.py Wed Feb 28 00:45:47 2007
@@ -2,6 +2,11 @@
""" Test suite for the fixer modules """
# Author: Collin Winter
+# Testing imports
+import support
+if __name__ == '__main__':
+ support.adjust_path()
+
# Python imports
from StringIO import StringIO
import re
@@ -45,7 +50,7 @@
self.verbose = False
-class FixerTestCase(unittest.TestCase):
+class FixerTestCase(support.TestCase):
def setUp(self):
options = Options(fix=[self.fixer], print_function=False)
self.refactor = refactor.RefactoringTool(options)
@@ -1120,7 +1125,5 @@
if __name__ == "__main__":
- import sys
- if not sys.argv[1:]:
- sys.argv.append("-v")
- unittest.main()
+ import __main__
+ support.run_all_tests(__main__)
Copied: sandbox/trunk/2to3/tests/test_pytree.py (from r53996, sandbox/trunk/2to3/tests.py)
==============================================================================
--- sandbox/trunk/2to3/tests.py (original)
+++ sandbox/trunk/2to3/tests/test_pytree.py Wed Feb 28 00:45:47 2007
@@ -10,14 +10,16 @@
especially when debugging a test.
"""
-# Python imports
-import unittest
+# Testing imports
+import support
+if __name__ == '__main__':
+ support.adjust_path()
# Local imports (XXX should become a package)
import pytree
-class TestNodes(unittest.TestCase):
+class TestNodes(support.TestCase):
"""Unit tests for nodes (Base, Leaf, Node)."""
@@ -117,7 +119,7 @@
pass
-class TestPatterns(unittest.TestCase):
+class TestPatterns(support.TestCase):
"""Unit tests for tree matching patterns."""
@@ -233,7 +235,5 @@
if __name__ == "__main__":
- import sys
- if not sys.argv[1:]:
- sys.argv.append("-v")
- unittest.main()
+ import __main__
+ support.run_all_tests(__main__)
More information about the Python-checkins
mailing list