-
Notifications
You must be signed in to change notification settings - Fork 729
Fail with a descriptive error when path-based rules are used on value-semantic types #3187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dennisdoomen
merged 1 commit into
main
from
copilot/fix-fluentassertions-member-exclusions
Apr 12, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Src/FluentAssertions/Equivalency/Selection/IPathBasedSelectionRule.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| namespace FluentAssertions.Equivalency.Selection; | ||
|
|
||
| /// <summary> | ||
| /// Represents a selection rule whose effect is determined by a configured member path. | ||
| /// This allows callers to ask whether the rule targets any members within the subtree rooted at | ||
| /// a given <see cref="INode"/>, even when the rule is wrapped by decorators such as the | ||
| /// collection-member options decorator. | ||
| /// </summary> | ||
| internal interface IPathBasedSelectionRule : IMemberSelectionRule | ||
| { | ||
| /// <summary> | ||
| /// Returns <c>true</c> when this rule targets at least one member of <paramref name="currentNode"/> | ||
| /// or one of its descendants. | ||
| /// </summary> | ||
| bool SelectsMembersOf(INode currentNode); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 55 additions & 9 deletions
64
Src/FluentAssertions/Equivalency/Selection/SelectMemberByPathSelectionRule.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,82 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text.RegularExpressions; | ||
| using FluentAssertions.Common; | ||
|
|
||
| namespace FluentAssertions.Equivalency.Selection; | ||
|
|
||
| internal abstract class SelectMemberByPathSelectionRule : IMemberSelectionRule | ||
| internal abstract class SelectMemberByPathSelectionRule : IPathBasedSelectionRule | ||
| { | ||
| private static readonly Regex LeadingCollectionIndexRegex = new(@"^\[[0-9]+]\.?"); | ||
|
|
||
| public virtual bool IncludesMembers => false; | ||
|
|
||
| protected abstract MemberPath MemberPath { get; } | ||
|
|
||
| public IEnumerable<IMember> SelectMembers(INode currentNode, IEnumerable<IMember> selectedMembers, | ||
| MemberSelectionContext context) | ||
| { | ||
| var currentPath = RemoveRootIndexQualifier(currentNode.Expectation.PathAndName); | ||
| var members = selectedMembers.ToList(); | ||
| AddOrRemoveMembersFrom(members, currentNode, currentPath, context); | ||
| AddOrRemoveMembersFrom(members, currentNode, GetPathRelativeToSelectionRoot(currentNode), context); | ||
|
|
||
| return members; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns <c>true</c> if this rule would select members within the subtree rooted at | ||
| /// <paramref name="currentNode"/>, meaning the rule path targets any member at or below the current node. | ||
| /// </summary> | ||
| public bool SelectsMembersOf(INode currentNode) | ||
| { | ||
| if (currentNode.IsRoot) | ||
| { | ||
| return !MemberPath.ToString().IsNullOrEmpty(); | ||
| } | ||
|
|
||
| string currentPath = GetPathRelativeToSelectionRoot(currentNode); | ||
|
|
||
| if (string.IsNullOrEmpty(currentPath)) | ||
| { | ||
| return !MemberPath.ToString().IsNullOrEmpty(); | ||
| } | ||
|
|
||
| // Compare normalized path segments rather than raw strings so collection rules like "Items[].Name" | ||
| // still match the concrete node path "Items[0].Name", and so paths built through | ||
| // MemberPath.AsParentCollectionOf are interpreted consistently. | ||
| return new MemberPath(currentPath).IsParentOf(MemberPath); | ||
| } | ||
|
|
||
| protected abstract void AddOrRemoveMembersFrom(List<IMember> selectedMembers, | ||
| INode parent, string parentPath, | ||
| MemberSelectionContext context); | ||
|
|
||
| private static string RemoveRootIndexQualifier(string path) | ||
| /// <summary> | ||
| /// Returns the path used for member-selection matching at <paramref name="currentNode"/>. | ||
| /// For items inside a root collection, Fluent Assertions exposes paths such as <c>[0].Name</c>, but | ||
| /// selection rules are expressed relative to the collection item type (for example <c>Name</c>). | ||
| /// This method removes that root-item index so both sides use the same coordinate system. | ||
| /// </summary> | ||
| private static string GetPathRelativeToSelectionRoot(INode currentNode) | ||
| { | ||
| Match match = new Regex(@"^\[[0-9]+]").Match(path); | ||
|
|
||
| if (match.Success) | ||
| if (currentNode.IsRoot) | ||
| { | ||
| path = path.Substring(match.Length); | ||
| return string.Empty; | ||
| } | ||
|
|
||
| return path; | ||
| string expectationPath = currentNode.Expectation.PathAndName; | ||
| return RemoveLeadingCollectionIndex(expectationPath); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Removes only the leading root-collection index from <paramref name="path"/>, such as turning | ||
| /// <c>[0]</c> into an empty path and <c>[0].Name</c> into <c>Name</c>. | ||
| /// Nested collection indices are intentionally left in place; they are handled later by | ||
| /// <see cref="MemberPath"/> comparison, which treats <c>[]</c> and concrete indices as equivalent. | ||
| /// </summary> | ||
| private static string RemoveLeadingCollectionIndex(string path) | ||
| { | ||
| Match match = LeadingCollectionIndexRegex.Match(path); | ||
| return match.Success ? path.Substring(match.Length) : path; | ||
|
dennisdoomen marked this conversation as resolved.
|
||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.