stryker-mutator / stryker-net

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

String Mutation Should Be Applied to Constant Fields #1505

Open dogac00 opened 3 years ago

dogac00 commented 3 years ago

Describe the bug I don't know if this behavior is a bug or not. String mutation applies when a string literal is used within a method. But it doesn't apply when a constant is used instead of string literal. Knowing const fields being a compile-time constant and replaced with the string literal in the compilation, we should apply string mutation to constant fields in the methods too.

Here's an example:

public class Repository : IRepository
{
    private const string GetByIdQuery = @"SELECT *
                                          FROM ""Entities""
                                          WHERE ""Id"" = @Id";

    public Task<Entity> GetById(IDbConnection connection, long id)
    {
        return connection.QueryFirstOrDefaultAsync<Entity>(GetByIdQuery, new
        {
            Id = id
        });
    }
}

When query is written inline, string mutation works, but when it is retrieved from a field, string mutation does not apply.

Desktop (please complete the following information):

dupdob commented 3 years ago

Hi @dogac00, this is not really a bug, more like a limitation of what can be achieved with the overall architecture of Stryker, let me explain:

Full support for build time mutations, such as mutating const values, requires building multiple versions of the assembly, which would result in a far slower process overall. There is no such evolution planned yet.

Alternatively, const values could be made mutable, by 'demoting' them to plain static fields, but this would often result in build errors, due to the fact that some constructions require const values, such as default value for a parameter or usage in some attribute.