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