Skip to content

Commit 6f683d3

Browse files
robintownpoljar
authored andcommitted
refactor(tests): Accept more types for sync builder state events
Refactoring the test event implementation to use the From trait rather than ad-hoc methods along the way.
1 parent 9242d18 commit 6f683d3

File tree

6 files changed

+84
-76
lines changed

6 files changed

+84
-76
lines changed

crates/matrix-sdk/tests/integration/room/joined.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ async fn test_call_notifications_dont_notify_room_without_mention_powerlevel() {
878878
let (client, server) = logged_in_client_with_server().await;
879879

880880
let mut sync_builder = SyncResponseBuilder::new();
881-
let mut power_level_event = StateTestEvent::PowerLevels.into_json_value();
881+
let mut power_level_event: Value = StateTestEvent::PowerLevels.into();
882882
// Allow noone to send room notify events.
883883
*power_level_event.get_mut("content").unwrap().get_mut("notifications").unwrap() =
884884
json!({"room": 101});

testing/matrix-sdk-test/src/sync_builder/invited_room.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl InvitedRoomBuilder {
2727

2828
/// Add an event to the state.
2929
pub fn add_state_event(mut self, event: StrippedStateTestEvent) -> Self {
30-
self.inner.invite_state.events.push(event.into_raw_event());
30+
self.inner.invite_state.events.push(event.into());
3131
self
3232
}
3333

testing/matrix-sdk-test/src/sync_builder/joined_room.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use ruma::{
99
};
1010
use serde_json::{from_value as from_json_value, Value as JsonValue};
1111

12-
use super::{RoomAccountDataTestEvent, StateTestEvent};
12+
use super::RoomAccountDataTestEvent;
1313
use crate::{event_factory::EventBuilder, DEFAULT_TEST_ROOM_ID};
1414

1515
pub struct JoinedRoomBuilder {
@@ -74,8 +74,8 @@ impl JoinedRoomBuilder {
7474
}
7575

7676
/// Add an event to the state.
77-
pub fn add_state_event(mut self, event: StateTestEvent) -> Self {
78-
self.inner.state.events.push(event.into_raw_event());
77+
pub fn add_state_event(mut self, event: impl Into<Raw<AnySyncStateEvent>>) -> Self {
78+
self.inner.state.events.push(event.into());
7979
self
8080
}
8181

@@ -102,7 +102,7 @@ impl JoinedRoomBuilder {
102102

103103
/// Add room account data.
104104
pub fn add_account_data(mut self, event: RoomAccountDataTestEvent) -> Self {
105-
self.inner.account_data.events.push(event.into_raw_event());
105+
self.inner.account_data.events.push(event.into());
106106
self
107107
}
108108

testing/matrix-sdk-test/src/sync_builder/knocked_room.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl KnockedRoomBuilder {
2727

2828
/// Add an event to the state.
2929
pub fn add_state_event(mut self, event: StrippedStateTestEvent) -> Self {
30-
self.inner.knock_state.events.push(event.into_raw_event());
30+
self.inner.knock_state.events.push(event.into());
3131
self
3232
}
3333

testing/matrix-sdk-test/src/sync_builder/left_room.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl LeftRoomBuilder {
7171

7272
/// Add an event to the state.
7373
pub fn add_state_event(mut self, event: StateTestEvent) -> Self {
74-
self.inner.state.events.push(event.into_raw_event());
74+
self.inner.state.events.push(event.into());
7575
self
7676
}
7777

@@ -86,7 +86,7 @@ impl LeftRoomBuilder {
8686

8787
/// Add room account data.
8888
pub fn add_account_data(mut self, event: RoomAccountDataTestEvent) -> Self {
89-
self.inner.account_data.events.push(event.into_raw_event());
89+
self.inner.account_data.events.push(event.into());
9090
self
9191
}
9292

testing/matrix-sdk-test/src/sync_builder/test_event.rs

Lines changed: 75 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,42 @@ pub enum StateTestEvent {
3333
Custom(JsonValue),
3434
}
3535

36-
impl StateTestEvent {
37-
/// Get the JSON representation of this test event.
38-
pub fn into_json_value(self) -> JsonValue {
39-
match self {
40-
Self::Alias => test_json::sync_events::ALIAS.to_owned(),
41-
Self::Aliases => test_json::sync_events::ALIASES.to_owned(),
42-
Self::Create => test_json::sync_events::CREATE.to_owned(),
43-
Self::Encryption => test_json::sync_events::ENCRYPTION.to_owned(),
44-
Self::HistoryVisibility => test_json::sync_events::HISTORY_VISIBILITY.to_owned(),
45-
Self::JoinRules => test_json::sync_events::JOIN_RULES.to_owned(),
46-
Self::Member => test_json::sync_events::MEMBER.to_owned(),
47-
Self::MemberAdditional => test_json::sync_events::MEMBER_ADDITIONAL.to_owned(),
48-
Self::MemberBan => test_json::sync_events::MEMBER_BAN.to_owned(),
49-
Self::MemberInvite => test_json::sync_events::MEMBER_INVITE.to_owned(),
50-
Self::MemberLeave => test_json::sync_events::MEMBER_LEAVE.to_owned(),
51-
Self::MemberNameChange => test_json::sync_events::MEMBER_NAME_CHANGE.to_owned(),
52-
Self::PowerLevels => test_json::sync_events::POWER_LEVELS.to_owned(),
53-
Self::RedactedInvalid => test_json::sync_events::REDACTED_INVALID.to_owned(),
54-
Self::RedactedState => test_json::sync_events::REDACTED_STATE.to_owned(),
55-
Self::RoomAvatar => test_json::sync_events::ROOM_AVATAR.to_owned(),
56-
Self::RoomName => test_json::sync_events::NAME.to_owned(),
57-
Self::RoomPinnedEvents => test_json::sync_events::PINNED_EVENTS.to_owned(),
58-
Self::RoomTopic => test_json::sync_events::TOPIC.to_owned(),
59-
Self::Custom(json) => json,
36+
impl From<StateTestEvent> for JsonValue {
37+
fn from(val: StateTestEvent) -> Self {
38+
match val {
39+
StateTestEvent::Alias => test_json::sync_events::ALIAS.to_owned(),
40+
StateTestEvent::Aliases => test_json::sync_events::ALIASES.to_owned(),
41+
StateTestEvent::Create => test_json::sync_events::CREATE.to_owned(),
42+
StateTestEvent::Encryption => test_json::sync_events::ENCRYPTION.to_owned(),
43+
StateTestEvent::HistoryVisibility => {
44+
test_json::sync_events::HISTORY_VISIBILITY.to_owned()
45+
}
46+
StateTestEvent::JoinRules => test_json::sync_events::JOIN_RULES.to_owned(),
47+
StateTestEvent::Member => test_json::sync_events::MEMBER.to_owned(),
48+
StateTestEvent::MemberAdditional => {
49+
test_json::sync_events::MEMBER_ADDITIONAL.to_owned()
50+
}
51+
StateTestEvent::MemberBan => test_json::sync_events::MEMBER_BAN.to_owned(),
52+
StateTestEvent::MemberInvite => test_json::sync_events::MEMBER_INVITE.to_owned(),
53+
StateTestEvent::MemberLeave => test_json::sync_events::MEMBER_LEAVE.to_owned(),
54+
StateTestEvent::MemberNameChange => {
55+
test_json::sync_events::MEMBER_NAME_CHANGE.to_owned()
56+
}
57+
StateTestEvent::PowerLevels => test_json::sync_events::POWER_LEVELS.to_owned(),
58+
StateTestEvent::RedactedInvalid => test_json::sync_events::REDACTED_INVALID.to_owned(),
59+
StateTestEvent::RedactedState => test_json::sync_events::REDACTED_STATE.to_owned(),
60+
StateTestEvent::RoomAvatar => test_json::sync_events::ROOM_AVATAR.to_owned(),
61+
StateTestEvent::RoomName => test_json::sync_events::NAME.to_owned(),
62+
StateTestEvent::RoomPinnedEvents => test_json::sync_events::PINNED_EVENTS.to_owned(),
63+
StateTestEvent::RoomTopic => test_json::sync_events::TOPIC.to_owned(),
64+
StateTestEvent::Custom(json) => json,
6065
}
6166
}
67+
}
6268

63-
/// Get the typed JSON representation of this test event.
64-
pub fn into_raw_event(self) -> Raw<AnySyncStateEvent> {
65-
from_json_value(self.into_json_value()).unwrap()
69+
impl From<StateTestEvent> for Raw<AnySyncStateEvent> {
70+
fn from(val: StateTestEvent) -> Self {
71+
from_json_value(val.into()).unwrap()
6672
}
6773
}
6874

@@ -73,19 +79,19 @@ pub enum StrippedStateTestEvent {
7379
Custom(JsonValue),
7480
}
7581

76-
impl StrippedStateTestEvent {
77-
/// Get the JSON representation of this test event.
78-
pub fn into_json_value(self) -> JsonValue {
79-
match self {
80-
Self::Member => test_json::sync_events::MEMBER_STRIPPED.to_owned(),
81-
Self::RoomName => test_json::sync_events::NAME_STRIPPED.to_owned(),
82-
Self::Custom(json) => json,
82+
impl From<StrippedStateTestEvent> for JsonValue {
83+
fn from(val: StrippedStateTestEvent) -> Self {
84+
match val {
85+
StrippedStateTestEvent::Member => test_json::sync_events::MEMBER_STRIPPED.to_owned(),
86+
StrippedStateTestEvent::RoomName => test_json::sync_events::NAME_STRIPPED.to_owned(),
87+
StrippedStateTestEvent::Custom(json) => json,
8388
}
8489
}
90+
}
8591

86-
/// Get the typed JSON representation of this test event.
87-
pub fn into_raw_event(self) -> Raw<AnyStrippedStateEvent> {
88-
from_json_value(self.into_json_value()).unwrap()
92+
impl From<StrippedStateTestEvent> for Raw<AnyStrippedStateEvent> {
93+
fn from(val: StrippedStateTestEvent) -> Self {
94+
from_json_value(val.into()).unwrap()
8995
}
9096
}
9197

@@ -97,20 +103,22 @@ pub enum RoomAccountDataTestEvent {
97103
Custom(JsonValue),
98104
}
99105

100-
impl RoomAccountDataTestEvent {
101-
/// Get the JSON representation of this test event.
102-
pub fn into_json_value(self) -> JsonValue {
103-
match self {
104-
Self::FullyRead => test_json::sync_events::FULLY_READ.to_owned(),
105-
Self::Tags => test_json::sync_events::TAG.to_owned(),
106-
Self::MarkedUnread => test_json::sync_events::MARKED_UNREAD.to_owned(),
107-
Self::Custom(json) => json,
106+
impl From<RoomAccountDataTestEvent> for JsonValue {
107+
fn from(val: RoomAccountDataTestEvent) -> Self {
108+
match val {
109+
RoomAccountDataTestEvent::FullyRead => test_json::sync_events::FULLY_READ.to_owned(),
110+
RoomAccountDataTestEvent::Tags => test_json::sync_events::TAG.to_owned(),
111+
RoomAccountDataTestEvent::MarkedUnread => {
112+
test_json::sync_events::MARKED_UNREAD.to_owned()
113+
}
114+
RoomAccountDataTestEvent::Custom(json) => json,
108115
}
109116
}
117+
}
110118

111-
/// Get the typed JSON representation of this test event.
112-
pub fn into_raw_event(self) -> Raw<AnyRoomAccountDataEvent> {
113-
from_json_value(self.into_json_value()).unwrap()
119+
impl From<RoomAccountDataTestEvent> for Raw<AnyRoomAccountDataEvent> {
120+
fn from(val: RoomAccountDataTestEvent) -> Self {
121+
from_json_value(val.into()).unwrap()
114122
}
115123
}
116124

@@ -120,18 +128,18 @@ pub enum PresenceTestEvent {
120128
Custom(JsonValue),
121129
}
122130

123-
impl PresenceTestEvent {
124-
/// Get the JSON representation of this test event.
125-
pub fn into_json_value(self) -> JsonValue {
126-
match self {
127-
Self::Presence => test_json::sync_events::PRESENCE.to_owned(),
128-
Self::Custom(json) => json,
131+
impl From<PresenceTestEvent> for JsonValue {
132+
fn from(val: PresenceTestEvent) -> Self {
133+
match val {
134+
PresenceTestEvent::Presence => test_json::sync_events::PRESENCE.to_owned(),
135+
PresenceTestEvent::Custom(json) => json,
129136
}
130137
}
138+
}
131139

132-
/// Get the typed JSON representation of this test event.
133-
pub fn into_raw_event(self) -> Raw<PresenceEvent> {
134-
from_json_value(self.into_json_value()).unwrap()
140+
impl From<PresenceTestEvent> for Raw<PresenceEvent> {
141+
fn from(val: PresenceTestEvent) -> Self {
142+
from_json_value(val.into()).unwrap()
135143
}
136144
}
137145

@@ -142,18 +150,18 @@ pub enum GlobalAccountDataTestEvent {
142150
Custom(JsonValue),
143151
}
144152

145-
impl GlobalAccountDataTestEvent {
146-
/// Get the JSON representation of this test event.
147-
pub fn into_json_value(self) -> JsonValue {
148-
match self {
149-
Self::Direct => test_json::sync_events::DIRECT.to_owned(),
150-
Self::PushRules => test_json::sync_events::PUSH_RULES.to_owned(),
151-
Self::Custom(json) => json,
153+
impl From<GlobalAccountDataTestEvent> for JsonValue {
154+
fn from(val: GlobalAccountDataTestEvent) -> Self {
155+
match val {
156+
GlobalAccountDataTestEvent::Direct => test_json::sync_events::DIRECT.to_owned(),
157+
GlobalAccountDataTestEvent::PushRules => test_json::sync_events::PUSH_RULES.to_owned(),
158+
GlobalAccountDataTestEvent::Custom(json) => json,
152159
}
153160
}
161+
}
154162

155-
/// Get the typed JSON representation of this test event.
156-
pub fn into_raw_event(self) -> Raw<AnyGlobalAccountDataEvent> {
157-
from_json_value(self.into_json_value()).unwrap()
163+
impl From<GlobalAccountDataTestEvent> for Raw<AnyGlobalAccountDataEvent> {
164+
fn from(val: GlobalAccountDataTestEvent) -> Self {
165+
from_json_value(val.into()).unwrap()
158166
}
159167
}

0 commit comments

Comments
 (0)