manim/tests/module/utils/test_tex.py
Viicos 68bd79093e
Refactor TexTemplate (#3520)
* Refactor `TexTemplate`

* Add tests, refactor some things

* Fixed Some tests

* Move typing imports

* Fix remaining tests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: JasonGrace2282 <aarush.deshpande@gmail.com>
Co-authored-by: Jason Grace <110117391+JasonGrace2282@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-01-15 18:39:03 +00:00

118 lines
2.8 KiB
Python

import pytest
from manim.utils.tex import TexTemplate
DEFAULT_BODY = r"""\documentclass[preview]{standalone}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
YourTextHere
\end{document}"""
BODY_WITH_ADDED_PREAMBLE = r"""\documentclass[preview]{standalone}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{testpackage}
\begin{document}
YourTextHere
\end{document}"""
BODY_WITH_PREPENDED_PREAMBLE = r"""\documentclass[preview]{standalone}
\usepackage{testpackage}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
YourTextHere
\end{document}"""
BODY_WITH_ADDED_DOCUMENT = r"""\documentclass[preview]{standalone}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
\boldmath
YourTextHere
\end{document}"""
BODY_REPLACE = r"""\documentclass[preview]{standalone}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
\sqrt{2}
\end{document}"""
BODY_REPLACE_IN_ENV = r"""\documentclass[preview]{standalone}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
\begin{align}
\sqrt{2}
\end{align}
\end{document}"""
def test_tex_template_default_body():
template = TexTemplate()
assert template.body == DEFAULT_BODY
def test_tex_template_preamble():
template = TexTemplate()
template.add_to_preamble(r"\usepackage{testpackage}")
assert template.body == BODY_WITH_ADDED_PREAMBLE
def test_tex_template_preprend_preamble():
template = TexTemplate()
template.add_to_preamble(r"\usepackage{testpackage}", prepend=True)
assert template.body == BODY_WITH_PREPENDED_PREAMBLE
def test_tex_template_document():
template = TexTemplate()
template.add_to_document(r"\boldmath")
assert template.body == BODY_WITH_ADDED_DOCUMENT
def test_tex_template_texcode_for_expression():
template = TexTemplate()
assert template.get_texcode_for_expression(r"\sqrt{2}") == BODY_REPLACE
def test_tex_template_texcode_for_expression_in_env():
template = TexTemplate()
assert (
template.get_texcode_for_expression_in_env(r"\sqrt{2}", environment="align")
== BODY_REPLACE_IN_ENV
)
def test_tex_template_fixed_body():
template = TexTemplate()
# Usually set when calling `from_file`
template.body = "dummy"
assert template.body == "dummy"
with pytest.warns(
UserWarning,
match="This TeX template was created with a fixed body, trying to add text the preamble will have no effect.",
):
template.add_to_preamble("dummys")
with pytest.warns(
UserWarning,
match="This TeX template was created with a fixed body, trying to add text the document will have no effect.",
):
template.add_to_document("dummy")