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