blob: 4de31ea51f8a6379a10e369182e9b2a2d9081487 [file] [log] [blame]
onqtam119cfb62017-04-17 10:46:55 +03001#include "doctest.h"
2
onqtamcb7bad62017-04-19 11:19:57 +03003#include "header.h"
4
onqtamabf39d22017-10-28 21:30:45 +03005DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
onqtam119cfb62017-04-17 10:46:55 +03006#include <vector>
onqtamabf39d22017-10-28 21:30:45 +03007DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
onqtamcb7bad62017-04-19 11:19:57 +03008
9TEST_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
24static int someTests() {
25 int some_var = 42;
onqtam321f2702017-05-01 19:14:58 +030026 INFO("lots of captures - some on heap: " << some_var << " " << some_var << " " << some_var << ";");
27 FAIL_CHECK("forcing the many captures (including those on the heap) to be stringified");
onqtamcb7bad62017-04-19 11:19:57 +030028 return some_var;
29}
30
31TEST_CASE("a test case that will end from an exception") {
32 int some_var = someTests();
33 INFO("someTests() returned: " << some_var); // note that we have to use a local variable - cannot pass a temporary
34 INFO("this should be printed if an exception is thrown even if no assert has failed: " << some_var);
35 {
36 INFO("in a nested scope this should be printed as well: " << some_var);
37 {
38 INFO("this should not be printed");
39 CAPTURE(some_var);
40 }
41
42 CHECK_MESSAGE(some_var == 666, "why is this not 666 ?!");
43
44 throw_if(true, 0);
45 }
46}
47
onqtam76305352017-05-01 20:25:09 +030048TEST_CASE("a test case that will end from an exception and should print the unprinted context") {
49 INFO("should be printed even if an exception is thrown and no assert fails before that");
50 throw_if(true, 0);
51}
52
onqtamcb7bad62017-04-19 11:19:57 +030053static void thirdPartyAssert(bool result, bool is_fatal, const char* file, int line) {
54 if(result == false) {
55 if(is_fatal)
56 ADD_FAIL_AT(file, line, "MY_ASSERT_FATAL(" << result << ")");
57 else
58 ADD_FAIL_CHECK_AT(file, line, "MY_ASSERT(" << result << ")");
59 }
60}
61
62#define MY_ASSERT(x) thirdPartyAssert(x, false, __FILE__, __LINE__)
63#define MY_ASSERT_FATAL(x) thirdPartyAssert(x, true, __FILE__, __LINE__)
64
65TEST_CASE("third party asserts can report failures to doctest") {
66 MY_ASSERT(1 == 2);
67 MY_ASSERT_FATAL(1 == 2);
68}
69
70TEST_CASE("explicit failures 1") {
71 FAIL_CHECK("this should not end the test case, but mark it as failing");
72 MESSAGE("reached!");
73}
74
75TEST_CASE("explicit failures 2") {
76 FAIL("fail the test case and also end it");
77 MESSAGE("never reached...");
78}