use googletest::description::Description;
use googletest::matcher::{Matcher, MatcherBase, MatcherResult};
use protobuf::__internal::MatcherEq;
pub fn proto_eq<T: MatcherEq>(expected: T) -> MessageMatcher<T> {
MessageMatcher { expected, partial: false }
}
pub fn proto_partially_eq<T: MatcherEq>(expected: T) -> MessageMatcher<T> {
MessageMatcher { expected, partial: true }
}
#[derive(MatcherBase)]
pub struct MessageMatcher<T: MatcherEq> {
expected: T,
partial: bool,
}
impl<T> Matcher<&T> for MessageMatcher<T>
where
T: MatcherEq,
{
fn matches(&self, actual: &T) -> MatcherResult {
if self.partial {
actual.matches_partially(&self.expected).into()
} else {
actual.matches(&self.expected).into()
}
}
fn describe(&self, matcher_result: MatcherResult) -> Description {
match (matcher_result, self.partial) {
(MatcherResult::Match, false) => format!("is equal to {:?}", self.expected).into(),
(MatcherResult::NoMatch, false) => {
format!("is not equal to {:?}", self.expected).into()
}
(MatcherResult::Match, true) => {
format!("is partially equal to {:?}", self.expected).into()
}
(MatcherResult::NoMatch, true) => {
format!("is not partially equal to {:?}", self.expected).into()
}
}
}
}
impl<T> Matcher<T> for MessageMatcher<T>
where
T: MatcherEq + Copy,
{
fn matches(&self, actual: T) -> MatcherResult {
if self.partial {
actual.matches_partially(&self.expected).into()
} else {
actual.matches(&self.expected).into()
}
}
fn describe(&self, matcher_result: MatcherResult) -> Description {
match (matcher_result, self.partial) {
(MatcherResult::Match, false) => format!("is equal to {:?}", self.expected).into(),
(MatcherResult::NoMatch, false) => {
format!("is not equal to {:?}", self.expected).into()
}
(MatcherResult::Match, true) => {
format!("is partially equal to {:?}", self.expected).into()
}
(MatcherResult::NoMatch, true) => {
format!("is not partially equal to {:?}", self.expected).into()
}
}
}
}