Skip to content

Commit

Permalink
Put test_rules_patterns tests where they belong
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Nov 13, 2022
1 parent d33ca93 commit 318a2b8
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 34 deletions.
27 changes: 27 additions & 0 deletions test/builtin/atomic/test_symbols.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
"""
Unit tests from mathics.builtin.atomic.symbols.
"""

from test.helper import check_evaluation


def test_downvalues():
for str_expr, str_expected, message in (
(
"DownValues[foo]={x_^2:>y}",
"{x_ ^ 2 :> y}",
"Issue #1251 part 1",
),
(
"PrependTo[DownValues[foo], {x_^3:>z}]",
"{{x_ ^ 3 :> z}, HoldPattern[x_ ^ 2] :> y}",
"Issue #1251 part 2",
),
(
"DownValues[foo]={x_^3:>y}",
"{x_ ^ 3 :> y}",
"Issue #1251 part 3",
),
):
check_evaluation(str_expr, str_expected, message)
101 changes: 67 additions & 34 deletions test/test_rules_patterns.py → test/builtin/test_attributes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-
import os
"""
Unit tests from mathics.builtin.attributes.
"""

import os
import pytest

from .helper import check_evaluation

from test.helper import check_evaluation

DEBUGRULESPAT = int(os.environ.get("DEBUGRULESPAT", "0")) == 1

Expand Down Expand Up @@ -128,44 +130,75 @@ def test_one_identity_stil_failing(str_expr, str_expected, msg):
)


def test_downvalues():
for str_expr, str_expected, message in (
@pytest.mark.parametrize(
("str_expr", "str_expected", "msg"),
[
(None, None, None),
# F has the attribute, but G doesn't.
("SetAttributes[F, OneIdentity]", "Null", None),
("SetAttributes[r, Flat]", "Null", None),
("SetAttributes[s, Flat]", "Null", None),
("SetAttributes[s, OneIdentity]", "Null", None),
("MatchQ[x, F[y_]]", "False", "With OneIdentity"),
("MatchQ[x, G[y_]]", "False", "Without OneIdentity"),
("MatchQ[x, F[x_:0,y_]]", "True", "With OneIdentity, and Default"),
("MatchQ[x, G[x_:0,y_]]", "False", "Without OneIdentity, and Default"),
("MatchQ[F[x], F[x_:0,y_]]", "True", "With OneIdentity, and Default"),
("MatchQ[G[x], G[x_:0,y_]]", "True", "Without OneIdentity, and Default"),
("MatchQ[F[F[F[x]]], F[x_:0,y_]]", "True", "With OneIdentity, nested"),
("MatchQ[G[G[G[x]]], G[x_:0,y_]]", "True", "Without OneIdentity, nested"),
("MatchQ[F[3, F[F[x]]], F[x_:0,y_]]", "True", "With OneIdentity, nested"),
("MatchQ[G[3, G[G[x]]], G[x_:0,y_]]", "True", "Without OneIdentity, nested"),
(
"DownValues[foo]={x_^2:>y}",
"{x_ ^ 2 :> y}",
"Issue #1251 part 1",
"MatchQ[x, F[x1_:0, F[x2_:0,y_]]]",
"True",
"With OneIdentity, pattern nested",
),
(
"PrependTo[DownValues[foo], {x_^3:>z}]",
"{{x_ ^ 3 :> z}, HoldPattern[x_ ^ 2] :> y}",
"Issue #1251 part 2",
"MatchQ[x, G[x1_:0, G[x2_:0,y_]]]",
"False",
"With OneIdentity, pattern nested",
),
(
"DownValues[foo]={x_^3:>y}",
"{x_ ^ 3 :> y}",
"Issue #1251 part 3",
"MatchQ[x, F[x1___:0, F[x2_:0,y_]]]",
"True",
"With OneIdentity, pattern nested",
),
):
check_evaluation(str_expr, str_expected, message)


def test_blank():
for str_expr, str_expected, message in (
(
"g[i] /. _[i] :> a",
"a",
"Issue #203",
"MatchQ[x, G[x1___:0, G[x2_:0,y_]]]",
"False",
"With OneIdentity, pattern nested",
),
):
check_evaluation(str_expr, str_expected, message)


def test_complex_rule():
for str_expr, str_expected, message in (
("MatchQ[x, F[F[x2_:0,y_],x1_:0]]", "True", "With OneIdentity, pattern nested"),
(
"a == d b + d c /. a_ x_ + a_ y_ -> a (x + y)",
"a == (b + c) d",
"Issue #212",
"MatchQ[x, G[G[x2_:0,y_],x1_:0]]",
"False",
"With OneIdentity, pattern nested",
),
):
check_evaluation(str_expr, str_expected, message)
("MatchQ[x, F[x_.,y_]]", "False", "With OneIdentity, and Optional, no default"),
(
"MatchQ[x, G[x_.,y_]]",
"False",
"Without OneIdentity, and Optional, no default",
),
("Default[F, 1]=1.", "1.", None),
("Default[G, 1]=2.", "2.", None),
("MatchQ[x, F[x_.,y_]]", "True", "With OneIdentity, and Optional, default"),
("MatchQ[x, G[x_.,y_]]", "False", "Without OneIdentity, and Optional, default"),
("MatchQ[F[F[H[y]]],F[x_:0,u_H]]", "False", None),
("MatchQ[G[G[H[y]]],G[x_:0,u_H]]", "False", None),
("MatchQ[F[p, F[p, H[y]]],F[x_:0,u_H]]", "False", None),
("MatchQ[G[p, G[p, H[y]]],G[x_:0,u_H]]", "False", None),
(None, None, None),
],
)
@skip_or_fail
def test_one_identity_stil_failing(str_expr, str_expected, msg):
check_evaluation(
str_expr,
str_expected,
to_string_expr=True,
to_string_expected=True,
hold_expected=True,
failure_message=msg,
)
28 changes: 28 additions & 0 deletions test/builtin/test_patterns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
"""
Unit tests from mathics.builtin.patterns.
"""

from test.helper import check_evaluation


def test_blank():
for str_expr, str_expected, message in (
(
"g[i] /. _[i] :> a",
"a",
"Issue #203",
),
):
check_evaluation(str_expr, str_expected, message)


def test_replace_all():
for str_expr, str_expected, message in (
(
"a == d b + d c /. a_ x_ + a_ y_ -> a (x + y)",
"a == (b + c) d",
"Issue #212",
),
):
check_evaluation(str_expr, str_expected, message)

0 comments on commit 318a2b8

Please sign in to comment.