unittest-cpp/UnitTest++/RequireMacros.h
Austin Gilbert e7b56d4e3c Introducing additional functionality to allow client code to stop a unit
test when an assert fails.

The following macro has been added:

	REQUIRE

An example of when these type of checks are useful:

    std::vector<int> v = foo();

    REQUIRE(CHECK_EQUAL(3, v.size())); // test stops here on a fail
                                       // so we don't segfault looking at
                                       // v[0] below.
    CHECK_EQUAL(1, v[0]);
    CHECK_EQUAL(2, v[1]);
    CHECK_EQUAL(3, v[2]);

Multiple checks are supported as follows:

    REQUIRE({
        CHECK_EQUAL(1, 2);
        CHECK(true);
    };

In the multiple check scenario, all the checks in the REQUIRE block will
be run. After which, if any failures were reported, the TEST case will
be stopped.

When UNITTEST_NO_EXCEPTIONS is defined, REQUIRE is a noop.
2016-02-06 09:28:48 -06:00

28 lines
797 B
C

#ifndef UNITTEST_REQUIREMACROS_H
#define UNITTEST_REQUIREMACROS_H
#include "HelperMacros.h"
#include "ExceptionMacros.h"
#include "CurrentTest.h"
#ifdef REQUIRE
#error UnitTest++ redefines REQUIRE
#endif
#ifndef UNITTEST_NO_EXCEPTIONS
#define REQUIRE(test) \
UNITTEST_MULTILINE_MACRO_BEGIN \
int const failuresBeforeTest = UnitTest::CurrentTest::Results()->GetFailureCount(); \
test; \
int const failuresAfterTest = UnitTest::CurrentTest::Results()->GetFailureCount(); \
if(failuresAfterTest > failuresBeforeTest) \
{ \
UT_THROW(UnitTest::AssertException()); \
} \
UNITTEST_MULTILINE_MACRO_END
#endif
#endif
#ifdef UNITTEST_NO_EXCEPTIONS
#define REQUIRE(test) test;
#endif