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

Commit

Permalink
Output assertion error stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
killercup committed Mar 27, 2017
1 parent b386d23 commit 2620e07
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
10 changes: 4 additions & 6 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ extern crate colored;
use self::colored::Colorize;

use difference::{Difference, Changeset};
use std::fmt::Write;
use std::fmt::{Write, Error as fmtError};

use errors::*;

pub fn render(&Changeset { ref diffs, .. }: &Changeset) -> Result<String> {
pub fn render(&Changeset { ref diffs, .. }: &Changeset) -> Result<String, fmtError> {
let mut t = String::new();

for (i, diff) in diffs.iter().enumerate() {
Expand Down Expand Up @@ -66,11 +64,11 @@ mod tests {
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.",
"Lorem ipsum dolor sit amet, consectetur adipisicing elit,
"Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor **incididunt** ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.",
"\n");
"\n");
println!("{}", render(&diff).unwrap());
assert_eq!(render(&diff).unwrap(), " Lorem ipsum dolor sit amet, consectetur adipisicing elit,\n\u{1b}[31m-sed do eiusmod tempor incididunt ut labore et dolore magna\u{1b}[0m\n\u{1b}[32m+\u{1b}[0m\u{1b}[32msed do eiusmod tempor\u{1b}[0m \u{1b}[7;32m**incididunt**\u{1b}[0m \u{1b}[32mut labore et dolore magna\u{1b}[0m \n aliqua. Ut enim ad minim veniam, quis nostrud exercitation\nullamco laboris nisi ut aliquip ex ea commodo consequat.\n");
}
Expand Down
26 changes: 10 additions & 16 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ error_chain! {
StatusMismatch(cmd: Vec<String>, expected: bool) {
description("Wrong status")
display(
"{}: `(command `{}` expected to {})` (command {})",
"{}: (command `{}` expected to {}) (command {})",
ERROR_PREFIX,
cmd.join(" "),
expected = if *expected { "succeed" } else { "fail" },
Expand All @@ -19,33 +19,27 @@ error_chain! {
ExitCodeMismatch(cmd: Vec<String>, expected: Option<i32>, got: Option<i32>) {
description("Wrong exit code")
display(
"{}: `(exit code of `{}` expected to be `{:?}`)` (exit code was: `{:?}`)",
"{}: (exit code of `{}` expected to be `{:?}`) (exit code was: `{:?}`)",
ERROR_PREFIX,
cmd.join(" "),
expected,
got,
)
}
OutputMismatch(output_name: String, cmd: Vec<String>, expected: String, got: String) {
StdoutMismatch(cmd: Vec<String>, output_err: ::output::Error) {
description("Output was not as expected")
display(
"{}: `({} of `{}` expected to contain `{:?}`)` (output was: `{:?}`)",
ERROR_PREFIX,
output_name,
cmd.join(" "),
expected,
got,
"{}: `{}` stdout mismatch: `{}`)",
ERROR_PREFIX, cmd.join(" "), output_err,
)
}
ExactOutputMismatch(output_name: String, cmd: Vec<String>, diff: String) {
description("Output was not as expected")
StderrMismatch(cmd: Vec<String>, output_err: ::output::Error) {
description("Error output was not as expected")
display(
"{}: `({} of `{}` was not as expected)`\n{}\n",
ERROR_PREFIX,
output_name,
cmd.join(" "),
diff.trim()
"{}: `{}` stderr mismatch: `{}`)",
ERROR_PREFIX, cmd.join(" "), output_err,
)
}

}
}
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,14 @@ impl Assert {
));
}

if let Some(ouput_assertion) = self.expect_stdout {
ouput_assertion.execute(&output)?;
if let Some(ref ouput_assertion) = self.expect_stdout {
ouput_assertion.execute(&output)
.map_err(|e| ErrorKind::StdoutMismatch(self.cmd.clone(), e))?;
}

if let Some(ouput_assertion) = self.expect_stderr {
ouput_assertion.execute(&output)?;
if let Some(ref ouput_assertion) = self.expect_stderr {
ouput_assertion.execute(&output)
.map_err(|e| ErrorKind::StderrMismatch(self.cmd.clone(), e))?;
}

Ok(())
Expand Down
35 changes: 23 additions & 12 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::process::Output;

use difference::Changeset;

use errors::*;
use self::errors::*;
pub use self::errors::{Error, ErrorKind};
use diff;

#[derive(Debug, Clone)]
Expand All @@ -16,12 +17,7 @@ pub struct OutputAssertion<T> {
impl<T: OutputType> OutputAssertion<T> {
fn matches_fuzzy(&self, got: &str) -> Result<()> {
if !got.contains(&self.expect) {
bail!(ErrorKind::OutputMismatch(
self.kind.to_string(),
vec!["Foo".to_string()],
self.expect.clone(),
got.into(),
));
bail!(ErrorKind::OutputMismatch(self.expect.clone(), got.into()));
}

Ok(())
Expand All @@ -32,11 +28,7 @@ impl<T: OutputType> OutputAssertion<T> {

if differences.distance > 0 {
let nice_diff = diff::render(&differences)?;
bail!(ErrorKind::ExactOutputMismatch(
self.kind.to_string(),
vec!["Foo".to_string()],
nice_diff
));
bail!(ErrorKind::ExactOutputMismatch(nice_diff));
}

Ok(())
Expand Down Expand Up @@ -89,3 +81,22 @@ impl OutputType for StdErr {
&o.stderr
}
}

mod errors {
error_chain! {
foreign_links {
// Io(::std::io::Error);
Fmt(::std::fmt::Error);
}
errors {
OutputMismatch(expected: String, got: String) {
description("Output was not as expected")
display("expected {:?}, got {:?}", expected, got)

This comment has been minimized.

Copy link
@colin-kiegel

colin-kiegel Mar 27, 2017

Collaborator

@killercup I'd suggest "expected to contain {:?}, ..." otherwise it's not clear that this is a fuzzy assertion. :-)

}
ExactOutputMismatch(diff: String) {
description("Output was not as expected")
display("{}", diff)
}
}
}
}

0 comments on commit 2620e07

Please sign in to comment.