BDD

Behavior Driven Development for the D programming language

Discussion

Home page: https://github.com/workhorsy/BDD

License

Boost Software License - Version 1.0

Examples

  1. import BDD; int add(int a, int b) { return a + b; } unittest { describe("math#add", it("Should add positive numbers", delegate() { add(5, 7).shouldEqual(12); }), it("Should add negative numbers", delegate() { add(5, -7).shouldEqual(-2); }) ); } // Prints the results of the tests int main() { return BDD.printResults(); }

  • Declaration

    void shouldEqual(T, U)(T a, U b, string message = null, string file = __FILE__, size_t line = __LINE__);

    Used to assert that one value is equal to another value.

    Parameters

    T a

    The value to test.

    U b

    The value it should be equal to.

    string file

    The file name that the assert failed in. Should be left as default.

    size_t line

    The file line that the assert failed in. Should be left as default.

    Throws

    If values are not equal, will throw an AssertError with expected and actual values.

    Examples

    1. // Will throw an exception like "AssertError@example.d(6): <3> expected to equal <5>." int z = 3; z.shouldEqual(5);

  • Declaration

    void shouldNotEqual(T, U)(T a, U b, string message = null, string file = __FILE__, size_t line = __LINE__);

    Used to assert that one value is NOT equal to another value.

    Parameters

    T a

    The value to test.

    U b

    The value it should NOT be equal to.

    string file

    The file name that the assert failed in. Should be left as default.

    size_t line

    The file line that the assert failed in. Should be left as default.

    Throws

    If values are NOT equal, will throw an AssertError with unexpected and actual values.

    Examples

    1. // Will throw an exception like "AssertError@example.d(6): <3> expected to NOT equal <3>." int z = 3; z.shouldNotEqual(3);

  • Declaration

    void shouldBeNull(T)(T a, string message = null, string file = __FILE__, size_t line = __LINE__);

    Used to assert that one value is equal to null.

    Parameters

    T a

    The value that should equal null.

    string file

    The file name that the assert failed in. Should be left as default.

    size_t line

    The file line that the assert failed in. Should be left as default.

    Throws

    If value is NOT null, will throw an AssertError.

    Examples

    1. // Will throw an exception like "AssertError@example.d(6): expected to be <null>." string z = "blah"; z.shouldBeNull();

  • Declaration

    void shouldNotBeNull(T)(T a, string message = null, string file = __FILE__, size_t line = __LINE__);

    Used to assert that one value is NOT equal to null.

    Parameters

    T a

    The value that should NOT equal null.

    string file

    The file name that the assert failed in. Should be left as default.

    size_t line

    The file line that the assert failed in. Should be left as default.

    Throws

    If value is null, will throw an AssertError.

    Examples

    1. // Will throw an exception like "AssertError@example.d(6): expected to NOT be <null>." string z = null; z.shouldNotBeNull();

  • Declaration

    void shouldBeIn(T, U)(T value, U[] valid_values, string file = __FILE__, size_t line = __LINE__);

    Used to assert that one value is in an array of specified values.

    Parameters

    T value

    The value to test.

    U[] valid_values

    An array of valid values.

    string file

    The file name that the assert failed in. Should be left as default.

    size_t line

    The file line that the assert failed in. Should be left as default.

    Throws

    If the value is not in the array, will throw an AssertError with the value and array values.

    Examples

    1. // Will throw an exception like "AssertError@example.d(6): <Bobrick> is not in <[Tim, Al]>." "Bobrick".shouldBeIn(["Tim", "Al"]);

  • Declaration

    void shouldBeGreater(T, U)(T a, U b, string message = null, string file = __FILE__, size_t line = __LINE__);

    Used to assert that one value is greater than another value.

    Parameters

    T a

    The value to test.

    U b

    The value it should be greater than.

    string file

    The file name that the assert failed in. Should be left as default.

    size_t line

    The file line that the assert failed in. Should be left as default.

    Throws

    If the value is NOT greater, will throw an AssertError with expected and actual values.

    Examples

    1. // Will throw an exception like "AssertError@example.d(6): <5> expected to be greater than <10>." 5.shouldBeGreater(10);

  • Declaration

    void shouldBeLess(T, U)(T a, U b, string message = null, string file = __FILE__, size_t line = __LINE__);

    Used to assert that one value is less than another value.

    Parameters

    T a

    The value to test.

    U b

    The value it should be less than.

    string file

    The file name that the assert failed in. Should be left as default.

    size_t line

    The file line that the assert failed in. Should be left as default.

    Throws

    If the value is NOT less, will throw an AssertError with expected and actual values.

    Examples

    1. // Will throw an exception like "AssertError@example.d(6): <10> expected to be less than <5>." 10.shouldBeLess(5);

  • Declaration

    void shouldBeGreaterOrEqual(T, U)(T a, U b, string message = null, string file = __FILE__, size_t line = __LINE__);

    Used to assert that one value is greater or equal than another value.

    Parameters

    T a

    The value to test.

    U b

    The value it should be greater or equal than.

    string file

    The file name that the assert failed in. Should be left as default.

    size_t line

    The file line that the assert failed in. Should be left as default.

    Throws

    If the value is NOT greater or equal, will throw an AssertError with expected and actual values.

    Examples

    1. // Will throw an exception like "AssertError@example.d(6): <5> expected to be greater or equal to <10>." 5.shouldBeGreaterOrEqual(10);

  • Declaration

    void shouldBeLessOrEqual(T, U)(T a, U b, string message = null, string file = __FILE__, size_t line = __LINE__);

    Used to assert that one value is less or equal than another value.

    Parameters

    T a

    The value to test.

    U b

    The value it should be less or equal than.

    string file

    The file name that the assert failed in. Should be left as default.

    size_t line

    The file line that the assert failed in. Should be left as default.

    Throws

    If the value is NOT less or equal, will throw an AssertError with expected and actual values.

    Examples

    1. // Will throw an exception like "AssertError@example.d(6): <10> expected to be less or equal to <5>." 10.shouldBeLessOrEqual(5);

  • Declaration

    void shouldThrow(void delegate() cb, string message = null, string file = __FILE__, size_t line = __LINE__);

    Used for asserting that a delegate will throw an exception.

    Parameters

    void delegate() cb

    The delegate that is expected to throw the exception.

    string message

    The message that is expected to be in the exception. Will not be tested, if it is null.

    string file

    The file name that the assert failed in. Should be left as default.

    size_t line

    The file line that the assert failed in. Should be left as default.

    Throws

    If delegate does NOT throw, will throw an AssertError.

    Examples

    1. // Makes sure it throws with the message "boom!" shouldThrow(delegate() { throw new Exception("boom!"); }, "boom!"); // Makes sure it throws, but does not check the message shouldThrow(delegate() { throw new Exception("boom!"); }); // Will throw an exception like "AssertError@test/example.d(7): Exception was not thrown. Expected: boom!" shouldThrow(delegate() { }, "boom!"); // Will throw an exception like "AssertError@test/example.d(7): Exception was not thrown. Expected one. shouldThrow(delegate() { });

  • Declaration

    int printResults();

    Prints the results of all the tests. Returns 0 if all tests pass, or 1 if any fail.

    Examples

    1. BDD.printResults();

    Output:

    1. Unit Test Results: 4 total, 2 successful, 2 failed math#add - "5 + 7 = 12: <12> expected to equal <123>." broken_math.d(2) math#subtract - "5 - 7 = -2: <-2> expected to equal <456>." broken_math.d(6)

  • Declaration

    void describe(TestPair...)(string describe_message, TestPair pairs);

    The message is usually the name of the thing being tested.

    Parameters

    string describe_message

    The thing that is being described.

    TestPair pairs

    All the 'it' delegate functions that will test the thing.

    Examples

    1. describe("example_library#thing_to_test", it("Should NOT fail", delegate() { // code here }) );

  • it

    Declaration

    TestPair it(string message, void delegate() func);

    The message should describe what the test should do.

    Parameters

    string message

    The message to print when the test fails.

    void delegate() func

    The delegate to call when running the test.

    Examples

    1. int a = 4; describe("example_library#a", it("Should equal 4", delegate() { a.shouldEqual(4); }), it("Should Not equal 5", delegate() { a.shouldNotEqual(5); }) );