@@ -737,6 +737,10 @@ mod tests {
737737 map_value : HashMap < u8 , usize > ,
738738 struct_value : SomeStruct ,
739739 tuple_struct_value : SomeTupleStruct ,
740+ unit_enum : SomeEnum ,
741+ newtype_enum : SomeEnum ,
742+ tuple_enum : SomeEnum ,
743+ struct_enum : SomeEnum ,
740744 custom_deserialize : CustomDeserialize ,
741745 }
742746
@@ -759,12 +763,21 @@ mod tests {
759763 inner_struct : SomeStruct ,
760764 }
761765
766+ #[ derive( Reflect , FromReflect , Debug , PartialEq ) ]
767+ enum SomeEnum {
768+ Unit ,
769+ NewType ( usize ) ,
770+ Tuple ( f32 , f32 ) ,
771+ Struct { foo : String } ,
772+ }
773+
762774 fn get_registry ( ) -> TypeRegistry {
763775 let mut registry = TypeRegistry :: default ( ) ;
764776 registry. register :: < MyStruct > ( ) ;
765777 registry. register :: < SomeStruct > ( ) ;
766778 registry. register :: < SomeTupleStruct > ( ) ;
767779 registry. register :: < CustomDeserialize > ( ) ;
780+ registry. register :: < SomeEnum > ( ) ;
768781 registry. register :: < i8 > ( ) ;
769782 registry. register :: < String > ( ) ;
770783 registry. register :: < i64 > ( ) ;
@@ -781,76 +794,6 @@ mod tests {
781794 registry
782795 }
783796
784- #[ test]
785- fn enum_should_deserialize ( ) {
786- #[ derive( Reflect ) ]
787- enum MyEnum {
788- Unit ,
789- NewType ( usize ) ,
790- Tuple ( f32 , f32 ) ,
791- Struct { value : String } ,
792- }
793-
794- let mut registry = get_registry ( ) ;
795- registry. register :: < MyEnum > ( ) ;
796-
797- // === Unit Variant === //
798- let input = r#"{
799- "bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
800- "Unit": (),
801- },
802- }"# ;
803- let reflect_deserializer = UntypedReflectDeserializer :: new ( & registry) ;
804- let mut deserializer = ron:: de:: Deserializer :: from_str ( input) . unwrap ( ) ;
805- let output = reflect_deserializer. deserialize ( & mut deserializer) . unwrap ( ) ;
806-
807- let expected = DynamicEnum :: from ( MyEnum :: Unit ) ;
808- assert ! ( expected. reflect_partial_eq( output. as_ref( ) ) . unwrap( ) ) ;
809-
810- // === NewType Variant === //
811- let input = r#"{
812- "bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
813- "NewType": (123),
814- },
815- }"# ;
816- let reflect_deserializer = UntypedReflectDeserializer :: new ( & registry) ;
817- let mut deserializer = ron:: de:: Deserializer :: from_str ( input) . unwrap ( ) ;
818- let output = reflect_deserializer. deserialize ( & mut deserializer) . unwrap ( ) ;
819-
820- let expected = DynamicEnum :: from ( MyEnum :: NewType ( 123 ) ) ;
821- assert ! ( expected. reflect_partial_eq( output. as_ref( ) ) . unwrap( ) ) ;
822-
823- // === Tuple Variant === //
824- let input = r#"{
825- "bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
826- "Tuple": (1.23, 3.21),
827- },
828- }"# ;
829- let reflect_deserializer = UntypedReflectDeserializer :: new ( & registry) ;
830- let mut deserializer = ron:: de:: Deserializer :: from_str ( input) . unwrap ( ) ;
831- let output = reflect_deserializer. deserialize ( & mut deserializer) . unwrap ( ) ;
832-
833- let expected = DynamicEnum :: from ( MyEnum :: Tuple ( 1.23 , 3.21 ) ) ;
834- assert ! ( expected. reflect_partial_eq( output. as_ref( ) ) . unwrap( ) ) ;
835-
836- // === Struct Variant === //
837- let input = r#"{
838- "bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
839- "Struct": {
840- "value": "I <3 Enums",
841- },
842- },
843- }"# ;
844- let reflect_deserializer = UntypedReflectDeserializer :: new ( & registry) ;
845- let mut deserializer = ron:: de:: Deserializer :: from_str ( input) . unwrap ( ) ;
846- let output = reflect_deserializer. deserialize ( & mut deserializer) . unwrap ( ) ;
847-
848- let expected = DynamicEnum :: from ( MyEnum :: Struct {
849- value : String :: from ( "I <3 Enums" ) ,
850- } ) ;
851- assert ! ( expected. reflect_partial_eq( output. as_ref( ) ) . unwrap( ) ) ;
852- }
853-
854797 #[ test]
855798 fn should_deserialize ( ) {
856799 let mut map = HashMap :: new ( ) ;
@@ -865,6 +808,12 @@ mod tests {
865808 map_value : map,
866809 struct_value : SomeStruct { foo : 999999999 } ,
867810 tuple_struct_value : SomeTupleStruct ( String :: from ( "Tuple Struct" ) ) ,
811+ unit_enum : SomeEnum :: Unit ,
812+ newtype_enum : SomeEnum :: NewType ( 123 ) ,
813+ tuple_enum : SomeEnum :: Tuple ( 1.23 , 3.21 ) ,
814+ struct_enum : SomeEnum :: Struct {
815+ foo : String :: from ( "Struct variant value" ) ,
816+ } ,
868817 custom_deserialize : CustomDeserialize {
869818 value : 100 ,
870819 inner_struct : SomeStruct { foo : 101 } ,
@@ -900,6 +849,20 @@ mod tests {
900849 "foo": 999999999,
901850 },
902851 "tuple_struct_value": ("Tuple Struct"),
852+ "unit_enum": {
853+ "Unit": (),
854+ },
855+ "newtype_enum": {
856+ "NewType": (123),
857+ },
858+ "tuple_enum": {
859+ "Tuple": (1.23, 3.21),
860+ },
861+ "struct_enum": {
862+ "Struct": {
863+ "foo": "Struct variant value",
864+ },
865+ },
903866 "custom_deserialize": (
904867 value: 100,
905868 renamed: (
@@ -962,4 +925,74 @@ mod tests {
962925 let output = <Foo as FromReflect >:: from_reflect ( dynamic_output. as_ref ( ) ) . unwrap ( ) ;
963926 assert_eq ! ( expected, output) ;
964927 }
928+
929+ #[ test]
930+ fn enum_should_deserialize ( ) {
931+ #[ derive( Reflect ) ]
932+ enum MyEnum {
933+ Unit ,
934+ NewType ( usize ) ,
935+ Tuple ( f32 , f32 ) ,
936+ Struct { value : String } ,
937+ }
938+
939+ let mut registry = get_registry ( ) ;
940+ registry. register :: < MyEnum > ( ) ;
941+
942+ // === Unit Variant === //
943+ let input = r#"{
944+ "bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
945+ "Unit": (),
946+ },
947+ }"# ;
948+ let reflect_deserializer = UntypedReflectDeserializer :: new ( & registry) ;
949+ let mut deserializer = ron:: de:: Deserializer :: from_str ( input) . unwrap ( ) ;
950+ let output = reflect_deserializer. deserialize ( & mut deserializer) . unwrap ( ) ;
951+
952+ let expected = DynamicEnum :: from ( MyEnum :: Unit ) ;
953+ assert ! ( expected. reflect_partial_eq( output. as_ref( ) ) . unwrap( ) ) ;
954+
955+ // === NewType Variant === //
956+ let input = r#"{
957+ "bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
958+ "NewType": (123),
959+ },
960+ }"# ;
961+ let reflect_deserializer = UntypedReflectDeserializer :: new ( & registry) ;
962+ let mut deserializer = ron:: de:: Deserializer :: from_str ( input) . unwrap ( ) ;
963+ let output = reflect_deserializer. deserialize ( & mut deserializer) . unwrap ( ) ;
964+
965+ let expected = DynamicEnum :: from ( MyEnum :: NewType ( 123 ) ) ;
966+ assert ! ( expected. reflect_partial_eq( output. as_ref( ) ) . unwrap( ) ) ;
967+
968+ // === Tuple Variant === //
969+ let input = r#"{
970+ "bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
971+ "Tuple": (1.23, 3.21),
972+ },
973+ }"# ;
974+ let reflect_deserializer = UntypedReflectDeserializer :: new ( & registry) ;
975+ let mut deserializer = ron:: de:: Deserializer :: from_str ( input) . unwrap ( ) ;
976+ let output = reflect_deserializer. deserialize ( & mut deserializer) . unwrap ( ) ;
977+
978+ let expected = DynamicEnum :: from ( MyEnum :: Tuple ( 1.23 , 3.21 ) ) ;
979+ assert ! ( expected. reflect_partial_eq( output. as_ref( ) ) . unwrap( ) ) ;
980+
981+ // === Struct Variant === //
982+ let input = r#"{
983+ "bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
984+ "Struct": {
985+ "value": "I <3 Enums",
986+ },
987+ },
988+ }"# ;
989+ let reflect_deserializer = UntypedReflectDeserializer :: new ( & registry) ;
990+ let mut deserializer = ron:: de:: Deserializer :: from_str ( input) . unwrap ( ) ;
991+ let output = reflect_deserializer. deserialize ( & mut deserializer) . unwrap ( ) ;
992+
993+ let expected = DynamicEnum :: from ( MyEnum :: Struct {
994+ value : String :: from ( "I <3 Enums" ) ,
995+ } ) ;
996+ assert ! ( expected. reflect_partial_eq( output. as_ref( ) ) . unwrap( ) ) ;
997+ }
965998}
0 commit comments