Skip to content

Commit

Permalink
Implement Arbitrary for TickAction, Operation, and VoxelBrush.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid committed Oct 21, 2023
1 parent 577c7b9 commit 54716cd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 3 additions & 2 deletions all-is-cubes/src/block/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl<'a> arbitrary::Arbitrary<'a> for BlockAttributes {
display_name: u.arbitrary::<alloc::string::String>()?.into(),
selectable: u.arbitrary()?,
rotation_rule: u.arbitrary()?,
tick_action: None, // TODO: need Arbitrary for Block
tick_action: u.arbitrary()?,
animation_hint: u.arbitrary()?,
})
}
Expand All @@ -174,7 +174,7 @@ impl<'a> arbitrary::Arbitrary<'a> for BlockAttributes {
alloc::string::String::size_hint(depth),
bool::size_hint(depth),
RotationPlacementRule::size_hint(depth),
crate::math::Rgb::size_hint(depth),
TickAction::size_hint(depth),
AnimationHint::size_hint(depth),
])
}
Expand Down Expand Up @@ -364,6 +364,7 @@ impl AnimationChange {
///
/// Stored in [`BlockAttributes`].
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[allow(clippy::exhaustive_structs)] // will deliberately break
pub struct TickAction {
/// Operation to perform on the schedule.
Expand Down
27 changes: 27 additions & 0 deletions all-is-cubes/src/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,33 @@ impl crate::universe::VisitRefs for VoxelBrush<'_> {
}
}

#[cfg(feature = "arbitrary")]
#[mutants::skip]
impl<'a, 'b> arbitrary::Arbitrary<'a> for VoxelBrush<'b> {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(VoxelBrush(
u.arbitrary_iter()?
.map(|result| {
result.map(|(offset, block): ([i32; 3], Block)| {
(offset.into(), Cow::Owned(block))
})
})
.collect::<arbitrary::Result<Vec<(GridVector, Cow<'b, Block>)>>>()?,
))
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
use arbitrary::{
size_hint::{and, and_all},
Arbitrary,
};
and(
and(GridAab::size_hint(depth), bool::size_hint(depth)),
and_all(&[<f64 as Arbitrary>::size_hint(depth); 6]),
)
}
}

/// Converts the return value of [`Space::set`] to the return value of
/// [`DrawTarget::draw_pixel`], by making out-of-bounds not an error.
fn ignore_out_of_bounds(result: Result<bool, SetCubeError>) -> Result<(), SetCubeError> {
Expand Down
1 change: 1 addition & 0 deletions all-is-cubes/src/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::universe::VisitRefs;
/// part of `BlockAttribtes` and `EvaluatedBlock` it is cloned a lot.
#[doc = include_str!("save/serde-warning.md")]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[non_exhaustive]
pub enum Operation {
/// Apply the brush centered on the cube.
Expand Down

0 comments on commit 54716cd

Please sign in to comment.