1"""Tests for the svg2pdf preprocessor""" 2 3# Copyright (c) IPython Development Team. 4# Distributed under the terms of the Modified BSD License. 5 6from nbformat import v4 as nbformat 7from unittest.mock import patch, Mock 8 9from .base import PreprocessorTestsBase 10from ..svg2pdf import SVG2PDFPreprocessor 11from ...tests.utils import onlyif_cmds_exist 12 13 14class Testsvg2pdf(PreprocessorTestsBase): 15 """Contains test functions for svg2pdf.py""" 16 17 simple_svg = """<?xml version="1.0" encoding="UTF-8" standalone="no"?> 18<!-- Created with Inkscape (http://www.inkscape.org/) --> 19<svg 20 xmlns:svg="http://www.w3.org/2000/svg" 21 xmlns="http://www.w3.org/2000/svg" 22 version="1.0" 23 x="0.00000000" 24 y="0.00000000" 25 width="500.00000" 26 height="500.00000" 27 id="svg2"> 28 <defs 29 id="defs4" /> 30 <g 31 id="layer1"> 32 <rect 33 width="300.00000" 34 height="300.00000" 35 x="100.00000" 36 y="100.00000" 37 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" 38 id="rect5719" /> 39 </g> 40</svg>""" 41 42 def build_notebook(self): 43 """Build a reveal slides notebook in memory for use with tests. 44 Overrides base in PreprocessorTestsBase""" 45 46 outputs = [nbformat.new_output(output_type='display_data', 47 data={'image/svg+xml':self.simple_svg}) 48 ] 49 50 cells=[nbformat.new_code_cell(source="", execution_count=1, outputs=outputs)] 51 52 return nbformat.new_notebook(cells=cells) 53 54 55 def build_preprocessor(self, **kwargs): 56 """Make an instance of a preprocessor""" 57 preprocessor = SVG2PDFPreprocessor(**kwargs) 58 preprocessor.enabled = True 59 return preprocessor 60 61 62 def test_constructor(self): 63 """Can a SVG2PDFPreprocessor be constructed?""" 64 self.build_preprocessor() 65 66 67 @onlyif_cmds_exist('inkscape') 68 def test_output(self): 69 """Test the output of the SVG2PDFPreprocessor""" 70 nb = self.build_notebook() 71 res = self.build_resources() 72 preprocessor = self.build_preprocessor() 73 nb, res = preprocessor(nb, res) 74 self.assertIn('application/pdf', nb.cells[0].outputs[0].data) 75 76 @patch('subprocess.Popen') 77 def test_inkscape_version_default(self, mock_popen): 78 mock_popen().communicate.return_value = (b'Inkscape 0.92.3 (2405546, 2018-03-11)', b'') 79 mock_popen().returncode = 0 80 81 preprocessor = self.build_preprocessor() 82 assert preprocessor.inkscape_version == '0.92.3' 83 84 def test_inkscape_pre_v1_command(self): 85 preprocessor = self.build_preprocessor(inkscape_version='0.92.3') 86 assert preprocessor.command == '0.92.3' 87 88 def test_inkscape_pre_v1_command(self): 89 preprocessor = self.build_preprocessor(inkscape='fake-inkscape', inkscape_version='0.92.3') 90 assert preprocessor.command == [ 91 'fake-inkscape', 92 '--without-gui', 93 '--export-pdf={to_filename}', 94 '{from_filename}' 95 ] 96 97 def test_inkscape_v1_command(self): 98 preprocessor = self.build_preprocessor(inkscape='fake-inkscape', inkscape_version='1.0beta2') 99 assert preprocessor.command == [ 100 'fake-inkscape', 101 '--export-filename={to_filename}', 102 '{from_filename}' 103 ] 104