diff --git a/pyproject.toml b/pyproject.toml index 3eab7483e02..ae68a8d4904 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -238,8 +238,6 @@ module = [ "tests.test_ext_napoleon.test_ext_napoleon", # tests/test_extensions "tests.test_extensions.test_ext_math", - # tests/test_markup - "tests.test_markup.test_markup", # tests/test_theming "tests.test_theming.test_templating", ] diff --git a/tests/test_markup/test_markup.py b/tests/test_markup/test_markup.py index fb4df4c400b..88e57f3ab4d 100644 --- a/tests/test_markup/test_markup.py +++ b/tests/test_markup/test_markup.py @@ -21,10 +21,15 @@ from sphinx.writers.html5 import HTML5Translator from sphinx.writers.latex import LaTeXTranslator, LaTeXWriter +TYPE_CHECKING = False +if TYPE_CHECKING: + from docutils.frontend import Values -@pytest.fixture -def settings(app): - env = app.env + from sphinx.environment import BuildEnvironment + from sphinx.testing.util import SphinxTestApp + + +def new_settings(env: BuildEnvironment) -> Values: texescape.init() # otherwise done by the latex builder settings = _get_settings( RstParser, HTMLWriter, LaTeXWriter, defaults=default_settings @@ -33,50 +38,42 @@ def settings(app): settings.env = env settings.env.current_document.docname = 'dummy' settings.contentsname = 'dummy' - domain_context = sphinx_domains(env) - domain_context.enable() - yield settings - domain_context.disable() - + return settings -@pytest.fixture -def new_document(settings): - def create(): - document = utils.new_document('test data', settings) - document['file'] = 'dummy' - return document - return create +def new_document(env: BuildEnvironment) -> nodes.document: + settings = new_settings(env) + document = utils.new_document('test data', settings) + document['file'] = 'dummy' + return document -@pytest.fixture -def inliner(new_document): - document = new_document() - document.reporter.get_source_and_line = lambda line=1: ('dummy.rst', line) +def new_inliner(env: BuildEnvironment) -> SimpleNamespace: + document = new_document(env) + document.reporter.get_source_and_line = lambda line=1: ('dummy.rst', line) # type: ignore[attr-defined] return SimpleNamespace(document=document, reporter=document.reporter) -@pytest.fixture -def parse(new_document): - def parse_(rst): - document = new_document() - parser = RstParser() - parser.parse(rst, document) - SphinxSmartQuotes(document, startnode=None).apply() - for msg in list(document.findall(nodes.system_message)): - if msg['level'] == 1: - msg.replace_self([]) - return document - - return parse_ +def parse_rst(rst: str, *, env: BuildEnvironment) -> nodes.document: + document = new_document(env) + parser = RstParser() + domain_context = sphinx_domains(env) + domain_context.enable() + parser.parse(rst, document) + domain_context.disable() + SphinxSmartQuotes(document, startnode=None).apply() + for msg in list(document.findall(nodes.system_message)): + if msg['level'] == 1: + msg.replace_self([]) + return document # since we're not resolving the markup afterwards, these nodes may remain class ForgivingTranslator: - def visit_pending_xref(self, node): + def visit_pending_xref(self, node: nodes.Element) -> None: pass - def depart_pending_xref(self, node): + def depart_pending_xref(self, node: nodes.Element) -> None: pass @@ -88,67 +85,31 @@ class ForgivingLaTeXTranslator(LaTeXTranslator, ForgivingTranslator): pass -@pytest.fixture -def verify_re_html(app, parse): - def verify(rst, html_expected): - document = parse(rst) - html_translator = ForgivingHTMLTranslator(document, app.builder) - document.walkabout(html_translator) - html_translated = ''.join(html_translator.fragment).strip() - assert re.match(html_expected, html_translated), 'from ' + rst - - return verify - +def rst_to_html(rst: str, *, app: SphinxTestApp) -> str: + document = parse_rst(rst, env=app.env) + html_translator = ForgivingHTMLTranslator(document, app.builder) + document.walkabout(html_translator) + html_translated = ''.join(html_translator.fragment).strip() + return html_translated -@pytest.fixture -def verify_re_latex(app, parse): - def verify(rst, latex_expected): - document = parse(rst) - app.builder = LaTeXBuilder(app, app.env) - app.builder.init() - theme = app.builder.themes.get('manual') - latex_translator = ForgivingLaTeXTranslator(document, app.builder, theme) - latex_translator.first_document = -1 # don't write \begin{document} - document.walkabout(latex_translator) - latex_translated = ''.join(latex_translator.body).strip() - assert re.match(latex_expected, latex_translated), 'from ' + repr(rst) - return verify - - -@pytest.fixture -def verify_re(verify_re_html, verify_re_latex): - def verify_re_(rst, html_expected, latex_expected): - if html_expected: - verify_re_html(rst, html_expected) - if latex_expected: - verify_re_latex(rst, latex_expected) - - return verify_re_ - - -@pytest.fixture -def verify(verify_re_html, verify_re_latex): - def verify_(rst, html_expected, latex_expected): - if html_expected: - verify_re_html(rst, re.escape(html_expected) + '$') - if latex_expected: - verify_re_latex(rst, re.escape(latex_expected) + '$') - - return verify_ - - -@pytest.fixture -def get_verifier(verify, verify_re): - return {'verify': verify, 'verify_re': verify_re}.__getitem__ +def rst_to_latex(rst: str, *, app: SphinxTestApp) -> str: + document = parse_rst(rst, env=app.env) + app.builder = LaTeXBuilder(app, app.env) + app.builder.init() + theme = app.builder.themes.get('manual') + latex_translator = ForgivingLaTeXTranslator(document, app.builder, theme) + latex_translator.first_document = -1 # don't write \begin{document} + document.walkabout(latex_translator) + latex_translated = ''.join(latex_translator.body).strip() + return latex_translated @pytest.mark.parametrize( - ('type', 'rst', 'html_expected', 'latex_expected'), + ('rst', 'html_expected', 'latex_expected'), [ ( # cve role - 'verify', ':cve:`2020-10735`', ( '
' - ), - r'\\sphinxAtStartPar\n\\sphinxcode{\\sphinxupquote{code sample}}', - ), ( # interpolation of arrows in menuselection - 'verify', ':menuselection:`a --> b`', '', '\\sphinxAtStartPar\n\\sphinxmenuselection{a \\(\\rightarrow\\) b}', ), ( # interpolation of ampersands in menuselection - 'verify', ':menuselection:`&Foo -&&- &Bar`', ( '