source: vendor/python/2.5/Lib/test/test_pprint.py@ 3225

Last change on this file since 3225 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 7.9 KB
Line 
1import pprint
2import test.test_support
3import unittest
4
5try:
6 uni = unicode
7except NameError:
8 def uni(x):
9 return x
10
11# list, tuple and dict subclasses that do or don't overwrite __repr__
12class list2(list):
13 pass
14
15class list3(list):
16 def __repr__(self):
17 return list.__repr__(self)
18
19class tuple2(tuple):
20 pass
21
22class tuple3(tuple):
23 def __repr__(self):
24 return tuple.__repr__(self)
25
26class dict2(dict):
27 pass
28
29class dict3(dict):
30 def __repr__(self):
31 return dict.__repr__(self)
32
33class QueryTestCase(unittest.TestCase):
34
35 def setUp(self):
36 self.a = range(100)
37 self.b = range(200)
38 self.a[-12] = self.b
39
40 def test_basic(self):
41 # Verify .isrecursive() and .isreadable() w/o recursion
42 verify = self.assert_
43 pp = pprint.PrettyPrinter()
44 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
45 self.a, self.b):
46 # module-level convenience functions
47 verify(not pprint.isrecursive(safe),
48 "expected not isrecursive for %r" % (safe,))
49 verify(pprint.isreadable(safe),
50 "expected isreadable for %r" % (safe,))
51 # PrettyPrinter methods
52 verify(not pp.isrecursive(safe),
53 "expected not isrecursive for %r" % (safe,))
54 verify(pp.isreadable(safe),
55 "expected isreadable for %r" % (safe,))
56
57 def test_knotted(self):
58 # Verify .isrecursive() and .isreadable() w/ recursion
59 # Tie a knot.
60 self.b[67] = self.a
61 # Messy dict.
62 self.d = {}
63 self.d[0] = self.d[1] = self.d[2] = self.d
64
65 verify = self.assert_
66 pp = pprint.PrettyPrinter()
67
68 for icky in self.a, self.b, self.d, (self.d, self.d):
69 verify(pprint.isrecursive(icky), "expected isrecursive")
70 verify(not pprint.isreadable(icky), "expected not isreadable")
71 verify(pp.isrecursive(icky), "expected isrecursive")
72 verify(not pp.isreadable(icky), "expected not isreadable")
73
74 # Break the cycles.
75 self.d.clear()
76 del self.a[:]
77 del self.b[:]
78
79 for safe in self.a, self.b, self.d, (self.d, self.d):
80 # module-level convenience functions
81 verify(not pprint.isrecursive(safe),
82 "expected not isrecursive for %r" % (safe,))
83 verify(pprint.isreadable(safe),
84 "expected isreadable for %r" % (safe,))
85 # PrettyPrinter methods
86 verify(not pp.isrecursive(safe),
87 "expected not isrecursive for %r" % (safe,))
88 verify(pp.isreadable(safe),
89 "expected isreadable for %r" % (safe,))
90
91 def test_unreadable(self):
92 # Not recursive but not readable anyway
93 verify = self.assert_
94 pp = pprint.PrettyPrinter()
95 for unreadable in type(3), pprint, pprint.isrecursive:
96 # module-level convenience functions
97 verify(not pprint.isrecursive(unreadable),
98 "expected not isrecursive for %r" % (unreadable,))
99 verify(not pprint.isreadable(unreadable),
100 "expected not isreadable for %r" % (unreadable,))
101 # PrettyPrinter methods
102 verify(not pp.isrecursive(unreadable),
103 "expected not isrecursive for %r" % (unreadable,))
104 verify(not pp.isreadable(unreadable),
105 "expected not isreadable for %r" % (unreadable,))
106
107 def test_same_as_repr(self):
108 # Simple objects, small containers and classes that overwrite __repr__
109 # For those the result should be the same as repr().
110 # Ahem. The docs don't say anything about that -- this appears to
111 # be testing an implementation quirk. Starting in Python 2.5, it's
112 # not true for dicts: pprint always sorts dicts by key now; before,
113 # it sorted a dict display if and only if the display required
114 # multiple lines. For that reason, dicts with more than one element
115 # aren't tested here.
116 verify = self.assert_
117 for simple in (0, 0L, 0+0j, 0.0, "", uni(""),
118 (), tuple2(), tuple3(),
119 [], list2(), list3(),
120 {}, dict2(), dict3(),
121 verify, pprint,
122 -6, -6L, -6-6j, -1.5, "x", uni("x"), (3,), [3], {3: 6},
123 (1,2), [3,4], {5: 6, 7: 8},
124 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
125 [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
126 {5: 6, 7: 8}, dict2({5: 6}), dict3({5: 6}),
127 range(10, -11, -1)
128 ):
129 native = repr(simple)
130 for function in "pformat", "saferepr":
131 f = getattr(pprint, function)
132 got = f(simple)
133 verify(native == got, "expected %s got %s from pprint.%s" %
134 (native, got, function))
135
136 def test_basic_line_wrap(self):
137 # verify basic line-wrapping operation
138 o = {'RPM_cal': 0,
139 'RPM_cal2': 48059,
140 'Speed_cal': 0,
141 'controldesk_runtime_us': 0,
142 'main_code_runtime_us': 0,
143 'read_io_runtime_us': 0,
144 'write_io_runtime_us': 43690}
145 exp = """\
146{'RPM_cal': 0,
147 'RPM_cal2': 48059,
148 'Speed_cal': 0,
149 'controldesk_runtime_us': 0,
150 'main_code_runtime_us': 0,
151 'read_io_runtime_us': 0,
152 'write_io_runtime_us': 43690}"""
153 for type in [dict, dict2]:
154 self.assertEqual(pprint.pformat(type(o)), exp)
155
156 o = range(100)
157 exp = '[%s]' % ',\n '.join(map(str, o))
158 for type in [list, list2]:
159 self.assertEqual(pprint.pformat(type(o)), exp)
160
161 o = tuple(range(100))
162 exp = '(%s)' % ',\n '.join(map(str, o))
163 for type in [tuple, tuple2]:
164 self.assertEqual(pprint.pformat(type(o)), exp)
165
166 # indent parameter
167 o = range(100)
168 exp = '[ %s]' % ',\n '.join(map(str, o))
169 for type in [list, list2]:
170 self.assertEqual(pprint.pformat(type(o), indent=4), exp)
171
172 def test_sorted_dict(self):
173 # Starting in Python 2.5, pprint sorts dict displays by key regardless
174 # of how small the dictionary may be.
175 # Before the change, on 32-bit Windows pformat() gave order
176 # 'a', 'c', 'b' here, so this test failed.
177 d = {'a': 1, 'b': 1, 'c': 1}
178 self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}")
179 self.assertEqual(pprint.pformat([d, d]),
180 "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]")
181
182 # The next one is kind of goofy. The sorted order depends on the
183 # alphabetic order of type names: "int" < "str" < "tuple". Before
184 # Python 2.5, this was in the test_same_as_repr() test. It's worth
185 # keeping around for now because it's one of few tests of pprint
186 # against a crazy mix of types.
187 self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}),
188 r"{5: [[]], 'xy\tab\n': (3,), (): {}}")
189
190 def test_subclassing(self):
191 o = {'names with spaces': 'should be presented using repr()',
192 'others.should.not.be': 'like.this'}
193 exp = """\
194{'names with spaces': 'should be presented using repr()',
195 others.should.not.be: like.this}"""
196 self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
197
198
199class DottedPrettyPrinter(pprint.PrettyPrinter):
200
201 def format(self, object, context, maxlevels, level):
202 if isinstance(object, str):
203 if ' ' in object:
204 return repr(object), 1, 0
205 else:
206 return object, 0, 0
207 else:
208 return pprint.PrettyPrinter.format(
209 self, object, context, maxlevels, level)
210
211
212def test_main():
213 test.test_support.run_unittest(QueryTestCase)
214
215
216if __name__ == "__main__":
217 test_main()
Note: See TracBrowser for help on using the repository browser.