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
9 changes: 8 additions & 1 deletion src/TUnit.Assertions/Extensions/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,16 @@ public static HashSetAssertion<TItem> That<TItem>(
/// Example: await Assert.That(nonGenericCollection).Contains("item");
/// </summary>
public static CollectionAssertion<object?> That(
IEnumerable value,
IEnumerable? value,
[CallerArgumentExpression(nameof(value))] string? expression = null)
{
// A null source must reach the assertion chain so IsNull() can pass and other
// assertions fail normally; Cast<>() would throw ArgumentNullException here.
if (value is null)
{
return new CollectionAssertion<object?>(null!, expression);
}

// Cast to IEnumerable<object?> directly without calling Cast<>() to preserve reference identity
// This allows IsSameReferenceAs to work correctly on non-generic IEnumerables
if (value is IEnumerable<object?> genericEnumerable)
Expand Down
55 changes: 55 additions & 0 deletions tests/TUnit.Assertions.Tests/Bugs/Tests7698.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections;
using TUnit.Assertions.Exceptions;

namespace TUnit.Assertions.Tests.Bugs;

/// <summary>
/// Regression tests for https://github.com/thomhurst/TUnit/issues/7698:
/// Assert.That on a null non-generic IEnumerable threw ArgumentNullException,
/// so IsNull() could never pass.
/// </summary>
public class Tests7698
{
[Test]
public async Task IsNull_Passes_For_Null_NonGeneric_IEnumerable()
{
IEnumerable? value = null;

await Assert.That(value).IsNull();
}

[Test]
public async Task IsNotNull_Fails_With_AssertionException_For_Null_NonGeneric_IEnumerable()
{
IEnumerable? value = null;

await Assert.That(async () => await Assert.That(value).IsNotNull())
.Throws<AssertionException>();
}

[Test]
public async Task IsEmpty_Fails_With_AssertionException_For_Null_NonGeneric_IEnumerable()
{
IEnumerable? value = null;

await Assert.That(async () => await Assert.That(value).IsEmpty())
.Throws<AssertionException>();
}

[Test]
public async Task Contains_Fails_With_AssertionException_For_Null_NonGeneric_IEnumerable()
{
IEnumerable? value = null;

await Assert.That(async () => await Assert.That(value).Contains(1))
.Throws<AssertionException>();
}

[Test]
public async Task IsNotNull_Passes_For_NonNull_NonGeneric_IEnumerable()
{
IEnumerable value = new ArrayList { 1, 2, 3 };

await Assert.That(value).IsNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ namespace
public static .<.RaisedEvent<T>> RaisesAsync<T>(<<T>> attach, <<T>> detach, <.> action)
where T : { }
public static . That( action, [.("action")] string? expression = null) { }
public static .<object?> That(.IEnumerable value, [.("value")] string? expression = null) { }
public static .<object?> That(.IEnumerable? value, [.("value")] string? expression = null) { }
public static . That(<.> action, [.("action")] string? expression = null) { }
public static . That(. task, [.("task")] string? expression = null) { }
[.(1)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ namespace
public static .<.RaisedEvent<T>> RaisesAsync<T>(<<T>> attach, <<T>> detach, <.> action)
where T : { }
public static . That( action, [.("action")] string? expression = null) { }
public static .<object?> That(.IEnumerable value, [.("value")] string? expression = null) { }
public static .<object?> That(.IEnumerable? value, [.("value")] string? expression = null) { }
public static . That(<.> action, [.("action")] string? expression = null) { }
public static . That(. task, [.("task")] string? expression = null) { }
public static . That(? value, [.("value")] string? expression = null) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ namespace
public static .<.RaisedEvent<T>> RaisesAsync<T>(<<T>> attach, <<T>> detach, <.> action)
where T : { }
public static . That( action, [.("action")] string? expression = null) { }
public static .<object?> That(.IEnumerable value, [.("value")] string? expression = null) { }
public static .<object?> That(.IEnumerable? value, [.("value")] string? expression = null) { }
public static . That(<.> action, [.("action")] string? expression = null) { }
public static . That(. task, [.("task")] string? expression = null) { }
[.(1)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ namespace
public static .<.RaisedEvent<T>> RaisesAsync<T>(<<T>> attach, <<T>> detach, <.> action)
where T : { }
public static . That( action, [.("action")] string? expression = null) { }
public static .<object?> That(.IEnumerable value, [.("value")] string? expression = null) { }
public static .<object?> That(.IEnumerable? value, [.("value")] string? expression = null) { }
public static . That(<.> action, [.("action")] string? expression = null) { }
public static . That(. task, [.("task")] string? expression = null) { }
public static . That(? value, [.("value")] string? expression = null) { }
Expand Down
Loading