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