Multiply change into Divide #1856
-
I have the following code condition ? request.Amount.Value : request.Amount.Value * -1 Styker mutates it to condition ? request.Amount.Value : request.Amount.Value / -1 But mathematically, correct if I am wrong, they are the same var r = new Random();
Console.WriteLine(string.Join("\n", Enumerable
.Range(1, 20)
.Select(_ => r.Next(minValue: -20, maxValue: 20))
.Select(x => $"Original: {x}, changed: {x * -1} @ {x / -1}"))) So, does make sense that mutation be executed in that specific scenario? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Good question. AnswerYour question is more theoretical than practical: it does not make sense to test this mutant: if there is no change in the program behavior, this is not an actual mutant, just a source code change. But, from a practical standpoint, it is normal and expected: Stryker.Net tests every source code mutation it generates. There is a filtering logic that removes mutations that are duplicates at the source code level, but there is no attempt to detect semantic duplicates. How to avoid ?When you have mutations that do not change the program behavior, this is a hint the code may be simpler. Here you could use: condition ? request.Amount.Value : -request.Amount.Value Why Stryker will not detect this?Adding the necessary logic to detect this specific case (* or / -1) to Stryker will result in a slightly slower mutation generation and a supplemental maintenance burden. Also, this code would still exhibit the same problem:
Last resortOf course, there are situations when the source code cannot be changed in order to prevent semantically equivalent mutant. For those cases, you can prevent the mutation from being tested at the source code level, via Stryker specific comments. See Stryker comments. |
Beta Was this translation helpful? Give feedback.
Good question.
Answer
Your question is more theoretical than practical: it does not make sense to test this mutant: if there is no change in the program behavior, this is not an actual mutant, just a source code change.
But, from a practical standpoint, it is normal and expected: Stryker.Net tests every source code mutation it generates. There is a filtering logic that removes mutations that are duplicates at the source code level, but there is no attempt to detect semantic duplicates.
How to avoid ?
When you have mutations that do not change the program behavior, this is a hint the code may be simpler. Here you could use:
Why Stryk…