stryker-net icon indicating copy to clipboard operation
stryker-net copied to clipboard

Remove line mutator

Open domagojmedo opened this issue 1 year ago • 9 comments

Is your feature request related to a problem? Please describe. Most of our service methods end with a

return new ReturnType 
{
    PropA = someVarA,
    PropB = someVarB
}

it would be great if mutator could remove these lines in object initializer, that way test would fail if we forgot to check each property of response

Describe the solution you'd like I would like for mutator to try and remove each line in object initializer. If that's not possible, it would be great to have an option to remove each line in a class when mutating. That would cover this case + any other that depends on line being removed.

Describe alternatives you've considered I don't really know any alternatives to this

Additional context Nothing

domagojmedo avatar Sep 01 '22 08:09 domagojmedo

Removing each line in a class while mutation would probably result in 99% of the mutants being compile errors. It won't be that useful.

rouke-broersma avatar Sep 02 '22 11:09 rouke-broersma

Ok, maybe not each line, but there are a lot of lines that assign some value and removing those would be very useful. Or when constructing EF linq query, if each where is on it's own line, removing one of them would be very useful for test.

I just said removing lines because that I'm guessing that one would be very simple to implement and IMO would be useful

domagojmedo avatar Sep 02 '22 11:09 domagojmedo

We have a statement mutator which removes statements. https://github.com/stryker-mutator/stryker-net/blob/master/src/Stryker.Core/Stryker.Core/Mutators/StatementMutator.cs

rouke-broersma avatar Sep 02 '22 12:09 rouke-broersma

But it won't remove PropA = somethingElse part, correct?

var x = new MyClass
{
    PropA = somethingElse
}

or .Where(x => x.Id == request.Id)

  var exists = await _context.MyTable
              .Where(x => x.Id == request.Id)
              .AnyAsync(cancellationToken);

And IMO those are really useful mutations. I don't know how hard it is to make specific mutations for these cases, that's why I said that maybe just a simple mutator that will try to remove each line would be "good enough". That mutator could be opt-in to not kill performance for everyone

domagojmedo avatar Sep 02 '22 12:09 domagojmedo

I don't think we would accept the 'easy' mutator but I do agree that the suggested mutations could be useful. It would just require more advanced targeting. Should not be impossible for the first case. The second case is not currently possible because we don't have the type information available to mutate such expressions in a way that does not create compile errors.

rouke-broersma avatar Sep 02 '22 13:09 rouke-broersma

For 'easy' mutator it would probably not be per line I'm guessing it would be trivial to not remove single curly braces, class declarations, method declaration and using statements?

Is creating your own mutators something that is supported? If I wanted to create my own "RemoveLineMutator" for example if you weren't going to add it in Core library

domagojmedo avatar Sep 02 '22 14:09 domagojmedo

We don't currently have a way to load custom mutators. We might be open to a contribution that makes it possible to load custom mutators. We are also open to contributions containing new mutators as long as we agree with the mutator implementation.

rouke-broersma avatar Sep 02 '22 14:09 rouke-broersma

Is mutator that removes lines but skips some that will obviously produce compile errors (lines with only open/close curly brace, class/method declaration, using statements) something that you would consider?

domagojmedo avatar Sep 02 '22 14:09 domagojmedo

hello this issue led me to add a support file covering what is required for a mutator. See here https://github.com/stryker-mutator/stryker-net/blob/master/adding_a_mutator.md

dupdob avatar Sep 12 '22 11:09 dupdob