1""" 2 test_directive_other 3 ~~~~~~~~~~~~~~~~~~~~ 4 5 Test the other directives. 6 7 :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS. 8 :license: BSD, see LICENSE for details. 9""" 10 11import pytest 12from docutils import nodes 13 14from sphinx import addnodes 15from sphinx.testing import restructuredtext 16from sphinx.testing.util import assert_node 17 18 19@pytest.mark.sphinx(testroot='toctree-glob') 20def test_toctree(app): 21 text = (".. toctree::\n" 22 "\n" 23 " foo\n" 24 " bar/index\n" 25 " baz\n") 26 27 app.env.find_files(app.config, app.builder) 28 doctree = restructuredtext.parse(app, text, 'index') 29 assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) 30 assert_node(doctree[0][0], 31 entries=[(None, 'foo'), (None, 'bar/index'), (None, 'baz')], 32 includefiles=['foo', 'bar/index', 'baz']) 33 34 35@pytest.mark.sphinx(testroot='toctree-glob') 36def test_relative_toctree(app): 37 text = (".. toctree::\n" 38 "\n" 39 " bar_1\n" 40 " bar_2\n" 41 " bar_3\n" 42 " ../quux\n") 43 44 app.env.find_files(app.config, app.builder) 45 doctree = restructuredtext.parse(app, text, 'bar/index') 46 assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) 47 assert_node(doctree[0][0], 48 entries=[(None, 'bar/bar_1'), (None, 'bar/bar_2'), (None, 'bar/bar_3'), 49 (None, 'quux')], 50 includefiles=['bar/bar_1', 'bar/bar_2', 'bar/bar_3', 'quux']) 51 52 53@pytest.mark.sphinx(testroot='toctree-glob') 54def test_toctree_urls_and_titles(app): 55 text = (".. toctree::\n" 56 "\n" 57 " Sphinx <https://www.sphinx-doc.org/>\n" 58 " https://readthedocs.org/\n" 59 " The BAR <bar/index>\n") 60 61 app.env.find_files(app.config, app.builder) 62 doctree = restructuredtext.parse(app, text, 'index') 63 assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) 64 assert_node(doctree[0][0], 65 entries=[('Sphinx', 'https://www.sphinx-doc.org/'), 66 (None, 'https://readthedocs.org/'), 67 ('The BAR', 'bar/index')], 68 includefiles=['bar/index']) 69 70 71@pytest.mark.sphinx(testroot='toctree-glob') 72def test_toctree_glob(app): 73 text = (".. toctree::\n" 74 " :glob:\n" 75 "\n" 76 " *\n") 77 78 app.env.find_files(app.config, app.builder) 79 doctree = restructuredtext.parse(app, text, 'index') 80 assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) 81 assert_node(doctree[0][0], 82 entries=[(None, 'baz'), (None, 'foo'), (None, 'quux')], 83 includefiles=['baz', 'foo', 'quux']) 84 85 # give both docname and glob (case1) 86 text = (".. toctree::\n" 87 " :glob:\n" 88 "\n" 89 " foo\n" 90 " *\n") 91 92 app.env.find_files(app.config, app.builder) 93 doctree = restructuredtext.parse(app, text, 'index') 94 assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) 95 assert_node(doctree[0][0], 96 entries=[(None, 'foo'), (None, 'baz'), (None, 'quux')], 97 includefiles=['foo', 'baz', 'quux']) 98 99 # give both docname and glob (case2) 100 text = (".. toctree::\n" 101 " :glob:\n" 102 "\n" 103 " *\n" 104 " foo\n") 105 106 app.env.find_files(app.config, app.builder) 107 doctree = restructuredtext.parse(app, text, 'index') 108 assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) 109 assert_node(doctree[0][0], 110 entries=[(None, 'baz'), (None, 'foo'), (None, 'quux'), (None, 'foo')], 111 includefiles=['baz', 'foo', 'quux', 'foo']) 112 113 114@pytest.mark.sphinx(testroot='toctree-glob') 115def test_toctree_glob_and_url(app): 116 text = (".. toctree::\n" 117 " :glob:\n" 118 "\n" 119 " https://example.com/?q=sphinx\n") 120 121 app.env.find_files(app.config, app.builder) 122 doctree = restructuredtext.parse(app, text, 'index') 123 assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) 124 assert_node(doctree[0][0], 125 entries=[(None, 'https://example.com/?q=sphinx')], 126 includefiles=[]) 127 128 129@pytest.mark.sphinx(testroot='toctree-glob') 130def test_reversed_toctree(app): 131 text = (".. toctree::\n" 132 " :reversed:\n" 133 "\n" 134 " foo\n" 135 " bar/index\n" 136 " baz\n") 137 138 app.env.find_files(app.config, app.builder) 139 doctree = restructuredtext.parse(app, text, 'index') 140 assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) 141 assert_node(doctree[0][0], 142 entries=[(None, 'baz'), (None, 'bar/index'), (None, 'foo')], 143 includefiles=['baz', 'bar/index', 'foo']) 144 145 146@pytest.mark.sphinx(testroot='toctree-glob') 147def test_toctree_twice(app): 148 text = (".. toctree::\n" 149 "\n" 150 " foo\n" 151 " foo\n") 152 153 app.env.find_files(app.config, app.builder) 154 doctree = restructuredtext.parse(app, text, 'index') 155 assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) 156 assert_node(doctree[0][0], 157 entries=[(None, 'foo'), (None, 'foo')], 158 includefiles=['foo', 'foo']) 159