| 1 |
"""Tests sphinx.util.rst functions.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from docutils.statemachine import StringList |
| 6 |
from jinja2 import Environment |
| 7 |
|
| 8 |
from sphinx.util.rst import ( |
| 9 |
_append_epilogue, |
| 10 |
_prepend_prologue, |
| 11 |
escape, |
| 12 |
heading, |
| 13 |
textwidth, |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
def test_escape() -> None: |
| 18 |
assert escape(':ref:`id`') == r'\:ref\:\`id\`' |
| 19 |
assert escape('footnote [#]_') == r'footnote \[\#\]\_' |
| 20 |
assert escape('sphinx.application') == r'sphinx.application' |
| 21 |
assert escape('.. toctree::') == r'\.. toctree\:\:' |
| 22 |
|
| 23 |
|
| 24 |
def test_append_epilogue() -> None: |
| 25 |
epilog = 'this is rst_epilog\ngood-bye reST!' |
| 26 |
content = StringList( |
| 27 |
['hello Sphinx world', 'Sphinx is a document generator'], |
| 28 |
'dummy.rst', |
| 29 |
) |
| 30 |
_append_epilogue(content, epilog) |
| 31 |
|
| 32 |
assert list(content.xitems()) == [ |
| 33 |
('dummy.rst', 0, 'hello Sphinx world'), |
| 34 |
('dummy.rst', 1, 'Sphinx is a document generator'), |
| 35 |
('dummy.rst', 2, ''), |
| 36 |
('<rst_epilogue>', 0, 'this is rst_epilog'), |
| 37 |
('<rst_epilogue>', 1, 'good-bye reST!'), |
| 38 |
] |
| 39 |
|
| 40 |
|
| 41 |
def test_prepend_prologue() -> None: |
| 42 |
prologue = 'this is rst_prolog\nhello reST!' |
| 43 |
content = StringList( |
| 44 |
[ |
| 45 |
':title: test of SphinxFileInput', |
| 46 |
':author: Sphinx team', |
| 47 |
'', |
| 48 |
'hello Sphinx world', |
| 49 |
'Sphinx is a document generator', |
| 50 |
], |
| 51 |
'dummy.rst', |
| 52 |
) |
| 53 |
_prepend_prologue(content, prologue) |
| 54 |
|
| 55 |
assert list(content.xitems()) == [ |
| 56 |
('dummy.rst', 0, ':title: test of SphinxFileInput'), |
| 57 |
('dummy.rst', 1, ':author: Sphinx team'), |
| 58 |
('<generated>', 0, ''), |
| 59 |
('<rst_prologue>', 0, 'this is rst_prolog'), |
| 60 |
('<rst_prologue>', 1, 'hello reST!'), |
| 61 |
('<generated>', 0, ''), |
| 62 |
('dummy.rst', 2, ''), |
| 63 |
('dummy.rst', 3, 'hello Sphinx world'), |
| 64 |
('dummy.rst', 4, 'Sphinx is a document generator'), |
| 65 |
] |
| 66 |
|
| 67 |
|
| 68 |
def test_prepend_prolog_with_CR() -> None: |
| 69 |
# prologue having CR at tail |
| 70 |
prologue = 'this is rst_prolog\nhello reST!\n' |
| 71 |
content = StringList( |
| 72 |
['hello Sphinx world', 'Sphinx is a document generator'], |
| 73 |
'dummy.rst', |
| 74 |
) |
| 75 |
_prepend_prologue(content, prologue) |
| 76 |
|
| 77 |
assert list(content.xitems()) == [ |
| 78 |
('<rst_prologue>', 0, 'this is rst_prolog'), |
| 79 |
('<rst_prologue>', 1, 'hello reST!'), |
| 80 |
('<generated>', 0, ''), |
| 81 |
('dummy.rst', 0, 'hello Sphinx world'), |
| 82 |
('dummy.rst', 1, 'Sphinx is a document generator'), |
| 83 |
] |
| 84 |
|
| 85 |
|
| 86 |
def test_prepend_prolog_without_CR() -> None: |
| 87 |
# prologue not having CR at tail |
| 88 |
prologue = 'this is rst_prolog\nhello reST!' |
| 89 |
content = StringList( |
| 90 |
['hello Sphinx world', 'Sphinx is a document generator'], |
| 91 |
'dummy.rst', |
| 92 |
) |
| 93 |
_prepend_prologue(content, prologue) |
| 94 |
|
| 95 |
assert list(content.xitems()) == [ |
| 96 |
('<rst_prologue>', 0, 'this is rst_prolog'), |
| 97 |
('<rst_prologue>', 1, 'hello reST!'), |
| 98 |
('<generated>', 0, ''), |
| 99 |
('dummy.rst', 0, 'hello Sphinx world'), |
| 100 |
('dummy.rst', 1, 'Sphinx is a document generator'), |
| 101 |
] |
| 102 |
|
| 103 |
|
| 104 |
def test_prepend_prolog_with_roles_in_sections() -> None: |
| 105 |
prologue = 'this is rst_prolog\nhello reST!' |
| 106 |
content = StringList( |
| 107 |
[ |
| 108 |
':title: test of SphinxFileInput', |
| 109 |
':author: Sphinx team', |
| 110 |
'', # this newline is required |
| 111 |
':mod:`foo`', |
| 112 |
'----------', |
| 113 |
'', |
| 114 |
'hello', |
| 115 |
], |
| 116 |
'dummy.rst', |
| 117 |
) |
| 118 |
_prepend_prologue(content, prologue) |
| 119 |
|
| 120 |
assert list(content.xitems()) == [ |
| 121 |
('dummy.rst', 0, ':title: test of SphinxFileInput'), |
| 122 |
('dummy.rst', 1, ':author: Sphinx team'), |
| 123 |
('<generated>', 0, ''), |
| 124 |
('<rst_prologue>', 0, 'this is rst_prolog'), |
| 125 |
('<rst_prologue>', 1, 'hello reST!'), |
| 126 |
('<generated>', 0, ''), |
| 127 |
('dummy.rst', 2, ''), |
| 128 |
('dummy.rst', 3, ':mod:`foo`'), |
| 129 |
('dummy.rst', 4, '----------'), |
| 130 |
('dummy.rst', 5, ''), |
| 131 |
('dummy.rst', 6, 'hello'), |
| 132 |
] |
| 133 |
|
| 134 |
|
| 135 |
def test_prepend_prolog_with_roles_in_sections_with_newline() -> None: |
| 136 |
# prologue with trailing line break |
| 137 |
prologue = 'this is rst_prolog\nhello reST!\n' |
| 138 |
content = StringList([':mod:`foo`', '-' * 10, '', 'hello'], 'dummy.rst') |
| 139 |
_prepend_prologue(content, prologue) |
| 140 |
|
| 141 |
assert list(content.xitems()) == [ |
| 142 |
('<rst_prologue>', 0, 'this is rst_prolog'), |
| 143 |
('<rst_prologue>', 1, 'hello reST!'), |
| 144 |
('<generated>', 0, ''), |
| 145 |
('dummy.rst', 0, ':mod:`foo`'), |
| 146 |
('dummy.rst', 1, '----------'), |
| 147 |
('dummy.rst', 2, ''), |
| 148 |
('dummy.rst', 3, 'hello'), |
| 149 |
] |
| 150 |
|
| 151 |
|
| 152 |
def test_prepend_prolog_with_roles_in_sections_without_newline() -> None: |
| 153 |
# prologue with no trailing line break |
| 154 |
prologue = 'this is rst_prolog\nhello reST!' |
| 155 |
content = StringList([':mod:`foo`', '-' * 10, '', 'hello'], 'dummy.rst') |
| 156 |
_prepend_prologue(content, prologue) |
| 157 |
|
| 158 |
assert list(content.xitems()) == [ |
| 159 |
('<rst_prologue>', 0, 'this is rst_prolog'), |
| 160 |
('<rst_prologue>', 1, 'hello reST!'), |
| 161 |
('<generated>', 0, ''), |
| 162 |
('dummy.rst', 0, ':mod:`foo`'), |
| 163 |
('dummy.rst', 1, '----------'), |
| 164 |
('dummy.rst', 2, ''), |
| 165 |
('dummy.rst', 3, 'hello'), |
| 166 |
] |
| 167 |
|
| 168 |
|
| 169 |
def test_textwidth() -> None: |
| 170 |
assert textwidth('Hello') == 5 |
| 171 |
assert textwidth('русский язык') == 12 |
| 172 |
assert textwidth('русский язык', 'WFA') == 23 # Cyrillic are ambiguous chars |
| 173 |
|
| 174 |
|
| 175 |
def test_heading() -> None: |
| 176 |
env = Environment(autoescape=True) |
| 177 |
env.extend(language=None) |
| 178 |
|
| 179 |
assert heading(env, 'Hello') == 'Hello\n=====' |
| 180 |
assert heading(env, 'Hello', 1) == 'Hello\n=====' |
| 181 |
assert heading(env, 'Hello', 2) == 'Hello\n-----' |
| 182 |
assert heading(env, 'Hello', 3) == 'Hello\n~~~~~' |
| 183 |
assert heading(env, 'русский язык', 1) == 'русский язык\n============' |
| 184 |
|
| 185 |
# language=ja: ambiguous |
| 186 |
env.language = 'ja' # type: ignore[attr-defined] |
| 187 |
assert heading(env, 'русский язык', 1) == 'русский язык\n=======================' |