File size: 3,752 Bytes
3eedfa7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import pytest

from manim.utils.tex import TexTemplate, _texcode_for_environment

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


def test_texcode_for_environment():
    """Test that the environment is correctly extracted from the input"""
    # environment without arguments
    assert _texcode_for_environment("align*") == (r"\begin{align*}", r"\end{align*}")
    assert _texcode_for_environment("{align*}") == (r"\begin{align*}", r"\end{align*}")
    assert _texcode_for_environment(r"\begin{align*}") == (
        r"\begin{align*}",
        r"\end{align*}",
    )
    # environment with arguments
    assert _texcode_for_environment("{tabular}[t]{cccl}") == (
        r"\begin{tabular}[t]{cccl}",
        r"\end{tabular}",
    )
    assert _texcode_for_environment("tabular}{cccl") == (
        r"\begin{tabular}{cccl}",
        r"\end{tabular}",
    )
    assert _texcode_for_environment(r"\begin{tabular}[t]{cccl}") == (
        r"\begin{tabular}[t]{cccl}",
        r"\end{tabular}",
    )