test_rst.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """Tests for RSTExporter"""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import io
  5. import re
  6. import nbformat
  7. from nbformat import v4
  8. from .base import ExportersTestsBase
  9. from ..rst import RSTExporter
  10. from ...tests.utils import onlyif_cmds_exist
  11. class TestRSTExporter(ExportersTestsBase):
  12. """Tests for RSTExporter"""
  13. exporter_class = RSTExporter
  14. should_include_raw = ['rst']
  15. def test_constructor(self):
  16. """
  17. Can a RSTExporter be constructed?
  18. """
  19. RSTExporter()
  20. @onlyif_cmds_exist('pandoc')
  21. def test_export(self):
  22. """
  23. Can a RSTExporter export something?
  24. """
  25. (output, resources) = RSTExporter().from_filename(self._get_notebook())
  26. assert len(output) > 0
  27. @onlyif_cmds_exist('pandoc')
  28. def test_empty_code_cell(self):
  29. """No empty code cells in rst"""
  30. nbname = self._get_notebook()
  31. with io.open(nbname, encoding='utf8') as f:
  32. nb = nbformat.read(f, 4)
  33. exporter = self.exporter_class()
  34. (output, resources) = exporter.from_notebook_node(nb)
  35. # add an empty code cell
  36. nb.cells.append(
  37. v4.new_code_cell(source="")
  38. )
  39. (output2, resources) = exporter.from_notebook_node(nb)
  40. # adding an empty code cell shouldn't change output
  41. self.assertEqual(output.strip(), output2.strip())
  42. @onlyif_cmds_exist('pandoc')
  43. def test_png_metadata(self):
  44. """
  45. Does RSTExporter treat pngs with width/height metadata correctly?
  46. """
  47. (output, resources) = RSTExporter().from_filename(
  48. self._get_notebook(nb_name="pngmetadata.ipynb"))
  49. assert len(output) > 0
  50. check_for_png = re.compile(
  51. r'.. image::.*?\n\s+(.*?)\n\s*\n',
  52. re.DOTALL)
  53. result = check_for_png.search(output)
  54. assert result is not None
  55. attr_string = result.group(1)
  56. assert ':width:' in attr_string
  57. assert ':height:' in attr_string
  58. assert 'px' in attr_string