-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(mutator): Add string method mutators #2904
feat(mutator): Add string method mutators #2904
Conversation
Thanks for this PR. This is a great stab at this.
|
Thanks for the improved version. new()
{
OriginalNode = node,
ReplacementNode = node.ReplaceNode(
member.Name,
SyntaxFactory.IdentifierName(replacement)
) I think it would be improved this way: new()
{
OriginalNode = node,
ReplacementNode = node.WithName(
SyntaxFactory.IdentifierName(replacement))
) I see two benefits:
About 5: Either way, this seems like a good mutation to try. Is it clear? |
I got 3 now, I was a bit confused since I thought the WithName method would be on the
Does this not check if the expression was called on a string type? private static bool IsOperationOnAString(ExpressionSyntax expression, SemanticModel model)
{
var typeInfo = model.GetTypeInfo(expression);
return typeInfo.Type?.SpecialType == SpecialType.System_String;
} I did try what you said with the PadLeft method; [Fact]
public void ShouldNotMutateMethodsWithSameName()
{
var syntaxTree = CSharpSyntaxTree.ParseText(
"""
class Padder {
public string PadLeft(string org) => " " + org;
}
class TestClass {
private Padder padder = new Padder();
void TestMethod() {
var padded = padder.PadLeft("test");
}
}
""");
var compilation = CSharpCompilation.Create("TestAssembly")
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location))
.AddSyntaxTrees(syntaxTree);
var semanticModel = compilation.GetSemanticModel(syntaxTree);
var expression = syntaxTree.GetRoot().DescendantNodes().OfType<InvocationExpressionSyntax>().First();
var target = new StringMethodMutator();
var result = target.ApplyMutations(expression, semanticModel).ToList();
result.ShouldBeEmpty();
} And this test passes. |
I'd like to see at least a few test cases in Our integration tests also won't pick this up. |
A good test would also be to add this line to the integration test project: |
Added mutators for string methods.
Closes #2868