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
#[cfg(not(google3))]
use crate as googletest;
use googletest::matcher::{Describe, Matcher, MatcherResult};
use std::fmt::Debug;
use std::marker::PhantomData;
pub fn none<T: Debug>() -> impl Matcher<Option<T>> {
NoneMatcher { phantom: Default::default() }
}
struct NoneMatcher<T> {
phantom: PhantomData<T>,
}
impl<T: Debug> Matcher<Option<T>> for NoneMatcher<T> {
fn matches(&self, actual: &Option<T>) -> MatcherResult {
if actual.is_none() { MatcherResult::Matches } else { MatcherResult::DoesNotMatch }
}
}
impl<T: Debug> Describe for NoneMatcher<T> {
fn describe(&self, matcher_result: MatcherResult) -> String {
match matcher_result {
MatcherResult::Matches => "is none".to_string(),
MatcherResult::DoesNotMatch => "is some(_)".to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(not(google3))]
use crate as googletest;
#[cfg(not(google3))]
use googletest::matchers;
use googletest::{google_test, verify_that, Result};
use matchers::eq;
#[google_test]
fn none_matches_option_with_none() -> Result<()> {
let matcher = none::<i32>();
let result = matcher.matches(&None);
verify_that!(result, eq(MatcherResult::Matches))
}
#[google_test]
fn none_does_not_match_option_with_value() -> Result<()> {
let matcher = none();
let result = matcher.matches(&Some(0));
verify_that!(result, eq(MatcherResult::DoesNotMatch))
}
}