assertion_macros.cpp (8171B)
1 #define DOCTEST_CONFIG_ASSERTS_RETURN_VALUES 2 #include <doctest/doctest.h> 3 4 #include "header.h" 5 6 DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN 7 #include <stdexcept> 8 #include <cmath> 9 DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END 10 11 TEST_CASE("normal macros") { 12 int a = 5; 13 int b = 5; 14 15 CHECK(throw_if(true, std::runtime_error("whops!")) == 42); 16 17 CHECK_FALSE(!(a == b)); 18 19 REQUIRE(a == b); 20 21 CHECK_EQ(a, b); 22 23 CHECK(doctest::Approx(0.1000001) == 0.1000002); 24 CHECK(doctest::Approx(0.502) == 0.501); 25 } 26 27 TEST_CASE("expressions should be evaluated only once") { 28 int a = 5; 29 REQUIRE(++a == 6); 30 REQUIRE_EQ(++a, 7); 31 } 32 33 TEST_CASE("exceptions-related macros") { 34 CHECK_THROWS(throw_if(true, 0)); 35 CHECK_THROWS(throw_if(false, 0)); // fails 36 CHECK_THROWS_AS(throw_if(true, 0), int); 37 CHECK_THROWS_AS(throw_if(true, 0), char); // fails 38 CHECK_THROWS_AS(throw_if(false, 0), int); // fails 39 40 CHECK_THROWS_WITH(throw_if(true, "whops!"), "whops! no match!"); // fails 41 CHECK_THROWS_WITH(throw_if(true, "whops! does it match?"), doctest::Contains("whops!")); 42 CHECK_THROWS_WITH(throw_if(true, "whops! does it match?"), doctest::Contains("whops! no match!")); // fails 43 CHECK_THROWS_WITH_AS(throw_if(true, "whops!"), "whops! no match!", bool); // fails 44 CHECK_THROWS_WITH_AS(throw_if(true, "whops!"), "whops!", int); // fails 45 CHECK_THROWS_WITH_AS(throw_if(true, "whops! does it match?"), doctest::Contains("whops! no match!"), int); // fails 46 47 CHECK_NOTHROW(throw_if(true, 0)); // fails 48 CHECK_NOTHROW(throw_if(false, 0)); 49 } 50 51 TEST_CASE("exceptions-related macros for std::exception") { 52 CHECK_THROWS(throw_if(false, 0)); 53 CHECK_THROWS_AS(throw_if(false, std::runtime_error("whops!")), std::exception); 54 CHECK_THROWS_AS(throw_if(true, std::runtime_error("whops!")), const std::exception&); 55 CHECK_THROWS_AS(throw_if(true, std::runtime_error("whops!")), int); 56 57 CHECK_THROWS_WITH(throw_if(false, ""), "whops!"); 58 59 REQUIRE_NOTHROW(throw_if(true, std::runtime_error("whops!"))); 60 } 61 62 // ================================================================================================= 63 // == TESTING (ALMOST) ALL ASSERTS THAT THEY ACT ACCORDINGLY - not interesting examples... 64 // ================================================================================================= 65 66 TEST_CASE("WARN level of asserts don't fail the test case") { 67 WARN(0); 68 WARN_FALSE(1); 69 WARN_THROWS(throw_if(false, 0)); 70 WARN_THROWS_WITH(throw_if(true, ""), "whops!"); 71 WARN_THROWS_WITH(throw_if(false, ""), "whops!"); 72 WARN_THROWS_AS(throw_if(false, 0), bool); 73 WARN_THROWS_AS(throw_if(true, 0), bool); 74 WARN_THROWS_WITH_AS(throw_if(false, ""), "whops!", int); 75 WARN_THROWS_WITH_AS(throw_if(true, ""), "whops!", int); 76 WARN_NOTHROW(throw_if(true, 0)); 77 78 WARN_EQ(1, 0); 79 doctest::String myStr = doctest::String("Hello world, how are you doing? Well, nice to meet you, Goodbye!"); 80 WARN_EQ(myStr, doctest::Contains("Hello")); 81 WARN(myStr == doctest::Contains("Goodbye")); 82 WARN(myStr != doctest::Contains("goodbye")); 83 WARN_UNARY(0); 84 WARN_UNARY_FALSE(1); 85 } 86 87 TEST_CASE("CHECK level of asserts fail the test case but don't abort it") { 88 CHECK(0); 89 CHECK_FALSE(1); 90 CHECK_THROWS(throw_if(false, 0)); 91 CHECK_THROWS_AS(throw_if(false, 0), bool); 92 CHECK_THROWS_AS(throw_if(true, 0), bool); 93 CHECK_THROWS_WITH(throw_if(true, 0), "unrecognized"); 94 CHECK_THROWS_WITH_AS(throw_if(true, 0), "unrecognized", int); 95 CHECK_NOTHROW(throw_if(true, 0)); 96 97 CHECK_EQ(1, 0); 98 doctest::String myStr = doctest::String("Hello world, how are you doing? Well, nice to meet you, Goodbye!"); 99 CHECK_EQ(myStr, doctest::Contains("Hello")); 100 CHECK(myStr == doctest::Contains("Goodbye")); 101 CHECK(myStr != doctest::Contains("goodbye")); 102 CHECK_UNARY(0); 103 CHECK_UNARY_FALSE(1); 104 105 MESSAGE("reached!"); 106 } 107 108 TEST_CASE("REQUIRE level of asserts fail and abort the test case - 1") { 109 REQUIRE(0); 110 MESSAGE("should not be reached!"); 111 } 112 TEST_CASE("REQUIRE level of asserts fail and abort the test case - 2") { 113 REQUIRE_FALSE(1); 114 MESSAGE("should not be reached!"); 115 } 116 TEST_CASE("REQUIRE level of asserts fail and abort the test case - 3") { 117 REQUIRE_THROWS(throw_if(false, 0)); 118 MESSAGE("should not be reached!"); 119 } 120 TEST_CASE("REQUIRE level of asserts fail and abort the test case - 4") { 121 REQUIRE_THROWS_AS(throw_if(false, 0), bool); 122 MESSAGE("should not be reached!"); 123 } 124 TEST_CASE("REQUIRE level of asserts fail and abort the test case - 5") { 125 REQUIRE_THROWS_AS(throw_if(true, 0), bool); 126 MESSAGE("should not be reached!"); 127 } 128 TEST_CASE("REQUIRE level of asserts fail and abort the test case - 6") { 129 REQUIRE_THROWS_WITH(throw_if(false, ""), "whops!"); 130 MESSAGE("should not be reached!"); 131 } 132 TEST_CASE("REQUIRE level of asserts fail and abort the test case - 7") { 133 REQUIRE_THROWS_WITH(throw_if(true, ""), "whops!"); 134 MESSAGE("should not be reached!"); 135 } 136 TEST_CASE("REQUIRE level of asserts fail and abort the test case - 8") { 137 REQUIRE_THROWS_WITH_AS(throw_if(false, ""), "whops!", bool); 138 MESSAGE("should not be reached!"); 139 } 140 TEST_CASE("REQUIRE level of asserts fail and abort the test case - 9") { 141 REQUIRE_THROWS_WITH_AS(throw_if(true, ""), "whops!", bool); 142 MESSAGE("should not be reached!"); 143 } 144 TEST_CASE("REQUIRE level of asserts fail and abort the test case - 10") { 145 REQUIRE_NOTHROW(throw_if(true, 0)); 146 MESSAGE("should not be reached!"); 147 } 148 TEST_CASE("REQUIRE level of asserts fail and abort the test case - 11") { 149 REQUIRE_EQ(1, 0); 150 MESSAGE("should not be reached!"); 151 } 152 TEST_CASE("REQUIRE level of asserts fail and abort the test case - 12") { 153 REQUIRE_UNARY(0); 154 MESSAGE("should not be reached!"); 155 } 156 TEST_CASE("REQUIRE level of asserts fail and abort the test case - 13") { 157 REQUIRE_UNARY_FALSE(1); 158 MESSAGE("should not be reached!"); 159 } 160 161 TEST_CASE("all binary assertions") { 162 WARN_EQ(1, 1); 163 CHECK_EQ(1, 1); 164 REQUIRE_EQ(1, 1); 165 WARN_NE(1, 0); 166 CHECK_NE(1, 0); 167 REQUIRE_NE(1, 0); 168 WARN_GT(1, 0); 169 CHECK_GT(1, 0); 170 REQUIRE_GT(1, 0); 171 WARN_LT(0, 1); 172 CHECK_LT(0, 1); 173 REQUIRE_LT(0, 1); 174 WARN_GE(1, 1); 175 CHECK_GE(1, 1); 176 REQUIRE_GE(1, 1); 177 WARN_LE(1, 1); 178 CHECK_LE(1, 1); 179 REQUIRE_LE(1, 1); 180 WARN_UNARY(1); 181 CHECK_UNARY(1); 182 REQUIRE_UNARY(1); 183 WARN_UNARY_FALSE(0); 184 CHECK_UNARY_FALSE(0); 185 REQUIRE_UNARY_FALSE(0); 186 } 187 188 static void someAssertsInFunction() { 189 int a = 5; 190 int b = 5; 191 CHECK(a == b); 192 CHECK_FALSE(a != b); 193 CHECK_THROWS(throw_if(true, 0)); 194 CHECK_THROWS_AS(throw_if(true, 0), int); 195 CHECK_THROWS_WITH(throw_if(true, false), "unknown exception"); 196 CHECK_THROWS_WITH_AS(throw_if(true, false), "unknown exception", int); 197 CHECK_NOTHROW(throw_if(false, 0)); 198 199 CHECK_EQ(a, b); 200 CHECK_UNARY(a == b); 201 CHECK_UNARY_FALSE(a != b); 202 } 203 204 TEST_CASE("some asserts used in a function called by a test case") { 205 someAssertsInFunction(); 206 } 207 208 // TODO: Remove NOLINT (if (false && (__VA_ARGS__));)? 209 inline DOCTEST_NOINLINE void comp(int a, int b) { // NOLINT(misc-unused-parameters) 210 if (CHECK(a == b)) { MESSAGE(":D"); } 211 if (CHECK_FALSE(a != b)) { MESSAGE(":D"); } 212 if (CHECK_EQ(a, b)) { MESSAGE(":D"); } 213 if (CHECK_UNARY(a == b)) { MESSAGE(":D"); } 214 if (CHECK_UNARY_FALSE(a != b)) { MESSAGE(":D"); } 215 } 216 217 DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4702) 218 TEST_CASE("check return values") { 219 comp(0, 0); 220 221 if (CHECK_THROWS(throw_if(true, true))) { MESSAGE(":D"); } 222 if (CHECK_THROWS_AS(throw_if(true, 2), int)) { MESSAGE(":D"); } 223 if (CHECK_NOTHROW(throw_if(false, 2))) { MESSAGE(":D"); } 224 if (CHECK_THROWS_WITH(throw_if(true, 2), "2")) { MESSAGE(":D"); } 225 } 226 227 TEST_CASE("check return values no print") { 228 comp(4, 2); 229 230 if (CHECK_THROWS(throw_if(false, false))) { MESSAGE(":D"); } 231 if (CHECK_THROWS_AS(throw_if(true, 2), doctest::Approx)) { MESSAGE(":D"); } 232 if (CHECK_NOTHROW(throw_if(true, 2))) { MESSAGE(":D"); } 233 if (CHECK_THROWS_WITH(throw_if(true, 2), "1")) { MESSAGE(":D"); } 234 } 235 DOCTEST_MSVC_SUPPRESS_WARNING_POP