From 4998d856aa63733b01d53ac2f8cec09359a513fa Mon Sep 17 00:00:00 2001 From: Gino Valente Date: Thu, 12 May 2022 21:51:56 -0700 Subject: [PATCH] Add reflected debug test --- crates/bevy_reflect/src/lib.rs | 81 ++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index b484e47738cfd..f39f11a7ae9a9 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -84,6 +84,7 @@ pub mod __macro_exports { #[cfg(test)] #[allow(clippy::blacklisted_name, clippy::approx_constant)] mod tests { + use std::fmt::{Debug, Formatter}; #[cfg(feature = "glam")] use ::glam::{vec3, Vec3}; use ::serde::de::DeserializeSeed; @@ -489,6 +490,86 @@ mod tests { let _ = trait_object.as_reflect(); } + #[test] + fn should_reflect_debug() { + #[derive(Reflect)] + struct Test { + value: usize, + list: Vec, + array: [f32; 3], + map: HashMap, + a_struct: SomeStruct, + a_tuple_struct: SomeTupleStruct, + custom: CustomDebug, + unknown: Option, + #[reflect(ignore)] + ignored: isize + } + + #[derive(Reflect)] + struct SomeStruct { + foo: String, + } + + #[derive(Reflect)] + struct SomeTupleStruct(String); + + #[derive(Reflect)] + #[reflect(Debug)] + struct CustomDebug; + impl Debug for CustomDebug { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_str("Cool debug!") + } + } + + let mut map = HashMap::new(); + map.insert(123, 1.23); + + let test = Test { + value: 123, + list: vec![String::from("A"), String::from("B"), String::from("C")], + array: [1.0, 2.0, 3.0], + map, + a_struct: SomeStruct { + foo: String::from("A Struct!"), + }, + a_tuple_struct: SomeTupleStruct(String::from("A Tuple Struct!")), + custom: CustomDebug, + unknown: Some(String::from("Enums aren't supported yet :(")), + ignored: 321 + }; + + let reflected: &dyn Reflect = &test; + let expected = r#" +bevy_reflect::tests::should_reflect_debug::Test { + value: 123, + list: [ + "A", + "B", + "C", + ], + array: [ + 1.0, + 2.0, + 3.0, + ], + map: { + 123: 1.23, + }, + a_struct: bevy_reflect::tests::should_reflect_debug::SomeStruct { + foo: "A Struct!", + }, + a_tuple_struct: bevy_reflect::tests::should_reflect_debug::SomeTupleStruct( + "A Tuple Struct!", + ), + custom: Cool debug!, + unknown: Reflect(core::option::Option), +}"#; + + assert_eq!(expected, format!("\n{:#?}", reflected)); + } + #[cfg(feature = "glam")] mod glam { use super::*;