Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Moq;
using Shouldly;
using Stryker.Core.Mutants;
using Stryker.Core.Mutators;
using Stryker.Core.Options;
using Xunit;

namespace Stryker.Core.UnitTest.Mutators;

public class DefaultParameterMutatorTests
{
[Fact]
public void ShouldBeMutationLevelStandard()
{
var options = new StrykerOptions();
var orchestratorMock = new Mock<ICsharpMutantOrchestrator>();
var target = new DefaultParameterMutator(orchestratorMock.Object, options);
target.MutationLevel.ShouldBe(MutationLevel.Standard);
}

[Fact]
public void ShouldMutateDefaultParameter()
{
var source = @"
using System;

class Program
{
void Method()
{
DefaultParameterMethod();
}

void DefaultParameterMethod(string parameter = ""default"")
{
Console.WriteLine(parameter);
}
}"";
";
var syntaxTree = CSharpSyntaxTree.ParseText(source);
GetMutations(syntaxTree).ShouldHaveSingleItem();
}

[Fact]
public void ShouldNotMutateExplicitlyUsedDefaultParameter()
{
var source = @"
using System;

class Program
{
void Method()
{
DefaultParameterMethod(""hello world"");
}

void DefaultParameterMethod(string parameter = ""default"")
{
Console.WriteLine(parameter);
}
}"";
";
var syntaxTree = CSharpSyntaxTree.ParseText(source);
GetMutations(syntaxTree).ShouldBeEmpty();
}

[Fact]
public void ShouldNotMutateExplicitlyUsedNamedDefaultParameter()
{
var source = @"
using System;

class Program
{
void Method()
{
DefaultParameterMethod(parameter2: ""hello world"");
}

void DefaultParameterMethod(string parameter1 = ""default1"", string parameter2 = ""default2"", string parameter3 = ""default3"")
{
Console.WriteLine(parameter);
}
}"";
";
var syntaxTree = CSharpSyntaxTree.ParseText(source);
var mutations = GetMutations(syntaxTree);

mutations.Count().ShouldBe(2);
mutations.ShouldNotContain(x => x.DisplayName.Contains("parameter2"));
}

[Fact]
public void ShouldMutateImplicitlyUsedDefaultParameter()
{
var source = @"
using System;

class Program
{
void Method()
{
DefaultParameterMethod(""Hello world"");
}

void DefaultParameterMethod(string parameter1 = ""default"", string parameter2 = ""default2"", string parameter3 = ""default3"")
{
Console.WriteLine(parameter);
}
}"";
";
var syntaxTree = CSharpSyntaxTree.ParseText(source);

var mutations = GetMutations(syntaxTree);

mutations.Count().ShouldBe(2);
mutations.ShouldNotContain(x => x.DisplayName.Contains("parameter1"));
}

private IEnumerable<Mutation> GetMutations(SyntaxTree tree)
{
var invocations = tree
.GetRoot()
.DescendantNodes()
.OfType<InvocationExpressionSyntax>();
var options = new StrykerOptions()
{
MutationLevel = MutationLevel.Complete
};
var orchestratorMock = new Mock<ICsharpMutantOrchestrator>();
orchestratorMock.SetupGet(m => m.Mutators).Returns(new List<IMutator> { new StringMutator() });

var target = new DefaultParameterMutator(orchestratorMock.Object, options);

var semanticModel = GetSemanticModel(tree);

return invocations.SelectMany(invocation => target.ApplyMutations(invocation, semanticModel));
}

private static SemanticModel GetSemanticModel(SyntaxTree tree)
{
var compilation = CSharpCompilation.Create("Test")
.AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location))
.AddSyntaxTrees(tree);
return compilation.GetSemanticModel(tree);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void ShouldValidateExcludedMutation()

var ex = Should.Throw<InputException>(() => target.Validate<Mutator>());

ex.Message.ShouldBe($"Invalid excluded mutation (gibberish). The excluded mutations options are [Statement, Arithmetic, Block, Equality, Boolean, Logical, Assignment, Unary, Update, Checked, Linq, String, Bitwise, Initializer, Regex, NullCoalescing, Math]");
ex.Message.ShouldBe($"Invalid excluded mutation (gibberish). The excluded mutations options are [Statement, Arithmetic, Block, Equality, Boolean, Logical, Assignment, Unary, Update, Checked, Linq, String, Bitwise, Initializer, Regex, NullCoalescing, Math, DefaultParameter]");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

namespace Stryker.Core.Mutants
{
/// <inheritdoc/>
public class CsharpMutantOrchestrator : BaseMutantOrchestrator<SyntaxNode, SemanticModel>
/// <inheritdoc cref="BaseMutantOrchestrator{T,TY}" />
public class CsharpMutantOrchestrator : BaseMutantOrchestrator<SyntaxNode, SemanticModel>, ICsharpMutantOrchestrator
{
private readonly TypeBasedStrategy<SyntaxNode, INodeMutator> _specificOrchestrator =
new();
Expand Down Expand Up @@ -80,13 +80,16 @@ public CsharpMutantOrchestrator(MutantPlacer placer, IEnumerable<IMutator> mutat
});
}

private static List<IMutator> DefaultMutatorList() =>
public IEnumerable<IMutator> Mutators { get; }

private List<IMutator> DefaultMutatorList() =>
new()
{
// the default list of mutators
new BinaryExpressionMutator(),
new BlockMutator(),
new BooleanMutator(),
new DefaultParameterMutator(this, _options),
new AssignmentExpressionMutator(),
new PrefixUnaryMutator(),
new PostfixUnaryMutator(),
Expand All @@ -107,8 +110,6 @@ private static List<IMutator> DefaultMutatorList() =>
new IsPatternExpressionMutator()
};

private IEnumerable<IMutator> Mutators { get; }

public MutantPlacer Placer { get; }

/// <summary>
Expand Down
26 changes: 26 additions & 0 deletions src/Stryker.Core/Stryker.Core/Mutants/ICsharpMutantOrchestrator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Stryker.Core.Mutators;

namespace Stryker.Core.Mutants;

public interface ICsharpMutantOrchestrator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public interface ICsharpMutantOrchestrator
public interface ICSharpMutantOrchestrator

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original class is also called CsharpMutantOrchestrator..
Do you want me to keep it as is, or do you want me to rename both the interface and the class to CSharp...?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be best to fix the casing

{
IEnumerable<IMutator> Mutators { get; }
MutantPlacer Placer { get; }
bool MustInjectCoverageLogic { get; }
ICollection<Mutant> Mutants { get; set; }
int MutantCount { get; set; }

/// <summary>
/// Recursively mutates a single SyntaxNode
/// </summary>
/// <param name="input">The current root node</param>
/// <returns>Mutated node</returns>
SyntaxNode Mutate(SyntaxNode input, SemanticModel semanticModel);

/// <summary>
/// Gets the stored mutants and resets the mutant list to an empty collection
/// </summary>
IReadOnlyCollection<Mutant> GetLatestMutantBatch();
}
94 changes: 94 additions & 0 deletions src/Stryker.Core/Stryker.Core/Mutators/DefaultParameterMutator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Stryker.Core.Mutants;
using Stryker.Core.Options;

namespace Stryker.Core.Mutators;

public class DefaultParameterMutator : MutatorBase<InvocationExpressionSyntax>
{
private readonly ICsharpMutantOrchestrator _orchestrator;
private readonly StrykerOptions _options;

public DefaultParameterMutator(ICsharpMutantOrchestrator orchestrator, StrykerOptions options)
{
_orchestrator = orchestrator;
_options = options;
}

public override IEnumerable<Mutation> ApplyMutations(InvocationExpressionSyntax node, SemanticModel semanticModel)
{
var methodSymbol = (IMethodSymbol)semanticModel.GetSymbolInfo(node).Symbol;
if (methodSymbol is null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should check whether the symbol is internal or external. We might not want to mutate optional parameters of external APIs like System.Linq. @rouke-broersma what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how we would be able to detect that

{
yield break;
}
var parameterSymbols = methodSymbol.Parameters;

var parametersWithDefaultValues = parameterSymbols.Where(p => p.HasExplicitDefaultValue);

var overridenDefaultParameters = new List<IParameterSymbol>();
foreach (var argument in node.ArgumentList.Arguments)
{
if (argument.NameColon is not null)
{
overridenDefaultParameters.AddRange(parametersWithDefaultValues.Where(p => p.Name == argument.NameColon.Name.Identifier.ValueText));
}
else
{
overridenDefaultParameters.AddRange(parametersWithDefaultValues.Where(p => p.Ordinal == node.ArgumentList.Arguments.IndexOf(argument)));
}
}

var unoverridenDefaultParameters = parametersWithDefaultValues.Except(overridenDefaultParameters);

foreach (var parameter in unoverridenDefaultParameters)
{
var parameterName = parameter.Name;
var argumentNameColon = SyntaxFactory.NameColon(parameterName);
var parameterSyntaxNode = (ParameterSyntax)parameter.DeclaringSyntaxReferences[0].GetSyntax();

var parameterDefaultExpressionNode = parameterSyntaxNode.Default!.Value;
var mutatedDefaultValues = MutateDefaultValueNode(parameterDefaultExpressionNode, semanticModel);

foreach (var mutatedDefaultValue in mutatedDefaultValues)
{
var namedArgument = SyntaxFactory.Argument(nameColon: argumentNameColon, expression: mutatedDefaultValue, refKindKeyword: default);

var arguments = node.ArgumentList.Arguments.Add(namedArgument);
var newArgumentList = SyntaxFactory.ArgumentList(arguments);

yield return new Mutation
{
OriginalNode = node,
ReplacementNode = node.WithArgumentList(newArgumentList),
DisplayName = $"Default parameter mutation: {parameterName}",
Type = Mutator.DefaultParameter
};
}
}
}

public override MutationLevel MutationLevel => MutationLevel.Standard;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, I would place this at the top of the class, not here. Like in our other mutator classes. And I think we should debate whether this should be a level standard mutation. This could add a lot of mutations.


private IEnumerable<ExpressionSyntax> MutateDefaultValueNode(SyntaxNode defaultValueNode, SemanticModel semanticModel)
{
List<ExpressionSyntax> nodeMutations = new();
foreach (var mutator in _orchestrator.Mutators)
{
if (mutator is not DefaultParameterMutator)
{
var mutations = mutator.Mutate(defaultValueNode, semanticModel, _options);
foreach (var mutation in mutations)
{
nodeMutations.Add((ExpressionSyntax)mutation.ReplacementNode);
}
}
}

return nodeMutations;
}
}
4 changes: 3 additions & 1 deletion src/Stryker.Core/Stryker.Core/Mutators/Mutator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public enum Mutator
[MutatorDescription("Null coalescing")]
NullCoalescing,
[MutatorDescription("Math methods")]
Math
Math,
[MutatorDescription("Default parameter")]
DefaultParameter
}

public static class EnumExtension
Expand Down