Skip to content

Commit cd42759

Browse files
authored
Merge pull request #18 from epage/pred
feat(assert): Show cause of failure
2 parents a84bc23 + befd968 commit cd42759

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ keywords = ["filesystem", "test", "assert", "fixture"]
1414
[dependencies]
1515
tempfile = "3.0"
1616
globwalk = "0.3"
17-
predicates = "0.5"
17+
predicates = "0.9"
18+
predicates-core = "0.9"
19+
predicates-tree = "0.9"
1820

1921
[dev-dependencies]
2022
docmatic = "0.1"

src/assert.rs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use std::path;
66
use predicates;
77
use predicates::path::PredicateFileContentExt;
88
use predicates::str::PredicateStrExt;
9+
use predicates_core;
10+
use predicates_tree::CaseTreeExt;
911

1012
use fs;
1113

@@ -15,7 +17,7 @@ use fs;
1517
///
1618
/// ```rust,ignore
1719
/// use assert_fs::*;
18-
/// use predicates::*;
20+
/// use predicates::prelude::*;
1921
///
2022
/// let temp = assert_fs::TempDir::new().unwrap();
2123
/// let input_file = temp.child("foo.txt");
@@ -30,14 +32,14 @@ pub trait PathAssert {
3032
fn assert<I, P>(&self, pred: I) -> &Self
3133
where
3234
I: IntoPathPredicate<P>,
33-
P: predicates::Predicate<path::Path>;
35+
P: predicates_core::Predicate<path::Path>;
3436
}
3537

3638
impl PathAssert for fs::TempDir {
3739
fn assert<I, P>(&self, pred: I) -> &Self
3840
where
3941
I: IntoPathPredicate<P>,
40-
P: predicates::Predicate<path::Path>,
42+
P: predicates_core::Predicate<path::Path>,
4143
{
4244
assert(self.path(), pred);
4345
self
@@ -48,7 +50,7 @@ impl PathAssert for fs::ChildPath {
4850
fn assert<I, P>(&self, pred: I) -> &Self
4951
where
5052
I: IntoPathPredicate<P>,
51-
P: predicates::Predicate<path::Path>,
53+
P: predicates_core::Predicate<path::Path>,
5254
{
5355
assert(self.path(), pred);
5456
self
@@ -58,18 +60,18 @@ impl PathAssert for fs::ChildPath {
5860
fn assert<I, P>(path: &path::Path, pred: I)
5961
where
6062
I: IntoPathPredicate<P>,
61-
P: predicates::Predicate<path::Path>,
63+
P: predicates_core::Predicate<path::Path>,
6264
{
6365
let pred = pred.into_path();
64-
if !pred.eval(path) {
65-
panic!("Predicate {} failed for {:?}", pred, path);
66+
if let Some(case) = pred.find_case(false, &path) {
67+
panic!("Unexpected file, failed {}\npath={:?}", case.tree(), path);
6668
}
6769
}
6870

6971
/// Used by `PathAssert` to convert Self into the needed `Predicate<Path>`.
7072
pub trait IntoPathPredicate<P>
7173
where
72-
P: predicates::Predicate<path::Path>,
74+
P: predicates_core::Predicate<path::Path>,
7375
{
7476
/// The type of the predicate being returned.
7577
type Predicate;
@@ -80,7 +82,7 @@ where
8082

8183
impl<P> IntoPathPredicate<P> for P
8284
where
83-
P: predicates::Predicate<path::Path>,
85+
P: predicates_core::Predicate<path::Path>,
8486
{
8587
type Predicate = P;
8688

@@ -103,7 +105,20 @@ impl BytesContentPathPredicate {
103105
}
104106
}
105107

106-
impl predicates::Predicate<path::Path> for BytesContentPathPredicate {
108+
impl predicates_core::reflection::PredicateReflection for BytesContentPathPredicate {
109+
fn parameters<'a>(
110+
&'a self,
111+
) -> Box<Iterator<Item = predicates_core::reflection::Parameter<'a>> + 'a> {
112+
self.0.parameters()
113+
}
114+
115+
/// Nested `Predicate`s of the current `Predicate`.
116+
fn children<'a>(&'a self) -> Box<Iterator<Item = predicates_core::reflection::Child<'a>> + 'a> {
117+
self.0.children()
118+
}
119+
}
120+
121+
impl predicates_core::Predicate<path::Path> for BytesContentPathPredicate {
107122
fn eval(&self, item: &path::Path) -> bool {
108123
self.0.eval(item)
109124
}
@@ -139,7 +154,20 @@ impl StrContentPathPredicate {
139154
}
140155
}
141156

142-
impl predicates::Predicate<path::Path> for StrContentPathPredicate {
157+
impl predicates_core::reflection::PredicateReflection for StrContentPathPredicate {
158+
fn parameters<'a>(
159+
&'a self,
160+
) -> Box<Iterator<Item = predicates_core::reflection::Parameter<'a>> + 'a> {
161+
self.0.parameters()
162+
}
163+
164+
/// Nested `Predicate`s of the current `Predicate`.
165+
fn children<'a>(&'a self) -> Box<Iterator<Item = predicates_core::reflection::Child<'a>> + 'a> {
166+
self.0.children()
167+
}
168+
}
169+
170+
impl predicates_core::Predicate<path::Path> for StrContentPathPredicate {
143171
fn eval(&self, item: &path::Path) -> bool {
144172
self.0.eval(item)
145173
}
@@ -170,7 +198,7 @@ mod test {
170198
fn convert_path<I, P>(pred: I) -> P
171199
where
172200
I: IntoPathPredicate<P>,
173-
P: predicates::Predicate<path::Path>,
201+
P: predicates_core::Predicate<path::Path>,
174202
{
175203
pred.into_path()
176204
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
extern crate globwalk;
2828
extern crate predicates;
29+
extern crate predicates_core;
30+
extern crate predicates_tree;
2931
extern crate tempfile;
3032

3133
pub mod assert;

0 commit comments

Comments
 (0)