-
Notifications
You must be signed in to change notification settings - Fork 0
feat: migrate NSubstitute property Received gets and sets #51
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -68,6 +68,9 @@ protected override async Task<Document> ConvertAssertionAsync(CodeFixContext con | |||||||||||||
| Dictionary<InvocationExpressionSyntax, InvocationExpressionSyntax> verifyReplacements = | ||||||||||||||
| FindAndBuildVerifyReplacements(allInvocations, semanticModel, mockSymbol, cancellationToken); | ||||||||||||||
|
|
||||||||||||||
| Dictionary<AssignmentExpressionSyntax, InvocationExpressionSyntax> propertyVerifyReplacements = | ||||||||||||||
| FindAndBuildPropertyVerifyReplacements(compilationUnit, semanticModel, mockSymbol, cancellationToken); | ||||||||||||||
|
|
||||||||||||||
| Dictionary<InvocationExpressionSyntax, InvocationExpressionSyntax> clearReplacements = | ||||||||||||||
| FindAndBuildClearReceivedCallsReplacements(allInvocations, semanticModel, mockSymbol, cancellationToken); | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -83,6 +86,7 @@ protected override async Task<Document> ConvertAssertionAsync(CodeFixContext con | |||||||||||||
| nodesToReplace.AddRange(clearReplacements.Keys); | ||||||||||||||
| nodesToReplace.AddRange(raiseReplacements.Keys); | ||||||||||||||
| nodesToReplace.AddRange(whenDoReplacements.Keys); | ||||||||||||||
| nodesToReplace.AddRange(propertyVerifyReplacements.Keys); | ||||||||||||||
|
|
||||||||||||||
| compilationUnit = compilationUnit.ReplaceNodes( | ||||||||||||||
| nodesToReplace, | ||||||||||||||
|
|
@@ -116,10 +120,17 @@ protected override async Task<Document> ConvertAssertionAsync(CodeFixContext con | |||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if (original is AssignmentExpressionSyntax assignment && | ||||||||||||||
| raiseReplacements.TryGetValue(assignment, out InvocationExpressionSyntax? raiseReplacement)) | ||||||||||||||
| if (original is AssignmentExpressionSyntax assignment) | ||||||||||||||
| { | ||||||||||||||
| return raiseReplacement; | ||||||||||||||
| if (raiseReplacements.TryGetValue(assignment, out InvocationExpressionSyntax? raiseReplacement)) | ||||||||||||||
| { | ||||||||||||||
| return raiseReplacement; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if (propertyVerifyReplacements.TryGetValue(assignment, out InvocationExpressionSyntax? propVerifyReplacement)) | ||||||||||||||
| { | ||||||||||||||
| return propVerifyReplacement; | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return original; | ||||||||||||||
|
|
@@ -132,7 +143,7 @@ protected override async Task<Document> ConvertAssertionAsync(CodeFixContext con | |||||||||||||
| compilationUnit = compilationUnit.AddUsings(usingDirective); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if (verifyReplacements.Count > 0) | ||||||||||||||
| if (verifyReplacements.Count > 0 || propertyVerifyReplacements.Count > 0) | ||||||||||||||
| { | ||||||||||||||
| bool hasVerifyUsing = compilationUnit.Usings.Any(u => u.Name?.ToString() == "Mockolate.Verify"); | ||||||||||||||
| if (!hasVerifyUsing) | ||||||||||||||
|
|
@@ -577,6 +588,167 @@ private static Dictionary<InvocationExpressionSyntax, InvocationExpressionSyntax | |||||||||||||
| return result; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /// <summary> | ||||||||||||||
| /// Translates property-style verifications: | ||||||||||||||
| /// <list type="bullet"> | ||||||||||||||
| /// <item><c>_ = sub.Received().Prop</c> → <c>sub.Mock.Verify.Prop.Got().AtLeastOnce()</c></item> | ||||||||||||||
| /// <item><c>sub.Received().Prop = v</c> → <c>sub.Mock.Verify.Prop.Set(v).AtLeastOnce()</c></item> | ||||||||||||||
| /// </list> | ||||||||||||||
| /// <c>Received(n)</c> / <c>DidNotReceive()</c> map to <c>Exactly(n)</c>/<c>Once()</c>/<c>Never()</c> in the same way as method-style. | ||||||||||||||
| /// </summary> | ||||||||||||||
| private static Dictionary<AssignmentExpressionSyntax, InvocationExpressionSyntax> FindAndBuildPropertyVerifyReplacements( | ||||||||||||||
| CompilationUnitSyntax compilationUnit, | ||||||||||||||
| SemanticModel? semanticModel, | ||||||||||||||
| ISymbol? mockSymbol, | ||||||||||||||
| CancellationToken cancellationToken) | ||||||||||||||
| { | ||||||||||||||
| if (semanticModel is null || mockSymbol is null) | ||||||||||||||
| { | ||||||||||||||
| return []; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| Dictionary<AssignmentExpressionSyntax, InvocationExpressionSyntax> result = []; | ||||||||||||||
|
|
||||||||||||||
| foreach (AssignmentExpressionSyntax assignment in compilationUnit.DescendantNodes().OfType<AssignmentExpressionSyntax>()) | ||||||||||||||
| { | ||||||||||||||
| if (!assignment.IsKind(SyntaxKind.SimpleAssignmentExpression)) | ||||||||||||||
| { | ||||||||||||||
| continue; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
||||||||||||||
| if (assignment.Parent is not ExpressionStatementSyntax) | |
| { | |
| continue; | |
| } |
Copilot
AI
May 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TryExtractReceivedPropertyAccess only checks the syntax shape sub.Received().X but doesn’t confirm that X resolves to an actual property. This can mis-rewrite cases like method-group accesses (e.g., _ = sub.Received().SomeMethod;) into Verify.SomeMethod.Got() which likely won’t compile. Consider using the semantic model to ensure propertyAccess (or propertyAccess.Name) binds to an IPropertySymbol before treating it as a property verification.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The XML doc example for property sets shows
sub.Mock.Verify.Prop.Set(v)..., but the implementation wraps non-matcher values inIt.Is(...)(e.g.,Set(It.Is("x"))). Consider updating the example/comment to reflect the actual output so the documentation matches behavior.