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