Show More
@@ -1,389 +1,420 | |||||
1 | """Tests for the Formatters.""" |
|
1 | """Tests for the Formatters.""" | |
2 |
|
2 | |||
3 | from math import pi |
|
3 | from math import pi | |
4 |
|
4 | |||
5 | try: |
|
5 | try: | |
6 | import numpy |
|
6 | import numpy | |
7 | except: |
|
7 | except: | |
8 | numpy = None |
|
8 | numpy = None | |
9 | import nose.tools as nt |
|
9 | import nose.tools as nt | |
10 |
|
10 | |||
11 | from IPython.config import Config |
|
11 | from IPython.config import Config | |
12 | from IPython.core.formatters import ( |
|
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 | from IPython.utils.io import capture_output |
|
16 | from IPython.utils.io import capture_output | |
16 |
|
17 | |||
17 | class A(object): |
|
18 | class A(object): | |
18 | def __repr__(self): |
|
19 | def __repr__(self): | |
19 | return 'A()' |
|
20 | return 'A()' | |
20 |
|
21 | |||
21 | class B(A): |
|
22 | class B(A): | |
22 | def __repr__(self): |
|
23 | def __repr__(self): | |
23 | return 'B()' |
|
24 | return 'B()' | |
24 |
|
25 | |||
25 | class C: |
|
26 | class C: | |
26 | pass |
|
27 | pass | |
27 |
|
28 | |||
28 | class BadRepr(object): |
|
29 | class BadRepr(object): | |
29 | def __repr__(self): |
|
30 | def __repr__(self): | |
30 | raise ValueError("bad repr") |
|
31 | raise ValueError("bad repr") | |
31 |
|
32 | |||
32 | class BadPretty(object): |
|
33 | class BadPretty(object): | |
33 | _repr_pretty_ = None |
|
34 | _repr_pretty_ = None | |
34 |
|
35 | |||
35 | class GoodPretty(object): |
|
36 | class GoodPretty(object): | |
36 | def _repr_pretty_(self, pp, cycle): |
|
37 | def _repr_pretty_(self, pp, cycle): | |
37 | pp.text('foo') |
|
38 | pp.text('foo') | |
38 |
|
39 | |||
39 | def __repr__(self): |
|
40 | def __repr__(self): | |
40 | return 'GoodPretty()' |
|
41 | return 'GoodPretty()' | |
41 |
|
42 | |||
42 | def foo_printer(obj, pp, cycle): |
|
43 | def foo_printer(obj, pp, cycle): | |
43 | pp.text('foo') |
|
44 | pp.text('foo') | |
44 |
|
45 | |||
45 | def test_pretty(): |
|
46 | def test_pretty(): | |
46 | f = PlainTextFormatter() |
|
47 | f = PlainTextFormatter() | |
47 | f.for_type(A, foo_printer) |
|
48 | f.for_type(A, foo_printer) | |
48 | nt.assert_equal(f(A()), 'foo') |
|
49 | nt.assert_equal(f(A()), 'foo') | |
49 | nt.assert_equal(f(B()), 'foo') |
|
50 | nt.assert_equal(f(B()), 'foo') | |
50 | nt.assert_equal(f(GoodPretty()), 'foo') |
|
51 | nt.assert_equal(f(GoodPretty()), 'foo') | |
51 | # Just don't raise an exception for the following: |
|
52 | # Just don't raise an exception for the following: | |
52 | f(BadPretty()) |
|
53 | f(BadPretty()) | |
53 |
|
54 | |||
54 | f.pprint = False |
|
55 | f.pprint = False | |
55 | nt.assert_equal(f(A()), 'A()') |
|
56 | nt.assert_equal(f(A()), 'A()') | |
56 | nt.assert_equal(f(B()), 'B()') |
|
57 | nt.assert_equal(f(B()), 'B()') | |
57 | nt.assert_equal(f(GoodPretty()), 'GoodPretty()') |
|
58 | nt.assert_equal(f(GoodPretty()), 'GoodPretty()') | |
58 |
|
59 | |||
59 |
|
60 | |||
60 | def test_deferred(): |
|
61 | def test_deferred(): | |
61 | f = PlainTextFormatter() |
|
62 | f = PlainTextFormatter() | |
62 |
|
63 | |||
63 | def test_precision(): |
|
64 | def test_precision(): | |
64 | """test various values for float_precision.""" |
|
65 | """test various values for float_precision.""" | |
65 | f = PlainTextFormatter() |
|
66 | f = PlainTextFormatter() | |
66 | nt.assert_equal(f(pi), repr(pi)) |
|
67 | nt.assert_equal(f(pi), repr(pi)) | |
67 | f.float_precision = 0 |
|
68 | f.float_precision = 0 | |
68 | if numpy: |
|
69 | if numpy: | |
69 | po = numpy.get_printoptions() |
|
70 | po = numpy.get_printoptions() | |
70 | nt.assert_equal(po['precision'], 0) |
|
71 | nt.assert_equal(po['precision'], 0) | |
71 | nt.assert_equal(f(pi), '3') |
|
72 | nt.assert_equal(f(pi), '3') | |
72 | f.float_precision = 2 |
|
73 | f.float_precision = 2 | |
73 | if numpy: |
|
74 | if numpy: | |
74 | po = numpy.get_printoptions() |
|
75 | po = numpy.get_printoptions() | |
75 | nt.assert_equal(po['precision'], 2) |
|
76 | nt.assert_equal(po['precision'], 2) | |
76 | nt.assert_equal(f(pi), '3.14') |
|
77 | nt.assert_equal(f(pi), '3.14') | |
77 | f.float_precision = '%g' |
|
78 | f.float_precision = '%g' | |
78 | if numpy: |
|
79 | if numpy: | |
79 | po = numpy.get_printoptions() |
|
80 | po = numpy.get_printoptions() | |
80 | nt.assert_equal(po['precision'], 2) |
|
81 | nt.assert_equal(po['precision'], 2) | |
81 | nt.assert_equal(f(pi), '3.14159') |
|
82 | nt.assert_equal(f(pi), '3.14159') | |
82 | f.float_precision = '%e' |
|
83 | f.float_precision = '%e' | |
83 | nt.assert_equal(f(pi), '3.141593e+00') |
|
84 | nt.assert_equal(f(pi), '3.141593e+00') | |
84 | f.float_precision = '' |
|
85 | f.float_precision = '' | |
85 | if numpy: |
|
86 | if numpy: | |
86 | po = numpy.get_printoptions() |
|
87 | po = numpy.get_printoptions() | |
87 | nt.assert_equal(po['precision'], 8) |
|
88 | nt.assert_equal(po['precision'], 8) | |
88 | nt.assert_equal(f(pi), repr(pi)) |
|
89 | nt.assert_equal(f(pi), repr(pi)) | |
89 |
|
90 | |||
90 | def test_bad_precision(): |
|
91 | def test_bad_precision(): | |
91 | """test various invalid values for float_precision.""" |
|
92 | """test various invalid values for float_precision.""" | |
92 | f = PlainTextFormatter() |
|
93 | f = PlainTextFormatter() | |
93 | def set_fp(p): |
|
94 | def set_fp(p): | |
94 | f.float_precision=p |
|
95 | f.float_precision=p | |
95 | nt.assert_raises(ValueError, set_fp, '%') |
|
96 | nt.assert_raises(ValueError, set_fp, '%') | |
96 | nt.assert_raises(ValueError, set_fp, '%.3f%i') |
|
97 | nt.assert_raises(ValueError, set_fp, '%.3f%i') | |
97 | nt.assert_raises(ValueError, set_fp, 'foo') |
|
98 | nt.assert_raises(ValueError, set_fp, 'foo') | |
98 | nt.assert_raises(ValueError, set_fp, -1) |
|
99 | nt.assert_raises(ValueError, set_fp, -1) | |
99 |
|
100 | |||
100 | def test_for_type(): |
|
101 | def test_for_type(): | |
101 | f = PlainTextFormatter() |
|
102 | f = PlainTextFormatter() | |
102 |
|
103 | |||
103 | # initial return, None |
|
104 | # initial return, None | |
104 | nt.assert_is(f.for_type(C, foo_printer), None) |
|
105 | nt.assert_is(f.for_type(C, foo_printer), None) | |
105 | # no func queries |
|
106 | # no func queries | |
106 | nt.assert_is(f.for_type(C), foo_printer) |
|
107 | nt.assert_is(f.for_type(C), foo_printer) | |
107 | # shouldn't change anything |
|
108 | # shouldn't change anything | |
108 | nt.assert_is(f.for_type(C), foo_printer) |
|
109 | nt.assert_is(f.for_type(C), foo_printer) | |
109 | # None should do the same |
|
110 | # None should do the same | |
110 | nt.assert_is(f.for_type(C, None), foo_printer) |
|
111 | nt.assert_is(f.for_type(C, None), foo_printer) | |
111 | nt.assert_is(f.for_type(C, None), foo_printer) |
|
112 | nt.assert_is(f.for_type(C, None), foo_printer) | |
112 |
|
113 | |||
113 | def test_for_type_string(): |
|
114 | def test_for_type_string(): | |
114 | f = PlainTextFormatter() |
|
115 | f = PlainTextFormatter() | |
115 |
|
116 | |||
116 | mod = C.__module__ |
|
117 | mod = C.__module__ | |
117 |
|
118 | |||
118 | type_str = '%s.%s' % (C.__module__, 'C') |
|
119 | type_str = '%s.%s' % (C.__module__, 'C') | |
119 |
|
120 | |||
120 | # initial return, None |
|
121 | # initial return, None | |
121 | nt.assert_is(f.for_type(type_str, foo_printer), None) |
|
122 | nt.assert_is(f.for_type(type_str, foo_printer), None) | |
122 | # no func queries |
|
123 | # no func queries | |
123 | nt.assert_is(f.for_type(type_str), foo_printer) |
|
124 | nt.assert_is(f.for_type(type_str), foo_printer) | |
124 | nt.assert_in(_mod_name_key(C), f.deferred_printers) |
|
125 | nt.assert_in(_mod_name_key(C), f.deferred_printers) | |
125 | nt.assert_is(f.for_type(C), foo_printer) |
|
126 | nt.assert_is(f.for_type(C), foo_printer) | |
126 | nt.assert_not_in(_mod_name_key(C), f.deferred_printers) |
|
127 | nt.assert_not_in(_mod_name_key(C), f.deferred_printers) | |
127 | nt.assert_in(C, f.type_printers) |
|
128 | nt.assert_in(C, f.type_printers) | |
128 |
|
129 | |||
129 | def test_for_type_by_name(): |
|
130 | def test_for_type_by_name(): | |
130 | f = PlainTextFormatter() |
|
131 | f = PlainTextFormatter() | |
131 |
|
132 | |||
132 | mod = C.__module__ |
|
133 | mod = C.__module__ | |
133 |
|
134 | |||
134 | # initial return, None |
|
135 | # initial return, None | |
135 | 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) | |
136 | # no func queries |
|
137 | # no func queries | |
137 | 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) | |
138 | # shouldn't change anything |
|
139 | # shouldn't change anything | |
139 | 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) | |
140 | # None should do the same |
|
141 | # None should do the same | |
141 | 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) | |
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 |
|
144 | |||
144 | def test_lookup(): |
|
145 | def test_lookup(): | |
145 | f = PlainTextFormatter() |
|
146 | f = PlainTextFormatter() | |
146 |
|
147 | |||
147 | f.for_type(C, foo_printer) |
|
148 | f.for_type(C, foo_printer) | |
148 | nt.assert_is(f.lookup(C()), foo_printer) |
|
149 | nt.assert_is(f.lookup(C()), foo_printer) | |
149 | with nt.assert_raises(KeyError): |
|
150 | with nt.assert_raises(KeyError): | |
150 | f.lookup(A()) |
|
151 | f.lookup(A()) | |
151 |
|
152 | |||
152 | def test_lookup_string(): |
|
153 | def test_lookup_string(): | |
153 | f = PlainTextFormatter() |
|
154 | f = PlainTextFormatter() | |
154 | type_str = '%s.%s' % (C.__module__, 'C') |
|
155 | type_str = '%s.%s' % (C.__module__, 'C') | |
155 |
|
156 | |||
156 | f.for_type(type_str, foo_printer) |
|
157 | f.for_type(type_str, foo_printer) | |
157 | nt.assert_is(f.lookup(C()), foo_printer) |
|
158 | nt.assert_is(f.lookup(C()), foo_printer) | |
158 | # should move from deferred to imported dict |
|
159 | # should move from deferred to imported dict | |
159 | nt.assert_not_in(_mod_name_key(C), f.deferred_printers) |
|
160 | nt.assert_not_in(_mod_name_key(C), f.deferred_printers) | |
160 | nt.assert_in(C, f.type_printers) |
|
161 | nt.assert_in(C, f.type_printers) | |
161 |
|
162 | |||
162 | def test_lookup_by_type(): |
|
163 | def test_lookup_by_type(): | |
163 | f = PlainTextFormatter() |
|
164 | f = PlainTextFormatter() | |
164 | f.for_type(C, foo_printer) |
|
165 | f.for_type(C, foo_printer) | |
165 | nt.assert_is(f.lookup_by_type(C), foo_printer) |
|
166 | nt.assert_is(f.lookup_by_type(C), foo_printer) | |
166 | type_str = '%s.%s' % (C.__module__, 'C') |
|
167 | type_str = '%s.%s' % (C.__module__, 'C') | |
167 | with nt.assert_raises(KeyError): |
|
168 | with nt.assert_raises(KeyError): | |
168 | f.lookup_by_type(A) |
|
169 | f.lookup_by_type(A) | |
169 |
|
170 | |||
170 | def test_lookup_by_type_string(): |
|
171 | def test_lookup_by_type_string(): | |
171 | f = PlainTextFormatter() |
|
172 | f = PlainTextFormatter() | |
172 | type_str = '%s.%s' % (C.__module__, 'C') |
|
173 | type_str = '%s.%s' % (C.__module__, 'C') | |
173 | f.for_type(type_str, foo_printer) |
|
174 | f.for_type(type_str, foo_printer) | |
174 |
|
175 | |||
175 | # verify insertion |
|
176 | # verify insertion | |
176 | nt.assert_in(_mod_name_key(C), f.deferred_printers) |
|
177 | nt.assert_in(_mod_name_key(C), f.deferred_printers) | |
177 | nt.assert_not_in(C, f.type_printers) |
|
178 | nt.assert_not_in(C, f.type_printers) | |
178 |
|
179 | |||
179 | nt.assert_is(f.lookup_by_type(type_str), foo_printer) |
|
180 | nt.assert_is(f.lookup_by_type(type_str), foo_printer) | |
180 | # lookup by string doesn't cause import |
|
181 | # lookup by string doesn't cause import | |
181 | nt.assert_in(_mod_name_key(C), f.deferred_printers) |
|
182 | nt.assert_in(_mod_name_key(C), f.deferred_printers) | |
182 | nt.assert_not_in(C, f.type_printers) |
|
183 | nt.assert_not_in(C, f.type_printers) | |
183 |
|
184 | |||
184 | nt.assert_is(f.lookup_by_type(C), foo_printer) |
|
185 | nt.assert_is(f.lookup_by_type(C), foo_printer) | |
185 | # should move from deferred to imported dict |
|
186 | # should move from deferred to imported dict | |
186 | nt.assert_not_in(_mod_name_key(C), f.deferred_printers) |
|
187 | nt.assert_not_in(_mod_name_key(C), f.deferred_printers) | |
187 | nt.assert_in(C, f.type_printers) |
|
188 | nt.assert_in(C, f.type_printers) | |
188 |
|
189 | |||
189 | def test_in_formatter(): |
|
190 | def test_in_formatter(): | |
190 | f = PlainTextFormatter() |
|
191 | f = PlainTextFormatter() | |
191 | f.for_type(C, foo_printer) |
|
192 | f.for_type(C, foo_printer) | |
192 | type_str = '%s.%s' % (C.__module__, 'C') |
|
193 | type_str = '%s.%s' % (C.__module__, 'C') | |
193 | nt.assert_in(C, f) |
|
194 | nt.assert_in(C, f) | |
194 | nt.assert_in(type_str, f) |
|
195 | nt.assert_in(type_str, f) | |
195 |
|
196 | |||
196 | def test_string_in_formatter(): |
|
197 | def test_string_in_formatter(): | |
197 | f = PlainTextFormatter() |
|
198 | f = PlainTextFormatter() | |
198 | type_str = '%s.%s' % (C.__module__, 'C') |
|
199 | type_str = '%s.%s' % (C.__module__, 'C') | |
199 | f.for_type(type_str, foo_printer) |
|
200 | f.for_type(type_str, foo_printer) | |
200 | nt.assert_in(type_str, f) |
|
201 | nt.assert_in(type_str, f) | |
201 | nt.assert_in(C, f) |
|
202 | nt.assert_in(C, f) | |
202 |
|
203 | |||
203 | def test_pop(): |
|
204 | def test_pop(): | |
204 | f = PlainTextFormatter() |
|
205 | f = PlainTextFormatter() | |
205 | f.for_type(C, foo_printer) |
|
206 | f.for_type(C, foo_printer) | |
206 | nt.assert_is(f.lookup_by_type(C), foo_printer) |
|
207 | nt.assert_is(f.lookup_by_type(C), foo_printer) | |
207 | nt.assert_is(f.pop(C, None), foo_printer) |
|
208 | nt.assert_is(f.pop(C, None), foo_printer) | |
208 | f.for_type(C, foo_printer) |
|
209 | f.for_type(C, foo_printer) | |
209 | nt.assert_is(f.pop(C), foo_printer) |
|
210 | nt.assert_is(f.pop(C), foo_printer) | |
210 | with nt.assert_raises(KeyError): |
|
211 | with nt.assert_raises(KeyError): | |
211 | f.lookup_by_type(C) |
|
212 | f.lookup_by_type(C) | |
212 | with nt.assert_raises(KeyError): |
|
213 | with nt.assert_raises(KeyError): | |
213 | f.pop(C) |
|
214 | f.pop(C) | |
214 | with nt.assert_raises(KeyError): |
|
215 | with nt.assert_raises(KeyError): | |
215 | f.pop(A) |
|
216 | f.pop(A) | |
216 | nt.assert_is(f.pop(A, None), None) |
|
217 | nt.assert_is(f.pop(A, None), None) | |
217 |
|
218 | |||
218 | def test_pop_string(): |
|
219 | def test_pop_string(): | |
219 | f = PlainTextFormatter() |
|
220 | f = PlainTextFormatter() | |
220 | type_str = '%s.%s' % (C.__module__, 'C') |
|
221 | type_str = '%s.%s' % (C.__module__, 'C') | |
221 |
|
222 | |||
222 | with nt.assert_raises(KeyError): |
|
223 | with nt.assert_raises(KeyError): | |
223 | f.pop(type_str) |
|
224 | f.pop(type_str) | |
224 |
|
225 | |||
225 | f.for_type(type_str, foo_printer) |
|
226 | f.for_type(type_str, foo_printer) | |
226 | f.pop(type_str) |
|
227 | f.pop(type_str) | |
227 | with nt.assert_raises(KeyError): |
|
228 | with nt.assert_raises(KeyError): | |
228 | f.lookup_by_type(C) |
|
229 | f.lookup_by_type(C) | |
229 | with nt.assert_raises(KeyError): |
|
230 | with nt.assert_raises(KeyError): | |
230 | f.pop(type_str) |
|
231 | f.pop(type_str) | |
231 |
|
232 | |||
232 | f.for_type(C, foo_printer) |
|
233 | f.for_type(C, foo_printer) | |
233 | nt.assert_is(f.pop(type_str, None), foo_printer) |
|
234 | nt.assert_is(f.pop(type_str, None), foo_printer) | |
234 | with nt.assert_raises(KeyError): |
|
235 | with nt.assert_raises(KeyError): | |
235 | f.lookup_by_type(C) |
|
236 | f.lookup_by_type(C) | |
236 | with nt.assert_raises(KeyError): |
|
237 | with nt.assert_raises(KeyError): | |
237 | f.pop(type_str) |
|
238 | f.pop(type_str) | |
238 | nt.assert_is(f.pop(type_str, None), None) |
|
239 | nt.assert_is(f.pop(type_str, None), None) | |
239 |
|
240 | |||
240 |
|
241 | |||
241 | def test_error_method(): |
|
242 | def test_error_method(): | |
242 | f = HTMLFormatter() |
|
243 | f = HTMLFormatter() | |
243 | class BadHTML(object): |
|
244 | class BadHTML(object): | |
244 | def _repr_html_(self): |
|
245 | def _repr_html_(self): | |
245 | raise ValueError("Bad HTML") |
|
246 | raise ValueError("Bad HTML") | |
246 | bad = BadHTML() |
|
247 | bad = BadHTML() | |
247 | with capture_output() as captured: |
|
248 | with capture_output() as captured: | |
248 | result = f(bad) |
|
249 | result = f(bad) | |
249 | nt.assert_is(result, None) |
|
250 | nt.assert_is(result, None) | |
250 | nt.assert_in("Traceback", captured.stdout) |
|
251 | nt.assert_in("Traceback", captured.stdout) | |
251 | nt.assert_in("Bad HTML", captured.stdout) |
|
252 | nt.assert_in("Bad HTML", captured.stdout) | |
252 | nt.assert_in("_repr_html_", captured.stdout) |
|
253 | nt.assert_in("_repr_html_", captured.stdout) | |
253 |
|
254 | |||
254 | def test_nowarn_notimplemented(): |
|
255 | def test_nowarn_notimplemented(): | |
255 | f = HTMLFormatter() |
|
256 | f = HTMLFormatter() | |
256 | class HTMLNotImplemented(object): |
|
257 | class HTMLNotImplemented(object): | |
257 | def _repr_html_(self): |
|
258 | def _repr_html_(self): | |
258 | raise NotImplementedError |
|
259 | raise NotImplementedError | |
259 | h = HTMLNotImplemented() |
|
260 | h = HTMLNotImplemented() | |
260 | with capture_output() as captured: |
|
261 | with capture_output() as captured: | |
261 | result = f(h) |
|
262 | result = f(h) | |
262 | nt.assert_is(result, None) |
|
263 | nt.assert_is(result, None) | |
263 | nt.assert_equal("", captured.stderr) |
|
264 | nt.assert_equal("", captured.stderr) | |
264 | nt.assert_equal("", captured.stdout) |
|
265 | nt.assert_equal("", captured.stdout) | |
265 |
|
266 | |||
266 | def test_warn_error_for_type(): |
|
267 | def test_warn_error_for_type(): | |
267 | f = HTMLFormatter() |
|
268 | f = HTMLFormatter() | |
268 | f.for_type(int, lambda i: name_error) |
|
269 | f.for_type(int, lambda i: name_error) | |
269 | with capture_output() as captured: |
|
270 | with capture_output() as captured: | |
270 | result = f(5) |
|
271 | result = f(5) | |
271 | nt.assert_is(result, None) |
|
272 | nt.assert_is(result, None) | |
272 | nt.assert_in("Traceback", captured.stdout) |
|
273 | nt.assert_in("Traceback", captured.stdout) | |
273 | nt.assert_in("NameError", captured.stdout) |
|
274 | nt.assert_in("NameError", captured.stdout) | |
274 | nt.assert_in("name_error", captured.stdout) |
|
275 | nt.assert_in("name_error", captured.stdout) | |
275 |
|
276 | |||
276 | def test_error_pretty_method(): |
|
277 | def test_error_pretty_method(): | |
277 | f = PlainTextFormatter() |
|
278 | f = PlainTextFormatter() | |
278 | class BadPretty(object): |
|
279 | class BadPretty(object): | |
279 | def _repr_pretty_(self): |
|
280 | def _repr_pretty_(self): | |
280 | return "hello" |
|
281 | return "hello" | |
281 | bad = BadPretty() |
|
282 | bad = BadPretty() | |
282 | with capture_output() as captured: |
|
283 | with capture_output() as captured: | |
283 | result = f(bad) |
|
284 | result = f(bad) | |
284 | nt.assert_is(result, None) |
|
285 | nt.assert_is(result, None) | |
285 | nt.assert_in("Traceback", captured.stdout) |
|
286 | nt.assert_in("Traceback", captured.stdout) | |
286 | nt.assert_in("_repr_pretty_", captured.stdout) |
|
287 | nt.assert_in("_repr_pretty_", captured.stdout) | |
287 | nt.assert_in("given", captured.stdout) |
|
288 | nt.assert_in("given", captured.stdout) | |
288 | nt.assert_in("argument", captured.stdout) |
|
289 | nt.assert_in("argument", captured.stdout) | |
289 |
|
290 | |||
290 |
|
291 | |||
291 | def test_bad_repr_traceback(): |
|
292 | def test_bad_repr_traceback(): | |
292 | f = PlainTextFormatter() |
|
293 | f = PlainTextFormatter() | |
293 | bad = BadRepr() |
|
294 | bad = BadRepr() | |
294 | with capture_output() as captured: |
|
295 | with capture_output() as captured: | |
295 | result = f(bad) |
|
296 | result = f(bad) | |
296 | # catches error, returns None |
|
297 | # catches error, returns None | |
297 | nt.assert_is(result, None) |
|
298 | nt.assert_is(result, None) | |
298 | nt.assert_in("Traceback", captured.stdout) |
|
299 | nt.assert_in("Traceback", captured.stdout) | |
299 | nt.assert_in("__repr__", captured.stdout) |
|
300 | nt.assert_in("__repr__", captured.stdout) | |
300 | nt.assert_in("ValueError", captured.stdout) |
|
301 | nt.assert_in("ValueError", captured.stdout) | |
301 |
|
302 | |||
302 |
|
303 | |||
303 | class MakePDF(object): |
|
304 | class MakePDF(object): | |
304 | def _repr_pdf_(self): |
|
305 | def _repr_pdf_(self): | |
305 | return 'PDF' |
|
306 | return 'PDF' | |
306 |
|
307 | |||
307 | def test_pdf_formatter(): |
|
308 | def test_pdf_formatter(): | |
308 | pdf = MakePDF() |
|
309 | pdf = MakePDF() | |
309 | f = PDFFormatter() |
|
310 | f = PDFFormatter() | |
310 | nt.assert_equal(f(pdf), 'PDF') |
|
311 | nt.assert_equal(f(pdf), 'PDF') | |
311 |
|
312 | |||
312 | def test_print_method_bound(): |
|
313 | def test_print_method_bound(): | |
313 | f = HTMLFormatter() |
|
314 | f = HTMLFormatter() | |
314 | class MyHTML(object): |
|
315 | class MyHTML(object): | |
315 | def _repr_html_(self): |
|
316 | def _repr_html_(self): | |
316 | return "hello" |
|
317 | return "hello" | |
317 | with capture_output() as captured: |
|
318 | with capture_output() as captured: | |
318 | result = f(MyHTML) |
|
319 | result = f(MyHTML) | |
319 | nt.assert_is(result, None) |
|
320 | nt.assert_is(result, None) | |
320 | nt.assert_not_in("FormatterWarning", captured.stderr) |
|
321 | nt.assert_not_in("FormatterWarning", captured.stderr) | |
321 |
|
322 | |||
322 | with capture_output() as captured: |
|
323 | with capture_output() as captured: | |
323 | result = f(MyHTML()) |
|
324 | result = f(MyHTML()) | |
324 | nt.assert_equal(result, "hello") |
|
325 | nt.assert_equal(result, "hello") | |
325 | nt.assert_equal(captured.stderr, "") |
|
326 | nt.assert_equal(captured.stderr, "") | |
326 |
|
327 | |||
327 | def test_print_method_weird(): |
|
328 | def test_print_method_weird(): | |
328 |
|
329 | |||
329 | class TextMagicHat(object): |
|
330 | class TextMagicHat(object): | |
330 | def __getattr__(self, key): |
|
331 | def __getattr__(self, key): | |
331 | return key |
|
332 | return key | |
332 |
|
333 | |||
333 | f = HTMLFormatter() |
|
334 | f = HTMLFormatter() | |
334 |
|
335 | |||
335 | text_hat = TextMagicHat() |
|
336 | text_hat = TextMagicHat() | |
336 | nt.assert_equal(text_hat._repr_html_, '_repr_html_') |
|
337 | nt.assert_equal(text_hat._repr_html_, '_repr_html_') | |
337 | with capture_output() as captured: |
|
338 | with capture_output() as captured: | |
338 | result = f(text_hat) |
|
339 | result = f(text_hat) | |
339 |
|
340 | |||
340 | nt.assert_is(result, None) |
|
341 | nt.assert_is(result, None) | |
341 | nt.assert_not_in("FormatterWarning", captured.stderr) |
|
342 | nt.assert_not_in("FormatterWarning", captured.stderr) | |
342 |
|
343 | |||
343 | class CallableMagicHat(object): |
|
344 | class CallableMagicHat(object): | |
344 | def __getattr__(self, key): |
|
345 | def __getattr__(self, key): | |
345 | return lambda : key |
|
346 | return lambda : key | |
346 |
|
347 | |||
347 | call_hat = CallableMagicHat() |
|
348 | call_hat = CallableMagicHat() | |
348 | with capture_output() as captured: |
|
349 | with capture_output() as captured: | |
349 | result = f(call_hat) |
|
350 | result = f(call_hat) | |
350 |
|
351 | |||
351 | nt.assert_equal(result, None) |
|
352 | nt.assert_equal(result, None) | |
352 |
|
353 | |||
353 | class BadReprArgs(object): |
|
354 | class BadReprArgs(object): | |
354 | def _repr_html_(self, extra, args): |
|
355 | def _repr_html_(self, extra, args): | |
355 | return "html" |
|
356 | return "html" | |
356 |
|
357 | |||
357 | bad = BadReprArgs() |
|
358 | bad = BadReprArgs() | |
358 | with capture_output() as captured: |
|
359 | with capture_output() as captured: | |
359 | result = f(bad) |
|
360 | result = f(bad) | |
360 |
|
361 | |||
361 | nt.assert_is(result, None) |
|
362 | nt.assert_is(result, None) | |
362 | nt.assert_not_in("FormatterWarning", captured.stderr) |
|
363 | nt.assert_not_in("FormatterWarning", captured.stderr) | |
363 |
|
364 | |||
364 |
|
365 | |||
365 | def test_format_config(): |
|
366 | def test_format_config(): | |
366 | """config objects don't pretend to support fancy reprs with lazy attrs""" |
|
367 | """config objects don't pretend to support fancy reprs with lazy attrs""" | |
367 | f = HTMLFormatter() |
|
368 | f = HTMLFormatter() | |
368 | cfg = Config() |
|
369 | cfg = Config() | |
369 | with capture_output() as captured: |
|
370 | with capture_output() as captured: | |
370 | result = f(cfg) |
|
371 | result = f(cfg) | |
371 | nt.assert_is(result, None) |
|
372 | nt.assert_is(result, None) | |
372 | nt.assert_equal(captured.stderr, "") |
|
373 | nt.assert_equal(captured.stderr, "") | |
373 |
|
374 | |||
374 | with capture_output() as captured: |
|
375 | with capture_output() as captured: | |
375 | result = f(Config) |
|
376 | result = f(Config) | |
376 | nt.assert_is(result, None) |
|
377 | nt.assert_is(result, None) | |
377 | nt.assert_equal(captured.stderr, "") |
|
378 | nt.assert_equal(captured.stderr, "") | |
378 |
|
379 | |||
379 | def test_pretty_max_seq_length(): |
|
380 | def test_pretty_max_seq_length(): | |
380 | f = PlainTextFormatter(max_seq_length=1) |
|
381 | f = PlainTextFormatter(max_seq_length=1) | |
381 | lis = list(range(3)) |
|
382 | lis = list(range(3)) | |
382 | text = f(lis) |
|
383 | text = f(lis) | |
383 | nt.assert_equal(text, '[0, ...]') |
|
384 | nt.assert_equal(text, '[0, ...]') | |
384 | f.max_seq_length = 0 |
|
385 | f.max_seq_length = 0 | |
385 | text = f(lis) |
|
386 | text = f(lis) | |
386 | nt.assert_equal(text, '[0, 1, 2]') |
|
387 | nt.assert_equal(text, '[0, 1, 2]') | |
387 | text = f(list(range(1024))) |
|
388 | text = f(list(range(1024))) | |
388 | lines = text.splitlines() |
|
389 | lines = text.splitlines() | |
389 | nt.assert_equal(len(lines), 1024) |
|
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