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