Show More
@@ -1,256 +1,270 b'' | |||
|
1 | # coding: utf-8 | |
|
1 | 2 | """Tests for IPython.lib.pretty.""" |
|
2 | 3 | |
|
3 | 4 | # Copyright (c) IPython Development Team. |
|
4 | 5 | # Distributed under the terms of the Modified BSD License. |
|
5 | 6 | |
|
6 | 7 | from __future__ import print_function |
|
7 | 8 | |
|
8 | # Third-party imports | |
|
9 | 9 | import nose.tools as nt |
|
10 | 10 | |
|
11 | # Our own imports | |
|
12 | 11 | from IPython.lib import pretty |
|
13 | 12 | from IPython.testing.decorators import skip_without |
|
14 | from IPython.utils.py3compat import PY3 | |
|
13 | from IPython.utils.py3compat import PY3, unicode_to_str | |
|
15 | 14 | |
|
16 | 15 | if PY3: |
|
17 | 16 | from io import StringIO |
|
18 | 17 | else: |
|
19 | 18 | from StringIO import StringIO |
|
20 | 19 | |
|
21 | 20 | |
|
22 | 21 | class MyList(object): |
|
23 | 22 | def __init__(self, content): |
|
24 | 23 | self.content = content |
|
25 | 24 | def _repr_pretty_(self, p, cycle): |
|
26 | 25 | if cycle: |
|
27 | 26 | p.text("MyList(...)") |
|
28 | 27 | else: |
|
29 | 28 | with p.group(3, "MyList(", ")"): |
|
30 | 29 | for (i, child) in enumerate(self.content): |
|
31 | 30 | if i: |
|
32 | 31 | p.text(",") |
|
33 | 32 | p.breakable() |
|
34 | 33 | else: |
|
35 | 34 | p.breakable("") |
|
36 | 35 | p.pretty(child) |
|
37 | 36 | |
|
38 | 37 | |
|
39 | 38 | class MyDict(dict): |
|
40 | 39 | def _repr_pretty_(self, p, cycle): |
|
41 | 40 | p.text("MyDict(...)") |
|
42 | 41 | |
|
43 | 42 | class MyObj(object): |
|
44 | 43 | def somemethod(self): |
|
45 | 44 | pass |
|
46 | 45 | |
|
47 | 46 | |
|
48 | 47 | class Dummy1(object): |
|
49 | 48 | def _repr_pretty_(self, p, cycle): |
|
50 | 49 | p.text("Dummy1(...)") |
|
51 | 50 | |
|
52 | 51 | class Dummy2(Dummy1): |
|
53 | 52 | _repr_pretty_ = None |
|
54 | 53 | |
|
55 | 54 | class NoModule(object): |
|
56 | 55 | pass |
|
57 | 56 | |
|
58 | 57 | NoModule.__module__ = None |
|
59 | 58 | |
|
60 | 59 | class Breaking(object): |
|
61 | 60 | def _repr_pretty_(self, p, cycle): |
|
62 | 61 | with p.group(4,"TG: ",":"): |
|
63 | 62 | p.text("Breaking(") |
|
64 | 63 | p.break_() |
|
65 | 64 | p.text(")") |
|
66 | 65 | |
|
67 | 66 | class BreakingRepr(object): |
|
68 | 67 | def __repr__(self): |
|
69 | 68 | return "Breaking(\n)" |
|
70 | 69 | |
|
71 | 70 | class BreakingReprParent(object): |
|
72 | 71 | def _repr_pretty_(self, p, cycle): |
|
73 | 72 | with p.group(4,"TG: ",":"): |
|
74 | 73 | p.pretty(BreakingRepr()) |
|
75 | 74 | |
|
76 | 75 | class BadRepr(object): |
|
77 | 76 | |
|
78 | 77 | def __repr__(self): |
|
79 | 78 | return 1/0 |
|
80 | 79 | |
|
81 | 80 | |
|
82 | 81 | def test_indentation(): |
|
83 | 82 | """Test correct indentation in groups""" |
|
84 | 83 | count = 40 |
|
85 | 84 | gotoutput = pretty.pretty(MyList(range(count))) |
|
86 | 85 | expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")" |
|
87 | 86 | |
|
88 | 87 | nt.assert_equal(gotoutput, expectedoutput) |
|
89 | 88 | |
|
90 | 89 | |
|
91 | 90 | def test_dispatch(): |
|
92 | 91 | """ |
|
93 | 92 | Test correct dispatching: The _repr_pretty_ method for MyDict |
|
94 | 93 | must be found before the registered printer for dict. |
|
95 | 94 | """ |
|
96 | 95 | gotoutput = pretty.pretty(MyDict()) |
|
97 | 96 | expectedoutput = "MyDict(...)" |
|
98 | 97 | |
|
99 | 98 | nt.assert_equal(gotoutput, expectedoutput) |
|
100 | 99 | |
|
101 | 100 | |
|
102 | 101 | def test_callability_checking(): |
|
103 | 102 | """ |
|
104 | 103 | Test that the _repr_pretty_ method is tested for callability and skipped if |
|
105 | 104 | not. |
|
106 | 105 | """ |
|
107 | 106 | gotoutput = pretty.pretty(Dummy2()) |
|
108 | 107 | expectedoutput = "Dummy1(...)" |
|
109 | 108 | |
|
110 | 109 | nt.assert_equal(gotoutput, expectedoutput) |
|
111 | 110 | |
|
112 | 111 | |
|
113 | 112 | def test_sets(): |
|
114 | 113 | """ |
|
115 | 114 | Test that set and frozenset use Python 3 formatting. |
|
116 | 115 | """ |
|
117 | 116 | objects = [set(), frozenset(), set([1]), frozenset([1]), set([1, 2]), |
|
118 | 117 | frozenset([1, 2]), set([-1, -2, -3])] |
|
119 | 118 | expected = ['set()', 'frozenset()', '{1}', 'frozenset({1})', '{1, 2}', |
|
120 | 119 | 'frozenset({1, 2})', '{-3, -2, -1}'] |
|
121 | 120 | for obj, expected_output in zip(objects, expected): |
|
122 | 121 | got_output = pretty.pretty(obj) |
|
123 | 122 | yield nt.assert_equal, got_output, expected_output |
|
124 | 123 | |
|
125 | 124 | |
|
126 | 125 | @skip_without('xxlimited') |
|
127 | 126 | def test_pprint_heap_allocated_type(): |
|
128 | 127 | """ |
|
129 | 128 | Test that pprint works for heap allocated types. |
|
130 | 129 | """ |
|
131 | 130 | import xxlimited |
|
132 | 131 | output = pretty.pretty(xxlimited.Null) |
|
133 | 132 | nt.assert_equal(output, 'xxlimited.Null') |
|
134 | 133 | |
|
135 | 134 | def test_pprint_nomod(): |
|
136 | 135 | """ |
|
137 | 136 | Test that pprint works for classes with no __module__. |
|
138 | 137 | """ |
|
139 | 138 | output = pretty.pretty(NoModule) |
|
140 | 139 | nt.assert_equal(output, 'NoModule') |
|
141 | 140 | |
|
142 | 141 | def test_pprint_break(): |
|
143 | 142 | """ |
|
144 | 143 | Test that p.break_ produces expected output |
|
145 | 144 | """ |
|
146 | 145 | output = pretty.pretty(Breaking()) |
|
147 | 146 | expected = "TG: Breaking(\n ):" |
|
148 | 147 | nt.assert_equal(output, expected) |
|
149 | 148 | |
|
150 | 149 | def test_pprint_break_repr(): |
|
151 | 150 | """ |
|
152 | 151 | Test that p.break_ is used in repr |
|
153 | 152 | """ |
|
154 | 153 | output = pretty.pretty(BreakingReprParent()) |
|
155 | 154 | expected = "TG: Breaking(\n ):" |
|
156 | 155 | nt.assert_equal(output, expected) |
|
157 | 156 | |
|
158 | 157 | def test_bad_repr(): |
|
159 | 158 | """Don't catch bad repr errors""" |
|
160 | 159 | with nt.assert_raises(ZeroDivisionError): |
|
161 | 160 | output = pretty.pretty(BadRepr()) |
|
162 | 161 | |
|
163 | 162 | class BadException(Exception): |
|
164 | 163 | def __str__(self): |
|
165 | 164 | return -1 |
|
166 | 165 | |
|
167 | 166 | class ReallyBadRepr(object): |
|
168 | 167 | __module__ = 1 |
|
169 | 168 | @property |
|
170 | 169 | def __class__(self): |
|
171 | 170 | raise ValueError("I am horrible") |
|
172 | 171 | |
|
173 | 172 | def __repr__(self): |
|
174 | 173 | raise BadException() |
|
175 | 174 | |
|
176 | 175 | def test_really_bad_repr(): |
|
177 | 176 | with nt.assert_raises(BadException): |
|
178 | 177 | output = pretty.pretty(ReallyBadRepr()) |
|
179 | 178 | |
|
180 | 179 | |
|
181 | 180 | class SA(object): |
|
182 | 181 | pass |
|
183 | 182 | |
|
184 | 183 | class SB(SA): |
|
185 | 184 | pass |
|
186 | 185 | |
|
187 | 186 | def test_super_repr(): |
|
188 | 187 | output = pretty.pretty(super(SA)) |
|
189 | 188 | nt.assert_in("SA", output) |
|
190 | 189 | |
|
191 | 190 | sb = SB() |
|
192 | 191 | output = pretty.pretty(super(SA, sb)) |
|
193 | 192 | nt.assert_in("SA", output) |
|
194 | 193 | |
|
195 | 194 | |
|
196 | 195 | def test_long_list(): |
|
197 | 196 | lis = list(range(10000)) |
|
198 | 197 | p = pretty.pretty(lis) |
|
199 | 198 | last2 = p.rsplit('\n', 2)[-2:] |
|
200 | 199 | nt.assert_equal(last2, [' 999,', ' ...]']) |
|
201 | 200 | |
|
202 | 201 | def test_long_set(): |
|
203 | 202 | s = set(range(10000)) |
|
204 | 203 | p = pretty.pretty(s) |
|
205 | 204 | last2 = p.rsplit('\n', 2)[-2:] |
|
206 | 205 | nt.assert_equal(last2, [' 999,', ' ...}']) |
|
207 | 206 | |
|
208 | 207 | def test_long_tuple(): |
|
209 | 208 | tup = tuple(range(10000)) |
|
210 | 209 | p = pretty.pretty(tup) |
|
211 | 210 | last2 = p.rsplit('\n', 2)[-2:] |
|
212 | 211 | nt.assert_equal(last2, [' 999,', ' ...)']) |
|
213 | 212 | |
|
214 | 213 | def test_long_dict(): |
|
215 | 214 | d = { n:n for n in range(10000) } |
|
216 | 215 | p = pretty.pretty(d) |
|
217 | 216 | last2 = p.rsplit('\n', 2)[-2:] |
|
218 | 217 | nt.assert_equal(last2, [' 999: 999,', ' ...}']) |
|
219 | 218 | |
|
220 | 219 | def test_unbound_method(): |
|
221 | 220 | output = pretty.pretty(MyObj.somemethod) |
|
222 | 221 | nt.assert_in('MyObj.somemethod', output) |
|
223 | 222 | |
|
224 | 223 | |
|
225 | 224 | class MetaClass(type): |
|
226 | 225 | def __new__(cls, name): |
|
227 | 226 | return type.__new__(cls, name, (object,), {'name': name}) |
|
228 | 227 | |
|
229 | 228 | def __repr__(self): |
|
230 | 229 | return "[CUSTOM REPR FOR CLASS %s]" % self.name |
|
231 | 230 | |
|
232 | 231 | |
|
233 | 232 | ClassWithMeta = MetaClass('ClassWithMeta') |
|
234 | 233 | |
|
235 | 234 | |
|
236 | 235 | def test_metaclass_repr(): |
|
237 | 236 | output = pretty.pretty(ClassWithMeta) |
|
238 | 237 | nt.assert_equal(output, "[CUSTOM REPR FOR CLASS ClassWithMeta]") |
|
239 | 238 | |
|
240 | 239 | |
|
240 | def test_unicode_repr(): | |
|
241 | u = u"üniço∂é" | |
|
242 | ustr = unicode_to_str(u) | |
|
243 | ||
|
244 | class C(object): | |
|
245 | def __repr__(self): | |
|
246 | return ustr | |
|
247 | ||
|
248 | c = C() | |
|
249 | p = pretty.pretty(c) | |
|
250 | nt.assert_equal(p, u) | |
|
251 | p = pretty.pretty([c]) | |
|
252 | nt.assert_equal(p, u'[%s]' % u) | |
|
253 | ||
|
254 | ||
|
241 | 255 | def test_basic_class(): |
|
242 | 256 | def type_pprint_wrapper(obj, p, cycle): |
|
243 | 257 | if obj is MyObj: |
|
244 | 258 | type_pprint_wrapper.called = True |
|
245 | 259 | return pretty._type_pprint(obj, p, cycle) |
|
246 | 260 | type_pprint_wrapper.called = False |
|
247 | 261 | |
|
248 | 262 | stream = StringIO() |
|
249 | 263 | printer = pretty.RepresentationPrinter(stream) |
|
250 | 264 | printer.type_pprinters[type] = type_pprint_wrapper |
|
251 | 265 | printer.pretty(MyObj) |
|
252 | 266 | printer.flush() |
|
253 | 267 | output = stream.getvalue() |
|
254 | 268 | |
|
255 | 269 | nt.assert_equal(output, '%s.MyObj' % __name__) |
|
256 | 270 | nt.assert_true(type_pprint_wrapper.called) |
General Comments 0
You need to be logged in to leave comments.
Login now