From 7d32dfec18f89dc2977a7836618580cdf9e9b006 Mon Sep 17 00:00:00 2001 From: AlephCubed <76791009+AlephCubed@users.noreply.github.com> Date: Mon, 26 May 2025 20:15:30 -0700 Subject: [PATCH] Add `insert_if_new` test for sparse set. (#19387) Fixes #19081. Simply created a duplicate of the existing `insert_if_new` test, but using sparse sets. ## Testing: The test passes on main, but fails if #19059 is reverted. --- crates/bevy_ecs/src/bundle.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/crates/bevy_ecs/src/bundle.rs b/crates/bevy_ecs/src/bundle.rs index a7fb4f6fd4..9871a33866 100644 --- a/crates/bevy_ecs/src/bundle.rs +++ b/crates/bevy_ecs/src/bundle.rs @@ -2237,6 +2237,28 @@ mod tests { assert_eq!(entity.get(), Some(&V("one"))); } + #[derive(Component, Debug, Eq, PartialEq)] + #[component(storage = "SparseSet")] + pub struct SparseV(&'static str); + + #[derive(Component, Debug, Eq, PartialEq)] + #[component(storage = "SparseSet")] + pub struct SparseA; + + #[test] + fn sparse_set_insert_if_new() { + let mut world = World::new(); + let id = world.spawn(SparseV("one")).id(); + let mut entity = world.entity_mut(id); + entity.insert_if_new(SparseV("two")); + entity.insert_if_new((SparseA, SparseV("three"))); + entity.flush(); + // should still contain "one" + let entity = world.entity(id); + assert!(entity.contains::()); + assert_eq!(entity.get(), Some(&SparseV("one"))); + } + #[test] fn sorted_remove() { let mut a = vec![1, 2, 3, 4, 5, 6, 7];