2 import test
.test_support
12 # list, tuple and dict subclasses that do or don't overwrite __repr__
18 return list.__repr
__(self
)
25 return tuple.__repr
__(self
)
32 return dict.__repr
__(self
)
34 class QueryTestCase(unittest
.TestCase
):
42 # Verify .isrecursive() and .isreadable() w/o recursion
43 verify
= self
.assertTrue
44 pp
= pprint
.PrettyPrinter()
45 for safe
in (2, 2.0, 2j
, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
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
,))
58 def test_knotted(self
):
59 # Verify .isrecursive() and .isreadable() w/ recursion
64 self
.d
[0] = self
.d
[1] = self
.d
[2] = self
.d
66 verify
= self
.assertTrue
67 pp
= pprint
.PrettyPrinter()
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")
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
,))
92 def test_unreadable(self
):
93 # Not recursive but not readable anyway
94 verify
= self
.assertTrue
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
,))
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
.assertTrue
118 for simple
in (0, 0L, 0+0j
, 0.0, "", uni(""),
119 (), tuple2(), tuple3(),
120 [], list2(), list3(),
121 {}, dict2(), dict3(),
123 -6, -6L, -6-6j
, -1.5, "x", uni("x"), (3,), [3], {3: 6},
124 (1,2), [3,4], {5: 6},
125 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
126 [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
127 dict2({5: 6}), dict3({5: 6}),
130 native
= repr(simple
)
131 for function
in "pformat", "saferepr":
132 f
= getattr(pprint
, function
)
134 verify(native
== got
, "expected %s got %s from pprint.%s" %
135 (native
, got
, function
))
137 def test_basic_line_wrap(self
):
138 # verify basic line-wrapping operation
142 'controldesk_runtime_us': 0,
143 'main_code_runtime_us': 0,
144 'read_io_runtime_us': 0,
145 'write_io_runtime_us': 43690}
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
)
158 exp
= '[%s]' % ',\n '.join(map(str, o
))
159 for type in [list, list2
]:
160 self
.assertEqual(pprint
.pformat(type(o
)), exp
)
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
)
169 exp
= '[ %s]' % ',\n '.join(map(str, o
))
170 for type in [list, list2
]:
171 self
.assertEqual(pprint
.pformat(type(o
), indent
=4), exp
)
173 def test_nested_indentations(self
):
175 o2
= dict(first
=1, second
=2, third
=3)
178 [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
182 self
.assertEqual(pprint
.pformat(o
, indent
=4, width
=42), expected
)
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}]")
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,), (): {}}")
202 def test_subclassing(self
):
203 o
= {'names with spaces': 'should be presented using repr()',
204 'others.should.not.be': 'like.this'}
206 {'names with spaces': 'should be presented using repr()',
207 others.should.not.be: like.this}"""
208 self
.assertEqual(DottedPrettyPrinter().pformat(o
), exp
)
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])')
216 {frozenset([]): frozenset([frozenset([2]), frozenset([0]), frozenset([1])]),
217 frozenset([0]): frozenset([frozenset(),
220 frozenset([1]): frozenset([frozenset(),
223 frozenset([2]): frozenset([frozenset(),
226 frozenset([1, 2]): frozenset([frozenset([2]),
228 frozenset([0, 1, 2])]),
229 frozenset([0, 2]): frozenset([frozenset([2]),
231 frozenset([0, 1, 2])]),
232 frozenset([0, 1]): frozenset([frozenset([0]),
234 frozenset([0, 1, 2])]),
235 frozenset([0, 1, 2]): frozenset([frozenset([1, 2]),
237 frozenset([0, 1])])}"""
238 cube
= test
.test_set
.cube(3)
239 self
.assertEqual(pprint
.pformat(cube
), cube_repr_tgt
)
241 {frozenset([frozenset([0, 2]), frozenset([0])]): frozenset([frozenset([frozenset([0,
246 frozenset([frozenset([0]),
249 frozenset([frozenset(),
251 frozenset([frozenset([2]),
254 frozenset([frozenset([0, 1]), frozenset([1])]): frozenset([frozenset([frozenset([0,
259 frozenset([frozenset([0]),
262 frozenset([frozenset([1]),
265 frozenset([frozenset(),
267 frozenset([frozenset([1, 2]), frozenset([1])]): frozenset([frozenset([frozenset([1,
272 frozenset([frozenset([2]),
275 frozenset([frozenset(),
277 frozenset([frozenset([1]),
280 frozenset([frozenset([1, 2]), frozenset([2])]): frozenset([frozenset([frozenset([1,
285 frozenset([frozenset([1]),
288 frozenset([frozenset([2]),
291 frozenset([frozenset(),
293 frozenset([frozenset([]), frozenset([0])]): frozenset([frozenset([frozenset([0]),
296 frozenset([frozenset([0]),
299 frozenset([frozenset(),
301 frozenset([frozenset(),
303 frozenset([frozenset([]), frozenset([1])]): frozenset([frozenset([frozenset(),
305 frozenset([frozenset([1]),
308 frozenset([frozenset(),
310 frozenset([frozenset([1]),
313 frozenset([frozenset([2]), frozenset([])]): frozenset([frozenset([frozenset([2]),
316 frozenset([frozenset(),
318 frozenset([frozenset(),
320 frozenset([frozenset([2]),
323 frozenset([frozenset([0, 1, 2]), frozenset([0, 1])]): frozenset([frozenset([frozenset([1,
328 frozenset([frozenset([0,
333 frozenset([frozenset([0]),
336 frozenset([frozenset([1]),
339 frozenset([frozenset([0]), frozenset([0, 1])]): frozenset([frozenset([frozenset(),
341 frozenset([frozenset([0,
346 frozenset([frozenset([0]),
349 frozenset([frozenset([1]),
352 frozenset([frozenset([2]), frozenset([0, 2])]): frozenset([frozenset([frozenset([0,
357 frozenset([frozenset([2]),
360 frozenset([frozenset([0]),
363 frozenset([frozenset(),
365 frozenset([frozenset([0, 1, 2]), frozenset([0, 2])]): frozenset([frozenset([frozenset([1,
370 frozenset([frozenset([0,
375 frozenset([frozenset([0]),
378 frozenset([frozenset([2]),
381 frozenset([frozenset([1, 2]), frozenset([0, 1, 2])]): frozenset([frozenset([frozenset([0,
386 frozenset([frozenset([0,
391 frozenset([frozenset([2]),
394 frozenset([frozenset([1]),
398 cubo
= test
.test_set
.linegraph(cube
)
399 self
.assertEqual(pprint
.pformat(cubo
), cubo_repr_tgt
)
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
))
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
)
417 class DottedPrettyPrinter(pprint
.PrettyPrinter
):
419 def format(self
, object, context
, maxlevels
, level
):
420 if isinstance(object, str):
422 return repr(object), 1, 0
426 return pprint
.PrettyPrinter
.format(
427 self
, object, context
, maxlevels
, level
)
431 test
.test_support
.run_unittest(QueryTestCase
)
434 if __name__
== "__main__":