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