Skip to content

[file-diet] Refactor Assert.Contains.cs (583 lines) into focused partial-class files #11778

Closed

Description

@github-actions

Overview

The file src/TestFramework/TestFramework/Assertions/Assert.Contains.cs has grown to 583 lines, making it harder to navigate and maintain. This task involves refactoring it into smaller, more focused files.

Current State

  • File: src/TestFramework/TestFramework/Assertions/Assert.Contains.cs
  • Size: 583 lines
  • Language: C#
Structural Analysis

This file contains a single #region Contains block inside the partial class Assert, holding roughly 28 public Contains overloads plus three private Report*Failed helper methods. The overloads fall into clearly separable groups:

  • Generic/object IEnumerable-based overloads (Contains<T>(T, IEnumerable<T>, ...), Contains(object?, IEnumerable, ...), with and without IEqualityComparer, plus predicate-based versions)
  • Array and generic TCollection overloads (Contains<T>(T, T[], ...), Contains<T, TCollection>(...))
  • Span<T> / ReadOnlySpan<T> / Memory<T> / ReadOnlyMemory<T> overloads (value, comparer, and predicate variants)
  • String substring overloads (Contains(string, string, ...), with StringComparison)
  • Shared private failure-reporting helpers (ReportAssertContainsItemFailed, ReportAssertContainsPredicateFailed, ReportAssertContainsSubstringFailed)

Each public overload is a thin wrapper that calls TelemetryCollector.TrackAssertionCall and delegates to a shared ContainsCore/report helper, so the bulk of the line count comes from XML doc comments repeated across the many overloads rather than complex logic.

Refactoring Strategy

Proposed File Splits

Since Assert is a partial class, the overloads can be split across additional partial-class files without changing the public API surface:

  1. Assert.Contains.Collections.cs

    • Contents: Contains<T>/Contains(object?, ...) overloads over IEnumerable<T>, IEnumerable, arrays, and generic TCollection, both value- and predicate-based, with and without IEqualityComparer
    • Responsibility: Containment assertions for enumerable/array-like collections
  2. Assert.Contains.SpanMemory.cs

    • Contents: All Span<T>, ReadOnlySpan<T>, Memory<T>, ReadOnlyMemory<T> overloads (value, comparer, and predicate variants)
    • Responsibility: Containment assertions for span/memory-based buffers
  3. Assert.Contains.String.cs

    • Contents: Contains(string substring, string value, ...) overloads (with and without StringComparison), and the shared ReportAssertContainsItemFailed, ReportAssertContainsPredicateFailed, ReportAssertContainsSubstringFailed private helpers
    • Responsibility: Substring containment assertions and shared failure-reporting helpers

Implementation Guidelines

  1. Preserve Behavior: All existing functionality must work identically after the split
  2. Maintain Public API: Keep exported/public symbols accessible with the same names
  3. Update Imports: Fix all import paths throughout the codebase
  4. Test After Each Split: Run the test suite after each incremental change
  5. One File at a Time: Split one module at a time to make review easier

Acceptance Criteria

  • Original file is split into focused modules
  • Each new file is under 300 lines
  • All tests pass after refactoring
  • No breaking changes to public API
  • All import paths updated correctly

Priority: Medium
Effort: Small
Expected Impact: Improved code navigability, easier testing, reduced merge conflicts

🤖 Automated content by GitHub Copilot. Generated by the Daily File Diet workflow. · copilot · auto · 40.6 AIC · ⌖ 5.95 AIC · ⊞ 12.2K · [◷]( · ◷)

  • expires on Oct 8, 2026, 7:07 PM UTC

Metadata

Metadata

Labels

type/automationCreated or maintained by an agentic workflow.type/tech-debtCode health, refactoring, simplification.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions