Update GetPath unit test and documentation for empty path usecase (#17150)

# Objective

- `GetPath` `path` related methods allow an empty string as the
parameter, but this is not included as a test or in documentation. This
PR adds both.
- Fixes #13459

## Solution

- Updates the `bevy_reflect` `GetPath` documentation and unit tests

## Testing

- `cargo run -p ci`
This commit is contained in:
Sean Kim 2025-01-04 18:45:26 -08:00 committed by GitHub
parent cf6c65522f
commit f90a41ff72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -127,10 +127,12 @@ impl<'a> ReflectPath<'a> for &'a str {
/// Note that a leading dot (`.`) or hash (`#`) token is implied for the first item in a path, /// Note that a leading dot (`.`) or hash (`#`) token is implied for the first item in a path,
/// and may therefore be omitted. /// and may therefore be omitted.
/// ///
/// Additionally, an empty path may be used to get the struct itself.
///
/// ### Example /// ### Example
/// ``` /// ```
/// # use bevy_reflect::{GetPath, Reflect}; /// # use bevy_reflect::{GetPath, Reflect};
/// #[derive(Reflect)] /// #[derive(Reflect, PartialEq, Debug)]
/// struct MyStruct { /// struct MyStruct {
/// value: u32 /// value: u32
/// } /// }
@ -140,6 +142,8 @@ impl<'a> ReflectPath<'a> for &'a str {
/// assert_eq!(my_struct.path::<u32>(".value").unwrap(), &123); /// assert_eq!(my_struct.path::<u32>(".value").unwrap(), &123);
/// // Access via field index /// // Access via field index
/// assert_eq!(my_struct.path::<u32>("#0").unwrap(), &123); /// assert_eq!(my_struct.path::<u32>("#0").unwrap(), &123);
/// // Access self
/// assert_eq!(*my_struct.path::<MyStruct>("").unwrap(), my_struct);
/// ``` /// ```
/// ///
/// ## Tuples and Tuple Structs /// ## Tuples and Tuple Structs
@ -512,7 +516,7 @@ mod tests {
use crate::*; use crate::*;
use alloc::vec; use alloc::vec;
#[derive(Reflect)] #[derive(Reflect, PartialEq, Debug)]
struct A { struct A {
w: usize, w: usize,
x: B, x: B,
@ -525,21 +529,21 @@ mod tests {
tuple: (bool, f32), tuple: (bool, f32),
} }
#[derive(Reflect)] #[derive(Reflect, PartialEq, Debug)]
struct B { struct B {
foo: usize, foo: usize,
łø: C, łø: C,
} }
#[derive(Reflect)] #[derive(Reflect, PartialEq, Debug)]
struct C { struct C {
mосква: f32, mосква: f32,
} }
#[derive(Reflect)] #[derive(Reflect, PartialEq, Debug)]
struct D(E); struct D(E);
#[derive(Reflect)] #[derive(Reflect, PartialEq, Debug)]
struct E(f32, usize); struct E(f32, usize);
#[derive(Reflect, PartialEq, Debug)] #[derive(Reflect, PartialEq, Debug)]
@ -739,6 +743,7 @@ mod tests {
fn reflect_path() { fn reflect_path() {
let mut a = a_sample(); let mut a = a_sample();
assert_eq!(*a.path::<A>("").unwrap(), a);
assert_eq!(*a.path::<usize>("w").unwrap(), 1); assert_eq!(*a.path::<usize>("w").unwrap(), 1);
assert_eq!(*a.path::<usize>("x.foo").unwrap(), 10); assert_eq!(*a.path::<usize>("x.foo").unwrap(), 10);
assert_eq!(*a.path::<f32>("x.łørđ.mосква").unwrap(), 3.14); assert_eq!(*a.path::<f32>("x.łørđ.mосква").unwrap(), 3.14);