Xmipp  v3.23.11-Nereus
Functions
gtest-assertion-result.h File Reference
#include <memory>
#include <ostream>
#include <string>
#include <type_traits>
#include "gtest/gtest-message.h"
#include "gtest/internal/gtest-port.h"
Include dependency graph for gtest-assertion-result.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

 GTEST_DISABLE_MSC_WARNINGS_PUSH_ (4251) namespace testing
 

Function Documentation

◆ GTEST_DISABLE_MSC_WARNINGS_PUSH_()

GTEST_DISABLE_MSC_WARNINGS_PUSH_ ( 4251  )

Definition at line 49 of file gtest-assertion-result.h.

52  {
53 
54 // A class for indicating whether an assertion was successful. When
55 // the assertion wasn't successful, the AssertionResult object
56 // remembers a non-empty message that describes how it failed.
57 //
58 // To create an instance of this class, use one of the factory functions
59 // (AssertionSuccess() and AssertionFailure()).
60 //
61 // This class is useful for two purposes:
62 // 1. Defining predicate functions to be used with Boolean test assertions
63 // EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts
64 // 2. Defining predicate-format functions to be
65 // used with predicate assertions (ASSERT_PRED_FORMAT*, etc).
66 //
67 // For example, if you define IsEven predicate:
68 //
69 // testing::AssertionResult IsEven(int n) {
70 // if ((n % 2) == 0)
71 // return testing::AssertionSuccess();
72 // else
73 // return testing::AssertionFailure() << n << " is odd";
74 // }
75 //
76 // Then the failed expectation EXPECT_TRUE(IsEven(Fib(5)))
77 // will print the message
78 //
79 // Value of: IsEven(Fib(5))
80 // Actual: false (5 is odd)
81 // Expected: true
82 //
83 // instead of a more opaque
84 //
85 // Value of: IsEven(Fib(5))
86 // Actual: false
87 // Expected: true
88 //
89 // in case IsEven is a simple Boolean predicate.
90 //
91 // If you expect your predicate to be reused and want to support informative
92 // messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up
93 // about half as often as positive ones in our tests), supply messages for
94 // both success and failure cases:
95 //
96 // testing::AssertionResult IsEven(int n) {
97 // if ((n % 2) == 0)
98 // return testing::AssertionSuccess() << n << " is even";
99 // else
100 // return testing::AssertionFailure() << n << " is odd";
101 // }
102 //
103 // Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print
104 //
105 // Value of: IsEven(Fib(6))
106 // Actual: true (8 is even)
107 // Expected: false
108 //
109 // NB: Predicates that support negative Boolean assertions have reduced
110 // performance in positive ones so be careful not to use them in tests
111 // that have lots (tens of thousands) of positive Boolean assertions.
112 //
113 // To use this class with EXPECT_PRED_FORMAT assertions such as:
114 //
115 // // Verifies that Foo() returns an even number.
116 // EXPECT_PRED_FORMAT1(IsEven, Foo());
117 //
118 // you need to define:
119 //
120 // testing::AssertionResult IsEven(const char* expr, int n) {
121 // if ((n % 2) == 0)
122 // return testing::AssertionSuccess();
123 // else
124 // return testing::AssertionFailure()
125 // << "Expected: " << expr << " is even\n Actual: it's " << n;
126 // }
127 //
128 // If Foo() returns 5, you will see the following message:
129 //
130 // Expected: Foo() is even
131 // Actual: it's 5
132 //
133 class GTEST_API_ AssertionResult {
134  public:
135  // Copy constructor.
136  // Used in EXPECT_TRUE/FALSE(assertion_result).
137  AssertionResult(const AssertionResult& other);
138 
139 // C4800 is a level 3 warning in Visual Studio 2015 and earlier.
140 // This warning is not emitted in Visual Studio 2017.
141 // This warning is off by default starting in Visual Studio 2019 but can be
142 // enabled with command-line options.
143 #if defined(_MSC_VER) && (_MSC_VER < 1910 || _MSC_VER >= 1920)
144  GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 /* forcing value to bool */)
145 #endif
146 
147  // Used in the EXPECT_TRUE/FALSE(bool_expression).
148  //
149  // T must be contextually convertible to bool.
150  //
151  // The second parameter prevents this overload from being considered if
152  // the argument is implicitly convertible to AssertionResult. In that case
153  // we want AssertionResult's copy constructor to be used.
154  template <typename T>
155  explicit AssertionResult(
156  const T& success,
157  typename std::enable_if<
158  !std::is_convertible<T, AssertionResult>::value>::type*
159  /*enabler*/
160  = nullptr)
161  : success_(success) {}
162 
163 #if defined(_MSC_VER) && (_MSC_VER < 1910 || _MSC_VER >= 1920)
165 #endif
166 
167  // Assignment operator.
168  AssertionResult& operator=(AssertionResult other) {
169  swap(other);
170  return *this;
171  }
172 
173  // Returns true if and only if the assertion succeeded.
174  operator bool() const { return success_; } // NOLINT
175 
176  // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE.
177  AssertionResult operator!() const;
178 
179  // Returns the text streamed into this AssertionResult. Test assertions
180  // use it when they fail (i.e., the predicate's outcome doesn't match the
181  // assertion's expectation). When nothing has been streamed into the
182  // object, returns an empty string.
183  const char* message() const {
184  return message_.get() != nullptr ? message_->c_str() : "";
185  }
186  // Deprecated; please use message() instead.
187  const char* failure_message() const { return message(); }
188 
189  // Streams a custom failure message into this object.
190  template <typename T>
191  AssertionResult& operator<<(const T& value) {
192  AppendMessage(Message() << value);
193  return *this;
194  }
195 
196  // Allows streaming basic output manipulators such as endl or flush into
197  // this object.
198  AssertionResult& operator<<(
199  ::std::ostream& (*basic_manipulator)(::std::ostream& stream)) {
200  AppendMessage(Message() << basic_manipulator);
201  return *this;
202  }
203 
204  private:
205  // Appends the contents of message to message_.
206  void AppendMessage(const Message& a_message) {
207  if (message_.get() == nullptr) message_.reset(new ::std::string);
208  message_->append(a_message.GetString().c_str());
209  }
210 
211  // Swap the contents of this AssertionResult with other.
212  void swap(AssertionResult& other);
213 
214  // Stores result of the assertion predicate.
215  bool success_;
216  // Stores the message describing the condition in case the expectation
217  // construct is not satisfied with the predicate's outcome.
218  // Referenced via a pointer to avoid taking too much stack frame space
219  // with test assertions.
220  std::unique_ptr< ::std::string> message_;
221 };
222 
223 // Makes a successful assertion result.
224 GTEST_API_ AssertionResult AssertionSuccess();
225 
226 // Makes a failed assertion result.
227 GTEST_API_ AssertionResult AssertionFailure();
228 
229 // Makes a failed assertion result with the given failure message.
230 // Deprecated; use AssertionFailure() << msg.
231 GTEST_API_ AssertionResult AssertionFailure(const Message& msg);
232 
233 } // namespace testing
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
Definition: gtest-port.h:344
#define GTEST_API_
Definition: gtest-port.h:793
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251) namespace testing
std::ostream & operator<<(std::ostream &os, const Message &sb)
viol type