|
| 1 | +"""Tests the directives""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from docutils import nodes |
| 5 | + |
| 6 | +from sphinx import addnodes |
| 7 | +from sphinx.testing import restructuredtext |
| 8 | +from sphinx.testing.util import assert_node |
| 9 | + |
| 10 | +DOMAINS = [ |
| 11 | + # directive, noindex, noindexentry, signature of f, signature of g, index entry of g |
| 12 | + ('c:function', False, True, 'void f()', 'void g()', ('single', 'g (C function)', 'c.g', '', None)), |
| 13 | + ('cpp:function', False, True, 'void f()', 'void g()', ('single', 'g (C++ function)', '_CPPv41gv', '', None)), |
| 14 | + ('js:function', True, True, 'f()', 'g()', ('single', 'g() (built-in function)', 'g', '', None)), |
| 15 | + ('py:function', True, True, 'f()', 'g()', ('pair', 'built-in function; g()', 'g', '', None)), |
| 16 | + ('rst:directive', True, False, 'f', 'g', ('single', 'g (directive)', 'directive-g', '', None)), |
| 17 | + ('cmdoption', True, False, 'f', 'g', ('pair', 'command line option; g', 'cmdoption-arg-g', '', None)), |
| 18 | + ('envvar', True, False, 'f', 'g', ('single', 'environment variable; g', 'envvar-g', '', None)), |
| 19 | +] |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize(('directive', 'noindex', 'noindexentry', 'sig_f', 'sig_g', 'index_g'), DOMAINS) |
| 23 | +def test_object_description_no_typesetting(app, directive, noindex, noindexentry, sig_f, sig_g, index_g): |
| 24 | + text = (f'.. {directive}:: {sig_f}\n' |
| 25 | + f' :no-typesetting:\n') |
| 26 | + doctree = restructuredtext.parse(app, text) |
| 27 | + assert_node(doctree, (addnodes.index, nodes.target)) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.parametrize(('directive', 'noindex', 'noindexentry', 'sig_f', 'sig_g', 'index_g'), DOMAINS) |
| 31 | +def test_object_description_no_typesetting_twice(app, directive, noindex, noindexentry, sig_f, sig_g, index_g): |
| 32 | + text = (f'.. {directive}:: {sig_f}\n' |
| 33 | + f' :no-typesetting:\n' |
| 34 | + f'.. {directive}:: {sig_g}\n' |
| 35 | + f' :no-typesetting:\n') |
| 36 | + doctree = restructuredtext.parse(app, text) |
| 37 | + # Note that all index nodes come before the target nodes |
| 38 | + assert_node(doctree, (addnodes.index, addnodes.index, nodes.target, nodes.target)) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize(('directive', 'noindex', 'noindexentry', 'sig_f', 'sig_g', 'index_g'), DOMAINS) |
| 42 | +def test_object_description_no_typesetting_noindex_orig(app, directive, noindex, noindexentry, sig_f, sig_g, index_g): |
| 43 | + if not noindex: |
| 44 | + pytest.skip(f'{directive} does not support :noindex: option') |
| 45 | + text = (f'.. {directive}:: {sig_f}\n' |
| 46 | + f' :noindex:\n' |
| 47 | + f'.. {directive}:: {sig_g}\n') |
| 48 | + doctree = restructuredtext.parse(app, text) |
| 49 | + assert_node(doctree, (addnodes.index, addnodes.desc, addnodes.index, addnodes.desc)) |
| 50 | + assert_node(doctree[2], addnodes.index, entries=[index_g]) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize(('directive', 'noindex', 'noindexentry', 'sig_f', 'sig_g', 'index_g'), DOMAINS) |
| 54 | +def test_object_description_no_typesetting_noindex(app, directive, noindex, noindexentry, sig_f, sig_g, index_g): |
| 55 | + if not noindex: |
| 56 | + pytest.skip(f'{directive} does not support :noindex: option') |
| 57 | + text = (f'.. {directive}:: {sig_f}\n' |
| 58 | + f' :noindex:\n' |
| 59 | + f' :no-typesetting:\n' |
| 60 | + f'.. {directive}:: {sig_g}\n' |
| 61 | + f' :no-typesetting:\n') |
| 62 | + doctree = restructuredtext.parse(app, text) |
| 63 | + assert_node(doctree, (addnodes.index, addnodes.index, nodes.target)) |
| 64 | + assert_node(doctree[0], addnodes.index, entries=[]) |
| 65 | + assert_node(doctree[1], addnodes.index, entries=[index_g]) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize(('directive', 'noindex', 'noindexentry', 'sig_f', 'sig_g', 'index_g'), DOMAINS) |
| 69 | +def test_object_description_no_typesetting_noindexentry(app, directive, noindex, noindexentry, sig_f, sig_g, index_g): |
| 70 | + if not noindexentry: |
| 71 | + pytest.skip(f'{directive} does not support :noindexentry: option') |
| 72 | + text = (f'.. {directive}:: {sig_f}\n' |
| 73 | + f' :noindexentry:\n' |
| 74 | + f' :no-typesetting:\n' |
| 75 | + f'.. {directive}:: {sig_g}\n' |
| 76 | + f' :no-typesetting:\n') |
| 77 | + doctree = restructuredtext.parse(app, text) |
| 78 | + assert_node(doctree, (addnodes.index, addnodes.index, nodes.target, nodes.target)) |
| 79 | + assert_node(doctree[0], addnodes.index, entries=[]) |
| 80 | + assert_node(doctree[1], addnodes.index, entries=[index_g]) |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.parametrize(('directive', 'noindex', 'noindexentry', 'sig_f', 'sig_g', 'index_g'), DOMAINS) |
| 84 | +def test_object_description_no_typesetting_code(app, directive, noindex, noindexentry, sig_f, sig_g, index_g): |
| 85 | + text = (f'.. {directive}:: {sig_f}\n' |
| 86 | + f' :no-typesetting:\n' |
| 87 | + f'.. {directive}:: {sig_g}\n' |
| 88 | + f' :no-typesetting:\n' |
| 89 | + f'.. code::\n' |
| 90 | + f'\n' |
| 91 | + f' code\n') |
| 92 | + doctree = restructuredtext.parse(app, text) |
| 93 | + # Note that all index nodes come before the targets |
| 94 | + assert_node(doctree, (addnodes.index, addnodes.index, nodes.target, nodes.target, nodes.literal_block)) |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.parametrize(('directive', 'noindex', 'noindexentry', 'sig_f', 'sig_g', 'index_g'), DOMAINS) |
| 98 | +def test_object_description_no_typesetting_heading(app, directive, noindex, noindexentry, sig_f, sig_g, index_g): |
| 99 | + text = (f'.. {directive}:: {sig_f}\n' |
| 100 | + f' :no-typesetting:\n' |
| 101 | + f'.. {directive}:: {sig_g}\n' |
| 102 | + f' :no-typesetting:\n' |
| 103 | + f'\n' |
| 104 | + f'Heading\n' |
| 105 | + f'=======\n') |
| 106 | + doctree = restructuredtext.parse(app, text) |
| 107 | + # Note that all index nodes come before the targets and the heading is floated before those. |
| 108 | + assert_node(doctree, (nodes.title, addnodes.index, addnodes.index, nodes.target, nodes.target)) |
0 commit comments