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 |
Clare Macrae | 8df12f2 | 2020-02-19 14:27:14 +0000 | [diff] [blame] | 27 | #include <Windows.h> |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 28 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END |
onqtam | b43aa04 | 2017-03-13 19:04:08 +0200 | [diff] [blame] | 29 | #ifdef _MSC_VER |
| 30 | #define LoadDynamicLib(lib) LoadLibrary(lib ".dll") |
| 31 | #else // _MSC_VER |
| 32 | #define LoadDynamicLib(lib) LoadLibrary("lib" lib ".dll") |
| 33 | #endif // _MSC_VER |
onqtam | b43aa04 | 2017-03-13 19:04:08 +0200 | [diff] [blame] | 34 | #else // _WIN32 |
| 35 | #include <dlfcn.h> |
| 36 | #ifdef __APPLE__ |
| 37 | #define LoadDynamicLib(lib) dlopen("lib" lib ".dylib", RTLD_NOW) |
| 38 | #else // __APPLE__ |
| 39 | #define LoadDynamicLib(lib) dlopen("lib" lib ".so", RTLD_NOW) |
| 40 | #endif // __APPLE__ |
onqtam | b43aa04 | 2017-03-13 19:04:08 +0200 | [diff] [blame] | 41 | #endif // _WIN32 |
| 42 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 43 | // set an exception translator for double |
| 44 | REGISTER_EXCEPTION_TRANSLATOR(double& e) { |
| 45 | return doctest::String("double: ") + doctest::toString(e); |
| 46 | } |
| 47 | |
onqtam | df8b6d4 | 2019-03-02 17:40:39 +0200 | [diff] [blame] | 48 | DOCTEST_SYMBOL_IMPORT void from_dll(); |
| 49 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 50 | int main(int argc, char** argv) { |
onqtam | b43aa04 | 2017-03-13 19:04:08 +0200 | [diff] [blame] | 51 | // 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] | 52 | from_dll(); |
onqtam | b43aa04 | 2017-03-13 19:04:08 +0200 | [diff] [blame] | 53 | |
| 54 | LoadDynamicLib("plugin"); // load the plugin so tests from it get registered |
| 55 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 56 | doctest::Context context(argc, argv); |
| 57 | int res = context.run(); |
| 58 | |
onqtam | 96bdf71 | 2016-09-13 16:45:52 +0300 | [diff] [blame] | 59 | if(context.shouldExit()) // important - query flags (and --exit) rely on the user doing this |
| 60 | return res; // propagate the result of the tests |
| 61 | |
onqtam | 8a0a023 | 2018-02-06 22:57:12 +0200 | [diff] [blame] | 62 | int client_stuff_return_code = 0; |
| 63 | // your program - if the testing framework is integrated in your production code |
onqtam | b29e617 | 2018-02-06 22:24:18 +0200 | [diff] [blame] | 64 | |
| 65 | 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] | 66 | } |