| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
""" |
| 7 |
Tests for docutils.transforms.frontmatter.DocInfo. |
| 8 |
""" |
| 9 |
|
| 10 |
from pathlib import Path |
| 11 |
import sys |
| 12 |
import unittest |
| 13 |
|
| 14 |
if __name__ == '__main__': |
| 15 |
|
| 16 |
|
| 17 |
sys.path.insert(0, str(Path(__file__).resolve().parents[2])) |
| 18 |
|
| 19 |
from docutils.frontend import get_default_settings |
| 20 |
from docutils.parsers.rst import Parser |
| 21 |
from docutils.transforms.frontmatter import DocInfo |
| 22 |
from docutils.transforms.universal import FilterMessages, TestMessages |
| 23 |
from docutils.utils import new_document |
| 24 |
|
| 25 |
|
| 26 |
class TransformTestCase(unittest.TestCase): |
| 27 |
|
| 28 |
maxDiff = None |
| 29 |
settings = get_default_settings(Parser) |
| 30 |
settings.warning_stream = '' |
| 31 |
parser = Parser() |
| 32 |
|
| 33 |
def check_output(self, samples, settings): |
| 34 |
|
| 35 |
for key, (transforms, cases) in samples.items(): |
| 36 |
for casenum, (case_input, case_expected) in enumerate(cases): |
| 37 |
with self.subTest(id=f'samples[{key!r}][{casenum}]'): |
| 38 |
document = new_document('test data', settings) |
| 39 |
self.parser.parse(case_input, document) |
| 40 |
|
| 41 |
|
| 42 |
document.transformer.add_transforms(transforms) |
| 43 |
document.transformer.add_transform(TestMessages) |
| 44 |
|
| 45 |
|
| 46 |
document.transformer.add_transform(FilterMessages, 890) |
| 47 |
document.transformer.apply_transforms() |
| 48 |
self.assertEqual(case_expected, document.pformat()) |
| 49 |
|
| 50 |
def test_transforms(self): |
| 51 |
self.check_output(totest, self.settings) |
| 52 |
|
| 53 |
def test_transforms_de(self): |
| 54 |
settings = self.settings.copy() |
| 55 |
settings.language_code = 'de' |
| 56 |
self.check_output(totest_de, settings) |
| 57 |
|
| 58 |
def test_transforms_ru(self): |
| 59 |
settings = self.settings.copy() |
| 60 |
settings.language_code = 'ru' |
| 61 |
self.check_output(totest_ru, settings) |
| 62 |
|
| 63 |
|
| 64 |
totest = {} |
| 65 |
totest_de = {} |
| 66 |
totest_ru = {} |
| 67 |
|
| 68 |
totest['bibliographic_field_lists'] = ((DocInfo,), [ |
| 69 |
["""\ |
| 70 |
.. Bibliographic element extraction. |
| 71 |
|
| 72 |
:Abstract: |
| 73 |
There can only be one abstract. |
| 74 |
|
| 75 |
It is automatically moved to the end of the other bibliographic elements. |
| 76 |
|
| 77 |
:Author: E. *Xample* |
| 78 |
|
| 79 |
:Version: 1 |
| 80 |
|
| 81 |
:Date: 2001-08-11 |
| 82 |
|
| 83 |
:Parameter i: integer |
| 84 |
""", |
| 85 |
"""\ |
| 86 |
<document source="test data"> |
| 87 |
<docinfo> |
| 88 |
<author> |
| 89 |
E. \n\ |
| 90 |
<emphasis> |
| 91 |
Xample |
| 92 |
<version> |
| 93 |
1 |
| 94 |
<date> |
| 95 |
2001-08-11 |
| 96 |
<field classes="parameter-i"> |
| 97 |
<field_name> |
| 98 |
Parameter i |
| 99 |
<field_body> |
| 100 |
<paragraph> |
| 101 |
integer |
| 102 |
<topic classes="abstract"> |
| 103 |
<title> |
| 104 |
Abstract |
| 105 |
<paragraph> |
| 106 |
There can only be one abstract. |
| 107 |
<paragraph> |
| 108 |
It is automatically moved to the end of the other bibliographic elements. |
| 109 |
<comment xml:space="preserve"> |
| 110 |
Bibliographic element extraction. |
| 111 |
"""], |
| 112 |
["""\ |
| 113 |
.. Bibliographic element extraction. |
| 114 |
|
| 115 |
:Abstract: Abstract 1. |
| 116 |
:Author: Me |
| 117 |
:Address: 123 My Street |
| 118 |
Example, EX |
| 119 |
:Contact: [email protected] |
| 120 |
:Version: 1 |
| 121 |
:Abstract: Abstract 2 (should generate a warning). |
| 122 |
:Date: 2001-08-11 |
| 123 |
:Parameter i: integer |
| 124 |
""", |
| 125 |
"""\ |
| 126 |
<document source="test data"> |
| 127 |
<docinfo> |
| 128 |
<author> |
| 129 |
Me |
| 130 |
<address xml:space="preserve"> |
| 131 |
123 My Street |
| 132 |
Example, EX |
| 133 |
<contact> |
| 134 |
<reference refuri="mailto:[email protected]"> |
| 135 |
[email protected] |
| 136 |
<version> |
| 137 |
1 |
| 138 |
<field classes="abstract"> |
| 139 |
<field_name> |
| 140 |
Abstract |
| 141 |
<field_body> |
| 142 |
<paragraph> |
| 143 |
Abstract 2 (should generate a warning). |
| 144 |
<system_message level="2" line="9" source="test data" type="WARNING"> |
| 145 |
<paragraph> |
| 146 |
There can only be one "Abstract" field. |
| 147 |
<date> |
| 148 |
2001-08-11 |
| 149 |
<field classes="parameter-i"> |
| 150 |
<field_name> |
| 151 |
Parameter i |
| 152 |
<field_body> |
| 153 |
<paragraph> |
| 154 |
integer |
| 155 |
<topic classes="abstract"> |
| 156 |
<title> |
| 157 |
Abstract |
| 158 |
<paragraph> |
| 159 |
Abstract 1. |
| 160 |
<comment xml:space="preserve"> |
| 161 |
Bibliographic element extraction. |
| 162 |
"""], |
| 163 |
["""\ |
| 164 |
:Author: - must be a paragraph |
| 165 |
:Status: a *simple* paragraph |
| 166 |
:Date: But only one |
| 167 |
|
| 168 |
paragraph. |
| 169 |
:Version: |
| 170 |
|
| 171 |
.. and not empty either |
| 172 |
""", |
| 173 |
"""\ |
| 174 |
<document source="test data"> |
| 175 |
<docinfo> |
| 176 |
<field classes="author"> |
| 177 |
<field_name> |
| 178 |
Author |
| 179 |
<field_body> |
| 180 |
<bullet_list bullet="-"> |
| 181 |
<list_item> |
| 182 |
<paragraph> |
| 183 |
must be a paragraph |
| 184 |
<system_message level="2" line="1" source="test data" type="WARNING"> |
| 185 |
<paragraph> |
| 186 |
Bibliographic field "Author" |
| 187 |
must contain a single <paragraph>, not a <bullet_list>. |
| 188 |
<status> |
| 189 |
a \n\ |
| 190 |
<emphasis> |
| 191 |
simple |
| 192 |
paragraph |
| 193 |
<field classes="date"> |
| 194 |
<field_name> |
| 195 |
Date |
| 196 |
<field_body> |
| 197 |
<paragraph> |
| 198 |
But only one |
| 199 |
<paragraph> |
| 200 |
paragraph. |
| 201 |
<system_message level="2" line="3" source="test data" type="WARNING"> |
| 202 |
<paragraph> |
| 203 |
Bibliographic field "Date" |
| 204 |
must contain a single <paragraph>, not [<paragraph>, <paragraph>]. |
| 205 |
<field classes="version"> |
| 206 |
<field_name> |
| 207 |
Version |
| 208 |
<field_body> |
| 209 |
<system_message level="2" line="6" source="test data" type="WARNING"> |
| 210 |
<paragraph> |
| 211 |
Cannot extract empty bibliographic field "Version". |
| 212 |
<comment xml:space="preserve"> |
| 213 |
and not empty either |
| 214 |
"""], |
| 215 |
["""\ |
| 216 |
:Authors: Me, Myself, **I** |
| 217 |
:Authors: PacMan; Ms. PacMan; PacMan, Jr. |
| 218 |
:Authors: |
| 219 |
Here |
| 220 |
|
| 221 |
There |
| 222 |
|
| 223 |
*Everywhere* |
| 224 |
:Authors: - First |
| 225 |
- Second |
| 226 |
- Third |
| 227 |
""", |
| 228 |
"""\ |
| 229 |
<document source="test data"> |
| 230 |
<docinfo> |
| 231 |
<authors> |
| 232 |
<author> |
| 233 |
Me |
| 234 |
<author> |
| 235 |
Myself |
| 236 |
<author> |
| 237 |
I |
| 238 |
<authors> |
| 239 |
<author> |
| 240 |
PacMan |
| 241 |
<author> |
| 242 |
Ms. PacMan |
| 243 |
<author> |
| 244 |
PacMan, Jr. |
| 245 |
<authors> |
| 246 |
<author> |
| 247 |
Here |
| 248 |
<author> |
| 249 |
There |
| 250 |
<author> |
| 251 |
<emphasis> |
| 252 |
Everywhere |
| 253 |
<authors> |
| 254 |
<author> |
| 255 |
First |
| 256 |
<author> |
| 257 |
Second |
| 258 |
<author> |
| 259 |
Third |
| 260 |
"""], |
| 261 |
["""\ |
| 262 |
:Authors: Only One |
| 263 |
:Authors: One, Only; |
| 264 |
""", |
| 265 |
"""\ |
| 266 |
<document source="test data"> |
| 267 |
<docinfo> |
| 268 |
<authors> |
| 269 |
<author> |
| 270 |
Only One |
| 271 |
<authors> |
| 272 |
<author> |
| 273 |
One, Only |
| 274 |
"""], |
| 275 |
[r""":Authors: Me\, Myself; **I** |
| 276 |
:Authors: Pac\;Man\\; Ms. Pac\Man; Pac\ Man, Jr. |
| 277 |
:Authors: |
| 278 |
Here |
| 279 |
|
| 280 |
The\re |
| 281 |
|
| 282 |
*Every\ where* |
| 283 |
:Authors: - First\\ |
| 284 |
- Se\ cond |
| 285 |
- Thir\d |
| 286 |
""", |
| 287 |
"""\ |
| 288 |
<document source="test data"> |
| 289 |
<docinfo> |
| 290 |
<authors> |
| 291 |
<author> |
| 292 |
Me, Myself |
| 293 |
<author> |
| 294 |
I |
| 295 |
<authors> |
| 296 |
<author> |
| 297 |
Pac;Man\\ |
| 298 |
<author> |
| 299 |
Ms. PacMan |
| 300 |
<author> |
| 301 |
PacMan, Jr. |
| 302 |
<authors> |
| 303 |
<author> |
| 304 |
Here |
| 305 |
<author> |
| 306 |
There |
| 307 |
<author> |
| 308 |
<emphasis> |
| 309 |
Everywhere |
| 310 |
<authors> |
| 311 |
<author> |
| 312 |
First\\ |
| 313 |
<author> |
| 314 |
Second |
| 315 |
<author> |
| 316 |
Third |
| 317 |
"""], |
| 318 |
["""\ |
| 319 |
:Authors: |
| 320 |
|
| 321 |
:Authors: A. Einstein |
| 322 |
B. Shaw |
| 323 |
|
| 324 |
:Authors: |
| 325 |
- |
| 326 |
- |
| 327 |
|
| 328 |
:Authors: |
| 329 |
- One |
| 330 |
|
| 331 |
Two |
| 332 |
|
| 333 |
:Authors: |
| 334 |
- One |
| 335 |
|
| 336 |
Two |
| 337 |
""", |
| 338 |
"""\ |
| 339 |
<document source="test data"> |
| 340 |
<docinfo> |
| 341 |
<field classes="authors"> |
| 342 |
<field_name> |
| 343 |
Authors |
| 344 |
<field_body> |
| 345 |
<system_message level="2" line="1" source="test data" type="WARNING"> |
| 346 |
<paragraph> |
| 347 |
Cannot extract empty bibliographic field "Authors". |
| 348 |
<field classes="authors"> |
| 349 |
<field_name> |
| 350 |
Authors |
| 351 |
<field_body> |
| 352 |
<enumerated_list enumtype="upperalpha" prefix="" suffix="."> |
| 353 |
<list_item> |
| 354 |
<paragraph> |
| 355 |
Einstein |
| 356 |
<list_item> |
| 357 |
<paragraph> |
| 358 |
Shaw |
| 359 |
<system_message level="2" line="3" source="test data" type="WARNING"> |
| 360 |
<paragraph> |
| 361 |
Cannot extract "Authors" from bibliographic field: |
| 362 |
Bibliographic field "Authors" must contain either |
| 363 |
a single paragraph (with author names separated by a character from the set ";,"), |
| 364 |
multiple paragraphs (one per author), |
| 365 |
or a bullet list with one author name per item. |
| 366 |
Note: Leading initials can cause (mis)recognizing names as enumerated list. |
| 367 |
<field classes="authors"> |
| 368 |
<field_name> |
| 369 |
Authors |
| 370 |
<field_body> |
| 371 |
<bullet_list bullet="-"> |
| 372 |
<list_item> |
| 373 |
<list_item> |
| 374 |
<system_message level="2" line="6" source="test data" type="WARNING"> |
| 375 |
<paragraph> |
| 376 |
Cannot extract "Authors" from bibliographic field: |
| 377 |
Bibliographic field "Authors" must contain either |
| 378 |
a single paragraph (with author names separated by a character from the set ";,"), |
| 379 |
multiple paragraphs (one per author), |
| 380 |
or a bullet list with one author name per item. |
| 381 |
Note: Leading initials can cause (mis)recognizing names as enumerated list. |
| 382 |
<field classes="authors"> |
| 383 |
<field_name> |
| 384 |
Authors |
| 385 |
<field_body> |
| 386 |
<bullet_list bullet="-"> |
| 387 |
<list_item> |
| 388 |
<paragraph> |
| 389 |
One |
| 390 |
<paragraph> |
| 391 |
Two |
| 392 |
<system_message level="2" line="10" source="test data" type="WARNING"> |
| 393 |
<paragraph> |
| 394 |
Cannot extract "Authors" from bibliographic field: |
| 395 |
Bibliographic field "Authors" must contain either |
| 396 |
a single paragraph (with author names separated by a character from the set ";,"), |
| 397 |
multiple paragraphs (one per author), |
| 398 |
or a bullet list with one author name per item. |
| 399 |
Note: Leading initials can cause (mis)recognizing names as enumerated list. |
| 400 |
<field classes="authors"> |
| 401 |
<field_name> |
| 402 |
Authors |
| 403 |
<field_body> |
| 404 |
<bullet_list bullet="-"> |
| 405 |
<list_item> |
| 406 |
<paragraph> |
| 407 |
One |
| 408 |
<paragraph> |
| 409 |
Two |
| 410 |
<system_message level="2" line="15" source="test data" type="WARNING"> |
| 411 |
<paragraph> |
| 412 |
Cannot extract "Authors" from bibliographic field: |
| 413 |
Bibliographic field "Authors" must contain either |
| 414 |
a single paragraph (with author names separated by a character from the set ";,"), |
| 415 |
multiple paragraphs (one per author), |
| 416 |
or a bullet list with one author name per item. |
| 417 |
Note: Leading initials can cause (mis)recognizing names as enumerated list. |
| 418 |
"""], |
| 419 |
["""\ |
| 420 |
.. RCS keyword extraction. |
| 421 |
|
| 422 |
:Status: (some text) $""" + """RCSfile: test_docinfo.py,v $ (more text) |
| 423 |
:Date: (some text) $""" + """Date: 2002/10/08 01:34:23 $ (more text) |
| 424 |
:Date: (some text) $""" + """Date: 2005-03-26T16:21:28.693201Z $ (more text) |
| 425 |
:Version: (some text) $""" + """Revision: 1.1 $ (more text) |
| 426 |
""", |
| 427 |
"""\ |
| 428 |
<document source="test data"> |
| 429 |
<docinfo> |
| 430 |
<status> |
| 431 |
(some text) test_docinfo.py (more text) |
| 432 |
<date> |
| 433 |
(some text) 2002-10-08 (more text) |
| 434 |
<date> |
| 435 |
(some text) 2005-03-26 (more text) |
| 436 |
<version> |
| 437 |
(some text) 1.1 (more text) |
| 438 |
<comment xml:space="preserve"> |
| 439 |
RCS keyword extraction. |
| 440 |
"""], |
| 441 |
]) |
| 442 |
|
| 443 |
totest_de['bibliographic_field_lists_de'] = ((DocInfo,), [ |
| 444 |
["""\ |
| 445 |
.. Bibliographic element extraction for a German document. |
| 446 |
|
| 447 |
:Zusammenfassung: Abstract 1. |
| 448 |
:Autor: Me |
| 449 |
:Adresse: 123 My Street |
| 450 |
Example, EX |
| 451 |
:Kontakt: [email protected] |
| 452 |
:Version: 1 |
| 453 |
:Datum: 2001-08-11 |
| 454 |
:Parameter i: integer |
| 455 |
""", |
| 456 |
"""\ |
| 457 |
<document source="test data"> |
| 458 |
<docinfo> |
| 459 |
<author> |
| 460 |
Me |
| 461 |
<address xml:space="preserve"> |
| 462 |
123 My Street |
| 463 |
Example, EX |
| 464 |
<contact> |
| 465 |
<reference refuri="mailto:[email protected]"> |
| 466 |
[email protected] |
| 467 |
<version> |
| 468 |
1 |
| 469 |
<date> |
| 470 |
2001-08-11 |
| 471 |
<field classes="parameter-i"> |
| 472 |
<field_name> |
| 473 |
Parameter i |
| 474 |
<field_body> |
| 475 |
<paragraph> |
| 476 |
integer |
| 477 |
<topic classes="abstract"> |
| 478 |
<title> |
| 479 |
Zusammenfassung |
| 480 |
<paragraph> |
| 481 |
Abstract 1. |
| 482 |
<comment xml:space="preserve"> |
| 483 |
Bibliographic element extraction for a German document. |
| 484 |
"""] |
| 485 |
]) |
| 486 |
|
| 487 |
totest_ru['bibliographic_field_lists_ru'] = ((DocInfo,), [ |
| 488 |
["""\ |
| 489 |
.. Bibliographic element extraction for a Russian document. |
| 490 |
|
| 491 |
:аннотация: Abstract 1. |
| 492 |
:автор: Me |
| 493 |
:адрес: 123 My Street |
| 494 |
Example, EX |
| 495 |
:контакт: me@my.org |
| 496 |
:версия: 1 |
| 497 |
:дата: 2001-08-11 |
| 498 |
:Parameter i: integer |
| 499 |
""", |
| 500 |
"""\ |
| 501 |
<document source="test data"> |
| 502 |
<docinfo> |
| 503 |
<author> |
| 504 |
Me |
| 505 |
<address xml:space="preserve"> |
| 506 |
123 My Street |
| 507 |
Example, EX |
| 508 |
<contact> |
| 509 |
<reference refuri="mailto:[email protected]"> |
| 510 |
[email protected] |
| 511 |
<version> |
| 512 |
1 |
| 513 |
<date> |
| 514 |
2001-08-11 |
| 515 |
<field classes="parameter-i"> |
| 516 |
<field_name> |
| 517 |
Parameter i |
| 518 |
<field_body> |
| 519 |
<paragraph> |
| 520 |
integer |
| 521 |
<topic classes="abstract"> |
| 522 |
<title> |
| 523 |
Аннотация |
| 524 |
<paragraph> |
| 525 |
Abstract 1. |
| 526 |
<comment xml:space="preserve"> |
| 527 |
Bibliographic element extraction for a Russian document. |
| 528 |
"""] |
| 529 |
]) |
| 530 |
|
| 531 |
|
| 532 |
if __name__ == '__main__': |
| 533 |
unittest.main() |