dunit.toolkit

Assert toolkit for more expressive unit testing.

Many methods implement compile-time parameters (file, line) that are set at the call site. It is preferred that these parameters are ignored when using these methods.

License

MIT. See LICENSE for full details.

void assertApprox(A, B)(A value, B target, long ulps = 10, string message = "Failed asserting approximately equal", string file = __FILE__, size_t line = __LINE__) if (isFloatingPoint!(CommonType!(A, B)));

Assert that two floating point values are approximately equal.

See Also

http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

Parameters

A value The value used during the assertion.
B target The target value.
long ulps The maximum space between two approximately equal floating point numbers measured in units of least precision. A higher number means more approximation.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

float smallestFloatSubnormal = float.min_normal * float.epsilon;
smallestFloatSubnormal.assertApprox(-smallestFloatSubnormal);

double smallestDoubleSubnormal = double.min_normal * double.epsilon;
smallestDoubleSubnormal.assertApprox(-smallestDoubleSubnormal);

0.0f.assertApprox(-0.0f);
(-0.0f).assertApprox(0.0f);
0.0.assertApprox(-0.0);
(-0.0).assertApprox(0.0);
2.0f.assertApprox(1.999999f);
1.999999f.assertApprox(2.0f);
2.0.assertApprox(1.999999999999999);
1.999999999999999.assertApprox(2.0);

// The following tests pass but are open for debate whether or not they should.
float.max.assertApprox(float.infinity);
float.infinity.assertApprox(float.max);
double.infinity.assertApprox(double.max);
double.max.assertApprox(double.infinity);
float.nan.assertApprox(float.nan);
double.nan.assertApprox(double.nan);

// Assert a DUnitAssertError is thrown if assertApprox fails.
10f.assertApprox(0f).assertThrow!(DUnitAssertError)("Failed asserting approximately equal");

void assertApprox(A, B)(A value, B target, double epsilon, string message = "Failed asserting approximately equal", string file = __FILE__, size_t line = __LINE__) if (isFloatingPoint!(CommonType!(A, B)));

Assert that two floating point values are approximately equal using an epsilon value.

See Also

http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

Parameters

A value The value used during the assertion.
B target The target value.
double epsilon An epsilon value to be used as the maximum absolute and relative error in the comparison.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

float smallestFloatSubnormal = float.min_normal * float.epsilon;
smallestFloatSubnormal.assertApprox(-smallestFloatSubnormal, 0.00001);

double smallestDoubleSubnormal = double.min_normal * double.epsilon;
smallestDoubleSubnormal.assertApprox(-smallestDoubleSubnormal, 0.00001);

0.0f.assertApprox(-0.0f, 0.00001);
(-0.0f).assertApprox(0.0f, 0.00001);
0.0.assertApprox(-0.0, 0.00001);
(-0.0).assertApprox(0.0, 0.00001);
2.0f.assertApprox(1.99f, 0.01);
1.99f.assertApprox(2.0f, 0.01);
2.0.assertApprox(1.99, 0.01);
1.99.assertApprox(2.0, 0.01);

// The following tests pass but are open for debate whether or not they should.
float.max.assertApprox(float.infinity, 0.00001);
float.infinity.assertApprox(float.max, 0.00001);
double.infinity.assertApprox(double.max, 0.00001);
double.max.assertApprox(double.infinity, 0.00001);
float.nan.assertApprox(float.nan, 0.00001);
double.nan.assertApprox(double.nan, 0.00001);

// Assert a DUnitAssertError is thrown if assertApprox fails.
10f.assertApprox(0f, 0.00001).assertThrow!(DUnitAssertError)("Failed asserting approximately equal");

void assertCount(A)(A array, ulong count, string message = "Failed asserting array count", string file = __FILE__, size_t line = __LINE__) if (isArray!A || isAssociativeArray!A);

Assert that an array contains a particular value count.

Parameters

A array The array to interogate.
ulong count The amount of values the array should hold.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

int[string] associativeArray;
int[] dynamicArray;
string string_;

associativeArray.assertCount(0);
dynamicArray.assertCount(0);
string_.assertCount(0);
[].assertCount(0);

"Hello".assertCount(5);
[1, 2, 3, 4].assertCount(4);
["foo", "bar", "baz", "qux"].assertCount(4);
[["foo", "bar"], ["baz", "qux"]].assertCount(2);
["foo":1, "bar":2, "baz":3, "qux":4].assertCount(4);

// Assert a DUnitAssertError is thrown if assertCount fails.
associativeArray.assertCount(1).assertThrow!(DUnitAssertError)("Failed asserting array count");

void assertEmpty(A)(A array, string message = "Failed asserting empty array", string file = __FILE__, size_t line = __LINE__) if (isArray!A || isAssociativeArray!A);

Assert that an array is empty.

Parameters

A array The array to interogate.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

int[string] associativeArray;
int[] dynamicArray;
string string_;

associativeArray.assertEmpty();
dynamicArray.assertEmpty();
string_.assertEmpty();
[].assertEmpty();

// Assert a DUnitAssertError is thrown if assertEmpty fails.
[1].assertEmpty().assertThrow!(DUnitAssertError)("Failed asserting empty array");

void assertEmpty(R)(R range, string message = "Failed asserting empty range", string file = __FILE__, size_t line = __LINE__) if ((is(R == class) || is(R == struct)) && isInputRange!R);

Assert that a range is empty.

Parameters

R range The range to interogate.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

class A
{
	void popFront() {};
	@property bool empty() { return true; };
	@property int front() { return 0; };
}

struct B
{
	void popFront() {};
	enum bool empty = false;
	@property int front() { return 1; };
}

static assert(isInputRange!(A));
static assert(isInputRange!(B));

auto emptyRange = new A();
emptyRange.assertEmpty();

B infiniteRange = B.init;
infiniteRange.takeNone.assertEmpty();

// Assert a DUnitAssertError is thrown if assertEmpty fails.
infiniteRange.assertEmpty().assertThrow!(DUnitAssertError)("Failed asserting empty range");
infiniteRange.take(10).assertEmpty().assertThrow!(DUnitAssertError)("Failed asserting empty range");

void assertEndsWith(string value, string suffix, string message = "Failed asserting ends with", string file = __FILE__, size_t line = __LINE__);

Assert that a string ends with a particular string.

Parameters

string value The value used during the assertion.
string suffix The suffix to match.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

"foo bar".assertEndsWith("bar");
"baz qux".assertEndsWith("qux");

// Assert a DUnitAssertError is thrown if assertEndsWith fails.
"foo".assertEndsWith("bar").assertThrow!(DUnitAssertError)("Failed asserting ends with");

void assertEqual(A, B)(A value, B target, string message = "Failed asserting equal", string file = __FILE__, size_t line = __LINE__);

Assert that two values are equal.

Parameters

A value The value used during the assertion.
B target The target value.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

123.assertEqual(123);
"hello".assertEqual("hello");

// Assert a DUnitAssertError is thrown if assertEqual fails.
1.assertEqual(2).assertThrow!(DUnitAssertError)("Failed asserting equal");

void assertFalse(T)(T value, string message = "Failed asserting false", string file = __FILE__, size_t line = __LINE__);

Assert that a boolean value is false.

Parameters

T value The value used during the assertion.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

false.assertFalse();

// Assert a DUnitAssertError is thrown if assertFalse fails.
true.assertFalse().assertThrow!(DUnitAssertError)("Failed asserting false");

void assertFalsey(T)(T value, string message = "Failed asserting falsey", string file = __FILE__, size_t line = __LINE__);

Assert that a value evaluates as false.

Parameters

T value The value used during the assertion.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

false.assertFalsey();
[].assertFalsey();
null.assertFalsey();
0.assertFalsey();

// Assert a DUnitAssertError is thrown if assertFalsey fails.
true.assertFalsey().assertThrow!(DUnitAssertError)("Failed asserting falsey");

void assertGreaterThan(A, B)(A value, B threshold, string message = "Failed asserting greater than", string file = __FILE__, size_t line = __LINE__);

Assert that a value is greater than a threshold value.

Parameters

A value The value used during the assertion.
B threshold The threshold value.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

11.assertGreaterThan(10);

// Assert a DUnitAssertError is thrown if assertGreaterThan fails.
11.assertGreaterThan(12).assertThrow!(DUnitAssertError)("Failed asserting greater than");

void assertGreaterThanOrEqual(A, B)(A value, B threshold, string message = "Failed asserting greater than or equal", string file = __FILE__, size_t line = __LINE__);

Assert that a value is greater than or equal to a threshold value.

Parameters

A value The value used during the assertion.
B threshold The threshold value.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

10.assertGreaterThanOrEqual(10);
11.assertGreaterThanOrEqual(10);

// Assert a DUnitAssertError is thrown if assertGreaterThanOrEqual fails.
11.assertGreaterThanOrEqual(12).assertThrow!(DUnitAssertError)("Failed asserting greater than or equal");

void assertHasKey(A, B)(A haystack, B needle, string message = "Failed asserting array has key", string file = __FILE__, size_t line = __LINE__) if (isAssociativeArray!A);

Assert that an associative array contains a particular key.

Parameters

A haystack The associative array to interogate.
B needle The key the array should contain.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

["foo":1, "bar":2, "baz":3, "qux":4].assertHasKey("foo");
[1:"foo", 2:"bar", 3:"baz", 4:"qux"].assertHasKey(1);

// Assert a DUnitAssertError is thrown if assertHasKey fails.
["foo":"bar"].assertHasKey("baz").assertThrow!(DUnitAssertError)("Failed asserting array has key");

void assertHasValue(A, B)(A haystack, B needle, string message = "Failed asserting array has value", string file = __FILE__, size_t line = __LINE__) if (isArray!A || isAssociativeArray!A);

Assert that an array contains a particular value.

Parameters

A haystack The array to interogate.
B needle The value the array should contain.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

"Hello".assertHasValue("H");
[1, 2, 3, 4].assertHasValue(2);
["foo", "bar", "baz", "qux"].assertHasValue("foo");
[["foo", "bar"], ["baz", "qux"]].assertHasValue(["foo", "bar"]);
["foo":1, "bar":2, "baz":3, "qux":4].assertHasValue(4);

// Assert a DUnitAssertError is thrown if assertHasValue fails.
["foo":"bar"].assertHasValue("baz").assertThrow!(DUnitAssertError)("Failed asserting array has value");

void assertInstanceOf(A, B)(B value, string message = "Failed asserting instance of", string file = __FILE__, size_t line = __LINE__);

Assert that a value is an instance of a type.

Parameters

B value The value used during the assertion.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

interface A {}
class B : A {}
class C : B {}

auto b = new B();
auto c = new C();

b.assertInstanceOf!(Object)();
b.assertInstanceOf!(A)();
b.assertInstanceOf!(B)();

c.assertInstanceOf!(Object)();
c.assertInstanceOf!(A)();
c.assertInstanceOf!(B)();
c.assertInstanceOf!(C)();

// Assert a DUnitAssertError is thrown if assertInstanceOf fails.
b.assertInstanceOf!(C)().assertThrow!(DUnitAssertError)("Failed asserting instance of");

void assertLessThan(A, B)(A value, B threshold, string message = "Failed asserting less than", string file = __FILE__, size_t line = __LINE__);

Assert that a value is less than a threshold value.

Parameters

A value The value used during the assertion.
B threshold The threshold value.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

9.assertLessThan(10);

// Assert a DUnitAssertError is thrown if assertLessThan fails.
9.assertLessThan(8).assertThrow!(DUnitAssertError)("Failed asserting less than");

void assertLessThanOrEqual(A, B)(A value, B threshold, string message = "Failed asserting less than or equal", string file = __FILE__, size_t line = __LINE__);

Assert that a value is less than or equal to a threshold value.

Parameters

A value The value used during the assertion.
B threshold The threshold value.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

10.assertLessThanOrEqual(10);
9.assertLessThanOrEqual(10);

// Assert a DUnitAssertError is thrown if assertLessThanOrEqual fails.
9.assertLessThanOrEqual(8).assertThrow!(DUnitAssertError)("Failed asserting less than or equal");

void assertMatchRegex(string value, string pattern, string message = "Failed asserting match to regex", string file = __FILE__, size_t line = __LINE__);

Assert that a string matches a regular expression.

Parameters

string value The value used during the assertion.
string pattern The regular expression pattern.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

"foo".assertMatchRegex(r"^foo$");
"192.168.0.1".assertMatchRegex(r"((?:[\d]{1,3}\.){3}[\d]{1,3})");

// Assert a DUnitAssertError is thrown if assertMatchRegex fails.
"foo".assertMatchRegex(r"^bar$").assertThrow!(DUnitAssertError)("Failed asserting match to regex");

void assertNull(A)(A value, string message = "Failed asserting null", string file = __FILE__, size_t line = __LINE__) if (A.init is null);

Assert that a value is null.

Parameters

A value The value to assert as null.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

class T {}

string foo;
int[] bar;
T t;

foo.assertNull();
bar.assertNull();
t.assertNull();
null.assertNull();

// Assert a DUnitAssertError is thrown if assertNull fails.
"foo".assertNull().assertThrow!(DUnitAssertError)("Failed asserting null");

void assertStartsWith(string value, string prefix, string message = "Failed asserting starts with", string file = __FILE__, size_t line = __LINE__);

Assert that a string starts with a particular string.

Parameters

string value The value used during the assertion.
string prefix The prefix to match.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

"foo bar".assertStartsWith("foo");
"baz qux".assertStartsWith("baz");

// Assert a DUnitAssertError is thrown if assertStartsWith fails.
"foo bar".assertStartsWith("baz").assertThrow!(DUnitAssertError)("Failed asserting starts with");

void assertThrow(A : Throwable = Exception, B)(lazy B expression, string expressionMsg = null, string message = "Failed asserting throw", string file = __FILE__, size_t line = __LINE__);

Assert that an expression throws an exception.

Parameters

B expression The expression to evaluate in order to assert the exception is thrown.
string expressionMsg An optional expected message of the thrown exception.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

import core.exception : AssertError, RangeError;

class Foo : Exception
{
	this(string message)
	{
		super(message);
	}
}

class Bar
{
	public void baz()
	{
		throw new Foo("Thrown from baz.");
	}
}

auto bar = new Bar();
bar.baz().assertThrow();
bar.baz().assertThrow!(Foo)("Thrown from baz.");

delegate(){throw new Foo("Thrown from delegate.");}().assertThrow!(Exception)("Thrown from delegate.");

auto baz = [0, 1, 2];
baz[3].assertThrow!(RangeError)();

assert(false).assertThrow!(AssertError)("Assertion failure");

// Assert a DUnitAssertError is thrown if assertThrow fails.
null.assertThrow().assertThrow!(DUnitAssertError)("Failed asserting throw");

// Assert a DUnitAssertError is thrown if assertThrow fails due to mismatched error message.
baz[3].assertThrow!(RangeError)("Foo").assertThrow!(DUnitAssertError)("Failed asserting throw");

void assertTrue(T)(T value, string message = "Failed asserting true", string file = __FILE__, size_t line = __LINE__);

Assert that a boolean value is true.

Parameters

T value The value used during the assertion.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

true.assertTrue();

// Assert a DUnitAssertError is thrown if assertTrue fails.
false.assertTrue().assertThrow!(DUnitAssertError)("Failed asserting true");

void assertTruthy(T)(T value, string message = "Failed asserting truthy", string file = __FILE__, size_t line = __LINE__);

Assert that a value evaluates as true.

Parameters

T value The value used during the assertion.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

true.assertTruthy();
["foo"].assertTruthy();
1.assertTruthy();

// Assert a DUnitAssertError is thrown if assertTruthy fails.
false.assertTruthy().assertThrow!(DUnitAssertError)("Failed asserting truthy");

void assertType(A, B)(B value, string message = "Failed asserting type", string file = __FILE__, size_t line = __LINE__);

Assert that a value is of a particular type.

Parameters

B value The value used during the assertion.
string message The error message to display.
string file The file name where the error occurred. The value is added automatically at the call site.
size_t line The line where the error occurred. The value is added automatically at the call site.

Exceptions Thrown

DUnitAssertError if the assertation fails.

Examples

1.assertType!(int)();
"foo".assertType!(string)();
["bar"].assertType!(string[])();
['a'].assertType!(char[])();

// Assert a DUnitAssertError is thrown if assertType fails.
false.assertType!(string)().assertThrow!(DUnitAssertError)("Failed asserting type");