use crate::spy::{
call_range::CallRange, expectation::Expectation, matcher::Matcher, sequence::Sequence,
};
pub struct ExpectationHandle<M: Matcher + 'static> {
expectation: Option<Expectation<M>>,
sequences: Option<Vec<Sequence>>,
expectation_callback: Box<dyn Fn(Expectation<M>)>,
sequence_callback: Box<dyn Fn(Vec<Sequence>)>,
}
impl<M: Matcher> ExpectationHandle<M> {
#[doc(hidden)]
pub fn new(
matcher: M,
function_name: impl Into<String>,
expectation_callback: impl Fn(Expectation<M>) + 'static,
sequence_callback: impl Fn(Vec<Sequence>) + 'static,
) -> Self {
Self {
expectation: Some(Expectation::new(matcher, function_name)),
sequences: Some(Vec::new()),
expectation_callback: Box::new(expectation_callback),
sequence_callback: Box::new(sequence_callback),
}
}
pub fn times(mut self, call_range: impl Into<CallRange>) -> Self {
if let Some(ref mut expectation) = self.expectation {
expectation.set_call_range(call_range.into());
} else {
unreachable!(
"The expectation of ExpectationHandle is None in times. This cannot happen because the only place the expectation in taken is in drop and times cannot be called after drop."
)
}
self
}
pub fn once(self) -> Self {
self.times(1)
}
pub fn never(self) -> Self {
self.times(0)
}
pub fn describe(mut self, display_name: String) -> Self {
if let Some(ref mut expectation) = self.expectation {
expectation.set_name(display_name);
} else {
unreachable!(
"The expectation of ExpectationHandle is None in display. This cannot happen because the only place the expectation in taken is in drop and display cannot be called after drop."
)
}
self
}
pub fn in_sequence(mut self, sequence: &mut Sequence) -> Self {
if let Some(ref mut sequences) = self.sequences {
sequences.push(sequence.clone());
} else {
unreachable!(
"The sequences field in ExpectationHandle is None in in_sequence. This cannot happen because the only place the sequences are taken is in drop and in_sequence cannot be called after drop."
);
}
self
}
}
impl<M: Matcher> Drop for ExpectationHandle<M> {
fn drop(&mut self) {
let Some(expectation) = self.expectation.take() else {
unreachable!(
"The expectation of ExpectationHandle is None in drop. This cannot happen because the only place the expectation in taken is in drop and drop cannot be called twice."
)
};
let Some(mut sequences) = self.sequences.take() else {
unreachable!(
"The sequences field in ExpectationHandle is None in drop. This cannot happen because the only place the sequences are taken is in drop and drop cannot be called twice."
)
};
if sequences.is_empty() {
(self.expectation_callback)(expectation);
} else {
for sequence in sequences.iter_mut() {
sequence.append_expectation(expectation.clone());
}
(self.sequence_callback)(sequences)
}
}
}