test_pyplot.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. def test_pyplot_up_to_date():
  9. gen_script = Path(mpl.__file__).parents[2] / "tools/boilerplate.py"
  10. if not gen_script.exists():
  11. pytest.skip("boilerplate.py not found")
  12. orig_contents = Path(plt.__file__).read_text()
  13. try:
  14. subprocess.run([sys.executable, str(gen_script)], check=True)
  15. new_contents = Path(plt.__file__).read_text()
  16. if orig_contents != new_contents:
  17. diff_msg = '\n'.join(
  18. difflib.unified_diff(
  19. orig_contents.split('\n'), new_contents.split('\n'),
  20. fromfile='found pyplot.py',
  21. tofile='expected pyplot.py',
  22. n=0, lineterm=''))
  23. pytest.fail(
  24. "pyplot.py is not up-to-date. Please run "
  25. "'python tools/boilerplate.py' to update pyplot.py. "
  26. "This needs to be done from an environment where your "
  27. "current working copy is installed (e.g. 'pip install -e'd). "
  28. "Here is a diff of unexpected differences:\n%s" % diff_msg
  29. )
  30. finally:
  31. Path(plt.__file__).write_text(orig_contents)
  32. def test_pyplot_box():
  33. fig, ax = plt.subplots()
  34. plt.box(False)
  35. assert not ax.get_frame_on()
  36. plt.box(True)
  37. assert ax.get_frame_on()
  38. plt.box()
  39. assert not ax.get_frame_on()
  40. plt.box()
  41. assert ax.get_frame_on()
  42. def test_stackplot_smoke():
  43. # Small smoke test for stackplot (see #12405)
  44. plt.stackplot([1, 2, 3], [1, 2, 3])