Skip to content

Commit

Permalink
Refactor!: Rename crate::shrink::number to int
Browse files Browse the repository at this point in the history
Make the shrinker name and module more specific and distinct.

This is a preparation step for in the future also adding
shrinkers for types f32 and f64, which are also numbers, but
will not be supported by the existing shrinker.
  • Loading branch information
jockbert committed Mar 1, 2024
1 parent 2d72255 commit b4a9826
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/gen/integers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ where
}

fn shrinker(&self) -> BoxShrink<E> {
crate::shrink::number()
crate::shrink::int()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/gen/mix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ mod test {
#[test]
fn mix_should_reuse_shrinker_from_first_generator() {
let with_shrinker = super::mix_evenly(&[
crate::gen::u8::any().with_shrinker(crate::shrink::number()),
crate::gen::u8::any().with_shrinker(crate::shrink::int()),
crate::gen::u8::any().with_shrinker(crate::shrink::none()),
]);

let without_shrinker = super::mix_evenly(&[
crate::gen::u8::any().with_shrinker(crate::shrink::none()),
crate::gen::u8::any().with_shrinker(crate::shrink::number()),
crate::gen::u8::any().with_shrinker(crate::shrink::int()),
]);

assert_generator_can_shrink(with_shrinker, 10);
Expand Down
2 changes: 1 addition & 1 deletion src/gen/other_shrink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{BoxGen, BoxIter, BoxShrink, Gen};
/// assert!(gen2.shrinker().candidates(123).next().is_none());
///
/// // let generator have other shrinker again (alternative way)
/// let gen3 = gen2.with_shrinker(shrink::number());
/// let gen3 = gen2.with_shrinker(shrink::int());
/// assert!(gen3.shrinker().candidates(123).next().is_some());
/// ```
pub fn other_shrinker<E: Clone + 'static>(
Expand Down
2 changes: 1 addition & 1 deletion src/gen/sized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Gen<usize> for SizeGen {
}

fn shrinker(&self) -> BoxShrink<usize> {
crate::shrink::number()
crate::shrink::int()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/shrink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
mod bool;
pub mod fixed;
mod integer;
mod map;
mod no_shrink;
mod num_shrink;
pub mod vec;
mod zip;

pub use bool::bool;
pub use bool::bool_to_true;
pub use integer::int_to_zero as int;
pub use map::map;
pub use no_shrink::none;
pub use num_shrink::to_zero as number;
pub use zip::zip;
10 changes: 5 additions & 5 deletions src/shrink/num_shrink.rs → src/shrink/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ use crate::Shrink;
use min_max_traits::Max;
use num_traits::Num;

/// Shrink number types towards the value zero.
pub fn to_zero<E>() -> BoxShrink<E>
/// Shrink integer types towards the value zero.
pub fn int_to_zero<E>() -> BoxShrink<E>
where
E: Num + Copy + std::cmp::PartialOrd + Max + 'static,
{
Box::new(NumShrink {})
Box::new(IntShrink {})
}

#[derive(Clone)]
struct NumShrink {}
struct IntShrink {}

impl<E> Shrink<E> for NumShrink
impl<E> Shrink<E> for IntShrink
where
E: Num + Copy + std::cmp::PartialOrd + Max + 'static,
{
Expand Down
10 changes: 5 additions & 5 deletions src/shrink/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ struct MappedShrink<E0, E1> {
/// use monkey_test::*;
///
/// let number_string_shrinker: BoxShrink<String> = shrink::map(
/// shrink::number::<i64>(),
/// shrink::int::<i64>(),
/// |i: i64| i.to_string(),
/// |s: String| s.parse().unwrap(),
/// );
///
/// let even_numbers_only_shrinker: BoxShrink<i64> = shrink::map(
/// shrink::number::<i64>(),
/// shrink::int::<i64>(),
/// |i:i64| i * 2,
/// |even: i64| even / 2,
/// );
///
/// // Shorthand way to do the same thing
/// let even_numbers_only_shrinker_2: BoxShrink<i64> = shrink::number::<i64>()
/// let even_numbers_only_shrinker_2: BoxShrink<i64> = shrink::int::<i64>()
/// .map(
/// |i:i64| i * 2,
/// |even: i64| even / 2,
Expand Down Expand Up @@ -70,8 +70,8 @@ where

#[cfg(test)]
mod test {
use crate::shrink::int;
use crate::shrink::none;
use crate::shrink::number;
use crate::testing::assert_shrinker_has_at_least_these_candidates;
use crate::BoxShrink;

Expand All @@ -92,7 +92,7 @@ mod test {
#[test]
fn returns_some_other_stringified_numbers() {
let shrink: BoxShrink<String> = super::map(
number::<i64>(),
int::<i64>(),
|i| i.to_string(),
|s: String| s.parse().unwrap(),
);
Expand Down
12 changes: 6 additions & 6 deletions src/shrink/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ struct ZipShrink<E0, E1> {
/// ```rust
/// use monkey_test::*;
///
/// let alfa1: BoxShrink<u8> = shrink::number::<u8>();
/// let beta1: BoxShrink<i64> = shrink::number::<i64>();
/// let alfa1: BoxShrink<u8> = shrink::int::<u8>();
/// let beta1: BoxShrink<i64> = shrink::int::<i64>();
///
/// let alfa2: BoxShrink<u8> = shrink::number::<u8>();
/// let beta2: BoxShrink<i64> = shrink::number::<i64>();
/// let alfa2: BoxShrink<u8> = shrink::int::<u8>();
/// let beta2: BoxShrink<i64> = shrink::int::<i64>();
///
///
/// // Zip two shrinkers to a tuple shrinker.
Expand Down Expand Up @@ -67,8 +67,8 @@ where

#[cfg(test)]
mod test {
use crate::shrink::int;
use crate::shrink::none;
use crate::shrink::number;
use crate::testing::assert_shrinker_has_at_least_these_candidates;
use crate::BoxShrink;

Expand All @@ -86,7 +86,7 @@ mod test {
/// initial behaviour.
#[test]
fn returns_permutations_of_inner_candidates() {
let shrink: BoxShrink<(u8, u8)> = super::zip(number(), number());
let shrink: BoxShrink<(u8, u8)> = super::zip(int(), int());

assert_shrinker_has_at_least_these_candidates(
shrink,
Expand Down
6 changes: 3 additions & 3 deletions tests/basic_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn can_fail_with_details_when_using_check() {
let actual_result: MonkeyResult<u8> = monkey_test()
.with_seed(123456)
.with_generator(gen::fixed::sequence(&[1, 2, 3, 10, 20, 30]))
.with_shrinker(shrink::number())
.with_shrinker(shrink::int())
.test_property(|x| x < 13);

assert_eq!(
Expand All @@ -34,7 +34,7 @@ fn can_fail_with_panic_when_using_assert() {
monkey_test()
.with_seed(123456)
.with_generator(gen::fixed::sequence(&[1, 2, 3, 10, 20, 30]))
.with_shrinker(shrink::number())
.with_shrinker(shrink::int())
.assert_true(|x| x < 15);
}

Expand All @@ -44,7 +44,7 @@ fn can_assert_minimumfail_with_panic_when_using_assert() {
monkey_test()
.with_seed(123456)
.with_generator(gen::fixed::sequence(&[1, 2, 3, 10, 20, 30]))
.with_shrinker(shrink::number())
.with_shrinker(shrink::int())
.test_property(|x| x < 15)
.assert_minimum_failure(15);
}
Expand Down

0 comments on commit b4a9826

Please sign in to comment.