gmock-matchers_test.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // Google Mock - a framework for writing C++ mock classes.
  30. //
  31. // This file tests some commonly used argument matchers.
  32. #ifndef GOOGLEMOCK_TEST_GMOCK_MATCHERS_TEST_H_
  33. #define GOOGLEMOCK_TEST_GMOCK_MATCHERS_TEST_H_
  34. #include <string.h>
  35. #include <time.h>
  36. #include <array>
  37. #include <cstdint>
  38. #include <deque>
  39. #include <forward_list>
  40. #include <functional>
  41. #include <iostream>
  42. #include <iterator>
  43. #include <limits>
  44. #include <list>
  45. #include <map>
  46. #include <memory>
  47. #include <set>
  48. #include <sstream>
  49. #include <string>
  50. #include <type_traits>
  51. #include <unordered_map>
  52. #include <unordered_set>
  53. #include <utility>
  54. #include <vector>
  55. #include "gmock/gmock-matchers.h"
  56. #include "gmock/gmock-more-matchers.h"
  57. #include "gmock/gmock.h"
  58. #include "gtest/gtest-spi.h"
  59. #include "gtest/gtest.h"
  60. namespace testing {
  61. namespace gmock_matchers_test {
  62. using std::greater;
  63. using std::less;
  64. using std::list;
  65. using std::make_pair;
  66. using std::map;
  67. using std::multimap;
  68. using std::multiset;
  69. using std::ostream;
  70. using std::pair;
  71. using std::set;
  72. using std::stringstream;
  73. using std::vector;
  74. using testing::internal::DummyMatchResultListener;
  75. using testing::internal::ElementMatcherPair;
  76. using testing::internal::ElementMatcherPairs;
  77. using testing::internal::ElementsAreArrayMatcher;
  78. using testing::internal::ExplainMatchFailureTupleTo;
  79. using testing::internal::FloatingEqMatcher;
  80. using testing::internal::FormatMatcherDescription;
  81. using testing::internal::IsReadableTypeName;
  82. using testing::internal::MatchMatrix;
  83. using testing::internal::PredicateFormatterFromMatcher;
  84. using testing::internal::RE;
  85. using testing::internal::StreamMatchResultListener;
  86. using testing::internal::Strings;
  87. // Helper for testing container-valued matchers in mock method context. It is
  88. // important to test matchers in this context, since it requires additional type
  89. // deduction beyond what EXPECT_THAT does, thus making it more restrictive.
  90. struct ContainerHelper {
  91. MOCK_METHOD1(Call, void(std::vector<std::unique_ptr<int>>));
  92. };
  93. // For testing ExplainMatchResultTo().
  94. template <typename T>
  95. struct GtestGreaterThanMatcher {
  96. using is_gtest_matcher = void;
  97. void DescribeTo(ostream* os) const { *os << "is > " << rhs; }
  98. void DescribeNegationTo(ostream* os) const { *os << "is <= " << rhs; }
  99. bool MatchAndExplain(T lhs, MatchResultListener* listener) const {
  100. if (lhs > rhs) {
  101. *listener << "which is " << (lhs - rhs) << " more than " << rhs;
  102. } else if (lhs == rhs) {
  103. *listener << "which is the same as " << rhs;
  104. } else {
  105. *listener << "which is " << (rhs - lhs) << " less than " << rhs;
  106. }
  107. return lhs > rhs;
  108. }
  109. T rhs;
  110. };
  111. template <typename T>
  112. GtestGreaterThanMatcher<typename std::decay<T>::type> GtestGreaterThan(
  113. T&& rhs) {
  114. return {rhs};
  115. }
  116. // As the matcher above, but using the base class with virtual functions.
  117. template <typename T>
  118. class GreaterThanMatcher : public MatcherInterface<T> {
  119. public:
  120. explicit GreaterThanMatcher(T rhs) : impl_{rhs} {}
  121. void DescribeTo(ostream* os) const override { impl_.DescribeTo(os); }
  122. void DescribeNegationTo(ostream* os) const override {
  123. impl_.DescribeNegationTo(os);
  124. }
  125. bool MatchAndExplain(T lhs, MatchResultListener* listener) const override {
  126. return impl_.MatchAndExplain(lhs, listener);
  127. }
  128. private:
  129. const GtestGreaterThanMatcher<T> impl_;
  130. };
  131. // Names and instantiates a new instance of GTestMatcherTestP.
  132. #define INSTANTIATE_GTEST_MATCHER_TEST_P(TestSuite) \
  133. using TestSuite##P = GTestMatcherTestP; \
  134. INSTANTIATE_TEST_SUITE_P(MatcherInterface, TestSuite##P, Values(false)); \
  135. INSTANTIATE_TEST_SUITE_P(GtestMatcher, TestSuite##P, Values(true))
  136. class GTestMatcherTestP : public testing::TestWithParam<bool> {
  137. public:
  138. template <typename T>
  139. Matcher<T> GreaterThan(T n) {
  140. if (use_gtest_matcher_) {
  141. return GtestGreaterThan(n);
  142. } else {
  143. return MakeMatcher(new GreaterThanMatcher<T>(n));
  144. }
  145. }
  146. const bool use_gtest_matcher_ = GetParam();
  147. };
  148. // Returns the description of the given matcher.
  149. template <typename T>
  150. std::string Describe(const Matcher<T>& m) {
  151. return DescribeMatcher<T>(m);
  152. }
  153. // Returns the description of the negation of the given matcher.
  154. template <typename T>
  155. std::string DescribeNegation(const Matcher<T>& m) {
  156. return DescribeMatcher<T>(m, true);
  157. }
  158. // Returns the reason why x matches, or doesn't match, m.
  159. template <typename MatcherType, typename Value>
  160. std::string Explain(const MatcherType& m, const Value& x) {
  161. StringMatchResultListener listener;
  162. ExplainMatchResult(m, x, &listener);
  163. return listener.str();
  164. }
  165. } // namespace gmock_matchers_test
  166. } // namespace testing
  167. #endif // GOOGLEMOCK_TEST_GMOCK_MATCHERS_TEST_H_