summaryrefslogtreecommitdiffstats
path: root/tests/test_directives_no_typesetting.py
blob: fd101fb4cf381f48c7a1aaa39949122e2ecccdeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""Tests the directives"""

import pytest
from docutils import nodes

from sphinx import addnodes
from sphinx.testing import restructuredtext
from sphinx.testing.util import assert_node

DOMAINS = [
    # directive, no-index, no-index-entry, signature of f, signature of g, index entry of g
    ('c:function', False, True, 'void f()', 'void g()', ('single', 'g (C function)', 'c.g', '', None)),
    ('cpp:function', False, True, 'void f()', 'void g()', ('single', 'g (C++ function)', '_CPPv41gv', '', None)),
    ('js:function', True, True, 'f()', 'g()', ('single', 'g() (built-in function)', 'g', '', None)),
    ('py:function', True, True, 'f()', 'g()', ('pair', 'built-in function; g()', 'g', '', None)),
    ('rst:directive', True, False, 'f', 'g', ('single', 'g (directive)', 'directive-g', '', None)),
    ('cmdoption', True, False, 'f', 'g', ('pair', 'command line option; g', 'cmdoption-arg-g', '', None)),
    ('envvar', True, False, 'f', 'g', ('single', 'environment variable; g', 'envvar-g', '', None)),
]


@pytest.mark.parametrize(('directive', 'no_index', 'no_index_entry', 'sig_f', 'sig_g', 'index_g'), DOMAINS)
def test_object_description_no_typesetting(app, directive, no_index, no_index_entry, sig_f, sig_g, index_g):
    text = (f'.. {directive}:: {sig_f}\n'
            f'   :no-typesetting:\n')
    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, (addnodes.index, nodes.target))


@pytest.mark.parametrize(('directive', 'no_index', 'no_index_entry', 'sig_f', 'sig_g', 'index_g'), DOMAINS)
def test_object_description_no_typesetting_twice(app, directive, no_index, no_index_entry, sig_f, sig_g, index_g):
    text = (f'.. {directive}:: {sig_f}\n'
            f'   :no-typesetting:\n'
            f'.. {directive}:: {sig_g}\n'
            f'   :no-typesetting:\n')
    doctree = restructuredtext.parse(app, text)
    # Note that all index nodes come before the target nodes
    assert_node(doctree, (addnodes.index, addnodes.index, nodes.target, nodes.target))


@pytest.mark.parametrize(('directive', 'no_index', 'no_index_entry', 'sig_f', 'sig_g', 'index_g'), DOMAINS)
def test_object_description_no_typesetting_noindex_orig(app, directive, no_index, no_index_entry, sig_f, sig_g, index_g):
    if not no_index:
        pytest.skip(f'{directive} does not support :no-index: option')
    text = (f'.. {directive}:: {sig_f}\n'
            f'   :no-index:\n'
            f'.. {directive}:: {sig_g}\n')
    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, (addnodes.index, addnodes.desc, addnodes.index, addnodes.desc))
    assert_node(doctree[2], addnodes.index, entries=[index_g])


@pytest.mark.parametrize(('directive', 'no_index', 'no_index_entry', 'sig_f', 'sig_g', 'index_g'), DOMAINS)
def test_object_description_no_typesetting_noindex(app, directive, no_index, no_index_entry, sig_f, sig_g, index_g):
    if not no_index:
        pytest.skip(f'{directive} does not support :no-index: option')
    text = (f'.. {directive}:: {sig_f}\n'
            f'   :no-index:\n'
            f'   :no-typesetting:\n'
            f'.. {directive}:: {sig_g}\n'
            f'   :no-typesetting:\n')
    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, (addnodes.index, addnodes.index, nodes.target))
    assert_node(doctree[0], addnodes.index, entries=[])
    assert_node(doctree[1], addnodes.index, entries=[index_g])


@pytest.mark.parametrize(('directive', 'no_index', 'no_index_entry', 'sig_f', 'sig_g', 'index_g'), DOMAINS)
def test_object_description_no_typesetting_no_index_entry(app, directive, no_index, no_index_entry, sig_f, sig_g, index_g):
    if not no_index_entry:
        pytest.skip(f'{directive} does not support :no-index-entry: option')
    text = (f'.. {directive}:: {sig_f}\n'
            f'   :no-index-entry:\n'
            f'   :no-typesetting:\n'
            f'.. {directive}:: {sig_g}\n'
            f'   :no-typesetting:\n')
    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, (addnodes.index, addnodes.index, nodes.target, nodes.target))
    assert_node(doctree[0], addnodes.index, entries=[])
    assert_node(doctree[1], addnodes.index, entries=[index_g])


@pytest.mark.parametrize(('directive', 'no_index', 'no_index_entry', 'sig_f', 'sig_g', 'index_g'), DOMAINS)
def test_object_description_no_typesetting_code(app, directive, no_index, no_index_entry, sig_f, sig_g, index_g):
    text = (f'.. {directive}:: {sig_f}\n'
            f'   :no-typesetting:\n'
            f'.. {directive}:: {sig_g}\n'
            f'   :no-typesetting:\n'
            f'.. code::\n'
            f'\n'
            f'   code\n')
    doctree = restructuredtext.parse(app, text)
    # Note that all index nodes come before the targets
    assert_node(doctree, (addnodes.index, addnodes.index, nodes.target, nodes.target, nodes.literal_block))


@pytest.mark.parametrize(('directive', 'no_index', 'no_index_entry', 'sig_f', 'sig_g', 'index_g'), DOMAINS)
def test_object_description_no_typesetting_heading(app, directive, no_index, no_index_entry, sig_f, sig_g, index_g):
    text = (f'.. {directive}:: {sig_f}\n'
            f'   :no-typesetting:\n'
            f'.. {directive}:: {sig_g}\n'
            f'   :no-typesetting:\n'
            f'\n'
            f'Heading\n'
            f'=======\n')
    doctree = restructuredtext.parse(app, text)
    # Note that all index nodes come before the targets and the heading is floated before those.
    assert_node(doctree, (nodes.title, addnodes.index, addnodes.index, nodes.target, nodes.target))