Path: blob/main/contrib/googletest/googlemock/test/gmock-matchers_test.h
48255 views
// Copyright 2007, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// Google Mock - a framework for writing C++ mock classes.30//31// This file tests some commonly used argument matchers.3233#ifndef GOOGLEMOCK_TEST_GMOCK_MATCHERS_TEST_H_34#define GOOGLEMOCK_TEST_GMOCK_MATCHERS_TEST_H_3536#include <string.h>37#include <time.h>3839#include <array>40#include <cstdint>41#include <deque>42#include <forward_list>43#include <functional>44#include <iostream>45#include <iterator>46#include <limits>47#include <list>48#include <map>49#include <memory>50#include <set>51#include <sstream>52#include <string>53#include <type_traits>54#include <unordered_map>55#include <unordered_set>56#include <utility>57#include <vector>5859#include "gmock/gmock-matchers.h"60#include "gmock/gmock-more-matchers.h"61#include "gmock/gmock.h"62#include "gtest/gtest-spi.h"63#include "gtest/gtest.h"6465namespace testing {66namespace gmock_matchers_test {6768using std::greater;69using std::less;70using std::list;71using std::make_pair;72using std::map;73using std::multimap;74using std::multiset;75using std::ostream;76using std::pair;77using std::set;78using std::stringstream;79using std::vector;80using testing::internal::DummyMatchResultListener;81using testing::internal::ElementMatcherPair;82using testing::internal::ElementMatcherPairs;83using testing::internal::ElementsAreArrayMatcher;84using testing::internal::ExplainMatchFailureTupleTo;85using testing::internal::FloatingEqMatcher;86using testing::internal::FormatMatcherDescription;87using testing::internal::IsReadableTypeName;88using testing::internal::MatchMatrix;89using testing::internal::PredicateFormatterFromMatcher;90using testing::internal::RE;91using testing::internal::StreamMatchResultListener;92using testing::internal::Strings;9394// Helper for testing container-valued matchers in mock method context. It is95// important to test matchers in this context, since it requires additional type96// deduction beyond what EXPECT_THAT does, thus making it more restrictive.97struct ContainerHelper {98MOCK_METHOD1(Call, void(std::vector<std::unique_ptr<int>>));99};100101// For testing ExplainMatchResultTo().102template <typename T>103struct GtestGreaterThanMatcher {104using is_gtest_matcher = void;105106void DescribeTo(ostream* os) const { *os << "is > " << rhs; }107void DescribeNegationTo(ostream* os) const { *os << "is <= " << rhs; }108109bool MatchAndExplain(T lhs, MatchResultListener* listener) const {110if (lhs > rhs) {111*listener << "which is " << (lhs - rhs) << " more than " << rhs;112} else if (lhs == rhs) {113*listener << "which is the same as " << rhs;114} else {115*listener << "which is " << (rhs - lhs) << " less than " << rhs;116}117118return lhs > rhs;119}120121T rhs;122};123124template <typename T>125GtestGreaterThanMatcher<typename std::decay<T>::type> GtestGreaterThan(126T&& rhs) {127return {rhs};128}129130// As the matcher above, but using the base class with virtual functions.131template <typename T>132class GreaterThanMatcher : public MatcherInterface<T> {133public:134explicit GreaterThanMatcher(T rhs) : impl_{rhs} {}135136void DescribeTo(ostream* os) const override { impl_.DescribeTo(os); }137void DescribeNegationTo(ostream* os) const override {138impl_.DescribeNegationTo(os);139}140141bool MatchAndExplain(T lhs, MatchResultListener* listener) const override {142return impl_.MatchAndExplain(lhs, listener);143}144145private:146const GtestGreaterThanMatcher<T> impl_;147};148149// Names and instantiates a new instance of GTestMatcherTestP.150#define INSTANTIATE_GTEST_MATCHER_TEST_P(TestSuite) \151using TestSuite##P = GTestMatcherTestP; \152INSTANTIATE_TEST_SUITE_P(MatcherInterface, TestSuite##P, Values(false)); \153INSTANTIATE_TEST_SUITE_P(GtestMatcher, TestSuite##P, Values(true))154155class GTestMatcherTestP : public testing::TestWithParam<bool> {156public:157template <typename T>158Matcher<T> GreaterThan(T n) {159if (use_gtest_matcher_) {160return GtestGreaterThan(n);161} else {162return MakeMatcher(new GreaterThanMatcher<T>(n));163}164}165const bool use_gtest_matcher_ = GetParam();166};167168// Returns the description of the given matcher.169template <typename T>170std::string Describe(const Matcher<T>& m) {171return DescribeMatcher<T>(m);172}173174// Returns the description of the negation of the given matcher.175template <typename T>176std::string DescribeNegation(const Matcher<T>& m) {177return DescribeMatcher<T>(m, true);178}179180// Returns the reason why x matches, or doesn't match, m.181template <typename MatcherType, typename Value>182std::string Explain(const MatcherType& m, const Value& x) {183StringMatchResultListener listener;184ExplainMatchResult(m, x, &listener);185return listener.str();186}187188} // namespace gmock_matchers_test189} // namespace testing190191#endif // GOOGLEMOCK_TEST_GMOCK_MATCHERS_TEST_H_192193194