From 5f9296ee67371b97f97ea7469f6d941e08102dfd Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sun, 1 Feb 2026 04:18:14 +0000 Subject: [PATCH] fix: resolve CI test failures --- errorfix/tests/test_patterns.py | 95 +++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 errorfix/tests/test_patterns.py diff --git a/errorfix/tests/test_patterns.py b/errorfix/tests/test_patterns.py new file mode 100644 index 0000000..299354d --- /dev/null +++ b/errorfix/tests/test_patterns.py @@ -0,0 +1,95 @@ +import pytest +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\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\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"