|
7 | 7 |
|
8 | 8 | import pytest |
9 | 9 |
|
10 | | -from mathics.core.util import print_expr_tree |
| 10 | +from mathics.core.util import print_expression_tree |
11 | 11 |
|
| 12 | +import sys |
12 | 13 |
|
13 | 14 | def test__listplot(): |
14 | 15 | """tests for module builtin.drawing.plot._ListPlot""" |
@@ -207,117 +208,110 @@ def test_plot(str_expr, msgs, str_expected, fail_msg): |
207 | 208 |
|
208 | 209 | # |
209 | 210 | # Call plotting functions and examine the structure of the output |
210 | | -# TODO: check_structure is a little fragile and a little hard to debug. Imrovements: |
211 | | -# some indication of where in the structure the error is occurring - e.g. tree coordinates? |
212 | 211 | # |
213 | 212 |
|
214 | | - |
215 | 213 | def check_structure(result, expected): |
216 | 214 | """Check that expected is a prefix of result at every node""" |
217 | 215 |
|
218 | | - # print_expr_tree(result) |
219 | | - # print_expr_tree(expected) |
220 | | - |
221 | | - def msg(s): |
222 | | - pos = getattr(getattr(expected, "location", None), "start_pos", None) |
223 | | - return f"in str_expected at pos {pos or '?'}: {s}" |
| 216 | + def error(msg): |
| 217 | + print(f"\nERROR: {msg}", file=sys.stderr) |
| 218 | + print("=== result:") |
| 219 | + print_expression_tree(result) |
| 220 | + print("=== expected:") |
| 221 | + print_expression_tree(expected) |
| 222 | + raise AssertionError(msg) |
224 | 223 |
|
225 | 224 | # do the heads match? |
226 | | - assert result.get_head() == expected.get_head(), msg("heads must match") |
| 225 | + if result.get_head() != expected.get_head(): |
| 226 | + error("heads don't match") |
227 | 227 |
|
228 | | - # do they either both or neither have elements? |
229 | | - assert hasattr(result, "elements") == hasattr(expected, "elements"), msg( |
230 | | - "either both or neither must have elements" |
231 | | - ) |
232 | | - |
233 | | - # do they match? |
| 228 | + # does the structure match? |
234 | 229 | if hasattr(expected, "elements"): |
| 230 | + if not hasattr(result, "elements"): |
| 231 | + error("expected elements but result has none") |
235 | 232 | for i, e in enumerate(expected.elements): |
236 | | - assert len(result.elements) > i, msg("result has too few elements") |
| 233 | + if len(result.elements) <= i: |
| 234 | + error("result has too few elements") |
237 | 235 | check_structure(result.elements[i], e) |
238 | 236 | else: |
239 | | - assert str(result) == str(expected), msg("leaves don't match") |
| 237 | + if str(result) != str(expected): |
| 238 | + error("leaves don't match") |
240 | 239 |
|
241 | 240 |
|
242 | | -@pytest.mark.parametrize( |
243 | | - ("str_expr", "str_expected"), |
244 | | - [ |
245 | | - # Plot3D, all default options |
246 | | - ( |
247 | | - """ |
248 | | - Plot3D[ |
249 | | - x+y, |
250 | | - {x,0,1}, {y,0,1}, |
251 | | - PlotPoints->{2,2}, |
252 | | - MaxRecursion->0 |
253 | | - ] |
254 | | - """, |
255 | | - """ |
256 | | - Graphics3D[ |
257 | | - { |
258 | | - Polygon[{{0.0,0.0,0.0}, {0.0,0.5,0.5}, {0.5,0.0,0.5}}], |
259 | | - Polygon[{{}}] |
260 | | - }, |
261 | | - AspectRatio -> 1, |
262 | | - Axes -> True, |
263 | | - AxesStyle -> {}, |
264 | | - Background -> Automatic, |
265 | | - BoxRatios -> {1, 1, 0.4}, |
266 | | - ImageSize -> Automatic, |
267 | | - LabelStyle -> {}, |
268 | | - PlotRange -> Automatic, |
269 | | - PlotRangePadding -> Automatic, |
270 | | - TicksStyle -> {} |
271 | | - ] |
272 | | - """, |
273 | | - ), |
274 | | - # Plot3D, all non-default options |
275 | | - ( |
276 | | - """ |
277 | | - Plot3D[ |
278 | | - x+y, |
279 | | - {x,0,1}, {y,0,1}, |
280 | | - PlotPoints->{2,2}, |
281 | | - MaxRecursion->0 |
282 | | - AspectRatio -> 0.5, |
283 | | - Axes -> False, |
284 | | - AxesStyle -> {Red,Blue}, |
285 | | - Background -> Green, |
286 | | - BoxRatios -> {10, 10, 1}, |
287 | | - ImageSize -> {200,200}, |
288 | | - LabelStyle -> Red, |
289 | | - PlotRange -> {0,1}, |
290 | | - PlotRangePadding -> {1,2}, |
291 | | - TicksStyle -> {Purple,White} |
292 | | - ] |
293 | | - """, |
294 | | - """ |
295 | | - Graphics3D[ |
296 | | - { |
297 | | - Polygon[{{0.0,0.0,0.0}, {0.0,0.5,0.5}, {0.5,0.0,0.5}}], |
298 | | - Polygon[{{}}] |
299 | | - }, |
300 | | - AspectRatio -> 1, (* TODO: is not passed through apparently - or is my misunderstanding? *) |
301 | | - Axes -> False, |
302 | | - AxesStyle -> {RGBColor[1,0,0],RGBColor[0,0,1]}, |
303 | | - Background -> RGBColor[0,1,0], |
304 | | - BoxRatios -> {10, 10, 1}, |
305 | | - ImageSize -> {200,200}, |
306 | | - LabelStyle -> RGBColor[1,0,0], |
307 | | - PlotRange -> {0,1}, |
308 | | - PlotRangePadding -> {1,2}, |
309 | | - TicksStyle -> {RGBColor[0.5,0,0.5],GrayLevel[1]} |
310 | | - ] |
311 | | - """, |
312 | | - ), |
313 | | - ], |
314 | | -) |
315 | | -def test_plot_structure(str_expr, str_expected): |
316 | | - # TODO: unfortunately this only adds location information to the top-level expression |
317 | | - # so not very useful |
318 | | - session.evaluate("Set[$TrackLocations, True]") |
319 | | - |
| 241 | +def eval_and_check_structure(str_expr, str_expected): |
320 | 242 | expr = session.parse(str_expr) |
321 | 243 | result = expr.evaluate(session.evaluation) |
322 | 244 | expected = session.parse(str_expected) |
323 | 245 | check_structure(result, expected) |
| 246 | + |
| 247 | + |
| 248 | +def test_plot3d_default(): |
| 249 | + eval_and_check_structure( |
| 250 | + """ |
| 251 | + Plot3D[ |
| 252 | + x+y, |
| 253 | + {x,0,1}, {y,0,1}, |
| 254 | + PlotPoints->{2,2}, |
| 255 | + MaxRecursion->0 |
| 256 | + ] |
| 257 | + """, |
| 258 | + """ |
| 259 | + Graphics3D[ |
| 260 | + { |
| 261 | + Polygon[{{0.0,0.0,0.0}, {0.0,0.5,0.5}, {0.5,0.0,0.5}}], |
| 262 | + Polygon[{{}}] |
| 263 | + }, |
| 264 | + AspectRatio -> 1, |
| 265 | + Axes -> True, |
| 266 | + XAxesStyle -> {}, |
| 267 | + Background -> Automatic, |
| 268 | + BoxRatios -> {1, 1, 0.4}, |
| 269 | + ImageSize -> Automatic, |
| 270 | + LabelStyle -> {}, |
| 271 | + PlotRange -> Automatic, |
| 272 | + PlotRangePadding -> Automatic, |
| 273 | + TicksStyle -> {} |
| 274 | + ] |
| 275 | + """, |
| 276 | + ) |
| 277 | + |
| 278 | +def test_plot3d_nondefault(): |
| 279 | + eval_and_check_structure( |
| 280 | + """ |
| 281 | + Plot3D[ |
| 282 | + x+y, |
| 283 | + {x,0,1}, {y,0,1}, |
| 284 | + PlotPoints->{2,2}, |
| 285 | + MaxRecursion->0 |
| 286 | + AspectRatio -> 0.5, |
| 287 | + Axes -> False, |
| 288 | + AxesStyle -> {Red,Blue}, |
| 289 | + Background -> Green, |
| 290 | + BoxRatios -> {10, 10, 1}, |
| 291 | + ImageSize -> {200,200}, |
| 292 | + LabelStyle -> Red, |
| 293 | + PlotRange -> {0,1}, |
| 294 | + PlotRangePadding -> {1,2}, |
| 295 | + TicksStyle -> {Purple,White} |
| 296 | + ] |
| 297 | + """, |
| 298 | + """ |
| 299 | + Graphics3D[ |
| 300 | + { |
| 301 | + Polygon[{{0.0,0.0,0.0}, {0.0,0.5,0.5}, {0.5,0.0,0.5}}], |
| 302 | + Polygon[{{}}] |
| 303 | + }, |
| 304 | + AspectRatio -> 1, (* TODO: is not passed through apparently - or is my misunderstanding? *) |
| 305 | + Axes -> False, |
| 306 | + AxesStyle -> {RGBColor[1,0,0],RGBColor[0,0,1]}, |
| 307 | + Background -> RGBColor[0,1,0], |
| 308 | + BoxRatios -> {10, 10, 1}, |
| 309 | + ImageSize -> {200,200}, |
| 310 | + LabelStyle -> RGBColor[1,0,0], |
| 311 | + PlotRange -> {0,1}, |
| 312 | + PlotRangePadding -> {1,2}, |
| 313 | + TicksStyle -> {RGBColor[0.5,0,0.5],GrayLevel[1]} |
| 314 | + ] |
| 315 | + """, |
| 316 | + ) |
| 317 | + |
0 commit comments