Skip to content

Commit

Permalink
Add reflected debug test
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed May 13, 2022
1 parent 24af17f commit 4998d85
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions crates/bevy_reflect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -489,6 +490,86 @@ mod tests {
let _ = trait_object.as_reflect();
}

#[test]
fn should_reflect_debug() {
#[derive(Reflect)]
struct Test {
value: usize,
list: Vec<String>,
array: [f32; 3],
map: HashMap<i32, f32>,
a_struct: SomeStruct,
a_tuple_struct: SomeTupleStruct,
custom: CustomDebug,
unknown: Option<String>,
#[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<alloc::string::String>),
}"#;

assert_eq!(expected, format!("\n{:#?}", reflected));
}

#[cfg(feature = "glam")]
mod glam {
use super::*;
Expand Down

0 comments on commit 4998d85

Please sign in to comment.