Use actual tests for SystemParam regression tests (#8270)

# Objective

Our regression tests for `SystemParam` currently consist of a bunch of
loosely dispersed struct definitions. This is messy, and doesn't fully
test their functionality.

## Solution

Group the struct definitions into functions annotated with `#[test]`.
This not only makes the module more organized, but it allows us to call
`assert_is_system`, which has the potential to catch some bugs that
would have been missed with the old approach. Also, this approach is
consistent with how `WorldQuery` regression tests are organized.
This commit is contained in:
JoJoJet 2023-03-30 17:47:36 -04:00 committed by GitHub
parent 300b275edc
commit 98954311b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1563,116 +1563,174 @@ mod tests {
use crate::{ use crate::{
self as bevy_ecs, // Necessary for the `SystemParam` Derive when used inside `bevy_ecs`. self as bevy_ecs, // Necessary for the `SystemParam` Derive when used inside `bevy_ecs`.
query::{ReadOnlyWorldQuery, WorldQuery}, query::{ReadOnlyWorldQuery, WorldQuery},
system::Query, system::{assert_is_system, Query},
}; };
use std::marker::PhantomData; use std::marker::PhantomData;
// Compile test for https://github.com/bevyengine/bevy/pull/2838. // Compile test for https://github.com/bevyengine/bevy/pull/2838.
#[derive(SystemParam)] #[test]
pub struct SpecialQuery< fn system_param_generic_bounds() {
'w, #[derive(SystemParam)]
's, pub struct SpecialQuery<
Q: WorldQuery + Send + Sync + 'static, 'w,
F: ReadOnlyWorldQuery + Send + Sync + 'static = (), 's,
> { Q: WorldQuery + Send + Sync + 'static,
_query: Query<'w, 's, Q, F>, F: ReadOnlyWorldQuery + Send + Sync + 'static = (),
> {
_query: Query<'w, 's, Q, F>,
}
fn my_system(_: SpecialQuery<(), ()>) {}
assert_is_system(my_system);
} }
// Compile tests for https://github.com/bevyengine/bevy/pull/6694. // Compile tests for https://github.com/bevyengine/bevy/pull/6694.
#[test]
fn system_param_flexibility() {
#[derive(SystemParam)]
pub struct SpecialRes<'w, T: Resource> {
_res: Res<'w, T>,
}
#[derive(SystemParam)] #[derive(SystemParam)]
pub struct SpecialRes<'w, T: Resource> { pub struct SpecialLocal<'s, T: FromWorld + Send + 'static> {
_res: Res<'w, T>, _local: Local<'s, T>,
} }
#[derive(SystemParam)] #[derive(Resource)]
pub struct SpecialLocal<'s, T: FromWorld + Send + 'static> { struct R;
_local: Local<'s, T>,
fn my_system(_: SpecialRes<R>, _: SpecialLocal<u32>) {}
assert_is_system(my_system);
} }
#[derive(Resource)] #[derive(Resource)]
pub struct R<const I: usize>; pub struct R<const I: usize>;
// Compile test for https://github.com/bevyengine/bevy/pull/7001. // Compile test for https://github.com/bevyengine/bevy/pull/7001.
#[derive(SystemParam)] #[test]
pub struct ConstGenericParam<'w, const I: usize>(Res<'w, R<I>>); fn system_param_const_generics() {
#[derive(SystemParam)]
pub struct ConstGenericParam<'w, const I: usize>(Res<'w, R<I>>);
// Compile test for https://github.com/bevyengine/bevy/pull/6867. fn my_system(_: ConstGenericParam<0>, _: ConstGenericParam<1000>) {}
#[derive(SystemParam)] assert_is_system(my_system);
pub struct LongParam<'w> {
_r0: Res<'w, R<0>>,
_r1: Res<'w, R<1>>,
_r2: Res<'w, R<2>>,
_r3: Res<'w, R<3>>,
_r4: Res<'w, R<4>>,
_r5: Res<'w, R<5>>,
_r6: Res<'w, R<6>>,
_r7: Res<'w, R<7>>,
_r8: Res<'w, R<8>>,
_r9: Res<'w, R<9>>,
_r10: Res<'w, R<10>>,
_r11: Res<'w, R<11>>,
_r12: Res<'w, R<12>>,
_r13: Res<'w, R<13>>,
_r14: Res<'w, R<14>>,
_r15: Res<'w, R<15>>,
_r16: Res<'w, R<16>>,
} }
#[allow(dead_code)] // Compile test for https://github.com/bevyengine/bevy/pull/6867.
fn long_system(_param: LongParam) { #[test]
crate::system::assert_is_system(long_system); fn system_param_field_limit() {
#[derive(SystemParam)]
pub struct LongParam<'w> {
// Each field should be a distinct type so there will
// be an error if the derive messes up the field order.
_r0: Res<'w, R<0>>,
_r1: Res<'w, R<1>>,
_r2: Res<'w, R<2>>,
_r3: Res<'w, R<3>>,
_r4: Res<'w, R<4>>,
_r5: Res<'w, R<5>>,
_r6: Res<'w, R<6>>,
_r7: Res<'w, R<7>>,
_r8: Res<'w, R<8>>,
_r9: Res<'w, R<9>>,
_r10: Res<'w, R<10>>,
_r11: Res<'w, R<11>>,
_r12: Res<'w, R<12>>,
_r13: Res<'w, R<13>>,
_r14: Res<'w, R<14>>,
_r15: Res<'w, R<15>>,
_r16: Res<'w, R<16>>,
}
fn long_system(_: LongParam) {}
assert_is_system(long_system);
} }
// Compile test for https://github.com/bevyengine/bevy/pull/6919. // Compile test for https://github.com/bevyengine/bevy/pull/6919.
// Regression test for https://github.com/bevyengine/bevy/issues/7447. // Regression test for https://github.com/bevyengine/bevy/issues/7447.
#[derive(SystemParam)] #[test]
struct IgnoredParam<'w, T: Resource, Marker: 'static> { fn system_param_phantom_data() {
_foo: Res<'w, T>, #[derive(SystemParam)]
#[system_param(ignore)] struct IgnoredParam<'w, T: Resource, Marker: 'static> {
marker: PhantomData<&'w Marker>, _foo: Res<'w, T>,
marker2: PhantomData<&'w Marker>, #[system_param(ignore)]
marker: PhantomData<&'w Marker>,
marker2: PhantomData<&'w Marker>,
}
fn my_system(_: IgnoredParam<R<0>, ()>) {}
assert_is_system(my_system);
} }
// Compile tests for https://github.com/bevyengine/bevy/pull/6957. // Compile tests for https://github.com/bevyengine/bevy/pull/6957.
#[test]
fn system_param_struct_variants() {
#[derive(SystemParam)]
pub struct UnitParam;
#[derive(SystemParam)] #[derive(SystemParam)]
pub struct UnitParam; pub struct TupleParam<'w, 's, R: Resource, L: FromWorld + Send + 'static>(
Res<'w, R>,
Local<'s, L>,
);
#[derive(SystemParam)] fn my_system(_: UnitParam, _: TupleParam<R<0>, u32>) {}
pub struct TupleParam<'w, 's, R: Resource, L: FromWorld + Send + 'static>( assert_is_system(my_system);
Res<'w, R>, }
Local<'s, L>,
);
#[derive(Resource)]
struct PrivateResource;
// Regression test for https://github.com/bevyengine/bevy/issues/4200. // Regression test for https://github.com/bevyengine/bevy/issues/4200.
#[derive(SystemParam)] #[test]
pub struct EncapsulatedParam<'w>(Res<'w, PrivateResource>); fn system_param_private_fields() {
#[derive(Resource)]
struct PrivateResource;
#[derive(SystemParam)]
pub struct EncapsulatedParam<'w>(Res<'w, PrivateResource>);
fn my_system(_: EncapsulatedParam) {}
assert_is_system(my_system);
}
// Regression test for https://github.com/bevyengine/bevy/issues/7103. // Regression test for https://github.com/bevyengine/bevy/issues/7103.
#[derive(SystemParam)] #[test]
pub struct WhereParam<'w, 's, Q> fn system_param_where_clause() {
where #[derive(SystemParam)]
Q: 'static + WorldQuery, pub struct WhereParam<'w, 's, Q>
{ where
_q: Query<'w, 's, Q, ()>, Q: 'static + WorldQuery,
{
_q: Query<'w, 's, Q, ()>,
}
fn my_system(_: WhereParam<()>) {}
assert_is_system(my_system);
} }
// Regression test for https://github.com/bevyengine/bevy/issues/1727. // Regression test for https://github.com/bevyengine/bevy/issues/1727.
#[derive(SystemParam)] #[test]
pub struct Collide<'w> { fn system_param_name_collision() {
_x: Res<'w, FetchState>, #[derive(Resource)]
} pub struct FetchState;
#[derive(Resource)] #[derive(SystemParam)]
pub struct FetchState; pub struct Collide<'w> {
_x: Res<'w, FetchState>,
}
fn my_system(_: Collide) {}
assert_is_system(my_system);
}
// Regression test for https://github.com/bevyengine/bevy/issues/8192. // Regression test for https://github.com/bevyengine/bevy/issues/8192.
#[derive(SystemParam)] #[test]
pub struct InvariantParam<'w, 's> { fn system_param_invariant_lifetime() {
_set: ParamSet<'w, 's, (Query<'w, 's, ()>,)>, #[derive(SystemParam)]
pub struct InvariantParam<'w, 's> {
_set: ParamSet<'w, 's, (Query<'w, 's, ()>,)>,
}
fn my_system(_: InvariantParam) {}
assert_is_system(my_system);
} }
} }