From 159ecdcbc42a62c981d3b0784318ee30efe36bc6 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Tue, 17 Jan 2017 22:05:52 -0500 Subject: [PATCH 1/2] Convert text/font tests to pytest. --- lib/matplotlib/__init__.py | 8 ---- lib/matplotlib/tests/test_dviread.py | 48 +++++++++-------------- lib/matplotlib/tests/test_font_manager.py | 7 ++-- lib/matplotlib/tests/test_mathtext.py | 5 --- lib/matplotlib/tests/test_text.py | 4 +- lib/matplotlib/tests/test_type1font.py | 15 ++++--- lib/matplotlib/tests/test_usetex.py | 8 ++-- 7 files changed, 34 insertions(+), 61 deletions(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index dea348d4c645..da25f5e8ef23 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -1482,14 +1482,11 @@ def _jupyter_nbextension_paths(): 'matplotlib.tests.test_backend_qt5', 'matplotlib.tests.test_backend_svg', 'matplotlib.tests.test_coding_standards', - 'matplotlib.tests.test_dviread', 'matplotlib.tests.test_figure', - 'matplotlib.tests.test_font_manager', 'matplotlib.tests.test_gridspec', 'matplotlib.tests.test_image', 'matplotlib.tests.test_legend', 'matplotlib.tests.test_lines', - 'matplotlib.tests.test_mathtext', 'matplotlib.tests.test_offsetbox', 'matplotlib.tests.test_patches', 'matplotlib.tests.test_path', @@ -1497,12 +1494,7 @@ def _jupyter_nbextension_paths(): 'matplotlib.tests.test_pickle', 'matplotlib.tests.test_png', 'matplotlib.tests.test_quiver', - 'matplotlib.tests.test_text', - 'matplotlib.tests.test_texmanager', - 'matplotlib.tests.test_type1font', - 'matplotlib.tests.test_ttconv', 'matplotlib.tests.test_units', - 'matplotlib.tests.test_usetex', 'matplotlib.tests.test_widgets', 'matplotlib.tests.test_cycles', 'matplotlib.tests.test_preprocess_data', diff --git a/lib/matplotlib/tests/test_dviread.py b/lib/matplotlib/tests/test_dviread.py index 4335f9857912..4d9ee14213c3 100644 --- a/lib/matplotlib/tests/test_dviread.py +++ b/lib/matplotlib/tests/test_dviread.py @@ -4,26 +4,14 @@ import six from matplotlib.testing.decorators import skip_if_command_unavailable - -from nose.tools import assert_equal, with_setup import matplotlib.dviread as dr import os.path import json -original_find_tex_file = dr.find_tex_file - - -def setup_PsfontsMap(): - dr.find_tex_file = lambda x: x - - -def teardown_PsfontsMap(): - dr.find_tex_file = original_find_tex_file - +def test_PsfontsMap(monkeypatch): + monkeypatch.setattr(dr, 'find_tex_file', lambda x: x) -@with_setup(setup_PsfontsMap, teardown_PsfontsMap) -def test_PsfontsMap(): filename = os.path.join( os.path.dirname(__file__), 'baseline_images', 'dviread', 'test.map') @@ -32,34 +20,34 @@ def test_PsfontsMap(): for n in [1, 2, 3, 4, 5]: key = 'TeXfont%d' % n entry = fontmap[key] - assert_equal(entry.texname, key) - assert_equal(entry.psname, 'PSfont%d' % n) + assert entry.texname == key + assert entry.psname == 'PSfont%d' % n if n not in [3, 5]: - assert_equal(entry.encoding, 'font%d.enc' % n) + assert entry.encoding == 'font%d.enc' % n elif n == 3: - assert_equal(entry.encoding, 'enc3.foo') + assert entry.encoding == 'enc3.foo' # We don't care about the encoding of TeXfont5, which specifies # multiple encodings. if n not in [1, 5]: - assert_equal(entry.filename, 'font%d.pfa' % n) + assert entry.filename == 'font%d.pfa' % n else: - assert_equal(entry.filename, 'font%d.pfb' % n) + assert entry.filename == 'font%d.pfb' % n if n == 4: - assert_equal(entry.effects, {'slant': -0.1, 'extend': 2.2}) + assert entry.effects == {'slant': -0.1, 'extend': 2.2} else: - assert_equal(entry.effects, {}) + assert entry.effects == {} # Some special cases entry = fontmap['TeXfont6'] - assert_equal(entry.filename, None) - assert_equal(entry.encoding, None) + assert entry.filename is None + assert entry.encoding is None entry = fontmap['TeXfont7'] - assert_equal(entry.filename, None) - assert_equal(entry.encoding, 'font7.enc') + assert entry.filename is None + assert entry.encoding == 'font7.enc' entry = fontmap['TeXfont8'] - assert_equal(entry.filename, 'font8.pfb') - assert_equal(entry.encoding, None) + assert entry.filename == 'font8.pfb' + assert entry.encoding is None entry = fontmap['TeXfont9'] - assert_equal(entry.filename, '/absolute/font9.pfb') + assert entry.filename == '/absolute/font9.pfb' @skip_if_command_unavailable(["kpsewhich", "-version"]) @@ -75,4 +63,4 @@ def test_dviread(): for t in page.text], 'boxes': [[b.x, b.y, b.height, b.width] for b in page.boxes]} for page in dvi] - assert_equal(data, correct) + assert data == correct diff --git a/lib/matplotlib/tests/test_font_manager.py b/lib/matplotlib/tests/test_font_manager.py index 51e529a4a341..b91f5876bf1b 100644 --- a/lib/matplotlib/tests/test_font_manager.py +++ b/lib/matplotlib/tests/test_font_manager.py @@ -1,7 +1,6 @@ from __future__ import (absolute_import, division, print_function, unicode_literals) -from nose.tools import assert_equal import six import os @@ -24,7 +23,7 @@ def test_font_priority(): ['cmmi10', 'Bitstream Vera Sans']}): font = findfont( FontProperties(family=["sans-serif"])) - assert_equal(os.path.basename(font), 'cmmi10.ttf') + assert os.path.basename(font) == 'cmmi10.ttf' # Smoketest get_charmap, which isn't used internally anymore font = get_font(font) @@ -51,8 +50,8 @@ def test_json_serialization(): {'family': 'Bitstream Vera Sans', 'weight': 700}, {'family': 'no such font family'}): fp = FontProperties(**prop) - assert_equal(fontManager.findfont(fp, rebuild_if_missing=False), - copy.findfont(fp, rebuild_if_missing=False)) + assert (fontManager.findfont(fp, rebuild_if_missing=False) == + copy.findfont(fp, rebuild_if_missing=False)) def test_otf(): diff --git a/lib/matplotlib/tests/test_mathtext.py b/lib/matplotlib/tests/test_mathtext.py index 38762987cd9a..e81243dd3c4f 100644 --- a/lib/matplotlib/tests/test_mathtext.py +++ b/lib/matplotlib/tests/test_mathtext.py @@ -243,8 +243,3 @@ def test_single_minus_sign(): # If this fails, it would be all white assert not np.all(array == 0xff) - - -if __name__ == '__main__': - import nose - nose.runmodule(argv=['-s', '--with-doctest'], exit=False) diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index 44d2abb38aee..2eae173340b4 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -6,7 +6,7 @@ import numpy as np from numpy.testing import assert_almost_equal -from nose.tools import eq_, assert_raises +import pytest from matplotlib.transforms import Bbox import matplotlib @@ -286,7 +286,7 @@ def test_get_rotation_int(): def test_get_rotation_raises(): from matplotlib import text - with assert_raises(ValueError): + with pytest.raises(ValueError): text.get_rotation('hozirontal') diff --git a/lib/matplotlib/tests/test_type1font.py b/lib/matplotlib/tests/test_type1font.py index 8a86dc3ed59f..013b7a6eb368 100644 --- a/lib/matplotlib/tests/test_type1font.py +++ b/lib/matplotlib/tests/test_type1font.py @@ -3,7 +3,6 @@ import six -from nose.tools import assert_equal, assert_in import matplotlib.type1font as t1f import os.path import difflib @@ -16,11 +15,11 @@ def test_Type1Font(): condensed = font.transform({'extend': 0.5}) with open(filename, 'rb') as fd: rawdata = fd.read() - assert_equal(font.parts[0], rawdata[0x0006:0x10c5]) - assert_equal(font.parts[1], rawdata[0x10cb:0x897f]) - assert_equal(font.parts[2], rawdata[0x8985:0x8ba6]) - assert_equal(font.parts[1:], slanted.parts[1:]) - assert_equal(font.parts[1:], condensed.parts[1:]) + assert font.parts[0] == rawdata[0x0006:0x10c5] + assert font.parts[1] == rawdata[0x10cb:0x897f] + assert font.parts[2] == rawdata[0x8985:0x8ba6] + assert font.parts[1:] == slanted.parts[1:] + assert font.parts[1:] == condensed.parts[1:] differ = difflib.Differ() diff = list(differ.compare( @@ -39,7 +38,7 @@ def test_Type1Font(): # Alters ItalicAngle '- /ItalicAngle 0 def', '+ /ItalicAngle -45.0 def'): - assert_in(line, diff, 'diff to slanted font must contain %s' % line) + assert line in diff, 'diff to slanted font must contain %s' % line diff = list(differ.compare(font.parts[0].decode('latin-1').splitlines(), condensed.parts[0].decode('latin-1').splitlines())) @@ -53,4 +52,4 @@ def test_Type1Font(): # Alters FontMatrix '- /FontMatrix [0.001 0 0 0.001 0 0 ]readonly def', '+ /FontMatrix [0.0005 0.0 0.0 0.001 0.0 0.0]readonly def'): - assert_in(line, diff, 'diff to condensed font must contain %s' % line) + assert line in diff, 'diff to condensed font must contain %s' % line diff --git a/lib/matplotlib/tests/test_usetex.py b/lib/matplotlib/tests/test_usetex.py index dfe9f082e378..e7d9b6fbf0a3 100644 --- a/lib/matplotlib/tests/test_usetex.py +++ b/lib/matplotlib/tests/test_usetex.py @@ -1,20 +1,20 @@ from __future__ import (absolute_import, division, print_function, unicode_literals) +import pytest + import matplotlib from matplotlib.testing.decorators import image_comparison from matplotlib.compat.subprocess import check_output import matplotlib.pyplot as plt +@pytest.mark.skipif(not matplotlib.checkdep_usetex(True), + reason='Missing TeX or Ghostscript or dvipng') @image_comparison(baseline_images=['test_usetex'], extensions=['pdf', 'png'], tol=0.3) def test_usetex(): - canusetex = matplotlib.checkdep_usetex(True) - if not canusetex: - from nose import SkipTest - raise SkipTest('Cannot run usetex_test') matplotlib.rcParams['text.usetex'] = True fig = plt.figure() ax = fig.add_subplot(111) From e82db62e169b78cc6c21bcf58f553c033fe5d14b Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Tue, 17 Jan 2017 23:09:25 -0500 Subject: [PATCH 2/2] Parametrize test_mathtext_exceptions. --- lib/matplotlib/tests/test_mathtext.py | 54 +++++++++++++++++++-------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/lib/matplotlib/tests/test_mathtext.py b/lib/matplotlib/tests/test_mathtext.py index e81243dd3c4f..4c3d8972a1c5 100644 --- a/lib/matplotlib/tests/test_mathtext.py +++ b/lib/matplotlib/tests/test_mathtext.py @@ -4,8 +4,11 @@ import six import io +import re import numpy as np +import pytest + import matplotlib from matplotlib.testing.decorators import image_comparison, knownfailureif, cleanup import matplotlib.pyplot as plt @@ -195,8 +198,10 @@ def test_fontinfo(): table = font.get_sfnt_table("head") assert table['version'] == (1, 0) -def test_mathtext_exceptions(): - errors = [ + +@pytest.mark.parametrize( + 'math, msg', + [ (r'$\hspace{}$', r'Expected \hspace{n}'), (r'$\hspace{foo}$', r'Expected \hspace{n}'), (r'$\frac$', r'Expected \frac{num}{den}'), @@ -205,8 +210,10 @@ def test_mathtext_exceptions(): (r'$\stackrel{}{}$', r'Expected \stackrel{num}{den}'), (r'$\binom$', r'Expected \binom{num}{den}'), (r'$\binom{}{}$', r'Expected \binom{num}{den}'), - (r'$\genfrac$', r'Expected \genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}'), - (r'$\genfrac{}{}{}{}{}{}$', r'Expected \genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}'), + (r'$\genfrac$', + r'Expected \genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}'), + (r'$\genfrac{}{}{}{}{}{}$', + r'Expected \genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}'), (r'$\sqrt$', r'Expected \sqrt{value}'), (r'$\sqrt f$', r'Expected \sqrt{value}'), (r'$\overline$', r'Expected \overline{value}'), @@ -214,19 +221,36 @@ def test_mathtext_exceptions(): (r'$\leftF$', r'Expected a delimiter'), (r'$\rightF$', r'Unknown symbol: \rightF'), (r'$\left(\right$', r'Expected a delimiter'), - (r'$\left($', r'Expected "\right"') - ] - + (r'$\left($', r'Expected "\right"'), + ], + ids=[ + 'hspace without value', + 'hspace with invalid value', + 'frac without parameters', + 'frac with empty parameters', + 'stackrel without parameters', + 'stackrel with empty parameters', + 'binom without parameters', + 'binom with empty parameters', + 'genfrac without parameters', + 'genfrac with empty parameters', + 'sqrt without parameters', + 'sqrt with invalid value', + 'overline without parameters', + 'overline with empty parameter', + 'left with invalid delimiter', + 'right with invalid delimiter', + 'unclosed parentheses with sizing', + 'unclosed parentheses without sizing', + ] +) +def test_mathtext_exceptions(math, msg): parser = mathtext.MathTextParser('agg') - for math, msg in errors: - try: - parser.parse(math) - except ValueError as e: - exc = str(e).split('\n') - assert exc[3].startswith(msg) - else: - assert False, "Expected '%s', but didn't get it" % msg + with pytest.raises(ValueError) as excinfo: + parser.parse(math) + excinfo.match(re.escape(msg)) + @cleanup def test_single_minus_sign():