1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use std::fmt;
use std::path;
use predicates;
use predicates::path::PredicateFileContentExt;
use predicates::str::PredicateStrExt;
use fs;
pub trait PathAssert {
fn assert<I, P>(&self, pred: I) -> &Self
where
I: IntoPathPredicate<P>,
P: predicates::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>,
{
assert(self.path(), pred);
self
}
}
impl PathAssert for fs::ChildPath {
fn assert<I, P>(&self, pred: I) -> &Self
where
I: IntoPathPredicate<P>,
P: predicates::Predicate<path::Path>,
{
assert(self.path(), pred);
self
}
}
fn assert<I, P>(path: &path::Path, pred: I)
where
I: IntoPathPredicate<P>,
P: predicates::Predicate<path::Path>,
{
let pred = pred.into_path();
if !pred.eval(path) {
panic!("Predicate {} failed for {:?}", pred, path);
}
}
pub trait IntoPathPredicate<P>
where
P: predicates::Predicate<path::Path>,
{
type Predicate;
fn into_path(self) -> P;
}
impl<P> IntoPathPredicate<P> for P
where
P: predicates::Predicate<path::Path>,
{
type Predicate = P;
fn into_path(self) -> Self::Predicate {
self
}
}
#[derive(Debug)]
pub struct BytesContentPathPredicate(
predicates::path::FileContentPredicate<predicates::ord::EqPredicate<&'static [u8]>>,
);
impl BytesContentPathPredicate {
pub(crate) fn new(value: &'static [u8]) -> Self {
let pred = predicates::ord::eq(value).from_file_path();
BytesContentPathPredicate(pred)
}
}
impl predicates::Predicate<path::Path> for BytesContentPathPredicate {
fn eval(&self, item: &path::Path) -> bool {
self.0.eval(item)
}
}
impl fmt::Display for BytesContentPathPredicate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl IntoPathPredicate<BytesContentPathPredicate> for &'static [u8] {
type Predicate = BytesContentPathPredicate;
fn into_path(self) -> Self::Predicate {
Self::Predicate::new(self)
}
}
#[derive(Debug, Clone)]
pub struct StrContentPathPredicate(
predicates::path::FileContentPredicate<
predicates::str::Utf8Predicate<predicates::str::DifferencePredicate>,
>,
);
impl StrContentPathPredicate {
pub(crate) fn new(value: &'static str) -> Self {
let pred = predicates::str::similar(value).from_utf8().from_file_path();
StrContentPathPredicate(pred)
}
}
impl predicates::Predicate<path::Path> for StrContentPathPredicate {
fn eval(&self, item: &path::Path) -> bool {
self.0.eval(item)
}
}
impl fmt::Display for StrContentPathPredicate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl IntoPathPredicate<StrContentPathPredicate> for &'static str {
type Predicate = StrContentPathPredicate;
fn into_path(self) -> Self::Predicate {
Self::Predicate::new(self)
}
}
#[cfg(test)]
mod test {
use super::*;
use predicates::prelude::*;
fn convert_path<I, P>(pred: I) -> P
where
I: IntoPathPredicate<P>,
P: predicates::Predicate<path::Path>,
{
pred.into_path()
}
#[test]
fn into_path_from_pred() {
let pred = convert_path(predicate::eq(path::Path::new("hello.md")));
assert!(pred.eval(path::Path::new("hello.md")));
}
#[test]
fn into_path_from_bytes() {
let pred = convert_path(b"hello\n" as &[u8]);
assert!(pred.eval(path::Path::new("tests/fixture/hello.txt")));
}
#[test]
fn into_path_from_str() {
let pred = convert_path("hello\n");
assert!(pred.eval(path::Path::new("tests/fixture/hello.txt")));
}
}