Skip to content

Commit 261ce8a

Browse files
committed
Add "lpn" checking in _ListPlot
"lpn" is "list of points" checking on ListPlot[] and ListLinePlot[]
1 parent 8ca7514 commit 261ce8a

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

mathics/builtin/drawing/plot.py

+12
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ class _ListPlot(Builtin, ABC):
291291
"an appropriate list of range specifications."
292292
),
293293
"joind": "Value of option Joined -> `1` is not True or False.",
294+
"lpn": "`1` is not a list of numbers or pairs of numbers.",
294295
}
295296

296297
use_log_scale = False
@@ -300,6 +301,17 @@ def eval(self, points, evaluation: Evaluation, options: dict):
300301

301302
class_name = self.__class__.__name__
302303

304+
if not isinstance(points, ListExpression):
305+
evaluation.message(class_name, "lpn", points)
306+
return
307+
308+
if not all(
309+
not isinstance(point_pair, ListExpression) or len(point_pair.elements) == 2
310+
for point_pair in points.elements
311+
):
312+
evaluation.message(class_name, "lpn", points)
313+
return
314+
303315
# Scale point values down by Log 10. Tick mark values will be adjusted to be 10^n in GraphicsBox.
304316
if self.use_log_scale:
305317
points = ListExpression(

test/builtin/drawing/test_plot.py

+32-5
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,

test/helper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def evaluate(str_expr: str):
3838
def check_evaluation(
3939
str_expr: Optional[str],
4040
str_expected: Optional[str] = None,
41-
failure_message: str = "",
41+
failure_message: Optional[str] = "",
4242
hold_expected: bool = False,
4343
to_string_expr: Optional[bool] = True,
4444
to_string_expected: bool = True,

0 commit comments

Comments
 (0)