clearcheck/assertions/option/
predicate.rs

1use crate::matchers::{Should, ShouldNot};
2use crate::matchers::option::be_some;
3use crate::matchers::option::predicate::satisfy;
4
5/// SomePredicateAssertion enables assertions about whether the Option value is both Some and that the contained value meets or doesn't meet certain conditions defined by a predicate.
6pub trait SomePredicateAssertion<T> {
7    /// - Asserts that the Option value is Some and satisfies the given predicate.
8    /// - Returns a reference to self for fluent chaining.
9    /// - Panics if the assertion fails.
10    /// # Example
11    /// ```
12    /// use clearcheck::assertions::option::predicate::SomePredicateAssertion;
13    ///
14    /// let option = Some(100);
15    /// option.should_be_some_and_satisfy(|value| value > &&50);
16    /// ```
17    fn should_be_some_and_satisfy<F: Fn(&&T) -> bool>(&self, predicate: F) -> &Self;
18
19    /// - Asserts that the Option value is Some and does not satisfy the given predicate.
20    /// - Returns a reference to self for fluent chaining.
21    /// - Panics if the assertion fails.
22    /// # Example
23    /// ```
24    /// use clearcheck::assertions::option::predicate::SomePredicateAssertion;
25    ///
26    /// let option = Some(100);
27    /// option.should_be_some_and_not_satisfy(|value| value > &&500);
28    /// ```
29    fn should_be_some_and_not_satisfy<F: Fn(&&T) -> bool>(&self, predicate: F) -> &Self;
30}
31
32impl<T> SomePredicateAssertion<T> for Option<T> {
33    fn should_be_some_and_satisfy<F: Fn(&&T) -> bool>(&self, predicate: F) -> &Self {
34        self.should(&be_some());
35        self.should(&satisfy(predicate));
36        self
37    }
38
39    fn should_be_some_and_not_satisfy<F: Fn(&&T) -> bool>(&self, predicate: F) -> &Self {
40        self.should(&be_some());
41        self.should_not(&satisfy(predicate));
42        self
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use crate::assertions::option::predicate::SomePredicateAssertion;
49
50    #[test]
51    fn should_be_some_and_satisfy_predicate() {
52        let option = Some(100);
53        option.should_be_some_and_satisfy(|value| value > &&50);
54    }
55
56    #[test]
57    #[should_panic]
58    fn should_be_some_and_satisfy_predicate_but_it_did_not() {
59        let option = Some(100);
60        option.should_be_some_and_satisfy(|value| value > &&100);
61    }
62
63    #[test]
64    #[should_panic]
65    fn should_be_some_and_satisfy_predicate_but_it_was_none() {
66        let option: Option<i32> = None;
67        option.should_be_some_and_satisfy(|value| value > &&100);
68    }
69
70    #[test]
71    fn should_be_some_and_not_satisfy_predicate() {
72        let option = Some(100);
73        option.should_be_some_and_not_satisfy(|value| value > &&500);
74    }
75
76    #[test]
77    #[should_panic]
78    fn should_be_some_and_not_satisfy_predicate_but_it_did() {
79        let option = Some(100);
80        option.should_be_some_and_not_satisfy(|value| value > &&50);
81    }
82
83    #[test]
84    #[should_panic]
85    fn should_be_some_and_not_satisfy_predicate_but_it_was_none() {
86        let option: Option<i32> = None;
87        option.should_be_some_and_not_satisfy(|value| value > &&100);
88    }
89}