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

Skip to content

Commit 1ec4655

Browse files
committed
Add some test
1 parent 6a8031a commit 1ec4655

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

lib/matplotlib/tests/test_backend_gtk3.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import os
12
from matplotlib import pyplot as plt
23

34
import pytest
5+
from unittest import mock
46

57

68
pytest.importorskip("matplotlib.backends.backend_gtk3agg")
@@ -49,3 +51,21 @@ def receive(event):
4951
fig.canvas.mpl_connect("draw_event", send)
5052
fig.canvas.mpl_connect("key_press_event", receive)
5153
plt.show()
54+
55+
56+
@pytest.mark.backend("gtk3agg", skip_on_importerror=True)
57+
def test_save_figure_return():
58+
from gi.repository import Gtk
59+
fig, ax = plt.subplots()
60+
ax.imshow([[1]])
61+
with mock.patch("gi.repository.Gtk.FileFilter") as fileFilter:
62+
filt = fileFilter.return_value
63+
filt.get_name.return_value = "Portable Network Graphics"
64+
with mock.patch("gi.repository.Gtk.FileChooserDialog") as dialogChooser:
65+
dialog = dialogChooser.return_value
66+
dialog.get_filter.return_value = filt
67+
dialog.get_filename.return_value = "foobar.png"
68+
dialog.run.return_value = Gtk.ResponseType.OK
69+
fname = fig.canvas.manager.toolbar.save_figure()
70+
os.remove("foobar.png")
71+
assert fname == "foobar.png"

lib/matplotlib/tests/test_backend_macosx.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
import pytest
4+
from unittest import mock
45

56
import matplotlib as mpl
67
import matplotlib.pyplot as plt
@@ -44,3 +45,14 @@ def new_choose_save_file(title, directory, filename):
4445
# Check the savefig.directory rcParam got updated because
4546
# we added a subdirectory "test"
4647
assert mpl.rcParams["savefig.directory"] == f"{tmp_path}/test"
48+
49+
50+
@pytest.mark.backend('macosx')
51+
def test_save_figure_return():
52+
fig, ax = plt.subplots()
53+
ax.imshow([[1]])
54+
prop = "matplotlib.backends._macosx.choose_save_file"
55+
with mock.patch(prop, return_value="foobar.png"):
56+
fname = fig.canvas.manager.toolbar.save_figure()
57+
os.remove("foobar.png")
58+
assert fname == "foobar.png"

lib/matplotlib/tests/test_backend_qt.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,17 @@ def test_figureoptions():
228228
fig.canvas.manager.toolbar.edit_parameters()
229229

230230

231+
@pytest.mark.backend('QtAgg', skip_on_importerror=True)
232+
def test_save_figure_return():
233+
fig, ax = plt.subplots()
234+
ax.imshow([[1]])
235+
prop = "matplotlib.backends.qt_compat.QtWidgets.QFileDialog.getSaveFileName"
236+
with mock.patch(prop, return_value=("foobar.png", None)):
237+
fname = fig.canvas.manager.toolbar.save_figure()
238+
os.remove("foobar.png")
239+
assert fname == "foobar.png"
240+
241+
231242
@pytest.mark.backend('QtAgg', skip_on_importerror=True)
232243
def test_figureoptions_with_datetime_axes():
233244
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)