source: python/trunk/Lib/test/test_pprint.py

Last change on this file was 391, checked in by dmik, 11 years ago

python: Merge vendor 2.7.6 to trunk.

  • Property svn:eol-style set to native
File size: 25.5 KB
Line 
1import pprint
2import test.test_support
3import unittest
4import test.test_set
5
6try:
7 uni = unicode
8except NameError:
9 def uni(x):
10 return x
11
12# list, tuple and dict subclasses that do or don't overwrite __repr__
13class list2(list):
14 pass
15
16class list3(list):
17 def __repr__(self):
18 return list.__repr__(self)
19
20class tuple2(tuple):
21 pass
22
23class tuple3(tuple):
24 def __repr__(self):
25 return tuple.__repr__(self)
26
27class set2(set):
28 pass
29
30class set3(set):
31 def __repr__(self):
32 return set.__repr__(self)
33
34class frozenset2(frozenset):
35 pass
36
37class frozenset3(frozenset):
38 def __repr__(self):
39 return frozenset.__repr__(self)
40
41class dict2(dict):
42 pass
43
44class dict3(dict):
45 def __repr__(self):
46 return dict.__repr__(self)
47
48class QueryTestCase(unittest.TestCase):
49
50 def setUp(self):
51 self.a = range(100)
52 self.b = range(200)
53 self.a[-12] = self.b
54
55 def test_basic(self):
56 # Verify .isrecursive() and .isreadable() w/o recursion
57 pp = pprint.PrettyPrinter()
58 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
59 self.a, self.b):
60 # module-level convenience functions
61 self.assertFalse(pprint.isrecursive(safe),
62 "expected not isrecursive for %r" % (safe,))
63 self.assertTrue(pprint.isreadable(safe),
64 "expected isreadable for %r" % (safe,))
65 # PrettyPrinter methods
66 self.assertFalse(pp.isrecursive(safe),
67 "expected not isrecursive for %r" % (safe,))
68 self.assertTrue(pp.isreadable(safe),
69 "expected isreadable for %r" % (safe,))
70
71 def test_knotted(self):
72 # Verify .isrecursive() and .isreadable() w/ recursion
73 # Tie a knot.
74 self.b[67] = self.a
75 # Messy dict.
76 self.d = {}
77 self.d[0] = self.d[1] = self.d[2] = self.d
78
79 pp = pprint.PrettyPrinter()
80
81 for icky in self.a, self.b, self.d, (self.d, self.d):
82 self.assertTrue(pprint.isrecursive(icky), "expected isrecursive")
83 self.assertFalse(pprint.isreadable(icky), "expected not isreadable")
84 self.assertTrue(pp.isrecursive(icky), "expected isrecursive")
85 self.assertFalse(pp.isreadable(icky), "expected not isreadable")
86
87 # Break the cycles.
88 self.d.clear()
89 del self.a[:]
90 del self.b[:]
91
92 for safe in self.a, self.b, self.d, (self.d, self.d):
93 # module-level convenience functions
94 self.assertFalse(pprint.isrecursive(safe),
95 "expected not isrecursive for %r" % (safe,))
96 self.assertTrue(pprint.isreadable(safe),
97 "expected isreadable for %r" % (safe,))
98 # PrettyPrinter methods
99 self.assertFalse(pp.isrecursive(safe),
100 "expected not isrecursive for %r" % (safe,))
101 self.assertTrue(pp.isreadable(safe),
102 "expected isreadable for %r" % (safe,))
103
104 def test_unreadable(self):
105 # Not recursive but not readable anyway
106 pp = pprint.PrettyPrinter()
107 for unreadable in type(3), pprint, pprint.isrecursive:
108 # module-level convenience functions
109 self.assertFalse(pprint.isrecursive(unreadable),
110 "expected not isrecursive for %r" % (unreadable,))
111 self.assertFalse(pprint.isreadable(unreadable),
112 "expected not isreadable for %r" % (unreadable,))
113 # PrettyPrinter methods
114 self.assertFalse(pp.isrecursive(unreadable),
115 "expected not isrecursive for %r" % (unreadable,))
116 self.assertFalse(pp.isreadable(unreadable),
117 "expected not isreadable for %r" % (unreadable,))
118
119 def test_same_as_repr(self):
120 # Simple objects, small containers and classes that overwrite __repr__
121 # For those the result should be the same as repr().
122 # Ahem. The docs don't say anything about that -- this appears to
123 # be testing an implementation quirk. Starting in Python 2.5, it's
124 # not true for dicts: pprint always sorts dicts by key now; before,
125 # it sorted a dict display if and only if the display required
126 # multiple lines. For that reason, dicts with more than one element
127 # aren't tested here.
128 for simple in (0, 0L, 0+0j, 0.0, "", uni(""),
129 (), tuple2(), tuple3(),
130 [], list2(), list3(),
131 set(), set2(), set3(),
132 frozenset(), frozenset2(), frozenset3(),
133 {}, dict2(), dict3(),
134 self.assertTrue, pprint,
135 -6, -6L, -6-6j, -1.5, "x", uni("x"), (3,), [3], {3: 6},
136 (1,2), [3,4], {5: 6},
137 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
138 [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
139 set({7}), set2({7}), set3({7}),
140 frozenset({8}), frozenset2({8}), frozenset3({8}),
141 dict2({5: 6}), dict3({5: 6}),
142 range(10, -11, -1)
143 ):
144 native = repr(simple)
145 self.assertEqual(pprint.pformat(simple), native)
146 self.assertEqual(pprint.pformat(simple, width=1, indent=0)
147 .replace('\n', ' '), native)
148 self.assertEqual(pprint.saferepr(simple), native)
149
150 def test_basic_line_wrap(self):
151 # verify basic line-wrapping operation
152 o = {'RPM_cal': 0,
153 'RPM_cal2': 48059,
154 'Speed_cal': 0,
155 'controldesk_runtime_us': 0,
156 'main_code_runtime_us': 0,
157 'read_io_runtime_us': 0,
158 'write_io_runtime_us': 43690}
159 exp = """\
160{'RPM_cal': 0,
161 'RPM_cal2': 48059,
162 'Speed_cal': 0,
163 'controldesk_runtime_us': 0,
164 'main_code_runtime_us': 0,
165 'read_io_runtime_us': 0,
166 'write_io_runtime_us': 43690}"""
167 for type in [dict, dict2]:
168 self.assertEqual(pprint.pformat(type(o)), exp)
169
170 o = range(100)
171 exp = '[%s]' % ',\n '.join(map(str, o))
172 for type in [list, list2]:
173 self.assertEqual(pprint.pformat(type(o)), exp)
174
175 o = tuple(range(100))
176 exp = '(%s)' % ',\n '.join(map(str, o))
177 for type in [tuple, tuple2]:
178 self.assertEqual(pprint.pformat(type(o)), exp)
179
180 # indent parameter
181 o = range(100)
182 exp = '[ %s]' % ',\n '.join(map(str, o))
183 for type in [list, list2]:
184 self.assertEqual(pprint.pformat(type(o), indent=4), exp)
185
186 def test_nested_indentations(self):
187 o1 = list(range(10))
188 o2 = dict(first=1, second=2, third=3)
189 o = [o1, o2]
190 expected = """\
191[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
192 { 'first': 1,
193 'second': 2,
194 'third': 3}]"""
195 self.assertEqual(pprint.pformat(o, indent=4, width=42), expected)
196
197 def test_sorted_dict(self):
198 # Starting in Python 2.5, pprint sorts dict displays by key regardless
199 # of how small the dictionary may be.
200 # Before the change, on 32-bit Windows pformat() gave order
201 # 'a', 'c', 'b' here, so this test failed.
202 d = {'a': 1, 'b': 1, 'c': 1}
203 self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}")
204 self.assertEqual(pprint.pformat([d, d]),
205 "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]")
206
207 # The next one is kind of goofy. The sorted order depends on the
208 # alphabetic order of type names: "int" < "str" < "tuple". Before
209 # Python 2.5, this was in the test_same_as_repr() test. It's worth
210 # keeping around for now because it's one of few tests of pprint
211 # against a crazy mix of types.
212 self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}),
213 r"{5: [[]], 'xy\tab\n': (3,), (): {}}")
214
215 def test_subclassing(self):
216 o = {'names with spaces': 'should be presented using repr()',
217 'others.should.not.be': 'like.this'}
218 exp = """\
219{'names with spaces': 'should be presented using repr()',
220 others.should.not.be: like.this}"""
221 self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
222
223 def test_set_reprs(self):
224 self.assertEqual(pprint.pformat(set()), 'set([])')
225 self.assertEqual(pprint.pformat(set(range(3))), 'set([0, 1, 2])')
226 self.assertEqual(pprint.pformat(set(range(7)), width=20), '''\
227set([0,
228 1,
229 2,
230 3,
231 4,
232 5,
233 6])''')
234 self.assertEqual(pprint.pformat(set2(range(7)), width=20), '''\
235set2([0,
236 1,
237 2,
238 3,
239 4,
240 5,
241 6])''')
242 self.assertEqual(pprint.pformat(set3(range(7)), width=20),
243 'set3([0, 1, 2, 3, 4, 5, 6])')
244
245 self.assertEqual(pprint.pformat(frozenset()), 'frozenset([])')
246 self.assertEqual(pprint.pformat(frozenset(range(3))),
247 'frozenset([0, 1, 2])')
248 self.assertEqual(pprint.pformat(frozenset(range(7)), width=20), '''\
249frozenset([0,
250 1,
251 2,
252 3,
253 4,
254 5,
255 6])''')
256 self.assertEqual(pprint.pformat(frozenset2(range(7)), width=20), '''\
257frozenset2([0,
258 1,
259 2,
260 3,
261 4,
262 5,
263 6])''')
264 self.assertEqual(pprint.pformat(frozenset3(range(7)), width=20),
265 'frozenset3([0, 1, 2, 3, 4, 5, 6])')
266
267 def test_set_of_sets_reprs(self):
268 cube_repr_tgt = """\
269{frozenset([]): frozenset([frozenset([2]), frozenset([0]), frozenset([1])]),
270 frozenset([0]): frozenset([frozenset([]),
271 frozenset([0, 2]),
272 frozenset([0, 1])]),
273 frozenset([1]): frozenset([frozenset([]),
274 frozenset([1, 2]),
275 frozenset([0, 1])]),
276 frozenset([2]): frozenset([frozenset([]),
277 frozenset([1, 2]),
278 frozenset([0, 2])]),
279 frozenset([1, 2]): frozenset([frozenset([2]),
280 frozenset([1]),
281 frozenset([0, 1, 2])]),
282 frozenset([0, 2]): frozenset([frozenset([2]),
283 frozenset([0]),
284 frozenset([0, 1, 2])]),
285 frozenset([0, 1]): frozenset([frozenset([0]),
286 frozenset([1]),
287 frozenset([0, 1, 2])]),
288 frozenset([0, 1, 2]): frozenset([frozenset([1, 2]),
289 frozenset([0, 2]),
290 frozenset([0, 1])])}"""
291 cube = test.test_set.cube(3)
292 self.assertEqual(pprint.pformat(cube), cube_repr_tgt)
293 cubo_repr_tgt = """\
294{frozenset([frozenset([0, 2]), frozenset([0])]): frozenset([frozenset([frozenset([0,
295 2]),
296 frozenset([0,
297 1,
298 2])]),
299 frozenset([frozenset([0]),
300 frozenset([0,
301 1])]),
302 frozenset([frozenset([]),
303 frozenset([0])]),
304 frozenset([frozenset([2]),
305 frozenset([0,
306 2])])]),
307 frozenset([frozenset([0, 1]), frozenset([1])]): frozenset([frozenset([frozenset([0,
308 1]),
309 frozenset([0,
310 1,
311 2])]),
312 frozenset([frozenset([0]),
313 frozenset([0,
314 1])]),
315 frozenset([frozenset([1]),
316 frozenset([1,
317 2])]),
318 frozenset([frozenset([]),
319 frozenset([1])])]),
320 frozenset([frozenset([1, 2]), frozenset([1])]): frozenset([frozenset([frozenset([1,
321 2]),
322 frozenset([0,
323 1,
324 2])]),
325 frozenset([frozenset([2]),
326 frozenset([1,
327 2])]),
328 frozenset([frozenset([]),
329 frozenset([1])]),
330 frozenset([frozenset([1]),
331 frozenset([0,
332 1])])]),
333 frozenset([frozenset([1, 2]), frozenset([2])]): frozenset([frozenset([frozenset([1,
334 2]),
335 frozenset([0,
336 1,
337 2])]),
338 frozenset([frozenset([1]),
339 frozenset([1,
340 2])]),
341 frozenset([frozenset([2]),
342 frozenset([0,
343 2])]),
344 frozenset([frozenset([]),
345 frozenset([2])])]),
346 frozenset([frozenset([]), frozenset([0])]): frozenset([frozenset([frozenset([0]),
347 frozenset([0,
348 1])]),
349 frozenset([frozenset([0]),
350 frozenset([0,
351 2])]),
352 frozenset([frozenset([]),
353 frozenset([1])]),
354 frozenset([frozenset([]),
355 frozenset([2])])]),
356 frozenset([frozenset([]), frozenset([1])]): frozenset([frozenset([frozenset([]),
357 frozenset([0])]),
358 frozenset([frozenset([1]),
359 frozenset([1,
360 2])]),
361 frozenset([frozenset([]),
362 frozenset([2])]),
363 frozenset([frozenset([1]),
364 frozenset([0,
365 1])])]),
366 frozenset([frozenset([2]), frozenset([])]): frozenset([frozenset([frozenset([2]),
367 frozenset([1,
368 2])]),
369 frozenset([frozenset([]),
370 frozenset([0])]),
371 frozenset([frozenset([]),
372 frozenset([1])]),
373 frozenset([frozenset([2]),
374 frozenset([0,
375 2])])]),
376 frozenset([frozenset([0, 1, 2]), frozenset([0, 1])]): frozenset([frozenset([frozenset([1,
377 2]),
378 frozenset([0,
379 1,
380 2])]),
381 frozenset([frozenset([0,
382 2]),
383 frozenset([0,
384 1,
385 2])]),
386 frozenset([frozenset([0]),
387 frozenset([0,
388 1])]),
389 frozenset([frozenset([1]),
390 frozenset([0,
391 1])])]),
392 frozenset([frozenset([0]), frozenset([0, 1])]): frozenset([frozenset([frozenset([]),
393 frozenset([0])]),
394 frozenset([frozenset([0,
395 1]),
396 frozenset([0,
397 1,
398 2])]),
399 frozenset([frozenset([0]),
400 frozenset([0,
401 2])]),
402 frozenset([frozenset([1]),
403 frozenset([0,
404 1])])]),
405 frozenset([frozenset([2]), frozenset([0, 2])]): frozenset([frozenset([frozenset([0,
406 2]),
407 frozenset([0,
408 1,
409 2])]),
410 frozenset([frozenset([2]),
411 frozenset([1,
412 2])]),
413 frozenset([frozenset([0]),
414 frozenset([0,
415 2])]),
416 frozenset([frozenset([]),
417 frozenset([2])])]),
418 frozenset([frozenset([0, 1, 2]), frozenset([0, 2])]): frozenset([frozenset([frozenset([1,
419 2]),
420 frozenset([0,
421 1,
422 2])]),
423 frozenset([frozenset([0,
424 1]),
425 frozenset([0,
426 1,
427 2])]),
428 frozenset([frozenset([0]),
429 frozenset([0,
430 2])]),
431 frozenset([frozenset([2]),
432 frozenset([0,
433 2])])]),
434 frozenset([frozenset([1, 2]), frozenset([0, 1, 2])]): frozenset([frozenset([frozenset([0,
435 2]),
436 frozenset([0,
437 1,
438 2])]),
439 frozenset([frozenset([0,
440 1]),
441 frozenset([0,
442 1,
443 2])]),
444 frozenset([frozenset([2]),
445 frozenset([1,
446 2])]),
447 frozenset([frozenset([1]),
448 frozenset([1,
449 2])])])}"""
450
451 cubo = test.test_set.linegraph(cube)
452 self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt)
453
454 def test_depth(self):
455 nested_tuple = (1, (2, (3, (4, (5, 6)))))
456 nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}}
457 nested_list = [1, [2, [3, [4, [5, [6, []]]]]]]
458 self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple))
459 self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict))
460 self.assertEqual(pprint.pformat(nested_list), repr(nested_list))
461
462 lv1_tuple = '(1, (...))'
463 lv1_dict = '{1: {...}}'
464 lv1_list = '[1, [...]]'
465 self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple)
466 self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict)
467 self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list)
468
469
470class DottedPrettyPrinter(pprint.PrettyPrinter):
471
472 def format(self, object, context, maxlevels, level):
473 if isinstance(object, str):
474 if ' ' in object:
475 return repr(object), 1, 0
476 else:
477 return object, 0, 0
478 else:
479 return pprint.PrettyPrinter.format(
480 self, object, context, maxlevels, level)
481
482
483def test_main():
484 test.test_support.run_unittest(QueryTestCase)
485
486
487if __name__ == "__main__":
488 test_main()
Note: See TracBrowser for help on using the repository browser.