| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Tests for misc.py "role" directive. |
| 9 |
""" |
| 10 |
|
| 11 |
from pathlib import Path |
| 12 |
import sys |
| 13 |
import unittest |
| 14 |
|
| 15 |
if __name__ == '__main__': |
| 16 |
|
| 17 |
|
| 18 |
sys.path.insert(0, str(Path(__file__).resolve().parents[4])) |
| 19 |
|
| 20 |
from docutils.frontend import get_default_settings |
| 21 |
from docutils.parsers.rst import Parser, roles |
| 22 |
from docutils.utils import new_document |
| 23 |
|
| 24 |
|
| 25 |
class ParserTestCase(unittest.TestCase): |
| 26 |
def test_parser(self): |
| 27 |
parser = Parser() |
| 28 |
settings = get_default_settings(Parser) |
| 29 |
settings.warning_stream = '' |
| 30 |
for name, cases in totest.items(): |
| 31 |
for casenum, (case_input, case_expected) in enumerate(cases): |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
roles._roles = {} |
| 37 |
with self.subTest(id=f'totest[{name!r}][{casenum}]'): |
| 38 |
document = new_document('test data', settings.copy()) |
| 39 |
parser.parse(case_input, document) |
| 40 |
output = document.pformat() |
| 41 |
self.assertEqual(case_expected, output) |
| 42 |
|
| 43 |
|
| 44 |
totest = {} |
| 45 |
|
| 46 |
totest['role'] = [ |
| 47 |
["""\ |
| 48 |
.. role:: custom |
| 49 |
.. role:: special |
| 50 |
|
| 51 |
:custom:`interpreted` and :special:`interpreted` |
| 52 |
""", |
| 53 |
"""\ |
| 54 |
<document source="test data"> |
| 55 |
<paragraph> |
| 56 |
<inline classes="custom"> |
| 57 |
interpreted |
| 58 |
and \n\ |
| 59 |
<inline classes="special"> |
| 60 |
interpreted |
| 61 |
"""], |
| 62 |
["""\ |
| 63 |
.. role:: custom |
| 64 |
:class: custom-class |
| 65 |
.. role:: special |
| 66 |
:class: special-class |
| 67 |
|
| 68 |
:custom:`interpreted` and :special:`interpreted` |
| 69 |
""", |
| 70 |
"""\ |
| 71 |
<document source="test data"> |
| 72 |
<paragraph> |
| 73 |
<inline classes="custom-class"> |
| 74 |
interpreted |
| 75 |
and \n\ |
| 76 |
<inline classes="special-class"> |
| 77 |
interpreted |
| 78 |
"""], |
| 79 |
["""\ |
| 80 |
Must define :custom:`interpreted` before using it. |
| 81 |
|
| 82 |
.. role:: custom |
| 83 |
|
| 84 |
Now that it's defined, :custom:`interpreted` works. |
| 85 |
""", |
| 86 |
"""\ |
| 87 |
<document source="test data"> |
| 88 |
<paragraph> |
| 89 |
Must define \n\ |
| 90 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 91 |
:custom:`interpreted` |
| 92 |
before using it. |
| 93 |
<system_message level="1" line="1" source="test data" type="INFO"> |
| 94 |
<paragraph> |
| 95 |
No role entry for "custom" in module "docutils.parsers.rst.languages.en". |
| 96 |
Trying "custom" as canonical role name. |
| 97 |
<system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR"> |
| 98 |
<paragraph> |
| 99 |
Unknown interpreted text role "custom". |
| 100 |
<paragraph> |
| 101 |
Now that it's defined, \n\ |
| 102 |
<inline classes="custom"> |
| 103 |
interpreted |
| 104 |
works. |
| 105 |
"""], |
| 106 |
["""\ |
| 107 |
.. role:: custom(emphasis) |
| 108 |
|
| 109 |
:custom:`text` |
| 110 |
""", |
| 111 |
"""\ |
| 112 |
<document source="test data"> |
| 113 |
<paragraph> |
| 114 |
<emphasis classes="custom"> |
| 115 |
text |
| 116 |
"""], |
| 117 |
["""\ |
| 118 |
.. role:: custom ( emphasis ) |
| 119 |
|
| 120 |
:custom:`text` |
| 121 |
""", |
| 122 |
"""\ |
| 123 |
<document source="test data"> |
| 124 |
<paragraph> |
| 125 |
<emphasis classes="custom"> |
| 126 |
text |
| 127 |
"""], |
| 128 |
["""\ |
| 129 |
.. role:: custom(emphasis) |
| 130 |
:class: special |
| 131 |
|
| 132 |
:custom:`text` |
| 133 |
""", |
| 134 |
"""\ |
| 135 |
<document source="test data"> |
| 136 |
<paragraph> |
| 137 |
<emphasis classes="special"> |
| 138 |
text |
| 139 |
"""], |
| 140 |
["""\ |
| 141 |
.. role:: custom(unknown-role) |
| 142 |
""", |
| 143 |
"""\ |
| 144 |
<document source="test data"> |
| 145 |
<system_message level="1" line="1" source="test data" type="INFO"> |
| 146 |
<paragraph> |
| 147 |
No role entry for "unknown-role" in module "docutils.parsers.rst.languages.en". |
| 148 |
Trying "unknown-role" as canonical role name. |
| 149 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 150 |
<paragraph> |
| 151 |
Unknown interpreted text role "unknown-role". |
| 152 |
<literal_block xml:space="preserve"> |
| 153 |
.. role:: custom(unknown-role) |
| 154 |
"""], |
| 155 |
["""\ |
| 156 |
.. role:: custom |
| 157 |
:class: 1 |
| 158 |
""", |
| 159 |
"""\ |
| 160 |
<document source="test data"> |
| 161 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 162 |
<paragraph> |
| 163 |
Error in "role" directive: |
| 164 |
invalid option value: (option: "class"; value: '1') |
| 165 |
cannot make "1" into a class name. |
| 166 |
<literal_block xml:space="preserve"> |
| 167 |
.. role:: custom |
| 168 |
:class: 1 |
| 169 |
"""], |
| 170 |
["""\ |
| 171 |
.. role:: 1 |
| 172 |
""", |
| 173 |
"""\ |
| 174 |
<document source="test data"> |
| 175 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 176 |
<paragraph> |
| 177 |
Invalid argument for "role" directive: |
| 178 |
cannot make "1" into a class name. |
| 179 |
<literal_block xml:space="preserve"> |
| 180 |
.. role:: 1 |
| 181 |
"""], |
| 182 |
["""\ |
| 183 |
.. role:: (error) |
| 184 |
""", |
| 185 |
"""\ |
| 186 |
<document source="test data"> |
| 187 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 188 |
<paragraph> |
| 189 |
"role" directive arguments not valid role names: "(error)". |
| 190 |
<literal_block xml:space="preserve"> |
| 191 |
.. role:: (error) |
| 192 |
"""], |
| 193 |
["""\ |
| 194 |
.. role:: |
| 195 |
""", |
| 196 |
"""\ |
| 197 |
<document source="test data"> |
| 198 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 199 |
<paragraph> |
| 200 |
"role" directive requires arguments on the first line. |
| 201 |
<literal_block xml:space="preserve"> |
| 202 |
.. role:: |
| 203 |
"""], |
| 204 |
["""\ |
| 205 |
Test |
| 206 |
---- |
| 207 |
|
| 208 |
.. role:: fileref(emphasis) |
| 209 |
|
| 210 |
Testing a :fileref:`role` in a nested parse. |
| 211 |
""", |
| 212 |
"""\ |
| 213 |
<document source="test data"> |
| 214 |
<section ids="test" names="test"> |
| 215 |
<title> |
| 216 |
Test |
| 217 |
<paragraph> |
| 218 |
Testing a \n\ |
| 219 |
<emphasis classes="fileref"> |
| 220 |
role |
| 221 |
in a nested parse. |
| 222 |
"""], |
| 223 |
["""\ |
| 224 |
.. role:: custom |
| 225 |
.. role:: special |
| 226 |
|
| 227 |
Empty :custom:`\\ ` and empty `\\ `:special: |
| 228 |
""", |
| 229 |
"""\ |
| 230 |
<document source="test data"> |
| 231 |
<paragraph> |
| 232 |
Empty \n\ |
| 233 |
<inline classes="custom"> |
| 234 |
and empty \n\ |
| 235 |
<inline classes="special"> |
| 236 |
"""], |
| 237 |
["""\ |
| 238 |
.. role:: CaSiNg |
| 239 |
|
| 240 |
Role names are :cAsInG:`case-insensitive`. |
| 241 |
""", |
| 242 |
"""\ |
| 243 |
<document source="test data"> |
| 244 |
<paragraph> |
| 245 |
Role names are \n\ |
| 246 |
<inline classes="casing"> |
| 247 |
case-insensitive |
| 248 |
. |
| 249 |
"""], |
| 250 |
] |
| 251 |
|
| 252 |
totest['raw_role'] = [ |
| 253 |
["""\ |
| 254 |
.. role:: html(raw) |
| 255 |
:format: html |
| 256 |
|
| 257 |
Here's some :html:`<i>raw HTML data</i>`. |
| 258 |
""", |
| 259 |
"""\ |
| 260 |
<document source="test data"> |
| 261 |
<paragraph> |
| 262 |
Here's some \n\ |
| 263 |
<raw classes="html" format="html" xml:space="preserve"> |
| 264 |
<i>raw HTML data</i> |
| 265 |
. |
| 266 |
"""], |
| 267 |
["""\ |
| 268 |
.. role:: itex(raw) |
| 269 |
:format: latex html |
| 270 |
|
| 271 |
Here's some itex markup: :itex:`$x^\\infty$`. |
| 272 |
""", |
| 273 |
"""\ |
| 274 |
<document source="test data"> |
| 275 |
<paragraph> |
| 276 |
Here's some itex markup: \n\ |
| 277 |
<raw classes="itex" format="latex html" xml:space="preserve"> |
| 278 |
$x^\\infty$ |
| 279 |
. |
| 280 |
"""], |
| 281 |
["""\ |
| 282 |
Can't use the :raw:`role` directly. |
| 283 |
""", |
| 284 |
"""\ |
| 285 |
<document source="test data"> |
| 286 |
<paragraph> |
| 287 |
Can't use the \n\ |
| 288 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 289 |
:raw:`role` |
| 290 |
directly. |
| 291 |
<system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR"> |
| 292 |
<paragraph> |
| 293 |
No format (Writer name) is associated with this role: "raw". |
| 294 |
The "raw" role cannot be used directly. |
| 295 |
Instead, use the "role" directive to create a new role with an associated format. |
| 296 |
"""], |
| 297 |
] |
| 298 |
|
| 299 |
|
| 300 |
if __name__ == '__main__': |
| 301 |
unittest.main() |