Skip to content

Provide an API for fetching SystemParam from World #2089

@concave-sphere

Description

@concave-sphere

What problem does this solve or what need does it fill?

I want to write tests of a custom SystemParam.

For systems that act on resources and components, I can just use APIs on Worldto fetch the final state of the resources and components.

But a SystemParam doesn't have side effects -- instead, it has an API that I want to exercise.

What solution would you like?

I'd like to be able to fetch SystemParam instances out of World the same way I can fetch components and resources:

let app = new_test_app();
// set up app.world with state

let param: MySystemParam = app.world.get_system_param_mut();
assert_eq!(wanted_value, param.method1(args...));
// Call a method for side effects:
param.method2(args...);
assert_eq!(wanted_state, get_state(&app.world));

What alternative(s) have you considered?

I can write a test system, something like this:

struct TestMySystemParam(fn(MySystemParam) -> Result<(), anyhow::Error>);
// The test resource is optional so I can share the app setup between several
// system params in one module.
fn test_my_system_param(
    opt_test: Option<Res<TestMySystemParam>>,
    mut out: ResMut<Result<anyhow::Error>>,
    my_system_param: MySystemParam) {
  if let Some(test) = opt_test {
    *out = test(my_system_param);
  }
}

#[test] my_test() {
  let app = new_test_app();
  // Set up app.world.

  app.world.insert_resource(TestMySystemParam(|param| {
    let got = param.method1();
    ensure!(want == got, "method1: wanted {:?}, got {:?}", want, got);
  }));
  app.update();
  let result: Result<(), anyhow::Error> = app.world.get_resource();
  result.unwrap();
}

However, this is kind of fiddly, and I have to do it for every SystemParam I want to test. It would be much nicer if Bevy could handle the details for me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-FeatureA new feature, making something new possible

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions