[][src]Macro mockall::expectation

macro_rules! expectation {
    (
        // First pattern, for methods returning 'static references
        //pub fn foo<>(&self, )
        $v:vis fn $module:ident < $( $generics:ident ),* >
        ($(&)?$(mut)?self $(,)? $( $args:ident : $argty:ty ),* )
            -> & 'static $o:ty
        {
            let ( $( $altargs:ident : &$matchty:ty ),* ) =
                ( $( $matchcall:expr ),* );
        }
    ) => { ... };
    (
        // Second pattern, for methods taking &self and returning immutable
        // references.
        $v:vis fn $module:ident
        < $( $generics:ident ),* >
        (&self $(,)? $( $args:ident : $argty:ty ),* ) -> & $o:ty
        {
            let ( $( $altargs:ident : &$matchty:ty ),* ) =
                ( $( $matchcall:expr ),* );
        }
    ) => { ... };
    (
        // Third pattern, for methods taking &mut self and returning mutable or
        // immutable references.
        $v:vis fn $module:ident
        < $( $generics:ident ),* >
        (&mut self $(,)? $( $args:ident : $argty:ty ),* ) -> & $(mut)? $o:ty
        {
            let ( $( $altargs:ident : &$matchty:ty ),* ) =
                ( $( $matchcall:expr ),* );
        }
    ) => { ... };
    (
        // Fourth pattern, for methods returning 'static values
        $v:vis fn $module:ident < $( $generics:ident ),* >
        ($(&)?$(mut)?self $(,)? $( $args:ident : $argty:ty ),* ) -> $o:ty
        {
            let ( $( $altargs:ident : &$matchty:ty ),* ) =
                ( $( $matchcall:expr ),* );
        }
    ) => { ... };
    (
        // Fifth pattern, for static methods
        $v:vis fn $module:ident < $( $generics:ident ),* >
        ($( $args:ident : $argty:ty ),* ) -> $o:ty
        {
            let ( $( $altargs:ident : &$matchty:ty ),* ) =
                ( $( $matchcall:expr ),* );
        }
    ) => { ... };
}

Generate Expectation and Expectations types for a single method.

This macro can mock most method types, whether they take self, &self, or &mut self references, 'static arguments or reference arguments, and 'static or reference return types.

Reference arguments will be given independent anonymous lifetimes. This is the usual behavior for methods that don't specify explicit lifetimes.

Methods that return references will use self's lifetime for the lifetime of the returned value.

Generic methods are allowed, as long as the generic parameters are 'static (but concrete types don't need to be). Don't repeat the generic bounds in the macro invocation.

Arguments

  • vis: Visibility qualifier relative to expectation!'s private module. Private visibility is not really useful.
  • module: Name of the module to create
  • generics: Comma-delimited sequence of generic parameters, sans bounds.
  • o: Owned version of the output type. Must be a 'static. The real output type will be a reference to this one. Returning references, both mutable and immutable, is allowed, but note that the & or &mut is technically not part of the macro argument.
  • argty: Comma-delimited sequence of arguments types for the method being mocked.
  • matchcall: Comma-delimited sequence of expressions that produce values of type matchty from values of type argty.
  • args: comma-delimited sequence of argument names for each argument. Ideally this wouldn't need to be specified, but it is due to Rust's macro hygiene rules.
  • altargs: Comma-delimited sequence of identifiers of the same length as args, but distinct.
  • matchty: comma-delimited sequence of types for each match argument. Must all be 'static.

TODO: document generated methods

Examples

Mock a method with a 'static return type like foo(&self, x: u32, y: &i16) -> u32

expectation! {
    pub fn foo<>(&self, x: u32, y: &i16) -> u32 {
        let (px: &u32, py: &i16) = (&x, y);
    }
}

Mock a generic method with a 'static return type like foo<D: Clone>(d: D, x: &u32) -> bool

expectation! {
    pub fn foo<D>(&self, d: D, x: &u32) -> bool {
        let (pd: &D, px: &u32) = (&d, x);
    }
}

Mock a method returning a reference like foo(&self, x: u32, y: &i16) -> &u32

expectation!{
    pub fn foo<>(&self, i0: u32, i1: &i16) -> &u32 {
        let (p0: &u32, p1: &i16) = (&i0, i1);
    }
}

Mock a method returning a mutable reference like foo(&mut self, x: u32, y: &i16) -> &mut u32

expectation!{
    pub fn foo<>(&mut self, i0: u32, i1: &i16) -> &mut u32 {
        let (p0: &u32, p1: &i16) = (&i0, i1);
    }
}