1- # encoding: utf-8
21"""Tests for IPython.utils.path.py"""
3-
42# Copyright (c) IPython Development Team.
53# Distributed under the terms of the Modified BSD License.
4+
5+ from contextlib import contextmanager
66from unittest .mock import patch
7+
78import nose .tools as nt
9+ import pytest
810
911from IPython .lib import latextools
1012from IPython .testing .decorators import onlyif_cmds_exist , skipif_not_matplotlib
1113from IPython .utils .process import FindCmdError
1214
1315
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 ):
2318 def mock_find_cmd (arg ):
2419 if arg == command :
2520 raise FindCmdError
2621
2722 with patch .object (latextools , "find_cmd" , mock_find_cmd ):
28- nt .assert_equal (latextools .latex_to_png_dvipng ("whatever" , True ),
29- None )
23+ assert latextools .latex_to_png_dvipng ("whatever" , True ) == None
3024
3125
3226@onlyif_cmds_exist ('latex' , 'dvipng' )
@@ -35,7 +29,7 @@ def test_latex_to_png_dvipng_runs():
3529 Test that latex_to_png_dvipng just runs without error.
3630 """
3731 def mock_kpsewhich (filename ):
38- nt . assert_equal ( filename , "breqn.sty" )
32+ assert filename == "breqn.sty"
3933 return None
4034
4135 for (s , wrap ) in [(u"$$x^2$$" , False ), (u"x^2" , True )]:
@@ -44,25 +38,39 @@ def mock_kpsewhich(filename):
4438 with patch .object (latextools , "kpsewhich" , mock_kpsewhich ):
4539 yield (latextools .latex_to_png_dvipng , s , wrap )
4640
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- def mock_kpsewhich (filename ):
53- nt . assert_equal ( filename , "breqn.sty" )
41+
42+ @ contextmanager
43+ def no_op ( * args , ** kwargs ):
44+ yield
45+
46+ def mock_kpsewhich (filename ):
47+ assert filename == "breqn.sty"
5448 return None
5549
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
5854
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 )
6169
6270@skipif_not_matplotlib
6371def test_latex_to_html ():
6472 img = latextools .latex_to_html ("$x^2$" )
65- nt . assert_in ( "data:image/png;base64,iVBOR" , img )
73+ assert "data:image/png;base64,iVBOR" in img
6674
6775
6876def test_genelatex_no_wrap ():
@@ -74,31 +82,27 @@ def mock_kpsewhich(filename):
7482 "(called with {0})" .format (filename ))
7583
7684 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}
8086\usepackage{amsmath}
8187\usepackage{amsthm}
8288\usepackage{amssymb}
8389\usepackage{bm}
8490\pagestyle{empty}
8591\begin{document}
8692body text
87- \end{document}''' )
93+ \end{document}'''
8894
8995
9096def test_genelatex_wrap_with_breqn ():
9197 """
9298 Test genelatex with wrap=True for the case breqn.sty is installed.
9399 """
94100 def mock_kpsewhich (filename ):
95- nt . assert_equal ( filename , "breqn.sty" )
101+ assert filename == "breqn.sty"
96102 return "path/to/breqn.sty"
97103
98104 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}
102106\usepackage{amsmath}
103107\usepackage{amsthm}
104108\usepackage{amssymb}
@@ -109,29 +113,27 @@ def mock_kpsewhich(filename):
109113\begin{dmath*}
110114x^2
111115\end{dmath*}
112- \end{document}''' )
116+ \end{document}'''
113117
114118
115119def test_genelatex_wrap_without_breqn ():
116120 """
117121 Test genelatex with wrap=True for the case breqn.sty is not installed.
118122 """
119123 def mock_kpsewhich (filename ):
120- nt . assert_equal ( filename , "breqn.sty" )
124+ assert filename == "breqn.sty"
121125 return None
122126
123127 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}
127129\usepackage{amsmath}
128130\usepackage{amsthm}
129131\usepackage{amssymb}
130132\usepackage{bm}
131133\pagestyle{empty}
132134\begin{document}
133135$$x^2$$
134- \end{document}''' )
136+ \end{document}'''
135137
136138
137139@skipif_not_matplotlib
@@ -146,28 +148,28 @@ def test_latex_to_png_color():
146148 color = '#000000' )
147149 dvipng_default = latextools .latex_to_png_dvipng (latex_string , False )
148150 dvipng_black = latextools .latex_to_png_dvipng (latex_string , False , 'Black' )
149- nt . assert_equal ( dvipng_default , dvipng_black )
151+ assert dvipng_default == dvipng_black
150152 mpl_default = latextools .latex_to_png_mpl (latex_string , False )
151153 mpl_black = latextools .latex_to_png_mpl (latex_string , False , 'Black' )
152- nt . assert_equal ( mpl_default , mpl_black )
153- nt . assert_in ( default_value , [dvipng_black , mpl_black ])
154- nt . assert_in ( default_hexblack , [dvipng_black , mpl_black ])
154+ assert mpl_default == mpl_black
155+ assert default_value in [dvipng_black , mpl_black ]
156+ assert default_hexblack in [dvipng_black , mpl_black ]
155157
156158 # Test that dvips name colors can be used without error
157159 dvipng_maroon = latextools .latex_to_png_dvipng (latex_string , False ,
158160 'Maroon' )
159161 # And that it doesn't return the black one
160- nt . assert_not_equal ( dvipng_black , dvipng_maroon )
162+ assert dvipng_black != dvipng_maroon
161163
162164 mpl_maroon = latextools .latex_to_png_mpl (latex_string , False , 'Maroon' )
163- nt . assert_not_equal ( mpl_black , mpl_maroon )
165+ assert mpl_black != mpl_maroon
164166 mpl_white = latextools .latex_to_png_mpl (latex_string , False , 'White' )
165167 mpl_hexwhite = latextools .latex_to_png_mpl (latex_string , False , '#FFFFFF' )
166- nt . assert_equal ( mpl_white , mpl_hexwhite )
168+ assert mpl_white == mpl_hexwhite
167169
168170 mpl_white_scale = latextools .latex_to_png_mpl (latex_string , False ,
169171 'White' , 1.2 )
170- nt . assert_not_equal ( mpl_white , mpl_white_scale )
172+ assert mpl_white != mpl_white_scale
171173
172174
173175def test_latex_to_png_invalid_hex_colors ():
0 commit comments