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