| 1 |
// Copyright 2007, Google Inc. |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// Redistribution and use in source and binary forms, with or without |
| 5 |
// modification, are permitted provided that the following conditions are |
| 6 |
// met: |
| 7 |
// |
| 8 |
// * Redistributions of source code must retain the above copyright |
| 9 |
// notice, this list of conditions and the following disclaimer. |
| 10 |
// * Redistributions in binary form must reproduce the above |
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
| 12 |
// in the documentation and/or other materials provided with the |
| 13 |
// distribution. |
| 14 |
// * Neither the name of Google Inc. nor the names of its |
| 15 |
// contributors may be used to endorse or promote products derived from |
| 16 |
// this software without specific prior written permission. |
| 17 |
// |
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
// Google Mock - a framework for writing C++ mock classes. |
| 31 |
// |
| 32 |
// This file tests some commonly used argument matchers. |
| 33 |
|
| 34 |
#ifndef GOOGLEMOCK_TEST_GMOCK_MATCHERS_TEST_H_ |
| 35 |
#define GOOGLEMOCK_TEST_GMOCK_MATCHERS_TEST_H_ |
| 36 |
|
| 37 |
#include <string.h> |
| 38 |
#include <time.h> |
| 39 |
|
| 40 |
#include <array> |
| 41 |
#include <cstdint> |
| 42 |
#include <deque> |
| 43 |
#include <forward_list> |
| 44 |
#include <functional> |
| 45 |
#include <iostream> |
| 46 |
#include <iterator> |
| 47 |
#include <limits> |
| 48 |
#include <list> |
| 49 |
#include <map> |
| 50 |
#include <memory> |
| 51 |
#include <set> |
| 52 |
#include <sstream> |
| 53 |
#include <string> |
| 54 |
#include <type_traits> |
| 55 |
#include <unordered_map> |
| 56 |
#include <unordered_set> |
| 57 |
#include <utility> |
| 58 |
#include <vector> |
| 59 |
|
| 60 |
#include "gmock/gmock-matchers.h" |
| 61 |
#include "gmock/gmock-more-matchers.h" |
| 62 |
#include "gmock/gmock.h" |
| 63 |
#include "gtest/gtest-spi.h" |
| 64 |
#include "gtest/gtest.h" |
| 65 |
|
| 66 |
namespace testing { |
| 67 |
namespace gmock_matchers_test { |
| 68 |
|
| 69 |
using std::greater; |
| 70 |
using std::less; |
| 71 |
using std::list; |
| 72 |
using std::make_pair; |
| 73 |
using std::map; |
| 74 |
using std::multimap; |
| 75 |
using std::multiset; |
| 76 |
using std::ostream; |
| 77 |
using std::pair; |
| 78 |
using std::set; |
| 79 |
using std::stringstream; |
| 80 |
using std::vector; |
| 81 |
using testing::internal::DummyMatchResultListener; |
| 82 |
using testing::internal::ElementMatcherPair; |
| 83 |
using testing::internal::ElementMatcherPairs; |
| 84 |
using testing::internal::ElementsAreArrayMatcher; |
| 85 |
using testing::internal::ExplainMatchFailureTupleTo; |
| 86 |
using testing::internal::FloatingEqMatcher; |
| 87 |
using testing::internal::FormatMatcherDescription; |
| 88 |
using testing::internal::IsReadableTypeName; |
| 89 |
using testing::internal::MatchMatrix; |
| 90 |
using testing::internal::PredicateFormatterFromMatcher; |
| 91 |
using testing::internal::RE; |
| 92 |
using testing::internal::StreamMatchResultListener; |
| 93 |
using testing::internal::Strings; |
| 94 |
|
| 95 |
// Helper for testing container-valued matchers in mock method context. It is |
| 96 |
// important to test matchers in this context, since it requires additional type |
| 97 |
// deduction beyond what EXPECT_THAT does, thus making it more restrictive. |
| 98 |
struct ContainerHelper { |
| 99 |
MOCK_METHOD1(Call, void(std::vector<std::unique_ptr<int>>)); |
| 100 |
}; |
| 101 |
|
| 102 |
// For testing ExplainMatchResultTo(). |
| 103 |
template <typename T> |
| 104 |
struct GtestGreaterThanMatcher { |
| 105 |
using is_gtest_matcher = void; |
| 106 |
|
| 107 |
void DescribeTo(ostream* os) const { *os << "is > " << rhs; } |
| 108 |
void DescribeNegationTo(ostream* os) const { *os << "is <= " << rhs; } |
| 109 |
|
| 110 |
bool MatchAndExplain(T lhs, MatchResultListener* listener) const { |
| 111 |
if (lhs > rhs) { |
| 112 |
*listener << "which is " << (lhs - rhs) << " more than " << rhs; |
| 113 |
} else if (lhs == rhs) { |
| 114 |
*listener << "which is the same as " << rhs; |
| 115 |
} else { |
| 116 |
*listener << "which is " << (rhs - lhs) << " less than " << rhs; |
| 117 |
} |
| 118 |
|
| 119 |
return lhs > rhs; |
| 120 |
} |
| 121 |
|
| 122 |
T rhs; |
| 123 |
}; |
| 124 |
|
| 125 |
template <typename T> |
| 126 |
GtestGreaterThanMatcher<typename std::decay<T>::type> GtestGreaterThan( |
| 127 |
T&& rhs) { |
| 128 |
return {rhs}; |
| 129 |
} |
| 130 |
|
| 131 |
// As the matcher above, but using the base class with virtual functions. |
| 132 |
template <typename T> |
| 133 |
class GreaterThanMatcher : public MatcherInterface<T> { |
| 134 |
public: |
| 135 |
explicit GreaterThanMatcher(T rhs) : impl_{rhs} {} |
| 136 |
|
| 137 |
void DescribeTo(ostream* os) const override { impl_.DescribeTo(os); } |
| 138 |
void DescribeNegationTo(ostream* os) const override { |
| 139 |
impl_.DescribeNegationTo(os); |
| 140 |
} |
| 141 |
|
| 142 |
bool MatchAndExplain(T lhs, MatchResultListener* listener) const override { |
| 143 |
return impl_.MatchAndExplain(lhs, listener); |
| 144 |
} |
| 145 |
|
| 146 |
private: |
| 147 |
const GtestGreaterThanMatcher<T> impl_; |
| 148 |
}; |
| 149 |
|
| 150 |
// Names and instantiates a new instance of GTestMatcherTestP. |
| 151 |
#define INSTANTIATE_GTEST_MATCHER_TEST_P(TestSuite) \ |
| 152 |
using TestSuite##P = GTestMatcherTestP; \ |
| 153 |
INSTANTIATE_TEST_SUITE_P(MatcherInterface, TestSuite##P, Values(false)); \ |
| 154 |
INSTANTIATE_TEST_SUITE_P(GtestMatcher, TestSuite##P, Values(true)) |
| 155 |
|
| 156 |
class GTestMatcherTestP : public testing::TestWithParam<bool> { |
| 157 |
public: |
| 158 |
template <typename T> |
| 159 |
Matcher<T> GreaterThan(T n) { |
| 160 |
if (use_gtest_matcher_) { |
| 161 |
return GtestGreaterThan(n); |
| 162 |
} else { |
| 163 |
return MakeMatcher(new GreaterThanMatcher<T>(n)); |
| 164 |
} |
| 165 |
} |
| 166 |
const bool use_gtest_matcher_ = GetParam(); |
| 167 |
}; |
| 168 |
|
| 169 |
// Returns the description of the given matcher. |
| 170 |
template <typename T> |
| 171 |
std::string Describe(const Matcher<T>& m) { |
| 172 |
return DescribeMatcher<T>(m); |
| 173 |
} |
| 174 |
|
| 175 |
// Returns the description of the negation of the given matcher. |
| 176 |
template <typename T> |
| 177 |
std::string DescribeNegation(const Matcher<T>& m) { |
| 178 |
return DescribeMatcher<T>(m, true); |
| 179 |
} |
| 180 |
|
| 181 |
// Returns the reason why x matches, or doesn't match, m. |
| 182 |
template <typename MatcherType, typename Value> |
| 183 |
std::string Explain(const MatcherType& m, const Value& x) { |
| 184 |
StringMatchResultListener listener; |
| 185 |
ExplainMatchResult(m, x, &listener); |
| 186 |
return listener.str(); |
| 187 |
} |
| 188 |
|
| 189 |
} // namespace gmock_matchers_test |
| 190 |
} // namespace testing |
| 191 |
|
| 192 |
#endif // GOOGLEMOCK_TEST_GMOCK_MATCHERS_TEST_H_ |