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 aaa1529 commit 8e7a3cf
Showing 1 changed file with 133 additions and 4 deletions.
137 changes: 133 additions & 4 deletions Tests/FluentAssertions.Equivalency.Specs/SelectionRulesSpecs.cs
Expand Up @@ -617,28 +617,157 @@ public void Excluding_the_property_hiding_the_base_class_one_does_not_reveal_the
act.Should().Throw<InvalidOperationException>().WithMessage("*No members were found *");
}

public class BaseWithProperty
private class BaseWithProperty
{
public object Property { get; set; }
}

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

public class AnotherBaseWithProperty
private class AnotherBaseWithProperty
{
public object Property { get; set; }
}

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

[Fact]
public void Ignores_fields_hidden_by_the_derived_class()
{
// Arrange
var subject = new SubclassAHidingField<string>
{
Field = "DerivedValue"
};

((BaseWithField)subject).Field = "ActualBaseValue";

var expectation = new SubclassBHidingField<string>
{
Field = "DerivedValue"
};

((AnotherBaseWithField)expectation).Field = "ExpectedBaseValue";

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

[Fact]
public void Includes_hidden_field_of_the_base_when_using_a_reference_to_the_base()
{
// Arrange
var subject = new SubclassAHidingField<string>
{
Field = "ActualDerivedValue"
};

((BaseWithField)subject).Field = "BaseValue";

AnotherBaseWithField expectation = new SubclassBHidingField<string>
{
Field = "ExpectedDerivedValue"
};

expectation.Field = "BaseValue";

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

[Fact]
public void Run_type_typing_ignores_hidden_fields_even_when_using_a_reference_to_the_base_class()
{
// Arrange
var subject = new SubclassAHidingField<string>
{
Field = "DerivedValue"
};

((BaseWithField)subject).Field = "ActualBaseValue";

AnotherBaseWithField expectation = new SubclassBHidingField<string>
{
Field = "DerivedValue"
};

expectation.Field = "ExpectedBaseValue";

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

[Fact]
public void Including_the_derived_field_excludes_the_hidden_field()
{
// Arrange
var subject = new SubclassAHidingField<string>
{
Field = "DerivedValue"
};

((BaseWithField)subject).Field = "ActualBaseValue";

var expectation = new SubclassBHidingField<string>
{
Field = "DerivedValue"
};

((AnotherBaseWithField)expectation).Field = "ExpectedBaseValue";

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

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

((BaseWithField)subject).Field = "ActualBaseValue";

var expectation = new SubclassBHidingField<string>();

((AnotherBaseWithField)expectation).Field = "ExpectedBaseValue";

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

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

public class BaseWithField
{
public object Field;
}

public class SubclassAHidingField<T> : BaseWithField
{
public new T Field;
}

public class AnotherBaseWithField
{
public object Field;
}

public class SubclassBHidingField<T> : AnotherBaseWithField
{
public new T Field;
}
}

[Fact]
Expand Down

0 comments on commit 8e7a3cf

Please sign in to comment.