Skip to content

Commit

Permalink
Fix converting NUnit asserts with null as expected value (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
jairbubbles authored Jan 17, 2024
1 parent af94ab8 commit 3523b34
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -785,4 +785,66 @@ public void MyTest()
}
""");
}

[Fact]
public Task Assert_AreEqual_to_null()
{
return Assert(
"""
using NUnit.Framework.Legacy;
class Test
{
public void MyTest()
{
var x = System.Array.Empty<int>();
[|ClassicAssert.AreEqual(null, x)|];
}
}
""",
"""
using FluentAssertions;
using NUnit.Framework.Legacy;
class Test
{
public void MyTest()
{
var x = System.Array.Empty<int>();
x.Should().BeNull();
}
}
""");
}

[Fact]
public Task Assert_AreNotEqual_to_null()
{
return Assert(
"""
using NUnit.Framework.Legacy;
class Test
{
public void MyTest()
{
var x = System.Array.Empty<int>();
[|ClassicAssert.AreNotEqual(x, null)|];
}
}
""",
"""
using FluentAssertions;
using NUnit.Framework.Legacy;
class Test
{
public void MyTest()
{
var x = System.Array.Empty<int>();
x.Should().NotBeNull();
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,15 @@ private static async Task<Document> Rewrite(Document document, SyntaxNode nodeTo
{
var (left, right) = GetLeftRight(arguments, semanticModel, cancellationToken);
var leftType = semanticModel.GetTypeInfo(left.Expression, cancellationToken).Type;
if (IsCollection(leftType))
if (left.Expression is LiteralExpressionSyntax { Token.Value: null })
{
result = rewrite.UsingShould(right, "BeNull", arguments.Skip(2));
}
else if (leftType is null)
{
// Not supported
}
else if (IsCollection(leftType))
{
result = rewrite.UsingShould(right, "Equal", ArgumentList(left, arguments.Skip(2)));
}
Expand All @@ -196,7 +204,15 @@ private static async Task<Document> Rewrite(Document document, SyntaxNode nodeTo
{
var (left, right) = GetLeftRight(arguments, semanticModel, cancellationToken);
var leftType = semanticModel.GetTypeInfo(left.Expression, cancellationToken).Type;
if (IsCollection(leftType))
if (left.Expression is LiteralExpressionSyntax { Token.Value: null })
{
result = rewrite.UsingShould(right, "NotBeNull", arguments.Skip(2));
}
else if (leftType is null)
{
// Not supported
}
else if (IsCollection(leftType))
{
result = rewrite.UsingShould(right, "NotEqual", ArgumentList(left, arguments.Skip(2)));
}
Expand Down

0 comments on commit 3523b34

Please sign in to comment.