Skip to content
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 @@ -699,7 +699,28 @@ private static void GenerateAssertConditionClassForMethod(SourceProductionContex
sourceBuilder.AppendLine();
}

if (!attributeData.TargetType.IsValueType)
// Check if the method's first parameter accepts null values
// For static methods like string.IsNullOrEmpty(string? value), the first parameter
// is the value being asserted. If it's marked as nullable (NullableAnnotation.Annotated),
// we should skip the null check and let the method handle null values.
var shouldGenerateNullCheck = !attributeData.TargetType.IsValueType;
if (shouldGenerateNullCheck && staticMethod.Parameters.Length > 0)
{
var firstParameter = staticMethod.Parameters[0];
// Skip null check if the parameter explicitly accepts null (e.g., string? value)
if (firstParameter.NullableAnnotation == NullableAnnotation.Annotated)
{
shouldGenerateNullCheck = false;
}
// For backwards compatibility with .NET Framework where NullableAnnotation might not be set,
// also check for well-known methods that accept null by design
else if (methodName == "IsNullOrEmpty" || methodName == "IsNullOrWhiteSpace")
{
shouldGenerateNullCheck = false;
}
}

if (shouldGenerateNullCheck)
{
sourceBuilder.AppendLine(" if (actualValue is null)");
sourceBuilder.AppendLine(" {");
Expand Down
91 changes: 91 additions & 0 deletions TUnit.Assertions.Tests/StringNullabilityAssertionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using TUnit.Assertions.Extensions;

namespace TUnit.Assertions.Tests;

/// <summary>
/// Tests for string assertions that accept null values (IsNullOrEmpty, IsNullOrWhiteSpace)
/// </summary>
public class StringNullabilityAssertionTests
{
[Test]
public async Task IsNullOrEmpty_WithNullString_Passes()
{
string? nullString = null;
await Assert.That(nullString).IsNullOrEmpty();
}

[Test]
public async Task IsNullOrEmpty_WithEmptyString_Passes()
{
var emptyString = "";
await Assert.That(emptyString).IsNullOrEmpty();
}

[Test]
public async Task IsNullOrEmpty_WithNonEmptyString_Fails()
{
var value = "Hello";
await Assert.That(async () => await Assert.That(value).IsNullOrEmpty())
.Throws<AssertionException>();
}

[Test]
public async Task IsNullOrEmpty_WithWhitespace_Fails()
{
var value = " ";
await Assert.That(async () => await Assert.That(value).IsNullOrEmpty())
.Throws<AssertionException>();
}

[Test]
public async Task IsNullOrWhiteSpace_WithNullString_Passes()
{
string? nullString = null;
await Assert.That(nullString).IsNullOrWhiteSpace();
}

[Test]
public async Task IsNullOrWhiteSpace_WithEmptyString_Passes()
{
var emptyString = "";
await Assert.That(emptyString).IsNullOrWhiteSpace();
}

[Test]
public async Task IsNullOrWhiteSpace_WithWhitespace_Passes()
{
var whitespace = " ";
await Assert.That(whitespace).IsNullOrWhiteSpace();
}

[Test]
public async Task IsNullOrWhiteSpace_WithNonEmptyString_Fails()
{
var value = "Hello";
await Assert.That(async () => await Assert.That(value).IsNullOrWhiteSpace())
.Throws<AssertionException>();
}

[Test]
public async Task IsNotNullOrEmpty_WithNullString_Fails()
{
string? nullString = null;
await Assert.That(async () => await Assert.That(nullString).IsNotNullOrEmpty())
.Throws<AssertionException>();
}

[Test]
public async Task IsNotNullOrEmpty_WithEmptyString_Fails()
{
var emptyString = "";
await Assert.That(async () => await Assert.That(emptyString).IsNotNullOrEmpty())
.Throws<AssertionException>();
}

[Test]
public async Task IsNotNullOrEmpty_WithNonEmptyString_Passes()
{
var value = "Hello";
await Assert.That(value).IsNotNullOrEmpty();
}
}
Loading