ncihnegn | c5458f2 | 2019-01-28 05:08:18 -0800 | [diff] [blame] | 1 | #include <doctest/doctest.h> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 2 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 3 | #include "header.h" |
| 4 | |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 5 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 6 | #include <iostream> |
onqtam | 3375222 | 2020-02-24 18:35:42 +0200 | [diff] [blame] | 7 | #include <string> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 8 | #include <vector> |
| 9 | using namespace std; |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 10 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 11 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 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! |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 23 | FAIL(""); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 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 | |
Stefan | eb8b04a | 2022-04-25 11:01:11 +0200 | [diff] [blame^] | 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 | |
onqtam | 378d670 | 2017-04-19 11:30:03 +0300 | [diff] [blame] | 57 | static void call_func() { |
onqtam | 5dbcb1e | 2017-05-02 23:07:56 +0300 | [diff] [blame] | 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 | } |
onqtam | 378d670 | 2017-04-19 11:30:03 +0300 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | TEST_CASE("subcases can be used in a separate function as well") { |
| 70 | call_func(); |
onqtam | a82c1e4 | 2017-05-07 17:36:41 +0300 | [diff] [blame] | 71 | MESSAGE("lala"); |
onqtam | 378d670 | 2017-04-19 11:30:03 +0300 | [diff] [blame] | 72 | } |
| 73 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 74 | SCENARIO("vectors can be sized and resized") { |
| 75 | GIVEN("A vector with some items") { |
| 76 | std::vector<int> v(5); |
| 77 | |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 78 | REQUIRE(v.size() == 5); |
| 79 | REQUIRE(v.capacity() >= 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 80 | |
| 81 | WHEN("the size is increased") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 82 | v.resize(10); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 83 | |
| 84 | THEN("the size and capacity change") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 85 | CHECK(v.size() == 20); |
| 86 | CHECK(v.capacity() >= 10); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | WHEN("the size is reduced") { |
| 90 | v.resize(0); |
| 91 | |
| 92 | THEN("the size changes but not capacity") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 93 | CHECK(v.size() == 0); |
| 94 | CHECK(v.capacity() >= 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | WHEN("more capacity is reserved") { |
| 98 | v.reserve(10); |
| 99 | |
| 100 | THEN("the capacity changes but not the size") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 101 | CHECK(v.size() == 5); |
| 102 | CHECK(v.capacity() >= 10); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | WHEN("less capacity is reserved") { |
| 106 | v.reserve(0); |
| 107 | |
| 108 | THEN("neither size nor capacity are changed") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 109 | CHECK(v.size() == 10); |
| 110 | CHECK(v.capacity() >= 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
onqtam | dc01198 | 2018-10-24 16:57:10 +0300 | [diff] [blame] | 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 | } |
onqtam | b656d60 | 2019-03-18 18:33:00 +0200 | [diff] [blame] | 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 | } |
onqtam | dca8b66 | 2019-09-22 17:34:50 +0300 | [diff] [blame] | 137 | |
| 138 | static void checks(int data) |
| 139 | { |
| 140 | DOCTEST_SUBCASE("check data 1") { REQUIRE(data % 2 == 0); } |
| 141 | DOCTEST_SUBCASE("check data 2") { REQUIRE(data % 4 == 0); } |
| 142 | } |
| 143 | |
onqtam | 8ee3545 | 2021-12-15 15:42:40 +0200 | [diff] [blame] | 144 | TEST_CASE("Nested - related to https://github.com/doctest/doctest/issues/282") |
onqtam | dca8b66 | 2019-09-22 17:34:50 +0300 | [diff] [blame] | 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 | } |
onqtam | 3375222 | 2020-02-24 18:35:42 +0200 | [diff] [blame] | 161 | |
onqtam | a828214 | 2020-02-24 19:25:54 +0200 | [diff] [blame] | 162 | DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation stuff |
| 163 | DOCTEST_GCC_SUPPRESS_WARNING("-Wuseless-cast") // for the std::string() cast |
onqtam | 3375222 | 2020-02-24 18:35:42 +0200 | [diff] [blame] | 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 | } |
Egor Suvorov | bef1965 | 2022-03-05 15:36:58 +0300 | [diff] [blame] | 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 | } |