sphinx/tests/test_directives/test_directive_patch.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

127 lines
4.0 KiB
Python
Raw Normal View History

"""Test the patched directives."""
import pytest
from docutils import nodes
from sphinx.testing import restructuredtext
from sphinx.testing.util import assert_node
@pytest.mark.sphinx('html', testroot='root')
def test_code_directive(app):
# normal case
2024-08-11 08:58:56 -05:00
text = '.. code::\n\n print("hello world")\n'
doctree = restructuredtext.parse(app, text)
assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
2024-08-11 08:58:56 -05:00
assert_node(doctree[0], language='default', highlight_args={})
# with language
2024-08-11 08:58:56 -05:00
text = '.. code:: python\n\n print("hello world")\n'
doctree = restructuredtext.parse(app, text)
assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
2024-08-11 08:58:56 -05:00
assert_node(doctree[0], language='python', highlight_args={})
# :number-lines: option
2024-08-11 08:58:56 -05:00
text = '.. code:: python\n :number-lines:\n\n print("hello world")\n'
doctree = restructuredtext.parse(app, text)
assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
2024-08-11 08:58:56 -05:00
assert_node(doctree[0], language='python', linenos=True, highlight_args={})
# :number-lines: option
2024-08-11 08:58:56 -05:00
text = '.. code:: python\n :number-lines: 5\n\n print("hello world")\n'
doctree = restructuredtext.parse(app, text)
assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
2024-08-11 08:58:56 -05:00
assert_node(
doctree[0], language='python', linenos=True, highlight_args={'linenostart': 5}
)
2019-03-04 09:41:08 -06:00
@pytest.mark.sphinx('html', testroot='directive-csv-table')
def test_csv_table_directive(app):
# relative path from current document
2024-08-11 08:58:56 -05:00
text = '.. csv-table::\n :file: example.csv\n'
doctree = restructuredtext.parse(app, text, docname='subdir/index')
assert_node(
doctree,
(
[
nodes.table,
nodes.tgroup,
(nodes.colspec, nodes.colspec, nodes.colspec, [nodes.tbody, nodes.row]),
],
),
)
assert_node(
doctree[0][0][3][0],
(
[nodes.entry, nodes.paragraph, 'FOO'],
[nodes.entry, nodes.paragraph, 'BAR'],
[nodes.entry, nodes.paragraph, 'BAZ'],
),
)
# absolute path from source directory
2024-08-11 08:58:56 -05:00
text = '.. csv-table::\n :file: /example.csv\n'
doctree = restructuredtext.parse(app, text, docname='subdir/index')
assert_node(
doctree,
(
[
nodes.table,
nodes.tgroup,
(nodes.colspec, nodes.colspec, nodes.colspec, [nodes.tbody, nodes.row]),
],
),
)
assert_node(
doctree[0][0][3][0],
(
[nodes.entry, nodes.paragraph, 'foo'],
[nodes.entry, nodes.paragraph, 'bar'],
[nodes.entry, nodes.paragraph, 'baz'],
),
)
@pytest.mark.sphinx('html', testroot='root')
2019-03-04 09:41:08 -06:00
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'])
# :name: option
2024-08-11 08:58:56 -05:00
text = '.. math:: E = mc^2\n :name: eq1\n'
2019-03-04 09:41:08 -06:00
doctree = restructuredtext.parse(app, text)
2024-08-11 08:58:56 -05:00
assert_node(
doctree, [nodes.document, (nodes.target, [nodes.math_block, 'E = mc^2\n\n'])]
)
assert_node(doctree[1], nodes.math_block, docname='index', label='eq1', number=1)
2019-03-04 09:41:08 -06:00
# :label: option
2024-08-11 08:58:56 -05:00
text = '.. math:: E = mc^2\n :label: eq2\n'
2019-03-04 09:41:08 -06:00
doctree = restructuredtext.parse(app, text)
2024-08-11 08:58:56 -05:00
assert_node(
doctree, [nodes.document, (nodes.target, [nodes.math_block, 'E = mc^2\n\n'])]
)
assert_node(doctree[1], nodes.math_block, docname='index', label='eq2', number=2)
2019-03-04 09:41:08 -06:00
# :label: option without value
2024-08-11 08:58:56 -05:00
text = '.. math:: E = mc^2\n :label:\n'
2019-03-04 09:41:08 -06:00
doctree = restructuredtext.parse(app, text)
2024-08-11 08:58:56 -05:00
assert_node(
doctree, [nodes.document, (nodes.target, [nodes.math_block, 'E = mc^2\n\n'])]
)
assert_node(
doctree[1],
nodes.math_block,
ids=['equation-index-0'],
docname='index',
label='index:0',
number=3,
)