Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
std.experimental.testing.should
This module implements custom assertions via shouldXXX functions
that throw exceptions containing information about why the assertion
failed.
- class UnitTestException: object.Exception;
- An exception to signal that a test case has failed.
- void shouldBeTrue(E)(lazy E condition, in string file = __FILE__, in size_t line = __LINE__);
- Verify that the condition is true.Throws:UnitTestException on failure.Examples:
shouldBeTrue(true);
- void shouldBeFalse(E)(lazy E condition, in string file = __FILE__, in size_t line = __LINE__);
- Verify that the condition is false.Throws:UnitTestException on failure.Examples:
shouldBeFalse(false);
- void shouldEqual(V, E)(V value, E expected, in string file = __FILE__, in size_t line = __LINE__);
- Verify that two values are the same. Floating point values are compared using std.math.approxEqual.Throws:UnitTestException on failureExamples:
shouldEqual(true, true); shouldEqual(false, false); shouldEqual(1, 1) ; shouldEqual("foo", "foo"); shouldEqual(1.0, 1.0) ; shouldEqual([2, 3], [2, 3]); shouldEqual(iota(3), [0, 1, 2]); shouldEqual([[0, 1], [0, 1, 2]], [[0, 1], [0, 1, 2]]); shouldEqual([[0, 1], [0, 1, 2]], [iota(2), iota(3)]); shouldEqual([iota(2), iota(3)], [[0, 1], [0, 1, 2]]); shouldEqual(3.0, 3.00001); //approximately equal
- void shouldNotEqual(V, E)(V value, E expected, in string file = __FILE__, in size_t line = __LINE__);
- Verify that two values are not the same.Throws:UnitTestException on failureExamples:
shouldNotEqual(true, false); shouldNotEqual(1, 2); shouldNotEqual("f", "b"); shouldNotEqual(1.0, 2.0); shouldNotEqual([2, 3], [2, 3, 4]);
- void shouldBeNull(T)(in T value, in string file = __FILE__, in size_t line = __LINE__);
- Verify that the value is null.Throws:UnitTestException on failureExamples:
shouldBeNull(null) ;
- void shouldNotBeNull(T)(in T value, in string file = __FILE__, in size_t line = __LINE__);
- Verify that the value is not null.Throws:UnitTestException on failureExamples:
class Foo { this(int i) { this.i = i; } override string toString() const { import std.conv: to; return i.to!string; } int i; } shouldNotBeNull(new Foo(4)) ; shouldEqual(new Foo(5), new Foo(5)); assertFail(shouldEqual(new Foo(5), new Foo(4))); shouldNotEqual(new Foo(5), new Foo(4)) ; assertFail(shouldNotEqual(new Foo(5), new Foo(5)));
- void shouldBeIn(T, U)(in T value, in U container, in string file = __FILE__, in size_t line = __LINE__) if (isAssociativeArray!U);
- Verify that the value is in the container.Throws:UnitTestException on failure
- void shouldBeIn(T, U)(in T value, U container, in string file = __FILE__, in size_t line = __LINE__) if (!isAssociativeArray!U && isInputRange!U);
- Verify that the value is in the container.Throws:UnitTestException on failureExamples:
shouldBeIn(4, [1, 2, 4]); shouldBeIn("foo", ["foo" : 1]);
- void shouldNotBeIn(T, U)(in T value, in U container, in string file = __FILE__, in size_t line = __LINE__) if (isAssociativeArray!U);
- Verify that the value is not in the container.Throws:UnitTestException on failure
- void shouldNotBeIn(T, U)(in T value, U container, in string file = __FILE__, in size_t line = __LINE__) if (!isAssociativeArray!U && isInputRange!U);
- Verify that the value is not in the container.Throws:UnitTestException on failureExamples:
shouldNotBeIn(3.5, [1.1, 2.2, 4.4]); shouldNotBeIn(1.0, [2.0 : 1, 3.0 : 2]);
- void shouldThrow(T : Throwable = Exception, E)(lazy E expr, in string file = __FILE__, in size_t line = __LINE__);
- Verify that expr throws the templated Exception class. This succeeds if the expression throws a child class of the template parameter.Throws:UnitTestException on failure (when expr does not throw the expected exception)
- void shouldThrowExactly(T : Throwable = Exception, E)(lazy E expr, in string file = __FILE__, in size_t line = __LINE__);
- Verify that expr throws the templated Exception class. This only succeeds if the expression throws an exception of the exact type of the template parameter.Throws:UnitTestException on failure (when expr does not throw the expected exception)
- void shouldNotThrow(T : Throwable = Exception, E)(lazy E expr, in string file = __FILE__, in size_t line = __LINE__);
- Verify that expr does not throw the templated Exception class.Throws:UnitTestException on failure
- void shouldBeEmpty(R)(R rng, in string file = __FILE__, in size_t line = __LINE__) if (isInputRange!R);
- Verify that rng is empty.Throws:UnitTestException on failure.
- void shouldBeEmpty(T)(in T aa, in string file = __FILE__, in size_t line = __LINE__) if (isAssociativeArray!T);
- Verify that aa is empty.Throws:UnitTestException on failure.Examples:
int[] ints; string[] strings; string[string] aa; shouldBeEmpty(ints); shouldBeEmpty(strings); shouldBeEmpty(aa); ints ~= 1; strings ~= "foo"; aa["foo"] = "bar"; assertFail(shouldBeEmpty(ints)); assertFail(shouldBeEmpty(strings)); assertFail(shouldBeEmpty(aa));
- void shouldNotBeEmpty(R)(R rng, in string file = __FILE__, in size_t line = __LINE__) if (isInputRange!R);
- Verify that rng is not empty.Throws:UnitTestException on failure.
- void shouldNotBeEmpty(T)(in T aa, in string file = __FILE__, in size_t line = __LINE__) if (isAssociativeArray!T);
- Verify that aa is not empty.Throws:UnitTestException on failure.Examples:
int[] ints; string[] strings; string[string] aa; assertFail(shouldNotBeEmpty(ints)); assertFail(shouldNotBeEmpty(strings)); assertFail(shouldNotBeEmpty(aa)); ints ~= 1; strings ~= "foo"; aa["foo"] = "bar"; shouldNotBeEmpty(ints); shouldNotBeEmpty(strings); shouldNotBeEmpty(aa);
- void shouldBeGreaterThan(T, U)(in T t, in U u, in string file = __FILE__, in size_t line = __LINE__);
- Verify that t is greater than u.Throws:UnitTestException on failure.Examples:
shouldBeGreaterThan(7, 5); assertFail(shouldBeGreaterThan(5, 7)); assertFail(shouldBeGreaterThan(7, 7));
- void shouldBeSmallerThan(T, U)(in T t, in U u, in string file = __FILE__, in size_t line = __LINE__);
- Verify that t is smaller than u.Throws:UnitTestException on failure.Examples:
shouldBeSmallerThan(5, 7); assertFail(shouldBeSmallerThan(7, 5)); assertFail(shouldBeSmallerThan(7, 7));
- void shouldBeSameSetAs(V, E)(V value, E expected, in string file = __FILE__, in size_t line = __LINE__) if (isInputRange!V && isInputRange!E && is(typeof(value.front != expected.front) == bool));
- Verify that t and u represent the same set (ordering is not important).Throws:UnitTestException on failure.Examples:
auto inOrder = iota(4); auto noOrder = [2, 3, 0, 1]; auto oops = [2, 3, 4, 5]; inOrder.shouldBeSameSetAs(noOrder); inOrder.shouldBeSameSetAs(oops).shouldThrow!UnitTestException; struct Struct { int i; } [Struct(1), Struct(4)].shouldBeSameSetAs([Struct(4), Struct(1)]);
- void shouldNotBeSameSetAs(V, E)(V value, E expected, in string file = __FILE__, in size_t line = __LINE__) if (isInputRange!V && isInputRange!E && is(typeof(value.front != expected.front) == bool));
- Verify that value and expected do not represent the same set (ordering is not important).Throws:UnitTestException on failure.Examples:
auto inOrder = iota(4); auto noOrder = [2, 3, 0, 1]; auto oops = [2, 3, 4, 5]; inOrder.shouldNotBeSameSetAs(oops); inOrder.shouldNotBeSameSetAs(noOrder).shouldThrow!UnitTestException;