Skip to content

Commit 19985af

Browse files
committed
remove git repo in test_utils
1 parent 8c16d8c commit 19985af

File tree

4 files changed

+236
-0
lines changed

4 files changed

+236
-0
lines changed

crates/test_utils/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "test_utils"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[dependencies]
8+
bevy = { workspace = true }
9+
10+
[lib]
11+
path = "src/lib.rs"

crates/test_utils/src/asserts.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

crates/test_utils/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod asserts;
2+
pub mod test_data;

crates/test_utils/src/test_data.rs

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
use std::alloc::Layout;
2+
use std::sync::{Arc, RwLock};
3+
4+
use bevy::ecs::{component::*, world::World};
5+
use bevy::prelude::*;
6+
use bevy::reflect::*;
7+
8+
/// Test component with Reflect and ReflectComponent registered
9+
#[derive(Component, Reflect, PartialEq, Eq, Debug)]
10+
#[reflect(Component)]
11+
pub struct TestComponent {
12+
pub strings: Vec<String>,
13+
}
14+
15+
impl TestComponent {
16+
pub fn init() -> Self {
17+
Self {
18+
strings: vec!["Initial".to_string(), "Value".to_string()],
19+
}
20+
}
21+
}
22+
23+
/// Test Resource with Reflect and ReflectResource registered
24+
#[derive(Resource, Reflect, Default, PartialEq, Eq, Debug)]
25+
#[reflect(Resource)]
26+
pub struct TestResource {
27+
pub bytes: Vec<u8>,
28+
}
29+
30+
impl TestResource {
31+
pub fn init() -> Self {
32+
Self {
33+
bytes: vec![0, 1, 2, 3, 4, 5],
34+
}
35+
}
36+
}
37+
38+
/// Component with Reflect and ReflectFromWorld registered but no ReflectComponent
39+
#[derive(Reflect, Component, PartialEq, Debug)]
40+
#[reflect(FromWorld)]
41+
pub struct CompWithFromWorld(pub String);
42+
43+
impl Default for CompWithFromWorld {
44+
fn default() -> Self {
45+
Self(String::from("Default"))
46+
}
47+
}
48+
49+
impl CompWithFromWorld {
50+
pub fn init() -> Self {
51+
Self(String::from("Initial Value"))
52+
}
53+
}
54+
55+
/// Component with Reflect and ReflectDefault but no ReflectComponent
56+
#[derive(Component, Reflect, PartialEq, Eq, Debug)]
57+
#[reflect(Default)]
58+
pub struct CompWithDefault(pub String);
59+
60+
impl CompWithDefault {
61+
pub fn init() -> Self {
62+
Self(String::from("Initial Value"))
63+
}
64+
}
65+
66+
impl Default for CompWithDefault {
67+
fn default() -> Self {
68+
Self(String::from("Default"))
69+
}
70+
}
71+
72+
#[derive(Component, Reflect, PartialEq, Eq, Debug)]
73+
#[reflect(Component, Default)]
74+
pub struct CompWithDefaultAndComponentData(pub String);
75+
impl Default for CompWithDefaultAndComponentData {
76+
fn default() -> Self {
77+
Self(String::from("Default"))
78+
}
79+
}
80+
81+
impl CompWithDefaultAndComponentData {
82+
pub fn init() -> Self {
83+
Self(String::from("Initial Value"))
84+
}
85+
}
86+
87+
#[derive(Component, Reflect, PartialEq, Eq, Debug)]
88+
#[reflect(Component, FromWorld)]
89+
pub struct CompWithFromWorldAndComponentData(pub String);
90+
impl Default for CompWithFromWorldAndComponentData {
91+
fn default() -> Self {
92+
Self(String::from("Default"))
93+
}
94+
}
95+
96+
impl CompWithFromWorldAndComponentData {
97+
pub fn init() -> Self {
98+
Self(String::from("Initial Value"))
99+
}
100+
}
101+
102+
pub(crate) const TEST_COMPONENT_ID_START: usize = 20;
103+
pub(crate) const TEST_ENTITY_ID_START: u32 = 0;
104+
105+
pub trait GetTestComponentId {
106+
fn test_component_id() -> ComponentId;
107+
}
108+
109+
pub trait GetTestEntityId {
110+
fn test_entity_id() -> Entity;
111+
}
112+
113+
pub trait EnumerateTestComponents {
114+
fn enumerate_test_components() -> Vec<(&'static str, ComponentId, Option<Entity>)>;
115+
}
116+
117+
macro_rules! impl_test_component_ids {
118+
([$($comp_type:ty => $comp_id:expr),* $(,)?], [$($res_type:ty => $res_id:expr),* $(,)?]) => {
119+
$(
120+
impl GetTestComponentId for $comp_type {
121+
fn test_component_id() -> ComponentId {
122+
ComponentId::new(TEST_COMPONENT_ID_START + $comp_id)
123+
}
124+
}
125+
126+
impl GetTestEntityId for $comp_type {
127+
fn test_entity_id() -> Entity {
128+
Entity::from_raw(TEST_ENTITY_ID_START + $comp_id)
129+
}
130+
}
131+
)*
132+
$(
133+
impl GetTestComponentId for $res_type {
134+
fn test_component_id() -> ComponentId {
135+
ComponentId::new(TEST_COMPONENT_ID_START + $res_id)
136+
}
137+
}
138+
)*
139+
140+
pub(crate) fn init_all_components(world: &mut World, registry: &mut TypeRegistry) {
141+
$(
142+
world.register_component::<$comp_type>();
143+
registry.register::<$comp_type>();
144+
let registered_id = world.component_id::<$comp_type>().unwrap().index();
145+
assert_eq!(registered_id, TEST_COMPONENT_ID_START + $comp_id, "Test setup failed. Did you register components before running setup_world?");
146+
let entity = world.spawn(<$comp_type>::init()).id();
147+
assert_eq!(entity.index(), TEST_ENTITY_ID_START + $comp_id, "Test setup failed. Did you spawn entities before running setup_world?");
148+
assert_eq!(entity.generation(), 1, "Test setup failed. Did you spawn entities before running setup_world?");
149+
)*
150+
$(
151+
world.init_resource::<$res_type>();
152+
registry.register::<$res_type>();
153+
let registered_id = world.resource_id::<$res_type>().unwrap().index();
154+
assert_eq!(registered_id, TEST_COMPONENT_ID_START + $res_id, "Test setup failed. Did you register components before running setup_world?");
155+
)*
156+
}
157+
158+
impl EnumerateTestComponents for World {
159+
fn enumerate_test_components() -> Vec<(&'static str, ComponentId, Option<Entity>)> {
160+
vec![
161+
$(
162+
(std::any::type_name::<$comp_type>(), <$comp_type as GetTestComponentId>::test_component_id(), Some(<$comp_type as GetTestEntityId>::test_entity_id()))
163+
),*
164+
$(
165+
,(std::any::type_name::<$res_type>(), <$res_type as GetTestComponentId>::test_component_id(), None)
166+
)*
167+
168+
]
169+
}
170+
}
171+
};
172+
}
173+
174+
impl_test_component_ids!(
175+
[ TestComponent => 0,
176+
CompWithFromWorld => 1,
177+
CompWithDefault => 2,
178+
CompWithDefaultAndComponentData => 3,
179+
CompWithFromWorldAndComponentData => 4
180+
],
181+
[
182+
TestResource => 5
183+
]
184+
);
185+
186+
/// Initializes a default world with a set of test components and resources with various properties and implemantations.
187+
pub fn setup_world<F: FnOnce(&mut World, &mut TypeRegistry)>(init: F) -> World {
188+
let mut world = World::default();
189+
190+
// find the number of ComponentId's registered, fill it up until we hit the offset
191+
while world.components().len() < TEST_COMPONENT_ID_START {
192+
unsafe {
193+
world.register_component_with_descriptor(ComponentDescriptor::new_with_layout(
194+
format!("Filler{}", world.components().len()),
195+
StorageType::Table,
196+
Layout::new::<usize>(),
197+
None,
198+
))
199+
};
200+
}
201+
202+
let mut type_registry = TypeRegistry::new();
203+
init_all_components(&mut world, &mut type_registry);
204+
205+
init(&mut world, &mut type_registry);
206+
207+
world.insert_resource(AppTypeRegistry(TypeRegistryArc {
208+
internal: Arc::new(RwLock::new(type_registry)),
209+
}));
210+
211+
world
212+
}
213+
214+
#[cfg(test)]
215+
mod test {
216+
use super::*;
217+
218+
#[test]
219+
fn setup_works() {
220+
setup_world(|_, _| {});
221+
}
222+
}

0 commit comments

Comments
 (0)