Answers

Struct Answers 

Source
pub struct Answers<V, Ctx = ()> { /* private fields */ }
Expand description

Answers for a function call.

Answers are similar to an Iterator, but with some additional functionality:

  • Response can be based on a certain context (the second type param) provided to Self::next_for().
  • The provided contexts are recorded for each call and then can be retrieved using Self::take_calls(). This can be used to verify calls.

The intended usage of Answers is as an element of Mock state used in one or more mock methods.

§Examples

let mut answers: Answers<usize> = Answers::from_values([1, 3, 5]);
let value: usize = answers.next_for(());
assert_eq!(value, 1);
assert_eq!(answers.next_for(()), 3);
assert_eq!(answers.take_calls().len(), 2);

Context-dependent Answers:

let mut counter = 0;
let mut answers = Answers::from_fn(move |s: &String| {
    if counter == 0 && s == "test" {
        counter += 1;
        42
    } else {
        s.len()
    }
});
assert_eq!(answers.next_for("test".into()), 42);
assert_eq!(answers.next_for("??".into()), 2);
assert_eq!(answers.next_for("test".into()), 4);

let calls = answers.take_calls();
assert_eq!(calls, ["test", "??", "test"]);

To deal with more complex cases, Answers can contain functional values.

#[mock(using = "SimpleMock::mock_fn")]
fn tested_fn(s: &str, start: usize) -> &str {
    &s[start..]
}

type StrFn = fn(&str) -> &str;

#[derive(Mock)]
#[mock(mut)]
struct SimpleMock {
    str_fns: Answers<StrFn, (String, usize)>,
}

impl SimpleMock {
    fn mock_fn<'s>(this: &Mut<Self>, s: &'s str, start: usize) -> &'s str {
        let context = (s.to_owned(), start);
        let str_fn = this.borrow().str_fns.next_for(context);
        str_fn(s)
    }
}

// Setup mock with 2 functions.
let return_test: StrFn = |_| "test";
let suffix: StrFn = |s| &s[1..];
let mock = SimpleMock {
    str_fns: Answers::from_values([return_test, suffix]),
};
let guard = mock.set_as_mock();

// Perform some tests.
assert_eq!(tested_fn("first", 0), "test");
assert_eq!(tested_fn("second", 3), "econd");

// Verify mock calls.
let calls = guard.into_inner().str_fns.take_calls();
assert_eq!(calls.len(), 2);
assert_eq!(calls[0].0, "first");
assert_eq!(calls[1].1, 3);

Implementations§

Source§

impl<V, Ctx> Answers<V, Ctx>

Source

pub fn from_fn<F>(function: F) -> Self
where F: FnMut(&Ctx) -> V + Send + 'static,

Answers based on the provided function.

Source

pub fn from_values<I>(iter: I) -> Self
where I: IntoIterator<Item = V>, I::IntoIter: Send + 'static,

Answers with values from the provided iterator. Once the iterator runs out of items, panics.

Source

pub fn next_for(&mut self, context: Ctx) -> V

Selects an answer based on the specified context. The context is recorded and can then be retrieved via Self::take_calls().

Source

pub fn take_calls(&mut self) -> Vec<Ctx>

Takes contexts for recorded calls since the last call to Self::take_calls(), or after creation if called for the first time.

Source§

impl<V: Send + 'static, Ctx> Answers<V, Ctx>

Source

pub fn from_value_once(value: V) -> Self

Answers with the provided value once. Further calls will panic.

Source§

impl<V: Clone + Send + 'static, Ctx> Answers<V, Ctx>

Source

pub fn from_value(value: V) -> Self

Answers with the provided value infinite number of times.

Trait Implementations§

Source§

impl<V, Ctx: Debug> Debug for Answers<V, Ctx>

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<V, Ctx> Default for Answers<V, Ctx>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<V, Ctx> Freeze for Answers<V, Ctx>

§

impl<V, Ctx = ()> !RefUnwindSafe for Answers<V, Ctx>

§

impl<V, Ctx> Send for Answers<V, Ctx>
where Ctx: Send,

§

impl<V, Ctx = ()> !Sync for Answers<V, Ctx>

§

impl<V, Ctx> Unpin for Answers<V, Ctx>
where Ctx: Unpin,

§

impl<V, Ctx = ()> !UnwindSafe for Answers<V, Ctx>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.