80 lines
2.4 KiB
C++
80 lines
2.4 KiB
C++
|
#include <doctest/doctest.h>
|
||
|
|
||
|
#include "header.h"
|
||
|
|
||
|
DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
|
||
|
#include <vector>
|
||
|
DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
|
||
|
|
||
|
TEST_CASE("logging the counter of a loop") {
|
||
|
std::vector<int> vec;
|
||
|
vec.push_back(1);
|
||
|
vec.push_back(2);
|
||
|
vec.push_back(4);
|
||
|
vec.push_back(8);
|
||
|
vec.push_back(16);
|
||
|
|
||
|
INFO("current iteration of loop:");
|
||
|
for(unsigned i = 0; i < vec.size(); ++i) {
|
||
|
CAPTURE(i);
|
||
|
CHECK(vec[i] != (1 << i));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static int someTests() {
|
||
|
int some_var = 42;
|
||
|
INFO("lots of captures: ", some_var, " ", some_var, " ", some_var, ";");
|
||
|
INFO("old way of capturing - using the streaming operator: " << some_var << " " << some_var);
|
||
|
FAIL_CHECK("forcing the many captures to be stringified");
|
||
|
return some_var;
|
||
|
}
|
||
|
|
||
|
TEST_CASE("a test case that will end from an exception") {
|
||
|
int some_var = someTests();
|
||
|
INFO("someTests() returned: ", some_var); // note that we have to use a local variable - cannot pass a temporary
|
||
|
INFO("this should be printed if an exception is thrown even if no assert has failed: ", some_var);
|
||
|
{
|
||
|
INFO("in a nested scope this should be printed as well: ", some_var);
|
||
|
{
|
||
|
INFO("this should not be printed");
|
||
|
CAPTURE(some_var);
|
||
|
}
|
||
|
|
||
|
CHECK_MESSAGE(some_var == 666, "why is this not 666 ?!");
|
||
|
|
||
|
throw_if(true, 0);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
TEST_CASE("a test case that will end from an exception and should print the unprinted context") {
|
||
|
INFO("should be printed even if an exception is thrown and no assert fails before that");
|
||
|
throw_if(true, 0);
|
||
|
}
|
||
|
|
||
|
static void thirdPartyAssert(bool result, bool is_fatal, const char* file, int line) {
|
||
|
if(result == false) {
|
||
|
if(is_fatal)
|
||
|
ADD_FAIL_AT(file, line, "MY_ASSERT_FATAL(" << result << ")");
|
||
|
else
|
||
|
ADD_FAIL_CHECK_AT(file, line, "MY_ASSERT(" << result << ")");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#define MY_ASSERT(x) thirdPartyAssert(x, false, __FILE__, __LINE__)
|
||
|
#define MY_ASSERT_FATAL(x) thirdPartyAssert(x, true, __FILE__, __LINE__)
|
||
|
|
||
|
TEST_CASE("third party asserts can report failures to doctest") {
|
||
|
MY_ASSERT(1 == 2);
|
||
|
MY_ASSERT_FATAL(1 == 2);
|
||
|
}
|
||
|
|
||
|
TEST_CASE("explicit failures 1") {
|
||
|
FAIL_CHECK("this should not end the test case, but mark it as failing");
|
||
|
MESSAGE("reached!");
|
||
|
}
|
||
|
|
||
|
TEST_CASE("explicit failures 2") {
|
||
|
FAIL("fail the test case and also end it");
|
||
|
MESSAGE("never reached...");
|
||
|
}
|