Path: blob/main/contrib/googletest/googlemock/include/gmock/gmock-more-matchers.h
48375 views
// Copyright 2013, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// 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 than34// gmock-more-matchers-test.cc.3536// IWYU pragma: private, include "gmock/gmock.h"37// IWYU pragma: friend gmock/.*3839#ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_MATCHERS_H_40#define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_MATCHERS_H_4142#include <ostream>43#include <string>4445#include "gmock/gmock-matchers.h"4647namespace testing {4849// Silence C4100 (unreferenced formal50// parameter) for MSVC51GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)52#if defined(_MSC_VER) && (_MSC_VER == 1900)53// and silence C4800 (C4800: 'int *const ': forcing value54// to bool 'true' or 'false') for MSVC 1455GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800)56#endif5758namespace internal {5960// Implements the polymorphic IsEmpty matcher, which61// can be used as a Matcher<T> as long as T is either a container that defines62// empty() and size() (e.g. std::vector or std::string), or a C-style string.63class IsEmptyMatcher {64public:65// Matches anything that defines empty() and size().66template <typename MatcheeContainerType>67bool MatchAndExplain(const MatcheeContainerType& c,68MatchResultListener* listener) const {69if (c.empty()) {70return true;71}72*listener << "whose size is " << c.size();73return false;74}7576// Matches C-style strings.77bool MatchAndExplain(const char* s, MatchResultListener* listener) const {78return MatchAndExplain(std::string(s), listener);79}8081// Describes what this matcher matches.82void DescribeTo(std::ostream* os) const { *os << "is empty"; }8384void DescribeNegationTo(std::ostream* os) const { *os << "isn't empty"; }85};8687} // namespace internal8889// Creates a polymorphic matcher that matches an empty container or C-style90// string. The container must support both size() and empty(), which all91// STL-like containers provide.92inline PolymorphicMatcher<internal::IsEmptyMatcher> IsEmpty() {93return MakePolymorphicMatcher(internal::IsEmptyMatcher());94}9596// Define a matcher that matches a value that evaluates in boolean97// context to true. Useful for types that define "explicit operator98// bool" operators and so can't be compared for equality with true99// and false.100MATCHER(IsTrue, negation ? "is false" : "is true") {101return static_cast<bool>(arg);102}103104// Define a matcher that matches a value that evaluates in boolean105// context to false. Useful for types that define "explicit operator106// bool" operators and so can't be compared for equality with true107// and false.108MATCHER(IsFalse, negation ? "is true" : "is false") {109return !static_cast<bool>(arg);110}111112#if defined(_MSC_VER) && (_MSC_VER == 1900)113GTEST_DISABLE_MSC_WARNINGS_POP_() // 4800114#endif115GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100116117} // namespace testing118119#endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_MATCHERS_H_120121122