Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 1 | import pprint |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 2 | import test.test_support |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 3 | import unittest |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 4 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 5 | try: |
| 6 | uni = unicode |
| 7 | except NameError: |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 8 | def uni(x): |
| 9 | return x |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +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 |
Tim Peters | d609b1a | 2006-06-02 23:22:51 +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) |
Tim Peters | d609b1a | 2006-06-02 23:22:51 +0000 | [diff] [blame] | 18 | |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 19 | class tuple2(tuple): |
| 20 | pass |
Tim Peters | d609b1a | 2006-06-02 23:22:51 +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) |
Tim Peters | d609b1a | 2006-06-02 23:22:51 +0000 | [diff] [blame] | 25 | |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 26 | class dict2(dict): |
| 27 | pass |
Tim Peters | d609b1a | 2006-06-02 23:22:51 +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 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 33 | class QueryTestCase(unittest.TestCase): |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 34 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 35 | def setUp(self): |
| 36 | self.a = range(100) |
| 37 | self.b = range(200) |
| 38 | self.a[-12] = self.b |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 39 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 40 | def test_basic(self): |
Guido van Rossum | 32c2ae7 | 2002-08-22 19:45:32 +0000 | [diff] [blame] | 41 | # Verify .isrecursive() and .isreadable() w/o recursion |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 42 | verify = self.assert_ |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 43 | pp = pprint.PrettyPrinter() |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 44 | for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"), |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 45 | self.a, self.b): |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 46 | # module-level convenience functions |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 47 | verify(not pprint.isrecursive(safe), |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 48 | "expected not isrecursive for %r" % (safe,)) |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 49 | verify(pprint.isreadable(safe), |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 50 | "expected isreadable for %r" % (safe,)) |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 51 | # PrettyPrinter methods |
| 52 | verify(not pp.isrecursive(safe), |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 53 | "expected not isrecursive for %r" % (safe,)) |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 54 | verify(pp.isreadable(safe), |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 55 | "expected isreadable for %r" % (safe,)) |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 56 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 57 | def test_knotted(self): |
Guido van Rossum | 32c2ae7 | 2002-08-22 19:45:32 +0000 | [diff] [blame] | 58 | # Verify .isrecursive() and .isreadable() w/ recursion |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 59 | # Tie a knot. |
| 60 | self.b[67] = self.a |
| 61 | # Messy dict. |
| 62 | self.d = {} |
| 63 | self.d[0] = self.d[1] = self.d[2] = self.d |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 64 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 65 | verify = self.assert_ |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 66 | pp = pprint.PrettyPrinter() |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 67 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 68 | for icky in self.a, self.b, self.d, (self.d, self.d): |
| 69 | verify(pprint.isrecursive(icky), "expected isrecursive") |
| 70 | verify(not pprint.isreadable(icky), "expected not isreadable") |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 71 | verify(pp.isrecursive(icky), "expected isrecursive") |
| 72 | verify(not pp.isreadable(icky), "expected not isreadable") |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 73 | |
| 74 | # Break the cycles. |
| 75 | self.d.clear() |
| 76 | del self.a[:] |
| 77 | del self.b[:] |
| 78 | |
| 79 | for safe in self.a, self.b, self.d, (self.d, self.d): |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 80 | # module-level convenience functions |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 81 | verify(not pprint.isrecursive(safe), |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 82 | "expected not isrecursive for %r" % (safe,)) |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 83 | verify(pprint.isreadable(safe), |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 84 | "expected isreadable for %r" % (safe,)) |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 85 | # PrettyPrinter methods |
| 86 | verify(not pp.isrecursive(safe), |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 87 | "expected not isrecursive for %r" % (safe,)) |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 88 | verify(pp.isreadable(safe), |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 89 | "expected isreadable for %r" % (safe,)) |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 90 | |
| 91 | def test_unreadable(self): |
Guido van Rossum | 32c2ae7 | 2002-08-22 19:45:32 +0000 | [diff] [blame] | 92 | # Not recursive but not readable anyway |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 93 | verify = self.assert_ |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 94 | pp = pprint.PrettyPrinter() |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 95 | for unreadable in type(3), pprint, pprint.isrecursive: |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 96 | # module-level convenience functions |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 97 | verify(not pprint.isrecursive(unreadable), |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 98 | "expected not isrecursive for %r" % (unreadable,)) |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 99 | verify(not pprint.isreadable(unreadable), |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 100 | "expected not isreadable for %r" % (unreadable,)) |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 101 | # PrettyPrinter methods |
| 102 | verify(not pp.isrecursive(unreadable), |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 103 | "expected not isrecursive for %r" % (unreadable,)) |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 104 | verify(not pp.isreadable(unreadable), |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 105 | "expected not isreadable for %r" % (unreadable,)) |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 106 | |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 107 | def test_same_as_repr(self): |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 108 | # Simple objects, small containers and classes that overwrite __repr__ |
Tim Peters | d609b1a | 2006-06-02 23:22:51 +0000 | [diff] [blame] | 109 | # For those the result should be the same as repr(). |
| 110 | # Ahem. The docs don't say anything about that -- this appears to |
| 111 | # be testing an implementation quirk. Starting in Python 2.5, it's |
| 112 | # not true for dicts: pprint always sorts dicts by key now; before, |
| 113 | # it sorted a dict display if and only if the display required |
| 114 | # multiple lines. For that reason, dicts with more than one element |
| 115 | # aren't tested here. |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 116 | verify = self.assert_ |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 117 | for simple in (0, 0L, 0+0j, 0.0, "", uni(""), |
| 118 | (), tuple2(), tuple3(), |
| 119 | [], list2(), list3(), |
| 120 | {}, dict2(), dict3(), |
| 121 | verify, pprint, |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 122 | -6, -6L, -6-6j, -1.5, "x", uni("x"), (3,), [3], {3: 6}, |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 123 | (1,2), [3,4], {5: 6, 7: 8}, |
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)), |
Tim Peters | d609b1a | 2006-06-02 23:22:51 +0000 | [diff] [blame] | 126 | {5: 6, 7: 8}, 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) |
| 133 | verify(native == got, "expected %s got %s from pprint.%s" % |
| 134 | (native, got, function)) |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 135 | |
Barry Warsaw | 00859c0 | 2001-11-28 05:49:39 +0000 | [diff] [blame] | 136 | def test_basic_line_wrap(self): |
Guido van Rossum | 32c2ae7 | 2002-08-22 19:45:32 +0000 | [diff] [blame] | 137 | # verify basic line-wrapping operation |
Barry Warsaw | 00859c0 | 2001-11-28 05:49:39 +0000 | [diff] [blame] | 138 | o = {'RPM_cal': 0, |
| 139 | 'RPM_cal2': 48059, |
| 140 | 'Speed_cal': 0, |
| 141 | 'controldesk_runtime_us': 0, |
| 142 | 'main_code_runtime_us': 0, |
| 143 | 'read_io_runtime_us': 0, |
| 144 | 'write_io_runtime_us': 43690} |
| 145 | exp = """\ |
| 146 | {'RPM_cal': 0, |
| 147 | 'RPM_cal2': 48059, |
| 148 | 'Speed_cal': 0, |
| 149 | 'controldesk_runtime_us': 0, |
| 150 | 'main_code_runtime_us': 0, |
| 151 | 'read_io_runtime_us': 0, |
| 152 | 'write_io_runtime_us': 43690}""" |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 153 | for type in [dict, dict2]: |
| 154 | self.assertEqual(pprint.pformat(type(o)), exp) |
| 155 | |
| 156 | o = range(100) |
| 157 | exp = '[%s]' % ',\n '.join(map(str, o)) |
| 158 | for type in [list, list2]: |
| 159 | self.assertEqual(pprint.pformat(type(o)), exp) |
| 160 | |
| 161 | o = tuple(range(100)) |
| 162 | exp = '(%s)' % ',\n '.join(map(str, o)) |
| 163 | for type in [tuple, tuple2]: |
| 164 | self.assertEqual(pprint.pformat(type(o)), exp) |
Barry Warsaw | 00859c0 | 2001-11-28 05:49:39 +0000 | [diff] [blame] | 165 | |
Walter Dörwald | c8de458 | 2003-12-03 20:26:05 +0000 | [diff] [blame] | 166 | # indent parameter |
| 167 | o = range(100) |
| 168 | exp = '[ %s]' % ',\n '.join(map(str, o)) |
| 169 | for type in [list, list2]: |
| 170 | self.assertEqual(pprint.pformat(type(o), indent=4), exp) |
| 171 | |
Tim Peters | d609b1a | 2006-06-02 23:22:51 +0000 | [diff] [blame] | 172 | def test_sorted_dict(self): |
| 173 | # Starting in Python 2.5, pprint sorts dict displays by key regardless |
| 174 | # of how small the dictionary may be. |
| 175 | # Before the change, on 32-bit Windows pformat() gave order |
| 176 | # 'a', 'c', 'b' here, so this test failed. |
| 177 | d = {'a': 1, 'b': 1, 'c': 1} |
| 178 | self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}") |
| 179 | self.assertEqual(pprint.pformat([d, d]), |
| 180 | "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]") |
| 181 | |
| 182 | # The next one is kind of goofy. The sorted order depends on the |
| 183 | # alphabetic order of type names: "int" < "str" < "tuple". Before |
| 184 | # Python 2.5, this was in the test_same_as_repr() test. It's worth |
| 185 | # keeping around for now because it's one of few tests of pprint |
| 186 | # against a crazy mix of types. |
| 187 | self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}), |
| 188 | r"{5: [[]], 'xy\tab\n': (3,), (): {}}") |
| 189 | |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 190 | def test_subclassing(self): |
| 191 | o = {'names with spaces': 'should be presented using repr()', |
| 192 | 'others.should.not.be': 'like.this'} |
| 193 | exp = """\ |
| 194 | {'names with spaces': 'should be presented using repr()', |
| 195 | others.should.not.be: like.this}""" |
| 196 | self.assertEqual(DottedPrettyPrinter().pformat(o), exp) |
| 197 | |
| 198 | |
| 199 | class DottedPrettyPrinter(pprint.PrettyPrinter): |
Guido van Rossum | 32c2ae7 | 2002-08-22 19:45:32 +0000 | [diff] [blame] | 200 | |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 201 | def format(self, object, context, maxlevels, level): |
| 202 | if isinstance(object, str): |
| 203 | if ' ' in object: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 204 | return repr(object), 1, 0 |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 205 | else: |
| 206 | return object, 0, 0 |
| 207 | else: |
| 208 | return pprint.PrettyPrinter.format( |
| 209 | self, object, context, maxlevels, level) |
| 210 | |
| 211 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 212 | def test_main(): |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 213 | test.test_support.run_unittest(QueryTestCase) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 214 | |
| 215 | |
| 216 | if __name__ == "__main__": |
| 217 | test_main() |