import pytest from errorfix.formatters import TextFormatter, JSONFormatter, StructuredFormatter from errorfix.patterns.matcher import MatchResult from errorfix.rules.rule import Rule def create_match_result(rule_id="test001", rule_name="Test Rule"): rule = Rule( id=rule_id, name=rule_name, pattern=r"TestError", fix="Apply this fix", description="Test error description", severity="error", language="python" ) return MatchResult( rule=rule, matched_text="TestError: something went wrong", groups={"1": "something"}, start_pos=0, end_pos=30, ) class TestTextFormatter: def test_format_single_match(self): formatter = TextFormatter(use_colors=False) match = create_match_result() result = formatter.format([match], "TestError: something went wrong") assert "Test Rule" in result assert "Apply this fix" in result assert "Test error description" in result def test_format_no_matches(self): formatter = TextFormatter(use_colors=False) result = formatter.format([], "Some unknown error") assert "No matching rules found" in result def test_format_multiple_matches(self): formatter = TextFormatter(use_colors=False) matches = [create_match_result("r1", "Rule 1"), create_match_result("r2", "Rule 2")] result = formatter.format(matches, "Test error") assert "Fix #1" in result assert "Fix #2" in result assert "Rule 1" in result assert "Rule 2" in result def test_colored_output(self): formatter = TextFormatter(use_colors=True) match = create_match_result() result = formatter.format([match], "Test error") assert "\033[" in result class TestJSONFormatter: def test_format_single_match(self): formatter = JSONFormatter(pretty=True) match = create_match_result() result = formatter.format([match], "Test error") import json data = json.loads(result) assert data["match_count"] == 1 assert data["matches"][0]["rule"]["id"] == "test001" def test_format_empty(self): formatter = JSONFormatter(pretty=True) result = formatter.format([], "Test error") import json data = json.loads(result) assert data["match_count"] == 0 assert len(data["matches"]) == 0 def test_pretty_false(self): formatter = JSONFormatter(pretty=False) match = create_match_result() result = formatter.format([match], "Test error") import json data = json.loads(result) assert data["match_count"] == 1 class TestStructuredFormatter: def test_format_returns_dict_string(self): formatter = StructuredFormatter() match = create_match_result() result = formatter.format([match], "Test error") assert "rule_id" in result or "dict" in result.lower()