Skip to content

Commit

Permalink
SAVEPOINT
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisdoomen committed Jan 14, 2023
1 parent a4fa412 commit 7e15016
Showing 1 changed file with 159 additions and 37 deletions.
196 changes: 159 additions & 37 deletions Tests/FluentAssertions.Equivalency.Specs/SelectionRulesSpecs.cs
Expand Up @@ -506,53 +506,175 @@ public void When_a_property_is_protected_it_should_be_ignored()
act.Should().NotThrow();
}

[Fact]
public void When_a_property_is_hidden_in_a_derived_class_it_should_ignore_it()
public class MemberHiding
{
// Arrange
var subject = new SubclassA<string> { Foo = "test" };
var expectation = new SubclassB<string> { Foo = "test" };
[Fact]
public void Ignores_properties_hidden_by_the_derived_class()
{
// Arrange
var subject = new SubclassAHidingProperty<string>
{
Property = "DerivedValue"
};

// Act
Action action = () => subject.Should().BeEquivalentTo(expectation);
((BaseWithFoo)subject).Property = "ActualBaseValue";

// Assert
action.Should().NotThrow();
}
var expectation = new SubclassBHidingProperty<string>
{
Property = "DerivedValue"
};

[Fact]
public void When_including_a_property_that_is_hidden_in_a_derived_class_it_should_select_the_correct_one()
{
// Arrange
var b1 = new ClassThatHidesBaseClassProperty();
var b2 = new ClassThatHidesBaseClassProperty();
((AnotherBaseWithFoo)expectation).Property = "ExpectedBaseValue";

// Act / Assert
b1.Should().BeEquivalentTo(b2, config => config.Including(b => b.Property));
}
// Act / Assert
subject.Should().BeEquivalentTo(expectation);
}

[Fact]
public void Excluding_a_property_hiding_a_base_class_property_should_not_reveal_the_latter()
{
// Arrange
var b1 = new ClassThatHidesBaseClassProperty();
var b2 = new ClassThatHidesBaseClassProperty();
[Fact]
public void Includes_hidden_property_of_the_base_when_using_a_reference_to_the_base()
{
// Arrange
var subject = new SubclassAHidingProperty<string>
{
Property = "ActualDerivedValue"
};

// Act
Action act = () => b1.Should().BeEquivalentTo(b2, config => config.Excluding(b => b.Property));
((BaseWithFoo)subject).Property = "BaseValue";

// Assert
act.Should().Throw<InvalidOperationException>().WithMessage("*No members were found *");
}
AnotherBaseWithFoo expectation = new SubclassBHidingProperty<string>
{
Property = "ExpectedDerivedValue"
};

private class ClassWithGuidProperty
{
public string Property { get; set; } = Guid.NewGuid().ToString();
}
expectation.Property = "BaseValue";

private class ClassThatHidesBaseClassProperty : ClassWithGuidProperty
{
public new string[] Property { get; set; }
// Act / Assert
subject.Should().BeEquivalentTo(expectation);
}

[Fact]
public void Run_type_typing_ignores_hidden_properties_even_when_using_a_reference_to_the_base_class()
{
// Arrange
var subject = new SubclassAHidingProperty<string>
{
Property = "DerivedValue"
};

((BaseWithFoo)subject).Property = "ActualBaseValue";

AnotherBaseWithFoo expectation = new SubclassBHidingProperty<string>
{
Property = "DerivedValue"
};

expectation.Property = "ExpectedBaseValue";

// Act / Assert
subject.Should().BeEquivalentTo(expectation, _ => _.RespectingRuntimeTypes());
}

[Fact]
public void Including_the_derived_property_excludes_the_hidden_property()
{
// Arrange
var subject = new SubclassAHidingProperty<string>
{
Property = "DerivedValue"
};

((BaseWithFoo)subject).Property = "ActualBaseValue";

var expectation = new SubclassBHidingProperty<string>
{
Property = "DerivedValue"
};

((AnotherBaseWithFoo)expectation).Property = "ExpectedBaseValue";

// Act / Assert
subject.Should().BeEquivalentTo(expectation, _ => _
.Including(_ => _.Property));
}

[Fact]
public void Excluding_the_property_hiding_the_base_class_one_does_not_reveal_the_latter()
{
// Arrange
var subject = new SubclassAHidingProperty<string>();

((BaseWithFoo)subject).Property = "ActualBaseValue";

var expectation = new SubclassBHidingProperty<string>();

((AnotherBaseWithFoo)expectation).Property = "ExpectedBaseValue";

// Act
Action act = () => subject.Should().BeEquivalentTo(expectation, _ => _
.Excluding(b => b.Property));

// Assert
act.Should().Throw<InvalidOperationException>().WithMessage("*No members were found *");
}

public class BaseWithFoo
{
public object Property { get; set; }
}

public class SubclassAHidingProperty<T> : BaseWithFoo
{
public new T Property { get; set; }
}

public class AnotherBaseWithFoo
{
public object Property { get; set; }
}

public class SubclassBHidingProperty<T> : AnotherBaseWithFoo
{
public new T Property
{
get; set;
}
}

[Fact]
public void Can_compare_objects_that_hide_a_base_class_field()
{
// Arrange
var subject = new ClassHidingBaseClassField { Id = "Foo" };

var expectation = new ClassHidingBaseClassField { Id = "Foo" };

// Act / Assert
subject.Should().BeEquivalentTo(expectation);
}

[Fact]
public void Foo()
{
// Arrange
var subject = new ClassHidingBaseClassField { Id = "Foo" };

ClassWithField expectation = new ClassHidingBaseClassField { Id = "Foo" };

// Act / Assert
subject.Should().BeEquivalentTo(expectation);
}

internal class ClassWithField
{
#pragma warning disable CS0649
public string Id;
#pragma warning restore CS0649
}

internal class ClassHidingBaseClassField : ClassWithField
{
public new string Id;
}
}

[Fact]
Expand Down

0 comments on commit 7e15016

Please sign in to comment.