Skip to content

Commit bbeb86f

Browse files
committed
TST: add tests for warnings and avoid warnings in tests
1 parent 384b1eb commit bbeb86f

File tree

1 file changed

+70
-4
lines changed

1 file changed

+70
-4
lines changed

lib/matplotlib/tests/test_animation.py

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
from pathlib import Path
33
import platform
4+
import re
45
import subprocess
56
import sys
67
import weakref
@@ -233,8 +234,11 @@ def test_animation_repr_html(writer, html, want, anim):
233234
assert want in html
234235

235236

236-
@pytest.mark.parametrize('anim', [dict(frames=iter(range(5)))],
237-
indirect=['anim'])
237+
@pytest.mark.parametrize(
238+
'anim',
239+
[{'save_count': 10, 'frames': iter(range(5))}],
240+
indirect=['anim']
241+
)
238242
def test_no_length_frames(anim):
239243
anim.save('unused.null', writer=NullMovieWriter())
240244

@@ -330,9 +334,11 @@ def frames_generator():
330334

331335
yield frame
332336

337+
MAX_FRAMES = 100
333338
anim = animation.FuncAnimation(fig, animate, init_func=init,
334339
frames=frames_generator,
335-
cache_frame_data=cache_frame_data)
340+
cache_frame_data=cache_frame_data,
341+
save_count=MAX_FRAMES)
336342

337343
writer = NullMovieWriter()
338344
anim.save('unused.null', writer=writer)
@@ -372,7 +378,9 @@ def animate(i):
372378
return return_value
373379

374380
with pytest.raises(RuntimeError):
375-
animation.FuncAnimation(fig, animate, blit=True)
381+
animation.FuncAnimation(
382+
fig, animate, blit=True, cache_frame_data=False
383+
)
376384

377385

378386
def test_exhausted_animation(tmpdir):
@@ -440,3 +448,61 @@ def animate(i):
440448

441449
# 5th frame's data
442450
ax.plot(x, np.sin(x + 4 / 100))
451+
452+
453+
@pytest.mark.parametrize('anim', [dict(klass=dict)], indirect=['anim'])
454+
def test_save_count_override_warnings_has_length(anim):
455+
456+
save_count = 5
457+
frames = list(range(2))
458+
match_target = (
459+
f'You passed in an explicit {save_count=} '
460+
"which is being ignored in favor of "
461+
f"{len(frames)=}."
462+
)
463+
464+
with pytest.warns(UserWarning, match=re.escape(match_target)):
465+
anim = animation.FuncAnimation(
466+
**{**anim, 'frames': frames, 'save_count': save_count}
467+
)
468+
assert anim._save_count == len(frames)
469+
anim._init_draw()
470+
471+
472+
@pytest.mark.parametrize('anim', [dict(klass=dict)], indirect=['anim'])
473+
def test_save_count_override_warnings_scaler(anim):
474+
save_count = 5
475+
frames = 7
476+
match_target = (
477+
f'You passed in an explicit {save_count=} ' +
478+
"which is being ignored in favor of " +
479+
f"{frames=}."
480+
)
481+
482+
with pytest.warns(UserWarning, match=re.escape(match_target)):
483+
anim = animation.FuncAnimation(
484+
**{**anim, 'frames': frames, 'save_count': save_count}
485+
)
486+
487+
assert anim._save_count == frames
488+
anim._init_draw()
489+
490+
491+
@pytest.mark.parametrize('anim', [dict(klass=dict)], indirect=['anim'])
492+
def test_disable_cache_warning(anim):
493+
cache_frame_data = True
494+
frames = iter(range(5))
495+
match_target = (
496+
f"{frames=!r} which we can infer the length of, "
497+
"did not pass an explicit *save_count* "
498+
f"and passed {cache_frame_data=}. To avoid a possibly "
499+
"unbounded cache, frame data caching has been disabled. "
500+
"To suppress this warning either pass "
501+
"`cache_frame_data=False` or `save_count=MAX_FRAMES`."
502+
)
503+
with pytest.warns(UserWarning, match=re.escape(match_target)):
504+
anim = animation.FuncAnimation(
505+
**{**anim, 'cache_frame_data': cache_frame_data, 'frames': frames}
506+
)
507+
assert anim._cache_frame_data is False
508+
anim._init_draw()

0 commit comments

Comments
 (0)