Ejemplo n.º 1
0
def test_style(fixture_file, options):
    """Test Markdown renderer renders expected style."""
    cases = read_fixture_file(Path(__file__).parent / "data" / fixture_file)
    for case in cases:
        line, title, text, expected = case
        md_new = mdformat.text(text, options=options)
        if md_new != expected:
            print(md_new)
        assert md_new == expected
Ejemplo n.º 2
0
from myst_parser.main import MdParserConfig, to_docutils
from myst_parser.sphinx_renderer import SphinxRenderer, mock_sphinx_env

FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures")


def test_minimal_sphinx():
    with mock_sphinx_env(conf={"author": "bob geldof"},
                         with_builder=True) as app:
        assert app.config["author"] == "bob geldof"


@pytest.mark.parametrize(
    "line,title,input,expected",
    read_fixture_file(FIXTURE_PATH.joinpath("syntax_elements.md")),
    ids=[
        f"{i[0]}-{i[1]}"
        for i in read_fixture_file(FIXTURE_PATH / "syntax_elements.md")
    ],
)
def test_syntax_elements(line, title, input, expected):
    document = to_docutils(input, in_sphinx_env=True)
    print(document.pformat())
    assert "\n".join([ll.rstrip()
                      for ll in document.pformat().splitlines()]) == "\n".join(
                          [ll.rstrip() for ll in expected.splitlines()])


@pytest.mark.parametrize(
    "line,title,input,expected",
Ejemplo n.º 3
0
FIXTURE_PATH = Path(__file__).parent


def test_plugin_parse(data_regression):
    md = MarkdownIt().use(amsmath_plugin)
    tokens = md.parse(
        dedent(
            """\
    a
    \\begin{equation}
    b=1
    c=2
    \\end{equation}
    d
    """
        )
    )
    data_regression.check([t.as_dict() for t in tokens])


@pytest.mark.parametrize(
    "line,title,input,expected",
    read_fixture_file(FIXTURE_PATH.joinpath("fixtures", "amsmath.md")),
)
def test_fixtures(line, title, input, expected):
    md = MarkdownIt("commonmark").use(amsmath_plugin)
    md.options["xhtmlOut"] = False
    text = md.render(input)
    print(text)
    assert text.rstrip() == expected.rstrip()
Ejemplo n.º 4
0
from pathlib import Path

import pytest
from markdown_it.utils import read_fixture_file

from myst_parser.parse_html import tokenize_html

FIXTURE_PATH = Path(__file__).parent


@pytest.mark.parametrize(
    "line,title,text,expected",
    read_fixture_file(FIXTURE_PATH / "html_ast.md"),
    ids=[f"{i[0]}-{i[1]}" for i in read_fixture_file(FIXTURE_PATH / "html_ast.md")],
)
def test_html_ast(line, title, text, expected):
    tokens = "\n".join(repr(t) for t in tokenize_html(text).walk(include_self=True))
    try:
        assert tokens.rstrip() == expected.rstrip()
    except AssertionError:
        print(tokens)
        raise


@pytest.mark.parametrize(
    "line,title,text,expected",
    read_fixture_file(FIXTURE_PATH / "html_round_trip.md"),
    ids=[
        f"{i[0]}-{i[1]}" for i in read_fixture_file(FIXTURE_PATH / "html_round_trip.md")
    ],
)
    }


def test_plugin_parse(data_regression):
    md = MarkdownIt().use(dollarmath_plugin)
    tokens = md.parse(
        dedent("""\
    $$
    a=1
    b=2
    $$ (abc)

    - ab $c=1$ d
    """))
    data_regression.check([t.as_dict() for t in tokens])


@pytest.mark.parametrize(
    "line,title,input,expected",
    read_fixture_file(FIXTURE_PATH.joinpath("dollar_math.md")),
)
def test_dollarmath_fixtures(line, title, input, expected):
    md = MarkdownIt("commonmark").use(dollarmath_plugin,
                                      allow_space=False,
                                      allow_digits=False,
                                      double_inline=True)
    md.options.xhtmlOut = False
    text = md.render(input)
    print(text)
    assert text.rstrip() == expected.rstrip()
Ejemplo n.º 6
0
from pathlib import Path

import pytest

from markdown_it import MarkdownIt
from markdown_it.utils import read_fixture_file

FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures")


@pytest.mark.parametrize("line,title,input,expected",
                         read_fixture_file(FIXTURE_PATH.joinpath("tables.md")))
def test_title(line, title, input, expected):
    md = MarkdownIt().enable("table")
    text = md.render(input)
    assert text.rstrip() == expected.rstrip()


@pytest.mark.parametrize(
    "line,title,input,expected",
    read_fixture_file(FIXTURE_PATH.joinpath("commonmark_extras.md")),
)
def test_commonmark_extras(line, title, input, expected):
    if line in [74, 88]:
        # TODO fix failing escaping tests
        # probably requires a fix of common.utils.stripEscape
        pytest.skip("escaping entities in link titles / fence.info")
    md = MarkdownIt("commonmark")
    md.options["langPrefix"] = ""
    text = md.render(input)
    if text.rstrip() != expected.rstrip():
    md = MarkdownIt().use(container_plugin, "name")
    tokens = md.parse(dedent("""\
    ::: name
    *content*
    :::
    """))
    data_regression.check([t.as_dict() for t in tokens])


def test_no_new_line_issue(data_regression):
    """Fixed an IndexError when no newline on final line."""
    md = MarkdownIt().use(container_plugin, "name")
    tokens = md.parse(dedent("""\
    ::: name
    *content*
    :::"""))
    data_regression.check([t.as_dict() for t in tokens])


FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures", "container.md")


@pytest.mark.parametrize("line,title,input,expected",
                         read_fixture_file(FIXTURE_PATH))
def test_all(line, title, input, expected):
    md = MarkdownIt("commonmark").use(container_plugin, "name")
    md.options["xhtmlOut"] = False
    text = md.render(input)
    print(text)
    assert text.rstrip() == expected.rstrip()
Ejemplo n.º 8
0
from pathlib import Path

import pytest

from markdown_it import MarkdownIt
from markdown_it.extensions.anchors import anchors_plugin
from markdown_it.utils import read_fixture_file

FIXTURE_PATH = Path(__file__).parent


@pytest.mark.parametrize(
    "line,title,input,expected",
    read_fixture_file(FIXTURE_PATH.joinpath("fixtures", "anchors.md")),
)
def test_fixtures(line, title, input, expected):
    md = MarkdownIt("commonmark").use(
        anchors_plugin,
        permalink="(permalink" in title,
        permalinkBefore="before)" in title,
    )
    text = md.render(input)
    assert text.rstrip() == expected.rstrip()
Ejemplo n.º 9
0
import os
from pathlib import Path

import pytest
from markdown_it.utils import read_fixture_file

from myst_parser.docutils_renderer import make_document
from myst_parser.main import to_docutils

FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures")


@pytest.mark.parametrize(
    "line,title,input,expected",
    read_fixture_file(FIXTURE_PATH.joinpath("mock_include.md")),
)
def test_render(line, title, input, expected, tmp_path):
    tmp_path.joinpath("other.md").write_text("a\nb\nc")
    tmp_path.joinpath("fmatter.md").write_text("---\na: 1\n---\nb")
    document = make_document(str(tmp_path / "test.md"))
    to_docutils(input, document=document, in_sphinx_env=True, srcdir=str(tmp_path))
    output = document.pformat().replace(str(tmp_path) + os.sep, "tmpdir" + "/").rstrip()
    print(output)
    assert output == expected.rstrip()


@pytest.mark.parametrize(
    "line,title,input,expected",
    read_fixture_file(FIXTURE_PATH.joinpath("mock_include_errors.md")),
)
def test_errors(line, title, input, expected, tmp_path):
Ejemplo n.º 10
0
from pathlib import Path

import pytest

from markdown_it import MarkdownIt
from markdown_it.utils import read_fixture_file

FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures")


@pytest.mark.parametrize(
    "line,title,input,expected",
    read_fixture_file(FIXTURE_PATH.joinpath("smartquotes.md")),
)
def test_smartquotes(line, title, input, expected):
    md = MarkdownIt().enable("replacements").enable("smartquotes")
    md.options["typographer"] = True
    text = md.render(input)
    assert text.rstrip() == expected.rstrip()


@pytest.mark.parametrize(
    "line,title,input,expected",
    read_fixture_file(FIXTURE_PATH.joinpath("typographer.md")),
)
def test_typographer(line, title, input, expected):
    md = MarkdownIt().enable("replacements")
    md.options["typographer"] = True
    text = md.render(input)
    assert text.rstrip() == expected.rstrip()
Ejemplo n.º 11
0
import nbformat
import pytest
import yaml

from markdown_it.utils import read_fixture_file
from myst_parser.docutils_renderer import make_document
from myst_parser.sphinx_renderer import mock_sphinx_env

from myst_nb.parser import nb_to_tokens, tokens_to_docutils


FIXTURE_PATH = Path(__file__).parent.joinpath("nb_fixtures")


@pytest.mark.parametrize(
    "line,title,input,expected", read_fixture_file(FIXTURE_PATH.joinpath("basic.txt"))
)
def test_render(line, title, input, expected):
    dct = yaml.safe_load(input)
    dct.setdefault("metadata", {})
    ntbk = nbformat.from_dict(dct)
    md, env, tokens = nb_to_tokens(ntbk)
    document = make_document()
    with mock_sphinx_env(document=document):
        tokens_to_docutils(md, env, tokens, document)
    output = document.pformat().rstrip()
    if output != expected.rstrip():
        print(output)
    assert output == expected.rstrip()

Ejemplo n.º 12
0
from pathlib import Path

import pytest

from markdown_it.utils import read_fixture_file
from myst_parser.docutils_renderer import make_document
from myst_parser.main import to_docutils, MdParserConfig

FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures")


@pytest.mark.parametrize(
    "line,title,input,expected",
    read_fixture_file(FIXTURE_PATH.joinpath("reporter_warnings.md")),
)
def test_basic(line, title, input, expected):
    document = make_document("source/path")
    messages = []

    def observer(msg_node):
        if msg_node["level"] > 1:
            messages.append(msg_node.astext())

    document.reporter.attach_observer(observer)
    to_docutils(input, MdParserConfig(renderer="docutils"), document=document)
    assert "\n".join(messages).rstrip() == expected.rstrip()
Ejemplo n.º 13
0
FIXTURE_PATH = Path(__file__).parent


def test_plugin_parse(data_regression):
    md = MarkdownIt().use(amsmath_plugin)
    tokens = md.parse(
        dedent(
            """\
    a
    \\begin{equation}
    b=1
    c=2
    \\end{equation}
    d
    """
        )
    )
    data_regression.check([t.as_dict() for t in tokens])


@pytest.mark.parametrize(
    "line,title,input,expected", read_fixture_file(FIXTURE_PATH.joinpath("fixtures.md"))
)
def test_fixtures(line, title, input, expected):
    md = MarkdownIt("commonmark").use(amsmath_plugin)
    md.options["xhtmlOut"] = False
    text = md.render(input)
    print(text)
    assert text.rstrip() == expected.rstrip()
Ejemplo n.º 14
0
from pathlib import Path

from markdown_it.utils import read_fixture_file
import mdformat
import mdformat._cli
import pytest

DEFAULT_STYLE_CASES = read_fixture_file(
    Path(__file__).parent / "data" / "default_style.md")
WRAP_WIDTH_50_CASES = read_fixture_file(
    Path(__file__).parent / "data" / "wrap_width_50.md")


@pytest.mark.parametrize(
    "line,title,text,expected",
    DEFAULT_STYLE_CASES,
    ids=[f[1] for f in DEFAULT_STYLE_CASES],
)
def test_default_style__api(line, title, text, expected):
    """Test fixtures in tests/data/default_style.md."""
    md_new = mdformat.text(text, extensions={"gfm"})
    if md_new != expected:
        print("Formatted (unexpected) Markdown below:")
        print(md_new)
    assert md_new == expected


@pytest.mark.parametrize(
    "line,title,text,expected",
    DEFAULT_STYLE_CASES,
    ids=[f[1] for f in DEFAULT_STYLE_CASES],
Ejemplo n.º 15
0
from markdown_it.utils import read_fixture_file
from myst_parser.main import to_docutils
from myst_parser.sphinx_renderer import mock_sphinx_env

FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures")


def test_minimal_sphinx():
    with mock_sphinx_env(conf={"author": "bob geldof"}) as app:
        assert app.config["author"] == "bob geldof"


@pytest.mark.parametrize(
    "line,title,input,expected",
    read_fixture_file(FIXTURE_PATH.joinpath("syntax_elements.md")),
)
def test_syntax_elements(line, title, input, expected):
    document = to_docutils(input, in_sphinx_env=True)
    print(document.pformat())
    assert "\n".join([l.rstrip()
                      for l in document.pformat().splitlines()]) == "\n".join(
                          [l.rstrip() for l in expected.splitlines()])


@pytest.mark.parametrize("line,title,input,expected",
                         read_fixture_file(FIXTURE_PATH.joinpath("tables.md")))
def test_tables(line, title, input, expected):
    document = to_docutils(input, in_sphinx_env=True)
    print(document.pformat())
    assert "\n".join([l.rstrip()
Ejemplo n.º 16
0
from markdown_it.utils import read_fixture_file
from myst_parser.main import to_docutils, MdParserConfig
from myst_parser.sphinx_renderer import mock_sphinx_env, SphinxRenderer

FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures")


def test_minimal_sphinx():
    with mock_sphinx_env(conf={"author": "bob geldof"}, with_builder=True) as app:
        assert app.config["author"] == "bob geldof"


@pytest.mark.parametrize(
    "line,title,input,expected",
    read_fixture_file(FIXTURE_PATH.joinpath("syntax_elements.md")),
)
def test_syntax_elements(line, title, input, expected):
    document = to_docutils(input, in_sphinx_env=True)
    print(document.pformat())
    assert "\n".join(
        [ll.rstrip() for ll in document.pformat().splitlines()]
    ) == "\n".join([ll.rstrip() for ll in expected.splitlines()])


@pytest.mark.parametrize(
    "line,title,input,expected", read_fixture_file(FIXTURE_PATH.joinpath("tables.md"))
)
def test_tables(line, title, input, expected):
    document = to_docutils(input, in_sphinx_env=True)
    print(document.pformat())
Ejemplo n.º 17
0
import nbformat
import pytest
import yaml
from markdown_it.utils import read_fixture_file
from myst_parser.docutils_renderer import make_document
from myst_parser.main import MdParserConfig
from myst_parser.sphinx_renderer import mock_sphinx_env

from myst_nb.parser import nb_to_tokens, tokens_to_docutils

FIXTURE_PATH = Path(__file__).parent.joinpath("nb_fixtures")


@pytest.mark.parametrize("line,title,input,expected",
                         read_fixture_file(FIXTURE_PATH.joinpath("basic.txt")))
def test_render(line, title, input, expected):
    dct = yaml.safe_load(input)
    dct.setdefault("metadata", {})
    ntbk = nbformat.from_dict(dct)
    md, env, tokens = nb_to_tokens(ntbk, MdParserConfig(), "default")
    document = make_document()
    with mock_sphinx_env(document=document):
        tokens_to_docutils(md, env, tokens, document)
    output = document.pformat().rstrip()
    if output != expected.rstrip():
        print(output)
    assert output == expected.rstrip()


@pytest.mark.parametrize(
Ejemplo n.º 18
0
from markdown_it import MarkdownIt
from markdown_it.utils import read_fixture_file
from markdown_it.extensions.tasklists import tasklists_plugin

FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures", "tasklists.md")


def test_plugin_parse(data_regression):
    md = MarkdownIt().use(tasklists_plugin)
    tokens = md.parse(
        dedent(
            """\
    * [ ] Task incomplete
    * [x] Task complete
      * [ ] Indented task incomplete
      * [x] Indented task complete
    """
        )
    )
    data_regression.check([t.as_dict() for t in tokens])


@pytest.mark.parametrize("line,title,input,expected", read_fixture_file(FIXTURE_PATH))
def test_all(line, title, input, expected):
    md = MarkdownIt("commonmark").use(tasklists_plugin)
    md.options["xhtmlOut"] = False
    text = md.render(input)
    print(text)
    assert text.rstrip() == expected.rstrip()
Ejemplo n.º 19
0
from pathlib import Path

from markdown_it.utils import read_fixture_file
import mdformat
import pytest

TEST_CASES = read_fixture_file(Path(__file__).parent / "data" / "fixtures.md")


@pytest.mark.parametrize("line,title,text,expected",
                         TEST_CASES,
                         ids=[f[1] for f in TEST_CASES])
def test_fixtures(line, title, text, expected):
    """Test fixtures in tests/data/fixtures.md."""
    if "NIGHTLY" in title:
        pytest.skip("nightly test not supported on stable")
    md_new = mdformat.text(text, codeformatters={"rust"})
    if md_new != expected:
        print("Formatted (unexpected) Markdown below:")
        print(md_new)
    assert md_new == expected


def test_rustfmt_error(capfd):
    """Test that any prints by rustfmt go to devnull."""
    unformatted_md = """~~~rust
blaalbal.ablaa
~~~
"""
    formatted_md = """```rust
blaalbal.ablaa
Ejemplo n.º 20
0
    md = MarkdownIt().use(texmath_plugin)
    tokens = md.parse(
        dedent("""\
    $$
    a=1
    b=2
    $$ (abc)

    - ab $c=1$ d
    """))
    data_regression.check([t.as_dict() for t in tokens])


@pytest.mark.parametrize(
    "line,title,input,expected",
    read_fixture_file(FIXTURE_PATH.joinpath("texmath_dollar.md")),
)
def test_dollar_fixtures(line, title, input, expected):
    md = MarkdownIt("commonmark").use(texmath_plugin)
    md.options["xhtmlOut"] = False
    text = md.render(input)
    print(text)
    assert text.rstrip() == expected.rstrip()


@pytest.mark.parametrize(
    "line,title,input,expected",
    read_fixture_file(FIXTURE_PATH.joinpath("texmath_bracket.md")),
)
def test_bracket_fixtures(line, title, input, expected):
    md = MarkdownIt("commonmark").use(texmath_plugin, delimiters="brackets")
Ejemplo n.º 21
0
        node += nodes.Text(content)
        return [node]

    return Mock(
        config={"myst_extensions": ["html_image", "html_admonition"]},
        document={"source": "source"},
        reporter=Mock(
            warning=Mock(return_value=nodes.system_message("warning")),
            error=Mock(return_value=nodes.system_message("error")),
        ),
        run_directive=_run_directive,
    )


@pytest.mark.parametrize(
    "line,title,text,expected",
    read_fixture_file(FIXTURE_PATH / "html_to_nodes.md"),
    ids=[
        f"{i[0]}-{i[1]}"
        for i in read_fixture_file(FIXTURE_PATH / "html_to_nodes.md")
    ],
)
def test_html_to_nodes(line, title, text, expected, mock_renderer):
    output = nodes.container()
    output += html_to_nodes(text, line_number=0, renderer=mock_renderer)
    try:
        assert output.pformat().rstrip() == expected.rstrip()
    except AssertionError:
        print(output.pformat())
        raise