Beispiel #1
0
 def mutate_Attribute(self, node):
     try:
         if node.value.id == 'self':
             return ast.Name(id=node.attr, ctx=ast.Load())
         else:
             raise MutationResign()
     except AttributeError:
         raise MutationResign()
Beispiel #2
0
 def mutate_FunctionDef(self, node):
     if not self.should_mutate(node):
         raise MutationResign()
     index, _ = self.get_super_call(node)
     if index is None:
         raise MutationResign()
     node.body[index] = ast.Pass(lineno=node.body[index].lineno)
     return node
Beispiel #3
0
 def mutate_FunctionDef(self, node):
     if not self.should_mutate(node):
         raise MutationResign()
     index, stmt = self.get_super_call(node)
     if index is not None:
         raise MutationResign()
     node.body.insert(0, self.create_super_call(node))
     self.shift_lines(node.body[1:], 1)
     return node
Beispiel #4
0
 def mutate_Assign(self, node):
     if len(node.targets) > 1:
         raise MutationResign()
     if isinstance(node.targets[0], ast.Name) and self.is_overridden(node, name=node.targets[0].id):
         return ast.Pass()
     elif isinstance(node.targets[0], ast.Tuple) and isinstance(node.value, ast.Tuple):
         return self.mutate_unpack(node)
     else:
         raise MutationResign()
Beispiel #5
0
 def mutate_FunctionDef(self, node):
     if not self.should_mutate(node):
         raise MutationResign()
     index, stmt = self.get_super_call(node)
     if index is None:
         raise MutationResign()
     super_call = node.body[index]
     del node.body[index]
     if index == 0:
         self.set_lineno(super_call, node.body[-1].lineno)
         self.shift_lines(node.body, -1)
         node.body.append(super_call)
     else:
         self.set_lineno(super_call, node.body[0].lineno)
         self.shift_lines(node.body, 1)
         node.body.insert(0, super_call)
     return node
Beispiel #6
0
    def mutate_Str(self, node):
        if utils.is_docstring(node):
            raise MutationResign()

        if node.s != self.FIRST_CONST_STRING:
            return ast.Str(s=self.FIRST_CONST_STRING)
        else:
            return ast.Str(s=self.SECOND_CONST_STRING)
Beispiel #7
0
 def is_overridden(self, node, name=None):
     if not isinstance(node.parent, ast.ClassDef):
         raise MutationResign()
     if not name:
         name = node.name
     parent = node.parent
     parent_names = []
     while parent:
         if not isinstance(parent, ast.Module):
             parent_names.append(parent.name)
         if not isinstance(parent, ast.ClassDef) and not isinstance(parent, ast.Module):
             raise MutationResign()
         parent = parent.parent
     getattr_rec = lambda obj, attr: functools.reduce(getattr, attr, obj)
     try:
         klass = getattr_rec(self.module, reversed(parent_names))
     except AttributeError:
         raise MutationResign()
     for base_klass in type.mro(klass)[1:-1]:
         if hasattr(base_klass, name):
             return True
     return False
Beispiel #8
0
 def mutate_FunctionDef(self, node):
     if not isinstance(node.parent, ast.ClassDef):
         raise MutationResign()
     for decorator in node.decorator_list:
         if isinstance(decorator, ast.Call):
             decorator_name = decorator.func.id
         elif isinstance(decorator, ast.Attribute):
             decorator_name = decorator.value.id
         else:
             decorator_name = decorator.id
         if decorator_name == self.get_decorator_name():
             raise MutationResign()
     if node.decorator_list:
         lineno = node.decorator_list[-1].lineno
     else:
         lineno = node.lineno
     decorator = ast.Name(id=self.get_decorator_name(),
                          ctx=ast.Load(),
                          lineno=lineno)
     self.shift_lines(node.body, 1)
     node.decorator_list.append(decorator)
     return node
Beispiel #9
0
 def mutate_unpack(self, node):
     target = node.targets[0]
     value = node.value
     new_targets = []
     new_values = []
     for target_element, value_element in zip(target.elts, value.elts):
         if not self.is_overridden(node, getattr(target_element, 'id', None)):
             new_targets.append(target_element)
             new_values.append(value_element)
     if len(new_targets) == len(target.elts):
         raise MutationResign()
     if not new_targets:
         return ast.Pass()
     elif len(new_targets) == 1:
         node.targets = new_targets
         node.value = new_values[0]
         return node
     else:
         target.elts = new_targets
         value.elts = new_values
         return node
Beispiel #10
0
 def mutate_Pow(self, node):
     if self.should_mutate(node):
         return ast.Mult()
     raise MutationResign()
Beispiel #11
0
    def mutate_Slice_remove_step(self, node):
        if not node.step:
            raise MutationResign()

        return ast.Slice(lower=node.lower, upper=node.upper, step=None)
Beispiel #12
0
 def mutate_Expr(self, node):
     if utils.is_docstring(node.value):
         raise MutationResign()
     return ast.Pass()
Beispiel #13
0
 def mutate_Sub(self, node):
     if self.should_mutate(node):
         return ast.Add()
     raise MutationResign()
Beispiel #14
0
 def mutate_FunctionDef(self, node):
     if self.is_overridden(node):
         return ast.Pass()
     raise MutationResign()
Beispiel #15
0
    def mutate_Str_empty(self, node):
        if not node.s or utils.is_docstring(node):
            raise MutationResign()

        return ast.Str(s='')
Beispiel #16
0
 def mutate_FloorDiv_to_Mult(self, node):
     if self.should_mutate(node):
         return ast.Mult()
     raise MutationResign()
Beispiel #17
0
 def mutate_Mult_to_Div(self, node):
     if self.should_mutate(node):
         return ast.Div()
     raise MutationResign()
Beispiel #18
0
 def mutate_FunctionDef(self, node):
     if node.decorator_list:
         node.decorator_list = []
         return node
     else:
         raise MutationResign()
Beispiel #19
0
 def mutate_ExceptHandler(self, node):
     if len(node.body) == 1 and isinstance(node.body[0], ast.Pass):
         raise MutationResign()
     return self._replace_exception_body(
         node, [ast.Pass(lineno=node.body[0].lineno)])