def test_sphinx2nb(): # test sphinx2nb script over all .rst files checking against .ipynb files for rst_fname in glob(pjoin(DATA_PATH, '*.rst')): nb_fname = rst_fname[:-3] + 'ipynb' expected = fcontents(nb_fname, 't') cmd = ['sphinx2nb', rst_fname] code, stdout, stderr = run_command(cmd) assert_nb_equiv(unsmart_nb(stdout.decode('utf-8')), expected)
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 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_conv_equal(rst_str, expected): assert_nb_equiv(to_nb_safe(rst_str), expected)
def test_pages(self): txt = self.get_built_file('a_page.txt') assert re.match( r'\n?A title\n\*{7}\n\n\nSome text.\n\n>>> a = 1\n>>> a\n1\n', txt) ipynb = self.get_built_file('a_page.ipynb') assert_nb_equiv( ipynb, r""" { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## A title\n", "\n", "\n", "\n", "\n", "Some text." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 1\n", "a" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 1 }""") assert self.get_built_file('clear.ipynb') == ipynb full = self.get_built_file('full.ipynb') assert_nb_equiv( full, r""" { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## A title\n", "\n", "\n", "\n", "\n", "Some text." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1\n", "a" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 1 }""")