Skip to content

Commit bb7c672

Browse files
authored
move private doctest to pytest for evaluation, procedural and system (#911)
another round
1 parent 14b8f21 commit bb7c672

File tree

10 files changed

+247
-127
lines changed

10 files changed

+247
-127
lines changed

mathics/builtin/evaluation.py

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,6 @@ class RecursionLimit(Predefined):
3838
>> a = a + a
3939
: Recursion depth of 512 exceeded.
4040
= $Aborted
41-
42-
#> $RecursionLimit = 20
43-
= 20
44-
#> a = a + a
45-
: Recursion depth of 20 exceeded.
46-
= $Aborted
47-
48-
#> $RecursionLimit = 200
49-
= 200
50-
51-
#> ClearAll[f];
52-
#> f[x_, 0] := x; f[x_, n_] := f[x + 1, n - 1];
53-
#> Block[{$RecursionLimit = 20}, f[0, 100]]
54-
= 100
55-
#> ClearAll[f];
56-
57-
#> ClearAll[f];
58-
#> f[x_, 0] := x; f[x_, n_] := Module[{y = x + 1}, f[y, n - 1]];
59-
#> Block[{$RecursionLimit = 20}, f[0, 100]]
60-
: Recursion depth of 20 exceeded.
61-
= $Aborted
62-
#> ClearAll[f];
6341
"""
6442

6543
name = "$RecursionLimit"
@@ -105,28 +83,6 @@ class IterationLimit(Predefined):
10583
10684
> $IterationLimit
10785
= 1000
108-
#> ClearAll[f]; f[x_] := f[x + 1];
109-
#> f[x]
110-
: Iteration limit of 1000 exceeded.
111-
= $Aborted
112-
#> ClearAll[f];
113-
114-
#> $IterationLimit = x;
115-
: Cannot set $IterationLimit to x; value must be an integer between 20 and Infinity.
116-
117-
#> ClearAll[f];
118-
#> f[x_, 0] := x; f[x_, n_] := f[x + 1, n - 1];
119-
#> Block[{$IterationLimit = 20}, f[0, 100]]
120-
: Iteration limit of 20 exceeded.
121-
= $Aborted
122-
#> ClearAll[f];
123-
124-
# FIX Later
125-
# #> ClearAll[f];
126-
# #> f[x_, 0] := x; f[x_, n_] := Module[{y = x + 1}, f[y, n - 1]];
127-
# #> Block[{$IterationLimit = 20}, f[0, 100]]
128-
# = 100
129-
# #> ClearAll[f];
13086
"""
13187

13288
name = "$IterationLimit"
@@ -280,10 +236,6 @@ class Unevaluated(Builtin):
280236
>> g[Unevaluated[Sequence[a, b, c]]]
281237
= g[Unevaluated[Sequence[a, b, c]]]
282238
283-
#> Attributes[h] = Flat;
284-
#> h[items___] := Plus[items]
285-
#> h[1, Unevaluated[Sequence[Unevaluated[2], 3]], Sequence[4, Unevaluated[5]]]
286-
= 15
287239
"""
288240

289241
attributes = A_HOLD_ALL_COMPLETE | A_PROTECTED

mathics/builtin/procedural.py

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -165,39 +165,6 @@ class CompoundExpression(BinaryOperator):
165165
= d
166166
If the last argument is omitted, 'Null' is taken:
167167
>> a;
168-
169-
## Parser Tests
170-
#> FullForm[Hold[; a]]
171-
: "FullForm[Hold[" cannot be followed by "; a]]" (line 1 of "<test>").
172-
#> FullForm[Hold[; a ;]]
173-
: "FullForm[Hold[" cannot be followed by "; a ;]]" (line 1 of "<test>").
174-
175-
## Issue331
176-
#> CompoundExpression[x, y, z]
177-
= z
178-
#> %
179-
= z
180-
181-
#> CompoundExpression[x, y, Null]
182-
#> %
183-
= y
184-
185-
#> CompoundExpression[CompoundExpression[x, y, Null], Null]
186-
#> %
187-
= y
188-
189-
#> CompoundExpression[x, Null, Null]
190-
#> %
191-
= x
192-
193-
#> CompoundExpression[]
194-
#> %
195-
196-
## Issue 531
197-
#> z = Max[1, 1 + x]; x = 2; z
198-
= 3
199-
200-
#> Clear[x]; Clear[z]
201168
"""
202169

203170
attributes = A_HOLD_ALL | A_PROTECTED | A_READ_PROTECTED
@@ -294,10 +261,6 @@ class Do(IterationFunction):
294261
| 5
295262
| 7
296263
| 9
297-
298-
#> Do[Print["hi"],{1+1}]
299-
| hi
300-
| hi
301264
"""
302265

303266
allow_loopcontrol = True
@@ -330,12 +293,6 @@ class For(Builtin):
330293
= 3628800
331294
>> n == 10!
332295
= True
333-
334-
#> n := 1
335-
#> For[i=1, i<=10, i=i+1, If[i > 5, Return[i]]; n = n * i]
336-
= 6
337-
#> n
338-
= 120
339296
"""
340297

341298
attributes = A_HOLD_REST | A_PROTECTED
@@ -473,17 +430,6 @@ class Return(Builtin):
473430
>> g[x_] := (Do[If[x < 0, Return[0]], {i, {2, 1, 0, -1}}]; x)
474431
>> g[-1]
475432
= -1
476-
477-
#> h[x_] := (If[x < 0, Return[]]; x)
478-
#> h[1]
479-
= 1
480-
#> h[-1]
481-
482-
## Issue 513
483-
#> f[x_] := Return[x];
484-
#> g[y_] := Module[{}, z = f[y]; 2]
485-
#> g[1]
486-
= 2
487433
"""
488434

489435
rules = {
@@ -518,16 +464,6 @@ class Switch(Builtin):
518464
>> Switch[2, 1]
519465
: Switch called with 2 arguments. Switch must be called with an odd number of arguments.
520466
= Switch[2, 1]
521-
522-
#> a; Switch[b, b]
523-
: Switch called with 2 arguments. Switch must be called with an odd number of arguments.
524-
= Switch[b, b]
525-
526-
## Issue 531
527-
#> z = Switch[b, b];
528-
: Switch called with 2 arguments. Switch must be called with an odd number of arguments.
529-
#> z
530-
= Switch[b, b]
531467
"""
532468

533469
summary_text = "switch based on a value, with patterns allowed"
@@ -636,9 +572,6 @@ class While(Builtin):
636572
>> While[b != 0, {a, b} = {b, Mod[a, b]}];
637573
>> a
638574
= 3
639-
640-
#> i = 1; While[True, If[i^2 > 100, Return[i + 1], i++]]
641-
= 12
642575
"""
643576

644577
summary_text = "evaluate an expression while a criterion is true"

mathics/builtin/system.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ class Packages(Predefined):
174174
175175
X> $Packages
176176
= {ImportExport`,XML`,Internal`,System`,Global`}
177-
#> MemberQ[$Packages, "System`"]
178-
= True
179177
"""
180178

181179
summary_text = "list the packages loaded in the current session"
@@ -197,8 +195,6 @@ class ParentProcessID(Predefined):
197195
>> $ParentProcessID
198196
= ...
199197
200-
#> Head[$ParentProcessID] == Integer
201-
= True
202198
"""
203199
summary_text = "id of the process that invoked Mathics"
204200
name = "$ParentProcessID"
@@ -218,9 +214,6 @@ class ProcessID(Predefined):
218214
219215
>> $ProcessID
220216
= ...
221-
222-
#> Head[$ProcessID] == Integer
223-
= True
224217
"""
225218
summary_text = "id of the Mathics process"
226219
name = "$ProcessID"
@@ -348,9 +341,6 @@ class SystemWordLength(Predefined):
348341
</dl>
349342
X> $SystemWordLength
350343
= 64
351-
352-
#> Head[$SystemWordLength] == Integer
353-
= True
354344
"""
355345
summary_text = "word length of computer system"
356346
name = "$SystemWordLength"

test/builtin/test_evalution.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Unit tests from mathics.builtin.evaluation.
4+
"""
5+
6+
7+
from test.helper import check_evaluation, session
8+
9+
import pytest
10+
11+
12+
@pytest.mark.parametrize(
13+
("str_expr", "msgs", "str_expected", "fail_msg"),
14+
[
15+
("ClearAll[a];$RecursionLimit = 20", None, "20", None),
16+
("a = a + a", ("Recursion depth of 20 exceeded.",), "$Aborted", None),
17+
("$RecursionLimit = 200", None, "200", None),
18+
(
19+
"ClearAll[f];f[x_, 0] := x; f[x_, n_] := f[x + 1, n - 1];Block[{$RecursionLimit = 20}, f[0, 100]]",
20+
None,
21+
"100",
22+
None,
23+
),
24+
(
25+
"ClearAll[f];f[x_, 0] := x; f[x_, n_] := Module[{y = x + 1}, f[y, n - 1]];Block[{$RecursionLimit = 20}, f[0, 100]]",
26+
("Recursion depth of 20 exceeded.",),
27+
"$Aborted",
28+
None,
29+
),
30+
(
31+
"ClearAll[f]; f[x_] := f[x + 1];f[x]",
32+
("Iteration limit of 1000 exceeded.",),
33+
"$Aborted",
34+
None,
35+
),
36+
(
37+
"$IterationLimit = x;",
38+
(
39+
"Cannot set $IterationLimit to x; value must be an integer between 20 and Infinity.",
40+
),
41+
None,
42+
None,
43+
),
44+
(
45+
"ClearAll[f];f[x_, 0] := x; f[x_, n_] := f[x + 1, n - 1];Block[{$IterationLimit = 20}, f[0, 100]]",
46+
("Iteration limit of 20 exceeded.",),
47+
"$Aborted",
48+
None,
49+
),
50+
("ClearAll[f];", None, None, None),
51+
(
52+
"Attributes[h] = Flat;h[items___] := Plus[items];h[1, Unevaluated[Sequence[Unevaluated[2], 3]], Sequence[4, Unevaluated[5]]]",
53+
None,
54+
"15",
55+
None,
56+
),
57+
# FIX Later
58+
(
59+
"ClearAll[f];f[x_, 0] := x; f[x_, n_] := Module[{y = x + 1}, f[y, n - 1]];Block[{$IterationLimit = 20}, f[0, 100]]",
60+
None,
61+
"100",
62+
"Fix me!",
63+
),
64+
("ClearAll[f];", None, None, None),
65+
],
66+
)
67+
def test_private_doctests_evaluation(str_expr, msgs, str_expected, fail_msg):
68+
"""These tests check the behavior of $RecursionLimit and $IterationLimit"""
69+
70+
# Here we do not use the session object to check the messages
71+
# produced by the exceptions. If $RecursionLimit / $IterationLimit
72+
# are reached during the evaluation using a MathicsSession object,
73+
# an exception is raised. On the other hand, using the `Evaluation.evaluate`
74+
# method, the exception is handled.
75+
#
76+
# TODO: Maybe it makes sense to clone this exception handling in
77+
# the check_evaluation function.
78+
#
79+
def eval_expr(expr_str):
80+
query = session.evaluation.parse(expr_str)
81+
res = session.evaluation.evaluate(query)
82+
session.evaluation.stopped = False
83+
return res
84+
85+
res = eval_expr(str_expr)
86+
if msgs is None:
87+
assert len(res.out) == 0
88+
else:
89+
assert len(res.out) == len(msgs)
90+
for li1, li2 in zip(res.out, msgs):
91+
assert li1.text == li2
92+
93+
assert res.result == str_expected

test/builtin/test_forms.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# -*- coding: utf-8 -*-
2+
"""
3+
Unit tests from mathics.builtin.forms.
4+
"""
25

36
from test.helper import check_evaluation, session
47

0 commit comments

Comments
 (0)