80 lines
2.5 KiB
Python
80 lines
2.5 KiB
Python
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from mdpolish import Pipeline, RunStatus
|
|
from mdpolish.modifiers import html_table_layout
|
|
|
|
TABLE = '<table class="x"><tr><td colspan="2">A</td></tr><tr><td>B</td><td>C</td></tr></table>'
|
|
|
|
|
|
def transform(markdown: str): # type: ignore[no-untyped-def]
|
|
return Pipeline([html_table_layout()]).transform(markdown)
|
|
|
|
|
|
def test_expands_rows_without_changing_tags_attributes_or_cells() -> None:
|
|
result = transform(f"before\n{TABLE}\nafter")
|
|
|
|
assert result.status is RunStatus.SUCCESS
|
|
assert result.output_markdown == (
|
|
"before\n<table class=\"x\">\n"
|
|
" <tr><td colspan=\"2\">A</td></tr>\n"
|
|
" <tr><td>B</td><td>C</td></tr>\n"
|
|
"</table>\nafter"
|
|
)
|
|
assert len(result.changes) == 1
|
|
assert result.modifiers[0].version == "1.0.1"
|
|
|
|
|
|
@pytest.mark.parametrize("line_ending", ["\n", "\r\n", "\r"])
|
|
def test_uses_the_documents_single_line_ending_style(line_ending: str) -> None:
|
|
markdown = f"before{line_ending}{TABLE}{line_ending}after"
|
|
result = transform(markdown)
|
|
assert result.output_markdown is not None
|
|
assert f"<table class=\"x\">{line_ending} <tr>" in result.output_markdown
|
|
|
|
|
|
def test_table_only_document_uses_lf() -> None:
|
|
assert transform(TABLE).output_markdown == (
|
|
'<table class="x">\n'
|
|
' <tr><td colspan="2">A</td></tr>\n'
|
|
" <tr><td>B</td><td>C</td></tr>\n"
|
|
"</table>"
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"markdown",
|
|
[
|
|
"before\n<table><tr><td>A</td></tr>\r\nafter",
|
|
"<table>\n <tr><td>A</td></tr>\n</table>",
|
|
"<table><tr><td>A</tr></table>",
|
|
"<table><tbody><tr><td>A</td></tr></tbody></table>",
|
|
"<table><tr><td><em>A</em></td></tr></table>",
|
|
],
|
|
)
|
|
def test_mixed_multiline_or_non_strict_tables_are_preserved(markdown: str) -> None:
|
|
assert transform(markdown).output_markdown == markdown
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"markdown",
|
|
[
|
|
"<table>broken<table><tr><td>A</td></tr></table>",
|
|
"<table broken <table><tr><td>A</td></tr></table>",
|
|
],
|
|
)
|
|
def test_damaged_outer_table_does_not_expose_complete_inner_table(markdown: str) -> None:
|
|
result = transform(markdown)
|
|
|
|
assert result.status is RunStatus.SUCCESS
|
|
assert result.output_markdown == markdown
|
|
assert result.changes == ()
|
|
|
|
|
|
def test_successful_output_is_idempotent() -> None:
|
|
pipeline = Pipeline([html_table_layout()])
|
|
first = pipeline.transform(TABLE)
|
|
assert first.output_markdown is not None
|
|
assert pipeline.transform(first.output_markdown).changes == ()
|