Round trip unit test

This commit is contained in:
2025-07-20 16:32:18 -04:00
parent babb74b2f7
commit 6afc35924c
2 changed files with 30 additions and 14 deletions

View File

@@ -18,6 +18,6 @@ impl PrefabAsset {
}
#[typetag::serde(tag = "type")]
pub trait ComponentBuilder {
pub trait ComponentBuilder: Send {
fn insert(&self, entity: &mut EntityCommands);
}

View File

@@ -1,37 +1,40 @@
use bevy::{
ecs::{component::Component, system::EntityCommands},
math::{Vec3, VectorSpace},
render::view::PostProcessWrite,
ecs::{
component::Component,
system::EntityCommands,
world::{CommandQueue, World},
},
math::Vec3,
};
use serde::{Deserialize, Serialize};
use crate::prefab::{ComponentBuilder, PrefabAsset};
#[derive(Component, Serialize, Deserialize)]
#[derive(Component, Serialize, Deserialize, Clone)]
struct Name {
pub name: String,
}
#[typetag::serde]
impl ComponentBuilder for Name {
fn insert(&self, entity: &mut EntityCommands) {
todo!()
fn insert(&self, _entity: &mut EntityCommands) {
assert_eq!(self.name, "Test".to_string(), "Name");
}
}
#[derive(Component, Serialize, Deserialize)]
#[derive(Component, Serialize, Deserialize, Clone)]
struct Position {
pub pos: Vec3,
}
#[typetag::serde]
impl ComponentBuilder for Position {
fn insert(&self, entity: &mut EntityCommands) {
todo!()
fn insert(&self, _entity: &mut EntityCommands) {
assert_eq!(self.pos, Vec3::X, "Position");
}
}
#[test]
fn serialize_test() {
fn round_trip_test() {
let prefab = PrefabAsset {
components: vec![
Box::new(Position { pos: Vec3::X }),
@@ -42,11 +45,24 @@ fn serialize_test() {
};
let serialized = ron::to_string(&prefab);
assert!(serialized.is_ok());
assert!(serialized.is_ok(), "Failed to serialzied");
let deserialize = ron::from_str::<PrefabAsset>(&serialized.unwrap());
assert!(deserialize.is_ok());
assert!(deserialize.is_ok(), "Failed to deserialize");
let loaded_asset = deserialize.unwrap();
assert!(loaded_asset.components.len() == prefab.components.len());
assert_eq!(
loaded_asset.components.len(),
prefab.components.len(),
"Prefab's components count do not match"
);
let mut world = World::new();
let e = world.spawn_empty().id();
let mut commands = world.commands();
let mut ec = commands.entity(e);
for component in loaded_asset.components {
component.insert(&mut ec);
}
}