You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all, thanks for a great testing framework. We use it extensively at work and are generally very pleased with it.
However, we've run recently into a weird situation where equality tests seem to be failing when they shouldn't. It's easier to explain with an example.
Consider a Person class, which implements both IEquatable<T> and IComparable<T>:
2 persons are equal if they have the same ID
the order between 2 persons is defined by their name (alphabetical). So a sorted list of 4 persons might look like [ Amy, Ben, Ben, Beth] (with no preference for which Ben comes first).
classTest{staticPersonperson1,person2;Establishcontext=()=>{person1=new Person(1,"John Doe");person2=new Person(2,"John Doe");// Same name, different person};Itshould_have_the_same_rank=()=> person1.CompareTo(person2).ShouldEqual(0);// assertion passesItshould_not_be_the_same_person=()=> person1.ShouldEqual(person2);// assertion passes (but should fail?)Itshould_be_2_different_persons_v1=()=> person1.Equals(person2).ShouldBeFalse();// assertion passes Itshould_be_2_different_persons_v2=()=> person1.ShouldNotEqual(person2);// assertion fails (but should pass?)}
For some reason, 2 of these assertions fail. We had a quick look at why that might be and it seems that MSpec uses a number of Comparer strategies, as defined here.
I could be wrong, but do these mean that MSpec gives IComparable<T> preference over IEquatable<T>? If so, that seems to me like a misunderstanding. I always thought IComparable was about ordering things and IEquatable was about defining what makes 2 things equal. So I think I would expect:
the EquatableComparer to come first in the list - and to be the only comparer used for cases which do implement IEquatable<T>. If I have explicitly defined the meaning of equality for a type, I'd expect this to be the only definition of equality.
IComparable<T> to not feature in the list, since it is about comparison, not about equality. To me that's a different use case (at least that 's how I read the docs: IComparable is about ordering and sorting, IEquatable is about equality). For instance, in the example above, 2 persons can have the same name, and therefore the same order, but that doesn't mean they are the same person.
Am I missing something? Is there an underlying reason for using IComparable<T> when determining equality? What is the recommended way to test equality for objects which implement IComparable<T>?
We also found this old issue, which is somewhat related.
Thanks for your help! :)
The text was updated successfully, but these errors were encountered:
Hi,
First of all, thanks for a great testing framework. We use it extensively at work and are generally very pleased with it.
However, we've run recently into a weird situation where equality tests seem to be failing when they shouldn't. It's easier to explain with an example.
Consider a
Person
class, which implements bothIEquatable<T>
andIComparable<T>
:[ Amy, Ben, Ben, Beth]
(with no preference for whichBen
comes first).And the following test:
For some reason, 2 of these assertions fail. We had a quick look at why that might be and it seems that MSpec uses a number of Comparer strategies, as defined here.
I could be wrong, but do these mean that MSpec gives
IComparable<T>
preference overIEquatable<T>
? If so, that seems to me like a misunderstanding. I always thought IComparable was about ordering things and IEquatable was about defining what makes 2 things equal. So I think I would expect:EquatableComparer
to come first in the list - and to be the only comparer used for cases which do implementIEquatable<T>
. If I have explicitly defined the meaning of equality for a type, I'd expect this to be the only definition of equality.IComparable<T>
to not feature in the list, since it is about comparison, not about equality. To me that's a different use case (at least that 's how I read the docs:IComparable
is about ordering and sorting,IEquatable
is about equality). For instance, in the example above, 2 persons can have the same name, and therefore the same order, but that doesn't mean they are the same person.Am I missing something? Is there an underlying reason for using
IComparable<T>
when determining equality? What is the recommended way to test equality for objects which implementIComparable<T>
?We also found this old issue, which is somewhat related.
Thanks for your help! :)
The text was updated successfully, but these errors were encountered: