Round trip unit test
This commit is contained in:
@@ -18,6 +18,6 @@ impl PrefabAsset {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[typetag::serde(tag = "type")]
|
#[typetag::serde(tag = "type")]
|
||||||
pub trait ComponentBuilder {
|
pub trait ComponentBuilder: Send {
|
||||||
fn insert(&self, entity: &mut EntityCommands);
|
fn insert(&self, entity: &mut EntityCommands);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,37 +1,40 @@
|
|||||||
use bevy::{
|
use bevy::{
|
||||||
ecs::{component::Component, system::EntityCommands},
|
ecs::{
|
||||||
math::{Vec3, VectorSpace},
|
component::Component,
|
||||||
render::view::PostProcessWrite,
|
system::EntityCommands,
|
||||||
|
world::{CommandQueue, World},
|
||||||
|
},
|
||||||
|
math::Vec3,
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::prefab::{ComponentBuilder, PrefabAsset};
|
use crate::prefab::{ComponentBuilder, PrefabAsset};
|
||||||
|
|
||||||
#[derive(Component, Serialize, Deserialize)]
|
#[derive(Component, Serialize, Deserialize, Clone)]
|
||||||
struct Name {
|
struct Name {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
#[typetag::serde]
|
#[typetag::serde]
|
||||||
impl ComponentBuilder for Name {
|
impl ComponentBuilder for Name {
|
||||||
fn insert(&self, entity: &mut EntityCommands) {
|
fn insert(&self, _entity: &mut EntityCommands) {
|
||||||
todo!()
|
assert_eq!(self.name, "Test".to_string(), "Name");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Serialize, Deserialize)]
|
#[derive(Component, Serialize, Deserialize, Clone)]
|
||||||
struct Position {
|
struct Position {
|
||||||
pub pos: Vec3,
|
pub pos: Vec3,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[typetag::serde]
|
#[typetag::serde]
|
||||||
impl ComponentBuilder for Position {
|
impl ComponentBuilder for Position {
|
||||||
fn insert(&self, entity: &mut EntityCommands) {
|
fn insert(&self, _entity: &mut EntityCommands) {
|
||||||
todo!()
|
assert_eq!(self.pos, Vec3::X, "Position");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn serialize_test() {
|
fn round_trip_test() {
|
||||||
let prefab = PrefabAsset {
|
let prefab = PrefabAsset {
|
||||||
components: vec![
|
components: vec![
|
||||||
Box::new(Position { pos: Vec3::X }),
|
Box::new(Position { pos: Vec3::X }),
|
||||||
@@ -42,11 +45,24 @@ fn serialize_test() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let serialized = ron::to_string(&prefab);
|
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());
|
let deserialize = ron::from_str::<PrefabAsset>(&serialized.unwrap());
|
||||||
assert!(deserialize.is_ok());
|
assert!(deserialize.is_ok(), "Failed to deserialize");
|
||||||
|
|
||||||
let loaded_asset = deserialize.unwrap();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user