Skip to content

Commit 7bd305d

Browse files
committed
Added better tests for binary formats
Now tests self-describing and non-self-describing formats.
1 parent 8e30d57 commit 7bd305d

File tree

3 files changed

+159
-56
lines changed

3 files changed

+159
-56
lines changed

crates/bevy_reflect/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ glam = { version = "0.21", features = ["serde"], optional = true }
3131

3232
[dev-dependencies]
3333
ron = "0.8.0"
34-
rmp-serde = "1.1"
34+
rmp-serde = "1.1"
35+
bincode = "1.3"

crates/bevy_reflect/src/serde/de.rs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@ fn get_registration<'a, E: Error>(
987987

988988
#[cfg(test)]
989989
mod tests {
990+
use bincode::Options;
990991
use std::any::TypeId;
991992
use std::f32::consts::PI;
992993

@@ -1322,7 +1323,7 @@ mod tests {
13221323
}
13231324

13241325
#[test]
1325-
fn should_deserialize_binary() {
1326+
fn should_deserialize_non_self_describing_binary() {
13261327
let mut map = HashMap::new();
13271328
map.insert(64, 32);
13281329

@@ -1348,6 +1349,63 @@ mod tests {
13481349
},
13491350
};
13501351

1352+
let registry = get_registry();
1353+
1354+
let input = vec![
1355+
1, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 98, 101, 118, 121, 95, 114, 101, 102,
1356+
108, 101, 99, 116, 58, 58, 115, 101, 114, 100, 101, 58, 58, 100, 101, 58, 58, 116, 101,
1357+
115, 116, 115, 58, 58, 77, 121, 83, 116, 114, 117, 99, 116, 123, 1, 12, 0, 0, 0, 0, 0,
1358+
0, 0, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33, 1, 123, 0, 0, 0, 0, 0,
1359+
0, 0, 219, 15, 73, 64, 57, 5, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 254, 255, 255,
1360+
255, 255, 255, 255, 255, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 254, 255, 255, 255, 255,
1361+
255, 255, 255, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 64, 32, 0,
1362+
0, 0, 0, 0, 0, 0, 255, 201, 154, 59, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 84, 117, 112,
1363+
108, 101, 32, 83, 116, 114, 117, 99, 116, 0, 0, 0, 0, 1, 0, 0, 0, 123, 0, 0, 0, 0, 0,
1364+
0, 0, 2, 0, 0, 0, 164, 112, 157, 63, 164, 112, 77, 64, 3, 0, 0, 0, 20, 0, 0, 0, 0, 0,
1365+
0, 0, 83, 116, 114, 117, 99, 116, 32, 118, 97, 114, 105, 97, 110, 116, 32, 118, 97,
1366+
108, 117, 101, 100, 0, 0, 0, 0, 0, 0, 0, 101, 0, 0, 0, 0, 0, 0, 0,
1367+
];
1368+
1369+
let deserializer = UntypedReflectDeserializer::new(&registry);
1370+
1371+
let dynamic_output = bincode::DefaultOptions::new()
1372+
.with_fixint_encoding()
1373+
.deserialize_seed(deserializer, &input)
1374+
.unwrap();
1375+
1376+
let output = <MyStruct as FromReflect>::from_reflect(dynamic_output.as_ref()).unwrap();
1377+
assert_eq!(expected, output);
1378+
}
1379+
1380+
#[test]
1381+
fn should_deserialize_self_describing_binary() {
1382+
let mut map = HashMap::new();
1383+
map.insert(64, 32);
1384+
1385+
let expected = MyStruct {
1386+
primitive_value: 123,
1387+
option_value: Some(String::from("Hello world!")),
1388+
option_value_complex: Some(SomeStruct { foo: 123 }),
1389+
tuple_value: (PI, 1337),
1390+
list_value: vec![-2, -1, 0, 1, 2],
1391+
array_value: [-2, -1, 0, 1, 2],
1392+
map_value: map,
1393+
struct_value: SomeStruct { foo: 999999999 },
1394+
tuple_struct_value: SomeTupleStruct(String::from("Tuple Struct")),
1395+
unit_enum: SomeEnum::Unit,
1396+
newtype_enum: SomeEnum::NewType(123),
1397+
tuple_enum: SomeEnum::Tuple(1.23, 3.21),
1398+
struct_enum: SomeEnum::Struct {
1399+
foo: String::from("Struct variant value"),
1400+
},
1401+
custom_deserialize: CustomDeserialize {
1402+
value: 100,
1403+
inner_struct: SomeDeserializableStruct { foo: 101 },
1404+
},
1405+
};
1406+
1407+
let registry = get_registry();
1408+
13511409
let input = vec![
13521410
129, 217, 40, 98, 101, 118, 121, 95, 114, 101, 102, 108, 101, 99, 116, 58, 58, 115,
13531411
101, 114, 100, 101, 58, 58, 100, 101, 58, 58, 116, 101, 115, 116, 115, 58, 58, 77, 121,
@@ -1361,7 +1419,6 @@ mod tests {
13611419
101, 146, 100, 145, 101,
13621420
];
13631421

1364-
let registry = get_registry();
13651422
let deserializer = UntypedReflectDeserializer::new(&registry);
13661423

13671424
let dynamic_output = deserializer

crates/bevy_reflect/src/serde/ser.rs

Lines changed: 98 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -542,59 +542,6 @@ mod tests {
542542
registry
543543
}
544544

545-
#[test]
546-
fn should_serialize_binary() {
547-
let mut map = HashMap::new();
548-
map.insert(64, 32);
549-
550-
let input = MyStruct {
551-
primitive_value: 123,
552-
option_value: Some(String::from("Hello world!")),
553-
option_value_complex: Some(SomeStruct { foo: 123 }),
554-
tuple_value: (PI, 1337),
555-
list_value: vec![-2, -1, 0, 1, 2],
556-
array_value: [-2, -1, 0, 1, 2],
557-
map_value: map,
558-
struct_value: SomeStruct { foo: 999999999 },
559-
tuple_struct_value: SomeTupleStruct(String::from("Tuple Struct")),
560-
unit_enum: SomeEnum::Unit,
561-
newtype_enum: SomeEnum::NewType(123),
562-
tuple_enum: SomeEnum::Tuple(1.23, 3.21),
563-
struct_enum: SomeEnum::Struct {
564-
foo: String::from("Struct variant value"),
565-
},
566-
custom_serialize: CustomSerialize {
567-
value: 100,
568-
inner_struct: SomeSerializableStruct { foo: 101 },
569-
},
570-
};
571-
572-
let mut registry = get_registry();
573-
registry.register::<SomeStruct>();
574-
registry.register::<Option<SomeStruct>>();
575-
registry.register::<(f32, usize)>();
576-
registry.register::<Vec<i32>>();
577-
registry.register::<[i32; 5]>();
578-
registry.register::<HashMap<u8, usize>>();
579-
580-
let serializer = ReflectSerializer::new(&input, &registry);
581-
let bytes: Vec<u8> = rmp_serde::to_vec(&serializer).unwrap();
582-
583-
let expected: Vec<u8> = vec![
584-
129, 217, 41, 98, 101, 118, 121, 95, 114, 101, 102, 108, 101, 99, 116, 58, 58, 115,
585-
101, 114, 100, 101, 58, 58, 115, 101, 114, 58, 58, 116, 101, 115, 116, 115, 58, 58, 77,
586-
121, 83, 116, 114, 117, 99, 116, 158, 123, 172, 72, 101, 108, 108, 111, 32, 119, 111,
587-
114, 108, 100, 33, 145, 123, 146, 202, 64, 73, 15, 219, 205, 5, 57, 149, 254, 255, 0,
588-
1, 2, 149, 254, 255, 0, 1, 2, 129, 64, 32, 145, 206, 59, 154, 201, 255, 145, 172, 84,
589-
117, 112, 108, 101, 32, 83, 116, 114, 117, 99, 116, 164, 85, 110, 105, 116, 129, 167,
590-
78, 101, 119, 84, 121, 112, 101, 123, 129, 165, 84, 117, 112, 108, 101, 146, 202, 63,
591-
157, 112, 164, 202, 64, 77, 112, 164, 129, 166, 83, 116, 114, 117, 99, 116, 145, 180,
592-
83, 116, 114, 117, 99, 116, 32, 118, 97, 114, 105, 97, 110, 116, 32, 118, 97, 108, 117,
593-
101, 146, 100, 145, 101,
594-
];
595-
assert_eq!(expected, bytes);
596-
}
597-
598545
#[test]
599546
fn should_serialize() {
600547
let mut map = HashMap::new();
@@ -782,4 +729,102 @@ mod tests {
782729
}"#;
783730
assert_eq!(expected, output);
784731
}
732+
733+
#[test]
734+
fn should_serialize_non_self_describing_binary() {
735+
let mut map = HashMap::new();
736+
map.insert(64, 32);
737+
738+
let input = MyStruct {
739+
primitive_value: 123,
740+
option_value: Some(String::from("Hello world!")),
741+
option_value_complex: Some(SomeStruct { foo: 123 }),
742+
tuple_value: (PI, 1337),
743+
list_value: vec![-2, -1, 0, 1, 2],
744+
array_value: [-2, -1, 0, 1, 2],
745+
map_value: map,
746+
struct_value: SomeStruct { foo: 999999999 },
747+
tuple_struct_value: SomeTupleStruct(String::from("Tuple Struct")),
748+
unit_enum: SomeEnum::Unit,
749+
newtype_enum: SomeEnum::NewType(123),
750+
tuple_enum: SomeEnum::Tuple(1.23, 3.21),
751+
struct_enum: SomeEnum::Struct {
752+
foo: String::from("Struct variant value"),
753+
},
754+
custom_serialize: CustomSerialize {
755+
value: 100,
756+
inner_struct: SomeSerializableStruct { foo: 101 },
757+
},
758+
};
759+
760+
let registry = get_registry();
761+
762+
let serializer = ReflectSerializer::new(&input, &registry);
763+
let bytes = bincode::serialize(&serializer).unwrap();
764+
765+
let expected: Vec<u8> = vec![
766+
1, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 0, 0, 0, 0, 98, 101, 118, 121, 95, 114, 101, 102,
767+
108, 101, 99, 116, 58, 58, 115, 101, 114, 100, 101, 58, 58, 115, 101, 114, 58, 58, 116,
768+
101, 115, 116, 115, 58, 58, 77, 121, 83, 116, 114, 117, 99, 116, 123, 1, 12, 0, 0, 0,
769+
0, 0, 0, 0, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33, 1, 123, 0, 0, 0,
770+
0, 0, 0, 0, 219, 15, 73, 64, 57, 5, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 254, 255,
771+
255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 254, 255, 255, 255,
772+
255, 255, 255, 255, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 64, 32,
773+
0, 0, 0, 0, 0, 0, 0, 255, 201, 154, 59, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 84, 117,
774+
112, 108, 101, 32, 83, 116, 114, 117, 99, 116, 0, 0, 0, 0, 1, 0, 0, 0, 123, 0, 0, 0, 0,
775+
0, 0, 0, 2, 0, 0, 0, 164, 112, 157, 63, 164, 112, 77, 64, 3, 0, 0, 0, 20, 0, 0, 0, 0,
776+
0, 0, 0, 83, 116, 114, 117, 99, 116, 32, 118, 97, 114, 105, 97, 110, 116, 32, 118, 97,
777+
108, 117, 101, 100, 0, 0, 0, 0, 0, 0, 0, 101, 0, 0, 0, 0, 0, 0, 0,
778+
];
779+
780+
assert_eq!(expected, bytes);
781+
}
782+
783+
#[test]
784+
fn should_serialize_self_describing_binary() {
785+
let mut map = HashMap::new();
786+
map.insert(64, 32);
787+
788+
let input = MyStruct {
789+
primitive_value: 123,
790+
option_value: Some(String::from("Hello world!")),
791+
option_value_complex: Some(SomeStruct { foo: 123 }),
792+
tuple_value: (PI, 1337),
793+
list_value: vec![-2, -1, 0, 1, 2],
794+
array_value: [-2, -1, 0, 1, 2],
795+
map_value: map,
796+
struct_value: SomeStruct { foo: 999999999 },
797+
tuple_struct_value: SomeTupleStruct(String::from("Tuple Struct")),
798+
unit_enum: SomeEnum::Unit,
799+
newtype_enum: SomeEnum::NewType(123),
800+
tuple_enum: SomeEnum::Tuple(1.23, 3.21),
801+
struct_enum: SomeEnum::Struct {
802+
foo: String::from("Struct variant value"),
803+
},
804+
custom_serialize: CustomSerialize {
805+
value: 100,
806+
inner_struct: SomeSerializableStruct { foo: 101 },
807+
},
808+
};
809+
810+
let registry = get_registry();
811+
812+
let serializer = ReflectSerializer::new(&input, &registry);
813+
let bytes: Vec<u8> = rmp_serde::to_vec(&serializer).unwrap();
814+
815+
let expected: Vec<u8> = vec![
816+
129, 217, 41, 98, 101, 118, 121, 95, 114, 101, 102, 108, 101, 99, 116, 58, 58, 115,
817+
101, 114, 100, 101, 58, 58, 115, 101, 114, 58, 58, 116, 101, 115, 116, 115, 58, 58, 77,
818+
121, 83, 116, 114, 117, 99, 116, 158, 123, 172, 72, 101, 108, 108, 111, 32, 119, 111,
819+
114, 108, 100, 33, 145, 123, 146, 202, 64, 73, 15, 219, 205, 5, 57, 149, 254, 255, 0,
820+
1, 2, 149, 254, 255, 0, 1, 2, 129, 64, 32, 145, 206, 59, 154, 201, 255, 145, 172, 84,
821+
117, 112, 108, 101, 32, 83, 116, 114, 117, 99, 116, 164, 85, 110, 105, 116, 129, 167,
822+
78, 101, 119, 84, 121, 112, 101, 123, 129, 165, 84, 117, 112, 108, 101, 146, 202, 63,
823+
157, 112, 164, 202, 64, 77, 112, 164, 129, 166, 83, 116, 114, 117, 99, 116, 145, 180,
824+
83, 116, 114, 117, 99, 116, 32, 118, 97, 114, 105, 97, 110, 116, 32, 118, 97, 108, 117,
825+
101, 146, 100, 145, 101,
826+
];
827+
828+
assert_eq!(expected, bytes);
829+
}
785830
}

0 commit comments

Comments
 (0)