Skip to content
Cyrille DUPUYDAUBY edited this page Jan 25, 2020 · 21 revisions

The following checks are avaible for all implementation of IEnumerable or IEnumerable.

Checks on properties

HasSize(expected), CountIs(expected)

Checks that the sut contains the expected number of entries, fails otherwise.

var sut = new int[10];
// this check succeeds
Check.That(sut).CountIs(10);
Check.That(sut).HasSize(10);
// this check fails
Check.That(sut).HasSize(11);

WhoseSize() (V2.6)

Allows to perform checks on the enumerable size.

var sut = new int[10];
// this check succeeds
Check.That(sut).WhoseSize().IsLessThan(15);

IsInAscendingOrder(...)

Checks that the sut contains item sorted in an ascending order. This check accepts an optional IComparer instance to verify a custom ordering.

// this checks succeed
Check.That(new[] { 1, 2, 3}).IsInAscendingOrder();
// this check fails
Check.That(new[] { 1, 3, 2}).IsInAscendingOrder();

IsInDescendingOrder(...)

Checks that the sut contains item sorted in an ascending order. This check accepts an optional IComparer instance to verify a custom ordering.

// this checks succeed
Check.That(new[] { 3, 2, 1}).IsInDescendingOrder();
// this check fails
Check.That(new[] { 3, 1, 2}).IsInDescendingOrder();

IsEmpty()

Checks that the sut is has no entry, fails otherwise.

var sut = new int[0];
// this checks succeed
Check.That(sut).IsNullOrEmpty();
// this check fails
Check.That(new int[1]).IsNullOrEmpty();

IsNullOrEmpty()

Checks that the sut is a null reference or has no entry, fails otherwise.

var sut = new int[0];
IEnumerable nullArray = null;
// those checks succeed
Check.That(sut).IsNullOrEmpty();
Check.That(nullArray).IsNullOrEmpty();
// this check fails
Check.That(new int[1]).IsNullOrEmpty();

Checks on content

IsEquivalentTo(...)

Checks that sut has the expected content, disregarding the order of items. Fails if any entry is missing or not expected.

var integers = new[] { 1, 2, 3};
// this check succeeds
Check.That(integers).IsEquivalentTo(1, 2, 3);
// this check succeeds
Check.That(integers).IsEquivalentTo(3, 2, 1);
// this check fails
Check.That(integers).IsEquivalentTo(3, 2);
// this check fails
Check.That(integers).IsEquivalentTo(3, 2, 1, 0);

Contains(...)

Checks that sut contains all provided entries. Duplicates value are allowed. Fails if any entry is absent from the enumeration.

var integers = new[] { 1, 2, 3, 4, 5, 666 };
// this check succeeds
Check.That(integers).Contains(3, 5, 666);
// this check fails
Check.That(integers).Contains(3, 5, 666, 0);

ContainsExactly(...)

Checks that sut contains all provided entries and no other. Fails if any entry is absent from the enumeration or if the enumeration contains extra entries.

var integers = new[] {3, 5, 666 };
// this check succeeds
Check.That(integers).ContainsExactly(3, 5, 666);
// this check fails
Check.That(integers).ContainsExactly(3, 5, 666, 0);

ContainsNoDuplicateItem()

Checks that sut contains no duplicate items. Fails it contains an item (or more) that is equal to another item.

// this check succeeds
Check.That(new[] {3, 5, 666 }).ContainsNoDuplicateItem();
// this check fails
Check.That(new[] {3, 5, 5, 666 }).ContainsNoDuplicateItem();

ContainsNoNull()

Checks that sut contains no null entry. Fails otherwise.

// this check succeeds
Check.That(new[] {3, 5, 666 }).ContainsNoNull();
// this check fails
Check.That(new[] {"test", "another", null}}).ContainsNoNull();

IsOnlyMadeOf(...)

Checks that sut contains only entries from a list of possible values, disregarding duplicated. It fails if the sut contains any value absent from the given list. Note that this implies this check is always successful when sut is empty.

var integers = new[] {3, 3, 5, 5, 5, 666 };
// this check succeeds
Check.That(integers).IsOnlyMadeOf(3, 5, 666);
// this check fails
Check.That(integers).IsOnlyMadeOf(3, 5, 666, 0);

IsSubsetOf(...)

Checks that sut is a subset of another enumerable. Example

var integers = new[] {3, 3, 5, 5, 5, 666 };
// this check succeeds
Check.That(new [] {3, 5, 5, 666}).IsSubjectOf(integers);
// this check fails
Check.That(new [] {3, 4, 5}).IsSubjectOf(integers);

Contains check extension

The Contains check can be refined using postfix keywords.

InThatOrder()

Requires that the IEnumerable content is in the expected order.

var integers = new[] { 1, 2, 3, 4, 5, 666 };
// this check succeeds
Check.That(integers).Contains(3, 5, 666).InThatOrder();
// this check fails
Check.That(integers).Contains(3, 5, 666, 1).InThatOrder();

Once()

Requires that the IEnumerable expected content is not duplicated.

var integers = new[] { 1, 2, 3, 4, 5, 666 };
// this check succeeds
Check.That(integers).Contains(3, 5, 666).Once();
// this check fails
Check.That(new[] { 1, 3, 3, 4, 5, 666 }).Contains(3, 5, 666).Once();

Only()

Requires that the IEnumerable contains only expected content.

var integers = new[] { 3, 5, 6 };
// this check succeeds
Check.That(integers).Contains(3, 5, 6).Only();
// this check fails
Check.That(new[] { 3, 4, 5, 6 }).Contains(3, 5, 666).Only();

Checks on element

The following checks play two roles: they are a check and an item selector. They can be followed by the Which() keyword which allows to check the selected item.

Which()

Can be used after a compatible check. Allows to check the item selected by the previous check. Example

var integers = new[] {3, 5, 666 };
// this check succeeds
Check.That(integers).HasElementAt(1).Which().IsEqualTo(5);
// this check fails
Check.That(integers).HasElementAt(0).Which().IsEqualTo(2);

HasElementAt(expected)

Checks that sut contains an entry at the expected index.

Can be followed by Which().

var integers = new[] {3, 5, 666 };
// those checks succeed
Check.That(integers).HasElementAt(2);
Check.That(integers).HasElementAt(1).Which().IsEqualTo(5);
// this check fails
Check.That(integers).HasElementAt(3);

HasFirstElement()

Checks that sut contains an entry at the first index. Fails if the array is empty.

Can be followed by Which().

var integers = new[] {3, 5, 666 };
// those checks succeed
Check.That(integers).HasFirstElement();
Check.That(integers).HasFirstElement().Which().IsEqualTo(3);
// this check fails
Check.That(new int[0]).HasFirstElement();

HasLastElement()

Checks that sut contains at least an entry. Fails if the array is empty.

Can be followed by Which().

var integers = new[] {3, 5, 666 };
// those checks succeed
Check.That(integers).HasLastElement();
Check.That(integers).HasLastElement().Which().IsEqualTo(666);
// this check fails
Check.That(new int[0]).HasLastElement();

HasOneElement()

Checks that sut contains one entry at the last index. Fails if the array is empty or has more than one item.

Can be followed by Which().

var integers = new[] {3};
// those checks succeed
Check.That(integers).HasOneElement();
Check.That(integers).HasOneElement().Which().IsEqualTo(3);
// this check fails
Check.That(new int[2]).HasOneElement();

HasElementThatMatches(predicate)

Checks that sut (at least) entry for which the predicate is true. Fails if non matches.

Can be followed by Which().

var integers = new[] {2, 6, 5};
// those checks succeed (at least one even entry)
Check.That(integers).HasElementThatMatches( x => x % 2 == 0);
Check.That(integers).HasElementThatMatches( x => x % 2 == 0).Which().IsEqualTo(2);
// this check fails
Check.That(integers).HasElementThatMatches( x => x < 0);

ContainsOnlyElementsThatMatch(predicate)

Checks that sut contains only entries for which the predicate is true. Fails otherwise.

var integers = new[] {2, 6};
// those checks succeed (at least one even entry)
Check.That(integers).ContainsOnlyElementsThatMatch( x => x % 2 == 0);
Check.That(integers).ContainsOnlyElementsThatMatch( x => x % 2 == 0).Which().IsEqualTo(2);
// this check fails
Check.That(integers).ContainsOnlyElementThatsMatch( x => x < 0);
Clone this wiki locally