Exemplo n.º 1
0
    def assert_mutation(self, original, mutants, lines=None, operator=None, with_coverage=False, with_exec=False):
        original_ast = utils.create_ast(original)
        if with_coverage:
            coverage_injector = coverage.CoverageInjector()
            coverage_injector.inject(original_ast)
        else:
            coverage_injector = None
        if not operator:
            operator = self.__class__.op
        if isinstance(mutants, str):
            mutants = [mutants]
        mutants = list(map(codegen.remove_extra_lines, mutants))
        module = None
        if with_exec:
            module = utils.create_module(original_ast)
        for mutation, mutatnt in operator.mutate(original_ast, coverage_injector=coverage_injector, module=module):
            mutant_code = codegen.remove_extra_lines(codegen.to_source(mutatnt))
            msg = '\n\nMutant:\n\n' + mutant_code + '\n\nNot found in:'
            for other_mutant in mutants:
                msg += '\n\n----------\n\n' + other_mutant
            self.assertIn(mutant_code, mutants, msg)
            mutants.remove(mutant_code)
            self.assert_location(mutatnt)
            if not lines is None:
                self.assert_mutation_lineo(mutation.node.lineno, lines)

        self.assertListEqual(mutants, [], 'did not generate all mutants')
Exemplo n.º 2
0
 def create_mutant_module(self, target_module, mutant_ast):
     try:
         with self.stdout_manager:
             return utils.create_module(
                 ast_node=mutant_ast,
                 module_name=target_module.__name__
             )
     except BaseException as exception:
         self.notify_incompetent(exception, tests_run=0)
         return None
Exemplo n.º 3
0
 def inject(self, node, module_name='coverage'):
     self.covered_nodes.clear()
     self.marker_transformer = MarkerNodeTransformer()
     marker_node = self.marker_transformer.visit(node)
     coverage_node = CoverageNodeTransformer().visit(copy.deepcopy(marker_node))
     self.covered_nodes.add(coverage_node.marker)
     with utils.StdoutManager():
         return utils.create_module(
             ast_node=coverage_node,
             module_name=module_name,
             module_dict={COVERAGE_SET_NAME: self.covered_nodes},
         )
Exemplo n.º 4
0
 def inject(self, node, module_name='coverage'):
     self.covered_nodes.clear()
     self.marker_transformer = MarkerNodeTransformer()
     marker_node = self.marker_transformer.visit(node)
     coverage_node = CoverageNodeTransformer().visit(
         copy.deepcopy(marker_node))
     self.covered_nodes.add(coverage_node.marker)
     with utils.StdoutManager():
         return utils.create_module(
             ast_node=coverage_node,
             module_name=module_name,
             module_dict={COVERAGE_SET_NAME: self.covered_nodes},
         )
Exemplo n.º 5
0
    def assert_mutation(self,
                        original,
                        mutants,
                        lines=None,
                        operator=None,
                        with_coverage=False,
                        with_exec=False):
        original_ast = utils.create_ast(original)
        if with_coverage:
            coverage_injector = coverage.CoverageInjector()
            coverage_injector.inject(original_ast)
        else:
            coverage_injector = None
        if not operator:
            operator = self.__class__.op
        if isinstance(mutants, str):
            mutants = [mutants]
        mutants = list(map(codegen.remove_extra_lines, mutants))
        module = None
        if with_exec:
            module = utils.create_module(original_ast)
        for mutation, mutatnt in operator.mutate(
                original_ast, coverage_injector=coverage_injector,
                module=module):
            mutant_code = codegen.remove_extra_lines(
                codegen.to_source(mutatnt))
            msg = '\n\nMutant:\n\n' + mutant_code + '\n\nNot found in:'
            for other_mutant in mutants:
                msg += '\n\n----------\n\n' + other_mutant
            self.assertIn(mutant_code, mutants, msg)
            mutants.remove(mutant_code)
            self.assert_location(mutatnt)
            if lines is not None:
                if not hasattr(mutation.node, 'lineno'):
                    self.assert_mutation_lineo(mutation.node.parent.lineno,
                                               lines)
                else:
                    self.assert_mutation_lineo(mutation.node.lineno, lines)

        self.assertListEqual(mutants, [], 'did not generate all mutants')