|
1 | 1 | # -*- coding: utf-8 -*- |
2 | | -import os |
| 2 | +""" |
| 3 | +Unit tests from mathics.builtin.attributes. |
| 4 | +""" |
3 | 5 |
|
| 6 | +import os |
4 | 7 | import pytest |
5 | 8 |
|
6 | | -from .helper import check_evaluation |
7 | | - |
| 9 | +from test.helper import check_evaluation |
8 | 10 |
|
9 | 11 | DEBUGRULESPAT = int(os.environ.get("DEBUGRULESPAT", "0")) == 1 |
10 | 12 |
|
@@ -128,44 +130,75 @@ def test_one_identity_stil_failing(str_expr, str_expected, msg): |
128 | 130 | ) |
129 | 131 |
|
130 | 132 |
|
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"), |
133 | 152 | ( |
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", |
137 | 156 | ), |
138 | 157 | ( |
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", |
142 | 161 | ), |
143 | 162 | ( |
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", |
147 | 166 | ), |
148 | | - ): |
149 | | - check_evaluation(str_expr, str_expected, message) |
150 | | - |
151 | | - |
152 | | -def test_blank(): |
153 | | - for str_expr, str_expected, message in ( |
154 | 167 | ( |
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", |
158 | 171 | ), |
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"), |
165 | 173 | ( |
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", |
169 | 177 | ), |
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 | + ) |
0 commit comments