BDD.tests.cpp 3.3 KB

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