Show More
@@ -1,502 +1,500 b'' | |||
|
1 | 1 | # coding: utf-8 |
|
2 | 2 | """Tests for IPython.lib.pretty.""" |
|
3 | 3 | |
|
4 | 4 | # Copyright (c) IPython Development Team. |
|
5 | 5 | # Distributed under the terms of the Modified BSD License. |
|
6 | 6 | |
|
7 | 7 | |
|
8 | 8 | from collections import Counter, defaultdict, deque, OrderedDict |
|
9 | 9 | import os |
|
10 | 10 | import types |
|
11 | 11 | import string |
|
12 | 12 | import unittest |
|
13 | 13 | |
|
14 | import nose.tools as nt | |
|
15 | 14 | import pytest |
|
16 | 15 | |
|
17 | 16 | from IPython.lib import pretty |
|
18 | 17 | from IPython.testing.decorators import skip_without, skip_iptest_but_not_pytest |
|
19 | 18 | |
|
20 | 19 | from io import StringIO |
|
21 | 20 | |
|
22 | 21 | |
|
23 | 22 | class MyList(object): |
|
24 | 23 | def __init__(self, content): |
|
25 | 24 | self.content = content |
|
26 | 25 | def _repr_pretty_(self, p, cycle): |
|
27 | 26 | if cycle: |
|
28 | 27 | p.text("MyList(...)") |
|
29 | 28 | else: |
|
30 | 29 | with p.group(3, "MyList(", ")"): |
|
31 | 30 | for (i, child) in enumerate(self.content): |
|
32 | 31 | if i: |
|
33 | 32 | p.text(",") |
|
34 | 33 | p.breakable() |
|
35 | 34 | else: |
|
36 | 35 | p.breakable("") |
|
37 | 36 | p.pretty(child) |
|
38 | 37 | |
|
39 | 38 | |
|
40 | 39 | class MyDict(dict): |
|
41 | 40 | def _repr_pretty_(self, p, cycle): |
|
42 | 41 | p.text("MyDict(...)") |
|
43 | 42 | |
|
44 | 43 | class MyObj(object): |
|
45 | 44 | def somemethod(self): |
|
46 | 45 | pass |
|
47 | 46 | |
|
48 | 47 | |
|
49 | 48 | class Dummy1(object): |
|
50 | 49 | def _repr_pretty_(self, p, cycle): |
|
51 | 50 | p.text("Dummy1(...)") |
|
52 | 51 | |
|
53 | 52 | class Dummy2(Dummy1): |
|
54 | 53 | _repr_pretty_ = None |
|
55 | 54 | |
|
56 | 55 | class NoModule(object): |
|
57 | 56 | pass |
|
58 | 57 | |
|
59 | 58 | NoModule.__module__ = None |
|
60 | 59 | |
|
61 | 60 | class Breaking(object): |
|
62 | 61 | def _repr_pretty_(self, p, cycle): |
|
63 | 62 | with p.group(4,"TG: ",":"): |
|
64 | 63 | p.text("Breaking(") |
|
65 | 64 | p.break_() |
|
66 | 65 | p.text(")") |
|
67 | 66 | |
|
68 | 67 | class BreakingRepr(object): |
|
69 | 68 | def __repr__(self): |
|
70 | 69 | return "Breaking(\n)" |
|
71 | 70 | |
|
72 | 71 | class BadRepr(object): |
|
73 | ||
|
74 | 72 | def __repr__(self): |
|
75 | 73 | return 1/0 |
|
76 | 74 | |
|
77 | 75 | |
|
78 | 76 | def test_indentation(): |
|
79 | 77 | """Test correct indentation in groups""" |
|
80 | 78 | count = 40 |
|
81 | 79 | gotoutput = pretty.pretty(MyList(range(count))) |
|
82 | 80 | expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")" |
|
83 | 81 | |
|
84 | 82 | assert gotoutput == expectedoutput |
|
85 | 83 | |
|
86 | 84 | |
|
87 | 85 | def test_dispatch(): |
|
88 | 86 | """ |
|
89 | 87 | Test correct dispatching: The _repr_pretty_ method for MyDict |
|
90 | 88 | must be found before the registered printer for dict. |
|
91 | 89 | """ |
|
92 | 90 | gotoutput = pretty.pretty(MyDict()) |
|
93 | 91 | expectedoutput = "MyDict(...)" |
|
94 | 92 | |
|
95 | 93 | assert gotoutput == expectedoutput |
|
96 | 94 | |
|
97 | 95 | |
|
98 | 96 | def test_callability_checking(): |
|
99 | 97 | """ |
|
100 | 98 | Test that the _repr_pretty_ method is tested for callability and skipped if |
|
101 | 99 | not. |
|
102 | 100 | """ |
|
103 | 101 | gotoutput = pretty.pretty(Dummy2()) |
|
104 | 102 | expectedoutput = "Dummy1(...)" |
|
105 | 103 | |
|
106 | 104 | assert gotoutput == expectedoutput |
|
107 | 105 | |
|
108 | 106 | |
|
109 | 107 | @pytest.mark.parametrize( |
|
110 | 108 | "obj,expected_output", |
|
111 | 109 | zip( |
|
112 | 110 | [ |
|
113 | 111 | set(), |
|
114 | 112 | frozenset(), |
|
115 | 113 | set([1]), |
|
116 | 114 | frozenset([1]), |
|
117 | 115 | set([1, 2]), |
|
118 | 116 | frozenset([1, 2]), |
|
119 | 117 | set([-1, -2, -3]), |
|
120 | 118 | ], |
|
121 | 119 | [ |
|
122 | 120 | "set()", |
|
123 | 121 | "frozenset()", |
|
124 | 122 | "{1}", |
|
125 | 123 | "frozenset({1})", |
|
126 | 124 | "{1, 2}", |
|
127 | 125 | "frozenset({1, 2})", |
|
128 | 126 | "{-3, -2, -1}", |
|
129 | 127 | ], |
|
130 | 128 | ), |
|
131 | 129 | ) |
|
132 | 130 | @skip_iptest_but_not_pytest |
|
133 | 131 | def test_sets(obj, expected_output): |
|
134 | 132 | """ |
|
135 | 133 | Test that set and frozenset use Python 3 formatting. |
|
136 | 134 | """ |
|
137 | 135 | got_output = pretty.pretty(obj) |
|
138 | 136 | assert got_output == expected_output |
|
139 | 137 | |
|
140 | 138 | |
|
141 | 139 | @skip_without('xxlimited') |
|
142 | 140 | def test_pprint_heap_allocated_type(): |
|
143 | 141 | """ |
|
144 | 142 | Test that pprint works for heap allocated types. |
|
145 | 143 | """ |
|
146 | 144 | import xxlimited |
|
147 | 145 | output = pretty.pretty(xxlimited.Null) |
|
148 | 146 | assert output == "xxlimited.Null" |
|
149 | 147 | |
|
150 | 148 | |
|
151 | 149 | def test_pprint_nomod(): |
|
152 | 150 | """ |
|
153 | 151 | Test that pprint works for classes with no __module__. |
|
154 | 152 | """ |
|
155 | 153 | output = pretty.pretty(NoModule) |
|
156 | 154 | assert output == "NoModule" |
|
157 | 155 | |
|
158 | 156 | |
|
159 | 157 | def test_pprint_break(): |
|
160 | 158 | """ |
|
161 | 159 | Test that p.break_ produces expected output |
|
162 | 160 | """ |
|
163 | 161 | output = pretty.pretty(Breaking()) |
|
164 | 162 | expected = "TG: Breaking(\n ):" |
|
165 | 163 | assert output == expected |
|
166 | 164 | |
|
167 | 165 | def test_pprint_break_repr(): |
|
168 | 166 | """ |
|
169 | 167 | Test that p.break_ is used in repr |
|
170 | 168 | """ |
|
171 | 169 | output = pretty.pretty([[BreakingRepr()]]) |
|
172 | 170 | expected = "[[Breaking(\n )]]" |
|
173 | 171 | assert output == expected |
|
174 | 172 | |
|
175 | 173 | output = pretty.pretty([[BreakingRepr()]*2]) |
|
176 | 174 | expected = "[[Breaking(\n ),\n Breaking(\n )]]" |
|
177 | 175 | assert output == expected |
|
178 | 176 | |
|
179 | 177 | def test_bad_repr(): |
|
180 | 178 | """Don't catch bad repr errors""" |
|
181 |
with |
|
|
179 | with pytest.raises(ZeroDivisionError): | |
|
182 | 180 | pretty.pretty(BadRepr()) |
|
183 | 181 | |
|
184 | 182 | class BadException(Exception): |
|
185 | 183 | def __str__(self): |
|
186 | 184 | return -1 |
|
187 | 185 | |
|
188 | 186 | class ReallyBadRepr(object): |
|
189 | 187 | __module__ = 1 |
|
190 | 188 | @property |
|
191 | 189 | def __class__(self): |
|
192 | 190 | raise ValueError("I am horrible") |
|
193 | ||
|
191 | ||
|
194 | 192 | def __repr__(self): |
|
195 | 193 | raise BadException() |
|
196 | 194 | |
|
197 | 195 | def test_really_bad_repr(): |
|
198 |
with |
|
|
196 | with pytest.raises(BadException): | |
|
199 | 197 | pretty.pretty(ReallyBadRepr()) |
|
200 | 198 | |
|
201 | 199 | |
|
202 | 200 | class SA(object): |
|
203 | 201 | pass |
|
204 | 202 | |
|
205 | 203 | class SB(SA): |
|
206 | 204 | pass |
|
207 | 205 | |
|
208 | 206 | class TestsPretty(unittest.TestCase): |
|
209 | 207 | |
|
210 | 208 | def test_super_repr(self): |
|
211 | 209 | # "<super: module_name.SA, None>" |
|
212 | 210 | output = pretty.pretty(super(SA)) |
|
213 | 211 | self.assertRegex(output, r"<super: \S+.SA, None>") |
|
214 | 212 | |
|
215 | 213 | # "<super: module_name.SA, <module_name.SB at 0x...>>" |
|
216 | 214 | sb = SB() |
|
217 | 215 | output = pretty.pretty(super(SA, sb)) |
|
218 | 216 | self.assertRegex(output, r"<super: \S+.SA,\s+<\S+.SB at 0x\S+>>") |
|
219 | 217 | |
|
220 | 218 | |
|
221 | 219 | def test_long_list(self): |
|
222 | 220 | lis = list(range(10000)) |
|
223 | 221 | p = pretty.pretty(lis) |
|
224 | 222 | last2 = p.rsplit('\n', 2)[-2:] |
|
225 | 223 | self.assertEqual(last2, [' 999,', ' ...]']) |
|
226 | 224 | |
|
227 | 225 | def test_long_set(self): |
|
228 | 226 | s = set(range(10000)) |
|
229 | 227 | p = pretty.pretty(s) |
|
230 | 228 | last2 = p.rsplit('\n', 2)[-2:] |
|
231 | 229 | self.assertEqual(last2, [' 999,', ' ...}']) |
|
232 | 230 | |
|
233 | 231 | def test_long_tuple(self): |
|
234 | 232 | tup = tuple(range(10000)) |
|
235 | 233 | p = pretty.pretty(tup) |
|
236 | 234 | last2 = p.rsplit('\n', 2)[-2:] |
|
237 | 235 | self.assertEqual(last2, [' 999,', ' ...)']) |
|
238 | 236 | |
|
239 | 237 | def test_long_dict(self): |
|
240 | 238 | d = { n:n for n in range(10000) } |
|
241 | 239 | p = pretty.pretty(d) |
|
242 | 240 | last2 = p.rsplit('\n', 2)[-2:] |
|
243 | 241 | self.assertEqual(last2, [' 999: 999,', ' ...}']) |
|
244 | 242 | |
|
245 | 243 | def test_unbound_method(self): |
|
246 | 244 | output = pretty.pretty(MyObj.somemethod) |
|
247 | 245 | self.assertIn('MyObj.somemethod', output) |
|
248 | 246 | |
|
249 | 247 | |
|
250 | 248 | class MetaClass(type): |
|
251 | 249 | def __new__(cls, name): |
|
252 | 250 | return type.__new__(cls, name, (object,), {'name': name}) |
|
253 | 251 | |
|
254 | 252 | def __repr__(self): |
|
255 | 253 | return "[CUSTOM REPR FOR CLASS %s]" % self.name |
|
256 | 254 | |
|
257 | 255 | |
|
258 | 256 | ClassWithMeta = MetaClass('ClassWithMeta') |
|
259 | 257 | |
|
260 | 258 | |
|
261 | 259 | def test_metaclass_repr(): |
|
262 | 260 | output = pretty.pretty(ClassWithMeta) |
|
263 | 261 | assert output == "[CUSTOM REPR FOR CLASS ClassWithMeta]" |
|
264 | 262 | |
|
265 | 263 | |
|
266 | 264 | def test_unicode_repr(): |
|
267 | 265 | u = u"üniçodé" |
|
268 | 266 | ustr = u |
|
269 | ||
|
267 | ||
|
270 | 268 | class C(object): |
|
271 | 269 | def __repr__(self): |
|
272 | 270 | return ustr |
|
273 | ||
|
271 | ||
|
274 | 272 | c = C() |
|
275 | 273 | p = pretty.pretty(c) |
|
276 | 274 | assert p == u |
|
277 | 275 | p = pretty.pretty([c]) |
|
278 | 276 | assert p == u"[%s]" % u |
|
279 | 277 | |
|
280 | 278 | |
|
281 | 279 | def test_basic_class(): |
|
282 | 280 | def type_pprint_wrapper(obj, p, cycle): |
|
283 | 281 | if obj is MyObj: |
|
284 | 282 | type_pprint_wrapper.called = True |
|
285 | 283 | return pretty._type_pprint(obj, p, cycle) |
|
286 | 284 | type_pprint_wrapper.called = False |
|
287 | 285 | |
|
288 | 286 | stream = StringIO() |
|
289 | 287 | printer = pretty.RepresentationPrinter(stream) |
|
290 | 288 | printer.type_pprinters[type] = type_pprint_wrapper |
|
291 | 289 | printer.pretty(MyObj) |
|
292 | 290 | printer.flush() |
|
293 | 291 | output = stream.getvalue() |
|
294 | 292 | |
|
295 | 293 | assert output == "%s.MyObj" % __name__ |
|
296 |
|
|
|
294 | assert type_pprint_wrapper.called is True | |
|
297 | 295 | |
|
298 | 296 | |
|
299 | 297 | # TODO : pytest.mark.parametrise once nose is gone. |
|
300 | 298 | def test_collections_defaultdict(): |
|
301 | 299 | # Create defaultdicts with cycles |
|
302 | 300 | a = defaultdict() |
|
303 | 301 | a.default_factory = a |
|
304 | 302 | b = defaultdict(list) |
|
305 | 303 | b['key'] = b |
|
306 | 304 | |
|
307 | 305 | # Dictionary order cannot be relied on, test against single keys. |
|
308 | 306 | cases = [ |
|
309 | 307 | (defaultdict(list), 'defaultdict(list, {})'), |
|
310 | 308 | (defaultdict(list, {'key': '-' * 50}), |
|
311 | 309 | "defaultdict(list,\n" |
|
312 | 310 | " {'key': '--------------------------------------------------'})"), |
|
313 | 311 | (a, 'defaultdict(defaultdict(...), {})'), |
|
314 | 312 | (b, "defaultdict(list, {'key': defaultdict(...)})"), |
|
315 | 313 | ] |
|
316 | 314 | for obj, expected in cases: |
|
317 | 315 | assert pretty.pretty(obj) == expected |
|
318 | 316 | |
|
319 | 317 | |
|
320 | 318 | # TODO : pytest.mark.parametrise once nose is gone. |
|
321 | 319 | def test_collections_ordereddict(): |
|
322 | 320 | # Create OrderedDict with cycle |
|
323 | 321 | a = OrderedDict() |
|
324 | 322 | a['key'] = a |
|
325 | 323 | |
|
326 | 324 | cases = [ |
|
327 | 325 | (OrderedDict(), 'OrderedDict()'), |
|
328 | 326 | (OrderedDict((i, i) for i in range(1000, 1010)), |
|
329 | 327 | 'OrderedDict([(1000, 1000),\n' |
|
330 | 328 | ' (1001, 1001),\n' |
|
331 | 329 | ' (1002, 1002),\n' |
|
332 | 330 | ' (1003, 1003),\n' |
|
333 | 331 | ' (1004, 1004),\n' |
|
334 | 332 | ' (1005, 1005),\n' |
|
335 | 333 | ' (1006, 1006),\n' |
|
336 | 334 | ' (1007, 1007),\n' |
|
337 | 335 | ' (1008, 1008),\n' |
|
338 | 336 | ' (1009, 1009)])'), |
|
339 | 337 | (a, "OrderedDict([('key', OrderedDict(...))])"), |
|
340 | 338 | ] |
|
341 | 339 | for obj, expected in cases: |
|
342 | 340 | assert pretty.pretty(obj) == expected |
|
343 | 341 | |
|
344 | 342 | |
|
345 | 343 | # TODO : pytest.mark.parametrise once nose is gone. |
|
346 | 344 | def test_collections_deque(): |
|
347 | 345 | # Create deque with cycle |
|
348 | 346 | a = deque() |
|
349 | 347 | a.append(a) |
|
350 | 348 | |
|
351 | 349 | cases = [ |
|
352 | 350 | (deque(), 'deque([])'), |
|
353 | 351 | (deque(i for i in range(1000, 1020)), |
|
354 | 352 | 'deque([1000,\n' |
|
355 | 353 | ' 1001,\n' |
|
356 | 354 | ' 1002,\n' |
|
357 | 355 | ' 1003,\n' |
|
358 | 356 | ' 1004,\n' |
|
359 | 357 | ' 1005,\n' |
|
360 | 358 | ' 1006,\n' |
|
361 | 359 | ' 1007,\n' |
|
362 | 360 | ' 1008,\n' |
|
363 | 361 | ' 1009,\n' |
|
364 | 362 | ' 1010,\n' |
|
365 | 363 | ' 1011,\n' |
|
366 | 364 | ' 1012,\n' |
|
367 | 365 | ' 1013,\n' |
|
368 | 366 | ' 1014,\n' |
|
369 | 367 | ' 1015,\n' |
|
370 | 368 | ' 1016,\n' |
|
371 | 369 | ' 1017,\n' |
|
372 | 370 | ' 1018,\n' |
|
373 | 371 | ' 1019])'), |
|
374 | 372 | (a, 'deque([deque(...)])'), |
|
375 | 373 | ] |
|
376 | 374 | for obj, expected in cases: |
|
377 | 375 | assert pretty.pretty(obj) == expected |
|
378 | 376 | |
|
379 | 377 | |
|
380 | 378 | # TODO : pytest.mark.parametrise once nose is gone. |
|
381 | 379 | def test_collections_counter(): |
|
382 | 380 | class MyCounter(Counter): |
|
383 | 381 | pass |
|
384 | 382 | cases = [ |
|
385 | 383 | (Counter(), 'Counter()'), |
|
386 | 384 | (Counter(a=1), "Counter({'a': 1})"), |
|
387 | 385 | (MyCounter(a=1), "MyCounter({'a': 1})"), |
|
388 | 386 | ] |
|
389 | 387 | for obj, expected in cases: |
|
390 | 388 | assert pretty.pretty(obj) == expected |
|
391 | 389 | |
|
392 | 390 | # TODO : pytest.mark.parametrise once nose is gone. |
|
393 | 391 | def test_mappingproxy(): |
|
394 | 392 | MP = types.MappingProxyType |
|
395 | 393 | underlying_dict = {} |
|
396 | 394 | mp_recursive = MP(underlying_dict) |
|
397 | 395 | underlying_dict[2] = mp_recursive |
|
398 | 396 | underlying_dict[3] = underlying_dict |
|
399 | 397 | |
|
400 | 398 | cases = [ |
|
401 | 399 | (MP({}), "mappingproxy({})"), |
|
402 | 400 | (MP({None: MP({})}), "mappingproxy({None: mappingproxy({})})"), |
|
403 | 401 | (MP({k: k.upper() for k in string.ascii_lowercase}), |
|
404 | 402 | "mappingproxy({'a': 'A',\n" |
|
405 | 403 | " 'b': 'B',\n" |
|
406 | 404 | " 'c': 'C',\n" |
|
407 | 405 | " 'd': 'D',\n" |
|
408 | 406 | " 'e': 'E',\n" |
|
409 | 407 | " 'f': 'F',\n" |
|
410 | 408 | " 'g': 'G',\n" |
|
411 | 409 | " 'h': 'H',\n" |
|
412 | 410 | " 'i': 'I',\n" |
|
413 | 411 | " 'j': 'J',\n" |
|
414 | 412 | " 'k': 'K',\n" |
|
415 | 413 | " 'l': 'L',\n" |
|
416 | 414 | " 'm': 'M',\n" |
|
417 | 415 | " 'n': 'N',\n" |
|
418 | 416 | " 'o': 'O',\n" |
|
419 | 417 | " 'p': 'P',\n" |
|
420 | 418 | " 'q': 'Q',\n" |
|
421 | 419 | " 'r': 'R',\n" |
|
422 | 420 | " 's': 'S',\n" |
|
423 | 421 | " 't': 'T',\n" |
|
424 | 422 | " 'u': 'U',\n" |
|
425 | 423 | " 'v': 'V',\n" |
|
426 | 424 | " 'w': 'W',\n" |
|
427 | 425 | " 'x': 'X',\n" |
|
428 | 426 | " 'y': 'Y',\n" |
|
429 | 427 | " 'z': 'Z'})"), |
|
430 | 428 | (mp_recursive, "mappingproxy({2: {...}, 3: {2: {...}, 3: {...}}})"), |
|
431 | 429 | (underlying_dict, |
|
432 | 430 | "{2: mappingproxy({2: {...}, 3: {...}}), 3: {...}}"), |
|
433 | 431 | ] |
|
434 | 432 | for obj, expected in cases: |
|
435 | 433 | assert pretty.pretty(obj) == expected |
|
436 | 434 | |
|
437 | 435 | |
|
438 | 436 | # TODO : pytest.mark.parametrise once nose is gone. |
|
439 | 437 | def test_simplenamespace(): |
|
440 | 438 | SN = types.SimpleNamespace |
|
441 | 439 | |
|
442 | 440 | sn_recursive = SN() |
|
443 | 441 | sn_recursive.first = sn_recursive |
|
444 | 442 | sn_recursive.second = sn_recursive |
|
445 | 443 | cases = [ |
|
446 | 444 | (SN(), "namespace()"), |
|
447 | 445 | (SN(x=SN()), "namespace(x=namespace())"), |
|
448 | 446 | (SN(a_long_name=[SN(s=string.ascii_lowercase)]*3, a_short_name=None), |
|
449 | 447 | "namespace(a_long_name=[namespace(s='abcdefghijklmnopqrstuvwxyz'),\n" |
|
450 | 448 | " namespace(s='abcdefghijklmnopqrstuvwxyz'),\n" |
|
451 | 449 | " namespace(s='abcdefghijklmnopqrstuvwxyz')],\n" |
|
452 | 450 | " a_short_name=None)"), |
|
453 | 451 | (sn_recursive, "namespace(first=namespace(...), second=namespace(...))"), |
|
454 | 452 | ] |
|
455 | 453 | for obj, expected in cases: |
|
456 | 454 | assert pretty.pretty(obj) == expected |
|
457 | 455 | |
|
458 | 456 | |
|
459 | 457 | def test_pretty_environ(): |
|
460 | 458 | dict_repr = pretty.pretty(dict(os.environ)) |
|
461 | 459 | # reindent to align with 'environ' prefix |
|
462 | 460 | dict_indented = dict_repr.replace('\n', '\n' + (' ' * len('environ'))) |
|
463 | 461 | env_repr = pretty.pretty(os.environ) |
|
464 | 462 | assert env_repr == "environ" + dict_indented |
|
465 | 463 | |
|
466 | 464 | |
|
467 | 465 | def test_function_pretty(): |
|
468 | 466 | "Test pretty print of function" |
|
469 | 467 | # posixpath is a pure python module, its interface is consistent |
|
470 | 468 | # across Python distributions |
|
471 | 469 | import posixpath |
|
472 | 470 | |
|
473 | 471 | assert pretty.pretty(posixpath.join) == "<function posixpath.join(a, *p)>" |
|
474 | 472 | |
|
475 | 473 | # custom function |
|
476 | 474 | def meaning_of_life(question=None): |
|
477 | 475 | if question: |
|
478 | 476 | return 42 |
|
479 | 477 | return "Don't panic" |
|
480 | 478 | |
|
481 |
|
|
|
479 | assert "meaning_of_life(question=None)" in pretty.pretty(meaning_of_life) | |
|
482 | 480 | |
|
483 | 481 | |
|
484 | 482 | class OrderedCounter(Counter, OrderedDict): |
|
485 | 483 | 'Counter that remembers the order elements are first encountered' |
|
486 | 484 | |
|
487 | 485 | def __repr__(self): |
|
488 | 486 | return '%s(%r)' % (self.__class__.__name__, OrderedDict(self)) |
|
489 | 487 | |
|
490 | 488 | def __reduce__(self): |
|
491 | 489 | return self.__class__, (OrderedDict(self),) |
|
492 | 490 | |
|
493 | 491 | class MySet(set): # Override repr of a basic type |
|
494 | 492 | def __repr__(self): |
|
495 | 493 | return 'mine' |
|
496 | 494 | |
|
497 | 495 | def test_custom_repr(): |
|
498 | 496 | """A custom repr should override a pretty printer for a parent type""" |
|
499 | 497 | oc = OrderedCounter("abracadabra") |
|
500 |
|
|
|
498 | assert "OrderedCounter(OrderedDict" in pretty.pretty(oc) | |
|
501 | 499 | |
|
502 | 500 | assert pretty.pretty(MySet()) == "mine" |
General Comments 0
You need to be logged in to leave comments.
Login now