Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
558 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
49 changes: 49 additions & 0 deletions
49
Src/FluentAssertions/Common/MemberPathSegmentEqualityComparer.cs
This file contains 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,49 @@ | ||
using System.Collections.Generic; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace FluentAssertions.Common | ||
{ | ||
/// <summary> | ||
/// Compares two segments of a <see cref="MemberPath"/>. | ||
/// Sets the <see cref="AnyIndexQualifier"/> equal with any numeric index qualifier. | ||
/// All other comparisons are default string equality. | ||
/// </summary> | ||
internal class MemberPathSegmentEqualityComparer : IEqualityComparer<string> | ||
{ | ||
private const string AnyIndexQualifier = "*"; | ||
private static readonly Regex IndexQualifierRegex = new(@"^\d+$"); | ||
|
||
/// <summary> | ||
/// Compares two segments of a <see cref="MemberPath"/>. | ||
/// </summary> | ||
/// <param name="x">Left part of the comparison.</param> | ||
/// <param name="y">Right part of the comparison.</param> | ||
/// <returns>True if segments are equal, false if not.</returns> | ||
public bool Equals(string x, string y) | ||
{ | ||
if (x == AnyIndexQualifier) | ||
{ | ||
return IsIndexQualifier(y); | ||
} | ||
|
||
if (y == AnyIndexQualifier) | ||
{ | ||
return IsIndexQualifier(x); | ||
} | ||
|
||
return x == y; | ||
} | ||
|
||
private static bool IsIndexQualifier(string segment) | ||
=> segment == AnyIndexQualifier || IndexQualifierRegex.IsMatch(segment); | ||
|
||
public int GetHashCode(string obj) | ||
{ | ||
#if NETCOREAPP2_1_OR_GREATER | ||
return obj.GetHashCode(System.StringComparison.Ordinal); | ||
#else | ||
return obj.GetHashCode(); | ||
#endif | ||
} | ||
} | ||
} |
This file contains 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
46 changes: 46 additions & 0 deletions
46
Src/FluentAssertions/Equivalency/NestedExclusionOptionBuilder.cs
This file contains 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,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using FluentAssertions.Common; | ||
using FluentAssertions.Equivalency.Selection; | ||
|
||
namespace FluentAssertions.Equivalency | ||
{ | ||
public class NestedExclusionOptionBuilder<TExpectation, TCurrent> | ||
{ | ||
/// <summary> | ||
/// The selected path starting at the first <see cref="EquivalencyAssertionOptions{TExpectation}.For{TNext}"/>. | ||
/// </summary> | ||
private readonly ExcludeMemberByPathSelectionRule currentPathSelectionRule; | ||
|
||
private readonly EquivalencyAssertionOptions<TExpectation> capturedAssertionOptions; | ||
|
||
internal NestedExclusionOptionBuilder(EquivalencyAssertionOptions<TExpectation> capturedAssertionOptions, | ||
ExcludeMemberByPathSelectionRule currentPathSelectionRule) | ||
{ | ||
this.capturedAssertionOptions = capturedAssertionOptions; | ||
this.currentPathSelectionRule = currentPathSelectionRule; | ||
} | ||
|
||
/// <summary> | ||
/// Selects a nested property to exclude. This ends the <see cref="For{TNext}"/> chain. | ||
/// </summary> | ||
public EquivalencyAssertionOptions<TExpectation> Exclude(Expression<Func<TCurrent, object>> expression) | ||
{ | ||
var nextPath = expression.GetMemberPath(); | ||
currentPathSelectionRule.AppendPath(nextPath); | ||
return capturedAssertionOptions; | ||
} | ||
|
||
/// <summary> | ||
/// Adds the selected collection to the <see cref="For{TNext}"/> chain. | ||
/// </summary> | ||
public NestedExclusionOptionBuilder<TExpectation, TNext> For<TNext>( | ||
Expression<Func<TCurrent, IEnumerable<TNext>>> expression) | ||
{ | ||
var nextPath = expression.GetMemberPath(); | ||
currentPathSelectionRule.AppendPath(nextPath); | ||
return new NestedExclusionOptionBuilder<TExpectation, TNext>(capturedAssertionOptions, currentPathSelectionRule); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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
Oops, something went wrong.