Skip to content

Commit dfb1177

Browse files
authored
feat: enhance empty collection assertions with detailed failure messages (#3383)
1 parent 17609d2 commit dfb1177

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

TUnit.Assertions.Tests/ArrayAssertionTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,40 @@ public async Task Test_Array_IsSingleElement_String()
5959
var array = new[] { "single" };
6060
await Assert.That(array).IsSingleElement();
6161
}
62+
63+
[Test]
64+
public async Task Test_Array_IsEmpty_Fails_With_Item_Details()
65+
{
66+
var array = new[] { 1, 2, 3 };
67+
var action = async () => await Assert.That(array).IsEmpty();
68+
69+
var exception = await Assert.That(action).Throws<AssertionException>();
70+
71+
await Assert.That(exception.Message)
72+
.Contains("collection contains items: [1, 2, 3]");
73+
}
74+
75+
[Test]
76+
public async Task Test_Array_IsEmpty_Fails_With_String_Item_Details()
77+
{
78+
var array = new[] { "hello", "world" };
79+
var action = async () => await Assert.That(array).IsEmpty();
80+
81+
var exception = await Assert.That(action).Throws<AssertionException>();
82+
83+
await Assert.That(exception.Message)
84+
.Contains("collection contains items: [hello, world]");
85+
}
86+
87+
[Test]
88+
public async Task Test_Array_IsEmpty_Fails_With_Large_Collection_Shows_Limited_Items()
89+
{
90+
var array = Enumerable.Range(1, 15).ToArray();
91+
var action = async () => await Assert.That(array).IsEmpty();
92+
93+
var exception = await Assert.That(action).Throws<AssertionException>();
94+
95+
await Assert.That(exception.Message)
96+
.Contains("collection contains items: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and 5 more...]");
97+
}
6298
}

TUnit.Assertions/Conditions/CollectionAssertions.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,35 @@ protected override Task<AssertionResult> CheckAsync(EvaluationMetadata<TValue> m
4141
return Task.FromResult(AssertionResult.Passed);
4242
}
4343

44-
return Task.FromResult(AssertionResult.Failed("collection contains items"));
44+
// Collection is not empty - collect items for error message
45+
var items = new List<object?>();
46+
const int maxItemsToShow = 10;
47+
var totalCount = 1; // We already have the first item
48+
49+
// Add first item
50+
items.Add(enumerator.Current);
51+
52+
// Collect remaining items up to the limit
53+
while (enumerator.MoveNext())
54+
{
55+
totalCount++;
56+
if (items.Count < maxItemsToShow)
57+
{
58+
items.Add(enumerator.Current);
59+
}
60+
}
61+
62+
// Build error message
63+
var sb = new StringBuilder("collection contains items: [");
64+
sb.Append(string.Join(", ", items));
65+
if (totalCount > maxItemsToShow)
66+
{
67+
var remainingCount = totalCount - maxItemsToShow;
68+
sb.Append($", and {remainingCount} more...");
69+
}
70+
sb.Append(']');
71+
72+
return Task.FromResult(AssertionResult.Failed(sb.ToString()));
4573
}
4674
finally
4775
{

0 commit comments

Comments
 (0)