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