coverage traits

This commit is contained in:
Dandaedre 2024-12-05 06:59:00 -06:00
parent 0e3a7fb3ad
commit 96e0a20055
1 changed files with 83 additions and 4 deletions

View File

@ -42,22 +42,101 @@ impl BorrowInner<EventPending> for ListPendingEvents {
/// Doing so will allow blanket implementations great for generic dictionnary handling
pub(crate) trait BorrowMutInner<R> {
/// Borrow the inner ongoing events
fn borrow_inner(&self) -> &HashMap<String, R>;
fn borrow_mut_inner(&mut self) -> &mut HashMap<String, R>;
}
/// Blanket inner dictionnary implementation
///
/// This trait is meant to allow for inner dictionnary manipulation
pub(crate) trait EventManipulation<R> {
/// get the inner value
fn get_attr(&self, s: &str) -> Option<&R>;
/// Get a key in the inner hashmap
fn get_key(&self, s: &str) -> Option<&R>;
// /// Insert a key in the inner hashmap
// fn insert(&self, s: &str);
}
impl<T, R> EventManipulation<R> for T
where
T: BorrowInner<R>,
{
fn get_attr(&self, s: &str) -> Option<&R> {
fn get_key(&self, s: &str) -> Option<&R> {
self.borrow_inner().get(s)
}
}
#[cfg(test)]
mod tests {
use crate::votes::events::ParsedVote;
use super::*;
#[derive(Default)]
struct TestI {
inner: HashMap<String, usize>,
}
impl BorrowInner<usize> for TestI {
fn borrow_inner(&self) -> &HashMap<String, usize> {
&self.inner
}
}
impl BorrowMutInner<usize> for TestI {
fn borrow_mut_inner(&mut self) -> &mut HashMap<String, usize> {
&mut self.inner
}
}
impl BorrowMutInner<EventPending> for ListPendingEvents {
fn borrow_mut_inner(&mut self) -> &mut HashMap<String, EventPending> {
&mut self.pending_events
}
}
#[test]
fn test_borrow_impl() {
let mut i = TestI::default();
assert!(i.borrow_inner().is_empty());
i.borrow_mut_inner().insert("Bob".to_owned(), 3);
assert!(i.borrow_inner().contains_key("Bob"));
}
#[test]
fn test_blanket_implementation() {
let mut i = TestI::default();
assert!(i.get_key("blanket").is_none());
i.borrow_mut_inner().insert("blanket".to_owned(), 42);
assert!(i.get_key("blanket").is_some());
}
#[test]
fn test_manual_implementation() {
let (mut e, mut v, mut w) = (
ListClosedEvents::default(),
ListOngoingEvents::default(),
ListPendingEvents::default(),
);
let pv = ParsedVote::default();
assert!(e.get_key("blanket1").is_none());
e.add_event(
"blanket1".to_owned(),
EventClosed {
parsed_vote: pv.clone(),
},
)
.unwrap();
assert!(e.get_key("blanket1").is_some());
assert!(v.get_key("blanket2").is_none());
v.add_event(
&"blanket2".to_owned(),
EventOngoing {
parsed_vote: pv.clone(),
},
)
.unwrap();
assert!(v.get_key("blanket2").is_some());
assert!(w.get_key("blanket3").is_none());
w.borrow_mut_inner()
.insert("blanket3".to_owned(), EventPending::default());
assert!(w.get_key("blanket3").is_some());
}
}