mockall/examples.rs
1// vim: tw=80
2//! Examples of Mockall's generated code
3use mockall::{mock, automock};
4
5/// Mock of a basic trait with several kinds of method.
6///
7/// It is mocked by the [`MockFoo`](struct.MockFoo.html) struct.
8#[automock]
9pub trait Foo {
10 /// A method with a `'static` return type
11 fn foo(&self, x: i32, y: i16) -> i32;
12
13 /// A method returning a reference
14 fn bar(&self, x: i32) -> &i32;
15
16 /// A method returning a mutable reference
17 fn baz(&mut self, x: i32) -> &mut i32;
18
19 /// A method returning a `'static` reference
20 fn bean(&self) -> &'static i32;
21
22 /// A static method
23 fn bang(x: i32) -> i32;
24}
25
26/// A trait implemented by a Struct we want to mock
27pub trait Bah {
28 /// Some trait method
29 fn bah(&self);
30}
31
32mock! {
33 /// Mock of a struct
34 ///
35 /// Structs can be mocked with [`mock!`].
36 /// Their mock methods have an identical API to the methods generated by
37 /// [`#[automock]`](automock).
38 pub Boo {
39 /// A method on a struct
40 fn boo(&self);
41 }
42 /// An implementation of a trait on a mocked struct
43 impl Bah for Boo {
44 fn bah(&self);
45 }
46}
47
48/// A module full of foreign C functions.
49#[automock]
50pub mod ffi {
51 extern "C" {
52 /// A foreign "C" function.
53 pub fn ffi_func();
54 }
55}
56
57/// Mock this entire module
58#[automock]
59pub mod my_module {
60 /// A function in a mocked module
61 pub fn modfunc() {
62 unimplemented!()
63 }
64}