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.
MIT. See LICENSE for full details.
Assert that two floating point values are approximately equal.
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
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. |
DUnitAssertError if the assertation fails.
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");
Assert that two floating point values are approximately equal using an epsilon value.
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
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. |
DUnitAssertError if the assertation fails.
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");
Assert that an array contains a particular value count.
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. |
DUnitAssertError if the assertation fails.
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");
Assert that an array is empty.
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. |
DUnitAssertError if the assertation fails.
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");
Assert that a range is empty.
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. |
DUnitAssertError if the assertation fails.
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");
Assert that a string ends with a particular string.
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. |
DUnitAssertError if the assertation fails.
"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");
Assert that two values are equal.
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. |
DUnitAssertError if the assertation fails.
123.assertEqual(123); "hello".assertEqual("hello"); // Assert a DUnitAssertError is thrown if assertEqual fails. 1.assertEqual(2).assertThrow!(DUnitAssertError)("Failed asserting equal");
Assert that a boolean value is false.
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. |
DUnitAssertError if the assertation fails.
false.assertFalse(); // Assert a DUnitAssertError is thrown if assertFalse fails. true.assertFalse().assertThrow!(DUnitAssertError)("Failed asserting false");
Assert that a value evaluates as false.
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. |
DUnitAssertError if the assertation fails.
false.assertFalsey(); [].assertFalsey(); null.assertFalsey(); 0.assertFalsey(); // Assert a DUnitAssertError is thrown if assertFalsey fails. true.assertFalsey().assertThrow!(DUnitAssertError)("Failed asserting falsey");
Assert that a value is greater than a threshold value.
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. |
DUnitAssertError if the assertation fails.
11.assertGreaterThan(10); // Assert a DUnitAssertError is thrown if assertGreaterThan fails. 11.assertGreaterThan(12).assertThrow!(DUnitAssertError)("Failed asserting greater than");
Assert that a value is greater than or equal to a threshold value.
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. |
DUnitAssertError if the assertation fails.
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");
Assert that an associative array contains a particular key.
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. |
DUnitAssertError if the assertation fails.
["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");
Assert that an array contains a particular value.
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. |
DUnitAssertError if the assertation fails.
"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");
Assert that a value is an instance of a type.
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. |
DUnitAssertError if the assertation fails.
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");
Assert that a value is less than a threshold value.
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. |
DUnitAssertError if the assertation fails.
9.assertLessThan(10); // Assert a DUnitAssertError is thrown if assertLessThan fails. 9.assertLessThan(8).assertThrow!(DUnitAssertError)("Failed asserting less than");
Assert that a value is less than or equal to a threshold value.
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. |
DUnitAssertError if the assertation fails.
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");
Assert that a string matches a regular expression.
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. |
DUnitAssertError if the assertation fails.
"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");
Assert that a value is null.
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. |
DUnitAssertError if the assertation fails.
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");
Assert that a string starts with a particular string.
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. |
DUnitAssertError if the assertation fails.
"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");
Assert that an expression throws an exception.
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. |
DUnitAssertError if the assertation fails.
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");
Assert that a boolean value is true.
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. |
DUnitAssertError if the assertation fails.
true.assertTrue(); // Assert a DUnitAssertError is thrown if assertTrue fails. false.assertTrue().assertThrow!(DUnitAssertError)("Failed asserting true");
Assert that a value evaluates as true.
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. |
DUnitAssertError if the assertation fails.
true.assertTruthy(); ["foo"].assertTruthy(); 1.assertTruthy(); // Assert a DUnitAssertError is thrown if assertTruthy fails. false.assertTruthy().assertThrow!(DUnitAssertError)("Failed asserting truthy");
Assert that a value is of a particular type.
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. |
DUnitAssertError if the assertation fails.
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");