Skip to content

Commit b85a46d

Browse files
committed
Add more complex Option value to tests
1 parent f64776f commit b85a46d

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

crates/bevy_reflect/src/serde/de.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ mod tests {
731731
struct MyStruct {
732732
primitive_value: i8,
733733
option_value: Option<String>,
734+
option_value_complex: Option<SomeStruct>,
734735
tuple_value: (f32, usize),
735736
list_value: Vec<i32>,
736737
array_value: [i32; 5],
@@ -744,14 +745,19 @@ mod tests {
744745
custom_deserialize: CustomDeserialize,
745746
}
746747

747-
#[derive(Reflect, FromReflect, Debug, PartialEq, Deserialize)]
748+
#[derive(Reflect, FromReflect, Debug, PartialEq)]
748749
struct SomeStruct {
749750
foo: i64,
750751
}
751752

752753
#[derive(Reflect, FromReflect, Debug, PartialEq)]
753754
struct SomeTupleStruct(String);
754755

756+
#[derive(Reflect, FromReflect, Debug, PartialEq, Deserialize)]
757+
struct SomeDeserializableStruct {
758+
foo: i64,
759+
}
760+
755761
/// Implements a custom deserialize using #[reflect(Deserialize)].
756762
///
757763
/// For testing purposes, this is just the auto-generated one from deriving.
@@ -760,7 +766,7 @@ mod tests {
760766
struct CustomDeserialize {
761767
value: usize,
762768
#[serde(rename = "renamed")]
763-
inner_struct: SomeStruct,
769+
inner_struct: SomeDeserializableStruct,
764770
}
765771

766772
#[derive(Reflect, FromReflect, Debug, PartialEq)]
@@ -777,6 +783,7 @@ mod tests {
777783
registry.register::<SomeStruct>();
778784
registry.register::<SomeTupleStruct>();
779785
registry.register::<CustomDeserialize>();
786+
registry.register::<SomeDeserializableStruct>();
780787
registry.register::<SomeEnum>();
781788
registry.register::<i8>();
782789
registry.register::<String>();
@@ -789,6 +796,7 @@ mod tests {
789796
registry.register::<[i32; 5]>();
790797
registry.register::<Vec<i32>>();
791798
registry.register::<HashMap<u8, usize>>();
799+
registry.register::<Option<SomeStruct>>();
792800
registry.register::<Option<String>>();
793801
registry.register_type_data::<Option<String>, ReflectDeserialize>();
794802
registry
@@ -802,6 +810,7 @@ mod tests {
802810
let expected = MyStruct {
803811
primitive_value: 123,
804812
option_value: Some(String::from("Hello world!")),
813+
option_value_complex: Some(SomeStruct { foo: 123 }),
805814
tuple_value: (PI, 1337),
806815
list_value: vec![-2, -1, 0, 1, 2],
807816
array_value: [-2, -1, 0, 1, 2],
@@ -816,14 +825,19 @@ mod tests {
816825
},
817826
custom_deserialize: CustomDeserialize {
818827
value: 100,
819-
inner_struct: SomeStruct { foo: 101 },
828+
inner_struct: SomeDeserializableStruct { foo: 101 },
820829
},
821830
};
822831

823832
let input = r#"{
824833
"bevy_reflect::serde::de::tests::MyStruct": {
825834
"primitive_value": 123,
826835
"option_value": Some("Hello world!"),
836+
"option_value_complex": {
837+
"Some": ({
838+
"foo": 123,
839+
}),
840+
},
827841
"tuple_value": (
828842
3.1415927,
829843
1337,

crates/bevy_reflect/src/serde/ser.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ impl<'a> Serialize for ArraySerializer<'a> {
361361
mod tests {
362362
use crate as bevy_reflect;
363363
use crate::serde::ReflectSerializer;
364-
use crate::{Reflect, ReflectSerialize, TypeRegistry};
364+
use crate::{FromReflect, Reflect, ReflectSerialize, TypeRegistry};
365365
use bevy_utils::HashMap;
366366
use ron::ser::PrettyConfig;
367367
use serde::Serialize;
@@ -371,6 +371,7 @@ mod tests {
371371
struct MyStruct {
372372
primitive_value: i8,
373373
option_value: Option<String>,
374+
option_value_complex: Option<SomeStruct>,
374375
tuple_value: (f32, usize),
375376
list_value: Vec<i32>,
376377
array_value: [i32; 5],
@@ -384,7 +385,7 @@ mod tests {
384385
custom_serialize: CustomSerialize,
385386
}
386387

387-
#[derive(Reflect, Debug, PartialEq, Serialize)]
388+
#[derive(Reflect, FromReflect, Debug, PartialEq)]
388389
struct SomeStruct {
389390
foo: i64,
390391
}
@@ -400,6 +401,11 @@ mod tests {
400401
Struct { foo: String },
401402
}
402403

404+
#[derive(Reflect, Debug, PartialEq, Serialize)]
405+
struct SomeSerializableStruct {
406+
foo: i64,
407+
}
408+
403409
/// Implements a custom serialize using `#[reflect(Serialize)]`.
404410
///
405411
/// For testing purposes, this just uses the generated one from deriving Serialize.
@@ -408,7 +414,7 @@ mod tests {
408414
struct CustomSerialize {
409415
value: usize,
410416
#[serde(rename = "renamed")]
411-
inner_struct: SomeStruct,
417+
inner_struct: SomeSerializableStruct,
412418
}
413419

414420
fn get_registry() -> TypeRegistry {
@@ -417,6 +423,8 @@ mod tests {
417423
registry.register::<SomeStruct>();
418424
registry.register::<SomeTupleStruct>();
419425
registry.register::<CustomSerialize>();
426+
registry.register::<SomeSerializableStruct>();
427+
registry.register_type_data::<SomeSerializableStruct, ReflectSerialize>();
420428
registry.register::<String>();
421429
registry.register::<Option<String>>();
422430
registry.register_type_data::<Option<String>, ReflectSerialize>();
@@ -431,6 +439,7 @@ mod tests {
431439
let input = MyStruct {
432440
primitive_value: 123,
433441
option_value: Some(String::from("Hello world!")),
442+
option_value_complex: Some(SomeStruct { foo: 123 }),
434443
tuple_value: (PI, 1337),
435444
list_value: vec![-2, -1, 0, 1, 2],
436445
array_value: [-2, -1, 0, 1, 2],
@@ -445,7 +454,7 @@ mod tests {
445454
},
446455
custom_serialize: CustomSerialize {
447456
value: 100,
448-
inner_struct: SomeStruct { foo: 101 },
457+
inner_struct: SomeSerializableStruct { foo: 101 },
449458
},
450459
};
451460

@@ -462,6 +471,11 @@ mod tests {
462471
"bevy_reflect::serde::ser::tests::MyStruct": {
463472
"primitive_value": 123,
464473
"option_value": Some("Hello world!"),
474+
"option_value_complex": {
475+
"Some": ({
476+
"foo": 123,
477+
}),
478+
},
465479
"tuple_value": (3.1415927, 1337),
466480
"list_value": [
467481
-2,

0 commit comments

Comments
 (0)