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

Make For/Excluding work with nested paths #1953

Merged
merged 2 commits into from Jul 7, 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
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Common/MemberPath.cs
Expand Up @@ -81,7 +81,7 @@ private bool IsChildOf(MemberPath candidate)
public MemberPath AsParentCollectionOf(MemberPath nextPath)
{
var extendedDottedPath = dottedPath.Combine(nextPath.dottedPath, "[]");
return new MemberPath(declaringType, nextPath.reflectedType, extendedDottedPath);
return new MemberPath(nextPath.reflectedType, nextPath.declaringType, extendedDottedPath);
}

/// <summary>
Expand Down
99 changes: 99 additions & 0 deletions Tests/FluentAssertions.Equivalency.Specs/CollectionSpecs.cs
Expand Up @@ -765,6 +765,105 @@ public void When_property_in_collection_in_collection_is_excluded_it_should_not_
);
}

[Fact]
public void When_property_in_object_in_collection_is_excluded_it_should_not_throw()
{
// Arrange
var subject = new
{
Collection = new[]
{
new
{
Level = new
{
Text = "Actual"
}
}
}
};

var expected = new
{
Collection = new[]
{
new
{
Level = new
{
Text = "Expected"
}
}
}
};

// Act / Assert
subject.Should().BeEquivalentTo(expected,
options => options
.For(x => x.Collection)
.Exclude(x => x.Level.Text)
);
}

[Fact]
public void When_property_in_object_in_collection_in_object_in_collection_is_excluded_it_should_not_throw()
{
// Arrange
var subject = new
{
Collection = new[]
{
new
{
Level = new
{
Collection = new[]
{
new
{
Level = new
{
Text = "Actual"
}
}
}
}
}
}
};

var expected = new
{
Collection = new[]
{
new
{
Level = new
{
Collection = new[]
{
new
{
Level = new
{
Text = "Expected"
}
}
}
}
}
}
};

// Act / Assert
subject.Should().BeEquivalentTo(expected,
options => options
.For(x => x.Collection)
.For(x => x.Level.Collection)
.Exclude(x => x.Level)
);
}

[Fact]
public void A_nested_exclusion_can_be_followed_by_a_root_level_exclusion()
{
Expand Down
7 changes: 7 additions & 0 deletions docs/_pages/releases.md
Expand Up @@ -7,6 +7,13 @@ sidebar:
nav: "sidebar"
---

## Unreleased

### What's new

### Fixes
* Fix `For`/`Exclude` not excluding properties in objects in a collection - [#1953](https://github.com/fluentassertions/fluentassertions/pull/1953)

## 6.7.0

### What's new
Expand Down