blob: 4e53cd8a015e5c78ad8306137668457ccf9e7cf3 [file] [log] [blame]
Tim Petersa814db52001-05-14 07:05:58 +00001import pprint
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002import test.support
Fred Drake43913dd2001-05-14 17:41:20 +00003import unittest
Christian Heimes969fe572008-01-25 11:23:10 +00004import test.test_set
Raymond Hettingera7da1662009-11-19 01:07:05 +00005import random
Raymond Hettingerbad3c882010-09-09 12:31:00 +00006import collections
7import itertools
Tim Petersa814db52001-05-14 07:05:58 +00008
Walter Dörwald7a7ede52003-12-03 20:15:28 +00009# list, tuple and dict subclasses that do or don't overwrite __repr__
10class list2(list):
11 pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000012
Walter Dörwald7a7ede52003-12-03 20:15:28 +000013class list3(list):
14 def __repr__(self):
15 return list.__repr__(self)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000016
Walter Dörwald7a7ede52003-12-03 20:15:28 +000017class tuple2(tuple):
18 pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000019
Walter Dörwald7a7ede52003-12-03 20:15:28 +000020class tuple3(tuple):
21 def __repr__(self):
22 return tuple.__repr__(self)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000023
Walter Dörwald7a7ede52003-12-03 20:15:28 +000024class dict2(dict):
25 pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000026
Walter Dörwald7a7ede52003-12-03 20:15:28 +000027class dict3(dict):
28 def __repr__(self):
29 return dict.__repr__(self)
Tim Petersa814db52001-05-14 07:05:58 +000030
Raymond Hettingera7da1662009-11-19 01:07:05 +000031class Unorderable:
32 def __repr__(self):
33 return str(id(self))
34
Fred Drake43913dd2001-05-14 17:41:20 +000035class QueryTestCase(unittest.TestCase):
Tim Petersa814db52001-05-14 07:05:58 +000036
Fred Drake43913dd2001-05-14 17:41:20 +000037 def setUp(self):
Guido van Rossum805365e2007-05-07 22:24:25 +000038 self.a = list(range(100))
39 self.b = list(range(200))
Fred Drake43913dd2001-05-14 17:41:20 +000040 self.a[-12] = self.b
Tim Petersa814db52001-05-14 07:05:58 +000041
Fred Drake43913dd2001-05-14 17:41:20 +000042 def test_basic(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000043 # Verify .isrecursive() and .isreadable() w/o recursion
Fred Drakeb456e4f2002-12-31 07:16:16 +000044 pp = pprint.PrettyPrinter()
Walter Dörwald5de48bd2007-06-11 21:38:39 +000045 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, "yaddayadda",
Fred Drake43913dd2001-05-14 17:41:20 +000046 self.a, self.b):
Fred Drakeb456e4f2002-12-31 07:16:16 +000047 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +000048 self.assertFalse(pprint.isrecursive(safe),
49 "expected not isrecursive for %r" % (safe,))
50 self.assertTrue(pprint.isreadable(safe),
51 "expected isreadable for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +000052 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +000053 self.assertFalse(pp.isrecursive(safe),
54 "expected not isrecursive for %r" % (safe,))
55 self.assertTrue(pp.isreadable(safe),
56 "expected isreadable for %r" % (safe,))
Tim Petersa814db52001-05-14 07:05:58 +000057
Fred Drake43913dd2001-05-14 17:41:20 +000058 def test_knotted(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000059 # Verify .isrecursive() and .isreadable() w/ recursion
Fred Drake43913dd2001-05-14 17:41:20 +000060 # Tie a knot.
61 self.b[67] = self.a
62 # Messy dict.
63 self.d = {}
64 self.d[0] = self.d[1] = self.d[2] = self.d
Tim Petersa814db52001-05-14 07:05:58 +000065
Fred Drakeb456e4f2002-12-31 07:16:16 +000066 pp = pprint.PrettyPrinter()
Tim Petersa814db52001-05-14 07:05:58 +000067
Fred Drake43913dd2001-05-14 17:41:20 +000068 for icky in self.a, self.b, self.d, (self.d, self.d):
Ezio Melottib19f43d2010-01-24 20:59:24 +000069 self.assertTrue(pprint.isrecursive(icky), "expected isrecursive")
70 self.assertFalse(pprint.isreadable(icky), "expected not isreadable")
71 self.assertTrue(pp.isrecursive(icky), "expected isrecursive")
72 self.assertFalse(pp.isreadable(icky), "expected not isreadable")
Fred Drake43913dd2001-05-14 17:41:20 +000073
74 # Break the cycles.
75 self.d.clear()
76 del self.a[:]
77 del self.b[:]
78
79 for safe in self.a, self.b, self.d, (self.d, self.d):
Fred Drakeb456e4f2002-12-31 07:16:16 +000080 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +000081 self.assertFalse(pprint.isrecursive(safe),
82 "expected not isrecursive for %r" % (safe,))
83 self.assertTrue(pprint.isreadable(safe),
84 "expected isreadable for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +000085 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +000086 self.assertFalse(pp.isrecursive(safe),
87 "expected not isrecursive for %r" % (safe,))
88 self.assertTrue(pp.isreadable(safe),
89 "expected isreadable for %r" % (safe,))
Fred Drake43913dd2001-05-14 17:41:20 +000090
91 def test_unreadable(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000092 # Not recursive but not readable anyway
Fred Drakeb456e4f2002-12-31 07:16:16 +000093 pp = pprint.PrettyPrinter()
Fred Drake43913dd2001-05-14 17:41:20 +000094 for unreadable in type(3), pprint, pprint.isrecursive:
Fred Drakeb456e4f2002-12-31 07:16:16 +000095 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +000096 self.assertFalse(pprint.isrecursive(unreadable),
97 "expected not isrecursive for %r" % (unreadable,))
98 self.assertFalse(pprint.isreadable(unreadable),
99 "expected not isreadable for %r" % (unreadable,))
Fred Drakeb456e4f2002-12-31 07:16:16 +0000100 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +0000101 self.assertFalse(pp.isrecursive(unreadable),
102 "expected not isrecursive for %r" % (unreadable,))
103 self.assertFalse(pp.isreadable(unreadable),
104 "expected not isreadable for %r" % (unreadable,))
Fred Drake43913dd2001-05-14 17:41:20 +0000105
Tim Peters95b3f782001-05-14 18:39:41 +0000106 def test_same_as_repr(self):
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000107 # Simple objects, small containers and classes that overwrite __repr__
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000108 # For those the result should be the same as repr().
109 # Ahem. The docs don't say anything about that -- this appears to
110 # be testing an implementation quirk. Starting in Python 2.5, it's
111 # not true for dicts: pprint always sorts dicts by key now; before,
112 # it sorted a dict display if and only if the display required
113 # multiple lines. For that reason, dicts with more than one element
114 # aren't tested here.
Walter Dörwald5de48bd2007-06-11 21:38:39 +0000115 for simple in (0, 0, 0+0j, 0.0, "", b"",
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000116 (), tuple2(), tuple3(),
117 [], list2(), list3(),
118 {}, dict2(), dict3(),
Ezio Melottib19f43d2010-01-24 20:59:24 +0000119 self.assertTrue, pprint,
Walter Dörwald5de48bd2007-06-11 21:38:39 +0000120 -6, -6, -6-6j, -1.5, "x", b"x", (3,), [3], {3: 6},
Benjamin Petersond23f8222009-04-05 19:13:16 +0000121 (1,2), [3,4], {5: 6},
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000122 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
123 [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
Benjamin Petersond23f8222009-04-05 19:13:16 +0000124 dict2({5: 6}), dict3({5: 6}),
Tim Peters95b3f782001-05-14 18:39:41 +0000125 range(10, -11, -1)
126 ):
127 native = repr(simple)
128 for function in "pformat", "saferepr":
129 f = getattr(pprint, function)
130 got = f(simple)
Ezio Melottib19f43d2010-01-24 20:59:24 +0000131 self.assertEqual(native, got,
132 "expected %s got %s from pprint.%s" %
133 (native, got, function))
Fred Drake43913dd2001-05-14 17:41:20 +0000134
Barry Warsaw00859c02001-11-28 05:49:39 +0000135 def test_basic_line_wrap(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000136 # verify basic line-wrapping operation
Barry Warsaw00859c02001-11-28 05:49:39 +0000137 o = {'RPM_cal': 0,
138 'RPM_cal2': 48059,
139 'Speed_cal': 0,
140 'controldesk_runtime_us': 0,
141 'main_code_runtime_us': 0,
142 'read_io_runtime_us': 0,
143 'write_io_runtime_us': 43690}
144 exp = """\
145{'RPM_cal': 0,
146 'RPM_cal2': 48059,
147 'Speed_cal': 0,
148 'controldesk_runtime_us': 0,
149 'main_code_runtime_us': 0,
150 'read_io_runtime_us': 0,
151 'write_io_runtime_us': 43690}"""
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000152 for type in [dict, dict2]:
153 self.assertEqual(pprint.pformat(type(o)), exp)
154
155 o = range(100)
156 exp = '[%s]' % ',\n '.join(map(str, o))
157 for type in [list, list2]:
158 self.assertEqual(pprint.pformat(type(o)), exp)
159
160 o = tuple(range(100))
161 exp = '(%s)' % ',\n '.join(map(str, o))
162 for type in [tuple, tuple2]:
163 self.assertEqual(pprint.pformat(type(o)), exp)
Barry Warsaw00859c02001-11-28 05:49:39 +0000164
Walter Dörwaldc8de4582003-12-03 20:26:05 +0000165 # indent parameter
166 o = range(100)
167 exp = '[ %s]' % ',\n '.join(map(str, o))
168 for type in [list, list2]:
169 self.assertEqual(pprint.pformat(type(o), indent=4), exp)
170
Georg Brandl3ccb7872008-07-16 03:00:45 +0000171 def test_nested_indentations(self):
172 o1 = list(range(10))
173 o2 = dict(first=1, second=2, third=3)
174 o = [o1, o2]
175 expected = """\
176[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
177 { 'first': 1,
178 'second': 2,
179 'third': 3}]"""
180 self.assertEqual(pprint.pformat(o, indent=4, width=42), expected)
181
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000182 def test_sorted_dict(self):
183 # Starting in Python 2.5, pprint sorts dict displays by key regardless
184 # of how small the dictionary may be.
185 # Before the change, on 32-bit Windows pformat() gave order
186 # 'a', 'c', 'b' here, so this test failed.
187 d = {'a': 1, 'b': 1, 'c': 1}
188 self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}")
189 self.assertEqual(pprint.pformat([d, d]),
190 "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]")
191
192 # The next one is kind of goofy. The sorted order depends on the
193 # alphabetic order of type names: "int" < "str" < "tuple". Before
194 # Python 2.5, this was in the test_same_as_repr() test. It's worth
195 # keeping around for now because it's one of few tests of pprint
196 # against a crazy mix of types.
197 self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}),
198 r"{5: [[]], 'xy\tab\n': (3,), (): {}}")
199
Raymond Hettingerbad3c882010-09-09 12:31:00 +0000200 def test_ordered_dict(self):
201 words = 'the quick brown fox jumped over a lazy dog'.split()
202 d = collections.OrderedDict(zip(words, itertools.count()))
203 self.assertEqual(pprint.pformat(d),
204"""\
205{'the': 0,
206 'quick': 1,
207 'brown': 2,
208 'fox': 3,
209 'jumped': 4,
210 'over': 5,
211 'a': 6,
212 'lazy': 7,
213 'dog': 8}""")
Fred Drakeaee113d2002-04-02 05:08:35 +0000214 def test_subclassing(self):
215 o = {'names with spaces': 'should be presented using repr()',
216 'others.should.not.be': 'like.this'}
217 exp = """\
218{'names with spaces': 'should be presented using repr()',
219 others.should.not.be: like.this}"""
220 self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
221
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000222 @test.support.cpython_only
Christian Heimes969fe572008-01-25 11:23:10 +0000223 def test_set_reprs(self):
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000224 # This test creates a complex arrangement of frozensets and
225 # compares the pretty-printed repr against a string hard-coded in
226 # the test. The hard-coded repr depends on the sort order of
227 # frozensets.
228 #
229 # However, as the docs point out: "Since sets only define
230 # partial ordering (subset relationships), the output of the
231 # list.sort() method is undefined for lists of sets."
232 #
233 # In a nutshell, the test assumes frozenset({0}) will always
234 # sort before frozenset({1}), but:
235 #
236 # >>> frozenset({0}) < frozenset({1})
237 # False
238 # >>> frozenset({1}) < frozenset({0})
239 # False
240 #
241 # Consequently, this test is fragile and
242 # implementation-dependent. Small changes to Python's sort
243 # algorithm cause the test to fail when it should pass.
244
Christian Heimes969fe572008-01-25 11:23:10 +0000245 self.assertEqual(pprint.pformat(set()), 'set()')
246 self.assertEqual(pprint.pformat(set(range(3))), '{0, 1, 2}')
247 self.assertEqual(pprint.pformat(frozenset()), 'frozenset()')
248 self.assertEqual(pprint.pformat(frozenset(range(3))), 'frozenset({0, 1, 2})')
249 cube_repr_tgt = """\
250{frozenset(): frozenset({frozenset({2}), frozenset({0}), frozenset({1})}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000251 frozenset({0}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000252 frozenset({0, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000253 frozenset({0, 1})}),
254 frozenset({1}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000255 frozenset({1, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000256 frozenset({0, 1})}),
257 frozenset({2}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000258 frozenset({1, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000259 frozenset({0, 2})}),
260 frozenset({1, 2}): frozenset({frozenset({2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000261 frozenset({1}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000262 frozenset({0, 1, 2})}),
263 frozenset({0, 2}): frozenset({frozenset({2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000264 frozenset({0}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000265 frozenset({0, 1, 2})}),
266 frozenset({0, 1}): frozenset({frozenset({0}),
Christian Heimes969fe572008-01-25 11:23:10 +0000267 frozenset({1}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000268 frozenset({0, 1, 2})}),
269 frozenset({0, 1, 2}): frozenset({frozenset({1, 2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000270 frozenset({0, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000271 frozenset({0, 1})})}"""
Christian Heimes969fe572008-01-25 11:23:10 +0000272 cube = test.test_set.cube(3)
273 self.assertEqual(pprint.pformat(cube), cube_repr_tgt)
274 cubo_repr_tgt = """\
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000275{frozenset({frozenset({0, 2}), frozenset({0})}): frozenset({frozenset({frozenset({0,
276 2}),
277 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000278 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000279 2})}),
280 frozenset({frozenset({0}),
281 frozenset({0,
282 1})}),
283 frozenset({frozenset(),
284 frozenset({0})}),
285 frozenset({frozenset({2}),
286 frozenset({0,
287 2})})}),
288 frozenset({frozenset({0, 1}), frozenset({1})}): frozenset({frozenset({frozenset({0,
289 1}),
290 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000291 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000292 2})}),
293 frozenset({frozenset({0}),
294 frozenset({0,
295 1})}),
296 frozenset({frozenset({1}),
297 frozenset({1,
298 2})}),
299 frozenset({frozenset(),
300 frozenset({1})})}),
301 frozenset({frozenset({1, 2}), frozenset({1})}): frozenset({frozenset({frozenset({1,
302 2}),
303 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000304 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000305 2})}),
306 frozenset({frozenset({2}),
307 frozenset({1,
308 2})}),
309 frozenset({frozenset(),
310 frozenset({1})}),
311 frozenset({frozenset({1}),
312 frozenset({0,
313 1})})}),
314 frozenset({frozenset({1, 2}), frozenset({2})}): frozenset({frozenset({frozenset({1,
315 2}),
316 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000317 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000318 2})}),
319 frozenset({frozenset({1}),
320 frozenset({1,
321 2})}),
322 frozenset({frozenset({2}),
323 frozenset({0,
324 2})}),
325 frozenset({frozenset(),
326 frozenset({2})})}),
327 frozenset({frozenset(), frozenset({0})}): frozenset({frozenset({frozenset({0}),
328 frozenset({0,
329 1})}),
330 frozenset({frozenset({0}),
331 frozenset({0,
332 2})}),
333 frozenset({frozenset(),
334 frozenset({1})}),
335 frozenset({frozenset(),
336 frozenset({2})})}),
337 frozenset({frozenset(), frozenset({1})}): frozenset({frozenset({frozenset(),
338 frozenset({0})}),
339 frozenset({frozenset({1}),
340 frozenset({1,
341 2})}),
342 frozenset({frozenset(),
343 frozenset({2})}),
344 frozenset({frozenset({1}),
345 frozenset({0,
346 1})})}),
347 frozenset({frozenset({2}), frozenset()}): frozenset({frozenset({frozenset({2}),
348 frozenset({1,
349 2})}),
350 frozenset({frozenset(),
351 frozenset({0})}),
352 frozenset({frozenset(),
353 frozenset({1})}),
354 frozenset({frozenset({2}),
355 frozenset({0,
356 2})})}),
357 frozenset({frozenset({0, 1, 2}), frozenset({0, 1})}): frozenset({frozenset({frozenset({1,
358 2}),
359 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000360 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000361 2})}),
362 frozenset({frozenset({0,
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({0}),
368 frozenset({0,
369 1})}),
370 frozenset({frozenset({1}),
371 frozenset({0,
372 1})})}),
373 frozenset({frozenset({0}), frozenset({0, 1})}): frozenset({frozenset({frozenset(),
374 frozenset({0})}),
375 frozenset({frozenset({0,
376 1}),
377 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000378 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000379 2})}),
380 frozenset({frozenset({0}),
381 frozenset({0,
382 2})}),
383 frozenset({frozenset({1}),
384 frozenset({0,
385 1})})}),
386 frozenset({frozenset({2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({0,
387 2}),
388 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000389 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000390 2})}),
391 frozenset({frozenset({2}),
392 frozenset({1,
393 2})}),
394 frozenset({frozenset({0}),
395 frozenset({0,
396 2})}),
397 frozenset({frozenset(),
398 frozenset({2})})}),
399 frozenset({frozenset({0, 1, 2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({1,
400 2}),
401 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000402 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000403 2})}),
404 frozenset({frozenset({0,
405 1}),
406 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000407 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000408 2})}),
409 frozenset({frozenset({0}),
410 frozenset({0,
411 2})}),
412 frozenset({frozenset({2}),
413 frozenset({0,
414 2})})}),
415 frozenset({frozenset({1, 2}), frozenset({0, 1, 2})}): frozenset({frozenset({frozenset({0,
416 2}),
417 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000418 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000419 2})}),
420 frozenset({frozenset({0,
421 1}),
422 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000423 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000424 2})}),
425 frozenset({frozenset({2}),
426 frozenset({1,
427 2})}),
428 frozenset({frozenset({1}),
429 frozenset({1,
430 2})})})}"""
Christian Heimes969fe572008-01-25 11:23:10 +0000431
432 cubo = test.test_set.linegraph(cube)
433 self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt)
434
Alexandre Vassalottieca20b62008-05-16 02:54:33 +0000435 def test_depth(self):
436 nested_tuple = (1, (2, (3, (4, (5, 6)))))
437 nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}}
438 nested_list = [1, [2, [3, [4, [5, [6, []]]]]]]
439 self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple))
440 self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict))
441 self.assertEqual(pprint.pformat(nested_list), repr(nested_list))
442
443 lv1_tuple = '(1, (...))'
444 lv1_dict = '{1: {...}}'
445 lv1_list = '[1, [...]]'
446 self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple)
447 self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict)
448 self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list)
449
Raymond Hettingera7da1662009-11-19 01:07:05 +0000450 def test_sort_unorderable_values(self):
451 # Issue 3976: sorted pprints fail for unorderable values.
452 n = 20
453 keys = [Unorderable() for i in range(n)]
454 random.shuffle(keys)
455 skeys = sorted(keys, key=id)
456 clean = lambda s: s.replace(' ', '').replace('\n','')
457
458 self.assertEqual(clean(pprint.pformat(set(keys))),
459 '{' + ','.join(map(repr, skeys)) + '}')
460 self.assertEqual(clean(pprint.pformat(frozenset(keys))),
461 'frozenset({' + ','.join(map(repr, skeys)) + '})')
462 self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys))),
463 '{' + ','.join('%r:None' % k for k in skeys) + '}')
Fred Drakeaee113d2002-04-02 05:08:35 +0000464
465class DottedPrettyPrinter(pprint.PrettyPrinter):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000466
Fred Drakeaee113d2002-04-02 05:08:35 +0000467 def format(self, object, context, maxlevels, level):
468 if isinstance(object, str):
469 if ' ' in object:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000470 return repr(object), 1, 0
Fred Drakeaee113d2002-04-02 05:08:35 +0000471 else:
472 return object, 0, 0
473 else:
474 return pprint.PrettyPrinter.format(
475 self, object, context, maxlevels, level)
476
477
Fred Drake2e2be372001-09-20 21:33:42 +0000478def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000479 test.support.run_unittest(QueryTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +0000480
481
482if __name__ == "__main__":
483 test_main()