Show More
@@ -1,191 +1,196 b'' | |||
|
1 | 1 | """Tests for IPython.utils.path.py""" |
|
2 | 2 | # Copyright (c) IPython Development Team. |
|
3 | 3 | # Distributed under the terms of the Modified BSD License. |
|
4 | 4 | |
|
5 | 5 | from contextlib import contextmanager |
|
6 | 6 | from unittest.mock import patch |
|
7 | 7 | |
|
8 | import nose.tools as nt | |
|
9 | 8 | import pytest |
|
10 | 9 | |
|
11 | 10 | from IPython.lib import latextools |
|
12 | 11 | from IPython.testing.decorators import ( |
|
13 | 12 | onlyif_cmds_exist, |
|
14 | 13 | skipif_not_matplotlib, |
|
15 | 14 | skip_iptest_but_not_pytest, |
|
16 | 15 | ) |
|
17 | 16 | from IPython.utils.process import FindCmdError |
|
18 | 17 | |
|
19 | 18 | |
|
20 | 19 | @pytest.mark.parametrize('command', ['latex', 'dvipng']) |
|
21 | 20 | @skip_iptest_but_not_pytest |
|
22 | 21 | def test_check_latex_to_png_dvipng_fails_when_no_cmd(command): |
|
23 | 22 | def mock_find_cmd(arg): |
|
24 | 23 | if arg == command: |
|
25 | 24 | raise FindCmdError |
|
26 | 25 | |
|
27 | 26 | with patch.object(latextools, "find_cmd", mock_find_cmd): |
|
28 | 27 | assert latextools.latex_to_png_dvipng("whatever", True) is None |
|
29 | 28 | |
|
30 | 29 | |
|
31 | 30 | @contextmanager |
|
32 | 31 | def no_op(*args, **kwargs): |
|
33 | 32 | yield |
|
34 | 33 | |
|
35 | 34 | |
|
36 | 35 | @skip_iptest_but_not_pytest |
|
37 | 36 | @onlyif_cmds_exist("latex", "dvipng") |
|
38 | 37 | @pytest.mark.parametrize("s, wrap", [(u"$$x^2$$", False), (u"x^2", True)]) |
|
39 | 38 | def test_latex_to_png_dvipng_runs(s, wrap): |
|
40 | 39 | """ |
|
41 | 40 | Test that latex_to_png_dvipng just runs without error. |
|
42 | 41 | """ |
|
43 | 42 | def mock_kpsewhich(filename): |
|
44 | 43 | assert filename == "breqn.sty" |
|
45 | 44 | return None |
|
46 | 45 | |
|
47 | 46 | latextools.latex_to_png_dvipng(s, wrap) |
|
48 | 47 | |
|
49 | 48 | with patch_latextool(mock_kpsewhich): |
|
50 | 49 | latextools.latex_to_png_dvipng(s, wrap) |
|
51 | 50 | |
|
52 | 51 | |
|
53 | 52 | def mock_kpsewhich(filename): |
|
54 | 53 | assert filename == "breqn.sty" |
|
55 | 54 | return None |
|
56 | 55 | |
|
57 | 56 | @contextmanager |
|
58 | 57 | def patch_latextool(mock=mock_kpsewhich): |
|
59 | 58 | with patch.object(latextools, "kpsewhich", mock): |
|
60 | 59 | yield |
|
61 | 60 | |
|
62 | 61 | @pytest.mark.parametrize('context', [no_op, patch_latextool]) |
|
63 | 62 | @pytest.mark.parametrize('s_wrap', [("$x^2$", False), ("x^2", True)]) |
|
64 | 63 | @skip_iptest_but_not_pytest |
|
65 | 64 | def test_latex_to_png_mpl_runs(s_wrap, context): |
|
66 | 65 | """ |
|
67 | 66 | Test that latex_to_png_mpl just runs without error. |
|
68 | 67 | """ |
|
69 | 68 | try: |
|
70 | 69 | import matplotlib |
|
71 | 70 | except ImportError: |
|
72 | 71 | pytest.skip("This needs matplotlib to be available") |
|
73 | 72 | return |
|
74 | 73 | s, wrap = s_wrap |
|
75 | 74 | with context(): |
|
76 | 75 | latextools.latex_to_png_mpl(s, wrap) |
|
77 | 76 | |
|
78 | 77 | @skipif_not_matplotlib |
|
79 | 78 | def test_latex_to_html(): |
|
80 | 79 | img = latextools.latex_to_html("$x^2$") |
|
81 | 80 | assert "data:image/png;base64,iVBOR" in img |
|
82 | 81 | |
|
83 | 82 | |
|
84 | 83 | def test_genelatex_no_wrap(): |
|
85 | 84 | """ |
|
86 | 85 | Test genelatex with wrap=False. |
|
87 | 86 | """ |
|
88 | 87 | def mock_kpsewhich(filename): |
|
89 | 88 | assert False, ("kpsewhich should not be called " |
|
90 | 89 | "(called with {0})".format(filename)) |
|
91 | 90 | |
|
92 | 91 | with patch_latextool(mock_kpsewhich): |
|
93 | 92 | assert '\n'.join(latextools.genelatex("body text", False)) == r'''\documentclass{article} |
|
94 | 93 | \usepackage{amsmath} |
|
95 | 94 | \usepackage{amsthm} |
|
96 | 95 | \usepackage{amssymb} |
|
97 | 96 | \usepackage{bm} |
|
98 | 97 | \pagestyle{empty} |
|
99 | 98 | \begin{document} |
|
100 | 99 | body text |
|
101 | 100 | \end{document}''' |
|
102 | 101 | |
|
103 | 102 | |
|
104 | 103 | def test_genelatex_wrap_with_breqn(): |
|
105 | 104 | """ |
|
106 | 105 | Test genelatex with wrap=True for the case breqn.sty is installed. |
|
107 | 106 | """ |
|
108 | 107 | def mock_kpsewhich(filename): |
|
109 | 108 | assert filename == "breqn.sty" |
|
110 | 109 | return "path/to/breqn.sty" |
|
111 | 110 | |
|
112 | 111 | with patch_latextool(mock_kpsewhich): |
|
113 | 112 | assert '\n'.join(latextools.genelatex("x^2", True)) == r'''\documentclass{article} |
|
114 | 113 | \usepackage{amsmath} |
|
115 | 114 | \usepackage{amsthm} |
|
116 | 115 | \usepackage{amssymb} |
|
117 | 116 | \usepackage{bm} |
|
118 | 117 | \usepackage{breqn} |
|
119 | 118 | \pagestyle{empty} |
|
120 | 119 | \begin{document} |
|
121 | 120 | \begin{dmath*} |
|
122 | 121 | x^2 |
|
123 | 122 | \end{dmath*} |
|
124 | 123 | \end{document}''' |
|
125 | 124 | |
|
126 | 125 | |
|
127 | 126 | def test_genelatex_wrap_without_breqn(): |
|
128 | 127 | """ |
|
129 | 128 | Test genelatex with wrap=True for the case breqn.sty is not installed. |
|
130 | 129 | """ |
|
131 | 130 | def mock_kpsewhich(filename): |
|
132 | 131 | assert filename == "breqn.sty" |
|
133 | 132 | return None |
|
134 | 133 | |
|
135 | 134 | with patch_latextool(mock_kpsewhich): |
|
136 | 135 | assert '\n'.join(latextools.genelatex("x^2", True)) == r'''\documentclass{article} |
|
137 | 136 | \usepackage{amsmath} |
|
138 | 137 | \usepackage{amsthm} |
|
139 | 138 | \usepackage{amssymb} |
|
140 | 139 | \usepackage{bm} |
|
141 | 140 | \pagestyle{empty} |
|
142 | 141 | \begin{document} |
|
143 | 142 | $$x^2$$ |
|
144 | 143 | \end{document}''' |
|
145 | 144 | |
|
146 | 145 | |
|
147 | 146 | @skipif_not_matplotlib |
|
148 | 147 | @onlyif_cmds_exist('latex', 'dvipng') |
|
149 | 148 | def test_latex_to_png_color(): |
|
150 | 149 | """ |
|
151 | 150 | Test color settings for latex_to_png. |
|
152 | 151 | """ |
|
153 | 152 | latex_string = "$x^2$" |
|
154 | 153 | default_value = latextools.latex_to_png(latex_string, wrap=False) |
|
155 | 154 | default_hexblack = latextools.latex_to_png(latex_string, wrap=False, |
|
156 | 155 | color='#000000') |
|
157 | 156 | dvipng_default = latextools.latex_to_png_dvipng(latex_string, False) |
|
158 | 157 | dvipng_black = latextools.latex_to_png_dvipng(latex_string, False, 'Black') |
|
159 | 158 | assert dvipng_default == dvipng_black |
|
160 | 159 | mpl_default = latextools.latex_to_png_mpl(latex_string, False) |
|
161 | 160 | mpl_black = latextools.latex_to_png_mpl(latex_string, False, 'Black') |
|
162 | 161 | assert mpl_default == mpl_black |
|
163 | 162 | assert default_value in [dvipng_black, mpl_black] |
|
164 | 163 | assert default_hexblack in [dvipng_black, mpl_black] |
|
165 | 164 | |
|
166 | 165 | # Test that dvips name colors can be used without error |
|
167 | 166 | dvipng_maroon = latextools.latex_to_png_dvipng(latex_string, False, |
|
168 | 167 | 'Maroon') |
|
169 | 168 | # And that it doesn't return the black one |
|
170 | 169 | assert dvipng_black != dvipng_maroon |
|
171 | 170 | |
|
172 | 171 | mpl_maroon = latextools.latex_to_png_mpl(latex_string, False, 'Maroon') |
|
173 | 172 | assert mpl_black != mpl_maroon |
|
174 | 173 | mpl_white = latextools.latex_to_png_mpl(latex_string, False, 'White') |
|
175 | 174 | mpl_hexwhite = latextools.latex_to_png_mpl(latex_string, False, '#FFFFFF') |
|
176 | 175 | assert mpl_white == mpl_hexwhite |
|
177 | 176 | |
|
178 | 177 | mpl_white_scale = latextools.latex_to_png_mpl(latex_string, False, |
|
179 | 178 | 'White', 1.2) |
|
180 | 179 | assert mpl_white != mpl_white_scale |
|
181 | 180 | |
|
182 | 181 | |
|
183 | 182 | def test_latex_to_png_invalid_hex_colors(): |
|
184 | 183 | """ |
|
185 | 184 | Test that invalid hex colors provided to dvipng gives an exception. |
|
186 | 185 | """ |
|
187 | 186 | latex_string = "$x^2$" |
|
188 | nt.assert_raises(ValueError, lambda: latextools.latex_to_png(latex_string, | |
|
189 | backend='dvipng', color="#f00bar")) | |
|
190 |
|
|
|
191 |
|
|
|
187 | pytest.raises( | |
|
188 | ValueError, | |
|
189 | lambda: latextools.latex_to_png( | |
|
190 | latex_string, backend="dvipng", color="#f00bar" | |
|
191 | ), | |
|
192 | ) | |
|
193 | pytest.raises( | |
|
194 | ValueError, | |
|
195 | lambda: latextools.latex_to_png(latex_string, backend="dvipng", color="#f00"), | |
|
196 | ) |
General Comments 0
You need to be logged in to leave comments.
Login now