Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/onqtam/doctest.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoronqtam <vik.kirilov@gmail.com>2018-07-02 21:37:30 +0300
committeronqtam <vik.kirilov@gmail.com>2018-08-23 16:02:57 +0300
commit1d68a136684fba60518055d267f85161067c0144 (patch)
tree72aedf1628e7b671097681732c3fb7cb0657729b /scripts/playground
parent6439abcc000ae4b16eb1a6d41b899cb59454948d (diff)
- added the ability to call asserts outside of a testing context without recompiling! currently the fast asserts are optimized for runtime speed to the maximum but the other asserts are not. Sadly logging macros - like INFO() - aren't supported... yet! - closes #114 - the way to do it is to call setAsDefaultForAssertsOutOfTestCases() on a doctest::Context and optionally to register a custom handler with doctest::Context::setAssertHandler()
- doctest::isRunningInTest() changed to doctest::is_running_in_test - a bool... for performance reasons - relates #56
Diffstat (limited to 'scripts/playground')
-rw-r--r--scripts/playground/main.cpp52
-rw-r--r--scripts/playground/test.cpp17
-rw-r--r--scripts/playground/test_output/playground.txt48
3 files changed, 114 insertions, 3 deletions
diff --git a/scripts/playground/main.cpp b/scripts/playground/main.cpp
index af44367b..7ff274d5 100644
--- a/scripts/playground/main.cpp
+++ b/scripts/playground/main.cpp
@@ -1,5 +1,51 @@
#include "parts/doctest_impl.h"
+static const char* getSuccessOrFailString(doctest::assertType::Enum at) {
+ using namespace doctest;
+ if(at & assertType::is_warn) //!OCLINT bitwise operator in conditional
+ return "WARNING: ";
+ if(at & assertType::is_check) //!OCLINT bitwise operator in conditional
+ return "ERROR: ";
+ if(at & assertType::is_require) //!OCLINT bitwise operator in conditional
+ return "FATAL ERROR: ";
+ return "";
+}
+
+static void handler(const doctest::AssertData& ad) {
+ using namespace doctest;
+
+ auto& s = std::cout;
+ auto& rb = ad;
+
+ s << Color::LightGrey << ad.m_file << (detail::getContextOptions()->gnu_file_line ? ":" : "(")
+ << ad.m_line << (detail::getContextOptions()->gnu_file_line ? ":" : "): ");
+ s << Color::Red << getSuccessOrFailString(ad.m_at);
+
+ if((rb.m_at & assertType::is_throws_as) == 0) //!OCLINT bitwise operator in conditional
+ s << Color::Cyan << assertString(rb.m_at) << "( " << rb.m_expr << " ) " << Color::None;
+
+ if(rb.m_at & assertType::is_throws) { //!OCLINT bitwise operator in conditional
+ s << (rb.m_threw ? "threw as expected!" : "did NOT throw at all!") << "\n";
+ } else if(rb.m_at & assertType::is_throws_as) { //!OCLINT bitwise operator in conditional
+ s << Color::Cyan << assertString(rb.m_at) << "( " << rb.m_expr << ", "
+ << rb.m_exception_type << " ) " << Color::None
+ << (rb.m_threw ?
+ (rb.m_threw_as ? "threw as expected!" : "threw a DIFFERENT exception: ") :
+ "did NOT throw at all!")
+ << Color::Cyan << rb.m_exception << "\n";
+ } else if(rb.m_at & assertType::is_nothrow) { //!OCLINT bitwise operator in conditional
+ s << (rb.m_threw ? "THREW exception: " : "didn't throw!") << Color::Cyan << rb.m_exception
+ << "\n";
+ } else {
+ s << (rb.m_threw ? "THREW exception: " :
+ (!rb.m_failed ? "is correct!\n" : "is NOT correct!\n"));
+ if(rb.m_threw)
+ s << rb.m_exception << "\n";
+ else
+ s << " values: " << assertString(rb.m_at) << "( " << rb.m_decomposition << " )\n";
+ }
+}
+
int main(int argc, char** argv) {
doctest::Context context;
@@ -14,6 +60,12 @@ int main(int argc, char** argv) {
// overrides
context.setOption("order-by", "file"); // sort the test cases by their name
+ // required so asserts can be used outside of a 'run' context
+ context.setAsDefaultForAssertsOutOfTestCases();
+ context.setAssertHandler(handler);
+ void some_func();
+ some_func();
+
int res = context.run(); // run
if(context.shouldExit()) // important - query flags (and --exit) rely on the user doing this
diff --git a/scripts/playground/test.cpp b/scripts/playground/test.cpp
index 043e3be7..e40535dd 100644
--- a/scripts/playground/test.cpp
+++ b/scripts/playground/test.cpp
@@ -1,6 +1,23 @@
+//#define DOCTEST_CONFIG_SUPER_FAST_ASSERTS
#include "parts/doctest_fwd.h"
DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
#include <iostream>
using namespace std;
DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
+
+void some_func();
+void some_func() {
+ FAST_CHECK_EQ(true, false);
+ FAST_CHECK_UNARY(false);
+ FAST_CHECK_UNARY_FALSE(true);
+
+ CHECK_EQ(true, false);
+ CHECK_UNARY(false);
+ CHECK_UNARY_FALSE(true);
+
+ CHECK(false);
+ CHECK_THROWS(((void)false));
+}
+
+TEST_CASE("some test case lololo!") { some_func(); }
diff --git a/scripts/playground/test_output/playground.txt b/scripts/playground/test_output/playground.txt
index cb40d7a0..cc0cc904 100644
--- a/scripts/playground/test_output/playground.txt
+++ b/scripts/playground/test_output/playground.txt
@@ -1,5 +1,47 @@
+D:\doctest\scripts\playground\test.cpp(11): ERROR: FAST_CHECK_EQ( true, false ) is NOT correct!
+ values: FAST_CHECK_EQ( true, false )
+D:\doctest\scripts\playground\test.cpp(12): ERROR: FAST_CHECK_UNARY( false ) is NOT correct!
+ values: FAST_CHECK_UNARY( false )
+D:\doctest\scripts\playground\test.cpp(13): ERROR: FAST_CHECK_UNARY_FALSE( true ) is NOT correct!
+ values: FAST_CHECK_UNARY_FALSE( true )
+D:\doctest\scripts\playground\test.cpp(15): ERROR: CHECK_EQ( true, false ) is NOT correct!
+ values: CHECK_EQ( true, false )
+D:\doctest\scripts\playground\test.cpp(16): ERROR: CHECK_UNARY( false ) is NOT correct!
+ values: CHECK_UNARY( false )
+D:\doctest\scripts\playground\test.cpp(17): ERROR: CHECK_UNARY_FALSE( true ) is NOT correct!
+ values: CHECK_UNARY_FALSE( true )
+D:\doctest\scripts\playground\test.cpp(19): ERROR: CHECK( false ) is NOT correct!
+ values: CHECK( false )
+D:\doctest\scripts\playground\test.cpp(20): ERROR: CHECK_THROWS( ((void)false) ) did NOT throw at all!
[doctest] run with "--help" for options
===============================================================================
-[doctest] test cases: 0 | 0 passed | 0 failed | 0 skipped
-[doctest] assertions: 0 | 0 passed | 0 failed |
-[doctest] Status: SUCCESS!
+test.cpp(0):
+TEST CASE: some test case lololo!
+
+test.cpp(0): ERROR: FAST_CHECK_EQ( true, false ) is NOT correct!
+ values: FAST_CHECK_EQ( true, false )
+
+test.cpp(0): ERROR: FAST_CHECK_UNARY( false ) is NOT correct!
+ values: FAST_CHECK_UNARY( false )
+
+test.cpp(0): ERROR: FAST_CHECK_UNARY_FALSE( true ) is NOT correct!
+ values: FAST_CHECK_UNARY_FALSE( true )
+
+test.cpp(0): ERROR: CHECK_EQ( true, false ) is NOT correct!
+ values: CHECK_EQ( true, false )
+
+test.cpp(0): ERROR: CHECK_UNARY( false ) is NOT correct!
+ values: CHECK_UNARY( false )
+
+test.cpp(0): ERROR: CHECK_UNARY_FALSE( true ) is NOT correct!
+ values: CHECK_UNARY_FALSE( true )
+
+test.cpp(0): ERROR: CHECK( false ) is NOT correct!
+ values: CHECK( false )
+
+test.cpp(0): ERROR: CHECK_THROWS( ((void)false) ) did NOT throw at all!
+
+===============================================================================
+[doctest] test cases: 1 | 0 passed | 1 failed | 0 skipped
+[doctest] assertions: 8 | 0 passed | 8 failed |
+[doctest] Status: FAILURE!