forked from ipython/ipython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ipython#3914 from jdfreder/trans_tests
nbconvert: Transformer tests
- Loading branch information
Showing
14 changed files
with
499 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
Module with utility functions for transformer tests | ||
""" | ||
|
||
#----------------------------------------------------------------------------- | ||
# Copyright (c) 2013, the IPython Development Team. | ||
# | ||
# Distributed under the terms of the Modified BSD License. | ||
# | ||
# The full license is in the file COPYING.txt, distributed with this software. | ||
#----------------------------------------------------------------------------- | ||
|
||
#----------------------------------------------------------------------------- | ||
# Imports | ||
#----------------------------------------------------------------------------- | ||
|
||
from IPython.nbformat import current as nbformat | ||
|
||
from ...tests.base import TestsBase | ||
from ...exporters.exporter import ResourcesDict | ||
|
||
#----------------------------------------------------------------------------- | ||
# Class | ||
#----------------------------------------------------------------------------- | ||
|
||
class TransformerTestsBase(TestsBase): | ||
"""Contains test functions transformer tests""" | ||
|
||
|
||
def build_notebook(self): | ||
"""Build a notebook in memory for use with transformer tests""" | ||
|
||
outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="a"), | ||
nbformat.new_output(output_type="text", output_text="b"), | ||
nbformat.new_output(output_type="stream", stream="stdout", output_text="c"), | ||
nbformat.new_output(output_type="stream", stream="stdout", output_text="d"), | ||
nbformat.new_output(output_type="stream", stream="stderr", output_text="e"), | ||
nbformat.new_output(output_type="stream", stream="stderr", output_text="f"), | ||
nbformat.new_output(output_type="png", output_png=b'Zw==')] #g | ||
|
||
cells=[nbformat.new_code_cell(input="$ e $", prompt_number=1,outputs=outputs), | ||
nbformat.new_text_cell('markdown', source="$ e $")] | ||
worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)] | ||
|
||
return nbformat.new_notebook(name="notebook1", worksheets=worksheets) | ||
|
||
|
||
def build_resources(self): | ||
"""Build an empty resources dictionary.""" | ||
|
||
res = ResourcesDict() | ||
res['metadata'] = ResourcesDict() | ||
return res |
38 changes: 38 additions & 0 deletions
38
IPython/nbconvert/transformers/tests/test_coalescestreams.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
Module with tests for the coalescestreams transformer | ||
""" | ||
|
||
#----------------------------------------------------------------------------- | ||
# Copyright (c) 2013, the IPython Development Team. | ||
# | ||
# Distributed under the terms of the Modified BSD License. | ||
# | ||
# The full license is in the file COPYING.txt, distributed with this software. | ||
#----------------------------------------------------------------------------- | ||
|
||
#----------------------------------------------------------------------------- | ||
# Imports | ||
#----------------------------------------------------------------------------- | ||
|
||
from .base import TransformerTestsBase | ||
from ..coalescestreams import coalesce_streams | ||
|
||
|
||
#----------------------------------------------------------------------------- | ||
# Class | ||
#----------------------------------------------------------------------------- | ||
|
||
class TestCoalesceStreams(TransformerTestsBase): | ||
"""Contains test functions for coalescestreams.py""" | ||
|
||
def test_coalesce_streams(self): | ||
"""coalesce_streams transformer output test""" | ||
nb = self.build_notebook() | ||
res = self.build_resources() | ||
nb, res = coalesce_streams(nb, res) | ||
outputs = nb.worksheets[0].cells[0].outputs | ||
self.assertEqual(outputs[0].text, "a") | ||
self.assertEqual(outputs[1].output_type, "text") | ||
self.assertEqual(outputs[2].text, "cd") | ||
self.assertEqual(outputs[3].text, "ef") | ||
|
47 changes: 47 additions & 0 deletions
47
IPython/nbconvert/transformers/tests/test_csshtmlheader.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
Module with tests for the csshtmlheader transformer | ||
""" | ||
|
||
#----------------------------------------------------------------------------- | ||
# Copyright (c) 2013, the IPython Development Team. | ||
# | ||
# Distributed under the terms of the Modified BSD License. | ||
# | ||
# The full license is in the file COPYING.txt, distributed with this software. | ||
#----------------------------------------------------------------------------- | ||
|
||
#----------------------------------------------------------------------------- | ||
# Imports | ||
#----------------------------------------------------------------------------- | ||
|
||
from .base import TransformerTestsBase | ||
from ..csshtmlheader import CSSHTMLHeaderTransformer | ||
|
||
|
||
#----------------------------------------------------------------------------- | ||
# Class | ||
#----------------------------------------------------------------------------- | ||
|
||
class TestCSSHTMLHeader(TransformerTestsBase): | ||
"""Contains test functions for csshtmlheader.py""" | ||
|
||
|
||
def build_transformer(self): | ||
"""Make an instance of a transformer""" | ||
transformer = CSSHTMLHeaderTransformer() | ||
transformer.enabled = True | ||
return transformer | ||
|
||
|
||
def test_constructor(self): | ||
"""Can a CSSHTMLHeaderTransformer be constructed?""" | ||
self.build_transformer() | ||
|
||
|
||
def test_output(self): | ||
"""Test the output of the CSSHTMLHeaderTransformer""" | ||
nb = self.build_notebook() | ||
res = self.build_resources() | ||
transformer = self.build_transformer() | ||
nb, res = transformer(nb, res) | ||
assert 'css' in res['inlining'] |
62 changes: 62 additions & 0 deletions
62
IPython/nbconvert/transformers/tests/test_extractoutput.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
""" | ||
Module with tests for the extractoutput transformer | ||
""" | ||
|
||
#----------------------------------------------------------------------------- | ||
# Copyright (c) 2013, the IPython Development Team. | ||
# | ||
# Distributed under the terms of the Modified BSD License. | ||
# | ||
# The full license is in the file COPYING.txt, distributed with this software. | ||
#----------------------------------------------------------------------------- | ||
|
||
#----------------------------------------------------------------------------- | ||
# Imports | ||
#----------------------------------------------------------------------------- | ||
|
||
from .base import TransformerTestsBase | ||
from ..extractoutput import ExtractOutputTransformer | ||
|
||
|
||
#----------------------------------------------------------------------------- | ||
# Class | ||
#----------------------------------------------------------------------------- | ||
|
||
class TestExtractOutput(TransformerTestsBase): | ||
"""Contains test functions for extractoutput.py""" | ||
|
||
|
||
def build_transformer(self): | ||
"""Make an instance of a transformer""" | ||
transformer = ExtractOutputTransformer() | ||
transformer.enabled = True | ||
return transformer | ||
|
||
|
||
def test_constructor(self): | ||
"""Can a ExtractOutputTransformer be constructed?""" | ||
self.build_transformer() | ||
|
||
|
||
def test_output(self): | ||
"""Test the output of the ExtractOutputTransformer""" | ||
nb = self.build_notebook() | ||
res = self.build_resources() | ||
transformer = self.build_transformer() | ||
nb, res = transformer(nb, res) | ||
|
||
# Check if text was extracted. | ||
assert 'text_filename' in nb.worksheets[0].cells[0].outputs[1] | ||
text_filename = nb.worksheets[0].cells[0].outputs[1]['text_filename'] | ||
|
||
# Check if png was extracted. | ||
assert 'png_filename' in nb.worksheets[0].cells[0].outputs[6] | ||
png_filename = nb.worksheets[0].cells[0].outputs[6]['png_filename'] | ||
|
||
# Verify text output | ||
assert text_filename in res['outputs'] | ||
self.assertEqual(res['outputs'][text_filename], b'b') | ||
|
||
# Verify png output | ||
assert png_filename in res['outputs'] | ||
self.assertEqual(res['outputs'][png_filename], b'g') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
Module with tests for the latex transformer | ||
""" | ||
|
||
#----------------------------------------------------------------------------- | ||
# Copyright (c) 2013, the IPython Development Team. | ||
# | ||
# Distributed under the terms of the Modified BSD License. | ||
# | ||
# The full license is in the file COPYING.txt, distributed with this software. | ||
#----------------------------------------------------------------------------- | ||
|
||
#----------------------------------------------------------------------------- | ||
# Imports | ||
#----------------------------------------------------------------------------- | ||
|
||
from .base import TransformerTestsBase | ||
from ..latex import LatexTransformer | ||
|
||
|
||
#----------------------------------------------------------------------------- | ||
# Class | ||
#----------------------------------------------------------------------------- | ||
|
||
class TestLatex(TransformerTestsBase): | ||
"""Contains test functions for latex.py""" | ||
|
||
|
||
def build_transformer(self): | ||
"""Make an instance of a transformer""" | ||
transformer = LatexTransformer() | ||
transformer.enabled = True | ||
return transformer | ||
|
||
def test_constructor(self): | ||
"""Can a LatexTransformer be constructed?""" | ||
self.build_transformer() | ||
|
||
|
||
def test_output(self): | ||
"""Test the output of the LatexTransformer""" | ||
nb = self.build_notebook() | ||
res = self.build_resources() | ||
transformer = self.build_transformer() | ||
nb, res = transformer(nb, res) | ||
|
||
# Make sure the code cell wasn't modified. | ||
self.assertEqual(nb.worksheets[0].cells[0].input, '$ e $') | ||
|
||
# Verify that the markdown cell was processed. | ||
self.assertEqual(nb.worksheets[0].cells[1].source, '$e$') |
Oops, something went wrong.