Skip to content

Commit

Permalink
Add CellsToTeX test
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatera authored and rocky committed Apr 3, 2021
1 parent 9ddd63a commit f8e3e36
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
1 change: 0 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ Enhancements
* ``StringSplit`` now accepts a list in the first argument.
* ``SetDelayed`` now accepts several conditions imposed both at LHS as well as RHS.


Bug fixes
+++++++++

Expand Down
107 changes: 107 additions & 0 deletions test/test_cellstotex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import os
from mathics.core.parser import parse, MathicsSingleLineFeeder
from mathics.core.definitions import Definitions
from mathics.core.evaluation import Evaluation
from mathics.core.expression import Symbol
import pytest
import urllib.request
import unittest

external_url = (
"https://raw.githubusercontent.com/jkuczm/MathematicaCellsToTeX/master/NoInstall.m"
)

pytestmark = pytest.mark.skipif(os.getenv("SKIP_CELLSTOTEX", None) is not None,
reason="SKIP_CELLSTOTEX environment variable set")

try:
http_code = urllib.request.urlopen(external_url).getcode()
except:
url_reachable = False
else:
url_reachable = http_code in (200,) # add other 2xx or 3xx's?

definitions = Definitions(add_builtin=True)
evaluation = Evaluation(definitions=definitions, catch_interrupt=False)
set_versionnumber = 'Unprotect[$VersionNumber];$VersionNumber=11;Protect[$VersionNumber];'
import_url = 'Import@"%s"' % external_url


def _evaluate(str_expression):
expr = parse(definitions, MathicsSingleLineFeeder(str_expression))
return expr.evaluate(evaluation)

_evaluate('LoadModule["pymathics.asy"]')

def test_load():
str_expected1 = "{}"
message1 = ""
_evaluate(set_versionnumber)
result1 = _evaluate(import_url)
expected1 = _evaluate(str_expected1)


if message1:
assert result1 == expected1, message1
else:
assert result1 == expected1

result2 = _evaluate('Names["CellsToTeX`*"]')
expected2 = _evaluate('{"CellToTeX", "CellsToTeXException", "CellsToTeXPreamble"}')
print(result2)
assert result2 == expected2


@pytest.mark.skipif(not url_reachable, reason="skipping since we can't reach %s" % external_url)
@pytest.mark.xfail
def test_load_and_run():
print("load and run")
str_expected0 = "None"
_evaluate(set_versionnumber)
result0 = _evaluate(import_url)
expected0 = _evaluate(str_expected0)

if result0 == Symbol("System`$Failed"):
return 0

str_expr1 = 'CellsToTeXPreamble[]'
str_expected1 = '"\\mmaSet{morefv={gobble=2,},}\\n"'
result1 = _evaluate(str_expr1)
expected1 = _evaluate(str_expected1)
assert result1 == expected1

str_expr1 = 'boxes=MakeBoxes[Pi];\
cell = Cell[BoxData[boxes], "Input"];res=Catch[CellToTeX[cell, Style->"Input"]]'
str_expected1 = '"\\begin{mmaCell}{Input}\n \\pi\n\\end{mmaCell}"'
message1 = ""
result1 = _evaluate(str_expr1)
expected1 = _evaluate(str_expected1)
if message1:
assert result1 == expected1, message1
else:
assert result1 == expected1

str_expr2 = 'boxes=MakeBoxes[(-b \\[PlusMinus] Sqrt[b^2-4*a*c])/(2 a)];\
cell = Cell[BoxData[boxes],"Input"];res=Catch[CellToTeX[cell], Style->"Input"]'
str_expected2 = '"\\begin{mmaCell}{Input}\n \\mmaFrac{-b\\(\\pmb{\\pm}\\)\\mmaSqrt{\\mmaSup{b}{2}-4 a c}}{2 a}\n\\end{mmaCell}"'
print(str_expr2)
message2 = ""
result2 = _evaluate(str_expr2)
expected2 = _evaluate(str_expected2)
if message2:
assert result2 == expected2, message2
else:
assert result2 == expected2

str_expr3 = 'boxes=MakeBoxes[Sqrt[Integrate[f[x],{x,a,b}]]];\
cell = Cell[BoxData[boxes],"Input"];res=Catch[CellToTeX[cell]]'
print(str_expr3)

str_expected3 = '"\\begin{mmaCell}[morefunctionlocal={x}]{Input}\n \\mmaSqrt{\\mmaSubSupM{\\int}{a}{b}f[x]dx}\n\\end{mmaCell}"'
message3 = ""
result3 = _evaluate(str_expr3)
expected3 = _evaluate(str_expected3)
if message3:
assert result3 == expected3, message3
else:
assert result3 == expected3

0 comments on commit f8e3e36

Please sign in to comment.