diff --git a/ruma-events/Cargo.toml b/ruma-events/Cargo.toml
index a31a8a8d..19788318 100644
--- a/ruma-events/Cargo.toml
+++ b/ruma-events/Cargo.toml
@@ -23,6 +23,7 @@ serde_json = { version = "1.0.60", features = ["raw_value"] }
pulldown-cmark = { version = "0.8", default-features = false, optional = true }
[dev-dependencies]
+assign = "1.1.1"
maplit = "1.0.2"
matches = "0.1.8"
ruma-identifiers = { version = "=0.18.0-alpha.1", path = "../ruma-identifiers", features = ["rand"] }
diff --git a/ruma-events/src/room/message.rs b/ruma-events/src/room/message.rs
index 8ef149ec..51ace353 100644
--- a/ruma-events/src/room/message.rs
+++ b/ruma-events/src/room/message.rs
@@ -585,306 +585,3 @@ pub struct KeyVerificationRequestEventContent {
/// events that have a m.reference relationship with this event.
pub to: UserId,
}
-
-#[cfg(test)]
-mod tests {
- use std::time::{Duration, UNIX_EPOCH};
-
- use matches::assert_matches;
- #[cfg(feature = "unstable-pre-spec")]
- use ruma_identifiers::DeviceIdBox;
- use ruma_identifiers::{event_id, room_id, user_id};
- use ruma_serde::Raw;
- use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
-
- #[cfg(feature = "unstable-pre-spec")]
- use super::KeyVerificationRequestEventContent;
- use super::{
- AudioMessageEventContent, FormattedBody, MessageEventContent, MessageFormat, Relation,
- };
- #[cfg(feature = "unstable-pre-spec")]
- use crate::key::verification::VerificationMethod;
- use crate::{
- room::{message::TextMessageEventContent, relationships::InReplyTo},
- MessageEvent, Unsigned,
- };
-
- #[test]
- fn serialization() {
- let ev = MessageEvent {
- content: MessageEventContent::Audio(AudioMessageEventContent {
- body: "test".into(),
- info: None,
- url: Some("http://example.com/audio.mp3".into()),
- file: None,
- }),
- event_id: event_id!("$143273582443PhrSn:example.org"),
- origin_server_ts: UNIX_EPOCH + Duration::from_millis(10_000),
- room_id: room_id!("!testroomid:example.org"),
- sender: user_id!("@user:example.org"),
- unsigned: Unsigned::default(),
- };
-
- assert_eq!(
- to_json_value(ev).unwrap(),
- json!({
- "type": "m.room.message",
- "event_id": "$143273582443PhrSn:example.org",
- "origin_server_ts": 10_000,
- "room_id": "!testroomid:example.org",
- "sender": "@user:example.org",
- "content": {
- "body": "test",
- "msgtype": "m.audio",
- "url": "http://example.com/audio.mp3",
- }
- })
- );
- }
-
- #[test]
- fn content_serialization() {
- let message_event_content = MessageEventContent::Audio(AudioMessageEventContent {
- body: "test".into(),
- info: None,
- url: Some("http://example.com/audio.mp3".into()),
- file: None,
- });
-
- assert_eq!(
- to_json_value(&message_event_content).unwrap(),
- json!({
- "body": "test",
- "msgtype": "m.audio",
- "url": "http://example.com/audio.mp3"
- })
- );
- }
-
- #[test]
- fn formatted_body_serialization() {
- let message_event_content = MessageEventContent::Text(TextMessageEventContent {
- body: "Hello, World!".into(),
- formatted: Some(FormattedBody {
- format: MessageFormat::Html,
- body: "Hello, World!".into(),
- }),
- relates_to: None,
- #[cfg(feature = "unstable-pre-spec")]
- new_content: None,
- });
-
- assert_eq!(
- to_json_value(&message_event_content).unwrap(),
- json!({
- "body": "Hello, World!",
- "msgtype": "m.text",
- "format": "org.matrix.custom.html",
- "formatted_body": "Hello, World!",
- })
- );
- }
-
- #[test]
- fn plain_text_content_serialization() {
- let message_event_content = MessageEventContent::Text(TextMessageEventContent::plain(
- "> <@test:example.com> test\n\ntest reply",
- ));
-
- assert_eq!(
- to_json_value(&message_event_content).unwrap(),
- json!({
- "body": "> <@test:example.com> test\n\ntest reply",
- "msgtype": "m.text"
- })
- );
- }
-
- #[test]
- fn relates_to_content_serialization() {
- let message_event_content = MessageEventContent::Text(TextMessageEventContent {
- body: "> <@test:example.com> test\n\ntest reply".to_owned(),
- formatted: None,
- relates_to: Some(Relation::Reply {
- in_reply_to: InReplyTo { event_id: event_id!("$15827405538098VGFWH:example.com") },
- }),
- #[cfg(feature = "unstable-pre-spec")]
- new_content: None,
- });
-
- let json_data = json!({
- "body": "> <@test:example.com> test\n\ntest reply",
- "msgtype": "m.text",
- "m.relates_to": {
- "m.in_reply_to": {
- "event_id": "$15827405538098VGFWH:example.com"
- }
- }
- });
-
- assert_eq!(to_json_value(&message_event_content).unwrap(), json_data);
- }
-
- #[test]
- #[cfg(not(feature = "unstable-pre-spec"))]
- fn edit_deserialization_061() {
- let json_data = json!({
- "body": "s/foo/bar",
- "msgtype": "m.text",
- "m.relates_to": {
- "rel_type": "m.replace",
- "event_id": event_id!("$1598361704261elfgc:localhost"),
- },
- "m.new_content": {
- "body": "bar",
- },
- });
-
- assert_matches!(
- from_json_value::(json_data).unwrap(),
- MessageEventContent::Text(TextMessageEventContent {
- body,
- formatted: None,
- relates_to: Some(Relation::Custom(_)),
- }) if body == "s/foo/bar"
- );
- }
-
- #[test]
- #[cfg(feature = "unstable-pre-spec")]
- fn edit_deserialization_future() {
- use crate::room::relationships::Replacement;
-
- let ev_id = event_id!("$1598361704261elfgc:localhost");
- let json_data = json!({
- "body": "s/foo/bar",
- "msgtype": "m.text",
- "m.relates_to": {
- "rel_type": "m.replace",
- "event_id": ev_id,
- },
- "m.new_content": {
- "body": "bar",
- "msgtype": "m.text",
- },
- });
-
- assert_matches!(
- from_json_value::(json_data).unwrap(),
- MessageEventContent::Text(TextMessageEventContent {
- body,
- formatted: None,
- relates_to: Some(Relation::Replacement(Replacement { event_id })),
- new_content: Some(new_content),
- }) if body == "s/foo/bar"
- && event_id == ev_id
- && matches!(
- &*new_content,
- MessageEventContent::Text(TextMessageEventContent {
- body,
- formatted: None,
- relates_to: None,
- new_content: None,
- }) if body == "bar"
- )
- );
- }
-
- #[test]
- #[cfg(feature = "unstable-pre-spec")]
- fn verification_request_deserialization() {
- let user_id = user_id!("@example2:localhost");
- let device_id: DeviceIdBox = "XOWLHHFSWM".into();
-
- let json_data = json!({
- "body": "@example:localhost is requesting to verify your key, ...",
- "msgtype": "m.key.verification.request",
- "to": user_id,
- "from_device": device_id,
- "methods": [
- "m.sas.v1",
- "m.qr_code.show.v1",
- "m.reciprocate.v1"
- ]
- });
-
- assert_matches!(
- from_json_value::(json_data).unwrap(),
- MessageEventContent::VerificationRequest(KeyVerificationRequestEventContent {
- body,
- to,
- from_device,
- methods
- }) if body == "@example:localhost is requesting to verify your key, ..."
- && to == user_id
- && from_device == device_id
- && methods.contains(&VerificationMethod::MSasV1)
- );
- }
-
- #[test]
- #[cfg(feature = "unstable-pre-spec")]
- fn verification_request_serialization() {
- let user_id = user_id!("@example2:localhost");
- let device_id: DeviceIdBox = "XOWLHHFSWM".into();
- let body = "@example:localhost is requesting to verify your key, ...".to_string();
-
- let methods = vec![
- VerificationMethod::MSasV1,
- VerificationMethod::_Custom("m.qr_code.show.v1".to_string()),
- VerificationMethod::_Custom("m.reciprocate.v1".to_string()),
- ];
-
- let json_data = json!({
- "body": body,
- "msgtype": "m.key.verification.request",
- "to": user_id,
- "from_device": device_id,
- "methods": methods
- });
-
- let content =
- MessageEventContent::VerificationRequest(KeyVerificationRequestEventContent {
- to: user_id,
- from_device: device_id,
- body,
- methods,
- });
-
- assert_eq!(to_json_value(&content).unwrap(), json_data,);
- }
-
- #[test]
- fn content_deserialization() {
- let json_data = json!({
- "body": "test",
- "msgtype": "m.audio",
- "url": "http://example.com/audio.mp3"
- });
-
- assert_matches!(
- from_json_value::>(json_data)
- .unwrap()
- .deserialize()
- .unwrap(),
- MessageEventContent::Audio(AudioMessageEventContent {
- body,
- info: None,
- url: Some(url),
- file: None,
- }) if body == "test" && url == "http://example.com/audio.mp3"
- );
- }
-
- #[test]
- fn content_deserialization_failure() {
- let json_data = json!({
- "body": "test","msgtype": "m.location",
- "url": "http://example.com/audio.mp3"
- });
- assert!(from_json_value::>(json_data)
- .unwrap()
- .deserialize()
- .is_err());
- }
-}
diff --git a/ruma-events/tests/room_message.rs b/ruma-events/tests/room_message.rs
new file mode 100644
index 00000000..f1004296
--- /dev/null
+++ b/ruma-events/tests/room_message.rs
@@ -0,0 +1,290 @@
+use std::time::{Duration, UNIX_EPOCH};
+
+use assign::assign;
+use matches::assert_matches;
+#[cfg(feature = "unstable-pre-spec")]
+use ruma_events::{
+ key::verification::VerificationMethod, room::message::KeyVerificationRequestEventContent,
+};
+use ruma_events::{
+ room::{
+ message::{
+ AudioMessageEventContent, MessageEventContent, Relation, TextMessageEventContent,
+ },
+ relationships::InReplyTo,
+ },
+ MessageEvent, Unsigned,
+};
+#[cfg(feature = "unstable-pre-spec")]
+use ruma_identifiers::DeviceIdBox;
+use ruma_identifiers::{event_id, room_id, user_id};
+use ruma_serde::Raw;
+use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
+
+#[test]
+fn serialization() {
+ let ev = MessageEvent {
+ content: MessageEventContent::Audio(AudioMessageEventContent {
+ body: "test".into(),
+ info: None,
+ url: Some("http://example.com/audio.mp3".into()),
+ file: None,
+ }),
+ event_id: event_id!("$143273582443PhrSn:example.org"),
+ origin_server_ts: UNIX_EPOCH + Duration::from_millis(10_000),
+ room_id: room_id!("!testroomid:example.org"),
+ sender: user_id!("@user:example.org"),
+ unsigned: Unsigned::default(),
+ };
+
+ assert_eq!(
+ to_json_value(ev).unwrap(),
+ json!({
+ "type": "m.room.message",
+ "event_id": "$143273582443PhrSn:example.org",
+ "origin_server_ts": 10_000,
+ "room_id": "!testroomid:example.org",
+ "sender": "@user:example.org",
+ "content": {
+ "body": "test",
+ "msgtype": "m.audio",
+ "url": "http://example.com/audio.mp3",
+ }
+ })
+ );
+}
+
+#[test]
+fn content_serialization() {
+ let message_event_content = MessageEventContent::Audio(AudioMessageEventContent {
+ body: "test".into(),
+ info: None,
+ url: Some("http://example.com/audio.mp3".into()),
+ file: None,
+ });
+
+ assert_eq!(
+ to_json_value(&message_event_content).unwrap(),
+ json!({
+ "body": "test",
+ "msgtype": "m.audio",
+ "url": "http://example.com/audio.mp3"
+ })
+ );
+}
+
+#[test]
+fn formatted_body_serialization() {
+ let message_event_content = MessageEventContent::Text(TextMessageEventContent::html(
+ "Hello, World!",
+ "Hello, World!",
+ ));
+
+ assert_eq!(
+ to_json_value(&message_event_content).unwrap(),
+ json!({
+ "body": "Hello, World!",
+ "msgtype": "m.text",
+ "format": "org.matrix.custom.html",
+ "formatted_body": "Hello, World!",
+ })
+ );
+}
+
+#[test]
+fn plain_text_content_serialization() {
+ let message_event_content = MessageEventContent::Text(TextMessageEventContent::plain(
+ "> <@test:example.com> test\n\ntest reply",
+ ));
+
+ assert_eq!(
+ to_json_value(&message_event_content).unwrap(),
+ json!({
+ "body": "> <@test:example.com> test\n\ntest reply",
+ "msgtype": "m.text"
+ })
+ );
+}
+
+#[test]
+fn relates_to_content_serialization() {
+ let message_event_content = MessageEventContent::Text(
+ assign!(TextMessageEventContent::plain("> <@test:example.com> test\n\ntest reply"), {
+ relates_to: Some(Relation::Reply {
+ in_reply_to: InReplyTo { event_id: event_id!("$15827405538098VGFWH:example.com") },
+ }),
+ }),
+ );
+
+ let json_data = json!({
+ "body": "> <@test:example.com> test\n\ntest reply",
+ "msgtype": "m.text",
+ "m.relates_to": {
+ "m.in_reply_to": {
+ "event_id": "$15827405538098VGFWH:example.com"
+ }
+ }
+ });
+
+ assert_eq!(to_json_value(&message_event_content).unwrap(), json_data);
+}
+
+#[test]
+#[cfg(not(feature = "unstable-pre-spec"))]
+fn edit_deserialization_061() {
+ let json_data = json!({
+ "body": "s/foo/bar",
+ "msgtype": "m.text",
+ "m.relates_to": {
+ "rel_type": "m.replace",
+ "event_id": event_id!("$1598361704261elfgc:localhost"),
+ },
+ "m.new_content": {
+ "body": "bar",
+ },
+ });
+
+ assert_matches!(
+ from_json_value::(json_data).unwrap(),
+ MessageEventContent::Text(TextMessageEventContent {
+ body,
+ formatted: None,
+ relates_to: Some(Relation::Custom(_)),
+ ..
+ }) if body == "s/foo/bar"
+ );
+}
+
+#[test]
+#[cfg(feature = "unstable-pre-spec")]
+fn edit_deserialization_future() {
+ use crate::room::relationships::Replacement;
+
+ let ev_id = event_id!("$1598361704261elfgc:localhost");
+ let json_data = json!({
+ "body": "s/foo/bar",
+ "msgtype": "m.text",
+ "m.relates_to": {
+ "rel_type": "m.replace",
+ "event_id": ev_id,
+ },
+ "m.new_content": {
+ "body": "bar",
+ "msgtype": "m.text",
+ },
+ });
+
+ assert_matches!(
+ from_json_value::(json_data).unwrap(),
+ MessageEventContent::Text(TextMessageEventContent {
+ body,
+ formatted: None,
+ relates_to: Some(Relation::Replacement(Replacement { event_id })),
+ new_content: Some(new_content),
+ }) if body == "s/foo/bar"
+ && event_id == ev_id
+ && matches!(
+ &*new_content,
+ MessageEventContent::Text(TextMessageEventContent {
+ body,
+ formatted: None,
+ relates_to: None,
+ new_content: None,
+ }) if body == "bar"
+ )
+ );
+}
+
+#[test]
+#[cfg(feature = "unstable-pre-spec")]
+fn verification_request_deserialization() {
+ let user_id = user_id!("@example2:localhost");
+ let device_id: DeviceIdBox = "XOWLHHFSWM".into();
+
+ let json_data = json!({
+ "body": "@example:localhost is requesting to verify your key, ...",
+ "msgtype": "m.key.verification.request",
+ "to": user_id,
+ "from_device": device_id,
+ "methods": [
+ "m.sas.v1",
+ "m.qr_code.show.v1",
+ "m.reciprocate.v1"
+ ]
+ });
+
+ assert_matches!(
+ from_json_value::(json_data).unwrap(),
+ MessageEventContent::VerificationRequest(KeyVerificationRequestEventContent {
+ body,
+ to,
+ from_device,
+ methods
+ }) if body == "@example:localhost is requesting to verify your key, ..."
+ && to == user_id
+ && from_device == device_id
+ && methods.contains(&VerificationMethod::MSasV1)
+ );
+}
+
+#[test]
+#[cfg(feature = "unstable-pre-spec")]
+fn verification_request_serialization() {
+ let user_id = user_id!("@example2:localhost");
+ let device_id: DeviceIdBox = "XOWLHHFSWM".into();
+ let body = "@example:localhost is requesting to verify your key, ...".to_string();
+
+ let methods = vec![
+ VerificationMethod::MSasV1,
+ VerificationMethod::_Custom("m.qr_code.show.v1".to_string()),
+ VerificationMethod::_Custom("m.reciprocate.v1".to_string()),
+ ];
+
+ let json_data = json!({
+ "body": body,
+ "msgtype": "m.key.verification.request",
+ "to": user_id,
+ "from_device": device_id,
+ "methods": methods
+ });
+
+ let content = MessageEventContent::VerificationRequest(KeyVerificationRequestEventContent {
+ to: user_id,
+ from_device: device_id,
+ body,
+ methods,
+ });
+
+ assert_eq!(to_json_value(&content).unwrap(), json_data,);
+}
+
+#[test]
+fn content_deserialization() {
+ let json_data = json!({
+ "body": "test",
+ "msgtype": "m.audio",
+ "url": "http://example.com/audio.mp3"
+ });
+
+ assert_matches!(
+ from_json_value::>(json_data)
+ .unwrap()
+ .deserialize()
+ .unwrap(),
+ MessageEventContent::Audio(AudioMessageEventContent {
+ body,
+ info: None,
+ url: Some(url),
+ file: None,
+ }) if body == "test" && url == "http://example.com/audio.mp3"
+ );
+}
+
+#[test]
+fn content_deserialization_failure() {
+ let json_data = json!({
+ "body": "test","msgtype": "m.location",
+ "url": "http://example.com/audio.mp3"
+ });
+ assert!(from_json_value::>(json_data).unwrap().deserialize().is_err());
+}