Skip to content

.Count(itemAssertion) per-item overload loses specialised source #5707

@thomhurst

Description

@thomhurst

Related: #5703, #5704, #5705, #5706 (same pattern).

Problem

CollectionAssertionBase.Count(Func<IAssertionSource<TItem>, Assertion<TItem>?> itemAssertion, ...) passes a generic IAssertionSource<TItem> into the lambda. When TItem is a string, collection, dictionary, or set, specialised assertions on the item are unreachable.

Repro

// IEnumerable<string>
await Assert.That(items).Count(s => s.Contains(\"foo\")); // fails: string-specific Contains not on IAssertionSource<string>

// IEnumerable<List<int>>
await Assert.That(listOfLists).Count(l => l.HasCount(3)); // fails

Affected signature

TUnit.Assertions/Sources/CollectionAssertionBase.cs:167

public CollectionCountSource<TCollection, TItem> Count(
    Func<IAssertionSource<TItem>, Assertion<TItem>?> itemAssertion,
    ...)

Proposed fix

Add specialised Count overloads keyed on TItem shape (string / IEnumerable / IDictionary / ISet / ...), passing the matching specialised source into the lambda. Use OverloadResolutionPriority for compiler steering.

Acceptance

  • Specialised assertions reachable inside the .Count(item => ...) lambda for string/collection/dict/set items.
  • Tests cover each specialised element shape.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions