gtest-assertion-result.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright 2005, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // The Google C++ Testing and Mocking Framework (Google Test)
  30. //
  31. // This file implements the AssertionResult type.
  32. // IWYU pragma: private, include "gtest/gtest.h"
  33. // IWYU pragma: friend gtest/.*
  34. // IWYU pragma: friend gmock/.*
  35. #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_
  36. #define GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_
  37. #include <memory>
  38. #include <ostream>
  39. #include <string>
  40. #include <type_traits>
  41. #include "gtest/gtest-message.h"
  42. #include "gtest/internal/gtest-port.h"
  43. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
  44. /* class A needs to have dll-interface to be used by clients of class B */)
  45. namespace testing {
  46. // A class for indicating whether an assertion was successful. When
  47. // the assertion wasn't successful, the AssertionResult object
  48. // remembers a non-empty message that describes how it failed.
  49. //
  50. // To create an instance of this class, use one of the factory functions
  51. // (AssertionSuccess() and AssertionFailure()).
  52. //
  53. // This class is useful for two purposes:
  54. // 1. Defining predicate functions to be used with Boolean test assertions
  55. // EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts
  56. // 2. Defining predicate-format functions to be
  57. // used with predicate assertions (ASSERT_PRED_FORMAT*, etc).
  58. //
  59. // For example, if you define IsEven predicate:
  60. //
  61. // testing::AssertionResult IsEven(int n) {
  62. // if ((n % 2) == 0)
  63. // return testing::AssertionSuccess();
  64. // else
  65. // return testing::AssertionFailure() << n << " is odd";
  66. // }
  67. //
  68. // Then the failed expectation EXPECT_TRUE(IsEven(Fib(5)))
  69. // will print the message
  70. //
  71. // Value of: IsEven(Fib(5))
  72. // Actual: false (5 is odd)
  73. // Expected: true
  74. //
  75. // instead of a more opaque
  76. //
  77. // Value of: IsEven(Fib(5))
  78. // Actual: false
  79. // Expected: true
  80. //
  81. // in case IsEven is a simple Boolean predicate.
  82. //
  83. // If you expect your predicate to be reused and want to support informative
  84. // messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up
  85. // about half as often as positive ones in our tests), supply messages for
  86. // both success and failure cases:
  87. //
  88. // testing::AssertionResult IsEven(int n) {
  89. // if ((n % 2) == 0)
  90. // return testing::AssertionSuccess() << n << " is even";
  91. // else
  92. // return testing::AssertionFailure() << n << " is odd";
  93. // }
  94. //
  95. // Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print
  96. //
  97. // Value of: IsEven(Fib(6))
  98. // Actual: true (8 is even)
  99. // Expected: false
  100. //
  101. // NB: Predicates that support negative Boolean assertions have reduced
  102. // performance in positive ones so be careful not to use them in tests
  103. // that have lots (tens of thousands) of positive Boolean assertions.
  104. //
  105. // To use this class with EXPECT_PRED_FORMAT assertions such as:
  106. //
  107. // // Verifies that Foo() returns an even number.
  108. // EXPECT_PRED_FORMAT1(IsEven, Foo());
  109. //
  110. // you need to define:
  111. //
  112. // testing::AssertionResult IsEven(const char* expr, int n) {
  113. // if ((n % 2) == 0)
  114. // return testing::AssertionSuccess();
  115. // else
  116. // return testing::AssertionFailure()
  117. // << "Expected: " << expr << " is even\n Actual: it's " << n;
  118. // }
  119. //
  120. // If Foo() returns 5, you will see the following message:
  121. //
  122. // Expected: Foo() is even
  123. // Actual: it's 5
  124. class GTEST_API_ AssertionResult {
  125. public:
  126. // Copy constructor.
  127. // Used in EXPECT_TRUE/FALSE(assertion_result).
  128. AssertionResult(const AssertionResult& other);
  129. // C4800 is a level 3 warning in Visual Studio 2015 and earlier.
  130. // This warning is not emitted in Visual Studio 2017.
  131. // This warning is off by default starting in Visual Studio 2019 but can be
  132. // enabled with command-line options.
  133. #if defined(_MSC_VER) && (_MSC_VER < 1910 || _MSC_VER >= 1920)
  134. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 /* forcing value to bool */)
  135. #endif
  136. // Used in the EXPECT_TRUE/FALSE(bool_expression).
  137. //
  138. // T must be contextually convertible to bool.
  139. //
  140. // The second parameter prevents this overload from being considered if
  141. // the argument is implicitly convertible to AssertionResult. In that case
  142. // we want AssertionResult's copy constructor to be used.
  143. template <typename T>
  144. explicit AssertionResult(
  145. const T& success,
  146. typename std::enable_if<
  147. !std::is_convertible<T, AssertionResult>::value>::type*
  148. /*enabler*/
  149. = nullptr)
  150. : success_(success) {}
  151. #if defined(_MSC_VER) && (_MSC_VER < 1910 || _MSC_VER >= 1920)
  152. GTEST_DISABLE_MSC_WARNINGS_POP_()
  153. #endif
  154. // Assignment operator.
  155. AssertionResult& operator=(AssertionResult other) {
  156. swap(other);
  157. return *this;
  158. }
  159. // Returns true if and only if the assertion succeeded.
  160. operator bool() const { return success_; } // NOLINT
  161. // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE.
  162. AssertionResult operator!() const;
  163. // Returns the text streamed into this AssertionResult. Test assertions
  164. // use it when they fail (i.e., the predicate's outcome doesn't match the
  165. // assertion's expectation). When nothing has been streamed into the
  166. // object, returns an empty string.
  167. const char* message() const {
  168. return message_ != nullptr ? message_->c_str() : "";
  169. }
  170. // Deprecated; please use message() instead.
  171. const char* failure_message() const { return message(); }
  172. // Streams a custom failure message into this object.
  173. template <typename T>
  174. AssertionResult& operator<<(const T& value) {
  175. AppendMessage(Message() << value);
  176. return *this;
  177. }
  178. // Allows streaming basic output manipulators such as endl or flush into
  179. // this object.
  180. AssertionResult& operator<<(
  181. ::std::ostream& (*basic_manipulator)(::std::ostream& stream)) {
  182. AppendMessage(Message() << basic_manipulator);
  183. return *this;
  184. }
  185. private:
  186. // Appends the contents of message to message_.
  187. void AppendMessage(const Message& a_message) {
  188. if (message_ == nullptr) message_ = ::std::make_unique<::std::string>();
  189. message_->append(a_message.GetString().c_str());
  190. }
  191. // Swap the contents of this AssertionResult with other.
  192. void swap(AssertionResult& other);
  193. // Stores result of the assertion predicate.
  194. bool success_;
  195. // Stores the message describing the condition in case the expectation
  196. // construct is not satisfied with the predicate's outcome.
  197. // Referenced via a pointer to avoid taking too much stack frame space
  198. // with test assertions.
  199. std::unique_ptr< ::std::string> message_;
  200. };
  201. // Makes a successful assertion result.
  202. GTEST_API_ AssertionResult AssertionSuccess();
  203. // Makes a failed assertion result.
  204. GTEST_API_ AssertionResult AssertionFailure();
  205. // Makes a failed assertion result with the given failure message.
  206. // Deprecated; use AssertionFailure() << msg.
  207. GTEST_API_ AssertionResult AssertionFailure(const Message& msg);
  208. } // namespace testing
  209. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
  210. #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_