More tests for ruma-events-macros

This commit is contained in:
Donough Liu 2020-03-11 14:36:18 +08:00
parent 12d725113b
commit e60e502183

View File

@ -1,11 +1,14 @@
use std::{ use std::{
borrow::Cow, borrow::Cow,
convert::Infallible, convert::{Infallible, TryFrom},
fmt::{Debug, Display, Formatter, Result as FmtResult}, fmt::{Debug, Display, Formatter, Result as FmtResult},
}; };
use js_int::UInt;
use ruma_events_macros::ruma_event;
use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId};
use serde::{de::DeserializeOwned, Deserialize, Deserializer, Serialize}; use serde::{de::DeserializeOwned, Deserialize, Deserializer, Serialize};
use serde_json::Value; use serde_json::{json, Value};
/// The type of an event. /// The type of an event.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
@ -232,16 +235,23 @@ impl Display for InvalidEvent {
} }
} }
pub fn serde_json_eq_try_from_raw<T>(de: T, se: serde_json::Value)
where
T: Clone + Debug + PartialEq + Serialize + TryFromRaw,
{
assert_eq!(se, serde_json::to_value(de.clone()).unwrap());
assert_eq!(
de,
serde_json::from_value::<EventResult<_>>(se)
.unwrap()
.into_result()
.unwrap()
);
}
// See note about wrapping macro expansion in a module from `src/lib.rs` // See note about wrapping macro expansion in a module from `src/lib.rs`
pub mod common_case { mod common_case {
use std::convert::TryFrom; use super::*;
use js_int::UInt;
use ruma_events_macros::ruma_event;
use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId};
use serde_json::Value;
use super::EventResult;
ruma_event! { ruma_event! {
/// Informs the room about what room aliases it has been given. /// Informs the room about what room aliases it has been given.
@ -256,7 +266,7 @@ pub mod common_case {
} }
#[test] #[test]
fn serialization_with_optional_fields_as_none() { fn optional_fields_as_none() {
let event = AliasesEvent { let event = AliasesEvent {
content: AliasesEventContent { content: AliasesEventContent {
aliases: Vec::with_capacity(0), aliases: Vec::with_capacity(0),
@ -269,15 +279,21 @@ pub mod common_case {
state_key: "example.com".to_string(), state_key: "example.com".to_string(),
unsigned: None, unsigned: None,
}; };
let json = json!({
let actual = serde_json::to_string(&event).unwrap(); "content": {
let expected = r#"{"content":{"aliases":[]},"event_id":"$h29iv0s8:example.com","origin_server_ts":1,"sender":"@carl:example.com","state_key":"example.com","type":"m.room.aliases"}"#; "aliases": []
},
assert_eq!(actual, expected); "event_id": "$h29iv0s8:example.com",
"origin_server_ts": 1,
"sender": "@carl:example.com",
"state_key": "example.com",
"type": "m.room.aliases"
});
serde_json_eq_try_from_raw(event, json);
} }
#[test] #[test]
fn serialization_with_some_optional_fields_as_some() { fn some_optional_fields_as_some() {
let event = AliasesEvent { let event = AliasesEvent {
content: AliasesEventContent { content: AliasesEventContent {
aliases: vec![RoomAliasId::try_from("#room:example.org").unwrap()], aliases: vec![RoomAliasId::try_from("#room:example.org").unwrap()],
@ -292,15 +308,25 @@ pub mod common_case {
state_key: "example.com".to_string(), state_key: "example.com".to_string(),
unsigned: None, unsigned: None,
}; };
let json = json!({
let actual = serde_json::to_string(&event).unwrap(); "content": {
let expected = r##"{"content":{"aliases":["#room:example.org"]},"event_id":"$h29iv0s8:example.com","origin_server_ts":1,"prev_content":{"aliases":[]},"room_id":"!n8f893n9:example.com","sender":"@carl:example.com","state_key":"example.com","type":"m.room.aliases"}"##; "aliases": ["#room:example.org"]
},
assert_eq!(actual, expected); "event_id": "$h29iv0s8:example.com",
"origin_server_ts": 1,
"prev_content": {
"aliases": []
},
"room_id": "!n8f893n9:example.com",
"sender": "@carl:example.com",
"state_key": "example.com",
"type": "m.room.aliases"
});
serde_json_eq_try_from_raw(event, json);
} }
#[test] #[test]
fn serialization_with_all_optional_fields_as_some() { fn all_optional_fields_as_some() {
let event = AliasesEvent { let event = AliasesEvent {
content: AliasesEventContent { content: AliasesEventContent {
aliases: vec![RoomAliasId::try_from("#room:example.org").unwrap()], aliases: vec![RoomAliasId::try_from("#room:example.org").unwrap()],
@ -315,42 +341,29 @@ pub mod common_case {
state_key: "example.com".to_string(), state_key: "example.com".to_string(),
unsigned: Some(serde_json::from_str::<Value>(r#"{"foo":"bar"}"#).unwrap()), unsigned: Some(serde_json::from_str::<Value>(r#"{"foo":"bar"}"#).unwrap()),
}; };
let json = json!({
let actual = serde_json::to_string(&event).unwrap(); "content": {
let expected = r##"{"content":{"aliases":["#room:example.org"]},"event_id":"$h29iv0s8:example.com","origin_server_ts":1,"prev_content":{"aliases":[]},"room_id":"!n8f893n9:example.com","sender":"@carl:example.com","state_key":"example.com","unsigned":{"foo":"bar"},"type":"m.room.aliases"}"##; "aliases": ["#room:example.org"]
assert_eq!(actual, expected);
}
#[test]
fn deserialization() {
let json = r##"{"content":{"aliases":["#room:example.org"]},"event_id":"$h29iv0s8:example.com","origin_server_ts":1,"prev_content":{"aliases":[]},"room_id":"!n8f893n9:example.com","sender":"@carl:example.com","state_key":"example.com","unsigned":{"foo":"bar"},"type":"m.room.aliases"}"##;
let event_result: EventResult<AliasesEvent> = serde_json::from_str(json).unwrap();
let actual: AliasesEvent = event_result.into_result().unwrap();
let expected = AliasesEvent {
content: AliasesEventContent {
aliases: vec![RoomAliasId::try_from("#room:example.org").unwrap()],
}, },
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), "event_id": "$h29iv0s8:example.com",
origin_server_ts: UInt::try_from(1).unwrap(), "origin_server_ts": 1,
prev_content: Some(AliasesEventContent { "prev_content": {
aliases: Vec::with_capacity(0), "aliases": []
}), },
room_id: Some(RoomId::try_from("!n8f893n9:example.com").unwrap()), "room_id": "!n8f893n9:example.com",
sender: UserId::try_from("@carl:example.com").unwrap(), "sender": "@carl:example.com",
state_key: "example.com".to_string(), "state_key": "example.com",
unsigned: Some(serde_json::from_str::<Value>(r#"{"foo":"bar"}"#).unwrap()), "unsigned": {
}; "foo": "bar"
},
assert_eq!(actual, expected); "type": "m.room.aliases"
});
serde_json_eq_try_from_raw(event, json);
} }
} }
pub mod custom_event_type { mod custom_event_type {
use ruma_events_macros::ruma_event; use super::*;
use serde_json::Value;
ruma_event! { ruma_event! {
/// A custom event. /// A custom event.
@ -363,10 +376,43 @@ pub mod custom_event_type {
}, },
} }
} }
#[test]
fn value_is_not_null() {
// Hint: serde_json::Value with default feature is sort
// alphabetically rather than preserve the sequence of json kv
// pairs. Check:
// + https://github.com/serde-rs/json/pull/80
// + https://github.com/serde-rs/json/blob/17d9a5ea9b8e11f01b0fcf13933c4a12d3f8db45/tests/map.rs.
let event = CustomEvent {
content: { serde_json::from_str::<Value>(r#"{"alice":["foo", "bar"]}"#).unwrap() },
event_type: "foo.bar".to_owned(),
};
let json = json!({
"content": {
"alice": ["foo", "bar"]
},
"type": "foo.bar"
});
serde_json_eq_try_from_raw(event, json);
}
#[test]
fn value_is_null() {
let event = CustomEvent {
content: { Value::Null },
event_type: "foo.bar".to_owned(),
};
let json = json!({
"content": null,
"type": "foo.bar"
});
serde_json_eq_try_from_raw(event, json);
}
} }
pub mod extra_fields { mod extra_fields {
use ruma_events_macros::ruma_event; use super::*;
ruma_event! { ruma_event! {
/// A redaction of an event. /// A redaction of an event.
@ -383,10 +429,38 @@ pub mod extra_fields {
}, },
} }
} }
#[test]
fn field_serialization_deserialization() {
let event = RedactionEvent {
content: RedactionEventContent { reason: None },
redacts: EventId::try_from("$h29iv0s8:example.com").unwrap(),
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
origin_server_ts: UInt::try_from(1).unwrap(),
room_id: Some(RoomId::try_from("!n8f893n9:example.com").unwrap()),
sender: UserId::try_from("@carl:example.com").unwrap(),
unsigned: Some(serde_json::from_str::<Value>(r#"{"foo":"bar"}"#).unwrap()),
};
let json = json!({
"content": {
"reason": null
},
"event_id": "$h29iv0s8:example.com",
"origin_server_ts": 1,
"redacts": "$h29iv0s8:example.com",
"room_id": "!n8f893n9:example.com",
"sender": "@carl:example.com",
"unsigned": {
"foo": "bar"
},
"type": "m.room.redaction"
});
serde_json_eq_try_from_raw(event, json);
}
} }
pub mod type_alias { mod type_alias {
use ruma_events_macros::ruma_event; use super::*;
ruma_event! { ruma_event! {
/// Informs the client about the rooms that are considered direct by a user. /// Informs the client about the rooms that are considered direct by a user.
@ -402,4 +476,34 @@ pub mod type_alias {
} }
} }
} }
#[test]
fn alias_is_not_empty() {
let content = vec![(
UserId::try_from("@bob:example.com").unwrap(),
vec![RoomId::try_from("!n8f893n9:example.com").unwrap()],
)]
.into_iter()
.collect();
let event = DirectEvent { content };
let json = json!({
"content": {
"@bob:example.com": ["!n8f893n9:example.com"]
},
"type": "m.direct"
});
serde_json_eq_try_from_raw(event, json);
}
#[test]
fn alias_empty() {
let content = Default::default();
let event = DirectEvent { content };
let json = json!({
"content": {},
"type": "m.direct"
});
serde_json_eq_try_from_raw(event, json);
}
} }