1 | n/a | # -*- coding: utf-8 -*- |
---|
2 | n/a | |
---|
3 | n/a | import collections |
---|
4 | n/a | import io |
---|
5 | n/a | import itertools |
---|
6 | n/a | import pprint |
---|
7 | n/a | import random |
---|
8 | n/a | import test.support |
---|
9 | n/a | import test.test_set |
---|
10 | n/a | import types |
---|
11 | n/a | import unittest |
---|
12 | n/a | |
---|
13 | n/a | # list, tuple and dict subclasses that do or don't overwrite __repr__ |
---|
14 | n/a | class list2(list): |
---|
15 | n/a | pass |
---|
16 | n/a | |
---|
17 | n/a | class list3(list): |
---|
18 | n/a | def __repr__(self): |
---|
19 | n/a | return list.__repr__(self) |
---|
20 | n/a | |
---|
21 | n/a | class tuple2(tuple): |
---|
22 | n/a | pass |
---|
23 | n/a | |
---|
24 | n/a | class tuple3(tuple): |
---|
25 | n/a | def __repr__(self): |
---|
26 | n/a | return tuple.__repr__(self) |
---|
27 | n/a | |
---|
28 | n/a | class set2(set): |
---|
29 | n/a | pass |
---|
30 | n/a | |
---|
31 | n/a | class set3(set): |
---|
32 | n/a | def __repr__(self): |
---|
33 | n/a | return set.__repr__(self) |
---|
34 | n/a | |
---|
35 | n/a | class frozenset2(frozenset): |
---|
36 | n/a | pass |
---|
37 | n/a | |
---|
38 | n/a | class frozenset3(frozenset): |
---|
39 | n/a | def __repr__(self): |
---|
40 | n/a | return frozenset.__repr__(self) |
---|
41 | n/a | |
---|
42 | n/a | class dict2(dict): |
---|
43 | n/a | pass |
---|
44 | n/a | |
---|
45 | n/a | class dict3(dict): |
---|
46 | n/a | def __repr__(self): |
---|
47 | n/a | return dict.__repr__(self) |
---|
48 | n/a | |
---|
49 | n/a | class Unorderable: |
---|
50 | n/a | def __repr__(self): |
---|
51 | n/a | return str(id(self)) |
---|
52 | n/a | |
---|
53 | n/a | # Class Orderable is orderable with any type |
---|
54 | n/a | class Orderable: |
---|
55 | n/a | def __init__(self, hash): |
---|
56 | n/a | self._hash = hash |
---|
57 | n/a | def __lt__(self, other): |
---|
58 | n/a | return False |
---|
59 | n/a | def __gt__(self, other): |
---|
60 | n/a | return self != other |
---|
61 | n/a | def __le__(self, other): |
---|
62 | n/a | return self == other |
---|
63 | n/a | def __ge__(self, other): |
---|
64 | n/a | return True |
---|
65 | n/a | def __eq__(self, other): |
---|
66 | n/a | return self is other |
---|
67 | n/a | def __ne__(self, other): |
---|
68 | n/a | return self is not other |
---|
69 | n/a | def __hash__(self): |
---|
70 | n/a | return self._hash |
---|
71 | n/a | |
---|
72 | n/a | class QueryTestCase(unittest.TestCase): |
---|
73 | n/a | |
---|
74 | n/a | def setUp(self): |
---|
75 | n/a | self.a = list(range(100)) |
---|
76 | n/a | self.b = list(range(200)) |
---|
77 | n/a | self.a[-12] = self.b |
---|
78 | n/a | |
---|
79 | n/a | def test_init(self): |
---|
80 | n/a | pp = pprint.PrettyPrinter() |
---|
81 | n/a | pp = pprint.PrettyPrinter(indent=4, width=40, depth=5, |
---|
82 | n/a | stream=io.StringIO(), compact=True) |
---|
83 | n/a | pp = pprint.PrettyPrinter(4, 40, 5, io.StringIO()) |
---|
84 | n/a | with self.assertRaises(TypeError): |
---|
85 | n/a | pp = pprint.PrettyPrinter(4, 40, 5, io.StringIO(), True) |
---|
86 | n/a | self.assertRaises(ValueError, pprint.PrettyPrinter, indent=-1) |
---|
87 | n/a | self.assertRaises(ValueError, pprint.PrettyPrinter, depth=0) |
---|
88 | n/a | self.assertRaises(ValueError, pprint.PrettyPrinter, depth=-1) |
---|
89 | n/a | self.assertRaises(ValueError, pprint.PrettyPrinter, width=0) |
---|
90 | n/a | |
---|
91 | n/a | def test_basic(self): |
---|
92 | n/a | # Verify .isrecursive() and .isreadable() w/o recursion |
---|
93 | n/a | pp = pprint.PrettyPrinter() |
---|
94 | n/a | for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, b"def", |
---|
95 | n/a | bytearray(b"ghi"), True, False, None, ..., |
---|
96 | n/a | self.a, self.b): |
---|
97 | n/a | # module-level convenience functions |
---|
98 | n/a | self.assertFalse(pprint.isrecursive(safe), |
---|
99 | n/a | "expected not isrecursive for %r" % (safe,)) |
---|
100 | n/a | self.assertTrue(pprint.isreadable(safe), |
---|
101 | n/a | "expected isreadable for %r" % (safe,)) |
---|
102 | n/a | # PrettyPrinter methods |
---|
103 | n/a | self.assertFalse(pp.isrecursive(safe), |
---|
104 | n/a | "expected not isrecursive for %r" % (safe,)) |
---|
105 | n/a | self.assertTrue(pp.isreadable(safe), |
---|
106 | n/a | "expected isreadable for %r" % (safe,)) |
---|
107 | n/a | |
---|
108 | n/a | def test_knotted(self): |
---|
109 | n/a | # Verify .isrecursive() and .isreadable() w/ recursion |
---|
110 | n/a | # Tie a knot. |
---|
111 | n/a | self.b[67] = self.a |
---|
112 | n/a | # Messy dict. |
---|
113 | n/a | self.d = {} |
---|
114 | n/a | self.d[0] = self.d[1] = self.d[2] = self.d |
---|
115 | n/a | |
---|
116 | n/a | pp = pprint.PrettyPrinter() |
---|
117 | n/a | |
---|
118 | n/a | for icky in self.a, self.b, self.d, (self.d, self.d): |
---|
119 | n/a | self.assertTrue(pprint.isrecursive(icky), "expected isrecursive") |
---|
120 | n/a | self.assertFalse(pprint.isreadable(icky), "expected not isreadable") |
---|
121 | n/a | self.assertTrue(pp.isrecursive(icky), "expected isrecursive") |
---|
122 | n/a | self.assertFalse(pp.isreadable(icky), "expected not isreadable") |
---|
123 | n/a | |
---|
124 | n/a | # Break the cycles. |
---|
125 | n/a | self.d.clear() |
---|
126 | n/a | del self.a[:] |
---|
127 | n/a | del self.b[:] |
---|
128 | n/a | |
---|
129 | n/a | for safe in self.a, self.b, self.d, (self.d, self.d): |
---|
130 | n/a | # module-level convenience functions |
---|
131 | n/a | self.assertFalse(pprint.isrecursive(safe), |
---|
132 | n/a | "expected not isrecursive for %r" % (safe,)) |
---|
133 | n/a | self.assertTrue(pprint.isreadable(safe), |
---|
134 | n/a | "expected isreadable for %r" % (safe,)) |
---|
135 | n/a | # PrettyPrinter methods |
---|
136 | n/a | self.assertFalse(pp.isrecursive(safe), |
---|
137 | n/a | "expected not isrecursive for %r" % (safe,)) |
---|
138 | n/a | self.assertTrue(pp.isreadable(safe), |
---|
139 | n/a | "expected isreadable for %r" % (safe,)) |
---|
140 | n/a | |
---|
141 | n/a | def test_unreadable(self): |
---|
142 | n/a | # Not recursive but not readable anyway |
---|
143 | n/a | pp = pprint.PrettyPrinter() |
---|
144 | n/a | for unreadable in type(3), pprint, pprint.isrecursive: |
---|
145 | n/a | # module-level convenience functions |
---|
146 | n/a | self.assertFalse(pprint.isrecursive(unreadable), |
---|
147 | n/a | "expected not isrecursive for %r" % (unreadable,)) |
---|
148 | n/a | self.assertFalse(pprint.isreadable(unreadable), |
---|
149 | n/a | "expected not isreadable for %r" % (unreadable,)) |
---|
150 | n/a | # PrettyPrinter methods |
---|
151 | n/a | self.assertFalse(pp.isrecursive(unreadable), |
---|
152 | n/a | "expected not isrecursive for %r" % (unreadable,)) |
---|
153 | n/a | self.assertFalse(pp.isreadable(unreadable), |
---|
154 | n/a | "expected not isreadable for %r" % (unreadable,)) |
---|
155 | n/a | |
---|
156 | n/a | def test_same_as_repr(self): |
---|
157 | n/a | # Simple objects, small containers and classes that overwrite __repr__ |
---|
158 | n/a | # For those the result should be the same as repr(). |
---|
159 | n/a | # Ahem. The docs don't say anything about that -- this appears to |
---|
160 | n/a | # be testing an implementation quirk. Starting in Python 2.5, it's |
---|
161 | n/a | # not true for dicts: pprint always sorts dicts by key now; before, |
---|
162 | n/a | # it sorted a dict display if and only if the display required |
---|
163 | n/a | # multiple lines. For that reason, dicts with more than one element |
---|
164 | n/a | # aren't tested here. |
---|
165 | n/a | for simple in (0, 0, 0+0j, 0.0, "", b"", bytearray(), |
---|
166 | n/a | (), tuple2(), tuple3(), |
---|
167 | n/a | [], list2(), list3(), |
---|
168 | n/a | set(), set2(), set3(), |
---|
169 | n/a | frozenset(), frozenset2(), frozenset3(), |
---|
170 | n/a | {}, dict2(), dict3(), |
---|
171 | n/a | self.assertTrue, pprint, |
---|
172 | n/a | -6, -6, -6-6j, -1.5, "x", b"x", bytearray(b"x"), |
---|
173 | n/a | (3,), [3], {3: 6}, |
---|
174 | n/a | (1,2), [3,4], {5: 6}, |
---|
175 | n/a | tuple2((1,2)), tuple3((1,2)), tuple3(range(100)), |
---|
176 | n/a | [3,4], list2([3,4]), list3([3,4]), list3(range(100)), |
---|
177 | n/a | set({7}), set2({7}), set3({7}), |
---|
178 | n/a | frozenset({8}), frozenset2({8}), frozenset3({8}), |
---|
179 | n/a | dict2({5: 6}), dict3({5: 6}), |
---|
180 | n/a | range(10, -11, -1), |
---|
181 | n/a | True, False, None, ..., |
---|
182 | n/a | ): |
---|
183 | n/a | native = repr(simple) |
---|
184 | n/a | self.assertEqual(pprint.pformat(simple), native) |
---|
185 | n/a | self.assertEqual(pprint.pformat(simple, width=1, indent=0) |
---|
186 | n/a | .replace('\n', ' '), native) |
---|
187 | n/a | self.assertEqual(pprint.saferepr(simple), native) |
---|
188 | n/a | |
---|
189 | n/a | def test_basic_line_wrap(self): |
---|
190 | n/a | # verify basic line-wrapping operation |
---|
191 | n/a | o = {'RPM_cal': 0, |
---|
192 | n/a | 'RPM_cal2': 48059, |
---|
193 | n/a | 'Speed_cal': 0, |
---|
194 | n/a | 'controldesk_runtime_us': 0, |
---|
195 | n/a | 'main_code_runtime_us': 0, |
---|
196 | n/a | 'read_io_runtime_us': 0, |
---|
197 | n/a | 'write_io_runtime_us': 43690} |
---|
198 | n/a | exp = """\ |
---|
199 | n/a | {'RPM_cal': 0, |
---|
200 | n/a | 'RPM_cal2': 48059, |
---|
201 | n/a | 'Speed_cal': 0, |
---|
202 | n/a | 'controldesk_runtime_us': 0, |
---|
203 | n/a | 'main_code_runtime_us': 0, |
---|
204 | n/a | 'read_io_runtime_us': 0, |
---|
205 | n/a | 'write_io_runtime_us': 43690}""" |
---|
206 | n/a | for type in [dict, dict2]: |
---|
207 | n/a | self.assertEqual(pprint.pformat(type(o)), exp) |
---|
208 | n/a | |
---|
209 | n/a | o = range(100) |
---|
210 | n/a | exp = '[%s]' % ',\n '.join(map(str, o)) |
---|
211 | n/a | for type in [list, list2]: |
---|
212 | n/a | self.assertEqual(pprint.pformat(type(o)), exp) |
---|
213 | n/a | |
---|
214 | n/a | o = tuple(range(100)) |
---|
215 | n/a | exp = '(%s)' % ',\n '.join(map(str, o)) |
---|
216 | n/a | for type in [tuple, tuple2]: |
---|
217 | n/a | self.assertEqual(pprint.pformat(type(o)), exp) |
---|
218 | n/a | |
---|
219 | n/a | # indent parameter |
---|
220 | n/a | o = range(100) |
---|
221 | n/a | exp = '[ %s]' % ',\n '.join(map(str, o)) |
---|
222 | n/a | for type in [list, list2]: |
---|
223 | n/a | self.assertEqual(pprint.pformat(type(o), indent=4), exp) |
---|
224 | n/a | |
---|
225 | n/a | def test_nested_indentations(self): |
---|
226 | n/a | o1 = list(range(10)) |
---|
227 | n/a | o2 = dict(first=1, second=2, third=3) |
---|
228 | n/a | o = [o1, o2] |
---|
229 | n/a | expected = """\ |
---|
230 | n/a | [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], |
---|
231 | n/a | {'first': 1, 'second': 2, 'third': 3}]""" |
---|
232 | n/a | self.assertEqual(pprint.pformat(o, indent=4, width=42), expected) |
---|
233 | n/a | expected = """\ |
---|
234 | n/a | [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], |
---|
235 | n/a | { 'first': 1, |
---|
236 | n/a | 'second': 2, |
---|
237 | n/a | 'third': 3}]""" |
---|
238 | n/a | self.assertEqual(pprint.pformat(o, indent=4, width=41), expected) |
---|
239 | n/a | |
---|
240 | n/a | def test_width(self): |
---|
241 | n/a | expected = """\ |
---|
242 | n/a | [[[[[[1, 2, 3], |
---|
243 | n/a | '1 2']]]], |
---|
244 | n/a | {1: [1, 2, 3], |
---|
245 | n/a | 2: [12, 34]}, |
---|
246 | n/a | 'abc def ghi', |
---|
247 | n/a | ('ab cd ef',), |
---|
248 | n/a | set2({1, 23}), |
---|
249 | n/a | [[[[[1, 2, 3], |
---|
250 | n/a | '1 2']]]]]""" |
---|
251 | n/a | o = eval(expected) |
---|
252 | n/a | self.assertEqual(pprint.pformat(o, width=15), expected) |
---|
253 | n/a | self.assertEqual(pprint.pformat(o, width=16), expected) |
---|
254 | n/a | self.assertEqual(pprint.pformat(o, width=25), expected) |
---|
255 | n/a | self.assertEqual(pprint.pformat(o, width=14), """\ |
---|
256 | n/a | [[[[[[1, |
---|
257 | n/a | 2, |
---|
258 | n/a | 3], |
---|
259 | n/a | '1 ' |
---|
260 | n/a | '2']]]], |
---|
261 | n/a | {1: [1, |
---|
262 | n/a | 2, |
---|
263 | n/a | 3], |
---|
264 | n/a | 2: [12, |
---|
265 | n/a | 34]}, |
---|
266 | n/a | 'abc def ' |
---|
267 | n/a | 'ghi', |
---|
268 | n/a | ('ab cd ' |
---|
269 | n/a | 'ef',), |
---|
270 | n/a | set2({1, |
---|
271 | n/a | 23}), |
---|
272 | n/a | [[[[[1, |
---|
273 | n/a | 2, |
---|
274 | n/a | 3], |
---|
275 | n/a | '1 ' |
---|
276 | n/a | '2']]]]]""") |
---|
277 | n/a | |
---|
278 | n/a | def test_sorted_dict(self): |
---|
279 | n/a | # Starting in Python 2.5, pprint sorts dict displays by key regardless |
---|
280 | n/a | # of how small the dictionary may be. |
---|
281 | n/a | # Before the change, on 32-bit Windows pformat() gave order |
---|
282 | n/a | # 'a', 'c', 'b' here, so this test failed. |
---|
283 | n/a | d = {'a': 1, 'b': 1, 'c': 1} |
---|
284 | n/a | self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}") |
---|
285 | n/a | self.assertEqual(pprint.pformat([d, d]), |
---|
286 | n/a | "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]") |
---|
287 | n/a | |
---|
288 | n/a | # The next one is kind of goofy. The sorted order depends on the |
---|
289 | n/a | # alphabetic order of type names: "int" < "str" < "tuple". Before |
---|
290 | n/a | # Python 2.5, this was in the test_same_as_repr() test. It's worth |
---|
291 | n/a | # keeping around for now because it's one of few tests of pprint |
---|
292 | n/a | # against a crazy mix of types. |
---|
293 | n/a | self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}), |
---|
294 | n/a | r"{5: [[]], 'xy\tab\n': (3,), (): {}}") |
---|
295 | n/a | |
---|
296 | n/a | def test_ordered_dict(self): |
---|
297 | n/a | d = collections.OrderedDict() |
---|
298 | n/a | self.assertEqual(pprint.pformat(d, width=1), 'OrderedDict()') |
---|
299 | n/a | d = collections.OrderedDict([]) |
---|
300 | n/a | self.assertEqual(pprint.pformat(d, width=1), 'OrderedDict()') |
---|
301 | n/a | words = 'the quick brown fox jumped over a lazy dog'.split() |
---|
302 | n/a | d = collections.OrderedDict(zip(words, itertools.count())) |
---|
303 | n/a | self.assertEqual(pprint.pformat(d), |
---|
304 | n/a | """\ |
---|
305 | n/a | OrderedDict([('the', 0), |
---|
306 | n/a | ('quick', 1), |
---|
307 | n/a | ('brown', 2), |
---|
308 | n/a | ('fox', 3), |
---|
309 | n/a | ('jumped', 4), |
---|
310 | n/a | ('over', 5), |
---|
311 | n/a | ('a', 6), |
---|
312 | n/a | ('lazy', 7), |
---|
313 | n/a | ('dog', 8)])""") |
---|
314 | n/a | |
---|
315 | n/a | def test_mapping_proxy(self): |
---|
316 | n/a | words = 'the quick brown fox jumped over a lazy dog'.split() |
---|
317 | n/a | d = dict(zip(words, itertools.count())) |
---|
318 | n/a | m = types.MappingProxyType(d) |
---|
319 | n/a | self.assertEqual(pprint.pformat(m), """\ |
---|
320 | n/a | mappingproxy({'a': 6, |
---|
321 | n/a | 'brown': 2, |
---|
322 | n/a | 'dog': 8, |
---|
323 | n/a | 'fox': 3, |
---|
324 | n/a | 'jumped': 4, |
---|
325 | n/a | 'lazy': 7, |
---|
326 | n/a | 'over': 5, |
---|
327 | n/a | 'quick': 1, |
---|
328 | n/a | 'the': 0})""") |
---|
329 | n/a | d = collections.OrderedDict(zip(words, itertools.count())) |
---|
330 | n/a | m = types.MappingProxyType(d) |
---|
331 | n/a | self.assertEqual(pprint.pformat(m), """\ |
---|
332 | n/a | mappingproxy(OrderedDict([('the', 0), |
---|
333 | n/a | ('quick', 1), |
---|
334 | n/a | ('brown', 2), |
---|
335 | n/a | ('fox', 3), |
---|
336 | n/a | ('jumped', 4), |
---|
337 | n/a | ('over', 5), |
---|
338 | n/a | ('a', 6), |
---|
339 | n/a | ('lazy', 7), |
---|
340 | n/a | ('dog', 8)]))""") |
---|
341 | n/a | |
---|
342 | n/a | def test_subclassing(self): |
---|
343 | n/a | o = {'names with spaces': 'should be presented using repr()', |
---|
344 | n/a | 'others.should.not.be': 'like.this'} |
---|
345 | n/a | exp = """\ |
---|
346 | n/a | {'names with spaces': 'should be presented using repr()', |
---|
347 | n/a | others.should.not.be: like.this}""" |
---|
348 | n/a | self.assertEqual(DottedPrettyPrinter().pformat(o), exp) |
---|
349 | n/a | |
---|
350 | n/a | def test_set_reprs(self): |
---|
351 | n/a | self.assertEqual(pprint.pformat(set()), 'set()') |
---|
352 | n/a | self.assertEqual(pprint.pformat(set(range(3))), '{0, 1, 2}') |
---|
353 | n/a | self.assertEqual(pprint.pformat(set(range(7)), width=20), '''\ |
---|
354 | n/a | {0, |
---|
355 | n/a | 1, |
---|
356 | n/a | 2, |
---|
357 | n/a | 3, |
---|
358 | n/a | 4, |
---|
359 | n/a | 5, |
---|
360 | n/a | 6}''') |
---|
361 | n/a | self.assertEqual(pprint.pformat(set2(range(7)), width=20), '''\ |
---|
362 | n/a | set2({0, |
---|
363 | n/a | 1, |
---|
364 | n/a | 2, |
---|
365 | n/a | 3, |
---|
366 | n/a | 4, |
---|
367 | n/a | 5, |
---|
368 | n/a | 6})''') |
---|
369 | n/a | self.assertEqual(pprint.pformat(set3(range(7)), width=20), |
---|
370 | n/a | 'set3({0, 1, 2, 3, 4, 5, 6})') |
---|
371 | n/a | |
---|
372 | n/a | self.assertEqual(pprint.pformat(frozenset()), 'frozenset()') |
---|
373 | n/a | self.assertEqual(pprint.pformat(frozenset(range(3))), |
---|
374 | n/a | 'frozenset({0, 1, 2})') |
---|
375 | n/a | self.assertEqual(pprint.pformat(frozenset(range(7)), width=20), '''\ |
---|
376 | n/a | frozenset({0, |
---|
377 | n/a | 1, |
---|
378 | n/a | 2, |
---|
379 | n/a | 3, |
---|
380 | n/a | 4, |
---|
381 | n/a | 5, |
---|
382 | n/a | 6})''') |
---|
383 | n/a | self.assertEqual(pprint.pformat(frozenset2(range(7)), width=20), '''\ |
---|
384 | n/a | frozenset2({0, |
---|
385 | n/a | 1, |
---|
386 | n/a | 2, |
---|
387 | n/a | 3, |
---|
388 | n/a | 4, |
---|
389 | n/a | 5, |
---|
390 | n/a | 6})''') |
---|
391 | n/a | self.assertEqual(pprint.pformat(frozenset3(range(7)), width=20), |
---|
392 | n/a | 'frozenset3({0, 1, 2, 3, 4, 5, 6})') |
---|
393 | n/a | |
---|
394 | n/a | @unittest.expectedFailure |
---|
395 | n/a | #See http://bugs.python.org/issue13907 |
---|
396 | n/a | @test.support.cpython_only |
---|
397 | n/a | def test_set_of_sets_reprs(self): |
---|
398 | n/a | # This test creates a complex arrangement of frozensets and |
---|
399 | n/a | # compares the pretty-printed repr against a string hard-coded in |
---|
400 | n/a | # the test. The hard-coded repr depends on the sort order of |
---|
401 | n/a | # frozensets. |
---|
402 | n/a | # |
---|
403 | n/a | # However, as the docs point out: "Since sets only define |
---|
404 | n/a | # partial ordering (subset relationships), the output of the |
---|
405 | n/a | # list.sort() method is undefined for lists of sets." |
---|
406 | n/a | # |
---|
407 | n/a | # In a nutshell, the test assumes frozenset({0}) will always |
---|
408 | n/a | # sort before frozenset({1}), but: |
---|
409 | n/a | # |
---|
410 | n/a | # >>> frozenset({0}) < frozenset({1}) |
---|
411 | n/a | # False |
---|
412 | n/a | # >>> frozenset({1}) < frozenset({0}) |
---|
413 | n/a | # False |
---|
414 | n/a | # |
---|
415 | n/a | # Consequently, this test is fragile and |
---|
416 | n/a | # implementation-dependent. Small changes to Python's sort |
---|
417 | n/a | # algorithm cause the test to fail when it should pass. |
---|
418 | n/a | # XXX Or changes to the dictionary implmentation... |
---|
419 | n/a | |
---|
420 | n/a | cube_repr_tgt = """\ |
---|
421 | n/a | {frozenset(): frozenset({frozenset({2}), frozenset({0}), frozenset({1})}), |
---|
422 | n/a | frozenset({0}): frozenset({frozenset(), |
---|
423 | n/a | frozenset({0, 2}), |
---|
424 | n/a | frozenset({0, 1})}), |
---|
425 | n/a | frozenset({1}): frozenset({frozenset(), |
---|
426 | n/a | frozenset({1, 2}), |
---|
427 | n/a | frozenset({0, 1})}), |
---|
428 | n/a | frozenset({2}): frozenset({frozenset(), |
---|
429 | n/a | frozenset({1, 2}), |
---|
430 | n/a | frozenset({0, 2})}), |
---|
431 | n/a | frozenset({1, 2}): frozenset({frozenset({2}), |
---|
432 | n/a | frozenset({1}), |
---|
433 | n/a | frozenset({0, 1, 2})}), |
---|
434 | n/a | frozenset({0, 2}): frozenset({frozenset({2}), |
---|
435 | n/a | frozenset({0}), |
---|
436 | n/a | frozenset({0, 1, 2})}), |
---|
437 | n/a | frozenset({0, 1}): frozenset({frozenset({0}), |
---|
438 | n/a | frozenset({1}), |
---|
439 | n/a | frozenset({0, 1, 2})}), |
---|
440 | n/a | frozenset({0, 1, 2}): frozenset({frozenset({1, 2}), |
---|
441 | n/a | frozenset({0, 2}), |
---|
442 | n/a | frozenset({0, 1})})}""" |
---|
443 | n/a | cube = test.test_set.cube(3) |
---|
444 | n/a | self.assertEqual(pprint.pformat(cube), cube_repr_tgt) |
---|
445 | n/a | cubo_repr_tgt = """\ |
---|
446 | n/a | {frozenset({frozenset({0, 2}), frozenset({0})}): frozenset({frozenset({frozenset({0, |
---|
447 | n/a | 2}), |
---|
448 | n/a | frozenset({0, |
---|
449 | n/a | 1, |
---|
450 | n/a | 2})}), |
---|
451 | n/a | frozenset({frozenset({0}), |
---|
452 | n/a | frozenset({0, |
---|
453 | n/a | 1})}), |
---|
454 | n/a | frozenset({frozenset(), |
---|
455 | n/a | frozenset({0})}), |
---|
456 | n/a | frozenset({frozenset({2}), |
---|
457 | n/a | frozenset({0, |
---|
458 | n/a | 2})})}), |
---|
459 | n/a | frozenset({frozenset({0, 1}), frozenset({1})}): frozenset({frozenset({frozenset({0, |
---|
460 | n/a | 1}), |
---|
461 | n/a | frozenset({0, |
---|
462 | n/a | 1, |
---|
463 | n/a | 2})}), |
---|
464 | n/a | frozenset({frozenset({0}), |
---|
465 | n/a | frozenset({0, |
---|
466 | n/a | 1})}), |
---|
467 | n/a | frozenset({frozenset({1}), |
---|
468 | n/a | frozenset({1, |
---|
469 | n/a | 2})}), |
---|
470 | n/a | frozenset({frozenset(), |
---|
471 | n/a | frozenset({1})})}), |
---|
472 | n/a | frozenset({frozenset({1, 2}), frozenset({1})}): frozenset({frozenset({frozenset({1, |
---|
473 | n/a | 2}), |
---|
474 | n/a | frozenset({0, |
---|
475 | n/a | 1, |
---|
476 | n/a | 2})}), |
---|
477 | n/a | frozenset({frozenset({2}), |
---|
478 | n/a | frozenset({1, |
---|
479 | n/a | 2})}), |
---|
480 | n/a | frozenset({frozenset(), |
---|
481 | n/a | frozenset({1})}), |
---|
482 | n/a | frozenset({frozenset({1}), |
---|
483 | n/a | frozenset({0, |
---|
484 | n/a | 1})})}), |
---|
485 | n/a | frozenset({frozenset({1, 2}), frozenset({2})}): frozenset({frozenset({frozenset({1, |
---|
486 | n/a | 2}), |
---|
487 | n/a | frozenset({0, |
---|
488 | n/a | 1, |
---|
489 | n/a | 2})}), |
---|
490 | n/a | frozenset({frozenset({1}), |
---|
491 | n/a | frozenset({1, |
---|
492 | n/a | 2})}), |
---|
493 | n/a | frozenset({frozenset({2}), |
---|
494 | n/a | frozenset({0, |
---|
495 | n/a | 2})}), |
---|
496 | n/a | frozenset({frozenset(), |
---|
497 | n/a | frozenset({2})})}), |
---|
498 | n/a | frozenset({frozenset(), frozenset({0})}): frozenset({frozenset({frozenset({0}), |
---|
499 | n/a | frozenset({0, |
---|
500 | n/a | 1})}), |
---|
501 | n/a | frozenset({frozenset({0}), |
---|
502 | n/a | frozenset({0, |
---|
503 | n/a | 2})}), |
---|
504 | n/a | frozenset({frozenset(), |
---|
505 | n/a | frozenset({1})}), |
---|
506 | n/a | frozenset({frozenset(), |
---|
507 | n/a | frozenset({2})})}), |
---|
508 | n/a | frozenset({frozenset(), frozenset({1})}): frozenset({frozenset({frozenset(), |
---|
509 | n/a | frozenset({0})}), |
---|
510 | n/a | frozenset({frozenset({1}), |
---|
511 | n/a | frozenset({1, |
---|
512 | n/a | 2})}), |
---|
513 | n/a | frozenset({frozenset(), |
---|
514 | n/a | frozenset({2})}), |
---|
515 | n/a | frozenset({frozenset({1}), |
---|
516 | n/a | frozenset({0, |
---|
517 | n/a | 1})})}), |
---|
518 | n/a | frozenset({frozenset({2}), frozenset()}): frozenset({frozenset({frozenset({2}), |
---|
519 | n/a | frozenset({1, |
---|
520 | n/a | 2})}), |
---|
521 | n/a | frozenset({frozenset(), |
---|
522 | n/a | frozenset({0})}), |
---|
523 | n/a | frozenset({frozenset(), |
---|
524 | n/a | frozenset({1})}), |
---|
525 | n/a | frozenset({frozenset({2}), |
---|
526 | n/a | frozenset({0, |
---|
527 | n/a | 2})})}), |
---|
528 | n/a | frozenset({frozenset({0, 1, 2}), frozenset({0, 1})}): frozenset({frozenset({frozenset({1, |
---|
529 | n/a | 2}), |
---|
530 | n/a | frozenset({0, |
---|
531 | n/a | 1, |
---|
532 | n/a | 2})}), |
---|
533 | n/a | frozenset({frozenset({0, |
---|
534 | n/a | 2}), |
---|
535 | n/a | frozenset({0, |
---|
536 | n/a | 1, |
---|
537 | n/a | 2})}), |
---|
538 | n/a | frozenset({frozenset({0}), |
---|
539 | n/a | frozenset({0, |
---|
540 | n/a | 1})}), |
---|
541 | n/a | frozenset({frozenset({1}), |
---|
542 | n/a | frozenset({0, |
---|
543 | n/a | 1})})}), |
---|
544 | n/a | frozenset({frozenset({0}), frozenset({0, 1})}): frozenset({frozenset({frozenset(), |
---|
545 | n/a | frozenset({0})}), |
---|
546 | n/a | frozenset({frozenset({0, |
---|
547 | n/a | 1}), |
---|
548 | n/a | frozenset({0, |
---|
549 | n/a | 1, |
---|
550 | n/a | 2})}), |
---|
551 | n/a | frozenset({frozenset({0}), |
---|
552 | n/a | frozenset({0, |
---|
553 | n/a | 2})}), |
---|
554 | n/a | frozenset({frozenset({1}), |
---|
555 | n/a | frozenset({0, |
---|
556 | n/a | 1})})}), |
---|
557 | n/a | frozenset({frozenset({2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({0, |
---|
558 | n/a | 2}), |
---|
559 | n/a | frozenset({0, |
---|
560 | n/a | 1, |
---|
561 | n/a | 2})}), |
---|
562 | n/a | frozenset({frozenset({2}), |
---|
563 | n/a | frozenset({1, |
---|
564 | n/a | 2})}), |
---|
565 | n/a | frozenset({frozenset({0}), |
---|
566 | n/a | frozenset({0, |
---|
567 | n/a | 2})}), |
---|
568 | n/a | frozenset({frozenset(), |
---|
569 | n/a | frozenset({2})})}), |
---|
570 | n/a | frozenset({frozenset({0, 1, 2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({1, |
---|
571 | n/a | 2}), |
---|
572 | n/a | frozenset({0, |
---|
573 | n/a | 1, |
---|
574 | n/a | 2})}), |
---|
575 | n/a | frozenset({frozenset({0, |
---|
576 | n/a | 1}), |
---|
577 | n/a | frozenset({0, |
---|
578 | n/a | 1, |
---|
579 | n/a | 2})}), |
---|
580 | n/a | frozenset({frozenset({0}), |
---|
581 | n/a | frozenset({0, |
---|
582 | n/a | 2})}), |
---|
583 | n/a | frozenset({frozenset({2}), |
---|
584 | n/a | frozenset({0, |
---|
585 | n/a | 2})})}), |
---|
586 | n/a | frozenset({frozenset({1, 2}), frozenset({0, 1, 2})}): frozenset({frozenset({frozenset({0, |
---|
587 | n/a | 2}), |
---|
588 | n/a | frozenset({0, |
---|
589 | n/a | 1, |
---|
590 | n/a | 2})}), |
---|
591 | n/a | frozenset({frozenset({0, |
---|
592 | n/a | 1}), |
---|
593 | n/a | frozenset({0, |
---|
594 | n/a | 1, |
---|
595 | n/a | 2})}), |
---|
596 | n/a | frozenset({frozenset({2}), |
---|
597 | n/a | frozenset({1, |
---|
598 | n/a | 2})}), |
---|
599 | n/a | frozenset({frozenset({1}), |
---|
600 | n/a | frozenset({1, |
---|
601 | n/a | 2})})})}""" |
---|
602 | n/a | |
---|
603 | n/a | cubo = test.test_set.linegraph(cube) |
---|
604 | n/a | self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt) |
---|
605 | n/a | |
---|
606 | n/a | def test_depth(self): |
---|
607 | n/a | nested_tuple = (1, (2, (3, (4, (5, 6))))) |
---|
608 | n/a | nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}} |
---|
609 | n/a | nested_list = [1, [2, [3, [4, [5, [6, []]]]]]] |
---|
610 | n/a | self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple)) |
---|
611 | n/a | self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict)) |
---|
612 | n/a | self.assertEqual(pprint.pformat(nested_list), repr(nested_list)) |
---|
613 | n/a | |
---|
614 | n/a | lv1_tuple = '(1, (...))' |
---|
615 | n/a | lv1_dict = '{1: {...}}' |
---|
616 | n/a | lv1_list = '[1, [...]]' |
---|
617 | n/a | self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple) |
---|
618 | n/a | self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict) |
---|
619 | n/a | self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list) |
---|
620 | n/a | |
---|
621 | n/a | def test_sort_unorderable_values(self): |
---|
622 | n/a | # Issue 3976: sorted pprints fail for unorderable values. |
---|
623 | n/a | n = 20 |
---|
624 | n/a | keys = [Unorderable() for i in range(n)] |
---|
625 | n/a | random.shuffle(keys) |
---|
626 | n/a | skeys = sorted(keys, key=id) |
---|
627 | n/a | clean = lambda s: s.replace(' ', '').replace('\n','') |
---|
628 | n/a | |
---|
629 | n/a | self.assertEqual(clean(pprint.pformat(set(keys))), |
---|
630 | n/a | '{' + ','.join(map(repr, skeys)) + '}') |
---|
631 | n/a | self.assertEqual(clean(pprint.pformat(frozenset(keys))), |
---|
632 | n/a | 'frozenset({' + ','.join(map(repr, skeys)) + '})') |
---|
633 | n/a | self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys))), |
---|
634 | n/a | '{' + ','.join('%r:None' % k for k in skeys) + '}') |
---|
635 | n/a | |
---|
636 | n/a | # Issue 10017: TypeError on user-defined types as dict keys. |
---|
637 | n/a | self.assertEqual(pprint.pformat({Unorderable: 0, 1: 0}), |
---|
638 | n/a | '{1: 0, ' + repr(Unorderable) +': 0}') |
---|
639 | n/a | |
---|
640 | n/a | # Issue 14998: TypeError on tuples with NoneTypes as dict keys. |
---|
641 | n/a | keys = [(1,), (None,)] |
---|
642 | n/a | self.assertEqual(pprint.pformat(dict.fromkeys(keys, 0)), |
---|
643 | n/a | '{%r: 0, %r: 0}' % tuple(sorted(keys, key=id))) |
---|
644 | n/a | |
---|
645 | n/a | def test_sort_orderable_and_unorderable_values(self): |
---|
646 | n/a | # Issue 22721: sorted pprints is not stable |
---|
647 | n/a | a = Unorderable() |
---|
648 | n/a | b = Orderable(hash(a)) # should have the same hash value |
---|
649 | n/a | # self-test |
---|
650 | n/a | self.assertLess(a, b) |
---|
651 | n/a | self.assertLess(str(type(b)), str(type(a))) |
---|
652 | n/a | self.assertEqual(sorted([b, a]), [a, b]) |
---|
653 | n/a | self.assertEqual(sorted([a, b]), [a, b]) |
---|
654 | n/a | # set |
---|
655 | n/a | self.assertEqual(pprint.pformat(set([b, a]), width=1), |
---|
656 | n/a | '{%r,\n %r}' % (a, b)) |
---|
657 | n/a | self.assertEqual(pprint.pformat(set([a, b]), width=1), |
---|
658 | n/a | '{%r,\n %r}' % (a, b)) |
---|
659 | n/a | # dict |
---|
660 | n/a | self.assertEqual(pprint.pformat(dict.fromkeys([b, a]), width=1), |
---|
661 | n/a | '{%r: None,\n %r: None}' % (a, b)) |
---|
662 | n/a | self.assertEqual(pprint.pformat(dict.fromkeys([a, b]), width=1), |
---|
663 | n/a | '{%r: None,\n %r: None}' % (a, b)) |
---|
664 | n/a | |
---|
665 | n/a | def test_str_wrap(self): |
---|
666 | n/a | # pprint tries to wrap strings intelligently |
---|
667 | n/a | fox = 'the quick brown fox jumped over a lazy dog' |
---|
668 | n/a | self.assertEqual(pprint.pformat(fox, width=19), """\ |
---|
669 | n/a | ('the quick brown ' |
---|
670 | n/a | 'fox jumped over ' |
---|
671 | n/a | 'a lazy dog')""") |
---|
672 | n/a | self.assertEqual(pprint.pformat({'a': 1, 'b': fox, 'c': 2}, |
---|
673 | n/a | width=25), """\ |
---|
674 | n/a | {'a': 1, |
---|
675 | n/a | 'b': 'the quick brown ' |
---|
676 | n/a | 'fox jumped over ' |
---|
677 | n/a | 'a lazy dog', |
---|
678 | n/a | 'c': 2}""") |
---|
679 | n/a | # With some special characters |
---|
680 | n/a | # - \n always triggers a new line in the pprint |
---|
681 | n/a | # - \t and \n are escaped |
---|
682 | n/a | # - non-ASCII is allowed |
---|
683 | n/a | # - an apostrophe doesn't disrupt the pprint |
---|
684 | n/a | special = "Portons dix bons \"whiskys\"\nà l'avocat goujat\t qui fumait au zoo" |
---|
685 | n/a | self.assertEqual(pprint.pformat(special, width=68), repr(special)) |
---|
686 | n/a | self.assertEqual(pprint.pformat(special, width=31), """\ |
---|
687 | n/a | ('Portons dix bons "whiskys"\\n' |
---|
688 | n/a | "Ã l'avocat goujat\\t qui " |
---|
689 | n/a | 'fumait au zoo')""") |
---|
690 | n/a | self.assertEqual(pprint.pformat(special, width=20), """\ |
---|
691 | n/a | ('Portons dix bons ' |
---|
692 | n/a | '"whiskys"\\n' |
---|
693 | n/a | "Ã l'avocat " |
---|
694 | n/a | 'goujat\\t qui ' |
---|
695 | n/a | 'fumait au zoo')""") |
---|
696 | n/a | self.assertEqual(pprint.pformat([[[[[special]]]]], width=35), """\ |
---|
697 | n/a | [[[[['Portons dix bons "whiskys"\\n' |
---|
698 | n/a | "Ã l'avocat goujat\\t qui " |
---|
699 | n/a | 'fumait au zoo']]]]]""") |
---|
700 | n/a | self.assertEqual(pprint.pformat([[[[[special]]]]], width=25), """\ |
---|
701 | n/a | [[[[['Portons dix bons ' |
---|
702 | n/a | '"whiskys"\\n' |
---|
703 | n/a | "Ã l'avocat " |
---|
704 | n/a | 'goujat\\t qui ' |
---|
705 | n/a | 'fumait au zoo']]]]]""") |
---|
706 | n/a | self.assertEqual(pprint.pformat([[[[[special]]]]], width=23), """\ |
---|
707 | n/a | [[[[['Portons dix ' |
---|
708 | n/a | 'bons "whiskys"\\n' |
---|
709 | n/a | "Ã l'avocat " |
---|
710 | n/a | 'goujat\\t qui ' |
---|
711 | n/a | 'fumait au ' |
---|
712 | n/a | 'zoo']]]]]""") |
---|
713 | n/a | # An unwrappable string is formatted as its repr |
---|
714 | n/a | unwrappable = "x" * 100 |
---|
715 | n/a | self.assertEqual(pprint.pformat(unwrappable, width=80), repr(unwrappable)) |
---|
716 | n/a | self.assertEqual(pprint.pformat(''), "''") |
---|
717 | n/a | # Check that the pprint is a usable repr |
---|
718 | n/a | special *= 10 |
---|
719 | n/a | for width in range(3, 40): |
---|
720 | n/a | formatted = pprint.pformat(special, width=width) |
---|
721 | n/a | self.assertEqual(eval(formatted), special) |
---|
722 | n/a | formatted = pprint.pformat([special] * 2, width=width) |
---|
723 | n/a | self.assertEqual(eval(formatted), [special] * 2) |
---|
724 | n/a | |
---|
725 | n/a | def test_compact(self): |
---|
726 | n/a | o = ([list(range(i * i)) for i in range(5)] + |
---|
727 | n/a | [list(range(i)) for i in range(6)]) |
---|
728 | n/a | expected = """\ |
---|
729 | n/a | [[], [0], [0, 1, 2, 3], |
---|
730 | n/a | [0, 1, 2, 3, 4, 5, 6, 7, 8], |
---|
731 | n/a | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, |
---|
732 | n/a | 14, 15], |
---|
733 | n/a | [], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], |
---|
734 | n/a | [0, 1, 2, 3, 4]]""" |
---|
735 | n/a | self.assertEqual(pprint.pformat(o, width=47, compact=True), expected) |
---|
736 | n/a | |
---|
737 | n/a | def test_compact_width(self): |
---|
738 | n/a | levels = 20 |
---|
739 | n/a | number = 10 |
---|
740 | n/a | o = [0] * number |
---|
741 | n/a | for i in range(levels - 1): |
---|
742 | n/a | o = [o] |
---|
743 | n/a | for w in range(levels * 2 + 1, levels + 3 * number - 1): |
---|
744 | n/a | lines = pprint.pformat(o, width=w, compact=True).splitlines() |
---|
745 | n/a | maxwidth = max(map(len, lines)) |
---|
746 | n/a | self.assertLessEqual(maxwidth, w) |
---|
747 | n/a | self.assertGreater(maxwidth, w - 3) |
---|
748 | n/a | |
---|
749 | n/a | def test_bytes_wrap(self): |
---|
750 | n/a | self.assertEqual(pprint.pformat(b'', width=1), "b''") |
---|
751 | n/a | self.assertEqual(pprint.pformat(b'abcd', width=1), "b'abcd'") |
---|
752 | n/a | letters = b'abcdefghijklmnopqrstuvwxyz' |
---|
753 | n/a | self.assertEqual(pprint.pformat(letters, width=29), repr(letters)) |
---|
754 | n/a | self.assertEqual(pprint.pformat(letters, width=19), """\ |
---|
755 | n/a | (b'abcdefghijkl' |
---|
756 | n/a | b'mnopqrstuvwxyz')""") |
---|
757 | n/a | self.assertEqual(pprint.pformat(letters, width=18), """\ |
---|
758 | n/a | (b'abcdefghijkl' |
---|
759 | n/a | b'mnopqrstuvwx' |
---|
760 | n/a | b'yz')""") |
---|
761 | n/a | self.assertEqual(pprint.pformat(letters, width=16), """\ |
---|
762 | n/a | (b'abcdefghijkl' |
---|
763 | n/a | b'mnopqrstuvwx' |
---|
764 | n/a | b'yz')""") |
---|
765 | n/a | special = bytes(range(16)) |
---|
766 | n/a | self.assertEqual(pprint.pformat(special, width=61), repr(special)) |
---|
767 | n/a | self.assertEqual(pprint.pformat(special, width=48), """\ |
---|
768 | n/a | (b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b' |
---|
769 | n/a | b'\\x0c\\r\\x0e\\x0f')""") |
---|
770 | n/a | self.assertEqual(pprint.pformat(special, width=32), """\ |
---|
771 | n/a | (b'\\x00\\x01\\x02\\x03' |
---|
772 | n/a | b'\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b' |
---|
773 | n/a | b'\\x0c\\r\\x0e\\x0f')""") |
---|
774 | n/a | self.assertEqual(pprint.pformat(special, width=1), """\ |
---|
775 | n/a | (b'\\x00\\x01\\x02\\x03' |
---|
776 | n/a | b'\\x04\\x05\\x06\\x07' |
---|
777 | n/a | b'\\x08\\t\\n\\x0b' |
---|
778 | n/a | b'\\x0c\\r\\x0e\\x0f')""") |
---|
779 | n/a | self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2}, |
---|
780 | n/a | width=21), """\ |
---|
781 | n/a | {'a': 1, |
---|
782 | n/a | 'b': b'abcdefghijkl' |
---|
783 | n/a | b'mnopqrstuvwx' |
---|
784 | n/a | b'yz', |
---|
785 | n/a | 'c': 2}""") |
---|
786 | n/a | self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2}, |
---|
787 | n/a | width=20), """\ |
---|
788 | n/a | {'a': 1, |
---|
789 | n/a | 'b': b'abcdefgh' |
---|
790 | n/a | b'ijklmnop' |
---|
791 | n/a | b'qrstuvwxyz', |
---|
792 | n/a | 'c': 2}""") |
---|
793 | n/a | self.assertEqual(pprint.pformat([[[[[[letters]]]]]], width=25), """\ |
---|
794 | n/a | [[[[[[b'abcdefghijklmnop' |
---|
795 | n/a | b'qrstuvwxyz']]]]]]""") |
---|
796 | n/a | self.assertEqual(pprint.pformat([[[[[[special]]]]]], width=41), """\ |
---|
797 | n/a | [[[[[[b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07' |
---|
798 | n/a | b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f']]]]]]""") |
---|
799 | n/a | # Check that the pprint is a usable repr |
---|
800 | n/a | for width in range(1, 64): |
---|
801 | n/a | formatted = pprint.pformat(special, width=width) |
---|
802 | n/a | self.assertEqual(eval(formatted), special) |
---|
803 | n/a | formatted = pprint.pformat([special] * 2, width=width) |
---|
804 | n/a | self.assertEqual(eval(formatted), [special] * 2) |
---|
805 | n/a | |
---|
806 | n/a | def test_bytearray_wrap(self): |
---|
807 | n/a | self.assertEqual(pprint.pformat(bytearray(), width=1), "bytearray(b'')") |
---|
808 | n/a | letters = bytearray(b'abcdefghijklmnopqrstuvwxyz') |
---|
809 | n/a | self.assertEqual(pprint.pformat(letters, width=40), repr(letters)) |
---|
810 | n/a | self.assertEqual(pprint.pformat(letters, width=28), """\ |
---|
811 | n/a | bytearray(b'abcdefghijkl' |
---|
812 | n/a | b'mnopqrstuvwxyz')""") |
---|
813 | n/a | self.assertEqual(pprint.pformat(letters, width=27), """\ |
---|
814 | n/a | bytearray(b'abcdefghijkl' |
---|
815 | n/a | b'mnopqrstuvwx' |
---|
816 | n/a | b'yz')""") |
---|
817 | n/a | self.assertEqual(pprint.pformat(letters, width=25), """\ |
---|
818 | n/a | bytearray(b'abcdefghijkl' |
---|
819 | n/a | b'mnopqrstuvwx' |
---|
820 | n/a | b'yz')""") |
---|
821 | n/a | special = bytearray(range(16)) |
---|
822 | n/a | self.assertEqual(pprint.pformat(special, width=72), repr(special)) |
---|
823 | n/a | self.assertEqual(pprint.pformat(special, width=57), """\ |
---|
824 | n/a | bytearray(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b' |
---|
825 | n/a | b'\\x0c\\r\\x0e\\x0f')""") |
---|
826 | n/a | self.assertEqual(pprint.pformat(special, width=41), """\ |
---|
827 | n/a | bytearray(b'\\x00\\x01\\x02\\x03' |
---|
828 | n/a | b'\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b' |
---|
829 | n/a | b'\\x0c\\r\\x0e\\x0f')""") |
---|
830 | n/a | self.assertEqual(pprint.pformat(special, width=1), """\ |
---|
831 | n/a | bytearray(b'\\x00\\x01\\x02\\x03' |
---|
832 | n/a | b'\\x04\\x05\\x06\\x07' |
---|
833 | n/a | b'\\x08\\t\\n\\x0b' |
---|
834 | n/a | b'\\x0c\\r\\x0e\\x0f')""") |
---|
835 | n/a | self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2}, |
---|
836 | n/a | width=31), """\ |
---|
837 | n/a | {'a': 1, |
---|
838 | n/a | 'b': bytearray(b'abcdefghijkl' |
---|
839 | n/a | b'mnopqrstuvwx' |
---|
840 | n/a | b'yz'), |
---|
841 | n/a | 'c': 2}""") |
---|
842 | n/a | self.assertEqual(pprint.pformat([[[[[letters]]]]], width=37), """\ |
---|
843 | n/a | [[[[[bytearray(b'abcdefghijklmnop' |
---|
844 | n/a | b'qrstuvwxyz')]]]]]""") |
---|
845 | n/a | self.assertEqual(pprint.pformat([[[[[special]]]]], width=50), """\ |
---|
846 | n/a | [[[[[bytearray(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07' |
---|
847 | n/a | b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f')]]]]]""") |
---|
848 | n/a | |
---|
849 | n/a | def test_default_dict(self): |
---|
850 | n/a | d = collections.defaultdict(int) |
---|
851 | n/a | self.assertEqual(pprint.pformat(d, width=1), "defaultdict(<class 'int'>, {})") |
---|
852 | n/a | words = 'the quick brown fox jumped over a lazy dog'.split() |
---|
853 | n/a | d = collections.defaultdict(int, zip(words, itertools.count())) |
---|
854 | n/a | self.assertEqual(pprint.pformat(d), |
---|
855 | n/a | """\ |
---|
856 | n/a | defaultdict(<class 'int'>, |
---|
857 | n/a | {'a': 6, |
---|
858 | n/a | 'brown': 2, |
---|
859 | n/a | 'dog': 8, |
---|
860 | n/a | 'fox': 3, |
---|
861 | n/a | 'jumped': 4, |
---|
862 | n/a | 'lazy': 7, |
---|
863 | n/a | 'over': 5, |
---|
864 | n/a | 'quick': 1, |
---|
865 | n/a | 'the': 0})""") |
---|
866 | n/a | |
---|
867 | n/a | def test_counter(self): |
---|
868 | n/a | d = collections.Counter() |
---|
869 | n/a | self.assertEqual(pprint.pformat(d, width=1), "Counter()") |
---|
870 | n/a | d = collections.Counter('senselessness') |
---|
871 | n/a | self.assertEqual(pprint.pformat(d, width=40), |
---|
872 | n/a | """\ |
---|
873 | n/a | Counter({'s': 6, |
---|
874 | n/a | 'e': 4, |
---|
875 | n/a | 'n': 2, |
---|
876 | n/a | 'l': 1})""") |
---|
877 | n/a | |
---|
878 | n/a | def test_chainmap(self): |
---|
879 | n/a | d = collections.ChainMap() |
---|
880 | n/a | self.assertEqual(pprint.pformat(d, width=1), "ChainMap({})") |
---|
881 | n/a | words = 'the quick brown fox jumped over a lazy dog'.split() |
---|
882 | n/a | items = list(zip(words, itertools.count())) |
---|
883 | n/a | d = collections.ChainMap(dict(items)) |
---|
884 | n/a | self.assertEqual(pprint.pformat(d), |
---|
885 | n/a | """\ |
---|
886 | n/a | ChainMap({'a': 6, |
---|
887 | n/a | 'brown': 2, |
---|
888 | n/a | 'dog': 8, |
---|
889 | n/a | 'fox': 3, |
---|
890 | n/a | 'jumped': 4, |
---|
891 | n/a | 'lazy': 7, |
---|
892 | n/a | 'over': 5, |
---|
893 | n/a | 'quick': 1, |
---|
894 | n/a | 'the': 0})""") |
---|
895 | n/a | d = collections.ChainMap(dict(items), collections.OrderedDict(items)) |
---|
896 | n/a | self.assertEqual(pprint.pformat(d), |
---|
897 | n/a | """\ |
---|
898 | n/a | ChainMap({'a': 6, |
---|
899 | n/a | 'brown': 2, |
---|
900 | n/a | 'dog': 8, |
---|
901 | n/a | 'fox': 3, |
---|
902 | n/a | 'jumped': 4, |
---|
903 | n/a | 'lazy': 7, |
---|
904 | n/a | 'over': 5, |
---|
905 | n/a | 'quick': 1, |
---|
906 | n/a | 'the': 0}, |
---|
907 | n/a | OrderedDict([('the', 0), |
---|
908 | n/a | ('quick', 1), |
---|
909 | n/a | ('brown', 2), |
---|
910 | n/a | ('fox', 3), |
---|
911 | n/a | ('jumped', 4), |
---|
912 | n/a | ('over', 5), |
---|
913 | n/a | ('a', 6), |
---|
914 | n/a | ('lazy', 7), |
---|
915 | n/a | ('dog', 8)]))""") |
---|
916 | n/a | |
---|
917 | n/a | def test_deque(self): |
---|
918 | n/a | d = collections.deque() |
---|
919 | n/a | self.assertEqual(pprint.pformat(d, width=1), "deque([])") |
---|
920 | n/a | d = collections.deque(maxlen=7) |
---|
921 | n/a | self.assertEqual(pprint.pformat(d, width=1), "deque([], maxlen=7)") |
---|
922 | n/a | words = 'the quick brown fox jumped over a lazy dog'.split() |
---|
923 | n/a | d = collections.deque(zip(words, itertools.count())) |
---|
924 | n/a | self.assertEqual(pprint.pformat(d), |
---|
925 | n/a | """\ |
---|
926 | n/a | deque([('the', 0), |
---|
927 | n/a | ('quick', 1), |
---|
928 | n/a | ('brown', 2), |
---|
929 | n/a | ('fox', 3), |
---|
930 | n/a | ('jumped', 4), |
---|
931 | n/a | ('over', 5), |
---|
932 | n/a | ('a', 6), |
---|
933 | n/a | ('lazy', 7), |
---|
934 | n/a | ('dog', 8)])""") |
---|
935 | n/a | d = collections.deque(zip(words, itertools.count()), maxlen=7) |
---|
936 | n/a | self.assertEqual(pprint.pformat(d), |
---|
937 | n/a | """\ |
---|
938 | n/a | deque([('brown', 2), |
---|
939 | n/a | ('fox', 3), |
---|
940 | n/a | ('jumped', 4), |
---|
941 | n/a | ('over', 5), |
---|
942 | n/a | ('a', 6), |
---|
943 | n/a | ('lazy', 7), |
---|
944 | n/a | ('dog', 8)], |
---|
945 | n/a | maxlen=7)""") |
---|
946 | n/a | |
---|
947 | n/a | def test_user_dict(self): |
---|
948 | n/a | d = collections.UserDict() |
---|
949 | n/a | self.assertEqual(pprint.pformat(d, width=1), "{}") |
---|
950 | n/a | words = 'the quick brown fox jumped over a lazy dog'.split() |
---|
951 | n/a | d = collections.UserDict(zip(words, itertools.count())) |
---|
952 | n/a | self.assertEqual(pprint.pformat(d), |
---|
953 | n/a | """\ |
---|
954 | n/a | {'a': 6, |
---|
955 | n/a | 'brown': 2, |
---|
956 | n/a | 'dog': 8, |
---|
957 | n/a | 'fox': 3, |
---|
958 | n/a | 'jumped': 4, |
---|
959 | n/a | 'lazy': 7, |
---|
960 | n/a | 'over': 5, |
---|
961 | n/a | 'quick': 1, |
---|
962 | n/a | 'the': 0}""") |
---|
963 | n/a | |
---|
964 | n/a | def test_user_list(self): |
---|
965 | n/a | d = collections.UserList() |
---|
966 | n/a | self.assertEqual(pprint.pformat(d, width=1), "[]") |
---|
967 | n/a | words = 'the quick brown fox jumped over a lazy dog'.split() |
---|
968 | n/a | d = collections.UserList(zip(words, itertools.count())) |
---|
969 | n/a | self.assertEqual(pprint.pformat(d), |
---|
970 | n/a | """\ |
---|
971 | n/a | [('the', 0), |
---|
972 | n/a | ('quick', 1), |
---|
973 | n/a | ('brown', 2), |
---|
974 | n/a | ('fox', 3), |
---|
975 | n/a | ('jumped', 4), |
---|
976 | n/a | ('over', 5), |
---|
977 | n/a | ('a', 6), |
---|
978 | n/a | ('lazy', 7), |
---|
979 | n/a | ('dog', 8)]""") |
---|
980 | n/a | |
---|
981 | n/a | def test_user_string(self): |
---|
982 | n/a | d = collections.UserString('') |
---|
983 | n/a | self.assertEqual(pprint.pformat(d, width=1), "''") |
---|
984 | n/a | d = collections.UserString('the quick brown fox jumped over a lazy dog') |
---|
985 | n/a | self.assertEqual(pprint.pformat(d, width=20), |
---|
986 | n/a | """\ |
---|
987 | n/a | ('the quick brown ' |
---|
988 | n/a | 'fox jumped over ' |
---|
989 | n/a | 'a lazy dog')""") |
---|
990 | n/a | self.assertEqual(pprint.pformat({1: d}, width=20), |
---|
991 | n/a | """\ |
---|
992 | n/a | {1: 'the quick ' |
---|
993 | n/a | 'brown fox ' |
---|
994 | n/a | 'jumped over a ' |
---|
995 | n/a | 'lazy dog'}""") |
---|
996 | n/a | |
---|
997 | n/a | |
---|
998 | n/a | class DottedPrettyPrinter(pprint.PrettyPrinter): |
---|
999 | n/a | |
---|
1000 | n/a | def format(self, object, context, maxlevels, level): |
---|
1001 | n/a | if isinstance(object, str): |
---|
1002 | n/a | if ' ' in object: |
---|
1003 | n/a | return repr(object), 1, 0 |
---|
1004 | n/a | else: |
---|
1005 | n/a | return object, 0, 0 |
---|
1006 | n/a | else: |
---|
1007 | n/a | return pprint.PrettyPrinter.format( |
---|
1008 | n/a | self, object, context, maxlevels, level) |
---|
1009 | n/a | |
---|
1010 | n/a | |
---|
1011 | n/a | if __name__ == "__main__": |
---|
1012 | n/a | unittest.main() |
---|