Skip to content

Commit e91e275

Browse files
authored
Simplfy invalid-argumnent-count pytest testing (#1488)
There was way too much verbiage and clutter around running pytests to check that we are giving a message when calling Mathics3 Builtin Function with the wrong number of arguments. Also, in pytest -s output, show the failure reason. This makes it easier to find an error in the pytest file.
1 parent 1ea1bea commit e91e275

File tree

8 files changed

+81
-129
lines changed

8 files changed

+81
-129
lines changed

test/builtin/arithmetic/test_basic.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
Unit tests for mathics.builtins.arithmetic.basic
44
"""
5-
from test.helper import check_evaluation
5+
from test.helper import check_evaluation, check_wrong_number_of_arguments
66

77
import pytest
88

@@ -493,9 +493,8 @@ def test_private_doctests_arithmetic(str_expr, msgs, str_expected, fail_msg):
493493
)
494494

495495

496-
@pytest.mark.parametrize(
497-
("str_expr", "msgs", "assert_fail_msg"),
498-
[
496+
def test_wrong_number_of_arguments():
497+
tests = [
499498
(
500499
"CubeRoot[a, b]",
501500
["CubeRoot called with 2 arguments; 1 argument is expected."],
@@ -536,16 +535,5 @@ def test_private_doctests_arithmetic(str_expr, msgs, str_expected, fail_msg):
536535
["Subtract called with 1 argument; 2 arguments are expected."],
537536
"Subtract argrx error call with 1 argument",
538537
),
539-
],
540-
)
541-
def test_wrong_number_of_arguments(str_expr, msgs, assert_fail_msg):
542-
""" """
543-
check_evaluation(
544-
str_expr,
545-
str_expr,
546-
to_string_expr=True,
547-
to_string_expected=True,
548-
hold_expected=True,
549-
failure_message=assert_fail_msg,
550-
expected_messages=msgs,
551-
)
538+
]
539+
check_wrong_number_of_arguments(tests)

test/builtin/atomic/test_strings.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
In particular, Alphabet
66
"""
7-
from test.helper import check_evaluation
7+
from test.helper import check_evaluation, check_wrong_number_of_arguments
88

99
import pytest
1010

@@ -120,26 +120,14 @@ def test_string(str_expr, warnings, str_expected):
120120
)
121121

122122

123-
@pytest.mark.parametrize(
124-
("str_expr", "msgs", "assert_fail_msg"),
125-
[
123+
def test_wrong_number_of_arguments():
124+
tests = [
126125
(
127126
"ToExpression[]",
128-
(
129-
"ToExpression called with 0 arguments; between 1 and 3 arguments are expected.",
130-
),
127+
[
128+
"ToExpression called with 0 arguments; between 1 and 3 arguments are expected."
129+
],
131130
"ToExpression argument error call",
132-
),
133-
],
134-
)
135-
def test_wrong_number_of_arguments(str_expr, msgs, assert_fail_msg):
136-
""" """
137-
check_evaluation(
138-
str_expr,
139-
str_expr,
140-
to_string_expr=True,
141-
to_string_expected=True,
142-
hold_expected=True,
143-
failure_message=assert_fail_msg,
144-
expected_messages=msgs,
145-
)
131+
)
132+
]
133+
check_wrong_number_of_arguments(tests)

test/builtin/specialfns/test_elliptic.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
"""
33
Unit tests for mathics.builtins.specialfns.elliptic
44
"""
5-
from test.helper import check_evaluation
5+
from test.helper import check_wrong_number_of_arguments
66

7-
import pytest
87

9-
10-
@pytest.mark.parametrize(
11-
("str_expr", "msgs", "assert_fail_msg"),
12-
[
8+
def test_wrong_number_of_arguments():
9+
tests = [
1310
(
1411
"EllipticE[]",
1512
["EllipticE called with 0 arguments; 1 or 2 arguments are expected."],
@@ -45,16 +42,5 @@
4542
["EllipticPi called with 0 arguments; 2 or 3 arguments are expected."],
4643
"EllipticPi error call with too many arguments; 'argt' tag",
4744
),
48-
],
49-
)
50-
def test_wrong_number_of_arguments(str_expr, msgs, assert_fail_msg):
51-
""" """
52-
check_evaluation(
53-
str_expr,
54-
str_expr,
55-
to_string_expr=True,
56-
to_string_expected=True,
57-
hold_expected=True,
58-
failure_message=assert_fail_msg,
59-
expected_messages=msgs,
60-
)
45+
]
46+
check_wrong_number_of_arguments(tests)

test/builtin/test_numeric.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
In particular, Rationalize and RealValuNumberQ
66
"""
7-
from test.helper import check_evaluation
7+
from test.helper import check_evaluation, check_wrong_number_of_arguments
88

99
import pytest
1010

@@ -121,7 +121,7 @@ def test_realvalued():
121121
('Sign["20"]', None, "Sign[20]", None),
122122
],
123123
)
124-
def test_private_doctests_numeric(str_expr, msgs, str_expected, fail_msg):
124+
def test_numeric(str_expr, msgs, str_expected, fail_msg):
125125
""" """
126126
check_evaluation(
127127
str_expr,
@@ -135,36 +135,23 @@ def test_private_doctests_numeric(str_expr, msgs, str_expected, fail_msg):
135135

136136

137137
@pytest.mark.parametrize(
138-
("str_expr", "msgs", "assert_fail_msg"),
138+
("str_expr", "assert_fail_msg"),
139139
[
140140
(
141141
"Round[a, b]",
142-
None,
143142
"Round with one symbolic argument should not give an error message",
144143
),
145144
(
146145
"Round[a, b]",
147-
None,
148146
"Round with two symbolic arguments should not give an error message",
149147
),
150-
(
151-
"Round[a, b, c]",
152-
("Round called with 3 arguments; 1 or 2 arguments are expected.",),
153-
"Round wrong number of arguments",
154-
),
155148
(
156149
"Sign[x]",
157-
None,
158150
"Sign with one symbolic argument should not give an error message",
159151
),
160-
(
161-
"Sign[4, 5, 6]",
162-
("Sign called with 3 arguments; 1 argument is expected.",),
163-
"Sign wrong number of arguments",
164-
),
165152
],
166153
)
167-
def test_wrong_number_of_arguments(str_expr, msgs, assert_fail_msg):
154+
def test_right_number_of_arguments(str_expr, assert_fail_msg):
168155
""" """
169156
check_evaluation(
170157
str_expr,
@@ -173,5 +160,21 @@ def test_wrong_number_of_arguments(str_expr, msgs, assert_fail_msg):
173160
to_string_expected=True,
174161
hold_expected=True,
175162
failure_message=assert_fail_msg,
176-
expected_messages=msgs,
163+
expected_messages=None,
177164
)
165+
166+
167+
def test_wrong_number_of_arguments():
168+
tests = [
169+
(
170+
"Round[a, b, c]",
171+
["Round called with 3 arguments; 1 or 2 arguments are expected."],
172+
"Round wrong number of arguments",
173+
),
174+
(
175+
"Sign[4, 5, 6]",
176+
["Sign called with 3 arguments; 1 argument is expected."],
177+
"Sign wrong number of arguments",
178+
),
179+
]
180+
check_wrong_number_of_arguments(tests)

test/builtin/test_procedural.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
Unit tests from mathics.builtin.procedural.
44
"""
55

6-
from test.helper import check_evaluation, check_evaluation_as_in_cli, session
6+
from test.helper import (
7+
check_evaluation,
8+
check_evaluation_as_in_cli,
9+
check_wrong_number_of_arguments,
10+
session,
11+
)
712

813
import pytest
914

@@ -152,9 +157,8 @@ def eval_expr(expr_str):
152157
session.evaluation.stopped = False
153158

154159

155-
@pytest.mark.parametrize(
156-
("str_expr", "msgs", "assert_fail_msg"),
157-
[
160+
def test_wrong_number_of_arguments():
161+
tests = [
158162
# (
159163
# "Abort[a, b]",
160164
# ["Abort called with 2 arguments; 0 arguments are expected."],
@@ -186,16 +190,5 @@ def eval_expr(expr_str):
186190
["While called with 3 arguments; 1 or 2 arguments are expected."],
187191
"While argument error call",
188192
),
189-
],
190-
)
191-
def test_wrong_number_of_arguments(str_expr, msgs, assert_fail_msg):
192-
""" """
193-
check_evaluation(
194-
str_expr,
195-
str_expr,
196-
to_string_expr=True,
197-
to_string_expected=True,
198-
hold_expected=True,
199-
failure_message=assert_fail_msg,
200-
expected_messages=msgs,
201-
)
193+
]
194+
check_wrong_number_of_arguments(tests)

test/builtin/test_quantities.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
In particular, Rationalize and RealValuNumberQ
66
"""
77

8-
from test.helper import check_evaluation
8+
from test.helper import check_evaluation, check_wrong_number_of_arguments
99

1010
import pytest
1111

@@ -180,9 +180,8 @@ def test_quantity_operations(str_expr, str_expected):
180180
)
181181

182182

183-
@pytest.mark.parametrize(
184-
("str_expr", "msgs", "assert_fail_msg"),
185-
[
183+
def test_wrong_number_of_arguments():
184+
tests = [
186185
(
187186
"QuantityQ[a, b]",
188187
["QuantityQ called with 2 arguments; 1 argument is expected."],
@@ -193,16 +192,5 @@ def test_quantity_operations(str_expr, str_expected):
193192
["Quantity called with 0 arguments; 1 argument is expected."],
194193
"Quantity called with wrong number of arguments",
195194
),
196-
],
197-
)
198-
def test_wrong_number_of_arguments(str_expr, msgs, assert_fail_msg):
199-
""" """
200-
check_evaluation(
201-
str_expr,
202-
str_expr,
203-
to_string_expr=True,
204-
to_string_expected=True,
205-
hold_expected=True,
206-
failure_message=assert_fail_msg,
207-
expected_messages=msgs,
208-
)
195+
]
196+
check_wrong_number_of_arguments(tests)

test/builtin/vectors/test_mathops.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
In particular, Curl[] and Norm[].
66
"""
77

8-
from test.helper import check_evaluation
8+
from test.helper import check_evaluation, check_wrong_number_of_arguments
99

1010
import pytest
1111

@@ -52,9 +52,8 @@ def test_norm():
5252
check_evaluation(str_expr=str_expr, str_expected=str_expected)
5353

5454

55-
@pytest.mark.parametrize(
56-
("str_expr", "msgs", "assert_fail_msg"),
57-
[
55+
def test_wrong_number_of_arguments():
56+
tests = [
5857
(
5958
"Curl[a]",
6059
["Curl called with 1 argument; 2 or 3 arguments are expected."],
@@ -65,16 +64,5 @@ def test_norm():
6564
["Curl called with 4 arguments; 2 or 3 arguments are expected."],
6665
"Curl with more than three arguments",
6766
),
68-
],
69-
)
70-
def test_wrong_number_of_arguments(str_expr, msgs, assert_fail_msg):
71-
""" """
72-
check_evaluation(
73-
str_expr,
74-
str_expr,
75-
to_string_expr=True,
76-
to_string_expected=True,
77-
hold_expected=True,
78-
failure_message=assert_fail_msg,
79-
expected_messages=msgs,
80-
)
67+
]
68+
check_wrong_number_of_arguments(tests)

test/helper.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os.path as osp
33
import re
44
import time
5-
from typing import Optional
5+
from typing import List, Optional, Tuple
66

77
from mathics.core.load_builtin import import_and_load_builtins
88
from mathics.session import MathicsSession
@@ -126,10 +126,10 @@ def check_evaluation(
126126

127127
print(time.asctime())
128128
if failure_message:
129-
print((result, expected))
129+
print(f"got: {result}, expect: {expected} -- {failure_message}")
130130
assert result == expected, failure_message
131131
else:
132-
print((result, expected))
132+
print(f"got: {result}, expect: {expected}")
133133
if isinstance(expected, re.Pattern):
134134
assert expected.match(result)
135135
else:
@@ -176,3 +176,21 @@ def check_evaluation_as_in_cli(
176176
if failure_message:
177177
assert res.result == str_expected, failure_message
178178
assert res.result == str_expected
179+
180+
181+
# List below could be a Tuple, but List looks better in the tests
182+
def check_wrong_number_of_arguments(tests: List[Tuple[str, List[str], str]]):
183+
"""
184+
Boilerplate code to check that for giving an error message when the wrong
185+
number of arguments is provided.
186+
"""
187+
for str_expr, msgs, assert_fail_msg in tests:
188+
check_evaluation(
189+
str_expr,
190+
str_expr,
191+
to_string_expr=True,
192+
to_string_expected=True,
193+
hold_expected=True,
194+
failure_message=assert_fail_msg,
195+
expected_messages=msgs,
196+
)

0 commit comments

Comments
 (0)