-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restructured error handling in mock store
- Loading branch information
Showing
10 changed files
with
200 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,70 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::InvocationError; | ||
|
||
use super::{unchecked::Unchecked, Mock}; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Store { | ||
stubs: HashMap<usize, Unchecked<'static>>, | ||
#[derive(Debug)] | ||
pub struct Store<'stub> { | ||
pub struct_name: &'static str, | ||
stubs: HashMap<usize, Unchecked<'stub>>, | ||
} | ||
|
||
impl Store { | ||
impl<'stub> Store<'stub> { | ||
pub fn new(struct_name: &'static str) -> Self { | ||
Store { | ||
struct_name, | ||
stubs: HashMap::new(), | ||
} | ||
} | ||
|
||
/// Returns a mutable reference to a [`Mock`] for a given function | ||
/// | ||
/// If the given function has not yet been mocked, an empty mock | ||
/// is created for the function. | ||
pub fn get_or_create<R, I, O>(&mut self, id: fn(R, I) -> O) -> &mut Mock<I, O> { | ||
pub fn get_or_create<R, I, O>( | ||
&mut self, | ||
id: fn(R, I) -> O, | ||
fn_name: &'static str, | ||
) -> &mut Mock<'stub, I, O> { | ||
let mock = self.stubs.entry(id as usize).or_insert_with(|| { | ||
let mock: Mock<I, O> = Mock::new(); | ||
let mock: Mock<I, O> = Mock::new(fn_name); | ||
mock.into() | ||
}); | ||
|
||
unsafe { mock.as_typed_mut() } | ||
let mock = unsafe { mock.as_typed_mut() }; | ||
assert_name(mock, fn_name); | ||
mock | ||
} | ||
|
||
/// Returns a reference to a [`Mock`] for a given function | ||
/// | ||
/// `None` is returned if the function was never mocked | ||
pub unsafe fn get<R, I, O>(&self, id: fn(R, I) -> O) -> Option<&Mock<I, O>> { | ||
self.stubs.get(&(id as usize)).map(|m| m.as_typed()) | ||
pub unsafe fn get<R, I, O>( | ||
&self, | ||
id: fn(R, I) -> O, | ||
fn_name: &'static str, | ||
) -> Result<&Mock<'stub, I, O>, InvocationError> { | ||
match self.stubs.get(&(id as usize)).map(|m| m.as_typed()) { | ||
Some(mock) => { | ||
assert_name(mock, fn_name); | ||
Ok(mock) | ||
} | ||
None => Err(InvocationError { | ||
fn_name, | ||
struct_name: self.struct_name, | ||
stub_error: super::InvocationError::NeverStubbed, | ||
}), | ||
} | ||
} | ||
} | ||
|
||
fn assert_name<I, O>(mock: &Mock<I, O>, fn_name: &'static str) { | ||
assert_eq!( | ||
mock.name(), | ||
fn_name, | ||
"faux bug: conflicting mock names: '{}' vs '{}'", | ||
mock.name(), | ||
fn_name | ||
); | ||
} |
Oops, something went wrong.