test_highlightmagics.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """Tests for the HighlightMagics preprocessor"""
  2. from .base import PreprocessorTestsBase
  3. from ..highlightmagics import HighlightMagicsPreprocessor
  4. class TestHighlightMagics(PreprocessorTestsBase):
  5. """Contains test functions for highlightmagics.py"""
  6. def build_preprocessor(self):
  7. """Make an instance of a preprocessor"""
  8. preprocessor = HighlightMagicsPreprocessor()
  9. preprocessor.enabled = True
  10. return preprocessor
  11. def test_constructor(self):
  12. """Can a HighlightMagicsPreprocessor be constructed?"""
  13. self.build_preprocessor()
  14. def test_tagging(self):
  15. """Test the HighlightMagicsPreprocessor tagging"""
  16. nb = self.build_notebook()
  17. res = self.build_resources()
  18. preprocessor = self.build_preprocessor()
  19. nb.cells[0].source = """%%R -i x,y -o XYcoef
  20. lm.fit <- lm(y~x)
  21. par(mfrow=c(2,2))
  22. print(summary(lm.fit))
  23. plot(lm.fit)
  24. XYcoef <- coef(lm.fit)"""
  25. nb, res = preprocessor(nb, res)
  26. assert('magics_language' in nb.cells[0]['metadata'])
  27. self.assertEqual(nb.cells[0]['metadata']['magics_language'], 'r')
  28. def test_no_false_positive(self):
  29. """Test that HighlightMagicsPreprocessor does not tag false positives"""
  30. nb = self.build_notebook()
  31. res = self.build_resources()
  32. preprocessor = self.build_preprocessor()
  33. nb.cells[0].source = """# this should not be detected
  34. print(\"""
  35. %%R -i x, y
  36. \""")"""
  37. nb, res = preprocessor(nb, res)
  38. assert('magics_language' not in nb.cells[0]['metadata'])