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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use regex::Regex;
use state::Cucumber;
use event::request::{InvokeArgument, Request};
use event::response::{InvokeResponse, Response, StepMatchesResponse};
use definitions::registration::{CucumberRegistrar, SimpleStep};
use std::panic::{self, AssertUnwindSafe};
use std::str::FromStr;
#[allow(dead_code)]
pub struct WorldRunner<World> {
cuke: Cucumber<World>,
world: World,
}
impl<World> WorldRunner<World> {
#[allow(dead_code)]
pub fn new(world: World) -> WorldRunner<World> {
WorldRunner {
cuke: Cucumber::new(),
world: world,
}
}
}
pub trait CommandRunner {
fn execute_cmd(&mut self, req: Request) -> Response;
}
impl<T: Fn(Request) -> Response> CommandRunner for T {
fn execute_cmd(&mut self, req: Request) -> Response {
self(req)
}
}
impl<World> CommandRunner for WorldRunner<World> {
fn execute_cmd(&mut self, req: Request) -> Response {
match req {
Request::BeginScenario(params) => {
self.cuke.tags = params.tags;
Response::BeginScenario
},
Request::Invoke(params) => {
let step = self.cuke
.step(u32::from_str(¶ms.id).unwrap())
.unwrap();
Response::Invoke(invoke_to_response(step, &self.cuke, &mut self.world, params.args))
},
Request::StepMatches(params) => {
let matches = self.cuke.find_match(¶ms.name_to_match);
if matches.len() == 0 {
Response::StepMatches(StepMatchesResponse::NoMatch)
} else {
Response::StepMatches(StepMatchesResponse::Match(matches))
}
},
Request::EndScenario(_) => {
self.cuke.tags = Vec::new();
Response::EndScenario
},
Request::SnippetText(params) => {
let text = format!(" // In a step registration block where cuke: &mut \
CucumberRegistrar<YourWorld>\n use cucumber::InvokeResponse;\n use \
cucumber::helpers::r;\n {}!(cuke, r(\"^{}$\"), Box::new(move |c, _, \
_| {{\n c.pending(\"TODO\")\n }}));",
params.step_keyword,
params.step_name);
Response::SnippetText(text)
},
}
}
}
impl<World> CucumberRegistrar<World> for WorldRunner<World> {
fn given(&mut self, file: &str, line: u32, regex: Regex, step: SimpleStep<World>) {
self.cuke.given(file, line, regex, step)
}
fn when(&mut self, file: &str, line: u32, regex: Regex, step: SimpleStep<World>) {
self.cuke.when(file, line, regex, step)
}
fn then(&mut self, file: &str, line: u32, regex: Regex, step: SimpleStep<World>) {
self.cuke.then(file, line, regex, step)
}
}
pub fn invoke_to_response<World>(test_body: &SimpleStep<World>,
cuke: &Cucumber<World>,
world: &mut World,
args: Vec<InvokeArgument>)
-> InvokeResponse {
let result = panic::catch_unwind(AssertUnwindSafe(|| test_body(cuke, world, args)));
match result {
Ok(()) => InvokeResponse::Success,
Err(err) => {
let msg = match err.downcast_ref::<&'static str>() {
Some(s) => *s,
None => {
match err.downcast_ref::<String>() {
Some(s) => &s[..],
None => {
match err.downcast_ref::<InvokeResponse>() {
Some(s) => return s.clone(),
None => "Box<Any>",
}
},
}
},
};
InvokeResponse::fail_from_str(msg)
},
}
}