-
Notifications
You must be signed in to change notification settings - Fork 32
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
Add analyzer and code fix for Is.EqualTo usage #146
Conversation
@mikkelbu |
Hi @Dreamescaper Your contribution is as usual much appreciated and I hope to take a look at it tomorrow or during the weekend. |
@mikkelbu |
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.
I still need to review the fixer and the two test files (also I would like to test this a little manually), but I don't have more time tonight. Here are my comments so far. It is mostly a lot of nitpick 😄.
documentation/NUnit2010.md
Outdated
|
||
## Description | ||
|
||
Using `Is.EqualTo` constraint will lead to better assertion messages in case of failure |
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.
Please add a concluding "."
documentation/NUnit2011.md
Outdated
|
||
## Description | ||
|
||
Using Is.Not.EqualTo constraint will lead to better assertion messages in case of failure |
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.
Please add a concluding ".".
documentation/NUnit2010.md
Outdated
@@ -0,0 +1,69 @@ | |||
# NUnit2010 | |||
## Use Is.EqualTo constraint |
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.
Please add a concluding ".".
documentation/NUnit2011.md
Outdated
@@ -0,0 +1,69 @@ | |||
# NUnit2011 | |||
## Use Is.Not.EqualTo constraint |
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.
Please add a concluding ".".
@@ -0,0 +1,12 @@ | |||
namespace NUnit.Analyzers.Constants | |||
{ | |||
internal static class EqualToConstraintUsageConstants |
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.
Please add concluding "." to all texts below.
equalToDescriptor, notEqualToDescriptor); | ||
|
||
|
||
protected override void AnalyzeAssertInvocation(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax assertExpression, IMethodSymbol methodSymbol) |
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.
Can we break this line, as it is quite long.
|
||
if (SupportedNegativeAssertMethods.Contains(methodSymbol.Name)) | ||
{ | ||
negated = !negated; |
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.
I know that the effect is the same, but I would rather have true
instead of !negated
, so I don't have to look up to figure out what the value of negated
.
{ | ||
return; | ||
} | ||
else if (memberAccess.Name.Identifier.Text == NunitFrameworkConstants.NameOfIsFalse) |
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.
I think that memberAccess
can be null.
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.
Not sure. Why? It is checked in 'if' above
|
||
if (shouldReport && !negated) | ||
{ | ||
context.ReportDiagnostic(Diagnostic.Create( |
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.
I think this can be written on one line (and similarly below)
{ | ||
shouldReport = true; | ||
} | ||
else if (actual.IsKind(SyntaxKind.NotEqualsExpression) |
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.
I think extracting some of the expressions into variables and/or perhaps adding "()" would help a lot on the readability.
@mikkelbu Updated PR based on your review. I'm also not sure about two separate descriptors anymore. It would require all condition analyzers (i.e. to replace bool assert with constraints, I think there might be a lot of them) to do same, which would be a bit of a pain. Wdyt? |
@Dreamescaper I had time to sleep, but not enough 😄. I have thought a bit about two separate descriptors vs. a single descriptor with parameterized message. Do I understand the problem correctly that you want to avoid the duplication in defining two almost identical descriptors. As far as I can tell we will still need to have the same logic and tests as we have now to determine which message to show, but is there more (duplication) that I overlook? The problem is not only the documentation and the documentation tests, but also VS as it will only then show one entry as there is only one descriptor, so it will not be possible to distinguish the two "cases", e.g. to enable one and disable another, and the title must be the same (and more general). Given the above I think I prefer to have two separate descriptors, but I feel that I might overlook something. |
Mostly yes. Also, similar analyzers, that could be added in future (e.g. for string methods Contains, StartsWith, EndsWith etc), would have to do same.
I'd say it's actually better. I highly doubt that someone will disable Is.EqualTo analyzer, but leave Is.Not.EqualTo enabled intentionally. Anyway, I'm fine with both ways. |
Then let us go with the one analyzer approach. With respect to the parameterized message, then we don't show that in the documentation, so I don't think that would be a problem. |
Ok, will update PR later then.
Not sure I understand this part. Could you clarify? What we don't show in documentation? |
For instance the message(format) of But perhaps we should also show the message(format) at some point. |
Updated PR to use single parameterized descriptor. Previously I checked exact descriptorId in CodeFixProvider to understand whether expresion is negated, now I put Properties dictionary with suggested constraint to Diagnostic for this purpose. |
Thanks @Dreamescaper. It looks good I'll be merging the contribution now. Sorry for the long delay, but I've not had enough time to keep up with all the NUnit work. |
PR adds analyzer and code fix for cases, when comparison via
==
or!=
operators or via.Equals
method is used instead ofIs.EqualTo
orIs.Not.EqualTo
constraints.