test_pyplot.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import difflib
  2. import subprocess
  3. import sys
  4. from pathlib import Path
  5. import pytest
  6. import matplotlib as mpl
  7. from matplotlib import pyplot as plt
  8. from matplotlib.cbook import MatplotlibDeprecationWarning
  9. def test_pyplot_up_to_date(tmpdir):
  10. gen_script = Path(mpl.__file__).parents[2] / "tools/boilerplate.py"
  11. if not gen_script.exists():
  12. pytest.skip("boilerplate.py not found")
  13. orig_contents = Path(plt.__file__).read_text()
  14. plt_file = tmpdir.join('pyplot.py')
  15. plt_file.write_text(orig_contents, 'utf-8')
  16. subprocess.run([sys.executable, str(gen_script), str(plt_file)],
  17. check=True)
  18. new_contents = plt_file.read_text('utf-8')
  19. if orig_contents != new_contents:
  20. diff_msg = '\n'.join(
  21. difflib.unified_diff(
  22. orig_contents.split('\n'), new_contents.split('\n'),
  23. fromfile='found pyplot.py',
  24. tofile='expected pyplot.py',
  25. n=0, lineterm=''))
  26. pytest.fail(
  27. "pyplot.py is not up-to-date. Please run "
  28. "'python tools/boilerplate.py' to update pyplot.py. "
  29. "This needs to be done from an environment where your "
  30. "current working copy is installed (e.g. 'pip install -e'd). "
  31. "Here is a diff of unexpected differences:\n%s" % diff_msg
  32. )
  33. def test_copy_docstring_and_deprecators(recwarn):
  34. @mpl.cbook._rename_parameter("(version)", "old", "new")
  35. @mpl.cbook._make_keyword_only("(version)", "kwo")
  36. def func(new, kwo=None):
  37. pass
  38. @plt._copy_docstring_and_deprecators(func)
  39. def wrapper_func(new, kwo=None):
  40. pass
  41. wrapper_func(None)
  42. wrapper_func(new=None)
  43. wrapper_func(None, kwo=None)
  44. wrapper_func(new=None, kwo=None)
  45. assert not recwarn
  46. with pytest.warns(MatplotlibDeprecationWarning):
  47. wrapper_func(old=None)
  48. with pytest.warns(MatplotlibDeprecationWarning):
  49. wrapper_func(None, None)
  50. def test_pyplot_box():
  51. fig, ax = plt.subplots()
  52. plt.box(False)
  53. assert not ax.get_frame_on()
  54. plt.box(True)
  55. assert ax.get_frame_on()
  56. plt.box()
  57. assert not ax.get_frame_on()
  58. plt.box()
  59. assert ax.get_frame_on()
  60. def test_stackplot_smoke():
  61. # Small smoke test for stackplot (see #12405)
  62. plt.stackplot([1, 2, 3], [1, 2, 3])
  63. def test_nrows_error():
  64. with pytest.raises(TypeError):
  65. plt.subplot(nrows=1)
  66. with pytest.raises(TypeError):
  67. plt.subplot(ncols=1)