|
1 import pprint |
|
2 import test.test_support |
|
3 import unittest |
|
4 import test.test_set |
|
5 |
|
6 try: |
|
7 uni = unicode |
|
8 except NameError: |
|
9 def uni(x): |
|
10 return x |
|
11 |
|
12 # list, tuple and dict subclasses that do or don't overwrite __repr__ |
|
13 class list2(list): |
|
14 pass |
|
15 |
|
16 class list3(list): |
|
17 def __repr__(self): |
|
18 return list.__repr__(self) |
|
19 |
|
20 class tuple2(tuple): |
|
21 pass |
|
22 |
|
23 class tuple3(tuple): |
|
24 def __repr__(self): |
|
25 return tuple.__repr__(self) |
|
26 |
|
27 class dict2(dict): |
|
28 pass |
|
29 |
|
30 class dict3(dict): |
|
31 def __repr__(self): |
|
32 return dict.__repr__(self) |
|
33 |
|
34 class QueryTestCase(unittest.TestCase): |
|
35 |
|
36 def setUp(self): |
|
37 self.a = range(100) |
|
38 self.b = range(200) |
|
39 self.a[-12] = self.b |
|
40 |
|
41 def test_basic(self): |
|
42 # Verify .isrecursive() and .isreadable() w/o recursion |
|
43 verify = self.assert_ |
|
44 pp = pprint.PrettyPrinter() |
|
45 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"), |
|
46 self.a, self.b): |
|
47 # module-level convenience functions |
|
48 verify(not pprint.isrecursive(safe), |
|
49 "expected not isrecursive for %r" % (safe,)) |
|
50 verify(pprint.isreadable(safe), |
|
51 "expected isreadable for %r" % (safe,)) |
|
52 # PrettyPrinter methods |
|
53 verify(not pp.isrecursive(safe), |
|
54 "expected not isrecursive for %r" % (safe,)) |
|
55 verify(pp.isreadable(safe), |
|
56 "expected isreadable for %r" % (safe,)) |
|
57 |
|
58 def test_knotted(self): |
|
59 # Verify .isrecursive() and .isreadable() w/ recursion |
|
60 # Tie a knot. |
|
61 self.b[67] = self.a |
|
62 # Messy dict. |
|
63 self.d = {} |
|
64 self.d[0] = self.d[1] = self.d[2] = self.d |
|
65 |
|
66 verify = self.assert_ |
|
67 pp = pprint.PrettyPrinter() |
|
68 |
|
69 for icky in self.a, self.b, self.d, (self.d, self.d): |
|
70 verify(pprint.isrecursive(icky), "expected isrecursive") |
|
71 verify(not pprint.isreadable(icky), "expected not isreadable") |
|
72 verify(pp.isrecursive(icky), "expected isrecursive") |
|
73 verify(not pp.isreadable(icky), "expected not isreadable") |
|
74 |
|
75 # Break the cycles. |
|
76 self.d.clear() |
|
77 del self.a[:] |
|
78 del self.b[:] |
|
79 |
|
80 for safe in self.a, self.b, self.d, (self.d, self.d): |
|
81 # module-level convenience functions |
|
82 verify(not pprint.isrecursive(safe), |
|
83 "expected not isrecursive for %r" % (safe,)) |
|
84 verify(pprint.isreadable(safe), |
|
85 "expected isreadable for %r" % (safe,)) |
|
86 # PrettyPrinter methods |
|
87 verify(not pp.isrecursive(safe), |
|
88 "expected not isrecursive for %r" % (safe,)) |
|
89 verify(pp.isreadable(safe), |
|
90 "expected isreadable for %r" % (safe,)) |
|
91 |
|
92 def test_unreadable(self): |
|
93 # Not recursive but not readable anyway |
|
94 verify = self.assert_ |
|
95 pp = pprint.PrettyPrinter() |
|
96 for unreadable in type(3), pprint, pprint.isrecursive: |
|
97 # module-level convenience functions |
|
98 verify(not pprint.isrecursive(unreadable), |
|
99 "expected not isrecursive for %r" % (unreadable,)) |
|
100 verify(not pprint.isreadable(unreadable), |
|
101 "expected not isreadable for %r" % (unreadable,)) |
|
102 # PrettyPrinter methods |
|
103 verify(not pp.isrecursive(unreadable), |
|
104 "expected not isrecursive for %r" % (unreadable,)) |
|
105 verify(not pp.isreadable(unreadable), |
|
106 "expected not isreadable for %r" % (unreadable,)) |
|
107 |
|
108 def test_same_as_repr(self): |
|
109 # Simple objects, small containers and classes that overwrite __repr__ |
|
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. |
|
117 verify = self.assert_ |
|
118 for simple in (0, 0L, 0+0j, 0.0, "", uni(""), |
|
119 (), tuple2(), tuple3(), |
|
120 [], list2(), list3(), |
|
121 {}, dict2(), dict3(), |
|
122 verify, pprint, |
|
123 -6, -6L, -6-6j, -1.5, "x", uni("x"), (3,), [3], {3: 6}, |
|
124 (1,2), [3,4], {5: 6, 7: 8}, |
|
125 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)), |
|
126 [3,4], list2([3,4]), list3([3,4]), list3(range(100)), |
|
127 {5: 6, 7: 8}, dict2({5: 6}), dict3({5: 6}), |
|
128 range(10, -11, -1) |
|
129 ): |
|
130 native = repr(simple) |
|
131 for function in "pformat", "saferepr": |
|
132 f = getattr(pprint, function) |
|
133 got = f(simple) |
|
134 verify(native == got, "expected %s got %s from pprint.%s" % |
|
135 (native, got, function)) |
|
136 |
|
137 def test_basic_line_wrap(self): |
|
138 # verify basic line-wrapping operation |
|
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}""" |
|
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) |
|
166 |
|
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 |
|
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 |
|
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 |
|
202 def test_subclassing(self): |
|
203 o = {'names with spaces': 'should be presented using repr()', |
|
204 'others.should.not.be': 'like.this'} |
|
205 exp = """\ |
|
206 {'names with spaces': 'should be presented using repr()', |
|
207 others.should.not.be: like.this}""" |
|
208 self.assertEqual(DottedPrettyPrinter().pformat(o), exp) |
|
209 |
|
210 def test_set_reprs(self): |
|
211 self.assertEqual(pprint.pformat(set()), 'set()') |
|
212 self.assertEqual(pprint.pformat(set(range(3))), 'set([0, 1, 2])') |
|
213 self.assertEqual(pprint.pformat(frozenset()), 'frozenset()') |
|
214 self.assertEqual(pprint.pformat(frozenset(range(3))), 'frozenset([0, 1, 2])') |
|
215 cube_repr_tgt = """\ |
|
216 {frozenset([]): frozenset([frozenset([2]), frozenset([0]), frozenset([1])]), |
|
217 frozenset([0]): frozenset([frozenset(), |
|
218 frozenset([0, 2]), |
|
219 frozenset([0, 1])]), |
|
220 frozenset([1]): frozenset([frozenset(), |
|
221 frozenset([1, 2]), |
|
222 frozenset([0, 1])]), |
|
223 frozenset([2]): frozenset([frozenset(), |
|
224 frozenset([1, 2]), |
|
225 frozenset([0, 2])]), |
|
226 frozenset([1, 2]): frozenset([frozenset([2]), |
|
227 frozenset([1]), |
|
228 frozenset([0, 1, 2])]), |
|
229 frozenset([0, 2]): frozenset([frozenset([2]), |
|
230 frozenset([0]), |
|
231 frozenset([0, 1, 2])]), |
|
232 frozenset([0, 1]): frozenset([frozenset([0]), |
|
233 frozenset([1]), |
|
234 frozenset([0, 1, 2])]), |
|
235 frozenset([0, 1, 2]): frozenset([frozenset([1, 2]), |
|
236 frozenset([0, 2]), |
|
237 frozenset([0, 1])])}""" |
|
238 cube = test.test_set.cube(3) |
|
239 self.assertEqual(pprint.pformat(cube), cube_repr_tgt) |
|
240 cubo_repr_tgt = """\ |
|
241 {frozenset([frozenset([0, 2]), frozenset([0])]): frozenset([frozenset([frozenset([0, |
|
242 2]), |
|
243 frozenset([0, |
|
244 1, |
|
245 2])]), |
|
246 frozenset([frozenset([0]), |
|
247 frozenset([0, |
|
248 1])]), |
|
249 frozenset([frozenset(), |
|
250 frozenset([0])]), |
|
251 frozenset([frozenset([2]), |
|
252 frozenset([0, |
|
253 2])])]), |
|
254 frozenset([frozenset([0, 1]), frozenset([1])]): frozenset([frozenset([frozenset([0, |
|
255 1]), |
|
256 frozenset([0, |
|
257 1, |
|
258 2])]), |
|
259 frozenset([frozenset([0]), |
|
260 frozenset([0, |
|
261 1])]), |
|
262 frozenset([frozenset([1]), |
|
263 frozenset([1, |
|
264 2])]), |
|
265 frozenset([frozenset(), |
|
266 frozenset([1])])]), |
|
267 frozenset([frozenset([1, 2]), frozenset([1])]): frozenset([frozenset([frozenset([1, |
|
268 2]), |
|
269 frozenset([0, |
|
270 1, |
|
271 2])]), |
|
272 frozenset([frozenset([2]), |
|
273 frozenset([1, |
|
274 2])]), |
|
275 frozenset([frozenset(), |
|
276 frozenset([1])]), |
|
277 frozenset([frozenset([1]), |
|
278 frozenset([0, |
|
279 1])])]), |
|
280 frozenset([frozenset([1, 2]), frozenset([2])]): frozenset([frozenset([frozenset([1, |
|
281 2]), |
|
282 frozenset([0, |
|
283 1, |
|
284 2])]), |
|
285 frozenset([frozenset([1]), |
|
286 frozenset([1, |
|
287 2])]), |
|
288 frozenset([frozenset([2]), |
|
289 frozenset([0, |
|
290 2])]), |
|
291 frozenset([frozenset(), |
|
292 frozenset([2])])]), |
|
293 frozenset([frozenset([]), frozenset([0])]): frozenset([frozenset([frozenset([0]), |
|
294 frozenset([0, |
|
295 1])]), |
|
296 frozenset([frozenset([0]), |
|
297 frozenset([0, |
|
298 2])]), |
|
299 frozenset([frozenset(), |
|
300 frozenset([1])]), |
|
301 frozenset([frozenset(), |
|
302 frozenset([2])])]), |
|
303 frozenset([frozenset([]), frozenset([1])]): frozenset([frozenset([frozenset(), |
|
304 frozenset([0])]), |
|
305 frozenset([frozenset([1]), |
|
306 frozenset([1, |
|
307 2])]), |
|
308 frozenset([frozenset(), |
|
309 frozenset([2])]), |
|
310 frozenset([frozenset([1]), |
|
311 frozenset([0, |
|
312 1])])]), |
|
313 frozenset([frozenset([2]), frozenset([])]): frozenset([frozenset([frozenset([2]), |
|
314 frozenset([1, |
|
315 2])]), |
|
316 frozenset([frozenset(), |
|
317 frozenset([0])]), |
|
318 frozenset([frozenset(), |
|
319 frozenset([1])]), |
|
320 frozenset([frozenset([2]), |
|
321 frozenset([0, |
|
322 2])])]), |
|
323 frozenset([frozenset([0, 1, 2]), frozenset([0, 1])]): frozenset([frozenset([frozenset([1, |
|
324 2]), |
|
325 frozenset([0, |
|
326 1, |
|
327 2])]), |
|
328 frozenset([frozenset([0, |
|
329 2]), |
|
330 frozenset([0, |
|
331 1, |
|
332 2])]), |
|
333 frozenset([frozenset([0]), |
|
334 frozenset([0, |
|
335 1])]), |
|
336 frozenset([frozenset([1]), |
|
337 frozenset([0, |
|
338 1])])]), |
|
339 frozenset([frozenset([0]), frozenset([0, 1])]): frozenset([frozenset([frozenset(), |
|
340 frozenset([0])]), |
|
341 frozenset([frozenset([0, |
|
342 1]), |
|
343 frozenset([0, |
|
344 1, |
|
345 2])]), |
|
346 frozenset([frozenset([0]), |
|
347 frozenset([0, |
|
348 2])]), |
|
349 frozenset([frozenset([1]), |
|
350 frozenset([0, |
|
351 1])])]), |
|
352 frozenset([frozenset([2]), frozenset([0, 2])]): frozenset([frozenset([frozenset([0, |
|
353 2]), |
|
354 frozenset([0, |
|
355 1, |
|
356 2])]), |
|
357 frozenset([frozenset([2]), |
|
358 frozenset([1, |
|
359 2])]), |
|
360 frozenset([frozenset([0]), |
|
361 frozenset([0, |
|
362 2])]), |
|
363 frozenset([frozenset(), |
|
364 frozenset([2])])]), |
|
365 frozenset([frozenset([0, 1, 2]), frozenset([0, 2])]): frozenset([frozenset([frozenset([1, |
|
366 2]), |
|
367 frozenset([0, |
|
368 1, |
|
369 2])]), |
|
370 frozenset([frozenset([0, |
|
371 1]), |
|
372 frozenset([0, |
|
373 1, |
|
374 2])]), |
|
375 frozenset([frozenset([0]), |
|
376 frozenset([0, |
|
377 2])]), |
|
378 frozenset([frozenset([2]), |
|
379 frozenset([0, |
|
380 2])])]), |
|
381 frozenset([frozenset([1, 2]), frozenset([0, 1, 2])]): frozenset([frozenset([frozenset([0, |
|
382 2]), |
|
383 frozenset([0, |
|
384 1, |
|
385 2])]), |
|
386 frozenset([frozenset([0, |
|
387 1]), |
|
388 frozenset([0, |
|
389 1, |
|
390 2])]), |
|
391 frozenset([frozenset([2]), |
|
392 frozenset([1, |
|
393 2])]), |
|
394 frozenset([frozenset([1]), |
|
395 frozenset([1, |
|
396 2])])])}""" |
|
397 |
|
398 cubo = test.test_set.linegraph(cube) |
|
399 self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt) |
|
400 |
|
401 def test_depth(self): |
|
402 nested_tuple = (1, (2, (3, (4, (5, 6))))) |
|
403 nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}} |
|
404 nested_list = [1, [2, [3, [4, [5, [6, []]]]]]] |
|
405 self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple)) |
|
406 self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict)) |
|
407 self.assertEqual(pprint.pformat(nested_list), repr(nested_list)) |
|
408 |
|
409 lv1_tuple = '(1, (...))' |
|
410 lv1_dict = '{1: {...}}' |
|
411 lv1_list = '[1, [...]]' |
|
412 self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple) |
|
413 self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict) |
|
414 self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list) |
|
415 |
|
416 |
|
417 class DottedPrettyPrinter(pprint.PrettyPrinter): |
|
418 |
|
419 def format(self, object, context, maxlevels, level): |
|
420 if isinstance(object, str): |
|
421 if ' ' in object: |
|
422 return repr(object), 1, 0 |
|
423 else: |
|
424 return object, 0, 0 |
|
425 else: |
|
426 return pprint.PrettyPrinter.format( |
|
427 self, object, context, maxlevels, level) |
|
428 |
|
429 |
|
430 def test_main(): |
|
431 test.test_support.run_unittest(QueryTestCase) |
|
432 |
|
433 |
|
434 if __name__ == "__main__": |
|
435 test_main() |