Skip to content

Commit cc92b6c

Browse files
authored
Merge pull request #612 from Mathics3/reduce-test_rules_patterns
Put test_rules_patterns tests where they belong
2 parents 42993dd + 9b36991 commit cc92b6c

File tree

3 files changed

+122
-34
lines changed

3 files changed

+122
-34
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Unit tests from mathics.builtin.atomic.symbols.
4+
"""
5+
6+
from test.helper import check_evaluation
7+
8+
9+
def test_downvalues():
10+
for str_expr, str_expected, message in (
11+
(
12+
"DownValues[foo]={x_^2:>y}",
13+
"{x_ ^ 2 :> y}",
14+
"Issue #1251 part 1",
15+
),
16+
(
17+
"PrependTo[DownValues[foo], {x_^3:>z}]",
18+
"{{x_ ^ 3 :> z}, HoldPattern[x_ ^ 2] :> y}",
19+
"Issue #1251 part 2",
20+
),
21+
(
22+
"DownValues[foo]={x_^3:>y}",
23+
"{x_ ^ 3 :> y}",
24+
"Issue #1251 part 3",
25+
),
26+
):
27+
check_evaluation(str_expr, str_expected, message)

test/test_rules_patterns.py renamed to test/builtin/test_attributes.py

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# -*- coding: utf-8 -*-
2-
import os
2+
"""
3+
Unit tests from mathics.builtin.attributes.
4+
"""
35

6+
import os
47
import pytest
58

6-
from .helper import check_evaluation
7-
9+
from test.helper import check_evaluation
810

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

@@ -128,44 +130,75 @@ def test_one_identity_stil_failing(str_expr, str_expected, msg):
128130
)
129131

130132

131-
def test_downvalues():
132-
for str_expr, str_expected, message in (
133+
@pytest.mark.parametrize(
134+
("str_expr", "str_expected", "msg"),
135+
[
136+
(None, None, None),
137+
# F has the attribute, but G doesn't.
138+
("SetAttributes[F, OneIdentity]", "Null", None),
139+
("SetAttributes[r, Flat]", "Null", None),
140+
("SetAttributes[s, Flat]", "Null", None),
141+
("SetAttributes[s, OneIdentity]", "Null", None),
142+
("MatchQ[x, F[y_]]", "False", "With OneIdentity"),
143+
("MatchQ[x, G[y_]]", "False", "Without OneIdentity"),
144+
("MatchQ[x, F[x_:0,y_]]", "True", "With OneIdentity, and Default"),
145+
("MatchQ[x, G[x_:0,y_]]", "False", "Without OneIdentity, and Default"),
146+
("MatchQ[F[x], F[x_:0,y_]]", "True", "With OneIdentity, and Default"),
147+
("MatchQ[G[x], G[x_:0,y_]]", "True", "Without OneIdentity, and Default"),
148+
("MatchQ[F[F[F[x]]], F[x_:0,y_]]", "True", "With OneIdentity, nested"),
149+
("MatchQ[G[G[G[x]]], G[x_:0,y_]]", "True", "Without OneIdentity, nested"),
150+
("MatchQ[F[3, F[F[x]]], F[x_:0,y_]]", "True", "With OneIdentity, nested"),
151+
("MatchQ[G[3, G[G[x]]], G[x_:0,y_]]", "True", "Without OneIdentity, nested"),
133152
(
134-
"DownValues[foo]={x_^2:>y}",
135-
"{x_ ^ 2 :> y}",
136-
"Issue #1251 part 1",
153+
"MatchQ[x, F[x1_:0, F[x2_:0,y_]]]",
154+
"True",
155+
"With OneIdentity, pattern nested",
137156
),
138157
(
139-
"PrependTo[DownValues[foo], {x_^3:>z}]",
140-
"{{x_ ^ 3 :> z}, HoldPattern[x_ ^ 2] :> y}",
141-
"Issue #1251 part 2",
158+
"MatchQ[x, G[x1_:0, G[x2_:0,y_]]]",
159+
"False",
160+
"With OneIdentity, pattern nested",
142161
),
143162
(
144-
"DownValues[foo]={x_^3:>y}",
145-
"{x_ ^ 3 :> y}",
146-
"Issue #1251 part 3",
163+
"MatchQ[x, F[x1___:0, F[x2_:0,y_]]]",
164+
"True",
165+
"With OneIdentity, pattern nested",
147166
),
148-
):
149-
check_evaluation(str_expr, str_expected, message)
150-
151-
152-
def test_blank():
153-
for str_expr, str_expected, message in (
154167
(
155-
"g[i] /. _[i] :> a",
156-
"a",
157-
"Issue #203",
168+
"MatchQ[x, G[x1___:0, G[x2_:0,y_]]]",
169+
"False",
170+
"With OneIdentity, pattern nested",
158171
),
159-
):
160-
check_evaluation(str_expr, str_expected, message)
161-
162-
163-
def test_complex_rule():
164-
for str_expr, str_expected, message in (
172+
("MatchQ[x, F[F[x2_:0,y_],x1_:0]]", "True", "With OneIdentity, pattern nested"),
165173
(
166-
"a == d b + d c /. a_ x_ + a_ y_ -> a (x + y)",
167-
"a == (b + c) d",
168-
"Issue #212",
174+
"MatchQ[x, G[G[x2_:0,y_],x1_:0]]",
175+
"False",
176+
"With OneIdentity, pattern nested",
169177
),
170-
):
171-
check_evaluation(str_expr, str_expected, message)
178+
("MatchQ[x, F[x_.,y_]]", "False", "With OneIdentity, and Optional, no default"),
179+
(
180+
"MatchQ[x, G[x_.,y_]]",
181+
"False",
182+
"Without OneIdentity, and Optional, no default",
183+
),
184+
("Default[F, 1]=1.", "1.", None),
185+
("Default[G, 1]=2.", "2.", None),
186+
("MatchQ[x, F[x_.,y_]]", "True", "With OneIdentity, and Optional, default"),
187+
("MatchQ[x, G[x_.,y_]]", "False", "Without OneIdentity, and Optional, default"),
188+
("MatchQ[F[F[H[y]]],F[x_:0,u_H]]", "False", None),
189+
("MatchQ[G[G[H[y]]],G[x_:0,u_H]]", "False", None),
190+
("MatchQ[F[p, F[p, H[y]]],F[x_:0,u_H]]", "False", None),
191+
("MatchQ[G[p, G[p, H[y]]],G[x_:0,u_H]]", "False", None),
192+
(None, None, None),
193+
],
194+
)
195+
@skip_or_fail
196+
def test_one_identity_stil_failing(str_expr, str_expected, msg):
197+
check_evaluation(
198+
str_expr,
199+
str_expected,
200+
to_string_expr=True,
201+
to_string_expected=True,
202+
hold_expected=True,
203+
failure_message=msg,
204+
)

test/builtin/test_patterns.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Unit tests from mathics.builtin.patterns.
4+
"""
5+
6+
from test.helper import check_evaluation
7+
8+
9+
def test_blank():
10+
for str_expr, str_expected, message in (
11+
(
12+
"g[i] /. _[i] :> a",
13+
"a",
14+
"Issue #203",
15+
),
16+
):
17+
check_evaluation(str_expr, str_expected, message)
18+
19+
20+
def test_replace_all():
21+
for str_expr, str_expected, message in (
22+
(
23+
"a == d b + d c /. a_ x_ + a_ y_ -> a (x + y)",
24+
"a == (b + c) d",
25+
"Issue #212",
26+
),
27+
):
28+
check_evaluation(str_expr, str_expected, message)

0 commit comments

Comments
 (0)