blob: 3d364c4595747380e50bf801d981d9438f27587c [file] [log] [blame]
Antoine Pitrou64c16c32013-03-23 20:30:39 +01001# -*- coding: utf-8 -*-
2
Tim Petersa814db52001-05-14 07:05:58 +00003import pprint
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004import test.support
Fred Drake43913dd2001-05-14 17:41:20 +00005import unittest
Christian Heimes969fe572008-01-25 11:23:10 +00006import test.test_set
Raymond Hettingera7da1662009-11-19 01:07:05 +00007import random
Raymond Hettingerbad3c882010-09-09 12:31:00 +00008import collections
9import itertools
Tim Petersa814db52001-05-14 07:05:58 +000010
Walter Dörwald7a7ede52003-12-03 20:15:28 +000011# list, tuple and dict subclasses that do or don't overwrite __repr__
12class list2(list):
13 pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000014
Walter Dörwald7a7ede52003-12-03 20:15:28 +000015class list3(list):
16 def __repr__(self):
17 return list.__repr__(self)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000018
Walter Dörwald7a7ede52003-12-03 20:15:28 +000019class tuple2(tuple):
20 pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000021
Walter Dörwald7a7ede52003-12-03 20:15:28 +000022class tuple3(tuple):
23 def __repr__(self):
24 return tuple.__repr__(self)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000025
Serhiy Storchaka51844382013-10-02 11:40:49 +030026class set2(set):
27 pass
28
29class set3(set):
30 def __repr__(self):
31 return set.__repr__(self)
32
33class frozenset2(frozenset):
34 pass
35
36class frozenset3(frozenset):
37 def __repr__(self):
38 return frozenset.__repr__(self)
39
Walter Dörwald7a7ede52003-12-03 20:15:28 +000040class dict2(dict):
41 pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000042
Walter Dörwald7a7ede52003-12-03 20:15:28 +000043class dict3(dict):
44 def __repr__(self):
45 return dict.__repr__(self)
Tim Petersa814db52001-05-14 07:05:58 +000046
Raymond Hettingera7da1662009-11-19 01:07:05 +000047class Unorderable:
48 def __repr__(self):
49 return str(id(self))
50
Fred Drake43913dd2001-05-14 17:41:20 +000051class QueryTestCase(unittest.TestCase):
Tim Petersa814db52001-05-14 07:05:58 +000052
Fred Drake43913dd2001-05-14 17:41:20 +000053 def setUp(self):
Guido van Rossum805365e2007-05-07 22:24:25 +000054 self.a = list(range(100))
55 self.b = list(range(200))
Fred Drake43913dd2001-05-14 17:41:20 +000056 self.a[-12] = self.b
Tim Petersa814db52001-05-14 07:05:58 +000057
Fred Drake43913dd2001-05-14 17:41:20 +000058 def test_basic(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000059 # Verify .isrecursive() and .isreadable() w/o recursion
Fred Drakeb456e4f2002-12-31 07:16:16 +000060 pp = pprint.PrettyPrinter()
Walter Dörwald5de48bd2007-06-11 21:38:39 +000061 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, "yaddayadda",
Fred Drake43913dd2001-05-14 17:41:20 +000062 self.a, self.b):
Fred Drakeb456e4f2002-12-31 07:16:16 +000063 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +000064 self.assertFalse(pprint.isrecursive(safe),
65 "expected not isrecursive for %r" % (safe,))
66 self.assertTrue(pprint.isreadable(safe),
67 "expected isreadable for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +000068 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +000069 self.assertFalse(pp.isrecursive(safe),
70 "expected not isrecursive for %r" % (safe,))
71 self.assertTrue(pp.isreadable(safe),
72 "expected isreadable for %r" % (safe,))
Tim Petersa814db52001-05-14 07:05:58 +000073
Fred Drake43913dd2001-05-14 17:41:20 +000074 def test_knotted(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000075 # Verify .isrecursive() and .isreadable() w/ recursion
Fred Drake43913dd2001-05-14 17:41:20 +000076 # Tie a knot.
77 self.b[67] = self.a
78 # Messy dict.
79 self.d = {}
80 self.d[0] = self.d[1] = self.d[2] = self.d
Tim Petersa814db52001-05-14 07:05:58 +000081
Fred Drakeb456e4f2002-12-31 07:16:16 +000082 pp = pprint.PrettyPrinter()
Tim Petersa814db52001-05-14 07:05:58 +000083
Fred Drake43913dd2001-05-14 17:41:20 +000084 for icky in self.a, self.b, self.d, (self.d, self.d):
Ezio Melottib19f43d2010-01-24 20:59:24 +000085 self.assertTrue(pprint.isrecursive(icky), "expected isrecursive")
86 self.assertFalse(pprint.isreadable(icky), "expected not isreadable")
87 self.assertTrue(pp.isrecursive(icky), "expected isrecursive")
88 self.assertFalse(pp.isreadable(icky), "expected not isreadable")
Fred Drake43913dd2001-05-14 17:41:20 +000089
90 # Break the cycles.
91 self.d.clear()
92 del self.a[:]
93 del self.b[:]
94
95 for safe in self.a, self.b, self.d, (self.d, self.d):
Fred Drakeb456e4f2002-12-31 07:16:16 +000096 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +000097 self.assertFalse(pprint.isrecursive(safe),
98 "expected not isrecursive for %r" % (safe,))
99 self.assertTrue(pprint.isreadable(safe),
100 "expected isreadable for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +0000101 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +0000102 self.assertFalse(pp.isrecursive(safe),
103 "expected not isrecursive for %r" % (safe,))
104 self.assertTrue(pp.isreadable(safe),
105 "expected isreadable for %r" % (safe,))
Fred Drake43913dd2001-05-14 17:41:20 +0000106
107 def test_unreadable(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000108 # Not recursive but not readable anyway
Fred Drakeb456e4f2002-12-31 07:16:16 +0000109 pp = pprint.PrettyPrinter()
Fred Drake43913dd2001-05-14 17:41:20 +0000110 for unreadable in type(3), pprint, pprint.isrecursive:
Fred Drakeb456e4f2002-12-31 07:16:16 +0000111 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +0000112 self.assertFalse(pprint.isrecursive(unreadable),
113 "expected not isrecursive for %r" % (unreadable,))
114 self.assertFalse(pprint.isreadable(unreadable),
115 "expected not isreadable for %r" % (unreadable,))
Fred Drakeb456e4f2002-12-31 07:16:16 +0000116 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +0000117 self.assertFalse(pp.isrecursive(unreadable),
118 "expected not isrecursive for %r" % (unreadable,))
119 self.assertFalse(pp.isreadable(unreadable),
120 "expected not isreadable for %r" % (unreadable,))
Fred Drake43913dd2001-05-14 17:41:20 +0000121
Tim Peters95b3f782001-05-14 18:39:41 +0000122 def test_same_as_repr(self):
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000123 # Simple objects, small containers and classes that overwrite __repr__
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000124 # For those the result should be the same as repr().
125 # Ahem. The docs don't say anything about that -- this appears to
126 # be testing an implementation quirk. Starting in Python 2.5, it's
127 # not true for dicts: pprint always sorts dicts by key now; before,
128 # it sorted a dict display if and only if the display required
129 # multiple lines. For that reason, dicts with more than one element
130 # aren't tested here.
Walter Dörwald5de48bd2007-06-11 21:38:39 +0000131 for simple in (0, 0, 0+0j, 0.0, "", b"",
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000132 (), tuple2(), tuple3(),
133 [], list2(), list3(),
Serhiy Storchaka51844382013-10-02 11:40:49 +0300134 set(), set2(), set3(),
135 frozenset(), frozenset2(), frozenset3(),
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000136 {}, dict2(), dict3(),
Ezio Melottib19f43d2010-01-24 20:59:24 +0000137 self.assertTrue, pprint,
Walter Dörwald5de48bd2007-06-11 21:38:39 +0000138 -6, -6, -6-6j, -1.5, "x", b"x", (3,), [3], {3: 6},
Benjamin Petersond23f8222009-04-05 19:13:16 +0000139 (1,2), [3,4], {5: 6},
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000140 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
141 [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
Serhiy Storchaka51844382013-10-02 11:40:49 +0300142 set({7}), set2({7}), set3({7}),
143 frozenset({8}), frozenset2({8}), frozenset3({8}),
Benjamin Petersond23f8222009-04-05 19:13:16 +0000144 dict2({5: 6}), dict3({5: 6}),
Tim Peters95b3f782001-05-14 18:39:41 +0000145 range(10, -11, -1)
146 ):
147 native = repr(simple)
Serhiy Storchaka51844382013-10-02 11:40:49 +0300148 self.assertEqual(pprint.pformat(simple), native)
149 self.assertEqual(pprint.pformat(simple, width=1, indent=0)
150 .replace('\n', ' '), native)
151 self.assertEqual(pprint.saferepr(simple), native)
Fred Drake43913dd2001-05-14 17:41:20 +0000152
Barry Warsaw00859c02001-11-28 05:49:39 +0000153 def test_basic_line_wrap(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000154 # verify basic line-wrapping operation
Barry Warsaw00859c02001-11-28 05:49:39 +0000155 o = {'RPM_cal': 0,
156 'RPM_cal2': 48059,
157 'Speed_cal': 0,
158 'controldesk_runtime_us': 0,
159 'main_code_runtime_us': 0,
160 'read_io_runtime_us': 0,
161 'write_io_runtime_us': 43690}
162 exp = """\
163{'RPM_cal': 0,
164 'RPM_cal2': 48059,
165 'Speed_cal': 0,
166 'controldesk_runtime_us': 0,
167 'main_code_runtime_us': 0,
168 'read_io_runtime_us': 0,
169 'write_io_runtime_us': 43690}"""
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000170 for type in [dict, dict2]:
171 self.assertEqual(pprint.pformat(type(o)), exp)
172
173 o = range(100)
174 exp = '[%s]' % ',\n '.join(map(str, o))
175 for type in [list, list2]:
176 self.assertEqual(pprint.pformat(type(o)), exp)
177
178 o = tuple(range(100))
179 exp = '(%s)' % ',\n '.join(map(str, o))
180 for type in [tuple, tuple2]:
181 self.assertEqual(pprint.pformat(type(o)), exp)
Barry Warsaw00859c02001-11-28 05:49:39 +0000182
Walter Dörwaldc8de4582003-12-03 20:26:05 +0000183 # indent parameter
184 o = range(100)
185 exp = '[ %s]' % ',\n '.join(map(str, o))
186 for type in [list, list2]:
187 self.assertEqual(pprint.pformat(type(o), indent=4), exp)
188
Georg Brandl3ccb7872008-07-16 03:00:45 +0000189 def test_nested_indentations(self):
190 o1 = list(range(10))
191 o2 = dict(first=1, second=2, third=3)
192 o = [o1, o2]
193 expected = """\
194[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
195 { 'first': 1,
196 'second': 2,
197 'third': 3}]"""
198 self.assertEqual(pprint.pformat(o, indent=4, width=42), expected)
199
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000200 def test_sorted_dict(self):
201 # Starting in Python 2.5, pprint sorts dict displays by key regardless
202 # of how small the dictionary may be.
203 # Before the change, on 32-bit Windows pformat() gave order
204 # 'a', 'c', 'b' here, so this test failed.
205 d = {'a': 1, 'b': 1, 'c': 1}
206 self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}")
207 self.assertEqual(pprint.pformat([d, d]),
208 "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]")
209
210 # The next one is kind of goofy. The sorted order depends on the
211 # alphabetic order of type names: "int" < "str" < "tuple". Before
212 # Python 2.5, this was in the test_same_as_repr() test. It's worth
213 # keeping around for now because it's one of few tests of pprint
214 # against a crazy mix of types.
215 self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}),
216 r"{5: [[]], 'xy\tab\n': (3,), (): {}}")
217
Raymond Hettingerbad3c882010-09-09 12:31:00 +0000218 def test_ordered_dict(self):
219 words = 'the quick brown fox jumped over a lazy dog'.split()
220 d = collections.OrderedDict(zip(words, itertools.count()))
221 self.assertEqual(pprint.pformat(d),
222"""\
223{'the': 0,
224 'quick': 1,
225 'brown': 2,
226 'fox': 3,
227 'jumped': 4,
228 'over': 5,
229 'a': 6,
230 'lazy': 7,
231 'dog': 8}""")
Fred Drakeaee113d2002-04-02 05:08:35 +0000232 def test_subclassing(self):
233 o = {'names with spaces': 'should be presented using repr()',
234 'others.should.not.be': 'like.this'}
235 exp = """\
236{'names with spaces': 'should be presented using repr()',
237 others.should.not.be: like.this}"""
238 self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
239
Serhiy Storchaka51844382013-10-02 11:40:49 +0300240 def test_set_reprs(self):
241 self.assertEqual(pprint.pformat(set()), 'set()')
242 self.assertEqual(pprint.pformat(set(range(3))), '{0, 1, 2}')
243 self.assertEqual(pprint.pformat(set(range(7)), width=20), '''\
244{0,
245 1,
246 2,
247 3,
248 4,
249 5,
250 6}''')
251 self.assertEqual(pprint.pformat(set2(range(7)), width=20), '''\
252set2({0,
253 1,
254 2,
255 3,
256 4,
257 5,
258 6})''')
259 self.assertEqual(pprint.pformat(set3(range(7)), width=20),
260 'set3({0, 1, 2, 3, 4, 5, 6})')
261
262 self.assertEqual(pprint.pformat(frozenset()), 'frozenset()')
263 self.assertEqual(pprint.pformat(frozenset(range(3))),
264 'frozenset({0, 1, 2})')
265 self.assertEqual(pprint.pformat(frozenset(range(7)), width=20), '''\
266frozenset({0,
267 1,
268 2,
269 3,
270 4,
271 5,
272 6})''')
273 self.assertEqual(pprint.pformat(frozenset2(range(7)), width=20), '''\
274frozenset2({0,
275 1,
276 2,
277 3,
278 4,
279 5,
280 6})''')
281 self.assertEqual(pprint.pformat(frozenset3(range(7)), width=20),
282 'frozenset3({0, 1, 2, 3, 4, 5, 6})')
283
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400284 @unittest.expectedFailure
285 #See http://bugs.python.org/issue13907
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000286 @test.support.cpython_only
Serhiy Storchaka51844382013-10-02 11:40:49 +0300287 def test_set_of_sets_reprs(self):
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000288 # This test creates a complex arrangement of frozensets and
289 # compares the pretty-printed repr against a string hard-coded in
290 # the test. The hard-coded repr depends on the sort order of
291 # frozensets.
292 #
293 # However, as the docs point out: "Since sets only define
294 # partial ordering (subset relationships), the output of the
295 # list.sort() method is undefined for lists of sets."
296 #
297 # In a nutshell, the test assumes frozenset({0}) will always
298 # sort before frozenset({1}), but:
299 #
300 # >>> frozenset({0}) < frozenset({1})
301 # False
302 # >>> frozenset({1}) < frozenset({0})
303 # False
304 #
305 # Consequently, this test is fragile and
306 # implementation-dependent. Small changes to Python's sort
307 # algorithm cause the test to fail when it should pass.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400308 # XXX Or changes to the dictionary implmentation...
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000309
Christian Heimes969fe572008-01-25 11:23:10 +0000310 cube_repr_tgt = """\
311{frozenset(): frozenset({frozenset({2}), frozenset({0}), frozenset({1})}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000312 frozenset({0}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000313 frozenset({0, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000314 frozenset({0, 1})}),
315 frozenset({1}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000316 frozenset({1, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000317 frozenset({0, 1})}),
318 frozenset({2}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000319 frozenset({1, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000320 frozenset({0, 2})}),
321 frozenset({1, 2}): frozenset({frozenset({2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000322 frozenset({1}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000323 frozenset({0, 1, 2})}),
324 frozenset({0, 2}): frozenset({frozenset({2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000325 frozenset({0}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000326 frozenset({0, 1, 2})}),
327 frozenset({0, 1}): frozenset({frozenset({0}),
Christian Heimes969fe572008-01-25 11:23:10 +0000328 frozenset({1}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000329 frozenset({0, 1, 2})}),
330 frozenset({0, 1, 2}): frozenset({frozenset({1, 2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000331 frozenset({0, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000332 frozenset({0, 1})})}"""
Christian Heimes969fe572008-01-25 11:23:10 +0000333 cube = test.test_set.cube(3)
334 self.assertEqual(pprint.pformat(cube), cube_repr_tgt)
335 cubo_repr_tgt = """\
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000336{frozenset({frozenset({0, 2}), frozenset({0})}): frozenset({frozenset({frozenset({0,
337 2}),
338 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000339 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000340 2})}),
341 frozenset({frozenset({0}),
342 frozenset({0,
343 1})}),
344 frozenset({frozenset(),
345 frozenset({0})}),
346 frozenset({frozenset({2}),
347 frozenset({0,
348 2})})}),
349 frozenset({frozenset({0, 1}), frozenset({1})}): frozenset({frozenset({frozenset({0,
350 1}),
351 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000352 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000353 2})}),
354 frozenset({frozenset({0}),
355 frozenset({0,
356 1})}),
357 frozenset({frozenset({1}),
358 frozenset({1,
359 2})}),
360 frozenset({frozenset(),
361 frozenset({1})})}),
362 frozenset({frozenset({1, 2}), frozenset({1})}): frozenset({frozenset({frozenset({1,
363 2}),
364 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000365 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000366 2})}),
367 frozenset({frozenset({2}),
368 frozenset({1,
369 2})}),
370 frozenset({frozenset(),
371 frozenset({1})}),
372 frozenset({frozenset({1}),
373 frozenset({0,
374 1})})}),
375 frozenset({frozenset({1, 2}), frozenset({2})}): frozenset({frozenset({frozenset({1,
376 2}),
377 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000378 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000379 2})}),
380 frozenset({frozenset({1}),
381 frozenset({1,
382 2})}),
383 frozenset({frozenset({2}),
384 frozenset({0,
385 2})}),
386 frozenset({frozenset(),
387 frozenset({2})})}),
388 frozenset({frozenset(), frozenset({0})}): frozenset({frozenset({frozenset({0}),
389 frozenset({0,
390 1})}),
391 frozenset({frozenset({0}),
392 frozenset({0,
393 2})}),
394 frozenset({frozenset(),
395 frozenset({1})}),
396 frozenset({frozenset(),
397 frozenset({2})})}),
398 frozenset({frozenset(), frozenset({1})}): frozenset({frozenset({frozenset(),
399 frozenset({0})}),
400 frozenset({frozenset({1}),
401 frozenset({1,
402 2})}),
403 frozenset({frozenset(),
404 frozenset({2})}),
405 frozenset({frozenset({1}),
406 frozenset({0,
407 1})})}),
408 frozenset({frozenset({2}), frozenset()}): frozenset({frozenset({frozenset({2}),
409 frozenset({1,
410 2})}),
411 frozenset({frozenset(),
412 frozenset({0})}),
413 frozenset({frozenset(),
414 frozenset({1})}),
415 frozenset({frozenset({2}),
416 frozenset({0,
417 2})})}),
418 frozenset({frozenset({0, 1, 2}), frozenset({0, 1})}): frozenset({frozenset({frozenset({1,
419 2}),
420 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000421 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000422 2})}),
423 frozenset({frozenset({0,
424 2}),
425 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000426 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000427 2})}),
428 frozenset({frozenset({0}),
429 frozenset({0,
430 1})}),
431 frozenset({frozenset({1}),
432 frozenset({0,
433 1})})}),
434 frozenset({frozenset({0}), frozenset({0, 1})}): frozenset({frozenset({frozenset(),
435 frozenset({0})}),
436 frozenset({frozenset({0,
437 1}),
438 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000439 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000440 2})}),
441 frozenset({frozenset({0}),
442 frozenset({0,
443 2})}),
444 frozenset({frozenset({1}),
445 frozenset({0,
446 1})})}),
447 frozenset({frozenset({2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({0,
448 2}),
449 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000450 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000451 2})}),
452 frozenset({frozenset({2}),
453 frozenset({1,
454 2})}),
455 frozenset({frozenset({0}),
456 frozenset({0,
457 2})}),
458 frozenset({frozenset(),
459 frozenset({2})})}),
460 frozenset({frozenset({0, 1, 2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({1,
461 2}),
462 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000463 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000464 2})}),
465 frozenset({frozenset({0,
466 1}),
467 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000468 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000469 2})}),
470 frozenset({frozenset({0}),
471 frozenset({0,
472 2})}),
473 frozenset({frozenset({2}),
474 frozenset({0,
475 2})})}),
476 frozenset({frozenset({1, 2}), frozenset({0, 1, 2})}): frozenset({frozenset({frozenset({0,
477 2}),
478 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000479 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000480 2})}),
481 frozenset({frozenset({0,
482 1}),
483 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000484 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000485 2})}),
486 frozenset({frozenset({2}),
487 frozenset({1,
488 2})}),
489 frozenset({frozenset({1}),
490 frozenset({1,
491 2})})})}"""
Christian Heimes969fe572008-01-25 11:23:10 +0000492
493 cubo = test.test_set.linegraph(cube)
494 self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt)
495
Alexandre Vassalottieca20b62008-05-16 02:54:33 +0000496 def test_depth(self):
497 nested_tuple = (1, (2, (3, (4, (5, 6)))))
498 nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}}
499 nested_list = [1, [2, [3, [4, [5, [6, []]]]]]]
500 self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple))
501 self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict))
502 self.assertEqual(pprint.pformat(nested_list), repr(nested_list))
503
504 lv1_tuple = '(1, (...))'
505 lv1_dict = '{1: {...}}'
506 lv1_list = '[1, [...]]'
507 self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple)
508 self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict)
509 self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list)
510
Raymond Hettingera7da1662009-11-19 01:07:05 +0000511 def test_sort_unorderable_values(self):
512 # Issue 3976: sorted pprints fail for unorderable values.
513 n = 20
514 keys = [Unorderable() for i in range(n)]
515 random.shuffle(keys)
516 skeys = sorted(keys, key=id)
517 clean = lambda s: s.replace(' ', '').replace('\n','')
518
519 self.assertEqual(clean(pprint.pformat(set(keys))),
520 '{' + ','.join(map(repr, skeys)) + '}')
521 self.assertEqual(clean(pprint.pformat(frozenset(keys))),
522 'frozenset({' + ','.join(map(repr, skeys)) + '})')
523 self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys))),
524 '{' + ','.join('%r:None' % k for k in skeys) + '}')
Fred Drakeaee113d2002-04-02 05:08:35 +0000525
Florent Xiclunad6da90f2012-07-21 11:17:38 +0200526 # Issue 10017: TypeError on user-defined types as dict keys.
527 self.assertEqual(pprint.pformat({Unorderable: 0, 1: 0}),
528 '{1: 0, ' + repr(Unorderable) +': 0}')
529
530 # Issue 14998: TypeError on tuples with NoneTypes as dict keys.
Florent Xicluna6e571d62012-07-21 12:44:20 +0200531 keys = [(1,), (None,)]
532 self.assertEqual(pprint.pformat(dict.fromkeys(keys, 0)),
533 '{%r: 0, %r: 0}' % tuple(sorted(keys, key=id)))
Florent Xiclunad6da90f2012-07-21 11:17:38 +0200534
Antoine Pitrou64c16c32013-03-23 20:30:39 +0100535 def test_str_wrap(self):
536 # pprint tries to wrap strings intelligently
537 fox = 'the quick brown fox jumped over a lazy dog'
538 self.assertEqual(pprint.pformat(fox, width=20), """\
539'the quick brown '
540'fox jumped over '
541'a lazy dog'""")
542 self.assertEqual(pprint.pformat({'a': 1, 'b': fox, 'c': 2},
543 width=26), """\
544{'a': 1,
545 'b': 'the quick brown '
546 'fox jumped over '
547 'a lazy dog',
548 'c': 2}""")
549 # With some special characters
550 # - \n always triggers a new line in the pprint
551 # - \t and \n are escaped
552 # - non-ASCII is allowed
553 # - an apostrophe doesn't disrupt the pprint
554 special = "Portons dix bons \"whiskys\"\nà l'avocat goujat\t qui fumait au zoo"
555 self.assertEqual(pprint.pformat(special, width=20), """\
556'Portons dix bons '
557'"whiskys"\\n'
558"à l'avocat "
559'goujat\\t qui '
560'fumait au zoo'""")
561 # An unwrappable string is formatted as its repr
562 unwrappable = "x" * 100
563 self.assertEqual(pprint.pformat(unwrappable, width=80), repr(unwrappable))
564 self.assertEqual(pprint.pformat(''), "''")
565 # Check that the pprint is a usable repr
566 special *= 10
567 for width in range(3, 40):
568 formatted = pprint.pformat(special, width=width)
569 self.assertEqual(eval("(" + formatted + ")"), special)
570
Serhiy Storchaka7c411a42013-10-02 11:56:18 +0300571 def test_compact(self):
572 o = ([list(range(i * i)) for i in range(5)] +
573 [list(range(i)) for i in range(6)])
574 expected = """\
575[[], [0], [0, 1, 2, 3],
576 [0, 1, 2, 3, 4, 5, 6, 7, 8],
577 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
578 14, 15],
579 [], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3],
580 [0, 1, 2, 3, 4]]"""
581 self.assertEqual(pprint.pformat(o, width=48, compact=True), expected)
582
Florent Xiclunad6da90f2012-07-21 11:17:38 +0200583
Fred Drakeaee113d2002-04-02 05:08:35 +0000584class DottedPrettyPrinter(pprint.PrettyPrinter):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000585
Fred Drakeaee113d2002-04-02 05:08:35 +0000586 def format(self, object, context, maxlevels, level):
587 if isinstance(object, str):
588 if ' ' in object:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000589 return repr(object), 1, 0
Fred Drakeaee113d2002-04-02 05:08:35 +0000590 else:
591 return object, 0, 0
592 else:
593 return pprint.PrettyPrinter.format(
594 self, object, context, maxlevels, level)
595
596
Fred Drake2e2be372001-09-20 21:33:42 +0000597def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000598 test.support.run_unittest(QueryTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +0000599
600
601if __name__ == "__main__":
602 test_main()