Skip to content

Commit

Permalink
updated example
Browse files Browse the repository at this point in the history
  • Loading branch information
ThoSe1990 committed Nov 12, 2024
1 parent 994edcf commit 6a58f54
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@


Tested compilers: GCC 13, Clang17 and MSVC 19
Full documentation: https://those1990.github.io/cwt-cucumber-docs
Conan Recipe: https://github.com/ThoSe1990/cwt-cucumber-conan
Conan (2.x) Recipe: https://github.com/ThoSe1990/cwt-cucumber-conan

Thanks to [Jörg Kreuzberger](https://github.com/kreuzberger), who has contributed and tested a lot lately, which has improved this project a lot.

Expand Down Expand Up @@ -614,9 +613,20 @@ Scenario: An example

### Example: Date-Range

A more complex example is defined below. We want to parse a range between two dates. The Dates are supposed to be written like this: `November 11, 2024`. Here we use this regex pattern: `from ([A-Za-z]+) (\d{1,2}), (\d{4}) to ([A-Za-z]+) (\d{1,2}), (\d{4})`.
A more complex example is defined below. We want to parse an event between single quotes and the date as a a range when the event happens. Therefore we need to regex patterns:
- Event in single quotes: `'(.*?)'` and
- The dates in the format: `from November 11, 2024 to December 12, 2024`: `from ([A-Za-z]+) (\d{1,2}), (\d{4}) to ([A-Za-z]+) (\d{1,2}), (\d{4})`.

`{event}` is fairly easy here:

```cpp
CUSTOM_PARAMETER(custom_event, "{event}", R"('(.*?)')", "a custom event")
{
return CUKE_PARAM_ARG(1).to_string();
}
```
In our implementation we define a struct which represents a `date` and a `date_range`:
`{date}` will requre two structs and should in the end return a `date_range`:
```cpp
struct date
Expand All @@ -633,7 +643,7 @@ struct date_range
};
```

We want now that our type `{date}` returns a `date_range` and we define it as following:
In order to create the `date_range` we implement now the custom parameter:

```cpp
CUSTOM_PARAMETER(
Expand Down Expand Up @@ -661,7 +671,7 @@ Note: When using structs, we have to be explicit about the types. You can either
And this is now our step we want to use, where we put an arbitrary string in front to describe the date range:
```cpp
WHEN(using_date, "{} is {date}")
WHEN(using_date, "{event} is {date}")
{
std::string event = CUKE_ARG(1);
date_range dr = CUKE_ARG(2);
Expand All @@ -675,7 +685,7 @@ Now we can use this for a Scenario (check the full implementation in `examples/s

```gherkin
Scenario: Date example
When A cool event is from April 25, 2025 to Mai 13, 2025
When 'The public festival in town' is from April 25, 2025 to Mai 13, 2025
Then The beginning month is April and the ending month is Mai
```

Expand Down
6 changes: 3 additions & 3 deletions examples/features/8_custom_parameters.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Feature: Custom Parameters
When this is var1=123, var2=99
Then their values are 123 and 99

Scenario: Date example
When A cool event is from April 25, 2025 to Mai 13, 2025
Scenario: Public Festival
When 'The public festival in town' is from April 25, 2025 to Mai 13, 2025
Then The beginning month is April and the ending month is Mai

Scenario: Christmas market
When The Christmas Market in Augsburg is from November 25, 2024 to December 24, 2024
When 'Christmas Market in Augsburg' is from November 25, 2024 to December 24, 2024
Then The beginning month is November and the ending month is December
7 changes: 6 additions & 1 deletion examples/step_definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ CUSTOM_PARAMETER(
return date_range{begin, end};
}

WHEN(using_date, "{} is {date}")
CUSTOM_PARAMETER(custom_event, "{event}", R"('(.*?)')", "a custom event")
{
return CUKE_PARAM_ARG(1).to_string();
}

WHEN(using_date, "{event} is {date}")
{
std::string event = CUKE_ARG(1);
date_range dr = CUKE_ARG(2);
Expand Down

0 comments on commit 6a58f54

Please sign in to comment.