Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Selecting members during object graph assertions now honors the decla…
…ring type Fixes #956
- Loading branch information
1 parent
31b959d
commit 7cd0ea1
Showing
9 changed files
with
141 additions
and
70 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace FluentAssertions.Common | ||
{ | ||
/// <summary> | ||
/// Encapsulates a dotted candidate to a (nested) member of a type as well as the | ||
/// declaring type of the deepest member. | ||
/// </summary> | ||
internal class MemberPath | ||
{ | ||
private readonly Type declaringType; | ||
private readonly string dottedPath; | ||
private readonly List<string> segments = new List<string>(); | ||
|
||
public MemberPath(Type declaringType, string dottedPath) | ||
{ | ||
this.declaringType = declaringType; | ||
this.dottedPath = dottedPath; | ||
segments.AddRange(Segmentize(dottedPath)); | ||
} | ||
|
||
public bool IsParentOrChildOf(string candidate) | ||
{ | ||
return IsParent(candidate) || IsChild(candidate); | ||
} | ||
|
||
public bool IsSameAs(string candidate, Type memberDeclaringType) | ||
{ | ||
string[] candidateSegments = Segmentize(candidate); | ||
|
||
return memberDeclaringType == declaringType && candidateSegments.SequenceEqual(segments); | ||
} | ||
|
||
private bool IsChild(string candidate) | ||
{ | ||
string[] candidateSegments = Segmentize(candidate); | ||
|
||
return candidateSegments.Length > segments.Count && | ||
candidateSegments.Take(segments.Count).SequenceEqual(segments); | ||
} | ||
|
||
private bool IsParent(string candidate) | ||
{ | ||
string[] candidateSegments = Segmentize(candidate); | ||
|
||
return candidateSegments.Length < segments.Count | ||
&& candidateSegments.SequenceEqual(segments.Take(candidateSegments.Length)); | ||
} | ||
|
||
private static string[] Segmentize(string dottedPath) | ||
{ | ||
return dottedPath.Split(new[] { '.', '[', ']' }, StringSplitOptions.RemoveEmptyEntries); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return dottedPath; | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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