BDD.tests.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include "catch.hpp"
  9. namespace { namespace BDDTests {
  10. #ifndef BDD_TEST_HELPERS_INCLUDED // Don't compile this more than once per TU
  11. #define BDD_TEST_HELPERS_INCLUDED
  12. inline bool itDoesThis() { return true; }
  13. inline bool itDoesThat() { return true; }
  14. namespace {
  15. // a trivial fixture example to support SCENARIO_METHOD tests
  16. struct Fixture {
  17. Fixture()
  18. : d_counter(0) {
  19. }
  20. int counter() {
  21. return d_counter++;
  22. }
  23. int d_counter;
  24. };
  25. }
  26. #endif
  27. SCENARIO("Do that thing with the thing", "[Tags]") {
  28. GIVEN("This stuff exists") {
  29. // make stuff exist
  30. AND_GIVEN("And some assumption") {
  31. // Validate assumption
  32. WHEN("I do this") {
  33. // do this
  34. THEN("it should do this") {
  35. REQUIRE(itDoesThis());
  36. AND_THEN("do that")REQUIRE(itDoesThat());
  37. }
  38. }
  39. }
  40. }
  41. }
  42. SCENARIO("Vector resizing affects size and capacity", "[vector][bdd][size][capacity]") {
  43. GIVEN("an empty vector") {
  44. std::vector<int> v;
  45. REQUIRE(v.size() == 0);
  46. WHEN("it is made larger") {
  47. v.resize(10);
  48. THEN("the size and capacity go up") {
  49. REQUIRE(v.size() == 10);
  50. REQUIRE(v.capacity() >= 10);
  51. AND_WHEN("it is made smaller again") {
  52. v.resize(5);
  53. THEN("the size goes down but the capacity stays the same") {
  54. REQUIRE(v.size() == 5);
  55. REQUIRE(v.capacity() >= 10);
  56. }
  57. }
  58. }
  59. }
  60. WHEN("we reserve more space") {
  61. v.reserve(10);
  62. THEN("The capacity is increased but the size remains the same") {
  63. REQUIRE(v.capacity() >= 10);
  64. REQUIRE(v.size() == 0);
  65. }
  66. }
  67. }
  68. }
  69. SCENARIO("This is a really long scenario name to see how the list command deals with wrapping",
  70. "[very long tags][lots][long][tags][verbose]"
  71. "[one very long tag name that should cause line wrapping writing out using the list command]"
  72. "[anotherReallyLongTagNameButThisOneHasNoObviousWrapPointsSoShouldSplitWithinAWordUsingADashCharacter]") {
  73. GIVEN("A section name that is so long that it cannot fit in a single console width")WHEN(
  74. "The test headers are printed as part of the normal running of the scenario")THEN(
  75. "The, deliberately very long and overly verbose (you see what I did there?) section names must wrap, along with an indent")SUCCEED(
  76. "boo!");
  77. }
  78. SCENARIO_METHOD(Fixture,
  79. "BDD tests requiring Fixtures to provide commonly-accessed data or methods",
  80. "[bdd][fixtures]") {
  81. const int before(counter());
  82. GIVEN("No operations precede me") {
  83. REQUIRE(before == 0);
  84. WHEN("We get the count") {
  85. const int after(counter());
  86. THEN("Subsequently values are higher") {
  87. REQUIRE(after > before);
  88. }
  89. }
  90. }
  91. }
  92. }} // namespace BDDtests