I am trying to come up with the way to implement a way to perform a Mutation test on my class (yes I am aware of Stryker .NET).
For the sake of simplicity, let's say that I want to write a Mutator class that some service will inherit from, and it will convert all additions to subtraction. I am wondering whether it is possible to achieve this without doing any changes to the service class.
I have this service that performs a simple addition
public class Service : Mutator
{
public int AddTwoNumbers(int firstNum, int secondNum)
{
return firstNum + secondNum;
}
}
at first, I simply tried to write Mutator class as follows:
public class Mutator
{
public static int operator +(int a, int b)
{
return a - b;
}
}
but of course quickly discovered that following code produces compiler error ("One of the parameters of a binary operator must be the containing type")
So I wonder, if there is a way to achieve this kind of mutation trough inheritance without changing the actual Service class.
The reason I choose operator overloading instead of method overloading or something like that is because I want to be able to inherit from this Mutator from any random class and to just simply change all + into -
It tells you that one of the operator params need to be the type of the overloading class itself. The
operator+
overload must have a param of typeMutator
.What you want to do is inherently impossible for all types because:
However, it is possible to achieve this to some degree. You can implement Mutator to be generic and add constraints to it so you know that a derived type will have an addition or subtraction that you can swap. You will not be able to use generic
T
as params alone, though. Mutator will have to be a wrapper class. Here is an example: