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/CollectionAssertionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,40 @@ await Assert.That(names)
.And.Contains("Bob")
.And.DoesNotContain("Dave");
}

[Test]
public async Task All_Predicate_Failure_Message_Contains_Index_And_Value()
{
var items = new[] { 2, 4, -5, 8 };

await Assert.That(async () =>
await Assert.That(items).All(x => x > 0)
).Throws<AssertionException>()
.WithMessageContaining("index 2")
.And.WithMessageContaining("[-5]");
}

[Test]
public async Task All_Predicate_Failure_Message_Contains_String_Value()
{
var names = new[] { "Alice", "Bob", "" };

await Assert.That(async () =>
await Assert.That(names).All(x => !string.IsNullOrEmpty(x))
).Throws<AssertionException>()
.WithMessageContaining("index 2")
.And.WithMessageContaining("[]");
}

[Test]
public async Task All_Predicate_Failure_Message_Contains_First_Failing_Item()
{
var items = new[] { 1, 2, 3, -1, -2, -3 };

await Assert.That(async () =>
await Assert.That(items).All(x => x > 0)
).Throws<AssertionException>()
.WithMessageContaining("index 3")
.And.WithMessageContaining("[-1]");
}
Comment on lines +274 to +308
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message directly interpolates {item} which could display as an empty string if the item is null. Consider adding a test case for collections containing null values to verify the error message displays appropriately (e.g., "with value [null]" or similar).

Copilot uses AI. Check for mistakes.
}
2 changes: 1 addition & 1 deletion TUnit.Assertions/Conditions/CollectionAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ protected override Task<AssertionResult> CheckAsync(EvaluationMetadata<TCollecti
{
if (!_predicate(item))
{
return Task.FromResult(AssertionResult.Failed($"item at index {index} does not satisfy predicate"));
return Task.FromResult(AssertionResult.Failed($"item at index {index} with value [{item}] does not satisfy predicate"));
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message format uses square brackets [{item}] to display the value, but other similar error messages in this file use parentheses ({item}) format (see lines 796 and 852 in CollectionIsInOrderAssertion and CollectionIsInDescendingOrderAssertion). For consistency, consider using parentheses format: "item at index {index} ({item}) does not satisfy predicate" to match the existing pattern in the codebase.

Suggested change
return Task.FromResult(AssertionResult.Failed($"item at index {index} with value [{item}] does not satisfy predicate"));
return Task.FromResult(AssertionResult.Failed($"item at index {index} ({item}) does not satisfy predicate"));

Copilot uses AI. Check for mistakes.
}

index++;
Expand Down
Loading