From 8ba957b70e89fb6d0ca1f50ea880e639928be488 Mon Sep 17 00:00:00 2001 From: Matthew Parnell Date: Tue, 14 Aug 2018 10:49:55 +0100 Subject: [PATCH 1/2] Add BDD AND_GIVEN based macros issue #1360 It is possible to have multple given contexts in a single BDD scenario; if you have to type 'and' in the GIVEN description; it's very likely you need an AND. A generic AND is not possible, thus a AND_GIVEN is added to complement the AND_WHEN and AND_THEN. Can be used without needing to increase indent: SCENARIO("...") { GIVEN("...") AND_GIVEN("...") { WHEN("...") { THEN("...") { // ... } } } } would correctly output, when requested/needed: Given: ... And given: ... When: ... Then: ... The padding had to be increased by a character in the output message, to continue to be uniform. --- include/catch.hpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/include/catch.hpp b/include/catch.hpp index 0a49cb3162..9e97c06f06 100644 --- a/include/catch.hpp +++ b/include/catch.hpp @@ -147,11 +147,12 @@ // "BDD-style" convenience wrappers #define CATCH_SCENARIO( ... ) CATCH_TEST_CASE( "Scenario: " __VA_ARGS__ ) #define CATCH_SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, "Scenario: " __VA_ARGS__ ) -#define CATCH_GIVEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " Given: " << desc ) -#define CATCH_WHEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " When: " << desc ) -#define CATCH_AND_WHEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( "And when: " << desc ) -#define CATCH_THEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " Then: " << desc ) -#define CATCH_AND_THEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " And: " << desc ) +#define CATCH_GIVEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " Given: " << desc ) +#define CATCH_AND_GIVEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( "And given: " << desc ) +#define CATCH_WHEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " When: " << desc ) +#define CATCH_AND_WHEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " And when: " << desc ) +#define CATCH_THEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " Then: " << desc ) +#define CATCH_AND_THEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " And: " << desc ) // If CATCH_CONFIG_PREFIX_ALL is not defined then the CATCH_ prefix is not required #else @@ -211,11 +212,12 @@ #define SCENARIO( ... ) TEST_CASE( "Scenario: " __VA_ARGS__ ) #define SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, "Scenario: " __VA_ARGS__ ) -#define GIVEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " Given: " << desc ) -#define WHEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " When: " << desc ) -#define AND_WHEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( "And when: " << desc ) -#define THEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " Then: " << desc ) -#define AND_THEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " And: " << desc ) +#define GIVEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " Given: " << desc ) +#define AND_GIVEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( "And given: " << desc ) +#define WHEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " When: " << desc ) +#define AND_WHEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " And when: " << desc ) +#define THEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " Then: " << desc ) +#define AND_THEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " And: " << desc ) using Catch::Detail::Approx; @@ -276,6 +278,7 @@ using Catch::Detail::Approx; #define CATCH_SCENARIO( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) #define CATCH_SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TESTCASE_METHOD_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), className ) #define CATCH_GIVEN( desc ) +#define CATCH_AND_GIVEN( desc ) #define CATCH_WHEN( desc ) #define CATCH_AND_WHEN( desc ) #define CATCH_THEN( desc ) @@ -340,6 +343,7 @@ using Catch::Detail::Approx; #define SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TESTCASE_METHOD_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), className ) #define GIVEN( desc ) +#define AND_GIVEN( desc ) #define WHEN( desc ) #define AND_WHEN( desc ) #define THEN( desc ) From 37a0e9f88bcfec72fffa449409008b7b7ce48141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Sun, 2 Sep 2018 15:38:33 +0200 Subject: [PATCH 2/2] Add test for AND_GIVEN and update the baselines --- .../Baselines/console.sw.approved.txt | 62 ++++++++++--------- .../SelfTest/Baselines/junit.sw.approved.txt | 4 +- .../SelfTest/Baselines/xml.sw.approved.txt | 29 +++++---- projects/SelfTest/UsageTests/BDD.tests.cpp | 13 ++-- 4 files changed, 58 insertions(+), 50 deletions(-) diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index 533fba1551..792c3d91f5 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -4699,7 +4699,7 @@ PASSED: ------------------------------------------------------------------------------- Scenario: BDD tests requiring Fixtures to provide commonly-accessed data or methods - Given: No operations precede me + Given: No operations precede me ------------------------------------------------------------------------------- BDD.tests.cpp: ............................................................................... @@ -4713,9 +4713,9 @@ with expansion: ------------------------------------------------------------------------------- Scenario: BDD tests requiring Fixtures to provide commonly-accessed data or methods - Given: No operations precede me - When: We get the count - Then: Subsequently values are higher + Given: No operations precede me + When: We get the count + Then: Subsequently values are higher ------------------------------------------------------------------------------- BDD.tests.cpp: ............................................................................... @@ -4728,9 +4728,10 @@ with expansion: ------------------------------------------------------------------------------- Scenario: Do that thing with the thing - Given: This stuff exists - When: I do this - Then: it should do this + Given: This stuff exists + And given: And some assumption + When: I do this + Then: it should do this ------------------------------------------------------------------------------- BDD.tests.cpp: ............................................................................... @@ -4743,10 +4744,11 @@ with expansion: ------------------------------------------------------------------------------- Scenario: Do that thing with the thing - Given: This stuff exists - When: I do this - Then: it should do this - And: do that + Given: This stuff exists + And given: And some assumption + When: I do this + Then: it should do this + And: do that ------------------------------------------------------------------------------- BDD.tests.cpp: ............................................................................... @@ -4760,12 +4762,12 @@ with expansion: ------------------------------------------------------------------------------- Scenario: This is a really long scenario name to see how the list command deals with wrapping - Given: A section name that is so long that it cannot fit in a single - console width - When: The test headers are printed as part of the normal running of the - scenario - Then: The, deliberately very long and overly verbose (you see what I did - there?) section names must wrap, along with an indent + Given: A section name that is so long that it cannot fit in a single + console width + When: The test headers are printed as part of the normal running of the + scenario + Then: The, deliberately very long and overly verbose (you see what I did + there?) section names must wrap, along with an indent ------------------------------------------------------------------------------- BDD.tests.cpp: ............................................................................... @@ -4777,7 +4779,7 @@ with message: ------------------------------------------------------------------------------- Scenario: Vector resizing affects size and capacity - Given: an empty vector + Given: an empty vector ------------------------------------------------------------------------------- BDD.tests.cpp: ............................................................................... @@ -4790,9 +4792,9 @@ with expansion: ------------------------------------------------------------------------------- Scenario: Vector resizing affects size and capacity - Given: an empty vector - When: it is made larger - Then: the size and capacity go up + Given: an empty vector + When: it is made larger + Then: the size and capacity go up ------------------------------------------------------------------------------- BDD.tests.cpp: ............................................................................... @@ -4811,11 +4813,11 @@ with expansion: ------------------------------------------------------------------------------- Scenario: Vector resizing affects size and capacity - Given: an empty vector - When: it is made larger - Then: the size and capacity go up - And when: it is made smaller again - Then: the size goes down but the capacity stays the same + Given: an empty vector + When: it is made larger + Then: the size and capacity go up + And when: it is made smaller again + Then: the size goes down but the capacity stays the same ------------------------------------------------------------------------------- BDD.tests.cpp: ............................................................................... @@ -4834,7 +4836,7 @@ with expansion: ------------------------------------------------------------------------------- Scenario: Vector resizing affects size and capacity - Given: an empty vector + Given: an empty vector ------------------------------------------------------------------------------- BDD.tests.cpp: ............................................................................... @@ -4847,9 +4849,9 @@ with expansion: ------------------------------------------------------------------------------- Scenario: Vector resizing affects size and capacity - Given: an empty vector - When: we reserve more space - Then: The capacity is increased but the size remains the same + Given: an empty vector + When: we reserve more space + Then: The capacity is increased but the size remains the same ------------------------------------------------------------------------------- BDD.tests.cpp: ............................................................................... diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index fb66299d27..b717b8c079 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -513,8 +513,8 @@ Matchers.tests.cpp: - - + + diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index 449448ce2a..df55e7e7c6 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -5463,26 +5463,29 @@
-
-
- - - itDoesThis() - - - true - - -
+
+
+
- itDoesThat() + itDoesThis() true - +
+ + + itDoesThat() + + + true + + + +
+
diff --git a/projects/SelfTest/UsageTests/BDD.tests.cpp b/projects/SelfTest/UsageTests/BDD.tests.cpp index f43fd96dc2..d0d3e03878 100644 --- a/projects/SelfTest/UsageTests/BDD.tests.cpp +++ b/projects/SelfTest/UsageTests/BDD.tests.cpp @@ -38,11 +38,14 @@ namespace { namespace BDDTests { SCENARIO("Do that thing with the thing", "[Tags]") { GIVEN("This stuff exists") { // make stuff exist - WHEN("I do this") { - // do this - THEN("it should do this") { - REQUIRE(itDoesThis()); - AND_THEN("do that")REQUIRE(itDoesThat()); + AND_GIVEN("And some assumption") { + // Validate assumption + WHEN("I do this") { + // do this + THEN("it should do this") { + REQUIRE(itDoesThis()); + AND_THEN("do that")REQUIRE(itDoesThat()); + } } } }