diff --git a/bin/task_bench/planted_mutant.py b/bin/task_bench/planted_mutant.py index 46dbf24e..6ff0d6ae 100644 --- a/bin/task_bench/planted_mutant.py +++ b/bin/task_bench/planted_mutant.py @@ -23,16 +23,19 @@ class Mutant(typing.NamedTuple): 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. - 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 - the line end the file's first line has.""" + 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 + `\\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") first = mutant.line - 1 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 - # 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") and end < len(lines) else b"" - lines[first:end] = (line + line_end for line in mutant.now) + line_end = b"\r" if lines[0].endswith(b"\r") else b"" + new = [line if line.endswith(b"\r") else 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) diff --git a/bin/task_bench/test_planted_mutant.py b/bin/task_bench/test_planted_mutant.py index 0937247e..cc9d397a 100644 --- a/bin/task_bench/test_planted_mutant.py +++ b/bin/task_bench/test_planted_mutant.py @@ -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.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" 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"\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): 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") diff --git a/bin/task_bench/test_python_bugs.py b/bin/task_bench/test_python_bugs.py index ccca6141..42013f06 100644 --- a/bin/task_bench/test_python_bugs.py +++ b/bin/task_bench/test_python_bugs.py @@ -163,7 +163,7 @@ class KilledMutantsTest(unittest.TestCase): 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*"]) - 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()) 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]"))])