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