use crate::std_facade::fmt;
use crate::strategy::{NewTree, Strategy, ValueTree};
use crate::test_runner::TestRunner;
macro_rules! noshrink {
() => {
fn simplify(&mut self) -> bool {
false
}
fn complicate(&mut self) -> bool {
false
}
};
}
#[derive(Clone, Copy, Debug)]
#[must_use = "strategies do nothing unless used"]
pub struct Just<T: Clone + fmt::Debug>(
pub T,
);
impl<T: Clone + fmt::Debug> Strategy for Just<T> {
type Tree = Self;
type Value = T;
fn new_tree(&self, _: &mut TestRunner) -> NewTree<Self> {
Ok(self.clone())
}
}
impl<T: Clone + fmt::Debug> ValueTree for Just<T> {
type Value = T;
noshrink!();
fn current(&self) -> T {
self.0.clone()
}
}
#[must_use = "strategies do nothing unless used"]
pub struct LazyJust<T, F: Fn() -> T> {
function: F,
}
pub type LazyJustFn<V> = LazyJust<V, fn() -> V>;
impl<T, F: Fn() -> T> LazyJust<T, F> {
pub fn new(function: F) -> Self {
Self { function }
}
}
impl<T: fmt::Debug, F: Clone + Fn() -> T> Strategy for LazyJust<T, F> {
type Tree = Self;
type Value = T;
fn new_tree(&self, _: &mut TestRunner) -> NewTree<Self> {
Ok(self.clone())
}
}
impl<T: fmt::Debug, F: Fn() -> T> ValueTree for LazyJust<T, F> {
type Value = T;
noshrink!();
fn current(&self) -> Self::Value {
(self.function)()
}
}
impl<T, F: Copy + Fn() -> T> Copy for LazyJust<T, F> {}
impl<T, F: Clone + Fn() -> T> Clone for LazyJust<T, F> {
fn clone(&self) -> Self {
Self {
function: self.function.clone(),
}
}
}
impl<T, F: Fn() -> T> fmt::Debug for LazyJust<T, F> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("LazyJust")
.field("function", &"<function>")
.finish()
}
}
impl<T: fmt::Debug> Strategy for fn() -> T {
type Tree = Self;
type Value = T;
fn new_tree(&self, _: &mut TestRunner) -> NewTree<Self> {
Ok(*self)
}
}
impl<T: fmt::Debug> ValueTree for fn() -> T {
type Value = T;
noshrink!();
fn current(&self) -> Self::Value {
self()
}
}