Round trip unit test
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user