-
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 1 commit
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 @@ | |||||||||||||
| 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 @@ | |||||||||||||
| nodesToReplace.AddRange(clearReplacements.Keys); | ||||||||||||||
| nodesToReplace.AddRange(raiseReplacements.Keys); | ||||||||||||||
| nodesToReplace.AddRange(whenDoReplacements.Keys); | ||||||||||||||
| nodesToReplace.AddRange(propertyVerifyReplacements.Keys); | ||||||||||||||
|
|
||||||||||||||
| compilationUnit = compilationUnit.ReplaceNodes( | ||||||||||||||
| nodesToReplace, | ||||||||||||||
|
|
@@ -116,10 +120,17 @@ | |||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| 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 @@ | |||||||||||||
| 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,124 @@ | |||||||||||||
| 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; | |
| } |
Check warning on line 651 in Source/Mockolate.Migration.Analyzers.CodeFixers/NSubstituteCodeFixProvider.cs
SonarQubeCloud / SonarCloud Code Analysis
Method has 8 parameters, which is greater than the 7 authorized.
See more on https://sonarcloud.io/project/issues?id=aweXpect_Mockolate.Migration&issues=AZ3jajBhIQccHbajVlt2&open=AZ3jajBhIQccHbajVlt2&pullRequest=51
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.