Use identifier macros for tests

This commit is contained in:
Jonas Platte 2020-08-04 23:58:40 +02:00
parent 066da75c7d
commit 9bc6ce9201
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
23 changed files with 239 additions and 302 deletions

View File

@ -38,13 +38,15 @@ ruma_api! {
fn request_serde() -> Result<(), Box<dyn std::error::Error + 'static>> { fn request_serde() -> Result<(), Box<dyn std::error::Error + 'static>> {
use std::convert::TryFrom; use std::convert::TryFrom;
use ruma_identifiers::user_id;
let req = Request { let req = Request {
hello: "hi".to_owned(), hello: "hi".to_owned(),
world: "test".to_owned(), world: "test".to_owned(),
q1: "query_param_special_chars %/&@!".to_owned(), q1: "query_param_special_chars %/&@!".to_owned(),
q2: 55, q2: 55,
bar: "barVal".to_owned(), bar: "barVal".to_owned(),
baz: UserId::try_from("@bazme:ruma.io")?, baz: user_id!("@bazme:ruma.io"),
}; };
let http_req = req.clone().try_into_http_request("https://homeserver.tld", None)?; let http_req = req.clone().try_into_http_request("https://homeserver.tld", None)?;

View File

@ -53,9 +53,7 @@ pub enum InvitationRecipient {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::convert::TryFrom; use ruma_identifiers::user_id;
use ruma_identifiers::UserId;
use serde_json::{from_value as from_json_value, json}; use serde_json::{from_value as from_json_value, json};
use super::InvitationRecipient; use super::InvitationRecipient;
@ -65,7 +63,7 @@ mod tests {
let incoming = let incoming =
from_json_value::<InvitationRecipient>(json!({ "user_id": "@carl:example.org" })) from_json_value::<InvitationRecipient>(json!({ "user_id": "@carl:example.org" }))
.unwrap(); .unwrap();
let user_id = UserId::try_from("@carl:example.org").unwrap(); let user_id = user_id!("@carl:example.org");
let recipient = InvitationRecipient::UserId { user_id }; let recipient = InvitationRecipient::UserId { user_id };
assert_eq!(incoming, recipient); assert_eq!(incoming, recipient);
} }

View File

@ -117,17 +117,15 @@ pub enum Direction {
mod tests { mod tests {
use super::{Direction, Request}; use super::{Direction, Request};
use std::convert::TryFrom;
use js_int::uint; use js_int::uint;
use ruma_api::Endpoint; use ruma_api::Endpoint;
use ruma_identifiers::RoomId; use ruma_identifiers::room_id;
use crate::r0::filter::{LazyLoadOptions, RoomEventFilter}; use crate::r0::filter::{LazyLoadOptions, RoomEventFilter};
#[test] #[test]
fn test_serialize_some_room_event_filter() { fn test_serialize_some_room_event_filter() {
let room_id = RoomId::try_from("!roomid:example.org").unwrap(); let room_id = room_id!("!roomid:example.org");
let filter = RoomEventFilter { let filter = RoomEventFilter {
lazy_load_options: LazyLoadOptions::Enabled { include_redundant_members: true }, lazy_load_options: LazyLoadOptions::Enabled { include_redundant_members: true },
rooms: Some(vec![room_id.clone()]), rooms: Some(vec![room_id.clone()]),
@ -154,7 +152,7 @@ mod tests {
#[test] #[test]
fn test_serialize_none_room_event_filter() { fn test_serialize_none_room_event_filter() {
let room_id = RoomId::try_from("!roomid:example.org").unwrap(); let room_id = room_id!("!roomid:example.org");
let req = Request { let req = Request {
room_id, room_id,
from: "token".into(), from: "token".into(),
@ -171,7 +169,7 @@ mod tests {
#[test] #[test]
fn test_serialize_default_room_event_filter() { fn test_serialize_default_room_event_filter() {
let room_id = RoomId::try_from("!roomid:example.org").unwrap(); let room_id = room_id!("!roomid:example.org");
let req = Request { let req = Request {
room_id, room_id,
from: "token".into(), from: "token".into(),

View File

@ -20,11 +20,9 @@ pub struct IgnoredUserListEventContent {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::convert::TryFrom;
use matches::assert_matches; use matches::assert_matches;
use ruma_common::Raw; use ruma_common::Raw;
use ruma_identifiers::UserId; use ruma_identifiers::user_id;
use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::IgnoredUserListEventContent; use super::IgnoredUserListEventContent;
@ -34,7 +32,7 @@ mod tests {
fn serialization() { fn serialization() {
let ignored_user_list_event = BasicEvent { let ignored_user_list_event = BasicEvent {
content: IgnoredUserListEventContent { content: IgnoredUserListEventContent {
ignored_users: vec![UserId::try_from("@carl:example.com").unwrap()], ignored_users: vec![user_id!("@carl:example.com")],
}, },
}; };
@ -68,7 +66,7 @@ mod tests {
.unwrap(), .unwrap(),
BasicEvent { BasicEvent {
content: AnyBasicEventContent::IgnoredUserList(IgnoredUserListEventContent { ignored_users, }), content: AnyBasicEventContent::IgnoredUserList(IgnoredUserListEventContent { ignored_users, }),
} if ignored_users == vec![UserId::try_from("@carl:example.com").unwrap()] } if ignored_users == vec![user_id!("@carl:example.com")]
); );
} }
} }

View File

@ -51,12 +51,10 @@ pub struct PresenceEventContent {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::convert::TryFrom;
use js_int::uint; use js_int::uint;
use matches::assert_matches; use matches::assert_matches;
use ruma_common::presence::PresenceState; use ruma_common::presence::PresenceState;
use ruma_identifiers::UserId; use ruma_identifiers::user_id;
use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::{PresenceEvent, PresenceEventContent}; use super::{PresenceEvent, PresenceEventContent};
@ -72,7 +70,7 @@ mod tests {
presence: PresenceState::Online, presence: PresenceState::Online,
status_msg: Some("Making cupcakes".into()), status_msg: Some("Making cupcakes".into()),
}, },
sender: UserId::try_from("@example:localhost").unwrap(), sender: user_id!("@example:localhost"),
}; };
let json = json!({ let json = json!({

View File

@ -39,13 +39,10 @@ impl CanonicalAliasEventContent {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{ use std::time::{Duration, UNIX_EPOCH};
convert::TryFrom,
time::{Duration, UNIX_EPOCH},
};
use ruma_common::Raw; use ruma_common::Raw;
use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId}; use ruma_identifiers::{event_id, room_alias_id, room_id, user_id};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::CanonicalAliasEventContent; use super::CanonicalAliasEventContent;
@ -55,14 +52,14 @@ mod tests {
fn serialization_with_optional_fields_as_none() { fn serialization_with_optional_fields_as_none() {
let canonical_alias_event = StateEvent { let canonical_alias_event = StateEvent {
content: CanonicalAliasEventContent { content: CanonicalAliasEventContent {
alias: Some(RoomAliasId::try_from("#somewhere:localhost").unwrap()), alias: Some(room_alias_id!("#somewhere:localhost")),
alt_aliases: Vec::new(), alt_aliases: Vec::new(),
}, },
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
prev_content: None, prev_content: None,
room_id: RoomId::try_from("!dummy:example.com").unwrap(), room_id: room_id!("!dummy:example.com"),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
state_key: "".into(), state_key: "".into(),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
}; };
@ -156,7 +153,7 @@ mod tests {
#[test] #[test]
fn nonempty_field_as_some() { fn nonempty_field_as_some() {
let alias = Some(RoomAliasId::try_from("#somewhere:localhost").unwrap()); let alias = Some(room_alias_id!("#somewhere:localhost"));
let json_data = json!({ let json_data = json!({
"content": { "content": {
"alias": "#somewhere:localhost" "alias": "#somewhere:localhost"

View File

@ -68,11 +68,9 @@ fn default_room_version_id() -> RoomVersionId {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::convert::TryFrom;
use matches::assert_matches; use matches::assert_matches;
use ruma_common::Raw; use ruma_common::Raw;
use ruma_identifiers::{RoomVersionId, UserId}; use ruma_identifiers::{user_id, RoomVersionId};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::CreateEventContent; use super::CreateEventContent;
@ -80,7 +78,7 @@ mod tests {
#[test] #[test]
fn serialization() { fn serialization() {
let content = CreateEventContent { let content = CreateEventContent {
creator: UserId::try_from("@carl:example.com").unwrap(), creator: user_id!("@carl:example.com"),
federate: false, federate: false,
room_version: RoomVersionId::Version4, room_version: RoomVersionId::Version4,
predecessor: None, predecessor: None,

View File

@ -406,14 +406,11 @@ impl TextMessageEventContent {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{ use std::time::{Duration, UNIX_EPOCH};
convert::TryFrom,
time::{Duration, UNIX_EPOCH},
};
use matches::assert_matches; use matches::assert_matches;
use ruma_common::Raw; use ruma_common::Raw;
use ruma_identifiers::{EventId, RoomId, UserId}; use ruma_identifiers::{event_id, room_id, user_id};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::{AudioMessageEventContent, FormattedBody, MessageEventContent, MessageFormat}; use super::{AudioMessageEventContent, FormattedBody, MessageEventContent, MessageFormat};
@ -431,10 +428,10 @@ mod tests {
url: Some("http://example.com/audio.mp3".into()), url: Some("http://example.com/audio.mp3".into()),
file: None, file: None,
}), }),
event_id: EventId::try_from("$143273582443PhrSn:example.org").unwrap(), event_id: event_id!("$143273582443PhrSn:example.org"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(10_000), origin_server_ts: UNIX_EPOCH + Duration::from_millis(10_000),
room_id: RoomId::try_from("!testroomid:example.org").unwrap(), room_id: room_id!("!testroomid:example.org"),
sender: UserId::try_from("@user:example.org").unwrap(), sender: user_id!("@user:example.org"),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
}; };
@ -518,7 +515,7 @@ mod tests {
formatted: None, formatted: None,
relates_to: Some(RelatesTo { relates_to: Some(RelatesTo {
in_reply_to: Some(InReplyTo { in_reply_to: Some(InReplyTo {
event_id: EventId::try_from("$15827405538098VGFWH:example.com").unwrap(), event_id: event_id!("$15827405538098VGFWH:example.com"),
}), }),
}), }),
}); });

View File

@ -57,7 +57,6 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{ use std::{
convert::TryFrom,
iter::FromIterator, iter::FromIterator,
time::{Duration, UNIX_EPOCH}, time::{Duration, UNIX_EPOCH},
}; };
@ -65,7 +64,7 @@ mod tests {
use js_int::Int; use js_int::Int;
use matches::assert_matches; use matches::assert_matches;
use ruma_common::Raw; use ruma_common::Raw;
use ruma_identifiers::{EventId, RoomId, UserId}; use ruma_identifiers::{event_id, room_id, user_id};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use crate::{StateEvent, Unsigned}; use crate::{StateEvent, Unsigned};
@ -76,11 +75,11 @@ mod tests {
fn serialization_with_optional_fields_as_none() { fn serialization_with_optional_fields_as_none() {
let name_event = StateEvent { let name_event = StateEvent {
content: NameEventContent { name: Some("The room name".into()) }, content: NameEventContent { name: Some("The room name".into()) },
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
prev_content: None, prev_content: None,
room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(), room_id: room_id!("!n8f893n9:example.com"),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
state_key: "".into(), state_key: "".into(),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
}; };
@ -105,11 +104,11 @@ mod tests {
fn serialization_with_all_fields() { fn serialization_with_all_fields() {
let name_event = StateEvent { let name_event = StateEvent {
content: NameEventContent { name: Some("The room name".into()) }, content: NameEventContent { name: Some("The room name".into()) },
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
prev_content: Some(NameEventContent { name: Some("The old name".into()) }), prev_content: Some(NameEventContent { name: Some("The old name".into()) }),
room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(), room_id: room_id!("!n8f893n9:example.com"),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
state_key: "".into(), state_key: "".into(),
unsigned: Unsigned { age: Some(Int::from(100)), ..Unsigned::default() }, unsigned: Unsigned { age: Some(Int::from(100)), ..Unsigned::default() },
}; };

View File

@ -119,13 +119,12 @@ fn is_default_power_level(l: &Int) -> bool {
mod tests { mod tests {
use std::{ use std::{
collections::BTreeMap, collections::BTreeMap,
convert::TryFrom,
time::{Duration, UNIX_EPOCH}, time::{Duration, UNIX_EPOCH},
}; };
use js_int::Int; use js_int::Int;
use maplit::btreemap; use maplit::btreemap;
use ruma_identifiers::{EventId, RoomId, UserId}; use ruma_identifiers::{event_id, room_id, user_id};
use serde_json::{json, to_value as to_json_value}; use serde_json::{json, to_value as to_json_value};
use super::{default_power_level, NotificationPowerLevels, PowerLevelsEventContent}; use super::{default_power_level, NotificationPowerLevels, PowerLevelsEventContent};
@ -148,12 +147,12 @@ mod tests {
users_default: Int::from(0), users_default: Int::from(0),
notifications: NotificationPowerLevels::default(), notifications: NotificationPowerLevels::default(),
}, },
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
prev_content: None, prev_content: None,
room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(), room_id: room_id!("!n8f893n9:example.com"),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
state_key: "".into(), state_key: "".into(),
}; };
@ -173,7 +172,7 @@ mod tests {
#[test] #[test]
fn serialization_with_all_fields() { fn serialization_with_all_fields() {
let user = UserId::try_from("@carl:example.com").unwrap(); let user = user_id!("@carl:example.com");
let power_levels_event = StateEvent { let power_levels_event = StateEvent {
content: PowerLevelsEventContent { content: PowerLevelsEventContent {
ban: Int::from(23), ban: Int::from(23),
@ -191,7 +190,7 @@ mod tests {
users_default: Int::from(23), users_default: Int::from(23),
notifications: NotificationPowerLevels { room: Int::from(23) }, notifications: NotificationPowerLevels { room: Int::from(23) },
}, },
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
prev_content: Some(PowerLevelsEventContent { prev_content: Some(PowerLevelsEventContent {
// Make just one field different so we at least know they're two different objects. // Make just one field different so we at least know they're two different objects.
@ -210,7 +209,7 @@ mod tests {
users_default: Int::from(42), users_default: Int::from(42),
notifications: NotificationPowerLevels { room: Int::from(42) }, notifications: NotificationPowerLevels { room: Int::from(42) },
}), }),
room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(), room_id: room_id!("!n8f893n9:example.com"),
unsigned: Unsigned { age: Some(Int::from(100)), ..Unsigned::default() }, unsigned: Unsigned { age: Some(Int::from(100)), ..Unsigned::default() },
sender: user, sender: user,
state_key: "".into(), state_key: "".into(),

View File

@ -33,9 +33,7 @@ pub struct RoomKeyEventContent {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::convert::TryFrom; use ruma_identifiers::room_id;
use ruma_identifiers::RoomId;
use serde_json::{json, to_value as to_json_value}; use serde_json::{json, to_value as to_json_value};
use super::RoomKeyEventContent; use super::RoomKeyEventContent;
@ -46,7 +44,7 @@ mod tests {
let ev = BasicEvent { let ev = BasicEvent {
content: RoomKeyEventContent { content: RoomKeyEventContent {
algorithm: Algorithm::MegolmV1AesSha2, algorithm: Algorithm::MegolmV1AesSha2,
room_id: RoomId::try_from("!testroomid:example.org").unwrap(), room_id: room_id!("!testroomid:example.org"),
session_id: "SessId".into(), session_id: "SessId".into(),
session_key: "SessKey".into(), session_key: "SessKey".into(),
}, },

View File

@ -1,7 +1,4 @@
use std::{ use std::time::{Duration, UNIX_EPOCH};
convert::TryFrom,
time::{Duration, UNIX_EPOCH},
};
use matches::assert_matches; use matches::assert_matches;
use ruma_common::Raw; use ruma_common::Raw;
@ -10,7 +7,7 @@ use ruma_events::{
AnySyncMessageEvent, AnySyncRoomEvent, MessageEvent, StateEvent, SyncMessageEvent, AnySyncMessageEvent, AnySyncRoomEvent, MessageEvent, StateEvent, SyncMessageEvent,
SyncStateEvent, Unsigned, SyncStateEvent, Unsigned,
}; };
use ruma_identifiers::{EventId, RoomId, UserId}; use ruma_identifiers::{event_id, room_id, user_id};
use serde_json::{ use serde_json::{
from_value as from_json_value, json, to_value as to_json_value, Value as JsonValue, from_value as from_json_value, json, to_value as to_json_value, Value as JsonValue,
}; };
@ -54,10 +51,10 @@ fn serialize_custom_message_event() {
}), }),
event_type: "m.room.message".into(), event_type: "m.room.message".into(),
}, },
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(10), origin_server_ts: UNIX_EPOCH + Duration::from_millis(10),
room_id: RoomId::try_from("!room:room.com").unwrap(), room_id: room_id!("!room:room.com"),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
}); });
@ -94,11 +91,11 @@ fn serialize_custom_state_event() {
}), }),
event_type: "m.made.up".into(), event_type: "m.made.up".into(),
}, },
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(10), origin_server_ts: UNIX_EPOCH + Duration::from_millis(10),
prev_content: None, prev_content: None,
room_id: RoomId::try_from("!roomid:room.com").unwrap(), room_id: room_id!("!roomid:room.com"),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
state_key: "".into(), state_key: "".into(),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
}); });
@ -148,10 +145,10 @@ fn deserialize_custom_state_event() {
state_key, state_key,
unsigned, unsigned,
}) if json == expected_content && event_type == "m.reaction" }) if json == expected_content && event_type == "m.reaction"
&& event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() && event_id == event_id!("$h29iv0s8:example.com")
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(10) && origin_server_ts == UNIX_EPOCH + Duration::from_millis(10)
&& sender == UserId::try_from("@carl:example.com").unwrap() && sender == user_id!("@carl:example.com")
&& room_id == RoomId::try_from("!room:room.com").unwrap() && room_id == room_id!("!room:room.com")
&& state_key == "" && state_key == ""
&& !unsigned.is_empty() && !unsigned.is_empty()
); );
@ -183,9 +180,9 @@ fn deserialize_custom_state_sync_event() {
state_key, state_key,
unsigned, unsigned,
} if json == expected_content && event_type == "m.reaction" } if json == expected_content && event_type == "m.reaction"
&& event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() && event_id == event_id!("$h29iv0s8:example.com")
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(10) && origin_server_ts == UNIX_EPOCH + Duration::from_millis(10)
&& sender == UserId::try_from("@carl:example.com").unwrap() && sender == user_id!("@carl:example.com")
&& state_key == "" && state_key == ""
&& !unsigned.is_empty() && !unsigned.is_empty()
); );
@ -231,9 +228,9 @@ fn deserialize_custom_message_sync_event() {
sender, sender,
unsigned, unsigned,
})) if json == expected_content && event_type == "m.reaction" })) if json == expected_content && event_type == "m.reaction"
&& event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() && event_id == event_id!("$h29iv0s8:example.com")
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(10) && origin_server_ts == UNIX_EPOCH + Duration::from_millis(10)
&& sender == UserId::try_from("@carl:example.com").unwrap() && sender == user_id!("@carl:example.com")
&& !unsigned.is_empty() && !unsigned.is_empty()
); );
} }

View File

@ -1,7 +1,5 @@
use std::convert::TryFrom;
use matches::assert_matches; use matches::assert_matches;
use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId}; use ruma_identifiers::{event_id, room_alias_id, room_id, user_id};
use serde_json::{from_value as from_json_value, json, Value as JsonValue}; use serde_json::{from_value as from_json_value, json, Value as JsonValue};
use ruma_events::{ use ruma_events::{
@ -166,7 +164,7 @@ fn aliases_event_sync_deserialization() {
.. ..
}) })
)) ))
if aliases == vec![ RoomAliasId::try_from("#somewhere:localhost").unwrap() ] if aliases == vec![ room_alias_id!("#somewhere:localhost") ]
); );
} }
@ -205,7 +203,7 @@ fn alias_room_event_deserialization() {
.. ..
}) })
)) ))
if aliases == vec![ RoomAliasId::try_from("#somewhere:localhost").unwrap() ] if aliases == vec![ room_alias_id!("#somewhere:localhost") ]
); );
} }
@ -244,7 +242,7 @@ fn alias_event_deserialization() {
.. ..
}) })
)) ))
if aliases == vec![ RoomAliasId::try_from("#somewhere:localhost").unwrap() ] if aliases == vec![ room_alias_id!("#somewhere:localhost") ]
); );
} }
@ -256,15 +254,15 @@ fn alias_event_field_access() {
from_json_value::<AnyEvent>(json_data.clone()), from_json_value::<AnyEvent>(json_data.clone()),
Ok(AnyEvent::State(state_event)) Ok(AnyEvent::State(state_event))
if state_event.state_key() == "" if state_event.state_key() == ""
&& state_event.room_id() == &RoomId::try_from("!room:room.com").unwrap() && state_event.room_id() == &room_id!("!room:room.com")
&& state_event.event_id() == &EventId::try_from("$152037280074GZeOm:localhost").unwrap() && state_event.event_id() == &event_id!("$152037280074GZeOm:localhost")
&& state_event.sender() == &UserId::try_from("@example:localhost").unwrap() && state_event.sender() == &user_id!("@example:localhost")
); );
let deser = from_json_value::<AnyStateEvent>(json_data).unwrap(); let deser = from_json_value::<AnyStateEvent>(json_data).unwrap();
if let AnyStateEventContent::RoomAliases(AliasesEventContent { aliases, .. }) = deser.content() if let AnyStateEventContent::RoomAliases(AliasesEventContent { aliases, .. }) = deser.content()
{ {
assert_eq!(aliases, vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()]) assert_eq!(aliases, vec![room_alias_id!("#somewhere:localhost")])
} else { } else {
panic!("the `Any*Event` enum's accessor methods may have been altered") panic!("the `Any*Event` enum's accessor methods may have been altered")
} }

View File

@ -1,12 +1,9 @@
use std::{ use std::time::{Duration, UNIX_EPOCH};
convert::TryFrom,
time::{Duration, UNIX_EPOCH},
};
use maplit::btreemap; use maplit::btreemap;
use matches::assert_matches; use matches::assert_matches;
use ruma_common::Raw; use ruma_common::Raw;
use ruma_identifiers::{EventId, RoomId, UserId}; use ruma_identifiers::{event_id, room_id, user_id};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use ruma_events::{ use ruma_events::{
@ -19,9 +16,9 @@ use ruma_events::{
fn ephemeral_serialize_typing() { fn ephemeral_serialize_typing() {
let aliases_event = EphemeralRoomEvent { let aliases_event = EphemeralRoomEvent {
content: AnyEphemeralRoomEventContent::Typing(TypingEventContent { content: AnyEphemeralRoomEventContent::Typing(TypingEventContent {
user_ids: vec![UserId::try_from("@carl:example.com").unwrap()], user_ids: vec![user_id!("@carl:example.com")],
}), }),
room_id: RoomId::try_from("!roomid:room.com").unwrap(), room_id: room_id!("!roomid:room.com"),
}; };
let actual = to_json_value(&aliases_event).unwrap(); let actual = to_json_value(&aliases_event).unwrap();
@ -56,15 +53,15 @@ fn deserialize_ephemeral_typing() {
user_ids, user_ids,
}), }),
room_id, room_id,
} if user_ids[0] == UserId::try_from("@carl:example.com").unwrap() } if user_ids[0] == user_id!("@carl:example.com")
&& room_id == RoomId::try_from("!roomid:room.com").unwrap() && room_id == room_id!("!roomid:room.com")
); );
} }
#[test] #[test]
fn ephemeral_serialize_receipt() { fn ephemeral_serialize_receipt() {
let event_id = EventId::try_from("$h29iv0s8:example.com").unwrap(); let event_id = event_id!("$h29iv0s8:example.com");
let user_id = UserId::try_from("@carl:example.com").unwrap(); let user_id = user_id!("@carl:example.com");
let aliases_event = EphemeralRoomEvent { let aliases_event = EphemeralRoomEvent {
content: AnyEphemeralRoomEventContent::Receipt(ReceiptEventContent(btreemap! { content: AnyEphemeralRoomEventContent::Receipt(ReceiptEventContent(btreemap! {
@ -74,7 +71,7 @@ fn ephemeral_serialize_receipt() {
}), }),
}, },
})), })),
room_id: RoomId::try_from("!roomid:room.com").unwrap(), room_id: room_id!("!roomid:room.com"),
}; };
let actual = to_json_value(&aliases_event).unwrap(); let actual = to_json_value(&aliases_event).unwrap();
@ -95,8 +92,8 @@ fn ephemeral_serialize_receipt() {
#[test] #[test]
fn deserialize_ephemeral_receipt() { fn deserialize_ephemeral_receipt() {
let event_id = EventId::try_from("$h29iv0s8:example.com").unwrap(); let event_id = event_id!("$h29iv0s8:example.com");
let user_id = UserId::try_from("@carl:example.com").unwrap(); let user_id = user_id!("@carl:example.com");
let json_data = json!({ let json_data = json!({
"content": { "content": {
@ -119,7 +116,7 @@ fn deserialize_ephemeral_receipt() {
content: AnyEphemeralRoomEventContent::Receipt(ReceiptEventContent(receipts)), content: AnyEphemeralRoomEventContent::Receipt(ReceiptEventContent(receipts)),
room_id, room_id,
} if !receipts.is_empty() && receipts.contains_key(&event_id) } if !receipts.is_empty() && receipts.contains_key(&event_id)
&& room_id == RoomId::try_from("!roomid:room.com").unwrap() && room_id == room_id!("!roomid:room.com")
&& receipts && receipts
.get(&event_id) .get(&event_id)
.map(|r| r.read.as_ref().unwrap().get(&user_id).unwrap().clone()) .map(|r| r.read.as_ref().unwrap().get(&user_id).unwrap().clone())

View File

@ -1,11 +1,8 @@
use std::{ use std::time::{Duration, UNIX_EPOCH};
convert::TryFrom,
time::{Duration, UNIX_EPOCH},
};
use js_int::UInt; use js_int::UInt;
use matches::assert_matches; use matches::assert_matches;
use ruma_identifiers::{EventId, RoomId, UserId}; use ruma_identifiers::{event_id, room_id, user_id};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use ruma_events::{ use ruma_events::{
@ -59,10 +56,10 @@ fn deserialize_message_event() {
sender, sender,
unsigned, unsigned,
}) if sdp == "Hello" && call_id == "foofoo" && version == UInt::new(1).unwrap() }) if sdp == "Hello" && call_id == "foofoo" && version == UInt::new(1).unwrap()
&& event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() && event_id == event_id!("$h29iv0s8:example.com")
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
&& room_id == RoomId::try_from("!roomid:room.com").unwrap() && room_id == room_id!("!roomid:room.com")
&& sender == UserId::try_from("@carl:example.com").unwrap() && sender == user_id!("@carl:example.com")
&& unsigned.is_empty() && unsigned.is_empty()
); );
} }
@ -88,10 +85,10 @@ fn serialize_message_event() {
}, },
url: "http://www.matrix.org".into(), url: "http://www.matrix.org".into(),
}, },
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
room_id: RoomId::try_from("!roomid:room.com").unwrap(), room_id: room_id!("!roomid:room.com"),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
}); });

View File

@ -1,7 +1,4 @@
use std::{ use std::time::{Duration, UNIX_EPOCH};
convert::TryFrom,
time::{Duration, UNIX_EPOCH},
};
use js_int::{uint, UInt}; use js_int::{uint, UInt};
use matches::assert_matches; use matches::assert_matches;
@ -12,7 +9,7 @@ use ruma_events::{
sticker::StickerEventContent, sticker::StickerEventContent,
AnyMessageEventContent, AnySyncMessageEvent, MessageEvent, RawExt, Unsigned, AnyMessageEventContent, AnySyncMessageEvent, MessageEvent, RawExt, Unsigned,
}; };
use ruma_identifiers::{EventId, RoomId, UserId}; use ruma_identifiers::{event_id, room_id, user_id};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
#[test] #[test]
@ -36,10 +33,10 @@ fn message_serialize_sticker() {
}, },
url: "http://www.matrix.org".into(), url: "http://www.matrix.org".into(),
}), }),
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
room_id: RoomId::try_from("!roomid:room.com").unwrap(), room_id: room_id!("!roomid:room.com"),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
}; };
@ -137,10 +134,10 @@ fn deserialize_message_call_answer() {
sender, sender,
unsigned, unsigned,
} if sdp == "Hello" && call_id == "foofoo" && version == UInt::new(1).unwrap() } if sdp == "Hello" && call_id == "foofoo" && version == UInt::new(1).unwrap()
&& event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() && event_id == event_id!("$h29iv0s8:example.com")
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
&& room_id == RoomId::try_from("!roomid:room.com").unwrap() && room_id == room_id!("!roomid:room.com")
&& sender == UserId::try_from("@carl:example.com").unwrap() && sender == user_id!("@carl:example.com")
&& unsigned.is_empty() && unsigned.is_empty()
); );
} }
@ -196,11 +193,11 @@ fn deserialize_message_sticker() {
room_id, room_id,
sender, sender,
unsigned unsigned
} if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() } if event_id == event_id!("$h29iv0s8:example.com")
&& body == "Hello" && body == "Hello"
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
&& room_id == RoomId::try_from("!roomid:room.com").unwrap() && room_id == room_id!("!roomid:room.com")
&& sender == UserId::try_from("@carl:example.com").unwrap() && sender == user_id!("@carl:example.com")
&& height == UInt::new(423) && height == UInt::new(423)
&& width == UInt::new(1011) && width == UInt::new(1011)
&& mimetype == "image/png" && mimetype == "image/png"
@ -225,7 +222,7 @@ fn deserialize_message_sticker() {
#[test] #[test]
fn deserialize_message_then_convert_to_full() { fn deserialize_message_then_convert_to_full() {
let rid = RoomId::try_from("!roomid:room.com").unwrap(); let rid = room_id!("!roomid:room.com");
let json_data = json!({ let json_data = json!({
"content": { "content": {
"answer": { "answer": {

View File

@ -1,6 +1,6 @@
use std::{ use std::{
collections::BTreeMap, collections::BTreeMap,
convert::{TryFrom, TryInto}, convert::TryInto,
time::{Duration, SystemTime}, time::{Duration, SystemTime},
}; };
@ -9,7 +9,7 @@ use ruma_events::{
pdu::{EventHash, Pdu, PduStub, RoomV1Pdu, RoomV1PduStub, RoomV3Pdu, RoomV3PduStub}, pdu::{EventHash, Pdu, PduStub, RoomV1Pdu, RoomV1PduStub, RoomV3Pdu, RoomV3PduStub},
EventType, EventType,
}; };
use ruma_identifiers::{EventId, RoomId, UserId}; use ruma_identifiers::{event_id, room_id, user_id};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
#[test] #[test]
@ -24,22 +24,22 @@ fn serialize_stub_as_v1() {
unsigned.insert("somekey".into(), json!({"a": 456})); unsigned.insert("somekey".into(), json!({"a": 456}));
let v1_stub = RoomV1PduStub { let v1_stub = RoomV1PduStub {
sender: UserId::try_from("@sender:example.com").unwrap(), sender: user_id!("@sender:example.com"),
origin: "matrix.org".into(), origin: "matrix.org".into(),
origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658), origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658),
kind: EventType::RoomPowerLevels, kind: EventType::RoomPowerLevels,
content: json!({"testing": 123}), content: json!({"testing": 123}),
state_key: Some("state".into()), state_key: Some("state".into()),
prev_events: vec![( prev_events: vec![(
EventId::try_from("$previousevent:matrix.org").unwrap(), event_id!("$previousevent:matrix.org"),
EventHash { sha256: "123567".into() }, EventHash { sha256: "123567".into() },
)], )],
depth: 2_u32.into(), depth: 2_u32.into(),
auth_events: vec![( auth_events: vec![(
EventId::try_from("$someauthevent:matrix.org").unwrap(), event_id!("$someauthevent:matrix.org"),
EventHash { sha256: "21389CFEDABC".into() }, EventHash { sha256: "21389CFEDABC".into() },
)], )],
redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()), redacts: Some(event_id!("$9654:matrix.org")),
unsigned, unsigned,
hashes: EventHash { sha256: "1233543bABACDEF".into() }, hashes: EventHash { sha256: "1233543bABACDEF".into() },
signatures, signatures,
@ -85,16 +85,16 @@ fn serialize_stub_as_v3() {
unsigned.insert("somekey".into(), json!({"a": 456})); unsigned.insert("somekey".into(), json!({"a": 456}));
let v3_stub = RoomV3PduStub { let v3_stub = RoomV3PduStub {
sender: UserId::try_from("@sender:example.com").unwrap(), sender: user_id!("@sender:example.com"),
origin: "matrix.org".into(), origin: "matrix.org".into(),
origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658), origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658),
kind: EventType::RoomPowerLevels, kind: EventType::RoomPowerLevels,
content: json!({"testing": 123}), content: json!({"testing": 123}),
state_key: Some("state".into()), state_key: Some("state".into()),
prev_events: vec![EventId::try_from("$previousevent:matrix.org").unwrap()], prev_events: vec![event_id!("$previousevent:matrix.org")],
depth: 2_u32.into(), depth: 2_u32.into(),
auth_events: vec![EventId::try_from("$someauthevent:matrix.org").unwrap()], auth_events: vec![event_id!("$someauthevent:matrix.org")],
redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()), redacts: Some(event_id!("$9654:matrix.org")),
unsigned, unsigned,
hashes: EventHash { sha256: "1233543bABACDEF".into() }, hashes: EventHash { sha256: "1233543bABACDEF".into() },
signatures, signatures,
@ -171,10 +171,7 @@ fn deserialize_stub_as_v1() {
match parsed { match parsed {
PduStub::RoomV1PduStub(v1_stub) => { PduStub::RoomV1PduStub(v1_stub) => {
assert_eq!( assert_eq!(v1_stub.auth_events.first().unwrap().0, event_id!("$abc123:matrix.org"));
v1_stub.auth_events.first().unwrap().0,
EventId::try_from("$abc123:matrix.org").unwrap()
);
assert_eq!( assert_eq!(
v1_stub.auth_events.first().unwrap().1.sha256, v1_stub.auth_events.first().unwrap().1.sha256,
"Base64EncodedSha256HashesShouldBe43BytesLong" "Base64EncodedSha256HashesShouldBe43BytesLong"
@ -222,10 +219,7 @@ fn deserialize_stub_as_v3() {
match parsed { match parsed {
PduStub::RoomV1PduStub(_) => panic!("Matched V1 stub"), PduStub::RoomV1PduStub(_) => panic!("Matched V1 stub"),
PduStub::RoomV3PduStub(v3_stub) => { PduStub::RoomV3PduStub(v3_stub) => {
assert_eq!( assert_eq!(v3_stub.auth_events.first().unwrap(), &event_id!("$abc123:matrix.org"));
v3_stub.auth_events.first().unwrap(),
&EventId::try_from("$abc123:matrix.org").unwrap()
);
} }
} }
} }
@ -242,24 +236,24 @@ fn serialize_pdu_as_v1() {
unsigned.insert("somekey".into(), json!({"a": 456})); unsigned.insert("somekey".into(), json!({"a": 456}));
let v1_pdu = RoomV1Pdu { let v1_pdu = RoomV1Pdu {
room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(), room_id: room_id!("!n8f893n9:example.com"),
event_id: EventId::try_from("$somejoinevent:matrix.org").unwrap(), event_id: event_id!("$somejoinevent:matrix.org"),
sender: UserId::try_from("@sender:example.com").unwrap(), sender: user_id!("@sender:example.com"),
origin: "matrix.org".into(), origin: "matrix.org".into(),
origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658), origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658),
kind: EventType::RoomPowerLevels, kind: EventType::RoomPowerLevels,
content: json!({"testing": 123}), content: json!({"testing": 123}),
state_key: Some("state".into()), state_key: Some("state".into()),
prev_events: vec![( prev_events: vec![(
EventId::try_from("$previousevent:matrix.org").unwrap(), event_id!("$previousevent:matrix.org"),
EventHash { sha256: "123567".into() }, EventHash { sha256: "123567".into() },
)], )],
depth: 2_u32.into(), depth: 2_u32.into(),
auth_events: vec![( auth_events: vec![(
EventId::try_from("$someauthevent:matrix.org").unwrap(), event_id!("$someauthevent:matrix.org"),
EventHash { sha256: "21389CFEDABC".into() }, EventHash { sha256: "21389CFEDABC".into() },
)], )],
redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()), redacts: Some(event_id!("$9654:matrix.org")),
unsigned, unsigned,
hashes: EventHash { sha256: "1233543bABACDEF".into() }, hashes: EventHash { sha256: "1233543bABACDEF".into() },
signatures, signatures,
@ -307,17 +301,17 @@ fn serialize_pdu_as_v3() {
unsigned.insert("somekey".into(), json!({"a": 456})); unsigned.insert("somekey".into(), json!({"a": 456}));
let v3_pdu = RoomV3Pdu { let v3_pdu = RoomV3Pdu {
room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(), room_id: room_id!("!n8f893n9:example.com"),
sender: UserId::try_from("@sender:example.com").unwrap(), sender: user_id!("@sender:example.com"),
origin: "matrix.org".into(), origin: "matrix.org".into(),
origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658), origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658),
kind: EventType::RoomPowerLevels, kind: EventType::RoomPowerLevels,
content: json!({"testing": 123}), content: json!({"testing": 123}),
state_key: Some("state".into()), state_key: Some("state".into()),
prev_events: vec![EventId::try_from("$previousevent:matrix.org").unwrap()], prev_events: vec![event_id!("$previousevent:matrix.org")],
depth: 2_u32.into(), depth: 2_u32.into(),
auth_events: vec![EventId::try_from("$someauthevent:matrix.org").unwrap()], auth_events: vec![event_id!("$someauthevent:matrix.org")],
redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()), redacts: Some(event_id!("$9654:matrix.org")),
unsigned, unsigned,
hashes: EventHash { sha256: "1233543bABACDEF".into() }, hashes: EventHash { sha256: "1233543bABACDEF".into() },
signatures, signatures,
@ -397,10 +391,7 @@ fn test_deserialize_pdu_as_v1() {
match parsed { match parsed {
Pdu::RoomV1Pdu(v1_pdu) => { Pdu::RoomV1Pdu(v1_pdu) => {
assert_eq!( assert_eq!(v1_pdu.auth_events.first().unwrap().0, event_id!("$abc123:matrix.org"));
v1_pdu.auth_events.first().unwrap().0,
EventId::try_from("$abc123:matrix.org").unwrap()
);
assert_eq!( assert_eq!(
v1_pdu.auth_events.first().unwrap().1.sha256, v1_pdu.auth_events.first().unwrap().1.sha256,
"Base64EncodedSha256HashesShouldBe43BytesLong" "Base64EncodedSha256HashesShouldBe43BytesLong"
@ -449,10 +440,7 @@ fn deserialize_pdu_as_v3() {
match parsed { match parsed {
Pdu::RoomV1Pdu(_) => panic!("Matched V1 PDU"), Pdu::RoomV1Pdu(_) => panic!("Matched V1 PDU"),
Pdu::RoomV3Pdu(v3_pdu) => { Pdu::RoomV3Pdu(v3_pdu) => {
assert_eq!( assert_eq!(v3_pdu.auth_events.first().unwrap(), &event_id!("$abc123:matrix.org"));
v3_pdu.auth_events.first().unwrap(),
&EventId::try_from("$abc123:matrix.org").unwrap()
);
} }
} }
} }
@ -469,22 +457,22 @@ fn convert_v1_stub_to_pdu() {
unsigned.insert("somekey".into(), json!({"a": 456})); unsigned.insert("somekey".into(), json!({"a": 456}));
let v1_stub = RoomV1PduStub { let v1_stub = RoomV1PduStub {
sender: UserId::try_from("@sender:example.com").unwrap(), sender: user_id!("@sender:example.com"),
origin: "matrix.org".into(), origin: "matrix.org".into(),
origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658), origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658),
kind: EventType::RoomPowerLevels, kind: EventType::RoomPowerLevels,
content: json!({"testing": 123}), content: json!({"testing": 123}),
state_key: Some("state".into()), state_key: Some("state".into()),
prev_events: vec![( prev_events: vec![(
EventId::try_from("$previousevent:matrix.org").unwrap(), event_id!("$previousevent:matrix.org"),
EventHash { sha256: "123567".into() }, EventHash { sha256: "123567".into() },
)], )],
depth: 2_u32.into(), depth: 2_u32.into(),
auth_events: vec![( auth_events: vec![(
EventId::try_from("$someauthevent:matrix.org").unwrap(), event_id!("$someauthevent:matrix.org"),
EventHash { sha256: "21389CFEDABC".into() }, EventHash { sha256: "21389CFEDABC".into() },
)], )],
redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()), redacts: Some(event_id!("$9654:matrix.org")),
unsigned: (&unsigned).clone(), unsigned: (&unsigned).clone(),
hashes: EventHash { sha256: "1233543bABACDEF".into() }, hashes: EventHash { sha256: "1233543bABACDEF".into() },
signatures: (&signatures).clone(), signatures: (&signatures).clone(),
@ -492,8 +480,8 @@ fn convert_v1_stub_to_pdu() {
assert_matches!( assert_matches!(
v1_stub.into_v1_pdu( v1_stub.into_v1_pdu(
RoomId::try_from("!n8f893n9:example.com").unwrap(), room_id!("!n8f893n9:example.com"),
EventId::try_from("$somejoinevent:matrix.org").unwrap() event_id!("$somejoinevent:matrix.org")
), ),
RoomV1Pdu { RoomV1Pdu {
room_id, room_id,
@ -511,20 +499,20 @@ fn convert_v1_stub_to_pdu() {
unsigned, unsigned,
hashes: EventHash { sha256 }, hashes: EventHash { sha256 },
signatures, signatures,
} if room_id == RoomId::try_from("!n8f893n9:example.com").unwrap() } if room_id == room_id!("!n8f893n9:example.com")
&& event_id == EventId::try_from("$somejoinevent:matrix.org").unwrap() && event_id == event_id!("$somejoinevent:matrix.org")
&& sender == UserId::try_from("@sender:example.com").unwrap() && sender == user_id!("@sender:example.com")
&& origin == "matrix.org" && origin == "matrix.org"
&& origin_server_ts == SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658) && origin_server_ts == SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658)
&& kind == EventType::RoomPowerLevels && kind == EventType::RoomPowerLevels
&& content == json!({"testing": 123}) && content == json!({"testing": 123})
&& state_key == Some("state".into()) && state_key == Some("state".into())
&& prev_events[0].0 == EventId::try_from("$previousevent:matrix.org").unwrap() && prev_events[0].0 == event_id!("$previousevent:matrix.org")
&& prev_events[0].1.sha256 == "123567" && prev_events[0].1.sha256 == "123567"
&& depth == 2_u32.into() && depth == 2_u32.into()
&& auth_events.first().unwrap().0 == EventId::try_from("$someauthevent:matrix.org").unwrap() && auth_events.first().unwrap().0 == event_id!("$someauthevent:matrix.org")
&& auth_events.first().unwrap().1.sha256 == "21389CFEDABC" && auth_events.first().unwrap().1.sha256 == "21389CFEDABC"
&& redacts == Some(EventId::try_from("$9654:matrix.org").unwrap()) && redacts == Some(event_id!("$9654:matrix.org"))
&& unsigned == (&unsigned).clone() && unsigned == (&unsigned).clone()
&& sha256 == "1233543bABACDEF" && sha256 == "1233543bABACDEF"
&& signatures == (&signatures).clone() && signatures == (&signatures).clone()
@ -544,23 +532,23 @@ fn convert_v3_stub_to_pdu() {
unsigned.insert("somekey".into(), json!({"a": 456})); unsigned.insert("somekey".into(), json!({"a": 456}));
let v3_stub = RoomV3PduStub { let v3_stub = RoomV3PduStub {
sender: UserId::try_from("@sender:example.com").unwrap(), sender: user_id!("@sender:example.com"),
origin: "matrix.org".into(), origin: "matrix.org".into(),
origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658), origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658),
kind: EventType::RoomPowerLevels, kind: EventType::RoomPowerLevels,
content: json!({"testing": 123}), content: json!({"testing": 123}),
state_key: Some("state".into()), state_key: Some("state".into()),
prev_events: vec![EventId::try_from("$previousevent:matrix.org").unwrap()], prev_events: vec![event_id!("$previousevent:matrix.org")],
depth: 2_u32.into(), depth: 2_u32.into(),
auth_events: vec![EventId::try_from("$someauthevent:matrix.org").unwrap()], auth_events: vec![event_id!("$someauthevent:matrix.org")],
redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()), redacts: Some(event_id!("$9654:matrix.org")),
unsigned: (&unsigned).clone(), unsigned: (&unsigned).clone(),
hashes: EventHash { sha256: "1233543bABACDEF".into() }, hashes: EventHash { sha256: "1233543bABACDEF".into() },
signatures: (&signatures).clone(), signatures: (&signatures).clone(),
}; };
assert_matches!( assert_matches!(
v3_stub.into_v3_pdu(RoomId::try_from("!n8f893n9:example.com").unwrap()), v3_stub.into_v3_pdu(room_id!("!n8f893n9:example.com")),
RoomV3Pdu { RoomV3Pdu {
room_id, room_id,
sender, sender,
@ -576,17 +564,17 @@ fn convert_v3_stub_to_pdu() {
unsigned, unsigned,
hashes: EventHash { sha256 }, hashes: EventHash { sha256 },
signatures, signatures,
} if room_id == RoomId::try_from("!n8f893n9:example.com").unwrap() } if room_id == room_id!("!n8f893n9:example.com")
&& sender == UserId::try_from("@sender:example.com").unwrap() && sender == user_id!("@sender:example.com")
&& origin == "matrix.org" && origin == "matrix.org"
&& origin_server_ts == SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658) && origin_server_ts == SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658)
&& kind == EventType::RoomPowerLevels && kind == EventType::RoomPowerLevels
&& content == json!({"testing": 123}) && content == json!({"testing": 123})
&& state_key == Some("state".into()) && state_key == Some("state".into())
&& prev_events == vec![EventId::try_from("$previousevent:matrix.org").unwrap()] && prev_events == vec![event_id!("$previousevent:matrix.org")]
&& depth == 2_u32.into() && depth == 2_u32.into()
&& auth_events == vec![EventId::try_from("$someauthevent:matrix.org").unwrap()] && auth_events == vec![event_id!("$someauthevent:matrix.org")]
&& redacts == Some(EventId::try_from("$9654:matrix.org").unwrap()) && redacts == Some(event_id!("$9654:matrix.org"))
&& unsigned == (&unsigned).clone() && unsigned == (&unsigned).clone()
&& sha256 == "1233543bABACDEF" && sha256 == "1233543bABACDEF"
&& signatures == (&signatures).clone() && signatures == (&signatures).clone()

View File

@ -1,7 +1,4 @@
use std::{ use std::time::{Duration, UNIX_EPOCH};
convert::TryFrom,
time::{Duration, UNIX_EPOCH},
};
use matches::assert_matches; use matches::assert_matches;
use ruma_common::Raw; use ruma_common::Raw;
@ -18,7 +15,7 @@ use ruma_events::{
RedactedSyncMessageEvent, RedactedSyncStateEvent, RedactedSyncUnsigned, RedactedUnsigned, RedactedSyncMessageEvent, RedactedSyncStateEvent, RedactedSyncUnsigned, RedactedUnsigned,
Unsigned, Unsigned,
}; };
use ruma_identifiers::{EventId, RoomId, RoomVersionId, UserId}; use ruma_identifiers::{event_id, room_id, user_id, RoomVersionId};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
fn sync_unsigned() -> RedactedSyncUnsigned { fn sync_unsigned() -> RedactedSyncUnsigned {
@ -28,10 +25,10 @@ fn sync_unsigned() -> RedactedSyncUnsigned {
// to the event type string. // to the event type string.
unsigned.redacted_because = Some(Box::new(SyncRedactionEvent { unsigned.redacted_because = Some(Box::new(SyncRedactionEvent {
content: RedactionEventContent { reason: Some("redacted because".into()) }, content: RedactionEventContent { reason: Some("redacted because".into()) },
redacts: EventId::try_from("$h29iv0s8:example.com").unwrap(), redacts: event_id!("$h29iv0s8:example.com"),
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
})); }));
@ -42,11 +39,11 @@ fn full_unsigned() -> RedactedUnsigned {
let mut unsigned = RedactedUnsigned::default(); let mut unsigned = RedactedUnsigned::default();
unsigned.redacted_because = Some(Box::new(RedactionEvent { unsigned.redacted_because = Some(Box::new(RedactionEvent {
content: RedactionEventContent { reason: Some("redacted because".into()) }, content: RedactionEventContent { reason: Some("redacted because".into()) },
room_id: RoomId::try_from("!roomid:room.com").unwrap(), room_id: room_id!("!roomid:room.com"),
redacts: EventId::try_from("$h29iv0s8:example.com").unwrap(), redacts: event_id!("$h29iv0s8:example.com"),
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
})); }));
@ -57,9 +54,9 @@ fn full_unsigned() -> RedactedUnsigned {
fn redacted_message_event_serialize() { fn redacted_message_event_serialize() {
let redacted = RedactedSyncMessageEvent { let redacted = RedactedSyncMessageEvent {
content: RedactedMessageEventContent, content: RedactedMessageEventContent,
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
unsigned: RedactedSyncUnsigned::default(), unsigned: RedactedSyncUnsigned::default(),
}; };
@ -78,10 +75,10 @@ fn redacted_message_event_serialize() {
fn redacted_aliases_event_serialize_no_content() { fn redacted_aliases_event_serialize_no_content() {
let redacted = RedactedSyncStateEvent { let redacted = RedactedSyncStateEvent {
content: RedactedAliasesEventContent { aliases: None }, content: RedactedAliasesEventContent { aliases: None },
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
state_key: "".into(), state_key: "".into(),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
unsigned: RedactedSyncUnsigned::default(), unsigned: RedactedSyncUnsigned::default(),
}; };
@ -101,10 +98,10 @@ fn redacted_aliases_event_serialize_no_content() {
fn redacted_aliases_event_serialize_with_content() { fn redacted_aliases_event_serialize_with_content() {
let redacted = RedactedSyncStateEvent { let redacted = RedactedSyncStateEvent {
content: RedactedAliasesEventContent { aliases: Some(vec![]) }, content: RedactedAliasesEventContent { aliases: Some(vec![]) },
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
state_key: "".to_string(), state_key: "".to_string(),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
unsigned: RedactedSyncUnsigned::default(), unsigned: RedactedSyncUnsigned::default(),
}; };
@ -146,7 +143,7 @@ fn redacted_aliases_deserialize() {
AnySyncRoomEvent::RedactedState(AnyRedactedSyncStateEvent::RoomAliases(RedactedSyncStateEvent { AnySyncRoomEvent::RedactedState(AnyRedactedSyncStateEvent::RoomAliases(RedactedSyncStateEvent {
content: RedactedAliasesEventContent { aliases }, content: RedactedAliasesEventContent { aliases },
event_id, .. event_id, ..
})) if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() })) if event_id == event_id!("$h29iv0s8:example.com")
&& aliases.is_none() && aliases.is_none()
) )
} }
@ -174,8 +171,8 @@ fn redacted_deserialize_any_room() {
AnyRoomEvent::RedactedMessage(AnyRedactedMessageEvent::RoomMessage(RedactedMessageEvent { AnyRoomEvent::RedactedMessage(AnyRedactedMessageEvent::RoomMessage(RedactedMessageEvent {
content: RedactedMessageEventContent, content: RedactedMessageEventContent,
event_id, room_id, .. event_id, room_id, ..
})) if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() })) if event_id == event_id!("$h29iv0s8:example.com")
&& room_id == RoomId::try_from("!roomid:room.com").unwrap() && room_id == room_id!("!roomid:room.com")
) )
} }
@ -187,11 +184,11 @@ fn redacted_deserialize_any_room_sync() {
// to the event type string. // to the event type string.
unsigned.redacted_because = Some(Box::new(RedactionEvent { unsigned.redacted_because = Some(Box::new(RedactionEvent {
content: RedactionEventContent { reason: Some("redacted because".into()) }, content: RedactionEventContent { reason: Some("redacted because".into()) },
redacts: EventId::try_from("$h29iv0s8:example.com").unwrap(), redacts: event_id!("$h29iv0s8:example.com"),
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
room_id: RoomId::try_from("!roomid:room.com").unwrap(), room_id: room_id!("!roomid:room.com"),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
})); }));
@ -213,7 +210,7 @@ fn redacted_deserialize_any_room_sync() {
AnySyncRoomEvent::RedactedMessage(AnyRedactedSyncMessageEvent::RoomMessage(RedactedSyncMessageEvent { AnySyncRoomEvent::RedactedMessage(AnyRedactedSyncMessageEvent::RoomMessage(RedactedSyncMessageEvent {
content: RedactedMessageEventContent, content: RedactedMessageEventContent,
event_id, .. event_id, ..
})) if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() })) if event_id == event_id!("$h29iv0s8:example.com")
) )
} }
@ -243,10 +240,10 @@ fn redacted_state_event_deserialize() {
creator, creator,
}, },
event_id, state_key, unsigned, .. event_id, state_key, unsigned, ..
})) if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() })) if event_id == event_id!("$h29iv0s8:example.com")
&& unsigned.redacted_because.is_some() && unsigned.redacted_because.is_some()
&& state_key == "hello there" && state_key == "hello there"
&& creator == UserId::try_from("@carl:example.com").unwrap() && creator == user_id!("@carl:example.com")
) )
} }
@ -273,7 +270,7 @@ fn redacted_custom_event_serialize() {
event_type, event_type,
}, },
event_id, state_key, unsigned, .. event_id, state_key, unsigned, ..
})) if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() })) if event_id == event_id!("$h29iv0s8:example.com")
&& unsigned.redacted_because.is_some() && unsigned.redacted_because.is_some()
&& state_key == "hello there" && state_key == "hello there"
&& event_type == "m.made.up" && event_type == "m.made.up"
@ -281,7 +278,7 @@ fn redacted_custom_event_serialize() {
let x = let x =
from_json_value::<Raw<AnyRedactedSyncStateEvent>>(redacted).unwrap().deserialize().unwrap(); from_json_value::<Raw<AnyRedactedSyncStateEvent>>(redacted).unwrap().deserialize().unwrap();
assert_eq!(x.event_id(), &EventId::try_from("$h29iv0s8:example.com").unwrap()) assert_eq!(x.event_id(), &event_id!("$h29iv0s8:example.com"))
} }
#[test] #[test]
@ -290,8 +287,8 @@ fn redacted_custom_event_deserialize() {
let redacted = RedactedSyncStateEvent { let redacted = RedactedSyncStateEvent {
content: RedactedCustomEventContent { event_type: "m.made.up".into() }, content: RedactedCustomEventContent { event_type: "m.made.up".into() },
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
state_key: "hello there".into(), state_key: "hello there".into(),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
unsigned: unsigned.clone(), unsigned: unsigned.clone(),
@ -327,11 +324,11 @@ fn redact_method_properly_redacts() {
let redaction = RedactionEvent { let redaction = RedactionEvent {
content: RedactionEventContent { reason: Some("redacted because".into()) }, content: RedactionEventContent { reason: Some("redacted because".into()) },
redacts: EventId::try_from("$143273582443PhrSn:example.com").unwrap(), redacts: event_id!("$143273582443PhrSn:example.com"),
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
room_id: RoomId::try_from("!roomid:room.com").unwrap(), room_id: room_id!("!roomid:room.com"),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
}; };
@ -346,10 +343,10 @@ fn redact_method_properly_redacts() {
sender, sender,
origin_server_ts, origin_server_ts,
unsigned, unsigned,
}) if event_id == EventId::try_from("$143273582443PhrSn:example.com").unwrap() }) if event_id == event_id!("$143273582443PhrSn:example.com")
&& unsigned.redacted_because.is_some() && unsigned.redacted_because.is_some()
&& room_id == RoomId::try_from("!roomid:room.com").unwrap() && room_id == room_id!("!roomid:room.com")
&& sender == UserId::try_from("@user:example.com").unwrap() && sender == user_id!("@user:example.com")
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
); );
} }

View File

@ -1,7 +1,4 @@
use std::{ use std::time::{Duration, UNIX_EPOCH};
convert::TryFrom,
time::{Duration, UNIX_EPOCH},
};
use matches::assert_matches; use matches::assert_matches;
use ruma_common::Raw; use ruma_common::Raw;
@ -9,7 +6,7 @@ use ruma_events::{
room::redaction::{RedactionEvent, RedactionEventContent}, room::redaction::{RedactionEvent, RedactionEventContent},
AnyMessageEvent, Unsigned, AnyMessageEvent, Unsigned,
}; };
use ruma_identifiers::{EventId, RoomId, UserId}; use ruma_identifiers::{event_id, room_id, user_id};
use serde_json::{ use serde_json::{
from_value as from_json_value, json, to_value as to_json_value, Value as JsonValue, from_value as from_json_value, json, to_value as to_json_value, Value as JsonValue,
}; };
@ -32,11 +29,11 @@ fn redaction() -> JsonValue {
fn serialize_redaction() { fn serialize_redaction() {
let aliases_event = RedactionEvent { let aliases_event = RedactionEvent {
content: RedactionEventContent { reason: Some("being a turd".into()) }, content: RedactionEventContent { reason: Some("being a turd".into()) },
redacts: EventId::try_from("$nomore:example.com").unwrap(), redacts: event_id!("$nomore:example.com"),
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
room_id: RoomId::try_from("!roomid:room.com").unwrap(), room_id: room_id!("!roomid:room.com"),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
}; };
@ -64,11 +61,11 @@ fn deserialize_redaction() {
sender, sender,
unsigned, unsigned,
}) if reas == "being a turd" }) if reas == "being a turd"
&& event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() && event_id == event_id!("$h29iv0s8:example.com")
&& redacts == EventId::try_from("$nomore:example.com").unwrap() && redacts == event_id!("$nomore:example.com")
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
&& room_id == RoomId::try_from("!roomid:room.com").unwrap() && room_id == room_id!("!roomid:room.com")
&& sender == UserId::try_from("@carl:example.com").unwrap() && sender == user_id!("@carl:example.com")
&& unsigned.is_empty() && unsigned.is_empty()
); );
} }

View File

@ -1,7 +1,4 @@
use std::{ use std::time::{Duration, UNIX_EPOCH};
convert::TryFrom,
time::{Duration, UNIX_EPOCH},
};
use js_int::UInt; use js_int::UInt;
use matches::assert_matches; use matches::assert_matches;
@ -11,7 +8,7 @@ use ruma_events::{
AnyRoomEvent, AnyStateEvent, AnyStateEventContent, AnySyncStateEvent, RawExt, StateEvent, AnyRoomEvent, AnyStateEvent, AnyStateEventContent, AnySyncStateEvent, RawExt, StateEvent,
SyncStateEvent, Unsigned, SyncStateEvent, Unsigned,
}; };
use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId}; use ruma_identifiers::{event_id, room_alias_id, room_id, user_id};
use serde_json::{ use serde_json::{
from_value as from_json_value, json, to_value as to_json_value, Value as JsonValue, from_value as from_json_value, json, to_value as to_json_value, Value as JsonValue,
}; };
@ -36,16 +33,16 @@ fn aliases_event_with_prev_content() -> JsonValue {
#[test] #[test]
fn serialize_aliases_with_prev_content() { fn serialize_aliases_with_prev_content() {
let aliases_event = StateEvent { let aliases_event = StateEvent {
content: AnyStateEventContent::RoomAliases(AliasesEventContent::new(vec![ content: AnyStateEventContent::RoomAliases(AliasesEventContent::new(vec![room_alias_id!(
RoomAliasId::try_from("#somewhere:localhost").unwrap(), "#somewhere:localhost"
])), )])),
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
prev_content: Some(AnyStateEventContent::RoomAliases(AliasesEventContent::new(vec![ prev_content: Some(AnyStateEventContent::RoomAliases(AliasesEventContent::new(vec![
RoomAliasId::try_from("#inner:localhost").unwrap(), room_alias_id!("#inner:localhost"),
]))), ]))),
room_id: RoomId::try_from("!roomid:room.com").unwrap(), room_id: room_id!("!roomid:room.com"),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
state_key: "".into(), state_key: "".into(),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
}; };
@ -59,14 +56,14 @@ fn serialize_aliases_with_prev_content() {
#[test] #[test]
fn serialize_aliases_without_prev_content() { fn serialize_aliases_without_prev_content() {
let aliases_event = StateEvent { let aliases_event = StateEvent {
content: AnyStateEventContent::RoomAliases(AliasesEventContent::new(vec![ content: AnyStateEventContent::RoomAliases(AliasesEventContent::new(vec![room_alias_id!(
RoomAliasId::try_from("#somewhere:localhost").unwrap(), "#somewhere:localhost"
])), )])),
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
prev_content: None, prev_content: None,
room_id: RoomId::try_from("!roomid:room.com").unwrap(), room_id: room_id!("!roomid:room.com"),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: user_id!("@carl:example.com"),
state_key: "".into(), state_key: "".into(),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
}; };
@ -99,7 +96,7 @@ fn deserialize_aliases_content() {
.deserialize_content("m.room.aliases") .deserialize_content("m.room.aliases")
.unwrap(), .unwrap(),
AnyStateEventContent::RoomAliases(content) AnyStateEventContent::RoomAliases(content)
if content.aliases == vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()] if content.aliases == vec![room_alias_id!("#somewhere:localhost")]
); );
} }
@ -121,12 +118,12 @@ fn deserialize_aliases_with_prev_content() {
sender, sender,
state_key, state_key,
unsigned, unsigned,
} if content.aliases == vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()] } if content.aliases == vec![room_alias_id!("#somewhere:localhost")]
&& event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() && event_id == event_id!("$h29iv0s8:example.com")
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
&& prev_content.aliases == vec![RoomAliasId::try_from("#inner:localhost").unwrap()] && prev_content.aliases == vec![room_alias_id!("#inner:localhost")]
&& room_id == RoomId::try_from("!roomid:room.com").unwrap() && room_id == room_id!("!roomid:room.com")
&& sender == UserId::try_from("@carl:example.com").unwrap() && sender == user_id!("@carl:example.com")
&& state_key == "" && state_key == ""
&& unsigned.is_empty() && unsigned.is_empty()
); );
@ -148,11 +145,11 @@ fn deserialize_aliases_sync_with_room_id() {
sender, sender,
state_key, state_key,
unsigned, unsigned,
} if content.aliases == vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()] } if content.aliases == vec![room_alias_id!("#somewhere:localhost")]
&& event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() && event_id == event_id!("$h29iv0s8:example.com")
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
&& prev_content.aliases == vec![RoomAliasId::try_from("#inner:localhost").unwrap()] && prev_content.aliases == vec![room_alias_id!("#inner:localhost")]
&& sender == UserId::try_from("@carl:example.com").unwrap() && sender == user_id!("@carl:example.com")
&& state_key == "" && state_key == ""
&& unsigned.is_empty() && unsigned.is_empty()
); );
@ -203,10 +200,10 @@ fn deserialize_avatar_without_prev_content() {
sender, sender,
state_key, state_key,
unsigned unsigned
} if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() } if event_id == event_id!("$h29iv0s8:example.com")
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
&& room_id == RoomId::try_from("!roomid:room.com").unwrap() && room_id == room_id!("!roomid:room.com")
&& sender == UserId::try_from("@carl:example.com").unwrap() && sender == user_id!("@carl:example.com")
&& state_key == "" && state_key == ""
&& matches!( && matches!(
info.as_ref(), info.as_ref(),
@ -279,9 +276,9 @@ fn deserialize_member_event_with_top_level_membership_field() {
sender, sender,
.. ..
} }
)) if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() )) if event_id == event_id!("$h29iv0s8:example.com")
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
&& sender == UserId::try_from("@example:localhost").unwrap() && sender == user_id!("@example:localhost")
&& content.displayname == Some("example".into()) && content.displayname == Some("example".into())
); );
} }
@ -309,10 +306,10 @@ fn deserialize_full_event_convert_to_sync() {
sender, sender,
state_key, state_key,
unsigned, unsigned,
}) if content.aliases == vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()] }) if content.aliases == vec![room_alias_id!("#somewhere:localhost")]
&& event_id == "$h29iv0s8:example.com" && event_id == "$h29iv0s8:example.com"
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
&& prev_content.aliases == vec![RoomAliasId::try_from("#inner:localhost").unwrap()] && prev_content.aliases == vec![room_alias_id!("#inner:localhost")]
&& sender == "@carl:example.com" && sender == "@carl:example.com"
&& state_key == "" && state_key == ""
&& unsigned.is_empty() && unsigned.is_empty()

View File

@ -1,11 +1,9 @@
use std::convert::TryFrom;
use js_int::uint; use js_int::uint;
use ruma_events::{ use ruma_events::{
room::{join_rules::JoinRule, topic::TopicEventContent}, room::{join_rules::JoinRule, topic::TopicEventContent},
AnyStateEventContent, AnyStrippedStateEvent, StrippedStateEvent, AnyStateEventContent, AnyStrippedStateEvent, StrippedStateEvent,
}; };
use ruma_identifiers::UserId; use ruma_identifiers::user_id;
use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
#[test] #[test]
@ -15,7 +13,7 @@ fn serialize_stripped_state_event_any_content() {
topic: "Testing room".into(), topic: "Testing room".into(),
}), }),
state_key: "".into(), state_key: "".into(),
sender: UserId::try_from("@example:localhost").unwrap(), sender: user_id!("@example:localhost"),
}; };
let json_data = json!({ let json_data = json!({
@ -35,7 +33,7 @@ fn serialize_stripped_state_event_any_event() {
let event = AnyStrippedStateEvent::RoomTopic(StrippedStateEvent { let event = AnyStrippedStateEvent::RoomTopic(StrippedStateEvent {
content: TopicEventContent { topic: "Testing room".into() }, content: TopicEventContent { topic: "Testing room".into() },
state_key: "".into(), state_key: "".into(),
sender: UserId::try_from("@example:localhost").unwrap(), sender: user_id!("@example:localhost"),
}); });
let json_data = json!({ let json_data = json!({

View File

@ -1,18 +1,16 @@
use std::convert::TryFrom;
use ruma_events::{ use ruma_events::{
room_key::RoomKeyEventContent, Algorithm, AnyToDeviceEventContent, ToDeviceEvent, room_key::RoomKeyEventContent, Algorithm, AnyToDeviceEventContent, ToDeviceEvent,
}; };
use ruma_identifiers::{RoomId, UserId}; use ruma_identifiers::{room_id, user_id};
use serde_json::{json, to_value as to_json_value}; use serde_json::{json, to_value as to_json_value};
#[test] #[test]
fn serialization() { fn serialization() {
let ev = ToDeviceEvent { let ev = ToDeviceEvent {
sender: UserId::try_from("@example:example.org").unwrap(), sender: user_id!("@example:example.org"),
content: AnyToDeviceEventContent::RoomKey(RoomKeyEventContent { content: AnyToDeviceEventContent::RoomKey(RoomKeyEventContent {
algorithm: Algorithm::MegolmV1AesSha2, algorithm: Algorithm::MegolmV1AesSha2,
room_id: RoomId::try_from("!testroomid:example.org").unwrap(), room_id: room_id!("!testroomid:example.org"),
session_id: "SessId".into(), session_id: "SessId".into(),
session_key: "SessKey".into(), session_key: "SessKey".into(),
}), }),

View File

@ -70,9 +70,9 @@ impl<'de> Visitor<'de> for PduProcessResponseVisitor {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{collections::BTreeMap, convert::TryFrom}; use std::collections::BTreeMap;
use ruma_identifiers::EventId; use ruma_identifiers::{event_id, EventId};
use serde_json::{json, value::Serializer as JsonSerializer}; use serde_json::{json, value::Serializer as JsonSerializer};
use super::{deserialize, serialize}; use super::{deserialize, serialize};
@ -80,10 +80,7 @@ mod tests {
#[test] #[test]
fn serialize_error() { fn serialize_error() {
let mut response: BTreeMap<EventId, Result<(), String>> = BTreeMap::new(); let mut response: BTreeMap<EventId, Result<(), String>> = BTreeMap::new();
response.insert( response.insert(event_id!("$someevent:matrix.org"), Err("Some processing error.".into()));
EventId::try_from("$someevent:matrix.org").unwrap(),
Err("Some processing error.".into()),
);
let serialized = serialize(&response, JsonSerializer).unwrap(); let serialized = serialize(&response, JsonSerializer).unwrap();
let json = json!({ let json = json!({
@ -95,7 +92,7 @@ mod tests {
#[test] #[test]
fn serialize_ok() { fn serialize_ok() {
let mut response: BTreeMap<EventId, Result<(), String>> = BTreeMap::new(); let mut response: BTreeMap<EventId, Result<(), String>> = BTreeMap::new();
response.insert(EventId::try_from("$someevent:matrix.org").unwrap(), Ok(())); response.insert(event_id!("$someevent:matrix.org"), Ok(()));
let serialized = serialize(&response, serde_json::value::Serializer).unwrap(); let serialized = serialize(&response, serde_json::value::Serializer).unwrap();
let json = json!({ let json = json!({
@ -111,7 +108,7 @@ mod tests {
}); });
let response = deserialize(json).unwrap(); let response = deserialize(json).unwrap();
let event_id = EventId::try_from("$someevent:matrix.org").unwrap(); let event_id = event_id!("$someevent:matrix.org");
let event_response = response.get(&event_id).unwrap().clone().unwrap_err(); let event_response = response.get(&event_id).unwrap().clone().unwrap_err();
assert_eq!(event_response, "Some processing error."); assert_eq!(event_response, "Some processing error.");
@ -124,7 +121,7 @@ mod tests {
}); });
let response = deserialize(json).unwrap(); let response = deserialize(json).unwrap();
let event_id = EventId::try_from("$someevent:matrix.org").unwrap(); let event_id = event_id!("$someevent:matrix.org");
assert!(response.get(&event_id).unwrap().is_ok()); assert!(response.get(&event_id).unwrap().is_ok());
} }
@ -136,7 +133,7 @@ mod tests {
}); });
let response = deserialize(json).unwrap(); let response = deserialize(json).unwrap();
let event_id = EventId::try_from("$someevent:matrix.org").unwrap(); let event_id = event_id!("$someevent:matrix.org");
let event_response = response.get(&event_id).unwrap().clone().unwrap_err(); let event_response = response.get(&event_id).unwrap().clone().unwrap_err();
assert_eq!(event_response, ""); assert_eq!(event_response, "");
@ -148,9 +145,6 @@ mod tests {
"$someevent:matrix.org": {} "$someevent:matrix.org": {}
}); });
let response = deserialize(json).unwrap(); let response = deserialize(json).unwrap();
assert!(response assert!(response.get(&event_id!("$someevent:matrix.org")).unwrap().is_ok());
.get(&EventId::try_from("$someevent:matrix.org").unwrap())
.unwrap()
.is_ok());
} }
} }