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

Skip to content

Commit b2d78d1

Browse files
committed
Add tests for ToolManager
1 parent 23730b4 commit b2d78d1

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

lib/matplotlib/backend_managers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def update_keymap(self, name, key):
185185
Keys to associate with the tool.
186186
"""
187187
if name not in self._tools:
188-
raise KeyError(f'{name} not in Tools')
188+
raise KeyError(f'{name!r} not in Tools')
189189
self._remove_keys(name)
190190
if isinstance(key, str):
191191
key = [key]
@@ -404,6 +404,7 @@ def get_tool(self, name, warn=True):
404404
return name
405405
if name not in self._tools:
406406
if warn:
407-
_api.warn_external(f"ToolManager does not control tool {name}")
407+
_api.warn_external(
408+
f"ToolManager does not control tool {name!r}")
408409
return None
409410
return self._tools[name]

lib/matplotlib/tests/test_backend_bases.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from matplotlib.backend_bases import (
55
FigureCanvasBase, LocationEvent, MouseButton, MouseEvent,
66
NavigationToolbar2, RendererBase)
7+
from matplotlib.backend_tools import RubberbandBase
78
from matplotlib.figure import Figure
89
from matplotlib.testing._markers import needs_pgf_xelatex
910
import matplotlib.pyplot as plt
@@ -349,3 +350,56 @@ def test_interactive_pan(key, mouseend, expectedxlim, expectedylim):
349350
# Should be close, but won't be exact due to screen integer resolution
350351
assert tuple(ax.get_xlim()) == pytest.approx(expectedxlim, abs=0.02)
351352
assert tuple(ax.get_ylim()) == pytest.approx(expectedylim, abs=0.02)
353+
354+
355+
def test_toolmanager_remove():
356+
expected_warning_regex = (
357+
r"Treat the new Tool classes introduced in "
358+
r"v[0-9]*.[0-9]* as experimental for now; "
359+
"the API and rcParam may change in future versions.")
360+
with pytest.warns(UserWarning, match=expected_warning_regex):
361+
plt.rcParams['toolbar'] = 'toolmanager'
362+
fig = plt.gcf()
363+
initial_len = len(fig.canvas.manager.toolmanager.tools)
364+
assert 'forward' in fig.canvas.manager.toolmanager.tools.keys()
365+
fig.canvas.manager.toolmanager.remove_tool('forward')
366+
assert len(fig.canvas.manager.toolmanager.tools) == initial_len - 1
367+
assert 'forward' not in fig.canvas.manager.toolmanager.tools.keys()
368+
369+
370+
def test_toolmanager_get_tool():
371+
expected_warning_regex = (
372+
r"Treat the new Tool classes introduced in "
373+
r"v[0-9]*.[0-9]* as experimental for now; "
374+
"the API and rcParam may change in future versions.")
375+
with pytest.warns(UserWarning, match=expected_warning_regex):
376+
plt.rcParams['toolbar'] = 'toolmanager'
377+
fig = plt.gcf()
378+
rubberband = fig.canvas.manager.toolmanager.get_tool('rubberband')
379+
assert isinstance(rubberband, RubberbandBase)
380+
assert fig.canvas.manager.toolmanager.get_tool(rubberband) is rubberband
381+
with pytest.warns(UserWarning,
382+
match="ToolManager does not control tool 'foo'"):
383+
assert fig.canvas.manager.toolmanager.get_tool('foo') is None
384+
assert fig.canvas.manager.toolmanager.get_tool('foo', warn=False) is None
385+
386+
with pytest.warns(UserWarning,
387+
match="ToolManager does not control tool 'foo'"):
388+
assert fig.canvas.manager.toolmanager.trigger_tool('foo') is None
389+
390+
391+
def test_toolmanager_update_keymap():
392+
expected_warning_regex = (
393+
r"Treat the new Tool classes introduced in "
394+
r"v[0-9]*.[0-9]* as experimental for now; "
395+
"the API and rcParam may change in future versions.")
396+
with pytest.warns(UserWarning, match=expected_warning_regex):
397+
plt.rcParams['toolbar'] = 'toolmanager'
398+
fig = plt.gcf()
399+
assert 'v' in fig.canvas.manager.toolmanager.get_tool_keymap('forward')
400+
with pytest.warns(UserWarning,
401+
match="Key c changed from back to forward"):
402+
fig.canvas.manager.toolmanager.update_keymap('forward', 'c')
403+
assert fig.canvas.manager.toolmanager.get_tool_keymap('forward') == ['c']
404+
with pytest.raises(KeyError, match="'foo' not in Tools"):
405+
fig.canvas.manager.toolmanager.update_keymap('foo', 'c')

0 commit comments

Comments
 (0)