Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions tests/test_semantic_mutants.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,133 @@ def test_comments_and_other_branches_are_left_alone(self):
self.assertEqual(sum(1 for m in self.mutants if m.line == line and m.label == "OpIAdd -> OpISub"), 1)
self.assertFalse(any(m.line == line and m.label == "OpISub -> OpIAdd" for m in self.mutants))

def test_docstrings_are_left_alone(self):
# A docstring is text: an OpIAdd/UMin named there can't fail a test, so a mutant of it always "survives".
source = textwrap.dedent('''\
class Gen:
def helper(self, x, y):
"""Emit OpIAdd, then UMin with self.cu(31)."""
self.emit(f"{r} = OpIAdd %uint {x} {y}")

def multi(self, x):
"""First line.

OpISub and NMin are named here too.
"""
self.emit(f"{t} = OpExtInst %uint %glsl UMin {x} {self.cu(31)}")
s = "OpISub in a plain string assignment is still code"
''').split("\n")
for name, code_lines in (("helper", [4]), ("multi", [11, 12])):
first, last = sm.find_function(source, name)
lines = {m.line for m in sm.mutants_for(source, first, last)}
with self.subTest(function=name):
self.assertEqual(sorted(lines), code_lines)
# A --lines range can start or end inside a docstring, without its opening or closing delimiter.
inside = next(i for i, s in enumerate(source) if "OpISub and NMin" in s) # 0-based
for first, last in ((inside, inside), (inside, inside + 2), (inside - 3, inside)):
with self.subTest(lines=(first + 1, last + 1)):
lines = {m.line for m in sm.mutants_for(source, first, last)}
self.assertEqual(sorted(lines), [n for n in (11,) if first < n <= last + 1])

def test_docstring_after_a_comment_with_quotes_and_concatenated_docstring(self):
# Two forms ast.get_docstring() accepts (@lixonik on #660): a `"""` in a comment before the docstring is no
# delimiter, and adjacent literals ("a " "b") are one docstring.
source = textwrap.dedent('''\
class Gen:
def f(self, x, y):
# a comment with """ in it, and one with \'\'\' too
"""Start of the docstring.

OpIAdd is named here.
"""
self.emit(f"{r} = OpIAdd %uint {x} {y}")

def g(self, x, y):
"OpIAdd is an example. " "Emit an instruction."
self.emit(f"{r} = OpIAdd %uint {x} {y}")

def h(self, x, y):
("UMin is not a docstring here: the call reads it, so it is code. "
"OpISub") and self.emit(f"{r} = OpIAdd %uint {x} {y}")
''').split("\n")
named = next(i for i, s in enumerate(source) if "OpIAdd is named here" in s)
self.assertEqual(sm.mutants_for(source, named, named), [])
for name, code_lines in (("f", [8]), ("g", [12])):
first, last = sm.find_function(source, name)
with self.subTest(function=name):
self.assertEqual(sorted({m.line for m in sm.mutants_for(source, first, last)}), code_lines)
first, last = sm.find_function(source, "h")
self.assertIn(16, {m.line for m in sm.mutants_for(source, first, last)}) # still code: mutated


LOGIC = textwrap.dedent('''\
class Gen:
def op(self, op, a):
if op == "v_other":
pass
elif op in ("v_logic",):
seen, out = set(), []
for r in a:
if r not in seen and r > 3:
out.append(r)
seen.update([r])
n = 0
n += 1
n = len(out)
ok = n is None or not out
both = ok and n and out
flag = True
self.emit(f"{t} = OpIAdd %uint {x} {self.cu(0)}")
s = "a == b and c in d" # a comment: x == y and not z
return [x for x in out if x in seen]
''').split("\n")


class PythonLogic(unittest.TestCase):
"""The analysis code around the emission: comparisons, and/or, not, True/False, silent statements."""

def setUp(self):
self.first, self.last = sm.find_branch(LOGIC, "v_logic")
self.mutants = sm.mutants_for(LOGIC, self.first, self.last)

def labels(self, text):
line = next(i for i, s in enumerate(LOGIC) if text in s) + 1
return sorted(m.label for m in self.mutants if m.line == line)

def test_comparisons_and_boolean_operators(self):
self.assertEqual(self.labels("if r not in seen"), ["> -> >=", "and -> or", "not in -> in"])
self.assertEqual(self.labels("ok = n is None"), ["is -> is not", "not removed", "or -> and"])
self.assertEqual(self.labels("both = ok"), ["and -> or #1", "and -> or #2"])
self.assertEqual(self.labels("flag = True"), ["True -> False"])

def test_silent_statements_are_removed(self):
for text in ("out.append(r)", "seen.update([r])", "n += 1", "n = len(out)"):
with self.subTest(statement=text):
self.assertEqual(self.labels(text), ["statement removed"])
removed = next(m for m in self.mutants if "out.append(r)" in m.old)
self.assertEqual(removed.new, removed.old[:len(removed.old) - len(removed.old.lstrip())] + "pass")

def test_first_assignments_and_compound_lines_stay(self):
# Removing a first assignment is only a NameError; `for`, `return` and the emit are not simple statements.
self.assertEqual(self.labels("seen, out = set()"), [])
self.assertEqual(self.labels("n = 0"), [])
self.assertEqual(self.labels("for r in a"), [])

def test_strings_comments_emit_and_branch_condition_are_left_alone(self):
self.assertEqual(self.labels('s = "a == b'), [])
self.assertEqual(self.labels("self.emit("), ["OpIAdd -> OpISub", "self.cu(0) -> self.cu(1)"])
self.assertEqual(self.labels('elif op in ("v_logic",)'), [])

def test_the_in_of_a_comprehension_loop_stays(self):
self.assertEqual(self.labels("return [x for x in out"), ["in -> not in"])

def test_every_mutant_still_compiles(self):
for m in self.mutants:
mutated = list(LOGIC)
mutated[m.line - 1] = m.new
with self.subTest(line=m.line, label=m.label):
compile("\n".join(mutated), "<mutant>", "exec")


class Tables(unittest.TestCase):
def test_entry_takes_the_other_values_of_its_table(self):
Expand Down
Loading
Loading