From c283a423762258526e878b068d3565fbef68ee17 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 26 May 2021 10:55:09 +0200 Subject: [PATCH] Add test for font selection by texmanager. --- lib/matplotlib/tests/test_texmanager.py | 27 ++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_texmanager.py b/lib/matplotlib/tests/test_texmanager.py index 170a8362e287..67e077b3117d 100644 --- a/lib/matplotlib/tests/test_texmanager.py +++ b/lib/matplotlib/tests/test_texmanager.py @@ -1,11 +1,13 @@ +from pathlib import Path +import re + import matplotlib.pyplot as plt from matplotlib.texmanager import TexManager +import pytest def test_fontconfig_preamble(): - """ - Test that the preamble is included in _fontconfig - """ + """Test that the preamble is included in _fontconfig.""" plt.rcParams['text.usetex'] = True tm1 = TexManager() @@ -16,3 +18,22 @@ def test_fontconfig_preamble(): font_config2 = tm2.get_font_config() assert font_config1 != font_config2 + + +@pytest.mark.parametrize( + "rc, preamble, family", [ + ({"font.family": "sans-serif", "font.sans-serif": "helvetica"}, + r"\usepackage{helvet}", r"\sffamily"), + ({"font.family": "serif", "font.serif": "palatino"}, + r"\usepackage{mathpazo}", r"\rmfamily"), + ({"font.family": "cursive", "font.cursive": "zapf chancery"}, + r"\usepackage{chancery}", r"\rmfamily"), + ({"font.family": "monospace", "font.monospace": "courier"}, + r"\usepackage{courier}", r"\ttfamily"), + ]) +def test_font_selection(rc, preamble, family): + plt.rcParams.update(rc) + tm = TexManager() + src = Path(tm.make_tex("hello, world", fontsize=12)).read_text() + assert preamble in src + assert [*re.findall(r"\\\w+family", src)] == [family]