From 5d741627ff2657bccade67d74ac6e9b12312416d Mon Sep 17 00:00:00 2001 From: rocky Date: Wed, 25 Dec 2024 05:59:28 -0500 Subject: [PATCH] Add "lpn" checking in _ListPlot "lpn" is "list of points" checking on ListPlot[] and ListLinePlot[] --- mathics/builtin/drawing/plot.py | 12 ++++++++++ test/builtin/drawing/test_plot.py | 37 ++++++++++++++++++++++++++----- test/helper.py | 2 +- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/mathics/builtin/drawing/plot.py b/mathics/builtin/drawing/plot.py index 9387c3534..cd0b1dd7c 100644 --- a/mathics/builtin/drawing/plot.py +++ b/mathics/builtin/drawing/plot.py @@ -291,6 +291,7 @@ class _ListPlot(Builtin, ABC): "an appropriate list of range specifications." ), "joind": "Value of option Joined -> `1` is not True or False.", + "lpn": "`1` is not a list of numbers or pairs of numbers.", } use_log_scale = False @@ -300,6 +301,17 @@ def eval(self, points, evaluation: Evaluation, options: dict): class_name = self.__class__.__name__ + if not isinstance(points, ListExpression): + evaluation.message(class_name, "lpn", points) + return + + if not all( + not isinstance(point_pair, ListExpression) or len(point_pair.elements) == 2 + for point_pair in points.elements + ): + evaluation.message(class_name, "lpn", points) + return + # Scale point values down by Log 10. Tick mark values will be adjusted to be 10^n in GraphicsBox. if self.use_log_scale: points = ListExpression( diff --git a/test/builtin/drawing/test_plot.py b/test/builtin/drawing/test_plot.py index 670ddbaf4..0cc3ad585 100644 --- a/test/builtin/drawing/test_plot.py +++ b/test/builtin/drawing/test_plot.py @@ -3,13 +3,40 @@ Unit tests from mathics.builtin.drawing.plot """ -import sys -import time -from test.helper import check_evaluation, evaluate +from test.helper import check_evaluation import pytest +def test__listplot(): + """tests for module builtin.drawing.plot._ListPlot""" + for str_expr, msgs, str_expected, fail_msg in ( + ( + "ListPlot[5]", + ("5 is not a list of numbers or pairs of numbers.",), + "ListPlot[5]", + "ListPlot with invalid list of point", + ), + ( + "ListLinePlot[{{}, {{1., 1.}}, {{1., 2.}}, {}}]", + ( + "{{}, {{1., 1.}}, {{1., 2.}}, {}} is not a list of numbers or pairs of numbers.", + ), + "ListLinePlot[{{}, {{1., 1.}}, {{1., 2.}}, {}}]", + "ListLinePlot with invalid list of point", + ), + ): + check_evaluation( + str_expr, + str_expected, + to_string_expr=True, + to_string_expected=True, + hold_expected=True, + failure_message=fail_msg, + expected_messages=msgs, + ) + + @pytest.mark.parametrize( ("str_expr", "msgs", "str_expected", "fail_msg"), [ @@ -159,8 +186,8 @@ ), ], ) -def test_private_doctests_plot(str_expr, msgs, str_expected, fail_msg): - """builtin.drawing.plot""" +def test_plot(str_expr, msgs, str_expected, fail_msg): + """tests for module builtin.drawing.plot""" check_evaluation( str_expr, str_expected, diff --git a/test/helper.py b/test/helper.py index 58ebc17f6..6e13f7982 100644 --- a/test/helper.py +++ b/test/helper.py @@ -38,7 +38,7 @@ def evaluate(str_expr: str): def check_evaluation( str_expr: Optional[str], str_expected: Optional[str] = None, - failure_message: str = "", + failure_message: Optional[str] = "", hold_expected: bool = False, to_string_expr: Optional[bool] = True, to_string_expected: bool = True,