-
Notifications
You must be signed in to change notification settings - Fork 746
Allow Is.Ordered to Compare Null Values #1473
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
Comments
It looks like the original purpose of the null check was to handle the case where we are comparing properties of the objects within the collection. If that is the case, why not just apply the constraint in that situation? Or, if the object is null, then the property is null too? It would be good to dig back into the history of that null check to see why it was originally added. |
If memory serves, I simply decided that a null value would be an error in any reasonable use case. Of course, I may have been wrong, but I'd want to actually see the use case. There are lots of instances where NUnit doesn't do the typical or standard .NET thing. That's because we're a test framework and we support scenarios that make sense in writing test code. I'm not sure there's a useful test to write here, but I'm willing to be shown. |
@CharliePoole I get what you mean, but I do think that there are actual use cases for this. In fact, I found the problem, because I had this very problem: I'm working on a collection library, where items can be This was my test: [Test]
public void Sort_RandomCollectionWithNull_Sorted()
{
// Arrange
var items = GetStrings(Random);
var index = Random.Next(1, items.Length);
items[index] = null;
var collection = GetList(items, allowsNull: true);
// Act
collection.Sort();
// Assert
Assert.That(collection, Is.Ordered);
} |
It would be a breaking change for someone who expects nulls to cause a failure but probably one we could live with. Such a user would have to assert Has.None.Null.And.Ordered |
@lundmikkel It has been a long time since we talked about this. What do you think should happen with a null item in the collection if the assertion is... Assert.That(collection, Is.Ordered.By("X"); Personally, I'd be inclined to keep the error in that case. |
I have real-world code that relies on
|
Yes, the distinction is clear. I was specifically referring to the case of a null item. I a, the agree that for null properties, a null value should be acceptable. @lundmikkel This is your issue - do you agree? |
@CharliePoole This issue has been underway a long time, and I'm unsure whether we will hear from @lundmikkel. I agree with @jnm2 that it should fail if the collection item is null, and that it should work if the value of the property is null. I propose that we move the issue to the backlog and mark the issue up-for-grabs. |
I agree. I would have uses for this too. |
Me too! |
Up-for-grabs - good find @mikkelbu 😄 |
Sorry, guys; I totally missed you comments! So, I think the assertion should work if the item being checked is null. That would mean that checking null items should be fine, just like checking properties on non-null items should be fine. If you order by a property, it should fail if the object is null, since that is a regular null pointer exception. I haven't done any kind of work on this, nor will I be doing anything. So if @mikkelbu is up for it, be my guest. |
I can take this one. Just so I'm clear on the intent here, Assert.That(new[] { null, "Hello", "World" }, Is.Ordered); should not thrown an exception. Or is the intent to explicitly control this via something like Assert.That(new[] { null, "Hello", "World" }, Is.Ordered.AllowingNull); as originally suggested in the issue description? |
I see several people in favor of the first and no one set against it. I also am in favor of not requiring We need to make a note of the behavioral change in the release notes so that people can do |
Alright. I take it the docs should be updated as well to explain this new behavior? |
They won't be incorrect after the change but being explicit always improves docs. @rprouse can give you access to edit the docs. I'm removing the up-for-grabs label to indicate that the issue is yours. |
Oh wait, it's called |
Allows null values for simple ordering in CollectionOrderedConstraint. Null sorts less than everything else. An ArgumentNullException will still be thrown if ordering by a property. This is a potentially breaking change. If the previous behavior of throwing an exception if a collection contains null items is desired, it can be achieved using `Has.None.Null.And.Ordered`. Fixes nunit#1473
If you try to assert that an enumerable containing a
null
value is ordered:An exception is thrown:
This is because the
CollectionOrderedConstraint
explicitly checks fornull
values:This seems like an unnecessary strict assertion. In the MSDN documentation, it states that:
This explicitly says that
null
is before anything else, and implicitly thatnull
values are comparable and allowed.I understand that
null
values are often not desired, but they are a part of C#'s type system and it should be allowed to check ifnull
references are ordered – at least using a setting if the default behavior is to disallow them:(this is merely the best name I could come up with on the fly 😉)
The text was updated successfully, but these errors were encountered: