forked from PythonCharmers/python-future
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_object.py
More file actions
140 lines (116 loc) · 4.21 KB
/
test_object.py
File metadata and controls
140 lines (116 loc) · 4.21 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""
Tests to make sure the newobject object (which defines Python 2-compatible
``__unicode__`` and ``next`` methods) is working.
"""
from __future__ import absolute_import, division
from future import utils
from future.builtins import object, str, next, int, super
from future.utils import implements_iterator, python_2_unicode_compatible
from future.tests.base import unittest
class TestNewObject(unittest.TestCase):
def test_object_implements_py2_unicode_method(self):
my_unicode_str = u'Unicode string: \u5b54\u5b50'
class A(object):
def __str__(self):
return my_unicode_str
a = A()
self.assertEqual(len(str(a)), 18)
if utils.PY2:
self.assertTrue(hasattr(a, '__unicode__'))
else:
self.assertFalse(hasattr(a, '__unicode__'))
self.assertEqual(str(a), my_unicode_str)
self.assertTrue(isinstance(str(a).encode('utf-8'), bytes))
if utils.PY2:
self.assertTrue(type(unicode(a)) == unicode)
self.assertEqual(unicode(a), my_unicode_str)
# Manual equivalent on Py2 without the decorator:
if not utils.PY3:
class B(object):
def __unicode__(self):
return u'Unicode string: \u5b54\u5b50'
def __str__(self):
return unicode(self).encode('utf-8')
b = B()
assert str(a) == str(b)
def test_implements_py2_iterator(self):
class Upper(object):
def __init__(self, iterable):
self._iter = iter(iterable)
def __next__(self): # note the Py3 interface
return next(self._iter).upper()
def __iter__(self):
return self
self.assertEqual(list(Upper('hello')), list('HELLO'))
# Try combining it with the next() function:
class MyIter(object):
def __next__(self):
return 'Next!'
def __iter__(self):
return self
itr = MyIter()
self.assertEqual(next(itr), 'Next!')
itr2 = MyIter()
for i, item in enumerate(itr2):
if i >= 10:
break
self.assertEqual(item, 'Next!')
def test_implements_py2_nonzero(self):
class EvenIsTrue(object):
"""
An integer that evaluates to True if even.
"""
def __init__(self, my_int):
self.my_int = my_int
def __bool__(self):
return self.my_int % 2 == 0
def __add__(self, other):
return type(self)(self.my_int + other)
k = EvenIsTrue(5)
self.assertFalse(k)
self.assertFalse(bool(k))
self.assertTrue(k + 1)
self.assertTrue(bool(k + 1))
self.assertFalse(k + 2)
def test_int_implements_py2_nonzero(self):
"""
Tests whether the newint object provides a __nonzero__ method that
maps to __bool__ in case the user redefines __bool__ in a subclass of
newint.
"""
class EvenIsTrue(int):
"""
An integer that evaluates to True if even.
"""
def __bool__(self):
return self % 2 == 0
def __add__(self, other):
val = super().__add__(other)
return type(self)(val)
k = EvenIsTrue(5)
self.assertFalse(k)
self.assertFalse(bool(k))
self.assertTrue(k + 1)
self.assertTrue(bool(k + 1))
self.assertFalse(k + 2)
def test_non_iterator(self):
"""
The default behaviour of next(o) for a newobject o should be to raise a
TypeError, as with the corresponding builtin object.
"""
o = object()
with self.assertRaises(TypeError):
next(o)
def test_bool_empty_object(self):
"""
The default result of bool(newobject()) should be True, as with builtin
objects.
"""
o = object()
self.assertTrue(bool(o))
class MyClass(object):
pass
obj = MyClass()
self.assertTrue(bool(obj))
if __name__ == '__main__':
unittest.main()