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
36 changes: 36 additions & 0 deletions TUnit.Assertions.Tests/ArrayAssertionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,40 @@ public async Task Test_Array_IsSingleElement_String()
var array = new[] { "single" };
await Assert.That(array).IsSingleElement();
}

[Test]
public async Task Test_Array_IsEmpty_Fails_With_Item_Details()
{
var array = new[] { 1, 2, 3 };
var action = async () => await Assert.That(array).IsEmpty();

var exception = await Assert.That(action).Throws<AssertionException>();

await Assert.That(exception.Message)
.Contains("collection contains items: [1, 2, 3]");
}

[Test]
public async Task Test_Array_IsEmpty_Fails_With_String_Item_Details()
{
var array = new[] { "hello", "world" };
var action = async () => await Assert.That(array).IsEmpty();

var exception = await Assert.That(action).Throws<AssertionException>();

await Assert.That(exception.Message)
.Contains("collection contains items: [hello, world]");
}

[Test]
public async Task Test_Array_IsEmpty_Fails_With_Large_Collection_Shows_Limited_Items()
{
var array = Enumerable.Range(1, 15).ToArray();
var action = async () => await Assert.That(array).IsEmpty();

var exception = await Assert.That(action).Throws<AssertionException>();

await Assert.That(exception.Message)
.Contains("collection contains items: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and 5 more...]");
}
}
30 changes: 29 additions & 1 deletion TUnit.Assertions/Conditions/CollectionAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,35 @@ protected override Task<AssertionResult> CheckAsync(EvaluationMetadata<TValue> m
return Task.FromResult(AssertionResult.Passed);
}

return Task.FromResult(AssertionResult.Failed("collection contains items"));
// Collection is not empty - collect items for error message
var items = new List<object?>();
const int maxItemsToShow = 10;
var totalCount = 1; // We already have the first item

// Add first item
items.Add(enumerator.Current);

// Collect remaining items up to the limit
while (enumerator.MoveNext())
{
totalCount++;
if (items.Count < maxItemsToShow)
{
items.Add(enumerator.Current);
}
}

// Build error message
var sb = new StringBuilder("collection contains items: [");
sb.Append(string.Join(", ", items));
if (totalCount > maxItemsToShow)
{
var remainingCount = totalCount - maxItemsToShow;
sb.Append($", and {remainingCount} more...");
}
sb.Append(']');

return Task.FromResult(AssertionResult.Failed(sb.ToString()));
}
finally
{
Expand Down
Loading