blob: f7f6ac6fd9377bcf804994949c1ed16fbc47fbf4 [file] [log] [blame]
Tim Petersa814db52001-05-14 07:05:58 +00001import pprint
Fred Drakeb456e4f2002-12-31 07:16:16 +00002import test.test_support
Fred Drake43913dd2001-05-14 17:41:20 +00003import unittest
Tim Petersa814db52001-05-14 07:05:58 +00004
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005try:
Guido van Rossumef87d6e2007-05-02 19:09:54 +00006 uni = str
Martin v. Löwis339d0f72001-08-17 18:39:25 +00007except NameError:
Fred Drakeb456e4f2002-12-31 07:16:16 +00008 def uni(x):
9 return x
Martin v. Löwis339d0f72001-08-17 18:39:25 +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
Fred Drake43913dd2001-05-14 17:41:20 +000033class QueryTestCase(unittest.TestCase):
Tim Petersa814db52001-05-14 07:05:58 +000034
Fred Drake43913dd2001-05-14 17:41:20 +000035 def setUp(self):
Guido van Rossum805365e2007-05-07 22:24:25 +000036 self.a = list(range(100))
37 self.b = list(range(200))
Fred Drake43913dd2001-05-14 17:41:20 +000038 self.a[-12] = self.b
Tim Petersa814db52001-05-14 07:05:58 +000039
Fred Drake43913dd2001-05-14 17:41:20 +000040 def test_basic(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000041 # Verify .isrecursive() and .isreadable() w/o recursion
Fred Drake43913dd2001-05-14 17:41:20 +000042 verify = self.assert_
Fred Drakeb456e4f2002-12-31 07:16:16 +000043 pp = pprint.PrettyPrinter()
Martin v. Löwis339d0f72001-08-17 18:39:25 +000044 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
Fred Drake43913dd2001-05-14 17:41:20 +000045 self.a, self.b):
Fred Drakeb456e4f2002-12-31 07:16:16 +000046 # module-level convenience functions
Fred Drake43913dd2001-05-14 17:41:20 +000047 verify(not pprint.isrecursive(safe),
Walter Dörwald70a6b492004-02-12 17:35:32 +000048 "expected not isrecursive for %r" % (safe,))
Fred Drake43913dd2001-05-14 17:41:20 +000049 verify(pprint.isreadable(safe),
Walter Dörwald70a6b492004-02-12 17:35:32 +000050 "expected isreadable for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +000051 # PrettyPrinter methods
52 verify(not pp.isrecursive(safe),
Walter Dörwald70a6b492004-02-12 17:35:32 +000053 "expected not isrecursive for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +000054 verify(pp.isreadable(safe),
Walter Dörwald70a6b492004-02-12 17:35:32 +000055 "expected isreadable for %r" % (safe,))
Tim Petersa814db52001-05-14 07:05:58 +000056
Fred Drake43913dd2001-05-14 17:41:20 +000057 def test_knotted(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000058 # Verify .isrecursive() and .isreadable() w/ recursion
Fred Drake43913dd2001-05-14 17:41:20 +000059 # 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 Petersa814db52001-05-14 07:05:58 +000064
Fred Drake43913dd2001-05-14 17:41:20 +000065 verify = self.assert_
Fred Drakeb456e4f2002-12-31 07:16:16 +000066 pp = pprint.PrettyPrinter()
Tim Petersa814db52001-05-14 07:05:58 +000067
Fred Drake43913dd2001-05-14 17:41:20 +000068 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 Drakeb456e4f2002-12-31 07:16:16 +000071 verify(pp.isrecursive(icky), "expected isrecursive")
72 verify(not pp.isreadable(icky), "expected not isreadable")
Fred Drake43913dd2001-05-14 17:41:20 +000073
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 Drakeb456e4f2002-12-31 07:16:16 +000080 # module-level convenience functions
Fred Drake43913dd2001-05-14 17:41:20 +000081 verify(not pprint.isrecursive(safe),
Walter Dörwald70a6b492004-02-12 17:35:32 +000082 "expected not isrecursive for %r" % (safe,))
Fred Drake43913dd2001-05-14 17:41:20 +000083 verify(pprint.isreadable(safe),
Walter Dörwald70a6b492004-02-12 17:35:32 +000084 "expected isreadable for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +000085 # PrettyPrinter methods
86 verify(not pp.isrecursive(safe),
Walter Dörwald70a6b492004-02-12 17:35:32 +000087 "expected not isrecursive for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +000088 verify(pp.isreadable(safe),
Walter Dörwald70a6b492004-02-12 17:35:32 +000089 "expected isreadable for %r" % (safe,))
Fred Drake43913dd2001-05-14 17:41:20 +000090
91 def test_unreadable(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000092 # Not recursive but not readable anyway
Fred Drake43913dd2001-05-14 17:41:20 +000093 verify = self.assert_
Fred Drakeb456e4f2002-12-31 07:16:16 +000094 pp = pprint.PrettyPrinter()
Fred Drake43913dd2001-05-14 17:41:20 +000095 for unreadable in type(3), pprint, pprint.isrecursive:
Fred Drakeb456e4f2002-12-31 07:16:16 +000096 # module-level convenience functions
Fred Drake43913dd2001-05-14 17:41:20 +000097 verify(not pprint.isrecursive(unreadable),
Walter Dörwald70a6b492004-02-12 17:35:32 +000098 "expected not isrecursive for %r" % (unreadable,))
Fred Drake43913dd2001-05-14 17:41:20 +000099 verify(not pprint.isreadable(unreadable),
Walter Dörwald70a6b492004-02-12 17:35:32 +0000100 "expected not isreadable for %r" % (unreadable,))
Fred Drakeb456e4f2002-12-31 07:16:16 +0000101 # PrettyPrinter methods
102 verify(not pp.isrecursive(unreadable),
Walter Dörwald70a6b492004-02-12 17:35:32 +0000103 "expected not isrecursive for %r" % (unreadable,))
Fred Drakeb456e4f2002-12-31 07:16:16 +0000104 verify(not pp.isreadable(unreadable),
Walter Dörwald70a6b492004-02-12 17:35:32 +0000105 "expected not isreadable for %r" % (unreadable,))
Fred Drake43913dd2001-05-14 17:41:20 +0000106
Tim Peters95b3f782001-05-14 18:39:41 +0000107 def test_same_as_repr(self):
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000108 # Simple objects, small containers and classes that overwrite __repr__
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000109 # 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 Peters95b3f782001-05-14 18:39:41 +0000116 verify = self.assert_
Guido van Rossume2a383d2007-01-15 16:59:06 +0000117 for simple in (0, 0, 0+0j, 0.0, "", uni(""),
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000118 (), tuple2(), tuple3(),
119 [], list2(), list3(),
120 {}, dict2(), dict3(),
121 verify, pprint,
Guido van Rossume2a383d2007-01-15 16:59:06 +0000122 -6, -6, -6-6j, -1.5, "x", uni("x"), (3,), [3], {3: 6},
Tim Peters95b3f782001-05-14 18:39:41 +0000123 (1,2), [3,4], {5: 6, 7: 8},
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)),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000126 {5: 6, 7: 8}, 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)
133 verify(native == got, "expected %s got %s from pprint.%s" %
134 (native, got, function))
Fred Drake43913dd2001-05-14 17:41:20 +0000135
Barry Warsaw00859c02001-11-28 05:49:39 +0000136 def test_basic_line_wrap(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000137 # verify basic line-wrapping operation
Barry Warsaw00859c02001-11-28 05:49:39 +0000138 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örwald7a7ede52003-12-03 20:15:28 +0000153 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 Warsaw00859c02001-11-28 05:49:39 +0000165
Walter Dörwaldc8de4582003-12-03 20:26:05 +0000166 # 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
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000172 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 Drakeaee113d2002-04-02 05:08:35 +0000190 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
199class DottedPrettyPrinter(pprint.PrettyPrinter):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000200
Fred Drakeaee113d2002-04-02 05:08:35 +0000201 def format(self, object, context, maxlevels, level):
202 if isinstance(object, str):
203 if ' ' in object:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000204 return repr(object), 1, 0
Fred Drakeaee113d2002-04-02 05:08:35 +0000205 else:
206 return object, 0, 0
207 else:
208 return pprint.PrettyPrinter.format(
209 self, object, context, maxlevels, level)
210
211
Fred Drake2e2be372001-09-20 21:33:42 +0000212def test_main():
Fred Drakeb456e4f2002-12-31 07:16:16 +0000213 test.test_support.run_unittest(QueryTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +0000214
215
216if __name__ == "__main__":
217 test_main()