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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
#[macro_export]
macro_rules! mock {
(
$mock: ident;
impl $mocked_trait: ident for $mocked_trait_target: ident {
$(
fn $func: ident$(<
$($type_param: tt$(: ($($type_param_bound: tt +)+))?),*
>)?(
self: ($($self_type: tt)+),
$($func_param: ident: $func_param_type: ty),*
)$( -> $return_type: ty)?
$(where ($($where: tt)*))?;
)*
}
) => {
$crate::__private::paste! {
mod [<__ $mock>] {
use super::*;
pub struct $mock {
$(
[<$func _expectations>]: std::collections::HashMap<
Vec<$crate::__private::type_id::TypeID>,
[<$mock Expectation _ $func>]$(<$($crate::__to_unit!($type_param)),*>)?
>,
)*
}
impl $mock
{
pub fn new() -> Self
{
Self {
$(
[<$func _expectations>]: std::collections::HashMap::new()
),*
}
}
$(
#[allow(unused)]
pub(crate) fn [<expect_ $func>]$(<
$($type_param$(: $($type_param_bound +)*)?),*
>)?(&mut self)
-> &mut [<$mock Expectation _ $func>]$(<$($type_param),*>)?
$(where $($where)*)?
{
let ids = vec![
$($($crate::__private::type_id::TypeID::of::<$type_param>()),*)?
];
let expectation =
[<$mock Expectation _ $func>]$(::<$($type_param),*>)?::new()
.strip_type_params();
self.[<$func _expectations>].insert(ids.clone(), expectation);
self.[<$func _expectations>]
.get_mut(&ids)
.unwrap()
.with_type_params_mut()
}
)*
}
impl $mocked_trait for $mock {
$(
fn $func$(<$($type_param$(: $($type_param_bound +)+)?),*>)?(
self: $($self_type)+,
$($func_param: $func_param_type),*
)$( -> $return_type)?
$(where $($where)*)?
{
let ids = vec![
$($($crate::__private::type_id::TypeID::of::<$type_param>()),*)?
];
let expectation = self
.[<$func _expectations>]
.get(&ids)
.expect(concat!(
"No expectation found for function ",
stringify!($func)
))
.with_type_params$(::<$($type_param),*>)?();
let Some(returning) = &expectation.returning else {
panic!(concat!(
"Expectation for function",
stringify!($func),
" is missing a function to call")
);
};
returning(self, $($func_param),*)
}
)*
}
$(
#[allow(non_camel_case_types, non_snake_case)]
pub struct [<$mock Expectation _ $func>]$(<$($type_param),*>)? {
returning: Option<
fn(
$crate::__replace_ref_type!($($self_type)*, $mock),
$($func_param_type),*
)$( -> $return_type)?>,
$(
$([<$type_param _phantom>]: std::marker::PhantomData<$type_param>,)*
)?
}
impl$(<$($type_param),*>)? [<$mock Expectation _ $func>]$(<
$($type_param),*
>)? {
#[allow(unused)]
fn new() -> Self {
Self {
returning: None,
$(
$([<$type_param _phantom>]: std::marker::PhantomData,)*
)?
}
}
#[allow(unused)]
pub fn returning(
&mut self,
func: fn(
$crate::__replace_ref_type!($($self_type)*, $mock),
$($func_param_type),*
)$( -> $return_type)?
) -> &mut Self
{
self.returning = Some(func);
self
}
#[allow(unused)]
fn strip_type_params(
self,
) -> [<$mock Expectation _ $func>]$(<$($crate::__to_unit!($type_param)),*>)?
{
$crate::__if_empty_else!(($($($type_param)*)?); {
// No type parameters are present
self
}, {
// Type parameters are present
unsafe { std::mem::transmute(self) }
})
}
}
impl [<$mock Expectation _ $func>]$(<$($crate::__to_unit!($type_param)),*>)? {
fn with_type_params$(<$($type_param),*>)?(
&self,
) -> &[<$mock Expectation _ $func>]$(<$($type_param),*>)?
{
unsafe { &*(self as *const Self).cast() }
}
#[allow(unused)]
fn with_type_params_mut$(<$($type_param),*>)?(
&mut self,
) -> &mut [<$mock Expectation _ $func>]$(<$($type_param),*>)?
{
unsafe { &mut *(self as *mut Self).cast() }
}
}
)*
}
use [<__ $mock>]::$mock;
}
};
}
#[doc(hidden)]
pub mod __private
{
pub use paste::paste;
pub mod type_id
{
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TypeID
{
id: usize,
}
impl TypeID
{
#[inline]
pub fn of<T>() -> Self
{
Self {
id: Self::of::<T> as usize,
}
}
}
}
#[macro_export]
#[doc(hidden)]
macro_rules! __to_unit {
($anything: tt) => {
()
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __replace_ref_type {
(&$old_type: tt, $new_type: tt) => {
&$new_type
};
(&mut $old_type: tt, $new_type: tt) => {
&mut $new_type
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __if_empty_else {
(($($input: tt)*); $if_empty: block, $else: block) => {
$crate::__if_empty_else!(@($($input)*); $if_empty, $else)
};
// Empty
(@(); $if_empty: block, $else: block) => {
$if_empty
};
// Not empty
(@($($input: tt)+); $if_empty: block, $else: block) => {
$else
};
}
}
|