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

Stryker could be able to mutate dates #1184

Open raschmitt opened 4 years ago

raschmitt commented 4 years ago

Is your feature request related to a problem? Please describe.

I think it would be interesting if stryker could mutate dates.

Describe the solution you'd like

DateTime.Now would be mutated into something like DateTime.Now.AddDays(1) and DateTime.Now.AddDays(-1).

Describe alternatives you've considered

Of course, this implementation could also be used for DateTimeOffset types.

Additional context

Initially, it could be easier to just mutate days, and further on we can walk our way into years, hours, seconds, and else...

What do you guys think?

If anyone feels interested I can volunteer for implementing it.

rouke-broersma commented 4 years ago

Hi! This has been proposed before. We had some concerns about creating a mutator like this then, due to there being a high chance of creating mutations that are unkillable.

See: #211

Do you see a way to implement this mutator without introducing unkillable mutations?

raschmitt commented 4 years ago

@Myoxocephalus why do you think these mutants might be unkillable?

Just because of the complexity of testing static properties like dates?

I have created this package which makes it easier to mock dates.

https://github.com/raschmitt/date-override

rouke-broersma commented 4 years ago

See this comment on the previous issue.

For example a test that checks a method to see if it is monday. If we would mutate a date to be a week in the future, the test would still see that the date would be monday. But the test was correct. I guess we have to think real good before we define a mutator like this.

Originally posted by @richardwerkman in https://github.com/stryker-mutator/stryker-net/issues/211#issuecomment-434053767

This is just one example of course.

raschmitt commented 4 years ago

What about if we just mutated specific properties of DateTime.Now, by increasing / decreasing "one" of it, like this:

DateTime.Now --> Doesn't get mutated

DateTime.Now.Date --> DateTime.Now.Date.AddDays(1) && DateTime.Now.Date.AddDays(-1) DateTime.Now.Day --> DateTime.Now.Day + 1 && DateTime.Now.Day + 1 DateTime.Now.DayOfWeek --> DateTime.Now.AddDays(1).DayOfWeek && DateTime.Now.AddDays(1).DayOfWeek

And so on...

richardwerkman commented 4 years ago

This would be better. But in most cases I think the DayOfWeek or AddDays would be called on an existing datetime instead on DateTime.Now. And we currently cannot see the type of variables. So we can't know for sure if DayOfWeek is called on a Date or another object.

We could just mutate the methods anyway. If the method was from another object than Date we will get a compile error and Stryker will handle it well.

We could also add this mutator to a higher mutation level as proposed in #987. That way the mutator would not be enabled by default so we can experiment with this without having a too big impact.