Skip to content

Commit

Permalink
misc: remove syntax parsing from ispattermutator, removed switchexpre…
Browse files Browse the repository at this point in the history
…ssionmutator and replace it with two extra mutators, add semancticinfo to unit tests
  • Loading branch information
dupdob committed Nov 20, 2024
1 parent cc16177 commit 06d1655
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 475 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis.CSharp;
using Stryker.Abstractions.Mutants;
using Stryker.Abstractions.Mutators;
using Stryker.Abstractions;
using Stryker.Core.Mutants;
using Stryker.Core.InjectedHelpers;
using Stryker.Abstractions.Options;
using System.Linq.Expressions;

namespace Stryker.Core.UnitTest.Mutants;

Expand All @@ -25,34 +24,37 @@ public class MutantOrchestratorTestsBase : TestBase

protected void ShouldMutateSourceToExpected(string actual, string expected)
{
var actualNode = Target.Mutate(CSharpSyntaxTree.ParseText(actual), null);
var syntaxTree = CSharpSyntaxTree.ParseText(actual);

var compilation = CSharpCompilation.Create(null)
.AddSyntaxTrees(syntaxTree);
var model = compilation.GetSemanticModel(syntaxTree);
var actualNode = Target.Mutate(syntaxTree, model);
actual = actualNode.GetRoot().ToFullString();
actual = actual.Replace(Injector.HelperNamespace, "StrykerNamespace");
actualNode = CSharpSyntaxTree.ParseText(actual);
actualNode.ShouldNotContainErrors();
var expectedNode = CSharpSyntaxTree.ParseText(expected);
actualNode.ShouldBeSemantically(expectedNode);
actualNode.ShouldNotContainErrors();
}

protected void ShouldMutateSourceInClassToExpected(string actual, string expected)
{
actual = @"using System;
using System.Collections.Generic;
using System.Text;
namespace StrykerNet.UnitTest.Mutants.TestResources
{
class TestClass
{" + actual + @"}
}";
namespace StrykerNet.UnitTest.Mutants.TestResources;
class TestClass
{" + actual + @"}
";

expected = @"using System;
using System.Collections.Generic;
using System.Text;
namespace StrykerNet.UnitTest.Mutants.TestResources
{
class TestClass
{" + expected + @"}
}";
namespace StrykerNet.UnitTest.Mutants.TestResources;
class TestClass
{" + expected + @"}
";
ShouldMutateSourceToExpected(actual, expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public void ShouldMutateIsToIsNot()

var mutation = target.ApplyMutations(expression, null).First();

mutation.OriginalNode.ShouldBeOfType<ConstantPatternSyntax>();
mutation.ReplacementNode.ShouldBeOfType<UnaryPatternSyntax>();
mutation.OriginalNode.ToString().ShouldBe("1 is 1");
mutation.ReplacementNode.ToString().ShouldBe("1 is not 1");
mutation.DisplayName.ShouldBe("Equality mutation");
}

Expand All @@ -35,8 +35,8 @@ public void ShouldMutateIsNotToIs()

var mutation = target.ApplyMutations(expression, null).First();

mutation.OriginalNode.ShouldBeOfType<UnaryPatternSyntax>();
mutation.ReplacementNode.ShouldBeOfType<ConstantPatternSyntax>();
mutation.OriginalNode.ToString().ShouldBe("1 is not 1");
mutation.ReplacementNode.ToString().ShouldBe("1 is 1");
mutation.DisplayName.ShouldBe("Equality mutation");
}

Expand All @@ -47,11 +47,11 @@ public void ShouldMutateIsNotToIs()
[DataRow("<=", new[] { SyntaxKind.GreaterThanToken, SyntaxKind.LessThanToken })]
public void ShouldMutateRelationalPattern(string @operator, SyntaxKind[] mutated)
{
var target = new IsPatternExpressionMutator();
var target = new RelationalPatternMutator();

var expression = GenerateWithRelationalPattern(@operator);
var expression = GenerateWithRelationalPattern(@operator).DescendantNodes().OfType<RelationalPatternSyntax>().First();

var result = target.ApplyMutations(expression, null).Skip(1).ToList();
var result = target.ApplyMutations(expression, null).ToList();

result.ForEach(mutation =>
{
Expand All @@ -71,11 +71,11 @@ public void ShouldMutateRelationalPattern(string @operator, SyntaxKind[] mutated
[DataRow("or", new[] { SyntaxKind.AndPattern })]
public void ShouldMutateLogicalPattern(string @operator, SyntaxKind[] mutated)
{
var target = new IsPatternExpressionMutator();
var target = new BinaryPatternMutator();

var expression = GenerateWithBinaryPattern(@operator);

var result = target.ApplyMutations(expression, null).Skip(1).ToList();
var result = target.ApplyMutations(expression, null).ToList();

result.ForEach(mutation =>
{
Expand Down Expand Up @@ -112,7 +112,7 @@ class Program
{{
static void Main(string[] args)
{{
var a = 1 is {(isNotPattern ? "not" : string.Empty)} 1;
var a = 1 is{(isNotPattern ? " not" : string.Empty)} 1;
}}
}}
}}");
Expand Down Expand Up @@ -147,7 +147,7 @@ static void Main(string[] args)
return isPatternExpression;
}

private IsPatternExpressionSyntax GenerateWithBinaryPattern(string pattern)
private BinaryPatternSyntax GenerateWithBinaryPattern(string pattern)
{
var tree = CSharpSyntaxTree.ParseText($@"
using System;
Expand All @@ -164,7 +164,7 @@ static void Main(string[] args)
}}");
var isPatternExpression = tree.GetRoot()
.DescendantNodes()
.OfType<IsPatternExpressionSyntax>()
.OfType<BinaryPatternSyntax>()
.Single();

return isPatternExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void ShouldMutateStringMethods(string expression, string mutatedMethod, s
{
var (semanticModel, expressionSyntax) = CreateSemanticModelFromExpression(expression);
var target = new StringMethodMutator();
var result = target.ApplyMutations(expressionSyntax, semanticModel).ToList();
var result = target.ApplyMutations((MemberAccessExpressionSyntax)expressionSyntax.Expression, semanticModel).ToList();

var mutation = result.ShouldHaveSingleItem();

Expand All @@ -83,7 +83,7 @@ public void ShouldMutateReplaceWithEmptyString(string methodName)
{
var expression = $"testString.{methodName}()";
var (semanticModel, expressionSyntax) = CreateSemanticModelFromExpression(expression);
var target = new StringMethodMutator();
var target = new StringMethodToConstantMutator();
var result = target.ApplyMutations(expressionSyntax, semanticModel).ToList();

var mutation = result.ShouldHaveSingleItem();
Expand All @@ -101,7 +101,7 @@ public void ShouldMutateReplaceWithChar(string methodName)
{
var expression = $"testString.{methodName}()";
var (semanticModel, expressionSyntax) = CreateSemanticModelFromExpression(expression);
var target = new StringMethodMutator();
var target = new StringMethodToConstantMutator();
var result = target.ApplyMutations(expressionSyntax, semanticModel).ToList();

var mutation = result.ShouldHaveSingleItem();
Expand All @@ -118,7 +118,7 @@ public void ShouldNotMutateWhenNotAString()
var expression = (InvocationExpressionSyntax)
SyntaxFactory.ParseExpression("Enumerable.Max(new[] { 1, 2, 3 })");
var target = new StringMethodMutator();
var result = target.ApplyMutations(expression, null).ToList();
var result = target.ApplyMutations((MemberAccessExpressionSyntax)expression.Expression, null).ToList();

result.ShouldBeEmpty();
}
Expand Down
Loading

0 comments on commit 06d1655

Please sign in to comment.