generated from coulomb/repo-seed
Parse Markdown files into a structured Python object
This commit is contained in:
89
tests/test_parse_contract.py
Normal file
89
tests/test_parse_contract.py
Normal file
@@ -0,0 +1,89 @@
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from click.testing import CliRunner
|
||||
|
||||
from markitect_tool import MarkdownParseError, parse_markdown, parse_markdown_file
|
||||
from markitect_tool.cli import main
|
||||
|
||||
|
||||
def test_parse_markdown_preserves_headings_and_paragraphs():
|
||||
document = parse_markdown("# Heading\n\nThis is a paragraph.")
|
||||
|
||||
assert document.frontmatter == {}
|
||||
assert document.headings[0].level == 1
|
||||
assert document.headings[0].text == "Heading"
|
||||
assert [block.type for block in document.blocks] == ["heading", "paragraph"]
|
||||
assert document.sections[0].heading.text == "Heading"
|
||||
assert document.sections[0].blocks[0].text == "This is a paragraph."
|
||||
assert document.tokens[0]["type"] == "heading_open"
|
||||
|
||||
|
||||
def test_parse_markdown_extracts_yaml_frontmatter():
|
||||
markdown = """---
|
||||
title: YAML Frontmatter Test Document
|
||||
tags:
|
||||
- yaml
|
||||
- frontmatter
|
||||
published: true
|
||||
nested:
|
||||
priority: high
|
||||
---
|
||||
|
||||
# YAML Frontmatter Test Document
|
||||
|
||||
Body text.
|
||||
"""
|
||||
|
||||
document = parse_markdown(markdown)
|
||||
|
||||
assert document.frontmatter["title"] == "YAML Frontmatter Test Document"
|
||||
assert document.frontmatter["tags"] == ["yaml", "frontmatter"]
|
||||
assert document.frontmatter["published"] is True
|
||||
assert document.frontmatter["nested"]["priority"] == "high"
|
||||
assert document.headings[0].line == 11
|
||||
assert document.body.lstrip().startswith("# YAML Frontmatter Test Document")
|
||||
|
||||
|
||||
def test_parse_markdown_without_frontmatter_is_graceful():
|
||||
document = parse_markdown("# Document Without Frontmatter\n\nText.")
|
||||
|
||||
assert document.frontmatter == {}
|
||||
assert document.headings[0].text == "Document Without Frontmatter"
|
||||
|
||||
|
||||
def test_parse_markdown_rejects_non_mapping_frontmatter():
|
||||
with pytest.raises(MarkdownParseError, match="Frontmatter must be a mapping"):
|
||||
parse_markdown("---\n- nope\n---\n\n# Bad")
|
||||
|
||||
|
||||
def test_parse_markdown_file_records_source_path(tmp_path: Path):
|
||||
source = tmp_path / "doc.md"
|
||||
source.write_text("# Test Document\n\nBody", encoding="utf-8")
|
||||
|
||||
document = parse_markdown_file(source)
|
||||
|
||||
assert document.source_path == str(source)
|
||||
assert document.headings[0].text == "Test Document"
|
||||
|
||||
|
||||
def test_mkt_parse_outputs_json(tmp_path: Path):
|
||||
source = tmp_path / "doc.md"
|
||||
source.write_text("# Test Document\n\nBody", encoding="utf-8")
|
||||
|
||||
result = CliRunner().invoke(main, ["parse", str(source)])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert '"headings"' in result.output
|
||||
assert "Test Document" in result.output
|
||||
|
||||
|
||||
def test_mkt_parse_outputs_tree(tmp_path: Path):
|
||||
source = tmp_path / "doc.md"
|
||||
source.write_text("# One\n\n## Two\n", encoding="utf-8")
|
||||
|
||||
result = CliRunner().invoke(main, ["parse", str(source), "--format", "tree"])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert "# One" in result.output
|
||||
assert "## Two" in result.output
|
||||
Reference in New Issue
Block a user