ncihnegn | c5458f2 | 2019-01-28 05:08:18 -0800 | [diff] [blame] | 1 | #include <doctest/doctest.h> |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 2 | |
onqtam | cb7bad6 | 2017-04-19 11:19:57 +0300 | [diff] [blame] | 3 | #include "header.h" |
| 4 | |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 5 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 6 | #include <vector> |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 7 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END |
onqtam | cb7bad6 | 2017-04-19 11:19:57 +0300 | [diff] [blame] | 8 | |
| 9 | TEST_CASE("logging the counter of a loop") { |
| 10 | std::vector<int> vec; |
| 11 | vec.push_back(1); |
| 12 | vec.push_back(2); |
| 13 | vec.push_back(4); |
| 14 | vec.push_back(8); |
| 15 | vec.push_back(16); |
| 16 | |
| 17 | INFO("current iteration of loop:"); |
| 18 | for(unsigned i = 0; i < vec.size(); ++i) { |
| 19 | CAPTURE(i); |
| 20 | CHECK(vec[i] != (1 << i)); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | static int someTests() { |
| 25 | int some_var = 42; |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 26 | INFO("lots of captures: ", some_var, " ", some_var, " ", some_var, ";"); |
| 27 | INFO("old way of capturing - using the streaming operator: " << some_var << " " << some_var); |
| 28 | FAIL_CHECK("forcing the many captures to be stringified"); |
onqtam | cb7bad6 | 2017-04-19 11:19:57 +0300 | [diff] [blame] | 29 | return some_var; |
| 30 | } |
| 31 | |
| 32 | TEST_CASE("a test case that will end from an exception") { |
| 33 | int some_var = someTests(); |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 34 | INFO("someTests() returned: ", some_var); // note that we have to use a local variable - cannot pass a temporary |
| 35 | INFO("this should be printed if an exception is thrown even if no assert has failed: ", some_var); |
onqtam | cb7bad6 | 2017-04-19 11:19:57 +0300 | [diff] [blame] | 36 | { |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 37 | INFO("in a nested scope this should be printed as well: ", some_var); |
onqtam | cb7bad6 | 2017-04-19 11:19:57 +0300 | [diff] [blame] | 38 | { |
| 39 | INFO("this should not be printed"); |
| 40 | CAPTURE(some_var); |
| 41 | } |
| 42 | |
| 43 | CHECK_MESSAGE(some_var == 666, "why is this not 666 ?!"); |
| 44 | |
| 45 | throw_if(true, 0); |
| 46 | } |
| 47 | } |
| 48 | |
onqtam | 7630535 | 2017-05-01 20:25:09 +0300 | [diff] [blame] | 49 | TEST_CASE("a test case that will end from an exception and should print the unprinted context") { |
| 50 | INFO("should be printed even if an exception is thrown and no assert fails before that"); |
| 51 | throw_if(true, 0); |
| 52 | } |
| 53 | |
Stefan | db758e0 | 2022-06-06 03:42:02 +0200 | [diff] [blame] | 54 | // TODO: Also remove |
| 55 | // NOLINTNEXTLINE(misc-unused-parameters) |
onqtam | cb7bad6 | 2017-04-19 11:19:57 +0300 | [diff] [blame] | 56 | static void thirdPartyAssert(bool result, bool is_fatal, const char* file, int line) { |
Stefan | db758e0 | 2022-06-06 03:42:02 +0200 | [diff] [blame] | 57 | if(!result) { |
| 58 | if(is_fatal) // NOLINT(bugprone-branch-clone) |
onqtam | cb7bad6 | 2017-04-19 11:19:57 +0300 | [diff] [blame] | 59 | ADD_FAIL_AT(file, line, "MY_ASSERT_FATAL(" << result << ")"); |
| 60 | else |
| 61 | ADD_FAIL_CHECK_AT(file, line, "MY_ASSERT(" << result << ")"); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | #define MY_ASSERT(x) thirdPartyAssert(x, false, __FILE__, __LINE__) |
| 66 | #define MY_ASSERT_FATAL(x) thirdPartyAssert(x, true, __FILE__, __LINE__) |
| 67 | |
| 68 | TEST_CASE("third party asserts can report failures to doctest") { |
| 69 | MY_ASSERT(1 == 2); |
| 70 | MY_ASSERT_FATAL(1 == 2); |
| 71 | } |
| 72 | |
| 73 | TEST_CASE("explicit failures 1") { |
| 74 | FAIL_CHECK("this should not end the test case, but mark it as failing"); |
| 75 | MESSAGE("reached!"); |
| 76 | } |
| 77 | |
| 78 | TEST_CASE("explicit failures 2") { |
| 79 | FAIL("fail the test case and also end it"); |
| 80 | MESSAGE("never reached..."); |
| 81 | } |