##// END OF EJS Templates
test formatter raising NotImplementedError
MinRK -
Show More
@@ -1,270 +1,282 b''
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.core.formatters import PlainTextFormatter, HTMLFormatter, _mod_name_key
11 from IPython.core.formatters import PlainTextFormatter, HTMLFormatter, _mod_name_key
12 from IPython.utils.io import capture_output
12 from IPython.utils.io import capture_output
13
13
14 class A(object):
14 class A(object):
15 def __repr__(self):
15 def __repr__(self):
16 return 'A()'
16 return 'A()'
17
17
18 class B(A):
18 class B(A):
19 def __repr__(self):
19 def __repr__(self):
20 return 'B()'
20 return 'B()'
21
21
22 class C:
22 class C:
23 pass
23 pass
24
24
25 class BadPretty(object):
25 class BadPretty(object):
26 _repr_pretty_ = None
26 _repr_pretty_ = None
27
27
28 class GoodPretty(object):
28 class GoodPretty(object):
29 def _repr_pretty_(self, pp, cycle):
29 def _repr_pretty_(self, pp, cycle):
30 pp.text('foo')
30 pp.text('foo')
31
31
32 def __repr__(self):
32 def __repr__(self):
33 return 'GoodPretty()'
33 return 'GoodPretty()'
34
34
35 def foo_printer(obj, pp, cycle):
35 def foo_printer(obj, pp, cycle):
36 pp.text('foo')
36 pp.text('foo')
37
37
38 def test_pretty():
38 def test_pretty():
39 f = PlainTextFormatter()
39 f = PlainTextFormatter()
40 f.for_type(A, foo_printer)
40 f.for_type(A, foo_printer)
41 nt.assert_equal(f(A()), 'foo')
41 nt.assert_equal(f(A()), 'foo')
42 nt.assert_equal(f(B()), 'foo')
42 nt.assert_equal(f(B()), 'foo')
43 nt.assert_equal(f(GoodPretty()), 'foo')
43 nt.assert_equal(f(GoodPretty()), 'foo')
44 # Just don't raise an exception for the following:
44 # Just don't raise an exception for the following:
45 f(BadPretty())
45 f(BadPretty())
46
46
47 f.pprint = False
47 f.pprint = False
48 nt.assert_equal(f(A()), 'A()')
48 nt.assert_equal(f(A()), 'A()')
49 nt.assert_equal(f(B()), 'B()')
49 nt.assert_equal(f(B()), 'B()')
50 nt.assert_equal(f(GoodPretty()), 'GoodPretty()')
50 nt.assert_equal(f(GoodPretty()), 'GoodPretty()')
51
51
52
52
53 def test_deferred():
53 def test_deferred():
54 f = PlainTextFormatter()
54 f = PlainTextFormatter()
55
55
56 def test_precision():
56 def test_precision():
57 """test various values for float_precision."""
57 """test various values for float_precision."""
58 f = PlainTextFormatter()
58 f = PlainTextFormatter()
59 nt.assert_equal(f(pi), repr(pi))
59 nt.assert_equal(f(pi), repr(pi))
60 f.float_precision = 0
60 f.float_precision = 0
61 if numpy:
61 if numpy:
62 po = numpy.get_printoptions()
62 po = numpy.get_printoptions()
63 nt.assert_equal(po['precision'], 0)
63 nt.assert_equal(po['precision'], 0)
64 nt.assert_equal(f(pi), '3')
64 nt.assert_equal(f(pi), '3')
65 f.float_precision = 2
65 f.float_precision = 2
66 if numpy:
66 if numpy:
67 po = numpy.get_printoptions()
67 po = numpy.get_printoptions()
68 nt.assert_equal(po['precision'], 2)
68 nt.assert_equal(po['precision'], 2)
69 nt.assert_equal(f(pi), '3.14')
69 nt.assert_equal(f(pi), '3.14')
70 f.float_precision = '%g'
70 f.float_precision = '%g'
71 if numpy:
71 if numpy:
72 po = numpy.get_printoptions()
72 po = numpy.get_printoptions()
73 nt.assert_equal(po['precision'], 2)
73 nt.assert_equal(po['precision'], 2)
74 nt.assert_equal(f(pi), '3.14159')
74 nt.assert_equal(f(pi), '3.14159')
75 f.float_precision = '%e'
75 f.float_precision = '%e'
76 nt.assert_equal(f(pi), '3.141593e+00')
76 nt.assert_equal(f(pi), '3.141593e+00')
77 f.float_precision = ''
77 f.float_precision = ''
78 if numpy:
78 if numpy:
79 po = numpy.get_printoptions()
79 po = numpy.get_printoptions()
80 nt.assert_equal(po['precision'], 8)
80 nt.assert_equal(po['precision'], 8)
81 nt.assert_equal(f(pi), repr(pi))
81 nt.assert_equal(f(pi), repr(pi))
82
82
83 def test_bad_precision():
83 def test_bad_precision():
84 """test various invalid values for float_precision."""
84 """test various invalid values for float_precision."""
85 f = PlainTextFormatter()
85 f = PlainTextFormatter()
86 def set_fp(p):
86 def set_fp(p):
87 f.float_precision=p
87 f.float_precision=p
88 nt.assert_raises(ValueError, set_fp, '%')
88 nt.assert_raises(ValueError, set_fp, '%')
89 nt.assert_raises(ValueError, set_fp, '%.3f%i')
89 nt.assert_raises(ValueError, set_fp, '%.3f%i')
90 nt.assert_raises(ValueError, set_fp, 'foo')
90 nt.assert_raises(ValueError, set_fp, 'foo')
91 nt.assert_raises(ValueError, set_fp, -1)
91 nt.assert_raises(ValueError, set_fp, -1)
92
92
93 def test_for_type():
93 def test_for_type():
94 f = PlainTextFormatter()
94 f = PlainTextFormatter()
95
95
96 # initial return, None
96 # initial return, None
97 nt.assert_is(f.for_type(C, foo_printer), None)
97 nt.assert_is(f.for_type(C, foo_printer), None)
98 # no func queries
98 # no func queries
99 nt.assert_is(f.for_type(C), foo_printer)
99 nt.assert_is(f.for_type(C), foo_printer)
100 # shouldn't change anything
100 # shouldn't change anything
101 nt.assert_is(f.for_type(C), foo_printer)
101 nt.assert_is(f.for_type(C), foo_printer)
102 # None should do the same
102 # None should do the same
103 nt.assert_is(f.for_type(C, None), foo_printer)
103 nt.assert_is(f.for_type(C, None), foo_printer)
104 nt.assert_is(f.for_type(C, None), foo_printer)
104 nt.assert_is(f.for_type(C, None), foo_printer)
105
105
106 def test_for_type_string():
106 def test_for_type_string():
107 f = PlainTextFormatter()
107 f = PlainTextFormatter()
108
108
109 mod = C.__module__
109 mod = C.__module__
110
110
111 type_str = '%s.%s' % (C.__module__, 'C')
111 type_str = '%s.%s' % (C.__module__, 'C')
112
112
113 # initial return, None
113 # initial return, None
114 nt.assert_is(f.for_type(type_str, foo_printer), None)
114 nt.assert_is(f.for_type(type_str, foo_printer), None)
115 # no func queries
115 # no func queries
116 nt.assert_is(f.for_type(type_str), foo_printer)
116 nt.assert_is(f.for_type(type_str), foo_printer)
117 nt.assert_in(_mod_name_key(C), f.deferred_printers)
117 nt.assert_in(_mod_name_key(C), f.deferred_printers)
118 nt.assert_is(f.for_type(C), foo_printer)
118 nt.assert_is(f.for_type(C), foo_printer)
119 nt.assert_not_in(_mod_name_key(C), f.deferred_printers)
119 nt.assert_not_in(_mod_name_key(C), f.deferred_printers)
120 nt.assert_in(C, f.type_printers)
120 nt.assert_in(C, f.type_printers)
121
121
122 def test_for_type_by_name():
122 def test_for_type_by_name():
123 f = PlainTextFormatter()
123 f = PlainTextFormatter()
124
124
125 mod = C.__module__
125 mod = C.__module__
126
126
127 # initial return, None
127 # initial return, None
128 nt.assert_is(f.for_type_by_name(mod, 'C', foo_printer), None)
128 nt.assert_is(f.for_type_by_name(mod, 'C', foo_printer), None)
129 # no func queries
129 # no func queries
130 nt.assert_is(f.for_type_by_name(mod, 'C'), foo_printer)
130 nt.assert_is(f.for_type_by_name(mod, 'C'), foo_printer)
131 # shouldn't change anything
131 # shouldn't change anything
132 nt.assert_is(f.for_type_by_name(mod, 'C'), foo_printer)
132 nt.assert_is(f.for_type_by_name(mod, 'C'), foo_printer)
133 # None should do the same
133 # None should do the same
134 nt.assert_is(f.for_type_by_name(mod, 'C', None), foo_printer)
134 nt.assert_is(f.for_type_by_name(mod, 'C', None), foo_printer)
135 nt.assert_is(f.for_type_by_name(mod, 'C', None), foo_printer)
135 nt.assert_is(f.for_type_by_name(mod, 'C', None), foo_printer)
136
136
137 def test_lookup():
137 def test_lookup():
138 f = PlainTextFormatter()
138 f = PlainTextFormatter()
139
139
140 f.for_type(C, foo_printer)
140 f.for_type(C, foo_printer)
141 nt.assert_is(f.lookup(C()), foo_printer)
141 nt.assert_is(f.lookup(C()), foo_printer)
142 with nt.assert_raises(KeyError):
142 with nt.assert_raises(KeyError):
143 f.lookup(A())
143 f.lookup(A())
144
144
145 def test_lookup_string():
145 def test_lookup_string():
146 f = PlainTextFormatter()
146 f = PlainTextFormatter()
147 type_str = '%s.%s' % (C.__module__, 'C')
147 type_str = '%s.%s' % (C.__module__, 'C')
148
148
149 f.for_type(type_str, foo_printer)
149 f.for_type(type_str, foo_printer)
150 nt.assert_is(f.lookup(C()), foo_printer)
150 nt.assert_is(f.lookup(C()), foo_printer)
151 # should move from deferred to imported dict
151 # should move from deferred to imported dict
152 nt.assert_not_in(_mod_name_key(C), f.deferred_printers)
152 nt.assert_not_in(_mod_name_key(C), f.deferred_printers)
153 nt.assert_in(C, f.type_printers)
153 nt.assert_in(C, f.type_printers)
154
154
155 def test_lookup_by_type():
155 def test_lookup_by_type():
156 f = PlainTextFormatter()
156 f = PlainTextFormatter()
157 f.for_type(C, foo_printer)
157 f.for_type(C, foo_printer)
158 nt.assert_is(f.lookup_by_type(C), foo_printer)
158 nt.assert_is(f.lookup_by_type(C), foo_printer)
159 type_str = '%s.%s' % (C.__module__, 'C')
159 type_str = '%s.%s' % (C.__module__, 'C')
160 with nt.assert_raises(KeyError):
160 with nt.assert_raises(KeyError):
161 f.lookup_by_type(A)
161 f.lookup_by_type(A)
162
162
163 def test_lookup_by_type_string():
163 def test_lookup_by_type_string():
164 f = PlainTextFormatter()
164 f = PlainTextFormatter()
165 type_str = '%s.%s' % (C.__module__, 'C')
165 type_str = '%s.%s' % (C.__module__, 'C')
166 f.for_type(type_str, foo_printer)
166 f.for_type(type_str, foo_printer)
167
167
168 # verify insertion
168 # verify insertion
169 nt.assert_in(_mod_name_key(C), f.deferred_printers)
169 nt.assert_in(_mod_name_key(C), f.deferred_printers)
170 nt.assert_not_in(C, f.type_printers)
170 nt.assert_not_in(C, f.type_printers)
171
171
172 nt.assert_is(f.lookup_by_type(type_str), foo_printer)
172 nt.assert_is(f.lookup_by_type(type_str), foo_printer)
173 # lookup by string doesn't cause import
173 # lookup by string doesn't cause import
174 nt.assert_in(_mod_name_key(C), f.deferred_printers)
174 nt.assert_in(_mod_name_key(C), f.deferred_printers)
175 nt.assert_not_in(C, f.type_printers)
175 nt.assert_not_in(C, f.type_printers)
176
176
177 nt.assert_is(f.lookup_by_type(C), foo_printer)
177 nt.assert_is(f.lookup_by_type(C), foo_printer)
178 # should move from deferred to imported dict
178 # should move from deferred to imported dict
179 nt.assert_not_in(_mod_name_key(C), f.deferred_printers)
179 nt.assert_not_in(_mod_name_key(C), f.deferred_printers)
180 nt.assert_in(C, f.type_printers)
180 nt.assert_in(C, f.type_printers)
181
181
182 def test_in_formatter():
182 def test_in_formatter():
183 f = PlainTextFormatter()
183 f = PlainTextFormatter()
184 f.for_type(C, foo_printer)
184 f.for_type(C, foo_printer)
185 type_str = '%s.%s' % (C.__module__, 'C')
185 type_str = '%s.%s' % (C.__module__, 'C')
186 nt.assert_in(C, f)
186 nt.assert_in(C, f)
187 nt.assert_in(type_str, f)
187 nt.assert_in(type_str, f)
188
188
189 def test_string_in_formatter():
189 def test_string_in_formatter():
190 f = PlainTextFormatter()
190 f = PlainTextFormatter()
191 type_str = '%s.%s' % (C.__module__, 'C')
191 type_str = '%s.%s' % (C.__module__, 'C')
192 f.for_type(type_str, foo_printer)
192 f.for_type(type_str, foo_printer)
193 nt.assert_in(type_str, f)
193 nt.assert_in(type_str, f)
194 nt.assert_in(C, f)
194 nt.assert_in(C, f)
195
195
196 def test_pop():
196 def test_pop():
197 f = PlainTextFormatter()
197 f = PlainTextFormatter()
198 f.for_type(C, foo_printer)
198 f.for_type(C, foo_printer)
199 nt.assert_is(f.lookup_by_type(C), foo_printer)
199 nt.assert_is(f.lookup_by_type(C), foo_printer)
200 nt.assert_is(f.pop(C, None), foo_printer)
200 nt.assert_is(f.pop(C, None), foo_printer)
201 f.for_type(C, foo_printer)
201 f.for_type(C, foo_printer)
202 nt.assert_is(f.pop(C), foo_printer)
202 nt.assert_is(f.pop(C), foo_printer)
203 with nt.assert_raises(KeyError):
203 with nt.assert_raises(KeyError):
204 f.lookup_by_type(C)
204 f.lookup_by_type(C)
205 with nt.assert_raises(KeyError):
205 with nt.assert_raises(KeyError):
206 f.pop(C)
206 f.pop(C)
207 with nt.assert_raises(KeyError):
207 with nt.assert_raises(KeyError):
208 f.pop(A)
208 f.pop(A)
209 nt.assert_is(f.pop(A, None), None)
209 nt.assert_is(f.pop(A, None), None)
210
210
211 def test_pop_string():
211 def test_pop_string():
212 f = PlainTextFormatter()
212 f = PlainTextFormatter()
213 type_str = '%s.%s' % (C.__module__, 'C')
213 type_str = '%s.%s' % (C.__module__, 'C')
214
214
215 with nt.assert_raises(KeyError):
215 with nt.assert_raises(KeyError):
216 f.pop(type_str)
216 f.pop(type_str)
217
217
218 f.for_type(type_str, foo_printer)
218 f.for_type(type_str, foo_printer)
219 f.pop(type_str)
219 f.pop(type_str)
220 with nt.assert_raises(KeyError):
220 with nt.assert_raises(KeyError):
221 f.lookup_by_type(C)
221 f.lookup_by_type(C)
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(C, foo_printer)
225 f.for_type(C, foo_printer)
226 nt.assert_is(f.pop(type_str, None), foo_printer)
226 nt.assert_is(f.pop(type_str, None), foo_printer)
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 nt.assert_is(f.pop(type_str, None), None)
231 nt.assert_is(f.pop(type_str, None), None)
232
232
233
233
234 def test_warn_error_method():
234 def test_warn_error_method():
235 f = HTMLFormatter()
235 f = HTMLFormatter()
236 class BadHTML(object):
236 class BadHTML(object):
237 def _repr_html_(self):
237 def _repr_html_(self):
238 return 1/0
238 return 1/0
239 bad = BadHTML()
239 bad = BadHTML()
240 with capture_output() as captured:
240 with capture_output() as captured:
241 result = f(bad)
241 result = f(bad)
242 nt.assert_is(result, None)
242 nt.assert_is(result, None)
243 nt.assert_in("WARNING", captured.stderr)
243 nt.assert_in("WARNING", captured.stderr)
244 nt.assert_in("text/html", captured.stderr)
244 nt.assert_in("text/html", captured.stderr)
245 nt.assert_in("zero", captured.stderr)
245 nt.assert_in("zero", captured.stderr)
246
246
247 def test_nowarn_notimplemented():
248 f = HTMLFormatter()
249 class HTMLNotImplemented(object):
250 def _repr_html_(self):
251 raise NotImplementedError
252 return 1/0
253 h = HTMLNotImplemented()
254 with capture_output() as captured:
255 result = f(h)
256 nt.assert_is(result, None)
257 nt.assert_not_in("WARNING", captured.stderr)
258
247 def test_warn_error_for_type():
259 def test_warn_error_for_type():
248 f = HTMLFormatter()
260 f = HTMLFormatter()
249 f.for_type(int, lambda i: name_error)
261 f.for_type(int, lambda i: name_error)
250 with capture_output() as captured:
262 with capture_output() as captured:
251 result = f(5)
263 result = f(5)
252 nt.assert_is(result, None)
264 nt.assert_is(result, None)
253 nt.assert_in("WARNING", captured.stderr)
265 nt.assert_in("WARNING", captured.stderr)
254 nt.assert_in("text/html", captured.stderr)
266 nt.assert_in("text/html", captured.stderr)
255 nt.assert_in("name_error", captured.stderr)
267 nt.assert_in("name_error", captured.stderr)
256
268
257 def test_warn_error_pretty_method():
269 def test_warn_error_pretty_method():
258 f = PlainTextFormatter()
270 f = PlainTextFormatter()
259 class BadPretty(object):
271 class BadPretty(object):
260 def _repr_pretty_(self):
272 def _repr_pretty_(self):
261 return "hello"
273 return "hello"
262 bad = BadPretty()
274 bad = BadPretty()
263 with capture_output() as captured:
275 with capture_output() as captured:
264 result = f(bad)
276 result = f(bad)
265 nt.assert_is(result, None)
277 nt.assert_is(result, None)
266 nt.assert_in("WARNING", captured.stderr)
278 nt.assert_in("WARNING", captured.stderr)
267 nt.assert_in("text/plain", captured.stderr)
279 nt.assert_in("text/plain", captured.stderr)
268 nt.assert_in("argument", captured.stderr)
280 nt.assert_in("argument", captured.stderr)
269
281
270
282
General Comments 0
You need to be logged in to leave comments. Login now