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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
pub use regex::{Captures, Regex};
use std::collections::HashMap;
use event::response::InvokeResponse;
use event::response::StepArg;
use event::response::Step as ResponseStep;
use event::request::InvokeArgument;
use definitions::registration::SimpleStep;
pub trait SendableStep<World>
: Send + Fn(&Cucumber<World>, &mut World, Vec<InvokeArgument>) -> InvokeResponse
{
}
impl<T, World> SendableStep<World> for T
where T: Send + Fn(&Cucumber<World>, &mut World, Vec<InvokeArgument>) -> InvokeResponse
{
}
pub type Step<World> = Box<SendableStep<World, Output = InvokeResponse>>;
pub type StepId = u32;
pub struct Cucumber<World> {
step_regexes: Vec<Regex>,
step_ids: HashMap<String, (StepId, String)>,
steps: HashMap<StepId, SimpleStep<World>>,
pub tags: Vec<String>,
}
impl<World> Cucumber<World> {
pub fn new() -> Cucumber<World> {
Cucumber {
step_regexes: Vec::new(),
step_ids: HashMap::new(),
steps: HashMap::new(),
tags: Vec::new(),
}
}
pub fn insert_step(&mut self, path: String, regex: Regex, step: SimpleStep<World>) {
let str_rep = regex.as_str().to_owned();
self.step_regexes.push(regex);
let this_id = self.step_ids.values().max().map(|&(ref res, _)| res + 1).unwrap_or(0);
self.step_ids.insert(str_rep, (this_id.clone(), path));
self.steps.insert(this_id, step);
}
pub fn find_match(&self, str: &str) -> Vec<ResponseStep> {
self.step_regexes.iter()
.filter_map(|ref regex| {
regex.captures(str).map(|captures| {
let captures: Vec<StepArg> =
captures
.iter_pos()
.enumerate()
.skip(1)
.map(|(idx, pos)| {
let pos = pos.map(|(begin_idx,_)| begin_idx as u32);
StepArg { pos: pos, val: captures.at(idx).map(|v| v.to_owned()) }
})
.collect();
let (id, path) = self.step_ids.get(regex.as_str()).unwrap().clone();
ResponseStep {id: id.to_string(), args: captures, source: path }
})
})
.collect()
}
pub fn pending(&self, message: &str) {
panic!(InvokeResponse::pending_from_str(message));
}
pub fn fail(&self, message: &str) {
panic!(InvokeResponse::fail_from_str(message));
}
pub fn succeed_immediately(&self) {
panic!(InvokeResponse::Success);
}
pub fn invoke(&self, str: &str, world: &mut World, extra_arg: Option<InvokeArgument>) {
let mut matches = self.find_match(str);
match matches.len() {
0 => panic!("Direct invoke matched no steps"),
1 => {
let response_step = matches.pop().unwrap();
let mut invoke_args: Vec<InvokeArgument> = response_step.args
.into_iter()
.map(|arg| InvokeArgument::from_step_arg(arg))
.collect();
if extra_arg.is_some() {
invoke_args.push(extra_arg.unwrap());
}
self.step(response_step.id
.parse()
.unwrap())
.unwrap()(&self, world, invoke_args)
},
_ => panic!("Direct invoke matched more than one step"),
}
}
pub fn step(&self, id: StepId) -> Option<&SimpleStep<World>> {
self.steps.get(&id)
}
}
#[cfg(test)]
mod test {
use super::*;
use cucumber_regex as regex;
use event::response::StepArg;
use event::response::Step as ResponseStep;
#[test]
fn cuke_instantiates() {
type World = u32;
let _: Cucumber<World> = Cucumber::new();
}
#[test]
fn cuke_inserts_step() {
type World = u32;
let mut cucumber: Cucumber<World> = Cucumber::new();
cucumber.insert_step("file:line".to_owned(),
regex::build("^example$"),
Box::new(|_, _, _| ()));
}
#[test]
fn cuke_invokes() {
type World = u32;
let mut world = 0;
let mut cucumber: Cucumber<World> = Cucumber::new();
cucumber.insert_step("file:line".to_owned(),
regex::build("^example$"),
Box::new(|_, _, _| ()));
cucumber.invoke("example", &mut world, None);
}
#[test]
#[should_panic(expected = "Direct invoke matched more than one step")]
fn cuke_invoke_fails_on_multiple_match() {
type World = u32;
let mut world = 0;
let mut cucumber: Cucumber<World> = Cucumber::new();
cucumber.insert_step("file:line".to_owned(),
regex::build("^example$"),
Box::new(|_, _, _| ()));
cucumber.insert_step("file:line".to_owned(),
regex::build("^ex"),
Box::new(|_, _, _| ()));
cucumber.invoke("example", &mut world, None);
}
#[test]
#[should_panic(expected = "Direct invoke matched no steps")]
fn cuke_invoke_fails_on_no_match() {
type World = u32;
let mut world = 0;
let cucumber: Cucumber<World> = Cucumber::new();
cucumber.invoke("example", &mut world, None)
}
#[test]
fn find_match_optional_args_work() {
type World = u32;
let mut cucumber: Cucumber<World> = Cucumber::new();
cucumber.insert_step("file:line".to_owned(),
regex::build("^example( stuff)? (\\d+)$"),
Box::new(|_, _, _| ()));
{
let mut step_matches = cucumber.find_match("example 5");
assert_eq!(step_matches.len(), 1);
let step_details = step_matches.pop().unwrap();
assert_eq!(step_details,
ResponseStep {
id: "0".to_owned(),
args: vec![StepArg {
val: None,
pos: None,
},
StepArg {
val: Some("5".to_owned()),
pos: Some(8),
}],
source: "file:line".to_owned(),
})
}
{
let mut step_matches = cucumber.find_match("example stuff 5");
assert_eq!(step_matches.len(), 1);
let step_details = step_matches.pop().unwrap();
assert_eq!(step_details,
ResponseStep {
id: "0".to_owned(),
args: vec![StepArg {
val: Some(" stuff".to_owned()),
pos: Some(7),
},
StepArg {
val: Some("5".to_owned()),
pos: Some(14),
}],
source: "file:line".to_owned(),
})
}
}
}