"""Unit tests for formatters."""
import pytest
from src.formatters.markdown import MarkdownFormatter
from src.models import CodeElement, CodeStructure, Parameter, ParseError
class TestMarkdownFormatter:
"""Test MarkdownFormatter class."""
def test_format_structure_empty(self) -> None:
"""Test formatting an empty structure."""
formatter = MarkdownFormatter()
structure = CodeStructure(file_path="test.py", language="python")
result = formatter.format_structure(structure)
assert "# Code Structure: `test.py`" in result
assert "**Language**: Python" in result
assert "**Total Classes**: 0" in result
assert "**Total Functions**: 0" in result
assert "**Max Nesting Depth**: 0" in result
def test_format_structure_with_class(self) -> None:
"""Test formatting a structure with a class."""
formatter = MarkdownFormatter()
structure = CodeStructure(file_path="test.py", language="python")
cls = CodeElement(
name="MyClass",
element_type="class",
start_line=10,
end_line=50,
nesting_level=0,
docstring="A test class.",
)
structure.add_class(cls)
result = formatter.format_structure(structure)
assert "## Classes" in result
assert "### `MyClass`" in result
assert "Lines 10-50" in result
assert "Nesting: 0" in result
assert "**Type**: Class" in result
assert "**Docstring**: A test class." in result
def test_format_structure_with_function(self) -> None:
"""Test formatting a structure with a function."""
formatter = MarkdownFormatter()
structure = CodeStructure(file_path="test.py", language="python")
func = CodeElement(
name="my_function",
element_type="function",
start_line=5,
end_line=15,
nesting_level=0,
parameters=[
Parameter(name="param1", type_annotation="str"),
Parameter(name="param2", type_annotation="int", default_value="0"),
],
return_type="bool",
docstring="A test function.",
)
structure.add_function(func)
result = formatter.format_structure(structure)
assert "## Functions" in result
assert "### `my_function`" in result
assert "Lines 5-15" in result
assert "Nesting: 0" in result
assert "**Type**: Function" in result
assert "**Parameters**: `param1: str, param2: int = 0`" in result
assert "**Return Type**: `bool`" in result
assert "**Docstring**: A test function." in result
def test_format_structure_with_nested_function(self) -> None:
"""Test formatting a structure with nested functions."""
formatter = MarkdownFormatter()
structure = CodeStructure(file_path="test.py", language="python")
outer_func = CodeElement(
name="outer_function",
element_type="function",
start_line=1,
end_line=20,
nesting_level=0,
)
structure.add_function(outer_func)
inner_func = CodeElement(
name="inner_function",
element_type="function",
start_line=5,
end_line=15,
nesting_level=1,
)
outer_func.add_child(inner_func)
result = formatter.format_structure(structure)
assert "### `outer_function`" in result
assert "### `inner_function`" in result
assert "**Nested Functions**:" in result
assert "Parent: `outer_function`" in result
def test_format_structure_with_error(self) -> None:
"""Test formatting a structure with parsing errors."""
formatter = MarkdownFormatter()
structure = CodeStructure(file_path="test.py", language="python")
error = ParseError(
line=42,
message="Syntax error",
context="return self.process(item",
)
structure.add_error(error)
result = formatter.format_structure(structure)
assert "## Parse Errors" in result
assert "⚠️ **Error at Line 42**" in result
assert "Syntax error" in result
assert "return self.process(item" in result
def test_format_nested_class(self) -> None:
"""Test formatting a nested class."""
formatter = MarkdownFormatter()
structure = CodeStructure(file_path="test.py", language="python")
outer_cls = CodeElement(
name="OuterClass",
element_type="class",
start_line=1,
end_line=30,
nesting_level=0,
)
structure.add_class(outer_cls)
inner_cls = CodeElement(
name="InnerClass",
element_type="class",
start_line=10,
end_line=25,
nesting_level=1,
)
outer_cls.add_child(inner_cls)
result = formatter.format_structure(structure)
assert "### `OuterClass`" in result
assert "### `InnerClass`" in result
assert "**Nested Classes**:" in result