refactor(tools): extract IDENTICAL_STRINGS_ERROR constant

The 3-sentence identical-edit message was snapshot-asserted verbatim in
two tests. House style avoids exact-string change-detector assertions;
both tests now import the constant from tools/fuzzy_match so rewording
the message can't silently break them.
This commit is contained in:
kshitij
2026-08-12 14:08:05 +05:30
parent 9c541de91a
commit 48db2011b9
2 changed files with 11 additions and 17 deletions

View File

@@ -1,6 +1,6 @@
"""Tests for the fuzzy matching module."""
from tools.fuzzy_match import fuzzy_find_and_replace
from tools.fuzzy_match import IDENTICAL_STRINGS_ERROR, fuzzy_find_and_replace
class TestExactMatch:
@@ -29,11 +29,7 @@ class TestExactMatch:
new, count, _, err = fuzzy_find_and_replace("abc", "abc", "abc")
assert count == 0
assert new == "abc"
assert err == (
"No edit was applied because old_string and new_string are identical. "
"Provide the existing text to replace in old_string and the changed "
"replacement text in new_string."
)
assert err == IDENTICAL_STRINGS_ERROR
def test_multiline_exact(self):
content = "line1\nline2\nline3"
@@ -442,12 +438,7 @@ class TestFormatNoMatchHint:
def test_silent_on_identical_strings(self):
"""old_string == new_string — hint irrelevant."""
result = self.fmt(
"No edit was applied because old_string and new_string are identical. "
"Provide the existing text to replace in old_string and the changed "
"replacement text in new_string.",
0, "foo", "foo bar\n",
)
result = self.fmt(IDENTICAL_STRINGS_ERROR, 0, "foo", "foo bar\n")
assert result == ""
def test_silent_when_match_count_nonzero(self):

View File

@@ -57,6 +57,13 @@ UNICODE_MAP = {
"\u3000": " ", # ideographic (CJK full-width) space
}
IDENTICAL_STRINGS_ERROR = (
"No edit was applied because old_string and new_string are identical. "
"Provide the existing text to replace in old_string and the changed "
"replacement text in new_string."
)
def _unicode_normalize(text: str) -> str:
"""Normalizes Unicode characters to their standard ASCII equivalents."""
for char, repl in UNICODE_MAP.items():
@@ -143,11 +150,7 @@ def fuzzy_find_and_replace(content: str, old_string: str, new_string: str,
return content, 0, None, "old_string is only whitespace — provide non-blank text to match"
if old_string == new_string:
return content, 0, None, (
"No edit was applied because old_string and new_string are identical. "
"Provide the existing text to replace in old_string and the changed "
"replacement text in new_string."
)
return content, 0, None, IDENTICAL_STRINGS_ERROR
# Try each matching strategy in order
strategies: List[Tuple[str, Callable]] = [