Ocean
Loading...
Searching...
No Matches
TestResult.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_TEST_TEST_RESULT_H
9#define META_OCEAN_TEST_TEST_RESULT_H
10
11#include "ocean/test/Test.h"
12
14
15namespace Ocean
16{
17
18namespace Test
19{
20
21/**
22 * This class implements a simple test result accumulator.
23 * The test result starts as succeeded (true) and can only be set to failed (false), never back to succeeded.
24 * This is useful for accumulating multiple test results where any failure marks the entire test as failed.
25 * Additionally, at least one test must be executed for the result to succeed.
26 *
27 * Usage example:
28 * @code
29 * bool testFunction()
30 * {
31 * TestResult testResult("My test");
32 *
33 * testResult = testSubFunction1(); // If false, testResult becomes false
34 * testResult = testSubFunction2(); // If false, testResult becomes false
35 * testResult = testSubFunction3(); // If false, testResult becomes false
36 *
37 * Log::info() << testResult; // Outputs "My test: succeeded." or "My test: FAILED!"
38 *
39 * return testResult.succeeded(); // Must be called before destruction
40 * }
41 * @endcode
42 *
43 * @see Validation
44 * @ingroup test
45 */
47{
48 public:
49
50 /**
51 * Default constructor.
52 */
53 TestResult() = default;
54
55 /**
56 * Constructor with test name.
57 * @param testName The name of the test, used for logging output
58 */
59 inline explicit TestResult(const std::string& testName);
60
61 /**
62 * Destructs this test result object.
63 */
64 inline ~TestResult();
65
66 /**
67 * Assignment operator for bool values.
68 * Only accepts false values; true values are ignored to prevent accidental resetting of a failed state.
69 * Marks that a test was executed.
70 * @param value The boolean value to assign; only false will change the state
71 * @return Reference to this object
72 */
73 inline TestResult& operator=(bool value);
74
75 /**
76 * Returns whether this test result has succeeded.
77 * This function must be called before the object is destroyed (checked in debug mode).
78 * @return True if all assigned values were true and at least one test was executed; false otherwise
79 */
80 [[nodiscard]] inline bool succeeded() const;
81
82 /**
83 * Returns whether this test result has succeeded so far without marking it as checked.
84 * @return True if all assigned values were true and at least one test was executed; false otherwise
85 */
86 [[nodiscard]] inline bool succeededSoFar() const;
87
88 /**
89 * Returns whether any test was executed.
90 * @return True if at least one test result was assigned; false otherwise
91 */
92 [[nodiscard]] inline bool anyTestExecuted() const;
93
94 /**
95 * Returns the name of the test.
96 * @return The test name, or empty string if no name was provided
97 */
98 [[nodiscard]] inline const std::string& testName() const;
99
100 protected:
101
102 /**
103 * Disabled copy constructor.
104 */
105 TestResult(const TestResult&) = delete;
106
107 /**
108 * Disabled move constructor.
109 */
111
112 /**
113 * Disabled copy assignment operator.
114 */
115 TestResult& operator=(const TestResult&) = delete;
116
117 /**
118 * Disabled move assignment operator.
119 */
121
122 protected:
123
124 /// The name of the test.
125 std::string testName_;
126
127 /// True if the test has succeeded; false if any test has failed.
128 bool succeeded_ = true;
129
130 /// True if at least one test was executed.
131 bool anyTestExecuted_ = false;
132
133#ifdef OCEAN_DEBUG
134 /// True if the success state of this result has been checked via succeeded().
135 mutable bool succeededChecked_ = false;
136#endif // OCEAN_DEBUG
137};
138
140{
141#ifdef OCEAN_DEBUG
142 ocean_assert(succeededChecked_ && "The test result has not been checked via succeeded()");
143#endif
144}
145
146inline TestResult::TestResult(const std::string& testName) :
147 testName_(testName)
148{
149 ocean_assert(!testName.empty());
150
151 if (!testName_.empty())
152 {
153 Log::info() << "--- " << testName_ << ": ---";
154 }
155}
156
158{
159 anyTestExecuted_ = true;
160
161 // Only accept false values; once failed, always failed
162 if (!value)
163 {
164 succeeded_ = false;
165 }
166
167 return *this;
168}
169
170inline bool TestResult::succeeded() const
171{
172#ifdef OCEAN_DEBUG
173 succeededChecked_ = true;
174#endif
175
177}
178
179inline bool TestResult::succeededSoFar() const
180{
182}
183
185{
186 return anyTestExecuted_;
187}
188
189inline const std::string& TestResult::testName() const
190{
191 return testName_;
192}
193
194/**
195 * Writes a test result to a stream.
196 * @param stream The stream to write to
197 * @param testResult The test result to write
198 * @return Reference to the stream
199 */
200inline std::ostream& operator<<(std::ostream& stream, const TestResult& testResult)
201{
202 if (!testResult.testName().empty())
203 {
204 stream << testResult.testName() << ": ";
205 }
206
207 if (testResult.succeededSoFar())
208 {
209 stream << "succeeded.";
210 }
211 else if (testResult.anyTestExecuted())
212 {
213 stream << "FAILED!";
214 }
215 else
216 {
217 stream << "No test was executed!";
218 }
219
220 return stream;
221}
222
223/**
224 * Writes a test result to a message object.
225 * @param messageObject The message object to write to
226 * @param testResult The test result to write
227 * @return Reference to the message object
228 * @tparam tActive True to write the message; false to suppress it
229 */
230template <bool tActive>
231MessageObject<tActive>& operator<<(MessageObject<tActive>& messageObject, const TestResult& testResult)
232{
233 if (!testResult.testName().empty())
234 {
235 messageObject << testResult.testName() << ": ";
236 }
237
238 if (testResult.succeededSoFar())
239 {
240 messageObject << "succeeded.";
241 }
242 else if (testResult.anyTestExecuted())
243 {
244 messageObject << "FAILED!";
245 }
246 else
247 {
248 messageObject << "No test was executed!";
249 }
250
251 return messageObject;
252}
253
254/**
255 * Writes a test result to a message object (rvalue reference version).
256 * @param messageObject The message object to write to
257 * @param testResult The test result to write
258 * @return Reference to the message object
259 * @tparam tActive True to write the message; false to suppress it
260 */
261template <bool tActive>
262MessageObject<tActive>& operator<<(MessageObject<tActive>&& messageObject, const TestResult& testResult)
263{
264 if (!testResult.testName().empty())
265 {
266 messageObject << testResult.testName() << ": ";
267 }
268
269 if (testResult.succeededSoFar())
270 {
271 messageObject << "succeeded.";
272 }
273 else if (testResult.anyTestExecuted())
274 {
275 messageObject << "FAILED!";
276 }
277 else
278 {
279 messageObject << "No test was executed!";
280 }
281
282 return messageObject;
283}
284
285}
286
287}
288
289#endif // META_OCEAN_TEST_TEST_RESULT_H
static MessageObject info()
Returns the message for information messages.
Definition Messenger.h:1085
Messenger object, one object for each message.
Definition Messenger.h:448
This class implements a simple test result accumulator.
Definition TestResult.h:47
TestResult()=default
Default constructor.
TestResult(TestResult &&)=delete
Disabled move constructor.
~TestResult()
Destructs this test result object.
Definition TestResult.h:139
bool succeeded() const
Returns whether this test result has succeeded.
Definition TestResult.h:170
bool anyTestExecuted_
True if at least one test was executed.
Definition TestResult.h:131
bool anyTestExecuted() const
Returns whether any test was executed.
Definition TestResult.h:184
bool succeededChecked_
True if the success state of this result has been checked via succeeded().
Definition TestResult.h:135
TestResult & operator=(TestResult &&)=delete
Disabled move assignment operator.
bool succeededSoFar() const
Returns whether this test result has succeeded so far without marking it as checked.
Definition TestResult.h:179
TestResult & operator=(bool value)
Assignment operator for bool values.
Definition TestResult.h:157
TestResult(const TestResult &)=delete
Disabled copy constructor.
TestResult & operator=(const TestResult &)=delete
Disabled copy assignment operator.
std::string testName_
The name of the test.
Definition TestResult.h:125
const std::string & testName() const
Returns the name of the test.
Definition TestResult.h:189
bool succeeded_
True if the test has succeeded; false if any test has failed.
Definition TestResult.h:128
std::ostream & operator<<(std::ostream &stream, const TestResult &testResult)
Writes a test result to a stream.
Definition TestResult.h:200
The namespace covering the entire Ocean framework.
Definition Accessor.h:15