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
166 changes: 154 additions & 12 deletions TUnit.Assertions.Tests/CollectionAssertionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Diagnostics.CodeAnalysis;
using TUnit.Assertions.Extensions;

namespace TUnit.Assertions.Tests;
Expand Down Expand Up @@ -37,17 +36,6 @@ public async Task Count2()
await Assert.That(() => items).Count().IsEqualTo(0);
}

[Test]
[SuppressMessage("Obsolete", "CS0618:Type or member is obsolete")]
public async Task Count_WithPredicate()
{
var items = new List<int> { 1, 2, 3, 4, 5 };

#pragma warning disable CS0618 // Type or member is obsolete
await Assert.That(items).Count(x => x > 2).IsEqualTo(3);
#pragma warning restore CS0618
}

[Test]
public async Task Count_WithInnerAssertion_IsGreaterThan()
{
Expand Down Expand Up @@ -128,4 +116,158 @@ public async Task Count_WithInnerAssertion_Lambda_Collection()
// Test with lambda-wrapped collection
await Assert.That(() => items).Count(item => item.IsGreaterThan(2)).IsEqualTo(3);
}

Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

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

The new collection chaining functionality introduced in this PR lacks test coverage for failure scenarios. While the happy path tests verify that chaining works when assertions pass, there are no tests verifying:

  1. Error messages when count assertions fail (e.g., expected 5 but found 3)
  2. Chained assertions after a failed count assertion
  3. Null collection handling with chained assertions
  4. Exception handling during item assertion evaluation in Count(item => ...)

Consider adding negative test cases like:

  • Count assertion fails with descriptive error message
  • Count with item filter fails and shows correct count
  • Chained assertion after count shows proper error context

Copilot uses AI. Check for mistakes.
// Tests for collection chaining after Count assertions

[Test]
public async Task Count_ThenAnd_Contains()
{
var items = new List<int> { 1, 2, 3, 4, 5 };

// Count and then chain with Contains
await Assert.That(items)
.Count().IsEqualTo(5)
.And.Contains(3);
}

[Test]
public async Task Count_ThenAnd_IsNotEmpty()
{
var items = new List<int> { 1, 2, 3, 4, 5 };

// Count and then chain with IsNotEmpty
await Assert.That(items)
.Count().IsGreaterThan(0)
.And.IsNotEmpty();
}

[Test]
public async Task Count_WithInnerAssertion_ThenAnd_Contains()
{
var items = new List<int> { 1, 2, 3, 4, 5 };

// Count with inner assertion and then chain with Contains
await Assert.That(items)
.Count(item => item.IsGreaterThan(2)).IsEqualTo(3)
.And.Contains(5);
}

[Test]
public async Task Count_ThenAnd_All()
{
var items = new List<int> { 1, 2, 3, 4, 5 };

// Count and then chain with All
await Assert.That(items)
.Count().IsEqualTo(5)
.And.All(x => x > 0);
}

[Test]
public async Task Count_ThenAnd_Count()
{
var items = new List<int> { 1, 2, 3, 4, 5 };

// Chain multiple Count assertions
await Assert.That(items)
.Count().IsGreaterThan(3)
.And.Count().IsLessThan(10);
}

[Test]
public async Task Count_WithInnerAssertion_ThenAnd_IsInOrder()
{
var items = new List<int> { 1, 2, 3, 4, 5 };

// Count with inner assertion and then check ordering
await Assert.That(items)
.Count(item => item.IsGreaterThan(0)).IsEqualTo(5)
.And.IsInOrder();
}

[Test]
public async Task Count_IsGreaterThan()
{
var items = new List<int> { 1, 2, 3, 4, 5 };

await Assert.That(items).Count().IsGreaterThan(3);
}

[Test]
public async Task Count_IsLessThan()
{
var items = new List<int> { 1, 2, 3 };

await Assert.That(items).Count().IsLessThan(5);
}

[Test]
public async Task Count_IsGreaterThanOrEqualTo()
{
var items = new List<int> { 1, 2, 3, 4, 5 };

await Assert.That(items).Count().IsGreaterThanOrEqualTo(5);
}

[Test]
public async Task Count_IsLessThanOrEqualTo()
{
var items = new List<int> { 1, 2, 3, 4, 5 };

await Assert.That(items).Count().IsLessThanOrEqualTo(5);
}

[Test]
public async Task Count_IsZero()
{
var items = new List<int>();

await Assert.That(items).Count().IsZero();
}

[Test]
public async Task Count_IsPositive()
{
var items = new List<int> { 1 };

await Assert.That(items).Count().IsPositive();
}

[Test]
public async Task Count_IsNotEqualTo()
{
var items = new List<int> { 1, 2, 3 };

await Assert.That(items).Count().IsNotEqualTo(5);
}

[Test]
public async Task Chained_Collection_Assertions()
{
var numbers = new[] { 1, 2, 3, 4, 5 };

// For collections of int, use Count().IsEqualTo(5) instead of Count(c => c.IsEqualTo(5))
// to avoid ambiguity with item-filtering
await Assert.That(numbers)
.IsNotEmpty()
.And.Count().IsEqualTo(5)
.And.Contains(3)
.And.DoesNotContain(10)
.And.IsInOrder()
.And.All(n => n > 0)
.And.Any(n => n == 5);
}

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

// For non-int collections, Count(c => c.IsEqualTo(3)) works unambiguously
await Assert.That(names)
.IsNotEmpty()
.And.Count(c => c.IsEqualTo(3))
.And.Contains("Bob")
.And.DoesNotContain("Dave");
}
}
Loading
Loading