-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of how to use TestPlugin
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//! This example illustrates test systems. | ||
fn main() { | ||
println!("This example is special! Run it with `cargo test --example automated_tests`."); | ||
println!( | ||
"Or use `cargo test --example automated_tests -- --nocapture` to see the debug output." | ||
); | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use bevy::prelude::*; | ||
|
||
#[test] | ||
fn simple_test() { | ||
// Setup the app with the TestPlugins – these will run fine in tests and in CI. | ||
// Note that many 3rd-party plugins will require DefaultPlugins, not just TestPlugins. | ||
let mut app = App::new(); | ||
app.add_plugins(TestPlugins).add_system(increment); | ||
|
||
// Spawn a new entity with a Counter component, and record its ID. | ||
let counter_id = app.world.spawn().insert(Counter::default()).id(); | ||
|
||
// Simulate for a 10 frames | ||
let num_frames = 10; | ||
for _ in 0..num_frames { | ||
app.update(); | ||
} | ||
|
||
// Check that the counter was incremented 10 times. | ||
let count = app.world.get::<Counter>(counter_id).unwrap().counter; | ||
assert_eq!(count, num_frames); | ||
|
||
println!("Success!"); | ||
} | ||
// Define a system and a component that we can use in our test. | ||
#[derive(Debug, Default, Component, Clone, Copy)] | ||
struct Counter { | ||
counter: u64, | ||
} | ||
|
||
/// Increment the counter every frame | ||
fn increment(mut query: Query<&mut Counter>) { | ||
for mut counter in query.iter_mut() { | ||
counter.counter += 1; | ||
println!("Counter: {}", counter.counter); | ||
} | ||
} | ||
} |