Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bevy_transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bevy_math = { path = "../bevy_math", version = "0.19.0-dev", default-features =
"approx",
] }
approx = "0.5.1"
ron = "0.8"

[features]
# Turning off default features leaves you with a barebones
Expand Down
80 changes: 80 additions & 0 deletions crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fn assert_is_normalized(message: &str, length_squared: f32) {
/// [transform_example]: https://github.com/bevyengine/bevy/blob/latest/examples/transforms/transform.rs
#[derive(Debug, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", serde(default))]
#[cfg_attr(
feature = "bevy-support",
derive(Component),
Expand Down Expand Up @@ -670,3 +671,82 @@ impl Mul<Vec3> for Transform {
reflect(Serialize, Deserialize)
)]
pub struct TransformTreeChanged;

#[cfg(all(test, feature = "serialize"))]
mod tests {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove these tests. They are testing ron's implementation and usage of serde(default), not code that we own or maintain. This is a waste of compute resources that could be spent elsewhere (or not spent at all).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alice-i-cecile thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, we can cut them.

use super::*;
use ron;

// use "serialize" feature flag for these

// make sure an empty config will serialize and deserialize properly
#[test]
fn test_empty_transform_defaults() {
let ron_data = "()";

let transform: Transform = ron::de::from_str(ron_data).unwrap();

assert_eq!(transform, Transform::IDENTITY);
}

#[test]
fn test_missing_rotation_and_scale() {
let ron_data = r#"
(
translation: (5.0, -2.0, 10.0),
)
"#;

let transform: Transform = ron::de::from_str(ron_data).unwrap();

assert_eq!(transform.translation, Vec3::new(5.0, -2.0, 10.0));
assert_eq!(transform.rotation, Quat::IDENTITY);
assert_eq!(transform.scale, Vec3::ONE);
}

#[test]
fn test_only_scale_provided() {
let ron_data = r#"
(
scale: (3.0, 4.0, 5.0),
)
"#;

let transform: Transform = ron::de::from_str(ron_data).unwrap();

assert_eq!(transform.translation, Vec3::ZERO);
assert_eq!(transform.rotation, Quat::IDENTITY);
assert_eq!(transform.scale, Vec3::new(3.0, 4.0, 5.0));
}

// round trip
#[test]
fn test_roundtrip_preserves_values() {
let original = Transform {
translation: Vec3::new(1.0, 2.0, 3.0),
rotation: Quat::IDENTITY,
scale: Vec3::splat(2.0),
};

let ron = ron::ser::to_string(&original).unwrap();
let deserialized: Transform = ron::de::from_str(&ron).unwrap();

assert_eq!(original, deserialized);
}

#[test]
fn test_partial_values_mix() {
let ron_data = r#"
(
translation: (9.0, 8.0, 7.0),
scale: (0.5, 0.5, 0.5),
)
"#;

let transform: Transform = ron::de::from_str(ron_data).unwrap();

assert_eq!(transform.translation, Vec3::new(9.0, 8.0, 7.0));
assert_eq!(transform.scale, Vec3::splat(0.5));
assert_eq!(transform.rotation, Quat::IDENTITY);
}
}