Skip to content

Assert.AreEqual overloads for nullable double are not useful #3449

Closed

Description

@jnm2

We've had these four methods as far back as NUnit source history goes. The double? overloads are the same as the first two overloads except that they throw InvalidOperationException if you pass null. Since they can't be used to pass null, they don't do anything that the safer double overloads don't do.

Should we obsolete them or fix them? What would fixing them look like?

namespace NUnit.Framework
{
    public abstract class Assert
    {
        public static void AreEqual(double expected, double actual, double delta, string message, params object[] args);
        
        public static void AreEqual(double expected, double actual, double delta);
        
        public static void AreEqual(double expected, double? actual, double delta, string message, params object[] args);
        
        public static void AreEqual(double expected, double? actual, double delta);
    }
}

Activity

  1. CharliePoole commented on Dec 27, 2019

    @CharliePoole
    Member

    The double? overloads were added in April 2008, and released with NUnit 2.5.1. See nunit-legacy/nunitv2@c50f287#diff-b2261783c755ae0aa95cb67fd6162517R1019

    I agree they don't make sense.

  2. stevenaw commented on Feb 2, 2020

    @stevenaw
    Member

    I agree that if you intend to compare two double/double? values based on a delta, you expect both expected and actual to be non-null, and that obsoleting the double? overload makes the most sense.

    Though the double? for me strikes less as a "doesn't make sense" method and more a convenience method, in case a user's data (or class properties) are of type double?. I'm guessing that's why only the actual parameter is a double?. Though if we take this approach, we still don't need both, and might as well remove the non-nullable overload (since double is implicitly converted to double?). Though that wouldn't seem terribly clear to me as a user (or future maintainer) to have the expected and actual params be different data types.

    I guess I'm thinking that while it could be disruptive to some users to eventually remove the double? overload, the simplicity it gives to the API (as well as the consistency with other floating-point equality assert APIs) would be worth it.

  3. jnm2 commented on Feb 3, 2020

    @jnm2
    ContributorAuthor

    I'm also thinking that because the overloads we would remove don't behave as you would expect when passed null, if they are used it is probably by accident.

  4. stevenaw commented on May 18, 2020

    @stevenaw
    Member

    Would it make sense to obsolete these now, and remove them in NUnit 4?

  5. jnm2 commented on May 19, 2020

    @jnm2
    ContributorAuthor

    Obsoleting sounds good to me. /cc @nunit/framework-team

  6. rprouse commented on May 20, 2020

    @rprouse
    Member

    Sounds good to me.

  7. Antash commented on Feb 21, 2021

    @Antash
    Contributor

    @stevenaw, @rprouse, I want to fix this in case this is not in progress yet.

  8. rprouse commented on Feb 21, 2021

    @rprouse
    Member

    Sure, but how do you propose to fix? We'd decided to obsolete the method, but we could remove in 4.0.

  9. jnm2 commented on Feb 21, 2021

    @jnm2
    ContributorAuthor

    Would it be best to do both?

  10. rprouse commented on Feb 21, 2021

    @rprouse
    Member

    I would agree with @jnm2. We should remove from the master branch (4.0) and obsolete it in the v3.13 branch.

    Do you want to do one or both of these @Antash?

  11. Antash commented on Feb 22, 2021

    @Antash
    Contributor

    @rprouse Will do both, no problem!

  12. mikkelbu commented on Feb 22, 2021

    @mikkelbu
    Member

    Thanks. I've assigned the issue to you @Antash

  13. added this to the 4.0 milestone on Feb 28, 2021
  14. 2r2d commented on Jul 15, 2021

    @2r2d

    This feature makes this code

    Assert.AreEqual(1, myObject?.X);
    

    1 line longer to this:

    Assert.NotNull(myObject);
    Assert.AreEqual(1, myObject.Value.X);
    
  15. jnm2 commented on Jul 16, 2021

    @jnm2
    ContributorAuthor

    @2r2d Assert.AreEqual(1, myObject?.X); was already exactly equivalent in behavior to Assert.AreEqual(1, myObject.Value.X); without the NotNull assert, which is why we got rid of it. It appeared to do one thing but actually did something different.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions