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
use self::fluid::core::prelude::*;
use crate as fluid;
use std::ops::Not;

/// Used to check if a result is an error.
#[derive(Debug, Drop)]
pub struct ContainIterator<I: Debug>
where
    I: Iterator,
    I::Item: PartialEq<I::Item> + Debug,
{
    should: ShouldImpl<I>,
    right: I::Item,
}

impl<I: Debug> AssertionImpl for ContainIterator<I>
where
    I: Iterator,
    I::Item: PartialEq<I::Item> + Debug,
{
    type Left = I;

    fn failure_message(&mut self) -> Option<String> {
        let mut left = self.should.left.take()?;
        let left_dbg = self.should.left_dbg();
        let truthness = self.should.truthness();

        if left.any(|item| item == self.right) != self.should.truthness {
            let message = if let Some(stringified) = self.should.stringified() {
                format!(
                    "\t{} does{} contain {}: {}\n\
                     \tbut it should{}.",
                    stringified,
                    truthness.not().str(),
                    self.right.dbg(),
                    left_dbg,
                    truthness.str()
                )
            } else {
                format!(
                    "\t{} does{} contain {}.",
                    left_dbg,
                    truthness.not().str(),
                    self.right.dbg()
                )
            };
            Some(message)
        } else {
            None
        }
    }

    fn consume_as_should(mut self) -> ShouldImpl<Self::Left> {
        self.should.take()
    }

    fn should_mut(&mut self) -> &mut ShouldImpl<Self::Left> {
        &mut self.should
    }
}

impl<I: Debug> Should<I>
where
    I: IntoIterator,
    I::Item: PartialEq<I::Item> + Debug,
    I::IntoIter: Debug,
{
    /// Checks if an iterator contains the right field.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use fluid::prelude::*;
    /// let it = vec![1, 2, 3, 4].into_iter();
    ///
    /// it.should().not().yield_the_item(0);
    /// ```
    pub fn yield_the_item(self, right: I::Item) -> ConsumingAssert<ContainIterator<I::IntoIter>> {
        let implem = ShouldImpl::from(self);
        let implem = ContainIterator {
            should: implem.map_left(|i| Some(i.into_iter())),
            right,
        };

        ConsumingAssert(implem)
    }
}

#[cfg(test)]
mod tests {
    use crate::prelude::*;

    #[fact]
    fn slice_yield_assertion() {
        let it = [1, 2, 3, 4].iter();

        it.should().yield_the_item(&1);
    }

    #[fact]
    fn option_yield_assertion() {
        let option = Some(0);

        option.should().yield_the_item(0);
    }

    #[fact]
    fn result_yield_assertion() {
        let result: Result<i32, _> = "42".parse();

        result.should().yield_the_item(42);
    }
}