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