Skip to content

Commit f78d0ba

Browse files
committed
add helper
1 parent 053557f commit f78d0ba

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

test/helper.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# -*- coding: utf-8 -*-
2+
import time
3+
from typing import Optional
4+
5+
6+
from mathics.core.symbols import Symbol
7+
from mathics.core.load_builtin import import_and_load_builtins
8+
from mathics.session import MathicsSession
9+
10+
import_and_load_builtins()
11+
12+
# Set up a Mathics session with definitions.
13+
# For consistency set the character encoding ASCII which is
14+
# the lowest common denominator available on all systems.
15+
session = MathicsSession(character_encoding="ASCII")
16+
17+
18+
def reset_session(add_builtin=True, catch_interrupt=False):
19+
global session
20+
session.reset()
21+
22+
23+
def evaluate_value(str_expr: str):
24+
expr = session.evaluate(str_expr)
25+
if isinstance(expr, Symbol):
26+
return expr.name
27+
return expr.value
28+
29+
30+
def evaluate(str_expr: str):
31+
return session.evaluate(str_expr)
32+
33+
34+
def check_evaluation(
35+
str_expr: str,
36+
str_expected: str,
37+
failure_message: str = "",
38+
hold_expected: bool = False,
39+
to_string_expr: bool = True,
40+
to_string_expected: bool = True,
41+
to_python_expected: bool = False,
42+
expected_messages: Optional[tuple] = None,
43+
):
44+
"""
45+
Helper function to test Mathics expression against
46+
its results
47+
48+
Compares the expressions represented by ``str_expr`` and ``str_expected`` by evaluating
49+
the first, and optionally, the second.
50+
51+
to_string_expr: If ``True`` (default value) the result of the evaluation is converted
52+
into a Python string. Otherwise, the expression is kept as an Expression
53+
object. If this argument is set to ``None``, the session is reset.
54+
55+
failure_message (str): message shown in case of failure
56+
hold_expected (bool): If ``False`` (default value) the ``str_expected`` is evaluated. Otherwise,
57+
the expression is considered literally.
58+
59+
to_string_expected: If ``True`` (default value) the expected expression is
60+
evaluated and then converted to a Python string. result of the evaluation is converted
61+
into a Python string. If ``False``, the expected expression is kept as an Expression object.
62+
63+
to_python_expected: If ``True``, and ``to_string_expected`` is ``False``, the result of evaluating ``str_expr``
64+
is compared against the result of the evaluation of ``str_expected``, converted into a
65+
Python object.
66+
67+
expected_messages ``Optional[tuple[str]]``: If a tuple of strings are passed into this parameter, messages and prints raised during
68+
the evaluation of ``str_expr`` are compared with the elements of the list. If ``None``, this comparison
69+
is ommited.
70+
"""
71+
if str_expr is None:
72+
reset_session()
73+
evaluate('LoadModule["pymathics.vectorizedplot"]')
74+
return
75+
76+
if to_string_expr:
77+
str_expr = f"ToString[{str_expr}]"
78+
result = evaluate_value(str_expr)
79+
else:
80+
result = evaluate(str_expr)
81+
82+
outs = [out.text for out in session.evaluation.out]
83+
84+
if to_string_expected:
85+
if hold_expected:
86+
expected = str_expected
87+
else:
88+
str_expected = f"ToString[{str_expected}]"
89+
expected = evaluate_value(str_expected)
90+
else:
91+
if hold_expected:
92+
if to_python_expected:
93+
expected = str_expected
94+
else:
95+
expected = evaluate(f"HoldForm[{str_expected}]").elements[0]
96+
else:
97+
expected = evaluate(str_expected)
98+
if to_python_expected:
99+
expected = expected.to_python(string_quotes=False)
100+
101+
print(time.asctime())
102+
if failure_message:
103+
print((result, expected))
104+
assert result == expected, failure_message
105+
else:
106+
print((result, expected))
107+
assert result == expected
108+
109+
if expected_messages is not None:
110+
msgs = list(expected_messages)
111+
expected_len = len(msgs)
112+
got_len = len(outs)
113+
assert (
114+
expected_len == got_len
115+
), f"expected {expected_len}; got {got_len}. Messages: {outs}"
116+
for out, msg in zip(outs, msgs):
117+
if out != msg:
118+
print(f"out:<<{out}>>")
119+
print(" and ")
120+
print(f"expected=<<{msg}>>")
121+
assert False, " do not match."

0 commit comments

Comments
 (0)