Path: blob/master/dep/googletest/include/gtest/gtest-assertion-result.h
4806 views
// Copyright 2005, 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// The Google C++ Testing and Mocking Framework (Google Test)30//31// This file implements the AssertionResult type.3233// IWYU pragma: private, include "gtest/gtest.h"34// IWYU pragma: friend gtest/.*35// IWYU pragma: friend gmock/.*3637#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_38#define GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_3940#include <memory>41#include <ostream>42#include <string>43#include <type_traits>4445#include "gtest/gtest-message.h"46#include "gtest/internal/gtest-port.h"4748GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \49/* class A needs to have dll-interface to be used by clients of class B */)5051namespace testing {5253// A class for indicating whether an assertion was successful. When54// the assertion wasn't successful, the AssertionResult object55// remembers a non-empty message that describes how it failed.56//57// To create an instance of this class, use one of the factory functions58// (AssertionSuccess() and AssertionFailure()).59//60// This class is useful for two purposes:61// 1. Defining predicate functions to be used with Boolean test assertions62// EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts63// 2. Defining predicate-format functions to be64// used with predicate assertions (ASSERT_PRED_FORMAT*, etc).65//66// For example, if you define IsEven predicate:67//68// testing::AssertionResult IsEven(int n) {69// if ((n % 2) == 0)70// return testing::AssertionSuccess();71// else72// return testing::AssertionFailure() << n << " is odd";73// }74//75// Then the failed expectation EXPECT_TRUE(IsEven(Fib(5)))76// will print the message77//78// Value of: IsEven(Fib(5))79// Actual: false (5 is odd)80// Expected: true81//82// instead of a more opaque83//84// Value of: IsEven(Fib(5))85// Actual: false86// Expected: true87//88// in case IsEven is a simple Boolean predicate.89//90// If you expect your predicate to be reused and want to support informative91// messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up92// about half as often as positive ones in our tests), supply messages for93// both success and failure cases:94//95// testing::AssertionResult IsEven(int n) {96// if ((n % 2) == 0)97// return testing::AssertionSuccess() << n << " is even";98// else99// return testing::AssertionFailure() << n << " is odd";100// }101//102// Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print103//104// Value of: IsEven(Fib(6))105// Actual: true (8 is even)106// Expected: false107//108// NB: Predicates that support negative Boolean assertions have reduced109// performance in positive ones so be careful not to use them in tests110// that have lots (tens of thousands) of positive Boolean assertions.111//112// To use this class with EXPECT_PRED_FORMAT assertions such as:113//114// // Verifies that Foo() returns an even number.115// EXPECT_PRED_FORMAT1(IsEven, Foo());116//117// you need to define:118//119// testing::AssertionResult IsEven(const char* expr, int n) {120// if ((n % 2) == 0)121// return testing::AssertionSuccess();122// else123// return testing::AssertionFailure()124// << "Expected: " << expr << " is even\n Actual: it's " << n;125// }126//127// If Foo() returns 5, you will see the following message:128//129// Expected: Foo() is even130// Actual: it's 5131//132133// Returned AssertionResult objects may not be ignored.134// Note: Disabled for SWIG as it doesn't parse attributes correctly.135#if !defined(SWIG)136class [[nodiscard]] AssertionResult;137#endif // !SWIG138139class GTEST_API_ AssertionResult {140public:141// Copy constructor.142// Used in EXPECT_TRUE/FALSE(assertion_result).143AssertionResult(const AssertionResult& other);144145// C4800 is a level 3 warning in Visual Studio 2015 and earlier.146// This warning is not emitted in Visual Studio 2017.147// This warning is off by default starting in Visual Studio 2019 but can be148// enabled with command-line options.149#if defined(_MSC_VER) && (_MSC_VER < 1910 || _MSC_VER >= 1920)150GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 /* forcing value to bool */)151#endif152153// Used in the EXPECT_TRUE/FALSE(bool_expression).154//155// T must be contextually convertible to bool.156//157// The second parameter prevents this overload from being considered if158// the argument is implicitly convertible to AssertionResult. In that case159// we want AssertionResult's copy constructor to be used.160template <typename T>161explicit AssertionResult(162const T& success,163typename std::enable_if<164!std::is_convertible<T, AssertionResult>::value>::type*165/*enabler*/166= nullptr)167: success_(success) {}168169#if defined(_MSC_VER) && (_MSC_VER < 1910 || _MSC_VER >= 1920)170GTEST_DISABLE_MSC_WARNINGS_POP_()171#endif172173// Assignment operator.174AssertionResult& operator=(AssertionResult other) {175swap(other);176return *this;177}178179// Returns true if and only if the assertion succeeded.180operator bool() const { return success_; } // NOLINT181182// Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE.183AssertionResult operator!() const;184185// Returns the text streamed into this AssertionResult. Test assertions186// use it when they fail (i.e., the predicate's outcome doesn't match the187// assertion's expectation). When nothing has been streamed into the188// object, returns an empty string.189const char* message() const {190return message_ != nullptr ? message_->c_str() : "";191}192// Deprecated; please use message() instead.193const char* failure_message() const { return message(); }194195// Streams a custom failure message into this object.196template <typename T>197AssertionResult& operator<<(const T& value) {198AppendMessage(Message() << value);199return *this;200}201202// Allows streaming basic output manipulators such as endl or flush into203// this object.204AssertionResult& operator<<(205::std::ostream& (*basic_manipulator)(::std::ostream& stream)) {206AppendMessage(Message() << basic_manipulator);207return *this;208}209210private:211// Appends the contents of message to message_.212void AppendMessage(const Message& a_message) {213if (message_ == nullptr) message_ = ::std::make_unique<::std::string>();214message_->append(a_message.GetString().c_str());215}216217// Swap the contents of this AssertionResult with other.218void swap(AssertionResult& other);219220// Stores result of the assertion predicate.221bool success_;222// Stores the message describing the condition in case the expectation223// construct is not satisfied with the predicate's outcome.224// Referenced via a pointer to avoid taking too much stack frame space225// with test assertions.226std::unique_ptr< ::std::string> message_;227};228229// Makes a successful assertion result.230GTEST_API_ AssertionResult AssertionSuccess();231232// Makes a failed assertion result.233GTEST_API_ AssertionResult AssertionFailure();234235// Makes a failed assertion result with the given failure message.236// Deprecated; use AssertionFailure() << msg.237GTEST_API_ AssertionResult AssertionFailure(const Message& msg);238239} // namespace testing240241GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251242243#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_244245246