From f0cd82dae77016aee465e415ca666d4ea0b551ba Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 12 Sep 2021 01:12:23 +0200 Subject: [PATCH] state-res: Refactor test_utils --- crates/ruma-state-res/src/event_auth.rs | 14 +- crates/ruma-state-res/src/lib.rs | 20 +- crates/ruma-state-res/src/test_utils.rs | 262 +++++++++++------------- 3 files changed, 142 insertions(+), 154 deletions(-) diff --git a/crates/ruma-state-res/src/event_auth.rs b/crates/ruma-state-res/src/event_auth.rs index c4106c77..73ea22cd 100644 --- a/crates/ruma-state-res/src/event_auth.rs +++ b/crates/ruma-state-res/src/event_auth.rs @@ -836,7 +836,7 @@ mod tests { use crate::{ event_auth::valid_membership_change, test_utils::{alice, charlie, event_id, member_content_ban, to_pdu_event, INITIAL_EVENTS}, - StateMap, + Event, StateMap, }; use ruma_events::EventType; @@ -847,11 +847,13 @@ mod tests { let events = INITIAL_EVENTS(); let prev_event = - events.values().find(|ev| ev.event_id().as_str().contains("IMC")).map(Arc::clone); + events.values().find(|ev| ev.event_id.as_str().contains("IMC")).map(Arc::clone); let auth_events = events .values() - .map(|ev| ((ev.event_type().to_owned(), ev.state_key().to_owned()), Arc::clone(ev))) + .map(|ev| { + ((ev.event_type().to_owned(), ev.state_key().unwrap().to_owned()), Arc::clone(ev)) + }) .collect::>(); let requester = to_pdu_event( @@ -889,11 +891,13 @@ mod tests { let events = INITIAL_EVENTS(); let prev_event = - events.values().find(|ev| ev.event_id().as_str().contains("IMC")).map(Arc::clone); + events.values().find(|ev| ev.event_id.as_str().contains("IMC")).map(Arc::clone); let auth_events = events .values() - .map(|ev| ((ev.event_type().to_owned(), ev.state_key().to_owned()), Arc::clone(ev))) + .map(|ev| { + ((ev.event_type().to_owned(), ev.state_key().unwrap().to_owned()), Arc::clone(ev)) + }) .collect::>(); let requester = to_pdu_event( diff --git a/crates/ruma-state-res/src/lib.rs b/crates/ruma-state-res/src/lib.rs index 10dbf46f..6f7b174e 100644 --- a/crates/ruma-state-res/src/lib.rs +++ b/crates/ruma-state-res/src/lib.rs @@ -650,7 +650,7 @@ mod tests { alice, bob, charlie, do_check, ella, event_id, member_content_ban, member_content_join, room_id, to_init_pdu_event, to_pdu_event, zara, StateEvent, TestStore, INITIAL_EVENTS, }, - EventMap, StateMap, + Event, EventMap, StateMap, }; fn test_event_sort() { @@ -660,7 +660,9 @@ mod tests { let event_map = events .values() - .map(|ev| ((ev.event_type().to_owned(), ev.state_key().to_owned()), ev.clone())) + .map(|ev| { + ((ev.event_type().to_owned(), ev.state_key().unwrap().to_owned()), ev.clone()) + }) .collect::>(); let auth_chain = HashSet::new(); @@ -668,7 +670,7 @@ mod tests { let power_events = event_map .values() .filter(|&pdu| is_power_event(&**pdu)) - .map(|pdu| pdu.event_id().clone()) + .map(|pdu| pdu.event_id.clone()) .collect::>(); let sorted_power_events = @@ -1059,7 +1061,9 @@ mod tests { inner.get(&event_id("PA")).unwrap(), ] .iter() - .map(|ev| ((ev.event_type().to_owned(), ev.state_key().to_owned()), ev.event_id().clone())) + .map(|ev| { + ((ev.event_type().to_owned(), ev.state_key().unwrap().to_owned()), ev.event_id.clone()) + }) .collect::>(); let state_set_b = [ @@ -1072,7 +1076,9 @@ mod tests { inner.get(&event_id("PA")).unwrap(), ] .iter() - .map(|ev| ((ev.event_type().to_owned(), ev.state_key().to_owned()), ev.event_id().clone())) + .map(|ev| { + ((ev.event_type().to_owned(), ev.state_key().unwrap().to_owned()), ev.event_id.clone()) + }) .collect::>(); let ev_map: EventMap> = store.0.clone(); @@ -1174,7 +1180,7 @@ mod tests { ), ] .into_iter() - .map(|ev| (ev.event_id().clone(), ev)) + .map(|ev| (ev.event_id.clone(), ev)) .collect() } @@ -1201,7 +1207,7 @@ mod tests { ), ] .into_iter() - .map(|ev| (ev.event_id().clone(), ev)) + .map(|ev| (ev.event_id.clone(), ev)) .collect() } } diff --git a/crates/ruma-state-res/src/test_utils.rs b/crates/ruma-state-res/src/test_utils.rs index 8ecea877..ee764c52 100644 --- a/crates/ruma-state-res/src/test_utils.rs +++ b/crates/ruma-state-res/src/test_utils.rs @@ -128,13 +128,13 @@ pub fn do_check( let mut state_after = state_before.clone(); let ty = fake_event.event_type().to_owned(); - let key = fake_event.state_key().to_owned(); + let key = fake_event.state_key().unwrap().to_owned(); state_after.insert((ty, key), event_id.clone()); let auth_types = auth_types_for_event( fake_event.event_type(), fake_event.sender(), - Some(fake_event.state_key()), + fake_event.state_key(), fake_event.content(), ); @@ -153,7 +153,7 @@ pub fn do_check( e.event_id().as_str(), e.sender().clone(), e.event_type().clone(), - Some(e.state_key()), + e.state_key(), e.content(), &auth_events, &prev_events.iter().cloned().collect::>(), @@ -177,7 +177,7 @@ pub fn do_check( ) }); - let key = (ev.event_type().to_owned(), ev.state_key().to_owned()); + let key = (ev.event_type().to_owned(), ev.state_key().unwrap().to_owned()); expected_state.insert(key, node); } @@ -303,17 +303,32 @@ impl TestStore { let state_at_bob = [&create_event, &alice_mem, &join_rules, &bob_mem] .iter() - .map(|e| ((e.event_type().to_owned(), e.state_key().to_owned()), e.event_id().clone())) + .map(|e| { + ( + (e.event_type().to_owned(), e.state_key().unwrap().to_owned()), + e.event_id().clone(), + ) + }) .collect::>(); let state_at_charlie = [&create_event, &alice_mem, &join_rules, &charlie_mem] .iter() - .map(|e| ((e.event_type().to_owned(), e.state_key().to_owned()), e.event_id().clone())) + .map(|e| { + ( + (e.event_type().to_owned(), e.state_key().unwrap().to_owned()), + e.event_id().clone(), + ) + }) .collect::>(); let expected = [&create_event, &alice_mem, &join_rules, &bob_mem, &charlie_mem] .iter() - .map(|e| ((e.event_type().to_owned(), e.state_key().to_owned()), e.event_id().clone())) + .map(|e| { + ( + (e.event_type().to_owned(), e.state_key().unwrap().to_owned()), + e.event_id().clone(), + ) + }) .collect::>(); (state_at_bob, state_at_charlie, expected) @@ -536,50 +551,124 @@ pub mod event { impl Event for StateEvent { fn event_id(&self) -> &EventId { - self.event_id() + &self.event_id } fn room_id(&self) -> &RoomId { - self.room_id() + match &self.rest { + Pdu::RoomV1Pdu(ev) => &ev.room_id, + Pdu::RoomV3Pdu(ev) => &ev.room_id, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), + } } fn sender(&self) -> &UserId { - self.sender() + match &self.rest { + Pdu::RoomV1Pdu(ev) => &ev.sender, + Pdu::RoomV3Pdu(ev) => &ev.sender, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), + } } + fn event_type(&self) -> &EventType { - self.event_type() + match &self.rest { + Pdu::RoomV1Pdu(ev) => &ev.kind, + Pdu::RoomV3Pdu(ev) => &ev.kind, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), + } } fn content(&self) -> serde_json::Value { - self.content() + match &self.rest { + Pdu::RoomV1Pdu(ev) => ev.content.clone(), + Pdu::RoomV3Pdu(ev) => ev.content.clone(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), + } } + fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch { - *self.origin_server_ts() + match &self.rest { + Pdu::RoomV1Pdu(ev) => ev.origin_server_ts, + Pdu::RoomV3Pdu(ev) => ev.origin_server_ts, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), + } } fn state_key(&self) -> Option<&str> { - Some(self.state_key()) + match &self.rest { + Pdu::RoomV1Pdu(ev) => ev.state_key.as_deref(), + Pdu::RoomV3Pdu(ev) => ev.state_key.as_deref(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), + } } + fn prev_events(&self) -> Box + '_> { - self.prev_event_ids() + match &self.rest { + Pdu::RoomV1Pdu(ev) => Box::new(ev.prev_events.iter().map(|(id, _)| id)), + Pdu::RoomV3Pdu(ev) => Box::new(ev.prev_events.iter()), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), + } } + fn depth(&self) -> &UInt { - self.depth() + match &self.rest { + Pdu::RoomV1Pdu(ev) => &ev.depth, + Pdu::RoomV3Pdu(ev) => &ev.depth, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), + } } + fn auth_events(&self) -> Box + '_> { - self.auth_events() + match &self.rest { + Pdu::RoomV1Pdu(ev) => Box::new(ev.auth_events.iter().map(|(id, _)| id)), + Pdu::RoomV3Pdu(ev) => Box::new(ev.auth_events.iter()), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), + } } + fn redacts(&self) -> Option<&EventId> { - self.redacts() + match &self.rest { + Pdu::RoomV1Pdu(ev) => ev.redacts.as_ref(), + Pdu::RoomV3Pdu(ev) => ev.redacts.as_ref(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), + } } + fn hashes(&self) -> &EventHash { - self.hashes() + match &self.rest { + Pdu::RoomV1Pdu(ev) => &ev.hashes, + Pdu::RoomV3Pdu(ev) => &ev.hashes, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), + } } + fn signatures(&self) -> BTreeMap, BTreeMap> { - self.signatures() + match &self.rest { + Pdu::RoomV1Pdu(_) => BTreeMap::new(), + Pdu::RoomV3Pdu(ev) => ev.signatures.clone(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), + } } + fn unsigned(&self) -> &BTreeMap { - self.unsigned() + match &self.rest { + Pdu::RoomV1Pdu(ev) => &ev.unsigned, + Pdu::RoomV3Pdu(ev) => &ev.unsigned, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), + } } } @@ -591,125 +680,14 @@ pub mod event { pub rest: Pdu, } - impl StateEvent { - pub fn origin_server_ts(&self) -> &MilliSecondsSinceUnixEpoch { - match &self.rest { - Pdu::RoomV1Pdu(ev) => &ev.origin_server_ts, - Pdu::RoomV3Pdu(ev) => &ev.origin_server_ts, - #[cfg(not(feature = "unstable-exhaustive-types"))] - _ => unreachable!("new PDU version"), - } - } - pub fn event_id(&self) -> &EventId { - &self.event_id - } - - pub fn sender(&self) -> &UserId { - match &self.rest { - Pdu::RoomV1Pdu(ev) => &ev.sender, - Pdu::RoomV3Pdu(ev) => &ev.sender, - #[cfg(not(feature = "unstable-exhaustive-types"))] - _ => unreachable!("new PDU version"), - } - } - - pub fn redacts(&self) -> Option<&EventId> { - match &self.rest { - Pdu::RoomV1Pdu(ev) => ev.redacts.as_ref(), - Pdu::RoomV3Pdu(ev) => ev.redacts.as_ref(), - #[cfg(not(feature = "unstable-exhaustive-types"))] - _ => unreachable!("new PDU version"), - } - } - - pub fn room_id(&self) -> &RoomId { - match &self.rest { - Pdu::RoomV1Pdu(ev) => &ev.room_id, - Pdu::RoomV3Pdu(ev) => &ev.room_id, - #[cfg(not(feature = "unstable-exhaustive-types"))] - _ => unreachable!("new PDU version"), - } - } - pub fn event_type(&self) -> &EventType { - match &self.rest { - Pdu::RoomV1Pdu(ev) => &ev.kind, - Pdu::RoomV3Pdu(ev) => &ev.kind, - #[cfg(not(feature = "unstable-exhaustive-types"))] - _ => unreachable!("new PDU version"), - } - } - pub fn state_key(&self) -> &str { - match &self.rest { - Pdu::RoomV1Pdu(ev) => ev.state_key.as_ref().unwrap(), - Pdu::RoomV3Pdu(ev) => ev.state_key.as_ref().unwrap(), - #[cfg(not(feature = "unstable-exhaustive-types"))] - _ => unreachable!("new PDU version"), - } - } - - pub fn prev_event_ids(&self) -> Box + '_> { - match &self.rest { - Pdu::RoomV1Pdu(ev) => Box::new(ev.prev_events.iter().map(|(id, _)| id)), - Pdu::RoomV3Pdu(ev) => Box::new(ev.prev_events.iter()), - #[cfg(not(feature = "unstable-exhaustive-types"))] - _ => unreachable!("new PDU version"), - } - } - - pub fn auth_events(&self) -> Box + '_> { - match &self.rest { - Pdu::RoomV1Pdu(ev) => Box::new(ev.auth_events.iter().map(|(id, _)| id)), - Pdu::RoomV3Pdu(ev) => Box::new(ev.auth_events.iter()), - #[cfg(not(feature = "unstable-exhaustive-types"))] - _ => unreachable!("new PDU version"), - } - } - - pub fn content(&self) -> serde_json::Value { - match &self.rest { - Pdu::RoomV1Pdu(ev) => ev.content.clone(), - Pdu::RoomV3Pdu(ev) => ev.content.clone(), - #[cfg(not(feature = "unstable-exhaustive-types"))] - _ => unreachable!("new PDU version"), - } - } - - pub fn unsigned(&self) -> &BTreeMap { - match &self.rest { - Pdu::RoomV1Pdu(ev) => &ev.unsigned, - Pdu::RoomV3Pdu(ev) => &ev.unsigned, - #[cfg(not(feature = "unstable-exhaustive-types"))] - _ => unreachable!("new PDU version"), - } - } - - pub fn signatures( - &self, - ) -> BTreeMap, BTreeMap> { - match &self.rest { - Pdu::RoomV1Pdu(_) => BTreeMap::new(), - Pdu::RoomV3Pdu(ev) => ev.signatures.clone(), - #[cfg(not(feature = "unstable-exhaustive-types"))] - _ => unreachable!("new PDU version"), - } - } - - pub fn hashes(&self) -> &EventHash { - match &self.rest { - Pdu::RoomV1Pdu(ev) => &ev.hashes, - Pdu::RoomV3Pdu(ev) => &ev.hashes, - #[cfg(not(feature = "unstable-exhaustive-types"))] - _ => unreachable!("new PDU version"), - } - } - - pub fn depth(&self) -> &UInt { - match &self.rest { - Pdu::RoomV1Pdu(ev) => &ev.depth, - Pdu::RoomV3Pdu(ev) => &ev.depth, - #[cfg(not(feature = "unstable-exhaustive-types"))] - _ => unreachable!("new PDU version"), - } - } - } + //impl StateEvent { + // pub fn state_key(&self) -> &str { + // match &self.rest { + // Pdu::RoomV1Pdu(ev) => ev.state_key.as_ref().unwrap(), + // Pdu::RoomV3Pdu(ev) => ev.state_key.as_ref().unwrap(), + // #[cfg(not(feature = "unstable-exhaustive-types"))] + // _ => unreachable!("new PDU version"), + // } + // } + //} }