doctest

FORK: The fastest feature-rich C++11/14/17/20 single-header testing framework
git clone https://git.neptards.moe/neptards/doctest.git
Log | Files | Refs | README

logging.cpp (2563B)


      1 #include <doctest/doctest.h>
      2 
      3 #include "header.h"
      4 
      5 DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
      6 #include <vector>
      7 DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
      8 
      9 TEST_CASE("logging the counter of a loop") {
     10     std::vector<int> vec;
     11     vec.push_back(1);
     12     vec.push_back(2);
     13     vec.push_back(4);
     14     vec.push_back(8);
     15     vec.push_back(16);
     16     
     17     INFO("current iteration of loop:");
     18     for(unsigned i = 0; i < vec.size(); ++i) {
     19         CAPTURE(i);
     20         CHECK(vec[i] != (1 << i));
     21     }
     22 }
     23 
     24 static int someTests() {
     25     int some_var = 42;
     26     INFO("lots of captures: ", some_var, " ", some_var, " ", some_var, ";");
     27     INFO("old way of capturing - using the streaming operator: " << some_var << " " << some_var);
     28     FAIL_CHECK("forcing the many captures to be stringified");
     29     return some_var;
     30 }
     31 
     32 TEST_CASE("a test case that will end from an exception") {
     33     int some_var = someTests();
     34     INFO("someTests() returned: ", some_var); // note that we have to use a local variable - cannot pass a temporary
     35     INFO("this should be printed if an exception is thrown even if no assert has failed: ", some_var);
     36     {
     37         INFO("in a nested scope this should be printed as well: ", some_var);
     38         {
     39             INFO("this should not be printed");
     40             CAPTURE(some_var);
     41         }
     42 
     43         CHECK_MESSAGE(some_var == 666, "why is this not 666 ?!");
     44 
     45         throw_if(true, 0);
     46     }
     47 }
     48 
     49 TEST_CASE("a test case that will end from an exception and should print the unprinted context") {
     50     INFO("should be printed even if an exception is thrown and no assert fails before that");
     51     throw_if(true, 0);
     52 }
     53 
     54 // TODO: Also remove
     55 // NOLINTNEXTLINE(misc-unused-parameters)
     56 static void thirdPartyAssert(bool result, bool is_fatal, const char* file, int line) {
     57     if(!result) {
     58         if(is_fatal) // NOLINT(bugprone-branch-clone)
     59             ADD_FAIL_AT(file, line, "MY_ASSERT_FATAL(" << result << ")");
     60         else
     61             ADD_FAIL_CHECK_AT(file, line, "MY_ASSERT(" << result << ")");
     62     }
     63 }
     64 
     65 #define MY_ASSERT(x) thirdPartyAssert(x, false, __FILE__, __LINE__)
     66 #define MY_ASSERT_FATAL(x) thirdPartyAssert(x, true, __FILE__, __LINE__)
     67 
     68 TEST_CASE("third party asserts can report failures to doctest") {
     69     MY_ASSERT(1 == 2);
     70     MY_ASSERT_FATAL(1 == 2);
     71 }
     72 
     73 TEST_CASE("explicit failures 1") {
     74     FAIL_CHECK("this should not end the test case, but mark it as failing");
     75     MESSAGE("reached!");
     76 }
     77 
     78 TEST_CASE("explicit failures 2") {
     79     FAIL("fail the test case and also end it");
     80     MESSAGE("never reached...");
     81 }