Skip to content

Commit

Permalink
Fix ExcludingMissingMembers from reverting WithMapping usages
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisdoomen committed Mar 8, 2022
1 parent d93bc09 commit 98e48ec
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,12 @@ public TSelf Including(Expression<Func<IMemberInfo, bool>> predicate)
}

/// <summary>
/// Tries to match the members of the subject with equally named members on the expectation. Ignores those
/// members that don't exist on the expectation and previously registered matching rules.
/// Tries to match the members of the expectation with equally named members on the subject. Ignores those
/// members that don't exist on the subject and previously registered matching rules.
/// </summary>
public TSelf ExcludingMissingMembers()
{
ClearMatchingRules();
matchingRules.RemoveAll(x => x is MustMatchByNameRule);
matchingRules.Add(new TryMatchByNameRule());
return (TSelf)this;
}
Expand All @@ -391,7 +391,7 @@ public TSelf ExcludingMissingMembers()
/// </summary>
public TSelf ThrowingOnMissingMembers()
{
ClearMatchingRules();
matchingRules.RemoveAll(x => x is TryMatchByNameRule);
matchingRules.Add(new MustMatchByNameRule());
return (TSelf)this;
}
Expand Down Expand Up @@ -463,7 +463,7 @@ public void WithoutSelectionRules()
/// </summary>
public void WithoutMatchingRules()
{
ClearMatchingRules();
matchingRules.Clear();
}

/// <summary>
Expand Down Expand Up @@ -844,12 +844,7 @@ private void RemoveSelectionRule<T>()
{
selectionRules.RemoveAll(selectionRule => selectionRule is T);
}

private void ClearMatchingRules()
{
matchingRules.Clear();
}


protected TSelf AddSelectionRule(IMemberSelectionRule selectionRule)
{
selectionRules.Add(selectionRule);
Expand Down
46 changes: 46 additions & 0 deletions Tests/FluentAssertions.Equivalency.Specs/MemberMatchingSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,52 @@ public void The_member_name_on_a_nested_type_mapping_must_be_a_valid_member()
.WithMessage("*does not have member NonExistingProperty*");
}

[Fact]
public void Exclusion_of_missing_members_works_with_mapping()
{
// Arrange
var subject = new
{
Property1 = 1
};

var expectation = new
{
Property2 = 2,
Ignore = 3
};

// Act / Assert
subject.Should()
.NotBeEquivalentTo(expectation, opt => opt
.WithMapping("Property2", "Property1")
.ExcludingMissingMembers()
);
}

[Fact]
public void Mapping_works_with_exclusion_of_missing_members()
{
// Arrange
var subject = new
{
Property1 = 1
};

var expectation = new
{
Property2 = 2,
Ignore = 3
};

// Act / Assert
subject.Should()
.NotBeEquivalentTo(expectation, opt => opt
.ExcludingMissingMembers()
.WithMapping("Property2", "Property1")
);
}

internal class ParentOfExpectationWithProperty2
{
public ExpectationWithProperty2[] Parent { get; }
Expand Down

0 comments on commit 98e48ec

Please sign in to comment.