Skip to content

feat(assert): Show cause of assert #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 1, 2018
Merged
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
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ keywords = ["cli", "test", "assert", "command", "duct"]
name = "bin_fixture"

[dependencies]
predicates = "0.5.1"
predicates = "0.9.0"
predicates-core = "0.9.0"
predicates-tree = "0.9.0"
escargot = "0.2"
serde = { version = "1.0", features = ["derive"] }

Expand Down
185 changes: 140 additions & 45 deletions src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use std::str;

use predicates;
use predicates::str::PredicateStrExt;
use predicates_core;
use predicates_tree::CaseTreeExt;

use errors::dump_buffer;
use errors::output_fmt;
Expand Down Expand Up @@ -185,23 +187,15 @@ impl Assert {
/// ```
pub fn failure(self) -> Self {
if self.output.status.success() {
panic!(
"Unexpected success\nstdout=```{}```\n{}",
dump_buffer(&self.output.stdout),
self
);
panic!("Unexpected success\n{}", self);
}
self
}

/// Ensure the command aborted before returning a code.
pub fn interrupted(self) -> Self {
if self.output.status.code().is_some() {
panic!(
"Unexpected completion\nstdout=```{}```\n{}",
dump_buffer(&self.output.stdout),
self
);
panic!("Unexpected completion\n{}", self);
}
self
}
Expand All @@ -210,40 +204,47 @@ impl Assert {
///
/// # Examples
///
/// ```rust
/// ```rust,ignore
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
/// use predicates::prelude::*;
///
/// Command::main_binary()
/// .unwrap()
/// .env("exit", "42")
/// .assert()
/// .code(predicate::eq(42));
///
/// // which can be shortened to:
/// Command::main_binary()
/// .unwrap()
/// .env("exit", "42")
/// .assert()
/// .code(42);
/// ```
///
/// See [`IntoCodePredicate`] for other built-in conversions.
///
/// [IntoCodePredicate]: trait.IntoCodePredicate.html
pub fn code<I, P>(self, pred: I) -> Self
where
I: IntoCodePredicate<P>,
P: predicates::Predicate<i32>,
P: predicates_core::Predicate<i32>,
{
self.code_impl(&pred.into_code())
}

fn code_impl(self, pred: &predicates::Predicate<i32>) -> Self {
fn code_impl(self, pred: &predicates_core::Predicate<i32>) -> Self {
let actual_code = self.output.status.code().unwrap_or_else(|| {
panic!(
"Command interrupted\nstderr=```{}```\n{}",
dump_buffer(&self.output.stderr),
self
)
});
if !pred.eval(&actual_code) {
panic!(
"Unexpected return code\nstdout=```{}```\nstderr=```{}```\n{}",
dump_buffer(&self.output.stdout),
dump_buffer(&self.output.stderr),
self
);
if let Some(case) = pred.find_case(false, &actual_code) {
panic!("Unexpected return code, failed {}\n{}", case.tree(), self);
}
self
}
Expand All @@ -252,31 +253,44 @@ impl Assert {
///
/// # Examples
///
/// ```rust
/// ```rust,ignore
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
/// use predicates::prelude::*;
///
/// Command::main_binary()
/// .unwrap()
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stdout(predicate::str::similar("hello\n").from_utf8());
///
/// // which can be shortened to:
/// Command::main_binary()
/// .unwrap()
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stdout("hello\n");
/// ```
///
/// See [`IntoOutputPredicate`] for other built-in conversions.
///
/// [IntoOutputPredicate]: trait.IntoOutputPredicate.html
pub fn stdout<I, P>(self, pred: I) -> Self
where
I: IntoOutputPredicate<P>,
P: predicates::Predicate<[u8]>,
P: predicates_core::Predicate<[u8]>,
{
self.stdout_impl(&pred.into_output())
}

fn stdout_impl(self, pred: &predicates::Predicate<[u8]>) -> Self {
fn stdout_impl(self, pred: &predicates_core::Predicate<[u8]>) -> Self {
{
let actual = &self.output.stdout;
if !pred.eval(actual) {
panic!("Unexpected stdout\n{}", self);
if let Some(case) = pred.find_case(false, &actual) {
panic!("Unexpected stdout, failed {}\n{}", case.tree(), self);
}
}
self
Expand All @@ -286,31 +300,44 @@ impl Assert {
///
/// # Examples
///
/// ```rust
/// ```rust,ignore
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
/// use predicates::prelude::*;
///
/// Command::main_binary()
/// .unwrap()
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stderr(predicate::str::similar("world\n").from_utf8());
///
/// // which can be shortened to:
/// Command::main_binary()
/// .unwrap()
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stderr("world\n");
/// ```
///
/// See [`IntoOutputPredicate`] for other built-in conversions.
///
/// [IntoOutputPredicate]: trait.IntoOutputPredicate.html
pub fn stderr<I, P>(self, pred: I) -> Self
where
I: IntoOutputPredicate<P>,
P: predicates::Predicate<[u8]>,
P: predicates_core::Predicate<[u8]>,
{
self.stderr_impl(&pred.into_output())
}

fn stderr_impl(self, pred: &predicates::Predicate<[u8]>) -> Self {
fn stderr_impl(self, pred: &predicates_core::Predicate<[u8]>) -> Self {
{
let actual = &self.output.stderr;
if !pred.eval(actual) {
panic!("Unexpected stderr\n{}", self);
if let Some(case) = pred.find_case(false, &actual) {
panic!("Unexpected stderr, failed {}\n\n{}", case.tree(), self);
}
}
self
Expand Down Expand Up @@ -343,25 +370,27 @@ impl fmt::Debug for Assert {
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
/// use predicates::prelude::*;
///
/// Command::main_binary()
/// .unwrap()
/// .env("exit", "42")
/// .assert()
/// .code(42);
/// // which is equivalent to
/// .code(predicate::eq(42));
///
/// // which can be shortened to:
/// Command::main_binary()
/// .unwrap()
/// .env("exit", "42")
/// .assert()
/// .code(predicates::ord::eq(42));
/// .code(42);
/// ```
///
/// [`Assert::code`]: struct.Assert.html#method.code
/// [`Predicate<i32>`]: https://docs.rs/predicates/0.5.2/predicates/trait.Predicate.html
/// [`Predicate<i32>`]: https://docs.rs/predicates-core/0.9.0/predicates_core/trait.Predicate.html
pub trait IntoCodePredicate<P>
where
P: predicates::Predicate<i32>,
P: predicates_core::Predicate<i32>,
{
/// The type of the predicate being returned.
type Predicate;
Expand All @@ -372,7 +401,7 @@ where

impl<P> IntoCodePredicate<P> for P
where
P: predicates::Predicate<i32>,
P: predicates_core::Predicate<i32>,
{
type Predicate = P;

Expand Down Expand Up @@ -408,12 +437,36 @@ impl IntoCodePredicate<predicates::iter::InPredicate<i32>> for &'static [i32] {
/// Used by [`Assert::stdout`] and [`Assert::stderr`] to convert Self
/// into the needed [`Predicate<[u8]>`].
///
/// # Examples
///
/// ```rust,ignore
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
/// use predicates::prelude::*;
///
/// Command::main_binary()
/// .unwrap()
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stdout(predicate::str::similar("hello\n").from_utf8());
///
/// // which can be shortened to:
/// Command::main_binary()
/// .unwrap()
/// .env("stdout", "hello")
/// .env("stderr", "world")
/// .assert()
/// .stdout("hello\n");
/// ```
///
/// [`Assert::stdout`]: struct.Assert.html#method.stdout
/// [`Assert::stderr`]: struct.Assert.html#method.stderr
/// [`Predicate<[u8]>`]: https://docs.rs/predicates/0.5.2/predicates/trait.Predicate.html
/// [`Predicate<[u8]>`]: https://docs.rs/predicates-core/0.9.0/predicates_core/trait.Predicate.html
pub trait IntoOutputPredicate<P>
where
P: predicates::Predicate<[u8]>,
P: predicates_core::Predicate<[u8]>,
{
/// The type of the predicate being returned.
type Predicate;
Expand All @@ -424,7 +477,7 @@ where

impl<P> IntoOutputPredicate<P> for P
where
P: predicates::Predicate<[u8]>,
P: predicates_core::Predicate<[u8]>,
{
type Predicate = P;

Expand All @@ -437,7 +490,7 @@ where
/// [Predicate] used by [`IntoOutputPredicate`] for bytes.
///
/// [`IntoOutputPredicate`]: trait.IntoOutputPredicate.html
/// [Predicate]: https://docs.rs/predicates/0.5.2/predicates/trait.Predicate.html
/// [Predicate]: https://docs.rs/predicates-core/0.9.0/predicates_core/trait.Predicate.html
#[derive(Debug)]
pub struct BytesContentOutputPredicate(predicates::ord::EqPredicate<&'static [u8]>);

Expand All @@ -448,10 +501,31 @@ impl BytesContentOutputPredicate {
}
}

impl predicates::Predicate<[u8]> for BytesContentOutputPredicate {
impl predicates_core::reflection::PredicateReflection for BytesContentOutputPredicate {
fn parameters<'a>(
&'a self,
) -> Box<Iterator<Item = predicates_core::reflection::Parameter<'a>> + 'a> {
self.0.parameters()
}

/// Nested `Predicate`s of the current `Predicate`.
fn children<'a>(&'a self) -> Box<Iterator<Item = predicates_core::reflection::Child<'a>> + 'a> {
self.0.children()
}
}

impl predicates_core::Predicate<[u8]> for BytesContentOutputPredicate {
fn eval(&self, item: &[u8]) -> bool {
self.0.eval(item)
}

fn find_case<'a>(
&'a self,
expected: bool,
variable: &[u8],
) -> Option<predicates_core::reflection::Case<'a>> {
self.0.find_case(expected, variable)
}
}

impl fmt::Display for BytesContentOutputPredicate {
Expand All @@ -472,7 +546,7 @@ impl IntoOutputPredicate<BytesContentOutputPredicate> for &'static [u8] {
/// [Predicate] used by [`IntoOutputPredicate`] for [`str`].
///
/// [`IntoOutputPredicate`]: trait.IntoOutputPredicate.html
/// [Predicate]: https://docs.rs/predicates/0.5.2/predicates/trait.Predicate.html
/// [Predicate]: https://docs.rs/predicates-core/0.9.0/predicates_core/trait.Predicate.html
/// [`str`]: https://doc.rust-lang.org/std/primitive.str.html
#[derive(Debug, Clone)]
pub struct StrContentOutputPredicate(
Expand All @@ -486,10 +560,31 @@ impl StrContentOutputPredicate {
}
}

impl predicates::Predicate<[u8]> for StrContentOutputPredicate {
impl predicates_core::reflection::PredicateReflection for StrContentOutputPredicate {
fn parameters<'a>(
&'a self,
) -> Box<Iterator<Item = predicates_core::reflection::Parameter<'a>> + 'a> {
self.0.parameters()
}

/// Nested `Predicate`s of the current `Predicate`.
fn children<'a>(&'a self) -> Box<Iterator<Item = predicates_core::reflection::Child<'a>> + 'a> {
self.0.children()
}
}

impl predicates_core::Predicate<[u8]> for StrContentOutputPredicate {
fn eval(&self, item: &[u8]) -> bool {
self.0.eval(item)
}

fn find_case<'a>(
&'a self,
expected: bool,
variable: &[u8],
) -> Option<predicates_core::reflection::Case<'a>> {
self.0.find_case(expected, variable)
}
}

impl fmt::Display for StrContentOutputPredicate {
Expand Down Expand Up @@ -517,7 +612,7 @@ mod test {
fn convert_code<I, P>(pred: I) -> P
where
I: IntoCodePredicate<P>,
P: predicates::Predicate<i32>,
P: predicates_core::Predicate<i32>,
{
pred.into_code()
}
Expand Down Expand Up @@ -551,7 +646,7 @@ mod test {
fn convert_output<I, P>(pred: I) -> P
where
I: IntoOutputPredicate<P>,
P: predicates::Predicate<[u8]>,
P: predicates_core::Predicate<[u8]>,
{
pred.into_output()
}
Expand Down
Loading