subcases.cpp (5256B)
1 #include <doctest/doctest.h> 2 3 #include "header.h" 4 5 DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN 6 #include <iostream> 7 #include <string> 8 #include <vector> 9 using namespace std; 10 DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END 11 12 TEST_CASE("lots of nested subcases") { 13 cout << endl << "root" << endl; 14 SUBCASE("") { 15 cout << "1" << endl; 16 SUBCASE("") { cout << "1.1" << endl; } 17 } 18 SUBCASE("") { 19 cout << "2" << endl; 20 SUBCASE("") { cout << "2.1" << endl; } 21 SUBCASE("") { 22 // whops! all the subcases below shouldn't be discovered and executed! 23 FAIL(""); 24 25 cout << "2.2" << endl; 26 SUBCASE("") { 27 cout << "2.2.1" << endl; 28 SUBCASE("") { cout << "2.2.1.1" << endl; } 29 SUBCASE("") { cout << "2.2.1.2" << endl; } 30 } 31 } 32 SUBCASE("") { cout << "2.3" << endl; } 33 SUBCASE("") { cout << "2.4" << endl; } 34 } 35 } 36 37 TEST_CASE("reentering subcase via regular control flow") { 38 cout << endl << "root" << endl; 39 for (int i : { 0, 1, 2 }) { 40 cout << "outside of subcase" << endl; 41 SUBCASE("") { cout << "inside subcase " << i << endl; } 42 SUBCASE("") { cout << "also inside " << i << endl; } 43 SUBCASE("") { 44 if (i != 0) { FAIL(i); } 45 cout << "fail inside " << i << endl; 46 } 47 SUBCASE("") { 48 cout << "inside outside" << endl; 49 for (int j : { 0, 1, 2 }) { 50 SUBCASE("") { cout << "nested twice " << i << ", " << j << endl; } 51 SUBCASE("") { cout << "also twice " << i << ", " << j << endl; } 52 } 53 } 54 } 55 } 56 57 static void call_func() { 58 SUBCASE("from function...") { 59 MESSAGE("print me twice"); 60 SUBCASE("sc1") { 61 MESSAGE("hello! from sc1"); 62 } 63 SUBCASE("sc2") { 64 MESSAGE("hello! from sc2"); 65 } 66 } 67 } 68 69 TEST_CASE("subcases can be used in a separate function as well") { 70 call_func(); 71 MESSAGE("lala"); 72 } 73 74 SCENARIO("vectors can be sized and resized") { 75 GIVEN("A vector with some items") { 76 std::vector<int> v(5); 77 78 REQUIRE(v.size() == 5); 79 REQUIRE(v.capacity() >= 5); 80 81 WHEN("the size is increased") { 82 v.resize(10); 83 84 THEN("the size and capacity change") { 85 CHECK(v.size() == 20); 86 CHECK(v.capacity() >= 10); 87 } 88 } 89 WHEN("the size is reduced") { 90 v.resize(0); 91 92 THEN("the size changes but not capacity") { 93 CHECK(v.size() == 0); 94 CHECK(v.capacity() >= 5); 95 } 96 } 97 WHEN("more capacity is reserved") { 98 v.reserve(10); 99 100 THEN("the capacity changes but not the size") { 101 CHECK(v.size() == 5); 102 CHECK(v.capacity() >= 10); 103 } 104 } 105 WHEN("less capacity is reserved") { 106 v.reserve(0); 107 108 THEN("neither size nor capacity are changed") { 109 CHECK(v.size() == 10); 110 CHECK(v.capacity() >= 5); 111 } 112 } 113 } 114 } 115 116 TEST_CASE("test case should fail even though the last subcase passes") { 117 SUBCASE("one") { 118 CHECK(false); 119 } 120 SUBCASE("two") { 121 CHECK(true); 122 } 123 } 124 125 TEST_CASE("fails from an exception but gets re-entered to traverse all subcases") { 126 SUBCASE("level zero") { 127 SUBCASE("one") { 128 CHECK(false); 129 } 130 SUBCASE("two") { 131 CHECK(false); 132 } 133 134 throw_if(true, "failure... but the show must go on!"); 135 } 136 } 137 138 static void checks(int data) // NOLINT(misc-unused-parameters) 139 { 140 DOCTEST_SUBCASE("check data 1") { REQUIRE(data % 2 == 0); } 141 DOCTEST_SUBCASE("check data 2") { REQUIRE(data % 4 == 0); } 142 } 143 144 TEST_CASE("Nested - related to https://github.com/doctest/doctest/issues/282") 145 { 146 DOCTEST_SUBCASE("generate data variant 1") 147 { 148 int data(44); 149 150 // checks 151 checks(data); 152 } 153 DOCTEST_SUBCASE("generate data variant 1") 154 { 155 int data(80); 156 157 // checks (identical in both variants) 158 checks(data); 159 } 160 } 161 162 DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation stuff 163 DOCTEST_GCC_SUPPRESS_WARNING("-Wuseless-cast") // for the std::string() cast 164 #undef SUBCASE 165 #define SUBCASE(...) DOCTEST_SUBCASE(std::string(__VA_ARGS__).c_str()) 166 167 TEST_CASE("subcases with changing names") { 168 for(int i = 0; i < 2; ++i) { 169 SUBCASE("outer " + std::to_string(i)) { 170 for(int k = 0; k < 2; ++k) { 171 SUBCASE("inner " + std::to_string(k)) { 172 MESSAGE("msg!"); 173 } 174 } 175 } 176 } 177 SUBCASE("separate") { 178 MESSAGE("separate msg!"); 179 } 180 } 181 182 TEST_SUITE("with a funny name,") { 183 TEST_CASE("with a funnier name\\:") { 184 SUBCASE("with the funniest name\\,") { 185 MESSAGE("Yes!"); 186 } 187 SUBCASE("with a slightly funny name :") { 188 MESSAGE("Yep!"); 189 } 190 SUBCASE("without a funny name") { 191 MESSAGE("NO!"); 192 } 193 } 194 195 TEST_CASE("without a funny name:") { 196 MESSAGE("Nooo"); 197 } 198 }