# list, tuple and dict subclasses that do or don't overwrite __repr__
return list.__repr
__(self
)
return tuple.__repr
__(self
)
return dict.__repr
__(self
)
class QueryTestCase(unittest
.TestCase
):
# Verify .isrecursive() and .isreadable() w/o recursion
pp
= pprint
.PrettyPrinter()
for safe
in (2, 2.0, 2j
, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
# module-level convenience functions
verify(not pprint
.isrecursive(safe
),
"expected not isrecursive for %r" % (safe
,))
verify(pprint
.isreadable(safe
),
"expected isreadable for %r" % (safe
,))
verify(not pp
.isrecursive(safe
),
"expected not isrecursive for %r" % (safe
,))
verify(pp
.isreadable(safe
),
"expected isreadable for %r" % (safe
,))
# Verify .isrecursive() and .isreadable() w/ recursion
self
.d
[0] = self
.d
[1] = self
.d
[2] = self
.d
pp
= pprint
.PrettyPrinter()
for icky
in self
.a
, self
.b
, self
.d
, (self
.d
, self
.d
):
verify(pprint
.isrecursive(icky
), "expected isrecursive")
verify(not pprint
.isreadable(icky
), "expected not isreadable")
verify(pp
.isrecursive(icky
), "expected isrecursive")
verify(not pp
.isreadable(icky
), "expected not isreadable")
for safe
in self
.a
, self
.b
, self
.d
, (self
.d
, self
.d
):
# module-level convenience functions
verify(not pprint
.isrecursive(safe
),
"expected not isrecursive for %r" % (safe
,))
verify(pprint
.isreadable(safe
),
"expected isreadable for %r" % (safe
,))
verify(not pp
.isrecursive(safe
),
"expected not isrecursive for %r" % (safe
,))
verify(pp
.isreadable(safe
),
"expected isreadable for %r" % (safe
,))
def test_unreadable(self
):
# Not recursive but not readable anyway
pp
= pprint
.PrettyPrinter()
for unreadable
in type(3), pprint
, pprint
.isrecursive
:
# module-level convenience functions
verify(not pprint
.isrecursive(unreadable
),
"expected not isrecursive for %r" % (unreadable
,))
verify(not pprint
.isreadable(unreadable
),
"expected not isreadable for %r" % (unreadable
,))
verify(not pp
.isrecursive(unreadable
),
"expected not isrecursive for %r" % (unreadable
,))
verify(not pp
.isreadable(unreadable
),
"expected not isreadable for %r" % (unreadable
,))
def test_same_as_repr(self
):
# Simple objects, small containers and classes that overwrite __repr__
# For those the result should be the same as repr()
for simple
in (0, 0L, 0+0j
, 0.0, "", uni(""),
-6, -6L, -6-6j
, -1.5, "x", uni("x"), (3,), [3], {3: 6},
(1,2), [3,4], {5: 6, 7: 8},
tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
[3,4], list2([3,4]), list3([3,4]), list3(range(100)),
{5: 6, 7: 8}, dict2({5: 6, 7: 8}), dict3({5: 6, 7: 8}),
dict3([(x
,x
) for x
in range(100)]),
{"xy\tab\n": (3,), 5: [[]], (): {}},
for function
in "pformat", "saferepr":
f
= getattr(pprint
, function
)
verify(native
== got
, "expected %s got %s from pprint.%s" %
def test_basic_line_wrap(self
):
# verify basic line-wrapping operation
'controldesk_runtime_us': 0,
'main_code_runtime_us': 0,
'write_io_runtime_us': 43690}
'controldesk_runtime_us': 0,
'main_code_runtime_us': 0,
'write_io_runtime_us': 43690}"""
for type in [dict, dict2
]:
self
.assertEqual(pprint
.pformat(type(o
)), exp
)
exp
= '[%s]' % ',\n '.join(map(str, o
))
for type in [list, list2
]:
self
.assertEqual(pprint
.pformat(type(o
)), exp
)
exp
= '(%s)' % ',\n '.join(map(str, o
))
for type in [tuple, tuple2
]:
self
.assertEqual(pprint
.pformat(type(o
)), exp
)
exp
= '[ %s]' % ',\n '.join(map(str, o
))
for type in [list, list2
]:
self
.assertEqual(pprint
.pformat(type(o
), indent
=4), exp
)
def test_subclassing(self
):
o
= {'names with spaces': 'should be presented using repr()',
'others.should.not.be': 'like.this'}
{'names with spaces': 'should be presented using repr()',
others.should.not.be: like.this}"""
self
.assertEqual(DottedPrettyPrinter().pformat(o
), exp
)
class DottedPrettyPrinter(pprint
.PrettyPrinter
):
def format(self
, object, context
, maxlevels
, level
):
if isinstance(object, str):
return repr(object), 1, 0
return pprint
.PrettyPrinter
.format(
self
, object, context
, maxlevels
, level
)
test
.test_support
.run_unittest(QueryTestCase
)
if __name__
== "__main__":