events: Clean up location event tests

This commit is contained in:
Jonas Platte 2022-06-03 10:29:18 +02:00
parent 8912182c56
commit 31d526ebfd
No known key found for this signature in database
GPG Key ID: AAA7A61F696C3E0C

View File

@ -6,10 +6,7 @@ use js_int::uint;
use ruma_common::{ use ruma_common::{
event_id, event_id,
events::{ events::{
location::{ location::{AssetType, LocationContent, LocationEventContent, ZoomLevel, ZoomLevelError},
AssetContent, AssetType, LocationContent, LocationEventContent, ZoomLevel,
ZoomLevelError,
},
message::MessageContent, message::MessageContent,
room::message::{ room::message::{
InReplyTo, LocationMessageEventContent, MessageType, Relation, RoomMessageEventContent, InReplyTo, LocationMessageEventContent, MessageType, Relation, RoomMessageEventContent,
@ -113,28 +110,15 @@ fn plain_content_deserialization() {
}, },
}); });
assert_matches!( let ev = from_json_value::<LocationEventContent>(json_data).unwrap();
from_json_value::<LocationEventContent>(json_data)
.unwrap(), assert_eq!(ev.message.find_plain(), Some("Alice was at geo:51.5008,0.1247;u=35"));
LocationEventContent { assert_eq!(ev.message.find_html(), None);
message, assert_eq!(ev.location.uri, "geo:51.5008,0.1247;u=35");
location: LocationContent { assert_eq!(ev.location.description, None);
uri, assert_matches!(ev.location.zoom_level, None);
description: None, assert_eq!(ev.asset.type_, AssetType::Self_);
zoom_level: None, assert_eq!(ev.ts, None);
..
},
asset: AssetContent {
type_: AssetType::Self_,
..
},
ts: None,
..
}
if message.find_plain() == Some("Alice was at geo:51.5008,0.1247;u=35")
&& message.find_html().is_none()
&& uri == "geo:51.5008,0.1247;u=35"
);
} }
#[test] #[test]
@ -160,12 +144,9 @@ fn zoomlevel_deserialization_too_high() {
"zoom_level": 30, "zoom_level": 30,
}); });
assert_matches!( let err = from_json_value::<LocationContent>(json_data).unwrap_err();
from_json_value::<LocationContent>(json_data), assert!(err.is_data());
Err(err) assert_eq!(err.to_string(), ZoomLevelError::TooHigh.to_string());
if err.is_data()
&& format!("{err}") == format!("{}", ZoomLevelError::TooHigh)
);
} }
#[test] #[test]
@ -194,41 +175,27 @@ fn message_event_deserialization() {
"type": "m.location", "type": "m.location",
}); });
assert_matches!( let ev = from_json_value::<AnyMessageLikeEvent>(json_data).unwrap();
from_json_value::<AnyMessageLikeEvent>(json_data).unwrap(),
AnyMessageLikeEvent::Location(MessageLikeEvent::Original(OriginalMessageLikeEvent { let ev =
content: LocationEventContent { assert_matches!(ev, AnyMessageLikeEvent::Location(MessageLikeEvent::Original(ev)) => ev);
message,
location: LocationContent { assert_eq!(
uri, ev.content.message.find_plain(),
description: Some(description), Some("Alice was at geo:51.5008,0.1247;u=35 as of Sat Nov 13 18:50:58 2021")
zoom_level: Some(zoom_level),
..
},
asset: AssetContent {
type_: AssetType::Self_,
..
},
ts: Some(ts),
..
},
event_id,
origin_server_ts,
room_id,
sender,
unsigned
})) if event_id == event_id!("$event:notareal.hs")
&& message.find_plain() == Some("Alice was at geo:51.5008,0.1247;u=35 as of Sat Nov 13 18:50:58 2021")
&& message.find_html().is_none()
&& uri == "geo:51.5008,0.1247;u=35"
&& description == "Alice's whereabouts"
&& zoom_level.get() == uint!(4)
&& ts == MilliSecondsSinceUnixEpoch(uint!(1_636_829_458))
&& origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(134_829_848))
&& room_id == room_id!("!roomid:notareal.hs")
&& sender == user_id!("@user:notareal.hs")
&& unsigned.is_empty()
); );
assert_eq!(ev.content.message.find_html(), None);
assert_eq!(ev.content.location.uri, "geo:51.5008,0.1247;u=35");
assert_eq!(ev.content.location.description.as_deref(), Some("Alice's whereabouts"));
assert_eq!(ev.content.location.zoom_level.unwrap().get(), uint!(4));
assert_eq!(ev.content.asset.type_, AssetType::Self_);
assert_eq!(ev.content.ts, Some(MilliSecondsSinceUnixEpoch(uint!(1_636_829_458))));
assert_eq!(ev.event_id, event_id!("$event:notareal.hs"));
assert_eq!(ev.origin_server_ts, MilliSecondsSinceUnixEpoch(uint!(134_829_848)));
assert_eq!(ev.room_id, room_id!("!roomid:notareal.hs"));
assert_eq!(ev.sender, user_id!("@user:notareal.hs"));
assert!(ev.unsigned.is_empty());
} }
#[test] #[test]
@ -266,8 +233,8 @@ fn room_message_stable_deserialization() {
}); });
let event_content = from_json_value::<RoomMessageEventContent>(json_data).unwrap(); let event_content = from_json_value::<RoomMessageEventContent>(json_data).unwrap();
assert_matches!(event_content.msgtype, MessageType::Location(_)); let content = assert_matches!(event_content.msgtype, MessageType::Location(c) => c);
if let MessageType::Location(content) = event_content.msgtype {
assert_eq!(content.body, "Alice was at geo:51.5008,0.1247;u=35"); assert_eq!(content.body, "Alice was at geo:51.5008,0.1247;u=35");
assert_eq!(content.geo_uri, "geo:51.5008,0.1247;u=35"); assert_eq!(content.geo_uri, "geo:51.5008,0.1247;u=35");
let message = content.message.unwrap(); let message = content.message.unwrap();
@ -275,7 +242,6 @@ fn room_message_stable_deserialization() {
assert_eq!(message[0].body, "Alice was at geo:51.5008,0.1247;u=35"); assert_eq!(message[0].body, "Alice was at geo:51.5008,0.1247;u=35");
assert_eq!(content.location.unwrap().uri, "geo:51.5008,0.1247;u=35"); assert_eq!(content.location.unwrap().uri, "geo:51.5008,0.1247;u=35");
} }
}
#[test] #[test]
fn room_message_unstable_deserialization() { fn room_message_unstable_deserialization() {
@ -290,8 +256,8 @@ fn room_message_unstable_deserialization() {
}); });
let event_content = from_json_value::<RoomMessageEventContent>(json_data).unwrap(); let event_content = from_json_value::<RoomMessageEventContent>(json_data).unwrap();
assert_matches!(event_content.msgtype, MessageType::Location(_)); let content = assert_matches!(event_content.msgtype, MessageType::Location(c) => c);
if let MessageType::Location(content) = event_content.msgtype {
assert_eq!(content.body, "Alice was at geo:51.5008,0.1247;u=35"); assert_eq!(content.body, "Alice was at geo:51.5008,0.1247;u=35");
assert_eq!(content.geo_uri, "geo:51.5008,0.1247;u=35"); assert_eq!(content.geo_uri, "geo:51.5008,0.1247;u=35");
let message = content.message.unwrap(); let message = content.message.unwrap();
@ -299,4 +265,3 @@ fn room_message_unstable_deserialization() {
assert_eq!(message[0].body, "Alice was at geo:51.5008,0.1247;u=35"); assert_eq!(message[0].body, "Alice was at geo:51.5008,0.1247;u=35");
assert_eq!(content.location.unwrap().uri, "geo:51.5008,0.1247;u=35"); assert_eq!(content.location.unwrap().uri, "geo:51.5008,0.1247;u=35");
} }
}