stryker-mutator / stryker-net

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

Add support for UTF8 string literals #2940

Open iamdmitrij opened 1 month ago

iamdmitrij commented 1 month ago

Fixes #2923.

Implemented the same logic as in plain strings:

SyntaxFactory.Literal(string) method used for string mutations doesn't work with UTF8 string literal type (ReadOnlySpan<byte>), so I've taken inspiration on how to make it work from dotnet/roslyn analyzers' code.

Tasks:

richardwerkman commented 1 month ago

I tested your PR on the following code and it broke:

        public string Test()
        {
            return "Hello " + " " + "World";
        }

So there is some issue with the mutator logic

iamdmitrij commented 1 month ago

I tested your PR on the following code and it broke:

        public string Test()
        {
            return "Hello " + " " + "World";
        }

So there is some issue with the mutator logic

Can you specify what exactly fails here?

I have noticed it doesn't compile when concatenation operator + is mutated to -.

var a1 = "Hello"u8 + " "u8 + "World"u8; // OK
var a2 = "Hello"u8 - " "u8 - "World"u8; // Doesn't compile

var a3 = "Hello" + " " + "World"; // OK
var a4 = "Hello" - " " - "World"; // Doesn't compile

If that's the case, should it be fixed somehow? Because I don't see how current or previous string mutator code has solution for that. My best guess would be to fix BinaryExpressionMutator implementation to avoid this.

richardelekta commented 1 month ago

I have noticed it doesn't compile when concatenation operator + is mutated to -.

This isn't the problem, we prevent this mutation from being placed.

var a1 = "Hello"u8 + " "u8 + "World"u8; // OK
var a2 = ""u8 + " "u8 + "World"u8; // Doesn't compile

var a3 = "Hello" + " " + "World"; // OK
var a4 = "" + " " + "World"; // OK

The above displays the issue. The interesting part is that the code should compile, but it doesn't because of how stryker places the mutation. I haven't investigate yet what exactly goes wrong, but my guess is that there is a flaw in our mutation placing logic

iamdmitrij commented 1 month ago

I have noticed it doesn't compile when concatenation operator + is mutated to -.

This isn't the problem, we prevent this mutation from being placed.

var a1 = "Hello"u8 + " "u8 + "World"u8; // OK
var a2 = ""u8 + " "u8 + "World"u8; // Doesn't compile

var a3 = "Hello" + " " + "World"; // OK
var a4 = "" + " " + "World"; // OK

The above displays the issue. The interesting part is that the code should compile, but it doesn't because of how stryker places the mutation. I haven't investigate yet what exactly goes wrong, but my guess is that there is a flaw in our mutation placing logic

Thank you. Now it's clear, I will look into this use-case.

richardwerkman commented 3 weeks ago

@iamdmitrij The integration tests have been updated and now also include my previous example.