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
//! ## Usage
//!
//! ``` rust
//! use easy_assert::{actual, Actual, expected, Expected};
//! use easy_assert::num_assertions::NumericAssert;
//!
//!  NumericAssert::<i32>::assert_that(actual(1))
//!  .is_equal()
//!  .to(expected(1));
//!
//!  NumericAssert::<i32>::assert_that(actual(-1))
//!  .is_not_equal()
//!  .to(expected(1));
//!
//!  NumericAssert::<i32>::assert_that(actual(2))
//!  .is_greater_or_equal()
//!  .to(expected(1));
//!
//!  NumericAssert::<i32>::assert_that(actual(2))
//!  .is_greater()
//!  .than(expected(1));
//!
//!  NumericAssert::<i32>::assert_that(actual(1))
//!  .is_less_or_equal()
//!  .to(expected(1));
//!
//!  NumericAssert::<i32>::assert_that(actual(1))
//!  .is_less()
//!  .than(expected(3));
//! ```

use crate::{Actual, Expected};
use num_traits::Num;

use crate::assertions::{Equals, Greater, GreaterOrEqual, Less, LessOrEqual, NotEquals};

pub struct NumericAssert<T>
where
    T: Num + PartialOrd,
{
    actual: Actual<T>,
}

impl<T> NumericAssert<T>
where
    T: Num + PartialOrd,
    T: 'static,
{
    pub fn assert_that(actual: Actual<T>) -> NumericAssert<T> {
        NumericAssert { actual }
    }

    pub fn is_equal(self) -> Box<dyn Equals<T>> {
        Box::new(self)
    }

    pub fn is_not_equal(self) -> Box<dyn NotEquals<T>> {
        Box::new(self)
    }

    pub fn is_less(self) -> Box<dyn Less<T>> {
        Box::new(self)
    }

    pub fn is_less_or_equal(self) -> Box<dyn LessOrEqual<T>> {
        Box::new(self)
    }

    pub fn is_greater(self) -> Box<dyn Greater<T>> {
        Box::new(self)
    }

    pub fn is_greater_or_equal(self) -> Box<dyn GreaterOrEqual<T>> {
        Box::new(self)
    }
}

impl<T> Equals<T> for NumericAssert<T>
where
    T: Num + PartialOrd,
{
    fn to(&self, expected: Expected<T>) {
        if self.actual.ne(&expected) {
            panic!(
                "\n Actual: {} \n not equal to expected \n {} \n",
                self.actual, expected
            );
        }
    }
}

impl<T> NotEquals<T> for NumericAssert<T>
where
    T: Num + PartialOrd,
{
    fn to(&self, expected: Expected<T>) {
        if self.actual.eq(&expected) {
            panic!(
                "\n Actual: {} \n is equal to expected \n {} \n",
                self.actual, expected
            );
        }
    }
}

impl<T> Less<T> for NumericAssert<T>
where
    T: Num + PartialOrd,
{
    fn than(&self, expected: Expected<T>) {
        if self.actual.value >= expected.value {
            panic!(
                "\n Actual: {} \n is bigger or equal to expected \n {} \n",
                self.actual, expected
            );
        }
    }
}

impl<T> LessOrEqual<T> for NumericAssert<T>
where
    T: Num + PartialOrd,
{
    fn to(&self, expected: Expected<T>) {
        if self.actual.value > expected.value {
            panic!(
                "\n Actual: {} \n is bigger to expected \n {} \n",
                self.actual, expected
            );
        }
    }
}

impl<T> Greater<T> for NumericAssert<T>
where
    T: Num + PartialOrd,
{
    fn than(&self, expected: Expected<T>) {
        if self.actual.value <= expected.value {
            panic!(
                "\n Actual: {} \n is smaller or equal to expected \n {} \n",
                self.actual, expected
            );
        }
    }
}

impl<T> GreaterOrEqual<T> for NumericAssert<T>
where
    T: Num + PartialOrd,
{
    fn to(&self, expected: Expected<T>) {
        if self.actual.value < expected.value {
            panic!(
                "\n Actual: {} \n is smaller to expected \n {} \n",
                self.actual, expected
            );
        }
    }
}