Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test checking that types are registered (as far as possible) #6061

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions crates/bevy_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ impl Plugin for CorePlugin {
.unwrap_or_default()
.create_default_pools();

app.register_type::<Entity>().register_type::<Name>();
app.register_type::<Entity>()
.register_type::<Vec<Entity>>()
.register_type::<Name>();

register_rust_types(app);
register_math_types(app);
Expand All @@ -47,7 +49,8 @@ fn register_rust_types(app: &mut App) {
.register_type::<Option<String>>()
.register_type::<Cow<'static, str>>()
.register_type::<Duration>()
.register_type::<Instant>();
.register_type::<Instant>()
.register_type::<Option<Instant>>();
}

fn register_math_types(app: &mut App) {
Expand Down Expand Up @@ -81,5 +84,8 @@ fn register_math_types(app: &mut App) {
.register_type::<bevy_math::Mat3A>()
.register_type::<bevy_math::Mat4>()
.register_type::<bevy_math::DQuat>()
.register_type::<bevy_math::Quat>();
.register_type::<bevy_math::Quat>()
.register_type::<bevy_math::Rect>()
.register_type::<Option<bevy_math::Rect>>()
.register_type::<Option<bevy_math::Vec2>>();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose to only register Option<Vec2> here. We could in theory register Option<T>, Vec<T> etc. for every conceivable type, but since there's no need to do this where the types are defined and it would introduce a lot of unnecessary TypeInfo into the binary I opted to only register those that are currently in use.

}
2 changes: 2 additions & 0 deletions crates/bevy_hierarchy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod events;
pub use events::*;

mod valid_parent_check_plugin;
use smallvec::SmallVec;
pub use valid_parent_check_plugin::*;

#[doc(hidden)]
Expand All @@ -36,6 +37,7 @@ pub struct HierarchyPlugin;
impl Plugin for HierarchyPlugin {
fn build(&self, app: &mut App) {
app.register_type::<Children>()
.register_type::<SmallVec<[bevy_ecs::prelude::Entity; 8]>>()
.register_type::<Parent>()
.add_event::<HierarchyEvent>();
}
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_render/src/camera/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl Plugin for CameraPlugin {
fn build(&self, app: &mut App) {
app.register_type::<Camera>()
.register_type::<Viewport>()
.register_type::<Option<Viewport>>()
.register_type::<Visibility>()
.register_type::<ComputedVisibility>()
.register_type::<VisibleEntities>()
Expand Down
105 changes: 105 additions & 0 deletions tests/check_types_registered.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use std::any::TypeId;

use bevy::{
prelude::*,
reflect::{TypeInfo, TypeRegistryInternal, VariantInfo},
};

#[test]
fn check_components_resources_registered() {
let mut app = App::new();
app.add_plugins_with(DefaultPlugins, |g| g.disable::<bevy::winit::WinitPlugin>().disable::<bevy::log::LogPlugin>());

let type_registry = app.world.resource::<AppTypeRegistry>();
let type_registry = type_registry.read();

for registration in app
.world
.components()
.iter()
.filter_map(|info| info.type_id())
.filter_map(|type_id| type_registry.get(type_id))
{
if app
.world
.components()
.get_resource_id(registration.type_id())
.is_some()
{
assert!(
registration.data::<ReflectResource>().is_some(),
"resource {} has no `ReflectResource`",
registration.short_name()
);
} else {
assert!(
registration.data::<ReflectComponent>().is_some(),
"component {} has no `ReflectComponent`",
registration.short_name()
);
}
}
}

#[test]
fn check_types_registered_recursive() {
let mut app = App::new();
app.add_plugins_with(DefaultPlugins, |g| g.disable::<bevy::winit::WinitPlugin>().disable::<bevy::log::LogPlugin>());

let type_registry = app.world.resource::<AppTypeRegistry>();
let type_registry = type_registry.read();

for registration in type_registry.iter() {
assert_registered_recursive(
&*type_registry,
registration.type_id(),
registration.type_name(),
);
}
}

fn assert_registered_recursive(
type_registry: &TypeRegistryInternal,
type_id: TypeId,
name: &'static str,
) {
let registration = type_registry
.get(type_id)
.unwrap_or_else(|| panic!("{name} is not registered"));
match registration.type_info() {
TypeInfo::Struct(info) => info.iter().for_each(|field| {
assert_registered_recursive(type_registry, field.type_id(), field.type_name())
}),
TypeInfo::TupleStruct(info) => info.iter().for_each(|field| {
assert_registered_recursive(type_registry, field.type_id(), field.type_name())
}),
TypeInfo::Tuple(info) => info.iter().for_each(|field| {
assert_registered_recursive(type_registry, field.type_id(), field.type_name())
}),
TypeInfo::List(info) => {
assert_registered_recursive(type_registry, info.item_type_id(), info.item_type_name())
}
TypeInfo::Array(info) => {
assert_registered_recursive(type_registry, info.item_type_id(), info.item_type_name())
}
TypeInfo::Map(info) => {
assert_registered_recursive(type_registry, info.key_type_id(), info.key_type_name());
assert_registered_recursive(
type_registry,
info.value_type_id(),
info.value_type_name(),
);
}
TypeInfo::Enum(info) => info.iter().for_each(|variant| match variant {
VariantInfo::Struct(variant) => variant.iter().for_each(|field| {
assert_registered_recursive(type_registry, field.type_id(), field.type_name())
}),
VariantInfo::Tuple(variant) => variant.iter().for_each(|field| {
assert_registered_recursive(type_registry, field.type_id(), field.type_name())
}),
VariantInfo::Unit(_) => {}
}),
TypeInfo::Value(_) => {}
TypeInfo::Dynamic(_) => todo!(),
}
}