Skip to content

Commit

Permalink
Refactor tests (#230)
Browse files Browse the repository at this point in the history
* Refactor tests

Current integration tests categorization doesn't scale well for the
amount of tests cases we have. I made the following changes to address
it:

1. Split `init_replication` test into `removals`, `insertion`, `spawn`
   and `despawn`.
2. Split insertion and removal test with multiple components into
   separate tests.
3. Thanks to code coverage I found a missing test case for spawning an
   entity from a mapped component, added.
  • Loading branch information
Shatur authored Apr 15, 2024
1 parent 13c3fbd commit 70db508
Show file tree
Hide file tree
Showing 6 changed files with 950 additions and 654 deletions.
11 changes: 5 additions & 6 deletions tests/update_replication.rs → tests/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ fn with_insertion() {
}),
))
.replicate::<BoolComponent>()
.replicate::<TableComponent>();
.replicate::<DummyComponent>();
}

server_app.connect_client(&mut client_app);
Expand All @@ -179,15 +179,15 @@ fn with_insertion() {

let mut server_entity = server_app.world.entity_mut(server_entity);
server_entity.get_mut::<BoolComponent>().unwrap().0 = true;
server_entity.insert(TableComponent);
server_entity.insert(DummyComponent);

server_app.update();
server_app.exchange_with_client(&mut client_app);
client_app.update();

let component = client_app
.world
.query_filtered::<&BoolComponent, With<TableComponent>>()
.query_filtered::<&BoolComponent, With<DummyComponent>>()
.single(&client_app.world);
assert!(component.0);
}
Expand All @@ -204,8 +204,7 @@ fn with_despawn() {
..Default::default()
}),
))
.replicate::<BoolComponent>()
.replicate::<TableComponent>();
.replicate::<BoolComponent>();
}

server_app.connect_client(&mut client_app);
Expand Down Expand Up @@ -384,7 +383,7 @@ fn acknowledgment() {
}

#[derive(Component, Deserialize, Serialize)]
struct TableComponent;
struct DummyComponent;

#[derive(Clone, Component, Copy, Deserialize, Serialize)]
struct BoolComponent(bool);
Expand Down
122 changes: 122 additions & 0 deletions tests/despawn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
use bevy::prelude::*;
use bevy_replicon::{
client::client_mapper::ServerEntityMap, prelude::*, test_app::ServerTestAppExt,
};
use serde::{Deserialize, Serialize};

#[test]
fn single() {
let mut server_app = App::new();
let mut client_app = App::new();
for app in [&mut server_app, &mut client_app] {
app.add_plugins((
MinimalPlugins,
RepliconPlugins.set(ServerPlugin {
tick_policy: TickPolicy::EveryFrame,
..Default::default()
}),
));
}

server_app.connect_client(&mut client_app);

let server_entity = server_app.world.spawn(Replication).id();
let client_entity = client_app.world.spawn(Replication).id();

client_app
.world
.resource_mut::<ServerEntityMap>()
.insert(server_entity, client_entity);

server_app.world.despawn(server_entity);

server_app.update();
server_app.exchange_with_client(&mut client_app);
client_app.update();

assert!(client_app.world.entities().is_empty());

let entity_map = client_app.world.resource::<ServerEntityMap>();
assert!(entity_map.to_client().is_empty());
assert!(entity_map.to_server().is_empty());
}

#[test]
fn with_heirarchy() {
let mut server_app = App::new();
let mut client_app = App::new();
for app in [&mut server_app, &mut client_app] {
app.add_plugins((
MinimalPlugins,
RepliconPlugins.set(ServerPlugin {
tick_policy: TickPolicy::EveryFrame,
..Default::default()
}),
));
}

server_app.connect_client(&mut client_app);

let server_child_entity = server_app.world.spawn(Replication).id();
let server_entity = server_app
.world
.spawn(Replication)
.push_children(&[server_child_entity])
.id();

let client_child_entity = client_app.world.spawn(Replication).id();
let client_entity = client_app
.world
.spawn(Replication)
.push_children(&[client_child_entity])
.id();

let mut entity_map = client_app.world.resource_mut::<ServerEntityMap>();
entity_map.insert(server_entity, client_entity);
entity_map.insert(server_child_entity, client_child_entity);

server_app.world.despawn(server_entity);
server_app.world.despawn(server_child_entity);

server_app.update();
server_app.exchange_with_client(&mut client_app);
client_app.update();

server_app.world.despawn(server_entity);
server_app.world.despawn(server_child_entity);

assert!(client_app.world.entities().is_empty());
}

#[test]
fn after_spawn() {
let mut server_app = App::new();
let mut client_app = App::new();
for app in [&mut server_app, &mut client_app] {
app.add_plugins((
MinimalPlugins,
RepliconPlugins.set(ServerPlugin {
tick_policy: TickPolicy::EveryFrame,
..Default::default()
}),
))
.replicate::<DummyComponent>();
}

server_app.connect_client(&mut client_app);

// Insert and remove `Replication` to trigger spawn and despawn for client at the same time.
server_app
.world
.spawn((Replication, DummyComponent))
.remove::<Replication>();

server_app.update();
server_app.exchange_with_client(&mut client_app);
client_app.update();

assert!(client_app.world.entities().is_empty());
}

#[derive(Component, Deserialize, Serialize)]
struct DummyComponent;
Loading

0 comments on commit 70db508

Please sign in to comment.