Skip to content

Commit 5442cf5

Browse files
LeinnanBD103
authored andcommitted
BRP serialization tests (#16724)
# Objective Start work on tests in BRP. ## Solution - Adds serialization tests to BRP types
1 parent 64ce194 commit 5442cf5

File tree

1 file changed

+52
-15
lines changed

1 file changed

+52
-15
lines changed

crates/bevy_remote/src/builtin_methods.rs

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub const BRP_LIST_AND_WATCH_METHOD: &str = "bevy/list+watch";
5858
/// ID.
5959
///
6060
/// The server responds with a [`BrpGetResponse`].
61-
#[derive(Debug, Serialize, Deserialize, Clone)]
61+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
6262
pub struct BrpGetParams {
6363
/// The ID of the entity from which components are to be requested.
6464
pub entity: Entity,
@@ -83,7 +83,7 @@ pub struct BrpGetParams {
8383
/// and component values that match.
8484
///
8585
/// The server responds with a [`BrpQueryResponse`].
86-
#[derive(Debug, Serialize, Deserialize, Clone)]
86+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
8787
pub struct BrpQueryParams {
8888
/// The components to select.
8989
pub data: BrpQuery,
@@ -98,7 +98,7 @@ pub struct BrpQueryParams {
9898
/// with its ID.
9999
///
100100
/// The server responds with a [`BrpSpawnResponse`].
101-
#[derive(Debug, Serialize, Deserialize, Clone)]
101+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
102102
pub struct BrpSpawnParams {
103103
/// A map from each component's full path to its serialized value.
104104
///
@@ -115,7 +115,7 @@ pub struct BrpSpawnParams {
115115
/// `bevy/destroy`: Given an ID, despawns the entity with that ID.
116116
///
117117
/// The server responds with an okay.
118-
#[derive(Debug, Serialize, Deserialize, Clone)]
118+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
119119
pub struct BrpDestroyParams {
120120
/// The ID of the entity to despawn.
121121
pub entity: Entity,
@@ -124,7 +124,7 @@ pub struct BrpDestroyParams {
124124
/// `bevy/remove`: Deletes one or more components from an entity.
125125
///
126126
/// The server responds with a null.
127-
#[derive(Debug, Serialize, Deserialize, Clone)]
127+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
128128
pub struct BrpRemoveParams {
129129
/// The ID of the entity from which components are to be removed.
130130
pub entity: Entity,
@@ -143,7 +143,7 @@ pub struct BrpRemoveParams {
143143
/// `bevy/insert`: Adds one or more components to an entity.
144144
///
145145
/// The server responds with a null.
146-
#[derive(Debug, Serialize, Deserialize, Clone)]
146+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
147147
pub struct BrpInsertParams {
148148
/// The ID of the entity that components are to be added to.
149149
pub entity: Entity,
@@ -163,7 +163,7 @@ pub struct BrpInsertParams {
163163
/// `bevy/reparent`: Assign a new parent to one or more entities.
164164
///
165165
/// The server responds with a null.
166-
#[derive(Debug, Serialize, Deserialize, Clone)]
166+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
167167
pub struct BrpReparentParams {
168168
/// The IDs of the entities that are to become the new children of the
169169
/// `parent`.
@@ -181,14 +181,14 @@ pub struct BrpReparentParams {
181181
/// system (no params provided), or those on an entity (params provided).
182182
///
183183
/// The server responds with a [`BrpListResponse`]
184-
#[derive(Debug, Serialize, Deserialize, Clone)]
184+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
185185
pub struct BrpListParams {
186186
/// The entity to query.
187187
pub entity: Entity,
188188
}
189189

190190
/// Describes the data that is to be fetched in a query.
191-
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
191+
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
192192
pub struct BrpQuery {
193193
/// The [full path] of the type name of each component that is to be
194194
/// fetched.
@@ -214,7 +214,7 @@ pub struct BrpQuery {
214214

215215
/// Additional constraints that can be placed on a query to include or exclude
216216
/// certain entities.
217-
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
217+
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
218218
pub struct BrpQueryFilter {
219219
/// The [full path] of the type name of each component that must not be
220220
/// present on the entity for it to be included in the results.
@@ -234,14 +234,14 @@ pub struct BrpQueryFilter {
234234
/// A response from the world to the client that specifies a single entity.
235235
///
236236
/// This is sent in response to `bevy/spawn`.
237-
#[derive(Debug, Serialize, Deserialize, Clone)]
237+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
238238
pub struct BrpSpawnResponse {
239239
/// The ID of the entity in question.
240240
pub entity: Entity,
241241
}
242242

243243
/// The response to a `bevy/get` request.
244-
#[derive(Debug, Serialize, Deserialize, Clone)]
244+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
245245
#[serde(untagged)]
246246
pub enum BrpGetResponse {
247247
/// The non-strict response that reports errors separately without failing the entire request.
@@ -257,7 +257,7 @@ pub enum BrpGetResponse {
257257
}
258258

259259
/// A single response from a `bevy/get+watch` request.
260-
#[derive(Debug, Serialize, Deserialize, Clone)]
260+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
261261
#[serde(untagged)]
262262
pub enum BrpGetWatchingResponse {
263263
/// The non-strict response that reports errors separately without failing the entire request.
@@ -285,7 +285,7 @@ pub enum BrpGetWatchingResponse {
285285
pub type BrpListResponse = Vec<String>;
286286

287287
/// A single response from a `bevy/list+watch` request.
288-
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
288+
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
289289
pub struct BrpListWatchingResponse {
290290
added: Vec<String>,
291291
removed: Vec<String>,
@@ -295,7 +295,7 @@ pub struct BrpListWatchingResponse {
295295
pub type BrpQueryResponse = Vec<BrpQueryRow>;
296296

297297
/// One query match result: a single entity paired with the requested components.
298-
#[derive(Debug, Serialize, Deserialize, Clone)]
298+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
299299
pub struct BrpQueryRow {
300300
/// The ID of the entity that matched.
301301
pub entity: Entity,
@@ -969,3 +969,40 @@ fn get_component_type_registration<'r>(
969969
.get_with_type_path(component_path)
970970
.ok_or_else(|| anyhow!("Unknown component type: `{}`", component_path))
971971
}
972+
973+
#[cfg(test)]
974+
mod tests {
975+
/// A generic function that tests serialization and deserialization of any type
976+
/// implementing Serialize and Deserialize traits.
977+
fn test_serialize_deserialize<T>(value: T)
978+
where
979+
T: Serialize + for<'a> Deserialize<'a> + PartialEq + core::fmt::Debug,
980+
{
981+
// Serialize the value to JSON string
982+
let serialized = serde_json::to_string(&value).expect("Failed to serialize");
983+
984+
// Deserialize the JSON string back into the original type
985+
let deserialized: T = serde_json::from_str(&serialized).expect("Failed to deserialize");
986+
987+
// Assert that the deserialized value is the same as the original
988+
assert_eq!(
989+
&value, &deserialized,
990+
"Deserialized value does not match original"
991+
);
992+
}
993+
use super::*;
994+
995+
#[test]
996+
fn serialization_tests() {
997+
test_serialize_deserialize(BrpQueryRow {
998+
components: Default::default(),
999+
entity: Entity::from_raw(0),
1000+
has: Default::default(),
1001+
});
1002+
test_serialize_deserialize(BrpListWatchingResponse::default());
1003+
test_serialize_deserialize(BrpQuery::default());
1004+
test_serialize_deserialize(BrpListParams {
1005+
entity: Entity::from_raw(0),
1006+
});
1007+
}
1008+
}

0 commit comments

Comments
 (0)