-
-
Notifications
You must be signed in to change notification settings - Fork 541
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
14 changed files
with
493 additions
and
8 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
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
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 is null || y is null) | ||
{ | ||
return x == y; | ||
} | ||
|
||
if (x == AnyIndexQualifier) | ||
{ | ||
return EqualsAnyIndexQualifier(y); | ||
} | ||
|
||
if (y == AnyIndexQualifier) | ||
{ | ||
return EqualsAnyIndexQualifier(x); | ||
} | ||
|
||
return x == y; | ||
} | ||
|
||
private static bool EqualsAnyIndexQualifier(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
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
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> : EquivalencyAssertionOptions<TExpectation> | ||
{ | ||
/// <summary> | ||
/// The selected path starting at the first <see cref="EquivalencyAssertionOptions{TExpectation}.Excluding{TNext}"/>. | ||
/// </summary> | ||
private readonly ExcludeMemberByPathSelectionRule currentPathSelectionRule; | ||
|
||
internal NestedExclusionOptionBuilder(EquivalencyAssertionOptions<TExpectation> equivalencyAssertionOptions, | ||
ExcludeMemberByPathSelectionRule currentPathSelectionRule) | ||
: base(equivalencyAssertionOptions) | ||
{ | ||
this.currentPathSelectionRule = currentPathSelectionRule; | ||
} | ||
|
||
/// <summary> | ||
/// Selects a property to use. This ends the <see cref="ThenExcluding"/> chain. | ||
/// </summary> | ||
public EquivalencyAssertionOptions<TExpectation> ThenExcluding(Expression<Func<TCurrent, object>> expression) | ||
{ | ||
var nextPath = expression.GetMemberPath(); | ||
currentPathSelectionRule.CombinePath(nextPath); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Adds the selected collection to the <see cref="ThenExcluding{TNext}"/> chain. | ||
/// If this is the last call to <see cref="ThenExcluding{TNext}"/>, this ends the chain. | ||
/// </summary> | ||
public NestedExclusionOptionBuilder<TExpectation, TNext> ThenExcluding<TNext>( | ||
Expression<Func<TCurrent, IEnumerable<TNext>>> expression) | ||
{ | ||
var nextPath = expression.GetMemberPath(); | ||
currentPathSelectionRule.CombinePath(nextPath); | ||
return new NestedExclusionOptionBuilder<TExpectation, TNext>(this, 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.