Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparable type assertions referential equality #2046

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 26 additions & 9 deletions Tests/FluentAssertions.Specs/Numeric/ComparableSpecs.cs
Expand Up @@ -21,20 +21,18 @@ public void When_two_instances_are_equal_it_should_succeed()
}

[Fact]
public void When_two_instances_are_the_same_reference_but_are_not_considered_equal_it_should_not_succeed()
public void When_two_instances_are_the_same_reference_but_are_not_considered_equal_it_should_succeed()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I can't get my head around this one ;-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's quite unlikely that someone would implement a type that is not reflexive.

{
// Arrange
var subject = new SameInstanceIsNotEqualClass();
var other = subject;

// Act
Action act = () => subject.Should().Be(other, "they have the same property values");
Action act = () => subject.Should().Be(other);

// Assert
act
.Should().Throw<XunitException>()
.WithMessage(
"Expected*SameInstanceIsNotEqualClass*because they have the same property values, but found*SameInstanceIsNotEqualClass*.");
act.Should().NotThrow(
"This is inconsistent with the behavior ObjectAssertions.Be but is how ComparableTypeAssertions.Be has always worked.");
}

[Fact]
Expand All @@ -58,7 +56,7 @@ public void When_two_instances_are_not_equal_it_should_throw()
public class NotBe
{
[Fact]
public void When_two_references_to_the_same_instance_are_not_equal_it_should_succeed()
public void When_two_references_to_the_same_instance_are_not_equal_it_should_throw()
{
// Arrange
var subject = new SameInstanceIsNotEqualClass();
Expand All @@ -68,7 +66,8 @@ public void When_two_references_to_the_same_instance_are_not_equal_it_should_suc
Action act = () => subject.Should().NotBe(other);

// Assert
act.Should().NotThrow();
act.Should().Throw<XunitException>(
"This is inconsistent with the behavior ObjectAssertions.Be but is how ComparableTypeAssertions.Be has always worked.");
}

[Fact]
Expand Down Expand Up @@ -120,6 +119,21 @@ public void When_a_value_is_not_equal_to_one_of_the_specified_values_it_should_t
.WithMessage("Expected value to be one of {4, 5} because those are the valid values, but found 3.");
}

[Fact]
public void When_two_instances_are_the_same_reference_but_are_not_considered_equal_it_should_succeed()
{
// Arrange
var subject = new SameInstanceIsNotEqualClass();
var other = subject;

// Act
Action act = () => subject.Should().BeOneOf(other);

// Assert
act.Should().NotThrow(
"This is inconsistent with the behavior ObjectAssertions.Be but is how ComparableTypeAssertions.Be has always worked.");
}

[Fact]
public void When_a_value_is_equal_to_one_of_the_specified_values_it_should_succeed()
{
Expand Down Expand Up @@ -656,7 +670,7 @@ public override string ToString()
}
}

public class SameInstanceIsNotEqualClass
public class SameInstanceIsNotEqualClass : IComparable<SameInstanceIsNotEqualClass>
{
public override bool Equals(object obj)
{
Expand All @@ -667,6 +681,9 @@ public override int GetHashCode()
{
return 1;
}

int IComparable<SameInstanceIsNotEqualClass>.CompareTo(SameInstanceIsNotEqualClass other) =>
throw new NotSupportedException("This type is meant for assertions using Equals()");
}

public class EquatableOfInt : IComparable<EquatableOfInt>
Expand Down