1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Google Mock - a framework for writing C++ mock classes.
32 // This file tests some commonly used argument matchers.
34 #ifndef GOOGLEMOCK_TEST_GMOCK_MATCHERS_TEST_H_
35 #define GOOGLEMOCK_TEST_GMOCK_MATCHERS_TEST_H_
43 #include <forward_list>
54 #include <type_traits>
55 #include <unordered_map>
56 #include <unordered_set>
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"
67 namespace gmock_matchers_test {
79 using std::stringstream;
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;
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>>));
102 // For testing ExplainMatchResultTo().
103 template <typename T>
104 struct GtestGreaterThanMatcher {
105 using is_gtest_matcher = void;
107 void DescribeTo(ostream* os) const { *os << "is > " << rhs; }
108 void DescribeNegationTo(ostream* os) const { *os << "is <= " << rhs; }
110 bool MatchAndExplain(T lhs, MatchResultListener* listener) const {
112 *listener << "which is " << (lhs - rhs) << " more than " << rhs;
113 } else if (lhs == rhs) {
114 *listener << "which is the same as " << rhs;
116 *listener << "which is " << (rhs - lhs) << " less than " << rhs;
125 template <typename T>
126 GtestGreaterThanMatcher<typename std::decay<T>::type> GtestGreaterThan(
131 // As the matcher above, but using the base class with virtual functions.
132 template <typename T>
133 class GreaterThanMatcher : public MatcherInterface<T> {
135 explicit GreaterThanMatcher(T rhs) : impl_{rhs} {}
137 void DescribeTo(ostream* os) const override { impl_.DescribeTo(os); }
138 void DescribeNegationTo(ostream* os) const override {
139 impl_.DescribeNegationTo(os);
142 bool MatchAndExplain(T lhs, MatchResultListener* listener) const override {
143 return impl_.MatchAndExplain(lhs, listener);
147 const GtestGreaterThanMatcher<T> impl_;
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))
156 class GTestMatcherTestP : public testing::TestWithParam<bool> {
158 template <typename T>
159 Matcher<T> GreaterThan(T n) {
160 if (use_gtest_matcher_) {
161 return GtestGreaterThan(n);
163 return MakeMatcher(new GreaterThanMatcher<T>(n));
166 const bool use_gtest_matcher_ = GetParam();
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);
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);
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();
189 } // namespace gmock_matchers_test
190 } // namespace testing
192 #endif // GOOGLEMOCK_TEST_GMOCK_MATCHERS_TEST_H_