forked from PythonCharmers/python-future
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.py
More file actions
106 lines (86 loc) · 3.2 KB
/
test_utils.py
File metadata and controls
106 lines (86 loc) · 3.2 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
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
"""
Tests for the various utility functions and classes in ``future.utils``
"""
from __future__ import absolute_import, unicode_literals, print_function
from future.builtins import *
from future.utils import old_div, istext, isbytes, native, PY2, PY3, native_str
from numbers import Integral
from future.tests.base import unittest
TEST_UNICODE_STR = u'ℝεα∂@ßʟ℮ ☂ℯṧт υηḯ¢☺ḓ℮'
class TestUtils(unittest.TestCase):
def setUp(self):
self.s = TEST_UNICODE_STR
self.s2 = str(self.s)
self.b = b'ABCDEFG'
self.b2 = bytes(self.b)
def test_old_div(self):
"""
Tests whether old_div(a, b) is always equal to Python 2's a / b.
"""
self.assertEqual(old_div(1, 2), 0)
self.assertEqual(old_div(2, 2), 1)
self.assertTrue(isinstance(old_div(2, 2), int))
self.assertEqual(old_div(3, 2), 1)
self.assertTrue(isinstance(old_div(3, 2), int))
self.assertEqual(old_div(3., 2), 1.5)
self.assertTrue(not isinstance(old_div(3., 2), int))
self.assertEqual(old_div(-1, 2.), -0.5)
self.assertTrue(not isinstance(old_div(-1, 2.), int))
with self.assertRaises(ZeroDivisionError):
old_div(0, 0)
with self.assertRaises(ZeroDivisionError):
old_div(1, 0)
def test_native_str(self):
"""
Tests whether native_str(b'blah') and native_str(u'blah') and
native_str('blah') are all equivalent to 'blah'. Should be true
on both Python 2 and Python 3.
"""
if PY2:
s = b'blah' # b because unicode_literals is in effect
native_s_type = type(s)
elif PY3:
s = 'blah'
native_s_type = type(s)
self.assertEqual(native_str(b'blah'), s)
self.assertTrue(isinstance(native_str(b'blah'), native_s_type))
self.assertEqual(native_str(u'blah'), s)
self.assertTrue(isinstance(native_str(u'blah'), native_s_type))
self.assertEqual(native_str('blah'), s)
self.assertTrue(isinstance(native_str('blah'), native_s_type))
def test_native(self):
a = int(10**20) # long int
b = native(a)
self.assertEqual(a, b)
if PY2:
self.assertEqual(type(b), long)
else:
self.assertEqual(type(b), int)
c = bytes(b'ABC')
d = native(c)
self.assertEqual(c, d)
if PY2:
self.assertEqual(type(d), type(b'Py2 byte-string'))
else:
self.assertEqual(type(d), bytes)
s = str(u'ABC')
t = native(s)
self.assertEqual(s, t)
if PY2:
self.assertEqual(type(t), unicode)
else:
self.assertEqual(type(t), str)
type(s)
def test_istext(self):
self.assertTrue(istext(self.s))
self.assertTrue(istext(self.s2))
self.assertFalse(istext(self.b))
self.assertFalse(istext(self.b2))
def test_isbytes(self):
self.assertTrue(isbytes(self.b))
self.assertTrue(isbytes(self.b2))
self.assertFalse(isbytes(self.s))
self.assertFalse(isbytes(self.s2))
if __name__ == '__main__':
unittest.main()