Show More
@@ -1,423 +1,423 b'' | |||||
1 | # coding: utf-8 |
|
1 | # coding: utf-8 | |
2 | """Tests for IPython.lib.pretty.""" |
|
2 | """Tests for IPython.lib.pretty.""" | |
3 |
|
3 | |||
4 | # Copyright (c) IPython Development Team. |
|
4 | # Copyright (c) IPython Development Team. | |
5 | # Distributed under the terms of the Modified BSD License. |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 |
|
6 | |||
7 |
|
7 | |||
8 | from collections import Counter, defaultdict, deque, OrderedDict |
|
8 | from collections import Counter, defaultdict, deque, OrderedDict | |
9 | import types |
|
9 | import types | |
10 | import string |
|
10 | import string | |
11 | import unittest |
|
11 | import unittest | |
12 |
|
12 | |||
13 | import nose.tools as nt |
|
13 | import nose.tools as nt | |
14 |
|
14 | |||
15 | from IPython.lib import pretty |
|
15 | from IPython.lib import pretty | |
16 | from IPython.testing.decorators import skip_without |
|
16 | from IPython.testing.decorators import skip_without | |
17 |
|
17 | |||
18 | from io import StringIO |
|
18 | from io import StringIO | |
19 |
|
19 | |||
20 |
|
20 | |||
21 | class MyList(object): |
|
21 | class MyList(object): | |
22 | def __init__(self, content): |
|
22 | def __init__(self, content): | |
23 | self.content = content |
|
23 | self.content = content | |
24 | def _repr_pretty_(self, p, cycle): |
|
24 | def _repr_pretty_(self, p, cycle): | |
25 | if cycle: |
|
25 | if cycle: | |
26 | p.text("MyList(...)") |
|
26 | p.text("MyList(...)") | |
27 | else: |
|
27 | else: | |
28 | with p.group(3, "MyList(", ")"): |
|
28 | with p.group(3, "MyList(", ")"): | |
29 | for (i, child) in enumerate(self.content): |
|
29 | for (i, child) in enumerate(self.content): | |
30 | if i: |
|
30 | if i: | |
31 | p.text(",") |
|
31 | p.text(",") | |
32 | p.breakable() |
|
32 | p.breakable() | |
33 | else: |
|
33 | else: | |
34 | p.breakable("") |
|
34 | p.breakable("") | |
35 | p.pretty(child) |
|
35 | p.pretty(child) | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | class MyDict(dict): |
|
38 | class MyDict(dict): | |
39 | def _repr_pretty_(self, p, cycle): |
|
39 | def _repr_pretty_(self, p, cycle): | |
40 | p.text("MyDict(...)") |
|
40 | p.text("MyDict(...)") | |
41 |
|
41 | |||
42 | class MyObj(object): |
|
42 | class MyObj(object): | |
43 | def somemethod(self): |
|
43 | def somemethod(self): | |
44 | pass |
|
44 | pass | |
45 |
|
45 | |||
46 |
|
46 | |||
47 | class Dummy1(object): |
|
47 | class Dummy1(object): | |
48 | def _repr_pretty_(self, p, cycle): |
|
48 | def _repr_pretty_(self, p, cycle): | |
49 | p.text("Dummy1(...)") |
|
49 | p.text("Dummy1(...)") | |
50 |
|
50 | |||
51 | class Dummy2(Dummy1): |
|
51 | class Dummy2(Dummy1): | |
52 | _repr_pretty_ = None |
|
52 | _repr_pretty_ = None | |
53 |
|
53 | |||
54 | class NoModule(object): |
|
54 | class NoModule(object): | |
55 | pass |
|
55 | pass | |
56 |
|
56 | |||
57 | NoModule.__module__ = None |
|
57 | NoModule.__module__ = None | |
58 |
|
58 | |||
59 | class Breaking(object): |
|
59 | class Breaking(object): | |
60 | def _repr_pretty_(self, p, cycle): |
|
60 | def _repr_pretty_(self, p, cycle): | |
61 | with p.group(4,"TG: ",":"): |
|
61 | with p.group(4,"TG: ",":"): | |
62 | p.text("Breaking(") |
|
62 | p.text("Breaking(") | |
63 | p.break_() |
|
63 | p.break_() | |
64 | p.text(")") |
|
64 | p.text(")") | |
65 |
|
65 | |||
66 | class BreakingRepr(object): |
|
66 | class BreakingRepr(object): | |
67 | def __repr__(self): |
|
67 | def __repr__(self): | |
68 | return "Breaking(\n)" |
|
68 | return "Breaking(\n)" | |
69 |
|
69 | |||
70 | class BreakingReprParent(object): |
|
70 | class BreakingReprParent(object): | |
71 | def _repr_pretty_(self, p, cycle): |
|
71 | def _repr_pretty_(self, p, cycle): | |
72 | with p.group(4,"TG: ",":"): |
|
72 | with p.group(4,"TG: ",":"): | |
73 | p.pretty(BreakingRepr()) |
|
73 | p.pretty(BreakingRepr()) | |
74 |
|
74 | |||
75 | class BadRepr(object): |
|
75 | class BadRepr(object): | |
76 |
|
76 | |||
77 | def __repr__(self): |
|
77 | def __repr__(self): | |
78 | return 1/0 |
|
78 | return 1/0 | |
79 |
|
79 | |||
80 |
|
80 | |||
81 | def test_indentation(): |
|
81 | def test_indentation(): | |
82 | """Test correct indentation in groups""" |
|
82 | """Test correct indentation in groups""" | |
83 | count = 40 |
|
83 | count = 40 | |
84 | gotoutput = pretty.pretty(MyList(range(count))) |
|
84 | gotoutput = pretty.pretty(MyList(range(count))) | |
85 | expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")" |
|
85 | expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")" | |
86 |
|
86 | |||
87 | nt.assert_equal(gotoutput, expectedoutput) |
|
87 | nt.assert_equal(gotoutput, expectedoutput) | |
88 |
|
88 | |||
89 |
|
89 | |||
90 | def test_dispatch(): |
|
90 | def test_dispatch(): | |
91 | """ |
|
91 | """ | |
92 | Test correct dispatching: The _repr_pretty_ method for MyDict |
|
92 | Test correct dispatching: The _repr_pretty_ method for MyDict | |
93 | must be found before the registered printer for dict. |
|
93 | must be found before the registered printer for dict. | |
94 | """ |
|
94 | """ | |
95 | gotoutput = pretty.pretty(MyDict()) |
|
95 | gotoutput = pretty.pretty(MyDict()) | |
96 | expectedoutput = "MyDict(...)" |
|
96 | expectedoutput = "MyDict(...)" | |
97 |
|
97 | |||
98 | nt.assert_equal(gotoutput, expectedoutput) |
|
98 | nt.assert_equal(gotoutput, expectedoutput) | |
99 |
|
99 | |||
100 |
|
100 | |||
101 | def test_callability_checking(): |
|
101 | def test_callability_checking(): | |
102 | """ |
|
102 | """ | |
103 | Test that the _repr_pretty_ method is tested for callability and skipped if |
|
103 | Test that the _repr_pretty_ method is tested for callability and skipped if | |
104 | not. |
|
104 | not. | |
105 | """ |
|
105 | """ | |
106 | gotoutput = pretty.pretty(Dummy2()) |
|
106 | gotoutput = pretty.pretty(Dummy2()) | |
107 | expectedoutput = "Dummy1(...)" |
|
107 | expectedoutput = "Dummy1(...)" | |
108 |
|
108 | |||
109 | nt.assert_equal(gotoutput, expectedoutput) |
|
109 | nt.assert_equal(gotoutput, expectedoutput) | |
110 |
|
110 | |||
111 |
|
111 | |||
112 | def test_sets(): |
|
112 | def test_sets(): | |
113 | """ |
|
113 | """ | |
114 | Test that set and frozenset use Python 3 formatting. |
|
114 | Test that set and frozenset use Python 3 formatting. | |
115 | """ |
|
115 | """ | |
116 | objects = [set(), frozenset(), set([1]), frozenset([1]), set([1, 2]), |
|
116 | objects = [set(), frozenset(), set([1]), frozenset([1]), set([1, 2]), | |
117 | frozenset([1, 2]), set([-1, -2, -3])] |
|
117 | frozenset([1, 2]), set([-1, -2, -3])] | |
118 | expected = ['set()', 'frozenset()', '{1}', 'frozenset({1})', '{1, 2}', |
|
118 | expected = ['set()', 'frozenset()', '{1}', 'frozenset({1})', '{1, 2}', | |
119 | 'frozenset({1, 2})', '{-3, -2, -1}'] |
|
119 | 'frozenset({1, 2})', '{-3, -2, -1}'] | |
120 | for obj, expected_output in zip(objects, expected): |
|
120 | for obj, expected_output in zip(objects, expected): | |
121 | got_output = pretty.pretty(obj) |
|
121 | got_output = pretty.pretty(obj) | |
122 | yield nt.assert_equal, got_output, expected_output |
|
122 | yield nt.assert_equal, got_output, expected_output | |
123 |
|
123 | |||
124 |
|
124 | |||
125 | @skip_without('xxlimited') |
|
125 | @skip_without('xxlimited') | |
126 | def test_pprint_heap_allocated_type(): |
|
126 | def test_pprint_heap_allocated_type(): | |
127 | """ |
|
127 | """ | |
128 | Test that pprint works for heap allocated types. |
|
128 | Test that pprint works for heap allocated types. | |
129 | """ |
|
129 | """ | |
130 | import xxlimited |
|
130 | import xxlimited | |
131 | output = pretty.pretty(xxlimited.Null) |
|
131 | output = pretty.pretty(xxlimited.Null) | |
132 | nt.assert_equal(output, 'xxlimited.Null') |
|
132 | nt.assert_equal(output, 'xxlimited.Null') | |
133 |
|
133 | |||
134 | def test_pprint_nomod(): |
|
134 | def test_pprint_nomod(): | |
135 | """ |
|
135 | """ | |
136 | Test that pprint works for classes with no __module__. |
|
136 | Test that pprint works for classes with no __module__. | |
137 | """ |
|
137 | """ | |
138 | output = pretty.pretty(NoModule) |
|
138 | output = pretty.pretty(NoModule) | |
139 | nt.assert_equal(output, 'NoModule') |
|
139 | nt.assert_equal(output, 'NoModule') | |
140 |
|
140 | |||
141 | def test_pprint_break(): |
|
141 | def test_pprint_break(): | |
142 | """ |
|
142 | """ | |
143 | Test that p.break_ produces expected output |
|
143 | Test that p.break_ produces expected output | |
144 | """ |
|
144 | """ | |
145 | output = pretty.pretty(Breaking()) |
|
145 | output = pretty.pretty(Breaking()) | |
146 | expected = "TG: Breaking(\n ):" |
|
146 | expected = "TG: Breaking(\n ):" | |
147 | nt.assert_equal(output, expected) |
|
147 | nt.assert_equal(output, expected) | |
148 |
|
148 | |||
149 | def test_pprint_break_repr(): |
|
149 | def test_pprint_break_repr(): | |
150 | """ |
|
150 | """ | |
151 | Test that p.break_ is used in repr |
|
151 | Test that p.break_ is used in repr | |
152 | """ |
|
152 | """ | |
153 | output = pretty.pretty(BreakingReprParent()) |
|
153 | output = pretty.pretty(BreakingReprParent()) | |
154 | expected = "TG: Breaking(\n ):" |
|
154 | expected = "TG: Breaking(\n ):" | |
155 | nt.assert_equal(output, expected) |
|
155 | nt.assert_equal(output, expected) | |
156 |
|
156 | |||
157 | def test_bad_repr(): |
|
157 | def test_bad_repr(): | |
158 | """Don't catch bad repr errors""" |
|
158 | """Don't catch bad repr errors""" | |
159 | with nt.assert_raises(ZeroDivisionError): |
|
159 | with nt.assert_raises(ZeroDivisionError): | |
160 | pretty.pretty(BadRepr()) |
|
160 | pretty.pretty(BadRepr()) | |
161 |
|
161 | |||
162 | class BadException(Exception): |
|
162 | class BadException(Exception): | |
163 | def __str__(self): |
|
163 | def __str__(self): | |
164 | return -1 |
|
164 | return -1 | |
165 |
|
165 | |||
166 | class ReallyBadRepr(object): |
|
166 | class ReallyBadRepr(object): | |
167 | __module__ = 1 |
|
167 | __module__ = 1 | |
168 | @property |
|
168 | @property | |
169 | def __class__(self): |
|
169 | def __class__(self): | |
170 | raise ValueError("I am horrible") |
|
170 | raise ValueError("I am horrible") | |
171 |
|
171 | |||
172 | def __repr__(self): |
|
172 | def __repr__(self): | |
173 | raise BadException() |
|
173 | raise BadException() | |
174 |
|
174 | |||
175 | def test_really_bad_repr(): |
|
175 | def test_really_bad_repr(): | |
176 | with nt.assert_raises(BadException): |
|
176 | with nt.assert_raises(BadException): | |
177 | pretty.pretty(ReallyBadRepr()) |
|
177 | pretty.pretty(ReallyBadRepr()) | |
178 |
|
178 | |||
179 |
|
179 | |||
180 | class SA(object): |
|
180 | class SA(object): | |
181 | pass |
|
181 | pass | |
182 |
|
182 | |||
183 | class SB(SA): |
|
183 | class SB(SA): | |
184 | pass |
|
184 | pass | |
185 |
|
185 | |||
186 | class TestsPretty(unittest.TestCase): |
|
186 | class TestsPretty(unittest.TestCase): | |
187 |
|
187 | |||
188 | def test_super_repr(self): |
|
188 | def test_super_repr(self): | |
189 | # "<super: module_name.SA, None>" |
|
189 | # "<super: module_name.SA, None>" | |
190 | output = pretty.pretty(super(SA)) |
|
190 | output = pretty.pretty(super(SA)) | |
191 | self.assertRegex(output, r"<super: \S+.SA, None>") |
|
191 | self.assertRegex(output, r"<super: \S+.SA, None>") | |
192 |
|
192 | |||
193 | # "<super: module_name.SA, <module_name.SB at 0x...>>" |
|
193 | # "<super: module_name.SA, <module_name.SB at 0x...>>" | |
194 | sb = SB() |
|
194 | sb = SB() | |
195 | output = pretty.pretty(super(SA, sb)) |
|
195 | output = pretty.pretty(super(SA, sb)) | |
196 | self.assertRegex(output, r"<super: \S+.SA,\s+<\S+.SB at 0x\S+>>") |
|
196 | self.assertRegex(output, r"<super: \S+.SA,\s+<\S+.SB at 0x\S+>>") | |
197 |
|
197 | |||
198 |
|
198 | |||
199 | def test_long_list(self): |
|
199 | def test_long_list(self): | |
200 | lis = list(range(10000)) |
|
200 | lis = list(range(10000)) | |
201 | p = pretty.pretty(lis) |
|
201 | p = pretty.pretty(lis) | |
202 | last2 = p.rsplit('\n', 2)[-2:] |
|
202 | last2 = p.rsplit('\n', 2)[-2:] | |
203 | self.assertEqual(last2, [' 999,', ' ...]']) |
|
203 | self.assertEqual(last2, [' 999,', ' ...]']) | |
204 |
|
204 | |||
205 | def test_long_set(self): |
|
205 | def test_long_set(self): | |
206 | s = set(range(10000)) |
|
206 | s = set(range(10000)) | |
207 | p = pretty.pretty(s) |
|
207 | p = pretty.pretty(s) | |
208 | last2 = p.rsplit('\n', 2)[-2:] |
|
208 | last2 = p.rsplit('\n', 2)[-2:] | |
209 | self.assertEqual(last2, [' 999,', ' ...}']) |
|
209 | self.assertEqual(last2, [' 999,', ' ...}']) | |
210 |
|
210 | |||
211 | def test_long_tuple(self): |
|
211 | def test_long_tuple(self): | |
212 | tup = tuple(range(10000)) |
|
212 | tup = tuple(range(10000)) | |
213 | p = pretty.pretty(tup) |
|
213 | p = pretty.pretty(tup) | |
214 | last2 = p.rsplit('\n', 2)[-2:] |
|
214 | last2 = p.rsplit('\n', 2)[-2:] | |
215 | self.assertEqual(last2, [' 999,', ' ...)']) |
|
215 | self.assertEqual(last2, [' 999,', ' ...)']) | |
216 |
|
216 | |||
217 | def test_long_dict(self): |
|
217 | def test_long_dict(self): | |
218 | d = { n:n for n in range(10000) } |
|
218 | d = { n:n for n in range(10000) } | |
219 | p = pretty.pretty(d) |
|
219 | p = pretty.pretty(d) | |
220 | last2 = p.rsplit('\n', 2)[-2:] |
|
220 | last2 = p.rsplit('\n', 2)[-2:] | |
221 | self.assertEqual(last2, [' 999: 999,', ' ...}']) |
|
221 | self.assertEqual(last2, [' 999: 999,', ' ...}']) | |
222 |
|
222 | |||
223 | def test_unbound_method(self): |
|
223 | def test_unbound_method(self): | |
224 | output = pretty.pretty(MyObj.somemethod) |
|
224 | output = pretty.pretty(MyObj.somemethod) | |
225 | self.assertIn('MyObj.somemethod', output) |
|
225 | self.assertIn('MyObj.somemethod', output) | |
226 |
|
226 | |||
227 |
|
227 | |||
228 | class MetaClass(type): |
|
228 | class MetaClass(type): | |
229 | def __new__(cls, name): |
|
229 | def __new__(cls, name): | |
230 | return type.__new__(cls, name, (object,), {'name': name}) |
|
230 | return type.__new__(cls, name, (object,), {'name': name}) | |
231 |
|
231 | |||
232 | def __repr__(self): |
|
232 | def __repr__(self): | |
233 | return "[CUSTOM REPR FOR CLASS %s]" % self.name |
|
233 | return "[CUSTOM REPR FOR CLASS %s]" % self.name | |
234 |
|
234 | |||
235 |
|
235 | |||
236 | ClassWithMeta = MetaClass('ClassWithMeta') |
|
236 | ClassWithMeta = MetaClass('ClassWithMeta') | |
237 |
|
237 | |||
238 |
|
238 | |||
239 | def test_metaclass_repr(): |
|
239 | def test_metaclass_repr(): | |
240 | output = pretty.pretty(ClassWithMeta) |
|
240 | output = pretty.pretty(ClassWithMeta) | |
241 | nt.assert_equal(output, "[CUSTOM REPR FOR CLASS ClassWithMeta]") |
|
241 | nt.assert_equal(output, "[CUSTOM REPR FOR CLASS ClassWithMeta]") | |
242 |
|
242 | |||
243 |
|
243 | |||
244 | def test_unicode_repr(): |
|
244 | def test_unicode_repr(): | |
245 | u = u"üniçodé" |
|
245 | u = u"üniçodé" | |
246 | ustr = u |
|
246 | ustr = u | |
247 |
|
247 | |||
248 | class C(object): |
|
248 | class C(object): | |
249 | def __repr__(self): |
|
249 | def __repr__(self): | |
250 | return ustr |
|
250 | return ustr | |
251 |
|
251 | |||
252 | c = C() |
|
252 | c = C() | |
253 | p = pretty.pretty(c) |
|
253 | p = pretty.pretty(c) | |
254 | nt.assert_equal(p, u) |
|
254 | nt.assert_equal(p, u) | |
255 | p = pretty.pretty([c]) |
|
255 | p = pretty.pretty([c]) | |
256 | nt.assert_equal(p, u'[%s]' % u) |
|
256 | nt.assert_equal(p, u'[%s]' % u) | |
257 |
|
257 | |||
258 |
|
258 | |||
259 | def test_basic_class(): |
|
259 | def test_basic_class(): | |
260 | def type_pprint_wrapper(obj, p, cycle): |
|
260 | def type_pprint_wrapper(obj, p, cycle): | |
261 | if obj is MyObj: |
|
261 | if obj is MyObj: | |
262 | type_pprint_wrapper.called = True |
|
262 | type_pprint_wrapper.called = True | |
263 | return pretty._type_pprint(obj, p, cycle) |
|
263 | return pretty._type_pprint(obj, p, cycle) | |
264 | type_pprint_wrapper.called = False |
|
264 | type_pprint_wrapper.called = False | |
265 |
|
265 | |||
266 | stream = StringIO() |
|
266 | stream = StringIO() | |
267 | printer = pretty.RepresentationPrinter(stream) |
|
267 | printer = pretty.RepresentationPrinter(stream) | |
268 | printer.type_pprinters[type] = type_pprint_wrapper |
|
268 | printer.type_pprinters[type] = type_pprint_wrapper | |
269 | printer.pretty(MyObj) |
|
269 | printer.pretty(MyObj) | |
270 | printer.flush() |
|
270 | printer.flush() | |
271 | output = stream.getvalue() |
|
271 | output = stream.getvalue() | |
272 |
|
272 | |||
273 | nt.assert_equal(output, '%s.MyObj' % __name__) |
|
273 | nt.assert_equal(output, '%s.MyObj' % __name__) | |
274 | nt.assert_true(type_pprint_wrapper.called) |
|
274 | nt.assert_true(type_pprint_wrapper.called) | |
275 |
|
275 | |||
276 |
|
276 | |||
277 | def test_collections_defaultdict(): |
|
277 | def test_collections_defaultdict(): | |
278 | # Create defaultdicts with cycles |
|
278 | # Create defaultdicts with cycles | |
279 | a = defaultdict() |
|
279 | a = defaultdict() | |
280 | a.default_factory = a |
|
280 | a.default_factory = a | |
281 | b = defaultdict(list) |
|
281 | b = defaultdict(list) | |
282 | b['key'] = b |
|
282 | b['key'] = b | |
283 |
|
283 | |||
284 | # Dictionary order cannot be relied on, test against single keys. |
|
284 | # Dictionary order cannot be relied on, test against single keys. | |
285 | cases = [ |
|
285 | cases = [ | |
286 | (defaultdict(list), 'defaultdict(list, {})'), |
|
286 | (defaultdict(list), 'defaultdict(list, {})'), | |
287 | (defaultdict(list, {'key': '-' * 50}), |
|
287 | (defaultdict(list, {'key': '-' * 50}), | |
288 | "defaultdict(list,\n" |
|
288 | "defaultdict(list,\n" | |
289 | " {'key': '--------------------------------------------------'})"), |
|
289 | " {'key': '--------------------------------------------------'})"), | |
290 | (a, 'defaultdict(defaultdict(...), {})'), |
|
290 | (a, 'defaultdict(defaultdict(...), {})'), | |
291 | (b, "defaultdict(list, {'key': defaultdict(...)})"), |
|
291 | (b, "defaultdict(list, {'key': defaultdict(...)})"), | |
292 | ] |
|
292 | ] | |
293 | for obj, expected in cases: |
|
293 | for obj, expected in cases: | |
294 | nt.assert_equal(pretty.pretty(obj), expected) |
|
294 | nt.assert_equal(pretty.pretty(obj), expected) | |
295 |
|
295 | |||
296 |
|
296 | |||
297 | def test_collections_ordereddict(): |
|
297 | def test_collections_ordereddict(): | |
298 | # Create OrderedDict with cycle |
|
298 | # Create OrderedDict with cycle | |
299 | a = OrderedDict() |
|
299 | a = OrderedDict() | |
300 | a['key'] = a |
|
300 | a['key'] = a | |
301 |
|
301 | |||
302 | cases = [ |
|
302 | cases = [ | |
303 | (OrderedDict(), 'OrderedDict()'), |
|
303 | (OrderedDict(), 'OrderedDict()'), | |
304 | (OrderedDict((i, i) for i in range(1000, 1010)), |
|
304 | (OrderedDict((i, i) for i in range(1000, 1010)), | |
305 | 'OrderedDict([(1000, 1000),\n' |
|
305 | 'OrderedDict([(1000, 1000),\n' | |
306 | ' (1001, 1001),\n' |
|
306 | ' (1001, 1001),\n' | |
307 | ' (1002, 1002),\n' |
|
307 | ' (1002, 1002),\n' | |
308 | ' (1003, 1003),\n' |
|
308 | ' (1003, 1003),\n' | |
309 | ' (1004, 1004),\n' |
|
309 | ' (1004, 1004),\n' | |
310 | ' (1005, 1005),\n' |
|
310 | ' (1005, 1005),\n' | |
311 | ' (1006, 1006),\n' |
|
311 | ' (1006, 1006),\n' | |
312 | ' (1007, 1007),\n' |
|
312 | ' (1007, 1007),\n' | |
313 | ' (1008, 1008),\n' |
|
313 | ' (1008, 1008),\n' | |
314 | ' (1009, 1009)])'), |
|
314 | ' (1009, 1009)])'), | |
315 | (a, "OrderedDict([('key', OrderedDict(...))])"), |
|
315 | (a, "OrderedDict([('key', OrderedDict(...))])"), | |
316 | ] |
|
316 | ] | |
317 | for obj, expected in cases: |
|
317 | for obj, expected in cases: | |
318 | nt.assert_equal(pretty.pretty(obj), expected) |
|
318 | nt.assert_equal(pretty.pretty(obj), expected) | |
319 |
|
319 | |||
320 |
|
320 | |||
321 | def test_collections_deque(): |
|
321 | def test_collections_deque(): | |
322 | # Create deque with cycle |
|
322 | # Create deque with cycle | |
323 | a = deque() |
|
323 | a = deque() | |
324 | a.append(a) |
|
324 | a.append(a) | |
325 |
|
325 | |||
326 | cases = [ |
|
326 | cases = [ | |
327 | (deque(), 'deque([])'), |
|
327 | (deque(), 'deque([])'), | |
328 | (deque(i for i in range(1000, 1020)), |
|
328 | (deque(i for i in range(1000, 1020)), | |
329 | 'deque([1000,\n' |
|
329 | 'deque([1000,\n' | |
330 | ' 1001,\n' |
|
330 | ' 1001,\n' | |
331 | ' 1002,\n' |
|
331 | ' 1002,\n' | |
332 | ' 1003,\n' |
|
332 | ' 1003,\n' | |
333 | ' 1004,\n' |
|
333 | ' 1004,\n' | |
334 | ' 1005,\n' |
|
334 | ' 1005,\n' | |
335 | ' 1006,\n' |
|
335 | ' 1006,\n' | |
336 | ' 1007,\n' |
|
336 | ' 1007,\n' | |
337 | ' 1008,\n' |
|
337 | ' 1008,\n' | |
338 | ' 1009,\n' |
|
338 | ' 1009,\n' | |
339 | ' 1010,\n' |
|
339 | ' 1010,\n' | |
340 | ' 1011,\n' |
|
340 | ' 1011,\n' | |
341 | ' 1012,\n' |
|
341 | ' 1012,\n' | |
342 | ' 1013,\n' |
|
342 | ' 1013,\n' | |
343 | ' 1014,\n' |
|
343 | ' 1014,\n' | |
344 | ' 1015,\n' |
|
344 | ' 1015,\n' | |
345 | ' 1016,\n' |
|
345 | ' 1016,\n' | |
346 | ' 1017,\n' |
|
346 | ' 1017,\n' | |
347 | ' 1018,\n' |
|
347 | ' 1018,\n' | |
348 | ' 1019])'), |
|
348 | ' 1019])'), | |
349 | (a, 'deque([deque(...)])'), |
|
349 | (a, 'deque([deque(...)])'), | |
350 | ] |
|
350 | ] | |
351 | for obj, expected in cases: |
|
351 | for obj, expected in cases: | |
352 | nt.assert_equal(pretty.pretty(obj), expected) |
|
352 | nt.assert_equal(pretty.pretty(obj), expected) | |
353 |
|
353 | |||
354 | def test_collections_counter(): |
|
354 | def test_collections_counter(): | |
355 | class MyCounter(Counter): |
|
355 | class MyCounter(Counter): | |
356 | pass |
|
356 | pass | |
357 | cases = [ |
|
357 | cases = [ | |
358 | (Counter(), 'Counter()'), |
|
358 | (Counter(), 'Counter()'), | |
359 | (Counter(a=1), "Counter({'a': 1})"), |
|
359 | (Counter(a=1), "Counter({'a': 1})"), | |
360 | (MyCounter(a=1), "MyCounter({'a': 1})"), |
|
360 | (MyCounter(a=1), "MyCounter({'a': 1})"), | |
361 | ] |
|
361 | ] | |
362 | for obj, expected in cases: |
|
362 | for obj, expected in cases: | |
363 | nt.assert_equal(pretty.pretty(obj), expected) |
|
363 | nt.assert_equal(pretty.pretty(obj), expected) | |
364 |
|
364 | |||
365 | def test_mappingproxy(): |
|
365 | def test_mappingproxy(): | |
366 | MP = types.MappingProxyType |
|
366 | MP = types.MappingProxyType | |
367 | underlying_dict = {} |
|
367 | underlying_dict = {} | |
368 | mp_recursive = MP(underlying_dict) |
|
368 | mp_recursive = MP(underlying_dict) | |
369 | underlying_dict[2] = mp_recursive |
|
369 | underlying_dict[2] = mp_recursive | |
370 | underlying_dict[3] = underlying_dict |
|
370 | underlying_dict[3] = underlying_dict | |
371 |
|
371 | |||
372 | cases = [ |
|
372 | cases = [ | |
373 | (MP({}), "mappingproxy({})"), |
|
373 | (MP({}), "mappingproxy({})"), | |
374 | (MP({None: MP({})}), "mappingproxy({None: mappingproxy({})})"), |
|
374 | (MP({None: MP({})}), "mappingproxy({None: mappingproxy({})})"), | |
375 | (MP({k: k.upper() for k in string.ascii_lowercase}), |
|
375 | (MP({k: k.upper() for k in string.ascii_lowercase}), | |
376 | "mappingproxy({'a': 'A',\n" |
|
376 | "mappingproxy({'a': 'A',\n" | |
377 | " 'b': 'B',\n" |
|
377 | " 'b': 'B',\n" | |
378 | " 'c': 'C',\n" |
|
378 | " 'c': 'C',\n" | |
379 | " 'd': 'D',\n" |
|
379 | " 'd': 'D',\n" | |
380 | " 'e': 'E',\n" |
|
380 | " 'e': 'E',\n" | |
381 | " 'f': 'F',\n" |
|
381 | " 'f': 'F',\n" | |
382 | " 'g': 'G',\n" |
|
382 | " 'g': 'G',\n" | |
383 | " 'h': 'H',\n" |
|
383 | " 'h': 'H',\n" | |
384 | " 'i': 'I',\n" |
|
384 | " 'i': 'I',\n" | |
385 | " 'j': 'J',\n" |
|
385 | " 'j': 'J',\n" | |
386 | " 'k': 'K',\n" |
|
386 | " 'k': 'K',\n" | |
387 | " 'l': 'L',\n" |
|
387 | " 'l': 'L',\n" | |
388 | " 'm': 'M',\n" |
|
388 | " 'm': 'M',\n" | |
389 | " 'n': 'N',\n" |
|
389 | " 'n': 'N',\n" | |
390 | " 'o': 'O',\n" |
|
390 | " 'o': 'O',\n" | |
391 | " 'p': 'P',\n" |
|
391 | " 'p': 'P',\n" | |
392 | " 'q': 'Q',\n" |
|
392 | " 'q': 'Q',\n" | |
393 | " 'r': 'R',\n" |
|
393 | " 'r': 'R',\n" | |
394 | " 's': 'S',\n" |
|
394 | " 's': 'S',\n" | |
395 | " 't': 'T',\n" |
|
395 | " 't': 'T',\n" | |
396 | " 'u': 'U',\n" |
|
396 | " 'u': 'U',\n" | |
397 | " 'v': 'V',\n" |
|
397 | " 'v': 'V',\n" | |
398 | " 'w': 'W',\n" |
|
398 | " 'w': 'W',\n" | |
399 | " 'x': 'X',\n" |
|
399 | " 'x': 'X',\n" | |
400 | " 'y': 'Y',\n" |
|
400 | " 'y': 'Y',\n" | |
401 | " 'z': 'Z'})"), |
|
401 | " 'z': 'Z'})"), | |
402 | (mp_recursive, "mappingproxy({2: {...}, 3: {2: {...}, 3: {...}}})"), |
|
402 | (mp_recursive, "mappingproxy({2: {...}, 3: {2: {...}, 3: {...}}})"), | |
403 | (underlying_dict, |
|
403 | (underlying_dict, | |
404 | "{2: mappingproxy({2: {...}, 3: {...}}), 3: {...}}"), |
|
404 | "{2: mappingproxy({2: {...}, 3: {...}}), 3: {...}}"), | |
405 | ] |
|
405 | ] | |
406 | for obj, expected in cases: |
|
406 | for obj, expected in cases: | |
407 | nt.assert_equal(pretty.pretty(obj), expected) |
|
407 | nt.assert_equal(pretty.pretty(obj), expected) | |
408 |
|
408 | |||
409 | def test_function_pretty(): |
|
409 | def test_function_pretty(): | |
410 | "Test pretty print of function" |
|
410 | "Test pretty print of function" | |
411 |
# posixpath is a pure python |
|
411 | # posixpath is a pure python module, its interface is consistent | |
412 | # across Python distributions |
|
412 | # across Python distributions | |
413 | import os |
|
413 | import posixpath | |
414 |
nt.assert_equal(pretty.pretty( |
|
414 | nt.assert_equal(pretty.pretty(posixpath.join), '<function posixpath.join(a, *p)>') | |
415 |
|
415 | |||
416 | # custom function |
|
416 | # custom function | |
417 | def meaning_of_life(question=None): |
|
417 | def meaning_of_life(question=None): | |
418 | if question: |
|
418 | if question: | |
419 | return 42 |
|
419 | return 42 | |
420 | return "Don't panic" |
|
420 | return "Don't panic" | |
421 |
|
421 | |||
422 | nt.assert_in('meaning_of_life(question=None)', pretty.pretty(meaning_of_life)) |
|
422 | nt.assert_in('meaning_of_life(question=None)', pretty.pretty(meaning_of_life)) | |
423 |
|
423 |
General Comments 0
You need to be logged in to leave comments.
Login now