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