From 96e0a20055dda4f76e64e0ad885d309ae8071b15 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 5 Dec 2024 06:59:00 -0600 Subject: [PATCH] coverage traits --- src/votes/traits.rs | 87 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 4 deletions(-) diff --git a/src/votes/traits.rs b/src/votes/traits.rs index a2f3857..30a699b 100644 --- a/src/votes/traits.rs +++ b/src/votes/traits.rs @@ -42,22 +42,101 @@ impl BorrowInner for ListPendingEvents { /// Doing so will allow blanket implementations great for generic dictionnary handling pub(crate) trait BorrowMutInner { /// Borrow the inner ongoing events - fn borrow_inner(&self) -> &HashMap; + fn borrow_mut_inner(&mut self) -> &mut HashMap; } /// Blanket inner dictionnary implementation /// /// This trait is meant to allow for inner dictionnary manipulation pub(crate) trait EventManipulation { - /// 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 EventManipulation for T where T: BorrowInner, { - 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, + } + + impl BorrowInner for TestI { + fn borrow_inner(&self) -> &HashMap { + &self.inner + } + } + impl BorrowMutInner for TestI { + fn borrow_mut_inner(&mut self) -> &mut HashMap { + &mut self.inner + } + } + impl BorrowMutInner for ListPendingEvents { + fn borrow_mut_inner(&mut self) -> &mut HashMap { + &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()); + } +}