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