|
| 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