Skip to content

feat(assert): Show cause of failure #18

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 1 commit 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 @@ -14,7 +14,9 @@ keywords = ["filesystem", "test", "assert", "fixture"]
[dependencies]
tempfile = "3.0"
globwalk = "0.3"
predicates = "0.5"
predicates = "0.9"
predicates-core = "0.9"
predicates-tree = "0.9"

[dev-dependencies]
docmatic = "0.1"
52 changes: 40 additions & 12 deletions src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::path;
use predicates;
use predicates::path::PredicateFileContentExt;
use predicates::str::PredicateStrExt;
use predicates_core;
use predicates_tree::CaseTreeExt;

use fs;

Expand All @@ -15,7 +17,7 @@ use fs;
///
/// ```rust,ignore
/// use assert_fs::*;
/// use predicates::*;
/// use predicates::prelude::*;
///
/// let temp = assert_fs::TempDir::new().unwrap();
/// let input_file = temp.child("foo.txt");
Expand All @@ -30,14 +32,14 @@ pub trait PathAssert {
fn assert<I, P>(&self, pred: I) -> &Self
where
I: IntoPathPredicate<P>,
P: predicates::Predicate<path::Path>;
P: predicates_core::Predicate<path::Path>;
}

impl PathAssert for fs::TempDir {
fn assert<I, P>(&self, pred: I) -> &Self
where
I: IntoPathPredicate<P>,
P: predicates::Predicate<path::Path>,
P: predicates_core::Predicate<path::Path>,
{
assert(self.path(), pred);
self
Expand All @@ -48,7 +50,7 @@ impl PathAssert for fs::ChildPath {
fn assert<I, P>(&self, pred: I) -> &Self
where
I: IntoPathPredicate<P>,
P: predicates::Predicate<path::Path>,
P: predicates_core::Predicate<path::Path>,
{
assert(self.path(), pred);
self
Expand All @@ -58,18 +60,18 @@ impl PathAssert for fs::ChildPath {
fn assert<I, P>(path: &path::Path, pred: I)
where
I: IntoPathPredicate<P>,
P: predicates::Predicate<path::Path>,
P: predicates_core::Predicate<path::Path>,
{
let pred = pred.into_path();
if !pred.eval(path) {
panic!("Predicate {} failed for {:?}", pred, path);
if let Some(case) = pred.find_case(false, &path) {
panic!("Unexpected file, failed {}\npath={:?}", case.tree(), path);
}
}

/// Used by `PathAssert` to convert Self into the needed `Predicate<Path>`.
pub trait IntoPathPredicate<P>
where
P: predicates::Predicate<path::Path>,
P: predicates_core::Predicate<path::Path>,
{
/// The type of the predicate being returned.
type Predicate;
Expand All @@ -80,7 +82,7 @@ where

impl<P> IntoPathPredicate<P> for P
where
P: predicates::Predicate<path::Path>,
P: predicates_core::Predicate<path::Path>,
{
type Predicate = P;

Expand All @@ -103,7 +105,20 @@ impl BytesContentPathPredicate {
}
}

impl predicates::Predicate<path::Path> for BytesContentPathPredicate {
impl predicates_core::reflection::PredicateReflection for BytesContentPathPredicate {
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<path::Path> for BytesContentPathPredicate {
fn eval(&self, item: &path::Path) -> bool {
self.0.eval(item)
}
Expand Down Expand Up @@ -139,7 +154,20 @@ impl StrContentPathPredicate {
}
}

impl predicates::Predicate<path::Path> for StrContentPathPredicate {
impl predicates_core::reflection::PredicateReflection for StrContentPathPredicate {
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<path::Path> for StrContentPathPredicate {
fn eval(&self, item: &path::Path) -> bool {
self.0.eval(item)
}
Expand Down Expand Up @@ -170,7 +198,7 @@ mod test {
fn convert_path<I, P>(pred: I) -> P
where
I: IntoPathPredicate<P>,
P: predicates::Predicate<path::Path>,
P: predicates_core::Predicate<path::Path>,
{
pred.into_path()
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

extern crate globwalk;
extern crate predicates;
extern crate predicates_core;
extern crate predicates_tree;
extern crate tempfile;

pub mod assert;
Expand Down