def test_draw_frame(): # test _draw_frame method fig, ax = plt.subplots() line, = ax.plot([]) def animate(i, arg): # general update func line.set_data([0, 1], [0, i]) if arg: return arg with pytest.raises(RuntimeError): # user forgot to return (returns None) animation.FuncAnimation(fig, animate, blit=True, fargs=(None, )) # user (for some reason) returned a string...AttributeError is raised animation.FuncAnimation(fig, animate, blit=True, fargs=('string', )) # user (for some reason) returned a string...AttributeError is raised animation.FuncAnimation(fig, animate, blit=True, fargs=(1, )) # user returns a sequence of other objects # e.g. a string instead of Artist animation.FuncAnimation(fig, animate, blit=True, fargs=(('string', ), )) # user forgot to put comma or return a sequence # TypeError will be raised (same with returning a number or bool) artist_obj = artist.Artist() animation.FuncAnimation(fig, animate, blit=True, fargs=(artist_obj, ))
def test_set_alpha(): art = martist.Artist() with pytest.raises(TypeError, match='^alpha must be numeric or None'): art.set_alpha('string') with pytest.raises(TypeError, match='^alpha must be numeric or None'): art.set_alpha([1, 2, 3]) with pytest.raises(ValueError, match="outside 0-1 range"): art.set_alpha(1.1) with pytest.raises(ValueError, match="outside 0-1 range"): art.set_alpha(np.nan)
def test_set_alpha_for_array(): art = martist.Artist() with pytest.raises(TypeError, match='^alpha must be numeric or None'): art._set_alpha_for_array('string') with pytest.raises(ValueError, match="outside 0-1 range"): art._set_alpha_for_array(1.1) with pytest.raises(ValueError, match="outside 0-1 range"): art._set_alpha_for_array(np.nan) with pytest.raises(ValueError, match="alpha must be between 0 and 1"): art._set_alpha_for_array([0.5, 1.1]) with pytest.raises(ValueError, match="alpha must be between 0 and 1"): art._set_alpha_for_array([0.5, np.nan])
def test_callbacks(): def func(artist): func.counter += 1 func.counter = 0 art = martist.Artist() oid = art.add_callback(func) assert func.counter == 0 art.pchanged() # must call the callback assert func.counter == 1 art.set_zorder(10) # setting a property must also call the callback assert func.counter == 2 art.remove_callback(oid) art.pchanged() # must not call the callback anymore assert func.counter == 2