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