| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- from __future__ import annotations
-
- from pathlib import Path
-
- from src.markdown.generator import (
- _validate_dollar_signs,
- _validate_latex_braces,
- _validate_left_right,
- generate_markdown,
- validate_markdown,
- )
- from src.ocr.paddle_engine import ParsedBlock
-
-
- class TestValidateBraces:
- def test_balanced(self) -> None:
- assert _validate_latex_braces(r"a{b}c") == []
-
- def test_unbalanced_open(self) -> None:
- warnings = _validate_latex_braces(r"a{b")
- assert len(warnings) > 0
-
- def test_unbalanced_close(self) -> None:
- warnings = _validate_latex_braces(r"a}b")
- assert len(warnings) > 0
-
- def test_nested_balanced(self) -> None:
- assert _validate_latex_braces(r"{a{b}c}") == []
-
- def test_complex_formula(self) -> None:
- assert _validate_latex_braces(r"\frac{a}{b}") == []
-
-
- class TestValidateDollar:
- def test_balanced_inline(self) -> None:
- assert _validate_dollar_signs("$a + b$") == []
-
- def test_balanced_display(self) -> None:
- assert _validate_dollar_signs(r"$$a + b$$") == []
-
- def test_unbalanced_inline(self) -> None:
- warnings = _validate_dollar_signs("$a + b")
- assert len(warnings) > 0
-
- def test_balanced_multiple(self) -> None:
- assert _validate_dollar_signs("$x$ and $y$") == []
-
- def test_unbalanced_multiple(self) -> None:
- warnings = _validate_dollar_signs("$x and $y$")
- assert len(warnings) > 0
-
-
- class TestValidateLeftRight:
- def test_balanced(self) -> None:
- assert _validate_left_right(r"\left( x \right)") == []
-
- def test_unbalanced(self) -> None:
- warnings = _validate_left_right(r"\left( x")
- assert len(warnings) > 0
-
-
- class TestValidateMarkdown:
- def test_valid(self) -> None:
- assert validate_markdown("text\n\n$$\na + b\n$$\n") == []
-
- def test_unbalanced_dollar(self) -> None:
- warnings = validate_markdown("$$\n$a\n$$\n")
- assert len(warnings) > 0
-
- def test_unbalanced_brace_in_formula(self) -> None:
- warnings = validate_markdown("$$\na{\nb\n$$\n")
- assert len(warnings) > 0
-
-
- class TestGenerateMarkdown:
- def test_text_block(self) -> None:
- import tempfile
-
- blocks = [ParsedBlock(label="text", content="Hello world", bbox=(0, 0, 100, 20))]
- with tempfile.TemporaryDirectory() as tmp:
- p = generate_markdown(blocks, Path(tmp) / "output.md")
- assert p.exists()
- content = p.read_text()
- assert "Hello world" in content
-
- def test_formula_block(self) -> None:
- import tempfile
-
- blocks = [ParsedBlock(label="formula", content=r"a + b = c", bbox=(0, 0, 100, 20))]
- with tempfile.TemporaryDirectory() as tmp:
- p = generate_markdown(blocks, Path(tmp) / "output.md")
- content = p.read_text()
- assert "$$\n" in content
- assert "a + b = c" in content
-
- def test_title_block(self) -> None:
- import tempfile
-
- blocks = [ParsedBlock(label="paragraph_title", content="Section 1", bbox=(0, 0, 100, 20))]
- with tempfile.TemporaryDirectory() as tmp:
- p = generate_markdown(blocks, Path(tmp) / "output.md")
- content = p.read_text()
- assert "## Section 1" in content
-
- def test_no_escaped_latex(self) -> None:
- import tempfile
-
- blocks = [ParsedBlock(label="text", content=r"alpha \sim", bbox=(0, 0, 100, 20))]
- with tempfile.TemporaryDirectory() as tmp:
- p = generate_markdown(blocks, Path(tmp) / "output.md")
- content = p.read_text()
- assert r"\textbackslash" not in content
- assert r"alpha" in content
|