test_backend_template.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """
  2. Backend-loading machinery tests, using variations on the template backend.
  3. """
  4. import sys
  5. from types import SimpleNamespace
  6. from unittest.mock import MagicMock
  7. import matplotlib as mpl
  8. from matplotlib import pyplot as plt
  9. from matplotlib.backends import backend_template
  10. from matplotlib.backends.backend_template import (
  11. FigureCanvasTemplate, FigureManagerTemplate)
  12. def test_load_template():
  13. mpl.use("template")
  14. assert type(plt.figure().canvas) == FigureCanvasTemplate
  15. def test_load_old_api(monkeypatch):
  16. mpl_test_backend = SimpleNamespace(**vars(backend_template))
  17. mpl_test_backend.new_figure_manager = (
  18. lambda num, *args, FigureClass=mpl.figure.Figure, **kwargs:
  19. FigureManagerTemplate(
  20. FigureCanvasTemplate(FigureClass(*args, **kwargs)), num))
  21. monkeypatch.setitem(sys.modules, "mpl_test_backend", mpl_test_backend)
  22. mpl.use("module://mpl_test_backend")
  23. assert type(plt.figure().canvas) == FigureCanvasTemplate
  24. plt.draw_if_interactive()
  25. def test_show(monkeypatch):
  26. mpl_test_backend = SimpleNamespace(**vars(backend_template))
  27. mock_show = MagicMock()
  28. monkeypatch.setattr(
  29. mpl_test_backend.FigureManagerTemplate, "pyplot_show", mock_show)
  30. monkeypatch.setitem(sys.modules, "mpl_test_backend", mpl_test_backend)
  31. mpl.use("module://mpl_test_backend")
  32. plt.show()
  33. mock_show.assert_called_with()
  34. def test_show_old_global_api(monkeypatch):
  35. mpl_test_backend = SimpleNamespace(**vars(backend_template))
  36. mock_show = MagicMock()
  37. monkeypatch.setattr(mpl_test_backend, "show", mock_show, raising=False)
  38. monkeypatch.setitem(sys.modules, "mpl_test_backend", mpl_test_backend)
  39. mpl.use("module://mpl_test_backend")
  40. plt.show()
  41. mock_show.assert_called_with()
  42. def test_load_case_sensitive(monkeypatch):
  43. mpl_test_backend = SimpleNamespace(**vars(backend_template))
  44. mock_show = MagicMock()
  45. monkeypatch.setattr(
  46. mpl_test_backend.FigureManagerTemplate, "pyplot_show", mock_show)
  47. monkeypatch.setitem(sys.modules, "mpl_Test_Backend", mpl_test_backend)
  48. mpl.use("module://mpl_Test_Backend")
  49. plt.show()
  50. mock_show.assert_called_with()