2 import test
.test_support
11 # list, tuple and dict subclasses that do or don't overwrite __repr__
16 return list.__repr
__(self
)
21 return tuple.__repr
__(self
)
26 return dict.__repr
__(self
)
28 class QueryTestCase(unittest
.TestCase
):
36 # Verify .isrecursive() and .isreadable() w/o recursion
38 pp
= pprint
.PrettyPrinter()
39 for safe
in (2, 2.0, 2j
, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
41 # module-level convenience functions
42 verify(not pprint
.isrecursive(safe
),
43 "expected not isrecursive for %r" % (safe
,))
44 verify(pprint
.isreadable(safe
),
45 "expected isreadable for %r" % (safe
,))
46 # PrettyPrinter methods
47 verify(not pp
.isrecursive(safe
),
48 "expected not isrecursive for %r" % (safe
,))
49 verify(pp
.isreadable(safe
),
50 "expected isreadable for %r" % (safe
,))
52 def test_knotted(self
):
53 # Verify .isrecursive() and .isreadable() w/ recursion
58 self
.d
[0] = self
.d
[1] = self
.d
[2] = self
.d
61 pp
= pprint
.PrettyPrinter()
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")
66 verify(pp
.isrecursive(icky
), "expected isrecursive")
67 verify(not pp
.isreadable(icky
), "expected not isreadable")
74 for safe
in self
.a
, self
.b
, self
.d
, (self
.d
, self
.d
):
75 # module-level convenience functions
76 verify(not pprint
.isrecursive(safe
),
77 "expected not isrecursive for %r" % (safe
,))
78 verify(pprint
.isreadable(safe
),
79 "expected isreadable for %r" % (safe
,))
80 # PrettyPrinter methods
81 verify(not pp
.isrecursive(safe
),
82 "expected not isrecursive for %r" % (safe
,))
83 verify(pp
.isreadable(safe
),
84 "expected isreadable for %r" % (safe
,))
86 def test_unreadable(self
):
87 # Not recursive but not readable anyway
89 pp
= pprint
.PrettyPrinter()
90 for unreadable
in type(3), pprint
, pprint
.isrecursive
:
91 # module-level convenience functions
92 verify(not pprint
.isrecursive(unreadable
),
93 "expected not isrecursive for %r" % (unreadable
,))
94 verify(not pprint
.isreadable(unreadable
),
95 "expected not isreadable for %r" % (unreadable
,))
96 # PrettyPrinter methods
97 verify(not pp
.isrecursive(unreadable
),
98 "expected not isrecursive for %r" % (unreadable
,))
99 verify(not pp
.isreadable(unreadable
),
100 "expected not isreadable for %r" % (unreadable
,))
102 def test_same_as_repr(self
):
103 # Simple objects, small containers and classes that overwrite __repr__
104 # For those the result should be the same as repr()
105 verify
= self
.assert_
106 for simple
in (0, 0L, 0+0j
, 0.0, "", uni(""),
107 (), tuple2(), tuple3(),
108 [], list2(), list3(),
109 {}, dict2(), dict3(),
111 -6, -6L, -6-6j
, -1.5, "x", uni("x"), (3,), [3], {3: 6},
112 (1,2), [3,4], {5: 6, 7: 8},
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)]),
117 {"xy\tab\n": (3,), 5: [[]], (): {}},
120 native
= repr(simple
)
121 for function
in "pformat", "saferepr":
122 f
= getattr(pprint
, function
)
124 verify(native
== got
, "expected %s got %s from pprint.%s" %
125 (native
, got
, function
))
127 def test_basic_line_wrap(self
):
128 # verify basic line-wrapping operation
132 'controldesk_runtime_us': 0,
133 'main_code_runtime_us': 0,
134 'read_io_runtime_us': 0,
135 'write_io_runtime_us': 43690}
140 'controldesk_runtime_us': 0,
141 'main_code_runtime_us': 0,
142 'read_io_runtime_us': 0,
143 'write_io_runtime_us': 43690}"""
144 for type in [dict, dict2
]:
145 self
.assertEqual(pprint
.pformat(type(o
)), exp
)
148 exp
= '[%s]' % ',\n '.join(map(str, o
))
149 for type in [list, list2
]:
150 self
.assertEqual(pprint
.pformat(type(o
)), exp
)
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
)
159 exp
= '[ %s]' % ',\n '.join(map(str, o
))
160 for type in [list, list2
]:
161 self
.assertEqual(pprint
.pformat(type(o
), indent
=4), exp
)
163 def test_subclassing(self
):
164 o
= {'names with spaces': 'should be presented using repr()',
165 'others.should.not.be': 'like.this'}
167 {'names with spaces': 'should be presented using repr()',
168 others.should.not.be: like.this}"""
169 self
.assertEqual(DottedPrettyPrinter().pformat(o
), exp
)
172 class DottedPrettyPrinter(pprint
.PrettyPrinter
):
174 def format(self
, object, context
, maxlevels
, level
):
175 if isinstance(object, str):
177 return repr(object), 1, 0
181 return pprint
.PrettyPrinter
.format(
182 self
, object, context
, maxlevels
, level
)
186 test
.test_support
.run_unittest(QueryTestCase
)
189 if __name__
== "__main__":