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