Skip to content

Commit

Permalink
feat: ignore more nunit assert methods (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
Meir017 authored May 23, 2024
1 parent 6037ed1 commit 50e5632
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 41 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,35 @@ namespace FluentAssertions.Analyzers.Tests.Tips;
[TestClass]
public class NunitTests
{
#region Assert.cs

[DataTestMethod]
[AssertionDiagnostic("Assert.Pass({0});")]
[AssertionDiagnostic("Assert.Inconclusive({0});")]
[AssertionDiagnostic("Assert.Ignore({0});")]
[Implemented]
public void Nunit3_NotReportedAsserts_TestAnalyzer(string assertion) => Nunit3VerifyNoDiagnostic(string.Empty, assertion);

[DataTestMethod]
[DataRow("Assert.Warn(\"warning message\");")]
[DataRow("Assert.Warn(\"warning message {0} and more.\", DateTime.Now);")]
[Implemented]
public void Nunit3_SpecialNotReportedAsserts_TestAnalyzer(string assertion) => Nunit3VerifyNoDiagnostic(string.Empty, assertion);

[DataTestMethod]
[DataRow("Assert.Pass();")]
[DataRow("Assert.Pass(\"passing message\");")]
[DataRow("Assert.Inconclusive();")]
[DataRow("Assert.Inconclusive(\"inconclusive message\");")]
[DataRow("Assert.Ignore();")]
[DataRow("Assert.Ignore(\"ignore message\");")]
[DataRow("Assert.Warn(\"warning message\");")]
[DataRow("Assert.Charlie();")]
[Implemented]
public void Nunit4_SpecialNotReportedAsserts_TestAnalyzer(string assertion) => Nunit4VerifyNoDiagnostic(string.Empty, assertion);

#endregion

#region Assert.Conditions.cs

[DataTestMethod]
Expand Down Expand Up @@ -1660,13 +1689,17 @@ public void Nunit4_CollectionAssertDoesNotContain_WithCasting_TestCodeFix(string

private void Nunit3VerifyDiagnostic(string methodArguments, string assertion)
=> VerifyDiagnostic(GenerateCode.Nunit3Assertion(methodArguments, assertion), PackageReference.Nunit_3_14_0);
private void Nunit3VerifyNoDiagnostic(string methodArguments, string assertion)
=> VerifyNoDiagnostic(GenerateCode.Nunit3Assertion(methodArguments, assertion), PackageReference.Nunit_3_14_0);
private void Nunit3VerifyFix(string methodArguments, string oldAssertion, string newAssertion)
=> VerifyFix(GenerateCode.Nunit3Assertion(methodArguments, oldAssertion), GenerateCode.Nunit3Assertion(methodArguments, newAssertion), PackageReference.Nunit_3_14_0);
private void Nunit3VerifyNoFix(string methodArguments, string assertion)
=> VerifyNoFix(GenerateCode.Nunit3Assertion(methodArguments, assertion), PackageReference.Nunit_3_14_0);

private void Nunit4VerifyDiagnostic(string methodArguments, string assertion)
=> VerifyDiagnostic(GenerateCode.Nunit4Assertion(methodArguments, assertion), PackageReference.Nunit_4_0_1);
private void Nunit4VerifyNoDiagnostic(string methodArguments, string assertion)
=> VerifyNoDiagnostic(GenerateCode.Nunit4Assertion(methodArguments, assertion), PackageReference.Nunit_4_0_1);
private void Nunit4VerifyFix(string methodArguments, string oldAssertion, string newAssertion)
=> VerifyFix(GenerateCode.Nunit4Assertion(methodArguments, oldAssertion), GenerateCode.Nunit4Assertion(methodArguments, newAssertion), PackageReference.Nunit_4_0_1);
private void Nunit4VerifyNoFix(string methodArguments, string assertion)
Expand Down Expand Up @@ -1709,4 +1742,12 @@ private void VerifyNoFix(string source, PackageReference nunit)
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0, nunit)
);
}
private void VerifyNoDiagnostic(string source, PackageReference nunit)
{
DiagnosticVerifier.VerifyDiagnostic(new DiagnosticVerifierArguments()
.WithAllAnalyzers()
.WithSources(source)
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0, nunit)
);
}
}
2 changes: 1 addition & 1 deletion src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void AnalyzeNunitInvocation(OperationAnalysisContext context)
var op = (IInvocationOperation)context.Operation;
if (IsNunitAssertClass(op.TargetMethod.ContainingType) && !IsMethodExcluded(context.Options, op))
{
if (op.TargetMethod.Name is "Inconclusive" or "Ignore" && op.TargetMethod.ContainingType.EqualsSymbol(_nunitAssertSymbol))
if (op.TargetMethod.Name is "Inconclusive" or "Ignore" or "Pass" or "Warn" or "Charlie" && op.TargetMethod.ContainingType.EqualsSymbol(_nunitAssertSymbol))
return;

context.ReportDiagnostic(Diagnostic.Create(NUnitRule, op.Syntax.GetLocation()));
Expand Down

0 comments on commit 50e5632

Please sign in to comment.