1

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 -

1 Answer 1

1

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 type Mutator.

What you want to do is inherently impossible for all types because:

  1. Any type could implement subtraction however they want,
  2. There are no guarantees that the + and - operators were overloaded by a type in the first place, and
  3. Operator overloads must have a parameter that matches the type overloading them.

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:

public static void Main()
{
    var origin = new Mutator<long>(10L);
    var x = origin + 3; // 7
    var y = origin - 3; // 13

    Console.WriteLine($"origin: {origin.Value}");
    Console.WriteLine($"x: {x}");
    Console.WriteLine($"y: {y}");
}

class Mutator<T>(T value)
    where T : IAdditionOperators<T, T, T>, ISubtractionOperators<T, T, T>
{
    public T Value { get; set; } = value;

    public static T operator +(Mutator<T> x, T y)
        => x.Value - y;

    public static T operator -(Mutator<T> x, T y)
        => x.Value + y;
}
Sign up to request clarification or add additional context in comments.

2 Comments

First of all, thank you for taking your time to answer. I used int for the sake of simplicity, and my Mutator class is a lot like yours, however I was wondering if there was a way to overload all the methods inside of the class that inherits from the Mutator, so instead of ``` var origin = new Mutator<long>(10L); var x = origin + 3; // 7 var y = origin - 3; // 13 ``` I would have ``` int firstNum = 5; int secondNum = 6; var add = new Service.AddTwoNumbers(firstNum, secondNum); // -1 ```
@MaoShengelia No it's not possible to do it for another class that you didn't implement. What you likely want is a wrapper struct/class for what you want that overloads operators and just does what you want on its wrapped value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.