blob: 5182b165fbca0a46119ba7215292e682e83d6c19 [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
Stefandb758e02022-06-06 03:42:02 +020054// TODO: Also remove
55// NOLINTNEXTLINE(misc-unused-parameters)
onqtamcb7bad62017-04-19 11:19:57 +030056static void thirdPartyAssert(bool result, bool is_fatal, const char* file, int line) {
Stefandb758e02022-06-06 03:42:02 +020057 if(!result) {
58 if(is_fatal) // NOLINT(bugprone-branch-clone)
onqtamcb7bad62017-04-19 11:19:57 +030059 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
68TEST_CASE("third party asserts can report failures to doctest") {
69 MY_ASSERT(1 == 2);
70 MY_ASSERT_FATAL(1 == 2);
71}
72
73TEST_CASE("explicit failures 1") {
74 FAIL_CHECK("this should not end the test case, but mark it as failing");
75 MESSAGE("reached!");
76}
77
78TEST_CASE("explicit failures 2") {
79 FAIL("fail the test case and also end it");
80 MESSAGE("never reached...");
81}