Skip to content

Commit

Permalink
Add "lpn" checking in _ListPlot
Browse files Browse the repository at this point in the history
"lpn" is "list of points" checking on ListPlot[] and ListLinePlot[]
  • Loading branch information
rocky committed Dec 25, 2024
1 parent 8ca7514 commit 5d74162
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
12 changes: 12 additions & 0 deletions mathics/builtin/drawing/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
37 changes: 32 additions & 5 deletions test/builtin/drawing/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
[
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5d74162

Please sign in to comment.