md2txt/tests/test_gemini_renderer.py

95 lines
4.2 KiB
Python

"""Targeted regression tests for the three gaps closed in the gemini
renderer: dropped tables, unconditional inline [n] markers, and hard
wrapping. This project has no existing test harness, so these are plain
assertions rather than the golden-file setup a larger suite might use.
"""
from __future__ import annotations
from md2txt import convert_markdown
from md2txt.models import FrontMatter
def render(text: str, *, width: int = 80, **frontmatter_kwargs) -> str:
lines = text.splitlines(keepends=True)
frontmatter = FrontMatter(**frontmatter_kwargs)
rendered = convert_markdown(
lines, width=width, frontmatter=frontmatter, renderer_name="gemini"
)
return "\n".join(rendered)
class TestTableRendering:
def test_table_is_no_longer_dropped(self):
markup = render("| A | B |\n| --- | --- |\n| 1 | 2 |\n")
assert "A" in markup and "B" in markup and "1" in markup and "2" in markup
def test_table_is_wrapped_in_a_preformat_block(self):
markup = render("| A | B |\n| --- | --- |\n| 1 | 2 |\n")
lines = markup.splitlines()
fence_indices = [i for i, line in enumerate(lines) if line.startswith("```")]
assert len(fence_indices) == 2, "expected an opening and closing ``` fence"
def test_columns_are_padded_to_equal_width(self):
markup = render("| A | Long Header |\n| --- | --- |\n| 1 | 2 |\n")
lines = [l for l in markup.splitlines() if l.strip() and not l.startswith("```")]
# Every row's first column should start at the same character
# offset -- the visible sign that padding is applied consistently.
first_col_starts = {line.index(line.split()[0]) for line in lines}
assert len(lines) >= 2
assert first_col_starts == {0}
class TestLinkMarkers:
SOURCE = "See the [spec](/spec.md) and [FAQ](/faq.md).\n"
def test_default_keeps_existing_marker_behavior(self):
markup = render(self.SOURCE, links_per_block=True)
assert "spec [1]" in markup
assert "=> /spec.md [1] spec" in markup
def test_link_markers_false_drops_inline_numbers(self):
markup = render(self.SOURCE, links_per_block=True, link_markers=False)
assert "See the spec and FAQ." in markup
assert "[1]" not in markup and "[2]" not in markup
def test_link_markers_false_still_emits_link_lines(self):
markup = render(self.SOURCE, links_per_block=True, link_markers=False)
assert "=> /spec.md spec" in markup
assert "=> /faq.md FAQ" in markup
class TestWidthZeroDisablesWrapping:
def test_long_paragraph_stays_on_one_line(self):
long_text = "word " * 40 # far past 80 columns
markup = render(long_text.strip() + "\n", width=0)
body_lines = [l for l in markup.splitlines() if l.strip()]
assert len(body_lines) == 1
assert body_lines[0] == long_text.strip()
def test_positive_width_still_wraps(self):
long_text = "word " * 40
markup = render(long_text.strip() + "\n", width=20)
body_lines = [l for l in markup.splitlines() if l.strip()]
assert len(body_lines) > 1
assert all(len(l) <= 20 for l in body_lines)
class TestWidthZeroDisablesFiglet:
def test_default_h1_font_does_not_crash_at_width_zero(self):
# h1_font defaults to a real pyfiglet font ("small"); pyfiglet lays
# characters out against the available width, so width=0 (used to
# mean "don't wrap, let the client wrap") used to raise
# pyfiglet.CharNotPrinted instead of degrading to a plain heading.
markup = render("# Title\n\nBody.\n", width=0)
assert "# Title" in markup
def test_explicit_figlet_font_does_not_crash_at_width_zero(self):
markup = render("# Title\n", width=0, h1_font="standard")
assert "# Title" in markup
def test_heading_levels_four_to_six_do_not_crash_at_width_zero(self):
# These levels have no FrontMatter field at all; the renderer's
# hardcoded getattr(..., "standard") fallback made them figlet
# candidates regardless of any frontmatter setting.
markup = render("#### Four\n\n##### Five\n\n###### Six\n", width=0)
assert "Four" in markup and "Five" in markup and "Six" in markup