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 | |
30 | // |
31 | // The Google C++ Testing and Mocking Framework (Google Test) |
32 | // |
33 | // This header file defines the public API for Google Test. It should be |
34 | // included by any test program that uses Google Test. |
35 | // |
36 | // IMPORTANT NOTE: Due to limitation of the C++ language, we have to |
37 | // leave some internal implementation details in this header file. |
38 | // They are clearly marked by comments like this: |
39 | // |
40 | // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
41 | // |
42 | // Such code is NOT meant to be used by a user directly, and is subject |
43 | // to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user |
44 | // program! |
45 | // |
46 | // Acknowledgment: Google Test borrowed the idea of automatic test |
47 | // registration from Barthelemy Dagenais' (barthelemy@prologique.com) |
48 | // easyUnit framework. |
49 | |
50 | // GOOGLETEST_CM0001 DO NOT DELETE |
51 | |
52 | #ifndef GTEST_INCLUDE_GTEST_GTEST_H_ |
53 | #define GTEST_INCLUDE_GTEST_GTEST_H_ |
54 | |
55 | #include <cstddef> |
56 | #include <limits> |
57 | #include <memory> |
58 | #include <ostream> |
59 | #include <type_traits> |
60 | #include <vector> |
61 | |
62 | #include "gtest/internal/gtest-internal.h" |
63 | #include "gtest/internal/gtest-string.h" |
64 | #include "gtest/gtest-death-test.h" |
65 | #include "gtest/gtest-matchers.h" |
66 | #include "gtest/gtest-message.h" |
67 | #include "gtest/gtest-param-test.h" |
68 | #include "gtest/gtest-printers.h" |
69 | #include "gtest/gtest_prod.h" |
70 | #include "gtest/gtest-test-part.h" |
71 | #include "gtest/gtest-typed-test.h" |
72 | |
73 | GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ |
74 | /* class A needs to have dll-interface to be used by clients of class B */) |
75 | |
76 | namespace testing { |
77 | |
78 | // Silence C4100 (unreferenced formal parameter) and 4805 |
79 | // unsafe mix of type 'const int' and type 'const bool' |
80 | #ifdef _MSC_VER |
81 | # pragma warning(push) |
82 | # pragma warning(disable:4805) |
83 | # pragma warning(disable:4100) |
84 | #endif |
85 | |
86 | |
87 | // Declares the flags. |
88 | |
89 | // This flag temporary enables the disabled tests. |
90 | GTEST_DECLARE_bool_(also_run_disabled_tests); |
91 | |
92 | // This flag brings the debugger on an assertion failure. |
93 | GTEST_DECLARE_bool_(break_on_failure); |
94 | |
95 | // This flag controls whether Google Test catches all test-thrown exceptions |
96 | // and logs them as failures. |
97 | GTEST_DECLARE_bool_(catch_exceptions); |
98 | |
99 | // This flag enables using colors in terminal output. Available values are |
100 | // "yes" to enable colors, "no" (disable colors), or "auto" (the default) |
101 | // to let Google Test decide. |
102 | GTEST_DECLARE_string_(color); |
103 | |
104 | // This flag sets up the filter to select by name using a glob pattern |
105 | // the tests to run. If the filter is not given all tests are executed. |
106 | GTEST_DECLARE_string_(filter); |
107 | |
108 | // This flag controls whether Google Test installs a signal handler that dumps |
109 | // debugging information when fatal signals are raised. |
110 | GTEST_DECLARE_bool_(install_failure_signal_handler); |
111 | |
112 | // This flag causes the Google Test to list tests. None of the tests listed |
113 | // are actually run if the flag is provided. |
114 | GTEST_DECLARE_bool_(list_tests); |
115 | |
116 | // This flag controls whether Google Test emits a detailed XML report to a file |
117 | // in addition to its normal textual output. |
118 | GTEST_DECLARE_string_(output); |
119 | |
120 | // This flags control whether Google Test prints the elapsed time for each |
121 | // test. |
122 | GTEST_DECLARE_bool_(print_time); |
123 | |
124 | // This flags control whether Google Test prints UTF8 characters as text. |
125 | GTEST_DECLARE_bool_(print_utf8); |
126 | |
127 | // This flag specifies the random number seed. |
128 | GTEST_DECLARE_int32_(random_seed); |
129 | |
130 | // This flag sets how many times the tests are repeated. The default value |
131 | // is 1. If the value is -1 the tests are repeating forever. |
132 | GTEST_DECLARE_int32_(repeat); |
133 | |
134 | // This flag controls whether Google Test includes Google Test internal |
135 | // stack frames in failure stack traces. |
136 | GTEST_DECLARE_bool_(show_internal_stack_frames); |
137 | |
138 | // When this flag is specified, tests' order is randomized on every iteration. |
139 | GTEST_DECLARE_bool_(shuffle); |
140 | |
141 | // This flag specifies the maximum number of stack frames to be |
142 | // printed in a failure message. |
143 | GTEST_DECLARE_int32_(stack_trace_depth); |
144 | |
145 | // When this flag is specified, a failed assertion will throw an |
146 | // exception if exceptions are enabled, or exit the program with a |
147 | // non-zero code otherwise. For use with an external test framework. |
148 | GTEST_DECLARE_bool_(throw_on_failure); |
149 | |
150 | // When this flag is set with a "host:port" string, on supported |
151 | // platforms test results are streamed to the specified port on |
152 | // the specified host machine. |
153 | GTEST_DECLARE_string_(stream_result_to); |
154 | |
155 | #if GTEST_USE_OWN_FLAGFILE_FLAG_ |
156 | GTEST_DECLARE_string_(flagfile); |
157 | #endif // GTEST_USE_OWN_FLAGFILE_FLAG_ |
158 | |
159 | // The upper limit for valid stack trace depths. |
160 | const int kMaxStackTraceDepth = 100; |
161 | |
162 | namespace internal { |
163 | |
164 | class AssertHelper; |
165 | class DefaultGlobalTestPartResultReporter; |
166 | class ExecDeathTest; |
167 | class NoExecDeathTest; |
168 | class FinalSuccessChecker; |
169 | class GTestFlagSaver; |
170 | class StreamingListenerTest; |
171 | class TestResultAccessor; |
172 | class TestEventListenersAccessor; |
173 | class TestEventRepeater; |
174 | class UnitTestRecordPropertyTestHelper; |
175 | class WindowsDeathTest; |
176 | class FuchsiaDeathTest; |
177 | class UnitTestImpl* GetUnitTestImpl(); |
178 | void ReportFailureInUnknownLocation(TestPartResult::Type result_type, |
179 | const std::string& message); |
180 | |
181 | } // namespace internal |
182 | |
183 | // The friend relationship of some of these classes is cyclic. |
184 | // If we don't forward declare them the compiler might confuse the classes |
185 | // in friendship clauses with same named classes on the scope. |
186 | class Test; |
187 | class TestSuite; |
188 | |
189 | // Old API is still available but deprecated |
190 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
191 | using TestCase = TestSuite; |
192 | #endif |
193 | class TestInfo; |
194 | class UnitTest; |
195 | |
196 | // A class for indicating whether an assertion was successful. When |
197 | // the assertion wasn't successful, the AssertionResult object |
198 | // remembers a non-empty message that describes how it failed. |
199 | // |
200 | // To create an instance of this class, use one of the factory functions |
201 | // (AssertionSuccess() and AssertionFailure()). |
202 | // |
203 | // This class is useful for two purposes: |
204 | // 1. Defining predicate functions to be used with Boolean test assertions |
205 | // EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts |
206 | // 2. Defining predicate-format functions to be |
207 | // used with predicate assertions (ASSERT_PRED_FORMAT*, etc). |
208 | // |
209 | // For example, if you define IsEven predicate: |
210 | // |
211 | // testing::AssertionResult IsEven(int n) { |
212 | // if ((n % 2) == 0) |
213 | // return testing::AssertionSuccess(); |
214 | // else |
215 | // return testing::AssertionFailure() << n << " is odd"; |
216 | // } |
217 | // |
218 | // Then the failed expectation EXPECT_TRUE(IsEven(Fib(5))) |
219 | // will print the message |
220 | // |
221 | // Value of: IsEven(Fib(5)) |
222 | // Actual: false (5 is odd) |
223 | // Expected: true |
224 | // |
225 | // instead of a more opaque |
226 | // |
227 | // Value of: IsEven(Fib(5)) |
228 | // Actual: false |
229 | // Expected: true |
230 | // |
231 | // in case IsEven is a simple Boolean predicate. |
232 | // |
233 | // If you expect your predicate to be reused and want to support informative |
234 | // messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up |
235 | // about half as often as positive ones in our tests), supply messages for |
236 | // both success and failure cases: |
237 | // |
238 | // testing::AssertionResult IsEven(int n) { |
239 | // if ((n % 2) == 0) |
240 | // return testing::AssertionSuccess() << n << " is even"; |
241 | // else |
242 | // return testing::AssertionFailure() << n << " is odd"; |
243 | // } |
244 | // |
245 | // Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print |
246 | // |
247 | // Value of: IsEven(Fib(6)) |
248 | // Actual: true (8 is even) |
249 | // Expected: false |
250 | // |
251 | // NB: Predicates that support negative Boolean assertions have reduced |
252 | // performance in positive ones so be careful not to use them in tests |
253 | // that have lots (tens of thousands) of positive Boolean assertions. |
254 | // |
255 | // To use this class with EXPECT_PRED_FORMAT assertions such as: |
256 | // |
257 | // // Verifies that Foo() returns an even number. |
258 | // EXPECT_PRED_FORMAT1(IsEven, Foo()); |
259 | // |
260 | // you need to define: |
261 | // |
262 | // testing::AssertionResult IsEven(const char* expr, int n) { |
263 | // if ((n % 2) == 0) |
264 | // return testing::AssertionSuccess(); |
265 | // else |
266 | // return testing::AssertionFailure() |
267 | // << "Expected: " << expr << " is even\n Actual: it's " << n; |
268 | // } |
269 | // |
270 | // If Foo() returns 5, you will see the following message: |
271 | // |
272 | // Expected: Foo() is even |
273 | // Actual: it's 5 |
274 | // |
275 | class GTEST_API_ AssertionResult { |
276 | public: |
277 | // Copy constructor. |
278 | // Used in EXPECT_TRUE/FALSE(assertion_result). |
279 | AssertionResult(const AssertionResult& other); |
280 | |
281 | #if defined(_MSC_VER) && _MSC_VER < 1910 |
282 | GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 /* forcing value to bool */) |
283 | #endif |
284 | |
285 | // Used in the EXPECT_TRUE/FALSE(bool_expression). |
286 | // |
287 | // T must be contextually convertible to bool. |
288 | // |
289 | // The second parameter prevents this overload from being considered if |
290 | // the argument is implicitly convertible to AssertionResult. In that case |
291 | // we want AssertionResult's copy constructor to be used. |
292 | template <typename T> |
293 | explicit AssertionResult( |
294 | const T& success, |
295 | typename internal::EnableIf< |
296 | !std::is_convertible<T, AssertionResult>::value>::type* |
297 | /*enabler*/ |
298 | = nullptr) |
299 | : success_(success) {} |
300 | |
301 | #if defined(_MSC_VER) && _MSC_VER < 1910 |
302 | GTEST_DISABLE_MSC_WARNINGS_POP_() |
303 | #endif |
304 | |
305 | // Assignment operator. |
306 | AssertionResult& operator=(AssertionResult other) { |
307 | swap(other); |
308 | return *this; |
309 | } |
310 | |
311 | // Returns true iff the assertion succeeded. |
312 | operator bool() const { return success_; } // NOLINT |
313 | |
314 | // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE. |
315 | AssertionResult operator!() const; |
316 | |
317 | // Returns the text streamed into this AssertionResult. Test assertions |
318 | // use it when they fail (i.e., the predicate's outcome doesn't match the |
319 | // assertion's expectation). When nothing has been streamed into the |
320 | // object, returns an empty string. |
321 | const char* message() const { |
322 | return message_.get() != nullptr ? message_->c_str() : "" ; |
323 | } |
324 | // Deprecated; please use message() instead. |
325 | const char* failure_message() const { return message(); } |
326 | |
327 | // Streams a custom failure message into this object. |
328 | template <typename T> AssertionResult& operator<<(const T& value) { |
329 | AppendMessage(Message() << value); |
330 | return *this; |
331 | } |
332 | |
333 | // Allows streaming basic output manipulators such as endl or flush into |
334 | // this object. |
335 | AssertionResult& operator<<( |
336 | ::std::ostream& (*basic_manipulator)(::std::ostream& stream)) { |
337 | AppendMessage(Message() << basic_manipulator); |
338 | return *this; |
339 | } |
340 | |
341 | private: |
342 | // Appends the contents of message to message_. |
343 | void AppendMessage(const Message& a_message) { |
344 | if (message_.get() == nullptr) message_.reset(new ::std::string); |
345 | message_->append(a_message.GetString().c_str()); |
346 | } |
347 | |
348 | // Swap the contents of this AssertionResult with other. |
349 | void swap(AssertionResult& other); |
350 | |
351 | // Stores result of the assertion predicate. |
352 | bool success_; |
353 | // Stores the message describing the condition in case the expectation |
354 | // construct is not satisfied with the predicate's outcome. |
355 | // Referenced via a pointer to avoid taking too much stack frame space |
356 | // with test assertions. |
357 | std::unique_ptr< ::std::string> message_; |
358 | }; |
359 | |
360 | // Makes a successful assertion result. |
361 | GTEST_API_ AssertionResult AssertionSuccess(); |
362 | |
363 | // Makes a failed assertion result. |
364 | GTEST_API_ AssertionResult AssertionFailure(); |
365 | |
366 | // Makes a failed assertion result with the given failure message. |
367 | // Deprecated; use AssertionFailure() << msg. |
368 | GTEST_API_ AssertionResult AssertionFailure(const Message& msg); |
369 | |
370 | } // namespace testing |
371 | |
372 | // Includes the auto-generated header that implements a family of generic |
373 | // predicate assertion macros. This include comes late because it relies on |
374 | // APIs declared above. |
375 | #include "gtest/gtest_pred_impl.h" |
376 | |
377 | namespace testing { |
378 | |
379 | // The abstract class that all tests inherit from. |
380 | // |
381 | // In Google Test, a unit test program contains one or many TestSuites, and |
382 | // each TestSuite contains one or many Tests. |
383 | // |
384 | // When you define a test using the TEST macro, you don't need to |
385 | // explicitly derive from Test - the TEST macro automatically does |
386 | // this for you. |
387 | // |
388 | // The only time you derive from Test is when defining a test fixture |
389 | // to be used in a TEST_F. For example: |
390 | // |
391 | // class FooTest : public testing::Test { |
392 | // protected: |
393 | // void SetUp() override { ... } |
394 | // void TearDown() override { ... } |
395 | // ... |
396 | // }; |
397 | // |
398 | // TEST_F(FooTest, Bar) { ... } |
399 | // TEST_F(FooTest, Baz) { ... } |
400 | // |
401 | // Test is not copyable. |
402 | class GTEST_API_ Test { |
403 | public: |
404 | friend class TestInfo; |
405 | |
406 | // The d'tor is virtual as we intend to inherit from Test. |
407 | virtual ~Test(); |
408 | |
409 | // Sets up the stuff shared by all tests in this test case. |
410 | // |
411 | // Google Test will call Foo::SetUpTestSuite() before running the first |
412 | // test in test case Foo. Hence a sub-class can define its own |
413 | // SetUpTestSuite() method to shadow the one defined in the super |
414 | // class. |
415 | static void SetUpTestSuite() {} |
416 | |
417 | // Tears down the stuff shared by all tests in this test suite. |
418 | // |
419 | // Google Test will call Foo::TearDownTestSuite() after running the last |
420 | // test in test case Foo. Hence a sub-class can define its own |
421 | // TearDownTestSuite() method to shadow the one defined in the super |
422 | // class. |
423 | static void TearDownTestSuite() {} |
424 | |
425 | // Legacy API is deprecated but still available |
426 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
427 | static void TearDownTestCase() {} |
428 | static void SetUpTestCase() {} |
429 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
430 | |
431 | // Returns true iff the current test has a fatal failure. |
432 | static bool HasFatalFailure(); |
433 | |
434 | // Returns true iff the current test has a non-fatal failure. |
435 | static bool HasNonfatalFailure(); |
436 | |
437 | // Returns true iff the current test was skipped. |
438 | static bool IsSkipped(); |
439 | |
440 | // Returns true iff the current test has a (either fatal or |
441 | // non-fatal) failure. |
442 | static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); } |
443 | |
444 | // Logs a property for the current test, test suite, or for the entire |
445 | // invocation of the test program when used outside of the context of a |
446 | // test suite. Only the last value for a given key is remembered. These |
447 | // are public static so they can be called from utility functions that are |
448 | // not members of the test fixture. Calls to RecordProperty made during |
449 | // lifespan of the test (from the moment its constructor starts to the |
450 | // moment its destructor finishes) will be output in XML as attributes of |
451 | // the <testcase> element. Properties recorded from fixture's |
452 | // SetUpTestSuite or TearDownTestSuite are logged as attributes of the |
453 | // corresponding <testsuite> element. Calls to RecordProperty made in the |
454 | // global context (before or after invocation of RUN_ALL_TESTS and from |
455 | // SetUp/TearDown method of Environment objects registered with Google |
456 | // Test) will be output as attributes of the <testsuites> element. |
457 | static void RecordProperty(const std::string& key, const std::string& value); |
458 | static void RecordProperty(const std::string& key, int value); |
459 | |
460 | protected: |
461 | // Creates a Test object. |
462 | Test(); |
463 | |
464 | // Sets up the test fixture. |
465 | virtual void SetUp(); |
466 | |
467 | // Tears down the test fixture. |
468 | virtual void TearDown(); |
469 | |
470 | private: |
471 | // Returns true iff the current test has the same fixture class as |
472 | // the first test in the current test suite. |
473 | static bool HasSameFixtureClass(); |
474 | |
475 | // Runs the test after the test fixture has been set up. |
476 | // |
477 | // A sub-class must implement this to define the test logic. |
478 | // |
479 | // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM. |
480 | // Instead, use the TEST or TEST_F macro. |
481 | virtual void TestBody() = 0; |
482 | |
483 | // Sets up, executes, and tears down the test. |
484 | void Run(); |
485 | |
486 | // Deletes self. We deliberately pick an unusual name for this |
487 | // internal method to avoid clashing with names used in user TESTs. |
488 | void DeleteSelf_() { delete this; } |
489 | |
490 | const std::unique_ptr<GTEST_FLAG_SAVER_> gtest_flag_saver_; |
491 | |
492 | // Often a user misspells SetUp() as Setup() and spends a long time |
493 | // wondering why it is never called by Google Test. The declaration of |
494 | // the following method is solely for catching such an error at |
495 | // compile time: |
496 | // |
497 | // - The return type is deliberately chosen to be not void, so it |
498 | // will be a conflict if void Setup() is declared in the user's |
499 | // test fixture. |
500 | // |
501 | // - This method is private, so it will be another compiler error |
502 | // if the method is called from the user's test fixture. |
503 | // |
504 | // DO NOT OVERRIDE THIS FUNCTION. |
505 | // |
506 | // If you see an error about overriding the following function or |
507 | // about it being private, you have mis-spelled SetUp() as Setup(). |
508 | struct Setup_should_be_spelled_SetUp {}; |
509 | virtual Setup_should_be_spelled_SetUp* Setup() { return nullptr; } |
510 | |
511 | // We disallow copying Tests. |
512 | GTEST_DISALLOW_COPY_AND_ASSIGN_(Test); |
513 | }; |
514 | |
515 | typedef internal::TimeInMillis TimeInMillis; |
516 | |
517 | // A copyable object representing a user specified test property which can be |
518 | // output as a key/value string pair. |
519 | // |
520 | // Don't inherit from TestProperty as its destructor is not virtual. |
521 | class TestProperty { |
522 | public: |
523 | // C'tor. TestProperty does NOT have a default constructor. |
524 | // Always use this constructor (with parameters) to create a |
525 | // TestProperty object. |
526 | TestProperty(const std::string& a_key, const std::string& a_value) : |
527 | key_(a_key), value_(a_value) { |
528 | } |
529 | |
530 | // Gets the user supplied key. |
531 | const char* key() const { |
532 | return key_.c_str(); |
533 | } |
534 | |
535 | // Gets the user supplied value. |
536 | const char* value() const { |
537 | return value_.c_str(); |
538 | } |
539 | |
540 | // Sets a new value, overriding the one supplied in the constructor. |
541 | void SetValue(const std::string& new_value) { |
542 | value_ = new_value; |
543 | } |
544 | |
545 | private: |
546 | // The key supplied by the user. |
547 | std::string key_; |
548 | // The value supplied by the user. |
549 | std::string value_; |
550 | }; |
551 | |
552 | // The result of a single Test. This includes a list of |
553 | // TestPartResults, a list of TestProperties, a count of how many |
554 | // death tests there are in the Test, and how much time it took to run |
555 | // the Test. |
556 | // |
557 | // TestResult is not copyable. |
558 | class GTEST_API_ TestResult { |
559 | public: |
560 | // Creates an empty TestResult. |
561 | TestResult(); |
562 | |
563 | // D'tor. Do not inherit from TestResult. |
564 | ~TestResult(); |
565 | |
566 | // Gets the number of all test parts. This is the sum of the number |
567 | // of successful test parts and the number of failed test parts. |
568 | int total_part_count() const; |
569 | |
570 | // Returns the number of the test properties. |
571 | int test_property_count() const; |
572 | |
573 | // Returns true iff the test passed (i.e. no test part failed). |
574 | bool Passed() const { return !Skipped() && !Failed(); } |
575 | |
576 | // Returns true iff the test was skipped. |
577 | bool Skipped() const; |
578 | |
579 | // Returns true iff the test failed. |
580 | bool Failed() const; |
581 | |
582 | // Returns true iff the test fatally failed. |
583 | bool HasFatalFailure() const; |
584 | |
585 | // Returns true iff the test has a non-fatal failure. |
586 | bool HasNonfatalFailure() const; |
587 | |
588 | // Returns the elapsed time, in milliseconds. |
589 | TimeInMillis elapsed_time() const { return elapsed_time_; } |
590 | |
591 | // Returns the i-th test part result among all the results. i can range from 0 |
592 | // to total_part_count() - 1. If i is not in that range, aborts the program. |
593 | const TestPartResult& GetTestPartResult(int i) const; |
594 | |
595 | // Returns the i-th test property. i can range from 0 to |
596 | // test_property_count() - 1. If i is not in that range, aborts the |
597 | // program. |
598 | const TestProperty& GetTestProperty(int i) const; |
599 | |
600 | private: |
601 | friend class TestInfo; |
602 | friend class TestSuite; |
603 | friend class UnitTest; |
604 | friend class internal::DefaultGlobalTestPartResultReporter; |
605 | friend class internal::ExecDeathTest; |
606 | friend class internal::TestResultAccessor; |
607 | friend class internal::UnitTestImpl; |
608 | friend class internal::WindowsDeathTest; |
609 | friend class internal::FuchsiaDeathTest; |
610 | |
611 | // Gets the vector of TestPartResults. |
612 | const std::vector<TestPartResult>& test_part_results() const { |
613 | return test_part_results_; |
614 | } |
615 | |
616 | // Gets the vector of TestProperties. |
617 | const std::vector<TestProperty>& test_properties() const { |
618 | return test_properties_; |
619 | } |
620 | |
621 | // Sets the elapsed time. |
622 | void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; } |
623 | |
624 | // Adds a test property to the list. The property is validated and may add |
625 | // a non-fatal failure if invalid (e.g., if it conflicts with reserved |
626 | // key names). If a property is already recorded for the same key, the |
627 | // value will be updated, rather than storing multiple values for the same |
628 | // key. xml_element specifies the element for which the property is being |
629 | // recorded and is used for validation. |
630 | void RecordProperty(const std::string& xml_element, |
631 | const TestProperty& test_property); |
632 | |
633 | // Adds a failure if the key is a reserved attribute of Google Test |
634 | // testsuite tags. Returns true if the property is valid. |
635 | // FIXME: Validate attribute names are legal and human readable. |
636 | static bool ValidateTestProperty(const std::string& xml_element, |
637 | const TestProperty& test_property); |
638 | |
639 | // Adds a test part result to the list. |
640 | void AddTestPartResult(const TestPartResult& test_part_result); |
641 | |
642 | // Returns the death test count. |
643 | int death_test_count() const { return death_test_count_; } |
644 | |
645 | // Increments the death test count, returning the new count. |
646 | int increment_death_test_count() { return ++death_test_count_; } |
647 | |
648 | // Clears the test part results. |
649 | void ClearTestPartResults(); |
650 | |
651 | // Clears the object. |
652 | void Clear(); |
653 | |
654 | // Protects mutable state of the property vector and of owned |
655 | // properties, whose values may be updated. |
656 | internal::Mutex test_properites_mutex_; |
657 | |
658 | // The vector of TestPartResults |
659 | std::vector<TestPartResult> test_part_results_; |
660 | // The vector of TestProperties |
661 | std::vector<TestProperty> test_properties_; |
662 | // Running count of death tests. |
663 | int death_test_count_; |
664 | // The elapsed time, in milliseconds. |
665 | TimeInMillis elapsed_time_; |
666 | |
667 | // We disallow copying TestResult. |
668 | GTEST_DISALLOW_COPY_AND_ASSIGN_(TestResult); |
669 | }; // class TestResult |
670 | |
671 | // A TestInfo object stores the following information about a test: |
672 | // |
673 | // Test suite name |
674 | // Test name |
675 | // Whether the test should be run |
676 | // A function pointer that creates the test object when invoked |
677 | // Test result |
678 | // |
679 | // The constructor of TestInfo registers itself with the UnitTest |
680 | // singleton such that the RUN_ALL_TESTS() macro knows which tests to |
681 | // run. |
682 | class GTEST_API_ TestInfo { |
683 | public: |
684 | // Destructs a TestInfo object. This function is not virtual, so |
685 | // don't inherit from TestInfo. |
686 | ~TestInfo(); |
687 | |
688 | // Returns the test suite name. |
689 | const char* test_suite_name() const { return test_suite_name_.c_str(); } |
690 | |
691 | // Legacy API is deprecated but still available |
692 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
693 | const char* test_case_name() const { return test_suite_name(); } |
694 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
695 | |
696 | // Returns the test name. |
697 | const char* name() const { return name_.c_str(); } |
698 | |
699 | // Returns the name of the parameter type, or NULL if this is not a typed |
700 | // or a type-parameterized test. |
701 | const char* type_param() const { |
702 | if (type_param_.get() != nullptr) return type_param_->c_str(); |
703 | return nullptr; |
704 | } |
705 | |
706 | // Returns the text representation of the value parameter, or NULL if this |
707 | // is not a value-parameterized test. |
708 | const char* value_param() const { |
709 | if (value_param_.get() != nullptr) return value_param_->c_str(); |
710 | return nullptr; |
711 | } |
712 | |
713 | // Returns the file name where this test is defined. |
714 | const char* file() const { return location_.file.c_str(); } |
715 | |
716 | // Returns the line where this test is defined. |
717 | int line() const { return location_.line; } |
718 | |
719 | // Return true if this test should not be run because it's in another shard. |
720 | bool is_in_another_shard() const { return is_in_another_shard_; } |
721 | |
722 | // Returns true if this test should run, that is if the test is not |
723 | // disabled (or it is disabled but the also_run_disabled_tests flag has |
724 | // been specified) and its full name matches the user-specified filter. |
725 | // |
726 | // Google Test allows the user to filter the tests by their full names. |
727 | // The full name of a test Bar in test suite Foo is defined as |
728 | // "Foo.Bar". Only the tests that match the filter will run. |
729 | // |
730 | // A filter is a colon-separated list of glob (not regex) patterns, |
731 | // optionally followed by a '-' and a colon-separated list of |
732 | // negative patterns (tests to exclude). A test is run if it |
733 | // matches one of the positive patterns and does not match any of |
734 | // the negative patterns. |
735 | // |
736 | // For example, *A*:Foo.* is a filter that matches any string that |
737 | // contains the character 'A' or starts with "Foo.". |
738 | bool should_run() const { return should_run_; } |
739 | |
740 | // Returns true iff this test will appear in the XML report. |
741 | bool is_reportable() const { |
742 | // The XML report includes tests matching the filter, excluding those |
743 | // run in other shards. |
744 | return matches_filter_ && !is_in_another_shard_; |
745 | } |
746 | |
747 | // Returns the result of the test. |
748 | const TestResult* result() const { return &result_; } |
749 | |
750 | private: |
751 | #if GTEST_HAS_DEATH_TEST |
752 | friend class internal::DefaultDeathTestFactory; |
753 | #endif // GTEST_HAS_DEATH_TEST |
754 | friend class Test; |
755 | friend class TestSuite; |
756 | friend class internal::UnitTestImpl; |
757 | friend class internal::StreamingListenerTest; |
758 | friend TestInfo* internal::MakeAndRegisterTestInfo( |
759 | const char* test_suite_name, const char* name, const char* type_param, |
760 | const char* value_param, internal::CodeLocation code_location, |
761 | internal::TypeId fixture_class_id, internal::SetUpTestSuiteFunc set_up_tc, |
762 | internal::TearDownTestSuiteFunc tear_down_tc, |
763 | internal::TestFactoryBase* factory); |
764 | |
765 | // Constructs a TestInfo object. The newly constructed instance assumes |
766 | // ownership of the factory object. |
767 | TestInfo(const std::string& test_suite_name, const std::string& name, |
768 | const char* a_type_param, // NULL if not a type-parameterized test |
769 | const char* a_value_param, // NULL if not a value-parameterized test |
770 | internal::CodeLocation a_code_location, |
771 | internal::TypeId fixture_class_id, |
772 | internal::TestFactoryBase* factory); |
773 | |
774 | // Increments the number of death tests encountered in this test so |
775 | // far. |
776 | int increment_death_test_count() { |
777 | return result_.increment_death_test_count(); |
778 | } |
779 | |
780 | // Creates the test object, runs it, records its result, and then |
781 | // deletes it. |
782 | void Run(); |
783 | |
784 | static void ClearTestResult(TestInfo* test_info) { |
785 | test_info->result_.Clear(); |
786 | } |
787 | |
788 | // These fields are immutable properties of the test. |
789 | const std::string test_suite_name_; // test suite name |
790 | const std::string name_; // Test name |
791 | // Name of the parameter type, or NULL if this is not a typed or a |
792 | // type-parameterized test. |
793 | const std::unique_ptr<const ::std::string> type_param_; |
794 | // Text representation of the value parameter, or NULL if this is not a |
795 | // value-parameterized test. |
796 | const std::unique_ptr<const ::std::string> value_param_; |
797 | internal::CodeLocation location_; |
798 | const internal::TypeId fixture_class_id_; // ID of the test fixture class |
799 | bool should_run_; // True iff this test should run |
800 | bool is_disabled_; // True iff this test is disabled |
801 | bool matches_filter_; // True if this test matches the |
802 | // user-specified filter. |
803 | bool is_in_another_shard_; // Will be run in another shard. |
804 | internal::TestFactoryBase* const factory_; // The factory that creates |
805 | // the test object |
806 | |
807 | // This field is mutable and needs to be reset before running the |
808 | // test for the second time. |
809 | TestResult result_; |
810 | |
811 | GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfo); |
812 | }; |
813 | |
814 | // A test suite, which consists of a vector of TestInfos. |
815 | // |
816 | // TestSuite is not copyable. |
817 | class GTEST_API_ TestSuite { |
818 | public: |
819 | // Creates a TestSuite with the given name. |
820 | // |
821 | // TestSuite does NOT have a default constructor. Always use this |
822 | // constructor to create a TestSuite object. |
823 | // |
824 | // Arguments: |
825 | // |
826 | // name: name of the test suite |
827 | // a_type_param: the name of the test's type parameter, or NULL if |
828 | // this is not a type-parameterized test. |
829 | // set_up_tc: pointer to the function that sets up the test suite |
830 | // tear_down_tc: pointer to the function that tears down the test suite |
831 | TestSuite(const char* name, const char* a_type_param, |
832 | internal::SetUpTestSuiteFunc set_up_tc, |
833 | internal::TearDownTestSuiteFunc tear_down_tc); |
834 | |
835 | // Destructor of TestSuite. |
836 | virtual ~TestSuite(); |
837 | |
838 | // Gets the name of the TestSuite. |
839 | const char* name() const { return name_.c_str(); } |
840 | |
841 | // Returns the name of the parameter type, or NULL if this is not a |
842 | // type-parameterized test suite. |
843 | const char* type_param() const { |
844 | if (type_param_.get() != nullptr) return type_param_->c_str(); |
845 | return nullptr; |
846 | } |
847 | |
848 | // Returns true if any test in this test suite should run. |
849 | bool should_run() const { return should_run_; } |
850 | |
851 | // Gets the number of successful tests in this test suite. |
852 | int successful_test_count() const; |
853 | |
854 | // Gets the number of skipped tests in this test suite. |
855 | int skipped_test_count() const; |
856 | |
857 | // Gets the number of failed tests in this test suite. |
858 | int failed_test_count() const; |
859 | |
860 | // Gets the number of disabled tests that will be reported in the XML report. |
861 | int reportable_disabled_test_count() const; |
862 | |
863 | // Gets the number of disabled tests in this test suite. |
864 | int disabled_test_count() const; |
865 | |
866 | // Gets the number of tests to be printed in the XML report. |
867 | int reportable_test_count() const; |
868 | |
869 | // Get the number of tests in this test suite that should run. |
870 | int test_to_run_count() const; |
871 | |
872 | // Gets the number of all tests in this test suite. |
873 | int total_test_count() const; |
874 | |
875 | // Returns true iff the test suite passed. |
876 | bool Passed() const { return !Failed(); } |
877 | |
878 | // Returns true iff the test suite failed. |
879 | bool Failed() const { return failed_test_count() > 0; } |
880 | |
881 | // Returns the elapsed time, in milliseconds. |
882 | TimeInMillis elapsed_time() const { return elapsed_time_; } |
883 | |
884 | // Returns the i-th test among all the tests. i can range from 0 to |
885 | // total_test_count() - 1. If i is not in that range, returns NULL. |
886 | const TestInfo* GetTestInfo(int i) const; |
887 | |
888 | // Returns the TestResult that holds test properties recorded during |
889 | // execution of SetUpTestSuite and TearDownTestSuite. |
890 | const TestResult& ad_hoc_test_result() const { return ad_hoc_test_result_; } |
891 | |
892 | private: |
893 | friend class Test; |
894 | friend class internal::UnitTestImpl; |
895 | |
896 | // Gets the (mutable) vector of TestInfos in this TestSuite. |
897 | std::vector<TestInfo*>& test_info_list() { return test_info_list_; } |
898 | |
899 | // Gets the (immutable) vector of TestInfos in this TestSuite. |
900 | const std::vector<TestInfo*>& test_info_list() const { |
901 | return test_info_list_; |
902 | } |
903 | |
904 | // Returns the i-th test among all the tests. i can range from 0 to |
905 | // total_test_count() - 1. If i is not in that range, returns NULL. |
906 | TestInfo* GetMutableTestInfo(int i); |
907 | |
908 | // Sets the should_run member. |
909 | void set_should_run(bool should) { should_run_ = should; } |
910 | |
911 | // Adds a TestInfo to this test suite. Will delete the TestInfo upon |
912 | // destruction of the TestSuite object. |
913 | void AddTestInfo(TestInfo * test_info); |
914 | |
915 | // Clears the results of all tests in this test suite. |
916 | void ClearResult(); |
917 | |
918 | // Clears the results of all tests in the given test suite. |
919 | static void ClearTestSuiteResult(TestSuite* test_suite) { |
920 | test_suite->ClearResult(); |
921 | } |
922 | |
923 | // Runs every test in this TestSuite. |
924 | void Run(); |
925 | |
926 | // Runs SetUpTestSuite() for this TestSuite. This wrapper is needed |
927 | // for catching exceptions thrown from SetUpTestSuite(). |
928 | void RunSetUpTestSuite() { |
929 | if (set_up_tc_ != nullptr) { |
930 | (*set_up_tc_)(); |
931 | } |
932 | } |
933 | |
934 | // Runs TearDownTestSuite() for this TestSuite. This wrapper is |
935 | // needed for catching exceptions thrown from TearDownTestSuite(). |
936 | void RunTearDownTestSuite() { |
937 | if (tear_down_tc_ != nullptr) { |
938 | (*tear_down_tc_)(); |
939 | } |
940 | } |
941 | |
942 | // Returns true iff test passed. |
943 | static bool TestPassed(const TestInfo* test_info) { |
944 | return test_info->should_run() && test_info->result()->Passed(); |
945 | } |
946 | |
947 | // Returns true iff test skipped. |
948 | static bool TestSkipped(const TestInfo* test_info) { |
949 | return test_info->should_run() && test_info->result()->Skipped(); |
950 | } |
951 | |
952 | // Returns true iff test failed. |
953 | static bool TestFailed(const TestInfo* test_info) { |
954 | return test_info->should_run() && test_info->result()->Failed(); |
955 | } |
956 | |
957 | // Returns true iff the test is disabled and will be reported in the XML |
958 | // report. |
959 | static bool TestReportableDisabled(const TestInfo* test_info) { |
960 | return test_info->is_reportable() && test_info->is_disabled_; |
961 | } |
962 | |
963 | // Returns true iff test is disabled. |
964 | static bool TestDisabled(const TestInfo* test_info) { |
965 | return test_info->is_disabled_; |
966 | } |
967 | |
968 | // Returns true iff this test will appear in the XML report. |
969 | static bool TestReportable(const TestInfo* test_info) { |
970 | return test_info->is_reportable(); |
971 | } |
972 | |
973 | // Returns true if the given test should run. |
974 | static bool ShouldRunTest(const TestInfo* test_info) { |
975 | return test_info->should_run(); |
976 | } |
977 | |
978 | // Shuffles the tests in this test suite. |
979 | void ShuffleTests(internal::Random* random); |
980 | |
981 | // Restores the test order to before the first shuffle. |
982 | void UnshuffleTests(); |
983 | |
984 | // Name of the test suite. |
985 | std::string name_; |
986 | // Name of the parameter type, or NULL if this is not a typed or a |
987 | // type-parameterized test. |
988 | const std::unique_ptr<const ::std::string> type_param_; |
989 | // The vector of TestInfos in their original order. It owns the |
990 | // elements in the vector. |
991 | std::vector<TestInfo*> test_info_list_; |
992 | // Provides a level of indirection for the test list to allow easy |
993 | // shuffling and restoring the test order. The i-th element in this |
994 | // vector is the index of the i-th test in the shuffled test list. |
995 | std::vector<int> test_indices_; |
996 | // Pointer to the function that sets up the test suite. |
997 | internal::SetUpTestSuiteFunc set_up_tc_; |
998 | // Pointer to the function that tears down the test suite. |
999 | internal::TearDownTestSuiteFunc tear_down_tc_; |
1000 | // True iff any test in this test suite should run. |
1001 | bool should_run_; |
1002 | // Elapsed time, in milliseconds. |
1003 | TimeInMillis elapsed_time_; |
1004 | // Holds test properties recorded during execution of SetUpTestSuite and |
1005 | // TearDownTestSuite. |
1006 | TestResult ad_hoc_test_result_; |
1007 | |
1008 | // We disallow copying TestSuites. |
1009 | GTEST_DISALLOW_COPY_AND_ASSIGN_(TestSuite); |
1010 | }; |
1011 | |
1012 | // An Environment object is capable of setting up and tearing down an |
1013 | // environment. You should subclass this to define your own |
1014 | // environment(s). |
1015 | // |
1016 | // An Environment object does the set-up and tear-down in virtual |
1017 | // methods SetUp() and TearDown() instead of the constructor and the |
1018 | // destructor, as: |
1019 | // |
1020 | // 1. You cannot safely throw from a destructor. This is a problem |
1021 | // as in some cases Google Test is used where exceptions are enabled, and |
1022 | // we may want to implement ASSERT_* using exceptions where they are |
1023 | // available. |
1024 | // 2. You cannot use ASSERT_* directly in a constructor or |
1025 | // destructor. |
1026 | class Environment { |
1027 | public: |
1028 | // The d'tor is virtual as we need to subclass Environment. |
1029 | virtual ~Environment() {} |
1030 | |
1031 | // Override this to define how to set up the environment. |
1032 | virtual void SetUp() {} |
1033 | |
1034 | // Override this to define how to tear down the environment. |
1035 | virtual void TearDown() {} |
1036 | private: |
1037 | // If you see an error about overriding the following function or |
1038 | // about it being private, you have mis-spelled SetUp() as Setup(). |
1039 | struct Setup_should_be_spelled_SetUp {}; |
1040 | virtual Setup_should_be_spelled_SetUp* Setup() { return nullptr; } |
1041 | }; |
1042 | |
1043 | #if GTEST_HAS_EXCEPTIONS |
1044 | |
1045 | // Exception which can be thrown from TestEventListener::OnTestPartResult. |
1046 | class GTEST_API_ AssertionException |
1047 | : public internal::GoogleTestFailureException { |
1048 | public: |
1049 | explicit AssertionException(const TestPartResult& result) |
1050 | : GoogleTestFailureException(result) {} |
1051 | }; |
1052 | |
1053 | #endif // GTEST_HAS_EXCEPTIONS |
1054 | |
1055 | // The interface for tracing execution of tests. The methods are organized in |
1056 | // the order the corresponding events are fired. |
1057 | class TestEventListener { |
1058 | public: |
1059 | virtual ~TestEventListener() {} |
1060 | |
1061 | // Fired before any test activity starts. |
1062 | virtual void OnTestProgramStart(const UnitTest& unit_test) = 0; |
1063 | |
1064 | // Fired before each iteration of tests starts. There may be more than |
1065 | // one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration |
1066 | // index, starting from 0. |
1067 | virtual void OnTestIterationStart(const UnitTest& unit_test, |
1068 | int iteration) = 0; |
1069 | |
1070 | // Fired before environment set-up for each iteration of tests starts. |
1071 | virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0; |
1072 | |
1073 | // Fired after environment set-up for each iteration of tests ends. |
1074 | virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0; |
1075 | |
1076 | // Fired before the test suite starts. |
1077 | virtual void OnTestSuiteStart(const TestSuite& /*test_suite*/) {} |
1078 | |
1079 | // Legacy API is deprecated but still available |
1080 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
1081 | virtual void OnTestCaseStart(const TestCase& /*test_case*/) {} |
1082 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
1083 | |
1084 | // Fired before the test starts. |
1085 | virtual void OnTestStart(const TestInfo& test_info) = 0; |
1086 | |
1087 | // Fired after a failed assertion or a SUCCEED() invocation. |
1088 | // If you want to throw an exception from this function to skip to the next |
1089 | // TEST, it must be AssertionException defined above, or inherited from it. |
1090 | virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0; |
1091 | |
1092 | // Fired after the test ends. |
1093 | virtual void OnTestEnd(const TestInfo& test_info) = 0; |
1094 | |
1095 | // Fired after the test suite ends. |
1096 | virtual void OnTestSuiteEnd(const TestSuite& /*test_suite*/) {} |
1097 | |
1098 | // Legacy API is deprecated but still available |
1099 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
1100 | virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {} |
1101 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
1102 | |
1103 | // Fired before environment tear-down for each iteration of tests starts. |
1104 | virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0; |
1105 | |
1106 | // Fired after environment tear-down for each iteration of tests ends. |
1107 | virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0; |
1108 | |
1109 | // Fired after each iteration of tests finishes. |
1110 | virtual void OnTestIterationEnd(const UnitTest& unit_test, |
1111 | int iteration) = 0; |
1112 | |
1113 | // Fired after all test activities have ended. |
1114 | virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0; |
1115 | }; |
1116 | |
1117 | // The convenience class for users who need to override just one or two |
1118 | // methods and are not concerned that a possible change to a signature of |
1119 | // the methods they override will not be caught during the build. For |
1120 | // comments about each method please see the definition of TestEventListener |
1121 | // above. |
1122 | class EmptyTestEventListener : public TestEventListener { |
1123 | public: |
1124 | void OnTestProgramStart(const UnitTest& /*unit_test*/) override {} |
1125 | void OnTestIterationStart(const UnitTest& /*unit_test*/, |
1126 | int /*iteration*/) override {} |
1127 | void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) override {} |
1128 | void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) override {} |
1129 | void OnTestSuiteStart(const TestSuite& /*test_suite*/) override {} |
1130 | // Legacy API is deprecated but still available |
1131 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
1132 | void OnTestCaseStart(const TestCase& /*test_case*/) override {} |
1133 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
1134 | |
1135 | void OnTestStart(const TestInfo& /*test_info*/) override {} |
1136 | void OnTestPartResult(const TestPartResult& /*test_part_result*/) override {} |
1137 | void OnTestEnd(const TestInfo& /*test_info*/) override {} |
1138 | void OnTestSuiteEnd(const TestSuite& /*test_suite*/) override {} |
1139 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
1140 | void OnTestCaseEnd(const TestCase& /*test_case*/) override {} |
1141 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
1142 | |
1143 | void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) override {} |
1144 | void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) override {} |
1145 | void OnTestIterationEnd(const UnitTest& /*unit_test*/, |
1146 | int /*iteration*/) override {} |
1147 | void OnTestProgramEnd(const UnitTest& /*unit_test*/) override {} |
1148 | }; |
1149 | |
1150 | // TestEventListeners lets users add listeners to track events in Google Test. |
1151 | class GTEST_API_ TestEventListeners { |
1152 | public: |
1153 | TestEventListeners(); |
1154 | ~TestEventListeners(); |
1155 | |
1156 | // Appends an event listener to the end of the list. Google Test assumes |
1157 | // the ownership of the listener (i.e. it will delete the listener when |
1158 | // the test program finishes). |
1159 | void Append(TestEventListener* listener); |
1160 | |
1161 | // Removes the given event listener from the list and returns it. It then |
1162 | // becomes the caller's responsibility to delete the listener. Returns |
1163 | // NULL if the listener is not found in the list. |
1164 | TestEventListener* Release(TestEventListener* listener); |
1165 | |
1166 | // Returns the standard listener responsible for the default console |
1167 | // output. Can be removed from the listeners list to shut down default |
1168 | // console output. Note that removing this object from the listener list |
1169 | // with Release transfers its ownership to the caller and makes this |
1170 | // function return NULL the next time. |
1171 | TestEventListener* default_result_printer() const { |
1172 | return default_result_printer_; |
1173 | } |
1174 | |
1175 | // Returns the standard listener responsible for the default XML output |
1176 | // controlled by the --gtest_output=xml flag. Can be removed from the |
1177 | // listeners list by users who want to shut down the default XML output |
1178 | // controlled by this flag and substitute it with custom one. Note that |
1179 | // removing this object from the listener list with Release transfers its |
1180 | // ownership to the caller and makes this function return NULL the next |
1181 | // time. |
1182 | TestEventListener* default_xml_generator() const { |
1183 | return default_xml_generator_; |
1184 | } |
1185 | |
1186 | private: |
1187 | friend class TestSuite; |
1188 | friend class TestInfo; |
1189 | friend class internal::DefaultGlobalTestPartResultReporter; |
1190 | friend class internal::NoExecDeathTest; |
1191 | friend class internal::TestEventListenersAccessor; |
1192 | friend class internal::UnitTestImpl; |
1193 | |
1194 | // Returns repeater that broadcasts the TestEventListener events to all |
1195 | // subscribers. |
1196 | TestEventListener* repeater(); |
1197 | |
1198 | // Sets the default_result_printer attribute to the provided listener. |
1199 | // The listener is also added to the listener list and previous |
1200 | // default_result_printer is removed from it and deleted. The listener can |
1201 | // also be NULL in which case it will not be added to the list. Does |
1202 | // nothing if the previous and the current listener objects are the same. |
1203 | void SetDefaultResultPrinter(TestEventListener* listener); |
1204 | |
1205 | // Sets the default_xml_generator attribute to the provided listener. The |
1206 | // listener is also added to the listener list and previous |
1207 | // default_xml_generator is removed from it and deleted. The listener can |
1208 | // also be NULL in which case it will not be added to the list. Does |
1209 | // nothing if the previous and the current listener objects are the same. |
1210 | void SetDefaultXmlGenerator(TestEventListener* listener); |
1211 | |
1212 | // Controls whether events will be forwarded by the repeater to the |
1213 | // listeners in the list. |
1214 | bool EventForwardingEnabled() const; |
1215 | void SuppressEventForwarding(); |
1216 | |
1217 | // The actual list of listeners. |
1218 | internal::TestEventRepeater* repeater_; |
1219 | // Listener responsible for the standard result output. |
1220 | TestEventListener* default_result_printer_; |
1221 | // Listener responsible for the creation of the XML output file. |
1222 | TestEventListener* default_xml_generator_; |
1223 | |
1224 | // We disallow copying TestEventListeners. |
1225 | GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventListeners); |
1226 | }; |
1227 | |
1228 | // A UnitTest consists of a vector of TestSuites. |
1229 | // |
1230 | // This is a singleton class. The only instance of UnitTest is |
1231 | // created when UnitTest::GetInstance() is first called. This |
1232 | // instance is never deleted. |
1233 | // |
1234 | // UnitTest is not copyable. |
1235 | // |
1236 | // This class is thread-safe as long as the methods are called |
1237 | // according to their specification. |
1238 | class GTEST_API_ UnitTest { |
1239 | public: |
1240 | // Gets the singleton UnitTest object. The first time this method |
1241 | // is called, a UnitTest object is constructed and returned. |
1242 | // Consecutive calls will return the same object. |
1243 | static UnitTest* GetInstance(); |
1244 | |
1245 | // Runs all tests in this UnitTest object and prints the result. |
1246 | // Returns 0 if successful, or 1 otherwise. |
1247 | // |
1248 | // This method can only be called from the main thread. |
1249 | // |
1250 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
1251 | int Run() GTEST_MUST_USE_RESULT_; |
1252 | |
1253 | // Returns the working directory when the first TEST() or TEST_F() |
1254 | // was executed. The UnitTest object owns the string. |
1255 | const char* original_working_dir() const; |
1256 | |
1257 | // Returns the TestSuite object for the test that's currently running, |
1258 | // or NULL if no test is running. |
1259 | const TestSuite* current_test_suite() const GTEST_LOCK_EXCLUDED_(mutex_); |
1260 | |
1261 | // Legacy API is still available but deprecated |
1262 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
1263 | const TestCase* current_test_case() const GTEST_LOCK_EXCLUDED_(mutex_); |
1264 | #endif |
1265 | |
1266 | // Returns the TestInfo object for the test that's currently running, |
1267 | // or NULL if no test is running. |
1268 | const TestInfo* current_test_info() const |
1269 | GTEST_LOCK_EXCLUDED_(mutex_); |
1270 | |
1271 | // Returns the random seed used at the start of the current test run. |
1272 | int random_seed() const; |
1273 | |
1274 | // Returns the ParameterizedTestSuiteRegistry object used to keep track of |
1275 | // value-parameterized tests and instantiate and register them. |
1276 | // |
1277 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
1278 | internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() |
1279 | GTEST_LOCK_EXCLUDED_(mutex_); |
1280 | |
1281 | // Gets the number of successful test suites. |
1282 | int successful_test_suite_count() const; |
1283 | |
1284 | // Gets the number of failed test suites. |
1285 | int failed_test_suite_count() const; |
1286 | |
1287 | // Gets the number of all test suites. |
1288 | int total_test_suite_count() const; |
1289 | |
1290 | // Gets the number of all test suites that contain at least one test |
1291 | // that should run. |
1292 | int test_suite_to_run_count() const; |
1293 | |
1294 | // Legacy API is deprecated but still available |
1295 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
1296 | int successful_test_case_count() const; |
1297 | int failed_test_case_count() const; |
1298 | int total_test_case_count() const; |
1299 | int test_case_to_run_count() const; |
1300 | #endif // EMOVE_LEGACY_TEST_CASEAPI |
1301 | |
1302 | // Gets the number of successful tests. |
1303 | int successful_test_count() const; |
1304 | |
1305 | // Gets the number of skipped tests. |
1306 | int skipped_test_count() const; |
1307 | |
1308 | // Gets the number of failed tests. |
1309 | int failed_test_count() const; |
1310 | |
1311 | // Gets the number of disabled tests that will be reported in the XML report. |
1312 | int reportable_disabled_test_count() const; |
1313 | |
1314 | // Gets the number of disabled tests. |
1315 | int disabled_test_count() const; |
1316 | |
1317 | // Gets the number of tests to be printed in the XML report. |
1318 | int reportable_test_count() const; |
1319 | |
1320 | // Gets the number of all tests. |
1321 | int total_test_count() const; |
1322 | |
1323 | // Gets the number of tests that should run. |
1324 | int test_to_run_count() const; |
1325 | |
1326 | // Gets the time of the test program start, in ms from the start of the |
1327 | // UNIX epoch. |
1328 | TimeInMillis start_timestamp() const; |
1329 | |
1330 | // Gets the elapsed time, in milliseconds. |
1331 | TimeInMillis elapsed_time() const; |
1332 | |
1333 | // Returns true iff the unit test passed (i.e. all test suites passed). |
1334 | bool Passed() const; |
1335 | |
1336 | // Returns true iff the unit test failed (i.e. some test suite failed |
1337 | // or something outside of all tests failed). |
1338 | bool Failed() const; |
1339 | |
1340 | // Gets the i-th test suite among all the test suites. i can range from 0 to |
1341 | // total_test_suite_count() - 1. If i is not in that range, returns NULL. |
1342 | const TestSuite* GetTestSuite(int i) const; |
1343 | |
1344 | // Legacy API is deprecated but still available |
1345 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
1346 | const TestCase* GetTestCase(int i) const; |
1347 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
1348 | |
1349 | // Returns the TestResult containing information on test failures and |
1350 | // properties logged outside of individual test suites. |
1351 | const TestResult& ad_hoc_test_result() const; |
1352 | |
1353 | // Returns the list of event listeners that can be used to track events |
1354 | // inside Google Test. |
1355 | TestEventListeners& listeners(); |
1356 | |
1357 | private: |
1358 | // Registers and returns a global test environment. When a test |
1359 | // program is run, all global test environments will be set-up in |
1360 | // the order they were registered. After all tests in the program |
1361 | // have finished, all global test environments will be torn-down in |
1362 | // the *reverse* order they were registered. |
1363 | // |
1364 | // The UnitTest object takes ownership of the given environment. |
1365 | // |
1366 | // This method can only be called from the main thread. |
1367 | Environment* AddEnvironment(Environment* env); |
1368 | |
1369 | // Adds a TestPartResult to the current TestResult object. All |
1370 | // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) |
1371 | // eventually call this to report their results. The user code |
1372 | // should use the assertion macros instead of calling this directly. |
1373 | void AddTestPartResult(TestPartResult::Type result_type, |
1374 | const char* file_name, |
1375 | int line_number, |
1376 | const std::string& message, |
1377 | const std::string& os_stack_trace) |
1378 | GTEST_LOCK_EXCLUDED_(mutex_); |
1379 | |
1380 | // Adds a TestProperty to the current TestResult object when invoked from |
1381 | // inside a test, to current TestSuite's ad_hoc_test_result_ when invoked |
1382 | // from SetUpTestSuite or TearDownTestSuite, or to the global property set |
1383 | // when invoked elsewhere. If the result already contains a property with |
1384 | // the same key, the value will be updated. |
1385 | void RecordProperty(const std::string& key, const std::string& value); |
1386 | |
1387 | // Gets the i-th test suite among all the test suites. i can range from 0 to |
1388 | // total_test_suite_count() - 1. If i is not in that range, returns NULL. |
1389 | TestSuite* GetMutableTestSuite(int i); |
1390 | |
1391 | // Accessors for the implementation object. |
1392 | internal::UnitTestImpl* impl() { return impl_; } |
1393 | const internal::UnitTestImpl* impl() const { return impl_; } |
1394 | |
1395 | // These classes and functions are friends as they need to access private |
1396 | // members of UnitTest. |
1397 | friend class ScopedTrace; |
1398 | friend class Test; |
1399 | friend class internal::AssertHelper; |
1400 | friend class internal::StreamingListenerTest; |
1401 | friend class internal::UnitTestRecordPropertyTestHelper; |
1402 | friend Environment* AddGlobalTestEnvironment(Environment* env); |
1403 | friend internal::UnitTestImpl* internal::GetUnitTestImpl(); |
1404 | friend void internal::ReportFailureInUnknownLocation( |
1405 | TestPartResult::Type result_type, |
1406 | const std::string& message); |
1407 | |
1408 | // Creates an empty UnitTest. |
1409 | UnitTest(); |
1410 | |
1411 | // D'tor |
1412 | virtual ~UnitTest(); |
1413 | |
1414 | // Pushes a trace defined by SCOPED_TRACE() on to the per-thread |
1415 | // Google Test trace stack. |
1416 | void PushGTestTrace(const internal::TraceInfo& trace) |
1417 | GTEST_LOCK_EXCLUDED_(mutex_); |
1418 | |
1419 | // Pops a trace from the per-thread Google Test trace stack. |
1420 | void PopGTestTrace() |
1421 | GTEST_LOCK_EXCLUDED_(mutex_); |
1422 | |
1423 | // Protects mutable state in *impl_. This is mutable as some const |
1424 | // methods need to lock it too. |
1425 | mutable internal::Mutex mutex_; |
1426 | |
1427 | // Opaque implementation object. This field is never changed once |
1428 | // the object is constructed. We don't mark it as const here, as |
1429 | // doing so will cause a warning in the constructor of UnitTest. |
1430 | // Mutable state in *impl_ is protected by mutex_. |
1431 | internal::UnitTestImpl* impl_; |
1432 | |
1433 | // We disallow copying UnitTest. |
1434 | GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTest); |
1435 | }; |
1436 | |
1437 | // A convenient wrapper for adding an environment for the test |
1438 | // program. |
1439 | // |
1440 | // You should call this before RUN_ALL_TESTS() is called, probably in |
1441 | // main(). If you use gtest_main, you need to call this before main() |
1442 | // starts for it to take effect. For example, you can define a global |
1443 | // variable like this: |
1444 | // |
1445 | // testing::Environment* const foo_env = |
1446 | // testing::AddGlobalTestEnvironment(new FooEnvironment); |
1447 | // |
1448 | // However, we strongly recommend you to write your own main() and |
1449 | // call AddGlobalTestEnvironment() there, as relying on initialization |
1450 | // of global variables makes the code harder to read and may cause |
1451 | // problems when you register multiple environments from different |
1452 | // translation units and the environments have dependencies among them |
1453 | // (remember that the compiler doesn't guarantee the order in which |
1454 | // global variables from different translation units are initialized). |
1455 | inline Environment* AddGlobalTestEnvironment(Environment* env) { |
1456 | return UnitTest::GetInstance()->AddEnvironment(env); |
1457 | } |
1458 | |
1459 | // Initializes Google Test. This must be called before calling |
1460 | // RUN_ALL_TESTS(). In particular, it parses a command line for the |
1461 | // flags that Google Test recognizes. Whenever a Google Test flag is |
1462 | // seen, it is removed from argv, and *argc is decremented. |
1463 | // |
1464 | // No value is returned. Instead, the Google Test flag variables are |
1465 | // updated. |
1466 | // |
1467 | // Calling the function for the second time has no user-visible effect. |
1468 | GTEST_API_ void InitGoogleTest(int* argc, char** argv); |
1469 | |
1470 | // This overloaded version can be used in Windows programs compiled in |
1471 | // UNICODE mode. |
1472 | GTEST_API_ void InitGoogleTest(int* argc, wchar_t** argv); |
1473 | |
1474 | // This overloaded version can be used on Arduino/embedded platforms where |
1475 | // there is no argc/argv. |
1476 | GTEST_API_ void InitGoogleTest(); |
1477 | |
1478 | namespace internal { |
1479 | |
1480 | // Separate the error generating code from the code path to reduce the stack |
1481 | // frame size of CmpHelperEQ. This helps reduce the overhead of some sanitizers |
1482 | // when calling EXPECT_* in a tight loop. |
1483 | template <typename T1, typename T2> |
1484 | AssertionResult CmpHelperEQFailure(const char* lhs_expression, |
1485 | const char* rhs_expression, |
1486 | const T1& lhs, const T2& rhs) { |
1487 | return EqFailure(lhs_expression, |
1488 | rhs_expression, |
1489 | FormatForComparisonFailureMessage(lhs, rhs), |
1490 | FormatForComparisonFailureMessage(rhs, lhs), |
1491 | false); |
1492 | } |
1493 | |
1494 | // This block of code defines operator==/!= |
1495 | // to block lexical scope lookup. |
1496 | // It prevents using invalid operator==/!= defined at namespace scope. |
1497 | struct faketype {}; |
1498 | inline bool operator==(faketype, faketype) { return true; } |
1499 | inline bool operator!=(faketype, faketype) { return false; } |
1500 | |
1501 | // The helper function for {ASSERT|EXPECT}_EQ. |
1502 | template <typename T1, typename T2> |
1503 | AssertionResult CmpHelperEQ(const char* lhs_expression, |
1504 | const char* rhs_expression, |
1505 | const T1& lhs, |
1506 | const T2& rhs) { |
1507 | if (lhs == rhs) { |
1508 | return AssertionSuccess(); |
1509 | } |
1510 | |
1511 | return CmpHelperEQFailure(lhs_expression, rhs_expression, lhs, rhs); |
1512 | } |
1513 | |
1514 | // With this overloaded version, we allow anonymous enums to be used |
1515 | // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous enums |
1516 | // can be implicitly cast to BiggestInt. |
1517 | GTEST_API_ AssertionResult CmpHelperEQ(const char* lhs_expression, |
1518 | const char* rhs_expression, |
1519 | BiggestInt lhs, |
1520 | BiggestInt rhs); |
1521 | |
1522 | class EqHelper { |
1523 | public: |
1524 | // This templatized version is for the general case. |
1525 | template < |
1526 | typename T1, typename T2, |
1527 | // Disable this overload for cases where one argument is a pointer |
1528 | // and the other is the null pointer constant. |
1529 | typename std::enable_if<!std::is_integral<T1>::value || |
1530 | !std::is_pointer<T2>::value>::type* = nullptr> |
1531 | static AssertionResult Compare(const char* lhs_expression, |
1532 | const char* rhs_expression, const T1& lhs, |
1533 | const T2& rhs) { |
1534 | return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs); |
1535 | } |
1536 | |
1537 | // With this overloaded version, we allow anonymous enums to be used |
1538 | // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous |
1539 | // enums can be implicitly cast to BiggestInt. |
1540 | // |
1541 | // Even though its body looks the same as the above version, we |
1542 | // cannot merge the two, as it will make anonymous enums unhappy. |
1543 | static AssertionResult Compare(const char* lhs_expression, |
1544 | const char* rhs_expression, |
1545 | BiggestInt lhs, |
1546 | BiggestInt rhs) { |
1547 | return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs); |
1548 | } |
1549 | |
1550 | template <typename T> |
1551 | static AssertionResult Compare( |
1552 | const char* lhs_expression, const char* rhs_expression, |
1553 | // Handle cases where '0' is used as a null pointer literal. |
1554 | std::nullptr_t /* lhs */, T* rhs) { |
1555 | // We already know that 'lhs' is a null pointer. |
1556 | return CmpHelperEQ(lhs_expression, rhs_expression, static_cast<T*>(nullptr), |
1557 | rhs); |
1558 | } |
1559 | }; |
1560 | |
1561 | // Separate the error generating code from the code path to reduce the stack |
1562 | // frame size of CmpHelperOP. This helps reduce the overhead of some sanitizers |
1563 | // when calling EXPECT_OP in a tight loop. |
1564 | template <typename T1, typename T2> |
1565 | AssertionResult CmpHelperOpFailure(const char* expr1, const char* expr2, |
1566 | const T1& val1, const T2& val2, |
1567 | const char* op) { |
1568 | return AssertionFailure() |
1569 | << "Expected: (" << expr1 << ") " << op << " (" << expr2 |
1570 | << "), actual: " << FormatForComparisonFailureMessage(val1, val2) |
1571 | << " vs " << FormatForComparisonFailureMessage(val2, val1); |
1572 | } |
1573 | |
1574 | // A macro for implementing the helper functions needed to implement |
1575 | // ASSERT_?? and EXPECT_??. It is here just to avoid copy-and-paste |
1576 | // of similar code. |
1577 | // |
1578 | // For each templatized helper function, we also define an overloaded |
1579 | // version for BiggestInt in order to reduce code bloat and allow |
1580 | // anonymous enums to be used with {ASSERT|EXPECT}_?? when compiled |
1581 | // with gcc 4. |
1582 | // |
1583 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
1584 | |
1585 | #define GTEST_IMPL_CMP_HELPER_(op_name, op)\ |
1586 | template <typename T1, typename T2>\ |
1587 | AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ |
1588 | const T1& val1, const T2& val2) {\ |
1589 | if (val1 op val2) {\ |
1590 | return AssertionSuccess();\ |
1591 | } else {\ |
1592 | return CmpHelperOpFailure(expr1, expr2, val1, val2, #op);\ |
1593 | }\ |
1594 | }\ |
1595 | GTEST_API_ AssertionResult CmpHelper##op_name(\ |
1596 | const char* expr1, const char* expr2, BiggestInt val1, BiggestInt val2) |
1597 | |
1598 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
1599 | |
1600 | // Implements the helper function for {ASSERT|EXPECT}_NE |
1601 | GTEST_IMPL_CMP_HELPER_(NE, !=); |
1602 | // Implements the helper function for {ASSERT|EXPECT}_LE |
1603 | GTEST_IMPL_CMP_HELPER_(LE, <=); |
1604 | // Implements the helper function for {ASSERT|EXPECT}_LT |
1605 | GTEST_IMPL_CMP_HELPER_(LT, <); |
1606 | // Implements the helper function for {ASSERT|EXPECT}_GE |
1607 | GTEST_IMPL_CMP_HELPER_(GE, >=); |
1608 | // Implements the helper function for {ASSERT|EXPECT}_GT |
1609 | GTEST_IMPL_CMP_HELPER_(GT, >); |
1610 | |
1611 | #undef GTEST_IMPL_CMP_HELPER_ |
1612 | |
1613 | // The helper function for {ASSERT|EXPECT}_STREQ. |
1614 | // |
1615 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
1616 | GTEST_API_ AssertionResult CmpHelperSTREQ(const char* s1_expression, |
1617 | const char* s2_expression, |
1618 | const char* s1, |
1619 | const char* s2); |
1620 | |
1621 | // The helper function for {ASSERT|EXPECT}_STRCASEEQ. |
1622 | // |
1623 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
1624 | GTEST_API_ AssertionResult CmpHelperSTRCASEEQ(const char* s1_expression, |
1625 | const char* s2_expression, |
1626 | const char* s1, |
1627 | const char* s2); |
1628 | |
1629 | // The helper function for {ASSERT|EXPECT}_STRNE. |
1630 | // |
1631 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
1632 | GTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression, |
1633 | const char* s2_expression, |
1634 | const char* s1, |
1635 | const char* s2); |
1636 | |
1637 | // The helper function for {ASSERT|EXPECT}_STRCASENE. |
1638 | // |
1639 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
1640 | GTEST_API_ AssertionResult CmpHelperSTRCASENE(const char* s1_expression, |
1641 | const char* s2_expression, |
1642 | const char* s1, |
1643 | const char* s2); |
1644 | |
1645 | |
1646 | // Helper function for *_STREQ on wide strings. |
1647 | // |
1648 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
1649 | GTEST_API_ AssertionResult CmpHelperSTREQ(const char* s1_expression, |
1650 | const char* s2_expression, |
1651 | const wchar_t* s1, |
1652 | const wchar_t* s2); |
1653 | |
1654 | // Helper function for *_STRNE on wide strings. |
1655 | // |
1656 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
1657 | GTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression, |
1658 | const char* s2_expression, |
1659 | const wchar_t* s1, |
1660 | const wchar_t* s2); |
1661 | |
1662 | } // namespace internal |
1663 | |
1664 | // IsSubstring() and IsNotSubstring() are intended to be used as the |
1665 | // first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by |
1666 | // themselves. They check whether needle is a substring of haystack |
1667 | // (NULL is considered a substring of itself only), and return an |
1668 | // appropriate error message when they fail. |
1669 | // |
1670 | // The {needle,haystack}_expr arguments are the stringified |
1671 | // expressions that generated the two real arguments. |
1672 | GTEST_API_ AssertionResult IsSubstring( |
1673 | const char* needle_expr, const char* haystack_expr, |
1674 | const char* needle, const char* haystack); |
1675 | GTEST_API_ AssertionResult IsSubstring( |
1676 | const char* needle_expr, const char* haystack_expr, |
1677 | const wchar_t* needle, const wchar_t* haystack); |
1678 | GTEST_API_ AssertionResult IsNotSubstring( |
1679 | const char* needle_expr, const char* haystack_expr, |
1680 | const char* needle, const char* haystack); |
1681 | GTEST_API_ AssertionResult IsNotSubstring( |
1682 | const char* needle_expr, const char* haystack_expr, |
1683 | const wchar_t* needle, const wchar_t* haystack); |
1684 | GTEST_API_ AssertionResult IsSubstring( |
1685 | const char* needle_expr, const char* haystack_expr, |
1686 | const ::std::string& needle, const ::std::string& haystack); |
1687 | GTEST_API_ AssertionResult IsNotSubstring( |
1688 | const char* needle_expr, const char* haystack_expr, |
1689 | const ::std::string& needle, const ::std::string& haystack); |
1690 | |
1691 | #if GTEST_HAS_STD_WSTRING |
1692 | GTEST_API_ AssertionResult IsSubstring( |
1693 | const char* needle_expr, const char* haystack_expr, |
1694 | const ::std::wstring& needle, const ::std::wstring& haystack); |
1695 | GTEST_API_ AssertionResult IsNotSubstring( |
1696 | const char* needle_expr, const char* haystack_expr, |
1697 | const ::std::wstring& needle, const ::std::wstring& haystack); |
1698 | #endif // GTEST_HAS_STD_WSTRING |
1699 | |
1700 | namespace internal { |
1701 | |
1702 | // Helper template function for comparing floating-points. |
1703 | // |
1704 | // Template parameter: |
1705 | // |
1706 | // RawType: the raw floating-point type (either float or double) |
1707 | // |
1708 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
1709 | template <typename RawType> |
1710 | AssertionResult CmpHelperFloatingPointEQ(const char* lhs_expression, |
1711 | const char* rhs_expression, |
1712 | RawType lhs_value, |
1713 | RawType rhs_value) { |
1714 | const FloatingPoint<RawType> lhs(lhs_value), rhs(rhs_value); |
1715 | |
1716 | if (lhs.AlmostEquals(rhs)) { |
1717 | return AssertionSuccess(); |
1718 | } |
1719 | |
1720 | ::std::stringstream lhs_ss; |
1721 | lhs_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) |
1722 | << lhs_value; |
1723 | |
1724 | ::std::stringstream rhs_ss; |
1725 | rhs_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) |
1726 | << rhs_value; |
1727 | |
1728 | return EqFailure(lhs_expression, |
1729 | rhs_expression, |
1730 | StringStreamToString(&lhs_ss), |
1731 | StringStreamToString(&rhs_ss), |
1732 | false); |
1733 | } |
1734 | |
1735 | // Helper function for implementing ASSERT_NEAR. |
1736 | // |
1737 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
1738 | GTEST_API_ AssertionResult DoubleNearPredFormat(const char* expr1, |
1739 | const char* expr2, |
1740 | const char* abs_error_expr, |
1741 | double val1, |
1742 | double val2, |
1743 | double abs_error); |
1744 | |
1745 | // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. |
1746 | // A class that enables one to stream messages to assertion macros |
1747 | class GTEST_API_ AssertHelper { |
1748 | public: |
1749 | // Constructor. |
1750 | AssertHelper(TestPartResult::Type type, |
1751 | const char* file, |
1752 | int line, |
1753 | const char* message); |
1754 | ~AssertHelper(); |
1755 | |
1756 | // Message assignment is a semantic trick to enable assertion |
1757 | // streaming; see the GTEST_MESSAGE_ macro below. |
1758 | void operator=(const Message& message) const; |
1759 | |
1760 | private: |
1761 | // We put our data in a struct so that the size of the AssertHelper class can |
1762 | // be as small as possible. This is important because gcc is incapable of |
1763 | // re-using stack space even for temporary variables, so every EXPECT_EQ |
1764 | // reserves stack space for another AssertHelper. |
1765 | struct AssertHelperData { |
1766 | AssertHelperData(TestPartResult::Type t, |
1767 | const char* srcfile, |
1768 | int line_num, |
1769 | const char* msg) |
1770 | : type(t), file(srcfile), line(line_num), message(msg) { } |
1771 | |
1772 | TestPartResult::Type const type; |
1773 | const char* const file; |
1774 | int const line; |
1775 | std::string const message; |
1776 | |
1777 | private: |
1778 | GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelperData); |
1779 | }; |
1780 | |
1781 | AssertHelperData* const data_; |
1782 | |
1783 | GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelper); |
1784 | }; |
1785 | |
1786 | enum GTestColor { COLOR_DEFAULT, COLOR_RED, COLOR_GREEN, COLOR_YELLOW }; |
1787 | |
1788 | GTEST_API_ GTEST_ATTRIBUTE_PRINTF_(2, 3) void ColoredPrintf(GTestColor color, |
1789 | const char* fmt, |
1790 | ...); |
1791 | |
1792 | } // namespace internal |
1793 | |
1794 | // The pure interface class that all value-parameterized tests inherit from. |
1795 | // A value-parameterized class must inherit from both ::testing::Test and |
1796 | // ::testing::WithParamInterface. In most cases that just means inheriting |
1797 | // from ::testing::TestWithParam, but more complicated test hierarchies |
1798 | // may need to inherit from Test and WithParamInterface at different levels. |
1799 | // |
1800 | // This interface has support for accessing the test parameter value via |
1801 | // the GetParam() method. |
1802 | // |
1803 | // Use it with one of the parameter generator defining functions, like Range(), |
1804 | // Values(), ValuesIn(), Bool(), and Combine(). |
1805 | // |
1806 | // class FooTest : public ::testing::TestWithParam<int> { |
1807 | // protected: |
1808 | // FooTest() { |
1809 | // // Can use GetParam() here. |
1810 | // } |
1811 | // ~FooTest() override { |
1812 | // // Can use GetParam() here. |
1813 | // } |
1814 | // void SetUp() override { |
1815 | // // Can use GetParam() here. |
1816 | // } |
1817 | // void TearDown override { |
1818 | // // Can use GetParam() here. |
1819 | // } |
1820 | // }; |
1821 | // TEST_P(FooTest, DoesBar) { |
1822 | // // Can use GetParam() method here. |
1823 | // Foo foo; |
1824 | // ASSERT_TRUE(foo.DoesBar(GetParam())); |
1825 | // } |
1826 | // INSTANTIATE_TEST_SUITE_P(OneToTenRange, FooTest, ::testing::Range(1, 10)); |
1827 | |
1828 | template <typename T> |
1829 | class WithParamInterface { |
1830 | public: |
1831 | typedef T ParamType; |
1832 | virtual ~WithParamInterface() {} |
1833 | |
1834 | // The current parameter value. Is also available in the test fixture's |
1835 | // constructor. |
1836 | static const ParamType& GetParam() { |
1837 | GTEST_CHECK_(parameter_ != nullptr) |
1838 | << "GetParam() can only be called inside a value-parameterized test " |
1839 | << "-- did you intend to write TEST_P instead of TEST_F?" ; |
1840 | return *parameter_; |
1841 | } |
1842 | |
1843 | private: |
1844 | // Sets parameter value. The caller is responsible for making sure the value |
1845 | // remains alive and unchanged throughout the current test. |
1846 | static void SetParam(const ParamType* parameter) { |
1847 | parameter_ = parameter; |
1848 | } |
1849 | |
1850 | // Static value used for accessing parameter during a test lifetime. |
1851 | static const ParamType* parameter_; |
1852 | |
1853 | // TestClass must be a subclass of WithParamInterface<T> and Test. |
1854 | template <class TestClass> friend class internal::ParameterizedTestFactory; |
1855 | }; |
1856 | |
1857 | template <typename T> |
1858 | const T* WithParamInterface<T>::parameter_ = nullptr; |
1859 | |
1860 | // Most value-parameterized classes can ignore the existence of |
1861 | // WithParamInterface, and can just inherit from ::testing::TestWithParam. |
1862 | |
1863 | template <typename T> |
1864 | class TestWithParam : public Test, public WithParamInterface<T> { |
1865 | }; |
1866 | |
1867 | // Macros for indicating success/failure in test code. |
1868 | |
1869 | // Skips test in runtime. |
1870 | // Skipping test aborts current function. |
1871 | // Skipped tests are neither successful nor failed. |
1872 | #define GTEST_SKIP() GTEST_SKIP_("Skipped") |
1873 | |
1874 | // ADD_FAILURE unconditionally adds a failure to the current test. |
1875 | // SUCCEED generates a success - it doesn't automatically make the |
1876 | // current test successful, as a test is only successful when it has |
1877 | // no failure. |
1878 | // |
1879 | // EXPECT_* verifies that a certain condition is satisfied. If not, |
1880 | // it behaves like ADD_FAILURE. In particular: |
1881 | // |
1882 | // EXPECT_TRUE verifies that a Boolean condition is true. |
1883 | // EXPECT_FALSE verifies that a Boolean condition is false. |
1884 | // |
1885 | // FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except |
1886 | // that they will also abort the current function on failure. People |
1887 | // usually want the fail-fast behavior of FAIL and ASSERT_*, but those |
1888 | // writing data-driven tests often find themselves using ADD_FAILURE |
1889 | // and EXPECT_* more. |
1890 | |
1891 | // Generates a nonfatal failure with a generic message. |
1892 | #define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed") |
1893 | |
1894 | // Generates a nonfatal failure at the given source file location with |
1895 | // a generic message. |
1896 | #define ADD_FAILURE_AT(file, line) \ |
1897 | GTEST_MESSAGE_AT_(file, line, "Failed", \ |
1898 | ::testing::TestPartResult::kNonFatalFailure) |
1899 | |
1900 | // Generates a fatal failure with a generic message. |
1901 | #define GTEST_FAIL() GTEST_FATAL_FAILURE_("Failed") |
1902 | |
1903 | // Like GTEST_FAIL(), but at the given source file location. |
1904 | #define GTEST_FAIL_AT(file, line) \ |
1905 | GTEST_MESSAGE_AT_(file, line, "Failed", \ |
1906 | ::testing::TestPartResult::kFatalFailure) |
1907 | |
1908 | // Define this macro to 1 to omit the definition of FAIL(), which is a |
1909 | // generic name and clashes with some other libraries. |
1910 | #if !GTEST_DONT_DEFINE_FAIL |
1911 | # define FAIL() GTEST_FAIL() |
1912 | #endif |
1913 | |
1914 | // Generates a success with a generic message. |
1915 | #define GTEST_SUCCEED() GTEST_SUCCESS_("Succeeded") |
1916 | |
1917 | // Define this macro to 1 to omit the definition of SUCCEED(), which |
1918 | // is a generic name and clashes with some other libraries. |
1919 | #if !GTEST_DONT_DEFINE_SUCCEED |
1920 | # define SUCCEED() GTEST_SUCCEED() |
1921 | #endif |
1922 | |
1923 | // Macros for testing exceptions. |
1924 | // |
1925 | // * {ASSERT|EXPECT}_THROW(statement, expected_exception): |
1926 | // Tests that the statement throws the expected exception. |
1927 | // * {ASSERT|EXPECT}_NO_THROW(statement): |
1928 | // Tests that the statement doesn't throw any exception. |
1929 | // * {ASSERT|EXPECT}_ANY_THROW(statement): |
1930 | // Tests that the statement throws an exception. |
1931 | |
1932 | #define EXPECT_THROW(statement, expected_exception) \ |
1933 | GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_) |
1934 | #define EXPECT_NO_THROW(statement) \ |
1935 | GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_) |
1936 | #define EXPECT_ANY_THROW(statement) \ |
1937 | GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_) |
1938 | #define ASSERT_THROW(statement, expected_exception) \ |
1939 | GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_) |
1940 | #define ASSERT_NO_THROW(statement) \ |
1941 | GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_) |
1942 | #define ASSERT_ANY_THROW(statement) \ |
1943 | GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_) |
1944 | |
1945 | // Boolean assertions. Condition can be either a Boolean expression or an |
1946 | // AssertionResult. For more information on how to use AssertionResult with |
1947 | // these macros see comments on that class. |
1948 | #define EXPECT_TRUE(condition) \ |
1949 | GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \ |
1950 | GTEST_NONFATAL_FAILURE_) |
1951 | #define EXPECT_FALSE(condition) \ |
1952 | GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \ |
1953 | GTEST_NONFATAL_FAILURE_) |
1954 | #define ASSERT_TRUE(condition) \ |
1955 | GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \ |
1956 | GTEST_FATAL_FAILURE_) |
1957 | #define ASSERT_FALSE(condition) \ |
1958 | GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \ |
1959 | GTEST_FATAL_FAILURE_) |
1960 | |
1961 | // Macros for testing equalities and inequalities. |
1962 | // |
1963 | // * {ASSERT|EXPECT}_EQ(v1, v2): Tests that v1 == v2 |
1964 | // * {ASSERT|EXPECT}_NE(v1, v2): Tests that v1 != v2 |
1965 | // * {ASSERT|EXPECT}_LT(v1, v2): Tests that v1 < v2 |
1966 | // * {ASSERT|EXPECT}_LE(v1, v2): Tests that v1 <= v2 |
1967 | // * {ASSERT|EXPECT}_GT(v1, v2): Tests that v1 > v2 |
1968 | // * {ASSERT|EXPECT}_GE(v1, v2): Tests that v1 >= v2 |
1969 | // |
1970 | // When they are not, Google Test prints both the tested expressions and |
1971 | // their actual values. The values must be compatible built-in types, |
1972 | // or you will get a compiler error. By "compatible" we mean that the |
1973 | // values can be compared by the respective operator. |
1974 | // |
1975 | // Note: |
1976 | // |
1977 | // 1. It is possible to make a user-defined type work with |
1978 | // {ASSERT|EXPECT}_??(), but that requires overloading the |
1979 | // comparison operators and is thus discouraged by the Google C++ |
1980 | // Usage Guide. Therefore, you are advised to use the |
1981 | // {ASSERT|EXPECT}_TRUE() macro to assert that two objects are |
1982 | // equal. |
1983 | // |
1984 | // 2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on |
1985 | // pointers (in particular, C strings). Therefore, if you use it |
1986 | // with two C strings, you are testing how their locations in memory |
1987 | // are related, not how their content is related. To compare two C |
1988 | // strings by content, use {ASSERT|EXPECT}_STR*(). |
1989 | // |
1990 | // 3. {ASSERT|EXPECT}_EQ(v1, v2) is preferred to |
1991 | // {ASSERT|EXPECT}_TRUE(v1 == v2), as the former tells you |
1992 | // what the actual value is when it fails, and similarly for the |
1993 | // other comparisons. |
1994 | // |
1995 | // 4. Do not depend on the order in which {ASSERT|EXPECT}_??() |
1996 | // evaluate their arguments, which is undefined. |
1997 | // |
1998 | // 5. These macros evaluate their arguments exactly once. |
1999 | // |
2000 | // Examples: |
2001 | // |
2002 | // EXPECT_NE(Foo(), 5); |
2003 | // EXPECT_EQ(a_pointer, NULL); |
2004 | // ASSERT_LT(i, array_size); |
2005 | // ASSERT_GT(records.size(), 0) << "There is no record left."; |
2006 | |
2007 | #define EXPECT_EQ(val1, val2) \ |
2008 | EXPECT_PRED_FORMAT2(::testing::internal::EqHelper::Compare, val1, val2) |
2009 | #define EXPECT_NE(val1, val2) \ |
2010 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2) |
2011 | #define EXPECT_LE(val1, val2) \ |
2012 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) |
2013 | #define EXPECT_LT(val1, val2) \ |
2014 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) |
2015 | #define EXPECT_GE(val1, val2) \ |
2016 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) |
2017 | #define EXPECT_GT(val1, val2) \ |
2018 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) |
2019 | |
2020 | #define GTEST_ASSERT_EQ(val1, val2) \ |
2021 | ASSERT_PRED_FORMAT2(::testing::internal::EqHelper::Compare, val1, val2) |
2022 | #define GTEST_ASSERT_NE(val1, val2) \ |
2023 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2) |
2024 | #define GTEST_ASSERT_LE(val1, val2) \ |
2025 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) |
2026 | #define GTEST_ASSERT_LT(val1, val2) \ |
2027 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) |
2028 | #define GTEST_ASSERT_GE(val1, val2) \ |
2029 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) |
2030 | #define GTEST_ASSERT_GT(val1, val2) \ |
2031 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) |
2032 | |
2033 | // Define macro GTEST_DONT_DEFINE_ASSERT_XY to 1 to omit the definition of |
2034 | // ASSERT_XY(), which clashes with some users' own code. |
2035 | |
2036 | #if !GTEST_DONT_DEFINE_ASSERT_EQ |
2037 | # define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2) |
2038 | #endif |
2039 | |
2040 | #if !GTEST_DONT_DEFINE_ASSERT_NE |
2041 | # define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2) |
2042 | #endif |
2043 | |
2044 | #if !GTEST_DONT_DEFINE_ASSERT_LE |
2045 | # define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2) |
2046 | #endif |
2047 | |
2048 | #if !GTEST_DONT_DEFINE_ASSERT_LT |
2049 | # define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2) |
2050 | #endif |
2051 | |
2052 | #if !GTEST_DONT_DEFINE_ASSERT_GE |
2053 | # define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2) |
2054 | #endif |
2055 | |
2056 | #if !GTEST_DONT_DEFINE_ASSERT_GT |
2057 | # define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2) |
2058 | #endif |
2059 | |
2060 | // C-string Comparisons. All tests treat NULL and any non-NULL string |
2061 | // as different. Two NULLs are equal. |
2062 | // |
2063 | // * {ASSERT|EXPECT}_STREQ(s1, s2): Tests that s1 == s2 |
2064 | // * {ASSERT|EXPECT}_STRNE(s1, s2): Tests that s1 != s2 |
2065 | // * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case |
2066 | // * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case |
2067 | // |
2068 | // For wide or narrow string objects, you can use the |
2069 | // {ASSERT|EXPECT}_??() macros. |
2070 | // |
2071 | // Don't depend on the order in which the arguments are evaluated, |
2072 | // which is undefined. |
2073 | // |
2074 | // These macros evaluate their arguments exactly once. |
2075 | |
2076 | #define EXPECT_STREQ(s1, s2) \ |
2077 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2) |
2078 | #define EXPECT_STRNE(s1, s2) \ |
2079 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2) |
2080 | #define EXPECT_STRCASEEQ(s1, s2) \ |
2081 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, s1, s2) |
2082 | #define EXPECT_STRCASENE(s1, s2)\ |
2083 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2) |
2084 | |
2085 | #define ASSERT_STREQ(s1, s2) \ |
2086 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2) |
2087 | #define ASSERT_STRNE(s1, s2) \ |
2088 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2) |
2089 | #define ASSERT_STRCASEEQ(s1, s2) \ |
2090 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, s1, s2) |
2091 | #define ASSERT_STRCASENE(s1, s2)\ |
2092 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2) |
2093 | |
2094 | // Macros for comparing floating-point numbers. |
2095 | // |
2096 | // * {ASSERT|EXPECT}_FLOAT_EQ(val1, val2): |
2097 | // Tests that two float values are almost equal. |
2098 | // * {ASSERT|EXPECT}_DOUBLE_EQ(val1, val2): |
2099 | // Tests that two double values are almost equal. |
2100 | // * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error): |
2101 | // Tests that v1 and v2 are within the given distance to each other. |
2102 | // |
2103 | // Google Test uses ULP-based comparison to automatically pick a default |
2104 | // error bound that is appropriate for the operands. See the |
2105 | // FloatingPoint template class in gtest-internal.h if you are |
2106 | // interested in the implementation details. |
2107 | |
2108 | #define EXPECT_FLOAT_EQ(val1, val2)\ |
2109 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \ |
2110 | val1, val2) |
2111 | |
2112 | #define EXPECT_DOUBLE_EQ(val1, val2)\ |
2113 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \ |
2114 | val1, val2) |
2115 | |
2116 | #define ASSERT_FLOAT_EQ(val1, val2)\ |
2117 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \ |
2118 | val1, val2) |
2119 | |
2120 | #define ASSERT_DOUBLE_EQ(val1, val2)\ |
2121 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \ |
2122 | val1, val2) |
2123 | |
2124 | #define EXPECT_NEAR(val1, val2, abs_error)\ |
2125 | EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \ |
2126 | val1, val2, abs_error) |
2127 | |
2128 | #define ASSERT_NEAR(val1, val2, abs_error)\ |
2129 | ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \ |
2130 | val1, val2, abs_error) |
2131 | |
2132 | // These predicate format functions work on floating-point values, and |
2133 | // can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g. |
2134 | // |
2135 | // EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0); |
2136 | |
2137 | // Asserts that val1 is less than, or almost equal to, val2. Fails |
2138 | // otherwise. In particular, it fails if either val1 or val2 is NaN. |
2139 | GTEST_API_ AssertionResult FloatLE(const char* expr1, const char* expr2, |
2140 | float val1, float val2); |
2141 | GTEST_API_ AssertionResult DoubleLE(const char* expr1, const char* expr2, |
2142 | double val1, double val2); |
2143 | |
2144 | |
2145 | #if GTEST_OS_WINDOWS |
2146 | |
2147 | // Macros that test for HRESULT failure and success, these are only useful |
2148 | // on Windows, and rely on Windows SDK macros and APIs to compile. |
2149 | // |
2150 | // * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr) |
2151 | // |
2152 | // When expr unexpectedly fails or succeeds, Google Test prints the |
2153 | // expected result and the actual result with both a human-readable |
2154 | // string representation of the error, if available, as well as the |
2155 | // hex result code. |
2156 | # define EXPECT_HRESULT_SUCCEEDED(expr) \ |
2157 | EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) |
2158 | |
2159 | # define ASSERT_HRESULT_SUCCEEDED(expr) \ |
2160 | ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) |
2161 | |
2162 | # define EXPECT_HRESULT_FAILED(expr) \ |
2163 | EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) |
2164 | |
2165 | # define ASSERT_HRESULT_FAILED(expr) \ |
2166 | ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) |
2167 | |
2168 | #endif // GTEST_OS_WINDOWS |
2169 | |
2170 | // Macros that execute statement and check that it doesn't generate new fatal |
2171 | // failures in the current thread. |
2172 | // |
2173 | // * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement); |
2174 | // |
2175 | // Examples: |
2176 | // |
2177 | // EXPECT_NO_FATAL_FAILURE(Process()); |
2178 | // ASSERT_NO_FATAL_FAILURE(Process()) << "Process() failed"; |
2179 | // |
2180 | #define ASSERT_NO_FATAL_FAILURE(statement) \ |
2181 | GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_FATAL_FAILURE_) |
2182 | #define EXPECT_NO_FATAL_FAILURE(statement) \ |
2183 | GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_NONFATAL_FAILURE_) |
2184 | |
2185 | // Causes a trace (including the given source file path and line number, |
2186 | // and the given message) to be included in every test failure message generated |
2187 | // by code in the scope of the lifetime of an instance of this class. The effect |
2188 | // is undone with the destruction of the instance. |
2189 | // |
2190 | // The message argument can be anything streamable to std::ostream. |
2191 | // |
2192 | // Example: |
2193 | // testing::ScopedTrace trace("file.cc", 123, "message"); |
2194 | // |
2195 | class GTEST_API_ ScopedTrace { |
2196 | public: |
2197 | // The c'tor pushes the given source file location and message onto |
2198 | // a trace stack maintained by Google Test. |
2199 | |
2200 | // Template version. Uses Message() to convert the values into strings. |
2201 | // Slow, but flexible. |
2202 | template <typename T> |
2203 | ScopedTrace(const char* file, int line, const T& message) { |
2204 | PushTrace(file, line, (Message() << message).GetString()); |
2205 | } |
2206 | |
2207 | // Optimize for some known types. |
2208 | ScopedTrace(const char* file, int line, const char* message) { |
2209 | PushTrace(file, line, message ? message : "(null)" ); |
2210 | } |
2211 | |
2212 | ScopedTrace(const char* file, int line, const std::string& message) { |
2213 | PushTrace(file, line, message); |
2214 | } |
2215 | |
2216 | // The d'tor pops the info pushed by the c'tor. |
2217 | // |
2218 | // Note that the d'tor is not virtual in order to be efficient. |
2219 | // Don't inherit from ScopedTrace! |
2220 | ~ScopedTrace(); |
2221 | |
2222 | private: |
2223 | void PushTrace(const char* file, int line, std::string message); |
2224 | |
2225 | GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedTrace); |
2226 | } GTEST_ATTRIBUTE_UNUSED_; // A ScopedTrace object does its job in its |
2227 | // c'tor and d'tor. Therefore it doesn't |
2228 | // need to be used otherwise. |
2229 | |
2230 | // Causes a trace (including the source file path, the current line |
2231 | // number, and the given message) to be included in every test failure |
2232 | // message generated by code in the current scope. The effect is |
2233 | // undone when the control leaves the current scope. |
2234 | // |
2235 | // The message argument can be anything streamable to std::ostream. |
2236 | // |
2237 | // In the implementation, we include the current line number as part |
2238 | // of the dummy variable name, thus allowing multiple SCOPED_TRACE()s |
2239 | // to appear in the same block - as long as they are on different |
2240 | // lines. |
2241 | // |
2242 | // Assuming that each thread maintains its own stack of traces. |
2243 | // Therefore, a SCOPED_TRACE() would (correctly) only affect the |
2244 | // assertions in its own thread. |
2245 | #define SCOPED_TRACE(message) \ |
2246 | ::testing::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)(\ |
2247 | __FILE__, __LINE__, (message)) |
2248 | |
2249 | |
2250 | // Compile-time assertion for type equality. |
2251 | // StaticAssertTypeEq<type1, type2>() compiles iff type1 and type2 are |
2252 | // the same type. The value it returns is not interesting. |
2253 | // |
2254 | // Instead of making StaticAssertTypeEq a class template, we make it a |
2255 | // function template that invokes a helper class template. This |
2256 | // prevents a user from misusing StaticAssertTypeEq<T1, T2> by |
2257 | // defining objects of that type. |
2258 | // |
2259 | // CAVEAT: |
2260 | // |
2261 | // When used inside a method of a class template, |
2262 | // StaticAssertTypeEq<T1, T2>() is effective ONLY IF the method is |
2263 | // instantiated. For example, given: |
2264 | // |
2265 | // template <typename T> class Foo { |
2266 | // public: |
2267 | // void Bar() { testing::StaticAssertTypeEq<int, T>(); } |
2268 | // }; |
2269 | // |
2270 | // the code: |
2271 | // |
2272 | // void Test1() { Foo<bool> foo; } |
2273 | // |
2274 | // will NOT generate a compiler error, as Foo<bool>::Bar() is never |
2275 | // actually instantiated. Instead, you need: |
2276 | // |
2277 | // void Test2() { Foo<bool> foo; foo.Bar(); } |
2278 | // |
2279 | // to cause a compiler error. |
2280 | template <typename T1, typename T2> |
2281 | bool StaticAssertTypeEq() { |
2282 | (void)internal::StaticAssertTypeEqHelper<T1, T2>(); |
2283 | return true; |
2284 | } |
2285 | |
2286 | // Defines a test. |
2287 | // |
2288 | // The first parameter is the name of the test suite, and the second |
2289 | // parameter is the name of the test within the test suite. |
2290 | // |
2291 | // The convention is to end the test suite name with "Test". For |
2292 | // example, a test suite for the Foo class can be named FooTest. |
2293 | // |
2294 | // Test code should appear between braces after an invocation of |
2295 | // this macro. Example: |
2296 | // |
2297 | // TEST(FooTest, InitializesCorrectly) { |
2298 | // Foo foo; |
2299 | // EXPECT_TRUE(foo.StatusIsOK()); |
2300 | // } |
2301 | |
2302 | // Note that we call GetTestTypeId() instead of GetTypeId< |
2303 | // ::testing::Test>() here to get the type ID of testing::Test. This |
2304 | // is to work around a suspected linker bug when using Google Test as |
2305 | // a framework on Mac OS X. The bug causes GetTypeId< |
2306 | // ::testing::Test>() to return different values depending on whether |
2307 | // the call is from the Google Test framework itself or from user test |
2308 | // code. GetTestTypeId() is guaranteed to always return the same |
2309 | // value, as it always calls GetTypeId<>() from the Google Test |
2310 | // framework. |
2311 | #define GTEST_TEST(test_suite_name, test_name) \ |
2312 | GTEST_TEST_(test_suite_name, test_name, ::testing::Test, \ |
2313 | ::testing::internal::GetTestTypeId()) |
2314 | |
2315 | // Define this macro to 1 to omit the definition of TEST(), which |
2316 | // is a generic name and clashes with some other libraries. |
2317 | #if !GTEST_DONT_DEFINE_TEST |
2318 | #define TEST(test_suite_name, test_name) GTEST_TEST(test_suite_name, test_name) |
2319 | #endif |
2320 | |
2321 | // Defines a test that uses a test fixture. |
2322 | // |
2323 | // The first parameter is the name of the test fixture class, which |
2324 | // also doubles as the test suite name. The second parameter is the |
2325 | // name of the test within the test suite. |
2326 | // |
2327 | // A test fixture class must be declared earlier. The user should put |
2328 | // the test code between braces after using this macro. Example: |
2329 | // |
2330 | // class FooTest : public testing::Test { |
2331 | // protected: |
2332 | // void SetUp() override { b_.AddElement(3); } |
2333 | // |
2334 | // Foo a_; |
2335 | // Foo b_; |
2336 | // }; |
2337 | // |
2338 | // TEST_F(FooTest, InitializesCorrectly) { |
2339 | // EXPECT_TRUE(a_.StatusIsOK()); |
2340 | // } |
2341 | // |
2342 | // TEST_F(FooTest, ReturnsElementCountCorrectly) { |
2343 | // EXPECT_EQ(a_.size(), 0); |
2344 | // EXPECT_EQ(b_.size(), 1); |
2345 | // } |
2346 | // |
2347 | // GOOGLETEST_CM0011 DO NOT DELETE |
2348 | #define TEST_F(test_fixture, test_name)\ |
2349 | GTEST_TEST_(test_fixture, test_name, test_fixture, \ |
2350 | ::testing::internal::GetTypeId<test_fixture>()) |
2351 | |
2352 | // Returns a path to temporary directory. |
2353 | // Tries to determine an appropriate directory for the platform. |
2354 | GTEST_API_ std::string TempDir(); |
2355 | |
2356 | #ifdef _MSC_VER |
2357 | # pragma warning(pop) |
2358 | #endif |
2359 | |
2360 | // Dynamically registers a test with the framework. |
2361 | // |
2362 | // This is an advanced API only to be used when the `TEST` macros are |
2363 | // insufficient. The macros should be preferred when possible, as they avoid |
2364 | // most of the complexity of calling this function. |
2365 | // |
2366 | // The `factory` argument is a factory callable (move-constructible) object or |
2367 | // function pointer that creates a new instance of the Test object. It |
2368 | // handles ownership to the caller. The signature of the callable is |
2369 | // `Fixture*()`, where `Fixture` is the test fixture class for the test. All |
2370 | // tests registered with the same `test_suite_name` must return the same |
2371 | // fixture type. This is checked at runtime. |
2372 | // |
2373 | // The framework will infer the fixture class from the factory and will call |
2374 | // the `SetUpTestSuite` and `TearDownTestSuite` for it. |
2375 | // |
2376 | // Must be called before `RUN_ALL_TESTS()` is invoked, otherwise behavior is |
2377 | // undefined. |
2378 | // |
2379 | // Use case example: |
2380 | // |
2381 | // class MyFixture : public ::testing::Test { |
2382 | // public: |
2383 | // // All of these optional, just like in regular macro usage. |
2384 | // static void SetUpTestSuite() { ... } |
2385 | // static void TearDownTestSuite() { ... } |
2386 | // void SetUp() override { ... } |
2387 | // void TearDown() override { ... } |
2388 | // }; |
2389 | // |
2390 | // class MyTest : public MyFixture { |
2391 | // public: |
2392 | // explicit MyTest(int data) : data_(data) {} |
2393 | // void TestBody() override { ... } |
2394 | // |
2395 | // private: |
2396 | // int data_; |
2397 | // }; |
2398 | // |
2399 | // void RegisterMyTests(const std::vector<int>& values) { |
2400 | // for (int v : values) { |
2401 | // ::testing::RegisterTest( |
2402 | // "MyFixture", ("Test" + std::to_string(v)).c_str(), nullptr, |
2403 | // std::to_string(v).c_str(), |
2404 | // __FILE__, __LINE__, |
2405 | // // Important to use the fixture type as the return type here. |
2406 | // [=]() -> MyFixture* { return new MyTest(v); }); |
2407 | // } |
2408 | // } |
2409 | // ... |
2410 | // int main(int argc, char** argv) { |
2411 | // std::vector<int> values_to_test = LoadValuesFromConfig(); |
2412 | // RegisterMyTests(values_to_test); |
2413 | // ... |
2414 | // return RUN_ALL_TESTS(); |
2415 | // } |
2416 | // |
2417 | template <int&... ExplicitParameterBarrier, typename Factory> |
2418 | TestInfo* RegisterTest(const char* test_suite_name, const char* test_name, |
2419 | const char* type_param, const char* value_param, |
2420 | const char* file, int line, Factory factory) { |
2421 | using TestT = typename std::remove_pointer<decltype(factory())>::type; |
2422 | |
2423 | class FactoryImpl : public internal::TestFactoryBase { |
2424 | public: |
2425 | explicit FactoryImpl(Factory f) : factory_(std::move(f)) {} |
2426 | Test* CreateTest() override { return factory_(); } |
2427 | |
2428 | private: |
2429 | Factory factory_; |
2430 | }; |
2431 | |
2432 | return internal::MakeAndRegisterTestInfo( |
2433 | test_suite_name, test_name, type_param, value_param, |
2434 | internal::CodeLocation(file, line), internal::GetTypeId<TestT>(), |
2435 | internal::SuiteApiResolver<TestT>::GetSetUpCaseOrSuite(file, line), |
2436 | internal::SuiteApiResolver<TestT>::GetTearDownCaseOrSuite(file, line), |
2437 | new FactoryImpl{std::move(factory)}); |
2438 | } |
2439 | |
2440 | } // namespace testing |
2441 | |
2442 | // Use this function in main() to run all tests. It returns 0 if all |
2443 | // tests are successful, or 1 otherwise. |
2444 | // |
2445 | // RUN_ALL_TESTS() should be invoked after the command line has been |
2446 | // parsed by InitGoogleTest(). |
2447 | // |
2448 | // This function was formerly a macro; thus, it is in the global |
2449 | // namespace and has an all-caps name. |
2450 | int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_; |
2451 | |
2452 | inline int RUN_ALL_TESTS() { |
2453 | return ::testing::UnitTest::GetInstance()->Run(); |
2454 | } |
2455 | |
2456 | GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 |
2457 | |
2458 | #endif // GTEST_INCLUDE_GTEST_GTEST_H_ |
2459 | |