Beispiel #1
0
def build_mutator(cfg):
    operators_set = set()

    if cfg.experimental_operators:
        operators_set |= operators.experimental_operators

    name_to_operator = build_name_to_operator_map()

    if cfg.operator:
        operators_set |= {
            get_operator(name, name_to_operator)
            for name in cfg.operator
        }
    else:
        operators_set |= operators.standard_operators

    operators_set -= {
        get_operator(name, name_to_operator)
        for name in cfg.disable_operator
    }

    if cfg.order == 1:
        return controller.FirstOrderMutator(operators_set, cfg.percentage)
    else:
        hom_strategy = build_hom_strategy(cfg)
        return controller.HighOrderMutator(operators_set,
                                           cfg.percentage,
                                           hom_strategy=hom_strategy)
Beispiel #2
0
    def test_second_order_mutation(self):
        mutator = controller.HighOrderMutator(
            operators=[operators.ArithmeticOperatorReplacement, operators.AssignmentOperatorReplacement],
        )
        target_ast = utils.create_ast('x += y + z')

        for number, (mutations, mutant) in enumerate(mutator.mutate(target_ast)):
            self.assertEqual(number, 0)
            self.assertEqual('x -= y - z', codegen.to_source(mutant))
            self.assertEqual(len(mutations), 2)

        self.assertEqual(codegen.to_source(target_ast), 'x += y + z')
Beispiel #3
0
 def test_second_order_mutation_with_multiple_visitors(self):
     mutator = controller.HighOrderMutator(
         operators=[operators.ConstantReplacement], )
     target_ast = utils.create_ast('x = "test"')
     number = None
     for number, (mutations,
                  mutant) in enumerate(mutator.mutate(target_ast)):
         if number == 0:
             self.assertEqual("x = 'mutpy'", codegen.to_source(mutant))
             self.assertEqual(len(mutations), 1)
         elif number == 1:
             self.assertEqual("x = ''", codegen.to_source(mutant))
             self.assertEqual(len(mutations), 1)
     self.assertEqual(number, 1)
     self.assertEqual(codegen.to_source(target_ast), "x = 'test'")
Beispiel #4
0
 def test_second_order_mutation_with_same_node_as_target(self):
     mutator = controller.HighOrderMutator(
         operators=[operators.ArithmeticOperatorDeletion, operators.ArithmeticOperatorReplacement],
     )
     target_ast = utils.create_ast('- a')
     number = None
     for number, (mutations, mutant) in enumerate(mutator.mutate(target_ast)):
         if number == 0:
             self.assertEqual('a', codegen.to_source(mutant))
             self.assertEqual(len(mutations), 1)
         elif number == 1:
             self.assertEqual('+a', codegen.to_source(mutant))
             self.assertEqual(len(mutations), 1)
     self.assertEqual(number, 1)
     self.assertEqual(codegen.to_source(target_ast), '-a')