Skip to content

Add support for Span<T>, ReadOnlySpan<T>, Memory<T> and ReadOnlyMemory<T>#3172

Merged
dennisdoomen merged 1 commit intofluentassertions:mainfrom
dennisdoomen:feature/902-span
Mar 8, 2026
Merged

Add support for Span<T>, ReadOnlySpan<T>, Memory<T> and ReadOnlyMemory<T>#3172
dennisdoomen merged 1 commit intofluentassertions:mainfrom
dennisdoomen:feature/902-span

Conversation

@dennisdoomen
Copy link
Copy Markdown
Member

@dennisdoomen dennisdoomen commented Mar 7, 2026

This pull request adds comprehensive support for assertion extensions for Span<T>, ReadOnlySpan<T>, Memory<T>, and ReadOnlyMemory<T> types in FluentAssertions for .NET 6.0 and .NET Standard 2.1 or greater. It introduces new assertion classes and methods, updates the public API, and ensures proper annotation and validation in tests. These changes enhance the ability to write fluent assertions for modern .NET types, improving test readability and coverage.

Assertion Extensions for Span and Memory Types

  • Added new Should extension methods for Span<T>, ReadOnlySpan<T>, Memory<T>, and ReadOnlyMemory<T>, returning appropriate assertion types (SpanAssertions<T>, GenericCollectionAssertions<T>, StringCollectionAssertions). These methods are conditionally compiled for .NET 6.0 and .NET Standard 2.1 or greater. (Src/FluentAssertions/AssertionExtensions.cs)
  • Introduced the SpanAssertions<T> class, enabling assertions on Span<T> and ReadOnlySpan<T>, including a BeEqualTo method for equality checks. (Src/FluentAssertions/Collections/SpanAssertions.cs)
  • Added string-specific assertion extension for SpanAssertions<char>, allowing direct string equality assertions. (Src/FluentAssertions/Collections/SpanAssertionsExtensions.cs)

Closes #902

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds assertion support for Span<T>, ReadOnlySpan<T>, Memory<T>, and ReadOnlyMemory<T> types in FluentAssertions. It introduces Should() extension methods that convert these types to arrays and delegate to existing collection assertion infrastructure, a new SpanAssertions<T> class with a BeEqualTo convenience method, and a Be(string) extension for char spans. The Reflectify dependency is upgraded to 1.8.0 to support IsRefStruct() checks in tests.

Changes:

  • Added Should() extension methods for Span<T>, ReadOnlySpan<T>, Memory<T>, ReadOnlyMemory<T>, and string-specific overloads for Span<string>/ReadOnlySpan<string>, with OverloadResolutionPriority(-1) to avoid conflicts with the object catch-all (now at -2).
  • Introduced SpanAssertions<T> class with BeEqualTo(Span<T>) and SpanAssertionsExtensions.Be(string) for char span assertions, along with comprehensive tests.
  • Updated documentation (collections page and release notes) and API approval test files for all target frameworks.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
Src/FluentAssertions/AssertionExtensions.cs Added Should() extension methods for span/memory types with appropriate overload resolution priorities
Src/FluentAssertions/Collections/SpanAssertions.cs New SpanAssertions<T> class extending GenericCollectionAssertions with BeEqualTo method
Src/FluentAssertions/Collections/SpanAssertionsExtensions.cs Extension method Be(string) for SpanAssertions<char> using StringValidator
Src/FluentAssertions/FluentAssertions.csproj Upgraded Reflectify from 1.7.0 to 1.8.0
Tests/FluentAssertions.Specs/Collections/SpanAndMemorySpecs.cs Comprehensive tests for span/memory assertions including happy paths and error cases
Tests/FluentAssertions.Specs/AssertionExtensionsSpecs.cs Updated [NotNull] attribute test to allow ref structs and structs
Tests/Approval.Tests/ApprovedApi/FluentAssertions/*.verified.txt Updated API approval files for all target frameworks
docs/_pages/releases.md Release notes for new span/memory support
docs/_pages/collections.md Documentation with usage examples

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 7, 2026

Qodana for .NET

It seems all right 👌

No new problems were found according to the checks applied

💡 Qodana analysis was run in the pull request mode: only the changed files were checked
☁️ View the detailed Qodana report

Contact Qodana team

Contact us at [email protected]

@dennisdoomen dennisdoomen force-pushed the feature/902-span branch 2 times, most recently from a52fce5 to bb4f48d Compare March 7, 2026 15:16
@dennisdoomen dennisdoomen force-pushed the feature/902-span branch 5 times, most recently from e14bd09 to 5a9fa92 Compare March 8, 2026 07:56
@dennisdoomen dennisdoomen requested a review from jnyrup March 8, 2026 07:56
This was referenced Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for Span and ReadOnlySpan

3 participants