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

Assert.AreEqual when types A and B implements IEquatable<A> no longer works #2365

Closed
Evangelink opened this issue Feb 16, 2024 · 4 comments · Fixed by #2381
Closed

Assert.AreEqual when types A and B implements IEquatable<A> no longer works #2365

Evangelink opened this issue Feb 16, 2024 · 4 comments · Fixed by #2381

Comments

@Evangelink
Copy link
Member

If type A implements IEquatable<A> and type B also implements IEquatable<A> (yes of A), then you could call Assert.AreEqual(instanceOfA, instanceOfB) before and it would work, now T is expected to be either both A or both B, even though they could be "equatabled".

I mean it's questionable to compare two different types, but I haven't really seen this scenario discussed as breaking, so I thought I mention it here. Guess, I'll have to convert on the test, rather than inside the Equals functions.

class A : IEquatable<A>
{
    public bool Equals(A other) { ... }
}

class B : IEquatable<A>
{
    public bool Equals(A other) { ... }
}

// ...

[TestMethod]
public void Test()
{
    var instanceOfA = new A();
    var instanceOfB = new B();

    Assert.AreEqual(instanceOfA, instanceOfB);
}

Originally posted by @eXpl0it3r in #254 (comment)

@Evangelink
Copy link
Member Author

@eXpl0it3r Thanks for the report, I definitely didn't consider this case. I guess we could simply add a new overload as public static void AreEqual<T>(IEquatable<T>? expected, IEquatable<T>? actual) and it would fix this case without breaking some other scenario (from what I can think of).

@Evangelink
Copy link
Member Author

This is interesting case, I have never done this kind of equality. So with your example B.Equals(A) is true but A.Equals(B) is false. At least with the dumb implementation of having Equals(object obj) calling Equals(T t).

@eXpl0it3r
Copy link

eXpl0it3r commented Feb 19, 2024

We had a case that did a sort of conversion/simplification inside the equals method for mathematical objects, so both direction worked. Not sure I'd design it that way myself, as it seems to hide what's actually happening and thus makes it more error prone.

Thank you for the quick fix! ❤️ 🎉

@Evangelink
Copy link
Member Author

We will be shipping the bugfix release this week (hopefully Wednesday).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants