Skip to content

Commit

Permalink
Fix AreEqual conversion on reference types
Browse files Browse the repository at this point in the history
  • Loading branch information
jairbubbles committed Jan 22, 2024
1 parent 902f300 commit 5f99a49
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -847,4 +847,66 @@ public void MyTest()
}
""");
}

[Fact]
public Task Assert_AreEqual_on_assemblies()
{
return Assert(
"""
using NUnit.Framework.Legacy;
using System.Reflection;
class Test
{
public void MyTest(Assembly assembly1, Assembly assembly2)
{
[|ClassicAssert.AreEqual(assembly1, assembly2)|];
}
}
""",
"""
using System.Reflection;
using FluentAssertions;
using NUnit.Framework.Legacy;
class Test
{
public void MyTest(Assembly assembly1, Assembly assembly2)
{
assembly2.Should().BeSameAs(assembly1);
}
}
""");
}

[Fact]
public Task Assert_AreNotEqual_on_assemblies()
{
return Assert(
"""
using NUnit.Framework.Legacy;
using System.Reflection;
class Test
{
public void MyTest(Assembly assembly1, Assembly assembly2)
{
[|ClassicAssert.AreNotEqual(assembly1, assembly2)|];
}
}
""",
"""
using System.Reflection;
using FluentAssertions;
using NUnit.Framework.Legacy;
class Test
{
public void MyTest(Assembly assembly1, Assembly assembly2)
{
assembly2.Should().NotBeSameAs(assembly1);
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Version>1.0.20</Version>
<Version>1.0.21</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 @@ -192,6 +192,10 @@ private static async Task<Document> Rewrite(Document document, SyntaxNode nodeTo
{
result = rewrite.UsingShould(right, "Equal", ArgumentList(left, arguments.Skip(2)));
}
else if (leftType.SpecialType != SpecialType.System_String && leftType.IsReferenceType)
{
result = rewrite.UsingShould(right, "BeSameAs", ArgumentList(left, arguments.Skip(2)));
}
else
{
var useBeApproximately = leftType.SpecialType is SpecialType.System_Double or SpecialType.System_Single
Expand All @@ -216,6 +220,10 @@ private static async Task<Document> Rewrite(Document document, SyntaxNode nodeTo
{
result = rewrite.UsingShould(right, "NotEqual", ArgumentList(left, arguments.Skip(2)));
}
else if (leftType.SpecialType != SpecialType.System_String && leftType.IsReferenceType)
{
result = rewrite.UsingShould(right, "NotBeSameAs", ArgumentList(left, arguments.Skip(2)));
}
else
{
result = rewrite.UsingShould(right, "NotBe", ArgumentList(left, arguments.Skip(2)));
Expand Down

0 comments on commit 5f99a49

Please sign in to comment.