Skip to content

Commit d2e8b31

Browse files
committed
tests(doctest_docutils): Stub out tests
1 parent 09c9262 commit d2e8b31

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed

tests/test_doctest_docutils.py

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
import doctest
2+
import pathlib
3+
import textwrap
4+
import typing as t
5+
6+
import pytest
7+
8+
import doctest_docutils
9+
10+
FixtureFileDict = t.Dict[str, str]
11+
12+
13+
class DocTestFinderFixture(t.NamedTuple):
14+
# pytest
15+
test_id: str
16+
17+
# Content
18+
files: FixtureFileDict
19+
tests_found: int
20+
21+
22+
FIXTURES = [
23+
#
24+
# Docutils
25+
#
26+
DocTestFinderFixture(
27+
test_id="reST-doctest_block",
28+
files={
29+
"example.rst": textwrap.dedent(
30+
"""
31+
>>> 4 + 4
32+
8
33+
"""
34+
)
35+
},
36+
tests_found=1,
37+
),
38+
DocTestFinderFixture(
39+
test_id="reST-doctest_directive",
40+
files={
41+
"example.rst": textwrap.dedent(
42+
"""
43+
.. doctest::
44+
45+
>>> 4 + 4
46+
8
47+
"""
48+
)
49+
},
50+
tests_found=1,
51+
),
52+
#
53+
# Markdown / myst-parser
54+
#
55+
DocTestFinderFixture(
56+
test_id="MyST-doctest_block",
57+
files={
58+
"example.md": textwrap.dedent(
59+
"""
60+
```
61+
>>> 4 + 4
62+
8
63+
```
64+
"""
65+
)
66+
},
67+
tests_found=1,
68+
),
69+
DocTestFinderFixture(
70+
test_id="MyST-doctest_block-indented",
71+
files={
72+
"example.md": textwrap.dedent(
73+
"""
74+
Here's a test:
75+
76+
>>> 4 + 4
77+
8
78+
"""
79+
)
80+
},
81+
tests_found=1,
82+
),
83+
DocTestFinderFixture(
84+
test_id="MyST-doctest_directive-colons",
85+
files={
86+
"example.md": textwrap.dedent(
87+
"""
88+
:::{doctest}
89+
90+
>>> 4 + 4
91+
8
92+
:::
93+
"""
94+
)
95+
},
96+
tests_found=1,
97+
),
98+
DocTestFinderFixture(
99+
test_id="MyST-doctest_directive-backticks",
100+
files={
101+
"example.md": textwrap.dedent(
102+
"""
103+
```{doctest}
104+
105+
>>> 4 + 4
106+
8
107+
```
108+
"""
109+
)
110+
},
111+
tests_found=1,
112+
),
113+
DocTestFinderFixture(
114+
test_id="MyST-doctest_directive-eval-rst-colons",
115+
files={
116+
"example.md": textwrap.dedent(
117+
"""
118+
:::{eval-rst}
119+
120+
.. doctest::
121+
122+
>>> 4 + 4
123+
8
124+
:::
125+
"""
126+
)
127+
},
128+
tests_found=1,
129+
),
130+
DocTestFinderFixture(
131+
test_id="MyST-doctest_directive-eval-rst-backticks",
132+
files={
133+
"example.md": textwrap.dedent(
134+
"""
135+
```{eval-rst}
136+
137+
.. doctest::
138+
139+
>>> 4 + 4
140+
8
141+
```
142+
"""
143+
)
144+
},
145+
tests_found=1,
146+
),
147+
]
148+
149+
150+
@pytest.mark.parametrize(
151+
DocTestFinderFixture._fields, FIXTURES, ids=[f.test_id for f in FIXTURES]
152+
)
153+
@pytest.mark.parametrize("file_path_mode", ["relative", "absolute"])
154+
def test_DocutilsDocTestFinder(
155+
tmp_path: pathlib.Path,
156+
monkeypatch: pytest.MonkeyPatch,
157+
test_id: str,
158+
files: FixtureFileDict,
159+
tests_found: int,
160+
file_path_mode: str,
161+
) -> None:
162+
# Initialize variables
163+
tests_path = tmp_path / "tests"
164+
first_test_key = list(files.keys())[0]
165+
first_test_filename = first_test_key
166+
if file_path_mode == "absolute":
167+
first_test_filename = str(tests_path / first_test_filename)
168+
elif file_path_mode != "relative":
169+
raise NotImplementedError(f"No file_path_mode supported for {file_path_mode}")
170+
171+
# Setup: Files
172+
tests_path.mkdir()
173+
for file_name, text in files.items():
174+
rst_file = tests_path / file_name
175+
rst_file.write_text(
176+
text,
177+
encoding="utf-8",
178+
)
179+
180+
# Setup: Environment
181+
if file_path_mode == "relative":
182+
monkeypatch.chdir(tests_path)
183+
184+
# Test
185+
finder = doctest_docutils.DocutilsDocTestFinder()
186+
text, _ = doctest._load_testfile( # type: ignore
187+
str(first_test_filename), package=None, module_relative=False, encoding="utf-8"
188+
)
189+
tests = finder.find(text, str(first_test_filename))
190+
tests.sort(key=lambda test: test.name)
191+
192+
assert len(tests) == tests_found
193+
194+
for test in tests:
195+
doctest.DebugRunner(verbose=False).run(test)

0 commit comments

Comments
 (0)