Skip to content

Type implementing IComparable<float> (or any IComparable) fails comparison. #3641

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

Closed
manfred-brands opened this issue Oct 6, 2020 · 1 comment · Fixed by #3642
Closed

Comments

@manfred-brands
Copy link
Member

manfred-brands commented Oct 6, 2020

Giving the following code:

private struct C : IComparable<float>
{
    public C(float v) => this.V = v;
    public float V { get; }
    public int CompareTo(float other) => this.V.CompareTo(other);
}

public void TestMethod()
{
    C x = new C(1.1f);
    float y = 1.0f;

    Assert.That(x.CompareTo(y), Is.GreaterThan(0));
    Assert.That(x,Is.GreaterThan(y));
}

The last test fails because NunitComparer.Compare checks y is IComparable before checking for xType.CompareTo(yType) and hence calls the Single.CompareTo(Object) method instead of C.CompareTo(float) method.

Can I switch the order such that the two tests on IComparable are last thereby preferencing IComparable<T> over IComparable?
I you think there are no unforeseen issues, I will raise a PR.

@jnm2
Copy link
Contributor

jnm2 commented Nov 21, 2020

This sounds good to me. I think we will only be preferring the most specific comparison available, and if that isn't what someone wants, they will have to use a custom comparer or do the comparison outside NUnit.

@jnm2 jnm2 added this to the 3.13 milestone Nov 21, 2020
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.

3 participants