forked from robotframework/robotframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_unic.py
More file actions
175 lines (128 loc) · 5.49 KB
/
Copy pathtest_unic.py
File metadata and controls
175 lines (128 loc) · 5.49 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import unittest
import re
from robot.utils import unic, prepr, DotDict
from robot.utils.asserts import assert_equal, assert_true
class TestUnic(unittest.TestCase):
def test_unicode_nfc_and_nfd_decomposition_equality(self):
import unicodedata
text = 'Hyv\xe4'
assert_equal(unic(unicodedata.normalize('NFC', text)), text)
# In Mac filesystem umlaut characters are presented in NFD-format.
# This is to check that unic normalizes all strings to NFC
assert_equal(unic(unicodedata.normalize('NFD', text)), text)
def test_object_containing_unicode_repr(self):
assert_equal(unic(UnicodeRepr()), 'Hyv\xe4')
def test_list_with_objects_containing_unicode_repr(self):
objects = [UnicodeRepr(), UnicodeRepr()]
result = unic(objects)
assert_equal(result, '[Hyv\xe4, Hyv\xe4]')
def test_bytes_below_128(self):
assert_equal(unic('\x00-\x01-\x02-\x7f'), '\x00-\x01-\x02-\x7f')
def test_bytes_above_128(self):
assert_equal(unic(b'hyv\xe4'), 'hyv\\xe4')
assert_equal(unic(b'\x00-\x01-\x02-\xe4'), '\x00-\x01-\x02-\\xe4')
def test_bytes_with_newlines_tabs_etc(self):
assert_equal(unic(b"\x00\xe4\n\t\r\\'"), "\x00\\xe4\n\t\r\\'")
def test_bytearray(self):
assert_equal(unic(bytearray(b'hyv\xe4')), 'hyv\\xe4')
assert_equal(unic(bytearray(b'\x00-\x01-\x02-\xe4')), '\x00-\x01-\x02-\\xe4')
assert_equal(unic(bytearray(b"\x00\xe4\n\t\r\\'")), "\x00\\xe4\n\t\r\\'")
def test_failure_in_str(self):
failing = StrFails()
assert_equal(unic(failing), failing.unrepr)
class TestPrettyRepr(unittest.TestCase):
def _verify(self, item, expected=None, **config):
if not expected:
expected = repr(item).lstrip('')
assert_equal(prepr(item, **config), expected)
if isinstance(item, (str, bytes)) and not config:
assert_equal(prepr([item]), '[%s]' % expected)
assert_equal(prepr((item,)), '(%s,)' % expected)
assert_equal(prepr({item: item}), '{%s: %s}' % (expected, expected))
assert_equal(prepr({item}), '{%s}' % expected)
def test_ascii_unicode(self):
self._verify('foo', "'foo'")
self._verify("f'o'o", "\"f'o'o\"")
def test_non_ascii_unicode(self):
self._verify('hyv\xe4', "'hyv\xe4'")
def test_unicode_in_nfd(self):
self._verify('hyva\u0308', "'hyv\xe4'")
def test_ascii_bytes(self):
self._verify(b'ascii', "b'ascii'")
def test_non_ascii_bytes(self):
self._verify(b'non-\xe4scii', "b'non-\\xe4scii'")
def test_bytearray(self):
self._verify(bytearray(b'foo'), "bytearray(b'foo')")
def test_non_strings(self):
for inp in [1, -2.0, True, None, -2.0, (), [], {}, StrFails()]:
self._verify(inp)
def test_failing_repr(self):
failing = ReprFails()
self._verify(failing, failing.unrepr)
def test_unicode_repr(self):
obj = UnicodeRepr()
self._verify(obj, 'Hyv\xe4')
def test_bytes_repr(self):
obj = BytesRepr()
self._verify(obj, obj.unrepr)
def test_collections(self):
self._verify(['foo', b'bar', 3], "['foo', b'bar', 3]")
self._verify(['foo', b'b\xe4r', ('x', b'y')], "['foo', b'b\\xe4r', ('x', b'y')]")
self._verify({'x': b'\xe4'}, "{'x': b'\\xe4'}")
self._verify(['\xe4'], "['\xe4']")
self._verify({'\xe4'}, "{'\xe4'}")
def test_dotdict(self):
self._verify(DotDict({'x': b'\xe4'}), "{'x': b'\\xe4'}")
def test_recursive(self):
x = [1, 2]
x.append(x)
match = re.match(r'\[1, 2. <Recursion on list with id=\d+>]', prepr(x))
assert_true(match is not None)
def test_split_big_collections(self):
self._verify(list(range(20)))
self._verify(list(range(100)), width=400)
self._verify(list(range(100)),
'[%s]' % ',\n '.join(str(i) for i in range(100)))
self._verify(['Hello, world!'] * 4,
'[%s]' % ', '.join(["'Hello, world!'"] * 4))
self._verify(['Hello, world!'] * 25,
'[%s]' % ', '.join(["'Hello, world!'"] * 25), width=500)
self._verify(['Hello, world!'] * 25,
'[%s]' % ',\n '.join(["'Hello, world!'"] * 25))
def test_dont_split_long_strings(self):
self._verify(' '.join(['Hello world!'] * 1000))
self._verify(b' '.join([b'Hello world!'] * 1000),
"b'%s'" % ' '.join(['Hello world!'] * 1000))
self._verify(bytearray(b' '.join([b'Hello world!'] * 1000)))
class UnRepr:
error = 'This, of course, should never happen...'
@property
def unrepr(self):
return self.format(type(self).__name__, self.error)
@staticmethod
def format(name, error):
return "<Unrepresentable object %s. Error: %s>" % (name, error)
class StrFails(UnRepr):
def __str__(self):
raise RuntimeError(self.error)
class ReprFails(UnRepr):
def __repr__(self):
raise RuntimeError(self.error)
class UnicodeRepr(UnRepr):
def __init__(self):
try:
repr(self)
except UnicodeEncodeError as err:
self.error = 'UnicodeEncodeError: %s' % err
def __repr__(self):
return 'Hyv\xe4'
class BytesRepr(UnRepr):
def __init__(self):
try:
repr(self)
except TypeError as err:
self.error = 'TypeError: %s' % err
def __repr__(self):
return b'Hyv\xe4'
if __name__ == '__main__':
unittest.main()