fluent_assertions/assertions/
option_assertion.rs

1use super::Assertion;
2use std::fmt::Debug;
3
4impl<T: Debug + PartialEq> Assertion<Option<T>> {
5    /// Asserts that the Option is Some
6    pub fn be_some(&self) -> &Self {
7        assert!(self.value.is_some(), "Expected Some, but got None");
8        self
9    }
10
11    /// Asserts that the Option is Some and contains the expected value
12    pub fn contains(&self, expected: &T) -> &Self {
13        assert_eq!(
14            self.value.as_ref(),
15            Some(expected),
16            "Expected Some({:?}), but was {:?}",
17            expected,
18            self.value
19        );
20        self
21    }
22
23    /// Asserts that the Option is None
24    pub fn be_none(&self) -> &Self {
25        assert!(self.value.is_none(), "Expected None, but got Some");
26        self
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use crate::assertions::*;
33    use rstest::*;
34
35    #[rstest]
36    #[case(None)]
37    fn should_be_none(#[case] input: Option<String>) {
38        input.should().be_none();
39    }
40
41    #[rstest]
42    #[case(42f64)]
43    #[case(0.0)]
44    fn should_be_some(#[case] expected: f64) {
45        let input = Some(expected);
46        input.should().be_some().contains(&expected);
47    }
48
49    #[rstest]
50    #[case("hello")]
51    fn should_contain(#[case] expected: &str) {
52        let input = Some(expected);
53        input.should().be_some().contains(&expected);
54    }
55
56    #[rstest]
57    #[case(Some(String::from("hello")))]
58    fn should_contain_string(#[case] input: Option<String>) {
59        input.should().be_some().contains(&String::from("hello"));
60    }
61}