Skip to content

Commit ed9c8bd

Browse files
committed
tests(doctest_docutils): Add doctestopt_re whitespace test (#56)
Backport: Sphinx commit ad0c343d3 (2025-01-04) Ref: sphinx-doc/sphinx@ad0c343d3 what: - Test doctestopt_re regex removes leading whitespace before flags - Covers trailing spaces, tabs, multiline, mixed whitespace - 6 test cases using NamedTuple parametrization Refs: sphinx-doc/sphinx#13164
1 parent b6319ed commit ed9c8bd

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

tests/test_doctest_docutils.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,74 @@ def test_DocutilsDocTestFinder(
250250

251251
for test in tests:
252252
doctest.DebugRunner(verbose=False).run(test)
253+
254+
255+
class DoctestOptReTestCase(t.NamedTuple):
256+
"""Test fixture for doctestopt_re regex.
257+
258+
Backported from Sphinx commit ad0c343d3 (2025-01-04).
259+
https://github.com/sphinx-doc/sphinx/commit/ad0c343d3
260+
261+
The original Sphinx test verified HTML output doesn't have trailing
262+
whitespace after flag trimming. This test verifies the regex correctly
263+
matches and removes leading whitespace before doctest flags.
264+
265+
Refs: sphinx-doc/sphinx#13164
266+
"""
267+
268+
test_id: str
269+
input_code: str
270+
expected_output: str
271+
272+
273+
DOCTESTOPT_RE_FIXTURES = [
274+
DoctestOptReTestCase(
275+
test_id="trailing-spaces-before-flag",
276+
input_code="result = func() # doctest: +SKIP",
277+
expected_output="result = func()",
278+
),
279+
DoctestOptReTestCase(
280+
test_id="tab-before-flag",
281+
input_code="result = func()\t# doctest: +SKIP",
282+
expected_output="result = func()",
283+
),
284+
DoctestOptReTestCase(
285+
test_id="no-space-before-flag",
286+
input_code="result = func()# doctest: +SKIP",
287+
expected_output="result = func()",
288+
),
289+
DoctestOptReTestCase(
290+
test_id="multiline-with-leading-whitespace",
291+
input_code="line1\nresult = func() # doctest: +SKIP\nline3",
292+
expected_output="line1\nresult = func()\nline3",
293+
),
294+
DoctestOptReTestCase(
295+
test_id="multiple-flags-on-separate-lines",
296+
input_code="a = 1 # doctest: +SKIP\nb = 2 # doctest: +ELLIPSIS",
297+
expected_output="a = 1\nb = 2",
298+
),
299+
DoctestOptReTestCase(
300+
test_id="mixed-tabs-and-spaces",
301+
input_code="result = func() \t # doctest: +NORMALIZE_WHITESPACE",
302+
expected_output="result = func()",
303+
),
304+
]
305+
306+
307+
@pytest.mark.parametrize(
308+
DoctestOptReTestCase._fields,
309+
DOCTESTOPT_RE_FIXTURES,
310+
ids=[f.test_id for f in DOCTESTOPT_RE_FIXTURES],
311+
)
312+
def test_doctestopt_re_whitespace_trimming(
313+
test_id: str,
314+
input_code: str,
315+
expected_output: str,
316+
) -> None:
317+
"""Verify doctestopt_re removes leading whitespace before doctest flags.
318+
319+
Regression test for Sphinx PR #13164.
320+
Backported from Sphinx commit ad0c343d3 (2025-01-04).
321+
"""
322+
result = doctest_docutils.doctestopt_re.sub("", input_code)
323+
assert result == expected_output

0 commit comments

Comments
 (0)