Show More
@@ -1,542 +1,544 b'' | |||||
1 | """Tests for the Formatters.""" |
|
1 | """Tests for the Formatters.""" | |
2 |
|
2 | |||
3 | import warnings |
|
3 | import warnings | |
4 | from math import pi |
|
4 | from math import pi | |
5 |
|
5 | |||
6 | try: |
|
6 | try: | |
7 | import numpy |
|
7 | import numpy | |
8 | except: |
|
8 | except: | |
9 | numpy = None |
|
9 | numpy = None | |
10 | import nose.tools as nt |
|
10 | import pytest | |
11 |
|
11 | |||
12 | from IPython import get_ipython |
|
12 | from IPython import get_ipython | |
13 | from traitlets.config import Config |
|
13 | from traitlets.config import Config | |
14 | from IPython.core.formatters import ( |
|
14 | from IPython.core.formatters import ( | |
15 | PlainTextFormatter, HTMLFormatter, PDFFormatter, _mod_name_key, |
|
15 | PlainTextFormatter, HTMLFormatter, PDFFormatter, _mod_name_key, | |
16 | DisplayFormatter, JSONFormatter, |
|
16 | DisplayFormatter, JSONFormatter, | |
17 | ) |
|
17 | ) | |
18 | from IPython.utils.io import capture_output |
|
18 | from IPython.utils.io import capture_output | |
19 |
|
19 | |||
20 | class A(object): |
|
20 | class A(object): | |
21 | def __repr__(self): |
|
21 | def __repr__(self): | |
22 | return 'A()' |
|
22 | return 'A()' | |
23 |
|
23 | |||
24 | class B(A): |
|
24 | class B(A): | |
25 | def __repr__(self): |
|
25 | def __repr__(self): | |
26 | return 'B()' |
|
26 | return 'B()' | |
27 |
|
27 | |||
28 | class C: |
|
28 | class C: | |
29 | pass |
|
29 | pass | |
30 |
|
30 | |||
31 | class BadRepr(object): |
|
31 | class BadRepr(object): | |
32 | def __repr__(self): |
|
32 | def __repr__(self): | |
33 | raise ValueError("bad repr") |
|
33 | raise ValueError("bad repr") | |
34 |
|
34 | |||
35 | class BadPretty(object): |
|
35 | class BadPretty(object): | |
36 | _repr_pretty_ = None |
|
36 | _repr_pretty_ = None | |
37 |
|
37 | |||
38 | class GoodPretty(object): |
|
38 | class GoodPretty(object): | |
39 | def _repr_pretty_(self, pp, cycle): |
|
39 | def _repr_pretty_(self, pp, cycle): | |
40 | pp.text('foo') |
|
40 | pp.text('foo') | |
41 |
|
41 | |||
42 | def __repr__(self): |
|
42 | def __repr__(self): | |
43 | return 'GoodPretty()' |
|
43 | return 'GoodPretty()' | |
44 |
|
44 | |||
45 | def foo_printer(obj, pp, cycle): |
|
45 | def foo_printer(obj, pp, cycle): | |
46 | pp.text('foo') |
|
46 | pp.text('foo') | |
47 |
|
47 | |||
48 | def test_pretty(): |
|
48 | def test_pretty(): | |
49 | f = PlainTextFormatter() |
|
49 | f = PlainTextFormatter() | |
50 | f.for_type(A, foo_printer) |
|
50 | f.for_type(A, foo_printer) | |
51 | assert f(A()) == "foo" |
|
51 | assert f(A()) == "foo" | |
52 | assert f(B()) == "B()" |
|
52 | assert f(B()) == "B()" | |
53 | assert f(GoodPretty()) == "foo" |
|
53 | assert f(GoodPretty()) == "foo" | |
54 | # Just don't raise an exception for the following: |
|
54 | # Just don't raise an exception for the following: | |
55 | f(BadPretty()) |
|
55 | f(BadPretty()) | |
56 |
|
56 | |||
57 | f.pprint = False |
|
57 | f.pprint = False | |
58 | assert f(A()) == "A()" |
|
58 | assert f(A()) == "A()" | |
59 | assert f(B()) == "B()" |
|
59 | assert f(B()) == "B()" | |
60 | assert f(GoodPretty()) == "GoodPretty()" |
|
60 | assert f(GoodPretty()) == "GoodPretty()" | |
61 |
|
61 | |||
62 |
|
62 | |||
63 | def test_deferred(): |
|
63 | def test_deferred(): | |
64 | f = PlainTextFormatter() |
|
64 | f = PlainTextFormatter() | |
65 |
|
65 | |||
66 | def test_precision(): |
|
66 | def test_precision(): | |
67 | """test various values for float_precision.""" |
|
67 | """test various values for float_precision.""" | |
68 | f = PlainTextFormatter() |
|
68 | f = PlainTextFormatter() | |
69 | assert f(pi) == repr(pi) |
|
69 | assert f(pi) == repr(pi) | |
70 | f.float_precision = 0 |
|
70 | f.float_precision = 0 | |
71 | if numpy: |
|
71 | if numpy: | |
72 | po = numpy.get_printoptions() |
|
72 | po = numpy.get_printoptions() | |
73 | assert po["precision"] == 0 |
|
73 | assert po["precision"] == 0 | |
74 | assert f(pi) == "3" |
|
74 | assert f(pi) == "3" | |
75 | f.float_precision = 2 |
|
75 | f.float_precision = 2 | |
76 | if numpy: |
|
76 | if numpy: | |
77 | po = numpy.get_printoptions() |
|
77 | po = numpy.get_printoptions() | |
78 | assert po["precision"] == 2 |
|
78 | assert po["precision"] == 2 | |
79 | assert f(pi) == "3.14" |
|
79 | assert f(pi) == "3.14" | |
80 | f.float_precision = "%g" |
|
80 | f.float_precision = "%g" | |
81 | if numpy: |
|
81 | if numpy: | |
82 | po = numpy.get_printoptions() |
|
82 | po = numpy.get_printoptions() | |
83 | assert po["precision"] == 2 |
|
83 | assert po["precision"] == 2 | |
84 | assert f(pi) == "3.14159" |
|
84 | assert f(pi) == "3.14159" | |
85 | f.float_precision = "%e" |
|
85 | f.float_precision = "%e" | |
86 | assert f(pi) == "3.141593e+00" |
|
86 | assert f(pi) == "3.141593e+00" | |
87 | f.float_precision = "" |
|
87 | f.float_precision = "" | |
88 | if numpy: |
|
88 | if numpy: | |
89 | po = numpy.get_printoptions() |
|
89 | po = numpy.get_printoptions() | |
90 | assert po["precision"] == 8 |
|
90 | assert po["precision"] == 8 | |
91 | assert f(pi) == repr(pi) |
|
91 | assert f(pi) == repr(pi) | |
92 |
|
92 | |||
93 |
|
93 | |||
94 | def test_bad_precision(): |
|
94 | def test_bad_precision(): | |
95 | """test various invalid values for float_precision.""" |
|
95 | """test various invalid values for float_precision.""" | |
96 | f = PlainTextFormatter() |
|
96 | f = PlainTextFormatter() | |
97 | def set_fp(p): |
|
97 | def set_fp(p): | |
98 | f.float_precision=p |
|
98 | f.float_precision = p | |
99 | nt.assert_raises(ValueError, set_fp, '%') |
|
99 | ||
100 |
|
|
100 | pytest.raises(ValueError, set_fp, "%") | |
101 |
|
|
101 | pytest.raises(ValueError, set_fp, "%.3f%i") | |
102 |
|
|
102 | pytest.raises(ValueError, set_fp, "foo") | |
|
103 | pytest.raises(ValueError, set_fp, -1) | |||
103 |
|
104 | |||
104 | def test_for_type(): |
|
105 | def test_for_type(): | |
105 | f = PlainTextFormatter() |
|
106 | f = PlainTextFormatter() | |
106 |
|
107 | |||
107 | # initial return, None |
|
108 | # initial return, None | |
108 |
|
|
109 | assert f.for_type(C, foo_printer) is None | |
109 | # no func queries |
|
110 | # no func queries | |
110 |
|
|
111 | assert f.for_type(C) is foo_printer | |
111 | # shouldn't change anything |
|
112 | # shouldn't change anything | |
112 |
|
|
113 | assert f.for_type(C) is foo_printer | |
113 | # None should do the same |
|
114 | # None should do the same | |
114 |
|
|
115 | assert f.for_type(C, None) is foo_printer | |
115 |
|
|
116 | assert f.for_type(C, None) is foo_printer | |
116 |
|
117 | |||
117 | def test_for_type_string(): |
|
118 | def test_for_type_string(): | |
118 | f = PlainTextFormatter() |
|
119 | f = PlainTextFormatter() | |
119 |
|
120 | |||
120 | type_str = '%s.%s' % (C.__module__, 'C') |
|
121 | type_str = '%s.%s' % (C.__module__, 'C') | |
121 |
|
122 | |||
122 | # initial return, None |
|
123 | # initial return, None | |
123 |
|
|
124 | assert f.for_type(type_str, foo_printer) is None | |
124 | # no func queries |
|
125 | # no func queries | |
125 |
|
|
126 | assert f.for_type(type_str) is foo_printer | |
126 |
|
|
127 | assert _mod_name_key(C) in f.deferred_printers | |
127 |
|
|
128 | assert f.for_type(C) is foo_printer | |
128 |
|
|
129 | assert _mod_name_key(C) not in f.deferred_printers | |
129 |
|
|
130 | assert C in f.type_printers | |
130 |
|
131 | |||
131 | def test_for_type_by_name(): |
|
132 | def test_for_type_by_name(): | |
132 | f = PlainTextFormatter() |
|
133 | f = PlainTextFormatter() | |
133 |
|
134 | |||
134 | mod = C.__module__ |
|
135 | mod = C.__module__ | |
135 |
|
136 | |||
136 | # initial return, None |
|
137 | # initial return, None | |
137 |
|
|
138 | assert f.for_type_by_name(mod, "C", foo_printer) is None | |
138 | # no func queries |
|
139 | # no func queries | |
139 |
|
|
140 | assert f.for_type_by_name(mod, "C") is foo_printer | |
140 | # shouldn't change anything |
|
141 | # shouldn't change anything | |
141 |
|
|
142 | assert f.for_type_by_name(mod, "C") is foo_printer | |
142 | # None should do the same |
|
143 | # None should do the same | |
143 |
|
|
144 | assert f.for_type_by_name(mod, "C", None) is foo_printer | |
144 |
|
|
145 | assert f.for_type_by_name(mod, "C", None) is foo_printer | |
|
146 | ||||
145 |
|
147 | |||
146 | def test_lookup(): |
|
148 | def test_lookup(): | |
147 | f = PlainTextFormatter() |
|
149 | f = PlainTextFormatter() | |
148 |
|
150 | |||
149 | f.for_type(C, foo_printer) |
|
151 | f.for_type(C, foo_printer) | |
150 |
|
|
152 | assert f.lookup(C()) is foo_printer | |
151 |
with |
|
153 | with pytest.raises(KeyError): | |
152 | f.lookup(A()) |
|
154 | f.lookup(A()) | |
153 |
|
155 | |||
154 | def test_lookup_string(): |
|
156 | def test_lookup_string(): | |
155 | f = PlainTextFormatter() |
|
157 | f = PlainTextFormatter() | |
156 | type_str = '%s.%s' % (C.__module__, 'C') |
|
158 | type_str = '%s.%s' % (C.__module__, 'C') | |
157 |
|
159 | |||
158 | f.for_type(type_str, foo_printer) |
|
160 | f.for_type(type_str, foo_printer) | |
159 |
|
|
161 | assert f.lookup(C()) is foo_printer | |
160 | # should move from deferred to imported dict |
|
162 | # should move from deferred to imported dict | |
161 |
|
|
163 | assert _mod_name_key(C) not in f.deferred_printers | |
162 |
|
|
164 | assert C in f.type_printers | |
163 |
|
165 | |||
164 | def test_lookup_by_type(): |
|
166 | def test_lookup_by_type(): | |
165 | f = PlainTextFormatter() |
|
167 | f = PlainTextFormatter() | |
166 | f.for_type(C, foo_printer) |
|
168 | f.for_type(C, foo_printer) | |
167 |
|
|
169 | assert f.lookup_by_type(C) is foo_printer | |
168 |
with |
|
170 | with pytest.raises(KeyError): | |
169 | f.lookup_by_type(A) |
|
171 | f.lookup_by_type(A) | |
170 |
|
172 | |||
171 | def test_lookup_by_type_string(): |
|
173 | def test_lookup_by_type_string(): | |
172 | f = PlainTextFormatter() |
|
174 | f = PlainTextFormatter() | |
173 | type_str = '%s.%s' % (C.__module__, 'C') |
|
175 | type_str = '%s.%s' % (C.__module__, 'C') | |
174 | f.for_type(type_str, foo_printer) |
|
176 | f.for_type(type_str, foo_printer) | |
175 |
|
177 | |||
176 | # verify insertion |
|
178 | # verify insertion | |
177 |
|
|
179 | assert _mod_name_key(C) in f.deferred_printers | |
178 |
|
|
180 | assert C not in f.type_printers | |
179 |
|
181 | |||
180 |
|
|
182 | assert f.lookup_by_type(type_str) is foo_printer | |
181 | # lookup by string doesn't cause import |
|
183 | # lookup by string doesn't cause import | |
182 |
|
|
184 | assert _mod_name_key(C) in f.deferred_printers | |
183 |
|
|
185 | assert C not in f.type_printers | |
184 |
|
186 | |||
185 |
|
|
187 | assert f.lookup_by_type(C) is foo_printer | |
186 | # should move from deferred to imported dict |
|
188 | # should move from deferred to imported dict | |
187 |
|
|
189 | assert _mod_name_key(C) not in f.deferred_printers | |
188 |
|
|
190 | assert C in f.type_printers | |
189 |
|
191 | |||
190 | def test_in_formatter(): |
|
192 | def test_in_formatter(): | |
191 | f = PlainTextFormatter() |
|
193 | f = PlainTextFormatter() | |
192 | f.for_type(C, foo_printer) |
|
194 | f.for_type(C, foo_printer) | |
193 | type_str = '%s.%s' % (C.__module__, 'C') |
|
195 | type_str = '%s.%s' % (C.__module__, 'C') | |
194 |
|
|
196 | assert C in f | |
195 |
|
|
197 | assert type_str in f | |
196 |
|
198 | |||
197 | def test_string_in_formatter(): |
|
199 | def test_string_in_formatter(): | |
198 | f = PlainTextFormatter() |
|
200 | f = PlainTextFormatter() | |
199 | type_str = '%s.%s' % (C.__module__, 'C') |
|
201 | type_str = '%s.%s' % (C.__module__, 'C') | |
200 | f.for_type(type_str, foo_printer) |
|
202 | f.for_type(type_str, foo_printer) | |
201 |
|
|
203 | assert type_str in f | |
202 |
|
|
204 | assert C in f | |
203 |
|
205 | |||
204 | def test_pop(): |
|
206 | def test_pop(): | |
205 | f = PlainTextFormatter() |
|
207 | f = PlainTextFormatter() | |
206 | f.for_type(C, foo_printer) |
|
208 | f.for_type(C, foo_printer) | |
207 |
|
|
209 | assert f.lookup_by_type(C) is foo_printer | |
208 |
|
|
210 | assert f.pop(C, None) is foo_printer | |
209 | f.for_type(C, foo_printer) |
|
211 | f.for_type(C, foo_printer) | |
210 |
|
|
212 | assert f.pop(C) is foo_printer | |
211 |
with |
|
213 | with pytest.raises(KeyError): | |
212 | f.lookup_by_type(C) |
|
214 | f.lookup_by_type(C) | |
213 |
with |
|
215 | with pytest.raises(KeyError): | |
214 | f.pop(C) |
|
216 | f.pop(C) | |
215 |
with |
|
217 | with pytest.raises(KeyError): | |
216 | f.pop(A) |
|
218 | f.pop(A) | |
217 |
|
|
219 | assert f.pop(A, None) is None | |
218 |
|
220 | |||
219 | def test_pop_string(): |
|
221 | def test_pop_string(): | |
220 | f = PlainTextFormatter() |
|
222 | f = PlainTextFormatter() | |
221 | type_str = '%s.%s' % (C.__module__, 'C') |
|
223 | type_str = '%s.%s' % (C.__module__, 'C') | |
222 |
|
224 | |||
223 |
with |
|
225 | with pytest.raises(KeyError): | |
224 | f.pop(type_str) |
|
226 | f.pop(type_str) | |
225 |
|
227 | |||
226 | f.for_type(type_str, foo_printer) |
|
228 | f.for_type(type_str, foo_printer) | |
227 | f.pop(type_str) |
|
229 | f.pop(type_str) | |
228 |
with |
|
230 | with pytest.raises(KeyError): | |
229 | f.lookup_by_type(C) |
|
231 | f.lookup_by_type(C) | |
230 |
with |
|
232 | with pytest.raises(KeyError): | |
231 | f.pop(type_str) |
|
233 | f.pop(type_str) | |
232 |
|
234 | |||
233 | f.for_type(C, foo_printer) |
|
235 | f.for_type(C, foo_printer) | |
234 |
|
|
236 | assert f.pop(type_str, None) is foo_printer | |
235 |
with |
|
237 | with pytest.raises(KeyError): | |
236 | f.lookup_by_type(C) |
|
238 | f.lookup_by_type(C) | |
237 |
with |
|
239 | with pytest.raises(KeyError): | |
238 | f.pop(type_str) |
|
240 | f.pop(type_str) | |
239 |
|
|
241 | assert f.pop(type_str, None) is None | |
240 |
|
242 | |||
241 |
|
243 | |||
242 | def test_error_method(): |
|
244 | def test_error_method(): | |
243 | f = HTMLFormatter() |
|
245 | f = HTMLFormatter() | |
244 | class BadHTML(object): |
|
246 | class BadHTML(object): | |
245 | def _repr_html_(self): |
|
247 | def _repr_html_(self): | |
246 | raise ValueError("Bad HTML") |
|
248 | raise ValueError("Bad HTML") | |
247 | bad = BadHTML() |
|
249 | bad = BadHTML() | |
248 | with capture_output() as captured: |
|
250 | with capture_output() as captured: | |
249 | result = f(bad) |
|
251 | result = f(bad) | |
250 |
|
|
252 | assert result is None | |
251 |
|
|
253 | assert "Traceback" in captured.stdout | |
252 |
|
|
254 | assert "Bad HTML" in captured.stdout | |
253 |
|
|
255 | assert "_repr_html_" in captured.stdout | |
254 |
|
256 | |||
255 | def test_nowarn_notimplemented(): |
|
257 | def test_nowarn_notimplemented(): | |
256 | f = HTMLFormatter() |
|
258 | f = HTMLFormatter() | |
257 | class HTMLNotImplemented(object): |
|
259 | class HTMLNotImplemented(object): | |
258 | def _repr_html_(self): |
|
260 | def _repr_html_(self): | |
259 | raise NotImplementedError |
|
261 | raise NotImplementedError | |
260 | h = HTMLNotImplemented() |
|
262 | h = HTMLNotImplemented() | |
261 | with capture_output() as captured: |
|
263 | with capture_output() as captured: | |
262 | result = f(h) |
|
264 | result = f(h) | |
263 |
|
|
265 | assert result is None | |
264 | assert "" == captured.stderr |
|
266 | assert "" == captured.stderr | |
265 | assert "" == captured.stdout |
|
267 | assert "" == captured.stdout | |
266 |
|
268 | |||
267 |
|
269 | |||
268 | def test_warn_error_for_type(): |
|
270 | def test_warn_error_for_type(): | |
269 | f = HTMLFormatter() |
|
271 | f = HTMLFormatter() | |
270 | f.for_type(int, lambda i: name_error) |
|
272 | f.for_type(int, lambda i: name_error) | |
271 | with capture_output() as captured: |
|
273 | with capture_output() as captured: | |
272 | result = f(5) |
|
274 | result = f(5) | |
273 |
|
|
275 | assert result is None | |
274 |
|
|
276 | assert "Traceback" in captured.stdout | |
275 |
|
|
277 | assert "NameError" in captured.stdout | |
276 |
|
|
278 | assert "name_error" in captured.stdout | |
277 |
|
279 | |||
278 | def test_error_pretty_method(): |
|
280 | def test_error_pretty_method(): | |
279 | f = PlainTextFormatter() |
|
281 | f = PlainTextFormatter() | |
280 | class BadPretty(object): |
|
282 | class BadPretty(object): | |
281 | def _repr_pretty_(self): |
|
283 | def _repr_pretty_(self): | |
282 | return "hello" |
|
284 | return "hello" | |
283 | bad = BadPretty() |
|
285 | bad = BadPretty() | |
284 | with capture_output() as captured: |
|
286 | with capture_output() as captured: | |
285 | result = f(bad) |
|
287 | result = f(bad) | |
286 |
|
|
288 | assert result is None | |
287 |
|
|
289 | assert "Traceback" in captured.stdout | |
288 |
|
|
290 | assert "_repr_pretty_" in captured.stdout | |
289 |
|
|
291 | assert "given" in captured.stdout | |
290 |
|
|
292 | assert "argument" in captured.stdout | |
291 |
|
293 | |||
292 |
|
294 | |||
293 | def test_bad_repr_traceback(): |
|
295 | def test_bad_repr_traceback(): | |
294 | f = PlainTextFormatter() |
|
296 | f = PlainTextFormatter() | |
295 | bad = BadRepr() |
|
297 | bad = BadRepr() | |
296 | with capture_output() as captured: |
|
298 | with capture_output() as captured: | |
297 | result = f(bad) |
|
299 | result = f(bad) | |
298 | # catches error, returns None |
|
300 | # catches error, returns None | |
299 |
|
|
301 | assert result is None | |
300 |
|
|
302 | assert "Traceback" in captured.stdout | |
301 |
|
|
303 | assert "__repr__" in captured.stdout | |
302 |
|
|
304 | assert "ValueError" in captured.stdout | |
303 |
|
305 | |||
304 |
|
306 | |||
305 | class MakePDF(object): |
|
307 | class MakePDF(object): | |
306 | def _repr_pdf_(self): |
|
308 | def _repr_pdf_(self): | |
307 | return 'PDF' |
|
309 | return 'PDF' | |
308 |
|
310 | |||
309 | def test_pdf_formatter(): |
|
311 | def test_pdf_formatter(): | |
310 | pdf = MakePDF() |
|
312 | pdf = MakePDF() | |
311 | f = PDFFormatter() |
|
313 | f = PDFFormatter() | |
312 | assert f(pdf) == "PDF" |
|
314 | assert f(pdf) == "PDF" | |
313 |
|
315 | |||
314 |
|
316 | |||
315 | def test_print_method_bound(): |
|
317 | def test_print_method_bound(): | |
316 | f = HTMLFormatter() |
|
318 | f = HTMLFormatter() | |
317 | class MyHTML(object): |
|
319 | class MyHTML(object): | |
318 | def _repr_html_(self): |
|
320 | def _repr_html_(self): | |
319 | return "hello" |
|
321 | return "hello" | |
320 | with capture_output() as captured: |
|
322 | with capture_output() as captured: | |
321 | result = f(MyHTML) |
|
323 | result = f(MyHTML) | |
322 |
|
|
324 | assert result is None | |
323 |
|
|
325 | assert "FormatterWarning" not in captured.stderr | |
324 |
|
326 | |||
325 | with capture_output() as captured: |
|
327 | with capture_output() as captured: | |
326 | result = f(MyHTML()) |
|
328 | result = f(MyHTML()) | |
327 | assert result == "hello" |
|
329 | assert result == "hello" | |
328 | assert captured.stderr == "" |
|
330 | assert captured.stderr == "" | |
329 |
|
331 | |||
330 |
|
332 | |||
331 | def test_print_method_weird(): |
|
333 | def test_print_method_weird(): | |
332 |
|
334 | |||
333 | class TextMagicHat(object): |
|
335 | class TextMagicHat(object): | |
334 | def __getattr__(self, key): |
|
336 | def __getattr__(self, key): | |
335 | return key |
|
337 | return key | |
336 |
|
338 | |||
337 | f = HTMLFormatter() |
|
339 | f = HTMLFormatter() | |
338 |
|
340 | |||
339 | text_hat = TextMagicHat() |
|
341 | text_hat = TextMagicHat() | |
340 | assert text_hat._repr_html_ == "_repr_html_" |
|
342 | assert text_hat._repr_html_ == "_repr_html_" | |
341 | with capture_output() as captured: |
|
343 | with capture_output() as captured: | |
342 | result = f(text_hat) |
|
344 | result = f(text_hat) | |
343 |
|
345 | |||
344 |
|
|
346 | assert result is None | |
345 |
|
|
347 | assert "FormatterWarning" not in captured.stderr | |
346 |
|
348 | |||
347 | class CallableMagicHat(object): |
|
349 | class CallableMagicHat(object): | |
348 | def __getattr__(self, key): |
|
350 | def __getattr__(self, key): | |
349 | return lambda : key |
|
351 | return lambda : key | |
350 |
|
352 | |||
351 | call_hat = CallableMagicHat() |
|
353 | call_hat = CallableMagicHat() | |
352 | with capture_output() as captured: |
|
354 | with capture_output() as captured: | |
353 | result = f(call_hat) |
|
355 | result = f(call_hat) | |
354 |
|
356 | |||
355 | assert result is None |
|
357 | assert result is None | |
356 |
|
358 | |||
357 | class BadReprArgs(object): |
|
359 | class BadReprArgs(object): | |
358 | def _repr_html_(self, extra, args): |
|
360 | def _repr_html_(self, extra, args): | |
359 | return "html" |
|
361 | return "html" | |
360 |
|
362 | |||
361 | bad = BadReprArgs() |
|
363 | bad = BadReprArgs() | |
362 | with capture_output() as captured: |
|
364 | with capture_output() as captured: | |
363 | result = f(bad) |
|
365 | result = f(bad) | |
364 |
|
366 | |||
365 |
|
|
367 | assert result is None | |
366 |
|
|
368 | assert "FormatterWarning" not in captured.stderr | |
367 |
|
369 | |||
368 |
|
370 | |||
369 | def test_format_config(): |
|
371 | def test_format_config(): | |
370 | """config objects don't pretend to support fancy reprs with lazy attrs""" |
|
372 | """config objects don't pretend to support fancy reprs with lazy attrs""" | |
371 | f = HTMLFormatter() |
|
373 | f = HTMLFormatter() | |
372 | cfg = Config() |
|
374 | cfg = Config() | |
373 | with capture_output() as captured: |
|
375 | with capture_output() as captured: | |
374 | result = f(cfg) |
|
376 | result = f(cfg) | |
375 |
|
|
377 | assert result is None | |
376 | assert captured.stderr == "" |
|
378 | assert captured.stderr == "" | |
377 |
|
379 | |||
378 | with capture_output() as captured: |
|
380 | with capture_output() as captured: | |
379 | result = f(Config) |
|
381 | result = f(Config) | |
380 |
|
|
382 | assert result is None | |
381 | assert captured.stderr == "" |
|
383 | assert captured.stderr == "" | |
382 |
|
384 | |||
383 |
|
385 | |||
384 | def test_pretty_max_seq_length(): |
|
386 | def test_pretty_max_seq_length(): | |
385 | f = PlainTextFormatter(max_seq_length=1) |
|
387 | f = PlainTextFormatter(max_seq_length=1) | |
386 | lis = list(range(3)) |
|
388 | lis = list(range(3)) | |
387 | text = f(lis) |
|
389 | text = f(lis) | |
388 | assert text == "[0, ...]" |
|
390 | assert text == "[0, ...]" | |
389 | f.max_seq_length = 0 |
|
391 | f.max_seq_length = 0 | |
390 | text = f(lis) |
|
392 | text = f(lis) | |
391 | assert text == "[0, 1, 2]" |
|
393 | assert text == "[0, 1, 2]" | |
392 | text = f(list(range(1024))) |
|
394 | text = f(list(range(1024))) | |
393 | lines = text.splitlines() |
|
395 | lines = text.splitlines() | |
394 | assert len(lines) == 1024 |
|
396 | assert len(lines) == 1024 | |
395 |
|
397 | |||
396 |
|
398 | |||
397 | def test_ipython_display_formatter(): |
|
399 | def test_ipython_display_formatter(): | |
398 | """Objects with _ipython_display_ defined bypass other formatters""" |
|
400 | """Objects with _ipython_display_ defined bypass other formatters""" | |
399 | f = get_ipython().display_formatter |
|
401 | f = get_ipython().display_formatter | |
400 | catcher = [] |
|
402 | catcher = [] | |
401 | class SelfDisplaying(object): |
|
403 | class SelfDisplaying(object): | |
402 | def _ipython_display_(self): |
|
404 | def _ipython_display_(self): | |
403 | catcher.append(self) |
|
405 | catcher.append(self) | |
404 |
|
406 | |||
405 | class NotSelfDisplaying(object): |
|
407 | class NotSelfDisplaying(object): | |
406 | def __repr__(self): |
|
408 | def __repr__(self): | |
407 | return "NotSelfDisplaying" |
|
409 | return "NotSelfDisplaying" | |
408 |
|
410 | |||
409 | def _ipython_display_(self): |
|
411 | def _ipython_display_(self): | |
410 | raise NotImplementedError |
|
412 | raise NotImplementedError | |
411 |
|
413 | |||
412 | save_enabled = f.ipython_display_formatter.enabled |
|
414 | save_enabled = f.ipython_display_formatter.enabled | |
413 | f.ipython_display_formatter.enabled = True |
|
415 | f.ipython_display_formatter.enabled = True | |
414 |
|
416 | |||
415 | yes = SelfDisplaying() |
|
417 | yes = SelfDisplaying() | |
416 | no = NotSelfDisplaying() |
|
418 | no = NotSelfDisplaying() | |
417 |
|
419 | |||
418 | d, md = f.format(no) |
|
420 | d, md = f.format(no) | |
419 | assert d == {"text/plain": repr(no)} |
|
421 | assert d == {"text/plain": repr(no)} | |
420 | assert md == {} |
|
422 | assert md == {} | |
421 | assert catcher == [] |
|
423 | assert catcher == [] | |
422 |
|
424 | |||
423 | d, md = f.format(yes) |
|
425 | d, md = f.format(yes) | |
424 | assert d == {} |
|
426 | assert d == {} | |
425 | assert md == {} |
|
427 | assert md == {} | |
426 | assert catcher == [yes] |
|
428 | assert catcher == [yes] | |
427 |
|
429 | |||
428 | f.ipython_display_formatter.enabled = save_enabled |
|
430 | f.ipython_display_formatter.enabled = save_enabled | |
429 |
|
431 | |||
430 |
|
432 | |||
431 | def test_json_as_string_deprecated(): |
|
433 | def test_json_as_string_deprecated(): | |
432 | class JSONString(object): |
|
434 | class JSONString(object): | |
433 | def _repr_json_(self): |
|
435 | def _repr_json_(self): | |
434 | return '{}' |
|
436 | return '{}' | |
435 |
|
437 | |||
436 | f = JSONFormatter() |
|
438 | f = JSONFormatter() | |
437 | with warnings.catch_warnings(record=True) as w: |
|
439 | with warnings.catch_warnings(record=True) as w: | |
438 | d = f(JSONString()) |
|
440 | d = f(JSONString()) | |
439 | assert d == {} |
|
441 | assert d == {} | |
440 | assert len(w) == 1 |
|
442 | assert len(w) == 1 | |
441 |
|
443 | |||
442 |
|
444 | |||
443 | def test_repr_mime(): |
|
445 | def test_repr_mime(): | |
444 | class HasReprMime(object): |
|
446 | class HasReprMime(object): | |
445 | def _repr_mimebundle_(self, include=None, exclude=None): |
|
447 | def _repr_mimebundle_(self, include=None, exclude=None): | |
446 | return { |
|
448 | return { | |
447 | 'application/json+test.v2': { |
|
449 | 'application/json+test.v2': { | |
448 | 'x': 'y' |
|
450 | 'x': 'y' | |
449 | }, |
|
451 | }, | |
450 | 'plain/text' : '<HasReprMime>', |
|
452 | 'plain/text' : '<HasReprMime>', | |
451 | 'image/png' : 'i-overwrite' |
|
453 | 'image/png' : 'i-overwrite' | |
452 | } |
|
454 | } | |
453 |
|
455 | |||
454 | def _repr_png_(self): |
|
456 | def _repr_png_(self): | |
455 | return 'should-be-overwritten' |
|
457 | return 'should-be-overwritten' | |
456 | def _repr_html_(self): |
|
458 | def _repr_html_(self): | |
457 | return '<b>hi!</b>' |
|
459 | return '<b>hi!</b>' | |
458 |
|
460 | |||
459 | f = get_ipython().display_formatter |
|
461 | f = get_ipython().display_formatter | |
460 | html_f = f.formatters['text/html'] |
|
462 | html_f = f.formatters['text/html'] | |
461 | save_enabled = html_f.enabled |
|
463 | save_enabled = html_f.enabled | |
462 | html_f.enabled = True |
|
464 | html_f.enabled = True | |
463 | obj = HasReprMime() |
|
465 | obj = HasReprMime() | |
464 | d, md = f.format(obj) |
|
466 | d, md = f.format(obj) | |
465 | html_f.enabled = save_enabled |
|
467 | html_f.enabled = save_enabled | |
466 |
|
468 | |||
467 | assert sorted(d) == [ |
|
469 | assert sorted(d) == [ | |
468 | "application/json+test.v2", |
|
470 | "application/json+test.v2", | |
469 | "image/png", |
|
471 | "image/png", | |
470 | "plain/text", |
|
472 | "plain/text", | |
471 | "text/html", |
|
473 | "text/html", | |
472 | "text/plain", |
|
474 | "text/plain", | |
473 | ] |
|
475 | ] | |
474 | assert md == {} |
|
476 | assert md == {} | |
475 |
|
477 | |||
476 | d, md = f.format(obj, include={"image/png"}) |
|
478 | d, md = f.format(obj, include={"image/png"}) | |
477 | assert list(d.keys()) == [ |
|
479 | assert list(d.keys()) == [ | |
478 | "image/png" |
|
480 | "image/png" | |
479 | ], "Include should filter out even things from repr_mimebundle" |
|
481 | ], "Include should filter out even things from repr_mimebundle" | |
480 |
|
482 | |||
481 | assert d["image/png"] == "i-overwrite", "_repr_mimebundle_ take precedence" |
|
483 | assert d["image/png"] == "i-overwrite", "_repr_mimebundle_ take precedence" | |
482 |
|
484 | |||
483 |
|
485 | |||
484 | def test_pass_correct_include_exclude(): |
|
486 | def test_pass_correct_include_exclude(): | |
485 | class Tester(object): |
|
487 | class Tester(object): | |
486 |
|
488 | |||
487 | def __init__(self, include=None, exclude=None): |
|
489 | def __init__(self, include=None, exclude=None): | |
488 | self.include = include |
|
490 | self.include = include | |
489 | self.exclude = exclude |
|
491 | self.exclude = exclude | |
490 |
|
492 | |||
491 | def _repr_mimebundle_(self, include, exclude, **kwargs): |
|
493 | def _repr_mimebundle_(self, include, exclude, **kwargs): | |
492 | if include and (include != self.include): |
|
494 | if include and (include != self.include): | |
493 | raise ValueError('include got modified: display() may be broken.') |
|
495 | raise ValueError('include got modified: display() may be broken.') | |
494 | if exclude and (exclude != self.exclude): |
|
496 | if exclude and (exclude != self.exclude): | |
495 | raise ValueError('exclude got modified: display() may be broken.') |
|
497 | raise ValueError('exclude got modified: display() may be broken.') | |
496 |
|
498 | |||
497 | return None |
|
499 | return None | |
498 |
|
500 | |||
499 | include = {'a', 'b', 'c'} |
|
501 | include = {'a', 'b', 'c'} | |
500 | exclude = {'c', 'e' , 'f'} |
|
502 | exclude = {'c', 'e' , 'f'} | |
501 |
|
503 | |||
502 | f = get_ipython().display_formatter |
|
504 | f = get_ipython().display_formatter | |
503 | f.format(Tester(include=include, exclude=exclude), include=include, exclude=exclude) |
|
505 | f.format(Tester(include=include, exclude=exclude), include=include, exclude=exclude) | |
504 | f.format(Tester(exclude=exclude), exclude=exclude) |
|
506 | f.format(Tester(exclude=exclude), exclude=exclude) | |
505 | f.format(Tester(include=include), include=include) |
|
507 | f.format(Tester(include=include), include=include) | |
506 |
|
508 | |||
507 |
|
509 | |||
508 | def test_repr_mime_meta(): |
|
510 | def test_repr_mime_meta(): | |
509 | class HasReprMimeMeta(object): |
|
511 | class HasReprMimeMeta(object): | |
510 | def _repr_mimebundle_(self, include=None, exclude=None): |
|
512 | def _repr_mimebundle_(self, include=None, exclude=None): | |
511 | data = { |
|
513 | data = { | |
512 | 'image/png': 'base64-image-data', |
|
514 | 'image/png': 'base64-image-data', | |
513 | } |
|
515 | } | |
514 | metadata = { |
|
516 | metadata = { | |
515 | 'image/png': { |
|
517 | 'image/png': { | |
516 | 'width': 5, |
|
518 | 'width': 5, | |
517 | 'height': 10, |
|
519 | 'height': 10, | |
518 | } |
|
520 | } | |
519 | } |
|
521 | } | |
520 | return (data, metadata) |
|
522 | return (data, metadata) | |
521 |
|
523 | |||
522 | f = get_ipython().display_formatter |
|
524 | f = get_ipython().display_formatter | |
523 | obj = HasReprMimeMeta() |
|
525 | obj = HasReprMimeMeta() | |
524 | d, md = f.format(obj) |
|
526 | d, md = f.format(obj) | |
525 | assert sorted(d) == ["image/png", "text/plain"] |
|
527 | assert sorted(d) == ["image/png", "text/plain"] | |
526 | assert md == { |
|
528 | assert md == { | |
527 | "image/png": { |
|
529 | "image/png": { | |
528 | "width": 5, |
|
530 | "width": 5, | |
529 | "height": 10, |
|
531 | "height": 10, | |
530 | } |
|
532 | } | |
531 | } |
|
533 | } | |
532 |
|
534 | |||
533 |
|
535 | |||
534 | def test_repr_mime_failure(): |
|
536 | def test_repr_mime_failure(): | |
535 | class BadReprMime(object): |
|
537 | class BadReprMime(object): | |
536 | def _repr_mimebundle_(self, include=None, exclude=None): |
|
538 | def _repr_mimebundle_(self, include=None, exclude=None): | |
537 | raise RuntimeError |
|
539 | raise RuntimeError | |
538 |
|
540 | |||
539 | f = get_ipython().display_formatter |
|
541 | f = get_ipython().display_formatter | |
540 | obj = BadReprMime() |
|
542 | obj = BadReprMime() | |
541 | d, md = f.format(obj) |
|
543 | d, md = f.format(obj) | |
542 | assert "text/plain" in d |
|
544 | assert "text/plain" in d |
General Comments 0
You need to be logged in to leave comments.
Login now