Skip to content

Commit

Permalink
Use Should().Equal() when subject is a collection (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
jairbubbles authored Dec 20, 2023
1 parent 33bf0cb commit 3a40aca
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -661,4 +661,128 @@ public void MyTest(float test)
}
""");
}

[Fact]
public Task Assert_AreEqual_arrays()
{
return Assert(
"""
using NUnit.Framework.Legacy;
class Test
{
public void MyTest()
{
var x = new int[] { 1, 2, 3 };
[|ClassicAssert.AreEqual(new int[] {}, x)|];
}
}
""",
"""
using FluentAssertions;
using NUnit.Framework.Legacy;
class Test
{
public void MyTest()
{
var x = new int[] { 1, 2, 3 };
x.Should().Equal(new int[] {});
}
}
""");
}

[Fact]
public Task Assert_AreEqual_lists()
{
return Assert(
"""
using NUnit.Framework.Legacy;
class Test
{
public void MyTest()
{
var x = new System.Collections.Generic.List<int>() { 1, 2, 3 };
[|ClassicAssert.AreEqual(new int[] {}, x)|];
}
}
""",
"""
using FluentAssertions;
using NUnit.Framework.Legacy;
class Test
{
public void MyTest()
{
var x = new System.Collections.Generic.List<int>() { 1, 2, 3 };
x.Should().Equal(new int[] {});
}
}
""");
}

[Fact]
public Task Assert_AreNotEqual_arrays()
{
return Assert(
"""
using NUnit.Framework.Legacy;
class Test
{
public void MyTest()
{
var x = new int[] { 1, 2, 3 };
[|ClassicAssert.AreNotEqual(new int[] {}, x)|];
}
}
""",
"""
using FluentAssertions;
using NUnit.Framework.Legacy;
class Test
{
public void MyTest()
{
var x = new int[] { 1, 2, 3 };
x.Should().NotEqual(new int[] {});
}
}
""");
}

[Fact]
public Task Assert_AreNotEqual_lists()
{
return Assert(
"""
using NUnit.Framework.Legacy;
class Test
{
public void MyTest()
{
var x = new System.Collections.Generic.List<int>() { 1, 2, 3 };
[|ClassicAssert.AreNotEqual(new int[] {}, x)|];
}
}
""",
"""
using FluentAssertions;
using NUnit.Framework.Legacy;
class Test
{
public void MyTest()
{
var x = new System.Collections.Generic.List<int>() { 1, 2, 3 };
x.Should().NotEqual(new int[] {});
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Version>1.0.17</Version>
<Version>1.0.18</Version>
<IncludeBuildOutput>false</IncludeBuildOutput>
<developmentDependency>true</developmentDependency>
<Description>A Roslyn analyzer to help migrate from Xunit / NUnit assertions to FluentAssertions</Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,31 @@ private static async Task<Document> Rewrite(Document document, SyntaxNode nodeTo
if (methodName is "AreEqual")
{
var (left, right) = GetLeftRight(arguments, semanticModel, cancellationToken);
var leftType = semanticModel.GetTypeInfo(left.Expression, cancellationToken).Type?.SpecialType;
var useBeApproximately = leftType is SpecialType.System_Double or SpecialType.System_Single
&& arguments.FirstOrDefault(x => x.NameColon?.Name.Identifier.ValueText is "delta") is not null;

var leftType = semanticModel.GetTypeInfo(left.Expression, cancellationToken).Type;
if (IsCollection(leftType))
{
result = rewrite.UsingShould(right, "Equal", ArgumentList(left, arguments.Skip(2)));
}
else
{
var useBeApproximately = leftType.SpecialType is SpecialType.System_Double or SpecialType.System_Single
&& arguments.FirstOrDefault(x => x.NameColon?.Name.Identifier.ValueText is "delta") is not null;

result = rewrite.UsingShould(right, useBeApproximately ? "BeApproximately" : "Be", ArgumentList(left, arguments.Skip(2)));
result = rewrite.UsingShould(right, useBeApproximately ? "BeApproximately" : "Be", ArgumentList(left, arguments.Skip(2)));
}
}
else if (methodName is "AreNotEqual")
{
var (left, right) = GetLeftRight(arguments, semanticModel, cancellationToken);
result = rewrite.UsingShould(right, "NotBe", ArgumentList(left, arguments.Skip(2)));
var leftType = semanticModel.GetTypeInfo(left.Expression, cancellationToken).Type;
if (IsCollection(leftType))
{
result = rewrite.UsingShould(right, "NotEqual", ArgumentList(left, arguments.Skip(2)));
}
else
{
result = rewrite.UsingShould(right, "NotBe", ArgumentList(left, arguments.Skip(2)));
}
}
else if (methodName is "AreSame")
{
Expand Down Expand Up @@ -599,10 +613,7 @@ private static async Task<Document> Rewrite(Document document, SyntaxNode nodeTo
if (argumentTypeSymbol is null || argumentTypeSymbol.SpecialType == SpecialType.System_String)
return (false, true);

var isCollection =
argumentTypeSymbol.OriginalDefinition.SpecialType == SpecialType.System_Collections_IEnumerable
|| argumentTypeSymbol.OriginalDefinition.AllInterfaces.Any(i =>
i.SpecialType == SpecialType.System_Collections_IEnumerable);
var isCollection = NunitAssertAnalyzerCodeFixProvider.IsCollection(argumentTypeSymbol);

var isSupportedCollection = argumentTypeSymbol.OriginalDefinition.TypeKind == TypeKind.Array
|| argumentTypeSymbol.OriginalDefinition.SpecialType == SpecialType.System_Collections_Generic_IEnumerable_T
Expand Down Expand Up @@ -855,6 +866,16 @@ bool IsGenericMethod(out ITypeSymbol typeArgument, ITypeSymbol root, params stri
return document;
}

private static bool IsCollection(ITypeSymbol argumentTypeSymbol)
{
if (argumentTypeSymbol.SpecialType == SpecialType.System_String)
return false;

return argumentTypeSymbol.OriginalDefinition.SpecialType == SpecialType.System_Collections_IEnumerable
|| argumentTypeSymbol.OriginalDefinition.AllInterfaces.Any(i =>
i.SpecialType == SpecialType.System_Collections_IEnumerable);
}

private static (ArgumentSyntax left, ArgumentSyntax right) GetLeftRight(SeparatedSyntaxList<ArgumentSyntax> arguments, SemanticModel semanticModel, CancellationToken cancellationToken)
{
var left = arguments[0];
Expand Down

0 comments on commit 3a40aca

Please sign in to comment.