Skip to content

Commit

Permalink
Implement Parameter derive macro for Cucumber Expressions (#168, #124)
Browse files Browse the repository at this point in the history
Co-authored-by: Kai Ren <[email protected]>
  • Loading branch information
ilslv and tyranron committed Nov 29, 2021
1 parent 4a581b1 commit b7e9ff9
Show file tree
Hide file tree
Showing 12 changed files with 685 additions and 79 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ All user visible changes to `cucumber` crate will be documented in this file. Th
- `writer::NonTransforming` trait required for `writer::Repeat`. ([#162])
- `writer::Summarizable` trait required for `writer::Summarize`. ([#162])
- Support for [Cucumber Expressions] via `#[given(expr = ...)]`, `#[when(expr = ...)]` and `#[then(expr = ...)]` syntax. ([#157])
- Support for custom parameters in [Cucumber Expressions] via `#[derive(cucumber::Parameter)]` macro. ([#168])
- Merging tags from `Feature` and `Rule` with `Scenario` when filtering with `--tags` CLI option. ([#166])

### Fixed
Expand All @@ -44,6 +45,7 @@ All user visible changes to `cucumber` crate will be documented in this file. Th
[#162]: /../../pull/162
[#163]: /../../pull/163
[#166]: /../../pull/166
[#168]: /../../pull/168
[0110-1]: https://llg.cubic.org/docs/junit
[0110-2]: https://github.com/cucumber/cucumber-json-schema

Expand Down
12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[features]
default = ["macros"]
# Enables step attributes and auto-wiring.
macros = ["cucumber-codegen", "inventory"]
macros = ["cucumber-codegen", "cucumber-expressions", "inventory"]
# Enables support for outputting in Cucumber JSON format.
output-json = ["Inflector", "serde", "serde_json", "timestamps"]
# Enables support for outputting JUnit XML report.
Expand All @@ -40,7 +40,7 @@ timestamps = []
async-trait = "0.1.40"
atty = "0.2.14"
console = "0.15"
derive_more = { version = "0.99.16", features = ["as_ref", "deref", "deref_mut", "display", "error", "from", "into"], default_features = false }
derive_more = { version = "0.99.17", features = ["as_ref", "deref", "deref_mut", "display", "error", "from", "into"], default_features = false }
either = "1.6"
futures = "0.3.17"
gherkin = { package = "gherkin_rust", version = "0.10" }
Expand All @@ -52,19 +52,21 @@ regex = "1.5"
sealed = "0.3"
structopt = "0.3.25"

# "macros" feature dependencies
# "macros" feature dependencies.
cucumber-codegen = { version = "0.11.0-dev", path = "./codegen", optional = true }
cucumber-expressions = { version = "0.1.0", features = ["into-regex"], optional = true }
inventory = { version = "0.2", optional = true }

# "output-json" feature dependencies
# "output-json" feature dependencies.
serde = { version = "1.0.103", features = ["derive"], optional = true }
serde_json = { version = "1.0.18", optional = true }
Inflector = { version = "0.11", default-features = false, optional = true }

# "output-junit" feature dependencies
# "output-junit" feature dependencies.
junit-report = { version = "0.7", optional = true }

[dev-dependencies]
derive_more = "0.99.17"
humantime = "2.1"
tempfile = "3.2"
tokio = { version = "1.12", features = ["macros", "rt-multi-thread", "time"] }
Expand Down
11 changes: 7 additions & 4 deletions book/src/Features.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ Feature: Animal feature
# use std::{convert::Infallible, str::FromStr, time::Duration};
#
# use async_trait::async_trait;
# use cucumber::{given, then, when, World, WorldInit};
# use cucumber::{given, then, when, Parameter, World, WorldInit};
# use tokio::time::sleep;
#
#[derive(Debug)]
Expand Down Expand Up @@ -230,6 +230,7 @@ impl World for AnimalWorld {
}
}

#[derive(Clone, Copy)]
enum State {
Hungry,
Satiated,
Expand All @@ -247,6 +248,8 @@ impl FromStr for State {
}
}

#[derive(Clone, Copy, Parameter)]
#[param(regex = "cat|dog|🦀")]
enum Animal {
Cat,
Dog,
Expand All @@ -266,7 +269,7 @@ impl FromStr for Animal {
}
}

#[given(regex = r"^a (\S+) (\S+)$")]
#[given(regex = r"^a (hungry|satiated) (cat|dog|🦀)$")]
async fn hungry_cat(world: &mut AnimalWorld, state: State, animal: Animal) {
sleep(Duration::from_secs(2)).await;

Expand All @@ -282,7 +285,7 @@ async fn hungry_cat(world: &mut AnimalWorld, state: State, animal: Animal) {
};
}

#[when(regex = r"^I feed the (\S+) (\d+) times?$")]
#[when(regex = r"^I feed the (cat|dog|🦀) (\d+) times?$")]
async fn feed_cat(world: &mut AnimalWorld, animal: Animal, times: usize) {
sleep(Duration::from_secs(2)).await;

Expand All @@ -295,7 +298,7 @@ async fn feed_cat(world: &mut AnimalWorld, animal: Animal, times: usize) {
}
}

#[then(expr = "the {word} is not hungry")]
#[then(expr = "the {animal} is not hungry")]
async fn cat_is_fed(world: &mut AnimalWorld, animal: Animal) {
sleep(Duration::from_secs(2)).await;

Expand Down
2 changes: 2 additions & 0 deletions codegen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ All user visible changes to `cucumber-codegen` crate will be documented in this

- Unwrapping `Result`s returned by step functions. ([#151])
- `expr = ...` argument to `#[given(...)]`, `#[when(...)]` and `#[then(...)]` attributes allowing [Cucumber Expressions]. ([#157])
- `#[derive(Parameter)]` attribute macro for implementing custom parameters of [Cucumber Expressions]. ([#168])

[#151]: /../../pull/151
[#157]: /../../pull/157
[#168]: /../../pull/168



Expand Down
2 changes: 2 additions & 0 deletions codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ proc-macro2 = "1.0.28"
quote = "1.0.9"
regex = "1.4"
syn = { version = "1.0.74", features = ["derive", "extra-traits", "full"] }
synthez = "0.2"

[dev-dependencies]
async-trait = "0.1"
cucumber = { path = "..", features = ["macros"] }
derive_more = "0.99.17"
futures = "0.3.17"
tempfile = "3.2"
tokio = { version = "1.12", features = ["macros", "rt-multi-thread", "time"] }
Expand Down
Loading

0 comments on commit b7e9ff9

Please sign in to comment.