Added tests for Spawn, SpawnIter, SpawnWith, WithRelated and WithOneRelated

This commit is contained in:
Freyja-moth 2025-05-03 19:04:56 +01:00
parent a94bb1137c
commit 61b9f04fdc

View File

@ -426,3 +426,121 @@ macro_rules! related {
<$relationship_target>::spawn(($($crate::spawn::Spawn($child)),*))
};
}
#[cfg(test)]
mod tests {
use std::{
string::ToString,
vec::{self, Vec},
};
use crate::{
name::Name,
prelude::{ChildOf, Children, Component, RelationshipTarget},
query::With,
relationship::RelatedSpawner,
world::World,
};
use super::{Spawn, SpawnIter, SpawnRelated, SpawnWith, WithOneRelated, WithRelated};
#[test]
fn spawn() {
let mut world = World::new();
let parent = world
.spawn((
Name::new("Parent"),
Children::spawn(Spawn(Name::new("Child1"))),
))
.id();
let children = world
.query::<&Children>()
.get(&world, parent)
.expect("An entity with Children should exist");
assert_eq!(children.iter().count(), 1);
}
#[test]
fn spawn_iter() {
let mut world = World::new();
let parent = world
.spawn((
Name::new("Parent"),
Children::spawn(SpawnIter(["Child1", "Child2"].into_iter().map(Name::new))),
))
.id();
let children = world
.query::<&Children>()
.get(&world, parent)
.expect("An entity with Children should exist");
assert_eq!(children.iter().count(), 2);
}
#[test]
fn spawn_with() {
let mut world = World::new();
let parent = world
.spawn((
Name::new("Parent"),
Children::spawn(SpawnWith(|parent: &mut RelatedSpawner<ChildOf>| {
parent.spawn(Name::new("Child1"));
parent.spawn(Name::new("Child2"));
})),
))
.id();
let children = world
.query::<&Children>()
.get(&world, parent)
.expect("An entity with Children should exist");
assert_eq!(children.iter().count(), 2);
}
#[test]
fn with_related() {
let mut world = World::new();
let child1 = world.spawn(Name::new("Child1")).id();
let child2 = world.spawn(Name::new("Child2")).id();
let parent = world
.spawn((
Name::new("Parent"),
Children::spawn(WithRelated([child1, child2].into_iter())),
))
.id();
let children = world
.query::<&Children>()
.get(&world, parent)
.expect("An entity with Children should exist");
assert_eq!(children.iter().count(), 2);
}
#[test]
fn with_one_related() {
let mut world = World::new();
let child1 = world.spawn(Name::new("Child1")).id();
let parent = world
.spawn((Name::new("Parent"), Children::spawn(WithOneRelated(child1))))
.id();
let children = world
.query::<&Children>()
.get(&world, parent)
.expect("An entity with Children should exist");
assert_eq!(children.iter().count(), 1);
}
}