Initial upload: ErrorFix CLI with rule engine and pattern matching
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
95
tests/unit/test_formatters.py
Normal file
95
tests/unit/test_formatters.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user