blob: 7ebc298337ad5ccac12505161bd790f1c6ea38a0 [file] [log] [blame]
Antoine Pitrou64c16c32013-03-23 20:30:39 +01001# -*- coding: utf-8 -*-
2
Raymond Hettingerbad3c882010-09-09 12:31:00 +00003import collections
Serhiy Storchakaf3fa3082015-03-26 08:43:21 +02004import io
Raymond Hettingerbad3c882010-09-09 12:31:00 +00005import itertools
Serhiy Storchakaf3fa3082015-03-26 08:43:21 +02006import pprint
7import random
8import test.support
9import test.test_set
Serhiy Storchaka87eb4822015-03-24 19:31:50 +020010import types
Serhiy Storchakaf3fa3082015-03-26 08:43:21 +020011import unittest
Tim Petersa814db52001-05-14 07:05:58 +000012
Walter Dörwald7a7ede52003-12-03 20:15:28 +000013# list, tuple and dict subclasses that do or don't overwrite __repr__
14class list2(list):
15 pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000016
Walter Dörwald7a7ede52003-12-03 20:15:28 +000017class list3(list):
18 def __repr__(self):
19 return list.__repr__(self)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000020
Walter Dörwald7a7ede52003-12-03 20:15:28 +000021class tuple2(tuple):
22 pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000023
Walter Dörwald7a7ede52003-12-03 20:15:28 +000024class tuple3(tuple):
25 def __repr__(self):
26 return tuple.__repr__(self)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000027
Serhiy Storchaka51844382013-10-02 11:40:49 +030028class set2(set):
29 pass
30
31class set3(set):
32 def __repr__(self):
33 return set.__repr__(self)
34
35class frozenset2(frozenset):
36 pass
37
38class frozenset3(frozenset):
39 def __repr__(self):
40 return frozenset.__repr__(self)
41
Walter Dörwald7a7ede52003-12-03 20:15:28 +000042class dict2(dict):
43 pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000044
Walter Dörwald7a7ede52003-12-03 20:15:28 +000045class dict3(dict):
46 def __repr__(self):
47 return dict.__repr__(self)
Tim Petersa814db52001-05-14 07:05:58 +000048
Raymond Hettingera7da1662009-11-19 01:07:05 +000049class Unorderable:
50 def __repr__(self):
51 return str(id(self))
52
Serhiy Storchaka62aa7dc2015-04-06 22:52:44 +030053# Class Orderable is orderable with any type
54class Orderable:
55 def __init__(self, hash):
56 self._hash = hash
57 def __lt__(self, other):
58 return False
59 def __gt__(self, other):
60 return self != other
61 def __le__(self, other):
62 return self == other
63 def __ge__(self, other):
64 return True
65 def __eq__(self, other):
66 return self is other
67 def __ne__(self, other):
68 return self is not other
69 def __hash__(self):
70 return self._hash
71
Fred Drake43913dd2001-05-14 17:41:20 +000072class QueryTestCase(unittest.TestCase):
Tim Petersa814db52001-05-14 07:05:58 +000073
Fred Drake43913dd2001-05-14 17:41:20 +000074 def setUp(self):
Guido van Rossum805365e2007-05-07 22:24:25 +000075 self.a = list(range(100))
76 self.b = list(range(200))
Fred Drake43913dd2001-05-14 17:41:20 +000077 self.a[-12] = self.b
Tim Petersa814db52001-05-14 07:05:58 +000078
Serhiy Storchakaf3fa3082015-03-26 08:43:21 +020079 def test_init(self):
80 pp = pprint.PrettyPrinter()
81 pp = pprint.PrettyPrinter(indent=4, width=40, depth=5,
82 stream=io.StringIO(), compact=True)
83 pp = pprint.PrettyPrinter(4, 40, 5, io.StringIO())
84 with self.assertRaises(TypeError):
85 pp = pprint.PrettyPrinter(4, 40, 5, io.StringIO(), True)
86 self.assertRaises(ValueError, pprint.PrettyPrinter, indent=-1)
87 self.assertRaises(ValueError, pprint.PrettyPrinter, depth=0)
88 self.assertRaises(ValueError, pprint.PrettyPrinter, depth=-1)
89 self.assertRaises(ValueError, pprint.PrettyPrinter, width=0)
90
Fred Drake43913dd2001-05-14 17:41:20 +000091 def test_basic(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000092 # Verify .isrecursive() and .isreadable() w/o recursion
Fred Drakeb456e4f2002-12-31 07:16:16 +000093 pp = pprint.PrettyPrinter()
Serhiy Storchakacbfe07e2015-05-20 19:37:10 +030094 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, b"def",
95 bytearray(b"ghi"), True, False, None, ...,
Fred Drake43913dd2001-05-14 17:41:20 +000096 self.a, self.b):
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,))
Tim Petersa814db52001-05-14 07:05:58 +0000107
Fred Drake43913dd2001-05-14 17:41:20 +0000108 def test_knotted(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000109 # Verify .isrecursive() and .isreadable() w/ recursion
Fred Drake43913dd2001-05-14 17:41:20 +0000110 # Tie a knot.
111 self.b[67] = self.a
112 # Messy dict.
113 self.d = {}
114 self.d[0] = self.d[1] = self.d[2] = self.d
Tim Petersa814db52001-05-14 07:05:58 +0000115
Fred Drakeb456e4f2002-12-31 07:16:16 +0000116 pp = pprint.PrettyPrinter()
Tim Petersa814db52001-05-14 07:05:58 +0000117
Fred Drake43913dd2001-05-14 17:41:20 +0000118 for icky in self.a, self.b, self.d, (self.d, self.d):
Ezio Melottib19f43d2010-01-24 20:59:24 +0000119 self.assertTrue(pprint.isrecursive(icky), "expected isrecursive")
120 self.assertFalse(pprint.isreadable(icky), "expected not isreadable")
121 self.assertTrue(pp.isrecursive(icky), "expected isrecursive")
122 self.assertFalse(pp.isreadable(icky), "expected not isreadable")
Fred Drake43913dd2001-05-14 17:41:20 +0000123
124 # Break the cycles.
125 self.d.clear()
126 del self.a[:]
127 del self.b[:]
128
129 for safe in self.a, self.b, self.d, (self.d, self.d):
Fred Drakeb456e4f2002-12-31 07:16:16 +0000130 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +0000131 self.assertFalse(pprint.isrecursive(safe),
132 "expected not isrecursive for %r" % (safe,))
133 self.assertTrue(pprint.isreadable(safe),
134 "expected isreadable for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +0000135 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +0000136 self.assertFalse(pp.isrecursive(safe),
137 "expected not isrecursive for %r" % (safe,))
138 self.assertTrue(pp.isreadable(safe),
139 "expected isreadable for %r" % (safe,))
Fred Drake43913dd2001-05-14 17:41:20 +0000140
141 def test_unreadable(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000142 # Not recursive but not readable anyway
Fred Drakeb456e4f2002-12-31 07:16:16 +0000143 pp = pprint.PrettyPrinter()
Fred Drake43913dd2001-05-14 17:41:20 +0000144 for unreadable in type(3), pprint, pprint.isrecursive:
Fred Drakeb456e4f2002-12-31 07:16:16 +0000145 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +0000146 self.assertFalse(pprint.isrecursive(unreadable),
147 "expected not isrecursive for %r" % (unreadable,))
148 self.assertFalse(pprint.isreadable(unreadable),
149 "expected not isreadable for %r" % (unreadable,))
Fred Drakeb456e4f2002-12-31 07:16:16 +0000150 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +0000151 self.assertFalse(pp.isrecursive(unreadable),
152 "expected not isrecursive for %r" % (unreadable,))
153 self.assertFalse(pp.isreadable(unreadable),
154 "expected not isreadable for %r" % (unreadable,))
Fred Drake43913dd2001-05-14 17:41:20 +0000155
Tim Peters95b3f782001-05-14 18:39:41 +0000156 def test_same_as_repr(self):
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000157 # Simple objects, small containers and classes that overwrite __repr__
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000158 # For those the result should be the same as repr().
159 # Ahem. The docs don't say anything about that -- this appears to
160 # be testing an implementation quirk. Starting in Python 2.5, it's
161 # not true for dicts: pprint always sorts dicts by key now; before,
162 # it sorted a dict display if and only if the display required
163 # multiple lines. For that reason, dicts with more than one element
164 # aren't tested here.
Serhiy Storchakacbfe07e2015-05-20 19:37:10 +0300165 for simple in (0, 0, 0+0j, 0.0, "", b"", bytearray(),
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000166 (), tuple2(), tuple3(),
167 [], list2(), list3(),
Serhiy Storchaka51844382013-10-02 11:40:49 +0300168 set(), set2(), set3(),
169 frozenset(), frozenset2(), frozenset3(),
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000170 {}, dict2(), dict3(),
Ezio Melottib19f43d2010-01-24 20:59:24 +0000171 self.assertTrue, pprint,
Serhiy Storchakacbfe07e2015-05-20 19:37:10 +0300172 -6, -6, -6-6j, -1.5, "x", b"x", bytearray(b"x"),
173 (3,), [3], {3: 6},
Benjamin Petersond23f8222009-04-05 19:13:16 +0000174 (1,2), [3,4], {5: 6},
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000175 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
176 [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
Serhiy Storchaka51844382013-10-02 11:40:49 +0300177 set({7}), set2({7}), set3({7}),
178 frozenset({8}), frozenset2({8}), frozenset3({8}),
Benjamin Petersond23f8222009-04-05 19:13:16 +0000179 dict2({5: 6}), dict3({5: 6}),
Serhiy Storchakacbfe07e2015-05-20 19:37:10 +0300180 range(10, -11, -1),
181 True, False, None, ...,
Tim Peters95b3f782001-05-14 18:39:41 +0000182 ):
183 native = repr(simple)
Serhiy Storchaka51844382013-10-02 11:40:49 +0300184 self.assertEqual(pprint.pformat(simple), native)
185 self.assertEqual(pprint.pformat(simple, width=1, indent=0)
186 .replace('\n', ' '), native)
187 self.assertEqual(pprint.saferepr(simple), native)
Fred Drake43913dd2001-05-14 17:41:20 +0000188
Barry Warsaw00859c02001-11-28 05:49:39 +0000189 def test_basic_line_wrap(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000190 # verify basic line-wrapping operation
Barry Warsaw00859c02001-11-28 05:49:39 +0000191 o = {'RPM_cal': 0,
192 'RPM_cal2': 48059,
193 'Speed_cal': 0,
194 'controldesk_runtime_us': 0,
195 'main_code_runtime_us': 0,
196 'read_io_runtime_us': 0,
197 'write_io_runtime_us': 43690}
198 exp = """\
199{'RPM_cal': 0,
200 'RPM_cal2': 48059,
201 'Speed_cal': 0,
202 'controldesk_runtime_us': 0,
203 'main_code_runtime_us': 0,
204 'read_io_runtime_us': 0,
205 'write_io_runtime_us': 43690}"""
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000206 for type in [dict, dict2]:
207 self.assertEqual(pprint.pformat(type(o)), exp)
208
209 o = range(100)
210 exp = '[%s]' % ',\n '.join(map(str, o))
211 for type in [list, list2]:
212 self.assertEqual(pprint.pformat(type(o)), exp)
213
214 o = tuple(range(100))
215 exp = '(%s)' % ',\n '.join(map(str, o))
216 for type in [tuple, tuple2]:
217 self.assertEqual(pprint.pformat(type(o)), exp)
Barry Warsaw00859c02001-11-28 05:49:39 +0000218
Walter Dörwaldc8de4582003-12-03 20:26:05 +0000219 # indent parameter
220 o = range(100)
221 exp = '[ %s]' % ',\n '.join(map(str, o))
222 for type in [list, list2]:
223 self.assertEqual(pprint.pformat(type(o), indent=4), exp)
224
Georg Brandl3ccb7872008-07-16 03:00:45 +0000225 def test_nested_indentations(self):
226 o1 = list(range(10))
227 o2 = dict(first=1, second=2, third=3)
228 o = [o1, o2]
229 expected = """\
230[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
Serhiy Storchakaa750ce32015-02-14 10:55:19 +0200231 {'first': 1, 'second': 2, 'third': 3}]"""
232 self.assertEqual(pprint.pformat(o, indent=4, width=42), expected)
233 expected = """\
234[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
Georg Brandl3ccb7872008-07-16 03:00:45 +0000235 { 'first': 1,
236 'second': 2,
237 'third': 3}]"""
Serhiy Storchakaa750ce32015-02-14 10:55:19 +0200238 self.assertEqual(pprint.pformat(o, indent=4, width=41), expected)
239
240 def test_width(self):
241 expected = """\
242[[[[[[1, 2, 3],
243 '1 2']]]],
244 {1: [1, 2, 3],
245 2: [12, 34]},
246 'abc def ghi',
247 ('ab cd ef',),
248 set2({1, 23}),
249 [[[[[1, 2, 3],
250 '1 2']]]]]"""
251 o = eval(expected)
252 self.assertEqual(pprint.pformat(o, width=15), expected)
253 self.assertEqual(pprint.pformat(o, width=16), expected)
254 self.assertEqual(pprint.pformat(o, width=25), expected)
255 self.assertEqual(pprint.pformat(o, width=14), """\
256[[[[[[1,
257 2,
258 3],
259 '1 '
260 '2']]]],
261 {1: [1,
262 2,
263 3],
264 2: [12,
265 34]},
266 'abc def '
267 'ghi',
268 ('ab cd '
269 'ef',),
270 set2({1,
271 23}),
272 [[[[[1,
273 2,
274 3],
275 '1 '
276 '2']]]]]""")
Georg Brandl3ccb7872008-07-16 03:00:45 +0000277
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000278 def test_sorted_dict(self):
279 # Starting in Python 2.5, pprint sorts dict displays by key regardless
280 # of how small the dictionary may be.
281 # Before the change, on 32-bit Windows pformat() gave order
282 # 'a', 'c', 'b' here, so this test failed.
283 d = {'a': 1, 'b': 1, 'c': 1}
284 self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}")
285 self.assertEqual(pprint.pformat([d, d]),
286 "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]")
287
288 # The next one is kind of goofy. The sorted order depends on the
289 # alphabetic order of type names: "int" < "str" < "tuple". Before
290 # Python 2.5, this was in the test_same_as_repr() test. It's worth
291 # keeping around for now because it's one of few tests of pprint
292 # against a crazy mix of types.
293 self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}),
294 r"{5: [[]], 'xy\tab\n': (3,), (): {}}")
295
Raymond Hettingerbad3c882010-09-09 12:31:00 +0000296 def test_ordered_dict(self):
Serhiy Storchakaaa4c36f2015-03-26 08:51:33 +0200297 d = collections.OrderedDict()
298 self.assertEqual(pprint.pformat(d, width=1), 'OrderedDict()')
299 d = collections.OrderedDict([])
300 self.assertEqual(pprint.pformat(d, width=1), 'OrderedDict()')
Raymond Hettingerbad3c882010-09-09 12:31:00 +0000301 words = 'the quick brown fox jumped over a lazy dog'.split()
302 d = collections.OrderedDict(zip(words, itertools.count()))
303 self.assertEqual(pprint.pformat(d),
304"""\
Serhiy Storchakaaa4c36f2015-03-26 08:51:33 +0200305OrderedDict([('the', 0),
306 ('quick', 1),
307 ('brown', 2),
308 ('fox', 3),
309 ('jumped', 4),
310 ('over', 5),
311 ('a', 6),
312 ('lazy', 7),
313 ('dog', 8)])""")
Serhiy Storchaka87eb4822015-03-24 19:31:50 +0200314
315 def test_mapping_proxy(self):
316 words = 'the quick brown fox jumped over a lazy dog'.split()
317 d = dict(zip(words, itertools.count()))
318 m = types.MappingProxyType(d)
319 self.assertEqual(pprint.pformat(m), """\
320mappingproxy({'a': 6,
321 'brown': 2,
322 'dog': 8,
323 'fox': 3,
324 'jumped': 4,
325 'lazy': 7,
326 'over': 5,
327 'quick': 1,
328 'the': 0})""")
329 d = collections.OrderedDict(zip(words, itertools.count()))
330 m = types.MappingProxyType(d)
331 self.assertEqual(pprint.pformat(m), """\
Serhiy Storchakaaa4c36f2015-03-26 08:51:33 +0200332mappingproxy(OrderedDict([('the', 0),
333 ('quick', 1),
334 ('brown', 2),
335 ('fox', 3),
336 ('jumped', 4),
337 ('over', 5),
338 ('a', 6),
339 ('lazy', 7),
340 ('dog', 8)]))""")
Serhiy Storchaka87eb4822015-03-24 19:31:50 +0200341
Fred Drakeaee113d2002-04-02 05:08:35 +0000342 def test_subclassing(self):
343 o = {'names with spaces': 'should be presented using repr()',
344 'others.should.not.be': 'like.this'}
345 exp = """\
346{'names with spaces': 'should be presented using repr()',
347 others.should.not.be: like.this}"""
348 self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
349
Serhiy Storchaka51844382013-10-02 11:40:49 +0300350 def test_set_reprs(self):
351 self.assertEqual(pprint.pformat(set()), 'set()')
352 self.assertEqual(pprint.pformat(set(range(3))), '{0, 1, 2}')
353 self.assertEqual(pprint.pformat(set(range(7)), width=20), '''\
354{0,
355 1,
356 2,
357 3,
358 4,
359 5,
360 6}''')
361 self.assertEqual(pprint.pformat(set2(range(7)), width=20), '''\
362set2({0,
363 1,
364 2,
365 3,
366 4,
367 5,
368 6})''')
369 self.assertEqual(pprint.pformat(set3(range(7)), width=20),
370 'set3({0, 1, 2, 3, 4, 5, 6})')
371
372 self.assertEqual(pprint.pformat(frozenset()), 'frozenset()')
373 self.assertEqual(pprint.pformat(frozenset(range(3))),
374 'frozenset({0, 1, 2})')
375 self.assertEqual(pprint.pformat(frozenset(range(7)), width=20), '''\
376frozenset({0,
377 1,
378 2,
379 3,
380 4,
381 5,
382 6})''')
383 self.assertEqual(pprint.pformat(frozenset2(range(7)), width=20), '''\
384frozenset2({0,
385 1,
386 2,
387 3,
388 4,
389 5,
390 6})''')
391 self.assertEqual(pprint.pformat(frozenset3(range(7)), width=20),
392 'frozenset3({0, 1, 2, 3, 4, 5, 6})')
393
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400394 @unittest.expectedFailure
395 #See http://bugs.python.org/issue13907
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000396 @test.support.cpython_only
Serhiy Storchaka51844382013-10-02 11:40:49 +0300397 def test_set_of_sets_reprs(self):
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000398 # This test creates a complex arrangement of frozensets and
399 # compares the pretty-printed repr against a string hard-coded in
400 # the test. The hard-coded repr depends on the sort order of
401 # frozensets.
402 #
403 # However, as the docs point out: "Since sets only define
404 # partial ordering (subset relationships), the output of the
405 # list.sort() method is undefined for lists of sets."
406 #
407 # In a nutshell, the test assumes frozenset({0}) will always
408 # sort before frozenset({1}), but:
409 #
410 # >>> frozenset({0}) < frozenset({1})
411 # False
412 # >>> frozenset({1}) < frozenset({0})
413 # False
414 #
415 # Consequently, this test is fragile and
416 # implementation-dependent. Small changes to Python's sort
417 # algorithm cause the test to fail when it should pass.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400418 # XXX Or changes to the dictionary implmentation...
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000419
Christian Heimes969fe572008-01-25 11:23:10 +0000420 cube_repr_tgt = """\
421{frozenset(): frozenset({frozenset({2}), frozenset({0}), frozenset({1})}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000422 frozenset({0}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000423 frozenset({0, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000424 frozenset({0, 1})}),
425 frozenset({1}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000426 frozenset({1, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000427 frozenset({0, 1})}),
428 frozenset({2}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000429 frozenset({1, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000430 frozenset({0, 2})}),
431 frozenset({1, 2}): frozenset({frozenset({2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000432 frozenset({1}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000433 frozenset({0, 1, 2})}),
434 frozenset({0, 2}): frozenset({frozenset({2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000435 frozenset({0}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000436 frozenset({0, 1, 2})}),
437 frozenset({0, 1}): frozenset({frozenset({0}),
Christian Heimes969fe572008-01-25 11:23:10 +0000438 frozenset({1}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000439 frozenset({0, 1, 2})}),
440 frozenset({0, 1, 2}): frozenset({frozenset({1, 2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000441 frozenset({0, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000442 frozenset({0, 1})})}"""
Christian Heimes969fe572008-01-25 11:23:10 +0000443 cube = test.test_set.cube(3)
444 self.assertEqual(pprint.pformat(cube), cube_repr_tgt)
445 cubo_repr_tgt = """\
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000446{frozenset({frozenset({0, 2}), frozenset({0})}): frozenset({frozenset({frozenset({0,
447 2}),
448 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000449 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000450 2})}),
451 frozenset({frozenset({0}),
452 frozenset({0,
453 1})}),
454 frozenset({frozenset(),
455 frozenset({0})}),
456 frozenset({frozenset({2}),
457 frozenset({0,
458 2})})}),
459 frozenset({frozenset({0, 1}), frozenset({1})}): frozenset({frozenset({frozenset({0,
460 1}),
461 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000462 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000463 2})}),
464 frozenset({frozenset({0}),
465 frozenset({0,
466 1})}),
467 frozenset({frozenset({1}),
468 frozenset({1,
469 2})}),
470 frozenset({frozenset(),
471 frozenset({1})})}),
472 frozenset({frozenset({1, 2}), frozenset({1})}): frozenset({frozenset({frozenset({1,
473 2}),
474 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000475 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000476 2})}),
477 frozenset({frozenset({2}),
478 frozenset({1,
479 2})}),
480 frozenset({frozenset(),
481 frozenset({1})}),
482 frozenset({frozenset({1}),
483 frozenset({0,
484 1})})}),
485 frozenset({frozenset({1, 2}), frozenset({2})}): frozenset({frozenset({frozenset({1,
486 2}),
487 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000488 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000489 2})}),
490 frozenset({frozenset({1}),
491 frozenset({1,
492 2})}),
493 frozenset({frozenset({2}),
494 frozenset({0,
495 2})}),
496 frozenset({frozenset(),
497 frozenset({2})})}),
498 frozenset({frozenset(), frozenset({0})}): frozenset({frozenset({frozenset({0}),
499 frozenset({0,
500 1})}),
501 frozenset({frozenset({0}),
502 frozenset({0,
503 2})}),
504 frozenset({frozenset(),
505 frozenset({1})}),
506 frozenset({frozenset(),
507 frozenset({2})})}),
508 frozenset({frozenset(), frozenset({1})}): frozenset({frozenset({frozenset(),
509 frozenset({0})}),
510 frozenset({frozenset({1}),
511 frozenset({1,
512 2})}),
513 frozenset({frozenset(),
514 frozenset({2})}),
515 frozenset({frozenset({1}),
516 frozenset({0,
517 1})})}),
518 frozenset({frozenset({2}), frozenset()}): frozenset({frozenset({frozenset({2}),
519 frozenset({1,
520 2})}),
521 frozenset({frozenset(),
522 frozenset({0})}),
523 frozenset({frozenset(),
524 frozenset({1})}),
525 frozenset({frozenset({2}),
526 frozenset({0,
527 2})})}),
528 frozenset({frozenset({0, 1, 2}), frozenset({0, 1})}): frozenset({frozenset({frozenset({1,
529 2}),
530 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000531 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000532 2})}),
533 frozenset({frozenset({0,
534 2}),
535 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000536 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000537 2})}),
538 frozenset({frozenset({0}),
539 frozenset({0,
540 1})}),
541 frozenset({frozenset({1}),
542 frozenset({0,
543 1})})}),
544 frozenset({frozenset({0}), frozenset({0, 1})}): frozenset({frozenset({frozenset(),
545 frozenset({0})}),
546 frozenset({frozenset({0,
547 1}),
548 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000549 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000550 2})}),
551 frozenset({frozenset({0}),
552 frozenset({0,
553 2})}),
554 frozenset({frozenset({1}),
555 frozenset({0,
556 1})})}),
557 frozenset({frozenset({2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({0,
558 2}),
559 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000560 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000561 2})}),
562 frozenset({frozenset({2}),
563 frozenset({1,
564 2})}),
565 frozenset({frozenset({0}),
566 frozenset({0,
567 2})}),
568 frozenset({frozenset(),
569 frozenset({2})})}),
570 frozenset({frozenset({0, 1, 2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({1,
571 2}),
572 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000573 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000574 2})}),
575 frozenset({frozenset({0,
576 1}),
577 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000578 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000579 2})}),
580 frozenset({frozenset({0}),
581 frozenset({0,
582 2})}),
583 frozenset({frozenset({2}),
584 frozenset({0,
585 2})})}),
586 frozenset({frozenset({1, 2}), frozenset({0, 1, 2})}): frozenset({frozenset({frozenset({0,
587 2}),
588 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000589 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000590 2})}),
591 frozenset({frozenset({0,
592 1}),
593 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000594 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000595 2})}),
596 frozenset({frozenset({2}),
597 frozenset({1,
598 2})}),
599 frozenset({frozenset({1}),
600 frozenset({1,
601 2})})})}"""
Christian Heimes969fe572008-01-25 11:23:10 +0000602
603 cubo = test.test_set.linegraph(cube)
604 self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt)
605
Alexandre Vassalottieca20b62008-05-16 02:54:33 +0000606 def test_depth(self):
607 nested_tuple = (1, (2, (3, (4, (5, 6)))))
608 nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}}
609 nested_list = [1, [2, [3, [4, [5, [6, []]]]]]]
610 self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple))
611 self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict))
612 self.assertEqual(pprint.pformat(nested_list), repr(nested_list))
613
614 lv1_tuple = '(1, (...))'
615 lv1_dict = '{1: {...}}'
616 lv1_list = '[1, [...]]'
617 self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple)
618 self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict)
619 self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list)
620
Raymond Hettingera7da1662009-11-19 01:07:05 +0000621 def test_sort_unorderable_values(self):
622 # Issue 3976: sorted pprints fail for unorderable values.
623 n = 20
624 keys = [Unorderable() for i in range(n)]
625 random.shuffle(keys)
626 skeys = sorted(keys, key=id)
627 clean = lambda s: s.replace(' ', '').replace('\n','')
628
629 self.assertEqual(clean(pprint.pformat(set(keys))),
630 '{' + ','.join(map(repr, skeys)) + '}')
631 self.assertEqual(clean(pprint.pformat(frozenset(keys))),
632 'frozenset({' + ','.join(map(repr, skeys)) + '})')
633 self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys))),
634 '{' + ','.join('%r:None' % k for k in skeys) + '}')
Fred Drakeaee113d2002-04-02 05:08:35 +0000635
Florent Xiclunad6da90f2012-07-21 11:17:38 +0200636 # Issue 10017: TypeError on user-defined types as dict keys.
637 self.assertEqual(pprint.pformat({Unorderable: 0, 1: 0}),
638 '{1: 0, ' + repr(Unorderable) +': 0}')
639
640 # Issue 14998: TypeError on tuples with NoneTypes as dict keys.
Florent Xicluna6e571d62012-07-21 12:44:20 +0200641 keys = [(1,), (None,)]
642 self.assertEqual(pprint.pformat(dict.fromkeys(keys, 0)),
643 '{%r: 0, %r: 0}' % tuple(sorted(keys, key=id)))
Florent Xiclunad6da90f2012-07-21 11:17:38 +0200644
Serhiy Storchaka62aa7dc2015-04-06 22:52:44 +0300645 def test_sort_orderable_and_unorderable_values(self):
646 # Issue 22721: sorted pprints is not stable
647 a = Unorderable()
648 b = Orderable(hash(a)) # should have the same hash value
649 # self-test
650 self.assertLess(a, b)
651 self.assertLess(str(type(b)), str(type(a)))
652 self.assertEqual(sorted([b, a]), [a, b])
653 self.assertEqual(sorted([a, b]), [a, b])
654 # set
655 self.assertEqual(pprint.pformat(set([b, a]), width=1),
656 '{%r,\n %r}' % (a, b))
657 self.assertEqual(pprint.pformat(set([a, b]), width=1),
658 '{%r,\n %r}' % (a, b))
659 # dict
660 self.assertEqual(pprint.pformat(dict.fromkeys([b, a]), width=1),
661 '{%r: None,\n %r: None}' % (a, b))
662 self.assertEqual(pprint.pformat(dict.fromkeys([a, b]), width=1),
663 '{%r: None,\n %r: None}' % (a, b))
664
Antoine Pitrou64c16c32013-03-23 20:30:39 +0100665 def test_str_wrap(self):
666 # pprint tries to wrap strings intelligently
667 fox = 'the quick brown fox jumped over a lazy dog'
Serhiy Storchakaa750ce32015-02-14 10:55:19 +0200668 self.assertEqual(pprint.pformat(fox, width=19), """\
669('the quick brown '
670 'fox jumped over '
671 'a lazy dog')""")
Antoine Pitrou64c16c32013-03-23 20:30:39 +0100672 self.assertEqual(pprint.pformat({'a': 1, 'b': fox, 'c': 2},
Serhiy Storchakaa750ce32015-02-14 10:55:19 +0200673 width=25), """\
Antoine Pitrou64c16c32013-03-23 20:30:39 +0100674{'a': 1,
675 'b': 'the quick brown '
676 'fox jumped over '
677 'a lazy dog',
678 'c': 2}""")
679 # With some special characters
680 # - \n always triggers a new line in the pprint
681 # - \t and \n are escaped
682 # - non-ASCII is allowed
683 # - an apostrophe doesn't disrupt the pprint
684 special = "Portons dix bons \"whiskys\"\nà l'avocat goujat\t qui fumait au zoo"
Serhiy Storchakaa750ce32015-02-14 10:55:19 +0200685 self.assertEqual(pprint.pformat(special, width=68), repr(special))
686 self.assertEqual(pprint.pformat(special, width=31), """\
687('Portons dix bons "whiskys"\\n'
688 "à l'avocat goujat\\t qui "
689 'fumait au zoo')""")
690 self.assertEqual(pprint.pformat(special, width=20), """\
691('Portons dix bons '
692 '"whiskys"\\n'
Serhiy Storchakafe3dc372014-12-20 20:57:15 +0200693 "à l'avocat "
694 'goujat\\t qui '
695 'fumait au zoo')""")
Serhiy Storchakaa750ce32015-02-14 10:55:19 +0200696 self.assertEqual(pprint.pformat([[[[[special]]]]], width=35), """\
697[[[[['Portons dix bons "whiskys"\\n'
698 "à l'avocat goujat\\t qui "
699 'fumait au zoo']]]]]""")
700 self.assertEqual(pprint.pformat([[[[[special]]]]], width=25), """\
701[[[[['Portons dix bons '
702 '"whiskys"\\n'
703 "à l'avocat "
704 'goujat\\t qui '
705 'fumait au zoo']]]]]""")
706 self.assertEqual(pprint.pformat([[[[[special]]]]], width=23), """\
707[[[[['Portons dix '
708 'bons "whiskys"\\n'
709 "à l'avocat "
710 'goujat\\t qui '
711 'fumait au '
712 'zoo']]]]]""")
Antoine Pitrou64c16c32013-03-23 20:30:39 +0100713 # An unwrappable string is formatted as its repr
714 unwrappable = "x" * 100
715 self.assertEqual(pprint.pformat(unwrappable, width=80), repr(unwrappable))
716 self.assertEqual(pprint.pformat(''), "''")
717 # Check that the pprint is a usable repr
718 special *= 10
719 for width in range(3, 40):
720 formatted = pprint.pformat(special, width=width)
Serhiy Storchakafe3dc372014-12-20 20:57:15 +0200721 self.assertEqual(eval(formatted), special)
722 formatted = pprint.pformat([special] * 2, width=width)
723 self.assertEqual(eval(formatted), [special] * 2)
Antoine Pitrou64c16c32013-03-23 20:30:39 +0100724
Serhiy Storchaka7c411a42013-10-02 11:56:18 +0300725 def test_compact(self):
726 o = ([list(range(i * i)) for i in range(5)] +
727 [list(range(i)) for i in range(6)])
728 expected = """\
729[[], [0], [0, 1, 2, 3],
730 [0, 1, 2, 3, 4, 5, 6, 7, 8],
731 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
732 14, 15],
733 [], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3],
734 [0, 1, 2, 3, 4]]"""
Serhiy Storchakaa750ce32015-02-14 10:55:19 +0200735 self.assertEqual(pprint.pformat(o, width=47, compact=True), expected)
736
737 def test_compact_width(self):
738 levels = 20
739 number = 10
740 o = [0] * number
741 for i in range(levels - 1):
742 o = [o]
743 for w in range(levels * 2 + 1, levels + 3 * number - 1):
744 lines = pprint.pformat(o, width=w, compact=True).splitlines()
745 maxwidth = max(map(len, lines))
746 self.assertLessEqual(maxwidth, w)
747 self.assertGreater(maxwidth, w - 3)
Serhiy Storchaka7c411a42013-10-02 11:56:18 +0300748
Serhiy Storchaka022f2032015-03-24 19:22:37 +0200749 def test_bytes_wrap(self):
750 self.assertEqual(pprint.pformat(b'', width=1), "b''")
751 self.assertEqual(pprint.pformat(b'abcd', width=1), "b'abcd'")
752 letters = b'abcdefghijklmnopqrstuvwxyz'
753 self.assertEqual(pprint.pformat(letters, width=29), repr(letters))
754 self.assertEqual(pprint.pformat(letters, width=19), """\
755(b'abcdefghijkl'
756 b'mnopqrstuvwxyz')""")
757 self.assertEqual(pprint.pformat(letters, width=18), """\
758(b'abcdefghijkl'
759 b'mnopqrstuvwx'
760 b'yz')""")
761 self.assertEqual(pprint.pformat(letters, width=16), """\
762(b'abcdefghijkl'
763 b'mnopqrstuvwx'
764 b'yz')""")
765 special = bytes(range(16))
766 self.assertEqual(pprint.pformat(special, width=61), repr(special))
767 self.assertEqual(pprint.pformat(special, width=48), """\
768(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'
769 b'\\x0c\\r\\x0e\\x0f')""")
770 self.assertEqual(pprint.pformat(special, width=32), """\
771(b'\\x00\\x01\\x02\\x03'
772 b'\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'
773 b'\\x0c\\r\\x0e\\x0f')""")
774 self.assertEqual(pprint.pformat(special, width=1), """\
775(b'\\x00\\x01\\x02\\x03'
776 b'\\x04\\x05\\x06\\x07'
777 b'\\x08\\t\\n\\x0b'
778 b'\\x0c\\r\\x0e\\x0f')""")
779 self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2},
780 width=21), """\
781{'a': 1,
782 'b': b'abcdefghijkl'
783 b'mnopqrstuvwx'
784 b'yz',
785 'c': 2}""")
786 self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2},
787 width=20), """\
788{'a': 1,
789 'b': b'abcdefgh'
790 b'ijklmnop'
791 b'qrstuvwxyz',
792 'c': 2}""")
793 self.assertEqual(pprint.pformat([[[[[[letters]]]]]], width=25), """\
794[[[[[[b'abcdefghijklmnop'
795 b'qrstuvwxyz']]]]]]""")
796 self.assertEqual(pprint.pformat([[[[[[special]]]]]], width=41), """\
797[[[[[[b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07'
798 b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f']]]]]]""")
799 # Check that the pprint is a usable repr
800 for width in range(1, 64):
801 formatted = pprint.pformat(special, width=width)
802 self.assertEqual(eval(formatted), special)
803 formatted = pprint.pformat([special] * 2, width=width)
804 self.assertEqual(eval(formatted), [special] * 2)
805
806 def test_bytearray_wrap(self):
807 self.assertEqual(pprint.pformat(bytearray(), width=1), "bytearray(b'')")
808 letters = bytearray(b'abcdefghijklmnopqrstuvwxyz')
809 self.assertEqual(pprint.pformat(letters, width=40), repr(letters))
810 self.assertEqual(pprint.pformat(letters, width=28), """\
811bytearray(b'abcdefghijkl'
812 b'mnopqrstuvwxyz')""")
813 self.assertEqual(pprint.pformat(letters, width=27), """\
814bytearray(b'abcdefghijkl'
815 b'mnopqrstuvwx'
816 b'yz')""")
817 self.assertEqual(pprint.pformat(letters, width=25), """\
818bytearray(b'abcdefghijkl'
819 b'mnopqrstuvwx'
820 b'yz')""")
821 special = bytearray(range(16))
822 self.assertEqual(pprint.pformat(special, width=72), repr(special))
823 self.assertEqual(pprint.pformat(special, width=57), """\
824bytearray(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'
825 b'\\x0c\\r\\x0e\\x0f')""")
826 self.assertEqual(pprint.pformat(special, width=41), """\
827bytearray(b'\\x00\\x01\\x02\\x03'
828 b'\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'
829 b'\\x0c\\r\\x0e\\x0f')""")
830 self.assertEqual(pprint.pformat(special, width=1), """\
831bytearray(b'\\x00\\x01\\x02\\x03'
832 b'\\x04\\x05\\x06\\x07'
833 b'\\x08\\t\\n\\x0b'
834 b'\\x0c\\r\\x0e\\x0f')""")
835 self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2},
836 width=31), """\
837{'a': 1,
838 'b': bytearray(b'abcdefghijkl'
839 b'mnopqrstuvwx'
840 b'yz'),
841 'c': 2}""")
842 self.assertEqual(pprint.pformat([[[[[letters]]]]], width=37), """\
843[[[[[bytearray(b'abcdefghijklmnop'
844 b'qrstuvwxyz')]]]]]""")
845 self.assertEqual(pprint.pformat([[[[[special]]]]], width=50), """\
846[[[[[bytearray(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07'
847 b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f')]]]]]""")
848
Serhiy Storchakabedbf962015-05-12 13:35:48 +0300849 def test_default_dict(self):
850 d = collections.defaultdict(int)
Benjamin Petersonab078e92016-07-13 21:13:29 -0700851 self.assertEqual(pprint.pformat(d, width=1), "defaultdict(<class 'int'>, {})")
Serhiy Storchakabedbf962015-05-12 13:35:48 +0300852 words = 'the quick brown fox jumped over a lazy dog'.split()
853 d = collections.defaultdict(int, zip(words, itertools.count()))
Benjamin Petersonab078e92016-07-13 21:13:29 -0700854 self.assertEqual(pprint.pformat(d),
855"""\
856defaultdict(<class 'int'>,
Serhiy Storchakabedbf962015-05-12 13:35:48 +0300857 {'a': 6,
858 'brown': 2,
859 'dog': 8,
860 'fox': 3,
861 'jumped': 4,
862 'lazy': 7,
863 'over': 5,
864 'quick': 1,
Benjamin Petersonab078e92016-07-13 21:13:29 -0700865 'the': 0})""")
Serhiy Storchakabedbf962015-05-12 13:35:48 +0300866
867 def test_counter(self):
868 d = collections.Counter()
869 self.assertEqual(pprint.pformat(d, width=1), "Counter()")
870 d = collections.Counter('senselessness')
871 self.assertEqual(pprint.pformat(d, width=40),
872"""\
873Counter({'s': 6,
874 'e': 4,
875 'n': 2,
876 'l': 1})""")
877
878 def test_chainmap(self):
879 d = collections.ChainMap()
880 self.assertEqual(pprint.pformat(d, width=1), "ChainMap({})")
881 words = 'the quick brown fox jumped over a lazy dog'.split()
882 items = list(zip(words, itertools.count()))
883 d = collections.ChainMap(dict(items))
884 self.assertEqual(pprint.pformat(d),
885"""\
886ChainMap({'a': 6,
887 'brown': 2,
888 'dog': 8,
889 'fox': 3,
890 'jumped': 4,
891 'lazy': 7,
892 'over': 5,
893 'quick': 1,
894 'the': 0})""")
895 d = collections.ChainMap(dict(items), collections.OrderedDict(items))
896 self.assertEqual(pprint.pformat(d),
897"""\
898ChainMap({'a': 6,
899 'brown': 2,
900 'dog': 8,
901 'fox': 3,
902 'jumped': 4,
903 'lazy': 7,
904 'over': 5,
905 'quick': 1,
906 'the': 0},
907 OrderedDict([('the', 0),
908 ('quick', 1),
909 ('brown', 2),
910 ('fox', 3),
911 ('jumped', 4),
912 ('over', 5),
913 ('a', 6),
914 ('lazy', 7),
915 ('dog', 8)]))""")
916
917 def test_deque(self):
918 d = collections.deque()
919 self.assertEqual(pprint.pformat(d, width=1), "deque([])")
920 d = collections.deque(maxlen=7)
921 self.assertEqual(pprint.pformat(d, width=1), "deque([], maxlen=7)")
922 words = 'the quick brown fox jumped over a lazy dog'.split()
923 d = collections.deque(zip(words, itertools.count()))
924 self.assertEqual(pprint.pformat(d),
925"""\
926deque([('the', 0),
927 ('quick', 1),
928 ('brown', 2),
929 ('fox', 3),
930 ('jumped', 4),
931 ('over', 5),
932 ('a', 6),
933 ('lazy', 7),
934 ('dog', 8)])""")
935 d = collections.deque(zip(words, itertools.count()), maxlen=7)
936 self.assertEqual(pprint.pformat(d),
937"""\
938deque([('brown', 2),
939 ('fox', 3),
940 ('jumped', 4),
941 ('over', 5),
942 ('a', 6),
943 ('lazy', 7),
944 ('dog', 8)],
945 maxlen=7)""")
946
947 def test_user_dict(self):
948 d = collections.UserDict()
949 self.assertEqual(pprint.pformat(d, width=1), "{}")
950 words = 'the quick brown fox jumped over a lazy dog'.split()
951 d = collections.UserDict(zip(words, itertools.count()))
952 self.assertEqual(pprint.pformat(d),
953"""\
954{'a': 6,
955 'brown': 2,
956 'dog': 8,
957 'fox': 3,
958 'jumped': 4,
959 'lazy': 7,
960 'over': 5,
961 'quick': 1,
962 'the': 0}""")
963
Serhiy Storchaka265cee02015-10-29 09:52:20 +0200964 def test_user_list(self):
Serhiy Storchakabedbf962015-05-12 13:35:48 +0300965 d = collections.UserList()
966 self.assertEqual(pprint.pformat(d, width=1), "[]")
967 words = 'the quick brown fox jumped over a lazy dog'.split()
968 d = collections.UserList(zip(words, itertools.count()))
969 self.assertEqual(pprint.pformat(d),
970"""\
971[('the', 0),
972 ('quick', 1),
973 ('brown', 2),
974 ('fox', 3),
975 ('jumped', 4),
976 ('over', 5),
977 ('a', 6),
978 ('lazy', 7),
979 ('dog', 8)]""")
980
981 def test_user_string(self):
982 d = collections.UserString('')
983 self.assertEqual(pprint.pformat(d, width=1), "''")
984 d = collections.UserString('the quick brown fox jumped over a lazy dog')
985 self.assertEqual(pprint.pformat(d, width=20),
986"""\
987('the quick brown '
988 'fox jumped over '
989 'a lazy dog')""")
990 self.assertEqual(pprint.pformat({1: d}, width=20),
991"""\
992{1: 'the quick '
993 'brown fox '
994 'jumped over a '
995 'lazy dog'}""")
996
Florent Xiclunad6da90f2012-07-21 11:17:38 +0200997
Fred Drakeaee113d2002-04-02 05:08:35 +0000998class DottedPrettyPrinter(pprint.PrettyPrinter):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000999
Fred Drakeaee113d2002-04-02 05:08:35 +00001000 def format(self, object, context, maxlevels, level):
1001 if isinstance(object, str):
1002 if ' ' in object:
Walter Dörwald70a6b492004-02-12 17:35:32 +00001003 return repr(object), 1, 0
Fred Drakeaee113d2002-04-02 05:08:35 +00001004 else:
1005 return object, 0, 0
1006 else:
1007 return pprint.PrettyPrinter.format(
1008 self, object, context, maxlevels, level)
1009
1010
Fred Drake2e2be372001-09-20 21:33:42 +00001011if __name__ == "__main__":
Serhiy Storchakacbfe07e2015-05-20 19:37:10 +03001012 unittest.main()