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