test_svg2pdf.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """Tests for the svg2pdf preprocessor"""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. from nbformat import v4 as nbformat
  5. from .base import PreprocessorTestsBase
  6. from ..svg2pdf import SVG2PDFPreprocessor
  7. from ...tests.utils import onlyif_cmds_exist
  8. class Testsvg2pdf(PreprocessorTestsBase):
  9. """Contains test functions for svg2pdf.py"""
  10. simple_svg = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
  11. <!-- Created with Inkscape (http://www.inkscape.org/) -->
  12. <svg
  13. xmlns:svg="http://www.w3.org/2000/svg"
  14. xmlns="http://www.w3.org/2000/svg"
  15. version="1.0"
  16. x="0.00000000"
  17. y="0.00000000"
  18. width="500.00000"
  19. height="500.00000"
  20. id="svg2">
  21. <defs
  22. id="defs4" />
  23. <g
  24. id="layer1">
  25. <rect
  26. width="300.00000"
  27. height="300.00000"
  28. x="100.00000"
  29. y="100.00000"
  30. style="opacity:1.0000000;fill:none;fill-opacity:1.0000000;fill-rule:evenodd;stroke:#000000;stroke-width:8.0000000;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4.0000000;stroke-dasharray:none;stroke-dashoffset:0.00000000;stroke-opacity:1.0000000"
  31. id="rect5719" />
  32. </g>
  33. </svg>"""
  34. def build_notebook(self):
  35. """Build a reveal slides notebook in memory for use with tests.
  36. Overrides base in PreprocessorTestsBase"""
  37. outputs = [nbformat.new_output(output_type='display_data',
  38. data={'image/svg+xml':self.simple_svg})
  39. ]
  40. cells=[nbformat.new_code_cell(source="", execution_count=1, outputs=outputs)]
  41. return nbformat.new_notebook(cells=cells)
  42. def build_preprocessor(self):
  43. """Make an instance of a preprocessor"""
  44. preprocessor = SVG2PDFPreprocessor()
  45. preprocessor.enabled = True
  46. return preprocessor
  47. def test_constructor(self):
  48. """Can a SVG2PDFPreprocessor be constructed?"""
  49. self.build_preprocessor()
  50. @onlyif_cmds_exist('inkscape')
  51. def test_output(self):
  52. """Test the output of the SVG2PDFPreprocessor"""
  53. nb = self.build_notebook()
  54. res = self.build_resources()
  55. preprocessor = self.build_preprocessor()
  56. nb, res = preprocessor(nb, res)
  57. self.assertIn('application/pdf', nb.cells[0].outputs[0].data)