Skip to content

Commit 7fcce32

Browse files
committed
Add FromReflect default attr tests
1 parent e4a5bda commit 7fcce32

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

crates/bevy_reflect/src/lib.rs

+30
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,36 @@ mod tests {
231231
assert_eq!(values, vec![1]);
232232
}
233233

234+
#[test]
235+
fn from_reflect_should_use_default_attributes() {
236+
#[derive(Reflect, FromReflect, Eq, PartialEq, Debug)]
237+
struct MyStruct {
238+
// Use `Default::default()`
239+
// Note that this isn't an ignored field
240+
#[reflect(default)]
241+
foo: String,
242+
243+
// Use `get_foo_default()`
244+
#[reflect(default = "get_bar_default")]
245+
#[reflect(ignore)]
246+
bar: usize,
247+
}
248+
249+
fn get_bar_default() -> usize {
250+
123
251+
}
252+
253+
let expected = MyStruct {
254+
foo: String::default(),
255+
bar: 123,
256+
};
257+
258+
let dyn_struct = DynamicStruct::default();
259+
let my_struct = <MyStruct as FromReflect>::from_reflect(&dyn_struct);
260+
261+
assert_eq!(Some(expected), my_struct);
262+
}
263+
234264
#[test]
235265
fn reflect_complex_patch() {
236266
#[derive(Reflect, Eq, PartialEq, Debug, FromReflect)]

0 commit comments

Comments
 (0)