blob: 8fb49a9859bdeadf8c9fae30ae853b2c945d033f [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>
7#include <vector>
8using namespace std;
onqtamabf39d22017-10-28 21:30:45 +03009DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
onqtam4a655632016-05-26 14:20:52 +030010
onqtam4a655632016-05-26 14:20:52 +030011TEST_CASE("lots of nested subcases") {
12 cout << endl << "root" << endl;
13 SUBCASE("") {
14 cout << "1" << endl;
15 SUBCASE("") { cout << "1.1" << endl; }
16 }
17 SUBCASE("") {
18 cout << "2" << endl;
19 SUBCASE("") { cout << "2.1" << endl; }
20 SUBCASE("") {
21 // whops! all the subcases below shouldn't be discovered and executed!
onqtam7cc0e962017-04-17 23:30:36 +030022 FAIL("");
onqtam4a655632016-05-26 14:20:52 +030023
24 cout << "2.2" << endl;
25 SUBCASE("") {
26 cout << "2.2.1" << endl;
27 SUBCASE("") { cout << "2.2.1.1" << endl; }
28 SUBCASE("") { cout << "2.2.1.2" << endl; }
29 }
30 }
31 SUBCASE("") { cout << "2.3" << endl; }
32 SUBCASE("") { cout << "2.4" << endl; }
33 }
34}
35
onqtam378d6702017-04-19 11:30:03 +030036static void call_func() {
onqtam5dbcb1e2017-05-02 23:07:56 +030037 SUBCASE("from function...") {
38 MESSAGE("print me twice");
39 SUBCASE("sc1") {
40 MESSAGE("hello! from sc1");
41 }
42 SUBCASE("sc2") {
43 MESSAGE("hello! from sc2");
44 }
onqtam378d6702017-04-19 11:30:03 +030045 }
46}
47
48TEST_CASE("subcases can be used in a separate function as well") {
49 call_func();
onqtama82c1e42017-05-07 17:36:41 +030050 MESSAGE("lala");
onqtam378d6702017-04-19 11:30:03 +030051}
52
onqtam4a655632016-05-26 14:20:52 +030053SCENARIO("vectors can be sized and resized") {
54 GIVEN("A vector with some items") {
55 std::vector<int> v(5);
56
onqtamf90739e2016-09-14 01:01:45 +030057 REQUIRE(v.size() == 5);
58 REQUIRE(v.capacity() >= 5);
onqtam4a655632016-05-26 14:20:52 +030059
60 WHEN("the size is increased") {
onqtamf90739e2016-09-14 01:01:45 +030061 v.resize(10);
onqtam4a655632016-05-26 14:20:52 +030062
63 THEN("the size and capacity change") {
onqtamf90739e2016-09-14 01:01:45 +030064 CHECK(v.size() == 20);
65 CHECK(v.capacity() >= 10);
onqtam4a655632016-05-26 14:20:52 +030066 }
67 }
68 WHEN("the size is reduced") {
69 v.resize(0);
70
71 THEN("the size changes but not capacity") {
onqtamf90739e2016-09-14 01:01:45 +030072 CHECK(v.size() == 0);
73 CHECK(v.capacity() >= 5);
onqtam4a655632016-05-26 14:20:52 +030074 }
75 }
76 WHEN("more capacity is reserved") {
77 v.reserve(10);
78
79 THEN("the capacity changes but not the size") {
onqtamf90739e2016-09-14 01:01:45 +030080 CHECK(v.size() == 5);
81 CHECK(v.capacity() >= 10);
onqtam4a655632016-05-26 14:20:52 +030082 }
83 }
84 WHEN("less capacity is reserved") {
85 v.reserve(0);
86
87 THEN("neither size nor capacity are changed") {
onqtamf90739e2016-09-14 01:01:45 +030088 CHECK(v.size() == 10);
89 CHECK(v.capacity() >= 5);
onqtam4a655632016-05-26 14:20:52 +030090 }
91 }
92 }
93}
onqtamdc011982018-10-24 16:57:10 +030094
95TEST_CASE("test case should fail even though the last subcase passes") {
96 SUBCASE("one") {
97 CHECK(false);
98 }
99 SUBCASE("two") {
100 CHECK(true);
101 }
102}
onqtamb656d602019-03-18 18:33:00 +0200103
104TEST_CASE("fails from an exception but gets re-entered to traverse all subcases") {
105 SUBCASE("level zero") {
106 SUBCASE("one") {
107 CHECK(false);
108 }
109 SUBCASE("two") {
110 CHECK(false);
111 }
112
113 throw_if(true, "failure... but the show must go on!");
114 }
115}