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

Add negative versions of string matches #34

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ impl Assert {
self.expect_stdout = Some(OutputAssertion {
expect: output.into(),
fuzzy: true,
expected_result: true,
kind: StdOut,
});
self
Expand All @@ -329,6 +330,7 @@ impl Assert {
self.expect_stdout = Some(OutputAssertion {
expect: output.into(),
fuzzy: false,
expected_result: true,
kind: StdOut,
});
self
Expand All @@ -351,6 +353,7 @@ impl Assert {
self.expect_stderr = Some(OutputAssertion {
expect: output.into(),
fuzzy: true,
expected_result: true,
kind: StdErr,
});
self
Expand All @@ -373,6 +376,99 @@ impl Assert {
self.expect_stderr = Some(OutputAssertion {
expect: output.into(),
fuzzy: false,
expected_result: true,
kind: StdErr,
});
self
}

/// Expect the command's output to not **contain** `output`.
///
/// # Examples
///
/// ```rust
/// extern crate assert_cli;
///
/// assert_cli::Assert::command(&["echo", "42"])
/// .doesnt_print("73")
/// .execute()
/// .unwrap();
/// ```
pub fn doesnt_print<O: Into<String>>(mut self, output: O) -> Self {
self.expect_stdout = Some(OutputAssertion {
expect: output.into(),
fuzzy: true,
expected_result: false,
kind: StdOut,
});
self
}

/// Expect the command to output to not be **exactly** this `output`.
///
/// # Examples
///
/// ```rust
/// extern crate assert_cli;
///
/// assert_cli::Assert::command(&["echo", "42"])
/// .doesnt_print_exactly("73")
/// .execute()
/// .unwrap();
/// ```
pub fn doesnt_print_exactly<O: Into<String>>(mut self, output: O) -> Self {
self.expect_stdout = Some(OutputAssertion {
expect: output.into(),
fuzzy: false,
expected_result: false,
kind: StdOut,
});
self
}

/// Expect the command's stderr output to not **contain** `output`.
///
/// # Examples
///
/// ```rust
/// extern crate assert_cli;
///
/// assert_cli::Assert::command(&["cat", "non-existing-file"])
/// .fails()
/// .and()
/// .doesnt_print_error("content")
/// .execute()
/// .unwrap();
/// ```
pub fn doesnt_print_error<O: Into<String>>(mut self, output: O) -> Self {
self.expect_stderr = Some(OutputAssertion {
expect: output.into(),
fuzzy: true,
expected_result: false,
kind: StdErr,
});
self
}

/// Expect the command to output to not be **exactly** this `output` to stderr.
///
/// # Examples
///
/// ```rust
/// extern crate assert_cli;
///
/// assert_cli::Assert::command(&["cat", "non-existing-file"])
/// .fails_with(1)
/// .and()
/// .doesnt_print_error_exactly("content")
/// .execute()
/// .unwrap();
/// ```
pub fn doesnt_print_error_exactly<O: Into<String>>(mut self, output: O) -> Self {
self.expect_stderr = Some(OutputAssertion {
expect: output.into(),
fuzzy: false,
expected_result: false,
kind: StdErr,
});
self
Expand Down
35 changes: 27 additions & 8 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,35 @@ use diff;
pub struct OutputAssertion<T> {
pub expect: String,
pub fuzzy: bool,
pub expected_result: bool,
pub kind: T,
}

impl<T: OutputType> OutputAssertion<T> {
fn matches_fuzzy(&self, got: &str) -> Result<()> {
if !got.contains(&self.expect) {
bail!(ErrorKind::OutputMismatch(self.expect.clone(), got.into()));
let result = got.contains(&self.expect);
if result != self.expected_result {
if self.expected_result {
bail!(ErrorKind::OutputDoesntContain(self.expect.clone(), got.into()));
} else {
bail!(ErrorKind::OutputContains(self.expect.clone(), got.into()));
}
}

Ok(())
}

fn matches_exact(&self, got: &str) -> Result<()> {
let differences = Changeset::new(self.expect.trim(), got.trim(), "\n");

if differences.distance > 0 {
let nice_diff = diff::render(&differences)?;
bail!(ErrorKind::ExactOutputMismatch(nice_diff));
let result = differences.distance == 0;

if result != self.expected_result {
if self.expected_result {
let nice_diff = diff::render(&differences)?;
bail!(ErrorKind::OutputDoesntMatch(nice_diff));
} else {
bail!(ErrorKind::OutputMatches(got.to_owned()));
}
}

Ok(())
Expand Down Expand Up @@ -88,14 +99,22 @@ mod errors {
Fmt(::std::fmt::Error);
}
errors {
OutputMismatch(expected: String, got: String) {
OutputDoesntContain(expected: String, got: String) {
description("Output was not as expected")
display("expected to contain {:?}, got {:?}", expected, got)
}
ExactOutputMismatch(diff: String) {
OutputContains(expected: String, got: String) {
description("Output was not as expected")
display("expected to not contain {:?}, got {:?}", expected, got)
}
OutputDoesntMatch(diff: String) {
description("Output was not as expected")
display("{}", diff)
}
OutputMatches(got: String) {
description("Output was not as expected")
display("{}", got)
}
}
}
}