Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WithMapping now works in equivalency assertions on collections #1858

Merged
merged 1 commit into from Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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