Skip to content

Commit

Permalink
feat: add nunit null assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Meir017 committed Jan 17, 2024
1 parent 7d4efb6 commit bdcf74e
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 20 deletions.
21 changes: 21 additions & 0 deletions src/FluentAssertions.Analyzers.TestUtils/GenerateCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,5 +254,26 @@ public static string Nunit3Assertion(string methodArguments, string assertion) =
.AppendLine(" }")
.AppendLine("}")
.ToString();

public static string Nunit4Assertion(string methodArguments, string assertion) => new StringBuilder()
.AppendLine("using System;")
.AppendLine("using System.Collections.Generic;")
.AppendLine("using System.Collections.Immutable;")
.AppendLine("using System.Text.RegularExpressions;")
.AppendLine("using FluentAssertions;")
.AppendLine("using FluentAssertions.Extensions;")
.AppendLine("using NUnit.Framework; using NUnit.Framework.Legacy;")
.AppendLine("using System.Threading.Tasks;")
.AppendLine("namespace TestNamespace")
.AppendLine("{")
.AppendLine(" class TestClass")
.AppendLine(" {")
.AppendLine($" void TestMethod({methodArguments})")
.AppendLine(" {")
.AppendLine($" {assertion}")
.AppendLine(" }")
.AppendLine(" }")
.AppendLine("}")
.ToString();
}
}
134 changes: 114 additions & 20 deletions src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ public class NunitTests
[AssertionDiagnostic("Assert.IsTrue(actual{0});")]
[AssertionDiagnostic("Assert.IsTrue(bool.Parse(\"true\"){0});")]
[Implemented]
public void AssertTrue_TestAnalyzer(string assertion) => VerifyDiagnostic("bool actual", assertion);
public void Nunit3_AssertTrue_TestAnalyzer(string assertion) => Nunit3VerifyDiagnostic("bool actual", assertion);

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.True(actual{0});")]
[AssertionDiagnostic("ClassicAssert.True(bool.Parse(\"true\"){0});")]
[AssertionDiagnostic("ClassicAssert.IsTrue(actual{0});")]
[AssertionDiagnostic("ClassicAssert.IsTrue(bool.Parse(\"true\"){0});")]
[Implemented]
public void Nunit4_AssertTrue_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("bool actual", assertion);

[DataTestMethod]
[AssertionCodeFix(
Expand Down Expand Up @@ -42,16 +50,51 @@ public class NunitTests
oldAssertion: "Assert.IsTrue(actual == false{0});",
newAssertion: "(actual == false).Should().BeTrue({0});")]
[Implemented]
public void AssertTrue_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyFix("bool actual", oldAssertion, newAssertion);
public void Nunit3_AssertTrue_TestCodeFix(string oldAssertion, string newAssertion) => Nunit3VerifyFix("bool actual", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.True(actual{0});",
newAssertion: "actual.Should().BeTrue({0});")]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.True(bool.Parse(\"true\"){0});",
newAssertion: "bool.Parse(\"true\").Should().BeTrue({0});")]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.True(!actual{0});",
newAssertion: "(!actual).Should().BeTrue({0});")]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.True(actual == false{0});",
newAssertion: "(actual == false).Should().BeTrue({0});")]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.IsTrue(actual{0});",
newAssertion: "actual.Should().BeTrue({0});")]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.IsTrue(bool.Parse(\"true\"){0});",
newAssertion: "bool.Parse(\"true\").Should().BeTrue({0});")]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.IsTrue(!actual{0});",
newAssertion: "(!actual).Should().BeTrue({0});")]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.IsTrue(actual == false{0});",
newAssertion: "(actual == false).Should().BeTrue({0});")]
[Implemented]
public void Nunit4_AssertTrue_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("bool actual", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("Assert.False(actual{0});")]
[AssertionDiagnostic("Assert.False(bool.Parse(\"false\"){0});")]
[AssertionDiagnostic("Assert.IsFalse(actual{0});")]
[AssertionDiagnostic("Assert.IsFalse(bool.Parse(\"false\"){0});")]
[Implemented]
public void AssertFalse_TestAnalyzer(string assertion) => VerifyDiagnostic("bool actual", assertion);
public void Nunit3_AssertFalse_TestAnalyzer(string assertion) => Nunit3VerifyDiagnostic("bool actual", assertion);

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.False(actual{0});")]
[AssertionDiagnostic("ClassicAssert.False(bool.Parse(\"false\"){0});")]
[AssertionDiagnostic("ClassicAssert.IsFalse(actual{0});")]
[AssertionDiagnostic("ClassicAssert.IsFalse(bool.Parse(\"false\"){0});")]
[Implemented]
public void Nunit4_AssertFalse_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("bool actual", assertion);

[DataTestMethod]
[AssertionCodeFix(
Expand All @@ -67,14 +110,35 @@ public void AssertTrue_TestCodeFix(string oldAssertion, string newAssertion)
oldAssertion: "Assert.IsFalse(bool.Parse(\"false\"){0});",
newAssertion: "bool.Parse(\"false\").Should().BeFalse({0});")]
[Implemented]
public void AssertFalse_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyFix("bool actual", oldAssertion, newAssertion);
public void Nunit3_AssertFalse_TestCodeFix(string oldAssertion, string newAssertion) => Nunit3VerifyFix("bool actual", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.False(actual{0});",
newAssertion: "actual.Should().BeFalse({0});")]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.False(bool.Parse(\"false\"){0});",
newAssertion: "bool.Parse(\"false\").Should().BeFalse({0});")]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.IsFalse(actual{0});",
newAssertion: "actual.Should().BeFalse({0});")]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.IsFalse(bool.Parse(\"false\"){0});",
newAssertion: "bool.Parse(\"false\").Should().BeFalse({0});")]
[Implemented]
public void Nunit4_AssertFalse_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("bool actual", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("Assert.Null(actual{0});")]
[AssertionDiagnostic("Assert.IsNull(actual{0});")]
[Implemented]
public void AssertNull_TestAnalyzer(string assertion) => VerifyDiagnostic("object actual", assertion);
public void Nunit3_AssertNull_TestAnalyzer(string assertion) => Nunit3VerifyDiagnostic("object actual", assertion);

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.Null(actual{0});")]
[AssertionDiagnostic("ClassicAssert.IsNull(actual{0});")]
[Implemented]
public void Nunit4_AssertNull_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("object actual", assertion);

[DataTestMethod]
[AssertionCodeFix(
Expand All @@ -84,14 +148,29 @@ public void AssertFalse_TestCodeFix(string oldAssertion, string newAssertion)
oldAssertion: "Assert.IsNull(actual{0});",
newAssertion: "actual.Should().BeNull({0});")]
[Implemented]
public void AssertNull_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyFix("object actual", oldAssertion, newAssertion);
public void Nunit3_AssertNull_TestCodeFix(string oldAssertion, string newAssertion) => Nunit3VerifyFix("object actual", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.Null(actual{0});",
newAssertion: "actual.Should().BeNull({0});")]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.IsNull(actual{0});",
newAssertion: "actual.Should().BeNull({0});")]
[Implemented]
public void Nunit4_AssertNull_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("object actual", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("Assert.NotNull(actual{0});")]
[AssertionDiagnostic("Assert.IsNotNull(actual{0});")]
[Implemented]
public void AssertNotNull_TestAnalyzer(string assertion) => VerifyDiagnostic("object actual", assertion);
public void Nunit3_AssertNotNull_TestAnalyzer(string assertion) => Nunit3VerifyDiagnostic("object actual", assertion);

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.NotNull(actual{0});")]
[AssertionDiagnostic("ClassicAssert.IsNotNull(actual{0});")]
[Implemented]
public void Nunit4_AssertNotNull_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("object actual", assertion);

[DataTestMethod]
[AssertionCodeFix(
Expand All @@ -101,16 +180,34 @@ public void AssertNull_TestCodeFix(string oldAssertion, string newAssertion)
oldAssertion: "Assert.IsNotNull(actual{0});",
newAssertion: "actual.Should().NotBeNull({0});")]
[Implemented]
public void AssertNotNull_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyFix("object actual", oldAssertion, newAssertion);
public void Nunit3_AssertNotNull_TestCodeFix(string oldAssertion, string newAssertion) => Nunit3VerifyFix("object actual", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.NotNull(actual{0});",
newAssertion: "actual.Should().NotBeNull({0});")]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.IsNotNull(actual{0});",
newAssertion: "actual.Should().NotBeNull({0});")]
[Implemented]
public void Nunit4_AssertNotNull_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("object actual", oldAssertion, newAssertion);

private void VerifyDiagnostic(string methodArguments, string assertion)
private void Nunit3VerifyDiagnostic(string methodArguments, string assertion)
=> VerifyDiagnostic(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 Nunit4VerifyDiagnostic(string methodArguments, string assertion)
=> VerifyDiagnostic(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 VerifyDiagnostic(string source, PackageReference nunit)
{
var source = GenerateCode.Nunit3Assertion(methodArguments, assertion);
DiagnosticVerifier.VerifyDiagnostic(new DiagnosticVerifierArguments()
.WithAllAnalyzers()
.WithSources(source)
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0, PackageReference.Nunit_3_14_0)
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0, nunit)
.WithExpectedDiagnostics(new DiagnosticResult
{
Id = AssertAnalyzer.NUnitRule.Id,
Expand All @@ -124,17 +221,14 @@ private void VerifyDiagnostic(string methodArguments, string assertion)
);
}

private void VerifyFix(string methodArguments, string oldAssertion, string newAssertion)
private void VerifyFix(string oldSource, string newSource, PackageReference nunit)
{
var oldSource = GenerateCode.Nunit3Assertion(methodArguments, oldAssertion);
var newSource = GenerateCode.Nunit3Assertion(methodArguments, newAssertion);

DiagnosticVerifier.VerifyFix(new CodeFixVerifierArguments()
.WithDiagnosticAnalyzer<AssertAnalyzer>()
.WithCodeFixProvider<NunitCodeFixProvider>()
.WithSources(oldSource)
.WithFixedSources(newSource)
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0, PackageReference.Nunit_3_14_0)
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0, nunit)
);
}
}

0 comments on commit bdcf74e

Please sign in to comment.