Add support for Span<T>, ReadOnlySpan<T>, Memory<T> and ReadOnlyMemory<T>#3172
Conversation
791a454 to
e34e35e
Compare
There was a problem hiding this comment.
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 forSpan<T>,ReadOnlySpan<T>,Memory<T>,ReadOnlyMemory<T>, and string-specific overloads forSpan<string>/ReadOnlySpan<string>, withOverloadResolutionPriority(-1)to avoid conflicts with theobjectcatch-all (now at-2). - Introduced
SpanAssertions<T>class withBeEqualTo(Span<T>)andSpanAssertionsExtensions.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.
Qodana for .NETIt 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 Contact Qodana teamContact us at [email protected]
|
a52fce5 to
bb4f48d
Compare
e14bd09 to
5a9fa92
Compare
5a9fa92 to
92d3bc5
Compare
92d3bc5 to
e58d3df
Compare
This pull request adds comprehensive support for assertion extensions for
Span<T>,ReadOnlySpan<T>,Memory<T>, andReadOnlyMemory<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
Shouldextension methods forSpan<T>,ReadOnlySpan<T>,Memory<T>, andReadOnlyMemory<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)SpanAssertions<T>class, enabling assertions onSpan<T>andReadOnlySpan<T>, including aBeEqualTomethod for equality checks. (Src/FluentAssertions/Collections/SpanAssertions.cs)SpanAssertions<char>, allowing direct string equality assertions. (Src/FluentAssertions/Collections/SpanAssertionsExtensions.cs)Closes #902