Answer stability round 1: 1 🔴, 0 🟡 (1 misread); mutated matches Gremlins' lines that keep their CR as well as mutmut's, and the last line of a mutant that ends the file takes no line end; the prose findings' rewording is in this commit

This commit is contained in:
2026-11-23 23:52:25 +01:00
parent a45279fe54
commit 763ee78296
3 changed files with 21 additions and 9 deletions
+9 -6
View File
@@ -23,16 +23,19 @@ class Mutant(typing.NamedTuple):
def mutated(source: bytes, mutant: Mutant) -> bytes | None: def mutated(source: bytes, mutant: Mutant) -> bytes | None:
"""A file's source with the mutant applied, or None where the lines the mutant changes don't stand at the mutant's line. """A file's source with the mutant applied, or None where the lines the mutant changes don't stand at the mutant's line.
The mutant's lines come from a file read as text, so they hold no `\\r`: they match a file's lines without theirs, and go in with A mutant's lines hold no `\\r` where its tool read the file as text, as mutmut does, and hold it where the tool split the bytes on
the line end the file's first line has.""" `\\n`, as Gremlins' planter does. Both match a file's lines, and a new line without `\\r` gets the line end of the file's first line."""
lines = source.split(b"\n") lines = source.split(b"\n")
first = mutant.line - 1 first = mutant.line - 1
end = first + len(mutant.was) end = first + len(mutant.was)
if not 0 <= first <= len(lines) or tuple(line.removesuffix(b"\r") for line in lines[first:end]) != mutant.was: if not 0 <= first <= len(lines) or tuple(line.removesuffix(b"\r") for line in lines[first:end]) != tuple(line.removesuffix(b"\r") for line in mutant.was):
return None return None
# The file's last line has no line end, so none of its lines takes one. line_end = b"\r" if lines[0].endswith(b"\r") else b""
line_end = b"\r" if lines[0].endswith(b"\r") and end < len(lines) else b"" new = [line if line.endswith(b"\r") else line + line_end for line in mutant.now]
lines[first:end] = (line + line_end for line in mutant.now) # Where the mutant's lines end the file, the last of them has no line end.
if new and end >= len(lines):
new[-1] = mutant.now[-1]
lines[first:end] = new
return b"\n".join(lines) return b"\n".join(lines)
+11 -2
View File
@@ -29,12 +29,21 @@ class MutatedTest(unittest.TestCase):
self.assertEqual(mutated(b"a\nb + c", Mutant("calc.go", 2, "x", (b"b + c",), (b"b - c",))), b"a\nb - c") self.assertEqual(mutated(b"a\nb + c", Mutant("calc.go", 2, "x", (b"b + c",), (b"b - c",))), b"a\nb - c")
self.assertIsNone(mutated(b"a\nb + c", Mutant("calc.go", 3, "x", (b"b + c",), (b"b - c",)))) self.assertIsNone(mutated(b"a\nb + c", Mutant("calc.go", 3, "x", (b"b + c",), (b"b - c",))))
def test_a_file_with_crlf_line_ends_matches_the_lines_without_their_cr_and_gets_the_mutants_lines_with_one(self): def test_a_crlf_file_matches_a_mutants_lines_without_cr_and_gets_its_new_lines_with_one(self):
source = b"a\r\n\tb + c\r\n\td,\r\n\te\r\n" source = b"a\r\n\tb + c\r\n\td,\r\n\te\r\n"
self.assertEqual(mutated(source, Mutant("calc.py", 2, "x", (b"\tb + c", b"\td,"), (b"\tb", b"\t- c", b"\td"))), b"a\r\n\tb\r\n\t- c\r\n\td\r\n\te\r\n") self.assertEqual(mutated(source, Mutant("calc.py", 2, "x", (b"\tb + c", b"\td,"), (b"\tb", b"\t- c", b"\td"))), b"a\r\n\tb\r\n\t- c\r\n\td\r\n\te\r\n")
self.assertEqual(mutated(source, Mutant("calc.py", 3, "x", (), (b"\tf,",))), b"a\r\n\tb + c\r\n\tf,\r\n\td,\r\n\te\r\n") self.assertEqual(mutated(source, Mutant("calc.py", 3, "x", (), (b"\tf,",))), b"a\r\n\tb + c\r\n\tf,\r\n\td,\r\n\te\r\n")
self.assertEqual(mutated(source, Mutant("calc.py", 3, "x", (b"\td,",), ())), b"a\r\n\tb + c\r\n\te\r\n") self.assertEqual(mutated(source, Mutant("calc.py", 3, "x", (b"\td,",), ())), b"a\r\n\tb + c\r\n\te\r\n")
self.assertIsNone(mutated(source, Mutant("calc.py", 2, "x", (b"\tb + c\r",), (b"z",)))) self.assertIsNone(mutated(source, Mutant("calc.py", 2, "x", (b"\tb - c",), (b"z",))))
self.assertEqual(mutated(source, Mutant("calc.py", 2, "x", (b"\tb + c", b"\td,"), (b"x", b"y"))), b"a\r\nx\r\ny\r\n\te\r\n")
def test_a_mutant_whose_lines_keep_their_cr_matches_and_goes_in_as_it_is(self):
source = b"a\r\n\tb + c\r\n\te"
self.assertEqual(mutated(source, Mutant("calc.go", 2, "x", (b"\tb + c\r",), (b"\tb - c\r",))), b"a\r\n\tb - c\r\n\te")
self.assertEqual(mutated(source, Mutant("calc.go", 3, "x", (b"\te",), (b"\tf",))), b"a\r\n\tb + c\r\n\tf")
self.assertEqual(mutated(source, Mutant("calc.go", 3, "x", (), (b"\tf\r",))), b"a\r\n\tb + c\r\n\tf\r\n\te")
self.assertEqual(mutated(source, Mutant("calc.go", 3, "x", (), (b"\tf",))), b"a\r\n\tb + c\r\n\tf\r\n\te")
self.assertEqual(mutated(source, Mutant("calc.go", 2, "x", (b"\tb + c", b"\te"), (b"x", b"y"))), b"a\r\nx\r\ny")
def test_a_file_that_mixes_line_ends_takes_the_one_of_its_first_line(self): def test_a_file_that_mixes_line_ends_takes_the_one_of_its_first_line(self):
self.assertEqual(mutated(b"a\r\nb\nc\n", Mutant("calc.py", 2, "x", (b"b",), (b"z",))), b"a\r\nz\r\nc\n") self.assertEqual(mutated(b"a\r\nb\nc\n", Mutant("calc.py", 2, "x", (b"b",), (b"z",))), b"a\r\nz\r\nc\n")
+1 -1
View File
@@ -163,7 +163,7 @@ class KilledMutantsTest(unittest.TestCase):
killed_mutants(str(self.top), ["bin/pkg/ends.py"], self.mutmut(), 3) killed_mutants(str(self.top), ["bin/pkg/ends.py"], self.mutmut(), 3)
self.assertEqual(self.ran[0][0][4:], ["pkg.ends.x_first*", "pkg.ends.x_last*"]) self.assertEqual(self.ran[0][0][4:], ["pkg.ends.x_first*", "pkg.ends.x_last*"])
def test_a_file_with_crlf_line_ends_gives_the_same_mutants_without_the_cr_and_applies_to_it(self): def test_a_crlf_file_gives_the_same_mutants_without_the_cr_and_they_apply_to_it(self):
(self.top / "bin/pkg/calc.py").write_bytes(CALC.replace("\n", "\r\n").encode()) (self.top / "bin/pkg/calc.py").write_bytes(CALC.replace("\n", "\r\n").encode())
found = killed_mutants(str(self.top), ["bin/pkg/calc.py"], self.mutmut(RESULTS.replace("calc.x_", "pkg.calc.x_")), 3) found = killed_mutants(str(self.top), ["bin/pkg/calc.py"], self.mutmut(RESULTS.replace("calc.x_", "pkg.calc.x_")), 3)
self.assertEqual([(mutant.line, mutant.was, mutant.now) for mutant in found], [(2, (b"\treturn n * 2",), (b"\treturn n / 2",)), (6, (b"\treturn (", b"\t\tn * 3", b"\t)"), (b"\treturn [", b"\t\tn * 3", b"\t]"))]) self.assertEqual([(mutant.line, mutant.was, mutant.now) for mutant in found], [(2, (b"\treturn n * 2",), (b"\treturn n / 2",)), (6, (b"\treturn (", b"\t\tn * 3", b"\t)"), (b"\treturn [", b"\t\tn * 3", b"\t]"))])