Is.InRange Constraint Ambiguity?

87 views
Skip to first unread message

Pierce Louderback

unread,
Oct 19, 2013, 1:03:28 PM10/19/13
to nunit-...@googlegroups.com
Hello all,

I wanted to inquire about some behavior in NUnit that confused me at first. I think I now understand why it's doing it, but it seems like it could be worded differently. For reference, I am using NUnit 2.6.3 from NuGet.

I recently started using Constraints with Assert.That, and in this particular case I was trying to verify that something was in range, e.g.:

Assert.That(5.5f, Is.InRange<float>(5.4f, 5.6f));

Which obviously passes, because 5.5 is between 5.4 and 5.6. What doesn't work, however, is when I try to swap the values in Is.InRange, e.g.:

Assert.That(5.5f, Is.InRange<float>(5.6f, 5.4f));

I now get a test failure:
Expected in range (5.6, 5.4)
Actual 5.5f

I've come to the understanding that Is.InRange is checking between a low and a high, but the ambiguity for me lies in the fact that the Intellisense for Is.InRange just says "from" and "to", and then the test result says "expected in range val1, val2". I was confused because reading the test report says that it is not in range (even though the number itself is) and it took me a moment to figure out that it was failing because of the order of arguments. I ended up encountering this when I had a test involving negative numbers - at first I thought it was having difficulty with it being negative, so I tried with positive (as above) and got the same results.

So, in short, I wanted to ask if Is.InRange is intended to be used as min to max (thus forcing direction), despite that intellisense and the test report suggest direction independence.

In the meantime, I am using Assert.AreEqual with a delta, as this particular test is performing a verification that it be within 1% of a tolerance (but was failing with Is.InRange when test cases used negative numbers to create the tolerance values).

Thank you.

Charlie Poole

unread,
Oct 19, 2013, 3:41:25 PM10/19/13
to nunit-...@googlegroups.com
Hi Pierce,

Yes, as you figured out, the two values are really a min and a max. The reason is simply that your use case is not something I ever thought of when I wrote it! If you would like to file a bug at http://github.com/nunit/nunit-framework, it will be easy enough to fix it so your case works.

As an aside, I think that use of Assert.That(myValue, Is.EqualTo(5.4).Within(1).PerCent) is the best way to do a case like this. It's exactly the situation it was designed for. Is.InRange is needed in more general cases, but typical + or - tolerances work better with Within. The "classic" AreEqual, of course, only allows you to specify a fixed value, so you have to calculate it yourself if what you want is a percentage. Additionally, the Assert.That form says exactly what it is you are doing.

Charlie


--
You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nunit-discus...@googlegroups.com.
To post to this group, send email to nunit-...@googlegroups.com.
Visit this group at http://groups.google.com/group/nunit-discuss.

For more options, visit https://groups.google.com/groups/opt_out.

Pierce Louderback

unread,
Oct 19, 2013, 4:09:03 PM10/19/13
to nunit-...@googlegroups.com
Excellent, I completely missed the .Within modifier for Is.EqualTo. I actually only recently started using the Constraint model (found it in the documentation and I like the way it reads better), so I'm still a little dry on what's available until I actually use them.

Thanks for the response! (And, as an aside, thanks for all the hard work on NUnit; I've been attached to TDD ever since I started using it)

Pierce

Charlie Poole

unread,
Oct 19, 2013, 4:58:32 PM10/19/13
to nunit-...@googlegroups.com
Hi Pierce,

I'm glad that works for you. Thanks for the bug and congratulations on filing the first new nunit bug on GitHub!

Charlie
Reply all
Reply to author
Forward
0 new messages