Skip to content

Commit b1e0937

Browse files
committed
Merge remote-tracking branch 'QuLogic/pytest-font-text'
Merge pull request #7872: Convert font/text tests to pytest.
2 parents 1f3c02b + e82db62 commit b1e0937

File tree

7 files changed

+73
-76
lines changed

7 files changed

+73
-76
lines changed

lib/matplotlib/__init__.py

-8
Original file line numberDiff line numberDiff line change
@@ -1475,22 +1475,14 @@ def _jupyter_nbextension_paths():
14751475

14761476
default_test_modules = [
14771477
'matplotlib.tests.test_coding_standards',
1478-
'matplotlib.tests.test_dviread',
1479-
'matplotlib.tests.test_font_manager',
1480-
'matplotlib.tests.test_mathtext',
14811478
'matplotlib.tests.test_offsetbox',
14821479
'matplotlib.tests.test_patches',
14831480
'matplotlib.tests.test_path',
14841481
'matplotlib.tests.test_patheffects',
14851482
'matplotlib.tests.test_pickle',
14861483
'matplotlib.tests.test_png',
14871484
'matplotlib.tests.test_quiver',
1488-
'matplotlib.tests.test_text',
1489-
'matplotlib.tests.test_texmanager',
1490-
'matplotlib.tests.test_type1font',
1491-
'matplotlib.tests.test_ttconv',
14921485
'matplotlib.tests.test_units',
1493-
'matplotlib.tests.test_usetex',
14941486
'matplotlib.tests.test_widgets',
14951487
'matplotlib.tests.test_cycles',
14961488
]

lib/matplotlib/tests/test_dviread.py

+18-30
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,14 @@
44
import six
55
from matplotlib.testing.decorators import skip_if_command_unavailable
66

7-
8-
from nose.tools import assert_equal, with_setup
97
import matplotlib.dviread as dr
108
import os.path
119
import json
1210

1311

14-
original_find_tex_file = dr.find_tex_file
15-
16-
17-
def setup_PsfontsMap():
18-
dr.find_tex_file = lambda x: x
19-
20-
21-
def teardown_PsfontsMap():
22-
dr.find_tex_file = original_find_tex_file
23-
12+
def test_PsfontsMap(monkeypatch):
13+
monkeypatch.setattr(dr, 'find_tex_file', lambda x: x)
2414

25-
@with_setup(setup_PsfontsMap, teardown_PsfontsMap)
26-
def test_PsfontsMap():
2715
filename = os.path.join(
2816
os.path.dirname(__file__),
2917
'baseline_images', 'dviread', 'test.map')
@@ -32,34 +20,34 @@ def test_PsfontsMap():
3220
for n in [1, 2, 3, 4, 5]:
3321
key = 'TeXfont%d' % n
3422
entry = fontmap[key]
35-
assert_equal(entry.texname, key)
36-
assert_equal(entry.psname, 'PSfont%d' % n)
23+
assert entry.texname == key
24+
assert entry.psname == 'PSfont%d' % n
3725
if n not in [3, 5]:
38-
assert_equal(entry.encoding, 'font%d.enc' % n)
26+
assert entry.encoding == 'font%d.enc' % n
3927
elif n == 3:
40-
assert_equal(entry.encoding, 'enc3.foo')
28+
assert entry.encoding == 'enc3.foo'
4129
# We don't care about the encoding of TeXfont5, which specifies
4230
# multiple encodings.
4331
if n not in [1, 5]:
44-
assert_equal(entry.filename, 'font%d.pfa' % n)
32+
assert entry.filename == 'font%d.pfa' % n
4533
else:
46-
assert_equal(entry.filename, 'font%d.pfb' % n)
34+
assert entry.filename == 'font%d.pfb' % n
4735
if n == 4:
48-
assert_equal(entry.effects, {'slant': -0.1, 'extend': 2.2})
36+
assert entry.effects == {'slant': -0.1, 'extend': 2.2}
4937
else:
50-
assert_equal(entry.effects, {})
38+
assert entry.effects == {}
5139
# Some special cases
5240
entry = fontmap['TeXfont6']
53-
assert_equal(entry.filename, None)
54-
assert_equal(entry.encoding, None)
41+
assert entry.filename is None
42+
assert entry.encoding is None
5543
entry = fontmap['TeXfont7']
56-
assert_equal(entry.filename, None)
57-
assert_equal(entry.encoding, 'font7.enc')
44+
assert entry.filename is None
45+
assert entry.encoding == 'font7.enc'
5846
entry = fontmap['TeXfont8']
59-
assert_equal(entry.filename, 'font8.pfb')
60-
assert_equal(entry.encoding, None)
47+
assert entry.filename == 'font8.pfb'
48+
assert entry.encoding is None
6149
entry = fontmap['TeXfont9']
62-
assert_equal(entry.filename, '/absolute/font9.pfb')
50+
assert entry.filename == '/absolute/font9.pfb'
6351

6452

6553
@skip_if_command_unavailable(["kpsewhich", "-version"])
@@ -75,4 +63,4 @@ def test_dviread():
7563
for t in page.text],
7664
'boxes': [[b.x, b.y, b.height, b.width] for b in page.boxes]}
7765
for page in dvi]
78-
assert_equal(data, correct)
66+
assert data == correct

lib/matplotlib/tests/test_font_manager.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import (absolute_import, division, print_function,
22
unicode_literals)
33

4-
from nose.tools import assert_equal
54
import six
65

76
import os
@@ -22,7 +21,7 @@ def test_font_priority():
2221
['cmmi10', 'Bitstream Vera Sans']}):
2322
font = findfont(
2423
FontProperties(family=["sans-serif"]))
25-
assert_equal(os.path.basename(font), 'cmmi10.ttf')
24+
assert os.path.basename(font) == 'cmmi10.ttf'
2625

2726
# Smoketest get_charmap, which isn't used internally anymore
2827
font = get_font(font)
@@ -49,8 +48,8 @@ def test_json_serialization():
4948
{'family': 'Bitstream Vera Sans', 'weight': 700},
5049
{'family': 'no such font family'}):
5150
fp = FontProperties(**prop)
52-
assert_equal(fontManager.findfont(fp, rebuild_if_missing=False),
53-
copy.findfont(fp, rebuild_if_missing=False))
51+
assert (fontManager.findfont(fp, rebuild_if_missing=False) ==
52+
copy.findfont(fp, rebuild_if_missing=False))
5453

5554

5655
def test_otf():

lib/matplotlib/tests/test_mathtext.py

+39-20
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
import six
55

66
import io
7+
import re
78

89
import numpy as np
10+
import pytest
11+
912
import matplotlib
1013
from matplotlib.testing.decorators import image_comparison, knownfailureif, cleanup
1114
import matplotlib.pyplot as plt
@@ -195,8 +198,10 @@ def test_fontinfo():
195198
table = font.get_sfnt_table("head")
196199
assert table['version'] == (1, 0)
197200

198-
def test_mathtext_exceptions():
199-
errors = [
201+
202+
@pytest.mark.parametrize(
203+
'math, msg',
204+
[
200205
(r'$\hspace{}$', r'Expected \hspace{n}'),
201206
(r'$\hspace{foo}$', r'Expected \hspace{n}'),
202207
(r'$\frac$', r'Expected \frac{num}{den}'),
@@ -205,28 +210,47 @@ def test_mathtext_exceptions():
205210
(r'$\stackrel{}{}$', r'Expected \stackrel{num}{den}'),
206211
(r'$\binom$', r'Expected \binom{num}{den}'),
207212
(r'$\binom{}{}$', r'Expected \binom{num}{den}'),
208-
(r'$\genfrac$', r'Expected \genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}'),
209-
(r'$\genfrac{}{}{}{}{}{}$', r'Expected \genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}'),
213+
(r'$\genfrac$',
214+
r'Expected \genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}'),
215+
(r'$\genfrac{}{}{}{}{}{}$',
216+
r'Expected \genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}'),
210217
(r'$\sqrt$', r'Expected \sqrt{value}'),
211218
(r'$\sqrt f$', r'Expected \sqrt{value}'),
212219
(r'$\overline$', r'Expected \overline{value}'),
213220
(r'$\overline{}$', r'Expected \overline{value}'),
214221
(r'$\leftF$', r'Expected a delimiter'),
215222
(r'$\rightF$', r'Unknown symbol: \rightF'),
216223
(r'$\left(\right$', r'Expected a delimiter'),
217-
(r'$\left($', r'Expected "\right"')
218-
]
219-
224+
(r'$\left($', r'Expected "\right"'),
225+
],
226+
ids=[
227+
'hspace without value',
228+
'hspace with invalid value',
229+
'frac without parameters',
230+
'frac with empty parameters',
231+
'stackrel without parameters',
232+
'stackrel with empty parameters',
233+
'binom without parameters',
234+
'binom with empty parameters',
235+
'genfrac without parameters',
236+
'genfrac with empty parameters',
237+
'sqrt without parameters',
238+
'sqrt with invalid value',
239+
'overline without parameters',
240+
'overline with empty parameter',
241+
'left with invalid delimiter',
242+
'right with invalid delimiter',
243+
'unclosed parentheses with sizing',
244+
'unclosed parentheses without sizing',
245+
]
246+
)
247+
def test_mathtext_exceptions(math, msg):
220248
parser = mathtext.MathTextParser('agg')
221249

222-
for math, msg in errors:
223-
try:
224-
parser.parse(math)
225-
except ValueError as e:
226-
exc = str(e).split('\n')
227-
assert exc[3].startswith(msg)
228-
else:
229-
assert False, "Expected '%s', but didn't get it" % msg
250+
with pytest.raises(ValueError) as excinfo:
251+
parser.parse(math)
252+
excinfo.match(re.escape(msg))
253+
230254

231255
@cleanup
232256
def test_single_minus_sign():
@@ -243,8 +267,3 @@ def test_single_minus_sign():
243267

244268
# If this fails, it would be all white
245269
assert not np.all(array == 0xff)
246-
247-
248-
if __name__ == '__main__':
249-
import nose
250-
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/tests/test_text.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import numpy as np
88
from numpy.testing import assert_almost_equal
9-
from nose.tools import eq_, assert_raises
9+
import pytest
1010

1111
from matplotlib.transforms import Bbox
1212
import matplotlib
@@ -286,7 +286,7 @@ def test_get_rotation_int():
286286

287287
def test_get_rotation_raises():
288288
from matplotlib import text
289-
with assert_raises(ValueError):
289+
with pytest.raises(ValueError):
290290
text.get_rotation('hozirontal')
291291

292292

lib/matplotlib/tests/test_type1font.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import six
55

6-
from nose.tools import assert_equal, assert_in
76
import matplotlib.type1font as t1f
87
import os.path
98
import difflib
@@ -16,11 +15,11 @@ def test_Type1Font():
1615
condensed = font.transform({'extend': 0.5})
1716
with open(filename, 'rb') as fd:
1817
rawdata = fd.read()
19-
assert_equal(font.parts[0], rawdata[0x0006:0x10c5])
20-
assert_equal(font.parts[1], rawdata[0x10cb:0x897f])
21-
assert_equal(font.parts[2], rawdata[0x8985:0x8ba6])
22-
assert_equal(font.parts[1:], slanted.parts[1:])
23-
assert_equal(font.parts[1:], condensed.parts[1:])
18+
assert font.parts[0] == rawdata[0x0006:0x10c5]
19+
assert font.parts[1] == rawdata[0x10cb:0x897f]
20+
assert font.parts[2] == rawdata[0x8985:0x8ba6]
21+
assert font.parts[1:] == slanted.parts[1:]
22+
assert font.parts[1:] == condensed.parts[1:]
2423

2524
differ = difflib.Differ()
2625
diff = list(differ.compare(
@@ -39,7 +38,7 @@ def test_Type1Font():
3938
# Alters ItalicAngle
4039
'- /ItalicAngle 0 def',
4140
'+ /ItalicAngle -45.0 def'):
42-
assert_in(line, diff, 'diff to slanted font must contain %s' % line)
41+
assert line in diff, 'diff to slanted font must contain %s' % line
4342

4443
diff = list(differ.compare(font.parts[0].decode('latin-1').splitlines(),
4544
condensed.parts[0].decode('latin-1').splitlines()))
@@ -53,4 +52,4 @@ def test_Type1Font():
5352
# Alters FontMatrix
5453
'- /FontMatrix [0.001 0 0 0.001 0 0 ]readonly def',
5554
'+ /FontMatrix [0.0005 0.0 0.0 0.001 0.0 0.0]readonly def'):
56-
assert_in(line, diff, 'diff to condensed font must contain %s' % line)
55+
assert line in diff, 'diff to condensed font must contain %s' % line

lib/matplotlib/tests/test_usetex.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
from __future__ import (absolute_import, division, print_function,
22
unicode_literals)
33

4+
import pytest
5+
46
import matplotlib
57
from matplotlib.testing.decorators import image_comparison
68
from matplotlib.compat.subprocess import check_output
79
import matplotlib.pyplot as plt
810

911

12+
@pytest.mark.skipif(not matplotlib.checkdep_usetex(True),
13+
reason='Missing TeX or Ghostscript or dvipng')
1014
@image_comparison(baseline_images=['test_usetex'],
1115
extensions=['pdf', 'png'],
1216
tol=0.3)
1317
def test_usetex():
14-
canusetex = matplotlib.checkdep_usetex(True)
15-
if not canusetex:
16-
from nose import SkipTest
17-
raise SkipTest('Cannot run usetex_test')
1818
matplotlib.rcParams['text.usetex'] = True
1919
fig = plt.figure()
2020
ax = fig.add_subplot(111)

0 commit comments

Comments
 (0)