forked from samuelcolvin/python-devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_prettier.py
More file actions
364 lines (293 loc) · 7.15 KB
/
Copy pathtest_prettier.py
File metadata and controls
364 lines (293 loc) · 7.15 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import os
import string
import sys
from collections import OrderedDict, namedtuple
from unittest.mock import MagicMock
import pytest
from devtools.ansi import strip_ansi
from devtools.prettier import PrettyFormat, pformat, pprint
try:
import numpy
except ImportError:
numpy = None
try:
from multidict import CIMultiDict, MultiDict
except ImportError:
CIMultiDict = None
MultiDict = None
try:
from asyncpg.protocol.protocol import _create_record as Record
except ImportError:
Record = None
def test_dict():
v = pformat({1: 2, 3: 4})
print(v)
assert v == (
'{\n'
' 1: 2,\n'
' 3: 4,\n'
'}')
def test_print(capsys):
pprint({1: 2, 3: 4})
stdout, stderr = capsys.readouterr()
assert strip_ansi(stdout) == (
'{\n'
' 1: 2,\n'
' 3: 4,\n'
'}\n')
assert stderr == ''
def test_colours():
v = pformat({1: 2, 3: 4}, highlight=True)
assert v.startswith('\x1b'), repr(v)
v2 = strip_ansi(v)
assert v2 == pformat({1: 2, 3: 4}), repr(v2)
def test_list():
v = pformat(list(range(6)))
assert v == (
'[\n'
' 0,\n'
' 1,\n'
' 2,\n'
' 3,\n'
' 4,\n'
' 5,\n'
']')
def test_set():
v = pformat(set(range(5)))
assert v == (
'{\n'
' 0,\n'
' 1,\n'
' 2,\n'
' 3,\n'
' 4,\n'
'}')
def test_tuple():
v = pformat(tuple(range(5)))
assert v == (
'(\n'
' 0,\n'
' 1,\n'
' 2,\n'
' 3,\n'
' 4,\n'
')')
def test_generator():
v = pformat((i for i in range(3)))
assert v == (
'(\n'
' 0,\n'
' 1,\n'
' 2,\n'
')')
def test_named_tuple():
f = namedtuple('Foobar', ['foo', 'bar', 'spam'])
v = pformat(f('x', 'y', 1))
assert v == ("Foobar(\n"
" foo='x',\n"
" bar='y',\n"
" spam=1,\n"
")")
def test_generator_no_yield():
pformat_ = PrettyFormat(yield_from_generators=False)
v = pformat_((i for i in range(3)))
assert v.startswith('<generator object test_generator_no_yield.<locals>.<genexpr> at ')
def test_str():
pformat_ = PrettyFormat(width=12)
v = pformat_(string.ascii_lowercase + '\n' + string.digits)
print(repr(v))
assert v == (
"(\n"
" 'abcde'\n"
" 'fghij'\n"
" 'klmno'\n"
" 'pqrst'\n"
" 'uvwxy'\n"
" 'z\\n"
"'\n"
" '01234'\n"
" '56789'\n"
")"
)
def test_str_repr():
pformat_ = PrettyFormat(repr_strings=True)
v = pformat_(string.ascii_lowercase + '\n' + string.digits)
assert v == "'abcdefghijklmnopqrstuvwxyz\\n0123456789'"
def test_bytes():
pformat_ = PrettyFormat(width=12)
v = pformat_(string.ascii_lowercase.encode())
assert v == """(
b'abcde'
b'fghij'
b'klmno'
b'pqrst'
b'uvwxy'
b'z'
)"""
def test_short_bytes():
assert "b'abcdefghijklmnopqrstuvwxyz'" == pformat(string.ascii_lowercase.encode())
@pytest.mark.skipif(numpy is None, reason='numpy not installed')
def test_indent_numpy():
v = pformat({'numpy test': numpy.array(range(20))})
assert v == """{
'numpy test': (
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])
),
}"""
@pytest.mark.skipif(numpy is None, reason='numpy not installed')
def test_indent_numpy_short():
v = pformat({'numpy test': numpy.array(range(10))})
assert v == """{
'numpy test': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
}"""
def test_ordered_dict():
v = pformat(OrderedDict([(1, 2), (3, 4), (5, 6)]))
print(v)
assert v == """\
OrderedDict([
(1, 2),
(3, 4),
(5, 6),
])"""
def test_frozenset():
v = pformat(frozenset(range(3)))
print(v)
assert v == """\
frozenset({
0,
1,
2,
})"""
def test_deep_objects():
f = namedtuple('Foobar', ['foo', 'bar', 'spam'])
v = pformat((
(
f('x', 'y', OrderedDict([(1, 2), (3, 4), (5, 6)])),
frozenset(range(3)),
[1, 2, {1: 2}]
),
{1, 2, 3}
))
print(v)
assert v == """\
(
(
Foobar(
foo='x',
bar='y',
spam=OrderedDict([
(1, 2),
(3, 4),
(5, 6),
]),
),
frozenset({
0,
1,
2,
}),
[
1,
2,
{1: 2},
],
),
{1, 2, 3},
)"""
@pytest.mark.skipif(sys.version_info > (3, 5, 3), reason='like this only for old 3.5')
def test_call_args_py353():
m = MagicMock()
m(1, 2, 3, a=4)
v = pformat(m.call_args)
assert v == """\
_Call(
(1, 2, 3),
{'a': 4},
)"""
@pytest.mark.skipif(sys.version_info <= (3, 5, 3), reason='different for old 3.5')
def test_call_args_py354():
m = MagicMock()
m(1, 2, 3, a=4)
v = pformat(m.call_args)
assert v == """\
_Call(
_fields=(1, 2, 3),
{'a': 4},
)"""
@pytest.mark.skipif(MultiDict is None, reason='MultiDict not installed')
def test_multidict():
d = MultiDict({'a': 1, 'b': 2})
d.add('b', 3)
v = pformat(d)
assert set(v.split('\n')) == {
"<MultiDict({",
" 'a': 1,",
" 'b': 2,",
" 'b': 3,",
"})>",
}
@pytest.mark.skipif(CIMultiDict is None, reason='MultiDict not installed')
def test_cimultidict():
v = pformat(CIMultiDict({'a': 1, 'b': 2}))
assert set(v.split('\n')) == {
"<CIMultiDict({",
" 'a': 1,",
" 'b': 2,",
"})>",
}
def test_os_environ():
v = pformat(os.environ)
assert v.startswith('<_Environ({')
assert " 'HOME': '" in v
class Foo:
a = 1
def __init__(self):
self.b = 2
self.c = 3
def test_dir():
assert pformat(vars(Foo())) == (
"{\n"
" 'b': 2,\n"
" 'c': 3,\n"
"}"
)
def test_instance_dict():
assert pformat(Foo().__dict__) == (
"{\n"
" 'b': 2,\n"
" 'c': 3,\n"
"}"
)
def test_class_dict():
s = pformat(Foo.__dict__)
assert s.startswith('<mappingproxy({\n')
assert " '__module__': 'tests.test_prettier',\n" in s
assert " 'a': 1,\n" in s
assert s.endswith('})>')
def test_dictlike():
class Dictlike:
_d = {'x': 4, 'y': 42, 3: None}
def items(self):
yield from self._d.items()
def __getitem__(self, item):
return self._d[item]
assert pformat(Dictlike()) == (
"<Dictlike({\n"
" 'x': 4,\n"
" 'y': 42,\n"
" 3: None,\n"
"})>"
)
@pytest.mark.skipif(Record is None, reason='asyncpg not installed')
def test_asyncpg_record():
r = Record({'a': 0, 'b': 1}, (41, 42))
assert dict(r) == {'a': 41, 'b': 42}
assert pformat(r) == (
"<Record({\n"
" 'a': 41,\n"
" 'b': 42,\n"
"})>"
)
def test_dict_type():
assert pformat(type({1: 2})) == "<class 'dict'>"