Documentation
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! # Moq
//!
//! This library provides the procedural macro that generates a mock struct that implements trait.
//!
//! ## User guide
//! - [Getting started](#getting-started)
//! - [Mock object name](#mock-object-name)
//! - [`async_trait`](#async_trait)
//! - [Constants](#constants)
//! - [Associated types](#associated-types)
//! - [Default function implementation](#default-function-implementation)
//! - [Impl trait](#impl-trait)
//! - [Generics](#generics)
//! - [HRTB](#hrtb)
//! - [Restrictions](#restrictions)
//!
//! ### Getting started
//!
//! To get started, add an attribute macro to the trait for which you want to generate a mock object:
//! ```
//! #[moq::automock] // <-- this macro
//! trait TraitForMocking {
//!     fn func_that_i_want_to_test(&self, arg: String) -> Result<(), std::io::Error>;
//! }
//! ```
//!
//! Next, you can create a mock object and specify the expected behaviour:
//! ```
//! # #[moq::automock]
//! # trait TraitForMocking {
//! #    fn func_that_i_want_to_test(&self, arg: String) -> Result<(), std::io::Error>;
//! # }
//! #[test]
//! fn some_test() {
//!     let mock = MockTraitForMocking::new()
//!         .expect_func_that_i_want_to_test(|arg: String| {
//!             assert_eq!(arg, "Hello, World!");
//!             Ok(())
//!         });
//!
//!     assert!(mock.func_that_i_want_to_test("Hello, World!".to_owned()).is_ok());
//! }
//! ```
//!
//! #### How its work
//! Generated mock struct checks that all specified expectation will calls in specified order.
//! If `f1()` is called while `f2()` is expected, then there will be a panic.
//! ```should_panic
//! #[moq::automock]
//! trait Trait {
//!     fn f1(&self);
//!     fn f2(&self);
//! }
//! let mock = MockTrait::new().expect_f2(||{});
//! mock.f1() // Panic
//! ```
//!
//! Checks if all expected functions are being called, if not then will be a panic.
//! ```should_panic
//! #[moq::automock]
//! trait Trait {
//!     fn f1(&self);
//!     fn f2(&self);
//! }
//! let mock = MockTrait::new()
//!     .expect_f1(||{})
//!     .expect_f2(||{});
//! mock.f1()
//! // Panic when mock will drop
//! ```
//!
//! ### Mock object name
//!
//! By default, mock object name will be `Mock%TRAIT%`, for example:
//! ```
//! // For trait:
//! #[moq::automock]
//! trait Trait { /*...*/ }
//!
//! // A mock will be generated:
//! # let _ = stringify!(
//! struct MockTrait { /*...*/ }
//! # );
//! # let _ = MockTrait::new();
//! ```
//!
//! If you want to customise it, then use `rename` attribute:
//! ```
//! #[moq::automock(rename = "FooBar")]
//! trait Trait { /*...*/ }
//!
//! // A mock will be generated:
//! # let _ = stringify!(
//! struct FooBar { /*...*/ }
//! # );
//! # let _ = FooBar::new();
//! ```
//!
//! You can also add an attribute directly to a trait:
//! ```
//! #[moq::automock]
//! #[moq(rename = "FooBar")]
//! trait Trait { /*...*/ }
//!
//! // A mock will be generated:
//! # let _ = stringify!(
//! struct FooBar { /*...*/ }
//! # );
//! # let _ = FooBar::new();
//! ```
//!
//! ### async_trait
//!
//! `automock` supports [`async_trait`](https://docs.rs/async-trait/latest/async_trait/):
//!
//! ```
//! #[moq::automock] // <- the main limitation is to specify
//!                  // the `automock` macro above the `async_trait`
//! #[async_trait::async_trait]
//! trait TraitForMocking {
//!    async fn func_that_i_want_to_test(&self, arg: String) -> Result<(), std::io::Error>;
//! }
//!
//! #[tokio::test]
//! async fn some_test() {
//!     let mock = MockTraitForMocking::new()
//!         .expect_func_that_i_want_to_test(|arg: String| async {
//!             assert_eq!(arg, "Hello, World!");
//!             Ok(())
//!         });
//!
//!     assert!(mock.func_that_i_want_to_test("Hello, World!".to_owned()).await.is_ok());
//! }
//! ```
//!
//! ### Constants
//!
//! There are 3 ways to specify the value of constants:
//! - By using the `default` attribute and specifying the 'as is' expression
//! - By using the `default_with` attribute and specifying the path to the constant function
//! - Through the default value
//!
//! ```
//! const fn const_str_default() -> &'static str { "string" }
//!
//! #[moq::automock]
//! trait Trait {
//!     #[moq(default_with = "const_str_default")]
//!     const CONST_STR: &'static str;
//!     #[moq(default = 1)]
//!     const CONST_INT: i32;
//!     const CONST_DEFAULT: usize = 42;
//! }
//!
//! assert_eq!(MockTrait::CONST_STR, "string");
//! assert_eq!(MockTrait::CONST_INT, 1);
//! assert_eq!(MockTrait::CONST_DEFAULT, 42);
//! ```
//!
//! ### Associated types
//!
//! You can specify the type of associated type by using the `default` attribute and specifying the type:
//!
//! ```
//! #[moq::automock]
//! trait Trait {
//!     #[moq(default = "String")]
//!     type AssocType: Clone;
//! }
//!
//! let _: String = <MockTrait as Trait>::AssocType::from("string");
//! ```
//!
//! ### Default function implementation
//!
//! If your trait has default implementation of function, then by default `automock` generate independent implementation:
//!
//! ```
//! #[moq::automock]
//! trait Trait {
//!     fn f1(&self);
//!
//!     fn f2(&self) {
//!         self.f1();
//!     }
//! }
//!
//! let mock = MockTrait::new()
//!     .expect_f1(|| {})
//!     .expect_f2(|| {});
//!
//! mock.f1();
//! mock.f2();
//! // Success
//! ```
//!
//! If you want to use default implementation of function, you need to mark function by `default` attribute:
//!
//! ```
//! #[moq::automock]
//! trait Trait {
//!     fn f1(&self);
//!
//!     #[moq(default)]
//!     fn f2(&self) {
//!         self.f1();
//!     }
//! }
//!
//! let mock = MockTrait::new()
//!     .expect_f1(|| {})
//!     .expect_f1(|| {}); // <- attention here
//!
//! mock.f1();
//! mock.f2();
//! // Success
//! ```
//!
//! ### Impl trait
//!
//! If you using RPITIT (Return Position Impl Trait In Trait), `automock` generates mock with boxed type:
//!
//! ```rust
//! trait DummyTrait { fn dummy(&self) -> String; }
//! impl DummyTrait for i32 { fn dummy(&self) -> String { format!("{self}") } }
//! impl DummyTrait for &'static str { fn dummy(&self) -> String { format!("{self}") } }
//! impl DummyTrait for Box<dyn DummyTrait> { fn dummy(&self) -> String { (**self).dummy() } }
//!
//! #[moq::automock]
//! trait Trait {
//!     fn f(&self) -> impl DummyTrait;
//! }
//!
//! let mock = MockTrait::new()
//!     .expect_f(|| -> Box<dyn DummyTrait> {
//!         Box::new(42i32)
//!     })
//!     .expect_f(|| -> Box<dyn DummyTrait> {
//!         Box::new("string")
//!     });
//!
//! assert_eq!(mock.f().dummy(), "42");
//! assert_eq!(mock.f().dummy(), "string");
//! ```
//!
//! If boxed dyn trait doesn't suit you, you can specify a specific type by `output` attribute:
//!
//! ```
//! trait DummyTrait { fn dummy(&self) -> String; }
//! impl DummyTrait for i32 { fn dummy(&self) -> String { format!("{self}") } }
//!
//! #[moq::automock]
//! trait Trait {
//!     #[moq(output = "i32")]
//!     fn f(&self) -> impl DummyTrait;
//! }
//!
//! let mock = MockTrait::new()
//!     .expect_f(|| -> i32 { 42i32 });
//!
//! assert_eq!(mock.f().dummy(), "42");
//! ```
//!
//! `output` attribute also supports infer syntax, may be useful for async:
//!
//! ```
//! # use std::future::Future;
//! # use std::pin::Pin;
//!
//! #[moq::automock]
//! trait Trait {
//!     #[moq(output = "Pin<_>")]
//!     fn f(&self) -> impl Future<Output = i32>;
//! }
//!
//! # async fn t() {
//! let mock = MockTrait::new()
//!     .expect_f(|| -> Pin<Box<dyn Future<Output = i32>>> {
//!         Box::pin(async { 42i32 })
//!     });
//!
//! assert_eq!(mock.f().await, 42);
//! # }
//! ```
//!
//! ### Generics
//!
//! Mocking generic methods is possible, but there are restrictions:
//! - Generic parameter must be `'static` (maybe it will be fixed in the future)
//! - Expectation function generic must be specified explicitly
//!
//! ```
//! # use std::fmt::Display;
//!
//! #[moq::automock]
//! trait Trait {
//!     fn f<T>(&self, arg: T) where T: Display + 'static;
//! }
//!
//! let mock = MockTrait::new()
//!     .expect_f(|arg: i32| assert_eq!(arg, 123)); // <- explicit arg type
//!
//! mock.f(123);
//! ```
//!
//! ### HRTB
//!
//! In rust you can't specify lifetimes for closure but you should, because lifetime
//! inference in closure is dumb. Then [`moq::lambda!(...)`](crate::lambda) macro will help.
//!
//! ```
//! #[derive(Debug, Eq, PartialEq)]
//! struct Struct<'a>(&'a str);
//!
//! #[moq::automock]
//! #[async_trait::async_trait]
//! trait Trait {
//!    async fn f<'a, 'b>(
//!         &self,
//!         arg1: Struct<'a>,
//!         arg2: Struct<'b>,
//!    ) -> Struct<'b>;
//! }
//!
//! # async fn some_test() {
//! let mock = MockTrait::new()
//!     .expect_f(moq::lambda!(
//!         async fn <'a, 'b>(arg1: Struct<'a>, arg2: Struct<'b>) -> Struct<'b> {
//!             assert_eq!(arg1, Struct("Hello"));
//!             assert_eq!(arg2, Struct("World"));
//!             arg2
//!         }
//!     ));
//!
//! assert_eq!(
//!     mock.f(
//!         Struct("Hello"),
//!         Struct("World"),
//!     ).await,
//!     Struct("World"),
//! );
//! # }
//! ```
//!
//! ### Restrictions
//!
//! - Generics must be `T: 'static`
//! - Expectation function with generics should accept specific types (`mock.expect_f::<T, _>(|arg: T| {})`)
//! - Anonymized lifetimes not allowed (`&'_ T`), make them explicit
//! - Impl trait in argument position not allowed (`arg: impl Trait`), use generics instead
//! - Static functions not supported yet

pub use async_trait::async_trait;
pub use moq_derive::automock;
pub use moq_lambda::lambda;

pub trait Func<Args, Ret> {
    fn call<'a>(&'a self, args: Args) -> Ret
    where
        Args: 'a,
        Ret: 'a;
}

macro_rules! impl_func (
    ($($param:ident)*) => {
        impl<Function, $($param,)* Ret> Func<($($param,)*), Ret> for Function
        where
            Function: Fn($($param),*) -> Ret + Sync + 'static,
            $($param: Send,)*
        {
            #[inline]
            #[allow(non_snake_case)]
            fn call<'a>(&'a self, ($($param,)*): ($($param,)*)) -> Ret
            where
                $($param: 'a,)*
                Ret: 'a,
            {
                (self)($($param,)*)
            }
        }
    }
);

impl_func!();
impl_func!(A);
impl_func!(A B);
impl_func!(A B C);
impl_func!(A B C D);
impl_func!(A B C D E);
impl_func!(A B C D E F);
impl_func!(A B C D E F G);
impl_func!(A B C D E F G H);
impl_func!(A B C D E F G H I);
impl_func!(A B C D E F G H I J);
impl_func!(A B C D E F G H I J K);
impl_func!(A B C D E F G H I J K L);

#[async_trait::async_trait]
pub trait AsyncFunc<Args, Ret> {
    async fn call<'a>(&'a self, args: Args) -> Ret
    where
        Args: 'a,
        Ret: 'a;
}

macro_rules! impl_async_func (
    ($($param:ident)*) => {
        #[async_trait::async_trait]
        impl<Function, $($param,)* Ret, Fut> AsyncFunc<($($param,)*), Ret> for Function
        where
            Function: Fn($($param),*) -> Fut + Sync + 'static,
            Fut: std::future::Future<Output = Ret> + Send + 'static,
            $($param: Send,)*
        {
            #[inline]
            #[allow(non_snake_case)]
            async fn call<'a>(&'a self, ($($param,)*): ($($param,)*)) -> Ret
            where
                $($param: 'a,)*
                Ret: 'a,
            {
                (self)($($param,)*).await
            }
        }
    }
);

impl_async_func!();
impl_async_func!(A);
impl_async_func!(A B);
impl_async_func!(A B C);
impl_async_func!(A B C D);
impl_async_func!(A B C D E);
impl_async_func!(A B C D E F);
impl_async_func!(A B C D E F G);
impl_async_func!(A B C D E F G H);
impl_async_func!(A B C D E F G H I);
impl_async_func!(A B C D E F G H I J);
impl_async_func!(A B C D E F G H I J K);
impl_async_func!(A B C D E F G H I J K L);