8000 Add and improve various tests · r3m0t/python-future@3f64c37 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 3f64c37

Browse files
committed
Add and improve various tests
1 parent 32ba322 commit 3f64c37

File tree

7 files changed

+161
-30
lines changed

7 files changed

+161
-30
lines changed

future/tests/base.py

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,11 @@ def reformat(self, code):
105105
code = code[1:]
106106
return dedent(code)
107107

108-
def compare(self, output, expected, ignore_imports=True):
108+
def check(self, output, expected, ignore_imports=True):
109109
"""
110-
Compares whether the code blocks are equal. Ignores any trailing
111-
whitespace like blank lines.
110+
Compares whether the code blocks are equal. If not, raises an
111+
exception so the test fails. Ignores any trailing whitespace like
112+
blank lines.
112113
113114
If ignore_imports is True, passes the code blocks into the
114115
strip_future_imports method.
@@ -118,7 +119,8 @@ def compare(self, output, expected, ignore_imports=True):
118119
if ignore_imports:
119120
output = self.strip_future_imports(output)
120121
expected = self.strip_future_imports(expected)
121-
self.assertEqual(output.rstrip(), expected.rstrip())
122+
self.assertEqual(self.order_future_lines(output.rstrip()),
123+
expected.rstrip())
122124

123125
def strip_future_imports(self, code):
124126
"""
@@ -151,41 +153,50 @@ def convert_check(self, before, expected, stages=(1, 2),
151153
152154
Reformats the code blocks automatically using the reformat()
153155
method.
156+
157+
If all_imports is passed, we add the appropriate import headers
158+
for the stage(s) selected to the ``expected`` code-block, so they
159+
needn't appear repeatedly in the test code.
160+
161+
If ignore_imports is True, ignores the presence of any lines
162+
beginning:
163+
164+
from __future__ import ...
165+
from future import ...
166+
167+
for the purpose of the comparison.
154168
"""
155169
output = self.convert(before, stages=stages,
156170
all_imports=all_imports, from3=from3,
157171
run=run)
172+
if all_imports:
173+
headers = self.headers2 if 2 in stages else self.headers1
174+
else:
175+
headers = ''
158176

159-
self.check(output, expected, stages=stages,
177+
self.check(output, self.reformat(headers + expected),
160178
ignore_imports=ignore_imports)
161179

162-
def check(self, output, expected, stages=(1, 2), ignore_imports=True):
180+
def check_old(self, output, expected, stages=(1, 2), ignore_imports=True):
163181
"""
164182
Checks that the output is equal to the expected output, after
165183
reformatting.
166184
167-
If ignore_imports is True, ignores the presence of any lines
168-
beginning:
169-
170-
from __future__ import ...
171-
from future import ...
172-
173185
Pass ``expected`` as a string (as a code block). It will be
174186
reformatted and compared with the resulting code. We assert that
175187
the output of the conversion of ``before`` with ``futurize`` is
176188
equal to ``after``. Unless ignore_imports is True, the
177189
appropriate headers for the stage(s) used are added automatically
178190
for the comparison.
179191
"""
180-
if expected is not None:
181-
headers = ''
182-
if not ignore_imports:
183-
if 2 in stages:
184-
headers = self.headers2
185-
else:
186-
headers = self.headers1
187-
self.compare(output, headers + self.reformat(expected),
188-
ignore_imports=ignore_imports)
192+
headers = ''
193+
# if not ignore_imports:
194+
# if 2 in stages:
195+
# headers = self.headers2
196+
# else:
197+
# headers = self.headers1
198+
self.compare(output, headers + self.reformat(expected),
199+
ignore_imports=ignore_imports)
189200

190201
def order_future_lines(self, code):
191202
"""

future/tests/test_builtins.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ def test_isinstance_str(self):
6262
self.assertTrue(isinstance(u'string', str))
6363
self.assertFalse(isinstance(u'string', bytes))
6464

65+
def test_type(self):
66+
"""
67+
The following fails when passed a unicode string on Python
68+
(including when unicode_literals is in effect) and fails when
69+
passed a byte-string on Python 3. So type() always wants a native
70+
string as the first argument.
71+
72+
TODO: provide a replacement that works identically on Py2/3?
73+
"""
74+
mytype = type('blah', (dict,), {"old": 1, "new": 2})
75+
d = mytype()
76+
self.assertTrue(isinstance(d, mytype))
77+
self.assertTrue(isinstance(d, dict))
78+
6579
def test_isinstance_tuple_of_types(self):
6680
# These two should be equivalent, even if ``int`` is a special
6781
# backported type.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Tests to make sure that all builtins can be imported explicitly from the
3+
future.builtins namespace.
4+
"""
5+
6+
from __future__ import absolute_import, division, unicode_literals
7+
from future.builtins import (filter, map, zip)
8+
from future.builtins import (ascii, chr, hex, input, isinstance, oct, open)
9+
from future.builtins import (bytes, int, range, round, str, super)
10+
from future.tests.base import unittest
11+
12+
13+
class TestBuiltinsExplicitImport(unittest.TestCase):
14+
pass
15+
16+
17+
if __name__ == '__main__':
18+
unittest.main()

future/tests/test_futurize.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,40 @@ def test_problematic_string(self):
2828
"""
2929
self.convert_check(before, after)
3030

31+
def test_import_builtins(self):
32+
before = """
33+
a = raw_input()
34+
b = open(a, b, c)
35+
c = filter(a, b)
36+
d = map(a, b)
37+
e = isinstance(a, str)
38+
f = bytes(a, encoding='utf-8')
39+
for g in xrange(10**10):
40+
pass
41+
super(MyClass, self)
42+
"""
43+
after = """
44+
from __future__ import unicode_literals
45+
from future.builtins import bytes
46+
from future.builtins import filter
47+
from future.builtins import input
48+
from future.builtins import isinstance
49+
from future.builtins import map
50+
from future.builtins import open
51+
from future.builtins import range
52+
from future.builtins import super
53+
a = input()
54+
b = open(a, b, c)
55+
c = list(filter(a, b))
56+
d = list(map(a, b))
57+
e = isinstance(a, str)
58+
f = bytes(a, encoding='utf-8')
59+
for g in range(10**10):
60+
pass
61+
super(MyClass, self)
62+
"""
63+
self.convert_check(before, after, ignore_imports=False, run=False)
64+
3165
def test_xrange(self):
3266
code = '''
3367
for i in xrange(10):
@@ -149,11 +183,6 @@ def greet(name):
149183
greet(name)
150184
"""
151185
self.convert_check(before, desired, run=False)
152-
# self._write_test_script(self.reformat(before))
153-
# self._futurize_test_script()
154-
# output = self._read_test_script()
155-
# self.compare(output, self.headers2 + self.reformat(desired),
156-
# ignore_imports=False)
157186

158187
for interpreter in self.interpreters:
159188
p1 = Popen([interpreter, self.tempdir + 'mytestscript.py'],

future/tests/test_standard_library.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,37 @@ def test_all(self):
3939
if '.' not in oldname:
4040
self.assertEqual(oldmod, newmod)
4141

42+
def test_suspend_hooks(self):
43+
example_PY2_check = False
44+
with standard_library.suspend_hooks():
45+
# An example of fragile import code that we don't want to break:
46+
try:
47+
import builtins
48+
except ImportError:
49+
example_PY2_check = True
50+
if utils.PY2:
51+
self.assertTrue(example_PY2_check)
52+
else:
53+
self.assertFalse(example_PY2_check)
54+
# The import should succeed again now:
55+
import builtins
56+
57+
def test_disable_hooks(self):
58+
example_PY2_check = False
59+
standard_library.disable_hooks()
60+
# An example of fragile import code that we don't want to break:
61+
try:
62+
import builtins
63+
except ImportError:
64+
example_PY2_check = True
65+
if utils.PY2:
66+
self.assertTrue(example_PY2_check)
67+
else:
68+
self.assertFalse(example_PY2_check)
69+
standard_library.enable_hooks()
70+
# The import should succeed again now:
71+
import builtins
72+
4273
@unittest.skipIf(utils.PY3, 'not testing for old urllib on Py3')
4374
def test_old_urllib_import(self):
4475
"""

future/tests/test_str.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from future import utils
99
from future.tests.base import unittest
1010

11+
import os
12+
1113
TEST_UNICODE_STR = u'ℝεα∂@ßʟ℮ ☂ℯṧт υηḯ¢☺ḓ℮'
1214

1315

@@ -17,6 +19,13 @@ def test_str(self):
1719
self.assertEqual(str('blah'), u'blah') # u'' prefix: Py3.3 and Py2 only
1820
self.assertEqual(str(b'1234'), "b'1234'")
1921

22+
def test_os_path_join(self):
23+
"""
24+
Issue #...: can't os.path.join(u'abc', str(u'def'))
25+
"""
26+
self.assertEqual(os.path.join(u'abc', str(u'def')),
27+
u'abc{}def'.format(os.sep))
28+
2029
def test_str_encode_utf8(self):
2130
b = str(TEST_UNICODE_STR).encode('utf-8')
2231
self.assertTrue(isinstance(b, bytes))
@@ -140,9 +149,7 @@ def test_str_plus_str(s 741A elf):
140149

141150
s4 = 'ZYXW' + s1
142151
self.assertEqual(s4, 'ZYXWABCD')
143-
# str objects don't have an __radd__ method, so the following is
144-
# not True. Is this a problem?
145-
# self.assertTrue(isinstance(s4, str))
152+
self.assertTrue(isinstance(s4, str))
146153

147154
def test_str_join_str(self):
148155
s = str(' * ')

future/tests/test_utils.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from __future__ import absolute_import, unicode_literals, print_function
77
from future.builtins import *
8-
from future.utils import old_div, istext, isbytes, native, PY2, PY3
8+
from future.utils import old_div, istext, isbytes, native, PY2, PY3, native_str
99

1010
from numbers import Integral
1111
from future.tests.base import unittest
@@ -42,6 +42,27 @@ def test_old_div(self):
4242
with self.assertRaises(ZeroDivisionError):
4343
old_div(1, 0)
4444

45+
def test_native_str(self):
46+
"""
47+
Tests whether native_str(b'blah') and native_str(u'blah') and
48+
native_str('blah') are all equivalent to 'blah'. Should be true
49+
on both Python 2 and Python 3.
50+
"""
51+
if PY2:
52+
s = b'blah' # b because unicode_literals is in effect
53+
native_s_type = type(s)
54+
elif PY3:
55+
s = 'blah'
56+
native_s_type = type(s)
57+
self.assertEqual(native_str(b'blah'), s)
58+
self.assertTrue(isinstance(native_str(b'blah'), native_s_type))
59+
60+
self.assertEqual(native_str(u'blah'), s)
61+
self.assertTrue(isinstance(native_str(u'blah'), native_s_type))
62+
63+
self.assertEqual(native_str('blah'), s)
64+
self.assertTrue(isinstance(native_str('blah'), native_s_type))
65+
4566
def test_native(self):
4667
a = int(10**20) # long int
4768
b = native(a)

0 commit comments

Comments
 (0)
0