Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds WithMapping to map properties and fields between subject and exp…
…ectation
- Loading branch information
1 parent
35d018a
commit 46409e4
Showing
18 changed files
with
907 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
52 changes: 52 additions & 0 deletions
52
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,52 @@ | ||
using System; | ||
using System.Text.RegularExpressions; | ||
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 member name and the target type. | ||
/// </summary> | ||
internal class MappedMemberMatchingRule<TExpectation, TSubject> : IMemberMatchingRule | ||
{ | ||
private readonly string expectationMemberName; | ||
private readonly string subjectMemberName; | ||
|
||
public MappedMemberMatchingRule(string expectationMemberName, string subjectMemberName) | ||
{ | ||
if (Regex.IsMatch(expectationMemberName, @"[\.\[\]]")) | ||
{ | ||
throw new ArgumentException("The expectation's member name cannot be a nested path"); | ||
} | ||
|
||
if (Regex.IsMatch(subjectMemberName, $@"[\.\[\]]")) | ||
{ | ||
throw new ArgumentException("The subject's member name cannot be a nested path"); | ||
} | ||
|
||
this.expectationMemberName = expectationMemberName; | ||
this.subjectMemberName = subjectMemberName; | ||
} | ||
|
||
public IMember Match(IMember expectedMember, object subject, INode parent, IEquivalencyAssertionOptions options) | ||
{ | ||
if (parent.Type.IsSameOrInherits(typeof(TExpectation)) && subject is TSubject) | ||
{ | ||
if (expectedMember.Name == expectationMemberName) | ||
{ | ||
var member = MemberFactory.Find(subject, subjectMemberName, expectedMember.Type, parent); | ||
if (member is null) | ||
{ | ||
throw new ArgumentException( | ||
$"Subject of type {typeof(TSubject)} does not have member {subjectMemberName}"); | ||
} | ||
|
||
return member; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
using System; | ||
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 = MemberFactory.Find(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; | ||
} | ||
} | ||
} |
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.