Skip to content

Commit

Permalink
More tests (#278)
Browse files Browse the repository at this point in the history
* Tests spawn scene

* release-testing

* registry-testing

* fix-test

* into_target and editor event registration
  • Loading branch information
naomijub authored Apr 1, 2024
1 parent 948090e commit 3b07d83
Show file tree
Hide file tree
Showing 5 changed files with 625 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- name: Install Dependencies
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev libwayland-dev
- name: Run cargo test
run: cargo test --workspace --no-default-features
run: cargo test --workspace --no-default-features --release

test_all:
name: Test all features
Expand All @@ -72,7 +72,7 @@ jobs:
- name: Install Dependencies
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev libwayland-dev
- name: Run cargo test
run: cargo test --workspace --all-features
run: cargo test --workspace --all-features --release

# Run cargo clippy -- -D warnings
clippy_check:
Expand Down
Binary file added crates/prefab/assets/gabe-idle-run.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
383 changes: 383 additions & 0 deletions crates/prefab/assets/low_poly_fighter_2.gltf

Large diffs are not rendered by default.

137 changes: 134 additions & 3 deletions crates/prefab/src/editor_registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl EditorRegistryExt for App {
self.editor_registry::<T>();

self.add_systems(OnEnter(SaveState::Save), generate_auto_structs::<T>);
self.add_systems(Update, despawn_auto_structs::<T>);
self.add_systems(Update, clear_auto_structs::<T>);
self
}

Expand Down Expand Up @@ -386,7 +386,7 @@ fn generate_auto_structs<T: Component + Reflect + FromReflect + Default + Clone>
}

/// Not used
fn despawn_auto_structs<T: Component + Reflect + FromReflect + Default + Clone>(
fn clear_auto_structs<T: Component + Reflect + FromReflect + Default + Clone>(
mut commands: Commands,
query: Query<(Entity, &AutoStruct<T>)>,
assets: Res<AssetServer>,
Expand All @@ -410,7 +410,47 @@ fn relation_system<T: Component, Relation: Component + Default>(
mod tests {
use bevy::{ecs::system::CommandQueue, prelude::*};

use crate::prelude::{EditorRegistry, EditorRegistryExt, EditorRegistryPlugin};
use super::*;

#[test]
fn entity_relation_test() {
#[derive(Component, Default)]
struct TestRelation;

let mut app = App::new();
app.add_plugins(MinimalPlugins);
app.add_systems(Update, relation_system::<Name, TestRelation>)
.add_systems(Startup, |mut commands: Commands| {
commands.spawn(Name::from("value"));
});
app.update();

let mut query = app.world.query::<(&Name, &TestRelation)>();
let s = query.single(&app.world);

assert_eq!(s.0, &Name::from("value"));
}

#[test]
fn entity_relation_plugin_test() {
#[derive(Component, Default, Reflect)]
struct TestRelation;

let mut app = App::new();
app.add_plugins((MinimalPlugins, EditorRegistryPlugin));
app.editor_registry::<Name>();
app.editor_registry::<TestRelation>();
app.editor_relation::<Name, TestRelation>();
app.add_systems(Startup, |mut commands: Commands| {
commands.spawn(Name::from("value"));
});
app.update();

let mut query = app.world.query::<(&Name, &TestRelation)>();
let s = query.single(&app.world);

assert_eq!(s.0, &Name::from("value"));
}

/// Test for clone logic in editor registry
#[test]
Expand Down Expand Up @@ -442,4 +482,95 @@ mod tests {
name
);
}

#[test]
fn send_events() {
#[derive(Default, Event, Resource, Clone, Debug)]
struct AnEvent {
val: usize,
}

let mut app = App::new();
app.add_plugins(MinimalPlugins)
.init_resource::<AnEvent>()
.add_event::<AnEvent>();

let send_event = SendEvent::new::<AnEvent>();
assert_eq!(send_event.name(), "AnEvent");
assert_eq!(
send_event.path(),
"space_prefab::editor_registry::tests::send_events::AnEvent"
);
assert_eq!(send_event.type_id, TypeId::of::<AnEvent>());

send_event.send(&mut app.world);
app.update();

let events = app.world.resource::<Events<AnEvent>>();
let mut events_reader = events.get_reader();
let an_event = events_reader.read(events).next().unwrap();

// Check the event has been sent
assert_eq!(an_event.val, 0);
let mut events = app.world.resource_mut::<Events<AnEvent>>();
events.clear();

// Change send event value
app.world.resource_mut::<AnEvent>().val = 17;
app.update();

send_event.send(&mut app.world);
app.update();

let events = app.world.resource::<Events<AnEvent>>();
let mut events_reader = events.get_reader();
let an_event = events_reader.read(events).next().unwrap();

assert_eq!(an_event.val, 17);
}

#[test]
fn into_target_component() {
#[derive(Component, Clone)]
pub struct Named {
name: String,
}

impl Into<Named> for Name {
fn into(self) -> Named {
Named {
name: self.to_string(),
}
}
}
let mut app = App::new();
app.add_plugins(MinimalPlugins)
.add_systems(Startup, |mut cmd: Commands| {
cmd.spawn(Name::from("value"));
})
.add_systems(Update, into_sync_system::<Name, Named>);

app.update();

let mut query = app.world.query::<(&Name, &Named)>();
let s = query.single(&app.world);
assert_eq!(s.1.name, "value");
}

#[test]
fn event_editor_registration() {
#[derive(Default, Event, Resource, Clone, Debug, Reflect)]
struct AnEvent {
val: usize,
}

let mut app = App::new();
app.add_plugins((MinimalPlugins, EditorRegistryPlugin))
.editor_registry_event::<AnEvent>()
.add_event::<AnEvent>();
app.update();

let registry = app.world.resource::<EditorRegistry>();
assert_eq!("AnEvent", registry.send_events.first().unwrap().name);
}
}
112 changes: 106 additions & 6 deletions crates/prefab/src/spawn_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn spawn_scene(
auto_children: Query<&SceneAutoChild>,
asset_server: Res<AssetServer>,
) {
for (e, prefab, children, vis, tr, auto_child) in prefabs.iter() {
for (e, prefab, children, visibility, transform, auto_child) in prefabs.iter() {
if let Some(children) = children {
for e in children {
if auto_children.contains(*e) {
Expand All @@ -50,14 +50,14 @@ pub fn spawn_scene(
} else if is_auto_child {
cmd.insert(SceneAutoChild);
} else {
cmd.insert(SceneAutoChild).insert(PrefabMarker);
cmd.insert((SceneAutoChild, PrefabMarker));
}
}));

if vis.is_none() {
if visibility.is_none() {
commands.entity(e).insert(VisibilityBundle::default());
}
if tr.is_none() {
if transform.is_none() {
commands.entity(e).insert(TransformBundle::default());
}
}
Expand Down Expand Up @@ -250,9 +250,9 @@ pub fn spawn_player_start(

#[cfg(test)]
mod tests {
use bevy::sprite::Mesh2dHandle;

use super::*;
use bevy::scene::ScenePlugin;
use bevy::sprite::Mesh2dHandle;

#[test]
fn sync_cube_mesh() {
Expand Down Expand Up @@ -485,4 +485,104 @@ mod tests {
.iter(&app.world)
.for_each(|d| assert!(possibilities.contains(&d.0)));
}

#[test]
fn sync_spritesheet_to_spritesheetbundle() {
let mut app = App::new();
app.add_plugins((
MinimalPlugins,
AssetPlugin::default(),
ImagePlugin::default(),
))
.init_resource::<Assets<TextureAtlasLayout>>()
.init_resource::<Assets<Image>>();
app.add_systems(Startup, |mut commands: Commands| {
commands.spawn((
SpritesheetTexture {
texture: String::from("gabe-idle-run.png"),
},
AnimationIndicesSpriteSheet::default(),
TextureAtlasPrefab::default(),
AnimationClipName::default(),
));
});
app.add_systems(Update, sync_spritesheet);

app.update();

let mut query = app.world.query::<(&TextureAtlas, &Sprite)>();

assert_eq!(query.iter(&app.world).count(), 1);
}

#[test]
fn spawns_gltf() {
#[derive(Component)]
struct DespawnTestChild;

let mut app = App::new();
app.add_plugins((
MinimalPlugins,
AssetPlugin::default(),
ScenePlugin::default(),
));
app.add_systems(Startup, |mut commands: Commands| {
let child = commands.spawn((SceneAutoChild, DespawnTestChild)).id();
commands
.spawn(GltfPrefab {
path: String::from("low_poly_fighter_2.gltf"),
scene: String::from("Scene0"),
})
.add_child(child);
})
.add_systems(Update, spawn_scene);

app.update();

let mut query = app
.world
.query::<(&Handle<Scene>, &SceneAutoRoot, &Visibility, &Transform)>();

let s = query.single(&app.world);

assert_eq!(
s.0.path().unwrap().to_string(),
"low_poly_fighter_2.gltf#Scene0"
);

let mut query = app.world.query::<(Entity, &DespawnTestChild)>();
assert!(query.get_single(&app.world).is_err());
}

#[test]
fn spawns_gltf_with_visibility_and_transform() {
let mut app = App::new();
app.add_plugins((
MinimalPlugins,
AssetPlugin::default(),
ScenePlugin::default(),
));
app.add_systems(Startup, |mut commands: Commands| {
commands.spawn((
GltfPrefab {
path: String::from("low_poly_fighter_2.gltf"),
scene: String::from("Scene0"),
},
Visibility::Hidden,
TransformBundle::IDENTITY,
));
})
.add_systems(Update, spawn_scene);

app.update();

let mut query = app
.world
.query::<(&Handle<Scene>, &Visibility, &Transform)>();

let s = query.single(&app.world);

assert_eq!(s.1, Visibility::Hidden);
assert_eq!(s.2, &Transform::IDENTITY);
}
}

0 comments on commit 3b07d83

Please sign in to comment.