Skip to content

Commit

Permalink
WithMapping now works in equivalency assertions on collections
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisdoomen committed Mar 19, 2022
1 parent 09c5821 commit 9dd1788
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
23 changes: 20 additions & 3 deletions Src/FluentAssertions/Common/MemberPath.cs
Expand Up @@ -11,9 +11,9 @@ namespace FluentAssertions.Common
/// </summary>
internal class MemberPath
{
private readonly string dottedPath;
private readonly Type reflectedType;
private readonly Type declaringType;
private string dottedPath;
private Type reflectedType;
private Type declaringType;

private string[] segments;

Expand All @@ -38,6 +38,10 @@ public MemberPath(string dottedPath)
this.dottedPath = dottedPath;
}

private MemberPath()
{
}

/// <summary>
/// Gets a value indicating whether the current object represents a child member of the <paramref name="candidate"/>
/// or that it is the parent of that candidate.
Expand Down Expand Up @@ -96,6 +100,19 @@ public bool HasSameParentAs(MemberPath path)
/// </summary>
public bool GetContainsSpecificCollectionIndex() => dottedPath.ContainsSpecificCollectionIndex();

/// <summary>
/// Returns a copy of the current object as if it represented an un-indexed item in a collection.
/// </summary>
public MemberPath WithCollectionAsRoot()
{
return new MemberPath
{
dottedPath = "[]." + dottedPath,
reflectedType = reflectedType,
declaringType = declaringType
};
}

private string[] Segments => segments ??= dottedPath.Split(new[] { '.', '[', ']' }, StringSplitOptions.RemoveEmptyEntries);

/// <summary>
Expand Down
Expand Up @@ -35,7 +35,13 @@ public MappedPathMatchingRule(string expectationMemberPath, string subjectMember

public IMember Match(IMember expectedMember, object subject, INode parent, IEquivalencyAssertionOptions options)
{
if (expectationPath.IsEquivalentTo(expectedMember.PathAndName))
MemberPath path = expectationPath;
if (expectedMember.RootIsCollection)
{
path = path.WithCollectionAsRoot();
}

if (path.IsEquivalentTo(expectedMember.PathAndName))
{
var member = MemberFactory.Find(subject, subjectPath.MemberName, expectedMember.Type, parent);
if (member is null)
Expand Down
38 changes: 38 additions & 0 deletions Tests/FluentAssertions.Equivalency.Specs/MemberMatchingSpecs.cs
Expand Up @@ -475,6 +475,44 @@ public void Mapping_works_with_exclusion_of_missing_members()
);
}

[Fact]
public void Can_map_members_of_a_root_collection()
{
// Arrange
var entity = new Entity
{
EntityId = 1,
Name = "Test"
};

var dto = new EntityDto
{
Id = 1,
Name = "Test"
};

var entityCol = new[] { entity };
var dtoCol = new[] { dto };

// Act / Assert
dtoCol.Should().BeEquivalentTo(entityCol, c =>
c.WithMapping<EntityDto>(s => s.EntityId, d => d.Id));
}

private class Entity
{
public int EntityId { get; init; }

public string Name { get; init; }
}

private class EntityDto
{
public int Id { get; init; }

public string Name { get; init; }
}

internal class ParentOfExpectationWithProperty2
{
public ExpectationWithProperty2[] Parent { get; }
Expand Down

0 comments on commit 9dd1788

Please sign in to comment.