120-Bdd-ScenarioGivenWhenThen.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // 120-Bdd-ScenarioGivenWhenThen.cpp
  2. // main() provided by linkage with Catch2WithMain
  3. #include <catch2/catch_test_macros.hpp>
  4. SCENARIO( "vectors can be sized and resized", "[vector]" ) {
  5. GIVEN( "A vector with some items" ) {
  6. std::vector<int> v( 5 );
  7. REQUIRE( v.size() == 5 );
  8. REQUIRE( v.capacity() >= 5 );
  9. WHEN( "the size is increased" ) {
  10. v.resize( 10 );
  11. THEN( "the size and capacity change" ) {
  12. REQUIRE( v.size() == 10 );
  13. REQUIRE( v.capacity() >= 10 );
  14. }
  15. }
  16. WHEN( "the size is reduced" ) {
  17. v.resize( 0 );
  18. THEN( "the size changes but not capacity" ) {
  19. REQUIRE( v.size() == 0 );
  20. REQUIRE( v.capacity() >= 5 );
  21. }
  22. }
  23. WHEN( "more capacity is reserved" ) {
  24. v.reserve( 10 );
  25. THEN( "the capacity changes but not the size" ) {
  26. REQUIRE( v.size() == 5 );
  27. REQUIRE( v.capacity() >= 10 );
  28. }
  29. }
  30. WHEN( "less capacity is reserved" ) {
  31. v.reserve( 0 );
  32. THEN( "neither size nor capacity are changed" ) {
  33. REQUIRE( v.size() == 5 );
  34. REQUIRE( v.capacity() >= 5 );
  35. }
  36. }
  37. }
  38. }
  39. // Compile & run:
  40. // - g++ -std=c++14 -Wall -I$(CATCH_SINGLE_INCLUDE) -o 120-Bdd-ScenarioGivenWhenThen 120-Bdd-ScenarioGivenWhenThen.cpp && 120-Bdd-ScenarioGivenWhenThen --success
  41. // - cl -EHsc -I%CATCH_SINGLE_INCLUDE% 120-Bdd-ScenarioGivenWhenThen.cpp && 120-Bdd-ScenarioGivenWhenThen --success
  42. // Expected compact output (all assertions):
  43. //
  44. // prompt> 120-Bdd-ScenarioGivenWhenThen.exe --reporter compact --success
  45. // 120-Bdd-ScenarioGivenWhenThen.cpp:12: passed: v.size() == 5 for: 5 == 5
  46. // 120-Bdd-ScenarioGivenWhenThen.cpp:13: passed: v.capacity() >= 5 for: 5 >= 5
  47. // 120-Bdd-ScenarioGivenWhenThen.cpp:19: passed: v.size() == 10 for: 10 == 10
  48. // 120-Bdd-ScenarioGivenWhenThen.cpp:20: passed: v.capacity() >= 10 for: 10 >= 10
  49. // 120-Bdd-ScenarioGivenWhenThen.cpp:12: passed: v.size() == 5 for: 5 == 5
  50. // 120-Bdd-ScenarioGivenWhenThen.cpp:13: passed: v.capacity() >= 5 for: 5 >= 5
  51. // 120-Bdd-ScenarioGivenWhenThen.cpp:27: passed: v.size() == 0 for: 0 == 0
  52. // 120-Bdd-ScenarioGivenWhenThen.cpp:28: passed: v.capacity() >= 5 for: 5 >= 5
  53. // 120-Bdd-ScenarioGivenWhenThen.cpp:12: passed: v.size() == 5 for: 5 == 5
  54. // 120-Bdd-ScenarioGivenWhenThen.cpp:13: passed: v.capacity() >= 5 for: 5 >= 5
  55. // 120-Bdd-ScenarioGivenWhenThen.cpp:35: passed: v.size() == 5 for: 5 == 5
  56. // 120-Bdd-ScenarioGivenWhenThen.cpp:36: passed: v.capacity() >= 10 for: 10 >= 10
  57. // 120-Bdd-ScenarioGivenWhenThen.cpp:12: passed: v.size() == 5 for: 5 == 5
  58. // 120-Bdd-ScenarioGivenWhenThen.cpp:13: passed: v.capacity() >= 5 for: 5 >= 5
  59. // 120-Bdd-ScenarioGivenWhenThen.cpp:43: passed: v.size() == 5 for: 5 == 5
  60. // 120-Bdd-ScenarioGivenWhenThen.cpp:44: passed: v.capacity() >= 5 for: 5 >= 5
  61. // Passed 1 test case with 16 assertions.