summaryrefslogtreecommitdiffstats
path: root/tests/test_domain_rst.py
blob: 4445da1d2014b681723d08ebf998608e261ec750 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""Tests the reStructuredText domain."""

from sphinx import addnodes
from sphinx.addnodes import (
    desc,
    desc_addname,
    desc_annotation,
    desc_content,
    desc_name,
    desc_signature,
)
from sphinx.domains.rst import parse_directive
from sphinx.testing import restructuredtext
from sphinx.testing.util import assert_node


def test_parse_directive():
    s = parse_directive(' foö  ')
    assert s == ('foö', '')

    s = parse_directive(' ..    foö ::  ')
    assert s == ('foö', '')

    s = parse_directive('.. foö:: args1 args2')
    assert s == ('foö', ' args1 args2')

    s = parse_directive('.. :: bar')
    assert s == ('.. :: bar', '')


def test_rst_directive(app):
    # bare
    text = ".. rst:directive:: toctree"
    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, (addnodes.index,
                          [desc, ([desc_signature, desc_name, ".. toctree::"],
                                  [desc_content, ()])]))
    assert_node(doctree[0],
                entries=[("single", "toctree (directive)", "directive-toctree", "", None)])
    assert_node(doctree[1], addnodes.desc, desctype="directive",
                domain="rst", objtype="directive", no_index=False)

    # decorated
    text = ".. rst:directive:: .. toctree::"
    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, (addnodes.index,
                          [desc, ([desc_signature, desc_name, ".. toctree::"],
                                  [desc_content, ()])]))
    assert_node(doctree[0],
                entries=[("single", "toctree (directive)", "directive-toctree", "", None)])
    assert_node(doctree[1], addnodes.desc, desctype="directive",
                domain="rst", objtype="directive", no_index=False)


def test_rst_directive_with_argument(app):
    text = ".. rst:directive:: .. toctree:: foo bar baz"
    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, (addnodes.index,
                          [desc, ([desc_signature, ([desc_name, ".. toctree::"],
                                                    [desc_addname, " foo bar baz"])],
                                  [desc_content, ()])]))
    assert_node(doctree[0],
                entries=[("single", "toctree (directive)", "directive-toctree", "", None)])
    assert_node(doctree[1], addnodes.desc, desctype="directive",
                domain="rst", objtype="directive", no_index=False)


def test_rst_directive_option(app):
    text = ".. rst:directive:option:: foo"
    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, (addnodes.index,
                          [desc, ([desc_signature, desc_name, ":foo:"],
                                  [desc_content, ()])]))
    assert_node(doctree[0],
                entries=[("single", ":foo: (directive option)",
                          "directive-option-foo", "", "F")])
    assert_node(doctree[1], addnodes.desc, desctype="directive:option",
                domain="rst", objtype="directive:option", no_index=False)


def test_rst_directive_option_with_argument(app):
    text = ".. rst:directive:option:: foo: bar baz"
    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, (addnodes.index,
                          [desc, ([desc_signature, ([desc_name, ":foo:"],
                                                    [desc_annotation, " bar baz"])],
                                  [desc_content, ()])]))
    assert_node(doctree[0],
                entries=[("single", ":foo: (directive option)",
                          "directive-option-foo", "", "F")])
    assert_node(doctree[1], addnodes.desc, desctype="directive:option",
                domain="rst", objtype="directive:option", no_index=False)


def test_rst_directive_option_type(app):
    text = (".. rst:directive:option:: foo\n"
            "   :type: directives.flags\n")
    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, (addnodes.index,
                          [desc, ([desc_signature, ([desc_name, ":foo:"],
                                                    [desc_annotation, " (directives.flags)"])],
                                  [desc_content, ()])]))
    assert_node(doctree[0],
                entries=[("single", ":foo: (directive option)",
                          "directive-option-foo", "", "F")])
    assert_node(doctree[1], addnodes.desc, desctype="directive:option",
                domain="rst", objtype="directive:option", no_index=False)


def test_rst_directive_and_directive_option(app):
    text = (".. rst:directive:: foo\n"
            "\n"
            "   .. rst:directive:option:: bar\n")
    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, (addnodes.index,
                          [desc, ([desc_signature, desc_name, ".. foo::"],
                                  [desc_content, (addnodes.index,
                                                  desc)])]))
    assert_node(doctree[1][1][0],
                entries=[("pair", "foo (directive); :bar: (directive option)",
                          "directive-option-foo-bar", "", "B")])
    assert_node(doctree[1][1][1], ([desc_signature, desc_name, ":bar:"],
                                   [desc_content, ()]))
    assert_node(doctree[1][1][1], addnodes.desc, desctype="directive:option",
                domain="rst", objtype="directive:option", no_index=False)


def test_rst_role(app):
    text = ".. rst:role:: ref"
    doctree = restructuredtext.parse(app, text)
    assert_node(doctree, (addnodes.index,
                          [desc, ([desc_signature, desc_name, ":ref:"],
                                  [desc_content, ()])]))
    assert_node(doctree[0],
                entries=[("single", "ref (role)", "role-ref", "", None)])
    assert_node(doctree[1], addnodes.desc, desctype="role",
                domain="rst", objtype="role", no_index=False)