0.0.3 |
|
---|---|
0.0.1 |
|
#8 in #碧玉
170KB
2.5K SLoC
测试工具包以支持对碧玉类型的测试,包括
包
状态
系统
- 资源加载。
- 系统在处理过程中使用的任意类型。
测试工具包将设置具有常见包的碧玉 Application
的样板代码最小化,并且可以通过一个薄接口接收通常被多层遮盖的逻辑。
使用方法
以下是一个简单的测试 状态
的示例。更多示例在 示例 部分。
#
#
#
#
#
#
#
#
// #[test]
fn loading_state_adds_load_resource() {
assert!(
sardonyxApplication::blank()
.with_state(|| LoadingState::new())
.with_assertion(|world| {
world.read_resource::<LoadResource>();
})
.run()
.is_ok()
);
}
#
使用以下函数之一初始化碧玉应用程序,每个函数提供一组不同的包
use sardonyx_test::prelude::*;
#[test]
fn test_name() {
// Start with no bundles
sardonyxApplication::blank();
// Start with the following bundles:
//
// * `TransformBundle`
// * `InputBundle`
// * `UiBundle`
//
// The type parameters here are the Axis and Action types for the
// `InputBundle` and `UiBundle`.
use sardonyx::input::StringBindings;
sardonyxApplication::ui_base::<StringBindings>();
// If you need types from the rendering bundle, make sure you have
// the `"test-support"` feature enabled:
//
// ```toml
// # Cargo.toml
// sardonyx = { version = "..", features = ["test-support"] }
// ```
//
// Then you can include the `RenderEmptyBundle`:
use sardonyx::renderer::{types::DefaultBackend, RenderEmptyBundle};
sardonyxApplication::blank()
.with_bundle(RenderEmptyBundle::<DefaultBackend>::new());
}
接下来,使用各种 .with_*(..)
方法附加您希望测试的逻辑
#
#
#
#[test]
fn test_name() {
let visibility = false; // Whether the window should be shown
sardonyxApplication::render_base::<String, String, _>("test_name", visibility)
.with_bundle(MyBundle::new()) // Registers a bundle.
.with_bundle_fn(|| MyNonSendBundle::new()) // Registers a `!Send` bundle.
.with_resource(MyResource::new()) // Adds a resource to the world.
.with_system(MySystem, "my_sys", &[]) // Registers a system with the main
// dispatcher.
// These are run in the order they are invoked.
// You may invoke them multiple times.
.with_setup(|world| { /* do something */ })
.with_state(|| MyState::new())
.with_effect(|world| { /* do something */ })
.with_assertion(|world| { /* do something */ })
// ...
}
最后,调用 .run()
运行应用程序。这将返回 sardonyx::Result<()>
,因此您可以用 assert!(..);
包裹它
#[test]
fn test_name() {
let visibility = false; // Whether the window should be shown
assert!(
sardonyxApplication::render_base("test_name", visibility)
// ...
.run()
.is_ok()
);
}
示例
测试包
#
#
#
#
#
#
// #[test]
fn bundle_registers_system_with_resource() {
assert!(
sardonyxApplication::blank()
.with_bundle(MyBundle)
.with_assertion(|world| { world.read_resource::<ApplicationResource>(); })
.run()
.is_ok()
);
}
#
测试系统
#
#
#
#
#
#
// #[test]
fn system_increases_component_value_by_one() {
assert!(
sardonyxApplication::blank()
.with_system(MySystem, "my_system", &[])
.with_effect(|world| {
let entity = world.create_entity().with(MyComponent(0)).build();
world.insert(EffectReturn(entity));
})
.with_assertion(|world| {
let entity = world.read_resource::<EffectReturn<Entity>>().0.clone();
let my_component_storage = world.read_storage::<MyComponent>();
let my_component = my_component_storage
.get(entity)
.expect("Entity should have a `MyComponent` component.");
// If the system ran, the value in the `MyComponent` should be 1.
assert_eq!(1, my_component.0);
})
.run()
.is_ok()
);
}
#
在自定义分发器中测试系统。当您的系统必须在设置完成后运行时,这很有用
#
#
#
#
#
// #[test]
fn system_increases_resource_value_by_one() {
assert!(
sardonyxApplication::blank()
.with_setup(|world| {
world.insert(MyResource(0));
})
.with_system_single(MySystem, "my_system", &[])
.with_assertion(|world| {
let my_resource = world.read_resource::<MyResource>();
// If the system ran, the value in the `MyResource` should be 1.
assert_eq!(1, my_resource.0);
})
.run()
.is_ok()
);
}
#
依赖关系
~32–47MB
~812K SLoC