blob: 180ddb0f825d61e87197b8af939eec21625dc51f [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()
Serhiy Storchakacbfe07e2015-05-20 19:37:10 +030061 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, b"def",
62 bytearray(b"ghi"), True, False, None, ...,
Fred Drake43913dd2001-05-14 17:41:20 +000063 self.a, self.b):
Fred Drakeb456e4f2002-12-31 07:16:16 +000064 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +000065 self.assertFalse(pprint.isrecursive(safe),
66 "expected not isrecursive for %r" % (safe,))
67 self.assertTrue(pprint.isreadable(safe),
68 "expected isreadable for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +000069 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +000070 self.assertFalse(pp.isrecursive(safe),
71 "expected not isrecursive for %r" % (safe,))
72 self.assertTrue(pp.isreadable(safe),
73 "expected isreadable for %r" % (safe,))
Tim Petersa814db52001-05-14 07:05:58 +000074
Fred Drake43913dd2001-05-14 17:41:20 +000075 def test_knotted(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000076 # Verify .isrecursive() and .isreadable() w/ recursion
Fred Drake43913dd2001-05-14 17:41:20 +000077 # Tie a knot.
78 self.b[67] = self.a
79 # Messy dict.
80 self.d = {}
81 self.d[0] = self.d[1] = self.d[2] = self.d
Tim Petersa814db52001-05-14 07:05:58 +000082
Fred Drakeb456e4f2002-12-31 07:16:16 +000083 pp = pprint.PrettyPrinter()
Tim Petersa814db52001-05-14 07:05:58 +000084
Fred Drake43913dd2001-05-14 17:41:20 +000085 for icky in self.a, self.b, self.d, (self.d, self.d):
Ezio Melottib19f43d2010-01-24 20:59:24 +000086 self.assertTrue(pprint.isrecursive(icky), "expected isrecursive")
87 self.assertFalse(pprint.isreadable(icky), "expected not isreadable")
88 self.assertTrue(pp.isrecursive(icky), "expected isrecursive")
89 self.assertFalse(pp.isreadable(icky), "expected not isreadable")
Fred Drake43913dd2001-05-14 17:41:20 +000090
91 # Break the cycles.
92 self.d.clear()
93 del self.a[:]
94 del self.b[:]
95
96 for safe in self.a, self.b, self.d, (self.d, self.d):
Fred Drakeb456e4f2002-12-31 07:16:16 +000097 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +000098 self.assertFalse(pprint.isrecursive(safe),
99 "expected not isrecursive for %r" % (safe,))
100 self.assertTrue(pprint.isreadable(safe),
101 "expected isreadable for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +0000102 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +0000103 self.assertFalse(pp.isrecursive(safe),
104 "expected not isrecursive for %r" % (safe,))
105 self.assertTrue(pp.isreadable(safe),
106 "expected isreadable for %r" % (safe,))
Fred Drake43913dd2001-05-14 17:41:20 +0000107
108 def test_unreadable(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000109 # Not recursive but not readable anyway
Fred Drakeb456e4f2002-12-31 07:16:16 +0000110 pp = pprint.PrettyPrinter()
Fred Drake43913dd2001-05-14 17:41:20 +0000111 for unreadable in type(3), pprint, pprint.isrecursive:
Fred Drakeb456e4f2002-12-31 07:16:16 +0000112 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +0000113 self.assertFalse(pprint.isrecursive(unreadable),
114 "expected not isrecursive for %r" % (unreadable,))
115 self.assertFalse(pprint.isreadable(unreadable),
116 "expected not isreadable for %r" % (unreadable,))
Fred Drakeb456e4f2002-12-31 07:16:16 +0000117 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +0000118 self.assertFalse(pp.isrecursive(unreadable),
119 "expected not isrecursive for %r" % (unreadable,))
120 self.assertFalse(pp.isreadable(unreadable),
121 "expected not isreadable for %r" % (unreadable,))
Fred Drake43913dd2001-05-14 17:41:20 +0000122
Tim Peters95b3f782001-05-14 18:39:41 +0000123 def test_same_as_repr(self):
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000124 # Simple objects, small containers and classes that overwrite __repr__
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000125 # For those the result should be the same as repr().
126 # Ahem. The docs don't say anything about that -- this appears to
127 # be testing an implementation quirk. Starting in Python 2.5, it's
128 # not true for dicts: pprint always sorts dicts by key now; before,
129 # it sorted a dict display if and only if the display required
130 # multiple lines. For that reason, dicts with more than one element
131 # aren't tested here.
Serhiy Storchakacbfe07e2015-05-20 19:37:10 +0300132 for simple in (0, 0, 0+0j, 0.0, "", b"", bytearray(),
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000133 (), tuple2(), tuple3(),
134 [], list2(), list3(),
Serhiy Storchaka51844382013-10-02 11:40:49 +0300135 set(), set2(), set3(),
136 frozenset(), frozenset2(), frozenset3(),
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000137 {}, dict2(), dict3(),
Ezio Melottib19f43d2010-01-24 20:59:24 +0000138 self.assertTrue, pprint,
Serhiy Storchakacbfe07e2015-05-20 19:37:10 +0300139 -6, -6, -6-6j, -1.5, "x", b"x", bytearray(b"x"),
140 (3,), [3], {3: 6},
Benjamin Petersond23f8222009-04-05 19:13:16 +0000141 (1,2), [3,4], {5: 6},
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000142 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
143 [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
Serhiy Storchaka51844382013-10-02 11:40:49 +0300144 set({7}), set2({7}), set3({7}),
145 frozenset({8}), frozenset2({8}), frozenset3({8}),
Benjamin Petersond23f8222009-04-05 19:13:16 +0000146 dict2({5: 6}), dict3({5: 6}),
Serhiy Storchakacbfe07e2015-05-20 19:37:10 +0300147 range(10, -11, -1),
148 True, False, None, ...,
Tim Peters95b3f782001-05-14 18:39:41 +0000149 ):
150 native = repr(simple)
Serhiy Storchaka51844382013-10-02 11:40:49 +0300151 self.assertEqual(pprint.pformat(simple), native)
152 self.assertEqual(pprint.pformat(simple, width=1, indent=0)
153 .replace('\n', ' '), native)
154 self.assertEqual(pprint.saferepr(simple), native)
Fred Drake43913dd2001-05-14 17:41:20 +0000155
Barry Warsaw00859c02001-11-28 05:49:39 +0000156 def test_basic_line_wrap(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000157 # verify basic line-wrapping operation
Barry Warsaw00859c02001-11-28 05:49:39 +0000158 o = {'RPM_cal': 0,
159 'RPM_cal2': 48059,
160 'Speed_cal': 0,
161 'controldesk_runtime_us': 0,
162 'main_code_runtime_us': 0,
163 'read_io_runtime_us': 0,
164 'write_io_runtime_us': 43690}
165 exp = """\
166{'RPM_cal': 0,
167 'RPM_cal2': 48059,
168 'Speed_cal': 0,
169 'controldesk_runtime_us': 0,
170 'main_code_runtime_us': 0,
171 'read_io_runtime_us': 0,
172 'write_io_runtime_us': 43690}"""
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000173 for type in [dict, dict2]:
174 self.assertEqual(pprint.pformat(type(o)), exp)
175
176 o = range(100)
177 exp = '[%s]' % ',\n '.join(map(str, o))
178 for type in [list, list2]:
179 self.assertEqual(pprint.pformat(type(o)), exp)
180
181 o = tuple(range(100))
182 exp = '(%s)' % ',\n '.join(map(str, o))
183 for type in [tuple, tuple2]:
184 self.assertEqual(pprint.pformat(type(o)), exp)
Barry Warsaw00859c02001-11-28 05:49:39 +0000185
Walter Dörwaldc8de4582003-12-03 20:26:05 +0000186 # indent parameter
187 o = range(100)
188 exp = '[ %s]' % ',\n '.join(map(str, o))
189 for type in [list, list2]:
190 self.assertEqual(pprint.pformat(type(o), indent=4), exp)
191
Georg Brandl3ccb7872008-07-16 03:00:45 +0000192 def test_nested_indentations(self):
193 o1 = list(range(10))
194 o2 = dict(first=1, second=2, third=3)
195 o = [o1, o2]
196 expected = """\
197[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
198 { 'first': 1,
199 'second': 2,
200 'third': 3}]"""
201 self.assertEqual(pprint.pformat(o, indent=4, width=42), expected)
202
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000203 def test_sorted_dict(self):
204 # Starting in Python 2.5, pprint sorts dict displays by key regardless
205 # of how small the dictionary may be.
206 # Before the change, on 32-bit Windows pformat() gave order
207 # 'a', 'c', 'b' here, so this test failed.
208 d = {'a': 1, 'b': 1, 'c': 1}
209 self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}")
210 self.assertEqual(pprint.pformat([d, d]),
211 "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]")
212
213 # The next one is kind of goofy. The sorted order depends on the
214 # alphabetic order of type names: "int" < "str" < "tuple". Before
215 # Python 2.5, this was in the test_same_as_repr() test. It's worth
216 # keeping around for now because it's one of few tests of pprint
217 # against a crazy mix of types.
218 self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}),
219 r"{5: [[]], 'xy\tab\n': (3,), (): {}}")
220
Raymond Hettingerbad3c882010-09-09 12:31:00 +0000221 def test_ordered_dict(self):
222 words = 'the quick brown fox jumped over a lazy dog'.split()
223 d = collections.OrderedDict(zip(words, itertools.count()))
224 self.assertEqual(pprint.pformat(d),
225"""\
226{'the': 0,
227 'quick': 1,
228 'brown': 2,
229 'fox': 3,
230 'jumped': 4,
231 'over': 5,
232 'a': 6,
233 'lazy': 7,
234 'dog': 8}""")
Fred Drakeaee113d2002-04-02 05:08:35 +0000235 def test_subclassing(self):
236 o = {'names with spaces': 'should be presented using repr()',
237 'others.should.not.be': 'like.this'}
238 exp = """\
239{'names with spaces': 'should be presented using repr()',
240 others.should.not.be: like.this}"""
241 self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
242
Serhiy Storchaka51844382013-10-02 11:40:49 +0300243 def test_set_reprs(self):
244 self.assertEqual(pprint.pformat(set()), 'set()')
245 self.assertEqual(pprint.pformat(set(range(3))), '{0, 1, 2}')
246 self.assertEqual(pprint.pformat(set(range(7)), width=20), '''\
247{0,
248 1,
249 2,
250 3,
251 4,
252 5,
253 6}''')
254 self.assertEqual(pprint.pformat(set2(range(7)), width=20), '''\
255set2({0,
256 1,
257 2,
258 3,
259 4,
260 5,
261 6})''')
262 self.assertEqual(pprint.pformat(set3(range(7)), width=20),
263 'set3({0, 1, 2, 3, 4, 5, 6})')
264
265 self.assertEqual(pprint.pformat(frozenset()), 'frozenset()')
266 self.assertEqual(pprint.pformat(frozenset(range(3))),
267 'frozenset({0, 1, 2})')
268 self.assertEqual(pprint.pformat(frozenset(range(7)), width=20), '''\
269frozenset({0,
270 1,
271 2,
272 3,
273 4,
274 5,
275 6})''')
276 self.assertEqual(pprint.pformat(frozenset2(range(7)), width=20), '''\
277frozenset2({0,
278 1,
279 2,
280 3,
281 4,
282 5,
283 6})''')
284 self.assertEqual(pprint.pformat(frozenset3(range(7)), width=20),
285 'frozenset3({0, 1, 2, 3, 4, 5, 6})')
286
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400287 @unittest.expectedFailure
288 #See http://bugs.python.org/issue13907
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000289 @test.support.cpython_only
Serhiy Storchaka51844382013-10-02 11:40:49 +0300290 def test_set_of_sets_reprs(self):
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000291 # This test creates a complex arrangement of frozensets and
292 # compares the pretty-printed repr against a string hard-coded in
293 # the test. The hard-coded repr depends on the sort order of
294 # frozensets.
295 #
296 # However, as the docs point out: "Since sets only define
297 # partial ordering (subset relationships), the output of the
298 # list.sort() method is undefined for lists of sets."
299 #
300 # In a nutshell, the test assumes frozenset({0}) will always
301 # sort before frozenset({1}), but:
302 #
303 # >>> frozenset({0}) < frozenset({1})
304 # False
305 # >>> frozenset({1}) < frozenset({0})
306 # False
307 #
308 # Consequently, this test is fragile and
309 # implementation-dependent. Small changes to Python's sort
310 # algorithm cause the test to fail when it should pass.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400311 # XXX Or changes to the dictionary implmentation...
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000312
Christian Heimes969fe572008-01-25 11:23:10 +0000313 cube_repr_tgt = """\
314{frozenset(): frozenset({frozenset({2}), frozenset({0}), frozenset({1})}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000315 frozenset({0}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000316 frozenset({0, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000317 frozenset({0, 1})}),
318 frozenset({1}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000319 frozenset({1, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000320 frozenset({0, 1})}),
321 frozenset({2}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000322 frozenset({1, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000323 frozenset({0, 2})}),
324 frozenset({1, 2}): frozenset({frozenset({2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000325 frozenset({1}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000326 frozenset({0, 1, 2})}),
327 frozenset({0, 2}): frozenset({frozenset({2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000328 frozenset({0}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000329 frozenset({0, 1, 2})}),
330 frozenset({0, 1}): frozenset({frozenset({0}),
Christian Heimes969fe572008-01-25 11:23:10 +0000331 frozenset({1}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000332 frozenset({0, 1, 2})}),
333 frozenset({0, 1, 2}): frozenset({frozenset({1, 2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000334 frozenset({0, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000335 frozenset({0, 1})})}"""
Christian Heimes969fe572008-01-25 11:23:10 +0000336 cube = test.test_set.cube(3)
337 self.assertEqual(pprint.pformat(cube), cube_repr_tgt)
338 cubo_repr_tgt = """\
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000339{frozenset({frozenset({0, 2}), frozenset({0})}): frozenset({frozenset({frozenset({0,
340 2}),
341 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000342 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000343 2})}),
344 frozenset({frozenset({0}),
345 frozenset({0,
346 1})}),
347 frozenset({frozenset(),
348 frozenset({0})}),
349 frozenset({frozenset({2}),
350 frozenset({0,
351 2})})}),
352 frozenset({frozenset({0, 1}), frozenset({1})}): frozenset({frozenset({frozenset({0,
353 1}),
354 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000355 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000356 2})}),
357 frozenset({frozenset({0}),
358 frozenset({0,
359 1})}),
360 frozenset({frozenset({1}),
361 frozenset({1,
362 2})}),
363 frozenset({frozenset(),
364 frozenset({1})})}),
365 frozenset({frozenset({1, 2}), frozenset({1})}): frozenset({frozenset({frozenset({1,
366 2}),
367 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000368 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000369 2})}),
370 frozenset({frozenset({2}),
371 frozenset({1,
372 2})}),
373 frozenset({frozenset(),
374 frozenset({1})}),
375 frozenset({frozenset({1}),
376 frozenset({0,
377 1})})}),
378 frozenset({frozenset({1, 2}), frozenset({2})}): frozenset({frozenset({frozenset({1,
379 2}),
380 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000381 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000382 2})}),
383 frozenset({frozenset({1}),
384 frozenset({1,
385 2})}),
386 frozenset({frozenset({2}),
387 frozenset({0,
388 2})}),
389 frozenset({frozenset(),
390 frozenset({2})})}),
391 frozenset({frozenset(), frozenset({0})}): frozenset({frozenset({frozenset({0}),
392 frozenset({0,
393 1})}),
394 frozenset({frozenset({0}),
395 frozenset({0,
396 2})}),
397 frozenset({frozenset(),
398 frozenset({1})}),
399 frozenset({frozenset(),
400 frozenset({2})})}),
401 frozenset({frozenset(), frozenset({1})}): frozenset({frozenset({frozenset(),
402 frozenset({0})}),
403 frozenset({frozenset({1}),
404 frozenset({1,
405 2})}),
406 frozenset({frozenset(),
407 frozenset({2})}),
408 frozenset({frozenset({1}),
409 frozenset({0,
410 1})})}),
411 frozenset({frozenset({2}), frozenset()}): frozenset({frozenset({frozenset({2}),
412 frozenset({1,
413 2})}),
414 frozenset({frozenset(),
415 frozenset({0})}),
416 frozenset({frozenset(),
417 frozenset({1})}),
418 frozenset({frozenset({2}),
419 frozenset({0,
420 2})})}),
421 frozenset({frozenset({0, 1, 2}), frozenset({0, 1})}): frozenset({frozenset({frozenset({1,
422 2}),
423 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000424 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000425 2})}),
426 frozenset({frozenset({0,
427 2}),
428 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000429 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000430 2})}),
431 frozenset({frozenset({0}),
432 frozenset({0,
433 1})}),
434 frozenset({frozenset({1}),
435 frozenset({0,
436 1})})}),
437 frozenset({frozenset({0}), frozenset({0, 1})}): frozenset({frozenset({frozenset(),
438 frozenset({0})}),
439 frozenset({frozenset({0,
440 1}),
441 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000442 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000443 2})}),
444 frozenset({frozenset({0}),
445 frozenset({0,
446 2})}),
447 frozenset({frozenset({1}),
448 frozenset({0,
449 1})})}),
450 frozenset({frozenset({2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({0,
451 2}),
452 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000453 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000454 2})}),
455 frozenset({frozenset({2}),
456 frozenset({1,
457 2})}),
458 frozenset({frozenset({0}),
459 frozenset({0,
460 2})}),
461 frozenset({frozenset(),
462 frozenset({2})})}),
463 frozenset({frozenset({0, 1, 2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({1,
464 2}),
465 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000466 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000467 2})}),
468 frozenset({frozenset({0,
469 1}),
470 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000471 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000472 2})}),
473 frozenset({frozenset({0}),
474 frozenset({0,
475 2})}),
476 frozenset({frozenset({2}),
477 frozenset({0,
478 2})})}),
479 frozenset({frozenset({1, 2}), frozenset({0, 1, 2})}): frozenset({frozenset({frozenset({0,
480 2}),
481 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000482 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000483 2})}),
484 frozenset({frozenset({0,
485 1}),
486 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000487 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000488 2})}),
489 frozenset({frozenset({2}),
490 frozenset({1,
491 2})}),
492 frozenset({frozenset({1}),
493 frozenset({1,
494 2})})})}"""
Christian Heimes969fe572008-01-25 11:23:10 +0000495
496 cubo = test.test_set.linegraph(cube)
497 self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt)
498
Alexandre Vassalottieca20b62008-05-16 02:54:33 +0000499 def test_depth(self):
500 nested_tuple = (1, (2, (3, (4, (5, 6)))))
501 nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}}
502 nested_list = [1, [2, [3, [4, [5, [6, []]]]]]]
503 self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple))
504 self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict))
505 self.assertEqual(pprint.pformat(nested_list), repr(nested_list))
506
507 lv1_tuple = '(1, (...))'
508 lv1_dict = '{1: {...}}'
509 lv1_list = '[1, [...]]'
510 self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple)
511 self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict)
512 self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list)
513
Raymond Hettingera7da1662009-11-19 01:07:05 +0000514 def test_sort_unorderable_values(self):
515 # Issue 3976: sorted pprints fail for unorderable values.
516 n = 20
517 keys = [Unorderable() for i in range(n)]
518 random.shuffle(keys)
519 skeys = sorted(keys, key=id)
520 clean = lambda s: s.replace(' ', '').replace('\n','')
521
522 self.assertEqual(clean(pprint.pformat(set(keys))),
523 '{' + ','.join(map(repr, skeys)) + '}')
524 self.assertEqual(clean(pprint.pformat(frozenset(keys))),
525 'frozenset({' + ','.join(map(repr, skeys)) + '})')
526 self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys))),
527 '{' + ','.join('%r:None' % k for k in skeys) + '}')
Fred Drakeaee113d2002-04-02 05:08:35 +0000528
Florent Xiclunad6da90f2012-07-21 11:17:38 +0200529 # Issue 10017: TypeError on user-defined types as dict keys.
530 self.assertEqual(pprint.pformat({Unorderable: 0, 1: 0}),
531 '{1: 0, ' + repr(Unorderable) +': 0}')
532
533 # Issue 14998: TypeError on tuples with NoneTypes as dict keys.
Florent Xicluna6e571d62012-07-21 12:44:20 +0200534 keys = [(1,), (None,)]
535 self.assertEqual(pprint.pformat(dict.fromkeys(keys, 0)),
536 '{%r: 0, %r: 0}' % tuple(sorted(keys, key=id)))
Florent Xiclunad6da90f2012-07-21 11:17:38 +0200537
Antoine Pitrou64c16c32013-03-23 20:30:39 +0100538 def test_str_wrap(self):
539 # pprint tries to wrap strings intelligently
540 fox = 'the quick brown fox jumped over a lazy dog'
541 self.assertEqual(pprint.pformat(fox, width=20), """\
Serhiy Storchakafe3dc372014-12-20 20:57:15 +0200542('the quick '
543 'brown fox '
544 'jumped over a '
545 'lazy dog')""")
Antoine Pitrou64c16c32013-03-23 20:30:39 +0100546 self.assertEqual(pprint.pformat({'a': 1, 'b': fox, 'c': 2},
547 width=26), """\
548{'a': 1,
549 'b': 'the quick brown '
550 'fox jumped over '
551 'a lazy dog',
552 'c': 2}""")
553 # With some special characters
554 # - \n always triggers a new line in the pprint
555 # - \t and \n are escaped
556 # - non-ASCII is allowed
557 # - an apostrophe doesn't disrupt the pprint
558 special = "Portons dix bons \"whiskys\"\nà l'avocat goujat\t qui fumait au zoo"
Serhiy Storchakafe3dc372014-12-20 20:57:15 +0200559 self.assertEqual(pprint.pformat(special, width=21), """\
560('Portons dix '
561 'bons "whiskys"\\n'
562 "à l'avocat "
563 'goujat\\t qui '
564 'fumait au zoo')""")
Antoine Pitrou64c16c32013-03-23 20:30:39 +0100565 # An unwrappable string is formatted as its repr
566 unwrappable = "x" * 100
567 self.assertEqual(pprint.pformat(unwrappable, width=80), repr(unwrappable))
568 self.assertEqual(pprint.pformat(''), "''")
569 # Check that the pprint is a usable repr
570 special *= 10
571 for width in range(3, 40):
572 formatted = pprint.pformat(special, width=width)
Serhiy Storchakafe3dc372014-12-20 20:57:15 +0200573 self.assertEqual(eval(formatted), special)
574 formatted = pprint.pformat([special] * 2, width=width)
575 self.assertEqual(eval(formatted), [special] * 2)
Antoine Pitrou64c16c32013-03-23 20:30:39 +0100576
Serhiy Storchaka7c411a42013-10-02 11:56:18 +0300577 def test_compact(self):
578 o = ([list(range(i * i)) for i in range(5)] +
579 [list(range(i)) for i in range(6)])
580 expected = """\
581[[], [0], [0, 1, 2, 3],
582 [0, 1, 2, 3, 4, 5, 6, 7, 8],
583 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
584 14, 15],
585 [], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3],
586 [0, 1, 2, 3, 4]]"""
587 self.assertEqual(pprint.pformat(o, width=48, compact=True), expected)
588
Florent Xiclunad6da90f2012-07-21 11:17:38 +0200589
Fred Drakeaee113d2002-04-02 05:08:35 +0000590class DottedPrettyPrinter(pprint.PrettyPrinter):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000591
Fred Drakeaee113d2002-04-02 05:08:35 +0000592 def format(self, object, context, maxlevels, level):
593 if isinstance(object, str):
594 if ' ' in object:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000595 return repr(object), 1, 0
Fred Drakeaee113d2002-04-02 05:08:35 +0000596 else:
597 return object, 0, 0
598 else:
599 return pprint.PrettyPrinter.format(
600 self, object, context, maxlevels, level)
601
602
Fred Drake2e2be372001-09-20 21:33:42 +0000603if __name__ == "__main__":
Serhiy Storchakacbfe07e2015-05-20 19:37:10 +0300604 unittest.main()