diff --git a/src/assert.rs b/src/assert.rs index 0a78f5c..e4a43f4 100644 --- a/src/assert.rs +++ b/src/assert.rs @@ -4,7 +4,7 @@ use std::path::PathBuf; use std::vec::Vec; use errors::*; -use output::{OutputAssertion, StdErr, StdOut}; +use output::{OutputAssertion, OutputKind}; /// Assertions for a specific command. #[derive(Debug)] @@ -13,8 +13,7 @@ pub struct Assert { current_dir: Option, expect_success: Option, expect_exit_code: Option, - expect_stdout: Vec>, - expect_stderr: Vec>, + expect_output: Vec, } impl default::Default for Assert { @@ -28,8 +27,7 @@ impl default::Default for Assert { current_dir: None, expect_success: Some(true), expect_exit_code: None, - expect_stdout: vec![], - expect_stderr: vec![], + expect_output: vec![], } } } @@ -190,11 +188,11 @@ impl Assert { /// .unwrap(); /// ``` pub fn prints>(mut self, output: O) -> Self { - self.expect_stdout.push(OutputAssertion { + self.expect_output.push(OutputAssertion { expect: output.into(), fuzzy: true, expected_result: true, - kind: StdOut, + kind: OutputKind::StdOut, }); self } @@ -211,11 +209,11 @@ impl Assert { /// .unwrap(); /// ``` pub fn prints_exactly>(mut self, output: O) -> Self { - self.expect_stdout.push(OutputAssertion { + self.expect_output.push(OutputAssertion { expect: output.into(), fuzzy: false, expected_result: true, - kind: StdOut, + kind: OutputKind::StdOut, }); self } @@ -234,11 +232,11 @@ impl Assert { /// .unwrap(); /// ``` pub fn prints_error>(mut self, output: O) -> Self { - self.expect_stderr.push(OutputAssertion { + self.expect_output.push(OutputAssertion { expect: output.into(), fuzzy: true, expected_result: true, - kind: StdErr, + kind: OutputKind::StdErr, }); self } @@ -257,11 +255,11 @@ impl Assert { /// .unwrap(); /// ``` pub fn prints_error_exactly>(mut self, output: O) -> Self { - self.expect_stderr.push(OutputAssertion { + self.expect_output.push(OutputAssertion { expect: output.into(), fuzzy: false, expected_result: true, - kind: StdErr, + kind: OutputKind::StdErr, }); self } @@ -279,11 +277,11 @@ impl Assert { /// .unwrap(); /// ``` pub fn doesnt_print>(mut self, output: O) -> Self { - self.expect_stdout.push(OutputAssertion { + self.expect_output.push(OutputAssertion { expect: output.into(), fuzzy: true, expected_result: false, - kind: StdOut, + kind: OutputKind::StdOut, }); self } @@ -301,11 +299,11 @@ impl Assert { /// .unwrap(); /// ``` pub fn doesnt_print_exactly>(mut self, output: O) -> Self { - self.expect_stdout.push(OutputAssertion { + self.expect_output.push(OutputAssertion { expect: output.into(), fuzzy: false, expected_result: false, - kind: StdOut, + kind: OutputKind::StdOut, }); self } @@ -325,11 +323,11 @@ impl Assert { /// .unwrap(); /// ``` pub fn doesnt_print_error>(mut self, output: O) -> Self { - self.expect_stderr.push(OutputAssertion { + self.expect_output.push(OutputAssertion { expect: output.into(), fuzzy: true, expected_result: false, - kind: StdErr, + kind: OutputKind::StdErr, }); self } @@ -349,11 +347,11 @@ impl Assert { /// .unwrap(); /// ``` pub fn doesnt_print_error_exactly>(mut self, output: O) -> Self { - self.expect_stderr.push(OutputAssertion { + self.expect_output.push(OutputAssertion { expect: output.into(), fuzzy: false, expected_result: false, - kind: StdErr, + kind: OutputKind::StdErr, }); self } @@ -399,13 +397,9 @@ impl Assert { )); } - self.expect_stdout + self.expect_output .iter() - .map(|a| a.execute(&output).map_err(|e| ErrorKind::StdoutMismatch(self.cmd.clone(), e).into())) - .collect::>>()?; - self.expect_stderr - .iter() - .map(|a| a.execute(&output).map_err(|e| ErrorKind::StderrMismatch(self.cmd.clone(), e).into())) + .map(|a| a.execute(&output, &self.cmd)) .collect::>>()?; Ok(()) diff --git a/src/output.rs b/src/output.rs index b95fee2..3376226 100644 --- a/src/output.rs +++ b/src/output.rs @@ -1,4 +1,3 @@ -use std::fmt; use std::process::Output; use difference::Changeset; @@ -8,14 +7,14 @@ pub use self::errors::{Error, ErrorKind}; use diff; #[derive(Debug, Clone)] -pub struct OutputAssertion { +pub struct OutputAssertion { pub expect: String, pub fuzzy: bool, pub expected_result: bool, - pub kind: T, + pub kind: OutputKind, } -impl OutputAssertion { +impl OutputAssertion { fn matches_fuzzy(&self, got: &str) -> Result<()> { let result = got.contains(&self.expect); if result != self.expected_result { @@ -45,51 +44,37 @@ impl OutputAssertion { Ok(()) } - pub fn execute(&self, output: &Output) -> Result<()> { + pub fn execute(&self, output: &Output, cmd: &[String]) -> super::errors::Result<()> { let observed = String::from_utf8_lossy(self.kind.select(output)); - if self.fuzzy { + let result = if self.fuzzy { self.matches_fuzzy(&observed) } else { self.matches_exact(&observed) - } + }; + result.map_err(|e| self.kind.map_err(e, cmd)) } } - -pub trait OutputType: fmt::Display { - fn select<'a>(&self, o: &'a Output) -> &'a [u8]; -} - - #[derive(Debug, Clone, Copy)] -pub struct StdOut; - -impl fmt::Display for StdOut { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "stdout") - } -} - -impl OutputType for StdOut { - fn select<'a>(&self, o: &'a Output) -> &'a [u8] { - &o.stdout - } +pub enum OutputKind { + StdOut, + StdErr, } - -#[derive(Debug, Clone, Copy)] -pub struct StdErr; - -impl fmt::Display for StdErr { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "stderr") +impl OutputKind { + pub fn select<'a>(self, o: &'a Output) -> &'a [u8] { + match self { + OutputKind::StdOut => &o.stdout, + OutputKind::StdErr => &o.stderr, + } } -} -impl OutputType for StdErr { - fn select<'a>(&self, o: &'a Output) -> &'a [u8] { - &o.stderr + pub fn map_err(self, e: Error, cmd: &[String]) -> super::errors::Error { + match self { + OutputKind::StdOut => super::errors::ErrorKind::StdoutMismatch(cmd.to_vec(), e).into(), + OutputKind::StdErr => super::errors::ErrorKind::StderrMismatch(cmd.to_vec(), e).into(), + } } }