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 pp
= pprint
.PrettyPrinter()
44 for safe
in (2, 2.0, 2j
, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
46 # module-level convenience functions
47 self
.assertFalse(pprint
.isrecursive(safe
),
48 "expected not isrecursive for %r" % (safe
,))
49 self
.assertTrue(pprint
.isreadable(safe
),
50 "expected isreadable for %r" % (safe
,))
51 # PrettyPrinter methods
52 self
.assertFalse(pp
.isrecursive(safe
),
53 "expected not isrecursive for %r" % (safe
,))
54 self
.assertTrue(pp
.isreadable(safe
),
55 "expected isreadable for %r" % (safe
,))
57 def test_knotted(self
):
58 # Verify .isrecursive() and .isreadable() w/ recursion
63 self
.d
[0] = self
.d
[1] = self
.d
[2] = self
.d
65 pp
= pprint
.PrettyPrinter()
67 for icky
in self
.a
, self
.b
, self
.d
, (self
.d
, self
.d
):
68 self
.assertTrue(pprint
.isrecursive(icky
), "expected isrecursive")
69 self
.assertFalse(pprint
.isreadable(icky
), "expected not isreadable")
70 self
.assertTrue(pp
.isrecursive(icky
), "expected isrecursive")
71 self
.assertFalse(pp
.isreadable(icky
), "expected not isreadable")
78 for safe
in self
.a
, self
.b
, self
.d
, (self
.d
, self
.d
):
79 # module-level convenience functions
80 self
.assertFalse(pprint
.isrecursive(safe
),
81 "expected not isrecursive for %r" % (safe
,))
82 self
.assertTrue(pprint
.isreadable(safe
),
83 "expected isreadable for %r" % (safe
,))
84 # PrettyPrinter methods
85 self
.assertFalse(pp
.isrecursive(safe
),
86 "expected not isrecursive for %r" % (safe
,))
87 self
.assertTrue(pp
.isreadable(safe
),
88 "expected isreadable for %r" % (safe
,))
90 def test_unreadable(self
):
91 # Not recursive but not readable anyway
92 pp
= pprint
.PrettyPrinter()
93 for unreadable
in type(3), pprint
, pprint
.isrecursive
:
94 # module-level convenience functions
95 self
.assertFalse(pprint
.isrecursive(unreadable
),
96 "expected not isrecursive for %r" % (unreadable
,))
97 self
.assertFalse(pprint
.isreadable(unreadable
),
98 "expected not isreadable for %r" % (unreadable
,))
99 # PrettyPrinter methods
100 self
.assertFalse(pp
.isrecursive(unreadable
),
101 "expected not isrecursive for %r" % (unreadable
,))
102 self
.assertFalse(pp
.isreadable(unreadable
),
103 "expected not isreadable for %r" % (unreadable
,))
105 def test_same_as_repr(self
):
106 # Simple objects, small containers and classes that overwrite __repr__
107 # For those the result should be the same as repr().
108 # Ahem. The docs don't say anything about that -- this appears to
109 # be testing an implementation quirk. Starting in Python 2.5, it's
110 # not true for dicts: pprint always sorts dicts by key now; before,
111 # it sorted a dict display if and only if the display required
112 # multiple lines. For that reason, dicts with more than one element
113 # aren't tested here.
114 for simple
in (0, 0L, 0+0j
, 0.0, "", uni(""),
115 (), tuple2(), tuple3(),
116 [], list2(), list3(),
117 {}, dict2(), dict3(),
118 self
.assertTrue
, pprint
,
119 -6, -6L, -6-6j
, -1.5, "x", uni("x"), (3,), [3], {3: 6},
120 (1,2), [3,4], {5: 6},
121 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
122 [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
123 dict2({5: 6}), dict3({5: 6}),
126 native
= repr(simple
)
127 for function
in "pformat", "saferepr":
128 f
= getattr(pprint
, function
)
130 self
.assertEqual(native
, got
,
131 "expected %s got %s from pprint.%s" %
132 (native
, got
, function
))
134 def test_basic_line_wrap(self
):
135 # verify basic line-wrapping operation
139 'controldesk_runtime_us': 0,
140 'main_code_runtime_us': 0,
141 'read_io_runtime_us': 0,
142 'write_io_runtime_us': 43690}
147 'controldesk_runtime_us': 0,
148 'main_code_runtime_us': 0,
149 'read_io_runtime_us': 0,
150 'write_io_runtime_us': 43690}"""
151 for type in [dict, dict2
]:
152 self
.assertEqual(pprint
.pformat(type(o
)), exp
)
155 exp
= '[%s]' % ',\n '.join(map(str, o
))
156 for type in [list, list2
]:
157 self
.assertEqual(pprint
.pformat(type(o
)), exp
)
159 o
= tuple(range(100))
160 exp
= '(%s)' % ',\n '.join(map(str, o
))
161 for type in [tuple, tuple2
]:
162 self
.assertEqual(pprint
.pformat(type(o
)), exp
)
166 exp
= '[ %s]' % ',\n '.join(map(str, o
))
167 for type in [list, list2
]:
168 self
.assertEqual(pprint
.pformat(type(o
), indent
=4), exp
)
170 def test_nested_indentations(self
):
172 o2
= dict(first
=1, second
=2, third
=3)
175 [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
179 self
.assertEqual(pprint
.pformat(o
, indent
=4, width
=42), expected
)
181 def test_sorted_dict(self
):
182 # Starting in Python 2.5, pprint sorts dict displays by key regardless
183 # of how small the dictionary may be.
184 # Before the change, on 32-bit Windows pformat() gave order
185 # 'a', 'c', 'b' here, so this test failed.
186 d
= {'a': 1, 'b': 1, 'c': 1}
187 self
.assertEqual(pprint
.pformat(d
), "{'a': 1, 'b': 1, 'c': 1}")
188 self
.assertEqual(pprint
.pformat([d
, d
]),
189 "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]")
191 # The next one is kind of goofy. The sorted order depends on the
192 # alphabetic order of type names: "int" < "str" < "tuple". Before
193 # Python 2.5, this was in the test_same_as_repr() test. It's worth
194 # keeping around for now because it's one of few tests of pprint
195 # against a crazy mix of types.
196 self
.assertEqual(pprint
.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}),
197 r
"{5: [[]], 'xy\tab\n': (3,), (): {}}")
199 def test_subclassing(self
):
200 o
= {'names with spaces': 'should be presented using repr()',
201 'others.should.not.be': 'like.this'}
203 {'names with spaces': 'should be presented using repr()',
204 others.should.not.be: like.this}"""
205 self
.assertEqual(DottedPrettyPrinter().pformat(o
), exp
)
207 def test_set_reprs(self
):
208 self
.assertEqual(pprint
.pformat(set()), 'set()')
209 self
.assertEqual(pprint
.pformat(set(range(3))), 'set([0, 1, 2])')
210 self
.assertEqual(pprint
.pformat(frozenset()), 'frozenset()')
211 self
.assertEqual(pprint
.pformat(frozenset(range(3))), 'frozenset([0, 1, 2])')
213 {frozenset([]): frozenset([frozenset([2]), frozenset([0]), frozenset([1])]),
214 frozenset([0]): frozenset([frozenset(),
217 frozenset([1]): frozenset([frozenset(),
220 frozenset([2]): frozenset([frozenset(),
223 frozenset([1, 2]): frozenset([frozenset([2]),
225 frozenset([0, 1, 2])]),
226 frozenset([0, 2]): frozenset([frozenset([2]),
228 frozenset([0, 1, 2])]),
229 frozenset([0, 1]): frozenset([frozenset([0]),
231 frozenset([0, 1, 2])]),
232 frozenset([0, 1, 2]): frozenset([frozenset([1, 2]),
234 frozenset([0, 1])])}"""
235 cube
= test
.test_set
.cube(3)
236 self
.assertEqual(pprint
.pformat(cube
), cube_repr_tgt
)
238 {frozenset([frozenset([0, 2]), frozenset([0])]): frozenset([frozenset([frozenset([0,
243 frozenset([frozenset([0]),
246 frozenset([frozenset(),
248 frozenset([frozenset([2]),
251 frozenset([frozenset([0, 1]), frozenset([1])]): frozenset([frozenset([frozenset([0,
256 frozenset([frozenset([0]),
259 frozenset([frozenset([1]),
262 frozenset([frozenset(),
264 frozenset([frozenset([1, 2]), frozenset([1])]): frozenset([frozenset([frozenset([1,
269 frozenset([frozenset([2]),
272 frozenset([frozenset(),
274 frozenset([frozenset([1]),
277 frozenset([frozenset([1, 2]), frozenset([2])]): frozenset([frozenset([frozenset([1,
282 frozenset([frozenset([1]),
285 frozenset([frozenset([2]),
288 frozenset([frozenset(),
290 frozenset([frozenset([]), frozenset([0])]): frozenset([frozenset([frozenset([0]),
293 frozenset([frozenset([0]),
296 frozenset([frozenset(),
298 frozenset([frozenset(),
300 frozenset([frozenset([]), frozenset([1])]): frozenset([frozenset([frozenset(),
302 frozenset([frozenset([1]),
305 frozenset([frozenset(),
307 frozenset([frozenset([1]),
310 frozenset([frozenset([2]), frozenset([])]): frozenset([frozenset([frozenset([2]),
313 frozenset([frozenset(),
315 frozenset([frozenset(),
317 frozenset([frozenset([2]),
320 frozenset([frozenset([0, 1, 2]), frozenset([0, 1])]): frozenset([frozenset([frozenset([1,
325 frozenset([frozenset([0,
330 frozenset([frozenset([0]),
333 frozenset([frozenset([1]),
336 frozenset([frozenset([0]), frozenset([0, 1])]): frozenset([frozenset([frozenset(),
338 frozenset([frozenset([0,
343 frozenset([frozenset([0]),
346 frozenset([frozenset([1]),
349 frozenset([frozenset([2]), frozenset([0, 2])]): frozenset([frozenset([frozenset([0,
354 frozenset([frozenset([2]),
357 frozenset([frozenset([0]),
360 frozenset([frozenset(),
362 frozenset([frozenset([0, 1, 2]), frozenset([0, 2])]): frozenset([frozenset([frozenset([1,
367 frozenset([frozenset([0,
372 frozenset([frozenset([0]),
375 frozenset([frozenset([2]),
378 frozenset([frozenset([1, 2]), frozenset([0, 1, 2])]): frozenset([frozenset([frozenset([0,
383 frozenset([frozenset([0,
388 frozenset([frozenset([2]),
391 frozenset([frozenset([1]),
395 cubo
= test
.test_set
.linegraph(cube
)
396 self
.assertEqual(pprint
.pformat(cubo
), cubo_repr_tgt
)
398 def test_depth(self
):
399 nested_tuple
= (1, (2, (3, (4, (5, 6)))))
400 nested_dict
= {1: {2: {3: {4: {5: {6: 6}}}}}}
401 nested_list
= [1, [2, [3, [4, [5, [6, []]]]]]]
402 self
.assertEqual(pprint
.pformat(nested_tuple
), repr(nested_tuple
))
403 self
.assertEqual(pprint
.pformat(nested_dict
), repr(nested_dict
))
404 self
.assertEqual(pprint
.pformat(nested_list
), repr(nested_list
))
406 lv1_tuple
= '(1, (...))'
407 lv1_dict
= '{1: {...}}'
408 lv1_list
= '[1, [...]]'
409 self
.assertEqual(pprint
.pformat(nested_tuple
, depth
=1), lv1_tuple
)
410 self
.assertEqual(pprint
.pformat(nested_dict
, depth
=1), lv1_dict
)
411 self
.assertEqual(pprint
.pformat(nested_list
, depth
=1), lv1_list
)
414 class DottedPrettyPrinter(pprint
.PrettyPrinter
):
416 def format(self
, object, context
, maxlevels
, level
):
417 if isinstance(object, str):
419 return repr(object), 1, 0
423 return pprint
.PrettyPrinter
.format(
424 self
, object, context
, maxlevels
, level
)
428 test
.test_support
.run_unittest(QueryTestCase
)
431 if __name__
== "__main__":