-
Notifications
You must be signed in to change notification settings - Fork 71
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
Restore before and after hooks (#141) #142
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3fd40d3
Add before and after hooks without propagating errors
ilslv 72ae552
Implement hook events
ilslv b740341
Mention hooks in Features book chapter
ilslv c49280b
Corrections
ilslv 192bace
CHANGELOG
ilslv 2d16c86
Corrections
tyranron 609c29d
Fix clap at beta.5 to prevent updating it with breaking changes
ilslv 54b714e
Fix `clap` to `3.0.0-beta.5` version
tyranron 558a9a4
Restore bad merge
tyranron 3d30bee
Merge branch 'main' into return-before-and-after-hooks
tyranron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
//! [`Runner`]: crate::Runner | ||
//! [Cucumber]: https://cucumber.io | ||
|
||
use std::{any::Any, sync::Arc}; | ||
use std::{any::Any, fmt, sync::Arc}; | ||
|
||
/// Alias for a [`catch_unwind()`] error. | ||
/// | ||
|
@@ -224,6 +224,63 @@ impl<World> Clone for Step<World> { | |
} | ||
} | ||
|
||
/// Type of the hook, executed before or after all [`Scenario`]'s [`Step`]s. | ||
/// | ||
/// [`Scenario`]: gherkin::Scenario | ||
/// [`Step`]: gherkin::Step | ||
#[derive(Clone, Copy, Debug)] | ||
pub enum HookTy { | ||
/// Hook, executed on every [`Scenario`] before any [`Step`]s. | ||
/// | ||
/// [`Scenario`]: gherkin::Scenario | ||
/// [`Step`]: gherkin::Step | ||
Before, | ||
|
||
/// Hook, executed on every [`Scenario`] after all [`Step`]s. | ||
/// | ||
/// [`Scenario`]: gherkin::Scenario | ||
/// [`Step`]: gherkin::Step | ||
After, | ||
} | ||
|
||
/// [`Before`] or [`After`] hook event. | ||
/// | ||
/// [`After`]: HookTy::After | ||
/// [`Before`]: HookTy::Before | ||
#[derive(Debug)] | ||
pub enum Hook<World> { | ||
/// Hook execution being started. | ||
Started, | ||
|
||
/// Hook passed. | ||
Passed, | ||
|
||
/// Hook failed. | ||
Failed(Option<Arc<World>>, Info), | ||
} | ||
|
||
impl fmt::Display for HookTy { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, do not mix declarations and |
||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let s = match self { | ||
HookTy::Before => "Before", | ||
HookTy::After => "After", | ||
}; | ||
write!(f, "{}", s) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can just piggyback to |
||
} | ||
} | ||
|
||
// Manual implementation is required to omit the redundant `World: Clone` trait | ||
// bound imposed by `#[derive(Clone)]`. | ||
impl<World> Clone for Hook<World> { | ||
fn clone(&self) -> Self { | ||
match self { | ||
Hook::Started => Hook::Started, | ||
Hook::Passed => Hook::Passed, | ||
Hook::Failed(w, i) => Hook::Failed(w.clone(), i.clone()), | ||
} | ||
} | ||
} | ||
|
||
/// Event specific to a particular [Scenario]. | ||
/// | ||
/// [Scenario]: https://cucumber.io/docs/gherkin/reference/#example | ||
|
@@ -234,6 +291,9 @@ pub enum Scenario<World> { | |
/// [`Scenario`]: gherkin::Scenario | ||
Started, | ||
|
||
/// [`Hook`] event. | ||
Hook(HookTy, Hook<World>), | ||
|
||
/// [`Background`] [`Step`] event. | ||
/// | ||
/// [`Background`]: gherkin::Background | ||
|
@@ -254,6 +314,7 @@ impl<World> Clone for Scenario<World> { | |
fn clone(&self) -> Self { | ||
match self { | ||
Self::Started => Self::Started, | ||
Self::Hook(ty, ev) => Self::Hook(*ty, ev.clone()), | ||
Self::Background(bg, ev) => { | ||
Self::Background(bg.clone(), ev.clone()) | ||
} | ||
|
@@ -264,6 +325,34 @@ impl<World> Clone for Scenario<World> { | |
} | ||
|
||
impl<World> Scenario<World> { | ||
/// Constructs an event of a [`Scenario`] hook being started. | ||
/// | ||
/// [`Scenario`]: gherkin::Scenario | ||
#[must_use] | ||
pub fn hook_started(which: HookTy) -> Self { | ||
Self::Hook(which, Hook::Started) | ||
} | ||
|
||
/// Constructs an event of a passed [`Scenario`] hook. | ||
/// | ||
/// [`Scenario`]: gherkin::Scenario | ||
#[must_use] | ||
pub fn hook_passed(which: HookTy) -> Self { | ||
Self::Hook(which, Hook::Passed) | ||
} | ||
|
||
/// Constructs an event of a failed [`Scenario`] hook. | ||
/// | ||
/// [`Scenario`]: gherkin::Scenario | ||
#[must_use] | ||
pub fn hook_failed( | ||
which: HookTy, | ||
world: Option<Arc<World>>, | ||
info: Info, | ||
) -> Self { | ||
Self::Hook(which, Hook::Failed(world, info)) | ||
} | ||
|
||
/// Constructs an event of a [`Step`] being started. | ||
/// | ||
/// [`Step`]: gherkin::Step | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In type names it's better not to use shortcuts. They're good for local variables only.