blob: bf136dec067ecf0c01ed86ccec7b59d631882e93 [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
Walter Dörwald7a7ede52003-12-03 20:15:28 +000026class dict2(dict):
27 pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000028
Walter Dörwald7a7ede52003-12-03 20:15:28 +000029class dict3(dict):
30 def __repr__(self):
31 return dict.__repr__(self)
Tim Petersa814db52001-05-14 07:05:58 +000032
Raymond Hettingera7da1662009-11-19 01:07:05 +000033class Unorderable:
34 def __repr__(self):
35 return str(id(self))
36
Fred Drake43913dd2001-05-14 17:41:20 +000037class QueryTestCase(unittest.TestCase):
Tim Petersa814db52001-05-14 07:05:58 +000038
Fred Drake43913dd2001-05-14 17:41:20 +000039 def setUp(self):
Guido van Rossum805365e2007-05-07 22:24:25 +000040 self.a = list(range(100))
41 self.b = list(range(200))
Fred Drake43913dd2001-05-14 17:41:20 +000042 self.a[-12] = self.b
Tim Petersa814db52001-05-14 07:05:58 +000043
Fred Drake43913dd2001-05-14 17:41:20 +000044 def test_basic(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000045 # Verify .isrecursive() and .isreadable() w/o recursion
Fred Drakeb456e4f2002-12-31 07:16:16 +000046 pp = pprint.PrettyPrinter()
Walter Dörwald5de48bd2007-06-11 21:38:39 +000047 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, "yaddayadda",
Fred Drake43913dd2001-05-14 17:41:20 +000048 self.a, self.b):
Fred Drakeb456e4f2002-12-31 07:16:16 +000049 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +000050 self.assertFalse(pprint.isrecursive(safe),
51 "expected not isrecursive for %r" % (safe,))
52 self.assertTrue(pprint.isreadable(safe),
53 "expected isreadable for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +000054 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +000055 self.assertFalse(pp.isrecursive(safe),
56 "expected not isrecursive for %r" % (safe,))
57 self.assertTrue(pp.isreadable(safe),
58 "expected isreadable for %r" % (safe,))
Tim Petersa814db52001-05-14 07:05:58 +000059
Fred Drake43913dd2001-05-14 17:41:20 +000060 def test_knotted(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000061 # Verify .isrecursive() and .isreadable() w/ recursion
Fred Drake43913dd2001-05-14 17:41:20 +000062 # Tie a knot.
63 self.b[67] = self.a
64 # Messy dict.
65 self.d = {}
66 self.d[0] = self.d[1] = self.d[2] = self.d
Tim Petersa814db52001-05-14 07:05:58 +000067
Fred Drakeb456e4f2002-12-31 07:16:16 +000068 pp = pprint.PrettyPrinter()
Tim Petersa814db52001-05-14 07:05:58 +000069
Fred Drake43913dd2001-05-14 17:41:20 +000070 for icky in self.a, self.b, self.d, (self.d, self.d):
Ezio Melottib19f43d2010-01-24 20:59:24 +000071 self.assertTrue(pprint.isrecursive(icky), "expected isrecursive")
72 self.assertFalse(pprint.isreadable(icky), "expected not isreadable")
73 self.assertTrue(pp.isrecursive(icky), "expected isrecursive")
74 self.assertFalse(pp.isreadable(icky), "expected not isreadable")
Fred Drake43913dd2001-05-14 17:41:20 +000075
76 # Break the cycles.
77 self.d.clear()
78 del self.a[:]
79 del self.b[:]
80
81 for safe in self.a, self.b, self.d, (self.d, self.d):
Fred Drakeb456e4f2002-12-31 07:16:16 +000082 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +000083 self.assertFalse(pprint.isrecursive(safe),
84 "expected not isrecursive for %r" % (safe,))
85 self.assertTrue(pprint.isreadable(safe),
86 "expected isreadable for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +000087 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +000088 self.assertFalse(pp.isrecursive(safe),
89 "expected not isrecursive for %r" % (safe,))
90 self.assertTrue(pp.isreadable(safe),
91 "expected isreadable for %r" % (safe,))
Fred Drake43913dd2001-05-14 17:41:20 +000092
93 def test_unreadable(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000094 # Not recursive but not readable anyway
Fred Drakeb456e4f2002-12-31 07:16:16 +000095 pp = pprint.PrettyPrinter()
Fred Drake43913dd2001-05-14 17:41:20 +000096 for unreadable in type(3), pprint, pprint.isrecursive:
Fred Drakeb456e4f2002-12-31 07:16:16 +000097 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +000098 self.assertFalse(pprint.isrecursive(unreadable),
99 "expected not isrecursive for %r" % (unreadable,))
100 self.assertFalse(pprint.isreadable(unreadable),
101 "expected not isreadable for %r" % (unreadable,))
Fred Drakeb456e4f2002-12-31 07:16:16 +0000102 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +0000103 self.assertFalse(pp.isrecursive(unreadable),
104 "expected not isrecursive for %r" % (unreadable,))
105 self.assertFalse(pp.isreadable(unreadable),
106 "expected not isreadable for %r" % (unreadable,))
Fred Drake43913dd2001-05-14 17:41:20 +0000107
Tim Peters95b3f782001-05-14 18:39:41 +0000108 def test_same_as_repr(self):
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000109 # Simple objects, small containers and classes that overwrite __repr__
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000110 # For those the result should be the same as repr().
111 # Ahem. The docs don't say anything about that -- this appears to
112 # be testing an implementation quirk. Starting in Python 2.5, it's
113 # not true for dicts: pprint always sorts dicts by key now; before,
114 # it sorted a dict display if and only if the display required
115 # multiple lines. For that reason, dicts with more than one element
116 # aren't tested here.
Walter Dörwald5de48bd2007-06-11 21:38:39 +0000117 for simple in (0, 0, 0+0j, 0.0, "", b"",
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000118 (), tuple2(), tuple3(),
119 [], list2(), list3(),
120 {}, dict2(), dict3(),
Ezio Melottib19f43d2010-01-24 20:59:24 +0000121 self.assertTrue, pprint,
Walter Dörwald5de48bd2007-06-11 21:38:39 +0000122 -6, -6, -6-6j, -1.5, "x", b"x", (3,), [3], {3: 6},
Benjamin Petersond23f8222009-04-05 19:13:16 +0000123 (1,2), [3,4], {5: 6},
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000124 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
125 [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
Benjamin Petersond23f8222009-04-05 19:13:16 +0000126 dict2({5: 6}), dict3({5: 6}),
Tim Peters95b3f782001-05-14 18:39:41 +0000127 range(10, -11, -1)
128 ):
129 native = repr(simple)
130 for function in "pformat", "saferepr":
131 f = getattr(pprint, function)
132 got = f(simple)
Ezio Melottib19f43d2010-01-24 20:59:24 +0000133 self.assertEqual(native, got,
134 "expected %s got %s from pprint.%s" %
135 (native, got, function))
Fred Drake43913dd2001-05-14 17:41:20 +0000136
Barry Warsaw00859c02001-11-28 05:49:39 +0000137 def test_basic_line_wrap(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000138 # verify basic line-wrapping operation
Barry Warsaw00859c02001-11-28 05:49:39 +0000139 o = {'RPM_cal': 0,
140 'RPM_cal2': 48059,
141 'Speed_cal': 0,
142 'controldesk_runtime_us': 0,
143 'main_code_runtime_us': 0,
144 'read_io_runtime_us': 0,
145 'write_io_runtime_us': 43690}
146 exp = """\
147{'RPM_cal': 0,
148 'RPM_cal2': 48059,
149 'Speed_cal': 0,
150 'controldesk_runtime_us': 0,
151 'main_code_runtime_us': 0,
152 'read_io_runtime_us': 0,
153 'write_io_runtime_us': 43690}"""
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000154 for type in [dict, dict2]:
155 self.assertEqual(pprint.pformat(type(o)), exp)
156
157 o = range(100)
158 exp = '[%s]' % ',\n '.join(map(str, o))
159 for type in [list, list2]:
160 self.assertEqual(pprint.pformat(type(o)), exp)
161
162 o = tuple(range(100))
163 exp = '(%s)' % ',\n '.join(map(str, o))
164 for type in [tuple, tuple2]:
165 self.assertEqual(pprint.pformat(type(o)), exp)
Barry Warsaw00859c02001-11-28 05:49:39 +0000166
Walter Dörwaldc8de4582003-12-03 20:26:05 +0000167 # indent parameter
168 o = range(100)
169 exp = '[ %s]' % ',\n '.join(map(str, o))
170 for type in [list, list2]:
171 self.assertEqual(pprint.pformat(type(o), indent=4), exp)
172
Georg Brandl3ccb7872008-07-16 03:00:45 +0000173 def test_nested_indentations(self):
174 o1 = list(range(10))
175 o2 = dict(first=1, second=2, third=3)
176 o = [o1, o2]
177 expected = """\
178[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
179 { 'first': 1,
180 'second': 2,
181 'third': 3}]"""
182 self.assertEqual(pprint.pformat(o, indent=4, width=42), expected)
183
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000184 def test_sorted_dict(self):
185 # Starting in Python 2.5, pprint sorts dict displays by key regardless
186 # of how small the dictionary may be.
187 # Before the change, on 32-bit Windows pformat() gave order
188 # 'a', 'c', 'b' here, so this test failed.
189 d = {'a': 1, 'b': 1, 'c': 1}
190 self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}")
191 self.assertEqual(pprint.pformat([d, d]),
192 "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]")
193
194 # The next one is kind of goofy. The sorted order depends on the
195 # alphabetic order of type names: "int" < "str" < "tuple". Before
196 # Python 2.5, this was in the test_same_as_repr() test. It's worth
197 # keeping around for now because it's one of few tests of pprint
198 # against a crazy mix of types.
199 self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}),
200 r"{5: [[]], 'xy\tab\n': (3,), (): {}}")
201
Raymond Hettingerbad3c882010-09-09 12:31:00 +0000202 def test_ordered_dict(self):
203 words = 'the quick brown fox jumped over a lazy dog'.split()
204 d = collections.OrderedDict(zip(words, itertools.count()))
205 self.assertEqual(pprint.pformat(d),
206"""\
207{'the': 0,
208 'quick': 1,
209 'brown': 2,
210 'fox': 3,
211 'jumped': 4,
212 'over': 5,
213 'a': 6,
214 'lazy': 7,
215 'dog': 8}""")
Fred Drakeaee113d2002-04-02 05:08:35 +0000216 def test_subclassing(self):
217 o = {'names with spaces': 'should be presented using repr()',
218 'others.should.not.be': 'like.this'}
219 exp = """\
220{'names with spaces': 'should be presented using repr()',
221 others.should.not.be: like.this}"""
222 self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
223
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400224 @unittest.expectedFailure
225 #See http://bugs.python.org/issue13907
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000226 @test.support.cpython_only
Christian Heimes969fe572008-01-25 11:23:10 +0000227 def test_set_reprs(self):
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000228 # This test creates a complex arrangement of frozensets and
229 # compares the pretty-printed repr against a string hard-coded in
230 # the test. The hard-coded repr depends on the sort order of
231 # frozensets.
232 #
233 # However, as the docs point out: "Since sets only define
234 # partial ordering (subset relationships), the output of the
235 # list.sort() method is undefined for lists of sets."
236 #
237 # In a nutshell, the test assumes frozenset({0}) will always
238 # sort before frozenset({1}), but:
239 #
240 # >>> frozenset({0}) < frozenset({1})
241 # False
242 # >>> frozenset({1}) < frozenset({0})
243 # False
244 #
245 # Consequently, this test is fragile and
246 # implementation-dependent. Small changes to Python's sort
247 # algorithm cause the test to fail when it should pass.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400248 # XXX Or changes to the dictionary implmentation...
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000249
Christian Heimes969fe572008-01-25 11:23:10 +0000250 self.assertEqual(pprint.pformat(set()), 'set()')
251 self.assertEqual(pprint.pformat(set(range(3))), '{0, 1, 2}')
252 self.assertEqual(pprint.pformat(frozenset()), 'frozenset()')
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400253
Christian Heimes969fe572008-01-25 11:23:10 +0000254 self.assertEqual(pprint.pformat(frozenset(range(3))), 'frozenset({0, 1, 2})')
255 cube_repr_tgt = """\
256{frozenset(): frozenset({frozenset({2}), frozenset({0}), frozenset({1})}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000257 frozenset({0}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000258 frozenset({0, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000259 frozenset({0, 1})}),
260 frozenset({1}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000261 frozenset({1, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000262 frozenset({0, 1})}),
263 frozenset({2}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000264 frozenset({1, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000265 frozenset({0, 2})}),
266 frozenset({1, 2}): frozenset({frozenset({2}),
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, 2}): frozenset({frozenset({2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000270 frozenset({0}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000271 frozenset({0, 1, 2})}),
272 frozenset({0, 1}): frozenset({frozenset({0}),
Christian Heimes969fe572008-01-25 11:23:10 +0000273 frozenset({1}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000274 frozenset({0, 1, 2})}),
275 frozenset({0, 1, 2}): frozenset({frozenset({1, 2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000276 frozenset({0, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000277 frozenset({0, 1})})}"""
Christian Heimes969fe572008-01-25 11:23:10 +0000278 cube = test.test_set.cube(3)
279 self.assertEqual(pprint.pformat(cube), cube_repr_tgt)
280 cubo_repr_tgt = """\
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000281{frozenset({frozenset({0, 2}), frozenset({0})}): frozenset({frozenset({frozenset({0,
282 2}),
283 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000284 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000285 2})}),
286 frozenset({frozenset({0}),
287 frozenset({0,
288 1})}),
289 frozenset({frozenset(),
290 frozenset({0})}),
291 frozenset({frozenset({2}),
292 frozenset({0,
293 2})})}),
294 frozenset({frozenset({0, 1}), frozenset({1})}): frozenset({frozenset({frozenset({0,
295 1}),
296 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000297 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000298 2})}),
299 frozenset({frozenset({0}),
300 frozenset({0,
301 1})}),
302 frozenset({frozenset({1}),
303 frozenset({1,
304 2})}),
305 frozenset({frozenset(),
306 frozenset({1})})}),
307 frozenset({frozenset({1, 2}), frozenset({1})}): frozenset({frozenset({frozenset({1,
308 2}),
309 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000310 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000311 2})}),
312 frozenset({frozenset({2}),
313 frozenset({1,
314 2})}),
315 frozenset({frozenset(),
316 frozenset({1})}),
317 frozenset({frozenset({1}),
318 frozenset({0,
319 1})})}),
320 frozenset({frozenset({1, 2}), frozenset({2})}): frozenset({frozenset({frozenset({1,
321 2}),
322 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000323 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000324 2})}),
325 frozenset({frozenset({1}),
326 frozenset({1,
327 2})}),
328 frozenset({frozenset({2}),
329 frozenset({0,
330 2})}),
331 frozenset({frozenset(),
332 frozenset({2})})}),
333 frozenset({frozenset(), frozenset({0})}): frozenset({frozenset({frozenset({0}),
334 frozenset({0,
335 1})}),
336 frozenset({frozenset({0}),
337 frozenset({0,
338 2})}),
339 frozenset({frozenset(),
340 frozenset({1})}),
341 frozenset({frozenset(),
342 frozenset({2})})}),
343 frozenset({frozenset(), frozenset({1})}): frozenset({frozenset({frozenset(),
344 frozenset({0})}),
345 frozenset({frozenset({1}),
346 frozenset({1,
347 2})}),
348 frozenset({frozenset(),
349 frozenset({2})}),
350 frozenset({frozenset({1}),
351 frozenset({0,
352 1})})}),
353 frozenset({frozenset({2}), frozenset()}): frozenset({frozenset({frozenset({2}),
354 frozenset({1,
355 2})}),
356 frozenset({frozenset(),
357 frozenset({0})}),
358 frozenset({frozenset(),
359 frozenset({1})}),
360 frozenset({frozenset({2}),
361 frozenset({0,
362 2})})}),
363 frozenset({frozenset({0, 1, 2}), frozenset({0, 1})}): frozenset({frozenset({frozenset({1,
364 2}),
365 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000366 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000367 2})}),
368 frozenset({frozenset({0,
369 2}),
370 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000371 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000372 2})}),
373 frozenset({frozenset({0}),
374 frozenset({0,
375 1})}),
376 frozenset({frozenset({1}),
377 frozenset({0,
378 1})})}),
379 frozenset({frozenset({0}), frozenset({0, 1})}): frozenset({frozenset({frozenset(),
380 frozenset({0})}),
381 frozenset({frozenset({0,
382 1}),
383 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000384 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000385 2})}),
386 frozenset({frozenset({0}),
387 frozenset({0,
388 2})}),
389 frozenset({frozenset({1}),
390 frozenset({0,
391 1})})}),
392 frozenset({frozenset({2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({0,
393 2}),
394 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000395 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000396 2})}),
397 frozenset({frozenset({2}),
398 frozenset({1,
399 2})}),
400 frozenset({frozenset({0}),
401 frozenset({0,
402 2})}),
403 frozenset({frozenset(),
404 frozenset({2})})}),
405 frozenset({frozenset({0, 1, 2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({1,
406 2}),
407 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000408 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000409 2})}),
410 frozenset({frozenset({0,
411 1}),
412 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000413 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000414 2})}),
415 frozenset({frozenset({0}),
416 frozenset({0,
417 2})}),
418 frozenset({frozenset({2}),
419 frozenset({0,
420 2})})}),
421 frozenset({frozenset({1, 2}), frozenset({0, 1, 2})}): frozenset({frozenset({frozenset({0,
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 1}),
428 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000429 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000430 2})}),
431 frozenset({frozenset({2}),
432 frozenset({1,
433 2})}),
434 frozenset({frozenset({1}),
435 frozenset({1,
436 2})})})}"""
Christian Heimes969fe572008-01-25 11:23:10 +0000437
438 cubo = test.test_set.linegraph(cube)
439 self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt)
440
Alexandre Vassalottieca20b62008-05-16 02:54:33 +0000441 def test_depth(self):
442 nested_tuple = (1, (2, (3, (4, (5, 6)))))
443 nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}}
444 nested_list = [1, [2, [3, [4, [5, [6, []]]]]]]
445 self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple))
446 self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict))
447 self.assertEqual(pprint.pformat(nested_list), repr(nested_list))
448
449 lv1_tuple = '(1, (...))'
450 lv1_dict = '{1: {...}}'
451 lv1_list = '[1, [...]]'
452 self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple)
453 self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict)
454 self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list)
455
Raymond Hettingera7da1662009-11-19 01:07:05 +0000456 def test_sort_unorderable_values(self):
457 # Issue 3976: sorted pprints fail for unorderable values.
458 n = 20
459 keys = [Unorderable() for i in range(n)]
460 random.shuffle(keys)
461 skeys = sorted(keys, key=id)
462 clean = lambda s: s.replace(' ', '').replace('\n','')
463
464 self.assertEqual(clean(pprint.pformat(set(keys))),
465 '{' + ','.join(map(repr, skeys)) + '}')
466 self.assertEqual(clean(pprint.pformat(frozenset(keys))),
467 'frozenset({' + ','.join(map(repr, skeys)) + '})')
468 self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys))),
469 '{' + ','.join('%r:None' % k for k in skeys) + '}')
Fred Drakeaee113d2002-04-02 05:08:35 +0000470
Florent Xiclunad6da90f2012-07-21 11:17:38 +0200471 # Issue 10017: TypeError on user-defined types as dict keys.
472 self.assertEqual(pprint.pformat({Unorderable: 0, 1: 0}),
473 '{1: 0, ' + repr(Unorderable) +': 0}')
474
475 # Issue 14998: TypeError on tuples with NoneTypes as dict keys.
Florent Xicluna6e571d62012-07-21 12:44:20 +0200476 keys = [(1,), (None,)]
477 self.assertEqual(pprint.pformat(dict.fromkeys(keys, 0)),
478 '{%r: 0, %r: 0}' % tuple(sorted(keys, key=id)))
Florent Xiclunad6da90f2012-07-21 11:17:38 +0200479
Antoine Pitrou64c16c32013-03-23 20:30:39 +0100480 def test_str_wrap(self):
481 # pprint tries to wrap strings intelligently
482 fox = 'the quick brown fox jumped over a lazy dog'
483 self.assertEqual(pprint.pformat(fox, width=20), """\
484'the quick brown '
485'fox jumped over '
486'a lazy dog'""")
487 self.assertEqual(pprint.pformat({'a': 1, 'b': fox, 'c': 2},
488 width=26), """\
489{'a': 1,
490 'b': 'the quick brown '
491 'fox jumped over '
492 'a lazy dog',
493 'c': 2}""")
494 # With some special characters
495 # - \n always triggers a new line in the pprint
496 # - \t and \n are escaped
497 # - non-ASCII is allowed
498 # - an apostrophe doesn't disrupt the pprint
499 special = "Portons dix bons \"whiskys\"\nà l'avocat goujat\t qui fumait au zoo"
500 self.assertEqual(pprint.pformat(special, width=20), """\
501'Portons dix bons '
502'"whiskys"\\n'
503"à l'avocat "
504'goujat\\t qui '
505'fumait au zoo'""")
506 # An unwrappable string is formatted as its repr
507 unwrappable = "x" * 100
508 self.assertEqual(pprint.pformat(unwrappable, width=80), repr(unwrappable))
509 self.assertEqual(pprint.pformat(''), "''")
510 # Check that the pprint is a usable repr
511 special *= 10
512 for width in range(3, 40):
513 formatted = pprint.pformat(special, width=width)
514 self.assertEqual(eval("(" + formatted + ")"), special)
515
Florent Xiclunad6da90f2012-07-21 11:17:38 +0200516
Fred Drakeaee113d2002-04-02 05:08:35 +0000517class DottedPrettyPrinter(pprint.PrettyPrinter):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000518
Fred Drakeaee113d2002-04-02 05:08:35 +0000519 def format(self, object, context, maxlevels, level):
520 if isinstance(object, str):
521 if ' ' in object:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000522 return repr(object), 1, 0
Fred Drakeaee113d2002-04-02 05:08:35 +0000523 else:
524 return object, 0, 0
525 else:
526 return pprint.PrettyPrinter.format(
527 self, object, context, maxlevels, level)
528
529
Fred Drake2e2be372001-09-20 21:33:42 +0000530def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000531 test.support.run_unittest(QueryTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +0000532
533
534if __name__ == "__main__":
535 test_main()