Skip to content

Commit 084c792

Browse files
committed
Add _ListPlot checking
1 parent a9792ff commit 084c792

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

mathics/builtin/drawing/plot.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,12 @@ class _ListPlot(Builtin, ABC):
286286
attributes = A_PROTECTED | A_READ_PROTECTED
287287

288288
messages = {
289+
"joind": "Value of option Joined -> `1` is not True or False.",
290+
"lpn": "`1` is not a list of numbers or pairs of numbers.",
289291
"prng": (
290292
"Value of option PlotRange -> `1` is not All, Automatic or "
291293
"an appropriate list of range specifications."
292294
),
293-
"joind": "Value of option Joined -> `1` is not True or False.",
294295
}
295296

296297
use_log_scale = False
@@ -309,6 +310,21 @@ def eval(self, points, evaluation: Evaluation, options: dict):
309310
)
310311
)
311312

313+
points = points.evaluate(evaluation)
314+
if not isinstance(points, ListExpression):
315+
evaluation.message(class_name, "lpn", points)
316+
return
317+
318+
if not all(
319+
element.is_numeric(evaluation)
320+
or isinstance(element, ListExpression)
321+
or (1 <= len(element.elements) <= 2)
322+
or (len(element.elements) == 1 and isinstance(element[0], ListExpression))
323+
for element in points.elements
324+
):
325+
evaluation.message(class_name, "lpn", points)
326+
return
327+
312328
# If "points" is a literal value with a Python representation,
313329
# it has a ".value" attribute with a non-None value. So here,
314330
# we don't have to eval_N().to_python().

test/builtin/drawing/test_plot.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,40 @@
33
Unit tests from mathics.builtin.drawing.plot
44
"""
55

6-
import sys
7-
import time
8-
from test.helper import check_evaluation, evaluate
6+
from test.helper import check_evaluation
97

108
import pytest
119

1210

11+
def test__listplot():
12+
"""tests for module builtin.drawing.plot._ListPlot"""
13+
for str_expr, msgs, str_expected, fail_msg in (
14+
(
15+
"ListPlot[5]",
16+
("5 is not a list of numbers or pairs of numbers.",),
17+
"ListPlot[5]",
18+
"ListPlot with invalid list of point",
19+
),
20+
(
21+
"ListLinePlot[{{}, {{1., 1.}}, {{1., 2.}}, {}}]",
22+
(
23+
"{{}, {{1., 1.}}, {{1., 2.}}, {}} is not a list of numbers or pairs of numbers.",
24+
),
25+
"ListLinePlot[{{}, {{1., 1.}}, {{1., 2.}}, {}}]",
26+
"ListLinePlot with invalid list of point",
27+
),
28+
):
29+
check_evaluation(
30+
str_expr,
31+
str_expected,
32+
to_string_expr=True,
33+
to_string_expected=True,
34+
hold_expected=True,
35+
failure_message=fail_msg,
36+
expected_messages=msgs,
37+
)
38+
39+
1340
@pytest.mark.parametrize(
1441
("str_expr", "msgs", "str_expected", "fail_msg"),
1542
[
@@ -159,8 +186,8 @@
159186
),
160187
],
161188
)
162-
def test_private_doctests_plot(str_expr, msgs, str_expected, fail_msg):
163-
"""builtin.drawing.plot"""
189+
def test_plot(str_expr, msgs, str_expected, fail_msg):
190+
"""tests for module builtin.drawing.plot"""
164191
check_evaluation(
165192
str_expr,
166193
str_expected,

0 commit comments

Comments
 (0)