Skip to content

feat: ignore more nunit assert methods #354

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -37,7 +37,7 @@
{
public static readonly string Message = "Use FluentAssertions equivalent";
public static readonly DiagnosticDescriptor XunitRule = new(
"FAA0002",

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / Performance regression check

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Check warning on line 40 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

title: "Replace Xunit assertion with Fluent Assertions equivalent",
messageFormat: Message,
description: "",
Expand All @@ -46,7 +46,7 @@
isEnabledByDefault: true);

public static readonly DiagnosticDescriptor MSTestsRule = new(
"FAA0003",

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / Performance regression check

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Check warning on line 49 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

title: "Replace MSTests assertion with Fluent Assertions equivalent",
messageFormat: Message,
description: "",
Expand All @@ -55,7 +55,7 @@
isEnabledByDefault: true);

public static readonly DiagnosticDescriptor NUnitRule = new(
"FAA0004",

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / Performance regression check

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Check warning on line 58 in src/FluentAssertions.Analyzers/Tips/AssertAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

title: "Replace NUnit assertion with Fluent Assertions equivalent",
messageFormat: Message,
description: "",
Expand Down Expand Up @@ -180,7 +180,7 @@
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
Loading