Skip to content

Commit f4686d0

Browse files
committed
More tests, More doc, less duplication
1 parent c9d931d commit f4686d0

File tree

5 files changed

+16
-18
lines changed

5 files changed

+16
-18
lines changed

mathics/builtin/scoping.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,26 @@ class With(Builtin):
6060
<dl>
6161
6262
<dt>'With[{$x$=$x0$, $y$=$y0$, ...}, $expr$]'
63-
<dd>specifies that all occurrences of the symbols $x$, $y$, ... in $expr$ should be replaced by $x0$, $y0$, ...
63+
<dd>specifies that all occurrences of the symbols $x$, $y$, ... in $expr$ should be replaced by $x0$, $y0$, ...
6464
</dl>
6565
6666
>> n = 10
6767
= 10
68+
69+
Evaluate an expression with x locally set to 5:
6870
>> With[{n = 5}, n ^ 2]
6971
= 25
7072
>> n
7173
= 10
74+
75+
'With' works even without evaluation:
76+
>> With[{x = a}, (1 + x^2) &]
77+
= 1 + a ^ 2&
78+
79+
Use 'With' to insert values into held expressions
7280
>> With[{x=y}, Hold[x]]
7381
= Hold[y]
82+
7483
>> Table[With[{i=j}, Hold[i]],{j,1,4}]
7584
= {Hold[1], Hold[2], Hold[3], Hold[4]}
7685
>> x=5; With[{x=x}, Hold[x]]
@@ -624,7 +633,7 @@ class BeginPackage(Builtin):
624633
System`Private`$ContextPathStack =
625634
Append[System`Private`$ContextPathStack, $ContextPath];
626635
$ContextPath = {context, "System`"};
627-
$Packages = If[MemberQ[System`$Packages,$Context],
636+
$Packages = If[MemberQ[System`$Packages,$Context],
628637
None,
629638
System`$Packages=Join[{$Context}, System`$Packages]];
630639
Protect[System`Private`$ContextPathStack, System`$Packages];

test/helper.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from mathics.core.parser import parse, SingleLineFeeder
2+
from mathics.core.definitions import Definitions
3+
from mathics.core.evaluation import Evaluation
14
from mathics.session import MathicsSession
25

36
session = MathicsSession(add_builtin=True, catch_interrupt=False)

test/test_combinatorica.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from .helper import session, check_evaluation
33

4-
import sys
5-
from mathics.core.parser import parse, SingleLineFeeder
6-
from mathics.core.definitions import Definitions
7-
from mathics.core.evaluation import Evaluation
8-
import pytest
9-
10-
114
def test_combinatorica():
125
session.evaluate(
136
"""

test/test_control.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from .helper import session, check_evaluation
33

4-
import sys
5-
from mathics.core.parser import parse, SingleLineFeeder
6-
from mathics.core.definitions import Definitions
7-
from mathics.core.evaluation import Evaluation
8-
import pytest
9-
10-
11-
def test_control():
4+
def test_catch():
125
session.evaluate(
136
"""
147
(* Define a function that can "throw an exception": *)

test/test_evaluation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
),
4747
],
4848
)
49-
def check_evaluation(str_expr: str, str_expected: str, message=""):
49+
def test_evaluation(str_expr: str, str_expected: str, message=""):
5050
result = session.evaluate(str_expr)
5151
expected = session.evaluate(str_expected)
5252

0 commit comments

Comments
 (0)