gmock-more-matchers.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2013, 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 implements some matchers that depend on gmock-matchers.h.
  32. //
  33. // Note that tests are implemented in gmock-matchers_test.cc rather than
  34. // gmock-more-matchers-test.cc.
  35. // IWYU pragma: private, include "gmock/gmock.h"
  36. // IWYU pragma: friend gmock/.*
  37. #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_MATCHERS_H_
  38. #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_MATCHERS_H_
  39. #include <ostream>
  40. #include <string>
  41. #include "gmock/gmock-matchers.h"
  42. namespace testing {
  43. // Silence C4100 (unreferenced formal
  44. // parameter) for MSVC
  45. #ifdef _MSC_VER
  46. #pragma warning(push)
  47. #pragma warning(disable : 4100)
  48. #if (_MSC_VER == 1900)
  49. // and silence C4800 (C4800: 'int *const ': forcing value
  50. // to bool 'true' or 'false') for MSVC 14
  51. #pragma warning(disable : 4800)
  52. #endif
  53. #endif
  54. namespace internal {
  55. // Implements the polymorphic IsEmpty matcher, which
  56. // can be used as a Matcher<T> as long as T is either a container that defines
  57. // empty() and size() (e.g. std::vector or std::string), or a C-style string.
  58. class IsEmptyMatcher {
  59. public:
  60. // Matches anything that defines empty() and size().
  61. template <typename MatcheeContainerType>
  62. bool MatchAndExplain(const MatcheeContainerType& c,
  63. MatchResultListener* listener) const {
  64. if (c.empty()) {
  65. return true;
  66. }
  67. *listener << "whose size is " << c.size();
  68. return false;
  69. }
  70. // Matches C-style strings.
  71. bool MatchAndExplain(const char* s, MatchResultListener* listener) const {
  72. return MatchAndExplain(std::string(s), listener);
  73. }
  74. // Describes what this matcher matches.
  75. void DescribeTo(std::ostream* os) const { *os << "is empty"; }
  76. void DescribeNegationTo(std::ostream* os) const { *os << "isn't empty"; }
  77. };
  78. } // namespace internal
  79. // Creates a polymorphic matcher that matches an empty container or C-style
  80. // string. The container must support both size() and empty(), which all
  81. // STL-like containers provide.
  82. inline PolymorphicMatcher<internal::IsEmptyMatcher> IsEmpty() {
  83. return MakePolymorphicMatcher(internal::IsEmptyMatcher());
  84. }
  85. // Define a matcher that matches a value that evaluates in boolean
  86. // context to true. Useful for types that define "explicit operator
  87. // bool" operators and so can't be compared for equality with true
  88. // and false.
  89. MATCHER(IsTrue, negation ? "is false" : "is true") {
  90. return static_cast<bool>(arg);
  91. }
  92. // Define a matcher that matches a value that evaluates in boolean
  93. // context to false. Useful for types that define "explicit operator
  94. // bool" operators and so can't be compared for equality with true
  95. // and false.
  96. MATCHER(IsFalse, negation ? "is true" : "is false") {
  97. return !static_cast<bool>(arg);
  98. }
  99. #ifdef _MSC_VER
  100. #pragma warning(pop)
  101. #endif
  102. } // namespace testing
  103. #endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_MATCHERS_H_