forked from PythonCharmers/python-future
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_builtins.py
More file actions
95 lines (82 loc) · 3.16 KB
/
Copy pathtest_builtins.py
File metadata and controls
95 lines (82 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Tests to make sure the behaviour of the builtins is sensible and correct.
"""
from __future__ import absolute_import, division, unicode_literals
from future.builtins import *
from future.utils import PY3
import textwrap
from subprocess import Popen, PIPE
from numbers import Integral
import unittest
import pytest
class TestBuiltins(unittest.TestCase):
@pytest.mark.xfail
def test_isinstance_int(self):
"""
Redefining ``int`` to ``long`` on Py2 would make this
test fail:
"""
self.assertTrue(isinstance(0, int))
def test_isinstance_Integral(self):
"""
Tests the preferred alternative to the above
"""
self.assertTrue(isinstance(0, Integral))
@pytest.mark.xfail # Py2's long doesn't inherit from int!
def test_long(self):
self.assertEqual(isinstance(10**100, int))
if not PY3:
self.assertEqual(isinstance(long(1), int))
# Note: the following is a SyntaxError on Py3:
# self.assertEqual(isinstance(1L, int))
def test_round(self):
"""
Note that the Python 2.x round() function fails these tests. The
Python 3.x round() function passes them, as should our custom
round() function.
"""
self.assertEqual(round(0.1250, 2), 0.12)
self.assertEqual(round(0.1350, 2), 0.14)
self.assertEqual(round(0.1251, 2), 0.13)
self.assertEqual(round(0.125000001, 2), 0.13)
self.assertEqual(round(123.5, 0), 124.0)
self.assertEqual(round(123.5), 124)
self.assertEqual(round(12.35, 2), 12.35)
self.assertEqual(round(12.35, 1), 12.3)
self.assertEqual(round(12.35, 0), 12.0)
self.assertEqual(round(123.5, 1), 123.5)
self.assertTrue(isinstance(round(123.5, 0), float))
self.assertTrue(isinstance(round(123.5), Integral))
@pytest.mark.xfail('negative ndigits not implemented yet')
def test_round_negative_ndigits(self):
self.assertEqual(round(10.1350, 0), 10.0)
self.assertEqual(round(10.1350, -1), 10.0)
self.assertEqual(round(10.1350, -2), 0.0)
self.assertEqual(round(10.1350, -3), 0.0)
self.assertEqual(round(12.35, -1), 10.0)
self.assertEqual(round(12.35, -2), 0.0)
self.assertEqual(round(123.5, -1), 120.0)
self.assertEqual(round(123.5, -2), 100.0)
self.assertEqual(round(123.551, -2), 100.0)
self.assertEqual(round(123.551, -3), 0.0)
def test_input(self, interpreter='python2'):
"""
Passes in a string to the waiting input()
"""
code = '''
from future.builtins import input
def greet(name):
print "Hello, {0}!".format(name)
print "What's your name?"
name = input()
greet(name)
'''
with open('mytestscript.py', 'w') as f:
f.write(textwrap.dedent(code))
p1 = Popen([interpreter, 'mytestscript.py'], stdout=PIPE, stdin=PIPE, stderr=None)
(stdout, stderr) = p1.communicate(b'Ed')
# print(stdout)
# print(stderr)
self.assertEqual(stdout, b"What's your name?\nHello, Ed!\n")
if __name__ == '__main__':
unittest.main()