Skip to content
This repository was archived by the owner on Dec 29, 2021. It is now read-only.

Commit c25b46b

Browse files
committed
Refactor Output Traits into Enum
This simplifies the logic for generically adding new assertions at the cost of an match statement for each output assertion executed.
1 parent 57245da commit c25b46b

File tree

2 files changed

+42
-63
lines changed

2 files changed

+42
-63
lines changed

src/assert.rs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::path::PathBuf;
44
use std::vec::Vec;
55

66
use errors::*;
7-
use output::{OutputAssertion, StdErr, StdOut};
7+
use output::{OutputAssertion, OutputKind};
88

99
/// Assertions for a specific command.
1010
#[derive(Debug)]
@@ -13,8 +13,7 @@ pub struct Assert {
1313
current_dir: Option<PathBuf>,
1414
expect_success: Option<bool>,
1515
expect_exit_code: Option<i32>,
16-
expect_stdout: Vec<OutputAssertion<StdOut>>,
17-
expect_stderr: Vec<OutputAssertion<StdErr>>,
16+
expect_output: Vec<OutputAssertion>,
1817
}
1918

2019
impl default::Default for Assert {
@@ -28,8 +27,7 @@ impl default::Default for Assert {
2827
current_dir: None,
2928
expect_success: Some(true),
3029
expect_exit_code: None,
31-
expect_stdout: vec![],
32-
expect_stderr: vec![],
30+
expect_output: vec![],
3331
}
3432
}
3533
}
@@ -190,11 +188,11 @@ impl Assert {
190188
/// .unwrap();
191189
/// ```
192190
pub fn prints<O: Into<String>>(mut self, output: O) -> Self {
193-
self.expect_stdout.push(OutputAssertion {
191+
self.expect_output.push(OutputAssertion {
194192
expect: output.into(),
195193
fuzzy: true,
196194
expected_result: true,
197-
kind: StdOut,
195+
kind: OutputKind::StdOut,
198196
});
199197
self
200198
}
@@ -211,11 +209,11 @@ impl Assert {
211209
/// .unwrap();
212210
/// ```
213211
pub fn prints_exactly<O: Into<String>>(mut self, output: O) -> Self {
214-
self.expect_stdout.push(OutputAssertion {
212+
self.expect_output.push(OutputAssertion {
215213
expect: output.into(),
216214
fuzzy: false,
217215
expected_result: true,
218-
kind: StdOut,
216+
kind: OutputKind::StdOut,
219217
});
220218
self
221219
}
@@ -234,11 +232,11 @@ impl Assert {
234232
/// .unwrap();
235233
/// ```
236234
pub fn prints_error<O: Into<String>>(mut self, output: O) -> Self {
237-
self.expect_stderr.push(OutputAssertion {
235+
self.expect_output.push(OutputAssertion {
238236
expect: output.into(),
239237
fuzzy: true,
240238
expected_result: true,
241-
kind: StdErr,
239+
kind: OutputKind::StdErr,
242240
});
243241
self
244242
}
@@ -257,11 +255,11 @@ impl Assert {
257255
/// .unwrap();
258256
/// ```
259257
pub fn prints_error_exactly<O: Into<String>>(mut self, output: O) -> Self {
260-
self.expect_stderr.push(OutputAssertion {
258+
self.expect_output.push(OutputAssertion {
261259
expect: output.into(),
262260
fuzzy: false,
263261
expected_result: true,
264-
kind: StdErr,
262+
kind: OutputKind::StdErr,
265263
});
266264
self
267265
}
@@ -279,11 +277,11 @@ impl Assert {
279277
/// .unwrap();
280278
/// ```
281279
pub fn doesnt_print<O: Into<String>>(mut self, output: O) -> Self {
282-
self.expect_stdout.push(OutputAssertion {
280+
self.expect_output.push(OutputAssertion {
283281
expect: output.into(),
284282
fuzzy: true,
285283
expected_result: false,
286-
kind: StdOut,
284+
kind: OutputKind::StdOut,
287285
});
288286
self
289287
}
@@ -301,11 +299,11 @@ impl Assert {
301299
/// .unwrap();
302300
/// ```
303301
pub fn doesnt_print_exactly<O: Into<String>>(mut self, output: O) -> Self {
304-
self.expect_stdout.push(OutputAssertion {
302+
self.expect_output.push(OutputAssertion {
305303
expect: output.into(),
306304
fuzzy: false,
307305
expected_result: false,
308-
kind: StdOut,
306+
kind: OutputKind::StdOut,
309307
});
310308
self
311309
}
@@ -325,11 +323,11 @@ impl Assert {
325323
/// .unwrap();
326324
/// ```
327325
pub fn doesnt_print_error<O: Into<String>>(mut self, output: O) -> Self {
328-
self.expect_stderr.push(OutputAssertion {
326+
self.expect_output.push(OutputAssertion {
329327
expect: output.into(),
330328
fuzzy: true,
331329
expected_result: false,
332-
kind: StdErr,
330+
kind: OutputKind::StdErr,
333331
});
334332
self
335333
}
@@ -349,11 +347,11 @@ impl Assert {
349347
/// .unwrap();
350348
/// ```
351349
pub fn doesnt_print_error_exactly<O: Into<String>>(mut self, output: O) -> Self {
352-
self.expect_stderr.push(OutputAssertion {
350+
self.expect_output.push(OutputAssertion {
353351
expect: output.into(),
354352
fuzzy: false,
355353
expected_result: false,
356-
kind: StdErr,
354+
kind: OutputKind::StdErr,
357355
});
358356
self
359357
}
@@ -399,13 +397,9 @@ impl Assert {
399397
));
400398
}
401399

402-
self.expect_stdout
400+
self.expect_output
403401
.iter()
404-
.map(|a| a.execute(&output).map_err(|e| ErrorKind::StdoutMismatch(self.cmd.clone(), e).into()))
405-
.collect::<Result<Vec<()>>>()?;
406-
self.expect_stderr
407-
.iter()
408-
.map(|a| a.execute(&output).map_err(|e| ErrorKind::StderrMismatch(self.cmd.clone(), e).into()))
402+
.map(|a| a.execute(&output, &self.cmd))
409403
.collect::<Result<Vec<()>>>()?;
410404

411405
Ok(())

src/output.rs

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::fmt;
21
use std::process::Output;
32

43
use difference::Changeset;
@@ -8,14 +7,14 @@ pub use self::errors::{Error, ErrorKind};
87
use diff;
98

109
#[derive(Debug, Clone)]
11-
pub struct OutputAssertion<T> {
10+
pub struct OutputAssertion {
1211
pub expect: String,
1312
pub fuzzy: bool,
1413
pub expected_result: bool,
15-
pub kind: T,
14+
pub kind: OutputKind,
1615
}
1716

18-
impl<T: OutputType> OutputAssertion<T> {
17+
impl OutputAssertion {
1918
fn matches_fuzzy(&self, got: &str) -> Result<()> {
2019
let result = got.contains(&self.expect);
2120
if result != self.expected_result {
@@ -45,51 +44,37 @@ impl<T: OutputType> OutputAssertion<T> {
4544
Ok(())
4645
}
4746

48-
pub fn execute(&self, output: &Output) -> Result<()> {
47+
pub fn execute(&self, output: &Output, cmd: &[String]) -> super::errors::Result<()> {
4948
let observed = String::from_utf8_lossy(self.kind.select(output));
5049

51-
if self.fuzzy {
50+
let result = if self.fuzzy {
5251
self.matches_fuzzy(&observed)
5352
} else {
5453
self.matches_exact(&observed)
55-
}
54+
};
55+
result.map_err(|e| self.kind.map_err(e, cmd))
5656
}
5757
}
5858

59-
60-
pub trait OutputType: fmt::Display {
61-
fn select<'a>(&self, o: &'a Output) -> &'a [u8];
62-
}
63-
64-
6559
#[derive(Debug, Clone, Copy)]
66-
pub struct StdOut;
67-
68-
impl fmt::Display for StdOut {
69-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70-
write!(f, "stdout")
71-
}
72-
}
73-
74-
impl OutputType for StdOut {
75-
fn select<'a>(&self, o: &'a Output) -> &'a [u8] {
76-
&o.stdout
77-
}
60+
pub enum OutputKind {
61+
StdOut,
62+
StdErr,
7863
}
7964

80-
81-
#[derive(Debug, Clone, Copy)]
82-
pub struct StdErr;
83-
84-
impl fmt::Display for StdErr {
85-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86-
write!(f, "stderr")
65+
impl OutputKind {
66+
pub fn select<'a>(self, o: &'a Output) -> &'a [u8] {
67+
match self {
68+
OutputKind::StdOut => &o.stdout,
69+
OutputKind::StdErr => &o.stderr,
70+
}
8771
}
88-
}
8972

90-
impl OutputType for StdErr {
91-
fn select<'a>(&self, o: &'a Output) -> &'a [u8] {
92-
&o.stderr
73+
pub fn map_err(self, e: Error, cmd: &[String]) -> super::errors::Error {
74+
match self {
75+
OutputKind::StdOut => super::errors::ErrorKind::StdoutMismatch(cmd.to_vec(), e).into(),
76+
OutputKind::StdErr => super::errors::ErrorKind::StderrMismatch(cmd.to_vec(), e).into(),
77+
}
9378
}
9479
}
9580

0 commit comments

Comments
 (0)