clearcheck/assertions/option/
predicate.rs1use crate::matchers::{Should, ShouldNot};
2use crate::matchers::option::be_some;
3use crate::matchers::option::predicate::satisfy;
4
5pub trait SomePredicateAssertion<T> {
7 fn should_be_some_and_satisfy<F: Fn(&&T) -> bool>(&self, predicate: F) -> &Self;
18
19 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}