From 4914e7ac8702140e45b24c50236b3284adda73d0 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sun, 1 Feb 2026 03:56:34 +0000 Subject: [PATCH] Initial upload: ErrorFix CLI with rule engine and pattern matching --- tests/unit/test_formatters.py | 95 +++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 tests/unit/test_formatters.py diff --git a/tests/unit/test_formatters.py b/tests/unit/test_formatters.py new file mode 100644 index 0000000..c48ba61 --- /dev/null +++ b/tests/unit/test_formatters.py @@ -0,0 +1,95 @@ +import pytest +from io import StringIO +from errorfix.formatters import TextFormatter, JSONFormatter +from errorfix.patterns import MatchResult +from errorfix.rules import Rule + + +class TestTextFormatter: + def test_format_single_match(self): + rule = Rule( + id='test-rule', + name='Test Rule', + pattern='TestError: .*', + fix='Fix it', + description='A test rule', + ) + result = MatchResult( + rule=rule, + matched_text='TestError: something', + groups={'message': 'something'}, + ) + formatter = TextFormatter(use_colors=False) + output = formatter.format([result], 'TestError: something') + assert 'Test Rule' in output + assert 'Fix it' in output + assert 'something' in output + + def test_format_no_match(self): + formatter = TextFormatter(use_colors=False) + output = formatter.format([], 'Unknown error') + assert 'No matching rules found' in output + + def test_write_to_stream(self): + rule = Rule( + id='test-rule', + name='Test Rule', + pattern='TestError: .*', + fix='Fix it', + description='A test rule', + ) + result = MatchResult(rule=rule, matched_text='TestError: test') + formatter = TextFormatter(use_colors=False) + output = StringIO() + formatter.write([result], 'TestError: test', output) + assert 'Test Rule' in output.getvalue() + + +class TestJSONFormatter: + def test_format_single_match(self): + rule = Rule( + id='test-rule', + name='Test Rule', + pattern='TestError: .*', + fix='Fix it', + description='A test rule', + ) + result = MatchResult( + rule=rule, + matched_text='TestError: something', + groups={'message': 'something'}, + ) + formatter = JSONFormatter(pretty=True) + output = formatter.format([result], 'TestError: something') + assert 'Test Rule' in output + assert '"suggested_fix"' in output + assert 'something' in output + + def test_format_no_match(self): + formatter = JSONFormatter(pretty=True) + output = formatter.format([], 'Unknown error') + assert '"match_count": 0' in output + + def test_pretty_output(self): + formatter = JSONFormatter(pretty=True) + rule = Rule( + id='test-rule', + name='Test Rule', + pattern='TestError: .*', + fix='Fix it', + description='A test rule', + ) + result = MatchResult(rule=rule, matched_text='TestError: test') + output = formatter.format([result], 'TestError: test') + assert '\n' in output + + def test_compact_output(self): + formatter = JSONFormatter(pretty=False) + rule = Rule( + id='test-rule', + name='Test Rule', + description='A test rule', + ) + result = MatchResult(rule=rule, matched_text='TestError: test') + output = formatter.format([result], 'TestError: test') + assert '\n' not in output