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 |
| 14 | class list3(list): |
| 15 | def __repr__(self): |
| 16 | return list.__repr__(self) |
| 17 | class tuple2(tuple): |
| 18 | pass |
| 19 | class tuple3(tuple): |
| 20 | def __repr__(self): |
| 21 | return tuple.__repr__(self) |
| 22 | class dict2(dict): |
| 23 | pass |
| 24 | class dict3(dict): |
| 25 | def __repr__(self): |
| 26 | return dict.__repr__(self) |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 27 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 28 | class QueryTestCase(unittest.TestCase): |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 29 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 30 | def setUp(self): |
| 31 | self.a = range(100) |
| 32 | self.b = range(200) |
| 33 | self.a[-12] = self.b |
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 test_basic(self): |
Guido van Rossum | 32c2ae7 | 2002-08-22 19:45:32 +0000 | [diff] [blame] | 36 | # Verify .isrecursive() and .isreadable() w/o recursion |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 37 | verify = self.assert_ |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 38 | pp = pprint.PrettyPrinter() |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 39 | 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] | 40 | self.a, self.b): |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 41 | # module-level convenience functions |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 42 | verify(not pprint.isrecursive(safe), |
| 43 | "expected not isrecursive for " + `safe`) |
| 44 | verify(pprint.isreadable(safe), |
| 45 | "expected isreadable for " + `safe`) |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 46 | # PrettyPrinter methods |
| 47 | verify(not pp.isrecursive(safe), |
| 48 | "expected not isrecursive for " + `safe`) |
| 49 | verify(pp.isreadable(safe), |
| 50 | "expected isreadable for " + `safe`) |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 51 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 52 | def test_knotted(self): |
Guido van Rossum | 32c2ae7 | 2002-08-22 19:45:32 +0000 | [diff] [blame] | 53 | # Verify .isrecursive() and .isreadable() w/ recursion |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 54 | # Tie a knot. |
| 55 | self.b[67] = self.a |
| 56 | # Messy dict. |
| 57 | self.d = {} |
| 58 | self.d[0] = self.d[1] = self.d[2] = self.d |
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 | verify = self.assert_ |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 61 | pp = pprint.PrettyPrinter() |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 62 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 63 | for icky in self.a, self.b, self.d, (self.d, self.d): |
| 64 | verify(pprint.isrecursive(icky), "expected isrecursive") |
| 65 | verify(not pprint.isreadable(icky), "expected not isreadable") |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 66 | verify(pp.isrecursive(icky), "expected isrecursive") |
| 67 | verify(not pp.isreadable(icky), "expected not isreadable") |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 68 | |
| 69 | # Break the cycles. |
| 70 | self.d.clear() |
| 71 | del self.a[:] |
| 72 | del self.b[:] |
| 73 | |
| 74 | 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] | 75 | # module-level convenience functions |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 76 | verify(not pprint.isrecursive(safe), |
| 77 | "expected not isrecursive for " + `safe`) |
| 78 | verify(pprint.isreadable(safe), |
| 79 | "expected isreadable for " + `safe`) |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 80 | # PrettyPrinter methods |
| 81 | verify(not pp.isrecursive(safe), |
| 82 | "expected not isrecursive for " + `safe`) |
| 83 | verify(pp.isreadable(safe), |
| 84 | "expected isreadable for " + `safe`) |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 85 | |
| 86 | def test_unreadable(self): |
Guido van Rossum | 32c2ae7 | 2002-08-22 19:45:32 +0000 | [diff] [blame] | 87 | # Not recursive but not readable anyway |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 88 | verify = self.assert_ |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 89 | pp = pprint.PrettyPrinter() |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 90 | for unreadable in type(3), pprint, pprint.isrecursive: |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 91 | # module-level convenience functions |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 92 | verify(not pprint.isrecursive(unreadable), |
| 93 | "expected not isrecursive for " + `unreadable`) |
| 94 | verify(not pprint.isreadable(unreadable), |
| 95 | "expected not isreadable for " + `unreadable`) |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 96 | # PrettyPrinter methods |
| 97 | verify(not pp.isrecursive(unreadable), |
| 98 | "expected not isrecursive for " + `unreadable`) |
| 99 | verify(not pp.isreadable(unreadable), |
| 100 | "expected not isreadable for " + `unreadable`) |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 101 | |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 102 | def test_same_as_repr(self): |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 103 | # Simple objects, small containers and classes that overwrite __repr__ |
| 104 | # For those the result should be the same as repr() |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 105 | verify = self.assert_ |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 106 | for simple in (0, 0L, 0+0j, 0.0, "", uni(""), |
| 107 | (), tuple2(), tuple3(), |
| 108 | [], list2(), list3(), |
| 109 | {}, dict2(), dict3(), |
| 110 | verify, pprint, |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 111 | -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] | 112 | (1,2), [3,4], {5: 6, 7: 8}, |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 113 | tuple2((1,2)), tuple3((1,2)), tuple3(range(100)), |
| 114 | [3,4], list2([3,4]), list3([3,4]), list3(range(100)), |
| 115 | {5: 6, 7: 8}, dict2({5: 6, 7: 8}), dict3({5: 6, 7: 8}), |
| 116 | dict3([(x,x) for x in range(100)]), |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 117 | {"xy\tab\n": (3,), 5: [[]], (): {}}, |
| 118 | range(10, -11, -1) |
| 119 | ): |
| 120 | native = repr(simple) |
| 121 | for function in "pformat", "saferepr": |
| 122 | f = getattr(pprint, function) |
| 123 | got = f(simple) |
| 124 | verify(native == got, "expected %s got %s from pprint.%s" % |
| 125 | (native, got, function)) |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 126 | |
Barry Warsaw | 00859c0 | 2001-11-28 05:49:39 +0000 | [diff] [blame] | 127 | def test_basic_line_wrap(self): |
Guido van Rossum | 32c2ae7 | 2002-08-22 19:45:32 +0000 | [diff] [blame] | 128 | # verify basic line-wrapping operation |
Barry Warsaw | 00859c0 | 2001-11-28 05:49:39 +0000 | [diff] [blame] | 129 | o = {'RPM_cal': 0, |
| 130 | 'RPM_cal2': 48059, |
| 131 | 'Speed_cal': 0, |
| 132 | 'controldesk_runtime_us': 0, |
| 133 | 'main_code_runtime_us': 0, |
| 134 | 'read_io_runtime_us': 0, |
| 135 | 'write_io_runtime_us': 43690} |
| 136 | exp = """\ |
| 137 | {'RPM_cal': 0, |
| 138 | 'RPM_cal2': 48059, |
| 139 | 'Speed_cal': 0, |
| 140 | 'controldesk_runtime_us': 0, |
| 141 | 'main_code_runtime_us': 0, |
| 142 | 'read_io_runtime_us': 0, |
| 143 | 'write_io_runtime_us': 43690}""" |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 144 | for type in [dict, dict2]: |
| 145 | self.assertEqual(pprint.pformat(type(o)), exp) |
| 146 | |
| 147 | o = range(100) |
| 148 | exp = '[%s]' % ',\n '.join(map(str, o)) |
| 149 | for type in [list, list2]: |
| 150 | self.assertEqual(pprint.pformat(type(o)), exp) |
| 151 | |
| 152 | o = tuple(range(100)) |
| 153 | exp = '(%s)' % ',\n '.join(map(str, o)) |
| 154 | for type in [tuple, tuple2]: |
| 155 | self.assertEqual(pprint.pformat(type(o)), exp) |
Barry Warsaw | 00859c0 | 2001-11-28 05:49:39 +0000 | [diff] [blame] | 156 | |
Walter Dörwald | c8de458 | 2003-12-03 20:26:05 +0000 | [diff] [blame] | 157 | # indent parameter |
| 158 | o = range(100) |
| 159 | exp = '[ %s]' % ',\n '.join(map(str, o)) |
| 160 | for type in [list, list2]: |
| 161 | self.assertEqual(pprint.pformat(type(o), indent=4), exp) |
| 162 | |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 163 | def test_subclassing(self): |
| 164 | o = {'names with spaces': 'should be presented using repr()', |
| 165 | 'others.should.not.be': 'like.this'} |
| 166 | exp = """\ |
| 167 | {'names with spaces': 'should be presented using repr()', |
| 168 | others.should.not.be: like.this}""" |
| 169 | self.assertEqual(DottedPrettyPrinter().pformat(o), exp) |
| 170 | |
| 171 | |
| 172 | class DottedPrettyPrinter(pprint.PrettyPrinter): |
Guido van Rossum | 32c2ae7 | 2002-08-22 19:45:32 +0000 | [diff] [blame] | 173 | |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 174 | def format(self, object, context, maxlevels, level): |
| 175 | if isinstance(object, str): |
| 176 | if ' ' in object: |
| 177 | return `object`, 1, 0 |
| 178 | else: |
| 179 | return object, 0, 0 |
| 180 | else: |
| 181 | return pprint.PrettyPrinter.format( |
| 182 | self, object, context, maxlevels, level) |
| 183 | |
| 184 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 185 | def test_main(): |
Fred Drake | b456e4f | 2002-12-31 07:16:16 +0000 | [diff] [blame] | 186 | test.test_support.run_unittest(QueryTestCase) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 187 | |
| 188 | |
| 189 | if __name__ == "__main__": |
| 190 | test_main() |