Skip to content

Commit 7cebeee

Browse files
Add num_entities() to World (bevyengine#19780)
There is a lot of `world.entities().len()`, especially in tests. In tests, usually, the assumption is made that empty worlds do not contain any entities. This is about to change (bevyengine#19711), and as such all of these tests are failing for that PR. `num_entities` is a convenience method that returns the number of entities inside a world. It can later be adapted to exclude 'unexpected' entities, associated with internal data structures such as Resources, Queries, Systems. In general I argue for a separation of concepts where `World` ignores internal entities in methods such as `iter_entities()` and `clear_entities()`, that discussion is, however, separate from this PR. I replaced most occurrences of `world.entities().len()` with `world.num_entities()` and the tests passed. --------- Co-authored-by: Alice Cecile <[email protected]>
1 parent e2b9078 commit 7cebeee

File tree

6 files changed

+16
-9
lines changed

6 files changed

+16
-9
lines changed

crates/bevy_app/src/app.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,8 +1582,7 @@ mod tests {
15821582
app.add_systems(EnterMainMenu, (foo, bar));
15831583

15841584
app.world_mut().run_schedule(EnterMainMenu);
1585-
let num_resources = app.world_mut().query::<&IsResource>().iter(&app.world()).count() as u32;
1586-
assert_eq!(app.world().entities().len(), num_resources + 2);
1585+
assert_eq!(app.world().num_entities(), 2);
15871586
}
15881587

15891588
#[test]

crates/bevy_ecs/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,7 @@ mod tests {
16401640

16411641
assert_eq!(q1.iter(&world).len(), 1);
16421642
assert_eq!(q2.iter(&world).len(), 1);
1643-
assert_eq!(world.entities().len() - num_resources, 2);
1643+
assert_eq!(world.num_entities(), 2);
16441644

16451645
world.clear_entities();
16461646

@@ -1655,7 +1655,7 @@ mod tests {
16551655
"world should not contain sparse set components"
16561656
);
16571657
assert_eq!(
1658-
world.entities().len(),
1658+
world.num_entities(),
16591659
0,
16601660
"world should not have any entities"
16611661
);

crates/bevy_ecs/src/observer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ mod tests {
13161316
world.spawn(A).flush();
13171317
assert_eq!(vec!["add_2", "add_1"], world.resource::<Order>().0);
13181318
// Our A entity plus our two observers
1319-
assert_eq!(world.entities().len() - num_resources, 3);
1319+
assert_eq!(world.num_entities(), 3);
13201320
}
13211321

13221322
#[test]

crates/bevy_ecs/src/system/commands/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2343,7 +2343,7 @@ mod tests {
23432343
.spawn((W(1u32), W(2u64)))
23442344
.id();
23452345
command_queue.apply(&mut world);
2346-
assert_eq!(world.entities().len() - num_resources, 1);
2346+
assert_eq!(world.num_entities(), 1);
23472347
let results = world
23482348
.query::<(&W<u32>, &W<u64>)>()
23492349
.iter(&world)

crates/bevy_ecs/src/world/command_queue.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,12 @@ mod test {
424424
let num_resources = world.components().num_resources() as u32;
425425
queue.apply(&mut world);
426426

427-
assert_eq!(world.entities().len() - num_resources, 2);
427+
assert_eq!(world.num_entities(), 2);
428428

429429
// The previous call to `apply` cleared the queue.
430430
// This call should do nothing.
431431
queue.apply(&mut world);
432-
assert_eq!(world.entities().len() - num_resources, 2);
432+
assert_eq!(world.num_entities(), 2);
433433
}
434434

435435
#[expect(
@@ -464,7 +464,7 @@ mod test {
464464
queue.push(SpawnCommand);
465465
queue.push(SpawnCommand);
466466
queue.apply(&mut world);
467-
assert_eq!(world.entities().len() - num_resources, 3);
467+
assert_eq!(world.num_entities(), 3);
468468
}
469469

470470
#[test]

crates/bevy_ecs/src/world/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ impl World {
218218
&mut self.entities
219219
}
220220

221+
/// Retrieves the number of [`Entities`] in the world.
222+
///
223+
/// This is helpful as a diagnostic, but it can also be used effectively in tests.
224+
#[inline]
225+
pub fn num_entities(&self) -> u32 {
226+
self.entities.len()
227+
}
228+
221229
/// Retrieves this world's [`Archetypes`] collection.
222230
#[inline]
223231
pub fn archetypes(&self) -> &Archetypes {

0 commit comments

Comments
 (0)