Quantcast
Channel: Fluent Assertions
Viewing all articles
Browse latest Browse all 1402

New Post: GenericCollectionAssertions BeInOrder/NotBeInOrder

$
0
0

Today I created two extension methods for the GenericCollectionsAssertions class to check that the collection is in order or is not in order.  I'm posting them here for anyone else to use or (if they are worthy) for them to be incorporated into the FluentAssertions library:

internal static AndConstraint<GenericCollectionAssertions<T>> BeInOrder<T>(
this GenericCollectionAssertions<T> gca)
{
var orderedItems = gca.Subject.OrderBy(item => item);
return gca.Subject.Should().ContainInOrder(orderedItems);
}

internal static AndConstraint<GenericCollectionAssertions<T>> NotBeInOrder<T>(
this GenericCollectionAssertions<T> gca)
{
var orderedItems = gca.Subject.OrderBy(item => item).ToList();
var items = gca.Subject.ToList();

for (var i = 0; i < items.Count()-1; i++)
if (!orderedItems[i].Equals(items[i]))
return new AndConstraint>(gca);

Execute.Verification
.FailWith("Expected collection {0} to not be in order.", gca.Subject);

return new AndConstraint>(gca);
}

Tony


Viewing all articles
Browse latest Browse all 1402

Trending Articles