8000 Fix the cherry-picked test. · matplotlib/matplotlib@428035c · GitHub
[go: up one dir, main page]

Skip to content

Commit 428035c

Browse files
committed
Fix the cherry-picked test.
1 parent a2c6a50 commit 428035c

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

lib/matplotlib/tests/test_backend_svg.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import numpy as np
22
from io import BytesIO
33
import os
4+
import re
45
import tempfile
56
import warnings
67
import xml.parsers.expat
78

89
import pytest
910

11+
import matplotlib as mpl
12+
from matplotlib import dviread
13+
from matplotlib.figure import Figure
1014
import matplotlib.pyplot as plt
1115
from matplotlib.testing.decorators import image_comparison
12-
import matplotlib
13-
from matplotlib import dviread
1416

1517

1618
with warnings.catch_warnings():
1719
warnings.simplefilter('ignore')
1820
needs_usetex = pytest.mark.skipif(
19-
not matplotlib.checkdep_usetex(True),
21+
not mpl.checkdep_usetex(True),
2022
reason="This test needs a TeX installation")
2123

2224

@@ -107,15 +109,10 @@ def test_bold_font_output_with_none_fonttype():
107109

108110
def _test_determinism_save(filename, usetex):
109111
# This function is mostly copy&paste from "def test_visibility"
110-
# To require no GUI, we use Figure and FigureCanvasSVG
111-
# instead of plt.figure and fig.savefig
112-
from matplotlib.figure import Figure
113-
from matplotlib.backends.backend_svg import FigureCanvasSVG
114-
from matplotlib import rc
115-
rc('svg', hashsalt='asdf')
116-
rc('text', usetex=usetex)
112+
mpl.rc('svg', hashsalt='asdf')
113+
mpl.rc('text', usetex=usetex)
117114

118-
fig = Figure()
115+
fig = Figure() # Require no GUI.
119116
ax = fig.add_subplot(111)
120117

121118
x = np.linspace(0, 4 * np.pi, 50)
@@ -129,7 +126,7 @@ def _test_determinism_save(filename, usetex):
129126
ax.set_xlabel('A string $1+2+\\sigma$')
130127
ax.set_ylabel('A string $1+2+\\sigma$')
131128

132-
FigureCanvasSVG(fig).print_svg(filename)
129+
fig.savefig(filename, format="svg")
133130

134131

135132
@pytest.mark.parametrize(
@@ -172,36 +169,30 @@ def test_determinism(filename, usetex):
172169
@needs_usetex
173170
def test_missing_psfont(monkeypatch):
174171
"""An error is raised if a TeX font lacks a Type-1 equivalent"""
175-
from matplotlib import rc
176172

177173
def psfont(*args, **kwargs):
178174
return dviread.PsFont(texname='texfont', psname='Some Font',
179175
effects=None, encoding=None, filename=None)
180176

181177
monkeypatch.setattr(dviread.PsfontsMap, '__getitem__', psfont)
182-
rc('text', usetex=True)
178+
mpl.rc('text', usetex=True)
183179
fig, ax = plt.subplots()
184180
ax.text(0.5, 0.5, 'hello')
185181
with tempfile.TemporaryFile() as tmpfile, pytest.raises(ValueError):
186182
fig.savefig(tmpfile, format='svg')
187183

188184

189-
@needs_tex
185+
# Use Computer Modern Sans Serif, not Helvetica (which has no \textwon).
186+
@pytest.mark.style('default')
187+
@needs_usetex
190188
def test_unicode_won():
191-
from pylab import rcParams, plot, ylabel, savefig
192-
rcParams.update({'text.usetex': True, 'text.latex.unicode': True})
193-
194-
plot(1, 1)
195-
ylabel(r'\textwon')
189+
fig = Figure()
190+
fig.text(.5, .5, r'\textwon', usetex=True)
196191

197-
fd = BytesIO()
198-
savefig(fd, format='svg')
199-
fd.seek(0)
200-
buf = fd.read().decode()
201-
fd.close()
192+
with BytesIO() as fd:
193+
fig.savefig(fd, format='svg')
194+
buf = fd.getvalue().decode('ascii')
202195

203196
won_id = 'Computer_Modern_Sans_Serif-142'
204-
def_regex = re.compile(r'<path d=(.|\s)*?id="{0}"/>'.format(won_id))
205-
use_regex = re.compile(r'<use[^/>]*? xlink:href="#{0}"/>'.format(won_id))
206-
assertTrue(bool(def_regex.search(buf)))
207-
assertTrue(bool(use_regex.search(buf)))
197+
assert re.search(r'<path d=(.|\s)*?id="{0}"/>'.format(won_id), buf)
198+
assert re.search(r'<use[^/>]*? xlink:href="#{0}"/>'.format(won_id), buf)

0 commit comments

Comments
 (0)
0