Skip to content

Commit a3cb4f6

Browse files
authored
Merge pull request #39 from executablebooks/add-tests
TEST: enable xml doctree tracking
2 parents 4a28f71 + 9367977 commit a3cb4f6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+859
-0
lines changed

.pre-commit-config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
# Install pre-commit hooks via
22
# pre-commit install
33

4+
exclude: >
5+
(?x)^(
6+
\.vscode/settings\.json|
7+
docs/source/conf\.py|
8+
tests/test_exercise/.*|
9+
tests/test_solution/.*|
10+
tests/test_hiddendirective/.*|
11+
)$
12+
413
repos:
514

615
- repo: git://github.com/pre-commit/pre-commit-hooks

tests/conftest.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import shutil
22
import pytest
33

4+
from pathlib import Path
45
from sphinx.testing.path import path
56

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

2425
return read
26+
27+
28+
@pytest.fixture
29+
def get_sphinx_app_doctree(file_regression):
30+
def read(app, docname="index", resolve=False, regress=False, flatten_outdir=False):
31+
if resolve:
32+
doctree = app.env.get_and_resolve_doctree(docname, app.builder)
33+
extension = ".resolved.xml"
34+
else:
35+
doctree = app.env.get_doctree(docname)
36+
extension = ".xml"
37+
38+
# convert absolute filenames
39+
for node in doctree.traverse(lambda n: "source" in n):
40+
node["source"] = Path(node["source"]).name
41+
42+
if flatten_outdir:
43+
docname = docname.split("/")[-1]
44+
45+
if regress:
46+
file_regression.check(
47+
doctree.pformat(), basename=docname, extension=extension
48+
)
49+
50+
return doctree
51+
52+
return read

tests/test_exercise.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,46 @@ def test_exercise(app, idir, file_regression):
2828
file_regression.check(str(excs), basename=idir.split(".")[0], extension=".html")
2929

3030

31+
@pytest.mark.sphinx("html", testroot="mybook")
32+
@pytest.mark.parametrize(
33+
"docname",
34+
[
35+
"_enum_duplicate_label",
36+
"_enum_mathtitle_label",
37+
"_enum_notitle_label",
38+
"_enum_numref_mathtitle",
39+
"_enum_numref_notitle",
40+
"_enum_numref_placeholders",
41+
"_enum_numref_title",
42+
"_enum_ref_mathtitle",
43+
"_enum_ref_notitle",
44+
"_enum_ref_title",
45+
"_enum_title_class_label",
46+
"_enum_title_nolabel",
47+
"_unenum_mathtitle_label",
48+
"_unenum_notitle_label",
49+
"_unenum_numref_mathtitle",
50+
"_unenum_numref_notitle",
51+
"_unenum_numref_title",
52+
"_unenum_ref_mathtitle",
53+
"_unenum_ref_notitle",
54+
"_unenum_ref_title",
55+
"_unenum_title_class_label",
56+
"_unenum_title_nolabel",
57+
],
58+
)
59+
def test_exercise_doctree(app, docname, file_regression, get_sphinx_app_doctree):
60+
app.build()
61+
docname = "exercise" + "/" + docname
62+
get_sphinx_app_doctree(
63+
app,
64+
docname,
65+
resolve=False,
66+
regress=True,
67+
flatten_outdir=True, # noqa: E501 flatten files "solution/<file> -> <file>.xml" for convenience
68+
)
69+
70+
3171
@pytest.mark.sphinx("html", testroot="mybook")
3272
@pytest.mark.parametrize(
3373
"idir",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<document source="_enum_duplicate_label.rst">
2+
<section ids="enum-duplicate-label" names="_enum_duplicate_label">
3+
<title>
4+
_enum_duplicate_label
5+
<exercise_node classes="exercise" docname="exercise/_enum_duplicate_label" hidden="False" ids="dup" label="dup" names="dup" title="(duplicate directive 1)" type="exercise">
6+
<title>
7+
(duplicate directive 1)
8+
<section ids="exercise-content">
9+
<paragraph>
10+
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.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<document source="_enum_mathtitle_label.rst">
2+
<section ids="enum-mathtitle-label" names="_enum_mathtitle_label">
3+
<title>
4+
_enum_mathtitle_label
5+
<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">
6+
<title>
7+
(Test
8+
<math>
9+
\mathbb{R}
10+
)
11+
<section ids="exercise-content">
12+
<paragraph>
13+
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.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<document source="_enum_notitle_label.rst">
2+
<section ids="enum-notitle-label" names="_enum_notitle_label">
3+
<title>
4+
_enum_notitle_label
5+
<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">
6+
<title>
7+
Exercise
8+
<section ids="exercise-content">
9+
<paragraph>
10+
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.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<document source="_enum_numref_mathtitle.rst">
2+
<section ids="enum-numref-mathtitle" names="_enum_numref_mathtitle">
3+
<title>
4+
_enum_numref_mathtitle
5+
<paragraph>
6+
This is a reference
7+
<pending_xref refdoc="exercise/_enum_numref_mathtitle" refdomain="std" refexplicit="False" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
8+
<literal classes="xref std std-numref">
9+
test-exc-label-math
10+
.
11+
<paragraph>
12+
This is a second reference
13+
<pending_xref refdoc="exercise/_enum_numref_mathtitle" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
14+
<literal classes="xref std std-numref">
15+
some text
16+
.
17+
<paragraph>
18+
This is a third reference
19+
<pending_xref refdoc="exercise/_enum_numref_mathtitle" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
20+
<literal classes="xref std std-numref">
21+
some text %s
22+
.
23+
<paragraph>
24+
This is a fourth reference
25+
<pending_xref refdoc="exercise/_enum_numref_mathtitle" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
26+
<literal classes="xref std std-numref">
27+
some text {number}
28+
.
29+
<paragraph>
30+
This is a fifth reference
31+
<pending_xref refdoc="exercise/_enum_numref_mathtitle" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
32+
<literal classes="xref std std-numref">
33+
some text {name}
34+
.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<document source="_enum_numref_notitle.rst">
2+
<section ids="enum-numref-notitle" names="_enum_numref_notitle">
3+
<title>
4+
_enum_numref_notitle
5+
<paragraph>
6+
This is a reference
7+
<pending_xref refdoc="exercise/_enum_numref_notitle" refdomain="std" refexplicit="False" reftarget="text-exc-notitle" reftype="numref" refwarn="True">
8+
<literal classes="xref std std-numref">
9+
text-exc-notitle
10+
.
11+
<paragraph>
12+
This is a second reference
13+
<pending_xref refdoc="exercise/_enum_numref_notitle" refdomain="std" refexplicit="True" reftarget="text-exc-notitle" reftype="numref" refwarn="True">
14+
<literal classes="xref std std-numref">
15+
some text
16+
.
17+
<paragraph>
18+
This is a third reference
19+
<pending_xref refdoc="exercise/_enum_numref_notitle" refdomain="std" refexplicit="True" reftarget="text-exc-notitle" reftype="numref" refwarn="True">
20+
<literal classes="xref std std-numref">
21+
some text %s
22+
.
23+
<paragraph>
24+
This is a fourth reference
25+
<pending_xref refdoc="exercise/_enum_numref_notitle" refdomain="std" refexplicit="True" reftarget="text-exc-notitle" reftype="numref" refwarn="True">
26+
<literal classes="xref std std-numref">
27+
some text {number}
28+
.
29+
<paragraph>
30+
This is a fifth reference
31+
<pending_xref refdoc="exercise/_enum_numref_notitle" refdomain="std" refexplicit="True" reftarget="text-exc-notitle" reftype="numref" refwarn="True">
32+
<literal classes="xref std std-numref">
33+
some text {name}
34+
.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<document source="_enum_numref_placeholders.rst">
2+
<section ids="enum-numref-placeholders" names="_enum_numref_placeholders">
3+
<title>
4+
_enum_numref_placeholders
5+
<paragraph>
6+
This reference does not include math
7+
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
8+
<literal classes="xref std std-numref">
9+
some {number} text %s test {name}
10+
.
11+
<paragraph>
12+
This reference does not include math
13+
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
14+
<literal classes="xref std std-numref">
15+
some {number} text %s test
16+
.
17+
<paragraph>
18+
This reference does not include math
19+
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
20+
<literal classes="xref std std-numref">
21+
some {name} text %s test
22+
.
23+
<paragraph>
24+
This reference does not include math
25+
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
26+
<literal classes="xref std std-numref">
27+
some {name} text {number} test
28+
.
29+
<paragraph>
30+
This reference does not include math
31+
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
32+
<literal classes="xref std std-numref">
33+
some %s text test
34+
.
35+
<paragraph>
36+
This reference includes math
37+
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
38+
<literal classes="xref std std-numref">
39+
some {number} text %s test {name}
40+
.
41+
<paragraph>
42+
This reference includes math
43+
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
44+
<literal classes="xref std std-numref">
45+
some {number} text %s test
46+
.
47+
<paragraph>
48+
This reference includes math
49+
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
50+
<literal classes="xref std std-numref">
51+
some {name} text %s test
52+
.
53+
<paragraph>
54+
This reference includes math
55+
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
56+
<literal classes="xref std std-numref">
57+
some {name} text {number} test
58+
.
59+
<paragraph>
60+
This reference includes math
61+
<pending_xref refdoc="exercise/_enum_numref_placeholders" refdomain="std" refexplicit="True" reftarget="test-exc-label-math" reftype="numref" refwarn="True">
62+
<literal classes="xref std std-numref">
63+
some %s text test
64+
.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<document source="_enum_numref_title.rst">
2+
<section ids="enum-numref-title" names="_enum_numref_title">
3+
<title>
4+
_enum_numref_title
5+
<paragraph>
6+
This is a reference
7+
<pending_xref refdoc="exercise/_enum_numref_title" refdomain="std" refexplicit="False" reftarget="test-exc-label" reftype="numref" refwarn="True">
8+
<literal classes="xref std std-numref">
9+
test-exc-label
10+
.
11+
<paragraph>
12+
This is a second reference
13+
<pending_xref refdoc="exercise/_enum_numref_title" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
14+
<literal classes="xref std std-numref">
15+
some text
16+
.
17+
<paragraph>
18+
This is a third reference
19+
<pending_xref refdoc="exercise/_enum_numref_title" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
20+
<literal classes="xref std std-numref">
21+
some text %s
22+
.
23+
<paragraph>
24+
This is a fourth reference
25+
<pending_xref refdoc="exercise/_enum_numref_title" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
26+
<literal classes="xref std std-numref">
27+
some text {number}
28+
.
29+
<paragraph>
30+
This is a fifth reference
31+
<pending_xref refdoc="exercise/_enum_numref_title" refdomain="std" refexplicit="True" reftarget="test-exc-label" reftype="numref" refwarn="True">
32+
<literal classes="xref std std-numref">
33+
some text {name}
34+
.

0 commit comments

Comments
 (0)