Skip to content

Commit

Permalink
Add nested enums to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed Aug 8, 2022
1 parent 92e08ec commit c70308e
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 70 deletions.
173 changes: 103 additions & 70 deletions crates/bevy_reflect/src/serde/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,10 @@ mod tests {
map_value: HashMap<u8, usize>,
struct_value: SomeStruct,
tuple_struct_value: SomeTupleStruct,
unit_enum: SomeEnum,
newtype_enum: SomeEnum,
tuple_enum: SomeEnum,
struct_enum: SomeEnum,
custom_deserialize: CustomDeserialize,
}

Expand All @@ -759,12 +763,21 @@ mod tests {
inner_struct: SomeStruct,
}

#[derive(Reflect, FromReflect, Debug, PartialEq)]
enum SomeEnum {
Unit,
NewType(usize),
Tuple(f32, f32),
Struct { foo: String },
}

fn get_registry() -> TypeRegistry {
let mut registry = TypeRegistry::default();
registry.register::<MyStruct>();
registry.register::<SomeStruct>();
registry.register::<SomeTupleStruct>();
registry.register::<CustomDeserialize>();
registry.register::<SomeEnum>();
registry.register::<i8>();
registry.register::<String>();
registry.register::<i64>();
Expand All @@ -781,76 +794,6 @@ mod tests {
registry
}

#[test]
fn enum_should_deserialize() {
#[derive(Reflect)]
enum MyEnum {
Unit,
NewType(usize),
Tuple(f32, f32),
Struct { value: String },
}

let mut registry = get_registry();
registry.register::<MyEnum>();

// === Unit Variant === //
let input = r#"{
"bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
"Unit": (),
},
}"#;
let reflect_deserializer = UntypedReflectDeserializer::new(&registry);
let mut deserializer = ron::de::Deserializer::from_str(input).unwrap();
let output = reflect_deserializer.deserialize(&mut deserializer).unwrap();

let expected = DynamicEnum::from(MyEnum::Unit);
assert!(expected.reflect_partial_eq(output.as_ref()).unwrap());

// === NewType Variant === //
let input = r#"{
"bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
"NewType": (123),
},
}"#;
let reflect_deserializer = UntypedReflectDeserializer::new(&registry);
let mut deserializer = ron::de::Deserializer::from_str(input).unwrap();
let output = reflect_deserializer.deserialize(&mut deserializer).unwrap();

let expected = DynamicEnum::from(MyEnum::NewType(123));
assert!(expected.reflect_partial_eq(output.as_ref()).unwrap());

// === Tuple Variant === //
let input = r#"{
"bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
"Tuple": (1.23, 3.21),
},
}"#;
let reflect_deserializer = UntypedReflectDeserializer::new(&registry);
let mut deserializer = ron::de::Deserializer::from_str(input).unwrap();
let output = reflect_deserializer.deserialize(&mut deserializer).unwrap();

let expected = DynamicEnum::from(MyEnum::Tuple(1.23, 3.21));
assert!(expected.reflect_partial_eq(output.as_ref()).unwrap());

// === Struct Variant === //
let input = r#"{
"bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
"Struct": {
"value": "I <3 Enums",
},
},
}"#;
let reflect_deserializer = UntypedReflectDeserializer::new(&registry);
let mut deserializer = ron::de::Deserializer::from_str(input).unwrap();
let output = reflect_deserializer.deserialize(&mut deserializer).unwrap();

let expected = DynamicEnum::from(MyEnum::Struct {
value: String::from("I <3 Enums"),
});
assert!(expected.reflect_partial_eq(output.as_ref()).unwrap());
}

#[test]
fn should_deserialize() {
let mut map = HashMap::new();
Expand All @@ -865,6 +808,12 @@ mod tests {
map_value: map,
struct_value: SomeStruct { foo: 999999999 },
tuple_struct_value: SomeTupleStruct(String::from("Tuple Struct")),
unit_enum: SomeEnum::Unit,
newtype_enum: SomeEnum::NewType(123),
tuple_enum: SomeEnum::Tuple(1.23, 3.21),
struct_enum: SomeEnum::Struct {
foo: String::from("Struct variant value"),
},
custom_deserialize: CustomDeserialize {
value: 100,
inner_struct: SomeStruct { foo: 101 },
Expand Down Expand Up @@ -900,6 +849,20 @@ mod tests {
"foo": 999999999,
},
"tuple_struct_value": ("Tuple Struct"),
"unit_enum": {
"Unit": (),
},
"newtype_enum": {
"NewType": (123),
},
"tuple_enum": {
"Tuple": (1.23, 3.21),
},
"struct_enum": {
"Struct": {
"foo": "Struct variant value",
},
},
"custom_deserialize": (
value: 100,
renamed: (
Expand Down Expand Up @@ -962,4 +925,74 @@ mod tests {
let output = <Foo as FromReflect>::from_reflect(dynamic_output.as_ref()).unwrap();
assert_eq!(expected, output);
}

#[test]
fn enum_should_deserialize() {
#[derive(Reflect)]
enum MyEnum {
Unit,
NewType(usize),
Tuple(f32, f32),
Struct { value: String },
}

let mut registry = get_registry();
registry.register::<MyEnum>();

// === Unit Variant === //
let input = r#"{
"bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
"Unit": (),
},
}"#;
let reflect_deserializer = UntypedReflectDeserializer::new(&registry);
let mut deserializer = ron::de::Deserializer::from_str(input).unwrap();
let output = reflect_deserializer.deserialize(&mut deserializer).unwrap();

let expected = DynamicEnum::from(MyEnum::Unit);
assert!(expected.reflect_partial_eq(output.as_ref()).unwrap());

// === NewType Variant === //
let input = r#"{
"bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
"NewType": (123),
},
}"#;
let reflect_deserializer = UntypedReflectDeserializer::new(&registry);
let mut deserializer = ron::de::Deserializer::from_str(input).unwrap();
let output = reflect_deserializer.deserialize(&mut deserializer).unwrap();

let expected = DynamicEnum::from(MyEnum::NewType(123));
assert!(expected.reflect_partial_eq(output.as_ref()).unwrap());

// === Tuple Variant === //
let input = r#"{
"bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
"Tuple": (1.23, 3.21),
},
}"#;
let reflect_deserializer = UntypedReflectDeserializer::new(&registry);
let mut deserializer = ron::de::Deserializer::from_str(input).unwrap();
let output = reflect_deserializer.deserialize(&mut deserializer).unwrap();

let expected = DynamicEnum::from(MyEnum::Tuple(1.23, 3.21));
assert!(expected.reflect_partial_eq(output.as_ref()).unwrap());

// === Struct Variant === //
let input = r#"{
"bevy_reflect::serde::de::tests::enum_should_deserialize::MyEnum": {
"Struct": {
"value": "I <3 Enums",
},
},
}"#;
let reflect_deserializer = UntypedReflectDeserializer::new(&registry);
let mut deserializer = ron::de::Deserializer::from_str(input).unwrap();
let output = reflect_deserializer.deserialize(&mut deserializer).unwrap();

let expected = DynamicEnum::from(MyEnum::Struct {
value: String::from("I <3 Enums"),
});
assert!(expected.reflect_partial_eq(output.as_ref()).unwrap());
}
}
32 changes: 32 additions & 0 deletions crates/bevy_reflect/src/serde/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ mod tests {
map_value: HashMap<u8, usize>,
struct_value: SomeStruct,
tuple_struct_value: SomeTupleStruct,
unit_enum: SomeEnum,
newtype_enum: SomeEnum,
tuple_enum: SomeEnum,
struct_enum: SomeEnum,
custom_serialize: CustomSerialize,
}

Expand All @@ -388,6 +392,14 @@ mod tests {
#[derive(Reflect, Debug, PartialEq)]
struct SomeTupleStruct(String);

#[derive(Reflect, Debug, PartialEq)]
enum SomeEnum {
Unit,
NewType(usize),
Tuple(f32, f32),
Struct { foo: String },
}

/// Implements a custom serialize using `#[reflect(Serialize)]`.
///
/// For testing purposes, this just uses the generated one from deriving Serialize.
Expand Down Expand Up @@ -425,6 +437,12 @@ mod tests {
map_value: map,
struct_value: SomeStruct { foo: 999999999 },
tuple_struct_value: SomeTupleStruct(String::from("Tuple Struct")),
unit_enum: SomeEnum::Unit,
newtype_enum: SomeEnum::NewType(123),
tuple_enum: SomeEnum::Tuple(1.23, 3.21),
struct_enum: SomeEnum::Struct {
foo: String::from("Struct variant value"),
},
custom_serialize: CustomSerialize {
value: 100,
inner_struct: SomeStruct { foo: 101 },
Expand Down Expand Up @@ -460,6 +478,20 @@ mod tests {
"foo": 999999999,
},
"tuple_struct_value": ("Tuple Struct"),
"unit_enum": {
"Unit": (),
},
"newtype_enum": {
"NewType": (123),
},
"tuple_enum": {
"Tuple": (1.23, 3.21),
},
"struct_enum": {
"Struct": {
"foo": "Struct variant value",
},
},
"custom_serialize": (
value: 100,
renamed: (
Expand Down

0 comments on commit c70308e

Please sign in to comment.