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