Antoine Pitrou | 64c16c3 | 2013-03-23 20:30:39 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 3 | import pprint |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | import test.support |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 5 | import unittest |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 6 | import test.test_set |
Raymond Hettinger | a7da166 | 2009-11-19 01:07:05 +0000 | [diff] [blame] | 7 | import random |
Raymond Hettinger | bad3c88 | 2010-09-09 12:31:00 +0000 | [diff] [blame] | 8 | import collections |
| 9 | import itertools |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 10 | |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 11 | # list, tuple and dict subclasses that do or don't overwrite __repr__ |
| 12 | class list2(list): |
| 13 | pass |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 14 | |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 15 | class list3(list): |
| 16 | def __repr__(self): |
| 17 | return list.__repr__(self) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 18 | |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 19 | class tuple2(tuple): |
| 20 | pass |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 21 | |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 22 | class tuple3(tuple): |
| 23 | def __repr__(self): |
| 24 | return tuple.__repr__(self) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 25 | |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 26 | class dict2(dict): |
| 27 | pass |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 28 | |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 29 | class dict3(dict): |
| 30 | def __repr__(self): |
| 31 | return dict.__repr__(self) |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 32 | |
Raymond Hettinger | a7da166 | 2009-11-19 01:07:05 +0000 | [diff] [blame] | 33 | class Unorderable: |
| 34 | def __repr__(self): |
| 35 | return str(id(self)) |
| 36 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 37 | class QueryTestCase(unittest.TestCase): |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 38 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 39 | def setUp(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 40 | self.a = list(range(100)) |
| 41 | self.b = list(range(200)) |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 42 | self.a[-12] = self.b |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 43 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 44 | def test_basic(self): |
Guido van Rossum | 32c2ae7 | 2002-08-22 19:45:32 +0000 | [diff] [blame] | 45 | # Verify .isrecursive() and .isreadable() w/o recursion |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 46 | pp = pprint.PrettyPrinter() |
Walter Dörwald | 5de48bd | 2007-06-11 21:38:39 +0000 | [diff] [blame] | 47 | for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, "yaddayadda", |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 48 | self.a, self.b): |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 49 | # module-level convenience functions |
Ezio Melotti | b19f43d | 2010-01-24 20:59:24 +0000 | [diff] [blame] | 50 | 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 Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 54 | # PrettyPrinter methods |
Ezio Melotti | b19f43d | 2010-01-24 20:59:24 +0000 | [diff] [blame] | 55 | 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 Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 59 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 60 | def test_knotted(self): |
Guido van Rossum | 32c2ae7 | 2002-08-22 19:45:32 +0000 | [diff] [blame] | 61 | # Verify .isrecursive() and .isreadable() w/ recursion |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 62 | # 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 Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 67 | |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 68 | pp = pprint.PrettyPrinter() |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 69 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 70 | for icky in self.a, self.b, self.d, (self.d, self.d): |
Ezio Melotti | b19f43d | 2010-01-24 20:59:24 +0000 | [diff] [blame] | 71 | 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 Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 75 | |
| 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 Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 82 | # module-level convenience functions |
Ezio Melotti | b19f43d | 2010-01-24 20:59:24 +0000 | [diff] [blame] | 83 | 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 Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 87 | # PrettyPrinter methods |
Ezio Melotti | b19f43d | 2010-01-24 20:59:24 +0000 | [diff] [blame] | 88 | 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 Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 92 | |
| 93 | def test_unreadable(self): |
Guido van Rossum | 32c2ae7 | 2002-08-22 19:45:32 +0000 | [diff] [blame] | 94 | # Not recursive but not readable anyway |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 95 | pp = pprint.PrettyPrinter() |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 96 | for unreadable in type(3), pprint, pprint.isrecursive: |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 97 | # module-level convenience functions |
Ezio Melotti | b19f43d | 2010-01-24 20:59:24 +0000 | [diff] [blame] | 98 | 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 Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 102 | # PrettyPrinter methods |
Ezio Melotti | b19f43d | 2010-01-24 20:59:24 +0000 | [diff] [blame] | 103 | 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 Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 107 | |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 108 | def test_same_as_repr(self): |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 109 | # Simple objects, small containers and classes that overwrite __repr__ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 110 | # 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örwald | 5de48bd | 2007-06-11 21:38:39 +0000 | [diff] [blame] | 117 | for simple in (0, 0, 0+0j, 0.0, "", b"", |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 118 | (), tuple2(), tuple3(), |
| 119 | [], list2(), list3(), |
| 120 | {}, dict2(), dict3(), |
Ezio Melotti | b19f43d | 2010-01-24 20:59:24 +0000 | [diff] [blame] | 121 | self.assertTrue, pprint, |
Walter Dörwald | 5de48bd | 2007-06-11 21:38:39 +0000 | [diff] [blame] | 122 | -6, -6, -6-6j, -1.5, "x", b"x", (3,), [3], {3: 6}, |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 123 | (1,2), [3,4], {5: 6}, |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 124 | tuple2((1,2)), tuple3((1,2)), tuple3(range(100)), |
| 125 | [3,4], list2([3,4]), list3([3,4]), list3(range(100)), |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 126 | dict2({5: 6}), dict3({5: 6}), |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 127 | 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 Melotti | b19f43d | 2010-01-24 20:59:24 +0000 | [diff] [blame] | 133 | self.assertEqual(native, got, |
| 134 | "expected %s got %s from pprint.%s" % |
| 135 | (native, got, function)) |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 136 | |
Barry Warsaw | 00859c0 | 2001-11-28 05:49:39 +0000 | [diff] [blame] | 137 | def test_basic_line_wrap(self): |
Guido van Rossum | 32c2ae7 | 2002-08-22 19:45:32 +0000 | [diff] [blame] | 138 | # verify basic line-wrapping operation |
Barry Warsaw | 00859c0 | 2001-11-28 05:49:39 +0000 | [diff] [blame] | 139 | 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örwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 154 | 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 Warsaw | 00859c0 | 2001-11-28 05:49:39 +0000 | [diff] [blame] | 166 | |
Walter Dörwald | c8de458 | 2003-12-03 20:26:05 +0000 | [diff] [blame] | 167 | # 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 Brandl | 3ccb787 | 2008-07-16 03:00:45 +0000 | [diff] [blame] | 173 | 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 Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 184 | 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 Hettinger | bad3c88 | 2010-09-09 12:31:00 +0000 | [diff] [blame] | 202 | 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 Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 216 | 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 Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 224 | @unittest.expectedFailure |
| 225 | #See http://bugs.python.org/issue13907 |
Daniel Stutzbach | c944cfc | 2010-09-21 21:08:09 +0000 | [diff] [blame] | 226 | @test.support.cpython_only |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 227 | def test_set_reprs(self): |
Daniel Stutzbach | c944cfc | 2010-09-21 21:08:09 +0000 | [diff] [blame] | 228 | # 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 Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 248 | # XXX Or changes to the dictionary implmentation... |
Daniel Stutzbach | c944cfc | 2010-09-21 21:08:09 +0000 | [diff] [blame] | 249 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 250 | 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 Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 253 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 254 | 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 Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 257 | frozenset({0}): frozenset({frozenset(), |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 258 | frozenset({0, 2}), |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 259 | frozenset({0, 1})}), |
| 260 | frozenset({1}): frozenset({frozenset(), |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 261 | frozenset({1, 2}), |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 262 | frozenset({0, 1})}), |
| 263 | frozenset({2}): frozenset({frozenset(), |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 264 | frozenset({1, 2}), |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 265 | frozenset({0, 2})}), |
| 266 | frozenset({1, 2}): frozenset({frozenset({2}), |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 267 | frozenset({1}), |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 268 | frozenset({0, 1, 2})}), |
| 269 | frozenset({0, 2}): frozenset({frozenset({2}), |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 270 | frozenset({0}), |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 271 | frozenset({0, 1, 2})}), |
| 272 | frozenset({0, 1}): frozenset({frozenset({0}), |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 273 | frozenset({1}), |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 274 | frozenset({0, 1, 2})}), |
| 275 | frozenset({0, 1, 2}): frozenset({frozenset({1, 2}), |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 276 | frozenset({0, 2}), |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 277 | frozenset({0, 1})})}""" |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 278 | cube = test.test_set.cube(3) |
| 279 | self.assertEqual(pprint.pformat(cube), cube_repr_tgt) |
| 280 | cubo_repr_tgt = """\ |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 281 | {frozenset({frozenset({0, 2}), frozenset({0})}): frozenset({frozenset({frozenset({0, |
| 282 | 2}), |
| 283 | frozenset({0, |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 284 | 1, |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 285 | 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 Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 297 | 1, |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 298 | 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 Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 310 | 1, |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 311 | 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 Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 323 | 1, |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 324 | 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 Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 366 | 1, |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 367 | 2})}), |
| 368 | frozenset({frozenset({0, |
| 369 | 2}), |
| 370 | frozenset({0, |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 371 | 1, |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 372 | 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 Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 384 | 1, |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 385 | 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 Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 395 | 1, |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 396 | 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 Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 408 | 1, |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 409 | 2})}), |
| 410 | frozenset({frozenset({0, |
| 411 | 1}), |
| 412 | frozenset({0, |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 413 | 1, |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 414 | 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 Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 424 | 1, |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 425 | 2})}), |
| 426 | frozenset({frozenset({0, |
| 427 | 1}), |
| 428 | frozenset({0, |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 429 | 1, |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 430 | 2})}), |
| 431 | frozenset({frozenset({2}), |
| 432 | frozenset({1, |
| 433 | 2})}), |
| 434 | frozenset({frozenset({1}), |
| 435 | frozenset({1, |
| 436 | 2})})})}""" |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 437 | |
| 438 | cubo = test.test_set.linegraph(cube) |
| 439 | self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt) |
| 440 | |
Alexandre Vassalotti | eca20b6 | 2008-05-16 02:54:33 +0000 | [diff] [blame] | 441 | 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 Hettinger | a7da166 | 2009-11-19 01:07:05 +0000 | [diff] [blame] | 456 | 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 Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 470 | |
Florent Xicluna | d6da90f | 2012-07-21 11:17:38 +0200 | [diff] [blame] | 471 | # 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 Xicluna | 6e571d6 | 2012-07-21 12:44:20 +0200 | [diff] [blame] | 476 | keys = [(1,), (None,)] |
| 477 | self.assertEqual(pprint.pformat(dict.fromkeys(keys, 0)), |
| 478 | '{%r: 0, %r: 0}' % tuple(sorted(keys, key=id))) |
Florent Xicluna | d6da90f | 2012-07-21 11:17:38 +0200 | [diff] [blame] | 479 | |
Antoine Pitrou | 64c16c3 | 2013-03-23 20:30:39 +0100 | [diff] [blame] | 480 | 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 Xicluna | d6da90f | 2012-07-21 11:17:38 +0200 | [diff] [blame] | 516 | |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 517 | class DottedPrettyPrinter(pprint.PrettyPrinter): |
Guido van Rossum | 32c2ae7 | 2002-08-22 19:45:32 +0000 | [diff] [blame] | 518 | |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 519 | def format(self, object, context, maxlevels, level): |
| 520 | if isinstance(object, str): |
| 521 | if ' ' in object: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 522 | return repr(object), 1, 0 |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 523 | else: |
| 524 | return object, 0, 0 |
| 525 | else: |
| 526 | return pprint.PrettyPrinter.format( |
| 527 | self, object, context, maxlevels, level) |
| 528 | |
| 529 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 530 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 531 | test.support.run_unittest(QueryTestCase) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 532 | |
| 533 | |
| 534 | if __name__ == "__main__": |
| 535 | test_main() |