stryker-mutator / stryker-js

Mutation testing for JavaScript and friends
https://stryker-mutator.io
Apache License 2.0
2.58k stars 248 forks source link

Add mutation for date operation #4917

Open boretti opened 1 month ago

boretti commented 1 month ago

Is your feature request related to a problem? Please describe. It would be interesting to have mutation produced for operation on Date.

Expression like :

aDate.setDate(aDate.getDate()+1)

will produce interesting mutation based on the +1. It would also be interesting to have mutation changing the setDate or getDate operation (as updating month and not the day of month).

Describe the solution you'd like I propose to add the following mutation (to method-expression-mutator) :

Describe alternatives you've considered

Maybe other method are interesting to be mutated, but I think, a first set to start with is enough

Additional context

I will also create a PR with the proposal and link it with this Feature Request.

nicojs commented 1 month ago

This sounds like a great addition and is right in the spirit of what the method expression mutator is supposed to do. However, I don't see why we should stop at these four. For some use cases, people might be more interested in smaller or larger units of time.

This would be my suggestion.

From To
Date.prototype.setDate() Date.prototype.setTime()
Date.prototype.setFullYear() Date.prototype.setMonth()
Date.prototype.setMonth() Date.prototype.setFullYear()
Date.prototype.setHours() Date.prototype.setMinutes()
Date.prototype.setMinutes() Date.prototype.setHours()
Date.prototype.setSeconds() Date.prototype.setMilliseconds()
Date.prototype.setMilliseconds() Date.prototype.setSeconds()
Date.prototype.setTime() Date.prototype.setDate()
Date.prototype.setUTCDate() Date.prototype.setTime()
Date.prototype.setUTCFullYear() Date.prototype.setUTCMonth()
Date.prototype.setUTCMonth() Date.prototype.setUTCFullYear()
Date.prototype.setUTCHours() Date.prototype.setUTCMinutes()
Date.prototype.setUTCMinutes() Date.prototype.setUTCHours()
Date.prototype.setUTCSeconds() Date.prototype.setUTCMilliseconds()
Date.prototype.setUTCMilliseconds() Date.prototype.setUTCSeconds()

What do you think?

boretti commented 1 month ago

I think this is a good idea to have all these methods supported. I would also support both the get and the set version of the method.

nicojs commented 1 month ago

I'm personally less in favor of getxxx mutations. Could you provide an example where it would be useful and is not covered by a setXxx mutation?

boretti commented 1 month ago

I have the following example for get mutation, which are mostly business rules based on the date or the time :

nicojs commented 1 month ago

Thanks for the examples. I these particular examples are already tackled by the equality operator group of mutants, correct?

The problem with adding mutants for all methods is that they often overlap with other mutants that we already have. You probably call getHours() to calculate something or call setHours on another Date. The calculations are already covered by existing mutators. Your PR will cover the setHours.

Can you agree with this?

boretti commented 1 month ago

I see your point.

The equality operator should cover these case. I will update the PR according.