-
Notifications
You must be signed in to change notification settings - Fork 71
/
cucumber.rs
82 lines (70 loc) · 2.47 KB
/
cucumber.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) 2018-2020 Brendan Molloy <[email protected]>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use futures::StreamExt;
use crate::steps::Steps;
use crate::{EventHandler, World};
pub struct Cucumber<W: World> {
steps: Steps<W>,
features: Vec<gherkin::Feature>,
event_handler: Box<dyn EventHandler>,
}
impl<W: World> Default for Cucumber<W> {
fn default() -> Self {
Cucumber {
steps: Default::default(),
features: Default::default(),
event_handler: Box::new(crate::output::BasicOutput::default()),
}
}
}
impl<W: World> Cucumber<W> {
pub fn new() -> Cucumber<W> {
Default::default()
}
pub fn with_handler<O: EventHandler>(event_handler: O) -> Self {
Cucumber {
steps: Default::default(),
features: Default::default(),
event_handler: Box::new(event_handler),
}
}
pub fn steps(mut self, steps: Steps<W>) -> Self {
self.steps.append(steps);
self
}
pub fn features(mut self, features: &[&str]) -> Self {
let features = features
.iter()
.map(|path| match std::path::Path::new(path).canonicalize() {
Ok(p) => globwalk::GlobWalkerBuilder::new(p, "*.feature")
.case_insensitive(true)
.build()
.expect("feature path is invalid"),
Err(e) => {
eprintln!("{}", e);
eprintln!("There was an error parsing {:?}; aborting.", path);
std::process::exit(1);
}
})
.flatten()
.filter_map(Result::ok)
.map(|entry| gherkin::Feature::parse_path(entry.path()))
.collect::<Result<Vec<_>, _>>();
let mut features = features.unwrap_or_else(|e| panic!(e));
features.sort();
self.features = features;
self
}
pub async fn run(mut self) {
let runner = crate::runner::Runner::new(self.steps.steps, std::rc::Rc::new(self.features));
let mut stream = runner.run();
while let Some(event) = stream.next().await {
self.event_handler.handle_event(event);
}
}
}