Generalize gmock-matchers_test to handle is_gtest_matcher-style matchers, too.

PiperOrigin-RevId: 444586594
Change-Id: I0de9b40b3773e3047a492f050266967ea935ae3e
This commit is contained in:
Abseil Team
2022-04-26 09:48:24 -07:00
committed by Copybara-Service
parent 0498660ea5
commit 238e4745c6
5 changed files with 144 additions and 50 deletions

View File

@@ -100,34 +100,72 @@ struct ContainerHelper {
};
// For testing ExplainMatchResultTo().
template <typename T = int>
class GreaterThanMatcher : public MatcherInterface<T> {
public:
explicit GreaterThanMatcher(T rhs) : rhs_(rhs) {}
template <typename T>
struct GtestGreaterThanMatcher {
using is_gtest_matcher = void;
void DescribeTo(ostream* os) const override { *os << "is > " << rhs_; }
void DescribeTo(ostream* os) const { *os << "is > " << rhs; }
void DescribeNegationTo(ostream* os) const { *os << "is <= " << rhs; }
bool MatchAndExplain(T lhs, MatchResultListener* listener) const override {
if (lhs > rhs_) {
*listener << "which is " << (lhs - rhs_) << " more than " << rhs_;
} else if (lhs == rhs_) {
*listener << "which is the same as " << rhs_;
bool MatchAndExplain(T lhs, MatchResultListener* listener) const {
if (lhs > rhs) {
*listener << "which is " << (lhs - rhs) << " more than " << rhs;
} else if (lhs == rhs) {
*listener << "which is the same as " << rhs;
} else {
*listener << "which is " << (rhs_ - lhs) << " less than " << rhs_;
*listener << "which is " << (rhs - lhs) << " less than " << rhs;
}
return lhs > rhs_;
return lhs > rhs;
}
private:
const T rhs_;
T rhs;
};
template <typename T>
Matcher<T> GreaterThan(T n) {
return MakeMatcher(new GreaterThanMatcher<T>(n));
GtestGreaterThanMatcher<typename std::decay<T>::type> GtestGreaterThan(
T&& rhs) {
return {rhs};
}
// As the matcher above, but using the base class with virtual functions.
template <typename T>
class GreaterThanMatcher : public MatcherInterface<T> {
public:
explicit GreaterThanMatcher(T rhs) : impl_{rhs} {}
void DescribeTo(ostream* os) const override { impl_.DescribeTo(os); }
void DescribeNegationTo(ostream* os) const override {
impl_.DescribeNegationTo(os);
}
bool MatchAndExplain(T lhs, MatchResultListener* listener) const override {
return impl_.MatchAndExplain(lhs, listener);
}
private:
const GtestGreaterThanMatcher<T> impl_;
};
// Names and instantiates a new instance of GTestMatcherTestP.
#define INSTANTIATE_GTEST_MATCHER_TEST_P(TestSuite) \
using TestSuite##P = GTestMatcherTestP; \
INSTANTIATE_TEST_SUITE_P(MatcherInterface, TestSuite##P, Values(false)); \
INSTANTIATE_TEST_SUITE_P(GtestMatcher, TestSuite##P, Values(true))
class GTestMatcherTestP : public testing::TestWithParam<bool> {
public:
template <typename T>
Matcher<T> GreaterThan(T n) {
if (use_gtest_matcher_) {
return GtestGreaterThan(n);
} else {
return MakeMatcher(new GreaterThanMatcher<T>(n));
}
}
const bool use_gtest_matcher_ = GetParam();
};
// Returns the description of the given matcher.
template <typename T>
std::string Describe(const Matcher<T>& m) {