1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
import contextlib
import io
from dataclasses import dataclass, field, fields
from textwrap import dedent
from typing import Literal
from myst_parser.mdit_to_docutils.base import make_document
from myst_parser.parsers.docutils_ import (
Parser,
attr_to_optparse_option,
cli_html,
cli_html5,
cli_html5_demo,
cli_latex,
cli_pseudoxml,
cli_xml,
to_html5_demo,
)
def test_attr_to_optparse_option():
@dataclass
class Config:
name: Literal["a"] = field(default="default")
output = attr_to_optparse_option(fields(Config)[0], "default")
assert len(output) == 3
def test_parser():
"""Test calling `Parser.parse` directly."""
parser = Parser()
document = make_document(parser_cls=Parser)
parser.parse("something", document)
assert (
document.pformat().strip()
== '<document source="notset">\n <paragraph>\n something'
)
def test_cli_html(monkeypatch, capsys):
monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"text")))
cli_html([])
captured = capsys.readouterr()
assert not captured.err
assert "text" in captured.out
def test_cli_html5(monkeypatch, capsys):
monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"text")))
cli_html5([])
captured = capsys.readouterr()
assert not captured.err
assert "text" in captured.out
def test_cli_html5_demo(monkeypatch, capsys):
monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"text")))
cli_html5_demo([])
captured = capsys.readouterr()
assert not captured.err
assert "text" in captured.out
def test_to_html5_demo():
assert to_html5_demo("text").strip() == "<p>text</p>"
def test_cli_latex(monkeypatch, capsys):
monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"text")))
cli_latex([])
captured = capsys.readouterr()
assert not captured.err
assert "text" in captured.out
def test_cli_xml(monkeypatch, capsys):
monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"text")))
cli_xml([])
captured = capsys.readouterr()
assert not captured.err
assert "text" in captured.out
def test_cli_pseudoxml(monkeypatch, capsys):
monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"text")))
cli_pseudoxml([])
captured = capsys.readouterr()
assert not captured.err
assert "text" in captured.out
def test_help_text():
"""Test retrieving settings help text."""
from docutils.core import Publisher
stream = io.StringIO()
pub = Publisher(parser=Parser())
with contextlib.redirect_stdout(stream):
try:
pub.process_command_line(["--help"])
except SystemExit as exc:
assert not exc.code
assert "MyST options" in stream.getvalue()
def test_include_from_rst(tmp_path):
"""Test including a MyST file from within an RST file."""
from docutils.parsers.rst import Parser as RSTParser
include_path = tmp_path.joinpath("include.md")
include_path.write_text("# Title")
parser = RSTParser()
document = make_document(parser_cls=RSTParser)
parser.parse(
f".. include:: {include_path}\n :parser: myst_parser.docutils_", document
)
assert (
document.pformat().strip()
== dedent(
"""\
<document source="notset">
<section ids="title" names="title">
<title>
Title
"""
).strip()
)
|