95 lines
3.6 KiB
Python
95 lines
3.6 KiB
Python
from errorfix.patterns.matcher import PatternMatcher
|
|
from errorfix.rules.rule import Rule
|
|
|
|
|
|
class TestPatternMatcher:
|
|
def setup_method(self):
|
|
self.matcher = PatternMatcher()
|
|
|
|
def test_simple_pattern_match(self):
|
|
rule = Rule(
|
|
id="test001",
|
|
name="Test",
|
|
pattern=r"NameError: name '(\w+)' is not defined",
|
|
fix="Declare variable",
|
|
description="Test rule"
|
|
)
|
|
result = self.matcher.match("NameError: name 'foo' is not defined", rule)
|
|
assert result is not None
|
|
assert result.matched_text == "NameError: name 'foo' is not defined"
|
|
|
|
def test_no_match(self):
|
|
rule = Rule(
|
|
id="test002",
|
|
name="Test",
|
|
pattern=r"NameError: name '(\w+)' is not defined",
|
|
fix="Fix",
|
|
description="Test"
|
|
)
|
|
result = self.matcher.match("This is just a regular message", rule)
|
|
assert result is None
|
|
|
|
def test_match_all_multiple_rules(self):
|
|
rules = [
|
|
Rule(id="s1", name="Syntax", pattern=r"SyntaxError: (.+)", fix="f", description="d"),
|
|
Rule(id="n1", name="Name", pattern=r"NameError: (.+)", fix="f", description="d"),
|
|
]
|
|
results = self.matcher.match_all("NameError: x is not defined", rules)
|
|
assert len(results) == 1
|
|
assert results[0].rule.id == "n1"
|
|
|
|
def test_priority_ordering(self):
|
|
rules = [
|
|
Rule(id="low", name="Low", pattern=r"UnknownError: (.+)", fix="f", description="d", priority=1),
|
|
Rule(id="high", name="High", pattern=r"NameError: (.+)", fix="f", description="d", priority=3),
|
|
Rule(id="med", name="Med", pattern=r"SyntaxError: (.+)", fix="f", description="d", priority=2),
|
|
]
|
|
results = self.matcher.match_with_priority("NameError: test", rules)
|
|
assert len(results) == 1
|
|
assert results[0].rule.id == "high"
|
|
|
|
def test_named_groups(self):
|
|
rule = Rule(
|
|
id="test003",
|
|
name="Test",
|
|
pattern=r"NameError: name '(?P<var_name>\w+)' is not defined",
|
|
fix="Declare {var_name}",
|
|
description="Test"
|
|
)
|
|
result = self.matcher.match("NameError: name 'myVar' is not defined", rule)
|
|
assert result is not None
|
|
assert result.groups.get("var_name") == "myVar"
|
|
|
|
def test_find_best_match(self):
|
|
rules = [
|
|
Rule(id="r1", name="R1", pattern=r"Error: (.+)", fix="f", description="d", priority=1),
|
|
Rule(id="r2", name="R2", pattern=r"NameError: (.+)", fix="f", description="d", priority=3),
|
|
]
|
|
result = self.matcher.find_best_match("NameError: test", rules)
|
|
assert result is not None
|
|
assert result.rule.id == "r2"
|
|
|
|
def test_extract_variables(self):
|
|
variables = self.matcher.extract_variables(
|
|
"Error code 404 found",
|
|
r"Error code (?P<code>\d+) found"
|
|
)
|
|
assert "code" in variables
|
|
assert variables["code"] == "404"
|
|
|
|
def test_replace_variables(self):
|
|
result = self.matcher.replace_variables(
|
|
"Fix {variable} here",
|
|
{"variable": "test_value"}
|
|
)
|
|
assert result == "Fix test_value here"
|
|
|
|
def test_match_with_priority_returns_all_matches_sorted(self):
|
|
rules = [
|
|
Rule(id="r1", name="R1", pattern=r"Error: (.+)", fix="f", description="d", priority=1),
|
|
Rule(id="r2", name="R2", pattern=r"(.+)", fix="f", description="d", priority=2),
|
|
]
|
|
results = self.matcher.match_with_priority("Error: test", rules)
|
|
assert len(results) == 2
|
|
assert results[0].rule.id == "r2"
|