| 1 | |
| 2 | // Copyright Catch2 Authors |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE.txt or copy at |
| 5 | // https://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | |
| 9 | #include <catch2/catch_test_macros.hpp> |
| 10 | |
| 11 | namespace { |
| 12 | |
| 13 | static bool itDoesThis() { return true; } |
| 14 | |
| 15 | static bool itDoesThat() { return true; } |
| 16 | |
| 17 | // a trivial fixture example to support SCENARIO_METHOD tests |
| 18 | struct Fixture { |
| 19 | Fixture(): d_counter( 0 ) {} |
| 20 | |
| 21 | int counter() { return d_counter++; } |
| 22 | |
| 23 | int d_counter; |
| 24 | }; |
| 25 | |
| 26 | } |
| 27 | |
| 28 | |
| 29 | SCENARIO("Do that thing with the thing", "[Tags]") { |
| 30 | GIVEN("This stuff exists") { |
| 31 | // make stuff exist |
| 32 | AND_GIVEN("And some assumption") { |
| 33 | // Validate assumption |
| 34 | WHEN("I do this") { |
| 35 | // do this |
| 36 | THEN("it should do this") { |
| 37 | REQUIRE(itDoesThis()); |
| 38 | AND_THEN("do that") { |
| 39 | REQUIRE(itDoesThat()); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | SCENARIO( "Vector resizing affects size and capacity", |
| 48 | "[vector][bdd][size][capacity]" ) { |
| 49 | GIVEN( "an empty vector" ) { |
| 50 | std::vector<int> v; |
| 51 | REQUIRE( v.size() == 0 ); |
| 52 | |
| 53 | WHEN( "it is made larger" ) { |
| 54 | v.resize( 10 ); |
| 55 | THEN( "the size and capacity go up" ) { |
| 56 | REQUIRE( v.size() == 10 ); |
| 57 | REQUIRE( v.capacity() >= 10 ); |
| 58 | |
| 59 | AND_WHEN( "it is made smaller again" ) { |
| 60 | v.resize( 5 ); |
| 61 | THEN( |
| 62 | "the size goes down but the capacity stays the same" ) { |
| 63 | REQUIRE( v.size() == 5 ); |
| 64 | REQUIRE( v.capacity() >= 10 ); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | WHEN( "we reserve more space" ) { |
| 71 | v.reserve( 10 ); |
| 72 | THEN( "The capacity is increased but the size remains the same" ) { |
| 73 | REQUIRE( v.capacity() >= 10 ); |
| 74 | REQUIRE( v.size() == 0 ); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | SCENARIO("This is a really long scenario name to see how the list command deals with wrapping", |
| 81 | "[very long tags][lots][long][tags][verbose]" |
| 82 | "[one very long tag name that should cause line wrapping writing out using the list command]" |
| 83 | "[anotherReallyLongTagNameButThisOneHasNoObviousWrapPointsSoShouldSplitWithinAWordUsingADashCharacter]") { |
| 84 | GIVEN("A section name that is so long that it cannot fit in a single console width") { |
| 85 | WHEN("The test headers are printed as part of the normal running of the scenario") { |
| 86 | THEN("The, deliberately very long and overly verbose (you see what I did there?) section names must wrap, along with an indent") { |
| 87 | SUCCEED("boo!"); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | SCENARIO_METHOD(Fixture, |
| 94 | "BDD tests requiring Fixtures to provide commonly-accessed data or methods", |
| 95 | "[bdd][fixtures]") { |
| 96 | const int before(counter()); |
| 97 | GIVEN("No operations precede me") { |
| 98 | REQUIRE(before == 0); |
| 99 | WHEN("We get the count") { |
| 100 | const int after(counter()); |
| 101 | THEN("Subsequently values are higher") { |
| 102 | REQUIRE(after > before); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |