~renierm/mutantpie/initial

« back to all changes in this revision

Viewing changes to operatormutators.py

  • Committer: Renier Marchand
  • Date: 2011-07-18 09:26:35 UTC
  • Revision ID: renierm@gmail.com-20110718092635-tkudnv46b74pnik4
Assignment operator moved to external class

Show diffs side-by-side

added added

removed removed

Lines of Context:
183
183
                            yield(self.ast, message)
184
184
                            self.replace_in_anchor(context[-1], new_op, i)
185
185
 
 
186
class AssignmentMutator(OperatorMutator):
 
187
 
 
188
    def convert_Assign_AugAssign(self, oldass, newop):
 
189
        return ast.AugAssign(target=oldass.targets[0], value=oldass.value, op=newop)
 
190
 
 
191
    def _operator_mutator(self, target_ast, to_operators, message_template):
 
192
        """
 
193
        Special method to handel mutation from Assign '=' to AugAssign '+='
 
194
        """
 
195
        for i, context in walk_in_order_with_context(target_ast):
 
196
            if isinstance(i, ast.Assign):
 
197
                for k in to_operators:
 
198
                    new_op = self.convert_Assign_AugAssign(i,k())
 
199
                    self.replace_in_anchor(context[-1], i, new_op)
 
200
                    ast.fix_missing_locations(target_ast)
 
201
                    message = message_template % ('=', to_operators[k], i.lineno, i.col_offset)
 
202
                    yield(self.ast, message)
 
203
                    self.replace_in_anchor(context[-1], new_op, i)
 
204
 
 
205
 
186
206
###########################################
187
207
###### Binary (non-assign) operators ######
188
208
###########################################
496
516
 
497
517
## Plain assignment operator (=)
498
518
 
499
 
class OEAA(OperatorMutator):
 
519
class OEAA(AssignmentMutator):
500
520
    """
501
521
    Cycles from [=] to [+=, -=, *=, /=, %=, //=, **=]
502
522
    """
503
523
    def mutant(self, target_ast):
504
 
        return self._assignment_mutator(target_ast, self.arithmetic_operators, 'change assignment operator (%s) to arithmetic assignment operator (%s) @ (%s,%s)')
 
524
        return self._operator_mutator(target_ast, self.arithmetic_operators, 'change assignment operator (%s) to arithmetic assignment operator (%s) @ (%s,%s)')
505
525
 
506
 
class OEBA(OperatorMutator):
 
526
class OEBA(AssignmentMutator):
507
527
    """
508
528
    Cycles from [=] to [|=, ^=, &=]
509
529
    """
510
530
    def mutant(self, target_ast):
511
 
        return self._assignment_mutator(target_ast, self.bitwise_operators, 'change assignment operator (%s) to bitwise assignment operator (%s) @ (%s,%s)')
 
531
        return self._operator_mutator(target_ast, self.bitwise_operators, 'change assignment operator (%s) to bitwise assignment operator (%s) @ (%s,%s)')
512
532
 
513
 
class OESA(OperatorMutator):
 
533
class OESA(AssignmentMutator):
514
534
    """
515
535
    Cycles from [=] to [>>=, <<=]
516
536
    """
517
537
    def mutant(self, target_ast):
518
 
        return self._assignment_mutator(target_ast, self.shift_operators, 'change assignment operator (%s) to shift assignment operator (%s) @ (%s,%s)')
 
538
        return self._operator_mutator(target_ast, self.shift_operators, 'change assignment operator (%s) to shift assignment operator (%s) @ (%s,%s)')
519
539
 
520
540
### Other operator mutators
521
541