Skip to content

Commit dde65d8

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

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

Diff for: mathics/builtin/drawing/plot.py

+16-1
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
@@ -300,6 +301,20 @@ 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+
element.is_numeric(evaluation)
310+
or isinstance(element, ListExpression)
311+
or (1 <= len(element.elements) <= 2)
312+
or (len(element.elements) == 1 and isinstance(element[0], ListExpression))
313+
for element in points.elements
314+
):
315+
evaluation.message(class_name, "lpn", points)
316+
return
317+
303318
# Scale point values down by Log 10. Tick mark values will be adjusted to be 10^n in GraphicsBox.
304319
if self.use_log_scale:
305320
points = ListExpression(

Diff for: 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,

Diff for: 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)