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 dde65d8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
17 changes: 16 additions & 1 deletion mathics/builtin/drawing/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,12 @@ class _ListPlot(Builtin, ABC):
attributes = A_PROTECTED | A_READ_PROTECTED

messages = {
"joind": "Value of option Joined -> `1` is not True or False.",
"lpn": "`1` is not a list of numbers or pairs of numbers.",
"prng": (
"Value of option PlotRange -> `1` is not All, Automatic or "
"an appropriate list of range specifications."
),
"joind": "Value of option Joined -> `1` is not True or False.",
}

use_log_scale = False
Expand All @@ -300,6 +301,20 @@ 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(
element.is_numeric(evaluation)
or isinstance(element, ListExpression)
or (1 <= len(element.elements) <= 2)
or (len(element.elements) == 1 and isinstance(element[0], ListExpression))
for element 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 dde65d8

Please sign in to comment.