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 | |
onqtam | 378d670 | 2017-04-19 11:30:03 +0300 | [diff] [blame] | 37 | static void call_func() { |
onqtam | 5dbcb1e | 2017-05-02 23:07:56 +0300 | [diff] [blame] | 38 | SUBCASE("from function...") { |
| 39 | MESSAGE("print me twice"); |
| 40 | SUBCASE("sc1") { |
| 41 | MESSAGE("hello! from sc1"); |
| 42 | } |
| 43 | SUBCASE("sc2") { |
| 44 | MESSAGE("hello! from sc2"); |
| 45 | } |
onqtam | 378d670 | 2017-04-19 11:30:03 +0300 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | TEST_CASE("subcases can be used in a separate function as well") { |
| 50 | call_func(); |
onqtam | a82c1e4 | 2017-05-07 17:36:41 +0300 | [diff] [blame] | 51 | MESSAGE("lala"); |
onqtam | 378d670 | 2017-04-19 11:30:03 +0300 | [diff] [blame] | 52 | } |
| 53 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 54 | SCENARIO("vectors can be sized and resized") { |
| 55 | GIVEN("A vector with some items") { |
| 56 | std::vector<int> v(5); |
| 57 | |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 58 | REQUIRE(v.size() == 5); |
| 59 | REQUIRE(v.capacity() >= 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 60 | |
| 61 | WHEN("the size is increased") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 62 | v.resize(10); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 63 | |
| 64 | THEN("the size and capacity change") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 65 | CHECK(v.size() == 20); |
| 66 | CHECK(v.capacity() >= 10); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | WHEN("the size is reduced") { |
| 70 | v.resize(0); |
| 71 | |
| 72 | THEN("the size changes but not capacity") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 73 | CHECK(v.size() == 0); |
| 74 | CHECK(v.capacity() >= 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | WHEN("more capacity is reserved") { |
| 78 | v.reserve(10); |
| 79 | |
| 80 | THEN("the capacity changes but not the size") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 81 | CHECK(v.size() == 5); |
| 82 | CHECK(v.capacity() >= 10); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | WHEN("less capacity is reserved") { |
| 86 | v.reserve(0); |
| 87 | |
| 88 | THEN("neither size nor capacity are changed") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 89 | CHECK(v.size() == 10); |
| 90 | CHECK(v.capacity() >= 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
onqtam | dc01198 | 2018-10-24 16:57:10 +0300 | [diff] [blame] | 95 | |
| 96 | TEST_CASE("test case should fail even though the last subcase passes") { |
| 97 | SUBCASE("one") { |
| 98 | CHECK(false); |
| 99 | } |
| 100 | SUBCASE("two") { |
| 101 | CHECK(true); |
| 102 | } |
| 103 | } |
onqtam | b656d60 | 2019-03-18 18:33:00 +0200 | [diff] [blame] | 104 | |
| 105 | TEST_CASE("fails from an exception but gets re-entered to traverse all subcases") { |
| 106 | SUBCASE("level zero") { |
| 107 | SUBCASE("one") { |
| 108 | CHECK(false); |
| 109 | } |
| 110 | SUBCASE("two") { |
| 111 | CHECK(false); |
| 112 | } |
| 113 | |
| 114 | throw_if(true, "failure... but the show must go on!"); |
| 115 | } |
| 116 | } |
onqtam | dca8b66 | 2019-09-22 17:34:50 +0300 | [diff] [blame] | 117 | |
| 118 | static void checks(int data) |
| 119 | { |
| 120 | DOCTEST_SUBCASE("check data 1") { REQUIRE(data % 2 == 0); } |
| 121 | DOCTEST_SUBCASE("check data 2") { REQUIRE(data % 4 == 0); } |
| 122 | } |
| 123 | |
onqtam | 8ee3545 | 2021-12-15 15:42:40 +0200 | [diff] [blame] | 124 | TEST_CASE("Nested - related to https://github.com/doctest/doctest/issues/282") |
onqtam | dca8b66 | 2019-09-22 17:34:50 +0300 | [diff] [blame] | 125 | { |
| 126 | DOCTEST_SUBCASE("generate data variant 1") |
| 127 | { |
| 128 | int data(44); |
| 129 | |
| 130 | // checks |
| 131 | checks(data); |
| 132 | } |
| 133 | DOCTEST_SUBCASE("generate data variant 1") |
| 134 | { |
| 135 | int data(80); |
| 136 | |
| 137 | // checks (identical in both variants) |
| 138 | checks(data); |
| 139 | } |
| 140 | } |
onqtam | 3375222 | 2020-02-24 18:35:42 +0200 | [diff] [blame] | 141 | |
onqtam | a828214 | 2020-02-24 19:25:54 +0200 | [diff] [blame] | 142 | DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation stuff |
| 143 | DOCTEST_GCC_SUPPRESS_WARNING("-Wuseless-cast") // for the std::string() cast |
onqtam | 3375222 | 2020-02-24 18:35:42 +0200 | [diff] [blame] | 144 | #undef SUBCASE |
| 145 | #define SUBCASE(...) DOCTEST_SUBCASE(std::string(__VA_ARGS__).c_str()) |
| 146 | |
| 147 | TEST_CASE("subcases with changing names") { |
| 148 | for(int i = 0; i < 2; ++i) { |
| 149 | SUBCASE("outer " + std::to_string(i)) { |
| 150 | for(int k = 0; k < 2; ++k) { |
| 151 | SUBCASE("inner " + std::to_string(k)) { |
| 152 | MESSAGE("msg!"); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | SUBCASE("separate") { |
| 158 | MESSAGE("separate msg!"); |
| 159 | } |
| 160 | } |