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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use gherkin;
use std::path::Path;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
use pathdiff::diff_paths;
use std::env;
use std::io::Write;
use TestResult;
use std::collections::HashMap;
use textwrap;
use std;
use OutputVisitor;
enum ScenarioResult {
Pass,
Fail,
Skip
}
pub struct DefaultOutput {
stdout: StandardStream,
cur_feature: String,
feature_count: u32,
feature_error_count: u32,
rule_count: u32,
scenarios: HashMap<gherkin::Scenario, ScenarioResult>,
step_count: u32,
skipped_count: u32,
fail_count: u32
}
impl std::default::Default for DefaultOutput {
fn default() -> DefaultOutput {
DefaultOutput {
stdout: StandardStream::stdout(ColorChoice::Always),
cur_feature: "".to_string(),
feature_count: 0,
feature_error_count: 0,
rule_count: 0,
scenarios: HashMap::new(),
step_count: 0,
skipped_count: 0,
fail_count: 0
}
}
}
fn wrap_with_comment(s: &str, c: &str, indent: &str) -> String {
let tw = textwrap::termwidth();
let w = tw - indent.chars().count();
let mut cs: Vec<String> = textwrap::wrap_iter(s, w)
.map(|x| format!("{}{}", indent, &x.trim()))
.collect();
let comment_space = tw - c.chars().count() - 2;
let last_count = cs.last().unwrap().chars().count();
if last_count > comment_space {
cs.push(format!("{: <1$}", "", comment_space))
} else {
cs.last_mut().unwrap().push_str(&format!("{: <1$}", "", comment_space - last_count));
}
cs.join("\n")
}
impl DefaultOutput {
fn set_color(&mut self, c: Color, b: bool) {
self.stdout.set_color(ColorSpec::new()
.set_fg(Some(c))
.set_bold(b)).unwrap();
}
fn write(&mut self, s: &str, c: Color, bold: bool) {
self.stdout.set_color(ColorSpec::new().set_fg(Some(c)).set_bold(bold)).unwrap();
write!(&mut self.stdout, "{}", s).unwrap();
self.stdout.set_color(ColorSpec::new().set_fg(None).set_bold(false)).unwrap();
}
fn writeln(&mut self, s: &str, c: Color, bold: bool) {
self.stdout.set_color(ColorSpec::new().set_fg(Some(c)).set_bold(bold)).unwrap();
writeln!(&mut self.stdout, "{}", s).unwrap();
self.stdout.set_color(ColorSpec::new().set_fg(None).set_bold(false)).unwrap();
}
fn writeln_cmt(&mut self, s: &str, cmt: &str, indent: &str, c: Color, bold: bool) {
self.stdout.set_color(ColorSpec::new().set_fg(Some(c)).set_bold(bold)).unwrap();
write!(&mut self.stdout, "{}", wrap_with_comment(s, cmt, indent)).unwrap();
self.stdout.set_color(ColorSpec::new().set_fg(Some(Color::White)).set_bold(false)).unwrap();
writeln!(&mut self.stdout, " {}", cmt).unwrap();
self.stdout.set_color(ColorSpec::new().set_fg(None)).unwrap();
}
fn println(&mut self, s: &str) {
writeln!(&mut self.stdout, "{}", s).unwrap();
}
fn red(&mut self, s: &str) {
self.writeln(s, Color::Red, false);
}
fn bold_white(&mut self, s: &str) {
self.writeln(s, Color::Green, true);
}
fn bold_white_comment(&mut self, s: &str, c: &str, indent: &str) {
self.writeln_cmt(s, c, indent, Color::White, true);
}
fn relpath(&self, target: &Path) -> std::path::PathBuf {
let target = target.canonicalize().expect("invalid target path");
diff_paths(&target, &env::current_dir().expect("invalid current directory"))
.expect("invalid target path")
}
fn print_step_extras(&mut self, step: &gherkin::Step) {
let indent = " ";
if let Some(ref table) = &step.table {
let mut max_size: Vec<usize> = (&table.header).iter().map(|h| h.len()).collect();
for row in &table.rows {
for (n, field) in row.iter().enumerate() {
if field.len() > max_size[n] {
max_size[n] = field.len();
}
}
}
let formatted_header_fields: Vec<String> = (&table.header).iter()
.enumerate()
.map(|(n, field)| {
format!(" {: <1$} ", field, max_size[n])
}).collect();
let formatted_row_fields: Vec<Vec<String>> = (&table.rows).iter()
.map(|row| {
row.iter().enumerate().map(|(n, field)| {
if field.parse::<f64>().is_ok() {
format!(" {: >1$} ", field, max_size[n])
} else {
format!(" {: <1$} ", field, max_size[n])
}
}).collect()
}).collect();
print!("{}", indent);
let border_color = Color::Magenta;
self.write("|", border_color.clone(), true);
for field in formatted_header_fields {
self.write(&field, Color::White, true);
self.write("|", border_color.clone(), true);
}
self.println("");
for row in formatted_row_fields {
print!("{}", indent);
self.write("|", border_color.clone(), false);
for field in row {
print!("{}", field);
self.write("|", border_color.clone(), false);
}
self.println("");
}
};
if let Some(ref docstring) = &step.docstring {
self.writeln(&format!("{}\"\"\"", indent), Color::Magenta, true);
println!("{}", textwrap::indent(docstring, indent).trim_right());
self.writeln(&format!("{}\"\"\"", indent), Color::Magenta, true);
}
}
fn print_finish(&mut self) -> Result<(), std::io::Error> {
self.set_color(Color::White, true);
write!(&mut self.stdout, "{} features", &self.feature_count)?;
if self.feature_error_count > 0 {
write!(&mut self.stdout, " (")?;
self.set_color(Color::Red, true);
write!(&mut self.stdout, "{} errored", self.feature_error_count)?;
self.set_color(Color::White, true);
write!(&mut self.stdout, ")")?;
}
if self.rule_count > 0 {
write!(&mut self.stdout, ", {} rules", &self.rule_count)?;
}
self.println("");
let scenario_passed_count = self.scenarios.values().filter(|v| {
match v {
ScenarioResult::Pass => true,
_ => false
}
}).count();
let scenario_fail_count = self.scenarios.values().filter(|v| {
match v {
ScenarioResult::Fail => true,
_ => false
}
}).count();
let scenario_skipped_count = self.scenarios.values().filter(|v| {
match v {
ScenarioResult::Skip => true,
_ => false
}
}).count();
write!(&mut self.stdout, "{} scenarios (", &self.scenarios.len())?;
if scenario_fail_count > 0 {
self.set_color(Color::Red, true);
write!(&mut self.stdout, "{} failed", scenario_fail_count)?;
self.set_color(Color::White, true);
}
if scenario_skipped_count > 0 {
if scenario_fail_count > 0 {
write!(&mut self.stdout, ", ")?;
}
self.set_color(Color::Cyan, true);
write!(&mut self.stdout, "{} skipped", scenario_skipped_count)?;
self.set_color(Color::White, true);
}
if scenario_fail_count > 0 || scenario_skipped_count > 0 {
write!(&mut self.stdout, ", ")?;
}
self.set_color(Color::Green, true);
write!(&mut self.stdout, "{} passed", scenario_passed_count)?;
self.set_color(Color::White, true);
write!(&mut self.stdout, ")")?;
self.println("");
let passed_count = self.step_count - self.skipped_count - self.fail_count;
write!(&mut self.stdout, "{} steps (", &self.step_count)?;
if self.fail_count > 0 {
self.set_color(Color::Red, true);
write!(&mut self.stdout, "{} failed", self.fail_count)?;
self.set_color(Color::White, true);
}
if self.skipped_count > 0 {
if self.fail_count > 0 {
write!(&mut self.stdout, ", ")?;
}
self.set_color(Color::Cyan, true);
write!(&mut self.stdout, "{} skipped", self.skipped_count)?;
self.set_color(Color::White, true);
}
if self.fail_count > 0 || self.skipped_count > 0 {
write!(&mut self.stdout, ", ")?;
}
self.set_color(Color::Green, true);
write!(&mut self.stdout, "{} passed", passed_count)?;
self.set_color(Color::White, true);
write!(&mut self.stdout, ")")?;
self.println("");
self.stdout.set_color(ColorSpec::new()
.set_fg(None)
.set_bold(false))?;
self.println("");
Ok(())
}
}
impl OutputVisitor for DefaultOutput {
fn visit_start(&mut self) {
self.bold_white(&format!("[Cucumber v{}]\n", env!("CARGO_PKG_VERSION")))
}
fn visit_feature(&mut self, feature: &gherkin::Feature, path: &Path) {
self.cur_feature = self.relpath(&path).to_string_lossy().to_string();
let msg = &format!("Feature: {}", &feature.name);
let cmt = &format!("{}:{}:{}", &self.cur_feature, feature.position.0, feature.position.1);
self.bold_white_comment(msg, cmt, "");
println!("");
self.feature_count += 1;
}
fn visit_feature_end(&mut self, _feature: &gherkin::Feature) {}
fn visit_feature_error<'r>(&mut self, path: &Path, error: &gherkin::Error<'r>) {
let position = gherkin::error_position(error);
let relpath = self.relpath(&path).to_string_lossy().to_string();
let loc = &format!("{}:{}:{}", &relpath, position.0, position.1);
self.writeln_cmt(
&format!(
"{:—<1$}", "! Parsing feature failed: ",
textwrap::termwidth() - loc.chars().count() - 7
),
&loc,
"———— ",
Color::Red,
true);
self.red(
&textwrap::indent(
&textwrap::fill(
&format!("{}", error),
textwrap::termwidth() - 4
),
" "
).trim_right()
);
self.writeln(&format!("{:—<1$}\n", "", textwrap::termwidth()), Color::Red, true);
self.feature_error_count += 1;
}
fn visit_rule(&mut self, rule: &gherkin::Rule) {
let cmt = &format!("{}:{}:{}", &self.cur_feature, rule.position.0, rule.position.1);
self.bold_white_comment(&format!("Rule: {}\n", &rule.name), cmt, " ");
}
fn visit_rule_end(&mut self, _rule: &gherkin::Rule) {
self.rule_count += 1;
}
fn visit_scenario(&mut self, rule: Option<&gherkin::Rule>, scenario: &gherkin::Scenario) {
let cmt = &format!("{}:{}:{}", &self.cur_feature, scenario.position.0, scenario.position.1);
let indent = if rule.is_some() { " " } else { " " };
self.bold_white_comment(&format!("Scenario: {}", &scenario.name), cmt, indent);
}
fn visit_scenario_skipped(&mut self, _rule: Option<&gherkin::Rule>, scenario: &gherkin::Scenario) {
if !self.scenarios.contains_key(scenario) {
self.scenarios.insert(scenario.clone(), ScenarioResult::Skip);
}
}
fn visit_scenario_end(&mut self, _rule: Option<&gherkin::Rule>, scenario: &gherkin::Scenario) {
if !self.scenarios.contains_key(scenario) {
self.scenarios.insert(scenario.clone(), ScenarioResult::Pass);
}
self.println("");
}
fn visit_step(&mut self, _rule: Option<&gherkin::Rule>, _scenario: &gherkin::Scenario, _step: &gherkin::Step) {
self.step_count += 1;
}
fn visit_step_result(&mut self, rule: Option<&gherkin::Rule>, scenario: &gherkin::Scenario, step: &gherkin::Step, result: &TestResult) {
let cmt = &format!("{}:{}:{}", &self.cur_feature, step.position.0, step.position.1);
let msg = &format!("{}", &step.to_string());
let indent = if rule.is_some() { " " } else { " " };
match result {
TestResult::Pass => {
self.writeln_cmt(&format!("✔ {}", msg), cmt, indent, Color::Green, false);
self.print_step_extras(step);
},
TestResult::Fail(panic_info, captured_output) => {
self.writeln_cmt(&format!("✘ {}", msg), cmt, indent, Color::Red, false);
self.print_step_extras(step);
self.writeln_cmt(
&format!(
"{:—<1$}", "! Step failed: ",
textwrap::termwidth() - panic_info.location.chars().count() - 7
),
&panic_info.location,
"———— ",
Color::Red,
true);
self.red(&textwrap::indent(&textwrap::fill(&panic_info.payload, textwrap::termwidth() - 4), " ").trim_right());
if captured_output.len() > 0 {
self.writeln(&format!("{:—<1$}", "———— Captured output: ", textwrap::termwidth()), Color::Red, true);
let output_str = String::from_utf8(captured_output.to_vec()).unwrap_or_else(|_| format!("{:?}", captured_output));
self.red(&textwrap::indent(&textwrap::fill(&output_str, textwrap::termwidth() - 4), " ").trim_right());
}
self.writeln(&format!("{:—<1$}", "", textwrap::termwidth()), Color::Red, true);
self.fail_count += 1;
self.scenarios.insert(scenario.clone(), ScenarioResult::Fail);
},
TestResult::MutexPoisoned => {
self.writeln_cmt(&format!("- {}", msg), cmt, indent, Color::Cyan, false);
self.print_step_extras(step);
self.write(&format!("{} ⚡ ", indent), Color::Yellow, false);
self.println("Skipped due to previous error (poisoned)");
self.fail_count += 1;
},
TestResult::Skipped => {
self.writeln_cmt(&format!("- {}", msg), cmt, indent, Color::Cyan, false);
self.print_step_extras(step);
self.skipped_count += 1;
}
TestResult::Unimplemented => {
self.writeln_cmt(&format!("- {}", msg), cmt, indent, Color::Cyan, false);
self.print_step_extras(step);
self.write(&format!("{} ⚡ ", indent), Color::Yellow, false);
self.println("Not yet implemented (skipped)");
self.skipped_count += 1;
}
};
}
fn visit_finish(&mut self) {
self.print_finish().unwrap();
}
}