blob: 506fdb7d00ebaeeef32280f08190ad2c90a921b0 [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
onqtam378d6702017-04-19 11:30:03 +030037static void call_func() {
onqtam5dbcb1e2017-05-02 23:07:56 +030038 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 }
onqtam378d6702017-04-19 11:30:03 +030046 }
47}
48
49TEST_CASE("subcases can be used in a separate function as well") {
50 call_func();
onqtama82c1e42017-05-07 17:36:41 +030051 MESSAGE("lala");
onqtam378d6702017-04-19 11:30:03 +030052}
53
onqtam4a655632016-05-26 14:20:52 +030054SCENARIO("vectors can be sized and resized") {
55 GIVEN("A vector with some items") {
56 std::vector<int> v(5);
57
onqtamf90739e2016-09-14 01:01:45 +030058 REQUIRE(v.size() == 5);
59 REQUIRE(v.capacity() >= 5);
onqtam4a655632016-05-26 14:20:52 +030060
61 WHEN("the size is increased") {
onqtamf90739e2016-09-14 01:01:45 +030062 v.resize(10);
onqtam4a655632016-05-26 14:20:52 +030063
64 THEN("the size and capacity change") {
onqtamf90739e2016-09-14 01:01:45 +030065 CHECK(v.size() == 20);
66 CHECK(v.capacity() >= 10);
onqtam4a655632016-05-26 14:20:52 +030067 }
68 }
69 WHEN("the size is reduced") {
70 v.resize(0);
71
72 THEN("the size changes but not capacity") {
onqtamf90739e2016-09-14 01:01:45 +030073 CHECK(v.size() == 0);
74 CHECK(v.capacity() >= 5);
onqtam4a655632016-05-26 14:20:52 +030075 }
76 }
77 WHEN("more capacity is reserved") {
78 v.reserve(10);
79
80 THEN("the capacity changes but not the size") {
onqtamf90739e2016-09-14 01:01:45 +030081 CHECK(v.size() == 5);
82 CHECK(v.capacity() >= 10);
onqtam4a655632016-05-26 14:20:52 +030083 }
84 }
85 WHEN("less capacity is reserved") {
86 v.reserve(0);
87
88 THEN("neither size nor capacity are changed") {
onqtamf90739e2016-09-14 01:01:45 +030089 CHECK(v.size() == 10);
90 CHECK(v.capacity() >= 5);
onqtam4a655632016-05-26 14:20:52 +030091 }
92 }
93 }
94}
onqtamdc011982018-10-24 16:57:10 +030095
96TEST_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}
onqtamb656d602019-03-18 18:33:00 +0200104
105TEST_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}
onqtamdca8b662019-09-22 17:34:50 +0300117
118static 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
onqtam8ee35452021-12-15 15:42:40 +0200124TEST_CASE("Nested - related to https://github.com/doctest/doctest/issues/282")
onqtamdca8b662019-09-22 17:34:50 +0300125{
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}
onqtam33752222020-02-24 18:35:42 +0200141
onqtama8282142020-02-24 19:25:54 +0200142DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation stuff
143DOCTEST_GCC_SUPPRESS_WARNING("-Wuseless-cast") // for the std::string() cast
onqtam33752222020-02-24 18:35:42 +0200144#undef SUBCASE
145#define SUBCASE(...) DOCTEST_SUBCASE(std::string(__VA_ARGS__).c_str())
146
147TEST_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}