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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

use std::fmt;
use core::{Matcher, Join};

/// A matcher for `be_ok` assertions for `Result<T, E>` type.
pub struct BeOk<E> {
    expected: Option<E>,
}

/// Returns new `BeOk` matcher.
pub fn be_ok<E>() -> BeOk<E> {
    BeOk { expected: None }
}

impl<E> BeOk<E> {
    /// Sets new `ok` value.
    pub fn value(mut self, v: E) -> BeOk<E> {
        self.expected = Some(v);
        self
    }
}

impl<A, E, T> Matcher<Result<A, T>, E> for BeOk<E>
    where
        A: PartialEq<E> + fmt::Debug,
        E: fmt::Debug,
        T: fmt::Debug {

    fn failure_message(&self, join: Join, actual: &Result<A, T>) -> String {
        format!("expected {} be Ok, got <{:?}>", join, actual)
    }

    fn matches(&self, actual: &Result<A, T>) -> bool {
        ::utils::is_some_value(actual.as_ref().ok(), self.expected.as_ref())
    }
}

/// A matcher for `be_err` assertions for `Result<T, E>` type.
pub struct BeErr<E> {
    expected: Option<E>,
}

/// Returns new `BeErr` matcher.
pub fn be_err<E>() -> BeErr<E> {
    BeErr { expected: None }
}

impl<E> BeErr<E> {
    /// Sets new `err` value.
    pub fn value(mut self, v: E) -> BeErr<E> {
        self.expected = Some(v);
        self
    }
}

impl<A, E, T> Matcher<Result<T, A>, E> for BeErr<E>
    where
        A: PartialEq<E> + fmt::Debug,
        E: fmt::Debug,
        T: fmt::Debug {

    fn failure_message(&self, join: Join, actual: &Result<T, A>) -> String {
        format!("expected {} be Err, got <{:?}>", join, actual)
    }

    fn matches(&self, actual: &Result<T, A>) -> bool {
        ::utils::is_some_value(actual.as_ref().err(), self.expected.as_ref())
    }
}

#[cfg(test)]
mod tests {
    use super::{be_ok, be_err};
    use core::Matcher;

    fn ok_result(value: u32) -> Result<u32, &'static str> {
        Ok(value)
    }

    fn err_result(value: &'static str) -> Result<u32, &'static str> {
        Err(value)
    }

    #[test]
    fn be_ok_matches_ok() {
        assert!(be_ok().matches(&ok_result(5)));
    }

    #[test]
    fn be_ok_value_matches_ok() {
        assert!(be_ok().value(1).matches(&ok_result(1)));
    }

    #[test]
    #[should_panic]
    fn be_ok_matches_err_should_panic() {
        assert!(be_ok().matches(&err_result("error")));
    }

    #[test]
    #[should_panic]
    fn be_ok_value_matches_ok_should_panic() {
        assert!(be_ok().value(1).matches(&ok_result(2)));
    }

    #[test]
    #[should_panic]
    fn be_ok_value_matches_err_should_panic() {
        assert!(be_ok().value(1).matches(&err_result("error")));
    }

    #[test]
    fn be_err_matches_err() {
        assert!(be_err::<&'static str>().matches(&err_result("error")));
    }

    #[test]
    fn be_err_value_matches_err() {
        assert!(be_err().value("error").matches(&err_result("error")));
    }

    #[test]
    #[should_panic]
    fn be_err_matches_ok_should_panic() {
        assert!(be_err::<&'static str>().matches(&ok_result(1)));
    }

    #[test]
    #[should_panic]
    fn be_err_value_matches_err_should_panic() {
        assert!(be_err().value("error").matches(&err_result("error!")));
    }

    #[test]
    #[should_panic]
    fn be_err_value_matches_ok_should_panic() {
        assert!(be_err().value("error").matches(&ok_result(1)));
    }
}