-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nbconvert: Transformer tests #3914
Changes from 20 commits
cedf738
ac99eb0
97a9537
1fae527
e78ff9c
b1f37db
d00bed1
b8f2f5e
8593daa
1642fb9
5201e89
d3d056d
b011672
d5fe07e
72286e9
a334f68
9f1ca07
7368969
0de1b7f
a275585
2aec628
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does metadata need to be a ResourceDict ? Or should we also test the fact that when it not a resource dict thing works ? (you probably know nbconvert more than me now) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, just so we can do conversions if something doesn't exist (like notebook name). Otherwise Jinja will crash when parsing if exporting without a notebook name. |
||
return res |
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") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
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 'inlining' in res | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. slightly dangerous as if something try to read something from
Or am I miss understanding ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch :) Will change |
||
assert 'css' in res['inlining'] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
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'] | ||
|
||
# Make sure an entry to the resources was added. | ||
assert 'outputs' in res | ||
|
||
# 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') |
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$') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
Module with tests for the revealhelp 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 IPython.nbformat import current as nbformat | ||
|
||
from .base import TransformerTestsBase | ||
from ..revealhelp import RevealHelpTransformer | ||
|
||
|
||
#----------------------------------------------------------------------------- | ||
# Class | ||
#----------------------------------------------------------------------------- | ||
|
||
class Testrevealhelp(TransformerTestsBase): | ||
"""Contains test functions for revealhelp.py""" | ||
|
||
def build_notebook(self): | ||
"""Build a reveal slides notebook in memory for use with tests. | ||
Overrides base in TransformerTestsBase""" | ||
|
||
outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="a")] | ||
|
||
slide_metadata = {'slideshow' : {'slide_type': 'slide'}} | ||
subslide_metadata = {'slideshow' : {'slide_type': 'subslide'}} | ||
|
||
cells=[nbformat.new_code_cell(input="", prompt_number=1, outputs=outputs), | ||
nbformat.new_text_cell('markdown', source="", metadata=slide_metadata), | ||
nbformat.new_code_cell(input="", prompt_number=2, outputs=outputs), | ||
nbformat.new_text_cell('markdown', source="", metadata=slide_metadata), | ||
nbformat.new_text_cell('markdown', source="", metadata=subslide_metadata)] | ||
worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)] | ||
|
||
return nbformat.new_notebook(name="notebook1", worksheets=worksheets) | ||
|
||
|
||
def build_transformer(self): | ||
"""Make an instance of a transformer""" | ||
transformer = RevealHelpTransformer() | ||
transformer.enabled = True | ||
return transformer | ||
|
||
|
||
def test_constructor(self): | ||
"""Can a RevealHelpTransformer be constructed?""" | ||
self.build_transformer() | ||
|
||
|
||
def test_reveal_attribute(self): | ||
"""Make sure the reveal url_prefix resources is set""" | ||
nb = self.build_notebook() | ||
res = self.build_resources() | ||
transformer = self.build_transformer() | ||
nb, res = transformer(nb, res) | ||
assert 'reveal' in res | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to |
||
assert 'url_prefix' in res['reveal'] | ||
|
||
|
||
def test_reveal_output(self): | ||
"""Make sure that the reveal transformer """ | ||
nb = self.build_notebook() | ||
res = self.build_resources() | ||
transformer = self.build_transformer() | ||
nb, res = transformer(nb, res) | ||
cells = nb.worksheets[0].cells | ||
|
||
# Make sure correct metadata tags are available on every cell. | ||
for cell in cells: | ||
assert 'slide_type' in cell.metadata | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's change all raw There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're just about to drop support for Python 2.6, so we'll be able to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think @takluyver don't like |
||
assert 'align_type' in cell.metadata | ||
|
||
# Make sure slide end is only applied to the cells preceeding slide | ||
# cells. | ||
assert 'slide_helper' not in cells[1].metadata | ||
|
||
# Verify 'slide-end' | ||
assert 'slide_helper' in cells[0].metadata | ||
self.assertEqual(cells[0].metadata['slide_helper'], 'slide_end') | ||
assert 'slide_helper' in cells[2].metadata | ||
self.assertEqual(cells[2].metadata['slide_helper'], 'slide_end') | ||
assert 'slide_helper' in cells[3].metadata | ||
self.assertEqual(cells[3].metadata['slide_helper'], 'subslide_end') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also use
IPython.nbformat.v3.tests.nbexamples
for an example notebook