Show More
@@ -1,389 +1,420 | |||
|
1 | 1 | """Tests for the Formatters.""" |
|
2 | 2 | |
|
3 | 3 | from math import pi |
|
4 | 4 | |
|
5 | 5 | try: |
|
6 | 6 | import numpy |
|
7 | 7 | except: |
|
8 | 8 | numpy = None |
|
9 | 9 | import nose.tools as nt |
|
10 | 10 | |
|
11 | 11 | from IPython.config import Config |
|
12 | 12 | from IPython.core.formatters import ( |
|
13 | PlainTextFormatter, HTMLFormatter, PDFFormatter, _mod_name_key | |
|
13 | PlainTextFormatter, HTMLFormatter, PDFFormatter, _mod_name_key, | |
|
14 | DisplayFormatter, | |
|
14 | 15 | ) |
|
15 | 16 | from IPython.utils.io import capture_output |
|
16 | 17 | |
|
17 | 18 | class A(object): |
|
18 | 19 | def __repr__(self): |
|
19 | 20 | return 'A()' |
|
20 | 21 | |
|
21 | 22 | class B(A): |
|
22 | 23 | def __repr__(self): |
|
23 | 24 | return 'B()' |
|
24 | 25 | |
|
25 | 26 | class C: |
|
26 | 27 | pass |
|
27 | 28 | |
|
28 | 29 | class BadRepr(object): |
|
29 | 30 | def __repr__(self): |
|
30 | 31 | raise ValueError("bad repr") |
|
31 | 32 | |
|
32 | 33 | class BadPretty(object): |
|
33 | 34 | _repr_pretty_ = None |
|
34 | 35 | |
|
35 | 36 | class GoodPretty(object): |
|
36 | 37 | def _repr_pretty_(self, pp, cycle): |
|
37 | 38 | pp.text('foo') |
|
38 | 39 | |
|
39 | 40 | def __repr__(self): |
|
40 | 41 | return 'GoodPretty()' |
|
41 | 42 | |
|
42 | 43 | def foo_printer(obj, pp, cycle): |
|
43 | 44 | pp.text('foo') |
|
44 | 45 | |
|
45 | 46 | def test_pretty(): |
|
46 | 47 | f = PlainTextFormatter() |
|
47 | 48 | f.for_type(A, foo_printer) |
|
48 | 49 | nt.assert_equal(f(A()), 'foo') |
|
49 | 50 | nt.assert_equal(f(B()), 'foo') |
|
50 | 51 | nt.assert_equal(f(GoodPretty()), 'foo') |
|
51 | 52 | # Just don't raise an exception for the following: |
|
52 | 53 | f(BadPretty()) |
|
53 | 54 | |
|
54 | 55 | f.pprint = False |
|
55 | 56 | nt.assert_equal(f(A()), 'A()') |
|
56 | 57 | nt.assert_equal(f(B()), 'B()') |
|
57 | 58 | nt.assert_equal(f(GoodPretty()), 'GoodPretty()') |
|
58 | 59 | |
|
59 | 60 | |
|
60 | 61 | def test_deferred(): |
|
61 | 62 | f = PlainTextFormatter() |
|
62 | 63 | |
|
63 | 64 | def test_precision(): |
|
64 | 65 | """test various values for float_precision.""" |
|
65 | 66 | f = PlainTextFormatter() |
|
66 | 67 | nt.assert_equal(f(pi), repr(pi)) |
|
67 | 68 | f.float_precision = 0 |
|
68 | 69 | if numpy: |
|
69 | 70 | po = numpy.get_printoptions() |
|
70 | 71 | nt.assert_equal(po['precision'], 0) |
|
71 | 72 | nt.assert_equal(f(pi), '3') |
|
72 | 73 | f.float_precision = 2 |
|
73 | 74 | if numpy: |
|
74 | 75 | po = numpy.get_printoptions() |
|
75 | 76 | nt.assert_equal(po['precision'], 2) |
|
76 | 77 | nt.assert_equal(f(pi), '3.14') |
|
77 | 78 | f.float_precision = '%g' |
|
78 | 79 | if numpy: |
|
79 | 80 | po = numpy.get_printoptions() |
|
80 | 81 | nt.assert_equal(po['precision'], 2) |
|
81 | 82 | nt.assert_equal(f(pi), '3.14159') |
|
82 | 83 | f.float_precision = '%e' |
|
83 | 84 | nt.assert_equal(f(pi), '3.141593e+00') |
|
84 | 85 | f.float_precision = '' |
|
85 | 86 | if numpy: |
|
86 | 87 | po = numpy.get_printoptions() |
|
87 | 88 | nt.assert_equal(po['precision'], 8) |
|
88 | 89 | nt.assert_equal(f(pi), repr(pi)) |
|
89 | 90 | |
|
90 | 91 | def test_bad_precision(): |
|
91 | 92 | """test various invalid values for float_precision.""" |
|
92 | 93 | f = PlainTextFormatter() |
|
93 | 94 | def set_fp(p): |
|
94 | 95 | f.float_precision=p |
|
95 | 96 | nt.assert_raises(ValueError, set_fp, '%') |
|
96 | 97 | nt.assert_raises(ValueError, set_fp, '%.3f%i') |
|
97 | 98 | nt.assert_raises(ValueError, set_fp, 'foo') |
|
98 | 99 | nt.assert_raises(ValueError, set_fp, -1) |
|
99 | 100 | |
|
100 | 101 | def test_for_type(): |
|
101 | 102 | f = PlainTextFormatter() |
|
102 | 103 | |
|
103 | 104 | # initial return, None |
|
104 | 105 | nt.assert_is(f.for_type(C, foo_printer), None) |
|
105 | 106 | # no func queries |
|
106 | 107 | nt.assert_is(f.for_type(C), foo_printer) |
|
107 | 108 | # shouldn't change anything |
|
108 | 109 | nt.assert_is(f.for_type(C), foo_printer) |
|
109 | 110 | # None should do the same |
|
110 | 111 | nt.assert_is(f.for_type(C, None), foo_printer) |
|
111 | 112 | nt.assert_is(f.for_type(C, None), foo_printer) |
|
112 | 113 | |
|
113 | 114 | def test_for_type_string(): |
|
114 | 115 | f = PlainTextFormatter() |
|
115 | 116 | |
|
116 | 117 | mod = C.__module__ |
|
117 | 118 | |
|
118 | 119 | type_str = '%s.%s' % (C.__module__, 'C') |
|
119 | 120 | |
|
120 | 121 | # initial return, None |
|
121 | 122 | nt.assert_is(f.for_type(type_str, foo_printer), None) |
|
122 | 123 | # no func queries |
|
123 | 124 | nt.assert_is(f.for_type(type_str), foo_printer) |
|
124 | 125 | nt.assert_in(_mod_name_key(C), f.deferred_printers) |
|
125 | 126 | nt.assert_is(f.for_type(C), foo_printer) |
|
126 | 127 | nt.assert_not_in(_mod_name_key(C), f.deferred_printers) |
|
127 | 128 | nt.assert_in(C, f.type_printers) |
|
128 | 129 | |
|
129 | 130 | def test_for_type_by_name(): |
|
130 | 131 | f = PlainTextFormatter() |
|
131 | 132 | |
|
132 | 133 | mod = C.__module__ |
|
133 | 134 | |
|
134 | 135 | # initial return, None |
|
135 | 136 | nt.assert_is(f.for_type_by_name(mod, 'C', foo_printer), None) |
|
136 | 137 | # no func queries |
|
137 | 138 | nt.assert_is(f.for_type_by_name(mod, 'C'), foo_printer) |
|
138 | 139 | # shouldn't change anything |
|
139 | 140 | nt.assert_is(f.for_type_by_name(mod, 'C'), foo_printer) |
|
140 | 141 | # None should do the same |
|
141 | 142 | nt.assert_is(f.for_type_by_name(mod, 'C', None), foo_printer) |
|
142 | 143 | nt.assert_is(f.for_type_by_name(mod, 'C', None), foo_printer) |
|
143 | 144 | |
|
144 | 145 | def test_lookup(): |
|
145 | 146 | f = PlainTextFormatter() |
|
146 | 147 | |
|
147 | 148 | f.for_type(C, foo_printer) |
|
148 | 149 | nt.assert_is(f.lookup(C()), foo_printer) |
|
149 | 150 | with nt.assert_raises(KeyError): |
|
150 | 151 | f.lookup(A()) |
|
151 | 152 | |
|
152 | 153 | def test_lookup_string(): |
|
153 | 154 | f = PlainTextFormatter() |
|
154 | 155 | type_str = '%s.%s' % (C.__module__, 'C') |
|
155 | 156 | |
|
156 | 157 | f.for_type(type_str, foo_printer) |
|
157 | 158 | nt.assert_is(f.lookup(C()), foo_printer) |
|
158 | 159 | # should move from deferred to imported dict |
|
159 | 160 | nt.assert_not_in(_mod_name_key(C), f.deferred_printers) |
|
160 | 161 | nt.assert_in(C, f.type_printers) |
|
161 | 162 | |
|
162 | 163 | def test_lookup_by_type(): |
|
163 | 164 | f = PlainTextFormatter() |
|
164 | 165 | f.for_type(C, foo_printer) |
|
165 | 166 | nt.assert_is(f.lookup_by_type(C), foo_printer) |
|
166 | 167 | type_str = '%s.%s' % (C.__module__, 'C') |
|
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 | nt.assert_equal("", captured.stderr) |
|
264 | 265 | nt.assert_equal("", captured.stdout) |
|
265 | 266 | |
|
266 | 267 | def test_warn_error_for_type(): |
|
267 | 268 | f = HTMLFormatter() |
|
268 | 269 | f.for_type(int, lambda i: name_error) |
|
269 | 270 | with capture_output() as captured: |
|
270 | 271 | result = f(5) |
|
271 | 272 | nt.assert_is(result, None) |
|
272 | 273 | nt.assert_in("Traceback", captured.stdout) |
|
273 | 274 | nt.assert_in("NameError", captured.stdout) |
|
274 | 275 | nt.assert_in("name_error", captured.stdout) |
|
275 | 276 | |
|
276 | 277 | def test_error_pretty_method(): |
|
277 | 278 | f = PlainTextFormatter() |
|
278 | 279 | class BadPretty(object): |
|
279 | 280 | def _repr_pretty_(self): |
|
280 | 281 | return "hello" |
|
281 | 282 | bad = BadPretty() |
|
282 | 283 | with capture_output() as captured: |
|
283 | 284 | result = f(bad) |
|
284 | 285 | nt.assert_is(result, None) |
|
285 | 286 | nt.assert_in("Traceback", captured.stdout) |
|
286 | 287 | nt.assert_in("_repr_pretty_", captured.stdout) |
|
287 | 288 | nt.assert_in("given", captured.stdout) |
|
288 | 289 | nt.assert_in("argument", captured.stdout) |
|
289 | 290 | |
|
290 | 291 | |
|
291 | 292 | def test_bad_repr_traceback(): |
|
292 | 293 | f = PlainTextFormatter() |
|
293 | 294 | bad = BadRepr() |
|
294 | 295 | with capture_output() as captured: |
|
295 | 296 | result = f(bad) |
|
296 | 297 | # catches error, returns None |
|
297 | 298 | nt.assert_is(result, None) |
|
298 | 299 | nt.assert_in("Traceback", captured.stdout) |
|
299 | 300 | nt.assert_in("__repr__", captured.stdout) |
|
300 | 301 | nt.assert_in("ValueError", captured.stdout) |
|
301 | 302 | |
|
302 | 303 | |
|
303 | 304 | class MakePDF(object): |
|
304 | 305 | def _repr_pdf_(self): |
|
305 | 306 | return 'PDF' |
|
306 | 307 | |
|
307 | 308 | def test_pdf_formatter(): |
|
308 | 309 | pdf = MakePDF() |
|
309 | 310 | f = PDFFormatter() |
|
310 | 311 | nt.assert_equal(f(pdf), 'PDF') |
|
311 | 312 | |
|
312 | 313 | def test_print_method_bound(): |
|
313 | 314 | f = HTMLFormatter() |
|
314 | 315 | class MyHTML(object): |
|
315 | 316 | def _repr_html_(self): |
|
316 | 317 | return "hello" |
|
317 | 318 | with capture_output() as captured: |
|
318 | 319 | result = f(MyHTML) |
|
319 | 320 | nt.assert_is(result, None) |
|
320 | 321 | nt.assert_not_in("FormatterWarning", captured.stderr) |
|
321 | 322 | |
|
322 | 323 | with capture_output() as captured: |
|
323 | 324 | result = f(MyHTML()) |
|
324 | 325 | nt.assert_equal(result, "hello") |
|
325 | 326 | nt.assert_equal(captured.stderr, "") |
|
326 | 327 | |
|
327 | 328 | def test_print_method_weird(): |
|
328 | 329 | |
|
329 | 330 | class TextMagicHat(object): |
|
330 | 331 | def __getattr__(self, key): |
|
331 | 332 | return key |
|
332 | 333 | |
|
333 | 334 | f = HTMLFormatter() |
|
334 | 335 | |
|
335 | 336 | text_hat = TextMagicHat() |
|
336 | 337 | nt.assert_equal(text_hat._repr_html_, '_repr_html_') |
|
337 | 338 | with capture_output() as captured: |
|
338 | 339 | result = f(text_hat) |
|
339 | 340 | |
|
340 | 341 | nt.assert_is(result, None) |
|
341 | 342 | nt.assert_not_in("FormatterWarning", captured.stderr) |
|
342 | 343 | |
|
343 | 344 | class CallableMagicHat(object): |
|
344 | 345 | def __getattr__(self, key): |
|
345 | 346 | return lambda : key |
|
346 | 347 | |
|
347 | 348 | call_hat = CallableMagicHat() |
|
348 | 349 | with capture_output() as captured: |
|
349 | 350 | result = f(call_hat) |
|
350 | 351 | |
|
351 | 352 | nt.assert_equal(result, None) |
|
352 | 353 | |
|
353 | 354 | class BadReprArgs(object): |
|
354 | 355 | def _repr_html_(self, extra, args): |
|
355 | 356 | return "html" |
|
356 | 357 | |
|
357 | 358 | bad = BadReprArgs() |
|
358 | 359 | with capture_output() as captured: |
|
359 | 360 | result = f(bad) |
|
360 | 361 | |
|
361 | 362 | nt.assert_is(result, None) |
|
362 | 363 | nt.assert_not_in("FormatterWarning", captured.stderr) |
|
363 | 364 | |
|
364 | 365 | |
|
365 | 366 | def test_format_config(): |
|
366 | 367 | """config objects don't pretend to support fancy reprs with lazy attrs""" |
|
367 | 368 | f = HTMLFormatter() |
|
368 | 369 | cfg = Config() |
|
369 | 370 | with capture_output() as captured: |
|
370 | 371 | result = f(cfg) |
|
371 | 372 | nt.assert_is(result, None) |
|
372 | 373 | nt.assert_equal(captured.stderr, "") |
|
373 | 374 | |
|
374 | 375 | with capture_output() as captured: |
|
375 | 376 | result = f(Config) |
|
376 | 377 | nt.assert_is(result, None) |
|
377 | 378 | nt.assert_equal(captured.stderr, "") |
|
378 | 379 | |
|
379 | 380 | def test_pretty_max_seq_length(): |
|
380 | 381 | f = PlainTextFormatter(max_seq_length=1) |
|
381 | 382 | lis = list(range(3)) |
|
382 | 383 | text = f(lis) |
|
383 | 384 | nt.assert_equal(text, '[0, ...]') |
|
384 | 385 | f.max_seq_length = 0 |
|
385 | 386 | text = f(lis) |
|
386 | 387 | nt.assert_equal(text, '[0, 1, 2]') |
|
387 | 388 | text = f(list(range(1024))) |
|
388 | 389 | lines = text.splitlines() |
|
389 | 390 | nt.assert_equal(len(lines), 1024) |
|
391 | ||
|
392 | ||
|
393 | def test_ipython_display_formatter(): | |
|
394 | """Objects with _ipython_display_ defined bypass other formatters""" | |
|
395 | f = get_ipython().display_formatter | |
|
396 | catcher = [] | |
|
397 | class SelfDisplaying(object): | |
|
398 | def _ipython_display_(self): | |
|
399 | catcher.append(self) | |
|
400 | ||
|
401 | class NotSelfDisplaying(object): | |
|
402 | def __repr__(self): | |
|
403 | return "NotSelfDisplaying" | |
|
404 | ||
|
405 | def _ipython_display_(self): | |
|
406 | raise NotImplementedError | |
|
407 | ||
|
408 | yes = SelfDisplaying() | |
|
409 | no = NotSelfDisplaying() | |
|
410 | ||
|
411 | d, md = f.format(no) | |
|
412 | nt.assert_equal(d, {'text/plain': repr(no)}) | |
|
413 | nt.assert_equal(md, {}) | |
|
414 | nt.assert_equal(catcher, []) | |
|
415 | ||
|
416 | d, md = f.format(yes) | |
|
417 | nt.assert_equal(d, {}) | |
|
418 | nt.assert_equal(md, {}) | |
|
419 | nt.assert_equal(catcher, [yes]) | |
|
420 |
General Comments 0
You need to be logged in to leave comments.
Login now