From e64a3cc491f9ca51332eb929027a83ce24c1953a Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Sat, 14 Sep 2024 15:21:18 -0700 Subject: [PATCH] [unstable-rust] Use `feature(assert_matches)`. --- all-is-cubes-port/src/gltf/tests.rs | 5 +- all-is-cubes-port/src/lib.rs | 1 + all-is-cubes-port/src/mv.rs | 9 +- all-is-cubes-ui/src/lib.rs | 1 + all-is-cubes-ui/src/ui_content/vui_manager.rs | 5 +- all-is-cubes/src/block/modifier/mod.rs | 10 +-- .../src/block/modifier/rotate_tests.rs | 89 +++++++++---------- all-is-cubes/src/character/tests.rs | 5 +- all-is-cubes/src/lib.rs | 1 + all-is-cubes/src/universe/universe_txn.rs | 3 +- 10 files changed, 67 insertions(+), 62 deletions(-) diff --git a/all-is-cubes-port/src/gltf/tests.rs b/all-is-cubes-port/src/gltf/tests.rs index a4eb008cc..ac7c0a134 100644 --- a/all-is-cubes-port/src/gltf/tests.rs +++ b/all-is-cubes-port/src/gltf/tests.rs @@ -1,3 +1,4 @@ +use std::assert_matches::assert_matches; use std::path::{Path, PathBuf}; use std::time::Duration; @@ -146,11 +147,11 @@ async fn export_character_not_supported() { ) .await .unwrap_err(); - assert!(matches!( + assert_matches!( error, ExportError::NotRepresentable { name: Some(name), .. } - if name == "x".into())); + if name == "x".into()); } diff --git a/all-is-cubes-port/src/lib.rs b/all-is-cubes-port/src/lib.rs index 7b387a6e6..9e2ee858f 100644 --- a/all-is-cubes-port/src/lib.rs +++ b/all-is-cubes-port/src/lib.rs @@ -1,3 +1,4 @@ +#![feature(assert_matches)] #![feature(large_assignments)] #![move_size_limit = "5000"] #![feature(let_chains)] diff --git a/all-is-cubes-port/src/mv.rs b/all-is-cubes-port/src/mv.rs index aa22864d6..5d408153d 100644 --- a/all-is-cubes-port/src/mv.rs +++ b/all-is-cubes-port/src/mv.rs @@ -335,7 +335,8 @@ fn aic_to_mv_coordinate_transform(aic_bounds: GridAab) -> Gridgid { #[cfg(test)] mod tests { - use super::*; + use std::assert_matches::assert_matches; + use super::*; use all_is_cubes::block::BlockDef; use all_is_cubes::universe::Handle; use all_is_cubes::util::yield_progress_for_testing; @@ -475,7 +476,7 @@ mod tests { ) .await .unwrap_err(); - assert!(matches!(error, ExportError::NotRepresentable { .. })); + assert_matches!(error, ExportError::NotRepresentable { .. }); } #[cfg(feature = "export")] @@ -492,13 +493,13 @@ mod tests { ) .await .unwrap_err(); - assert!(matches!( + assert_matches!( error, ExportError::NotRepresentable { name: Some(name), .. } - if name == "x".into())); + if name == "x".into()); } // TODO: add tests of loading valid files (we will need to create test data files) diff --git a/all-is-cubes-ui/src/lib.rs b/all-is-cubes-ui/src/lib.rs index 701658996..632e2a445 100644 --- a/all-is-cubes-ui/src/lib.rs +++ b/all-is-cubes-ui/src/lib.rs @@ -1,3 +1,4 @@ +#![feature(assert_matches)] #![feature(large_assignments)] #![move_size_limit = "2500"] // TODO: look at `Session` size #![feature(let_chains)] diff --git a/all-is-cubes-ui/src/ui_content/vui_manager.rs b/all-is-cubes-ui/src/ui_content/vui_manager.rs index b07e784cc..1f60659f6 100644 --- a/all-is-cubes-ui/src/ui_content/vui_manager.rs +++ b/all-is-cubes-ui/src/ui_content/vui_manager.rs @@ -580,6 +580,7 @@ pub(crate) enum CueMessage { #[cfg(test)] mod tests { use super::*; + use std::assert_matches::assert_matches; async fn new_vui_for_test(paused: bool) -> (Vui, flume::Receiver) { let (cctx, ccrx) = flume::bounded(1); @@ -603,7 +604,7 @@ mod tests { let (mut vui, control_channel) = new_vui_for_test(false).await; vui.back(); let msg = control_channel.try_recv().unwrap(); - assert!(matches!(msg, ControlMessage::TogglePause), "{msg:?}"); + assert_matches!(msg, ControlMessage::TogglePause); assert!(control_channel.try_recv().is_err()); } @@ -613,7 +614,7 @@ mod tests { vui.set_state(VuiPageState::Paused); vui.back(); let msg = control_channel.try_recv().unwrap(); - assert!(matches!(msg, ControlMessage::TogglePause), "{msg:?}"); + assert_matches!(msg, ControlMessage::TogglePause); assert!(control_channel.try_recv().is_err()); } } diff --git a/all-is-cubes/src/block/modifier/mod.rs b/all-is-cubes/src/block/modifier/mod.rs index b0dcc8ece..d81b61234 100644 --- a/all-is-cubes/src/block/modifier/mod.rs +++ b/all-is-cubes/src/block/modifier/mod.rs @@ -287,6 +287,7 @@ mod tests { use super::*; use crate::block::BlockAttributes; use pretty_assertions::assert_eq; + /// Track the size of the `Modifier` enum to make sure we don't accidentally make it bigger /// by giving one variant more data. @@ -313,10 +314,8 @@ mod tests { Modifier::Composite(Composite::new(block::AIR, CompositeOperator::Over)), Modifier::Inventory(inv::Inventory::from_slots([])), ]; - assert_eq!( - format!("{modifiers:#?}"), - indoc::indoc! { - r#"[ + assert_eq!(format!("{modifiers:#?}"), indoc::indoc! { + r#"[ BlockAttributes { display_name: "hello", }, @@ -336,7 +335,6 @@ mod tests { slots: [], }, ]"# - } - ); + }); } } diff --git a/all-is-cubes/src/block/modifier/rotate_tests.rs b/all-is-cubes/src/block/modifier/rotate_tests.rs index 932deab9c..fc8be6b9f 100644 --- a/all-is-cubes/src/block/modifier/rotate_tests.rs +++ b/all-is-cubes/src/block/modifier/rotate_tests.rs @@ -2,6 +2,8 @@ //! //! The modifier implementation itself is so simple that it does not have its own file. +use std::assert_matches::assert_matches; + use super::*; use crate::block::{ BlockAttributes, BlockCollision, EvaluatedBlock, Evoxel, Primitive, Resolution::R2, TickAction, @@ -51,54 +53,51 @@ fn rotate_evaluation() { let rotated = block.clone().rotate(rotation); let re = rotated.evaluate().unwrap(); - assert_eq!( - re, - EvaluatedBlock { - block: rotated.clone(), - voxels: Evoxels::from_many( - R2, + assert_eq!(re, EvaluatedBlock { + block: rotated.clone(), + voxels: Evoxels::from_many( + R2, + Vol::from_fn(block_bounds, |cube| { + Evoxel { + color: rotated_color_fn(cube), + emission: Rgb::ZERO, + selectable: true, + collision: BlockCollision::Hard, + } + }) + ), + attributes: BlockAttributes { + display_name: "foo".into(), + tick_action: Some(TickAction::from(Operation::Become( + replacement.rotate(rotation).clone() + ))), + rotation_rule: block::RotationPlacementRule::Attach { by: Face6::PY }, + ..BlockAttributes::default() + }, + cost: block::Cost { + components: 3, // Primitive + display_name + Rotate + voxels: 2u32.pow(3) * 2, // original + rotation + recursion: 0 + }, + derived: block::Derived { + color: be.color(), + face_colors: be.face_colors().rotate(rotation), + light_emission: Rgb::ZERO, + opaque: FaceMap::splat(false).with(rotation.transform(Face6::NY), true), + visible: true, + uniform_collision: Some(BlockCollision::Hard), + voxel_opacity_mask: block::VoxelOpacityMask::new_raw( + resolution, Vol::from_fn(block_bounds, |cube| { - Evoxel { - color: rotated_color_fn(cube), - emission: Rgb::ZERO, - selectable: true, - collision: BlockCollision::Hard, + if cube.x == 0 { + OpacityCategory::Opaque + } else { + OpacityCategory::Invisible } }) ), - attributes: BlockAttributes { - display_name: "foo".into(), - tick_action: Some(TickAction::from(Operation::Become( - replacement.rotate(rotation).clone() - ))), - rotation_rule: block::RotationPlacementRule::Attach { by: Face6::PY }, - ..BlockAttributes::default() - }, - cost: block::Cost { - components: 3, // Primitive + display_name + Rotate - voxels: 2u32.pow(3) * 2, // original + rotation - recursion: 0 - }, - derived: block::Derived { - color: be.color(), - face_colors: be.face_colors().rotate(rotation), - light_emission: Rgb::ZERO, - opaque: FaceMap::splat(false).with(rotation.transform(Face6::NY), true), - visible: true, - uniform_collision: Some(BlockCollision::Hard), - voxel_opacity_mask: block::VoxelOpacityMask::new_raw( - resolution, - Vol::from_fn(block_bounds, |cube| { - if cube.x == 0 { - OpacityCategory::Opaque - } else { - OpacityCategory::Invisible - } - }) - ), - }, - } - ); + }, + }); } /// Check that [`Block::rotate`]'s pre-composition is consistent with the interpretation @@ -107,7 +106,7 @@ fn rotate_evaluation() { fn rotate_rotated_consistency() { let mut universe = Universe::new(); let [block] = make_some_voxel_blocks(&mut universe); - assert!(matches!(block.primitive(), Primitive::Recur { .. })); + assert_matches!(block.primitive(), Primitive::Recur { .. }); // Two rotations not in the same plane, so they are not commutative. let rotation_1 = GridRotation::RyXZ; diff --git a/all-is-cubes/src/character/tests.rs b/all-is-cubes/src/character/tests.rs index f9a56a31d..27707a2c5 100644 --- a/all-is-cubes/src/character/tests.rs +++ b/all-is-cubes/src/character/tests.rs @@ -1,4 +1,5 @@ use alloc::sync::Arc; +use std::assert_matches::assert_matches; use euclid::{point3, Vector3D}; @@ -258,13 +259,13 @@ fn click_wrong_space_or_correct_space() { let cursor = cursor_raycast(Ray::new([0.5, 0.5, 0.5], [1., 0., 0.]), &sp1, 10.); assert!(cursor.is_some()); let error = Character::click(character.clone(), cursor.as_ref(), 0).unwrap_err(); - assert!(matches!(error, ToolError::Internal(_))); + assert_matches!(error, ToolError::Internal(_)); // Click in right space let cursor = cursor_raycast(Ray::new([0.5, 0.5, 0.5], [1., 0., 0.]), &sp2, 10.); assert!(cursor.is_some()); let error = Character::click(character, cursor.as_ref(), 0).unwrap_err(); - assert!(matches!(error, ToolError::NoTool)); + assert_matches!(error, ToolError::NoTool); } #[test] diff --git a/all-is-cubes/src/lib.rs b/all-is-cubes/src/lib.rs index c56075a50..d0159ffca 100644 --- a/all-is-cubes/src/lib.rs +++ b/all-is-cubes/src/lib.rs @@ -1,3 +1,4 @@ +#![feature(assert_matches)] #![feature(doc_notable_trait)] #![feature(impl_trait_in_assoc_type)] #![feature(large_assignments)] diff --git a/all-is-cubes/src/universe/universe_txn.rs b/all-is-cubes/src/universe/universe_txn.rs index b5dbf7cd0..8355332f0 100644 --- a/all-is-cubes/src/universe/universe_txn.rs +++ b/all-is-cubes/src/universe/universe_txn.rs @@ -980,6 +980,7 @@ mod tests { use crate::universe::{self, HandleError}; use alloc::sync::Arc; use indoc::indoc; + use std::assert_matches::assert_matches; #[test] fn has_default() { @@ -1215,7 +1216,7 @@ mod tests { let t1 = SpaceTransaction::set_cube([0, 0, 0], None, Some(block)).bind(s1); let e = t1.execute(&mut u2, &mut drop).unwrap_err(); - assert!(matches!(e, ExecuteError::Check(_))); + assert_matches!(e, ExecuteError::Check(_)); } #[test]