forked from PythonCharmers/python-future
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_decorators.py
More file actions
57 lines (48 loc) · 1.7 KB
/
test_decorators.py
File metadata and controls
57 lines (48 loc) · 1.7 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
"""
Tests to make sure the decorators (implements_iterator and
python2_unicode_compatible) are working.
"""
from __future__ import absolute_import, division
from future import utils
from future.builtins import *
from future.utils import implements_iterator, python_2_unicode_compatible
from future.tests.base import unittest
class TestDecorators(unittest.TestCase):
def test_python_2_unicode_compatible_decorator(self):
my_unicode_str = u'Unicode string: \u5b54\u5b50'
# With the decorator:
@python_2_unicode_compatible
class A(object):
def __str__(self):
return my_unicode_str
a = A()
assert len(str(a)) == 18
if not utils.PY3:
assert hasattr(a, '__unicode__')
self.assertEqual(str(a), my_unicode_str)
self.assertTrue(isinstance(str(a).encode('utf-8'), bytes))
# 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_iterator(self):
@implements_iterator
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 >= 3:
break
self.assertEqual(item, 'Next!')
if __name__ == '__main__':
unittest.main()