4.9. Step - 07 : Use regular expressions for tests¶
Now, let’s update steps_addition.cpp
, to use regular expressions and re-use steps.
features/step_definitions/steps_addition.cpp
With re-usable implementation, it looks like below.
#include <gtest/gtest.h> #include <cucumber-cpp/autodetect.hpp> #include <libMath.h> using cucumber::ScenarioScope; struct libMathCtx { int param1; int param2; int result; }; GIVEN("^I have '(\\d+)' and '(\\d+)'$") { ScenarioScope<libMathCtx> context; REGEX_PARAM(int, param1); REGEX_PARAM(int, param2); context->param1 = param1; context->param2 = param2; } WHEN("^I add them$") { ScenarioScope<libMathCtx> context; context->result = libMath_add(context->param1, context->param2); } THEN("^The result must be '(\\d+)'$") { REGEX_PARAM(int, result); ScenarioScope<libMathCtx> context; EXPECT_EQ(result, context->result); }
Now, let’s add a tabulated Feature/Scenario file. This is very similar to BDD - Python / Step - 05 : Tabulated entries
The updated directory structure would be like this:
.
|-- features
| |-- step_definitions
| | |-- cucumber.wire
| | `-- steps_addition.cpp
| |-- CMakeLists.txt
| |-- simple-addition.feature
| `-- table-addition.feature
|-- lib
| |-- CMakeLists.txt
| |-- libMath.c
| `-- libMath.h
`-- CMakeLists.txt
features/table-addition.feature
This is same as BDD - Python / Step - 05 : Tabulated entries
# For repeated parameters, # we can use table Feature: Tabulated maths Scenario Outline: Addition from a table Given I have '<num1>' and '<num2>' When I add them Then The result must be '<total>' Examples: | num1 | num2 | total | | 1 | 2 | 3 | | 10 | 20 | 30 | | 22 | 33 | 55 | | 22 | 33 | 55 |
4.9.1. Changes¶
As compared to previous step (Step - 06 : Make tests pass), the diff is:
Files old/CMakeLists.txt and new/CMakeLists.txt are identical
Files old/features/CMakeLists.txt and new/features/CMakeLists.txt are identical
Files old/features/simple-addition.feature and new/features/simple-addition.feature are identical
Files old/features/step_definitions/cucumber.wire and new/features/step_definitions/cucumber.wire are identical
diff '--unified=3' --new-file --ignore-all-space --text --recursive --show-c-function --report-identical-files old/features/step_definitions/steps_addition.cpp new/features/step_definitions/steps_addition.cpp
--- old/features/step_definitions/steps_addition.cpp 2022-12-11 00:00:00.000000000 +0530
+++ new/features/step_definitions/steps_addition.cpp 2022-12-11 00:00:00.000000000 +0530
@@ -12,21 +12,12 @@ struct libMathCtx {
int result;
};
-
-// We could have used Regular Expressions here
-// to reduce the duplicated steps.
-// but we will introduce that concept later.
-
-GIVEN("^I have '1' and '3'$") {
- ScenarioScope<libMathCtx> context;
- context->param1 = 1;
- context->param2 = 3;
-}
-
-GIVEN("^I have '70' and '29'$") {
+GIVEN("^I have '(\\d+)' and '(\\d+)'$") {
ScenarioScope<libMathCtx> context;
- context->param1 = 70;
- context->param2 = 29;
+ REGEX_PARAM(int, param1);
+ REGEX_PARAM(int, param2);
+ context->param1 = param1;
+ context->param2 = param2;
}
WHEN("^I add them$") {
@@ -34,12 +25,8 @@ WHEN("^I add them$") {
context->result = libMath_add(context->param1, context->param2);
}
-THEN("^The result must be '4'$") {
- ScenarioScope<libMathCtx> context;
- EXPECT_EQ(4, context->result);
-}
-
-THEN("^The result must be '99'$") {
+THEN("^The result must be '(\\d+)'$") {
+ REGEX_PARAM(int, result);
ScenarioScope<libMathCtx> context;
- EXPECT_EQ(99, context->result);
+ EXPECT_EQ(result, context->result);
}
diff '--unified=3' --new-file --ignore-all-space --text --recursive --show-c-function --report-identical-files old/features/table-addition.feature new/features/table-addition.feature
--- old/features/table-addition.feature 1970-01-01 05:30:00.000000000 +0530
+++ new/features/table-addition.feature 2022-12-11 00:00:00.000000000 +0530
@@ -0,0 +1,16 @@
+# For repeated parameters,
+# we can use table
+
+Feature: Tabulated maths
+
+ Scenario Outline: Addition from a table
+ Given I have '<num1>' and '<num2>'
+ When I add them
+ Then The result must be '<total>'
+
+ Examples:
+ | num1 | num2 | total |
+ | 1 | 2 | 3 |
+ | 10 | 20 | 30 |
+ | 22 | 33 | 55 |
+ | 22 | 33 | 55 |
Files old/lib/CMakeLists.txt and new/lib/CMakeLists.txt are identical
Files old/lib/libMath.c and new/lib/libMath.c are identical
Files old/lib/libMath.h and new/lib/libMath.h are identical
4.9.2. Pre-Compile¶
As mentioned in Step - 01 : Setup of C/C++ Code and Step - 05 : Make tests fail please ensure you run CMake
so that workspace is created, and run make all
so that feature executable and math Library is compiled.
4.9.3. Running feature executable¶
As a first step, we need to run the feature executable in background.
./features/features -v &
If everything is setup perfectly, this process will wait for Cucumber in the background.
Listening on 127.0.0.1:3902
4.9.4. Running cucumber¶
Now let’s run Cucumber.
cucumber
The output is as follows.
*** THIS RUBY IMPLEMENTATION DOESN'T REPORT FILE AND LINE FOR PROCS ***
# features/sample-addition.feature
Feature: Simple Addition
Showcase simple addition for the BDD Book.
Scenario: Addition of single digit numbers # features/simple-addition.feature:7
Given I have '1' and '3' # steps_addition.cpp:15
When I add them # steps_addition.cpp:23
Then The result must be '4' # steps_addition.cpp:28
Scenario: Addition of double digit numbers # features/simple-addition.feature:12
Given I have '70' and '29' # steps_addition.cpp:15
When I add them # steps_addition.cpp:23
Then The result must be '99' # steps_addition.cpp:28
# For repeated parameters,
# we can use table
Feature: Tabulated maths
Scenario Outline: Addition from a table # features/table-addition.feature:6
Given I have '<num1>' and '<num2>' # features/table-addition.feature:7
When I add them # features/table-addition.feature:8
Then The result must be '<total>' # features/table-addition.feature:9
Examples:
| num1 | num2 | total |
| 1 | 2 | 3 |
| 10 | 20 | 30 |
| 22 | 33 | 55 |
| 22 | 33 | 55 |
6 scenarios (6 passed)
18 steps (18 passed)
0m0.056s
As compared to Step - 06 : Make tests pass, we have more tests for addition, but it’s nice to see that All tests have passed!!!