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 = int>
104 class GreaterThanMatcher : public MatcherInterface<T> {
105 public:
GreaterThanMatcher(T rhs)106 explicit GreaterThanMatcher(T rhs) : rhs_(rhs) {}
107
DescribeTo(ostream * os)108 void DescribeTo(ostream* os) const override { *os << "is > " << rhs_; }
109
MatchAndExplain(T lhs,MatchResultListener * listener)110 bool MatchAndExplain(T lhs, MatchResultListener* listener) const override {
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 private:
123 const T rhs_;
124 };
125
126 template <typename T>
GreaterThan(T n)127 Matcher<T> GreaterThan(T n) {
128 return MakeMatcher(new GreaterThanMatcher<T>(n));
129 }
130
131 // Returns the description of the given matcher.
132 template <typename T>
Describe(const Matcher<T> & m)133 std::string Describe(const Matcher<T>& m) {
134 return DescribeMatcher<T>(m);
135 }
136
137 // Returns the description of the negation of the given matcher.
138 template <typename T>
DescribeNegation(const Matcher<T> & m)139 std::string DescribeNegation(const Matcher<T>& m) {
140 return DescribeMatcher<T>(m, true);
141 }
142
143 // Returns the reason why x matches, or doesn't match, m.
144 template <typename MatcherType, typename Value>
Explain(const MatcherType & m,const Value & x)145 std::string Explain(const MatcherType& m, const Value& x) {
146 StringMatchResultListener listener;
147 ExplainMatchResult(m, x, &listener);
148 return listener.str();
149 }
150
151 } // namespace gmock_matchers_test
152 } // namespace testing
153
154 #endif // GOOGLEMOCK_TEST_GMOCK_MATCHERS_TEST_H_
155