121 lines · c
1// Copyright 2013, Google Inc.2// All rights reserved.3//4// Redistribution and use in source and binary forms, with or without5// modification, are permitted provided that the following conditions are6// met:7//8// * Redistributions of source code must retain the above copyright9// notice, this list of conditions and the following disclaimer.10// * Redistributions in binary form must reproduce the above11// copyright notice, this list of conditions and the following disclaimer12// in the documentation and/or other materials provided with the13// distribution.14// * Neither the name of Google Inc. nor the names of its15// contributors may be used to endorse or promote products derived from16// this software without specific prior written permission.17//18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE28// 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 than35// 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 formal51// parameter) for MSVC52GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)53#if defined(_MSC_VER) && (_MSC_VER == 1900)54// and silence C4800 (C4800: 'int *const ': forcing value55// to bool 'true' or 'false') for MSVC 1456GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800)57#endif58 59namespace internal {60 61// Implements the polymorphic IsEmpty matcher, which62// can be used as a Matcher<T> as long as T is either a container that defines63// empty() and size() (e.g. std::vector or std::string), or a C-style string.64class IsEmptyMatcher {65 public:66 // Matches anything that defines empty() and size().67 template <typename MatcheeContainerType>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.78 bool MatchAndExplain(const char* s, MatchResultListener* listener) const {79 return MatchAndExplain(std::string(s), listener);80 }81 82 // Describes what this matcher matches.83 void DescribeTo(std::ostream* os) const { *os << "is empty"; }84 85 void DescribeNegationTo(std::ostream* os) const { *os << "isn't empty"; }86};87 88} // namespace internal89 90// Creates a polymorphic matcher that matches an empty container or C-style91// string. The container must support both size() and empty(), which all92// STL-like containers provide.93inline PolymorphicMatcher<internal::IsEmptyMatcher> IsEmpty() {94 return MakePolymorphicMatcher(internal::IsEmptyMatcher());95}96 97// Define a matcher that matches a value that evaluates in boolean98// context to true. Useful for types that define "explicit operator99// bool" operators and so can't be compared for equality with true100// and false.101MATCHER(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 boolean106// context to false. Useful for types that define "explicit operator107// bool" operators and so can't be compared for equality with true108// and false.109MATCHER(IsFalse, negation ? "is true" : "is false") {110 return !static_cast<bool>(arg);111}112 113#if defined(_MSC_VER) && (_MSC_VER == 1900)114GTEST_DISABLE_MSC_WARNINGS_POP_() // 4800115#endif116GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100117 118} // namespace testing119 120#endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_MATCHERS_H_121