- logging example done!
- changed order of printing captures when an exception has occurred
diff --git a/examples/all_features/CMakeLists.txt b/examples/all_features/CMakeLists.txt
index 690bebe..7c12048 100644
--- a/examples/all_features/CMakeLists.txt
+++ b/examples/all_features/CMakeLists.txt
@@ -60,6 +60,7 @@
# TODO: think about fixing these in a different way! - see issue #61 or commit 6b61e8aa3818c5ea100cedc1bb48a60ea10df6e8
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
target_compile_options(disabled PRIVATE /wd4505) # unreferenced local function has been removed
+ target_compile_options(disabled PRIVATE /wd4100) # unreferenced formal parameter
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(disabled PRIVATE -Wno-unknown-warning-option)
target_compile_options(disabled PRIVATE -Wno-unneeded-internal-declaration)
@@ -67,4 +68,6 @@
target_compile_options(disabled PRIVATE -Wno-unused-local-typedef)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_compile_options(disabled PRIVATE -Wno-unused-local-typedefs)
+ target_compile_options(disabled PRIVATE -Wno-unused-parameter)
+ target_compile_options(disabled PRIVATE -Wno-unused-function)
endif()
diff --git a/examples/all_features/logging.cpp b/examples/all_features/logging.cpp
index 889b118..905fa99 100644
--- a/examples/all_features/logging.cpp
+++ b/examples/all_features/logging.cpp
@@ -1,5 +1,70 @@
#include "doctest.h"
-#include <iostream>
+#include "header.h"
+
#include <vector>
-using namespace std;
+
+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 on heap: " << some_var << " " << some_var << " " << some_var);
+ 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);
+ }
+}
+
+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...");
+}
diff --git a/examples/all_features/test_output/logging.cpp.txt b/examples/all_features/test_output/logging.cpp.txt
index eca69f0..25842d0 100644
--- a/examples/all_features/test_output/logging.cpp.txt
+++ b/examples/all_features/test_output/logging.cpp.txt
@@ -1,5 +1,91 @@
[doctest] run with "--help" for options
+== TEST CASE ==================================================================
+logging.cpp(0)
+logging the counter of a loop
+
+logging.cpp(0) ERROR!
+ CHECK( vec[i] != (1 << i) )
+with expansion:
+ CHECK( 1 != 1 )
+with context:
+ current iteration of loop:
+ i := 0
+
+logging.cpp(0) ERROR!
+ CHECK( vec[i] != (1 << i) )
+with expansion:
+ CHECK( 2 != 2 )
+with context:
+ current iteration of loop:
+ i := 1
+
+logging.cpp(0) ERROR!
+ CHECK( vec[i] != (1 << i) )
+with expansion:
+ CHECK( 4 != 4 )
+with context:
+ current iteration of loop:
+ i := 2
+
+logging.cpp(0) ERROR!
+ CHECK( vec[i] != (1 << i) )
+with expansion:
+ CHECK( 8 != 8 )
+with context:
+ current iteration of loop:
+ i := 3
+
+logging.cpp(0) ERROR!
+ CHECK( vec[i] != (1 << i) )
+with expansion:
+ CHECK( 16 != 16 )
+with context:
+ current iteration of loop:
+ i := 4
+
+== TEST CASE ==================================================================
+logging.cpp(0)
+a test case that will end from an exception
+
+logging.cpp(0) ERROR!
+ CHECK( some_var == 666 )
+with expansion:
+ CHECK( 42 == 666 )
+with context:
+ someTests() returned: 42
+ this should be printed if an exception is thrown even if no assert has failed: 42
+ in a nested scope this should be printed as well: 42
+ why is this not 666 ?!
+
+TEST CASE FAILED!
+threw exception:
+ 0
+
+== TEST CASE ==================================================================
+logging.cpp(0)
+third party asserts can report failures to doctest
+
+logging.cpp(0) ERROR!
+ MY_ASSERT(false)
+
+logging.cpp(0) FATAL ERROR!
+ MY_ASSERT_FATAL(false)
+
+== TEST CASE ==================================================================
+logging.cpp(0)
+explicit failures 1
+
+logging.cpp(0) ERROR!
+ this should not end the test case, but mark it as failing
+
+== TEST CASE ==================================================================
+logging.cpp(0)
+explicit failures 2
+
+logging.cpp(0) FATAL ERROR!
+ fail the test case and also end it
+
===============================================================================
-[doctest] test cases: 0 | 0 passed | 0 failed |
-[doctest] assertions: 0 | 0 passed | 0 failed |
+[doctest] test cases: 5 | 0 passed | 5 failed |
+[doctest] assertions: 6 | 0 passed | 6 failed |
Program code.