-
Notifications
You must be signed in to change notification settings - Fork 14
/
trait_impl.rs
82 lines (68 loc) · 1.48 KB
/
trait_impl.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
trait MyTrait {
fn assoc();
fn method(&self);
}
trait GenericTrait<'a, T> {
fn g_assoc() {}
fn g_method(&self) {}
}
#[faux::create]
struct MyStruct {}
// normal impl can be mocked
#[faux::methods]
impl MyStruct {
fn new() -> Self {
MyStruct {}
}
}
// mocking trait impl works even if there is another mocked impl
#[faux::methods]
impl MyTrait for MyStruct {
fn assoc() {}
fn method(&self) {}
}
// mocking a second immpl trait in the same mod works
#[faux::methods]
impl<'a, T> GenericTrait<'a, T> for MyStruct {
fn g_assoc() {}
fn g_method(&self) {}
}
// more complicated generic stuff
#[allow(dead_code)]
#[faux::create]
struct GenericStruct<T, S> {
t: T,
s: S,
}
#[faux::methods]
impl<T> GenericStruct<T, i32> {
fn make(t: T, s: i32) -> Self {
GenericStruct { t, s }
}
}
#[faux::methods]
impl<'a, T> GenericTrait<'a, T> for GenericStruct<T, i32>
where
T: std::fmt::Debug,
{
fn g_assoc() {}
fn g_method(&self) {}
}
#[test]
fn simple_trait() {
MyStruct::assoc();
let my_struct = MyStruct::new();
my_struct.method();
let mut faux = MyStruct::faux();
faux::when!(faux.method).then(|_| {});
faux.method();
}
#[test]
fn generic_trait() {
GenericStruct::<String, i32>::g_assoc();
let gen_struct = GenericStruct::make("foo", 3);
gen_struct.g_method();
let mut faux = GenericStruct::<&'static str, i32>::faux();
faux::when!(faux.g_method).then(|_| {});
faux.g_method();
}