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>
impl<V, Ctx> Answers<V, Ctx>
Sourcepub fn from_values<I>(iter: I) -> Self
pub fn from_values<I>(iter: I) -> Self
Answers with values from the provided iterator. Once the iterator runs out of items, panics.
Sourcepub fn next_for(&mut self, context: Ctx) -> V
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().
Sourcepub fn take_calls(&mut self) -> Vec<Ctx>
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.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more