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 27, 2022
1 parent 09c5821 commit d63e6bc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Src/FluentAssertions/Common/MemberPath.cs
Expand Up @@ -96,6 +96,14 @@ 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(reflectedType, declaringType, "[]." + dottedPath);
}

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
1 change: 1 addition & 0 deletions docs/_pages/releases.md
Expand Up @@ -18,6 +18,7 @@ sidebar:
* `EnumAssertions.Be` did not determine the caller name - [#1835](https://github.com/fluentassertions/fluentassertions/pull/1835)
* Ensure `ExcludingMissingMembers` doesn't undo usage of `WithMapping` in `BeEquivalentTo` - [#1838](https://github.com/fluentassertions/fluentassertions/pull/1838)
* Better handling of NaN in various numeric assertions - [#1822](https://github.com/fluentassertions/fluentassertions/pull/1822)
* `WithMapping` in `BeEquivalentTo` now also works when the root is a collection - [#1858](https://github.com/fluentassertions/fluentassertions/pull/1858)

### Fixes (Extensibility)

Expand Down

0 comments on commit d63e6bc

Please sign in to comment.