generated from coulomb/repo-seed
extension for ref resolve, explode, implode, weave, tangle
This commit is contained in:
105
tests/test_processor_registry.py
Normal file
105
tests/test_processor_registry.py
Normal file
@@ -0,0 +1,105 @@
|
||||
from pathlib import Path
|
||||
|
||||
from click.testing import CliRunner
|
||||
|
||||
from markitect_tool.cli import main
|
||||
from markitect_tool.core import parse_markdown
|
||||
from markitect_tool.processor import (
|
||||
ProcessorContext,
|
||||
default_processor_registry,
|
||||
discover_fenced_processors,
|
||||
run_fenced_processors,
|
||||
)
|
||||
from markitect_tool.reference import load_namespaces
|
||||
|
||||
|
||||
def test_discover_fenced_processors_from_language_prefix():
|
||||
markdown = """# Doc
|
||||
|
||||
```mkt-uppercase {#shout}
|
||||
hello
|
||||
```
|
||||
"""
|
||||
|
||||
blocks = discover_fenced_processors(markdown, source_path="doc.md")
|
||||
|
||||
assert len(blocks) == 1
|
||||
assert blocks[0].processor == "uppercase"
|
||||
assert blocks[0].unit_id == "shout"
|
||||
assert blocks[0].line_start == 3
|
||||
|
||||
|
||||
def test_default_registry_runs_uppercase_processor():
|
||||
markdown = """```mkt-uppercase {#shout}
|
||||
hello
|
||||
```
|
||||
"""
|
||||
context = ProcessorContext()
|
||||
|
||||
run = run_fenced_processors(markdown, context=context)
|
||||
|
||||
assert run.valid
|
||||
assert run.results[0].content == "HELLO\n"
|
||||
assert run.results[0].provenance[0].operation == "processor.uppercase"
|
||||
|
||||
|
||||
def test_include_processor_uses_reference_resolver(tmp_path: Path):
|
||||
source = tmp_path / "doc.md"
|
||||
partial = tmp_path / "partial.md"
|
||||
source.write_text(
|
||||
"""---
|
||||
namespaces:
|
||||
local: .
|
||||
---
|
||||
|
||||
```mkt-include {#intro ref="local:partial.md#summary"}
|
||||
```
|
||||
""",
|
||||
encoding="utf-8",
|
||||
)
|
||||
partial.write_text("# Partial\n\n## Summary\n\nIncluded summary.\n", encoding="utf-8")
|
||||
document = parse_markdown(source.read_text(encoding="utf-8"), source_path=str(source))
|
||||
context = ProcessorContext(
|
||||
root=tmp_path,
|
||||
current_path=source,
|
||||
namespaces=load_namespaces(document.frontmatter),
|
||||
)
|
||||
|
||||
run = run_fenced_processors(source.read_text(encoding="utf-8"), context=context)
|
||||
|
||||
assert run.valid
|
||||
assert run.results[0].dependencies == [str(partial.resolve())]
|
||||
assert "Included summary" in run.results[0].content
|
||||
|
||||
|
||||
def test_unknown_processor_returns_diagnostic():
|
||||
markdown = """```mkt-nope {#x}
|
||||
content
|
||||
```
|
||||
"""
|
||||
registry = default_processor_registry()
|
||||
|
||||
run = run_fenced_processors(markdown, context=ProcessorContext(), registry=registry)
|
||||
|
||||
assert not run.valid
|
||||
assert run.results[0].diagnostics[0].code == "processor.unknown"
|
||||
|
||||
|
||||
def test_mkt_process_outputs_text(tmp_path: Path):
|
||||
source = tmp_path / "doc.md"
|
||||
source.write_text(
|
||||
"""# Doc
|
||||
|
||||
```mkt-uppercase {#shout}
|
||||
hello
|
||||
```
|
||||
""",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
result = CliRunner().invoke(main, ["process", str(source), "--root", str(tmp_path)])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert "valid" in result.output
|
||||
assert "uppercase shout" in result.output
|
||||
assert "HELLO" in result.output
|
||||
Reference in New Issue
Block a user