blob: ddc95d3184b10ced2a2aee136f3f524090116730 [file] [log] [blame]
ncihnegnc5458f22019-01-28 05:08:18 -08001#include <doctest/doctest.h>
onqtam119cfb62017-04-17 10:46:55 +03002
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;
Viktor Kirilov17d984c2020-12-04 15:13:33 +020026 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");
onqtamcb7bad62017-04-19 11:19:57 +030029 return some_var;
30}
31
32TEST_CASE("a test case that will end from an exception") {
33 int some_var = someTests();
Viktor Kirilov17d984c2020-12-04 15:13:33 +020034 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);
onqtamcb7bad62017-04-19 11:19:57 +030036 {
Viktor Kirilov17d984c2020-12-04 15:13:33 +020037 INFO("in a nested scope this should be printed as well: ", some_var);
onqtamcb7bad62017-04-19 11:19:57 +030038 {
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
onqtam76305352017-05-01 20:25:09 +030049TEST_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
onqtamcb7bad62017-04-19 11:19:57 +030054static void thirdPartyAssert(bool result, bool is_fatal, const char* file, int line) {
55 if(result == false) {
56 if(is_fatal)
57 ADD_FAIL_AT(file, line, "MY_ASSERT_FATAL(" << result << ")");
58 else
59 ADD_FAIL_CHECK_AT(file, line, "MY_ASSERT(" << result << ")");
60 }
61}
62
63#define MY_ASSERT(x) thirdPartyAssert(x, false, __FILE__, __LINE__)
64#define MY_ASSERT_FATAL(x) thirdPartyAssert(x, true, __FILE__, __LINE__)
65
66TEST_CASE("third party asserts can report failures to doctest") {
67 MY_ASSERT(1 == 2);
68 MY_ASSERT_FATAL(1 == 2);
69}
70
71TEST_CASE("explicit failures 1") {
72 FAIL_CHECK("this should not end the test case, but mark it as failing");
73 MESSAGE("reached!");
74}
75
76TEST_CASE("explicit failures 2") {
77 FAIL("fail the test case and also end it");
78 MESSAGE("never reached...");
79}