100 lines
6.0 KiB
Python
100 lines
6.0 KiB
Python
"""How task-bench re-applies a mutant: as the same change to the same lines at the base and at the head."""
|
|
import pathlib
|
|
import sys
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2]))
|
|
|
|
from task_bench.bugs.planted_mutant import bug_at_sides, line_at_head, Mutant, mutated
|
|
|
|
|
|
class MutatedTest(unittest.TestCase):
|
|
SOURCE = b"a\n\tb + c\n\td,\n\te\n"
|
|
|
|
def test_puts_the_mutants_lines_in_place_of_the_ones_it_changes_and_changes_no_other_byte(self):
|
|
self.assertEqual(mutated(self.SOURCE, Mutant("calc.py", 2, "calc.py:2 x", (b"\tb + c",), (b"\tb - c",))), b"a\n\tb - c\n\td,\n\te\n")
|
|
self.assertEqual(mutated(self.SOURCE, Mutant("calc.py", 2, "calc.py:2 x", (b"\tb + c", b"\td,"), (b"\tb", b"\t- c", b"\td"))), b"a\n\tb\n\t- c\n\td\n\te\n")
|
|
|
|
def test_removes_lines_and_adds_them(self):
|
|
self.assertEqual(mutated(self.SOURCE, Mutant("calc.py", 3, "calc.py:3 x", (b"\td,",), ())), b"a\n\tb + c\n\te\n")
|
|
self.assertEqual(mutated(self.SOURCE, Mutant("calc.py", 3, "calc.py:3 x", (), (b"\tf,",))), b"a\n\tb + c\n\tf,\n\td,\n\te\n")
|
|
|
|
def test_is_none_where_the_line_does_not_start_the_lines_the_mutant_changes(self):
|
|
for line, was in ((1, (b"\tb + c",)), (2, (b"\tb + c", b"\te")), (2, (b"b + c",)), (0, (b"a",)), (-1, (b"\te",)), (5, (b"\te", b"")), (9, (b"",)), (7, ())):
|
|
with self.subTest(line=line, was=was):
|
|
self.assertIsNone(mutated(self.SOURCE, Mutant("calc.py", line, "x", was, (b"z",))))
|
|
self.assertEqual(mutated(self.SOURCE, Mutant("calc.py", 6, "x", (), (b"z",))), self.SOURCE + b"\nz")
|
|
|
|
def test_mutates_the_last_line_of_a_file(self):
|
|
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_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",), (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")
|
|
self.assertEqual(mutated(b"a\nb\r\nc\r\n", Mutant("calc.py", 2, "x", (b"b",), (b"z",))), b"a\nz\nc\r\n")
|
|
|
|
def test_the_last_line_of_a_crlf_file_has_no_line_end_to_take(self):
|
|
self.assertEqual(mutated(b"a\r\nb + c", Mutant("calc.py", 2, "x", (b"b + c",), (b"b - c",))), b"a\r\nb - c")
|
|
|
|
|
|
class LineAtHeadTest(unittest.TestCase):
|
|
# Lines 3 and 4 became one, two lines went in after line 9, and line 20 went.
|
|
DIFF = "diff --git a/calc.go b/calc.go\n--- a/calc.go\n+++ b/calc.go\n@@ -3,2 +3 @@\n-a\n-b\n+c\n@@ -9,0 +9,2 @@\n+d\n+e\n@@ -20 +20,0 @@\n-f\n"
|
|
|
|
def test_a_line_above_every_change_keeps_its_number(self):
|
|
self.assertEqual(line_at_head(self.DIFF, 2), 2)
|
|
|
|
def test_a_line_below_a_change_moves_by_the_lines_it_added_and_removed(self):
|
|
self.assertEqual([line_at_head(self.DIFF, line) for line in (5, 9, 10, 19, 21)], [4, 8, 11, 20, 21])
|
|
|
|
def test_a_line_the_head_changed_or_removed_has_none(self):
|
|
self.assertEqual([line_at_head(self.DIFF, line) for line in (3, 4, 20)], [None, None, None])
|
|
|
|
def test_no_diff_keeps_every_line(self):
|
|
self.assertEqual(line_at_head("", 12), 12)
|
|
|
|
|
|
class BugAtSidesTest(unittest.TestCase):
|
|
MUTANT = Mutant("calc.go", 3, "calc.go:3:11 ARITHMETIC_BASE", (b"\treturn a + b",), (b"\treturn a - b",))
|
|
BASE = b"package calc\n\n\treturn a + b\n"
|
|
# The head adds a line above the bug's.
|
|
HEAD = b"package calc\n// Add adds.\n\n\treturn a + b\n"
|
|
DIFF = "@@ -1,0 +2 @@\n+// Add adds.\n"
|
|
|
|
def test_the_bug_is_the_same_change_at_each_side_on_the_line_as_that_side_numbers_it(self):
|
|
found = bug_at_sides(self.MUTANT, self.DIFF, {"base": self.BASE, "head": self.HEAD})
|
|
self.assertEqual(found, {"base": self.BASE.replace(b"+", b"-"), "head": self.HEAD.replace(b"+", b"-")})
|
|
|
|
def test_a_line_the_head_changed_holds_no_bug(self):
|
|
self.assertIsNone(bug_at_sides(self.MUTANT, "@@ -3 +3 @@\n-\treturn a + b\n+\treturn b + a\n", {"base": self.BASE, "head": self.BASE}))
|
|
|
|
def test_a_side_without_the_file_or_without_the_line_the_mutant_changes_holds_no_bug(self):
|
|
self.assertIsNone(bug_at_sides(self.MUTANT, "", {"base": self.BASE, "head": None}))
|
|
self.assertIsNone(bug_at_sides(self.MUTANT, "", {"base": self.BASE, "head": b"package calc\n\n\treturn sum(a, b)\n"}))
|
|
|
|
def test_a_mutant_of_several_lines_holds_no_bug_where_the_head_changed_one_after_the_first(self):
|
|
mutant = Mutant("calc.py", 1, "calc.py:1 x", (b"low = min(", b"\ta,"), (b"low = max(", b"\ta,"))
|
|
base, head = b"low = min(\n\ta,\n\tb)\n", b"low = min(\n\tc,\n\tb)\n"
|
|
self.assertIsNone(bug_at_sides(mutant, "@@ -2 +2 @@\n-\ta,\n+\tc,\n", {"base": base, "head": head}))
|
|
self.assertEqual(bug_at_sides(mutant, "", {"base": base, "head": base}), {"base": base.replace(b"min", b"max"), "head": base.replace(b"min", b"max")})
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|