1 #! /usr/bin/env python3
4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
11 from pathlib
import Path
15 if __name__
== '__main__':
16 # prepend the "docutils root" to the Python library path
17 # so we import the local `docutils` package.
18 sys
.path
.insert(0, str(Path(__file__
).resolve().parents
[3]))
20 from docutils
.frontend
import get_default_settings
21 from docutils
.parsers
.rst
import Parser
22 from docutils
.utils
import new_document
25 class ParserTestCase(unittest
.TestCase
):
28 def test_parser(self
):
30 settings
= get_default_settings(Parser
)
31 settings
.warning_stream
= ''
32 for name
, cases
in totest
.items():
33 for casenum
, (case_input
, case_expected
) in enumerate(cases
):
34 with self
.subTest(id=f
'totest[{name!r}][{casenum}]'):
35 document
= new_document('test data', settings
.copy())
36 parser
.parse(case_input
, document
)
37 output
= document
.pformat()
38 self
.assertEqual(case_expected
, output
)
43 totest
['option_lists'] = [
54 <document source="test data">
71 <option_argument delimiter=" ">
81 <option_argument delimiter=" ">
91 --bbbb=file option --bbbb
92 --cccc name option --cccc
93 --d-e-f-g option --d-e-f-g
94 --h_i_j_k option --h_i_j_k
97 <document source="test data">
114 <option_argument delimiter="=">
124 <option_argument delimiter=" ">
147 Old GNU-style options:
156 <document source="test data">
158 Old GNU-style options:
173 <option_argument delimiter=" ">
183 <option_argument delimiter=" ">
190 VMS/DOS-style options:
195 /DDD string option /DDD
199 <document source="test data">
201 VMS/DOS-style options:
216 <option_argument delimiter=" ">
234 <option_argument delimiter=" ">
244 <option_argument delimiter="=">
251 Mixed short, long, and VMS/DOS options:
254 --bbbb=file option -bbbb
256 --dddd name option --dddd
261 <document source="test data">
263 Mixed short, long, and VMS/DOS options:
278 <option_argument delimiter="=">
296 <option_argument delimiter=" ">
306 <option_argument delimiter=" ">
316 <option_argument delimiter=" ">
325 -a, --aaaa, /A option -a, --aaaa, /A
326 -b file, --bbbb=file, /B file option -b, --bbbb, /B
329 <document source="test data">
346 option -a, --aaaa, /A
352 <option_argument delimiter=" ">
357 <option_argument delimiter="=">
362 <option_argument delimiter=" ">
366 option -b, --bbbb, /B
369 Multiple lines in descriptions, aligned:
373 -b file option -b, line 1
377 <document source="test data">
379 Multiple lines in descriptions, aligned:
395 <option_argument delimiter=" ">
403 Multiple lines in descriptions, not aligned:
407 -b file option -b, line 1
411 <document source="test data">
413 Multiple lines in descriptions, not aligned:
429 <option_argument delimiter=" ">
437 Descriptions begin on next line:
447 <document source="test data">
449 Descriptions begin on next line:
465 <option_argument delimiter=" ">
473 Multiple body elements in descriptions:
484 <document source="test data">
486 Multiple body elements in descriptions:
503 <option_argument delimiter=" ">
513 empty item above, no blank line
516 <document source="test data">
519 empty item above, no blank line
522 An option list using equals:
524 --long1=arg1 Description 1
525 --long2=arg2 Description 2
527 An option list using spaces:
529 --long1 arg1 Description 1
530 --long2 arg2 Description 2
532 An option list using mixed delimiters:
534 --long1=arg1 Description 1
535 --long2 arg2 Description 2
537 An option list using mixed delimiters in one line:
539 --long1=arg1, --long2 arg2 Description
542 <document source="test data">
544 An option list using equals:
551 <option_argument delimiter="=">
561 <option_argument delimiter="=">
567 An option list using spaces:
574 <option_argument delimiter=" ">
584 <option_argument delimiter=" ">
590 An option list using mixed delimiters:
597 <option_argument delimiter="=">
607 <option_argument delimiter=" ">
613 An option list using mixed delimiters in one line:
620 <option_argument delimiter="=">
625 <option_argument delimiter=" ">
634 --option=arg arg too many arguments
636 --option=arg,arg not supported (yet?)
638 --option=arg=arg too many arguments
640 --option arg arg too many arguments
642 -a letter arg2 too many arguments
644 /A letter arg2 too many arguments
646 --option= argument missing
648 --=argument option missing
650 -- everything missing
652 - this should be a bullet list item
654 These next ones should be simple paragraphs:
665 <document source="test data">
669 --option=arg arg too many arguments
671 --option=arg,arg not supported (yet?)
673 --option=arg=arg too many arguments
675 --option arg arg too many arguments
677 -a letter arg2 too many arguments
679 /A letter arg2 too many arguments
681 --option= argument missing
683 --=argument option missing
685 -- everything missing
686 <bullet_list bullet="-">
689 this should be a bullet list item
691 These next ones should be simple paragraphs:
704 --source-url=<URL> Use the supplied <URL> verbatim.
705 --output-encoding=<name[:handler]>, -o<name[:handler]>
706 Specify the text encoding for output.
707 --af=<filter1[=parameter1:parameter2:...],filter2,...>
708 Setup a chain of audio filters.
709 Option argument containing delimiter ``=``.
710 -f <[path]filename> Send output to file.
711 -d <src dest> Use diff from <src> to <dest>.
712 --point=<x y z> 3D coordinates.
713 --vector <"x, y, z"> Another option expecting 3 arguments.
714 --cvs=<'a[, b[, ...]]]'>
715 Comma separated arguments.
716 -g <a, , c> Complex arg with empty list element.
719 <document source="test data">
728 <option_argument delimiter="=">
732 Use the supplied <URL> verbatim.
738 <option_argument delimiter="=">
743 <option_argument delimiter="">
747 Specify the text encoding for output.
753 <option_argument delimiter="=">
754 <filter1[=parameter1:parameter2:...],filter2,...>
757 Setup a chain of audio filters.
758 Option argument containing delimiter \n\
767 <option_argument delimiter=" ">
777 <option_argument delimiter=" ">
781 Use diff from <src> to <dest>.
787 <option_argument delimiter="=">
797 <option_argument delimiter=" ">
801 Another option expecting 3 arguments.
807 <option_argument delimiter="=">
811 Comma separated arguments.
817 <option_argument delimiter=" ">
821 Complex arg with empty list element.
824 Incorrect option syntax → no option list.
826 -f, , --force Empty synonym.
827 -f <a, , b Unbalanced angle bracket.
828 -f a, , b> Unbalanced angle bracket.
831 <document source="test data">
833 Incorrect option syntax → no option list.
835 -f, , --force Empty synonym.
836 -f <a, , b Unbalanced angle bracket.
837 -f a, , b> Unbalanced angle bracket.
842 if __name__
== '__main__':