diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-29 04:23:02 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-29 04:23:02 +0000 |
commit | 943e3dc057eca53e68ddec51529bd6a1279ebd8e (patch) | |
tree | 61fb7bac619a56dfbcdcbdb7b0d4d6535fc36fe9 /tests/test_renderers/test_parse_directives.py | |
parent | Initial commit. (diff) | |
download | myst-parser-upstream.tar.xz myst-parser-upstream.zip |
Adding upstream version 0.18.1.upstream/0.18.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/test_renderers/test_parse_directives.py')
-rw-r--r-- | tests/test_renderers/test_parse_directives.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/test_renderers/test_parse_directives.py b/tests/test_renderers/test_parse_directives.py new file mode 100644 index 0000000..ae6792b --- /dev/null +++ b/tests/test_renderers/test_parse_directives.py @@ -0,0 +1,46 @@ +from pathlib import Path + +import pytest +import yaml +from docutils.parsers.rst.directives.admonitions import Admonition, Note +from docutils.parsers.rst.directives.body import Rubric +from markdown_it import MarkdownIt + +from myst_parser.parsers.directives import DirectiveParsingError, parse_directive_text + +FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures") + + +@pytest.mark.param_file(FIXTURE_PATH / "directive_parsing.txt") +def test_parsing(file_params): + """Test parsing of directive text.""" + tokens = MarkdownIt("commonmark").parse(file_params.content) + assert len(tokens) == 1 and tokens[0].type == "fence" + name, *first_line = tokens[0].info.split(maxsplit=1) + if name == "{note}": + klass = Note + elif name == "{admonition}": + klass = Admonition + else: + raise AssertionError(f"Unknown directive: {name}") + arguments, options, body_lines, content_offset = parse_directive_text( + klass, first_line[0] if first_line else "", tokens[0].content + ) + outcome = yaml.safe_dump( + { + "arguments": arguments, + "options": options, + "body": body_lines, + "content_offset": content_offset, + }, + sort_keys=True, + ) + file_params.assert_expected(outcome, rstrip_lines=True) + + +@pytest.mark.parametrize( + "descript,klass,arguments,content", [("no content", Rubric, "", "a")] +) +def test_parsing_errors(descript, klass, arguments, content): + with pytest.raises(DirectiveParsingError): + parse_directive_text(klass, arguments, content) |