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

Skip to content

Commit a4ac25b

Browse files
committed
Add tests for resolving backend name and gui loop
1 parent f9ce8c2 commit a4ac25b

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

IPython/core/tests/test_pylabtools.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,77 @@ def test_figure_no_canvas():
268268
fig = Figure()
269269
fig.canvas = None
270270
pt.print_figure(fig)
271+
272+
273+
@pytest.mark.parametrize("name, expected_gui, expected_backend", [
274+
# name is gui
275+
("gtk3", "gtk3", "gtk3agg"),
276+
("gtk4", "gtk4", "gtk4agg"),
277+
("headless", "headless", "agg"),
278+
("osx", "osx", "macosx"),
279+
("qt", "qt", "qtagg"),
280+
("qt5", "qt5", "qt5agg"),
281+
("qt6", "qt6", "qt6agg"),
282+
("tk", "tk", "tkagg"),
283+
("wx", "wx", "wxagg"),
284+
# name is backend
285+
("agg", None, "agg"),
286+
("cairo", None, "cairo"),
287+
("pdf", None, "pdf"),
288+
("ps", None, "ps"),
289+
("svg", None, "svg"),
290+
("template", None, "template"),
291+
("gtk3agg", "gtk3", "gtk3agg"),
292+
("gtk3cairo", "gtk3", "gtk3cairo"),
293+
("gtk4agg", "gtk4", "gtk4agg"),
294+
("gtk4cairo", "gtk4", "gtk4cairo"),
295+
("macosx", "osx", "macosx"),
296+
("nbagg", "nbagg", "nbagg"),
297+
("notebook", "nbagg", "notebook"),
298+
("qtagg", "qt", "qtagg"),
299+
("qtcairo", "qt", "qtcairo"),
300+
("qt5agg", "qt5", "qt5agg"),
301+
("qt5cairo", "qt5", "qt5cairo"),
302+
("qt6agg", "qt", "qt6agg"),
303+
("qt6cairo", "qt", "qt6cairo"),
304+
("tkagg", "tk", "tkagg"),
305+
("tkcairo", "tk", "tkcairo"),
306+
("webagg", "webagg", "webagg"),
307+
("wxagg", "wx", "wxagg"),
308+
("wxcairo", "wx", "wxcairo"),
309+
])
310+
def test_backend_builtin(name, expected_gui, expected_backend):
311+
# Test correct identification of Matplotlib built-in backends without importing and using them,
312+
# otherwise we would need to ensure all the complex dependencies such as windowing toolkits are
313+
# installed.
314+
315+
mpl_manages_backends = pt._matplotlib_manages_backends()
316+
if not mpl_manages_backends:
317+
# Backends not supported before _matplotlib_manages_backends or supported
318+
# but with different expected_gui or expected_backend.
319+
if (name.endswith("agg") or name.endswith("cairo") or
320+
name in ("headless", "macosx", "pdf", "ps", "svg", "template")):
321+
pytest.skip()
322+
elif name == "qt6":
323+
expected_backend = "qtagg"
324+
elif name == "notebook":
325+
expected_backend, expected_gui = expected_gui, expected_backend
326+
327+
gui, backend = pt.find_gui_and_backend(name)
328+
if not mpl_manages_backends:
329+
gui = gui.lower() if gui else None
330+
backend = backend.lower() if backend else None
331+
assert gui == expected_gui
332+
assert backend == expected_backend
333+
334+
335+
def test_backend_entry_point():
336+
gui, backend = pt.find_gui_and_backend("inline")
337+
assert gui is None
338+
expected_backend = "inline" if pt._matplotlib_manages_backends() else "module://matplotlib_inline.backend_inline"
339+
assert backend == expected_backend
340+
341+
342+
def test_backend_unknown():
343+
with pytest.raises(RuntimeError if pt._matplotlib_manages_backends() else KeyError):
344+
pt.find_gui_and_backend("name-does-not-exist")

0 commit comments

Comments
 (0)