Skip to content

Commit 2090117

Browse files
committed
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 8be0a7d commit 2090117

File tree

6 files changed

+81
-75
lines changed

6 files changed

+81
-75
lines changed

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

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

735735
let mut sync_builder = SyncResponseBuilder::new();
736-
let mut power_level_event = StateTestEvent::PowerLevels.into_json_value();
736+
let mut power_level_event: Value = StateTestEvent::PowerLevels.into();
737737
// Allow noone to send room notify events.
738738
*power_level_event.get_mut("content").unwrap().get_mut("notifications").unwrap() =
739739
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: 72 additions & 66 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

@@ -96,19 +102,19 @@ pub enum RoomAccountDataTestEvent {
96102
Custom(JsonValue),
97103
}
98104

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

109-
/// Get the typed JSON representation of this test event.
110-
pub fn into_raw_event(self) -> Raw<AnyRoomAccountDataEvent> {
111-
from_json_value(self.into_json_value()).unwrap()
115+
impl From<RoomAccountDataTestEvent> for Raw<AnyRoomAccountDataEvent> {
116+
fn from(val: RoomAccountDataTestEvent) -> Self {
117+
from_json_value(val.into()).unwrap()
112118
}
113119
}
114120

@@ -118,18 +124,18 @@ pub enum PresenceTestEvent {
118124
Custom(JsonValue),
119125
}
120126

121-
impl PresenceTestEvent {
122-
/// Get the JSON representation of this test event.
123-
pub fn into_json_value(self) -> JsonValue {
124-
match self {
125-
Self::Presence => test_json::sync_events::PRESENCE.to_owned(),
126-
Self::Custom(json) => json,
127+
impl From<PresenceTestEvent> for JsonValue {
128+
fn from(val: PresenceTestEvent) -> Self {
129+
match val {
130+
PresenceTestEvent::Presence => test_json::sync_events::PRESENCE.to_owned(),
131+
PresenceTestEvent::Custom(json) => json,
127132
}
128133
}
134+
}
129135

130-
/// Get the typed JSON representation of this test event.
131-
pub fn into_raw_event(self) -> Raw<PresenceEvent> {
132-
from_json_value(self.into_json_value()).unwrap()
136+
impl From<PresenceTestEvent> for Raw<PresenceEvent> {
137+
fn from(val: PresenceTestEvent) -> Self {
138+
from_json_value(val.into()).unwrap()
133139
}
134140
}
135141

@@ -140,18 +146,18 @@ pub enum GlobalAccountDataTestEvent {
140146
Custom(JsonValue),
141147
}
142148

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

153-
/// Get the typed JSON representation of this test event.
154-
pub fn into_raw_event(self) -> Raw<AnyGlobalAccountDataEvent> {
155-
from_json_value(self.into_json_value()).unwrap()
159+
impl From<GlobalAccountDataTestEvent> for Raw<AnyGlobalAccountDataEvent> {
160+
fn from(val: GlobalAccountDataTestEvent) -> Self {
161+
from_json_value(val.into()).unwrap()
156162
}
157163
}

0 commit comments

Comments
 (0)