Skip to content

Commit

Permalink
Fix the BehaviorStrict fixer with Mock.Of<T>() when using in construc…
Browse files Browse the repository at this point in the history
…tors (fixes #41).
  • Loading branch information
GillesTourreau committed Oct 24, 2024
1 parent cb128a3 commit 7595618
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/Moq.Analyzers/CodeFixes/SetBehaviorToStrictCodeFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,36 +42,37 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;

// Find the "ObjectCreationExpressionSyntax" or "InvocationExpression" in the parent of the location where is located the issue in the code.
var parent = root.FindToken(diagnosticSpan.Start).Parent;
// Gets the syntax node where is located the issue in the code.
var node = root.FindNode(diagnosticSpan, getInnermostNodeForTie: true);

if (parent is null)
if (node is null)
{
return;
}

var mockCreationExpression = parent.AncestorsAndSelf().OfType<ObjectCreationExpressionSyntax>().FirstOrDefault();

if (mockCreationExpression is null)
if (node is ObjectCreationExpressionSyntax mockCreationExpression)
{
var invocationExpression = parent.AncestorsAndSelf().OfType<InvocationExpressionSyntax>().First();

// Register a code to fix the enumeration.
context.RegisterCodeFix(
CodeAction.Create(
title: "Defines the MockBehavior to Strict",
createChangedDocument: cancellationToken => AddMockBehiavorStrictArgumentAsync(context.Document, invocationExpression, cancellationToken),
createChangedDocument: cancellationToken => AddMockBehiavorStrictArgumentAsync(context.Document, mockCreationExpression, cancellationToken),
equivalenceKey: "Defines the MockBehavior to Strict"),
diagnostic);

return;
}
else

if (node is InvocationExpressionSyntax invocationExpression)
{
// Register a code to fix the enumeration.
context.RegisterCodeFix(
CodeAction.Create(
title: "Defines the MockBehavior to Strict",
createChangedDocument: cancellationToken => AddMockBehiavorStrictArgumentAsync(context.Document, mockCreationExpression, cancellationToken),
createChangedDocument: cancellationToken => AddMockBehiavorStrictArgumentAsync(context.Document, invocationExpression, cancellationToken),
equivalenceKey: "Defines the MockBehavior to Strict"),
diagnostic);

return;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,65 @@ public interface I

await Verifier.VerifyCodeFixAsync(source, expectedFixedSource);
}

[Theory]
[InlineData("")]
[InlineData("MockBehavior.Loose")]
[InlineData("MockBehavior.Default")]
public async Task MockOf_InConstructor_Fix(string behavior)
{
var source = @"
namespace ConsoleApplication1
{
using Moq;
public class TestClass
{
public void TestMethod()
{
var mock = new C([|Mock.Of<I>(" + behavior + @")|], [|Mock.Of<I>(" + behavior + @")|]);
}
}
public interface I
{
}
public class C
{
public C(I r1, I r2)
{
}
}
}";

var expectedFixedSource =
@"
namespace ConsoleApplication1
{
using Moq;
public class TestClass
{
public void TestMethod()
{
var mock = new C(Mock.Of<I>(MockBehavior.Strict), Mock.Of<I>(MockBehavior.Strict));
}
}
public interface I
{
}
public class C
{
public C(I r1, I r2)
{
}
}
}";

await Verifier.VerifyCodeFixAsync(source, expectedFixedSource);
}
}
}

0 comments on commit 7595618

Please sign in to comment.