Skip to content

Commit

Permalink
You can exclude explicitly implemented properties from BeEquivalentTo…
Browse files Browse the repository at this point in the history
… via ExcludingExplicitlyImplementedProperties
  • Loading branch information
dennisdoomen committed Nov 26, 2024
1 parent 5062f49 commit 6a0a792
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ internal class AllPropertiesSelectionRule : IMemberSelectionRule
public IEnumerable<IMember> SelectMembers(INode currentNode, IEnumerable<IMember> selectedMembers,
MemberSelectionContext context)
{
MemberVisibility visibility = context.IncludedProperties | MemberVisibility.ExplicitlyImplemented |
MemberVisibility.DefaultInterfaceProperties;
MemberVisibility visibility = context.IncludedProperties;

IEnumerable<IMember> selectedProperties = context.Type
.GetProperties(visibility.ToMemberKind())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ public TSelf ExcludingFields()
/// </remarks>
public TSelf IncludingProperties()
{
includedProperties = MemberVisibility.Public;
includedProperties = MemberVisibility.Public | MemberVisibility.ExplicitlyImplemented |
MemberVisibility.DefaultInterfaceProperties;
return (TSelf)this;
}

Expand All @@ -282,7 +283,8 @@ public TSelf IncludingProperties()
/// </summary>
public TSelf IncludingInternalProperties()
{
includedProperties = MemberVisibility.Public | MemberVisibility.Internal;
includedProperties = MemberVisibility.Public | MemberVisibility.Internal | MemberVisibility.ExplicitlyImplemented |
MemberVisibility.DefaultInterfaceProperties;
return (TSelf)this;
}

Expand All @@ -298,6 +300,15 @@ public TSelf ExcludingProperties()
return (TSelf)this;
}

/// <summary>
/// Excludes properties that are explicitly implemented from the equivalency comparison.
/// </summary>
public TSelf ExcludingExplicitlyImplementedProperties()
{
includedProperties &= ~MemberVisibility.ExplicitlyImplemented;
return (TSelf)this;
}

/// <summary>
/// Instructs the comparison to exclude non-browsable members in the expectation (members set to
/// <see cref="EditorBrowsableState.Never"/>). It is not required that they be marked non-browsable in the subject. Use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,37 @@ public void Can_find_explicitly_implemented_property_on_the_subject()
person.Should().BeEquivalentTo(new { Name = "Bob" });
}

[Fact]
public void Can_exclude_explicitly_implemented_properties()
{
// Arrange
var subject = new Person
{
NormalProperty = "Normal",
};

((IPerson)subject).Name = "Bob";

var expectation = new Person
{
NormalProperty = "Normal",
};

((IPerson)expectation).Name = "Jim";

// Act / Assert
subject.Should().BeEquivalentTo(expectation, options => options.ExcludingExplicitlyImplementedProperties());
}

private interface IPerson
{
string Name { get; set; }
}

private class Person : IPerson
{
public string NormalProperty { get; set; }

string IPerson.Name { get; set; }
}

Expand Down
3 changes: 2 additions & 1 deletion docs/_pages/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ sidebar:
nav: "sidebar"
---

## 7.0 Alpha X
## 8.0 Alpha X

### What's new

Expand Down Expand Up @@ -41,6 +41,7 @@ sidebar:
* All assertions that support chaining using the `.Which` construct will now amend the caller identifier - [2539](https://github.com/fluentassertions/pull/2539)
* Introduced a `MethodInfoFormatter` and improved the `PropertyInfoFormatter` - [2539](https://github.com/fluentassertions/pull/2539)
* `Excluding()` / `For().Exclude()` and `Including()` on `BeEquivalentTo()` now also accepts an anonymous object to include/exclude multiple members at once - [#2488](https://github.com/fluentassertions/fluentassertions/pull/2488)
* You can exclude explicitly implemented properties from `BeEquivalentTo` via `ExcludingExplicitlyImplementedProperties` - [9999](https://github.com/fluentassertions/pull/9999)

### Fixes
* Fixed formatting error when checking nullable `DateTimeOffset` with
Expand Down

0 comments on commit 6a0a792

Please sign in to comment.