|
||||
File indexing completed on 2024-11-15 09:43:09
0001 // Copyright 2005, Google Inc. 0002 // All rights reserved. 0003 // 0004 // Redistribution and use in source and binary forms, with or without 0005 // modification, are permitted provided that the following conditions are 0006 // met: 0007 // 0008 // * Redistributions of source code must retain the above copyright 0009 // notice, this list of conditions and the following disclaimer. 0010 // * Redistributions in binary form must reproduce the above 0011 // copyright notice, this list of conditions and the following disclaimer 0012 // in the documentation and/or other materials provided with the 0013 // distribution. 0014 // * Neither the name of Google Inc. nor the names of its 0015 // contributors may be used to endorse or promote products derived from 0016 // this software without specific prior written permission. 0017 // 0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0029 0030 // The Google C++ Testing and Mocking Framework (Google Test) 0031 // 0032 // This file implements the AssertionResult type. 0033 0034 // IWYU pragma: private, include "gtest/gtest.h" 0035 // IWYU pragma: friend gtest/.* 0036 // IWYU pragma: friend gmock/.* 0037 0038 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_ 0039 #define GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_ 0040 0041 #include <memory> 0042 #include <ostream> 0043 #include <string> 0044 #include <type_traits> 0045 0046 #include "gtest/gtest-message.h" 0047 #include "gtest/internal/gtest-port.h" 0048 0049 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ 0050 /* class A needs to have dll-interface to be used by clients of class B */) 0051 0052 namespace testing { 0053 0054 // A class for indicating whether an assertion was successful. When 0055 // the assertion wasn't successful, the AssertionResult object 0056 // remembers a non-empty message that describes how it failed. 0057 // 0058 // To create an instance of this class, use one of the factory functions 0059 // (AssertionSuccess() and AssertionFailure()). 0060 // 0061 // This class is useful for two purposes: 0062 // 1. Defining predicate functions to be used with Boolean test assertions 0063 // EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts 0064 // 2. Defining predicate-format functions to be 0065 // used with predicate assertions (ASSERT_PRED_FORMAT*, etc). 0066 // 0067 // For example, if you define IsEven predicate: 0068 // 0069 // testing::AssertionResult IsEven(int n) { 0070 // if ((n % 2) == 0) 0071 // return testing::AssertionSuccess(); 0072 // else 0073 // return testing::AssertionFailure() << n << " is odd"; 0074 // } 0075 // 0076 // Then the failed expectation EXPECT_TRUE(IsEven(Fib(5))) 0077 // will print the message 0078 // 0079 // Value of: IsEven(Fib(5)) 0080 // Actual: false (5 is odd) 0081 // Expected: true 0082 // 0083 // instead of a more opaque 0084 // 0085 // Value of: IsEven(Fib(5)) 0086 // Actual: false 0087 // Expected: true 0088 // 0089 // in case IsEven is a simple Boolean predicate. 0090 // 0091 // If you expect your predicate to be reused and want to support informative 0092 // messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up 0093 // about half as often as positive ones in our tests), supply messages for 0094 // both success and failure cases: 0095 // 0096 // testing::AssertionResult IsEven(int n) { 0097 // if ((n % 2) == 0) 0098 // return testing::AssertionSuccess() << n << " is even"; 0099 // else 0100 // return testing::AssertionFailure() << n << " is odd"; 0101 // } 0102 // 0103 // Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print 0104 // 0105 // Value of: IsEven(Fib(6)) 0106 // Actual: true (8 is even) 0107 // Expected: false 0108 // 0109 // NB: Predicates that support negative Boolean assertions have reduced 0110 // performance in positive ones so be careful not to use them in tests 0111 // that have lots (tens of thousands) of positive Boolean assertions. 0112 // 0113 // To use this class with EXPECT_PRED_FORMAT assertions such as: 0114 // 0115 // // Verifies that Foo() returns an even number. 0116 // EXPECT_PRED_FORMAT1(IsEven, Foo()); 0117 // 0118 // you need to define: 0119 // 0120 // testing::AssertionResult IsEven(const char* expr, int n) { 0121 // if ((n % 2) == 0) 0122 // return testing::AssertionSuccess(); 0123 // else 0124 // return testing::AssertionFailure() 0125 // << "Expected: " << expr << " is even\n Actual: it's " << n; 0126 // } 0127 // 0128 // If Foo() returns 5, you will see the following message: 0129 // 0130 // Expected: Foo() is even 0131 // Actual: it's 5 0132 // 0133 class GTEST_API_ AssertionResult { 0134 public: 0135 // Copy constructor. 0136 // Used in EXPECT_TRUE/FALSE(assertion_result). 0137 AssertionResult(const AssertionResult& other); 0138 0139 // C4800 is a level 3 warning in Visual Studio 2015 and earlier. 0140 // This warning is not emitted in Visual Studio 2017. 0141 // This warning is off by default starting in Visual Studio 2019 but can be 0142 // enabled with command-line options. 0143 #if defined(_MSC_VER) && (_MSC_VER < 1910 || _MSC_VER >= 1920) 0144 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 /* forcing value to bool */) 0145 #endif 0146 0147 // Used in the EXPECT_TRUE/FALSE(bool_expression). 0148 // 0149 // T must be contextually convertible to bool. 0150 // 0151 // The second parameter prevents this overload from being considered if 0152 // the argument is implicitly convertible to AssertionResult. In that case 0153 // we want AssertionResult's copy constructor to be used. 0154 template <typename T> 0155 explicit AssertionResult( 0156 const T& success, 0157 typename std::enable_if< 0158 !std::is_convertible<T, AssertionResult>::value>::type* 0159 /*enabler*/ 0160 = nullptr) 0161 : success_(success) {} 0162 0163 #if defined(_MSC_VER) && (_MSC_VER < 1910 || _MSC_VER >= 1920) 0164 GTEST_DISABLE_MSC_WARNINGS_POP_() 0165 #endif 0166 0167 // Assignment operator. 0168 AssertionResult& operator=(AssertionResult other) { 0169 swap(other); 0170 return *this; 0171 } 0172 0173 // Returns true if and only if the assertion succeeded. 0174 operator bool() const { return success_; } // NOLINT 0175 0176 // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE. 0177 AssertionResult operator!() const; 0178 0179 // Returns the text streamed into this AssertionResult. Test assertions 0180 // use it when they fail (i.e., the predicate's outcome doesn't match the 0181 // assertion's expectation). When nothing has been streamed into the 0182 // object, returns an empty string. 0183 const char* message() const { 0184 return message_.get() != nullptr ? message_->c_str() : ""; 0185 } 0186 // Deprecated; please use message() instead. 0187 const char* failure_message() const { return message(); } 0188 0189 // Streams a custom failure message into this object. 0190 template <typename T> 0191 AssertionResult& operator<<(const T& value) { 0192 AppendMessage(Message() << value); 0193 return *this; 0194 } 0195 0196 // Allows streaming basic output manipulators such as endl or flush into 0197 // this object. 0198 AssertionResult& operator<<( 0199 ::std::ostream& (*basic_manipulator)(::std::ostream& stream)) { 0200 AppendMessage(Message() << basic_manipulator); 0201 return *this; 0202 } 0203 0204 private: 0205 // Appends the contents of message to message_. 0206 void AppendMessage(const Message& a_message) { 0207 if (message_.get() == nullptr) message_.reset(new ::std::string); 0208 message_->append(a_message.GetString().c_str()); 0209 } 0210 0211 // Swap the contents of this AssertionResult with other. 0212 void swap(AssertionResult& other); 0213 0214 // Stores result of the assertion predicate. 0215 bool success_; 0216 // Stores the message describing the condition in case the expectation 0217 // construct is not satisfied with the predicate's outcome. 0218 // Referenced via a pointer to avoid taking too much stack frame space 0219 // with test assertions. 0220 std::unique_ptr< ::std::string> message_; 0221 }; 0222 0223 // Makes a successful assertion result. 0224 GTEST_API_ AssertionResult AssertionSuccess(); 0225 0226 // Makes a failed assertion result. 0227 GTEST_API_ AssertionResult AssertionFailure(); 0228 0229 // Makes a failed assertion result with the given failure message. 0230 // Deprecated; use AssertionFailure() << msg. 0231 GTEST_API_ AssertionResult AssertionFailure(const Message& msg); 0232 0233 } // namespace testing 0234 0235 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 0236 0237 #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |