Skip to content

Commit

Permalink
Merge pull request #39 from executablebooks/add-tests
Browse files Browse the repository at this point in the history
TEST: enable xml doctree tracking
  • Loading branch information
mmcky authored Nov 1, 2021
2 parents 4a28f71 + 9367977 commit a3cb4f6
Show file tree
Hide file tree
Showing 46 changed files with 859 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# Install pre-commit hooks via
# pre-commit install

exclude: >
(?x)^(
\.vscode/settings\.json|
docs/source/conf\.py|
tests/test_exercise/.*|
tests/test_solution/.*|
tests/test_hiddendirective/.*|
)$
repos:

- repo: git://github.com/pre-commit/pre-commit-hooks
Expand Down
28 changes: 28 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import shutil
import pytest

from pathlib import Path
from sphinx.testing.path import path

pytest_plugins = "sphinx.testing.fixtures"
Expand All @@ -22,3 +23,30 @@ def read(app):
return app._warning.getvalue().strip()

return read


@pytest.fixture
def get_sphinx_app_doctree(file_regression):
def read(app, docname="index", resolve=False, regress=False, flatten_outdir=False):
if resolve:
doctree = app.env.get_and_resolve_doctree(docname, app.builder)
extension = ".resolved.xml"
else:
doctree = app.env.get_doctree(docname)
extension = ".xml"

# convert absolute filenames
for node in doctree.traverse(lambda n: "source" in n):
node["source"] = Path(node["source"]).name

if flatten_outdir:
docname = docname.split("/")[-1]

if regress:
file_regression.check(
doctree.pformat(), basename=docname, extension=extension
)

return doctree

return read
40 changes: 40 additions & 0 deletions tests/test_exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,46 @@ def test_exercise(app, idir, file_regression):
file_regression.check(str(excs), basename=idir.split(".")[0], extension=".html")


@pytest.mark.sphinx("html", testroot="mybook")
@pytest.mark.parametrize(
"docname",
[
"_enum_duplicate_label",
"_enum_mathtitle_label",
"_enum_notitle_label",
"_enum_numref_mathtitle",
"_enum_numref_notitle",
"_enum_numref_placeholders",
"_enum_numref_title",
"_enum_ref_mathtitle",
"_enum_ref_notitle",
"_enum_ref_title",
"_enum_title_class_label",
"_enum_title_nolabel",
"_unenum_mathtitle_label",
"_unenum_notitle_label",
"_unenum_numref_mathtitle",
"_unenum_numref_notitle",
"_unenum_numref_title",
"_unenum_ref_mathtitle",
"_unenum_ref_notitle",
"_unenum_ref_title",
"_unenum_title_class_label",
"_unenum_title_nolabel",
],
)
def test_exercise_doctree(app, docname, file_regression, get_sphinx_app_doctree):
app.build()
docname = "exercise" + "/" + docname
get_sphinx_app_doctree(
app,
docname,
resolve=False,
regress=True,
flatten_outdir=True, # noqa: E501 flatten files "solution/<file> -> <file>.xml" for convenience
)


@pytest.mark.sphinx("html", testroot="mybook")
@pytest.mark.parametrize(
"idir",
Expand Down
10 changes: 10 additions & 0 deletions tests/test_exercise/_enum_duplicate_label.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<document source="_enum_duplicate_label.rst">
<section ids="enum-duplicate-label" names="_enum_duplicate_label">
<title>
_enum_duplicate_label
<exercise_node classes="exercise" docname="exercise/_enum_duplicate_label" hidden="False" ids="dup" label="dup" names="dup" title="(duplicate directive 1)" type="exercise">
<title>
(duplicate directive 1)
<section ids="exercise-content">
<paragraph>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
13 changes: 13 additions & 0 deletions tests/test_exercise/_enum_mathtitle_label.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<document source="_enum_mathtitle_label.rst">
<section ids="enum-mathtitle-label" names="_enum_mathtitle_label">
<title>
_enum_mathtitle_label
<exercise_node classes="exercise" docname="exercise/_enum_mathtitle_label" hidden="False" ids="test-exc-label-math" label="test-exc-label-math" names="test-exc-label-math" title="(Test :math:`\mathbb{R}`)" type="exercise">
<title>
(Test
<math>
\mathbb{R}
)
<section ids="exercise-content">
<paragraph>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
10 changes: 10 additions & 0 deletions tests/test_exercise/_enum_notitle_label.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<document source="_enum_notitle_label.rst">
<section ids="enum-notitle-label" names="_enum_notitle_label">
<title>
_enum_notitle_label
<exercise_node classes="exercise" docname="exercise/_enum_notitle_label" hidden="False" ids="text-exc-notitle" label="text-exc-notitle" names="text-exc-notitle" title="Exercise " type="exercise">
<title>
Exercise
<section ids="exercise-content">
<paragraph>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
34 changes: 34 additions & 0 deletions tests/test_exercise/_enum_numref_mathtitle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<document source="_enum_numref_mathtitle.rst">
<section ids="enum-numref-mathtitle" names="_enum_numref_mathtitle">
<title>
_enum_numref_mathtitle
<paragraph>
This is a reference
<pending_xref refdoc="exercise/_enum_numref_mathtitle" refdomain="std" refexplicit="False" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
test-exc-label-math
.
<paragraph>
This is a second reference
<pending_xref refdoc="exercise/_enum_numref_mathtitle" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some text
.
<paragraph>
This is a third reference
<pending_xref refdoc="exercise/_enum_numref_mathtitle" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some text %s
.
<paragraph>
This is a fourth reference
<pending_xref refdoc="exercise/_enum_numref_mathtitle" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some text {number}
.
<paragraph>
This is a fifth reference
<pending_xref refdoc="exercise/_enum_numref_mathtitle" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some text {name}
.
34 changes: 34 additions & 0 deletions tests/test_exercise/_enum_numref_notitle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<document source="_enum_numref_notitle.rst">
<section ids="enum-numref-notitle" names="_enum_numref_notitle">
<title>
_enum_numref_notitle
<paragraph>
This is a reference
<pending_xref refdoc="exercise/_enum_numref_notitle" refdomain="std" refexplicit="False" reftarget="text-exc-notitle" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
text-exc-notitle
.
<paragraph>
This is a second reference
<pending_xref refdoc="exercise/_enum_numref_notitle" refdomain="std" refexplicit="True" reftarget="text-exc-notitle" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some text
.
<paragraph>
This is a third reference
<pending_xref refdoc="exercise/_enum_numref_notitle" refdomain="std" refexplicit="True" reftarget="text-exc-notitle" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some text %s
.
<paragraph>
This is a fourth reference
<pending_xref refdoc="exercise/_enum_numref_notitle" refdomain="std" refexplicit="True" reftarget="text-exc-notitle" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some text {number}
.
<paragraph>
This is a fifth reference
<pending_xref refdoc="exercise/_enum_numref_notitle" refdomain="std" refexplicit="True" reftarget="text-exc-notitle" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some text {name}
.
64 changes: 64 additions & 0 deletions tests/test_exercise/_enum_numref_placeholders.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<document source="_enum_numref_placeholders.rst">
<section ids="enum-numref-placeholders" names="_enum_numref_placeholders">
<title>
_enum_numref_placeholders
<paragraph>
This reference does not include math
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some {number} text %s test {name}
.
<paragraph>
This reference does not include math
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some {number} text %s test
.
<paragraph>
This reference does not include math
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some {name} text %s test
.
<paragraph>
This reference does not include math
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some {name} text {number} test
.
<paragraph>
This reference does not include math
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some %s text test
.
<paragraph>
This reference includes math
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some {number} text %s test {name}
.
<paragraph>
This reference includes math
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some {number} text %s test
.
<paragraph>
This reference includes math
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some {name} text %s test
.
<paragraph>
This reference includes math
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some {name} text {number} test
.
<paragraph>
This reference includes math
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some %s text test
.
34 changes: 34 additions & 0 deletions tests/test_exercise/_enum_numref_title.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<document source="_enum_numref_title.rst">
<section ids="enum-numref-title" names="_enum_numref_title">
<title>
_enum_numref_title
<paragraph>
This is a reference
<pending_xref refdoc="exercise/_enum_numref_title" refdomain="std" refexplicit="False" reftarget="test-exc-label" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
test-exc-label
.
<paragraph>
This is a second reference
<pending_xref refdoc="exercise/_enum_numref_title" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some text
.
<paragraph>
This is a third reference
<pending_xref refdoc="exercise/_enum_numref_title" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some text %s
.
<paragraph>
This is a fourth reference
<pending_xref refdoc="exercise/_enum_numref_title" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some text {number}
.
<paragraph>
This is a fifth reference
<pending_xref refdoc="exercise/_enum_numref_title" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
<literal classes="xref std std-numref">
some text {name}
.
16 changes: 16 additions & 0 deletions tests/test_exercise/_enum_ref_mathtitle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<document source="_enum_ref_mathtitle.rst">
<section ids="enum-ref-mathtitle" names="_enum_ref_mathtitle">
<title>
_enum_ref_mathtitle
<paragraph>
This is a reference
<pending_xref refdoc="exercise/_enum_ref_mathtitle" refdomain="std" refexplicit="False" reftarget="test-exc-label-math" reftype="ref" refwarn="True">
<inline classes="xref std std-ref">
test-exc-label-math
.
<paragraph>
This is a second reference
<pending_xref refdoc="exercise/_enum_ref_mathtitle" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="ref" refwarn="True">
<inline classes="xref std std-ref">
some text
.
16 changes: 16 additions & 0 deletions tests/test_exercise/_enum_ref_notitle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<document source="_enum_ref_notitle.rst">
<section ids="enum-ref-notitle" names="_enum_ref_notitle">
<title>
_enum_ref_notitle
<paragraph>
This is a reference
<pending_xref refdoc="exercise/_enum_ref_notitle" refdomain="std" refexplicit="False" reftarget="text-exc-notitle" reftype="ref" refwarn="True">
<inline classes="xref std std-ref">
text-exc-notitle
.
<paragraph>
This is a second reference
<pending_xref refdoc="exercise/_enum_ref_notitle" refdomain="std" refexplicit="True" reftarget="text-exc-notitle" reftype="ref" refwarn="True">
<inline classes="xref std std-ref">
some text
.
16 changes: 16 additions & 0 deletions tests/test_exercise/_enum_ref_title.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<document source="_enum_ref_title.rst">
<section ids="enum-ref-title" names="_enum_ref_title">
<title>
_enum_ref_title
<paragraph>
This is a reference
<pending_xref refdoc="exercise/_enum_ref_title" refdomain="std" refexplicit="False" reftarget="test-exc-label" reftype="ref" refwarn="True">
<inline classes="xref std std-ref">
test-exc-label
.
<paragraph>
This is a second reference
<pending_xref refdoc="exercise/_enum_ref_title" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="ref" refwarn="True">
<inline classes="xref std std-ref">
some text
.
10 changes: 10 additions & 0 deletions tests/test_exercise/_enum_title_class_label.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<document source="_enum_title_class_label.rst">
<section ids="enum-title-class-label" names="_enum_title_class_label">
<title>
_enum_title_class_label
<exercise_node classes="exercise test-exc" docname="exercise/_enum_title_class_label" hidden="False" ids="test-exc-label" label="test-exc-label" names="test-exc-label" title="(Test exercise directive)" type="exercise">
<title>
(Test exercise directive)
<section ids="exercise-content">
<paragraph>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
10 changes: 10 additions & 0 deletions tests/test_exercise/_enum_title_nolabel.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<document source="_enum_title_nolabel.rst">
<section ids="enum-title-nolabel" names="_enum_title_nolabel">
<title>
_enum_title_nolabel
<exercise_node classes="exercise" docname="exercise/_enum_title_nolabel" hidden="False" ids="exercise/_enum_title_nolabel-exercise-0" label="exercise/_enum_title_nolabel-exercise-0" names="exercise/_enum_title_nolabel-exercise-0" title="(Test exercise directive)" type="exercise">
<title>
(Test exercise directive)
<section ids="exercise-content">
<paragraph>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Loading

0 comments on commit a3cb4f6

Please sign in to comment.