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
196
197
198
199
200
201
use super::{AssertionFailure, Spec};
use std::path::Path;
pub trait PathAssertions {
fn exists(&mut self);
fn does_not_exist(&mut self);
fn is_a_file(&mut self);
fn is_a_directory(&mut self);
fn has_file_name(&mut self, expected_file_name: &str);
}
impl<'s> PathAssertions for Spec<'s, &'s Path> {
fn exists(&mut self) {
let subject = self.subject;
if !self.subject.exists() {
AssertionFailure::from_spec(self)
.with_expected(format!("Path of <{:?}> to exist", subject))
.with_actual(format!("a non-existent Path"))
.fail();
}
}
fn does_not_exist(&mut self) {
let subject = self.subject;
if self.subject.exists() {
AssertionFailure::from_spec(self)
.with_expected(format!("Path of <{:?}> to not exist", subject))
.with_actual(format!("a resolvable Path"))
.fail();
}
}
fn is_a_file(&mut self) {
let subject = self.subject;
if !self.subject.is_file() {
AssertionFailure::from_spec(self)
.with_expected(format!("Path of <{:?}> to be a file", subject))
.with_actual(format!("not a resolvable file"))
.fail();
}
}
fn is_a_directory(&mut self) {
let subject = self.subject;
if !self.subject.is_dir() {
AssertionFailure::from_spec(self)
.with_expected(format!("Path of <{:?}> to be a directory", subject))
.with_actual(format!("not a resolvable directory"))
.fail();
}
}
fn has_file_name(&mut self, expected_file_name: &str) {
let subject = self.subject;
let subject_file_name = match subject.file_name() {
Some(os_string) => {
match os_string.to_str() {
Some(val) => val,
None => {
AssertionFailure::from_spec(self)
.with_expected(build_file_name_message(expected_file_name))
.with_actual(format!("an invalid UTF-8 file name"))
.fail();
unreachable!();
}
}
}
None => {
AssertionFailure::from_spec(self)
.with_expected(build_file_name_message(expected_file_name))
.with_actual(format!("a non-resolvable path <{:?}>", subject))
.fail();
unreachable!();
}
};
if !subject_file_name.eq(expected_file_name) {
AssertionFailure::from_spec(self)
.with_expected(build_file_name_message(expected_file_name))
.with_actual(format!("<{}>", subject_file_name))
.fail();
}
}
}
fn build_file_name_message(file_name: &str) -> String {
format!("Path with file name of <{}>", file_name)
}
#[cfg(test)]
mod tests {
use super::super::prelude::*;
use std::path::Path;
static MANIFEST_PATH: &'static str = env!("CARGO_MANIFEST_DIR");
#[test]
pub fn should_not_panic_if_path_exists() {
assert_that(&Path::new(MANIFEST_PATH)).exists();
}
#[test]
#[should_panic]
pub fn should_panic_if_path_does_not_exist() {
let failing_path = MANIFEST_PATH.to_string() + "/does-not-exist";
assert_that(&Path::new(&failing_path)).exists();
}
#[test]
pub fn should_not_panic_if_path_represents_a_directory() {
assert_that(&Path::new(MANIFEST_PATH)).is_a_directory();
}
#[test]
pub fn should_not_panic_if_path_does_not_exist_when_expected() {
let failing_path = MANIFEST_PATH.to_string() + "/does-not-exist";
assert_that(&Path::new(&failing_path)).does_not_exist();
}
#[test]
#[should_panic]
pub fn should_panic_if_path_exists_when_not_expected() {
assert_that(&Path::new(MANIFEST_PATH)).does_not_exist();
}
#[test]
#[should_panic]
pub fn should_panic_if_path_does_not_represent_a_directory() {
let path = MANIFEST_PATH.to_string() + "/Cargo.toml";
assert_that(&Path::new(&path)).is_a_directory();
}
#[test]
pub fn should_not_panic_if_path_represents_a_file() {
let path = MANIFEST_PATH.to_string() + "/Cargo.toml";
assert_that(&Path::new(&path)).is_a_file();
}
#[test]
#[should_panic]
pub fn should_panic_if_path_does_not_represent_a_file() {
assert_that(&Path::new(&MANIFEST_PATH)).is_a_file();
}
#[test]
pub fn should_not_panic_if_path_has_correct_file_name() {
let path = MANIFEST_PATH.to_string() + "/Cargo.toml";
assert_that(&Path::new(&path)).has_file_name(&"Cargo.toml");
}
#[test]
#[should_panic]
pub fn should_panic_if_path_does_not_have_correct_file_name() {
let path = MANIFEST_PATH.to_string() + "/Cargo.toml";
assert_that(&Path::new(&path)).has_file_name(&"pom.xml");
}
#[test]
#[should_panic]
pub fn should_panic_if_path_does_not_have_a_file_name() {
let path = MANIFEST_PATH.to_string() + "/..";
assert_that(&Path::new(&path)).has_file_name(&"pom.xml");
}
}