test_clearoutput.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """
  2. Module with tests for the clearoutput preprocessor.
  3. """
  4. # Copyright (c) IPython Development Team.
  5. # Distributed under the terms of the Modified BSD License.
  6. from .base import PreprocessorTestsBase
  7. from ..clearoutput import ClearOutputPreprocessor
  8. class TestClearOutput(PreprocessorTestsBase):
  9. """Contains test functions for clearoutput.py"""
  10. def build_notebook(self):
  11. notebook = super(TestClearOutput, self).build_notebook()
  12. # Add a test field to the first cell
  13. if 'metadata' not in notebook.cells[0]:
  14. notebook.cells[0].metadata = {}
  15. notebook.cells[0].metadata['test_field'] = 'test_value'
  16. return notebook
  17. def build_preprocessor(self):
  18. """Make an instance of a preprocessor"""
  19. preprocessor = ClearOutputPreprocessor()
  20. preprocessor.enabled = True
  21. return preprocessor
  22. def test_constructor(self):
  23. """Can a ClearOutputPreprocessor be constructed?"""
  24. self.build_preprocessor()
  25. def test_output(self):
  26. """Test the output of the ClearOutputPreprocessor"""
  27. for remove_test_field in [False, True]:
  28. nb = self.build_notebook()
  29. res = self.build_resources()
  30. preprocessor = self.build_preprocessor()
  31. # Also remove the test field in addition to defaults
  32. if remove_test_field:
  33. preprocessor.remove_metadata_fields.add('test_field')
  34. nb, res = preprocessor(nb, res)
  35. assert nb.cells[0].outputs == []
  36. assert nb.cells[0].execution_count is None
  37. if 'metadata' in nb.cells[0]:
  38. for field in preprocessor.remove_metadata_fields:
  39. assert field not in nb.cells[0].metadata
  40. # Ensure the test field is only removed when added to the traitlet
  41. assert remove_test_field or 'test_field' in nb.cells[0].metadata