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. //
  125. class GTEST_API_ AssertionResult {
  126. public:
  127. // Copy constructor.
  128. // Used in EXPECT_TRUE/FALSE(assertion_result).
  129. AssertionResult(const AssertionResult& other);
  130. // C4800 is a level 3 warning in Visual Studio 2015 and earlier.
  131. // This warning is not emitted in Visual Studio 2017.
  132. // This warning is off by default starting in Visual Studio 2019 but can be
  133. // enabled with command-line options.
  134. #if defined(_MSC_VER) && (_MSC_VER < 1910 || _MSC_VER >= 1920)
  135. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 /* forcing value to bool */)
  136. #endif
  137. // Used in the EXPECT_TRUE/FALSE(bool_expression).
  138. //
  139. // T must be contextually convertible to bool.
  140. //
  141. // The second parameter prevents this overload from being considered if
  142. // the argument is implicitly convertible to AssertionResult. In that case
  143. // we want AssertionResult's copy constructor to be used.
  144. template <typename T>
  145. explicit AssertionResult(
  146. const T& success,
  147. typename std::enable_if<
  148. !std::is_convertible<T, AssertionResult>::value>::type*
  149. /*enabler*/
  150. = nullptr)
  151. : success_(success) {}
  152. #if defined(_MSC_VER) && (_MSC_VER < 1910 || _MSC_VER >= 1920)
  153. GTEST_DISABLE_MSC_WARNINGS_POP_()
  154. #endif
  155. // Assignment operator.
  156. AssertionResult& operator=(AssertionResult other) {
  157. swap(other);
  158. return *this;
  159. }
  160. // Returns true if and only if the assertion succeeded.
  161. operator bool() const { return success_; } // NOLINT
  162. // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE.
  163. AssertionResult operator!() const;
  164. // Returns the text streamed into this AssertionResult. Test assertions
  165. // use it when they fail (i.e., the predicate's outcome doesn't match the
  166. // assertion's expectation). When nothing has been streamed into the
  167. // object, returns an empty string.
  168. const char* message() const {
  169. return message_.get() != nullptr ? message_->c_str() : "";
  170. }
  171. // Deprecated; please use message() instead.
  172. const char* failure_message() const { return message(); }
  173. // Streams a custom failure message into this object.
  174. template <typename T>
  175. AssertionResult& operator<<(const T& value) {
  176. AppendMessage(Message() << value);
  177. return *this;
  178. }
  179. // Allows streaming basic output manipulators such as endl or flush into
  180. // this object.
  181. AssertionResult& operator<<(
  182. ::std::ostream& (*basic_manipulator)(::std::ostream& stream)) {
  183. AppendMessage(Message() << basic_manipulator);
  184. return *this;
  185. }
  186. private:
  187. // Appends the contents of message to message_.
  188. void AppendMessage(const Message& a_message) {
  189. if (message_.get() == nullptr) message_.reset(new ::std::string);
  190. message_->append(a_message.GetString().c_str());
  191. }
  192. // Swap the contents of this AssertionResult with other.
  193. void swap(AssertionResult& other);
  194. // Stores result of the assertion predicate.
  195. bool success_;
  196. // Stores the message describing the condition in case the expectation
  197. // construct is not satisfied with the predicate's outcome.
  198. // Referenced via a pointer to avoid taking too much stack frame space
  199. // with test assertions.
  200. std::unique_ptr< ::std::string> message_;
  201. };
  202. // Makes a successful assertion result.
  203. GTEST_API_ AssertionResult AssertionSuccess();
  204. // Makes a failed assertion result.
  205. GTEST_API_ AssertionResult AssertionFailure();
  206. // Makes a failed assertion result with the given failure message.
  207. // Deprecated; use AssertionFailure() << msg.
  208. GTEST_API_ AssertionResult AssertionFailure(const Message& msg);
  209. } // namespace testing
  210. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
  211. #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_