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
48namespace testing {
49
50// Silence C4100 (unreferenced formal
51// parameter) for MSVC
52#ifdef _MSC_VER
53#pragma warning(push)
54#pragma warning(disable : 4100)
55#if (_MSC_VER == 1900)
56// and silence C4800 (C4800: 'int *const ': forcing value
57// to bool 'true' or 'false') for MSVC 14
58#pragma warning(disable : 4800)
59#endif
60#endif
61
62namespace internal {
63
64// Implements the polymorphic IsEmpty matcher, which
65// can be used as a Matcher<T> as long as T is either a container that defines
66// empty() and size() (e.g. std::vector or std::string), or a C-style string.
67class IsEmptyMatcher {
68 public:
69 // Matches anything that defines empty() and size().
70 template <typename MatcheeContainerType>
71 bool MatchAndExplain(const MatcheeContainerType& c,
72 MatchResultListener* listener) const {
73 if (c.empty()) {
74 return true;
75 }
76 *listener << "whose size is " << c.size();
77 return false;
78 }
79
80 // Matches C-style strings.
81 bool MatchAndExplain(const char* s, MatchResultListener* listener) const {
82 return MatchAndExplain(c: std::string(s), listener);
83 }
84
85 // Describes what this matcher matches.
86 void DescribeTo(std::ostream* os) const { *os << "is empty"; }
87
88 void DescribeNegationTo(std::ostream* os) const { *os << "isn't empty"; }
89};
90
91} // namespace internal
92
93// Creates a polymorphic matcher that matches an empty container or C-style
94// string. The container must support both size() and empty(), which all
95// STL-like containers provide.
96inline PolymorphicMatcher<internal::IsEmptyMatcher> IsEmpty() {
97 return MakePolymorphicMatcher(impl: internal::IsEmptyMatcher());
98}
99
100// Define a matcher that matches a value that evaluates in boolean
101// context to true. Useful for types that define "explicit operator
102// bool" operators and so can't be compared for equality with true
103// and false.
104MATCHER(IsTrue, negation ? "is false" : "is true") {
105 return static_cast<bool>(arg);
106}
107
108// Define a matcher that matches a value that evaluates in boolean
109// context to false. Useful for types that define "explicit operator
110// bool" operators and so can't be compared for equality with true
111// and false.
112MATCHER(IsFalse, negation ? "is true" : "is false") {
113 return !static_cast<bool>(arg);
114}
115
116#ifdef _MSC_VER
117#pragma warning(pop)
118#endif
119
120} // namespace testing
121
122#endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_MATCHERS_H_
123