1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
pub mod array_like;
pub mod assertion_hook;
pub mod empty_types;
pub mod float_diff_provider;
pub mod should_result;
pub mod specifics;
#[cfg(test)]
mod tests;
pub mod wrapper_types;

use crate::assertion_hook::{AssertionHook, NoOpAssertionHook, NotAssertionHook, OrAssertionHook};
use crate::float_diff_provider::{EnvFloatDiffProvider, FloatDiffProvider};
use crate::should_result::ResultsContainer;
use std::borrow::Borrow;
use std::fmt::Debug;
use std::marker::PhantomData;
use std::ops::DerefMut;

pub struct Should<
    'a,
    Inner,
    Hook: AssertionHook = NoOpAssertionHook,
    FloatDiff: FloatDiffProvider = EnvFloatDiffProvider,
> {
    results: ResultsContainer,
    inner: &'a Inner,
    hook: PhantomData<Hook>,
    float_diff: PhantomData<FloatDiff>,
}

impl<'a, Inner, Hook, FloatDiff> Should<'a, Inner, Hook, FloatDiff>
where
    Hook: AssertionHook,
    FloatDiff: FloatDiffProvider,
{
    pub(crate) fn internal_assert(&mut self, initial: bool, message: String) {
        Hook::create_result(initial, message, self.results.deref_mut())
    }
    pub(crate) fn change_optional_generics<T: AssertionHook, L: FloatDiffProvider>(
        self,
    ) -> Should<'a, Inner, T, L> {
        Should::new(self.inner, self.results)
    }
    pub(crate) fn normalize(self) -> Should<'a, Inner, NoOpAssertionHook, FloatDiff> {
        self.change_optional_generics()
    }
    pub(crate) fn new(inner: &'a Inner, results: ResultsContainer) -> Self {
        Self {
            results,
            inner,
            hook: Default::default(),
            float_diff: Default::default(),
        }
    }
}

impl<'a, Inner, Hook, FloatDiff> Should<'a, Inner, Hook, FloatDiff>
where
    Inner: Shoulda,
    Hook: AssertionHook,
    FloatDiff: FloatDiffProvider,
{
    pub fn eq<K: Borrow<Inner>>(mut self, other: K) -> Self {
        let other = other.borrow();
        self.internal_assert(
            self.inner.test_eq::<FloatDiff>(other),
            format!("expected = {:?}, actual = {:?}", &self.inner, other),
        );
        self
    }
    pub fn equal<K: Borrow<Inner>>(self, other: K) -> Self {
        self.eq(other)
    }
    pub fn be(self) -> Self {
        self
    }
    pub fn and(self) -> Self {
        self
    }
    pub fn float_diff<T: FloatDiffProvider>(self) -> Should<'a, Inner, Hook, T> {
        self.change_optional_generics()
    }
}

impl<'a, Inner, FloatDiff> Should<'a, Inner, NoOpAssertionHook, FloatDiff>
where
    Inner: Shoulda,
    FloatDiff: FloatDiffProvider,
{
    pub fn not(self) -> Should<'a, Inner, NotAssertionHook, FloatDiff> {
        self.change_optional_generics()
    }
}

impl<'a, Inner, FloatDiff> Should<'a, Inner, NoOpAssertionHook, FloatDiff>
where
    Inner: Shoulda,
    FloatDiff: FloatDiffProvider,
{
    pub fn or(self) -> Should<'a, Inner, OrAssertionHook, FloatDiff> {
        self.change_optional_generics()
    }
}

impl<'a, Inner, FloatDiff> Should<'a, Inner, NotAssertionHook, FloatDiff>
where
    Inner: Shoulda,
    FloatDiff: FloatDiffProvider,
{
    pub fn not(self) -> Should<'a, Inner, NoOpAssertionHook, FloatDiff> {
        self.change_optional_generics()
    }
}

pub trait Shoulda: Debug {
    fn test_eq<FloatDiff: FloatDiffProvider>(&self, other: &Self) -> bool;
    fn should(&self) -> Should<Self>
    where
        Self: Sized,
    {
        Should::new(self, ResultsContainer::default())
    }
}

pub fn expect<T: Shoulda>(t: &T) -> Should<T> {
    t.should()
}

macro_rules! eq_assertable_impl {
    ($x:ty) => {
        impl Shoulda for $x {
            fn test_eq<FloatDiff: FloatDiffProvider>(&self, other: &Self) -> bool {
                self.eq(other)
            }
        }
    };
}

macro_rules! float_assertable_impl {
    ($x:ty) => {
        impl Shoulda for $x {
            fn test_eq<FloatDiff: FloatDiffProvider>(&self, other: &Self) -> bool {
                (self - other).abs() < (FloatDiff::diff() as $x)
            }
        }
    };
}

eq_assertable_impl!(String);
eq_assertable_impl!(str);
eq_assertable_impl!(std::ffi::CString);
eq_assertable_impl!(std::ffi::CStr);
eq_assertable_impl!(std::ffi::OsString);
eq_assertable_impl!(std::ffi::OsStr);
eq_assertable_impl!(std::fs::FileType);
eq_assertable_impl!(std::fs::Permissions);
eq_assertable_impl!(std::net::Ipv4Addr);
eq_assertable_impl!(std::net::Ipv6Addr);
eq_assertable_impl!(std::net::SocketAddrV4);
eq_assertable_impl!(std::net::SocketAddrV6);
eq_assertable_impl!(std::path::Path);
eq_assertable_impl!(std::path::PathBuf);
eq_assertable_impl!(std::thread::ThreadId);
eq_assertable_impl!(std::time::Duration);
eq_assertable_impl!(std::time::Instant);
eq_assertable_impl!(std::time::SystemTime);


eq_assertable_impl!(bool);
eq_assertable_impl!(u8);
eq_assertable_impl!(i8);
eq_assertable_impl!(u16);
eq_assertable_impl!(i16);
eq_assertable_impl!(u32);
eq_assertable_impl!(i32);
eq_assertable_impl!(u64);
eq_assertable_impl!(i64);
eq_assertable_impl!(u128);
eq_assertable_impl!(i128);
eq_assertable_impl!(usize);
eq_assertable_impl!(isize);

eq_assertable_impl!(std::num::NonZeroU8);
eq_assertable_impl!(std::num::NonZeroI8);
eq_assertable_impl!(std::num::NonZeroU16);
eq_assertable_impl!(std::num::NonZeroI16);
eq_assertable_impl!(std::num::NonZeroU32);
eq_assertable_impl!(std::num::NonZeroI32);
eq_assertable_impl!(std::num::NonZeroU64);
eq_assertable_impl!(std::num::NonZeroI64);
eq_assertable_impl!(std::num::NonZeroU128);
eq_assertable_impl!(std::num::NonZeroI128);
eq_assertable_impl!(std::num::NonZeroUsize);
eq_assertable_impl!(std::num::NonZeroIsize);

float_assertable_impl!(f32);
float_assertable_impl!(f64);

impl<T> Shoulda for &T
where
    T: Shoulda,
{
    fn test_eq<FloatDiff: FloatDiffProvider>(&self, other: &Self) -> bool {
        T::test_eq::<FloatDiff>(self, other)
    }
}