stryker-mutator / stryker-net

Mutation testing for .NET core and .NET framework!
https://stryker-mutator.io
Apache License 2.0
1.76k stars 175 forks source link

Mutators for C# 9 Pattern Matching #1338

Closed hannahchan closed 1 year ago

hannahchan commented 3 years ago

I'm only just learning about this now so this might not make sense. I've got code like this;

if (myInput is not null)
{
    doSomething();
}

The is not pattern combination is new to C# 9 and Stryker is currently unable to mutate it. It looks like there are other pattern operators such as and and/or or 😄 . It would be nice to have mutators for those as well.

Someone will need to take a look at the pattern matching specification of C# 9. The following might help.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/patterns3

rouke-broersma commented 3 years ago

Pattern matching mutators would be awesome :)

richardwerkman commented 3 years ago

I think there is not much else than not, and and or. So the mutator would look like:

Pattern matching

Original Mutated
is is not
is not is
and or
or and

We could also remove brackets like:

bool IsLetter(char c) => c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z');
bool IsLetter(char c) => c is >= 'a' and <= 'z' or >= 'A' and <= 'Z';
rouke-broersma commented 3 years ago

Those are the interesting new operators to mutate, but I don't think we're currently mutating all existing pattern matching values since sometimes they need to be static. There might also be some fun stuff we could do with the is Type pattern matching. For example change an is Type pattern to a is var pattern.

rouke-broersma commented 1 year ago

Implemented by #2313 for switch patterns