onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 1 | #include "doctest.h" |
| 2 | |
onqtam | cb7bad6 | 2017-04-19 11:19:57 +0300 | [diff] [blame] | 3 | #include "header.h" |
| 4 | |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 5 | #include <vector> |
onqtam | cb7bad6 | 2017-04-19 11:19:57 +0300 | [diff] [blame] | 6 | |
| 7 | TEST_CASE("logging the counter of a loop") { |
| 8 | std::vector<int> vec; |
| 9 | vec.push_back(1); |
| 10 | vec.push_back(2); |
| 11 | vec.push_back(4); |
| 12 | vec.push_back(8); |
| 13 | vec.push_back(16); |
| 14 | |
| 15 | INFO("current iteration of loop:"); |
| 16 | for(unsigned i = 0; i < vec.size(); ++i) { |
| 17 | CAPTURE(i); |
| 18 | CHECK(vec[i] != (1 << i)); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | static int someTests() { |
| 23 | int some_var = 42; |
| 24 | INFO("lots of captures - some on heap: " << some_var << " " << some_var << " " << some_var); |
| 25 | return some_var; |
| 26 | } |
| 27 | |
| 28 | TEST_CASE("a test case that will end from an exception") { |
| 29 | int some_var = someTests(); |
| 30 | INFO("someTests() returned: " << some_var); // note that we have to use a local variable - cannot pass a temporary |
| 31 | INFO("this should be printed if an exception is thrown even if no assert has failed: " << some_var); |
| 32 | { |
| 33 | INFO("in a nested scope this should be printed as well: " << some_var); |
| 34 | { |
| 35 | INFO("this should not be printed"); |
| 36 | CAPTURE(some_var); |
| 37 | } |
| 38 | |
| 39 | CHECK_MESSAGE(some_var == 666, "why is this not 666 ?!"); |
| 40 | |
| 41 | throw_if(true, 0); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | static void thirdPartyAssert(bool result, bool is_fatal, const char* file, int line) { |
| 46 | if(result == false) { |
| 47 | if(is_fatal) |
| 48 | ADD_FAIL_AT(file, line, "MY_ASSERT_FATAL(" << result << ")"); |
| 49 | else |
| 50 | ADD_FAIL_CHECK_AT(file, line, "MY_ASSERT(" << result << ")"); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | #define MY_ASSERT(x) thirdPartyAssert(x, false, __FILE__, __LINE__) |
| 55 | #define MY_ASSERT_FATAL(x) thirdPartyAssert(x, true, __FILE__, __LINE__) |
| 56 | |
| 57 | TEST_CASE("third party asserts can report failures to doctest") { |
| 58 | MY_ASSERT(1 == 2); |
| 59 | MY_ASSERT_FATAL(1 == 2); |
| 60 | } |
| 61 | |
| 62 | TEST_CASE("explicit failures 1") { |
| 63 | FAIL_CHECK("this should not end the test case, but mark it as failing"); |
| 64 | MESSAGE("reached!"); |
| 65 | } |
| 66 | |
| 67 | TEST_CASE("explicit failures 2") { |
| 68 | FAIL("fail the test case and also end it"); |
| 69 | MESSAGE("never reached..."); |
| 70 | } |