Skip to content

Add xunit equality assert analyzers #166

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

Merged
merged 1 commit into from
Dec 23, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
<PackageReference Include="xunit.assert" Version="2.4.1" />
<PackageReference Include="xunit.assert" Version="2.4.2" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/FluentAssertions.Analyzers.Tests/GenerateCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public static string GenericIListExpressionBodyAssertion(string assertion) => Ge

public static string XunitAssertion(string methodArguments, string assertion) => new StringBuilder()
.AppendLine("using System;")
.AppendLine("using System.Collections.Generic;")
.AppendLine("using FluentAssertions;")
.AppendLine("using FluentAssertions.Extensions;")
.AppendLine("using Xunit;")
Expand Down
229 changes: 228 additions & 1 deletion src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,233 @@ public void AssertSame_TestCodeFix(string oldAssertion, string newAssertion)
public void AssertNotSame_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertNotSameCodeFix, AssertNotSameAnalyzer>("object actual, object expected", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.Equal(expected, actual, tolerance);")]
[DataRow("Assert.Equal(expected, actual, 0.6f);")]
[Implemented]
public void AssertFloatEqualWithTolerance_TestAnalyzer(string assertion) =>
VerifyCSharpDiagnostic<AssertEqualAnalyzer>("float actual, float expected, float tolerance", assertion);

[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.Equal(expected, actual, tolerance);",
/* newAssertion: */ "actual.Should().BeApproximately(expected, tolerance);")]
[DataRow(
/* oldAssertion: */ "Assert.Equal(expected, actual, 0.6f);",
/* newAssertion: */ "actual.Should().BeApproximately(expected, 0.6f);")]
[Implemented]
public void AssertFloatEqualWithTolerance_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertEqualCodeFix, AssertEqualAnalyzer>("float actual, float expected, float tolerance", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.Equal(expected, actual, tolerance);")]
[DataRow("Assert.Equal(expected, actual, 0.6);")]
[Implemented]
public void AssertDoubleEqualWithTolerance_TestAnalyzer(string assertion) =>
VerifyCSharpDiagnostic<AssertEqualAnalyzer>("double actual, double expected, double tolerance", assertion);

[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.Equal(expected, actual, tolerance);",
/* newAssertion: */ "actual.Should().BeApproximately(expected, tolerance);")]
[DataRow(
/* oldAssertion: */ "Assert.Equal(expected, actual, 0.6);",
/* newAssertion: */ "actual.Should().BeApproximately(expected, 0.6);")]
[Implemented]
public void AssertDoubleEqualWithTolerance_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertEqualCodeFix, AssertEqualAnalyzer>("double actual, double expected, double tolerance", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.Equal(expected, actual, precision);")]
[DataRow("Assert.Equal(expected, actual, 5);")]
[Implemented]
public void AssertDoubleEqualWithPrecision_TestAnalyzer(string assertion) =>
VerifyCSharpDiagnostic<AssertEqualAnalyzer>("double actual, double expected, int precision", assertion);

// There is no corresponding API in FluentAssertions
[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.Equal(expected, actual, precision);",
/* newAssertion: */ "Assert.Equal(expected, actual, precision);")]
[DataRow(
/* oldAssertion: */ "Assert.Equal(expected, actual, 5);",
/* newAssertion: */ "Assert.Equal(expected, actual, 5);")]
[DataRow(
/* oldAssertion: */ "Assert.Equal(expected, actual, precision, rounding);",
/* newAssertion: */ "Assert.Equal(expected, actual, precision, rounding);")]
[DataRow(
/* oldAssertion: */ "Assert.Equal(expected, actual, 5, MidpointRounding.ToEven);",
/* newAssertion: */ "Assert.Equal(expected, actual, 5, MidpointRounding.ToEven);")]
[Implemented]
public void AssertDoubleEqualWithPrecision_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertEqualCodeFix, AssertEqualAnalyzer>("double actual, double expected, int precision, MidpointRounding rounding", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.Equal(expected, actual, precision);")]
[DataRow("Assert.Equal(expected, actual, 5);")]
[Implemented]
public void AssertDecimalEqualWithPrecision_TestAnalyzer(string assertion) =>
VerifyCSharpDiagnostic<AssertEqualAnalyzer>("decimal actual, decimal expected, int precision", assertion);

// There is no corresponding API in FluentAssertions
[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.Equal(expected, actual, precision);",
/* newAssertion: */ "Assert.Equal(expected, actual, precision);")]
[DataRow(
/* oldAssertion: */ "Assert.Equal(expected, actual, 5);",
/* newAssertion: */ "Assert.Equal(expected, actual, 5);")]
[Implemented]
public void AssertDecimalEqualWithPrecision_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertEqualCodeFix, AssertEqualAnalyzer>("decimal actual, decimal expected, int precision", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.Equal(expected, actual, precision);")]
[DataRow("Assert.Equal(expected, actual, TimeSpan.FromSeconds(1));")]
[Implemented]
public void AssertDateTimeEqual_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<AssertEqualAnalyzer>("DateTime actual, DateTime expected, TimeSpan precision", assertion);

[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.Equal(expected, actual, precision);",
/* newAssertion: */ "actual.Should().BeCloseTo(expected, precision);")]
[DataRow(
/* oldAssertion: */ "Assert.Equal(expected, actual, TimeSpan.FromSeconds(1));",
/* newAssertion: */ "actual.Should().BeCloseTo(expected, TimeSpan.FromSeconds(1));")]
[Implemented]
public void AssertDateTimeEqual_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertEqualCodeFix, AssertEqualAnalyzer>("DateTime actual, DateTime expected, TimeSpan precision", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.Equal(expected, actual, comparer);")]
[DataRow("Assert.Equal(expected, actual, ReferenceEqualityComparer.Instance);")]
[Implemented]
public void AssertObjectEqualWithComparer_TestAnalyzer(string assertion) =>
VerifyCSharpDiagnostic<AssertEqualAnalyzer>("object actual, object expected, IEqualityComparer<object> comparer", assertion);

[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.Equal(expected, actual, comparer);",
/* newAssertion: */ "actual.Should().BeEquivalentTo(expected, options => options.Using(comparer));")]
[DataRow(
/* oldAssertion: */ "Assert.Equal(expected, actual, ReferenceEqualityComparer.Instance);",
/* newAssertion: */ "actual.Should().BeEquivalentTo(expected, options => options.Using(ReferenceEqualityComparer.Instance));")]
[Implemented]
public void AssertObjectEqualWithComparer_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertEqualCodeFix, AssertEqualAnalyzer>("object actual, object expected, IEqualityComparer<object> comparer", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.Equal(expected, actual);")]
[Implemented]
public void AssertObjectEqual_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<AssertEqualAnalyzer>("object actual, object expected", assertion);

[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.Equal(expected, actual);",
/* newAssertion: */ "actual.Should().Be(expected);")]
[Implemented]
public void AssertObjectEqual_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertEqualCodeFix, AssertEqualAnalyzer>("object actual, object expected", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.StrictEqual(expected, actual);")]
[Implemented]
public void AssertStrictEqual_TestAnalyzer(string assertion) =>
VerifyCSharpDiagnostic<AssertStrictEqualAnalyzer>("object actual, object expected", assertion);

[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.StrictEqual(expected, actual);",
/* newAssertion: */ "actual.Should().Be(expected);")]
[Implemented]
public void AssertStrictEqual_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertStrictEqualCodeFix, AssertStrictEqualAnalyzer>("object actual, object expected", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.NotEqual(expected, actual, precision);")]
[DataRow("Assert.NotEqual(expected, actual, 5);")]
[Implemented]
public void AssertDoubleNotEqualWithPrecision_TestAnalyzer(string assertion) =>
VerifyCSharpDiagnostic<AssertNotEqualAnalyzer>("double actual, double expected, int precision", assertion);

// There is no corresponding API in FluentAssertions
[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.NotEqual(expected, actual, precision);",
/* newAssertion: */ "Assert.NotEqual(expected, actual, precision);")]
[DataRow(
/* oldAssertion: */ "Assert.NotEqual(expected, actual, 5);",
/* newAssertion: */ "Assert.NotEqual(expected, actual, 5);")]
[Implemented]
public void AssertDoubleNotEqualWithPrecision_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertNotEqualCodeFix, AssertNotEqualAnalyzer>("double actual, double expected, int precision", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.NotEqual(expected, actual, precision);")]
[DataRow("Assert.NotEqual(expected, actual, 5);")]
[Implemented]
public void AssertDecimalNotEqualWithPrecision_TestAnalyzer(string assertion) =>
VerifyCSharpDiagnostic<AssertNotEqualAnalyzer>("decimal actual, decimal expected, int precision", assertion);

// There is no corresponding API in FluentAssertions
[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.NotEqual(expected, actual, precision);",
/* newAssertion: */ "Assert.NotEqual(expected, actual, precision);")]
[DataRow(
/* oldAssertion: */ "Assert.NotEqual(expected, actual, 5);",
/* newAssertion: */ "Assert.NotEqual(expected, actual, 5);")]
[Implemented]
public void AssertDecimalNotEqualWithPrecision_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertNotEqualCodeFix, AssertNotEqualAnalyzer>("decimal actual, decimal expected, int precision", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.NotEqual(expected, actual, comparer);")]
[DataRow("Assert.NotEqual(expected, actual, ReferenceEqualityComparer.Instance);")]
[Implemented]
public void AssertObjectNotEqualWithComparer_TestAnalyzer(string assertion) =>
VerifyCSharpDiagnostic<AssertNotEqualAnalyzer>("object actual, object expected, IEqualityComparer<object> comparer", assertion);

[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.NotEqual(expected, actual, comparer);",
/* newAssertion: */ "actual.Should().NotBeEquivalentTo(expected, options => options.Using(comparer));")]
[DataRow(
/* oldAssertion: */ "Assert.NotEqual(expected, actual, ReferenceEqualityComparer.Instance);",
/* newAssertion: */ "actual.Should().NotBeEquivalentTo(expected, options => options.Using(ReferenceEqualityComparer.Instance));")]
[Implemented]
public void AssertObjectNotEqualWithComparer_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertNotEqualCodeFix, AssertNotEqualAnalyzer>("object actual, object expected, IEqualityComparer<object> comparer", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.NotEqual(expected, actual);")]
[Implemented]
public void AssertObjectNotEqual_TestAnalyzer(string assertion) =>
VerifyCSharpDiagnostic<AssertNotEqualAnalyzer>("object actual, object expected", assertion);

[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.NotEqual(expected, actual);",
/* newAssertion: */ "actual.Should().NotBe(expected);")]
[Implemented]
public void AssertObjectNotEqual_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertNotEqualCodeFix, AssertNotEqualAnalyzer>("object actual, object expected", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.NotStrictEqual(expected, actual);")]
[Implemented]
public void AssertObjectNotStrictEqual_TestAnalyzer(string assertion) =>
VerifyCSharpDiagnostic<AssertNotStrictEqualAnalyzer>("object actual, object expected", assertion);

[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.NotStrictEqual(expected, actual);",
/* newAssertion: */ "actual.Should().NotBe(expected);")]
[Implemented]
public void AssertObjectNotStrictEqual_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertNotStrictEqualCodeFix, AssertNotStrictEqualAnalyzer>("object actual, object expected", oldAssertion, newAssertion);


private void VerifyCSharpDiagnostic<TDiagnosticAnalyzer>(string methodArguments, string assertion) where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new()
{
var source = GenerateCode.XunitAssertion(methodArguments, assertion);
Expand All @@ -98,7 +325,7 @@ public void AssertNotSame_TestCodeFix(string oldAssertion, string newAssertion)
Message = message,
Locations = new DiagnosticResultLocation[]
{
new DiagnosticResultLocation("Test0.cs", 12, 13)
new("Test0.cs", 13, 13)
},
Severity = DiagnosticSeverity.Info
});
Expand Down
4 changes: 4 additions & 0 deletions src/FluentAssertions.Analyzers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public static class Xunit
public const string AssertFalse = nameof(AssertFalse);
public const string AssertSame = nameof(AssertSame);
public const string AssertNotSame = nameof(AssertNotSame);
public const string AssertEqual = nameof(AssertEqual);
public const string AssertStrictEqual = nameof(AssertStrictEqual);
public const string AssertNotEqual = nameof(AssertNotEqual);
public const string AssertNotStrictEqual = nameof(AssertNotStrictEqual);
}
}

Expand Down
Loading