Skip to content

Commit

Permalink
Fix tests failing on trailing substitution definition
Browse files Browse the repository at this point in the history
Some of the tests that have rst_epilog or rst_prolog set need to be
adjusted to not fail when encountering substitution definitions which
have reappeared since fixing sphinx-doc#7953.
  • Loading branch information
brechtm committed Sep 9, 2020
1 parent c9d8eac commit 97e7e09
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 48 deletions.
30 changes: 19 additions & 11 deletions tests/test_directive_patch.py
Expand Up @@ -21,7 +21,8 @@ def test_code_directive(app):
' print("hello world")\n')

doctree = restructuredtext.parse(app, text)
assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
assert_node(doctree, ([nodes.literal_block, 'print("hello world")'],
nodes.substitution_definition))
assert_node(doctree[0], language="default", highlight_args={})

# with language
Expand All @@ -30,7 +31,8 @@ def test_code_directive(app):
' print("hello world")\n')

doctree = restructuredtext.parse(app, text)
assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
assert_node(doctree, ([nodes.literal_block, 'print("hello world")'],
nodes.substitution_definition))
assert_node(doctree[0], language="python", highlight_args={})

# :number-lines: option
Expand All @@ -40,7 +42,8 @@ def test_code_directive(app):
' print("hello world")\n')

doctree = restructuredtext.parse(app, text)
assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
assert_node(doctree, ([nodes.literal_block, 'print("hello world")'],
nodes.substitution_definition))
assert_node(doctree[0], language="python", linenos=True, highlight_args={})

# :number-lines: option
Expand All @@ -50,37 +53,42 @@ def test_code_directive(app):
' print("hello world")\n')

doctree = restructuredtext.parse(app, text)
assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
assert_node(doctree, ([nodes.literal_block, 'print("hello world")'],
nodes.substitution_definition))
assert_node(doctree[0], language="python", linenos=True, highlight_args={'linenostart': 5})


def test_math_directive(app):
# normal case
text = '.. math:: E = mc^2'
doctree = restructuredtext.parse(app, text)
assert_node(doctree, [nodes.document, nodes.math_block, 'E = mc^2\n\n'])
assert_node(doctree, ([nodes.math_block, "E = mc^2\n\n"],
nodes.substitution_definition))

# :name: option
text = ('.. math:: E = mc^2\n'
' :name: eq1\n')
doctree = restructuredtext.parse(app, text)
assert_node(doctree, [nodes.document, (nodes.target,
[nodes.math_block, "E = mc^2\n\n"])])
assert_node(doctree, (nodes.target,
[nodes.math_block, "E = mc^2\n\n"],
nodes.substitution_definition))
assert_node(doctree[1], nodes.math_block, docname='index', label="eq1", number=1)

# :label: option
text = ('.. math:: E = mc^2\n'
' :label: eq2\n')
doctree = restructuredtext.parse(app, text)
assert_node(doctree, [nodes.document, (nodes.target,
[nodes.math_block, 'E = mc^2\n\n'])])
assert_node(doctree, (nodes.target,
[nodes.math_block, "E = mc^2\n\n"],
nodes.substitution_definition))
assert_node(doctree[1], nodes.math_block, docname='index', label="eq2", number=2)

# :label: option without value
text = ('.. math:: E = mc^2\n'
' :label:\n')
doctree = restructuredtext.parse(app, text)
assert_node(doctree, [nodes.document, (nodes.target,
[nodes.math_block, 'E = mc^2\n\n'])])
assert_node(doctree, (nodes.target,
[nodes.math_block, "E = mc^2\n\n"],
nodes.substitution_definition))
assert_node(doctree[1], nodes.math_block, ids=['equation-index-0'],
docname='index', label="index:0", number=3)
4 changes: 3 additions & 1 deletion tests/test_domain_c.py
Expand Up @@ -8,6 +8,7 @@
:license: BSD, see LICENSE for details.
"""
import pytest
from docutils import nodes

from sphinx import addnodes
from sphinx.addnodes import desc
Expand Down Expand Up @@ -601,6 +602,7 @@ def test_noindexentry(app):
".. c:function:: void g()\n"
" :noindexentry:\n")
doctree = restructuredtext.parse(app, text)
assert_node(doctree, (addnodes.index, desc, addnodes.index, desc))
assert_node(doctree, (addnodes.index, desc, addnodes.index, desc,
nodes.substitution_definition))
assert_node(doctree[0], addnodes.index, entries=[('single', 'f (C function)', 'c.f', '', None)])
assert_node(doctree[2], addnodes.index, entries=[])
4 changes: 3 additions & 1 deletion tests/test_domain_cpp.py
Expand Up @@ -11,6 +11,7 @@
import re

import pytest
from docutils import nodes

import sphinx.domains.cpp as cppDomain
from sphinx import addnodes
Expand Down Expand Up @@ -1233,6 +1234,7 @@ def test_noindexentry(app):
".. cpp:function:: void g()\n"
" :noindexentry:\n")
doctree = restructuredtext.parse(app, text)
assert_node(doctree, (addnodes.index, desc, addnodes.index, desc))
assert_node(doctree, (addnodes.index, desc, addnodes.index, desc,
nodes.substitution_definition))
assert_node(doctree[0], addnodes.index, entries=[('single', 'f (C++ function)', '_CPPv41fv', '', None)])
assert_node(doctree[2], addnodes.index, entries=[])
15 changes: 10 additions & 5 deletions tests/test_domain_js.py
Expand Up @@ -176,7 +176,8 @@ def test_js_module(app):
text = ".. js:module:: sphinx"
doctree = restructuredtext.parse(app, text)
assert_node(doctree, (nodes.target,
addnodes.index))
addnodes.index,
nodes.substitution_definition))
assert_node(doctree[0], nodes.target, ids=["module-sphinx"])
assert_node(doctree[1], addnodes.index,
entries=[("single", "sphinx (module)", "module-sphinx", "", None)])
Expand All @@ -188,7 +189,8 @@ def test_js_function(app):
assert_node(doctree, (addnodes.index,
[desc, ([desc_signature, ([desc_name, "sum"],
desc_parameterlist)],
[desc_content, ()])]))
[desc_content, ()])],
nodes.substitution_definition))
assert_node(doctree[1][0][1], [desc_parameterlist, ([desc_parameter, "a"],
[desc_parameter, "b"])])
assert_node(doctree[0], addnodes.index,
Expand All @@ -203,7 +205,8 @@ def test_js_class(app):
[desc, ([desc_signature, ([desc_annotation, "class "],
[desc_name, "Application"],
[desc_parameterlist, ()])],
[desc_content, ()])]))
[desc_content, ()])],
nodes.substitution_definition))
assert_node(doctree[0], addnodes.index,
entries=[("single", "Application() (class)", "Application", "", None)])
assert_node(doctree[1], addnodes.desc, domain="js", objtype="class", noindex=False)
Expand All @@ -214,7 +217,8 @@ def test_js_data(app):
doctree = restructuredtext.parse(app, text)
assert_node(doctree, (addnodes.index,
[desc, ([desc_signature, desc_name, "name"],
[desc_content, ()])]))
[desc_content, ()])],
nodes.substitution_definition))
assert_node(doctree[0], addnodes.index,
entries=[("single", "name (global variable or constant)", "name", "", None)])
assert_node(doctree[1], addnodes.desc, domain="js", objtype="data", noindex=False)
Expand All @@ -225,6 +229,7 @@ def test_noindexentry(app):
".. js:function:: g()\n"
" :noindexentry:\n")
doctree = restructuredtext.parse(app, text)
assert_node(doctree, (addnodes.index, desc, addnodes.index, desc))
assert_node(doctree, (addnodes.index, desc, addnodes.index, desc,
nodes.substitution_definition))
assert_node(doctree[0], addnodes.index, entries=[('single', 'f() (built-in function)', 'f', '', None)])
assert_node(doctree[2], addnodes.index, entries=[])
57 changes: 38 additions & 19 deletions tests/test_domain_py.py
Expand Up @@ -301,7 +301,8 @@ def test_pyfunction_signature(app):
[desc, ([desc_signature, ([desc_name, "hello"],
desc_parameterlist,
[desc_returns, pending_xref, "str"])],
desc_content)]))
desc_content)],
nodes.substitution_definition))
assert_node(doctree[1], addnodes.desc, desctype="function",
domain="py", objtype="function", noindex=False)
assert_node(doctree[1][0][1],
Expand All @@ -319,7 +320,8 @@ def test_pyfunction_signature_full(app):
[desc, ([desc_signature, ([desc_name, "hello"],
desc_parameterlist,
[desc_returns, pending_xref, "str"])],
desc_content)]))
desc_content)],
nodes.substitution_definition))
assert_node(doctree[1], addnodes.desc, desctype="function",
domain="py", objtype="function", noindex=False)
assert_node(doctree[1][0][1],
Expand Down Expand Up @@ -393,7 +395,8 @@ def test_optional_pyfunction_signature(app):
[desc, ([desc_signature, ([desc_name, "compile"],
desc_parameterlist,
[desc_returns, pending_xref, "ast object"])],
desc_content)]))
desc_content)],
nodes.substitution_definition))
assert_node(doctree[1], addnodes.desc, desctype="function",
domain="py", objtype="function", noindex=False)
assert_node(doctree[1][0][1],
Expand All @@ -409,7 +412,8 @@ def test_pyexception_signature(app):
[desc, ([desc_signature, ([desc_annotation, "exception "],
[desc_addname, "exceptions."],
[desc_name, "IOError"])],
desc_content)]))
desc_content)],
nodes.substitution_definition))
assert_node(doctree[1], desc, desctype="exception",
domain="py", objtype="exception", noindex=False)

Expand All @@ -421,7 +425,8 @@ def test_exceptions_module_is_ignored(app):
assert_node(doctree, (addnodes.index,
[desc, ([desc_signature, ([desc_annotation, "exception "],
[desc_name, "IOError"])],
desc_content)]))
desc_content)],
nodes.substitution_definition))
assert_node(doctree[1], desc, desctype="exception",
domain="py", objtype="exception", noindex=False)

Expand All @@ -436,7 +441,8 @@ def test_pydata_signature(app):
[desc_annotation, (": ",
[pending_xref, "int"])],
[desc_annotation, " = 1"])],
desc_content)]))
desc_content)],
nodes.substitution_definition))
assert_node(doctree[1], addnodes.desc, desctype="data",
domain="py", objtype="data", noindex=False)

Expand All @@ -448,7 +454,8 @@ def test_pydata_signature_old(app):
assert_node(doctree, (addnodes.index,
[desc, ([desc_signature, ([desc_name, "version"],
[desc_annotation, " = 1"])],
desc_content)]))
desc_content)],
nodes.substitution_definition))
assert_node(doctree[1], addnodes.desc, desctype="data",
domain="py", objtype="data", noindex=False)

Expand All @@ -465,7 +472,8 @@ def test_pyobject_prefix(app):
[desc_content, (addnodes.index,
desc,
addnodes.index,
desc)])]))
desc)])],
nodes.substitution_definition))
assert doctree[1][1][1].astext().strip() == 'say()' # prefix is stripped
assert doctree[1][1][3].astext().strip() == 'FooBar.say()' # not stripped

Expand All @@ -483,7 +491,8 @@ def test_pydata(app):
[desc_name, "var"],
[desc_annotation, (": ",
[pending_xref, "int"])])],
[desc_content, ()])]))
[desc_content, ()])],
nodes.substitution_definition))
assert_node(doctree[3][0][2][1], pending_xref, **{"py:module": "example"})
assert 'example.var' in domain.objects
assert domain.objects['example.var'] == ('index', 'example.var', 'data')
Expand All @@ -507,7 +516,8 @@ def test_pyfunction(app):
[desc_addname, "example."],
[desc_name, "func2"],
[desc_parameterlist, ()])],
[desc_content, ()])]))
[desc_content, ()])],
nodes.substitution_definition))
assert_node(doctree[0], addnodes.index,
entries=[('pair', 'built-in function; func1()', 'func1', '', None)])
assert_node(doctree[3], addnodes.index,
Expand All @@ -534,7 +544,8 @@ def test_pyclass_options(app):
addnodes.index,
[desc, ([desc_signature, ([desc_annotation, "final class "],
[desc_name, "Class2"])],
[desc_content, ()])]))
[desc_content, ()])],
nodes.substitution_definition))

# class
assert_node(doctree[0], addnodes.index,
Expand Down Expand Up @@ -583,7 +594,8 @@ def test_pymethod_options(app):
addnodes.index,
desc,
addnodes.index,
desc)])]))
desc)])],
nodes.substitution_definition))

# method
assert_node(doctree[1][1][0], addnodes.index,
Expand Down Expand Up @@ -664,7 +676,8 @@ def test_pyclassmethod(app):
[desc, ([desc_signature, ([desc_annotation, "class "],
[desc_name, "Class"])],
[desc_content, (addnodes.index,
desc)])]))
desc)])],
nodes.substitution_definition))
assert_node(doctree[1][1][0], addnodes.index,
entries=[('single', 'meth() (Class class method)', 'Class.meth', '', None)])
assert_node(doctree[1][1][1], ([desc_signature, ([desc_annotation, "classmethod "],
Expand All @@ -685,7 +698,8 @@ def test_pystaticmethod(app):
[desc, ([desc_signature, ([desc_annotation, "class "],
[desc_name, "Class"])],
[desc_content, (addnodes.index,
desc)])]))
desc)])],
nodes.substitution_definition))
assert_node(doctree[1][1][0], addnodes.index,
entries=[('single', 'meth() (Class static method)', 'Class.meth', '', None)])
assert_node(doctree[1][1][1], ([desc_signature, ([desc_annotation, "static "],
Expand All @@ -708,7 +722,8 @@ def test_pyattribute(app):
[desc, ([desc_signature, ([desc_annotation, "class "],
[desc_name, "Class"])],
[desc_content, (addnodes.index,
desc)])]))
desc)])],
nodes.substitution_definition))
assert_node(doctree[1][1][0], addnodes.index,
entries=[('single', 'attr (Class attribute)', 'Class.attr', '', None)])
assert_node(doctree[1][1][1], ([desc_signature, ([desc_name, "attr"],
Expand All @@ -732,7 +747,8 @@ def test_pydecorator_signature(app):
assert_node(doctree, (addnodes.index,
[desc, ([desc_signature, ([desc_addname, "@"],
[desc_name, "deco"])],
desc_content)]))
desc_content)],
nodes.substitution_definition))
assert_node(doctree[1], addnodes.desc, desctype="function",
domain="py", objtype="function", noindex=False)

Expand All @@ -747,7 +763,8 @@ def test_pydecoratormethod_signature(app):
assert_node(doctree, (addnodes.index,
[desc, ([desc_signature, ([desc_addname, "@"],
[desc_name, "deco"])],
desc_content)]))
desc_content)],
nodes.substitution_definition))
assert_node(doctree[1], addnodes.desc, desctype="method",
domain="py", objtype="method", noindex=False)

Expand Down Expand Up @@ -827,14 +844,16 @@ def test_noindexentry(app):
".. py:function:: g()\n"
" :noindexentry:\n")
doctree = restructuredtext.parse(app, text)
assert_node(doctree, (addnodes.index, desc, addnodes.index, desc))
assert_node(doctree, (addnodes.index, desc, addnodes.index, desc,
nodes.substitution_definition))
assert_node(doctree[0], addnodes.index, entries=[('pair', 'built-in function; f()', 'f', '', None)])
assert_node(doctree[2], addnodes.index, entries=[])

text = (".. py:class:: f\n"
".. py:class:: g\n"
" :noindexentry:\n")
doctree = restructuredtext.parse(app, text)
assert_node(doctree, (addnodes.index, desc, addnodes.index, desc))
assert_node(doctree, (addnodes.index, desc, addnodes.index, desc,
nodes.substitution_definition))
assert_node(doctree[0], addnodes.index, entries=[('single', 'f (built-in class)', 'f', '', None)])
assert_node(doctree[2], addnodes.index, entries=[])

0 comments on commit 97e7e09

Please sign in to comment.