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
1 parent
9b2d8e2
commit 4af5e23
Showing
15 changed files
with
704 additions
and
23 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
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
50 changes: 50 additions & 0 deletions
50
Src/FluentAssertions/Equivalency/Matching/MappedMemberMatchingRule.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,50 @@ | ||
using System; | ||
using System.Text.RegularExpressions; | ||
using FluentAssertions.Common; | ||
|
||
namespace FluentAssertions.Equivalency.Matching | ||
{ | ||
internal class MappedMemberMatchingRule<TExpectation, TSubject> : IMemberMatchingRule | ||
{ | ||
private readonly string expectationPropertyName; | ||
private readonly string subjectPropertyName; | ||
|
||
/// <summary> | ||
/// Creates an instance of this rule that matches the two direct properties of the specified subject and expectation types. | ||
/// </summary> | ||
public MappedMemberMatchingRule(string expectationPropertyName, string subjectPropertyName) | ||
{ | ||
// TODO: declaredtype vs reflectedtype | ||
|
||
if (Regex.IsMatch(@"(\.|\[|\])", expectationPropertyName)) | ||
{ | ||
throw new ArgumentException("The expectation's member name cannot be a nested path"); | ||
} | ||
|
||
if (Regex.IsMatch(@"(\.|\[|\])", subjectPropertyName)) | ||
{ | ||
throw new ArgumentException("The subject's member name cannot be a nested path"); | ||
} | ||
|
||
this.expectationPropertyName = expectationPropertyName; | ||
this.subjectPropertyName = subjectPropertyName; | ||
} | ||
|
||
public IMember Match(IMember expectedMember, object subject, INode parent, IEquivalencyAssertionOptions options) | ||
{ | ||
// TODO: What if the subject property does not exist | ||
// TODO: What if the expectation property does not exist | ||
// TODO: What if the expectation is null | ||
// TODO: What if the subject path is invalid. Can we even verify that? | ||
if (expectedMember.ReflectedType.IsSameOrInherits(typeof(TExpectation)) && subject is TSubject) | ||
{ | ||
if (expectedMember.Name == expectationPropertyName) | ||
{ | ||
return MemberFactory.Create(subject.GetType().GetProperty(subjectPropertyName), parent); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
Src/FluentAssertions/Equivalency/Matching/MappedPathMatchingRule.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,61 @@ | ||
using System; | ||
using System.Reflection; | ||
using FluentAssertions.Common; | ||
|
||
namespace FluentAssertions.Equivalency.Matching | ||
{ | ||
/// <summary> | ||
/// Allows mapping a member (property or field) of the expectation to a differently named member | ||
/// of the subject-under-test using a nested member path in the form of "Parent.NestedCollection[].Member" | ||
/// </summary> | ||
internal class MappedPathMatchingRule : IMemberMatchingRule | ||
{ | ||
private readonly MemberPath expectationPath; | ||
private readonly MemberPath subjectPath; | ||
|
||
public MappedPathMatchingRule(string expectationMemberPath, string subjectMemberPath) | ||
{ | ||
expectationPath = new MemberPath(expectationMemberPath); | ||
subjectPath = new MemberPath(subjectMemberPath); | ||
|
||
if (expectationPath.ContainsSpecificCollectionIndex || subjectPath.ContainsSpecificCollectionIndex) | ||
{ | ||
throw new ArgumentException("Mapping properties containing a collection index must use the [] format without specific index."); | ||
} | ||
|
||
if (!expectationPath.HasSameParentAs(subjectPath)) | ||
{ | ||
throw new ArgumentException("The member paths must have the same parent."); | ||
} | ||
} | ||
|
||
public IMember Match(IMember expectedMember, object subject, INode parent, IEquivalencyAssertionOptions options) | ||
{ | ||
if (expectationPath.IsEquivalentTo(expectedMember.PathAndName)) | ||
{ | ||
var member = FindSubjectMember(subject, subjectPath.MemberName, expectedMember.Type, parent); | ||
if (member is null) | ||
{ | ||
throw new ArgumentException( | ||
$"Subject of type {subject?.GetType().Name} does not have member {subjectPath.MemberName}"); | ||
} | ||
|
||
return member; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static IMember FindSubjectMember(object subject, string memberName, Type expectedMemberType, INode parent) | ||
{ | ||
PropertyInfo property = subject.GetType().FindProperty(memberName, expectedMemberType); | ||
if ((property is not null) && !property.IsIndexer()) | ||
{ | ||
return new Property(property, parent); | ||
} | ||
|
||
FieldInfo field = subject.GetType().FindField(memberName, expectedMemberType); | ||
return (field is not null) ? new Field(field, parent) : null; | ||
} | ||
} | ||
} |
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.