2 import test
.test_support
12 class QueryTestCase(unittest
.TestCase
):
20 # Verify .isrecursive() and .isreadable() w/o recursion
22 pp
= pprint
.PrettyPrinter()
23 for safe
in (2, 2.0, 2j
, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
25 # module-level convenience functions
26 verify(not pprint
.isrecursive(safe
),
27 "expected not isrecursive for " + `safe`
)
28 verify(pprint
.isreadable(safe
),
29 "expected isreadable for " + `safe`
)
30 # PrettyPrinter methods
31 verify(not pp
.isrecursive(safe
),
32 "expected not isrecursive for " + `safe`
)
33 verify(pp
.isreadable(safe
),
34 "expected isreadable for " + `safe`
)
36 def test_knotted(self
):
37 # Verify .isrecursive() and .isreadable() w/ recursion
42 self
.d
[0] = self
.d
[1] = self
.d
[2] = self
.d
45 pp
= pprint
.PrettyPrinter()
47 for icky
in self
.a
, self
.b
, self
.d
, (self
.d
, self
.d
):
48 verify(pprint
.isrecursive(icky
), "expected isrecursive")
49 verify(not pprint
.isreadable(icky
), "expected not isreadable")
50 verify(pp
.isrecursive(icky
), "expected isrecursive")
51 verify(not pp
.isreadable(icky
), "expected not isreadable")
58 for safe
in self
.a
, self
.b
, self
.d
, (self
.d
, self
.d
):
59 # module-level convenience functions
60 verify(not pprint
.isrecursive(safe
),
61 "expected not isrecursive for " + `safe`
)
62 verify(pprint
.isreadable(safe
),
63 "expected isreadable for " + `safe`
)
64 # PrettyPrinter methods
65 verify(not pp
.isrecursive(safe
),
66 "expected not isrecursive for " + `safe`
)
67 verify(pp
.isreadable(safe
),
68 "expected isreadable for " + `safe`
)
70 def test_unreadable(self
):
71 # Not recursive but not readable anyway
73 pp
= pprint
.PrettyPrinter()
74 for unreadable
in type(3), pprint
, pprint
.isrecursive
:
75 # module-level convenience functions
76 verify(not pprint
.isrecursive(unreadable
),
77 "expected not isrecursive for " + `unreadable`
)
78 verify(not pprint
.isreadable(unreadable
),
79 "expected not isreadable for " + `unreadable`
)
80 # PrettyPrinter methods
81 verify(not pp
.isrecursive(unreadable
),
82 "expected not isrecursive for " + `unreadable`
)
83 verify(not pp
.isreadable(unreadable
),
84 "expected not isreadable for " + `unreadable`
)
86 def test_same_as_repr(self
):
87 # Simple objects and small containers that should be same as repr()
89 for simple
in (0, 0L, 0+0j
, 0.0, "", uni(""), (), [], {}, verify
, pprint
,
90 -6, -6L, -6-6j
, -1.5, "x", uni("x"), (3,), [3], {3: 6},
91 (1,2), [3,4], {5: 6, 7: 8},
92 {"xy\tab\n": (3,), 5: [[]], (): {}},
96 for function
in "pformat", "saferepr":
97 f
= getattr(pprint
, function
)
99 verify(native
== got
, "expected %s got %s from pprint.%s" %
100 (native
, got
, function
))
103 def test_basic_line_wrap(self
):
104 # verify basic line-wrapping operation
108 'controldesk_runtime_us': 0,
109 'main_code_runtime_us': 0,
110 'read_io_runtime_us': 0,
111 'write_io_runtime_us': 43690}
116 'controldesk_runtime_us': 0,
117 'main_code_runtime_us': 0,
118 'read_io_runtime_us': 0,
119 'write_io_runtime_us': 43690}"""
120 self
.assertEqual(pprint
.pformat(o
), exp
)
122 def test_subclassing(self
):
123 o
= {'names with spaces': 'should be presented using repr()',
124 'others.should.not.be': 'like.this'}
126 {'names with spaces': 'should be presented using repr()',
127 others.should.not.be: like.this}"""
128 self
.assertEqual(DottedPrettyPrinter().pformat(o
), exp
)
131 class DottedPrettyPrinter(pprint
.PrettyPrinter
):
133 def format(self
, object, context
, maxlevels
, level
):
134 if isinstance(object, str):
136 return `
object`
, 1, 0
140 return pprint
.PrettyPrinter
.format(
141 self
, object, context
, maxlevels
, level
)
145 test
.test_support
.run_unittest(QueryTestCase
)
148 if __name__
== "__main__":