main.cpp (2117B)
1 #define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL 2 #include <doctest/doctest.h> 3 4 DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN 5 #include <iostream> 6 DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END 7 8 template<typename T> 9 static int conditional_throw(bool in, const T& ex) { 10 if(in) 11 #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS 12 throw ex; // NOLINT 13 #else // DOCTEST_CONFIG_NO_EXCEPTIONS 14 ((void)ex); 15 #endif // DOCTEST_CONFIG_NO_EXCEPTIONS 16 return 42; 17 } 18 19 TEST_CASE("executable") { 20 std::cout << "I am a test from the executable!" << std::endl; 21 conditional_throw(true, 'a'); 22 } 23 24 #ifdef _WIN32 25 #define WIN32_LEAN_AND_MEAN 26 DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN 27 DOCTEST_CLANG_SUPPRESS_WARNING("-Wnonportable-system-include-path") 28 #include <windows.h> 29 DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END 30 #ifdef _MSC_VER 31 #define LoadDynamicLib(lib) LoadLibrary(lib ".dll") 32 #else // _MSC_VER 33 #define LoadDynamicLib(lib) LoadLibrary("lib" lib ".dll") 34 #endif // _MSC_VER 35 #else // _WIN32 36 #include <dlfcn.h> 37 #ifdef __APPLE__ 38 #define LoadDynamicLib(lib) dlopen("lib" lib ".dylib", RTLD_NOW) 39 #else // __APPLE__ 40 #define LoadDynamicLib(lib) dlopen("lib" lib ".so", RTLD_NOW) 41 #endif // __APPLE__ 42 #endif // _WIN32 43 44 // set an exception translator for double 45 REGISTER_EXCEPTION_TRANSLATOR(double& e) { 46 return doctest::String("double: ") + doctest::toString(e); 47 } 48 49 DOCTEST_SYMBOL_IMPORT void from_dll(); 50 51 int main(int argc, char** argv) { 52 // force the use of a symbol from the dll so tests from it get registered 53 from_dll(); 54 55 LoadDynamicLib("plugin"); // load the plugin so tests from it get registered 56 57 doctest::Context context(argc, argv); 58 int res = context.run(); 59 60 if(context.shouldExit()) // important - query flags (and --exit) rely on the user doing this 61 return res; // propagate the result of the tests 62 63 int client_stuff_return_code = 0; 64 // your program - if the testing framework is integrated in your production code 65 66 return res + client_stuff_return_code; // the result from doctest is propagated here as well 67 }