From 63d17e8494a62cf12dc552e10cbf1f53fc2d8bdc Mon Sep 17 00:00:00 2001 From: Tygyh <32486062+tygyh@users.noreply.github.com> Date: Sun, 17 Dec 2023 00:58:41 +0100 Subject: [PATCH] Simplify equality assertions (#10988) # Objective - Shorten assertions. ## Solution - Replace '==' assertions with 'assert_eq()' and '!=' assertions with 'assert_ne()' . --- crates/bevy_derive/src/bevy_main.rs | 6 +-- crates/bevy_ecs/src/change_detection.rs | 4 +- crates/bevy_ecs/src/entity/mod.rs | 8 ++-- crates/bevy_ecs/src/schedule/mod.rs | 2 +- crates/bevy_ecs/src/system/commands/mod.rs | 2 +- crates/bevy_ecs/src/system/function_system.rs | 2 +- crates/bevy_ecs/src/world/entity_ref.rs | 2 +- crates/bevy_hierarchy/src/hierarchy.rs | 2 +- crates/bevy_input/src/touch.rs | 2 +- crates/bevy_ptr/src/lib.rs | 7 +-- crates/bevy_render/src/render_graph/graph.rs | 45 +++++++++++-------- crates/bevy_render/src/texture/image.rs | 9 ++-- 12 files changed, 51 insertions(+), 40 deletions(-) diff --git a/crates/bevy_derive/src/bevy_main.rs b/crates/bevy_derive/src/bevy_main.rs index 36773c214b89a..ac7d6a5e9d1a8 100644 --- a/crates/bevy_derive/src/bevy_main.rs +++ b/crates/bevy_derive/src/bevy_main.rs @@ -4,9 +4,9 @@ use syn::{parse_macro_input, ItemFn}; pub fn bevy_main(_attr: TokenStream, item: TokenStream) -> TokenStream { let input = parse_macro_input!(item as ItemFn); - assert!( - input.sig.ident == "main", - "`bevy_main` can only be used on a function called 'main'.", + assert_eq!( + input.sig.ident, "main", + "`bevy_main` can only be used on a function called 'main'." ); TokenStream::from(quote! { diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index 27a6c08c1cd1f..f5226824c203e 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -1051,8 +1051,8 @@ mod tests { for tracker in query.iter(&world) { let ticks_since_insert = change_tick.relative_to(*tracker.ticks.added).get(); let ticks_since_change = change_tick.relative_to(*tracker.ticks.changed).get(); - assert!(ticks_since_insert == MAX_CHANGE_AGE); - assert!(ticks_since_change == MAX_CHANGE_AGE); + assert_eq!(ticks_since_insert, MAX_CHANGE_AGE); + assert_eq!(ticks_since_change, MAX_CHANGE_AGE); } } diff --git a/crates/bevy_ecs/src/entity/mod.rs b/crates/bevy_ecs/src/entity/mod.rs index 0ac11ae04c63e..61c12e2fa7f5d 100644 --- a/crates/bevy_ecs/src/entity/mod.rs +++ b/crates/bevy_ecs/src/entity/mod.rs @@ -977,10 +977,10 @@ mod tests { // This is intentionally testing `lt` and `ge` as separate functions. #![allow(clippy::nonminimal_bool)] - assert!(Entity::new(123, 456) == Entity::new(123, 456)); - assert!(Entity::new(123, 789) != Entity::new(123, 456)); - assert!(Entity::new(123, 456) != Entity::new(123, 789)); - assert!(Entity::new(123, 456) != Entity::new(456, 123)); + assert_eq!(Entity::new(123, 456), Entity::new(123, 456)); + assert_ne!(Entity::new(123, 789), Entity::new(123, 456)); + assert_ne!(Entity::new(123, 456), Entity::new(123, 789)); + assert_ne!(Entity::new(123, 456), Entity::new(456, 123)); // ordering is by generation then by index diff --git a/crates/bevy_ecs/src/schedule/mod.rs b/crates/bevy_ecs/src/schedule/mod.rs index 7c7876186613c..d2b32452b7c1b 100644 --- a/crates/bevy_ecs/src/schedule/mod.rs +++ b/crates/bevy_ecs/src/schedule/mod.rs @@ -239,7 +239,7 @@ mod tests { partially_ordered == [8, 9, 10] || partially_ordered == [10, 8, 9], "partially_ordered must be [8, 9, 10] or [10, 8, 9]" ); - assert!(order.len() == 11, "must have exactly 11 order entries"); + assert_eq!(order.len(), 11, "must have exactly 11 order entries"); } } diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index abcbfab0f3b9b..80de73f9f15ab 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -1297,7 +1297,7 @@ mod tests { .spawn((W(1u32), W(2u64))) .id(); command_queue.apply(&mut world); - assert!(world.entities().len() == 1); + assert_eq!(world.entities().len(), 1); let results = world .query::<(&W, &W)>() .iter(&world) diff --git a/crates/bevy_ecs/src/system/function_system.rs b/crates/bevy_ecs/src/system/function_system.rs index 431809ac9e08f..9056a1648153e 100644 --- a/crates/bevy_ecs/src/system/function_system.rs +++ b/crates/bevy_ecs/src/system/function_system.rs @@ -520,7 +520,7 @@ where } fn update_archetype_component_access(&mut self, world: UnsafeWorldCell) { - assert!(self.world_id == Some(world.id()), "Encountered a mismatched World. A System cannot be used with Worlds other than the one it was initialized with."); + assert_eq!(self.world_id, Some(world.id()), "Encountered a mismatched World. A System cannot be used with Worlds other than the one it was initialized with."); let archetypes = world.archetypes(); let old_generation = std::mem::replace(&mut self.archetype_generation, archetypes.generation()); diff --git a/crates/bevy_ecs/src/world/entity_ref.rs b/crates/bevy_ecs/src/world/entity_ref.rs index 586fc03246079..7d21ae61a64f1 100644 --- a/crates/bevy_ecs/src/world/entity_ref.rs +++ b/crates/bevy_ecs/src/world/entity_ref.rs @@ -1812,7 +1812,7 @@ mod tests { assert!(res.is_err()); // Ensure that the location has been properly updated. - assert!(entity.location() != old_location); + assert_ne!(entity.location(), old_location); } // regression test for https://github.com/bevyengine/bevy/pull/7805 diff --git a/crates/bevy_hierarchy/src/hierarchy.rs b/crates/bevy_hierarchy/src/hierarchy.rs index 79b56b9fd183a..817b5e14c5d86 100644 --- a/crates/bevy_hierarchy/src/hierarchy.rs +++ b/crates/bevy_hierarchy/src/hierarchy.rs @@ -272,7 +272,7 @@ mod tests { // The parent's Children component should still have two children. let children = world.entity(parent).get::(); assert!(children.is_some()); - assert!(children.unwrap().len() == 2_usize); + assert_eq!(children.unwrap().len(), 2_usize); // The original child should be despawned. assert!(world.get_entity(child).is_none()); } diff --git a/crates/bevy_input/src/touch.rs b/crates/bevy_input/src/touch.rs index 419e844e181eb..be8235fec0253 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -481,7 +481,7 @@ mod test { assert!(touches.pressed.get(&touch_event.id).is_none()); let touch = touches.just_released.get(&touch_event.id).unwrap(); // Make sure the position is updated from TouchPhase::Moved and TouchPhase::Ended - assert!(touch.previous_position != touch.position); + assert_ne!(touch.previous_position, touch.position); } #[test] diff --git a/crates/bevy_ptr/src/lib.rs b/crates/bevy_ptr/src/lib.rs index bebfc7de51a4c..6d5e230eb8e46 100644 --- a/crates/bevy_ptr/src/lib.rs +++ b/crates/bevy_ptr/src/lib.rs @@ -464,12 +464,13 @@ impl DebugEnsureAligned for *mut T { // ptr.is_aligned_to. // // Replace once https://github.com/rust-lang/rust/issues/96284 is stable. - assert!( - self as usize & (align - 1) == 0, + assert_eq!( + self as usize & (align - 1), + 0, "pointer is not aligned. Address {:p} does not have alignment {} for type {}", self, align, - core::any::type_name::(), + core::any::type_name::() ); self } diff --git a/crates/bevy_render/src/render_graph/graph.rs b/crates/bevy_render/src/render_graph/graph.rs index 03d06b9a4f0ec..0263ede658368 100644 --- a/crates/bevy_render/src/render_graph/graph.rs +++ b/crates/bevy_render/src/render_graph/graph.rs @@ -755,28 +755,33 @@ mod tests { graph.add_slot_edge("C", 0, "D", 0); assert!(input_nodes("A", &graph).is_empty(), "A has no inputs"); - assert!( - output_nodes("A", &graph) == HashSet::from_iter(vec![c_id]), + assert_eq!( + output_nodes("A", &graph), + HashSet::from_iter(vec![c_id]), "A outputs to C" ); assert!(input_nodes("B", &graph).is_empty(), "B has no inputs"); - assert!( - output_nodes("B", &graph) == HashSet::from_iter(vec![c_id]), + assert_eq!( + output_nodes("B", &graph), + HashSet::from_iter(vec![c_id]), "B outputs to C" ); - assert!( - input_nodes("C", &graph) == HashSet::from_iter(vec![a_id, b_id]), + assert_eq!( + input_nodes("C", &graph), + HashSet::from_iter(vec![a_id, b_id]), "A and B input to C" ); - assert!( - output_nodes("C", &graph) == HashSet::from_iter(vec![d_id]), + assert_eq!( + output_nodes("C", &graph), + HashSet::from_iter(vec![d_id]), "C outputs to D" ); - assert!( - input_nodes("D", &graph) == HashSet::from_iter(vec![c_id]), + assert_eq!( + input_nodes("D", &graph), + HashSet::from_iter(vec![c_id]), "C inputs to D" ); assert!(output_nodes("D", &graph).is_empty(), "D has no outputs"); @@ -880,20 +885,24 @@ mod tests { graph.add_node_edges(&["A", "B", "C"]); - assert!( - output_nodes("A", &graph) == HashSet::from_iter(vec![b_id]), + assert_eq!( + output_nodes("A", &graph), + HashSet::from_iter(vec![b_id]), "A -> B" ); - assert!( - input_nodes("B", &graph) == HashSet::from_iter(vec![a_id]), + assert_eq!( + input_nodes("B", &graph), + HashSet::from_iter(vec![a_id]), "A -> B" ); - assert!( - output_nodes("B", &graph) == HashSet::from_iter(vec![c_id]), + assert_eq!( + output_nodes("B", &graph), + HashSet::from_iter(vec![c_id]), "B -> C" ); - assert!( - input_nodes("C", &graph) == HashSet::from_iter(vec![b_id]), + assert_eq!( + input_nodes("C", &graph), + HashSet::from_iter(vec![b_id]), "B -> C" ); } diff --git a/crates/bevy_render/src/texture/image.rs b/crates/bevy_render/src/texture/image.rs index 91c995580dcf2..327ede2be3ece 100644 --- a/crates/bevy_render/src/texture/image.rs +++ b/crates/bevy_render/src/texture/image.rs @@ -573,8 +573,9 @@ impl Image { /// # Panics /// Panics if the `new_size` does not have the same volume as to old one. pub fn reinterpret_size(&mut self, new_size: Extent3d) { - assert!( - new_size.volume() == self.texture_descriptor.size.volume(), + assert_eq!( + new_size.volume(), + self.texture_descriptor.size.volume(), "Incompatible sizes: old = {:?} new = {:?}", self.texture_descriptor.size, new_size @@ -592,8 +593,8 @@ impl Image { /// the `layers`. pub fn reinterpret_stacked_2d_as_array(&mut self, layers: u32) { // Must be a stacked image, and the height must be divisible by layers. - assert!(self.texture_descriptor.dimension == TextureDimension::D2); - assert!(self.texture_descriptor.size.depth_or_array_layers == 1); + assert_eq!(self.texture_descriptor.dimension, TextureDimension::D2); + assert_eq!(self.texture_descriptor.size.depth_or_array_layers, 1); assert_eq!(self.height() % layers, 0); self.reinterpret_size(Extent3d {