blob: 3fad25fd023aa38e33eaa2e0223f388df9b7b119 [file] [log] [blame]
ncihnegnc5458f22019-01-28 05:08:18 -08001#include <doctest/doctest.h>
onqtam4a655632016-05-26 14:20:52 +03002
onqtam7cc0e962017-04-17 23:30:36 +03003#include "header.h"
4
onqtamabf39d22017-10-28 21:30:45 +03005DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
onqtam4a655632016-05-26 14:20:52 +03006#include <iostream>
onqtam33752222020-02-24 18:35:42 +02007#include <string>
onqtam4a655632016-05-26 14:20:52 +03008#include <vector>
9using namespace std;
onqtamabf39d22017-10-28 21:30:45 +030010DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
onqtam4a655632016-05-26 14:20:52 +030011
onqtam4a655632016-05-26 14:20:52 +030012TEST_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!
onqtam7cc0e962017-04-17 23:30:36 +030023 FAIL("");
onqtam4a655632016-05-26 14:20:52 +030024
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
Stefaneb8b04a2022-04-25 11:01:11 +020037TEST_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
onqtam378d6702017-04-19 11:30:03 +030057static void call_func() {
onqtam5dbcb1e2017-05-02 23:07:56 +030058 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 }
onqtam378d6702017-04-19 11:30:03 +030066 }
67}
68
69TEST_CASE("subcases can be used in a separate function as well") {
70 call_func();
onqtama82c1e42017-05-07 17:36:41 +030071 MESSAGE("lala");
onqtam378d6702017-04-19 11:30:03 +030072}
73
onqtam4a655632016-05-26 14:20:52 +030074SCENARIO("vectors can be sized and resized") {
75 GIVEN("A vector with some items") {
76 std::vector<int> v(5);
77
onqtamf90739e2016-09-14 01:01:45 +030078 REQUIRE(v.size() == 5);
79 REQUIRE(v.capacity() >= 5);
onqtam4a655632016-05-26 14:20:52 +030080
81 WHEN("the size is increased") {
onqtamf90739e2016-09-14 01:01:45 +030082 v.resize(10);
onqtam4a655632016-05-26 14:20:52 +030083
84 THEN("the size and capacity change") {
onqtamf90739e2016-09-14 01:01:45 +030085 CHECK(v.size() == 20);
86 CHECK(v.capacity() >= 10);
onqtam4a655632016-05-26 14:20:52 +030087 }
88 }
89 WHEN("the size is reduced") {
90 v.resize(0);
91
92 THEN("the size changes but not capacity") {
onqtamf90739e2016-09-14 01:01:45 +030093 CHECK(v.size() == 0);
94 CHECK(v.capacity() >= 5);
onqtam4a655632016-05-26 14:20:52 +030095 }
96 }
97 WHEN("more capacity is reserved") {
98 v.reserve(10);
99
100 THEN("the capacity changes but not the size") {
onqtamf90739e2016-09-14 01:01:45 +0300101 CHECK(v.size() == 5);
102 CHECK(v.capacity() >= 10);
onqtam4a655632016-05-26 14:20:52 +0300103 }
104 }
105 WHEN("less capacity is reserved") {
106 v.reserve(0);
107
108 THEN("neither size nor capacity are changed") {
onqtamf90739e2016-09-14 01:01:45 +0300109 CHECK(v.size() == 10);
110 CHECK(v.capacity() >= 5);
onqtam4a655632016-05-26 14:20:52 +0300111 }
112 }
113 }
114}
onqtamdc011982018-10-24 16:57:10 +0300115
116TEST_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}
onqtamb656d602019-03-18 18:33:00 +0200124
125TEST_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}
onqtamdca8b662019-09-22 17:34:50 +0300137
138static 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
onqtam8ee35452021-12-15 15:42:40 +0200144TEST_CASE("Nested - related to https://github.com/doctest/doctest/issues/282")
onqtamdca8b662019-09-22 17:34:50 +0300145{
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}
onqtam33752222020-02-24 18:35:42 +0200161
onqtama8282142020-02-24 19:25:54 +0200162DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation stuff
163DOCTEST_GCC_SUPPRESS_WARNING("-Wuseless-cast") // for the std::string() cast
onqtam33752222020-02-24 18:35:42 +0200164#undef SUBCASE
165#define SUBCASE(...) DOCTEST_SUBCASE(std::string(__VA_ARGS__).c_str())
166
167TEST_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 Suvorovbef19652022-03-05 15:36:58 +0300181
182TEST_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}