Dunit Assertions

Like most xUnit frameworks, dunit provides a set of assertions that you can use in your tests. Dunit's expect module supports a fluent interface, like that of NUnit, which can represent a large variety of constraints with few methods to recall.

As an example:

// [1, 2, 5] cannot contain 2 elements equal to 2
expect ([1, 2, 5], not.has (2).equal_to (2));

API documentation

expect(T)(T value, IConstraint constraint)

Start an assertion based on the given value.

equals

aliases: equal, equalTo, equal_to

Succeeds if the test item and the expected item compare as equal using the "==" operator.

expect(5, equals(5.0)); // true (automatic conversion)
expect(this, equals(new Object()));

sameAs

aliases: same, same_as

Succeeds if the test item and the expected item compare as equal using the "is" operator.

// False: 5.0 has a different bit pattern than 5
expect(5, sameAs(5.0));
// False: 'this' and 'new Object' point to different things
expect(this, sameAs(new Object()));
// True (obviously)
expect(this, sameAs(this));

greaterThan

aliases: greater_than, greaterthan

Succeeds if the test item is greater than the expected item.

// True: 5 > 3
expect(5, greaterThan(3));
// True: trivially, all unsigned are greater than all negative
expect(5UL, greaterThan(-2));

lessThan

aliases: less_than, lessthan

Succeeds if the test item is less than the expected item.

expect(5, lessThan(5.1));

has(int min, int max)

has(int count)

aliases: have

Succeeds if the test item is a collection containing the appropriate number of elements that match the constraints that follow.

expect([1, 2, 3], has(1, 2).lessThan(3));
expect([1, 2, 3], has(3).lessThan(4));

hasNone

Equivalent to has(0).

hasOne

Equivalent to has(1).

hasSome

aliases: havesome, have_some, hasSome, hassome, has_some

Equivalent to has(1, int.max).

not

Aliases: what, not's not good enough for you?

Negates the assertion. Anything appearing after it is negated; anything appearing before it is not negated. This has more of an effect in collection constraints:

// This one succeeds because the array doesn't have two things equal to 2
expect ([1, 2, 5], not.has (2).equalto (2));
// This one succeeds because the array has exactly 2 things equal to 2
expect ([1, 2, 5], has (2).not.equalto (2));

has..where (T)(bool delegate (T) sieve)

Aliases: see not.

Succeeds if the test item is a collection of T that contains an appropriate number of items where sieve(item) returns true.

expect ([1, 2, 3, 4, 5], has (3).where ((int i) { return i % 2 == 1; }));

isFalse

Aliases: is_false, isfalse

Succeeds if the original object is a boolean and is false.

expect ((1 == 0), isFalse);

isTrue

Aliases: is_true, istrue

Succeeds if the original object is a boolean and is true.

expect ((1 == 1), isTrue);

isNull

Aliases: isnull, is_null

Succeeds if the original object compares same as null.

expect (null, isNull);

isNaN

Aliases: is_nan, isnan, nan

Succeeds if isnan returns true for the original object.

expect (float.nan, isNaN);

isA(T)

Aliases: instanceOf, instanceof, instance_of

Succeeds if the original object can be cast to an object of type T.

expect (cast(MyBaseClass)new MyClass, instanceof!(MyClass));