onqtam | b43aa04 | 2017-03-13 19:04:08 +0200 | [diff] [blame] | 1 | #define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL |
ncihnegn | c5458f2 | 2019-01-28 05:08:18 -0800 | [diff] [blame] | 2 | #include <doctest/doctest.h> |
onqtam | b43aa04 | 2017-03-13 19:04:08 +0200 | [diff] [blame] | 3 | |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 4 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN |
onqtam | b43aa04 | 2017-03-13 19:04:08 +0200 | [diff] [blame] | 5 | #include <cstdio> |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 6 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 7 | |
onqtam | 036fa31 | 2017-03-17 11:44:30 +0200 | [diff] [blame] | 8 | template<typename T> |
onqtam | 4b68df3 | 2017-03-17 20:45:54 +0200 | [diff] [blame] | 9 | static int conditional_throw(bool in, const T& ex) { |
| 10 | if(in) |
| 11 | #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS |
| 12 | throw ex; |
| 13 | #else // DOCTEST_CONFIG_NO_EXCEPTIONS |
| 14 | ((void)ex); |
| 15 | #endif // DOCTEST_CONFIG_NO_EXCEPTIONS |
| 16 | return 42; |
| 17 | } |
onqtam | 8327c6c | 2017-03-17 11:18:45 +0200 | [diff] [blame] | 18 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 19 | TEST_CASE("executable") { |
| 20 | printf("I am a test from the executable!\n"); |
onqtam | 8327c6c | 2017-03-17 11:18:45 +0200 | [diff] [blame] | 21 | conditional_throw(true, 'a'); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 22 | } |
| 23 | |
onqtam | b43aa04 | 2017-03-13 19:04:08 +0200 | [diff] [blame] | 24 | #ifdef _WIN32 |
| 25 | #define WIN32_LEAN_AND_MEAN |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 26 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN |
Viktor Kirilov | ba32eb4 | 2021-02-02 16:27:47 +0200 | [diff] [blame] | 27 | DOCTEST_CLANG_SUPPRESS_WARNING("-Wnonportable-system-include-path") |
Jan Niklas Hasse | a702cab | 2021-02-02 15:26:47 +0100 | [diff] [blame] | 28 | #include <windows.h> |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 29 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END |
onqtam | b43aa04 | 2017-03-13 19:04:08 +0200 | [diff] [blame] | 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 |
onqtam | b43aa04 | 2017-03-13 19:04:08 +0200 | [diff] [blame] | 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__ |
onqtam | b43aa04 | 2017-03-13 19:04:08 +0200 | [diff] [blame] | 42 | #endif // _WIN32 |
| 43 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 44 | // set an exception translator for double |
| 45 | REGISTER_EXCEPTION_TRANSLATOR(double& e) { |
| 46 | return doctest::String("double: ") + doctest::toString(e); |
| 47 | } |
| 48 | |
onqtam | df8b6d4 | 2019-03-02 17:40:39 +0200 | [diff] [blame] | 49 | DOCTEST_SYMBOL_IMPORT void from_dll(); |
| 50 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 51 | int main(int argc, char** argv) { |
onqtam | b43aa04 | 2017-03-13 19:04:08 +0200 | [diff] [blame] | 52 | // force the use of a symbol from the dll so tests from it get registered |
onqtam | df8b6d4 | 2019-03-02 17:40:39 +0200 | [diff] [blame] | 53 | from_dll(); |
onqtam | b43aa04 | 2017-03-13 19:04:08 +0200 | [diff] [blame] | 54 | |
| 55 | LoadDynamicLib("plugin"); // load the plugin so tests from it get registered |
| 56 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 57 | doctest::Context context(argc, argv); |
| 58 | int res = context.run(); |
| 59 | |
onqtam | 96bdf71 | 2016-09-13 16:45:52 +0300 | [diff] [blame] | 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 | |
onqtam | 8a0a023 | 2018-02-06 22:57:12 +0200 | [diff] [blame] | 63 | int client_stuff_return_code = 0; |
| 64 | // your program - if the testing framework is integrated in your production code |
onqtam | b29e617 | 2018-02-06 22:24:18 +0200 | [diff] [blame] | 65 | |
| 66 | return res + client_stuff_return_code; // the result from doctest is propagated here as well |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 67 | } |