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    trait Bah {
44        fn bah(&self);
45    }
46}
47
48#[automock(mod mock_ffi;)]
49extern "C" {
50    /// A foreign "C" function.
51    pub fn ffi_func();
52}
53
54#[cfg(feature = "nightly")]
55/// Mock this entire module
56#[automock]
57pub mod my_module {
58    /// A function in a mocked module
59    pub fn modfunc() {
60        unimplemented!()
61    }
62}