Skip to content

Commit 07d4530

Browse files
authored
Merge pull request matplotlib#19261 from anntzer/mathtextlightweighttests
Add machinery for png-only, single-font mathtext tests.
2 parents 33ef669 + 7f7fad9 commit 07d4530

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,7 +2158,8 @@ def __init__(self):
21582158

21592159
p.sqrt <<= Group(
21602160
Suppress(Literal(r"\sqrt"))
2161-
- ((Optional(p.lbracket + p.int_literal + p.rbracket, default=None)
2161+
- ((Group(Optional(
2162+
p.lbracket + OneOrMore(~p.rbracket + p.token) + p.rbracket))
21622163
+ p.required_group)
21632164
| Error("Expected \\sqrt{value}"))
21642165
)
@@ -2864,10 +2865,10 @@ def sqrt(self, s, loc, toks):
28642865

28652866
# Add the root and shift it upward so it is above the tick.
28662867
# The value of 0.6 is a hard-coded hack ;)
2867-
if root is None:
2868+
if not root:
28682869
root = Box(check.width * 0.5, 0., 0.)
28692870
else:
2870-
root = Hlist([Char(x, state) for x in root])
2871+
root = Hlist(root)
28712872
root.shrink()
28722873
root.shrink()
28732874

1.85 KB
Loading

lib/matplotlib/tests/test_mathtext.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@
110110
r'$\left(X\right)_{a}^{b}$', # github issue 7615
111111
r'$\dfrac{\$100.00}{y}$', # github issue #1888
112112
]
113+
# 'Lightweight' tests test only a single fontset (dejavusans, which is the
114+
# default) and only png outputs, in order to minimize the size of baseline
115+
# images.
116+
lightweight_math_tests = [
117+
r'$\sqrt[ab]{123}$', # github issue #8665
118+
]
113119

114120
digits = "0123456789"
115121
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@@ -165,42 +171,48 @@
165171
for set in chars:
166172
font_tests.append(wrapper % set)
167173

168-
font_tests = list(filter(lambda x: x[1] is not None, enumerate(font_tests)))
169-
170174

171175
@pytest.fixture
172-
def baseline_images(request, fontset, index):
176+
def baseline_images(request, fontset, index, text):
177+
if text is None:
178+
pytest.skip("test has been removed")
173179
return ['%s_%s_%02d' % (request.param, fontset, index)]
174180

175181

176-
cur_math_tests = list(filter(lambda x: x[1] is not None, enumerate(math_tests)))
177-
178-
179-
@pytest.mark.parametrize('index, test', cur_math_tests,
180-
ids=[str(index) for index, _ in cur_math_tests])
181-
@pytest.mark.parametrize('fontset',
182-
['cm', 'stix', 'stixsans', 'dejavusans',
183-
'dejavuserif'])
182+
@pytest.mark.parametrize(
183+
'index, text', enumerate(math_tests), ids=range(len(math_tests)))
184+
@pytest.mark.parametrize(
185+
'fontset', ['cm', 'stix', 'stixsans', 'dejavusans', 'dejavuserif'])
184186
@pytest.mark.parametrize('baseline_images', ['mathtext'], indirect=True)
185187
@image_comparison(baseline_images=None)
186-
def test_mathtext_rendering(baseline_images, fontset, index, test):
188+
def test_mathtext_rendering(baseline_images, fontset, index, text):
187189
mpl.rcParams['mathtext.fontset'] = fontset
188190
fig = plt.figure(figsize=(5.25, 0.75))
189-
fig.text(0.5, 0.5, test,
191+
fig.text(0.5, 0.5, text,
190192
horizontalalignment='center', verticalalignment='center')
191193

192194

193-
@pytest.mark.parametrize('index, test', font_tests,
194-
ids=[str(index) for index, _ in font_tests])
195-
@pytest.mark.parametrize('fontset',
196-
['cm', 'stix', 'stixsans', 'dejavusans',
197-
'dejavuserif'])
195+
@pytest.mark.parametrize('index, text', enumerate(lightweight_math_tests),
196+
ids=range(len(lightweight_math_tests)))
197+
@pytest.mark.parametrize('fontset', ['dejavusans'])
198+
@pytest.mark.parametrize('baseline_images', ['mathtext1'], indirect=True)
199+
@image_comparison(baseline_images=None, extensions=['png'])
200+
def test_mathtext_rendering_lightweight(baseline_images, fontset, index, text):
201+
fig = plt.figure(figsize=(5.25, 0.75))
202+
fig.text(0.5, 0.5, text, math_fontfamily=fontset,
203+
horizontalalignment='center', verticalalignment='center')
204+
205+
206+
@pytest.mark.parametrize(
207+
'index, text', enumerate(font_tests), ids=range(len(font_tests)))
208+
@pytest.mark.parametrize(
209+
'fontset', ['cm', 'stix', 'stixsans', 'dejavusans', 'dejavuserif'])
198210
@pytest.mark.parametrize('baseline_images', ['mathfont'], indirect=True)
199211
@image_comparison(baseline_images=None, extensions=['png'])
200-
def test_mathfont_rendering(baseline_images, fontset, index, test):
212+
def test_mathfont_rendering(baseline_images, fontset, index, text):
201213
mpl.rcParams['mathtext.fontset'] = fontset
202214
fig = plt.figure(figsize=(5.25, 0.75))
203-
fig.text(0.5, 0.5, test,
215+
fig.text(0.5, 0.5, text,
204216
horizontalalignment='center', verticalalignment='center')
205217

206218

0 commit comments

Comments
 (0)