Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a22a47

Browse files
committed
Add some animation tests.
1 parent d610a5d commit 0a22a47

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

lib/matplotlib/tests/test_animation.py

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
from __future__ import absolute_import, division, print_function
2-
3-
import six
4-
1+
from pathlib import Path
52
import sys
63
import tempfile
74

@@ -42,25 +39,30 @@ def finish(self):
4239
pass
4340

4441

45-
def test_null_movie_writer():
46-
# Test running an animation with NullMovieWriter.
47-
48-
fig = plt.figure()
42+
def make_animation(**kwargs):
43+
fig, ax = plt.subplots()
44+
line, = ax.plot([])
4945

5046
def init():
5147
pass
5248

5349
def animate(i):
54-
pass
50+
line.set_data([0, 1], [0, i])
51+
return line,
52+
53+
return animation.FuncAnimation(fig, animate, **kwargs)
54+
55+
56+
def test_null_movie_writer():
57+
# Test running an animation with NullMovieWriter.
58+
59+
anim = make_animation(frames=5)
5560

56-
num_frames = 5
5761
filename = "unused.null"
5862
dpi = 50
5963
savefig_kwargs = dict(foo=0)
60-
61-
anim = animation.FuncAnimation(fig, animate, init_func=init,
62-
frames=num_frames)
6364
writer = NullMovieWriter()
65+
6466
anim.save(filename, dpi=dpi, writer=writer,
6567
savefig_kwargs=savefig_kwargs)
6668

@@ -178,23 +180,8 @@ def animate(i):
178180

179181

180182
def test_no_length_frames():
181-
fig, ax = plt.subplots()
182-
line, = ax.plot([], [])
183-
184-
def init():
185-
line.set_data([], [])
186-
return line,
187-
188-
def animate(i):
189-
x = np.linspace(0, 10, 100)
190-
y = np.sin(x + i)
191-
line.set_data(x, y)
192-
return line,
193-
194-
anim = animation.FuncAnimation(fig, animate, init_func=init,
195-
frames=iter(range(5)))
196-
writer = NullMovieWriter()
197-
anim.save('unused.null', writer=writer)
183+
(make_animation(frames=iter(range(5)))
184+
.save('unused.null', writer=NullMovieWriter()))
198185

199186

200187
def test_movie_writer_registry():
@@ -212,10 +199,30 @@ def test_movie_writer_registry():
212199
assert not animation.writers.is_available("ffmpeg")
213200
# something which is guaranteed to be available in path
214201
# and exits immediately
215-
bin = u"true" if sys.platform != 'win32' else u"where"
202+
bin = "true" if sys.platform != 'win32' else "where"
216203
mpl.rcParams['animation.ffmpeg_path'] = bin
217204
assert animation.writers._dirty
218205
animation.writers.list() # resets
219206
assert not animation.writers._dirty
220207
assert animation.writers.is_available("ffmpeg")
221208
mpl.rcParams['animation.ffmpeg_path'] = ffmpeg_path
209+
210+
211+
@pytest.mark.parametrize("method_name", ["to_html5_video", "to_jshtml"])
212+
def test_embed_limit(method_name, caplog):
213+
mpl.rcParams["animation.embed_limit"] = 1e-6 # Approximately 1 byte.
214+
getattr(make_animation(frames=1), method_name)()
215+
assert len(caplog.records) == 1
216+
record, = caplog.records
217+
assert (record.name == "matplotlib.animation"
218+
and record.levelname == "WARNING")
219+
220+
221+
@pytest.mark.parametrize(
222+
"method_name",
223+
["to_html5_video",
224+
pytest.mark.xfail("to_jshtml")]) # Needs to be fixed.
225+
def test_cleanup_temporaries(method_name, tmpdir):
226+
with tmpdir.as_cwd():
227+
getattr(make_animation(frames=1), method_name)()
228+
assert list(Path(tmpdir).iterdir()) == []

0 commit comments

Comments
 (0)