Skip to content

Commit

Permalink
Merge pull request #1953 from whymatter/issue-1952
Browse files Browse the repository at this point in the history
Make For/Excluding work with nested paths
  • Loading branch information
jnyrup committed Jul 7, 2022
2 parents 59e96d4 + 24641f6 commit c1283b7
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
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

0 comments on commit c1283b7

Please sign in to comment.