diff options
Diffstat (limited to 'Master/texmf-dist/doc/latex/jupynotex/tests')
4 files changed, 430 insertions, 0 deletions
diff --git a/Master/texmf-dist/doc/latex/jupynotex/tests/run b/Master/texmf-dist/doc/latex/jupynotex/tests/run new file mode 100755 index 00000000000..a5fa69f44d2 --- /dev/null +++ b/Master/texmf-dist/doc/latex/jupynotex/tests/run @@ -0,0 +1,3 @@ +#!/bin/sh + +PYTHONPATH=. fades -d pytest -x pytest -sv "$@" diff --git a/Master/texmf-dist/doc/latex/jupynotex/tests/test_cellparser.py b/Master/texmf-dist/doc/latex/jupynotex/tests/test_cellparser.py new file mode 100644 index 00000000000..f9108f4bb8f --- /dev/null +++ b/Master/texmf-dist/doc/latex/jupynotex/tests/test_cellparser.py @@ -0,0 +1,79 @@ +# Copyright 2020 Facundo Batista +# All Rights Reserved +# Licensed under Apache 2.0 + +import pytest +import re + +from jupynotex import _parse_cells + + +def test_empty(): + msg = "Empty cells spec not allowed" + with pytest.raises(ValueError, match=re.escape(msg)): + _parse_cells('', 100) + + +def test_simple(): + r = _parse_cells('1', 100) + assert r == [1] + + +def test_several_comma(): + r = _parse_cells('1,3,5,9,7', 100) + assert r == [1, 3, 5, 7, 9] + + +def test_several_range(): + r = _parse_cells('1-9', 100) + assert r == [1, 2, 3, 4, 5, 6, 7, 8, 9] + + +def test_several_limited(): + msg = "Notebook loaded of len 3, smaller than requested cells: [1, 2, 3, 4]" + with pytest.raises(ValueError, match=re.escape(msg)): + _parse_cells('1-4', 3) + + +def test_range_default_start(): + r = _parse_cells('-3', 8) + assert r == [1, 2, 3] + + +def test_range_default_end(): + r = _parse_cells('5-', 8) + assert r == [5, 6, 7, 8] + + +def test_not_int(): + msg = "Found forbidden characters in cells definition (allowed digits, '-' and ',')" + with pytest.raises(ValueError, match=re.escape(msg)): + _parse_cells('1,a', 3) + + +def test_not_positive(): + msg = "Cells need to be >=1" + with pytest.raises(ValueError, match=re.escape(msg)): + _parse_cells('3,0', 3) + + +def test_several_mixed(): + r = _parse_cells('1,3,5-7,2,9,11-13', 80) + assert r == [1, 2, 3, 5, 6, 7, 9, 11, 12, 13] + + +def test_overlapped(): + r = _parse_cells('3,5-7,6-9,8', 80) + assert r == [3, 5, 6, 7, 8, 9] + + +def test_bad_range_equal(): + msg = "Range 'from' need to be smaller than 'to' (got '12-12')" + with pytest.raises(ValueError, match=re.escape(msg)): + _parse_cells('12-12', 80) + + +def test_bad_range_smaller(): + msg = "Range 'from' need to be smaller than 'to' (got '3-2')" + with pytest.raises(ValueError, match=re.escape(msg)): + _parse_cells('3-2', 80) diff --git a/Master/texmf-dist/doc/latex/jupynotex/tests/test_main.py b/Master/texmf-dist/doc/latex/jupynotex/tests/test_main.py new file mode 100644 index 00000000000..fabfa4e01eb --- /dev/null +++ b/Master/texmf-dist/doc/latex/jupynotex/tests/test_main.py @@ -0,0 +1,111 @@ +# Copyright 2020 Facundo Batista +# All Rights Reserved +# Licensed under Apache 2.0 + +import textwrap + +import jupynotex +from jupynotex import main + + +class FakeNotebook: + """Fake notebook. + + The instance supports calling (as it if were instantiated). The .get will return the + value in a dict for received key; raise it if exception. + """ + + def __init__(self, side_effects): + self.side_effects = side_effects + + def __call__(self, path): + return self + + def __len__(self): + return len(self.side_effects) + + def get(self, key): + """Return or raise the stored side effect.""" + value = self.side_effects[key] + if isinstance(value, Exception): + raise value + else: + return value + + +def test_simple_ok(monkeypatch, capsys): + fake_notebook = FakeNotebook({ + 1: ("test cell content up", "test cell content down"), + }) + monkeypatch.setattr(jupynotex, 'Notebook', fake_notebook) + + main('boguspath', '1') + expected = textwrap.dedent("""\ + \\begin{tcolorbox}[title=Cell {01}] + test cell content up + \\tcblower + test cell content down + \\end{tcolorbox} + """) + assert expected == capsys.readouterr().out + + +def test_simple_only_first(monkeypatch, capsys): + fake_notebook = FakeNotebook({ + 1: ("test cell content up", ""), + }) + monkeypatch.setattr(jupynotex, 'Notebook', fake_notebook) + + main('boguspath', '1') + expected = textwrap.dedent("""\ + \\begin{tcolorbox}[title=Cell {01}] + test cell content up + \\end{tcolorbox} + """) + assert expected == capsys.readouterr().out + + +def test_simple_error(monkeypatch, capsys): + fake_notebook = FakeNotebook({ + 1: ValueError("test problem"), + }) + monkeypatch.setattr(jupynotex, 'Notebook', fake_notebook) + + main('boguspath', '1') + + # verify the beginning and the end, as the middle part is specific to the environment + # where the test runs + expected_ini = [ + r"\begin{tcolorbox}[colback=red!5!white,colframe=red!75!,title={ERROR when parsing cell 1}]", # NOQA + r"\begin{verbatim}", + r"Traceback (most recent call last):", + ] + expected_end = [ + r"ValueError: test problem", + r"\end{verbatim}", + r"\end{tcolorbox}", + ] + out = [line for line in capsys.readouterr().out.split('\n') if line] + assert expected_ini == out[:3] + assert expected_end == out[-3:] + + +def test_multiple(monkeypatch, capsys): + fake_notebook = FakeNotebook({ + 1: ("test cell content up", "test cell content down"), + 2: ("test cell content ONLY up", ""), + }) + monkeypatch.setattr(jupynotex, 'Notebook', fake_notebook) + + main('boguspath', '1-2') + expected = textwrap.dedent("""\ + \\begin{tcolorbox}[title=Cell {01}] + test cell content up + \\tcblower + test cell content down + \\end{tcolorbox} + \\begin{tcolorbox}[title=Cell {02}] + test cell content ONLY up + \\end{tcolorbox} + """) + assert expected == capsys.readouterr().out diff --git a/Master/texmf-dist/doc/latex/jupynotex/tests/test_notebook.py b/Master/texmf-dist/doc/latex/jupynotex/tests/test_notebook.py new file mode 100644 index 00000000000..bf6511cbd30 --- /dev/null +++ b/Master/texmf-dist/doc/latex/jupynotex/tests/test_notebook.py @@ -0,0 +1,237 @@ +# Copyright 2020 Facundo Batista +# All Rights Reserved +# Licensed under Apache 2.0 + +import base64 +import json +import os +import pathlib +import re +import tempfile +import textwrap + +import pytest + +from jupynotex import Notebook + + +@pytest.fixture +def notebook(): + _, name = tempfile.mkstemp() + + def _f(cells): + with open(name, 'wt', encoding='utf8') as fh: + json.dump({'cells': cells}, fh) + + return Notebook(name) + + yield _f + os.unlink(name) + + +def test_empty(notebook): + nb = notebook([]) + assert len(nb) == 0 + + +def test_source_code(notebook): + rawcell = { + 'cell_type': 'code', + 'source': ['line1\n', ' line2\n'], + } + nb = notebook([rawcell]) + assert len(nb) == 1 + + src, _ = nb.get(1) + expected = textwrap.dedent("""\ + \\begin{verbatim} + line1 + line2 + \\end{verbatim} + """) + assert src == expected + + +def test_source_markdown(notebook): + rawcell = { + 'cell_type': 'markdown', + 'source': ['line1\n', ' line2\n'], + } + nb = notebook([rawcell]) + assert len(nb) == 1 + + src, _ = nb.get(1) + expected = textwrap.dedent("""\ + \\begin{verbatim} + line1 + line2 + \\end{verbatim} + """) + assert src == expected + + +def test_output_missing(notebook): + rawcell = { + 'cell_type': 'code', + 'source': [], + } + nb = notebook([rawcell]) + assert len(nb) == 1 + + _, out = nb.get(1) + assert out is None + + +def test_output_simple_executeresult_plain(notebook): + rawcell = { + 'cell_type': 'code', + 'source': [], + 'outputs': [ + { + 'output_type': 'execute_result', + 'data': { + 'text/plain': ['default always present', 'line2'], + }, + }, + ], + } + nb = notebook([rawcell]) + assert len(nb) == 1 + + _, out = nb.get(1) + expected = textwrap.dedent("""\ + \\begin{verbatim} + default always present + line2 + \\end{verbatim} + """) + assert out == expected + + +def test_output_simple_executeresult_latex(notebook): + rawcell = { + 'cell_type': 'code', + 'source': [], + 'outputs': [ + { + 'output_type': 'execute_result', + 'data': { + 'text/latex': ['some latex line', 'latex 2'], + 'text/plain': ['default always present'], + }, + }, + ], + } + nb = notebook([rawcell]) + assert len(nb) == 1 + + _, out = nb.get(1) + expected = textwrap.dedent("""\ + some latex line + latex 2 + """) + assert out == expected + + +def test_output_simple_executeresult_image(notebook): + raw_content = b"\x01\x02 asdlklda3wudghlaskgdlask" + rawcell = { + 'cell_type': 'code', + 'source': [], + 'outputs': [ + { + 'output_type': 'execute_result', + 'data': { + 'image/png': base64.b64encode(raw_content).decode('ascii'), + 'text/plain': ['default always present'], + }, + }, + ], + } + nb = notebook([rawcell]) + assert len(nb) == 1 + + _, out = nb.get(1) + m = re.match(r'\\includegraphics\{(.+)\}\n', out) + assert m + (fpath,) = m.groups() + assert pathlib.Path(fpath).read_bytes() == raw_content + + +def test_output_simple_stream(notebook): + rawcell = { + 'cell_type': 'code', + 'source': [], + 'outputs': [ + { + 'output_type': 'stream', + 'text': ['some text line', 'text 2'], + }, + ], + } + nb = notebook([rawcell]) + assert len(nb) == 1 + + _, out = nb.get(1) + expected = textwrap.dedent("""\ + \\begin{verbatim} + some text line + text 2 + \\end{verbatim} + """) + assert out == expected + + +def test_output_simple_display_data(notebook): + raw_content = b"\x01\x02 asdlklda3wudghlaskgdlask" + rawcell = { + 'cell_type': 'code', + 'source': [], + 'outputs': [ + { + 'output_type': 'display_data', + 'data': { + 'image/png': base64.b64encode(raw_content).decode('ascii'), + }, + }, + ], + } + nb = notebook([rawcell]) + assert len(nb) == 1 + + _, out = nb.get(1) + m = re.match(r'\\includegraphics\{(.+)\}\n', out) + assert m + (fpath,) = m.groups() + assert pathlib.Path(fpath).read_bytes() == raw_content + + +def test_output_multiple(notebook): + rawcell = { + 'cell_type': 'code', + 'source': [], + 'outputs': [ + { + 'output_type': 'execute_result', + 'data': { + 'text/latex': ['some latex line', 'latex 2'], + }, + }, { + 'output_type': 'stream', + 'text': ['some text line', 'text 2'], + }, + ], + } + nb = notebook([rawcell]) + assert len(nb) == 1 + + _, out = nb.get(1) + expected = textwrap.dedent("""\ + some latex line + latex 2 + \\begin{verbatim} + some text line + text 2 + \\end{verbatim} + """) + assert out == expected |