From 77e116d71382e7eed9aec5d55be3192d60a68f50 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 16 Jul 2022 13:15:40 -0500 Subject: [PATCH] test(TestDirectives::test_doctest): myst-parser example --- tests/test_sphinx_doctest.py | 49 ++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/tests/test_sphinx_doctest.py b/tests/test_sphinx_doctest.py index 255663d..5af478d 100644 --- a/tests/test_sphinx_doctest.py +++ b/tests/test_sphinx_doctest.py @@ -44,7 +44,7 @@ def __call__( must_raise: bool = False, file_type: str = "rst", sphinxopts: Optional[List[str]] = None, - ): + ) -> str: index_rst = self.tmp_path / "source" / "index.rst" index_file = self.tmp_path / "source" / f"index.{file_type}" file_content = textwrap.dedent(file_content) @@ -142,19 +142,52 @@ def test_testcode( plugin_result = testdir.runpytest("--doctest-glob=index.rst").stdout plugin_result.fnmatch_lines(["*=== 1 passed in *"]) - def test_doctest( - self, testdir: Testdir, sphinx_tester: SphinxDoctestRunner - ) -> None: - code = """ + @pytest.mark.parametrize( + "file_type,code", + [ + [ + "rst", + """ .. doctest:: >>> print("msg from testcode directive") msg from testcode directive - """ - sphinx_output = sphinx_tester(code) + """, + ], + [ + "md", + """ + ```{eval-rst} + .. doctest:: + + >>> print("msg from testcode directive") + msg from testcode directive + + ``` + + """.strip(), + ], + ], + ) + def test_doctest( + self, + testdir: Testdir, + sphinx_tester: SphinxDoctestRunner, + file_type: str, + code: str, + ) -> None: + if file_type == "md": # Skip if no myst-parser + pytest.importorskip("myst_parser") + sphinx_output = sphinx_tester( + code, + file_type=file_type, + sphinxopts=None + if file_type == "rst" + else ["-D", "extensions=myst_parser,sphinx.ext.doctest"], + ) assert "1 items passed all tests" in sphinx_output - plugin_result = testdir.runpytest("--doctest-glob=index.rst").stdout + plugin_result = testdir.runpytest(f"--doctest-glob=index.{file_type}").stdout plugin_result.fnmatch_lines(["*=== 1 passed in *"]) def test_doctest_multiple(