11cb0ef41Sopenharmony_ci// Copyright 2005, Google Inc. 21cb0ef41Sopenharmony_ci// All rights reserved. 31cb0ef41Sopenharmony_ci// 41cb0ef41Sopenharmony_ci// Redistribution and use in source and binary forms, with or without 51cb0ef41Sopenharmony_ci// modification, are permitted provided that the following conditions are 61cb0ef41Sopenharmony_ci// met: 71cb0ef41Sopenharmony_ci// 81cb0ef41Sopenharmony_ci// * Redistributions of source code must retain the above copyright 91cb0ef41Sopenharmony_ci// notice, this list of conditions and the following disclaimer. 101cb0ef41Sopenharmony_ci// * Redistributions in binary form must reproduce the above 111cb0ef41Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer 121cb0ef41Sopenharmony_ci// in the documentation and/or other materials provided with the 131cb0ef41Sopenharmony_ci// distribution. 141cb0ef41Sopenharmony_ci// * Neither the name of Google Inc. nor the names of its 151cb0ef41Sopenharmony_ci// contributors may be used to endorse or promote products derived from 161cb0ef41Sopenharmony_ci// this software without specific prior written permission. 171cb0ef41Sopenharmony_ci// 181cb0ef41Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 191cb0ef41Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 201cb0ef41Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 211cb0ef41Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 221cb0ef41Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 231cb0ef41Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 241cb0ef41Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 251cb0ef41Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 261cb0ef41Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 271cb0ef41Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 281cb0ef41Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci// The Google C++ Testing and Mocking Framework (Google Test) 311cb0ef41Sopenharmony_ci// 321cb0ef41Sopenharmony_ci// This file implements the AssertionResult type. 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci// IWYU pragma: private, include "gtest/gtest.h" 351cb0ef41Sopenharmony_ci// IWYU pragma: friend gtest/.* 361cb0ef41Sopenharmony_ci// IWYU pragma: friend gmock/.* 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_ 391cb0ef41Sopenharmony_ci#define GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_ 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci#include <memory> 421cb0ef41Sopenharmony_ci#include <ostream> 431cb0ef41Sopenharmony_ci#include <string> 441cb0ef41Sopenharmony_ci#include <type_traits> 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci#include "gtest/gtest-message.h" 471cb0ef41Sopenharmony_ci#include "gtest/internal/gtest-port.h" 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ciGTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ 501cb0ef41Sopenharmony_ci/* class A needs to have dll-interface to be used by clients of class B */) 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_cinamespace testing { 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci// A class for indicating whether an assertion was successful. When 551cb0ef41Sopenharmony_ci// the assertion wasn't successful, the AssertionResult object 561cb0ef41Sopenharmony_ci// remembers a non-empty message that describes how it failed. 571cb0ef41Sopenharmony_ci// 581cb0ef41Sopenharmony_ci// To create an instance of this class, use one of the factory functions 591cb0ef41Sopenharmony_ci// (AssertionSuccess() and AssertionFailure()). 601cb0ef41Sopenharmony_ci// 611cb0ef41Sopenharmony_ci// This class is useful for two purposes: 621cb0ef41Sopenharmony_ci// 1. Defining predicate functions to be used with Boolean test assertions 631cb0ef41Sopenharmony_ci// EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts 641cb0ef41Sopenharmony_ci// 2. Defining predicate-format functions to be 651cb0ef41Sopenharmony_ci// used with predicate assertions (ASSERT_PRED_FORMAT*, etc). 661cb0ef41Sopenharmony_ci// 671cb0ef41Sopenharmony_ci// For example, if you define IsEven predicate: 681cb0ef41Sopenharmony_ci// 691cb0ef41Sopenharmony_ci// testing::AssertionResult IsEven(int n) { 701cb0ef41Sopenharmony_ci// if ((n % 2) == 0) 711cb0ef41Sopenharmony_ci// return testing::AssertionSuccess(); 721cb0ef41Sopenharmony_ci// else 731cb0ef41Sopenharmony_ci// return testing::AssertionFailure() << n << " is odd"; 741cb0ef41Sopenharmony_ci// } 751cb0ef41Sopenharmony_ci// 761cb0ef41Sopenharmony_ci// Then the failed expectation EXPECT_TRUE(IsEven(Fib(5))) 771cb0ef41Sopenharmony_ci// will print the message 781cb0ef41Sopenharmony_ci// 791cb0ef41Sopenharmony_ci// Value of: IsEven(Fib(5)) 801cb0ef41Sopenharmony_ci// Actual: false (5 is odd) 811cb0ef41Sopenharmony_ci// Expected: true 821cb0ef41Sopenharmony_ci// 831cb0ef41Sopenharmony_ci// instead of a more opaque 841cb0ef41Sopenharmony_ci// 851cb0ef41Sopenharmony_ci// Value of: IsEven(Fib(5)) 861cb0ef41Sopenharmony_ci// Actual: false 871cb0ef41Sopenharmony_ci// Expected: true 881cb0ef41Sopenharmony_ci// 891cb0ef41Sopenharmony_ci// in case IsEven is a simple Boolean predicate. 901cb0ef41Sopenharmony_ci// 911cb0ef41Sopenharmony_ci// If you expect your predicate to be reused and want to support informative 921cb0ef41Sopenharmony_ci// messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up 931cb0ef41Sopenharmony_ci// about half as often as positive ones in our tests), supply messages for 941cb0ef41Sopenharmony_ci// both success and failure cases: 951cb0ef41Sopenharmony_ci// 961cb0ef41Sopenharmony_ci// testing::AssertionResult IsEven(int n) { 971cb0ef41Sopenharmony_ci// if ((n % 2) == 0) 981cb0ef41Sopenharmony_ci// return testing::AssertionSuccess() << n << " is even"; 991cb0ef41Sopenharmony_ci// else 1001cb0ef41Sopenharmony_ci// return testing::AssertionFailure() << n << " is odd"; 1011cb0ef41Sopenharmony_ci// } 1021cb0ef41Sopenharmony_ci// 1031cb0ef41Sopenharmony_ci// Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print 1041cb0ef41Sopenharmony_ci// 1051cb0ef41Sopenharmony_ci// Value of: IsEven(Fib(6)) 1061cb0ef41Sopenharmony_ci// Actual: true (8 is even) 1071cb0ef41Sopenharmony_ci// Expected: false 1081cb0ef41Sopenharmony_ci// 1091cb0ef41Sopenharmony_ci// NB: Predicates that support negative Boolean assertions have reduced 1101cb0ef41Sopenharmony_ci// performance in positive ones so be careful not to use them in tests 1111cb0ef41Sopenharmony_ci// that have lots (tens of thousands) of positive Boolean assertions. 1121cb0ef41Sopenharmony_ci// 1131cb0ef41Sopenharmony_ci// To use this class with EXPECT_PRED_FORMAT assertions such as: 1141cb0ef41Sopenharmony_ci// 1151cb0ef41Sopenharmony_ci// // Verifies that Foo() returns an even number. 1161cb0ef41Sopenharmony_ci// EXPECT_PRED_FORMAT1(IsEven, Foo()); 1171cb0ef41Sopenharmony_ci// 1181cb0ef41Sopenharmony_ci// you need to define: 1191cb0ef41Sopenharmony_ci// 1201cb0ef41Sopenharmony_ci// testing::AssertionResult IsEven(const char* expr, int n) { 1211cb0ef41Sopenharmony_ci// if ((n % 2) == 0) 1221cb0ef41Sopenharmony_ci// return testing::AssertionSuccess(); 1231cb0ef41Sopenharmony_ci// else 1241cb0ef41Sopenharmony_ci// return testing::AssertionFailure() 1251cb0ef41Sopenharmony_ci// << "Expected: " << expr << " is even\n Actual: it's " << n; 1261cb0ef41Sopenharmony_ci// } 1271cb0ef41Sopenharmony_ci// 1281cb0ef41Sopenharmony_ci// If Foo() returns 5, you will see the following message: 1291cb0ef41Sopenharmony_ci// 1301cb0ef41Sopenharmony_ci// Expected: Foo() is even 1311cb0ef41Sopenharmony_ci// Actual: it's 5 1321cb0ef41Sopenharmony_ci// 1331cb0ef41Sopenharmony_ciclass GTEST_API_ AssertionResult { 1341cb0ef41Sopenharmony_ci public: 1351cb0ef41Sopenharmony_ci // Copy constructor. 1361cb0ef41Sopenharmony_ci // Used in EXPECT_TRUE/FALSE(assertion_result). 1371cb0ef41Sopenharmony_ci AssertionResult(const AssertionResult& other); 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ci// C4800 is a level 3 warning in Visual Studio 2015 and earlier. 1401cb0ef41Sopenharmony_ci// This warning is not emitted in Visual Studio 2017. 1411cb0ef41Sopenharmony_ci// This warning is off by default starting in Visual Studio 2019 but can be 1421cb0ef41Sopenharmony_ci// enabled with command-line options. 1431cb0ef41Sopenharmony_ci#if defined(_MSC_VER) && (_MSC_VER < 1910 || _MSC_VER >= 1920) 1441cb0ef41Sopenharmony_ci GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 /* forcing value to bool */) 1451cb0ef41Sopenharmony_ci#endif 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_ci // Used in the EXPECT_TRUE/FALSE(bool_expression). 1481cb0ef41Sopenharmony_ci // 1491cb0ef41Sopenharmony_ci // T must be contextually convertible to bool. 1501cb0ef41Sopenharmony_ci // 1511cb0ef41Sopenharmony_ci // The second parameter prevents this overload from being considered if 1521cb0ef41Sopenharmony_ci // the argument is implicitly convertible to AssertionResult. In that case 1531cb0ef41Sopenharmony_ci // we want AssertionResult's copy constructor to be used. 1541cb0ef41Sopenharmony_ci template <typename T> 1551cb0ef41Sopenharmony_ci explicit AssertionResult( 1561cb0ef41Sopenharmony_ci const T& success, 1571cb0ef41Sopenharmony_ci typename std::enable_if< 1581cb0ef41Sopenharmony_ci !std::is_convertible<T, AssertionResult>::value>::type* 1591cb0ef41Sopenharmony_ci /*enabler*/ 1601cb0ef41Sopenharmony_ci = nullptr) 1611cb0ef41Sopenharmony_ci : success_(success) {} 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ci#if defined(_MSC_VER) && (_MSC_VER < 1910 || _MSC_VER >= 1920) 1641cb0ef41Sopenharmony_ci GTEST_DISABLE_MSC_WARNINGS_POP_() 1651cb0ef41Sopenharmony_ci#endif 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_ci // Assignment operator. 1681cb0ef41Sopenharmony_ci AssertionResult& operator=(AssertionResult other) { 1691cb0ef41Sopenharmony_ci swap(other); 1701cb0ef41Sopenharmony_ci return *this; 1711cb0ef41Sopenharmony_ci } 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ci // Returns true if and only if the assertion succeeded. 1741cb0ef41Sopenharmony_ci operator bool() const { return success_; } // NOLINT 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE. 1771cb0ef41Sopenharmony_ci AssertionResult operator!() const; 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ci // Returns the text streamed into this AssertionResult. Test assertions 1801cb0ef41Sopenharmony_ci // use it when they fail (i.e., the predicate's outcome doesn't match the 1811cb0ef41Sopenharmony_ci // assertion's expectation). When nothing has been streamed into the 1821cb0ef41Sopenharmony_ci // object, returns an empty string. 1831cb0ef41Sopenharmony_ci const char* message() const { 1841cb0ef41Sopenharmony_ci return message_ != nullptr ? message_->c_str() : ""; 1851cb0ef41Sopenharmony_ci } 1861cb0ef41Sopenharmony_ci // Deprecated; please use message() instead. 1871cb0ef41Sopenharmony_ci const char* failure_message() const { return message(); } 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci // Streams a custom failure message into this object. 1901cb0ef41Sopenharmony_ci template <typename T> 1911cb0ef41Sopenharmony_ci AssertionResult& operator<<(const T& value) { 1921cb0ef41Sopenharmony_ci AppendMessage(Message() << value); 1931cb0ef41Sopenharmony_ci return *this; 1941cb0ef41Sopenharmony_ci } 1951cb0ef41Sopenharmony_ci 1961cb0ef41Sopenharmony_ci // Allows streaming basic output manipulators such as endl or flush into 1971cb0ef41Sopenharmony_ci // this object. 1981cb0ef41Sopenharmony_ci AssertionResult& operator<<( 1991cb0ef41Sopenharmony_ci ::std::ostream& (*basic_manipulator)(::std::ostream& stream)) { 2001cb0ef41Sopenharmony_ci AppendMessage(Message() << basic_manipulator); 2011cb0ef41Sopenharmony_ci return *this; 2021cb0ef41Sopenharmony_ci } 2031cb0ef41Sopenharmony_ci 2041cb0ef41Sopenharmony_ci private: 2051cb0ef41Sopenharmony_ci // Appends the contents of message to message_. 2061cb0ef41Sopenharmony_ci void AppendMessage(const Message& a_message) { 2071cb0ef41Sopenharmony_ci if (message_ == nullptr) message_ = ::std::make_unique<::std::string>(); 2081cb0ef41Sopenharmony_ci message_->append(a_message.GetString().c_str()); 2091cb0ef41Sopenharmony_ci } 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ci // Swap the contents of this AssertionResult with other. 2121cb0ef41Sopenharmony_ci void swap(AssertionResult& other); 2131cb0ef41Sopenharmony_ci 2141cb0ef41Sopenharmony_ci // Stores result of the assertion predicate. 2151cb0ef41Sopenharmony_ci bool success_; 2161cb0ef41Sopenharmony_ci // Stores the message describing the condition in case the expectation 2171cb0ef41Sopenharmony_ci // construct is not satisfied with the predicate's outcome. 2181cb0ef41Sopenharmony_ci // Referenced via a pointer to avoid taking too much stack frame space 2191cb0ef41Sopenharmony_ci // with test assertions. 2201cb0ef41Sopenharmony_ci std::unique_ptr< ::std::string> message_; 2211cb0ef41Sopenharmony_ci}; 2221cb0ef41Sopenharmony_ci 2231cb0ef41Sopenharmony_ci// Makes a successful assertion result. 2241cb0ef41Sopenharmony_ciGTEST_API_ AssertionResult AssertionSuccess(); 2251cb0ef41Sopenharmony_ci 2261cb0ef41Sopenharmony_ci// Makes a failed assertion result. 2271cb0ef41Sopenharmony_ciGTEST_API_ AssertionResult AssertionFailure(); 2281cb0ef41Sopenharmony_ci 2291cb0ef41Sopenharmony_ci// Makes a failed assertion result with the given failure message. 2301cb0ef41Sopenharmony_ci// Deprecated; use AssertionFailure() << msg. 2311cb0ef41Sopenharmony_ciGTEST_API_ AssertionResult AssertionFailure(const Message& msg); 2321cb0ef41Sopenharmony_ci 2331cb0ef41Sopenharmony_ci} // namespace testing 2341cb0ef41Sopenharmony_ci 2351cb0ef41Sopenharmony_ciGTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ci#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_ 238