Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge tags from Feature and Rule with Scenario on filtering (#156) #166

Merged
merged 11 commits into from
Nov 26, 2021
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ All user visible changes to `cucumber` crate will be documented in this file. Th
- Moved `World` type parameter of `WriterExt` trait to methods. ([#160])
- Renamed `Normalized` and `Summarized` `Writer`s to `Normalize` and `Summarize`. ([#162])
- Removed `writer::Basic` `Default` impl and change `writer::Basic::new()` return type to `writer::Normalize<writer::Basic>`. ([#162])
- Merge tags from `Feature` and `Rule` with `Scenario` on filtering with `--tags` CLI option. ([#166])

### Added

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

Expand Down
4 changes: 3 additions & 1 deletion book/src/Features.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,9 @@ OPTIONS:
-c, --concurrency <int> Number of scenarios to run concurrently. If not specified, uses the value
configured in tests runner, or 64 by default
-n, --name <regex> Regex to filter scenarios by their name [aliases: scenario-name]
-t, --tags <tagexpr> Tag expression to filter scenarios by [aliases: scenario-tags]
-t, --tags <tagexpr> Tag expression to filter scenarios by.
Note: Tags from Feature, Rule and Scenario are merged together on filtering,
so be careful about conflicting tags on different levels.
```

Example with [tag expressions](https://cucumber.io/docs/cucumber/api#tag-expressions) for filtering `Scenario`s:
Expand Down
4 changes: 3 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ where
pub re_filter: Option<Regex>,

/// Tag expression to filter scenarios by.
///
/// Note: Tags from Feature, Rule and Scenario are merged together on
/// filtering, so be careful about conflicting tags on different levels.
#[structopt(
short = "t",
long = "tags",
name = "tagexpr",
visible_alias = "scenario-tags",
conflicts_with = "regex"
)]
pub tags_filter: Option<TagOperation>,
Expand Down
19 changes: 15 additions & 4 deletions src/cucumber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,11 +818,22 @@ where
s: &gherkin::Scenario| {
re_filter.as_ref().map_or_else(
|| {
tags_filter
.as_ref()
.map_or_else(|| filter(f, r, s), |f| f.eval(&s.tags))
tags_filter.as_ref().map_or_else(
|| filter(f, r, s),
|tags| {
tags.eval(
f.tags
.iter()
.chain(
r.into_iter()
.flat_map(|r| r.tags.iter()),
)
.chain(s.tags.iter()),
)
},
)
},
|f| f.is_match(&s.name),
|re| re.is_match(&s.name),
)
};

Expand Down
17 changes: 12 additions & 5 deletions src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@ use sealed::sealed;
pub trait Ext {
/// Evaluates this [`TagOperation`] for the given `tags`.
#[must_use]
fn eval(&self, tags: &[String]) -> bool;
fn eval<I, S>(&self, tags: I) -> bool
where
S: AsRef<str>,
I: Iterator<Item = S> + Clone;
Copy link
Member

@tyranron tyranron Nov 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In APIs it's almost always better to accept IntoIterator instead of just an Iterator.

}

#[sealed]
impl Ext for TagOperation {
fn eval(&self, tags: &[String]) -> bool {
fn eval<I, S>(&self, mut tags: I) -> bool
where
S: AsRef<str>,
I: Iterator<Item = S> + Clone,
{
match self {
Self::And(l, r) => l.eval(tags) & r.eval(tags),
Self::Or(l, r) => l.eval(tags) | r.eval(tags),
Self::And(l, r) => l.eval(tags.clone()) & r.eval(tags),
Self::Or(l, r) => l.eval(tags.clone()) | r.eval(tags),
Self::Not(t) => !t.eval(tags),
Self::Tag(t) => tags.iter().any(|tag| tag == t),
Self::Tag(t) => tags.any(|tag| tag.as_ref() == t),
}
}
}