-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mutator): Add conditional operator mutator (#2583)
* feat(Mutators): Add conditional operator mutator Add conditional operator mutator Add tests for conditional operator mutator Include conditional operator mutator in mutations.md * Fixes to unit tests * feat(Mutators): Remove NegateConditionMutator's conditional expression mutation * Fix integration tests * Fix last integration test * ConditionalExpressionMutator should not mutate declaration patterns. Fix tests. * Fix ValidateStrykerResults compilation. --------- Co-authored-by: Michał Isalski <[email protected]> Co-authored-by: Liam Rougoor <[email protected]> Co-authored-by: Liam Rougoor <[email protected]> Co-authored-by: Rouke Broersma <[email protected]>
- Loading branch information
1 parent
c68c46c
commit 6556ab9
Showing
10 changed files
with
185 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/Stryker.Core/Stryker.Core.UnitTest/Mutators/ConditionalExpressionMutatorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Shouldly; | ||
using Stryker.Core.Mutators; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Xunit; | ||
|
||
namespace Stryker.Core.UnitTest.Mutators | ||
{ | ||
public class ConditionalExpressionMutatorTests : TestBase | ||
{ | ||
[Fact] | ||
public void ShouldBeMutationLevelStandard() | ||
{ | ||
var target = new ConditionalExpressionMutator(); | ||
target.MutationLevel.ShouldBe(MutationLevel.Standard); | ||
} | ||
|
||
[Fact] | ||
public void ShouldMutate_TwoMutations() | ||
{ | ||
var target = new ConditionalExpressionMutator(); | ||
var source = @"251 == 73 ? 1 : 0"; | ||
var tree = CSharpSyntaxTree.ParseText(source); | ||
var originalNode = tree.GetRoot().DescendantNodes().OfType<ConditionalExpressionSyntax>().Single(); | ||
|
||
var result = target.ApplyMutations(originalNode, null).ToList(); | ||
|
||
result.Count.ShouldBe(2, "Two mutations should have been made"); | ||
Assert.Collection( | ||
result, | ||
m1 => (m1.ReplacementNode is ParenthesizedExpressionSyntax pes && pes.Expression is ConditionalExpressionSyntax ces && ces.Condition.Kind() is SyntaxKind.TrueLiteralExpression).ShouldBeTrue(), | ||
m2 => (m2.ReplacementNode is ParenthesizedExpressionSyntax pes && pes.Expression is ConditionalExpressionSyntax ces && ces.Condition.Kind() is SyntaxKind.FalseLiteralExpression).ShouldBeTrue() | ||
); | ||
} | ||
|
||
[Fact] | ||
public void ShouldMutate_DoNotTouchBranches() | ||
{ | ||
var target = new ConditionalExpressionMutator(); | ||
var source = "251 == 73 ? 1 : 0"; | ||
var tree = CSharpSyntaxTree.ParseText(source); | ||
var originalNode = tree.GetRoot().DescendantNodes().OfType<ConditionalExpressionSyntax>().Single(); | ||
|
||
var result = target.ApplyMutations(originalNode, null).ToList(); | ||
|
||
foreach (var mutation in result) | ||
{ | ||
var pes = mutation.ReplacementNode.ShouldBeOfType<ParenthesizedExpressionSyntax>(); | ||
var ces = pes.Expression.ShouldBeOfType<ConditionalExpressionSyntax>(); | ||
ces.WhenTrue.IsEquivalentTo(originalNode.WhenTrue).ShouldBeTrue(); | ||
ces.WhenFalse.IsEquivalentTo(originalNode.WhenFalse).ShouldBeTrue(); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ShouldNotMutateDeclarationPatterns() | ||
{ | ||
var target = new ConditionalExpressionMutator(); | ||
var source = "var y = x is object result ? result.ToString() : null;"; | ||
SyntaxTree tree = CSharpSyntaxTree.ParseText(source); | ||
|
||
var expressionSyntax = tree.GetRoot().DescendantNodes().OfType<ConditionalExpressionSyntax>().Single(); | ||
var result = target.ApplyMutations(expressionSyntax, null).ToList(); | ||
|
||
result.ShouldBeEmpty(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.