asserting/option/
mod.rs

1//! Implementation of assertions for `Option` values.
2
3use crate::assertions::{AssertHasValue, AssertOption, AssertOptionValue};
4use crate::colored::{mark_missing, mark_unexpected};
5use crate::expectations::{HasValue, IsNone, IsSome};
6use crate::spec::{DiffFormat, Expectation, Expression, FailingStrategy, Spec, Unknown};
7use crate::std::fmt::Debug;
8use crate::std::{format, string::String};
9
10impl<S, R> AssertOption for Spec<'_, Option<S>, R>
11where
12    S: Debug,
13    R: FailingStrategy,
14{
15    fn is_some(self) -> Self {
16        self.expecting(IsSome)
17    }
18
19    fn is_none(self) -> Self {
20        self.expecting(IsNone)
21    }
22}
23
24impl<'a, T, R> AssertOptionValue<'a, T, R> for Spec<'a, Option<T>, R>
25where
26    R: FailingStrategy,
27{
28    fn some(self) -> Spec<'a, T, R> {
29        self.mapping(|subject| match subject {
30            None => {
31                panic!("assertion failed: expected the subject to be `Some(_)`, but was `None`")
32            },
33            Some(value) => value,
34        })
35    }
36}
37
38impl<S, E, R> AssertHasValue<E> for Spec<'_, Option<S>, R>
39where
40    S: PartialEq<E> + Debug,
41    E: Debug,
42    R: FailingStrategy,
43{
44    fn has_value(self, expected: E) -> Self {
45        self.expecting(HasValue { expected })
46    }
47}
48
49impl<T> Expectation<Option<T>> for IsSome
50where
51    T: Debug,
52{
53    fn test(&mut self, subject: &Option<T>) -> bool {
54        subject.is_some()
55    }
56
57    fn message(
58        &self,
59        expression: Expression<'_>,
60        actual: &Option<T>,
61        format: &DiffFormat,
62    ) -> String {
63        let expected = Some(Unknown);
64        let marked_actual = mark_unexpected(actual, format);
65        let marked_expected = mark_missing(&expected, format);
66        format!(
67            "expected {expression} is {expected:?}\n   but was: {marked_actual}\n  expected: {marked_expected}"
68        )
69    }
70}
71
72impl<T> Expectation<Option<T>> for IsNone
73where
74    T: Debug,
75{
76    fn test(&mut self, subject: &Option<T>) -> bool {
77        subject.is_none()
78    }
79
80    fn message(
81        &self,
82        expression: Expression<'_>,
83        actual: &Option<T>,
84        format: &DiffFormat,
85    ) -> String {
86        let expected = None::<Unknown>;
87        let marked_actual = mark_unexpected(actual, format);
88        let marked_expected = mark_missing(&expected, format);
89        format!(
90            "expected {expression} is {expected:?}\n   but was: {marked_actual}\n  expected: {marked_expected}"
91        )
92    }
93}
94
95impl<T, E> Expectation<Option<T>> for HasValue<E>
96where
97    T: PartialEq<E> + Debug,
98    E: Debug,
99{
100    fn test(&mut self, subject: &Option<T>) -> bool {
101        subject
102            .as_ref()
103            .is_some_and(|value| value == &self.expected)
104    }
105
106    fn message(
107        &self,
108        expression: Expression<'_>,
109        actual: &Option<T>,
110        format: &DiffFormat,
111    ) -> String {
112        let expected = &self.expected;
113        let marked_actual = mark_unexpected(actual, format);
114        let marked_expected = mark_missing(&Some(expected), format);
115        format!("expected {expression} is some containing {expected:?}\n   but was: {marked_actual}\n  expected: {marked_expected}")
116    }
117}
118
119#[cfg(test)]
120mod tests;