Skip to content

Commit ca5aeb1

Browse files
authored
Add tests for MathicsSession objects. Fix MathicsSession.format (#1549)
This PR adds a Pytest module for checking the basic behaviour of the MathicsSession object.
1 parent 342f0ab commit ca5aeb1

File tree

4 files changed

+87
-8
lines changed

4 files changed

+87
-8
lines changed

mathics/core/systemsymbols.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
SymbolDigitCharacter = Symbol("System`DigitCharacter")
9494
SymbolDirectedInfinity = Symbol("System`DirectedInfinity")
9595
SymbolDispatch = Symbol("System`Dispatch")
96+
SymbolDivide = Symbol("System`Divide")
9697
SymbolDot = Symbol("System`Dot")
9798
SymbolDownValues = Symbol("System`DownValues")
9899
SymbolDrop = Symbol("System`Drop")

mathics/session.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,14 @@ def evaluate_as_in_cli(self, str_expression, timeout=None, form=None, src_name="
174174
return res
175175

176176
def format_result(self, str_expression=None, timeout=None, form=None):
177+
if form is None:
178+
form = self.form
179+
177180
if str_expression:
178-
self.evaluate(str_expression, timeout=None, form=None)
181+
return self.evaluate(str_expression, timeout=timeout, form=form)
179182

180183
res = self.last_result
181-
if form is None:
182-
form = self.form
183-
return res.do_format(self.evaluation, form)
184+
return self.evaluation.format_output(res, form)
184185

185186
def parse(self, str_expression, src_name=""):
186187
"""

test/builtin/box/test_custom_boxexpression.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ def test_custom_boxconstruct():
9898
defs = session.evaluation.definitions
9999
instance_custom_atom = CustomAtom(expression=False)
100100
instance_custom_atom.contribute(defs, is_pymodule=True)
101-
evaluate("MakeBoxes[CustomAtom, InputForm]")
102-
formatted = session.format_result().boxes_to_mathml()
101+
formatted = session.evaluate("MakeBoxes[InputForm[CustomAtom]]").boxes_to_mathml()
103102
assert formatted == "CustomBoxExpression<<[1, 2, 3]>>"
104103

105104

@@ -109,8 +108,8 @@ def test_custom_graphicsbox_constructor():
109108
expression=False, evaluation=session.evaluation
110109
)
111110
instance_customgb_atom.contribute(defs, is_pymodule=True)
112-
evaluate("MakeBoxes[Graphics[{Circle[{0,0},1]}], OutputForm]")
113-
formatted = session.format_result().boxes_to_mathml()
111+
result = session.evaluate("MakeBoxes[OutputForm[Graphics[{Circle[{0,0},1]}]]]")
112+
formatted = result.boxes_to_mathml()
114113
assert (
115114
formatted
116115
== "--custom graphics--: I should plot (<Expression: <Symbol: System`Circle>[<ListExpression: (<Integer: 0>, <Integer: 0>)>, <Integer: 1>]>,) items"

test/test_session.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Basic tests for the MathicsSession class.
4+
5+
"""
6+
7+
8+
from mathics.core.atoms import Integer1, Integer2, IntegerM1
9+
from mathics.core.evaluation import Result
10+
from mathics.core.expression import Expression
11+
from mathics.core.symbols import Symbol, SymbolNull
12+
from mathics.core.systemsymbols import SymbolDirectedInfinity, SymbolPower, SymbolTimes
13+
from mathics.session import MathicsSession
14+
15+
session = MathicsSession()
16+
17+
18+
def test_session_evaluation():
19+
"""Some basic tests"""
20+
session.reset()
21+
assert session.evaluate("(**)") is SymbolNull
22+
assert session.evaluate("1/0").sameQ(Expression(SymbolDirectedInfinity))
23+
out = session.evaluation.out
24+
assert (
25+
len(session.evaluation.out) == 1
26+
and out[0].text == "Infinite expression 1 / 0 encountered."
27+
)
28+
assert session.evaluate("F[x]").sameQ(
29+
Expression(Symbol("Global`F"), Symbol("Global`x"))
30+
)
31+
assert len(session.evaluation.out) == 0
32+
assert session.evaluate("$Line") is Symbol("$Line")
33+
34+
35+
def test_session_evaluation_as_in_cli():
36+
# `evaluation_as_in_cli` returns a `Result` object
37+
session.reset()
38+
assert session.evaluate("$Line") is Symbol("$Line")
39+
result = session.evaluate_as_in_cli('Print["Hola"]')
40+
assert isinstance(result, Result)
41+
assert len(result.out) == 1 and result.out[0].text == "Hola"
42+
# Use session.evaluate(...) does not modify the `$Line` or
43+
# `Out` definitions, while `evaluate_as_in_cli` does:
44+
assert session.evaluate("$Line") is Integer1
45+
assert session.evaluate_as_in_cli("$Line", form="unformatted").result == Integer2
46+
assert session.evaluate("Out[1]") is SymbolNull
47+
assert session.evaluate_as_in_cli("Out[1]").result is None
48+
session.reset()
49+
assert session.evaluate("$Line") is Symbol("$Line")
50+
51+
52+
def test_session_format_evaluation():
53+
result = session.evaluate("a/b")
54+
assert session.format_result(form="unformatted").sameQ(result)
55+
assert session.format_result(form="text") == "a / b"
56+
assert session.format_result(form="latex") == "\\frac{a}{b}"
57+
assert session.format_result(form="xml") == (
58+
'<math display="block"><mfrac>' "<mi>a</mi> <mi>b</mi>" "</mfrac></math>"
59+
)
60+
61+
62+
def test_session_parse():
63+
parsed = session.parse("a/b")
64+
expected = Expression(
65+
SymbolTimes,
66+
Symbol("Global`a"),
67+
Expression(SymbolPower, Symbol("Global`b"), IntegerM1),
68+
)
69+
print("parsed:", parsed)
70+
print("expected:", expected)
71+
assert parsed.sameQ(expected)
72+
73+
74+
if __name__ == "__main__":
75+
test_session_evaluation()
76+
test_session_evaluation_as_in_cli()
77+
test_session_format_evaluation()
78+
test_session_parse()

0 commit comments

Comments
 (0)