-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.rs
59 lines (47 loc) · 1.21 KB
/
simple.rs
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
extern crate pseudo;
use pseudo::Mock;
trait Dependency: Clone {
fn greet<S: AsRef<str>>(&self, name: S) {
println!("Hello, {}", name.as_ref())
}
}
struct Consumer<T: Dependency> {
dependency: T,
}
impl<T: Dependency> Consumer<T> {
fn new(dependency: &T) -> Self {
Consumer {
dependency: dependency.clone(),
}
}
fn greet_everyone<S: AsRef<str>>(&self, names: Vec<S>) {
for name in names {
self.dependency.greet(name)
}
}
}
#[derive(Debug, Clone)]
struct MockDependency {
pub greet: Mock<String, ()>,
}
impl Dependency for MockDependency {
fn greet<S: AsRef<str>>(&self, name: S) {
self.greet.call(name.as_ref().to_string())
}
}
impl Default for MockDependency {
fn default() -> Self {
MockDependency {
greet: Mock::default(),
}
}
}
fn main() {
let mock = MockDependency::default();
let consumer = Consumer::new(&mock);
consumer.greet_everyone(vec!["Fido", "Spot", "Princess"]);
assert_eq!(mock.greet.num_calls(), 3);
assert!(mock.greet.called_with("Fido"));
assert!(mock.greet.called_with("Spot"));
assert!(mock.greet.called_with("Princess"));
}