1b89a7cc2SEnji Cooper // Copyright 2013, Google Inc.
2b89a7cc2SEnji Cooper // All rights reserved.
3b89a7cc2SEnji Cooper //
4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6b89a7cc2SEnji Cooper // met:
7b89a7cc2SEnji Cooper //
8b89a7cc2SEnji Cooper // * Redistributions of source code must retain the above copyright
9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10b89a7cc2SEnji Cooper // * Redistributions in binary form must reproduce the above
11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13b89a7cc2SEnji Cooper // distribution.
14b89a7cc2SEnji Cooper // * Neither the name of Google Inc. nor the names of its
15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16b89a7cc2SEnji Cooper // this software without specific prior written permission.
17b89a7cc2SEnji Cooper //
18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b89a7cc2SEnji Cooper
30b89a7cc2SEnji Cooper // Google Mock - a framework for writing C++ mock classes.
31b89a7cc2SEnji Cooper //
32*28f6c2f2SEnji Cooper // This file implements some matchers that depend on gmock-matchers.h.
33b89a7cc2SEnji Cooper //
34b89a7cc2SEnji Cooper // Note that tests are implemented in gmock-matchers_test.cc rather than
35b89a7cc2SEnji Cooper // gmock-more-matchers-test.cc.
36b89a7cc2SEnji Cooper
37*28f6c2f2SEnji Cooper // IWYU pragma: private, include "gmock/gmock.h"
38*28f6c2f2SEnji Cooper // IWYU pragma: friend gmock/.*
39b89a7cc2SEnji Cooper
40*28f6c2f2SEnji Cooper #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_MATCHERS_H_
41*28f6c2f2SEnji Cooper #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_MATCHERS_H_
42b89a7cc2SEnji Cooper
43*28f6c2f2SEnji Cooper #include <ostream>
44*28f6c2f2SEnji Cooper #include <string>
45*28f6c2f2SEnji Cooper
46*28f6c2f2SEnji Cooper #include "gmock/gmock-matchers.h"
47b89a7cc2SEnji Cooper
48b89a7cc2SEnji Cooper namespace testing {
49b89a7cc2SEnji Cooper
50b89a7cc2SEnji Cooper // Silence C4100 (unreferenced formal
51b89a7cc2SEnji Cooper // parameter) for MSVC
52*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
53*28f6c2f2SEnji Cooper #if defined(_MSC_VER) && (_MSC_VER == 1900)
54b89a7cc2SEnji Cooper // and silence C4800 (C4800: 'int *const ': forcing value
55b89a7cc2SEnji Cooper // to bool 'true' or 'false') for MSVC 14
56*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800)
57b89a7cc2SEnji Cooper #endif
58b89a7cc2SEnji Cooper
59*28f6c2f2SEnji Cooper namespace internal {
60*28f6c2f2SEnji Cooper
61*28f6c2f2SEnji Cooper // Implements the polymorphic IsEmpty matcher, which
62*28f6c2f2SEnji Cooper // can be used as a Matcher<T> as long as T is either a container that defines
63*28f6c2f2SEnji Cooper // empty() and size() (e.g. std::vector or std::string), or a C-style string.
64*28f6c2f2SEnji Cooper class IsEmptyMatcher {
65*28f6c2f2SEnji Cooper public:
66*28f6c2f2SEnji Cooper // Matches anything that defines empty() and size().
67*28f6c2f2SEnji Cooper template <typename MatcheeContainerType>
MatchAndExplain(const MatcheeContainerType & c,MatchResultListener * listener)68*28f6c2f2SEnji Cooper bool MatchAndExplain(const MatcheeContainerType& c,
69*28f6c2f2SEnji Cooper MatchResultListener* listener) const {
70*28f6c2f2SEnji Cooper if (c.empty()) {
71b89a7cc2SEnji Cooper return true;
72b89a7cc2SEnji Cooper }
73*28f6c2f2SEnji Cooper *listener << "whose size is " << c.size();
74b89a7cc2SEnji Cooper return false;
75b89a7cc2SEnji Cooper }
76b89a7cc2SEnji Cooper
77*28f6c2f2SEnji Cooper // Matches C-style strings.
MatchAndExplain(const char * s,MatchResultListener * listener)78*28f6c2f2SEnji Cooper bool MatchAndExplain(const char* s, MatchResultListener* listener) const {
79*28f6c2f2SEnji Cooper return MatchAndExplain(std::string(s), listener);
80*28f6c2f2SEnji Cooper }
81*28f6c2f2SEnji Cooper
82*28f6c2f2SEnji Cooper // Describes what this matcher matches.
DescribeTo(std::ostream * os)83*28f6c2f2SEnji Cooper void DescribeTo(std::ostream* os) const { *os << "is empty"; }
84*28f6c2f2SEnji Cooper
DescribeNegationTo(std::ostream * os)85*28f6c2f2SEnji Cooper void DescribeNegationTo(std::ostream* os) const { *os << "isn't empty"; }
86*28f6c2f2SEnji Cooper };
87*28f6c2f2SEnji Cooper
88*28f6c2f2SEnji Cooper } // namespace internal
89*28f6c2f2SEnji Cooper
90*28f6c2f2SEnji Cooper // Creates a polymorphic matcher that matches an empty container or C-style
91*28f6c2f2SEnji Cooper // string. The container must support both size() and empty(), which all
92*28f6c2f2SEnji Cooper // STL-like containers provide.
IsEmpty()93*28f6c2f2SEnji Cooper inline PolymorphicMatcher<internal::IsEmptyMatcher> IsEmpty() {
94*28f6c2f2SEnji Cooper return MakePolymorphicMatcher(internal::IsEmptyMatcher());
95*28f6c2f2SEnji Cooper }
96*28f6c2f2SEnji Cooper
97b89a7cc2SEnji Cooper // Define a matcher that matches a value that evaluates in boolean
98b89a7cc2SEnji Cooper // context to true. Useful for types that define "explicit operator
99b89a7cc2SEnji Cooper // bool" operators and so can't be compared for equality with true
100b89a7cc2SEnji Cooper // and false.
101b89a7cc2SEnji Cooper MATCHER(IsTrue, negation ? "is false" : "is true") {
102b89a7cc2SEnji Cooper return static_cast<bool>(arg);
103b89a7cc2SEnji Cooper }
104b89a7cc2SEnji Cooper
105b89a7cc2SEnji Cooper // Define a matcher that matches a value that evaluates in boolean
106b89a7cc2SEnji Cooper // context to false. Useful for types that define "explicit operator
107b89a7cc2SEnji Cooper // bool" operators and so can't be compared for equality with true
108b89a7cc2SEnji Cooper // and false.
109b89a7cc2SEnji Cooper MATCHER(IsFalse, negation ? "is true" : "is false") {
110b89a7cc2SEnji Cooper return !static_cast<bool>(arg);
111b89a7cc2SEnji Cooper }
112b89a7cc2SEnji Cooper
113*28f6c2f2SEnji Cooper #if defined(_MSC_VER) && (_MSC_VER == 1900)
114*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_() // 4800
115b89a7cc2SEnji Cooper #endif
116*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100
117b89a7cc2SEnji Cooper
118b89a7cc2SEnji Cooper } // namespace testing
119b89a7cc2SEnji Cooper
120*28f6c2f2SEnji Cooper #endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_MATCHERS_H_
121