def test_regression(): # Test documentation worked example input_nb_fname = pjoin(DATA, 'example_notebook.ipynb') output_rst_fname = pjoin(DATA, 'converted_example.rst') # Convert to ReST, add trailing CR from output script rst = convert_nb_fname(input_nb_fname) + '\n' assert rst.encode('utf8') == fcontents(output_rst_fname) # Convert ReST to output formats py_file = to_py.from_rst(rst) assert (py_file.encode('utf8') == fcontents( pjoin(DATA, 'converted_plus_notebooks.py'))) ipy_file = to_notebook.from_rst(rst) assert_nb_equiv( ipy_file, fcontents(pjoin(DATA, 'converted_plus_notebooks.ipynb')).decode('utf8'))
def test_default_mathdollar(): # Test mathdollar extension present by default. ipynb = to_notebook.from_rst(r'Some text with $a = 1$ math.') expected = r"""{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Some text with $a = 1$ math." ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 1 }""" assert_nb_equiv(ipynb, expected)
def assert_rst_cells_equal(rst_text, cells): actual = to_notebook.from_rst(rst_text) expected = cells2json(cells) assert actual == expected
def to_nb_safe(rst_str): out = to_notebook.from_rst(rst_str) return unsmart_nb(out)
def test_notebook_basic(): # Test conversion of basic ReST to ipynb JSON ipynb = to_notebook.from_rst(r""" Title ===== Some text with :math:`a = 1` math. .. math:: \textrm{math block} .. nbplot:: >>> c = 1 >>> c 1 More text. .. nbplot:: >>> d = 2 >>> d 2""") expected = r"""{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Title\n", "\n", "Some text with $a = 1$ math.\n", "\n", "$$\n", "\\textrm{math block}\n", "$$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c = 1\n", "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "More text." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = 2\n", "d" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 1 }""" assert_nb_equiv(ipynb, expected)
def assert_rst_cells_equal(rst_text, cells): actual = to_notebook.from_rst(rst_text) expected = cells2json(cells) assert rm_json_id(actual) == rm_json_id(expected)
def assert_conv_equal(rst_str, expected): assert_nb_equiv(to_notebook.from_rst(rst_str), expected)