Skip to content

Commit

Permalink
Merge 6807f9b into 418a405
Browse files Browse the repository at this point in the history
  • Loading branch information
logiclrd committed Feb 27, 2022
2 parents 418a405 + 6807f9b commit a89ad7a
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ public IMember Match(IMember expectedMember, object subject, INode parent, IEqui

if (subjectMember is null)
{
Execute.Assertion.FailWith(
$"Expectation has {expectedMember.Description} that the other object does not have.");
bool ignoreMemberBecauseItIsNotBrowsable = config.ExcludeNonBrowsable && !expectedMember.IsBrowsable;

if (!ignoreMemberBecauseItIsNotBrowsable)
{
Execute.Assertion.FailWith(
$"Expectation has {expectedMember.Description} that the other object does not have.");
}
}

return subjectMember;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class StructuralEqualityEquivalencyStep : IEquivalencyStep
IMember matchingMember = FindMatchFor(selectedMember, context.CurrentNode, comparands.Subject, options);
if (matchingMember is not null)
{
if (!options.ExcludeNonBrowsable || matchingMember.IsBrowsable)
if (!options.ExcludeNonBrowsable || (selectedMember.IsBrowsable && matchingMember.IsBrowsable))
{
var nestedComparands = new Comparands
{
Expand Down
210 changes: 206 additions & 4 deletions Tests/FluentAssertions.Equivalency.Specs/SelectionRulesSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1723,29 +1723,231 @@ public void When_non_browsable_property_differs_excluding_non_browsable_members_
subject.Should().BeEquivalentTo(expectation, config => config.ExcludingNonBrowsableMembers());
}

[Fact]
public void When_property_is_non_browsable_only_in_subject_excluding_non_browsable_members_should_make_it_succeed()
{
// Arrange
var subject = new ClassWithNonBrowsableMembers() { NonBrowsableProperty = 0 };
var expectation = new ClassWithMatchingMemberNamesButEverythingIsBrowsable() { NonBrowsableProperty = 1 };

// Act & Assert
subject.Should().BeEquivalentTo(expectation, config => config.ExcludingNonBrowsableMembers());
}

[Fact]
public void When_property_is_non_browsable_only_in_expectation_excluding_non_browsable_members_should_make_it_succeed()
{
// Arrange
var subject = new ClassWithMatchingMemberNamesButEverythingIsBrowsable() { NonBrowsableProperty = 0 };
var expectation = new ClassWithNonBrowsableMembers() { NonBrowsableProperty = 1 };

// Act & Assert
subject.Should().BeEquivalentTo(expectation, config => config.ExcludingNonBrowsableMembers());
}

[Fact]
public void When_field_is_non_browsable_only_in_subject_excluding_non_browsable_members_should_make_it_succeed()
{
// Arrange
var subject = new ClassWithNonBrowsableMembers() { NonBrowsableField = 0 };
var expectation = new ClassWithMatchingMemberNamesButEverythingIsBrowsable() { NonBrowsableField = 1 };

// Act & Assert
subject.Should().BeEquivalentTo(expectation, config => config.ExcludingNonBrowsableMembers());
}

[Fact]
public void When_field_is_non_browsable_only_in_expectation_excluding_non_browsable_members_should_make_it_succeed()
{
// Arrange
var subject = new ClassWithMatchingMemberNamesButEverythingIsBrowsable() { NonBrowsableField = 0 };
var expectation = new ClassWithNonBrowsableMembers() { NonBrowsableField = 1 };

// Act & Assert
subject.Should().BeEquivalentTo(expectation, config => config.ExcludingNonBrowsableMembers());
}

public class NonBrowsableOnOneButMissingFromTheOther
{
[Fact]
public void When_property_is_missing_from_subject_excluding_non_browsable_members_should_make_it_succeed()
{
// Arrange
var subject =
new
{
BrowsableField = 1,
BrowsableProperty = 1,
ExplicitlyBrowsableField = 1,
ExplicitlyBrowsableProperty = 1,
AdvancedBrowsableField = 1,
AdvancedBrowsableProperty = 1,
NonBrowsableField = 2,
/* NonBrowsableProperty missing */
};

var expected =
new ClassWithNonBrowsableMembers
{
BrowsableField = 1,
BrowsableProperty = 1,
ExplicitlyBrowsableField = 1,
ExplicitlyBrowsableProperty = 1,
AdvancedBrowsableField = 1,
AdvancedBrowsableProperty = 1,
NonBrowsableField = 2,
NonBrowsableProperty = 2,
};

// Act & Assert
subject.Should().BeEquivalentTo(expected, opt => opt.ExcludingNonBrowsableMembers());
}

[Fact]
public void When_field_is_missing_from_subject_excluding_non_browsable_members_should_make_it_succeed()
{
// Arrange
var subject =
new
{
BrowsableField = 1,
BrowsableProperty = 1,
ExplicitlyBrowsableField = 1,
ExplicitlyBrowsableProperty = 1,
AdvancedBrowsableField = 1,
AdvancedBrowsableProperty = 1,
/* NonBrowsableField missing */
NonBrowsableProperty = 2,
};

var expected =
new ClassWithNonBrowsableMembers
{
BrowsableField = 1,
BrowsableProperty = 1,
ExplicitlyBrowsableField = 1,
ExplicitlyBrowsableProperty = 1,
AdvancedBrowsableField = 1,
AdvancedBrowsableProperty = 1,
NonBrowsableField = 2,
NonBrowsableProperty = 2,
};

// Act & Assert
subject.Should().BeEquivalentTo(expected, opt => opt.ExcludingNonBrowsableMembers());
}

[Fact]
public void When_property_is_missing_from_expectation_excluding_non_browsable_members_should_make_it_succeed()
{
// Arrange
var subject =
new ClassWithNonBrowsableMembers
{
BrowsableField = 1,
BrowsableProperty = 1,
ExplicitlyBrowsableField = 1,
ExplicitlyBrowsableProperty = 1,
AdvancedBrowsableField = 1,
AdvancedBrowsableProperty = 1,
NonBrowsableField = 2,
NonBrowsableProperty = 2,
};

var expected =
new
{
BrowsableField = 1,
BrowsableProperty = 1,
ExplicitlyBrowsableField = 1,
ExplicitlyBrowsableProperty = 1,
AdvancedBrowsableField = 1,
AdvancedBrowsableProperty = 1,
NonBrowsableField = 2,
/* NonBrowsableProperty missing */
};

// Act & Assert
subject.Should().BeEquivalentTo(expected, opt => opt.ExcludingNonBrowsableMembers());
}

[Fact]
public void When_field_is_missing_from_expectation_excluding_non_browsable_members_should_make_it_succeed()
{
// Arrange
var subject =
new ClassWithNonBrowsableMembers
{
BrowsableField = 1,
BrowsableProperty = 1,
ExplicitlyBrowsableField = 1,
ExplicitlyBrowsableProperty = 1,
AdvancedBrowsableField = 1,
AdvancedBrowsableProperty = 1,
NonBrowsableField = 2,
NonBrowsableProperty = 2,
};

var expected =
new
{
BrowsableField = 1,
BrowsableProperty = 1,
ExplicitlyBrowsableField = 1,
ExplicitlyBrowsableProperty = 1,
AdvancedBrowsableField = 1,
AdvancedBrowsableProperty = 1,
/* NonBrowsableField missing */
NonBrowsableProperty = 2,
};

// Act & Assert
subject.Should().BeEquivalentTo(expected, opt => opt.ExcludingNonBrowsableMembers());
}
}

private class ClassWithNonBrowsableMembers
{
public int BrowsableField;
public int BrowsableField = -1;

public int BrowsableProperty { get; set; }

[EditorBrowsable(EditorBrowsableState.Always)]
public int ExplicitlyBrowsableField;
public int ExplicitlyBrowsableField = -1;

[EditorBrowsable(EditorBrowsableState.Always)]
public int ExplicitlyBrowsableProperty { get; set; }

[EditorBrowsable(EditorBrowsableState.Advanced)]
public int AdvancedBrowsableField;
public int AdvancedBrowsableField = -1;

[EditorBrowsable(EditorBrowsableState.Advanced)]
public int AdvancedBrowsableProperty { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public int NonBrowsableField;
public int NonBrowsableField = -1;

[EditorBrowsable(EditorBrowsableState.Never)]
public int NonBrowsableProperty { get; set; }
}

private class ClassWithMatchingMemberNamesButEverythingIsBrowsable
{
public int BrowsableField = -1;

public int BrowsableProperty { get; set; }

public int ExplicitlyBrowsableField = -1;

public int ExplicitlyBrowsableProperty { get; set; }

public int AdvancedBrowsableField = -1;

public int AdvancedBrowsableProperty { get; set; }

public int NonBrowsableField = -1;

public int NonBrowsableProperty { get; set; }
}
}
}

0 comments on commit a89ad7a

Please sign in to comment.