| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Tests for states.py. |
| 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[3])) |
| 19 |
|
| 20 |
from docutils.frontend import get_default_settings |
| 21 |
from docutils.parsers.rst import Parser |
| 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 |
with self.subTest(id=f'totest[{name!r}][{casenum}]'): |
| 33 |
document = new_document('test data', settings.copy()) |
| 34 |
parser.parse(case_input, document) |
| 35 |
output = document.pformat() |
| 36 |
self.assertEqual(case_expected, output) |
| 37 |
|
| 38 |
|
| 39 |
totest = {} |
| 40 |
|
| 41 |
totest['line_blocks'] = [ |
| 42 |
["""\ |
| 43 |
| This is a line block. |
| 44 |
| Line breaks are *preserved*. |
| 45 |
|
| 46 |
| This is a second line block. |
| 47 |
|
| 48 |
| This is a third. |
| 49 |
""", |
| 50 |
"""\ |
| 51 |
<document source="test data"> |
| 52 |
<line_block> |
| 53 |
<line> |
| 54 |
This is a line block. |
| 55 |
<line> |
| 56 |
Line breaks are \n\ |
| 57 |
<emphasis> |
| 58 |
preserved |
| 59 |
. |
| 60 |
<line_block> |
| 61 |
<line> |
| 62 |
This is a second line block. |
| 63 |
<line_block> |
| 64 |
<line> |
| 65 |
This is a third. |
| 66 |
"""], |
| 67 |
["""\ |
| 68 |
| In line blocks, |
| 69 |
| Initial indentation is *also* preserved. |
| 70 |
""", |
| 71 |
"""\ |
| 72 |
<document source="test data"> |
| 73 |
<line_block> |
| 74 |
<line> |
| 75 |
In line blocks, |
| 76 |
<line_block> |
| 77 |
<line> |
| 78 |
Initial indentation is \n\ |
| 79 |
<emphasis> |
| 80 |
also |
| 81 |
preserved. |
| 82 |
"""], |
| 83 |
["""\ |
| 84 |
| Individual lines in line blocks |
| 85 |
*may* wrap, as indicated by the lack of a vertical bar prefix. |
| 86 |
| These are called "continuation lines". |
| 87 |
""", |
| 88 |
"""\ |
| 89 |
<document source="test data"> |
| 90 |
<line_block> |
| 91 |
<line> |
| 92 |
Individual lines in line blocks |
| 93 |
<emphasis> |
| 94 |
may |
| 95 |
wrap, as indicated by the lack of a vertical bar prefix. |
| 96 |
<line> |
| 97 |
These are called "continuation lines". |
| 98 |
"""], |
| 99 |
["""\ |
| 100 |
| Inline markup in line blocks may also wrap *to |
| 101 |
continuation lines*. |
| 102 |
| But not to following lines. |
| 103 |
""", |
| 104 |
"""\ |
| 105 |
<document source="test data"> |
| 106 |
<line_block> |
| 107 |
<line> |
| 108 |
Inline markup in line blocks may also wrap \n\ |
| 109 |
<emphasis> |
| 110 |
to |
| 111 |
continuation lines |
| 112 |
. |
| 113 |
<line> |
| 114 |
But not to following lines. |
| 115 |
"""], |
| 116 |
["""\ |
| 117 |
\\| This is not a line block. |
| 118 |
The vertical bar is simply part of a paragraph. |
| 119 |
""", |
| 120 |
"""\ |
| 121 |
<document source="test data"> |
| 122 |
<paragraph> |
| 123 |
| This is not a line block. |
| 124 |
The vertical bar is simply part of a paragraph. |
| 125 |
"""], |
| 126 |
["""\ |
| 127 |
| This line block is incomplete. |
| 128 |
There should be a blank line before this paragraph. |
| 129 |
""", |
| 130 |
"""\ |
| 131 |
<document source="test data"> |
| 132 |
<line_block> |
| 133 |
<line> |
| 134 |
This line block is incomplete. |
| 135 |
<system_message level="2" line="2" source="test data" type="WARNING"> |
| 136 |
<paragraph> |
| 137 |
Line block ends without a blank line. |
| 138 |
<paragraph> |
| 139 |
There should be a blank line before this paragraph. |
| 140 |
"""], |
| 141 |
["""\ |
| 142 |
| This line block contains |
| 143 |
| |
| 144 |
| blank lines. |
| 145 |
""", |
| 146 |
"""\ |
| 147 |
<document source="test data"> |
| 148 |
<line_block> |
| 149 |
<line> |
| 150 |
This line block contains |
| 151 |
<line> |
| 152 |
<line> |
| 153 |
blank lines. |
| 154 |
"""], |
| 155 |
["""\ |
| 156 |
| The blank lines in this block |
| 157 |
| \n\ |
| 158 |
| \n\ |
| 159 |
| have bogus spaces. |
| 160 |
""", |
| 161 |
"""\ |
| 162 |
<document source="test data"> |
| 163 |
<line_block> |
| 164 |
<line> |
| 165 |
The blank lines in this block |
| 166 |
<line> |
| 167 |
<line> |
| 168 |
<line> |
| 169 |
have bogus spaces. |
| 170 |
"""], |
| 171 |
["""\ |
| 172 |
| Initial indentation is also significant and preserved: |
| 173 |
| |
| 174 |
| Indented 4 spaces |
| 175 |
| Not indented |
| 176 |
| Indented 2 spaces |
| 177 |
| Indented 4 spaces |
| 178 |
| Only one space |
| 179 |
| |
| 180 |
| Continuation lines may be indented less |
| 181 |
than their base lines. |
| 182 |
""", |
| 183 |
"""\ |
| 184 |
<document source="test data"> |
| 185 |
<line_block> |
| 186 |
<line> |
| 187 |
Initial indentation is also significant and preserved: |
| 188 |
<line> |
| 189 |
<line_block> |
| 190 |
<line> |
| 191 |
Indented 4 spaces |
| 192 |
<line> |
| 193 |
Not indented |
| 194 |
<line_block> |
| 195 |
<line_block> |
| 196 |
<line> |
| 197 |
Indented 2 spaces |
| 198 |
<line_block> |
| 199 |
<line> |
| 200 |
Indented 4 spaces |
| 201 |
<line> |
| 202 |
Only one space |
| 203 |
<line> |
| 204 |
<line_block> |
| 205 |
<line> |
| 206 |
Continuation lines may be indented less |
| 207 |
than their base lines. |
| 208 |
"""], |
| 209 |
["""\ |
| 210 |
| |
| 211 |
| This block begins and ends with blank lines. |
| 212 |
| |
| 213 |
""", |
| 214 |
"""\ |
| 215 |
<document source="test data"> |
| 216 |
<line_block> |
| 217 |
<line> |
| 218 |
<line> |
| 219 |
This block begins and ends with blank lines. |
| 220 |
<line> |
| 221 |
"""], |
| 222 |
["""\ |
| 223 |
This is not |
| 224 |
| a line block. |
| 225 |
""", |
| 226 |
"""\ |
| 227 |
<document source="test data"> |
| 228 |
<paragraph> |
| 229 |
This is not |
| 230 |
| a line block. |
| 231 |
"""], |
| 232 |
["""\ |
| 233 |
| The first line is indented. |
| 234 |
| The second line is more indented. |
| 235 |
""", |
| 236 |
"""\ |
| 237 |
<document source="test data"> |
| 238 |
<line_block> |
| 239 |
<line> |
| 240 |
The first line is indented. |
| 241 |
<line_block> |
| 242 |
<line> |
| 243 |
The second line is more indented. |
| 244 |
"""], |
| 245 |
["""\ |
| 246 |
| The first line is indented. |
| 247 |
| The second line is less indented. |
| 248 |
""", |
| 249 |
"""\ |
| 250 |
<document source="test data"> |
| 251 |
<line_block> |
| 252 |
<line_block> |
| 253 |
<line> |
| 254 |
The first line is indented. |
| 255 |
<line> |
| 256 |
The second line is less indented. |
| 257 |
"""], |
| 258 |
["""\ |
| 259 |
|This is not |
| 260 |
|a line block |
| 261 |
|
| 262 |
| This is an |
| 263 |
|incomplete line block. |
| 264 |
""", |
| 265 |
"""\ |
| 266 |
<document source="test data"> |
| 267 |
<paragraph> |
| 268 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 269 |
| |
| 270 |
This is not |
| 271 |
<problematic ids="problematic-2" refid="system-message-2"> |
| 272 |
| |
| 273 |
a line block |
| 274 |
<system_message backrefs="problematic-1" ids="system-message-1" level="2" line="1" source="test data" type="WARNING"> |
| 275 |
<paragraph> |
| 276 |
Inline substitution_reference start-string without end-string. |
| 277 |
<system_message backrefs="problematic-2" ids="system-message-2" level="2" line="1" source="test data" type="WARNING"> |
| 278 |
<paragraph> |
| 279 |
Inline substitution_reference start-string without end-string. |
| 280 |
<line_block> |
| 281 |
<line> |
| 282 |
This is an |
| 283 |
<system_message level="2" line="5" source="test data" type="WARNING"> |
| 284 |
<paragraph> |
| 285 |
Line block ends without a blank line. |
| 286 |
<paragraph> |
| 287 |
<problematic ids="problematic-3" refid="system-message-3"> |
| 288 |
| |
| 289 |
incomplete line block. |
| 290 |
<system_message backrefs="problematic-3" ids="system-message-3" level="2" line="5" source="test data" type="WARNING"> |
| 291 |
<paragraph> |
| 292 |
Inline substitution_reference start-string without end-string. |
| 293 |
"""], |
| 294 |
["""\ |
| 295 |
| Inline markup *may not |
| 296 |
| wrap* over several lines. |
| 297 |
""", |
| 298 |
"""\ |
| 299 |
<document source="test data"> |
| 300 |
<line_block> |
| 301 |
<line> |
| 302 |
Inline markup \n\ |
| 303 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 304 |
* |
| 305 |
may not |
| 306 |
<line> |
| 307 |
wrap* over several lines. |
| 308 |
<system_message backrefs="problematic-1" ids="system-message-1" level="2" line="1" source="test data" type="WARNING"> |
| 309 |
<paragraph> |
| 310 |
Inline emphasis start-string without end-string. |
| 311 |
"""], |
| 312 |
["""\ |
| 313 |
| * Block level markup |
| 314 |
| * is not recognized. |
| 315 |
""", |
| 316 |
"""\ |
| 317 |
<document source="test data"> |
| 318 |
<line_block> |
| 319 |
<line> |
| 320 |
* Block level markup |
| 321 |
<line> |
| 322 |
* is not recognized. |
| 323 |
"""], |
| 324 |
["""\ |
| 325 |
System messages are no longer inserted between <line>s: |
| 326 |
|
| 327 |
| `uff <test1>`_ |
| 328 |
| `uff <test2>`_ |
| 329 |
""", |
| 330 |
"""\ |
| 331 |
<document source="test data"> |
| 332 |
<paragraph> |
| 333 |
System messages are no longer inserted between <line>s: |
| 334 |
<line_block> |
| 335 |
<line> |
| 336 |
<reference name="uff" refuri="test1"> |
| 337 |
uff |
| 338 |
<target dupnames="uff" ids="uff" refuri="test1"> |
| 339 |
<line> |
| 340 |
<reference name="uff" refuri="test2"> |
| 341 |
uff |
| 342 |
<target dupnames="uff" ids="uff-1" refuri="test2"> |
| 343 |
"""], |
| 344 |
] |
| 345 |
|
| 346 |
|
| 347 |
if __name__ == '__main__': |
| 348 |
unittest.main() |