summaryrefslogtreecommitdiffstats
path: root/Lib/lib2to3/tests
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/lib2to3/tests')
-rw-r--r--Lib/lib2to3/tests/test_all_fixers.py4
-rwxr-xr-xLib/lib2to3/tests/test_fixers.py69
-rw-r--r--Lib/lib2to3/tests/test_main.py36
3 files changed, 104 insertions, 5 deletions
diff --git a/Lib/lib2to3/tests/test_all_fixers.py b/Lib/lib2to3/tests/test_all_fixers.py
index 61bcc54..f64b3d9 100644
--- a/Lib/lib2to3/tests/test_all_fixers.py
+++ b/Lib/lib2to3/tests/test_all_fixers.py
@@ -9,12 +9,12 @@ running time.
import unittest
# Local imports
-from .. import pytree
-from .. import refactor
+from lib2to3 import refactor
from . import support
class Test_all(support.TestCase):
+
def setUp(self):
self.refactor = support.get_refactorer()
diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py
index ea361de..2e0092f 100755
--- a/Lib/lib2to3/tests/test_fixers.py
+++ b/Lib/lib2to3/tests/test_fixers.py
@@ -2725,16 +2725,79 @@ class Test_callable(FixerTestCase):
def test_prefix_preservation(self):
b = """callable( x)"""
- a = """hasattr( x, '__call__')"""
+ a = """import collections\nisinstance( x, collections.Callable)"""
self.check(b, a)
b = """if callable(x): pass"""
- a = """if hasattr(x, '__call__'): pass"""
+ a = """import collections
+if isinstance(x, collections.Callable): pass"""
self.check(b, a)
def test_callable_call(self):
b = """callable(x)"""
- a = """hasattr(x, '__call__')"""
+ a = """import collections\nisinstance(x, collections.Callable)"""
+ self.check(b, a)
+
+ def test_global_import(self):
+ b = """
+def spam(foo):
+ callable(foo)"""[1:]
+ a = """
+import collections
+def spam(foo):
+ isinstance(foo, collections.Callable)"""[1:]
+ self.check(b, a)
+
+ b = """
+import collections
+def spam(foo):
+ callable(foo)"""[1:]
+ # same output if it was already imported
+ self.check(b, a)
+
+ b = """
+from collections import *
+def spam(foo):
+ callable(foo)"""[1:]
+ a = """
+from collections import *
+import collections
+def spam(foo):
+ isinstance(foo, collections.Callable)"""[1:]
+ self.check(b, a)
+
+ b = """
+do_stuff()
+do_some_other_stuff()
+assert callable(do_stuff)"""[1:]
+ a = """
+import collections
+do_stuff()
+do_some_other_stuff()
+assert isinstance(do_stuff, collections.Callable)"""[1:]
+ self.check(b, a)
+
+ b = """
+if isinstance(do_stuff, Callable):
+ assert callable(do_stuff)
+ do_stuff(do_stuff)
+ if not callable(do_stuff):
+ exit(1)
+ else:
+ assert callable(do_stuff)
+else:
+ assert not callable(do_stuff)"""[1:]
+ a = """
+import collections
+if isinstance(do_stuff, Callable):
+ assert isinstance(do_stuff, collections.Callable)
+ do_stuff(do_stuff)
+ if not isinstance(do_stuff, collections.Callable):
+ exit(1)
+ else:
+ assert isinstance(do_stuff, collections.Callable)
+else:
+ assert not isinstance(do_stuff, collections.Callable)"""[1:]
self.check(b, a)
def test_callable_should_not_change(self):
diff --git a/Lib/lib2to3/tests/test_main.py b/Lib/lib2to3/tests/test_main.py
new file mode 100644
index 0000000..23d1b56
--- /dev/null
+++ b/Lib/lib2to3/tests/test_main.py
@@ -0,0 +1,36 @@
+# -*- coding: utf-8 -*-
+import sys
+import codecs
+import io
+import unittest
+
+from lib2to3 import main
+
+
+class TestMain(unittest.TestCase):
+
+ def run_2to3_capture(self, args, in_capture, out_capture, err_capture):
+ save_stdin = sys.stdin
+ save_stdout = sys.stdout
+ save_stderr = sys.stderr
+ sys.stdin = in_capture
+ sys.stdout = out_capture
+ sys.stderr = err_capture
+ try:
+ return main.main("lib2to3.fixes", args)
+ finally:
+ sys.stdin = save_stdin
+ sys.stdout = save_stdout
+ sys.stderr = save_stderr
+
+ def test_unencodable_diff(self):
+ input_stream = io.StringIO("print 'nothing'\nprint u'über'\n")
+ out = io.StringIO()
+ out_enc = codecs.getwriter("ascii")(out)
+ err = io.StringIO()
+ ret = self.run_2to3_capture(["-"], input_stream, out_enc, err)
+ self.assertEqual(ret, 0)
+ output = out.getvalue()
+ self.assertTrue("-print 'nothing'" in output)
+ self.assertTrue("WARNING: couldn't encode <stdin>'s diff for "
+ "your terminal" in err.getvalue())