1b89a7cc2SEnji Cooper // Copyright 2005, Google Inc.
2b89a7cc2SEnji Cooper // All rights reserved.
3b89a7cc2SEnji Cooper //
4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6b89a7cc2SEnji Cooper // met:
7b89a7cc2SEnji Cooper //
8b89a7cc2SEnji Cooper //     * Redistributions of source code must retain the above copyright
9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10b89a7cc2SEnji Cooper //     * Redistributions in binary form must reproduce the above
11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13b89a7cc2SEnji Cooper // distribution.
14b89a7cc2SEnji Cooper //     * Neither the name of Google Inc. nor the names of its
15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16b89a7cc2SEnji Cooper // this software without specific prior written permission.
17b89a7cc2SEnji Cooper //
18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b89a7cc2SEnji Cooper 
30b89a7cc2SEnji Cooper // The Google C++ Testing and Mocking Framework (Google Test)
31b89a7cc2SEnji Cooper //
32b89a7cc2SEnji Cooper // This header file defines the public API for Google Test.  It should be
33b89a7cc2SEnji Cooper // included by any test program that uses Google Test.
34b89a7cc2SEnji Cooper //
35b89a7cc2SEnji Cooper // IMPORTANT NOTE: Due to limitation of the C++ language, we have to
36b89a7cc2SEnji Cooper // leave some internal implementation details in this header file.
37b89a7cc2SEnji Cooper // They are clearly marked by comments like this:
38b89a7cc2SEnji Cooper //
39b89a7cc2SEnji Cooper //   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
40b89a7cc2SEnji Cooper //
41b89a7cc2SEnji Cooper // Such code is NOT meant to be used by a user directly, and is subject
42b89a7cc2SEnji Cooper // to CHANGE WITHOUT NOTICE.  Therefore DO NOT DEPEND ON IT in a user
43b89a7cc2SEnji Cooper // program!
44b89a7cc2SEnji Cooper //
45b89a7cc2SEnji Cooper // Acknowledgment: Google Test borrowed the idea of automatic test
46b89a7cc2SEnji Cooper // registration from Barthelemy Dagenais' (barthelemy@prologique.com)
47b89a7cc2SEnji Cooper // easyUnit framework.
48b89a7cc2SEnji Cooper 
4928f6c2f2SEnji Cooper #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_H_
5028f6c2f2SEnji Cooper #define GOOGLETEST_INCLUDE_GTEST_GTEST_H_
51b89a7cc2SEnji Cooper 
5228f6c2f2SEnji Cooper #include <cstddef>
5328f6c2f2SEnji Cooper #include <cstdint>
5428f6c2f2SEnji Cooper #include <iomanip>
55b89a7cc2SEnji Cooper #include <limits>
5628f6c2f2SEnji Cooper #include <memory>
57b89a7cc2SEnji Cooper #include <ostream>
5828f6c2f2SEnji Cooper #include <set>
5928f6c2f2SEnji Cooper #include <sstream>
6028f6c2f2SEnji Cooper #include <string>
6128f6c2f2SEnji Cooper #include <type_traits>
62b89a7cc2SEnji Cooper #include <vector>
63b89a7cc2SEnji Cooper 
6428f6c2f2SEnji Cooper #include "gtest/gtest-assertion-result.h"
65b89a7cc2SEnji Cooper #include "gtest/gtest-death-test.h"
6628f6c2f2SEnji Cooper #include "gtest/gtest-matchers.h"
67b89a7cc2SEnji Cooper #include "gtest/gtest-message.h"
68b89a7cc2SEnji Cooper #include "gtest/gtest-param-test.h"
69b89a7cc2SEnji Cooper #include "gtest/gtest-printers.h"
70b89a7cc2SEnji Cooper #include "gtest/gtest-test-part.h"
71b89a7cc2SEnji Cooper #include "gtest/gtest-typed-test.h"
7228f6c2f2SEnji Cooper #include "gtest/gtest_pred_impl.h"
7328f6c2f2SEnji Cooper #include "gtest/gtest_prod.h"
7428f6c2f2SEnji Cooper #include "gtest/internal/gtest-internal.h"
7528f6c2f2SEnji Cooper #include "gtest/internal/gtest-string.h"
76b89a7cc2SEnji Cooper 
77b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
78b89a7cc2SEnji Cooper /* class A needs to have dll-interface to be used by clients of class B */)
79b89a7cc2SEnji Cooper 
80b89a7cc2SEnji Cooper // Declares the flags.
81b89a7cc2SEnji Cooper 
82b89a7cc2SEnji Cooper // This flag temporary enables the disabled tests.
83b89a7cc2SEnji Cooper GTEST_DECLARE_bool_(also_run_disabled_tests);
84b89a7cc2SEnji Cooper 
85b89a7cc2SEnji Cooper // This flag brings the debugger on an assertion failure.
86b89a7cc2SEnji Cooper GTEST_DECLARE_bool_(break_on_failure);
87b89a7cc2SEnji Cooper 
88b89a7cc2SEnji Cooper // This flag controls whether Google Test catches all test-thrown exceptions
89b89a7cc2SEnji Cooper // and logs them as failures.
90b89a7cc2SEnji Cooper GTEST_DECLARE_bool_(catch_exceptions);
91b89a7cc2SEnji Cooper 
92b89a7cc2SEnji Cooper // This flag enables using colors in terminal output. Available values are
93b89a7cc2SEnji Cooper // "yes" to enable colors, "no" (disable colors), or "auto" (the default)
94b89a7cc2SEnji Cooper // to let Google Test decide.
95b89a7cc2SEnji Cooper GTEST_DECLARE_string_(color);
96b89a7cc2SEnji Cooper 
9728f6c2f2SEnji Cooper // This flag controls whether the test runner should continue execution past
9828f6c2f2SEnji Cooper // first failure.
9928f6c2f2SEnji Cooper GTEST_DECLARE_bool_(fail_fast);
10028f6c2f2SEnji Cooper 
101b89a7cc2SEnji Cooper // This flag sets up the filter to select by name using a glob pattern
102b89a7cc2SEnji Cooper // the tests to run. If the filter is not given all tests are executed.
103b89a7cc2SEnji Cooper GTEST_DECLARE_string_(filter);
104b89a7cc2SEnji Cooper 
105b89a7cc2SEnji Cooper // This flag controls whether Google Test installs a signal handler that dumps
106b89a7cc2SEnji Cooper // debugging information when fatal signals are raised.
107b89a7cc2SEnji Cooper GTEST_DECLARE_bool_(install_failure_signal_handler);
108b89a7cc2SEnji Cooper 
109b89a7cc2SEnji Cooper // This flag causes the Google Test to list tests. None of the tests listed
110b89a7cc2SEnji Cooper // are actually run if the flag is provided.
111b89a7cc2SEnji Cooper GTEST_DECLARE_bool_(list_tests);
112b89a7cc2SEnji Cooper 
113b89a7cc2SEnji Cooper // This flag controls whether Google Test emits a detailed XML report to a file
114b89a7cc2SEnji Cooper // in addition to its normal textual output.
115b89a7cc2SEnji Cooper GTEST_DECLARE_string_(output);
116b89a7cc2SEnji Cooper 
11728f6c2f2SEnji Cooper // This flags control whether Google Test prints only test failures.
11828f6c2f2SEnji Cooper GTEST_DECLARE_bool_(brief);
11928f6c2f2SEnji Cooper 
120b89a7cc2SEnji Cooper // This flags control whether Google Test prints the elapsed time for each
121b89a7cc2SEnji Cooper // test.
122b89a7cc2SEnji Cooper GTEST_DECLARE_bool_(print_time);
123b89a7cc2SEnji Cooper 
124b89a7cc2SEnji Cooper // This flags control whether Google Test prints UTF8 characters as text.
125b89a7cc2SEnji Cooper GTEST_DECLARE_bool_(print_utf8);
126b89a7cc2SEnji Cooper 
127b89a7cc2SEnji Cooper // This flag specifies the random number seed.
128b89a7cc2SEnji Cooper GTEST_DECLARE_int32_(random_seed);
129b89a7cc2SEnji Cooper 
130b89a7cc2SEnji Cooper // This flag sets how many times the tests are repeated. The default value
131b89a7cc2SEnji Cooper // is 1. If the value is -1 the tests are repeating forever.
132b89a7cc2SEnji Cooper GTEST_DECLARE_int32_(repeat);
133b89a7cc2SEnji Cooper 
13428f6c2f2SEnji Cooper // This flag controls whether Google Test Environments are recreated for each
13528f6c2f2SEnji Cooper // repeat of the tests. The default value is true. If set to false the global
13628f6c2f2SEnji Cooper // test Environment objects are only set up once, for the first iteration, and
13728f6c2f2SEnji Cooper // only torn down once, for the last.
13828f6c2f2SEnji Cooper GTEST_DECLARE_bool_(recreate_environments_when_repeating);
13928f6c2f2SEnji Cooper 
140b89a7cc2SEnji Cooper // This flag controls whether Google Test includes Google Test internal
141b89a7cc2SEnji Cooper // stack frames in failure stack traces.
142b89a7cc2SEnji Cooper GTEST_DECLARE_bool_(show_internal_stack_frames);
143b89a7cc2SEnji Cooper 
144b89a7cc2SEnji Cooper // When this flag is specified, tests' order is randomized on every iteration.
145b89a7cc2SEnji Cooper GTEST_DECLARE_bool_(shuffle);
146b89a7cc2SEnji Cooper 
147b89a7cc2SEnji Cooper // This flag specifies the maximum number of stack frames to be
148b89a7cc2SEnji Cooper // printed in a failure message.
149b89a7cc2SEnji Cooper GTEST_DECLARE_int32_(stack_trace_depth);
150b89a7cc2SEnji Cooper 
151b89a7cc2SEnji Cooper // When this flag is specified, a failed assertion will throw an
152b89a7cc2SEnji Cooper // exception if exceptions are enabled, or exit the program with a
153b89a7cc2SEnji Cooper // non-zero code otherwise. For use with an external test framework.
154b89a7cc2SEnji Cooper GTEST_DECLARE_bool_(throw_on_failure);
155b89a7cc2SEnji Cooper 
156b89a7cc2SEnji Cooper // When this flag is set with a "host:port" string, on supported
157b89a7cc2SEnji Cooper // platforms test results are streamed to the specified port on
158b89a7cc2SEnji Cooper // the specified host machine.
159b89a7cc2SEnji Cooper GTEST_DECLARE_string_(stream_result_to);
160b89a7cc2SEnji Cooper 
161b89a7cc2SEnji Cooper #if GTEST_USE_OWN_FLAGFILE_FLAG_
162b89a7cc2SEnji Cooper GTEST_DECLARE_string_(flagfile);
163b89a7cc2SEnji Cooper #endif  // GTEST_USE_OWN_FLAGFILE_FLAG_
164b89a7cc2SEnji Cooper 
16528f6c2f2SEnji Cooper namespace testing {
16628f6c2f2SEnji Cooper 
16728f6c2f2SEnji Cooper // Silence C4100 (unreferenced formal parameter) and 4805
16828f6c2f2SEnji Cooper // unsafe mix of type 'const int' and type 'const bool'
16928f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4805 4100)
17028f6c2f2SEnji Cooper 
171b89a7cc2SEnji Cooper // The upper limit for valid stack trace depths.
172b89a7cc2SEnji Cooper const int kMaxStackTraceDepth = 100;
173b89a7cc2SEnji Cooper 
174b89a7cc2SEnji Cooper namespace internal {
175b89a7cc2SEnji Cooper 
176b89a7cc2SEnji Cooper class AssertHelper;
177b89a7cc2SEnji Cooper class DefaultGlobalTestPartResultReporter;
178b89a7cc2SEnji Cooper class ExecDeathTest;
179b89a7cc2SEnji Cooper class NoExecDeathTest;
180b89a7cc2SEnji Cooper class FinalSuccessChecker;
181b89a7cc2SEnji Cooper class GTestFlagSaver;
182b89a7cc2SEnji Cooper class StreamingListenerTest;
183b89a7cc2SEnji Cooper class TestResultAccessor;
184b89a7cc2SEnji Cooper class TestEventListenersAccessor;
185b89a7cc2SEnji Cooper class TestEventRepeater;
186b89a7cc2SEnji Cooper class UnitTestRecordPropertyTestHelper;
187b89a7cc2SEnji Cooper class WindowsDeathTest;
188b89a7cc2SEnji Cooper class FuchsiaDeathTest;
189b89a7cc2SEnji Cooper class UnitTestImpl* GetUnitTestImpl();
190b89a7cc2SEnji Cooper void ReportFailureInUnknownLocation(TestPartResult::Type result_type,
191b89a7cc2SEnji Cooper                                     const std::string& message);
19228f6c2f2SEnji Cooper std::set<std::string>* GetIgnoredParameterizedTestSuites();
19328f6c2f2SEnji Cooper 
19428f6c2f2SEnji Cooper // A base class that prevents subclasses from being copyable.
19528f6c2f2SEnji Cooper // We do this instead of using '= delete' so as to avoid triggering warnings
19628f6c2f2SEnji Cooper // inside user code regarding any of our declarations.
19728f6c2f2SEnji Cooper class GTestNonCopyable {
19828f6c2f2SEnji Cooper  public:
19928f6c2f2SEnji Cooper   GTestNonCopyable() = default;
20028f6c2f2SEnji Cooper   GTestNonCopyable(const GTestNonCopyable&) = delete;
20128f6c2f2SEnji Cooper   GTestNonCopyable& operator=(const GTestNonCopyable&) = delete;
20228f6c2f2SEnji Cooper   ~GTestNonCopyable() = default;
20328f6c2f2SEnji Cooper };
204b89a7cc2SEnji Cooper 
205b89a7cc2SEnji Cooper }  // namespace internal
206b89a7cc2SEnji Cooper 
207b89a7cc2SEnji Cooper // The friend relationship of some of these classes is cyclic.
208b89a7cc2SEnji Cooper // If we don't forward declare them the compiler might confuse the classes
209b89a7cc2SEnji Cooper // in friendship clauses with same named classes on the scope.
210b89a7cc2SEnji Cooper class Test;
21128f6c2f2SEnji Cooper class TestSuite;
21228f6c2f2SEnji Cooper 
21328f6c2f2SEnji Cooper // Old API is still available but deprecated
21428f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
21528f6c2f2SEnji Cooper using TestCase = TestSuite;
21628f6c2f2SEnji Cooper #endif
217b89a7cc2SEnji Cooper class TestInfo;
218b89a7cc2SEnji Cooper class UnitTest;
219b89a7cc2SEnji Cooper 
220b89a7cc2SEnji Cooper // The abstract class that all tests inherit from.
221b89a7cc2SEnji Cooper //
22228f6c2f2SEnji Cooper // In Google Test, a unit test program contains one or many TestSuites, and
22328f6c2f2SEnji Cooper // each TestSuite contains one or many Tests.
224b89a7cc2SEnji Cooper //
225b89a7cc2SEnji Cooper // When you define a test using the TEST macro, you don't need to
226b89a7cc2SEnji Cooper // explicitly derive from Test - the TEST macro automatically does
227b89a7cc2SEnji Cooper // this for you.
228b89a7cc2SEnji Cooper //
229b89a7cc2SEnji Cooper // The only time you derive from Test is when defining a test fixture
230b89a7cc2SEnji Cooper // to be used in a TEST_F.  For example:
231b89a7cc2SEnji Cooper //
232b89a7cc2SEnji Cooper //   class FooTest : public testing::Test {
233b89a7cc2SEnji Cooper //    protected:
234b89a7cc2SEnji Cooper //     void SetUp() override { ... }
235b89a7cc2SEnji Cooper //     void TearDown() override { ... }
236b89a7cc2SEnji Cooper //     ...
237b89a7cc2SEnji Cooper //   };
238b89a7cc2SEnji Cooper //
239b89a7cc2SEnji Cooper //   TEST_F(FooTest, Bar) { ... }
240b89a7cc2SEnji Cooper //   TEST_F(FooTest, Baz) { ... }
241b89a7cc2SEnji Cooper //
242b89a7cc2SEnji Cooper // Test is not copyable.
243b89a7cc2SEnji Cooper class GTEST_API_ Test {
244b89a7cc2SEnji Cooper  public:
245b89a7cc2SEnji Cooper   friend class TestInfo;
246b89a7cc2SEnji Cooper 
247b89a7cc2SEnji Cooper   // The d'tor is virtual as we intend to inherit from Test.
248b89a7cc2SEnji Cooper   virtual ~Test();
249b89a7cc2SEnji Cooper 
25028f6c2f2SEnji Cooper   // Sets up the stuff shared by all tests in this test suite.
251b89a7cc2SEnji Cooper   //
25228f6c2f2SEnji Cooper   // Google Test will call Foo::SetUpTestSuite() before running the first
25328f6c2f2SEnji Cooper   // test in test suite Foo.  Hence a sub-class can define its own
25428f6c2f2SEnji Cooper   // SetUpTestSuite() method to shadow the one defined in the super
255b89a7cc2SEnji Cooper   // class.
SetUpTestSuite()25628f6c2f2SEnji Cooper   static void SetUpTestSuite() {}
257b89a7cc2SEnji Cooper 
25828f6c2f2SEnji Cooper   // Tears down the stuff shared by all tests in this test suite.
259b89a7cc2SEnji Cooper   //
26028f6c2f2SEnji Cooper   // Google Test will call Foo::TearDownTestSuite() after running the last
26128f6c2f2SEnji Cooper   // test in test suite Foo.  Hence a sub-class can define its own
26228f6c2f2SEnji Cooper   // TearDownTestSuite() method to shadow the one defined in the super
263b89a7cc2SEnji Cooper   // class.
TearDownTestSuite()26428f6c2f2SEnji Cooper   static void TearDownTestSuite() {}
26528f6c2f2SEnji Cooper 
26628f6c2f2SEnji Cooper   // Legacy API is deprecated but still available. Use SetUpTestSuite and
26728f6c2f2SEnji Cooper   // TearDownTestSuite instead.
26828f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
TearDownTestCase()269b89a7cc2SEnji Cooper   static void TearDownTestCase() {}
SetUpTestCase()27028f6c2f2SEnji Cooper   static void SetUpTestCase() {}
27128f6c2f2SEnji Cooper #endif  // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
272b89a7cc2SEnji Cooper 
27328f6c2f2SEnji Cooper   // Returns true if and only if the current test has a fatal failure.
274b89a7cc2SEnji Cooper   static bool HasFatalFailure();
275b89a7cc2SEnji Cooper 
27628f6c2f2SEnji Cooper   // Returns true if and only if the current test has a non-fatal failure.
277b89a7cc2SEnji Cooper   static bool HasNonfatalFailure();
278b89a7cc2SEnji Cooper 
27928f6c2f2SEnji Cooper   // Returns true if and only if the current test was skipped.
280da1a9eb0SAlan Somers   static bool IsSkipped();
281da1a9eb0SAlan Somers 
28228f6c2f2SEnji Cooper   // Returns true if and only if the current test has a (either fatal or
283b89a7cc2SEnji Cooper   // non-fatal) failure.
HasFailure()284b89a7cc2SEnji Cooper   static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); }
285b89a7cc2SEnji Cooper 
28628f6c2f2SEnji Cooper   // Logs a property for the current test, test suite, or for the entire
287b89a7cc2SEnji Cooper   // invocation of the test program when used outside of the context of a
28828f6c2f2SEnji Cooper   // test suite.  Only the last value for a given key is remembered.  These
289b89a7cc2SEnji Cooper   // are public static so they can be called from utility functions that are
290b89a7cc2SEnji Cooper   // not members of the test fixture.  Calls to RecordProperty made during
291b89a7cc2SEnji Cooper   // lifespan of the test (from the moment its constructor starts to the
292b89a7cc2SEnji Cooper   // moment its destructor finishes) will be output in XML as attributes of
293b89a7cc2SEnji Cooper   // the <testcase> element.  Properties recorded from fixture's
29428f6c2f2SEnji Cooper   // SetUpTestSuite or TearDownTestSuite are logged as attributes of the
295b89a7cc2SEnji Cooper   // corresponding <testsuite> element.  Calls to RecordProperty made in the
296b89a7cc2SEnji Cooper   // global context (before or after invocation of RUN_ALL_TESTS and from
297b89a7cc2SEnji Cooper   // SetUp/TearDown method of Environment objects registered with Google
298b89a7cc2SEnji Cooper   // Test) will be output as attributes of the <testsuites> element.
299b89a7cc2SEnji Cooper   static void RecordProperty(const std::string& key, const std::string& value);
30028f6c2f2SEnji Cooper   // We do not define a custom serialization except for values that can be
30128f6c2f2SEnji Cooper   // converted to int64_t, but other values could be logged in this way.
30228f6c2f2SEnji Cooper   template <typename T, std::enable_if_t<std::is_convertible<T, int64_t>::value,
30328f6c2f2SEnji Cooper                                          bool> = true>
RecordProperty(const std::string & key,const T & value)30428f6c2f2SEnji Cooper   static void RecordProperty(const std::string& key, const T& value) {
30528f6c2f2SEnji Cooper     RecordProperty(key, (Message() << value).GetString());
30628f6c2f2SEnji Cooper   }
307b89a7cc2SEnji Cooper 
308b89a7cc2SEnji Cooper  protected:
309b89a7cc2SEnji Cooper   // Creates a Test object.
310b89a7cc2SEnji Cooper   Test();
311b89a7cc2SEnji Cooper 
312b89a7cc2SEnji Cooper   // Sets up the test fixture.
313b89a7cc2SEnji Cooper   virtual void SetUp();
314b89a7cc2SEnji Cooper 
315b89a7cc2SEnji Cooper   // Tears down the test fixture.
316b89a7cc2SEnji Cooper   virtual void TearDown();
317b89a7cc2SEnji Cooper 
318b89a7cc2SEnji Cooper  private:
31928f6c2f2SEnji Cooper   // Returns true if and only if the current test has the same fixture class
32028f6c2f2SEnji Cooper   // as the first test in the current test suite.
321b89a7cc2SEnji Cooper   static bool HasSameFixtureClass();
322b89a7cc2SEnji Cooper 
323b89a7cc2SEnji Cooper   // Runs the test after the test fixture has been set up.
324b89a7cc2SEnji Cooper   //
325b89a7cc2SEnji Cooper   // A sub-class must implement this to define the test logic.
326b89a7cc2SEnji Cooper   //
327b89a7cc2SEnji Cooper   // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM.
328b89a7cc2SEnji Cooper   // Instead, use the TEST or TEST_F macro.
329b89a7cc2SEnji Cooper   virtual void TestBody() = 0;
330b89a7cc2SEnji Cooper 
331b89a7cc2SEnji Cooper   // Sets up, executes, and tears down the test.
332b89a7cc2SEnji Cooper   void Run();
333b89a7cc2SEnji Cooper 
334b89a7cc2SEnji Cooper   // Deletes self.  We deliberately pick an unusual name for this
335b89a7cc2SEnji Cooper   // internal method to avoid clashing with names used in user TESTs.
DeleteSelf_()336b89a7cc2SEnji Cooper   void DeleteSelf_() { delete this; }
337b89a7cc2SEnji Cooper 
33828f6c2f2SEnji Cooper   const std::unique_ptr<GTEST_FLAG_SAVER_> gtest_flag_saver_;
339b89a7cc2SEnji Cooper 
340b89a7cc2SEnji Cooper   // Often a user misspells SetUp() as Setup() and spends a long time
341b89a7cc2SEnji Cooper   // wondering why it is never called by Google Test.  The declaration of
342b89a7cc2SEnji Cooper   // the following method is solely for catching such an error at
343b89a7cc2SEnji Cooper   // compile time:
344b89a7cc2SEnji Cooper   //
345b89a7cc2SEnji Cooper   //   - The return type is deliberately chosen to be not void, so it
346b89a7cc2SEnji Cooper   //   will be a conflict if void Setup() is declared in the user's
347b89a7cc2SEnji Cooper   //   test fixture.
348b89a7cc2SEnji Cooper   //
349b89a7cc2SEnji Cooper   //   - This method is private, so it will be another compiler error
350b89a7cc2SEnji Cooper   //   if the method is called from the user's test fixture.
351b89a7cc2SEnji Cooper   //
352b89a7cc2SEnji Cooper   // DO NOT OVERRIDE THIS FUNCTION.
353b89a7cc2SEnji Cooper   //
354b89a7cc2SEnji Cooper   // If you see an error about overriding the following function or
355b89a7cc2SEnji Cooper   // about it being private, you have mis-spelled SetUp() as Setup().
356b89a7cc2SEnji Cooper   struct Setup_should_be_spelled_SetUp {};
Setup()35728f6c2f2SEnji Cooper   virtual Setup_should_be_spelled_SetUp* Setup() { return nullptr; }
358b89a7cc2SEnji Cooper 
359b89a7cc2SEnji Cooper   // We disallow copying Tests.
36028f6c2f2SEnji Cooper   Test(const Test&) = delete;
36128f6c2f2SEnji Cooper   Test& operator=(const Test&) = delete;
362b89a7cc2SEnji Cooper };
363b89a7cc2SEnji Cooper 
364b89a7cc2SEnji Cooper typedef internal::TimeInMillis TimeInMillis;
365b89a7cc2SEnji Cooper 
366b89a7cc2SEnji Cooper // A copyable object representing a user specified test property which can be
367b89a7cc2SEnji Cooper // output as a key/value string pair.
368b89a7cc2SEnji Cooper //
369b89a7cc2SEnji Cooper // Don't inherit from TestProperty as its destructor is not virtual.
370b89a7cc2SEnji Cooper class TestProperty {
371b89a7cc2SEnji Cooper  public:
372b89a7cc2SEnji Cooper   // C'tor.  TestProperty does NOT have a default constructor.
373b89a7cc2SEnji Cooper   // Always use this constructor (with parameters) to create a
374b89a7cc2SEnji Cooper   // TestProperty object.
TestProperty(const std::string & a_key,const std::string & a_value)37528f6c2f2SEnji Cooper   TestProperty(const std::string& a_key, const std::string& a_value)
37628f6c2f2SEnji Cooper       : key_(a_key), value_(a_value) {}
377b89a7cc2SEnji Cooper 
378b89a7cc2SEnji Cooper   // Gets the user supplied key.
key()37928f6c2f2SEnji Cooper   const char* key() const { return key_.c_str(); }
380b89a7cc2SEnji Cooper 
381b89a7cc2SEnji Cooper   // Gets the user supplied value.
value()38228f6c2f2SEnji Cooper   const char* value() const { return value_.c_str(); }
383b89a7cc2SEnji Cooper 
384b89a7cc2SEnji Cooper   // Sets a new value, overriding the one supplied in the constructor.
SetValue(const std::string & new_value)38528f6c2f2SEnji Cooper   void SetValue(const std::string& new_value) { value_ = new_value; }
386b89a7cc2SEnji Cooper 
387b89a7cc2SEnji Cooper  private:
388b89a7cc2SEnji Cooper   // The key supplied by the user.
389b89a7cc2SEnji Cooper   std::string key_;
390b89a7cc2SEnji Cooper   // The value supplied by the user.
391b89a7cc2SEnji Cooper   std::string value_;
392b89a7cc2SEnji Cooper };
393b89a7cc2SEnji Cooper 
394b89a7cc2SEnji Cooper // The result of a single Test.  This includes a list of
395b89a7cc2SEnji Cooper // TestPartResults, a list of TestProperties, a count of how many
396b89a7cc2SEnji Cooper // death tests there are in the Test, and how much time it took to run
397b89a7cc2SEnji Cooper // the Test.
398b89a7cc2SEnji Cooper //
399b89a7cc2SEnji Cooper // TestResult is not copyable.
400b89a7cc2SEnji Cooper class GTEST_API_ TestResult {
401b89a7cc2SEnji Cooper  public:
402b89a7cc2SEnji Cooper   // Creates an empty TestResult.
403b89a7cc2SEnji Cooper   TestResult();
404b89a7cc2SEnji Cooper 
405b89a7cc2SEnji Cooper   // D'tor.  Do not inherit from TestResult.
406b89a7cc2SEnji Cooper   ~TestResult();
407b89a7cc2SEnji Cooper 
408b89a7cc2SEnji Cooper   // Gets the number of all test parts.  This is the sum of the number
409b89a7cc2SEnji Cooper   // of successful test parts and the number of failed test parts.
410b89a7cc2SEnji Cooper   int total_part_count() const;
411b89a7cc2SEnji Cooper 
412b89a7cc2SEnji Cooper   // Returns the number of the test properties.
413b89a7cc2SEnji Cooper   int test_property_count() const;
414b89a7cc2SEnji Cooper 
41528f6c2f2SEnji Cooper   // Returns true if and only if the test passed (i.e. no test part failed).
Passed()416da1a9eb0SAlan Somers   bool Passed() const { return !Skipped() && !Failed(); }
417da1a9eb0SAlan Somers 
41828f6c2f2SEnji Cooper   // Returns true if and only if the test was skipped.
419da1a9eb0SAlan Somers   bool Skipped() const;
420b89a7cc2SEnji Cooper 
42128f6c2f2SEnji Cooper   // Returns true if and only if the test failed.
422b89a7cc2SEnji Cooper   bool Failed() const;
423b89a7cc2SEnji Cooper 
42428f6c2f2SEnji Cooper   // Returns true if and only if the test fatally failed.
425b89a7cc2SEnji Cooper   bool HasFatalFailure() const;
426b89a7cc2SEnji Cooper 
42728f6c2f2SEnji Cooper   // Returns true if and only if the test has a non-fatal failure.
428b89a7cc2SEnji Cooper   bool HasNonfatalFailure() const;
429b89a7cc2SEnji Cooper 
430b89a7cc2SEnji Cooper   // Returns the elapsed time, in milliseconds.
elapsed_time()431b89a7cc2SEnji Cooper   TimeInMillis elapsed_time() const { return elapsed_time_; }
432b89a7cc2SEnji Cooper 
43328f6c2f2SEnji Cooper   // Gets the time of the test case start, in ms from the start of the
43428f6c2f2SEnji Cooper   // UNIX epoch.
start_timestamp()43528f6c2f2SEnji Cooper   TimeInMillis start_timestamp() const { return start_timestamp_; }
43628f6c2f2SEnji Cooper 
437b89a7cc2SEnji Cooper   // Returns the i-th test part result among all the results. i can range from 0
438b89a7cc2SEnji Cooper   // to total_part_count() - 1. If i is not in that range, aborts the program.
439b89a7cc2SEnji Cooper   const TestPartResult& GetTestPartResult(int i) const;
440b89a7cc2SEnji Cooper 
441b89a7cc2SEnji Cooper   // Returns the i-th test property. i can range from 0 to
442b89a7cc2SEnji Cooper   // test_property_count() - 1. If i is not in that range, aborts the
443b89a7cc2SEnji Cooper   // program.
444b89a7cc2SEnji Cooper   const TestProperty& GetTestProperty(int i) const;
445b89a7cc2SEnji Cooper 
446b89a7cc2SEnji Cooper  private:
447b89a7cc2SEnji Cooper   friend class TestInfo;
44828f6c2f2SEnji Cooper   friend class TestSuite;
449b89a7cc2SEnji Cooper   friend class UnitTest;
450b89a7cc2SEnji Cooper   friend class internal::DefaultGlobalTestPartResultReporter;
451b89a7cc2SEnji Cooper   friend class internal::ExecDeathTest;
452b89a7cc2SEnji Cooper   friend class internal::TestResultAccessor;
453b89a7cc2SEnji Cooper   friend class internal::UnitTestImpl;
454b89a7cc2SEnji Cooper   friend class internal::WindowsDeathTest;
455b89a7cc2SEnji Cooper   friend class internal::FuchsiaDeathTest;
456b89a7cc2SEnji Cooper 
457b89a7cc2SEnji Cooper   // Gets the vector of TestPartResults.
test_part_results()458b89a7cc2SEnji Cooper   const std::vector<TestPartResult>& test_part_results() const {
459b89a7cc2SEnji Cooper     return test_part_results_;
460b89a7cc2SEnji Cooper   }
461b89a7cc2SEnji Cooper 
462b89a7cc2SEnji Cooper   // Gets the vector of TestProperties.
test_properties()463b89a7cc2SEnji Cooper   const std::vector<TestProperty>& test_properties() const {
464b89a7cc2SEnji Cooper     return test_properties_;
465b89a7cc2SEnji Cooper   }
466b89a7cc2SEnji Cooper 
46728f6c2f2SEnji Cooper   // Sets the start time.
set_start_timestamp(TimeInMillis start)46828f6c2f2SEnji Cooper   void set_start_timestamp(TimeInMillis start) { start_timestamp_ = start; }
46928f6c2f2SEnji Cooper 
470b89a7cc2SEnji Cooper   // Sets the elapsed time.
set_elapsed_time(TimeInMillis elapsed)471b89a7cc2SEnji Cooper   void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; }
472b89a7cc2SEnji Cooper 
473b89a7cc2SEnji Cooper   // Adds a test property to the list. The property is validated and may add
474b89a7cc2SEnji Cooper   // a non-fatal failure if invalid (e.g., if it conflicts with reserved
475b89a7cc2SEnji Cooper   // key names). If a property is already recorded for the same key, the
476b89a7cc2SEnji Cooper   // value will be updated, rather than storing multiple values for the same
477b89a7cc2SEnji Cooper   // key.  xml_element specifies the element for which the property is being
478b89a7cc2SEnji Cooper   // recorded and is used for validation.
479b89a7cc2SEnji Cooper   void RecordProperty(const std::string& xml_element,
480b89a7cc2SEnji Cooper                       const TestProperty& test_property);
481b89a7cc2SEnji Cooper 
482b89a7cc2SEnji Cooper   // Adds a failure if the key is a reserved attribute of Google Test
48328f6c2f2SEnji Cooper   // testsuite tags.  Returns true if the property is valid.
484b89a7cc2SEnji Cooper   // FIXME: Validate attribute names are legal and human readable.
485b89a7cc2SEnji Cooper   static bool ValidateTestProperty(const std::string& xml_element,
486b89a7cc2SEnji Cooper                                    const TestProperty& test_property);
487b89a7cc2SEnji Cooper 
488b89a7cc2SEnji Cooper   // Adds a test part result to the list.
489b89a7cc2SEnji Cooper   void AddTestPartResult(const TestPartResult& test_part_result);
490b89a7cc2SEnji Cooper 
491b89a7cc2SEnji Cooper   // Returns the death test count.
death_test_count()492b89a7cc2SEnji Cooper   int death_test_count() const { return death_test_count_; }
493b89a7cc2SEnji Cooper 
494b89a7cc2SEnji Cooper   // Increments the death test count, returning the new count.
increment_death_test_count()495b89a7cc2SEnji Cooper   int increment_death_test_count() { return ++death_test_count_; }
496b89a7cc2SEnji Cooper 
497b89a7cc2SEnji Cooper   // Clears the test part results.
498b89a7cc2SEnji Cooper   void ClearTestPartResults();
499b89a7cc2SEnji Cooper 
500b89a7cc2SEnji Cooper   // Clears the object.
501b89a7cc2SEnji Cooper   void Clear();
502b89a7cc2SEnji Cooper 
503b89a7cc2SEnji Cooper   // Protects mutable state of the property vector and of owned
504b89a7cc2SEnji Cooper   // properties, whose values may be updated.
50528f6c2f2SEnji Cooper   internal::Mutex test_properties_mutex_;
506b89a7cc2SEnji Cooper 
507b89a7cc2SEnji Cooper   // The vector of TestPartResults
508b89a7cc2SEnji Cooper   std::vector<TestPartResult> test_part_results_;
509b89a7cc2SEnji Cooper   // The vector of TestProperties
510b89a7cc2SEnji Cooper   std::vector<TestProperty> test_properties_;
511b89a7cc2SEnji Cooper   // Running count of death tests.
512b89a7cc2SEnji Cooper   int death_test_count_;
51328f6c2f2SEnji Cooper   // The start time, in milliseconds since UNIX Epoch.
51428f6c2f2SEnji Cooper   TimeInMillis start_timestamp_;
515b89a7cc2SEnji Cooper   // The elapsed time, in milliseconds.
516b89a7cc2SEnji Cooper   TimeInMillis elapsed_time_;
517b89a7cc2SEnji Cooper 
518b89a7cc2SEnji Cooper   // We disallow copying TestResult.
51928f6c2f2SEnji Cooper   TestResult(const TestResult&) = delete;
52028f6c2f2SEnji Cooper   TestResult& operator=(const TestResult&) = delete;
521b89a7cc2SEnji Cooper };  // class TestResult
522b89a7cc2SEnji Cooper 
523b89a7cc2SEnji Cooper // A TestInfo object stores the following information about a test:
524b89a7cc2SEnji Cooper //
52528f6c2f2SEnji Cooper //   Test suite name
526b89a7cc2SEnji Cooper //   Test name
527b89a7cc2SEnji Cooper //   Whether the test should be run
528b89a7cc2SEnji Cooper //   A function pointer that creates the test object when invoked
529b89a7cc2SEnji Cooper //   Test result
530b89a7cc2SEnji Cooper //
531b89a7cc2SEnji Cooper // The constructor of TestInfo registers itself with the UnitTest
532b89a7cc2SEnji Cooper // singleton such that the RUN_ALL_TESTS() macro knows which tests to
533b89a7cc2SEnji Cooper // run.
534b89a7cc2SEnji Cooper class GTEST_API_ TestInfo {
535b89a7cc2SEnji Cooper  public:
536b89a7cc2SEnji Cooper   // Destructs a TestInfo object.  This function is not virtual, so
537b89a7cc2SEnji Cooper   // don't inherit from TestInfo.
538b89a7cc2SEnji Cooper   ~TestInfo();
539b89a7cc2SEnji Cooper 
54028f6c2f2SEnji Cooper   // Returns the test suite name.
test_suite_name()54128f6c2f2SEnji Cooper   const char* test_suite_name() const { return test_suite_name_.c_str(); }
54228f6c2f2SEnji Cooper 
54328f6c2f2SEnji Cooper // Legacy API is deprecated but still available
54428f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
test_case_name()54528f6c2f2SEnji Cooper   const char* test_case_name() const { return test_suite_name(); }
54628f6c2f2SEnji Cooper #endif  // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
547b89a7cc2SEnji Cooper 
548b89a7cc2SEnji Cooper   // Returns the test name.
name()549b89a7cc2SEnji Cooper   const char* name() const { return name_.c_str(); }
550b89a7cc2SEnji Cooper 
551b89a7cc2SEnji Cooper   // Returns the name of the parameter type, or NULL if this is not a typed
552b89a7cc2SEnji Cooper   // or a type-parameterized test.
type_param()553b89a7cc2SEnji Cooper   const char* type_param() const {
55428f6c2f2SEnji Cooper     if (type_param_ != nullptr) return type_param_->c_str();
55528f6c2f2SEnji Cooper     return nullptr;
556b89a7cc2SEnji Cooper   }
557b89a7cc2SEnji Cooper 
558b89a7cc2SEnji Cooper   // Returns the text representation of the value parameter, or NULL if this
559b89a7cc2SEnji Cooper   // is not a value-parameterized test.
value_param()560b89a7cc2SEnji Cooper   const char* value_param() const {
56128f6c2f2SEnji Cooper     if (value_param_ != nullptr) return value_param_->c_str();
56228f6c2f2SEnji Cooper     return nullptr;
563b89a7cc2SEnji Cooper   }
564b89a7cc2SEnji Cooper 
565b89a7cc2SEnji Cooper   // Returns the file name where this test is defined.
file()566b89a7cc2SEnji Cooper   const char* file() const { return location_.file.c_str(); }
567b89a7cc2SEnji Cooper 
568b89a7cc2SEnji Cooper   // Returns the line where this test is defined.
line()569b89a7cc2SEnji Cooper   int line() const { return location_.line; }
570b89a7cc2SEnji Cooper 
571b89a7cc2SEnji Cooper   // Return true if this test should not be run because it's in another shard.
is_in_another_shard()572b89a7cc2SEnji Cooper   bool is_in_another_shard() const { return is_in_another_shard_; }
573b89a7cc2SEnji Cooper 
574b89a7cc2SEnji Cooper   // Returns true if this test should run, that is if the test is not
575b89a7cc2SEnji Cooper   // disabled (or it is disabled but the also_run_disabled_tests flag has
576b89a7cc2SEnji Cooper   // been specified) and its full name matches the user-specified filter.
577b89a7cc2SEnji Cooper   //
578b89a7cc2SEnji Cooper   // Google Test allows the user to filter the tests by their full names.
57928f6c2f2SEnji Cooper   // The full name of a test Bar in test suite Foo is defined as
580b89a7cc2SEnji Cooper   // "Foo.Bar".  Only the tests that match the filter will run.
581b89a7cc2SEnji Cooper   //
582b89a7cc2SEnji Cooper   // A filter is a colon-separated list of glob (not regex) patterns,
583b89a7cc2SEnji Cooper   // optionally followed by a '-' and a colon-separated list of
584b89a7cc2SEnji Cooper   // negative patterns (tests to exclude).  A test is run if it
585b89a7cc2SEnji Cooper   // matches one of the positive patterns and does not match any of
586b89a7cc2SEnji Cooper   // the negative patterns.
587b89a7cc2SEnji Cooper   //
588b89a7cc2SEnji Cooper   // For example, *A*:Foo.* is a filter that matches any string that
589b89a7cc2SEnji Cooper   // contains the character 'A' or starts with "Foo.".
should_run()590b89a7cc2SEnji Cooper   bool should_run() const { return should_run_; }
591b89a7cc2SEnji Cooper 
59228f6c2f2SEnji Cooper   // Returns true if and only if this test will appear in the XML report.
is_reportable()593b89a7cc2SEnji Cooper   bool is_reportable() const {
594b89a7cc2SEnji Cooper     // The XML report includes tests matching the filter, excluding those
595b89a7cc2SEnji Cooper     // run in other shards.
596b89a7cc2SEnji Cooper     return matches_filter_ && !is_in_another_shard_;
597b89a7cc2SEnji Cooper   }
598b89a7cc2SEnji Cooper 
599b89a7cc2SEnji Cooper   // Returns the result of the test.
result()600b89a7cc2SEnji Cooper   const TestResult* result() const { return &result_; }
601b89a7cc2SEnji Cooper 
602b89a7cc2SEnji Cooper  private:
60328f6c2f2SEnji Cooper #ifdef GTEST_HAS_DEATH_TEST
604b89a7cc2SEnji Cooper   friend class internal::DefaultDeathTestFactory;
605b89a7cc2SEnji Cooper #endif  // GTEST_HAS_DEATH_TEST
606b89a7cc2SEnji Cooper   friend class Test;
60728f6c2f2SEnji Cooper   friend class TestSuite;
608b89a7cc2SEnji Cooper   friend class internal::UnitTestImpl;
609b89a7cc2SEnji Cooper   friend class internal::StreamingListenerTest;
610b89a7cc2SEnji Cooper   friend TestInfo* internal::MakeAndRegisterTestInfo(
61128f6c2f2SEnji Cooper       const char* test_suite_name, const char* name, const char* type_param,
61228f6c2f2SEnji Cooper       const char* value_param, internal::CodeLocation code_location,
61328f6c2f2SEnji Cooper       internal::TypeId fixture_class_id, internal::SetUpTestSuiteFunc set_up_tc,
61428f6c2f2SEnji Cooper       internal::TearDownTestSuiteFunc tear_down_tc,
615b89a7cc2SEnji Cooper       internal::TestFactoryBase* factory);
616b89a7cc2SEnji Cooper 
617b89a7cc2SEnji Cooper   // Constructs a TestInfo object. The newly constructed instance assumes
618b89a7cc2SEnji Cooper   // ownership of the factory object.
61928f6c2f2SEnji Cooper   TestInfo(const std::string& test_suite_name, const std::string& name,
620b89a7cc2SEnji Cooper            const char* a_type_param,   // NULL if not a type-parameterized test
621b89a7cc2SEnji Cooper            const char* a_value_param,  // NULL if not a value-parameterized test
622b89a7cc2SEnji Cooper            internal::CodeLocation a_code_location,
623b89a7cc2SEnji Cooper            internal::TypeId fixture_class_id,
624b89a7cc2SEnji Cooper            internal::TestFactoryBase* factory);
625b89a7cc2SEnji Cooper 
626b89a7cc2SEnji Cooper   // Increments the number of death tests encountered in this test so
627b89a7cc2SEnji Cooper   // far.
increment_death_test_count()628b89a7cc2SEnji Cooper   int increment_death_test_count() {
629b89a7cc2SEnji Cooper     return result_.increment_death_test_count();
630b89a7cc2SEnji Cooper   }
631b89a7cc2SEnji Cooper 
632b89a7cc2SEnji Cooper   // Creates the test object, runs it, records its result, and then
633b89a7cc2SEnji Cooper   // deletes it.
634b89a7cc2SEnji Cooper   void Run();
635b89a7cc2SEnji Cooper 
63628f6c2f2SEnji Cooper   // Skip and records the test result for this object.
63728f6c2f2SEnji Cooper   void Skip();
63828f6c2f2SEnji Cooper 
ClearTestResult(TestInfo * test_info)639b89a7cc2SEnji Cooper   static void ClearTestResult(TestInfo* test_info) {
640b89a7cc2SEnji Cooper     test_info->result_.Clear();
641b89a7cc2SEnji Cooper   }
642b89a7cc2SEnji Cooper 
643b89a7cc2SEnji Cooper   // These fields are immutable properties of the test.
64428f6c2f2SEnji Cooper   const std::string test_suite_name_;  // test suite name
645b89a7cc2SEnji Cooper   const std::string name_;             // Test name
646b89a7cc2SEnji Cooper   // Name of the parameter type, or NULL if this is not a typed or a
647b89a7cc2SEnji Cooper   // type-parameterized test.
64828f6c2f2SEnji Cooper   const std::unique_ptr<const ::std::string> type_param_;
649b89a7cc2SEnji Cooper   // Text representation of the value parameter, or NULL if this is not a
650b89a7cc2SEnji Cooper   // value-parameterized test.
65128f6c2f2SEnji Cooper   const std::unique_ptr<const ::std::string> value_param_;
652b89a7cc2SEnji Cooper   internal::CodeLocation location_;
653b89a7cc2SEnji Cooper   const internal::TypeId fixture_class_id_;  // ID of the test fixture class
65428f6c2f2SEnji Cooper   bool should_run_;           // True if and only if this test should run
65528f6c2f2SEnji Cooper   bool is_disabled_;          // True if and only if this test is disabled
656b89a7cc2SEnji Cooper   bool matches_filter_;       // True if this test matches the
657b89a7cc2SEnji Cooper                               // user-specified filter.
658b89a7cc2SEnji Cooper   bool is_in_another_shard_;  // Will be run in another shard.
659b89a7cc2SEnji Cooper   internal::TestFactoryBase* const factory_;  // The factory that creates
660b89a7cc2SEnji Cooper                                               // the test object
661b89a7cc2SEnji Cooper 
662b89a7cc2SEnji Cooper   // This field is mutable and needs to be reset before running the
663b89a7cc2SEnji Cooper   // test for the second time.
664b89a7cc2SEnji Cooper   TestResult result_;
665b89a7cc2SEnji Cooper 
66628f6c2f2SEnji Cooper   TestInfo(const TestInfo&) = delete;
66728f6c2f2SEnji Cooper   TestInfo& operator=(const TestInfo&) = delete;
668b89a7cc2SEnji Cooper };
669b89a7cc2SEnji Cooper 
67028f6c2f2SEnji Cooper // A test suite, which consists of a vector of TestInfos.
671b89a7cc2SEnji Cooper //
67228f6c2f2SEnji Cooper // TestSuite is not copyable.
67328f6c2f2SEnji Cooper class GTEST_API_ TestSuite {
674b89a7cc2SEnji Cooper  public:
67528f6c2f2SEnji Cooper   // Creates a TestSuite with the given name.
676b89a7cc2SEnji Cooper   //
67728f6c2f2SEnji Cooper   // TestSuite does NOT have a default constructor.  Always use this
67828f6c2f2SEnji Cooper   // constructor to create a TestSuite object.
679b89a7cc2SEnji Cooper   //
680b89a7cc2SEnji Cooper   // Arguments:
681b89a7cc2SEnji Cooper   //
68228f6c2f2SEnji Cooper   //   name:         name of the test suite
683b89a7cc2SEnji Cooper   //   a_type_param: the name of the test's type parameter, or NULL if
684b89a7cc2SEnji Cooper   //                 this is not a type-parameterized test.
68528f6c2f2SEnji Cooper   //   set_up_tc:    pointer to the function that sets up the test suite
68628f6c2f2SEnji Cooper   //   tear_down_tc: pointer to the function that tears down the test suite
68728f6c2f2SEnji Cooper   TestSuite(const char* name, const char* a_type_param,
68828f6c2f2SEnji Cooper             internal::SetUpTestSuiteFunc set_up_tc,
68928f6c2f2SEnji Cooper             internal::TearDownTestSuiteFunc tear_down_tc);
690b89a7cc2SEnji Cooper 
69128f6c2f2SEnji Cooper   // Destructor of TestSuite.
69228f6c2f2SEnji Cooper   virtual ~TestSuite();
693b89a7cc2SEnji Cooper 
69428f6c2f2SEnji Cooper   // Gets the name of the TestSuite.
name()695b89a7cc2SEnji Cooper   const char* name() const { return name_.c_str(); }
696b89a7cc2SEnji Cooper 
697b89a7cc2SEnji Cooper   // Returns the name of the parameter type, or NULL if this is not a
69828f6c2f2SEnji Cooper   // type-parameterized test suite.
type_param()699b89a7cc2SEnji Cooper   const char* type_param() const {
70028f6c2f2SEnji Cooper     if (type_param_ != nullptr) return type_param_->c_str();
70128f6c2f2SEnji Cooper     return nullptr;
702b89a7cc2SEnji Cooper   }
703b89a7cc2SEnji Cooper 
70428f6c2f2SEnji Cooper   // Returns true if any test in this test suite should run.
should_run()705b89a7cc2SEnji Cooper   bool should_run() const { return should_run_; }
706b89a7cc2SEnji Cooper 
70728f6c2f2SEnji Cooper   // Gets the number of successful tests in this test suite.
708b89a7cc2SEnji Cooper   int successful_test_count() const;
709b89a7cc2SEnji Cooper 
71028f6c2f2SEnji Cooper   // Gets the number of skipped tests in this test suite.
711da1a9eb0SAlan Somers   int skipped_test_count() const;
712da1a9eb0SAlan Somers 
71328f6c2f2SEnji Cooper   // Gets the number of failed tests in this test suite.
714b89a7cc2SEnji Cooper   int failed_test_count() const;
715b89a7cc2SEnji Cooper 
716b89a7cc2SEnji Cooper   // Gets the number of disabled tests that will be reported in the XML report.
717b89a7cc2SEnji Cooper   int reportable_disabled_test_count() const;
718b89a7cc2SEnji Cooper 
71928f6c2f2SEnji Cooper   // Gets the number of disabled tests in this test suite.
720b89a7cc2SEnji Cooper   int disabled_test_count() const;
721b89a7cc2SEnji Cooper 
722b89a7cc2SEnji Cooper   // Gets the number of tests to be printed in the XML report.
723b89a7cc2SEnji Cooper   int reportable_test_count() const;
724b89a7cc2SEnji Cooper 
72528f6c2f2SEnji Cooper   // Get the number of tests in this test suite that should run.
726b89a7cc2SEnji Cooper   int test_to_run_count() const;
727b89a7cc2SEnji Cooper 
72828f6c2f2SEnji Cooper   // Gets the number of all tests in this test suite.
729b89a7cc2SEnji Cooper   int total_test_count() const;
730b89a7cc2SEnji Cooper 
73128f6c2f2SEnji Cooper   // Returns true if and only if the test suite passed.
Passed()732b89a7cc2SEnji Cooper   bool Passed() const { return !Failed(); }
733b89a7cc2SEnji Cooper 
73428f6c2f2SEnji Cooper   // Returns true if and only if the test suite failed.
Failed()73528f6c2f2SEnji Cooper   bool Failed() const {
73628f6c2f2SEnji Cooper     return failed_test_count() > 0 || ad_hoc_test_result().Failed();
73728f6c2f2SEnji Cooper   }
738b89a7cc2SEnji Cooper 
739b89a7cc2SEnji Cooper   // Returns the elapsed time, in milliseconds.
elapsed_time()740b89a7cc2SEnji Cooper   TimeInMillis elapsed_time() const { return elapsed_time_; }
741b89a7cc2SEnji Cooper 
74228f6c2f2SEnji Cooper   // Gets the time of the test suite start, in ms from the start of the
74328f6c2f2SEnji Cooper   // UNIX epoch.
start_timestamp()74428f6c2f2SEnji Cooper   TimeInMillis start_timestamp() const { return start_timestamp_; }
74528f6c2f2SEnji Cooper 
746b89a7cc2SEnji Cooper   // Returns the i-th test among all the tests. i can range from 0 to
747b89a7cc2SEnji Cooper   // total_test_count() - 1. If i is not in that range, returns NULL.
748b89a7cc2SEnji Cooper   const TestInfo* GetTestInfo(int i) const;
749b89a7cc2SEnji Cooper 
750b89a7cc2SEnji Cooper   // Returns the TestResult that holds test properties recorded during
75128f6c2f2SEnji Cooper   // execution of SetUpTestSuite and TearDownTestSuite.
ad_hoc_test_result()752b89a7cc2SEnji Cooper   const TestResult& ad_hoc_test_result() const { return ad_hoc_test_result_; }
753b89a7cc2SEnji Cooper 
754b89a7cc2SEnji Cooper  private:
755b89a7cc2SEnji Cooper   friend class Test;
756b89a7cc2SEnji Cooper   friend class internal::UnitTestImpl;
757b89a7cc2SEnji Cooper 
75828f6c2f2SEnji Cooper   // Gets the (mutable) vector of TestInfos in this TestSuite.
test_info_list()759b89a7cc2SEnji Cooper   std::vector<TestInfo*>& test_info_list() { return test_info_list_; }
760b89a7cc2SEnji Cooper 
76128f6c2f2SEnji Cooper   // Gets the (immutable) vector of TestInfos in this TestSuite.
test_info_list()762b89a7cc2SEnji Cooper   const std::vector<TestInfo*>& test_info_list() const {
763b89a7cc2SEnji Cooper     return test_info_list_;
764b89a7cc2SEnji Cooper   }
765b89a7cc2SEnji Cooper 
766b89a7cc2SEnji Cooper   // Returns the i-th test among all the tests. i can range from 0 to
767b89a7cc2SEnji Cooper   // total_test_count() - 1. If i is not in that range, returns NULL.
768b89a7cc2SEnji Cooper   TestInfo* GetMutableTestInfo(int i);
769b89a7cc2SEnji Cooper 
770b89a7cc2SEnji Cooper   // Sets the should_run member.
set_should_run(bool should)771b89a7cc2SEnji Cooper   void set_should_run(bool should) { should_run_ = should; }
772b89a7cc2SEnji Cooper 
77328f6c2f2SEnji Cooper   // Adds a TestInfo to this test suite.  Will delete the TestInfo upon
77428f6c2f2SEnji Cooper   // destruction of the TestSuite object.
775b89a7cc2SEnji Cooper   void AddTestInfo(TestInfo* test_info);
776b89a7cc2SEnji Cooper 
77728f6c2f2SEnji Cooper   // Clears the results of all tests in this test suite.
778b89a7cc2SEnji Cooper   void ClearResult();
779b89a7cc2SEnji Cooper 
78028f6c2f2SEnji Cooper   // Clears the results of all tests in the given test suite.
ClearTestSuiteResult(TestSuite * test_suite)78128f6c2f2SEnji Cooper   static void ClearTestSuiteResult(TestSuite* test_suite) {
78228f6c2f2SEnji Cooper     test_suite->ClearResult();
783b89a7cc2SEnji Cooper   }
784b89a7cc2SEnji Cooper 
78528f6c2f2SEnji Cooper   // Runs every test in this TestSuite.
786b89a7cc2SEnji Cooper   void Run();
787b89a7cc2SEnji Cooper 
78828f6c2f2SEnji Cooper   // Skips the execution of tests under this TestSuite
78928f6c2f2SEnji Cooper   void Skip();
790b89a7cc2SEnji Cooper 
79128f6c2f2SEnji Cooper   // Runs SetUpTestSuite() for this TestSuite.  This wrapper is needed
79228f6c2f2SEnji Cooper   // for catching exceptions thrown from SetUpTestSuite().
RunSetUpTestSuite()79328f6c2f2SEnji Cooper   void RunSetUpTestSuite() {
79428f6c2f2SEnji Cooper     if (set_up_tc_ != nullptr) {
79528f6c2f2SEnji Cooper       (*set_up_tc_)();
79628f6c2f2SEnji Cooper     }
79728f6c2f2SEnji Cooper   }
798b89a7cc2SEnji Cooper 
79928f6c2f2SEnji Cooper   // Runs TearDownTestSuite() for this TestSuite.  This wrapper is
80028f6c2f2SEnji Cooper   // needed for catching exceptions thrown from TearDownTestSuite().
RunTearDownTestSuite()80128f6c2f2SEnji Cooper   void RunTearDownTestSuite() {
80228f6c2f2SEnji Cooper     if (tear_down_tc_ != nullptr) {
80328f6c2f2SEnji Cooper       (*tear_down_tc_)();
80428f6c2f2SEnji Cooper     }
80528f6c2f2SEnji Cooper   }
80628f6c2f2SEnji Cooper 
80728f6c2f2SEnji Cooper   // Returns true if and only if test passed.
TestPassed(const TestInfo * test_info)808b89a7cc2SEnji Cooper   static bool TestPassed(const TestInfo* test_info) {
809b89a7cc2SEnji Cooper     return test_info->should_run() && test_info->result()->Passed();
810b89a7cc2SEnji Cooper   }
811b89a7cc2SEnji Cooper 
81228f6c2f2SEnji Cooper   // Returns true if and only if test skipped.
TestSkipped(const TestInfo * test_info)813da1a9eb0SAlan Somers   static bool TestSkipped(const TestInfo* test_info) {
814da1a9eb0SAlan Somers     return test_info->should_run() && test_info->result()->Skipped();
815da1a9eb0SAlan Somers   }
816da1a9eb0SAlan Somers 
81728f6c2f2SEnji Cooper   // Returns true if and only if test failed.
TestFailed(const TestInfo * test_info)818b89a7cc2SEnji Cooper   static bool TestFailed(const TestInfo* test_info) {
819b89a7cc2SEnji Cooper     return test_info->should_run() && test_info->result()->Failed();
820b89a7cc2SEnji Cooper   }
821b89a7cc2SEnji Cooper 
82228f6c2f2SEnji Cooper   // Returns true if and only if the test is disabled and will be reported in
82328f6c2f2SEnji Cooper   // the XML report.
TestReportableDisabled(const TestInfo * test_info)824b89a7cc2SEnji Cooper   static bool TestReportableDisabled(const TestInfo* test_info) {
825b89a7cc2SEnji Cooper     return test_info->is_reportable() && test_info->is_disabled_;
826b89a7cc2SEnji Cooper   }
827b89a7cc2SEnji Cooper 
82828f6c2f2SEnji Cooper   // Returns true if and only if test is disabled.
TestDisabled(const TestInfo * test_info)829b89a7cc2SEnji Cooper   static bool TestDisabled(const TestInfo* test_info) {
830b89a7cc2SEnji Cooper     return test_info->is_disabled_;
831b89a7cc2SEnji Cooper   }
832b89a7cc2SEnji Cooper 
83328f6c2f2SEnji Cooper   // Returns true if and only if this test will appear in the XML report.
TestReportable(const TestInfo * test_info)834b89a7cc2SEnji Cooper   static bool TestReportable(const TestInfo* test_info) {
835b89a7cc2SEnji Cooper     return test_info->is_reportable();
836b89a7cc2SEnji Cooper   }
837b89a7cc2SEnji Cooper 
838b89a7cc2SEnji Cooper   // Returns true if the given test should run.
ShouldRunTest(const TestInfo * test_info)839b89a7cc2SEnji Cooper   static bool ShouldRunTest(const TestInfo* test_info) {
840b89a7cc2SEnji Cooper     return test_info->should_run();
841b89a7cc2SEnji Cooper   }
842b89a7cc2SEnji Cooper 
84328f6c2f2SEnji Cooper   // Shuffles the tests in this test suite.
844b89a7cc2SEnji Cooper   void ShuffleTests(internal::Random* random);
845b89a7cc2SEnji Cooper 
846b89a7cc2SEnji Cooper   // Restores the test order to before the first shuffle.
847b89a7cc2SEnji Cooper   void UnshuffleTests();
848b89a7cc2SEnji Cooper 
84928f6c2f2SEnji Cooper   // Name of the test suite.
850b89a7cc2SEnji Cooper   std::string name_;
851b89a7cc2SEnji Cooper   // Name of the parameter type, or NULL if this is not a typed or a
852b89a7cc2SEnji Cooper   // type-parameterized test.
85328f6c2f2SEnji Cooper   const std::unique_ptr<const ::std::string> type_param_;
854b89a7cc2SEnji Cooper   // The vector of TestInfos in their original order.  It owns the
855b89a7cc2SEnji Cooper   // elements in the vector.
856b89a7cc2SEnji Cooper   std::vector<TestInfo*> test_info_list_;
857b89a7cc2SEnji Cooper   // Provides a level of indirection for the test list to allow easy
858b89a7cc2SEnji Cooper   // shuffling and restoring the test order.  The i-th element in this
859b89a7cc2SEnji Cooper   // vector is the index of the i-th test in the shuffled test list.
860b89a7cc2SEnji Cooper   std::vector<int> test_indices_;
86128f6c2f2SEnji Cooper   // Pointer to the function that sets up the test suite.
86228f6c2f2SEnji Cooper   internal::SetUpTestSuiteFunc set_up_tc_;
86328f6c2f2SEnji Cooper   // Pointer to the function that tears down the test suite.
86428f6c2f2SEnji Cooper   internal::TearDownTestSuiteFunc tear_down_tc_;
86528f6c2f2SEnji Cooper   // True if and only if any test in this test suite should run.
866b89a7cc2SEnji Cooper   bool should_run_;
86728f6c2f2SEnji Cooper   // The start time, in milliseconds since UNIX Epoch.
86828f6c2f2SEnji Cooper   TimeInMillis start_timestamp_;
869b89a7cc2SEnji Cooper   // Elapsed time, in milliseconds.
870b89a7cc2SEnji Cooper   TimeInMillis elapsed_time_;
87128f6c2f2SEnji Cooper   // Holds test properties recorded during execution of SetUpTestSuite and
87228f6c2f2SEnji Cooper   // TearDownTestSuite.
873b89a7cc2SEnji Cooper   TestResult ad_hoc_test_result_;
874b89a7cc2SEnji Cooper 
87528f6c2f2SEnji Cooper   // We disallow copying TestSuites.
87628f6c2f2SEnji Cooper   TestSuite(const TestSuite&) = delete;
87728f6c2f2SEnji Cooper   TestSuite& operator=(const TestSuite&) = delete;
878b89a7cc2SEnji Cooper };
879b89a7cc2SEnji Cooper 
880b89a7cc2SEnji Cooper // An Environment object is capable of setting up and tearing down an
881b89a7cc2SEnji Cooper // environment.  You should subclass this to define your own
882b89a7cc2SEnji Cooper // environment(s).
883b89a7cc2SEnji Cooper //
884b89a7cc2SEnji Cooper // An Environment object does the set-up and tear-down in virtual
885b89a7cc2SEnji Cooper // methods SetUp() and TearDown() instead of the constructor and the
886b89a7cc2SEnji Cooper // destructor, as:
887b89a7cc2SEnji Cooper //
888b89a7cc2SEnji Cooper //   1. You cannot safely throw from a destructor.  This is a problem
889b89a7cc2SEnji Cooper //      as in some cases Google Test is used where exceptions are enabled, and
890b89a7cc2SEnji Cooper //      we may want to implement ASSERT_* using exceptions where they are
891b89a7cc2SEnji Cooper //      available.
892b89a7cc2SEnji Cooper //   2. You cannot use ASSERT_* directly in a constructor or
893b89a7cc2SEnji Cooper //      destructor.
894b89a7cc2SEnji Cooper class Environment {
895b89a7cc2SEnji Cooper  public:
896b89a7cc2SEnji Cooper   // The d'tor is virtual as we need to subclass Environment.
89728f6c2f2SEnji Cooper   virtual ~Environment() = default;
898b89a7cc2SEnji Cooper 
899b89a7cc2SEnji Cooper   // Override this to define how to set up the environment.
SetUp()900b89a7cc2SEnji Cooper   virtual void SetUp() {}
901b89a7cc2SEnji Cooper 
902b89a7cc2SEnji Cooper   // Override this to define how to tear down the environment.
TearDown()903b89a7cc2SEnji Cooper   virtual void TearDown() {}
90428f6c2f2SEnji Cooper 
905b89a7cc2SEnji Cooper  private:
906b89a7cc2SEnji Cooper   // If you see an error about overriding the following function or
907b89a7cc2SEnji Cooper   // about it being private, you have mis-spelled SetUp() as Setup().
908b89a7cc2SEnji Cooper   struct Setup_should_be_spelled_SetUp {};
Setup()90928f6c2f2SEnji Cooper   virtual Setup_should_be_spelled_SetUp* Setup() { return nullptr; }
910b89a7cc2SEnji Cooper };
911b89a7cc2SEnji Cooper 
912b89a7cc2SEnji Cooper #if GTEST_HAS_EXCEPTIONS
913b89a7cc2SEnji Cooper 
914b89a7cc2SEnji Cooper // Exception which can be thrown from TestEventListener::OnTestPartResult.
915b89a7cc2SEnji Cooper class GTEST_API_ AssertionException
916b89a7cc2SEnji Cooper     : public internal::GoogleTestFailureException {
917b89a7cc2SEnji Cooper  public:
AssertionException(const TestPartResult & result)918b89a7cc2SEnji Cooper   explicit AssertionException(const TestPartResult& result)
919b89a7cc2SEnji Cooper       : GoogleTestFailureException(result) {}
920b89a7cc2SEnji Cooper };
921b89a7cc2SEnji Cooper 
922b89a7cc2SEnji Cooper #endif  // GTEST_HAS_EXCEPTIONS
923b89a7cc2SEnji Cooper 
924b89a7cc2SEnji Cooper // The interface for tracing execution of tests. The methods are organized in
925b89a7cc2SEnji Cooper // the order the corresponding events are fired.
926b89a7cc2SEnji Cooper class TestEventListener {
927b89a7cc2SEnji Cooper  public:
92828f6c2f2SEnji Cooper   virtual ~TestEventListener() = default;
929b89a7cc2SEnji Cooper 
930b89a7cc2SEnji Cooper   // Fired before any test activity starts.
931b89a7cc2SEnji Cooper   virtual void OnTestProgramStart(const UnitTest& unit_test) = 0;
932b89a7cc2SEnji Cooper 
933b89a7cc2SEnji Cooper   // Fired before each iteration of tests starts.  There may be more than
934b89a7cc2SEnji Cooper   // one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration
935b89a7cc2SEnji Cooper   // index, starting from 0.
936b89a7cc2SEnji Cooper   virtual void OnTestIterationStart(const UnitTest& unit_test,
937b89a7cc2SEnji Cooper                                     int iteration) = 0;
938b89a7cc2SEnji Cooper 
939b89a7cc2SEnji Cooper   // Fired before environment set-up for each iteration of tests starts.
940b89a7cc2SEnji Cooper   virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0;
941b89a7cc2SEnji Cooper 
942b89a7cc2SEnji Cooper   // Fired after environment set-up for each iteration of tests ends.
943b89a7cc2SEnji Cooper   virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0;
944b89a7cc2SEnji Cooper 
94528f6c2f2SEnji Cooper   // Fired before the test suite starts.
OnTestSuiteStart(const TestSuite &)94628f6c2f2SEnji Cooper   virtual void OnTestSuiteStart(const TestSuite& /*test_suite*/) {}
94728f6c2f2SEnji Cooper 
94828f6c2f2SEnji Cooper   //  Legacy API is deprecated but still available
94928f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
OnTestCaseStart(const TestCase &)95028f6c2f2SEnji Cooper   virtual void OnTestCaseStart(const TestCase& /*test_case*/) {}
95128f6c2f2SEnji Cooper #endif  //  GTEST_REMOVE_LEGACY_TEST_CASEAPI_
952b89a7cc2SEnji Cooper 
953b89a7cc2SEnji Cooper   // Fired before the test starts.
954b89a7cc2SEnji Cooper   virtual void OnTestStart(const TestInfo& test_info) = 0;
955b89a7cc2SEnji Cooper 
95628f6c2f2SEnji Cooper   // Fired when a test is disabled
OnTestDisabled(const TestInfo &)95728f6c2f2SEnji Cooper   virtual void OnTestDisabled(const TestInfo& /*test_info*/) {}
95828f6c2f2SEnji Cooper 
959b89a7cc2SEnji Cooper   // Fired after a failed assertion or a SUCCEED() invocation.
960b89a7cc2SEnji Cooper   // If you want to throw an exception from this function to skip to the next
961b89a7cc2SEnji Cooper   // TEST, it must be AssertionException defined above, or inherited from it.
962b89a7cc2SEnji Cooper   virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0;
963b89a7cc2SEnji Cooper 
964b89a7cc2SEnji Cooper   // Fired after the test ends.
965b89a7cc2SEnji Cooper   virtual void OnTestEnd(const TestInfo& test_info) = 0;
966b89a7cc2SEnji Cooper 
96728f6c2f2SEnji Cooper   // Fired after the test suite ends.
OnTestSuiteEnd(const TestSuite &)96828f6c2f2SEnji Cooper   virtual void OnTestSuiteEnd(const TestSuite& /*test_suite*/) {}
96928f6c2f2SEnji Cooper 
97028f6c2f2SEnji Cooper //  Legacy API is deprecated but still available
97128f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
OnTestCaseEnd(const TestCase &)97228f6c2f2SEnji Cooper   virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {}
97328f6c2f2SEnji Cooper #endif  //  GTEST_REMOVE_LEGACY_TEST_CASEAPI_
974b89a7cc2SEnji Cooper 
975b89a7cc2SEnji Cooper   // Fired before environment tear-down for each iteration of tests starts.
976b89a7cc2SEnji Cooper   virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0;
977b89a7cc2SEnji Cooper 
978b89a7cc2SEnji Cooper   // Fired after environment tear-down for each iteration of tests ends.
979b89a7cc2SEnji Cooper   virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0;
980b89a7cc2SEnji Cooper 
981b89a7cc2SEnji Cooper   // Fired after each iteration of tests finishes.
98228f6c2f2SEnji Cooper   virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration) = 0;
983b89a7cc2SEnji Cooper 
984b89a7cc2SEnji Cooper   // Fired after all test activities have ended.
985b89a7cc2SEnji Cooper   virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0;
986b89a7cc2SEnji Cooper };
987b89a7cc2SEnji Cooper 
988b89a7cc2SEnji Cooper // The convenience class for users who need to override just one or two
989b89a7cc2SEnji Cooper // methods and are not concerned that a possible change to a signature of
990b89a7cc2SEnji Cooper // the methods they override will not be caught during the build.  For
991b89a7cc2SEnji Cooper // comments about each method please see the definition of TestEventListener
992b89a7cc2SEnji Cooper // above.
993b89a7cc2SEnji Cooper class EmptyTestEventListener : public TestEventListener {
994b89a7cc2SEnji Cooper  public:
OnTestProgramStart(const UnitTest &)99528f6c2f2SEnji Cooper   void OnTestProgramStart(const UnitTest& /*unit_test*/) override {}
OnTestIterationStart(const UnitTest &,int)99628f6c2f2SEnji Cooper   void OnTestIterationStart(const UnitTest& /*unit_test*/,
99728f6c2f2SEnji Cooper                             int /*iteration*/) override {}
OnEnvironmentsSetUpStart(const UnitTest &)99828f6c2f2SEnji Cooper   void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) override {}
OnEnvironmentsSetUpEnd(const UnitTest &)99928f6c2f2SEnji Cooper   void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) override {}
OnTestSuiteStart(const TestSuite &)100028f6c2f2SEnji Cooper   void OnTestSuiteStart(const TestSuite& /*test_suite*/) override {}
100128f6c2f2SEnji Cooper //  Legacy API is deprecated but still available
100228f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
OnTestCaseStart(const TestCase &)100328f6c2f2SEnji Cooper   void OnTestCaseStart(const TestCase& /*test_case*/) override {}
100428f6c2f2SEnji Cooper #endif  //  GTEST_REMOVE_LEGACY_TEST_CASEAPI_
100528f6c2f2SEnji Cooper 
OnTestStart(const TestInfo &)100628f6c2f2SEnji Cooper   void OnTestStart(const TestInfo& /*test_info*/) override {}
OnTestDisabled(const TestInfo &)100728f6c2f2SEnji Cooper   void OnTestDisabled(const TestInfo& /*test_info*/) override {}
OnTestPartResult(const TestPartResult &)100828f6c2f2SEnji Cooper   void OnTestPartResult(const TestPartResult& /*test_part_result*/) override {}
OnTestEnd(const TestInfo &)100928f6c2f2SEnji Cooper   void OnTestEnd(const TestInfo& /*test_info*/) override {}
OnTestSuiteEnd(const TestSuite &)101028f6c2f2SEnji Cooper   void OnTestSuiteEnd(const TestSuite& /*test_suite*/) override {}
101128f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
OnTestCaseEnd(const TestCase &)101228f6c2f2SEnji Cooper   void OnTestCaseEnd(const TestCase& /*test_case*/) override {}
101328f6c2f2SEnji Cooper #endif  //  GTEST_REMOVE_LEGACY_TEST_CASEAPI_
101428f6c2f2SEnji Cooper 
OnEnvironmentsTearDownStart(const UnitTest &)101528f6c2f2SEnji Cooper   void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) override {}
OnEnvironmentsTearDownEnd(const UnitTest &)101628f6c2f2SEnji Cooper   void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) override {}
OnTestIterationEnd(const UnitTest &,int)101728f6c2f2SEnji Cooper   void OnTestIterationEnd(const UnitTest& /*unit_test*/,
101828f6c2f2SEnji Cooper                           int /*iteration*/) override {}
OnTestProgramEnd(const UnitTest &)101928f6c2f2SEnji Cooper   void OnTestProgramEnd(const UnitTest& /*unit_test*/) override {}
1020b89a7cc2SEnji Cooper };
1021b89a7cc2SEnji Cooper 
1022b89a7cc2SEnji Cooper // TestEventListeners lets users add listeners to track events in Google Test.
1023b89a7cc2SEnji Cooper class GTEST_API_ TestEventListeners {
1024b89a7cc2SEnji Cooper  public:
1025b89a7cc2SEnji Cooper   TestEventListeners();
1026b89a7cc2SEnji Cooper   ~TestEventListeners();
1027b89a7cc2SEnji Cooper 
1028b89a7cc2SEnji Cooper   // Appends an event listener to the end of the list. Google Test assumes
1029b89a7cc2SEnji Cooper   // the ownership of the listener (i.e. it will delete the listener when
1030b89a7cc2SEnji Cooper   // the test program finishes).
1031b89a7cc2SEnji Cooper   void Append(TestEventListener* listener);
1032b89a7cc2SEnji Cooper 
1033b89a7cc2SEnji Cooper   // Removes the given event listener from the list and returns it.  It then
1034b89a7cc2SEnji Cooper   // becomes the caller's responsibility to delete the listener. Returns
1035b89a7cc2SEnji Cooper   // NULL if the listener is not found in the list.
1036b89a7cc2SEnji Cooper   TestEventListener* Release(TestEventListener* listener);
1037b89a7cc2SEnji Cooper 
1038b89a7cc2SEnji Cooper   // Returns the standard listener responsible for the default console
1039b89a7cc2SEnji Cooper   // output.  Can be removed from the listeners list to shut down default
1040b89a7cc2SEnji Cooper   // console output.  Note that removing this object from the listener list
1041b89a7cc2SEnji Cooper   // with Release transfers its ownership to the caller and makes this
1042b89a7cc2SEnji Cooper   // function return NULL the next time.
default_result_printer()1043b89a7cc2SEnji Cooper   TestEventListener* default_result_printer() const {
1044b89a7cc2SEnji Cooper     return default_result_printer_;
1045b89a7cc2SEnji Cooper   }
1046b89a7cc2SEnji Cooper 
1047b89a7cc2SEnji Cooper   // Returns the standard listener responsible for the default XML output
1048b89a7cc2SEnji Cooper   // controlled by the --gtest_output=xml flag.  Can be removed from the
1049b89a7cc2SEnji Cooper   // listeners list by users who want to shut down the default XML output
1050b89a7cc2SEnji Cooper   // controlled by this flag and substitute it with custom one.  Note that
1051b89a7cc2SEnji Cooper   // removing this object from the listener list with Release transfers its
1052b89a7cc2SEnji Cooper   // ownership to the caller and makes this function return NULL the next
1053b89a7cc2SEnji Cooper   // time.
default_xml_generator()1054b89a7cc2SEnji Cooper   TestEventListener* default_xml_generator() const {
1055b89a7cc2SEnji Cooper     return default_xml_generator_;
1056b89a7cc2SEnji Cooper   }
1057b89a7cc2SEnji Cooper 
105828f6c2f2SEnji Cooper   // Controls whether events will be forwarded by the repeater to the
105928f6c2f2SEnji Cooper   // listeners in the list.
106028f6c2f2SEnji Cooper   void SuppressEventForwarding(bool);
106128f6c2f2SEnji Cooper 
1062b89a7cc2SEnji Cooper  private:
106328f6c2f2SEnji Cooper   friend class TestSuite;
1064b89a7cc2SEnji Cooper   friend class TestInfo;
1065b89a7cc2SEnji Cooper   friend class internal::DefaultGlobalTestPartResultReporter;
1066b89a7cc2SEnji Cooper   friend class internal::NoExecDeathTest;
1067b89a7cc2SEnji Cooper   friend class internal::TestEventListenersAccessor;
1068b89a7cc2SEnji Cooper   friend class internal::UnitTestImpl;
1069b89a7cc2SEnji Cooper 
1070b89a7cc2SEnji Cooper   // Returns repeater that broadcasts the TestEventListener events to all
1071b89a7cc2SEnji Cooper   // subscribers.
1072b89a7cc2SEnji Cooper   TestEventListener* repeater();
1073b89a7cc2SEnji Cooper 
1074b89a7cc2SEnji Cooper   // Sets the default_result_printer attribute to the provided listener.
1075b89a7cc2SEnji Cooper   // The listener is also added to the listener list and previous
1076b89a7cc2SEnji Cooper   // default_result_printer is removed from it and deleted. The listener can
1077b89a7cc2SEnji Cooper   // also be NULL in which case it will not be added to the list. Does
1078b89a7cc2SEnji Cooper   // nothing if the previous and the current listener objects are the same.
1079b89a7cc2SEnji Cooper   void SetDefaultResultPrinter(TestEventListener* listener);
1080b89a7cc2SEnji Cooper 
1081b89a7cc2SEnji Cooper   // Sets the default_xml_generator attribute to the provided listener.  The
1082b89a7cc2SEnji Cooper   // listener is also added to the listener list and previous
1083b89a7cc2SEnji Cooper   // default_xml_generator is removed from it and deleted. The listener can
1084b89a7cc2SEnji Cooper   // also be NULL in which case it will not be added to the list. Does
1085b89a7cc2SEnji Cooper   // nothing if the previous and the current listener objects are the same.
1086b89a7cc2SEnji Cooper   void SetDefaultXmlGenerator(TestEventListener* listener);
1087b89a7cc2SEnji Cooper 
1088b89a7cc2SEnji Cooper   // Controls whether events will be forwarded by the repeater to the
1089b89a7cc2SEnji Cooper   // listeners in the list.
1090b89a7cc2SEnji Cooper   bool EventForwardingEnabled() const;
1091b89a7cc2SEnji Cooper 
1092b89a7cc2SEnji Cooper   // The actual list of listeners.
1093b89a7cc2SEnji Cooper   internal::TestEventRepeater* repeater_;
1094b89a7cc2SEnji Cooper   // Listener responsible for the standard result output.
1095b89a7cc2SEnji Cooper   TestEventListener* default_result_printer_;
1096b89a7cc2SEnji Cooper   // Listener responsible for the creation of the XML output file.
1097b89a7cc2SEnji Cooper   TestEventListener* default_xml_generator_;
1098b89a7cc2SEnji Cooper 
1099b89a7cc2SEnji Cooper   // We disallow copying TestEventListeners.
110028f6c2f2SEnji Cooper   TestEventListeners(const TestEventListeners&) = delete;
110128f6c2f2SEnji Cooper   TestEventListeners& operator=(const TestEventListeners&) = delete;
1102b89a7cc2SEnji Cooper };
1103b89a7cc2SEnji Cooper 
110428f6c2f2SEnji Cooper // A UnitTest consists of a vector of TestSuites.
1105b89a7cc2SEnji Cooper //
1106b89a7cc2SEnji Cooper // This is a singleton class.  The only instance of UnitTest is
1107b89a7cc2SEnji Cooper // created when UnitTest::GetInstance() is first called.  This
1108b89a7cc2SEnji Cooper // instance is never deleted.
1109b89a7cc2SEnji Cooper //
1110b89a7cc2SEnji Cooper // UnitTest is not copyable.
1111b89a7cc2SEnji Cooper //
1112b89a7cc2SEnji Cooper // This class is thread-safe as long as the methods are called
1113b89a7cc2SEnji Cooper // according to their specification.
1114b89a7cc2SEnji Cooper class GTEST_API_ UnitTest {
1115b89a7cc2SEnji Cooper  public:
1116b89a7cc2SEnji Cooper   // Gets the singleton UnitTest object.  The first time this method
1117b89a7cc2SEnji Cooper   // is called, a UnitTest object is constructed and returned.
1118b89a7cc2SEnji Cooper   // Consecutive calls will return the same object.
1119b89a7cc2SEnji Cooper   static UnitTest* GetInstance();
1120b89a7cc2SEnji Cooper 
1121b89a7cc2SEnji Cooper   // Runs all tests in this UnitTest object and prints the result.
1122b89a7cc2SEnji Cooper   // Returns 0 if successful, or 1 otherwise.
1123b89a7cc2SEnji Cooper   //
1124b89a7cc2SEnji Cooper   // This method can only be called from the main thread.
1125b89a7cc2SEnji Cooper   //
1126b89a7cc2SEnji Cooper   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1127b89a7cc2SEnji Cooper   int Run() GTEST_MUST_USE_RESULT_;
1128b89a7cc2SEnji Cooper 
1129b89a7cc2SEnji Cooper   // Returns the working directory when the first TEST() or TEST_F()
1130b89a7cc2SEnji Cooper   // was executed.  The UnitTest object owns the string.
1131b89a7cc2SEnji Cooper   const char* original_working_dir() const;
1132b89a7cc2SEnji Cooper 
113328f6c2f2SEnji Cooper   // Returns the TestSuite object for the test that's currently running,
1134b89a7cc2SEnji Cooper   // or NULL if no test is running.
113528f6c2f2SEnji Cooper   const TestSuite* current_test_suite() const GTEST_LOCK_EXCLUDED_(mutex_);
113628f6c2f2SEnji Cooper 
113728f6c2f2SEnji Cooper // Legacy API is still available but deprecated
113828f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
113928f6c2f2SEnji Cooper   const TestCase* current_test_case() const GTEST_LOCK_EXCLUDED_(mutex_);
114028f6c2f2SEnji Cooper #endif
1141b89a7cc2SEnji Cooper 
1142b89a7cc2SEnji Cooper   // Returns the TestInfo object for the test that's currently running,
1143b89a7cc2SEnji Cooper   // or NULL if no test is running.
114428f6c2f2SEnji Cooper   const TestInfo* current_test_info() const GTEST_LOCK_EXCLUDED_(mutex_);
1145b89a7cc2SEnji Cooper 
1146b89a7cc2SEnji Cooper   // Returns the random seed used at the start of the current test run.
1147b89a7cc2SEnji Cooper   int random_seed() const;
1148b89a7cc2SEnji Cooper 
114928f6c2f2SEnji Cooper   // Returns the ParameterizedTestSuiteRegistry object used to keep track of
1150b89a7cc2SEnji Cooper   // value-parameterized tests and instantiate and register them.
1151b89a7cc2SEnji Cooper   //
1152b89a7cc2SEnji Cooper   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
115328f6c2f2SEnji Cooper   internal::ParameterizedTestSuiteRegistry& parameterized_test_registry()
1154b89a7cc2SEnji Cooper       GTEST_LOCK_EXCLUDED_(mutex_);
1155b89a7cc2SEnji Cooper 
115628f6c2f2SEnji Cooper   // Gets the number of successful test suites.
115728f6c2f2SEnji Cooper   int successful_test_suite_count() const;
1158b89a7cc2SEnji Cooper 
115928f6c2f2SEnji Cooper   // Gets the number of failed test suites.
116028f6c2f2SEnji Cooper   int failed_test_suite_count() const;
1161b89a7cc2SEnji Cooper 
116228f6c2f2SEnji Cooper   // Gets the number of all test suites.
116328f6c2f2SEnji Cooper   int total_test_suite_count() const;
1164b89a7cc2SEnji Cooper 
116528f6c2f2SEnji Cooper   // Gets the number of all test suites that contain at least one test
1166b89a7cc2SEnji Cooper   // that should run.
116728f6c2f2SEnji Cooper   int test_suite_to_run_count() const;
116828f6c2f2SEnji Cooper 
116928f6c2f2SEnji Cooper   //  Legacy API is deprecated but still available
117028f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
117128f6c2f2SEnji Cooper   int successful_test_case_count() const;
117228f6c2f2SEnji Cooper   int failed_test_case_count() const;
117328f6c2f2SEnji Cooper   int total_test_case_count() const;
1174b89a7cc2SEnji Cooper   int test_case_to_run_count() const;
117528f6c2f2SEnji Cooper #endif  //  GTEST_REMOVE_LEGACY_TEST_CASEAPI_
1176b89a7cc2SEnji Cooper 
1177b89a7cc2SEnji Cooper   // Gets the number of successful tests.
1178b89a7cc2SEnji Cooper   int successful_test_count() const;
1179b89a7cc2SEnji Cooper 
1180da1a9eb0SAlan Somers   // Gets the number of skipped tests.
1181da1a9eb0SAlan Somers   int skipped_test_count() const;
1182da1a9eb0SAlan Somers 
1183b89a7cc2SEnji Cooper   // Gets the number of failed tests.
1184b89a7cc2SEnji Cooper   int failed_test_count() const;
1185b89a7cc2SEnji Cooper 
1186b89a7cc2SEnji Cooper   // Gets the number of disabled tests that will be reported in the XML report.
1187b89a7cc2SEnji Cooper   int reportable_disabled_test_count() const;
1188b89a7cc2SEnji Cooper 
1189b89a7cc2SEnji Cooper   // Gets the number of disabled tests.
1190b89a7cc2SEnji Cooper   int disabled_test_count() const;
1191b89a7cc2SEnji Cooper 
1192b89a7cc2SEnji Cooper   // Gets the number of tests to be printed in the XML report.
1193b89a7cc2SEnji Cooper   int reportable_test_count() const;
1194b89a7cc2SEnji Cooper 
1195b89a7cc2SEnji Cooper   // Gets the number of all tests.
1196b89a7cc2SEnji Cooper   int total_test_count() const;
1197b89a7cc2SEnji Cooper 
1198b89a7cc2SEnji Cooper   // Gets the number of tests that should run.
1199b89a7cc2SEnji Cooper   int test_to_run_count() const;
1200b89a7cc2SEnji Cooper 
1201b89a7cc2SEnji Cooper   // Gets the time of the test program start, in ms from the start of the
1202b89a7cc2SEnji Cooper   // UNIX epoch.
1203b89a7cc2SEnji Cooper   TimeInMillis start_timestamp() const;
1204b89a7cc2SEnji Cooper 
1205b89a7cc2SEnji Cooper   // Gets the elapsed time, in milliseconds.
1206b89a7cc2SEnji Cooper   TimeInMillis elapsed_time() const;
1207b89a7cc2SEnji Cooper 
120828f6c2f2SEnji Cooper   // Returns true if and only if the unit test passed (i.e. all test suites
120928f6c2f2SEnji Cooper   // passed).
1210b89a7cc2SEnji Cooper   bool Passed() const;
1211b89a7cc2SEnji Cooper 
121228f6c2f2SEnji Cooper   // Returns true if and only if the unit test failed (i.e. some test suite
121328f6c2f2SEnji Cooper   // failed or something outside of all tests failed).
1214b89a7cc2SEnji Cooper   bool Failed() const;
1215b89a7cc2SEnji Cooper 
121628f6c2f2SEnji Cooper   // Gets the i-th test suite among all the test suites. i can range from 0 to
121728f6c2f2SEnji Cooper   // total_test_suite_count() - 1. If i is not in that range, returns NULL.
121828f6c2f2SEnji Cooper   const TestSuite* GetTestSuite(int i) const;
121928f6c2f2SEnji Cooper 
122028f6c2f2SEnji Cooper //  Legacy API is deprecated but still available
122128f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
1222b89a7cc2SEnji Cooper   const TestCase* GetTestCase(int i) const;
122328f6c2f2SEnji Cooper #endif  //  GTEST_REMOVE_LEGACY_TEST_CASEAPI_
1224b89a7cc2SEnji Cooper 
1225b89a7cc2SEnji Cooper   // Returns the TestResult containing information on test failures and
122628f6c2f2SEnji Cooper   // properties logged outside of individual test suites.
1227b89a7cc2SEnji Cooper   const TestResult& ad_hoc_test_result() const;
1228b89a7cc2SEnji Cooper 
1229b89a7cc2SEnji Cooper   // Returns the list of event listeners that can be used to track events
1230b89a7cc2SEnji Cooper   // inside Google Test.
1231b89a7cc2SEnji Cooper   TestEventListeners& listeners();
1232b89a7cc2SEnji Cooper 
1233b89a7cc2SEnji Cooper  private:
1234b89a7cc2SEnji Cooper   // Registers and returns a global test environment.  When a test
1235b89a7cc2SEnji Cooper   // program is run, all global test environments will be set-up in
1236b89a7cc2SEnji Cooper   // the order they were registered.  After all tests in the program
1237b89a7cc2SEnji Cooper   // have finished, all global test environments will be torn-down in
1238b89a7cc2SEnji Cooper   // the *reverse* order they were registered.
1239b89a7cc2SEnji Cooper   //
1240b89a7cc2SEnji Cooper   // The UnitTest object takes ownership of the given environment.
1241b89a7cc2SEnji Cooper   //
1242b89a7cc2SEnji Cooper   // This method can only be called from the main thread.
1243b89a7cc2SEnji Cooper   Environment* AddEnvironment(Environment* env);
1244b89a7cc2SEnji Cooper 
1245b89a7cc2SEnji Cooper   // Adds a TestPartResult to the current TestResult object.  All
1246b89a7cc2SEnji Cooper   // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc)
1247b89a7cc2SEnji Cooper   // eventually call this to report their results.  The user code
1248b89a7cc2SEnji Cooper   // should use the assertion macros instead of calling this directly.
1249b89a7cc2SEnji Cooper   void AddTestPartResult(TestPartResult::Type result_type,
125028f6c2f2SEnji Cooper                          const char* file_name, int line_number,
1251b89a7cc2SEnji Cooper                          const std::string& message,
1252b89a7cc2SEnji Cooper                          const std::string& os_stack_trace)
1253b89a7cc2SEnji Cooper       GTEST_LOCK_EXCLUDED_(mutex_);
1254b89a7cc2SEnji Cooper 
1255b89a7cc2SEnji Cooper   // Adds a TestProperty to the current TestResult object when invoked from
125628f6c2f2SEnji Cooper   // inside a test, to current TestSuite's ad_hoc_test_result_ when invoked
125728f6c2f2SEnji Cooper   // from SetUpTestSuite or TearDownTestSuite, or to the global property set
1258b89a7cc2SEnji Cooper   // when invoked elsewhere.  If the result already contains a property with
1259b89a7cc2SEnji Cooper   // the same key, the value will be updated.
1260b89a7cc2SEnji Cooper   void RecordProperty(const std::string& key, const std::string& value);
1261b89a7cc2SEnji Cooper 
126228f6c2f2SEnji Cooper   // Gets the i-th test suite among all the test suites. i can range from 0 to
126328f6c2f2SEnji Cooper   // total_test_suite_count() - 1. If i is not in that range, returns NULL.
126428f6c2f2SEnji Cooper   TestSuite* GetMutableTestSuite(int i);
1265b89a7cc2SEnji Cooper 
1266b89a7cc2SEnji Cooper   // Accessors for the implementation object.
impl()1267b89a7cc2SEnji Cooper   internal::UnitTestImpl* impl() { return impl_; }
impl()1268b89a7cc2SEnji Cooper   const internal::UnitTestImpl* impl() const { return impl_; }
1269b89a7cc2SEnji Cooper 
1270b89a7cc2SEnji Cooper   // These classes and functions are friends as they need to access private
1271b89a7cc2SEnji Cooper   // members of UnitTest.
1272b89a7cc2SEnji Cooper   friend class ScopedTrace;
1273b89a7cc2SEnji Cooper   friend class Test;
1274b89a7cc2SEnji Cooper   friend class internal::AssertHelper;
1275b89a7cc2SEnji Cooper   friend class internal::StreamingListenerTest;
1276b89a7cc2SEnji Cooper   friend class internal::UnitTestRecordPropertyTestHelper;
1277b89a7cc2SEnji Cooper   friend Environment* AddGlobalTestEnvironment(Environment* env);
127828f6c2f2SEnji Cooper   friend std::set<std::string>* internal::GetIgnoredParameterizedTestSuites();
1279b89a7cc2SEnji Cooper   friend internal::UnitTestImpl* internal::GetUnitTestImpl();
1280b89a7cc2SEnji Cooper   friend void internal::ReportFailureInUnknownLocation(
128128f6c2f2SEnji Cooper       TestPartResult::Type result_type, const std::string& message);
1282b89a7cc2SEnji Cooper 
1283b89a7cc2SEnji Cooper   // Creates an empty UnitTest.
1284b89a7cc2SEnji Cooper   UnitTest();
1285b89a7cc2SEnji Cooper 
1286b89a7cc2SEnji Cooper   // D'tor
1287b89a7cc2SEnji Cooper   virtual ~UnitTest();
1288b89a7cc2SEnji Cooper 
1289b89a7cc2SEnji Cooper   // Pushes a trace defined by SCOPED_TRACE() on to the per-thread
1290b89a7cc2SEnji Cooper   // Google Test trace stack.
1291b89a7cc2SEnji Cooper   void PushGTestTrace(const internal::TraceInfo& trace)
1292b89a7cc2SEnji Cooper       GTEST_LOCK_EXCLUDED_(mutex_);
1293b89a7cc2SEnji Cooper 
1294b89a7cc2SEnji Cooper   // Pops a trace from the per-thread Google Test trace stack.
129528f6c2f2SEnji Cooper   void PopGTestTrace() GTEST_LOCK_EXCLUDED_(mutex_);
1296b89a7cc2SEnji Cooper 
1297b89a7cc2SEnji Cooper   // Protects mutable state in *impl_.  This is mutable as some const
1298b89a7cc2SEnji Cooper   // methods need to lock it too.
1299b89a7cc2SEnji Cooper   mutable internal::Mutex mutex_;
1300b89a7cc2SEnji Cooper 
1301b89a7cc2SEnji Cooper   // Opaque implementation object.  This field is never changed once
1302b89a7cc2SEnji Cooper   // the object is constructed.  We don't mark it as const here, as
1303b89a7cc2SEnji Cooper   // doing so will cause a warning in the constructor of UnitTest.
1304b89a7cc2SEnji Cooper   // Mutable state in *impl_ is protected by mutex_.
1305b89a7cc2SEnji Cooper   internal::UnitTestImpl* impl_;
1306b89a7cc2SEnji Cooper 
1307b89a7cc2SEnji Cooper   // We disallow copying UnitTest.
130828f6c2f2SEnji Cooper   UnitTest(const UnitTest&) = delete;
130928f6c2f2SEnji Cooper   UnitTest& operator=(const UnitTest&) = delete;
1310b89a7cc2SEnji Cooper };
1311b89a7cc2SEnji Cooper 
1312b89a7cc2SEnji Cooper // A convenient wrapper for adding an environment for the test
1313b89a7cc2SEnji Cooper // program.
1314b89a7cc2SEnji Cooper //
1315b89a7cc2SEnji Cooper // You should call this before RUN_ALL_TESTS() is called, probably in
1316b89a7cc2SEnji Cooper // main().  If you use gtest_main, you need to call this before main()
1317b89a7cc2SEnji Cooper // starts for it to take effect.  For example, you can define a global
1318b89a7cc2SEnji Cooper // variable like this:
1319b89a7cc2SEnji Cooper //
1320b89a7cc2SEnji Cooper //   testing::Environment* const foo_env =
1321b89a7cc2SEnji Cooper //       testing::AddGlobalTestEnvironment(new FooEnvironment);
1322b89a7cc2SEnji Cooper //
1323b89a7cc2SEnji Cooper // However, we strongly recommend you to write your own main() and
1324b89a7cc2SEnji Cooper // call AddGlobalTestEnvironment() there, as relying on initialization
1325b89a7cc2SEnji Cooper // of global variables makes the code harder to read and may cause
1326b89a7cc2SEnji Cooper // problems when you register multiple environments from different
1327b89a7cc2SEnji Cooper // translation units and the environments have dependencies among them
1328b89a7cc2SEnji Cooper // (remember that the compiler doesn't guarantee the order in which
1329b89a7cc2SEnji Cooper // global variables from different translation units are initialized).
AddGlobalTestEnvironment(Environment * env)1330b89a7cc2SEnji Cooper inline Environment* AddGlobalTestEnvironment(Environment* env) {
1331b89a7cc2SEnji Cooper   return UnitTest::GetInstance()->AddEnvironment(env);
1332b89a7cc2SEnji Cooper }
1333b89a7cc2SEnji Cooper 
1334b89a7cc2SEnji Cooper // Initializes Google Test.  This must be called before calling
1335b89a7cc2SEnji Cooper // RUN_ALL_TESTS().  In particular, it parses a command line for the
1336b89a7cc2SEnji Cooper // flags that Google Test recognizes.  Whenever a Google Test flag is
1337b89a7cc2SEnji Cooper // seen, it is removed from argv, and *argc is decremented.
1338b89a7cc2SEnji Cooper //
1339b89a7cc2SEnji Cooper // No value is returned.  Instead, the Google Test flag variables are
1340b89a7cc2SEnji Cooper // updated.
1341b89a7cc2SEnji Cooper //
1342b89a7cc2SEnji Cooper // Calling the function for the second time has no user-visible effect.
1343b89a7cc2SEnji Cooper GTEST_API_ void InitGoogleTest(int* argc, char** argv);
1344b89a7cc2SEnji Cooper 
1345b89a7cc2SEnji Cooper // This overloaded version can be used in Windows programs compiled in
1346b89a7cc2SEnji Cooper // UNICODE mode.
1347b89a7cc2SEnji Cooper GTEST_API_ void InitGoogleTest(int* argc, wchar_t** argv);
1348b89a7cc2SEnji Cooper 
134928f6c2f2SEnji Cooper // This overloaded version can be used on Arduino/embedded platforms where
135028f6c2f2SEnji Cooper // there is no argc/argv.
135128f6c2f2SEnji Cooper GTEST_API_ void InitGoogleTest();
135228f6c2f2SEnji Cooper 
1353b89a7cc2SEnji Cooper namespace internal {
1354b89a7cc2SEnji Cooper 
1355b89a7cc2SEnji Cooper // Separate the error generating code from the code path to reduce the stack
1356b89a7cc2SEnji Cooper // frame size of CmpHelperEQ. This helps reduce the overhead of some sanitizers
1357b89a7cc2SEnji Cooper // when calling EXPECT_* in a tight loop.
1358b89a7cc2SEnji Cooper template <typename T1, typename T2>
CmpHelperEQFailure(const char * lhs_expression,const char * rhs_expression,const T1 & lhs,const T2 & rhs)1359b89a7cc2SEnji Cooper AssertionResult CmpHelperEQFailure(const char* lhs_expression,
136028f6c2f2SEnji Cooper                                    const char* rhs_expression, const T1& lhs,
136128f6c2f2SEnji Cooper                                    const T2& rhs) {
136228f6c2f2SEnji Cooper   return EqFailure(lhs_expression, rhs_expression,
1363b89a7cc2SEnji Cooper                    FormatForComparisonFailureMessage(lhs, rhs),
136428f6c2f2SEnji Cooper                    FormatForComparisonFailureMessage(rhs, lhs), false);
1365b89a7cc2SEnji Cooper }
1366b89a7cc2SEnji Cooper 
136728f6c2f2SEnji Cooper // This block of code defines operator==/!=
136828f6c2f2SEnji Cooper // to block lexical scope lookup.
136928f6c2f2SEnji Cooper // It prevents using invalid operator==/!= defined at namespace scope.
137028f6c2f2SEnji Cooper struct faketype {};
137128f6c2f2SEnji Cooper inline bool operator==(faketype, faketype) { return true; }
137228f6c2f2SEnji Cooper inline bool operator!=(faketype, faketype) { return false; }
137328f6c2f2SEnji Cooper 
1374b89a7cc2SEnji Cooper // The helper function for {ASSERT|EXPECT}_EQ.
1375b89a7cc2SEnji Cooper template <typename T1, typename T2>
CmpHelperEQ(const char * lhs_expression,const char * rhs_expression,const T1 & lhs,const T2 & rhs)1376b89a7cc2SEnji Cooper AssertionResult CmpHelperEQ(const char* lhs_expression,
137728f6c2f2SEnji Cooper                             const char* rhs_expression, const T1& lhs,
1378b89a7cc2SEnji Cooper                             const T2& rhs) {
1379b89a7cc2SEnji Cooper   if (lhs == rhs) {
1380b89a7cc2SEnji Cooper     return AssertionSuccess();
1381b89a7cc2SEnji Cooper   }
1382b89a7cc2SEnji Cooper 
1383b89a7cc2SEnji Cooper   return CmpHelperEQFailure(lhs_expression, rhs_expression, lhs, rhs);
1384b89a7cc2SEnji Cooper }
1385b89a7cc2SEnji Cooper 
1386b89a7cc2SEnji Cooper class EqHelper {
1387b89a7cc2SEnji Cooper  public:
1388b89a7cc2SEnji Cooper   // This templatized version is for the general case.
138928f6c2f2SEnji Cooper   template <
139028f6c2f2SEnji Cooper       typename T1, typename T2,
139128f6c2f2SEnji Cooper       // Disable this overload for cases where one argument is a pointer
139228f6c2f2SEnji Cooper       // and the other is the null pointer constant.
139328f6c2f2SEnji Cooper       typename std::enable_if<!std::is_integral<T1>::value ||
139428f6c2f2SEnji Cooper                               !std::is_pointer<T2>::value>::type* = nullptr>
Compare(const char * lhs_expression,const char * rhs_expression,const T1 & lhs,const T2 & rhs)1395b89a7cc2SEnji Cooper   static AssertionResult Compare(const char* lhs_expression,
139628f6c2f2SEnji Cooper                                  const char* rhs_expression, const T1& lhs,
1397b89a7cc2SEnji Cooper                                  const T2& rhs) {
1398b89a7cc2SEnji Cooper     return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs);
1399b89a7cc2SEnji Cooper   }
1400b89a7cc2SEnji Cooper 
1401b89a7cc2SEnji Cooper   // With this overloaded version, we allow anonymous enums to be used
1402b89a7cc2SEnji Cooper   // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous
1403b89a7cc2SEnji Cooper   // enums can be implicitly cast to BiggestInt.
1404b89a7cc2SEnji Cooper   //
1405b89a7cc2SEnji Cooper   // Even though its body looks the same as the above version, we
1406b89a7cc2SEnji Cooper   // cannot merge the two, as it will make anonymous enums unhappy.
Compare(const char * lhs_expression,const char * rhs_expression,BiggestInt lhs,BiggestInt rhs)1407b89a7cc2SEnji Cooper   static AssertionResult Compare(const char* lhs_expression,
140828f6c2f2SEnji Cooper                                  const char* rhs_expression, BiggestInt lhs,
1409b89a7cc2SEnji Cooper                                  BiggestInt rhs) {
1410b89a7cc2SEnji Cooper     return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs);
1411b89a7cc2SEnji Cooper   }
1412b89a7cc2SEnji Cooper 
1413b89a7cc2SEnji Cooper   template <typename T>
Compare(const char * lhs_expression,const char * rhs_expression,std::nullptr_t,T * rhs)1414b89a7cc2SEnji Cooper   static AssertionResult Compare(
141528f6c2f2SEnji Cooper       const char* lhs_expression, const char* rhs_expression,
141628f6c2f2SEnji Cooper       // Handle cases where '0' is used as a null pointer literal.
141728f6c2f2SEnji Cooper       std::nullptr_t /* lhs */, T* rhs) {
1418b89a7cc2SEnji Cooper     // We already know that 'lhs' is a null pointer.
141928f6c2f2SEnji Cooper     return CmpHelperEQ(lhs_expression, rhs_expression, static_cast<T*>(nullptr),
142028f6c2f2SEnji Cooper                        rhs);
1421b89a7cc2SEnji Cooper   }
1422b89a7cc2SEnji Cooper };
1423b89a7cc2SEnji Cooper 
1424b89a7cc2SEnji Cooper // Separate the error generating code from the code path to reduce the stack
1425b89a7cc2SEnji Cooper // frame size of CmpHelperOP. This helps reduce the overhead of some sanitizers
1426b89a7cc2SEnji Cooper // when calling EXPECT_OP in a tight loop.
1427b89a7cc2SEnji Cooper template <typename T1, typename T2>
CmpHelperOpFailure(const char * expr1,const char * expr2,const T1 & val1,const T2 & val2,const char * op)1428b89a7cc2SEnji Cooper AssertionResult CmpHelperOpFailure(const char* expr1, const char* expr2,
1429b89a7cc2SEnji Cooper                                    const T1& val1, const T2& val2,
1430b89a7cc2SEnji Cooper                                    const char* op) {
1431b89a7cc2SEnji Cooper   return AssertionFailure()
1432b89a7cc2SEnji Cooper          << "Expected: (" << expr1 << ") " << op << " (" << expr2
1433b89a7cc2SEnji Cooper          << "), actual: " << FormatForComparisonFailureMessage(val1, val2)
1434b89a7cc2SEnji Cooper          << " vs " << FormatForComparisonFailureMessage(val2, val1);
1435b89a7cc2SEnji Cooper }
1436b89a7cc2SEnji Cooper 
1437b89a7cc2SEnji Cooper // A macro for implementing the helper functions needed to implement
1438b89a7cc2SEnji Cooper // ASSERT_?? and EXPECT_??.  It is here just to avoid copy-and-paste
1439b89a7cc2SEnji Cooper // of similar code.
1440b89a7cc2SEnji Cooper //
1441b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1442b89a7cc2SEnji Cooper 
1443b89a7cc2SEnji Cooper #define GTEST_IMPL_CMP_HELPER_(op_name, op)                                \
1444b89a7cc2SEnji Cooper   template <typename T1, typename T2>                                      \
1445b89a7cc2SEnji Cooper   AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
1446b89a7cc2SEnji Cooper                                      const T1& val1, const T2& val2) {     \
1447b89a7cc2SEnji Cooper     if (val1 op val2) {                                                    \
1448b89a7cc2SEnji Cooper       return AssertionSuccess();                                           \
1449b89a7cc2SEnji Cooper     } else {                                                               \
1450b89a7cc2SEnji Cooper       return CmpHelperOpFailure(expr1, expr2, val1, val2, #op);            \
1451b89a7cc2SEnji Cooper     }                                                                      \
145228f6c2f2SEnji Cooper   }
1453b89a7cc2SEnji Cooper 
1454b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1455b89a7cc2SEnji Cooper 
1456b89a7cc2SEnji Cooper // Implements the helper function for {ASSERT|EXPECT}_NE
145728f6c2f2SEnji Cooper GTEST_IMPL_CMP_HELPER_(NE, !=)
1458b89a7cc2SEnji Cooper // Implements the helper function for {ASSERT|EXPECT}_LE
145928f6c2f2SEnji Cooper GTEST_IMPL_CMP_HELPER_(LE, <=)
1460b89a7cc2SEnji Cooper // Implements the helper function for {ASSERT|EXPECT}_LT
146128f6c2f2SEnji Cooper GTEST_IMPL_CMP_HELPER_(LT, <)
1462b89a7cc2SEnji Cooper // Implements the helper function for {ASSERT|EXPECT}_GE
146328f6c2f2SEnji Cooper GTEST_IMPL_CMP_HELPER_(GE, >=)
1464b89a7cc2SEnji Cooper // Implements the helper function for {ASSERT|EXPECT}_GT
146528f6c2f2SEnji Cooper GTEST_IMPL_CMP_HELPER_(GT, >)
1466b89a7cc2SEnji Cooper 
1467b89a7cc2SEnji Cooper #undef GTEST_IMPL_CMP_HELPER_
1468b89a7cc2SEnji Cooper 
1469b89a7cc2SEnji Cooper // The helper function for {ASSERT|EXPECT}_STREQ.
1470b89a7cc2SEnji Cooper //
1471b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1472b89a7cc2SEnji Cooper GTEST_API_ AssertionResult CmpHelperSTREQ(const char* s1_expression,
1473b89a7cc2SEnji Cooper                                           const char* s2_expression,
147428f6c2f2SEnji Cooper                                           const char* s1, const char* s2);
1475b89a7cc2SEnji Cooper 
1476b89a7cc2SEnji Cooper // The helper function for {ASSERT|EXPECT}_STRCASEEQ.
1477b89a7cc2SEnji Cooper //
1478b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1479b89a7cc2SEnji Cooper GTEST_API_ AssertionResult CmpHelperSTRCASEEQ(const char* s1_expression,
1480b89a7cc2SEnji Cooper                                               const char* s2_expression,
148128f6c2f2SEnji Cooper                                               const char* s1, const char* s2);
1482b89a7cc2SEnji Cooper 
1483b89a7cc2SEnji Cooper // The helper function for {ASSERT|EXPECT}_STRNE.
1484b89a7cc2SEnji Cooper //
1485b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1486b89a7cc2SEnji Cooper GTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression,
1487b89a7cc2SEnji Cooper                                           const char* s2_expression,
148828f6c2f2SEnji Cooper                                           const char* s1, const char* s2);
1489b89a7cc2SEnji Cooper 
1490b89a7cc2SEnji Cooper // The helper function for {ASSERT|EXPECT}_STRCASENE.
1491b89a7cc2SEnji Cooper //
1492b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1493b89a7cc2SEnji Cooper GTEST_API_ AssertionResult CmpHelperSTRCASENE(const char* s1_expression,
1494b89a7cc2SEnji Cooper                                               const char* s2_expression,
149528f6c2f2SEnji Cooper                                               const char* s1, const char* s2);
1496b89a7cc2SEnji Cooper 
1497b89a7cc2SEnji Cooper // Helper function for *_STREQ on wide strings.
1498b89a7cc2SEnji Cooper //
1499b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1500b89a7cc2SEnji Cooper GTEST_API_ AssertionResult CmpHelperSTREQ(const char* s1_expression,
1501b89a7cc2SEnji Cooper                                           const char* s2_expression,
150228f6c2f2SEnji Cooper                                           const wchar_t* s1, const wchar_t* s2);
1503b89a7cc2SEnji Cooper 
1504b89a7cc2SEnji Cooper // Helper function for *_STRNE on wide strings.
1505b89a7cc2SEnji Cooper //
1506b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1507b89a7cc2SEnji Cooper GTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression,
1508b89a7cc2SEnji Cooper                                           const char* s2_expression,
150928f6c2f2SEnji Cooper                                           const wchar_t* s1, const wchar_t* s2);
1510b89a7cc2SEnji Cooper 
1511b89a7cc2SEnji Cooper }  // namespace internal
1512b89a7cc2SEnji Cooper 
1513b89a7cc2SEnji Cooper // IsSubstring() and IsNotSubstring() are intended to be used as the
1514b89a7cc2SEnji Cooper // first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by
1515b89a7cc2SEnji Cooper // themselves.  They check whether needle is a substring of haystack
1516b89a7cc2SEnji Cooper // (NULL is considered a substring of itself only), and return an
1517b89a7cc2SEnji Cooper // appropriate error message when they fail.
1518b89a7cc2SEnji Cooper //
1519b89a7cc2SEnji Cooper // The {needle,haystack}_expr arguments are the stringified
1520b89a7cc2SEnji Cooper // expressions that generated the two real arguments.
152128f6c2f2SEnji Cooper GTEST_API_ AssertionResult IsSubstring(const char* needle_expr,
152228f6c2f2SEnji Cooper                                        const char* haystack_expr,
152328f6c2f2SEnji Cooper                                        const char* needle,
152428f6c2f2SEnji Cooper                                        const char* haystack);
152528f6c2f2SEnji Cooper GTEST_API_ AssertionResult IsSubstring(const char* needle_expr,
152628f6c2f2SEnji Cooper                                        const char* haystack_expr,
152728f6c2f2SEnji Cooper                                        const wchar_t* needle,
152828f6c2f2SEnji Cooper                                        const wchar_t* haystack);
152928f6c2f2SEnji Cooper GTEST_API_ AssertionResult IsNotSubstring(const char* needle_expr,
153028f6c2f2SEnji Cooper                                           const char* haystack_expr,
153128f6c2f2SEnji Cooper                                           const char* needle,
153228f6c2f2SEnji Cooper                                           const char* haystack);
153328f6c2f2SEnji Cooper GTEST_API_ AssertionResult IsNotSubstring(const char* needle_expr,
153428f6c2f2SEnji Cooper                                           const char* haystack_expr,
153528f6c2f2SEnji Cooper                                           const wchar_t* needle,
153628f6c2f2SEnji Cooper                                           const wchar_t* haystack);
153728f6c2f2SEnji Cooper GTEST_API_ AssertionResult IsSubstring(const char* needle_expr,
153828f6c2f2SEnji Cooper                                        const char* haystack_expr,
153928f6c2f2SEnji Cooper                                        const ::std::string& needle,
154028f6c2f2SEnji Cooper                                        const ::std::string& haystack);
154128f6c2f2SEnji Cooper GTEST_API_ AssertionResult IsNotSubstring(const char* needle_expr,
154228f6c2f2SEnji Cooper                                           const char* haystack_expr,
154328f6c2f2SEnji Cooper                                           const ::std::string& needle,
154428f6c2f2SEnji Cooper                                           const ::std::string& haystack);
1545b89a7cc2SEnji Cooper 
1546b89a7cc2SEnji Cooper #if GTEST_HAS_STD_WSTRING
154728f6c2f2SEnji Cooper GTEST_API_ AssertionResult IsSubstring(const char* needle_expr,
154828f6c2f2SEnji Cooper                                        const char* haystack_expr,
154928f6c2f2SEnji Cooper                                        const ::std::wstring& needle,
155028f6c2f2SEnji Cooper                                        const ::std::wstring& haystack);
155128f6c2f2SEnji Cooper GTEST_API_ AssertionResult IsNotSubstring(const char* needle_expr,
155228f6c2f2SEnji Cooper                                           const char* haystack_expr,
155328f6c2f2SEnji Cooper                                           const ::std::wstring& needle,
155428f6c2f2SEnji Cooper                                           const ::std::wstring& haystack);
1555b89a7cc2SEnji Cooper #endif  // GTEST_HAS_STD_WSTRING
1556b89a7cc2SEnji Cooper 
1557b89a7cc2SEnji Cooper namespace internal {
1558b89a7cc2SEnji Cooper 
1559b89a7cc2SEnji Cooper // Helper template function for comparing floating-points.
1560b89a7cc2SEnji Cooper //
1561b89a7cc2SEnji Cooper // Template parameter:
1562b89a7cc2SEnji Cooper //
1563b89a7cc2SEnji Cooper //   RawType: the raw floating-point type (either float or double)
1564b89a7cc2SEnji Cooper //
1565b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1566b89a7cc2SEnji Cooper template <typename RawType>
CmpHelperFloatingPointEQ(const char * lhs_expression,const char * rhs_expression,RawType lhs_value,RawType rhs_value)1567b89a7cc2SEnji Cooper AssertionResult CmpHelperFloatingPointEQ(const char* lhs_expression,
1568b89a7cc2SEnji Cooper                                          const char* rhs_expression,
156928f6c2f2SEnji Cooper                                          RawType lhs_value, RawType rhs_value) {
1570b89a7cc2SEnji Cooper   const FloatingPoint<RawType> lhs(lhs_value), rhs(rhs_value);
1571b89a7cc2SEnji Cooper 
1572b89a7cc2SEnji Cooper   if (lhs.AlmostEquals(rhs)) {
1573b89a7cc2SEnji Cooper     return AssertionSuccess();
1574b89a7cc2SEnji Cooper   }
1575b89a7cc2SEnji Cooper 
1576b89a7cc2SEnji Cooper   ::std::stringstream lhs_ss;
1577b89a7cc2SEnji Cooper   lhs_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
1578b89a7cc2SEnji Cooper          << lhs_value;
1579b89a7cc2SEnji Cooper 
1580b89a7cc2SEnji Cooper   ::std::stringstream rhs_ss;
1581b89a7cc2SEnji Cooper   rhs_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
1582b89a7cc2SEnji Cooper          << rhs_value;
1583b89a7cc2SEnji Cooper 
158428f6c2f2SEnji Cooper   return EqFailure(lhs_expression, rhs_expression,
158528f6c2f2SEnji Cooper                    StringStreamToString(&lhs_ss), StringStreamToString(&rhs_ss),
1586b89a7cc2SEnji Cooper                    false);
1587b89a7cc2SEnji Cooper }
1588b89a7cc2SEnji Cooper 
1589b89a7cc2SEnji Cooper // Helper function for implementing ASSERT_NEAR.
1590b89a7cc2SEnji Cooper //
1591b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1592b89a7cc2SEnji Cooper GTEST_API_ AssertionResult DoubleNearPredFormat(const char* expr1,
1593b89a7cc2SEnji Cooper                                                 const char* expr2,
1594b89a7cc2SEnji Cooper                                                 const char* abs_error_expr,
159528f6c2f2SEnji Cooper                                                 double val1, double val2,
1596b89a7cc2SEnji Cooper                                                 double abs_error);
1597b89a7cc2SEnji Cooper 
1598b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
1599b89a7cc2SEnji Cooper // A class that enables one to stream messages to assertion macros
1600b89a7cc2SEnji Cooper class GTEST_API_ AssertHelper {
1601b89a7cc2SEnji Cooper  public:
1602b89a7cc2SEnji Cooper   // Constructor.
160328f6c2f2SEnji Cooper   AssertHelper(TestPartResult::Type type, const char* file, int line,
1604b89a7cc2SEnji Cooper                const char* message);
1605b89a7cc2SEnji Cooper   ~AssertHelper();
1606b89a7cc2SEnji Cooper 
1607b89a7cc2SEnji Cooper   // Message assignment is a semantic trick to enable assertion
1608b89a7cc2SEnji Cooper   // streaming; see the GTEST_MESSAGE_ macro below.
1609b89a7cc2SEnji Cooper   void operator=(const Message& message) const;
1610b89a7cc2SEnji Cooper 
1611b89a7cc2SEnji Cooper  private:
1612b89a7cc2SEnji Cooper   // We put our data in a struct so that the size of the AssertHelper class can
1613b89a7cc2SEnji Cooper   // be as small as possible.  This is important because gcc is incapable of
1614b89a7cc2SEnji Cooper   // re-using stack space even for temporary variables, so every EXPECT_EQ
1615b89a7cc2SEnji Cooper   // reserves stack space for another AssertHelper.
1616b89a7cc2SEnji Cooper   struct AssertHelperData {
AssertHelperDataAssertHelperData161728f6c2f2SEnji Cooper     AssertHelperData(TestPartResult::Type t, const char* srcfile, int line_num,
1618b89a7cc2SEnji Cooper                      const char* msg)
1619b89a7cc2SEnji Cooper         : type(t), file(srcfile), line(line_num), message(msg) {}
1620b89a7cc2SEnji Cooper 
1621b89a7cc2SEnji Cooper     TestPartResult::Type const type;
1622b89a7cc2SEnji Cooper     const char* const file;
1623b89a7cc2SEnji Cooper     int const line;
1624b89a7cc2SEnji Cooper     std::string const message;
1625b89a7cc2SEnji Cooper 
1626b89a7cc2SEnji Cooper    private:
162728f6c2f2SEnji Cooper     AssertHelperData(const AssertHelperData&) = delete;
162828f6c2f2SEnji Cooper     AssertHelperData& operator=(const AssertHelperData&) = delete;
1629b89a7cc2SEnji Cooper   };
1630b89a7cc2SEnji Cooper 
1631b89a7cc2SEnji Cooper   AssertHelperData* const data_;
1632b89a7cc2SEnji Cooper 
163328f6c2f2SEnji Cooper   AssertHelper(const AssertHelper&) = delete;
163428f6c2f2SEnji Cooper   AssertHelper& operator=(const AssertHelper&) = delete;
1635b89a7cc2SEnji Cooper };
1636b89a7cc2SEnji Cooper 
1637b89a7cc2SEnji Cooper }  // namespace internal
1638b89a7cc2SEnji Cooper 
1639b89a7cc2SEnji Cooper // The pure interface class that all value-parameterized tests inherit from.
1640b89a7cc2SEnji Cooper // A value-parameterized class must inherit from both ::testing::Test and
1641b89a7cc2SEnji Cooper // ::testing::WithParamInterface. In most cases that just means inheriting
1642b89a7cc2SEnji Cooper // from ::testing::TestWithParam, but more complicated test hierarchies
1643b89a7cc2SEnji Cooper // may need to inherit from Test and WithParamInterface at different levels.
1644b89a7cc2SEnji Cooper //
1645b89a7cc2SEnji Cooper // This interface has support for accessing the test parameter value via
1646b89a7cc2SEnji Cooper // the GetParam() method.
1647b89a7cc2SEnji Cooper //
1648b89a7cc2SEnji Cooper // Use it with one of the parameter generator defining functions, like Range(),
164928f6c2f2SEnji Cooper // Values(), ValuesIn(), Bool(), Combine(), and ConvertGenerator<T>().
1650b89a7cc2SEnji Cooper //
1651b89a7cc2SEnji Cooper // class FooTest : public ::testing::TestWithParam<int> {
1652b89a7cc2SEnji Cooper //  protected:
1653b89a7cc2SEnji Cooper //   FooTest() {
1654b89a7cc2SEnji Cooper //     // Can use GetParam() here.
1655b89a7cc2SEnji Cooper //   }
165628f6c2f2SEnji Cooper //   ~FooTest() override {
1657b89a7cc2SEnji Cooper //     // Can use GetParam() here.
1658b89a7cc2SEnji Cooper //   }
165928f6c2f2SEnji Cooper //   void SetUp() override {
1660b89a7cc2SEnji Cooper //     // Can use GetParam() here.
1661b89a7cc2SEnji Cooper //   }
166228f6c2f2SEnji Cooper //   void TearDown override {
1663b89a7cc2SEnji Cooper //     // Can use GetParam() here.
1664b89a7cc2SEnji Cooper //   }
1665b89a7cc2SEnji Cooper // };
1666b89a7cc2SEnji Cooper // TEST_P(FooTest, DoesBar) {
1667b89a7cc2SEnji Cooper //   // Can use GetParam() method here.
1668b89a7cc2SEnji Cooper //   Foo foo;
1669b89a7cc2SEnji Cooper //   ASSERT_TRUE(foo.DoesBar(GetParam()));
1670b89a7cc2SEnji Cooper // }
167128f6c2f2SEnji Cooper // INSTANTIATE_TEST_SUITE_P(OneToTenRange, FooTest, ::testing::Range(1, 10));
1672b89a7cc2SEnji Cooper 
1673b89a7cc2SEnji Cooper template <typename T>
1674b89a7cc2SEnji Cooper class WithParamInterface {
1675b89a7cc2SEnji Cooper  public:
1676b89a7cc2SEnji Cooper   typedef T ParamType;
167728f6c2f2SEnji Cooper   virtual ~WithParamInterface() = default;
1678b89a7cc2SEnji Cooper 
1679b89a7cc2SEnji Cooper   // The current parameter value. Is also available in the test fixture's
168028f6c2f2SEnji Cooper   // constructor.
GetParam()168128f6c2f2SEnji Cooper   static const ParamType& GetParam() {
168228f6c2f2SEnji Cooper     GTEST_CHECK_(parameter_ != nullptr)
1683b89a7cc2SEnji Cooper         << "GetParam() can only be called inside a value-parameterized test "
1684b89a7cc2SEnji Cooper         << "-- did you intend to write TEST_P instead of TEST_F?";
1685b89a7cc2SEnji Cooper     return *parameter_;
1686b89a7cc2SEnji Cooper   }
1687b89a7cc2SEnji Cooper 
1688b89a7cc2SEnji Cooper  private:
1689b89a7cc2SEnji Cooper   // Sets parameter value. The caller is responsible for making sure the value
1690b89a7cc2SEnji Cooper   // remains alive and unchanged throughout the current test.
SetParam(const ParamType * parameter)169128f6c2f2SEnji Cooper   static void SetParam(const ParamType* parameter) { parameter_ = parameter; }
1692b89a7cc2SEnji Cooper 
1693b89a7cc2SEnji Cooper   // Static value used for accessing parameter during a test lifetime.
1694b89a7cc2SEnji Cooper   static const ParamType* parameter_;
1695b89a7cc2SEnji Cooper 
1696b89a7cc2SEnji Cooper   // TestClass must be a subclass of WithParamInterface<T> and Test.
169728f6c2f2SEnji Cooper   template <class TestClass>
169828f6c2f2SEnji Cooper   friend class internal::ParameterizedTestFactory;
1699b89a7cc2SEnji Cooper };
1700b89a7cc2SEnji Cooper 
1701b89a7cc2SEnji Cooper template <typename T>
170228f6c2f2SEnji Cooper const T* WithParamInterface<T>::parameter_ = nullptr;
1703b89a7cc2SEnji Cooper 
1704b89a7cc2SEnji Cooper // Most value-parameterized classes can ignore the existence of
1705b89a7cc2SEnji Cooper // WithParamInterface, and can just inherit from ::testing::TestWithParam.
1706b89a7cc2SEnji Cooper 
1707b89a7cc2SEnji Cooper template <typename T>
170828f6c2f2SEnji Cooper class TestWithParam : public Test, public WithParamInterface<T> {};
1709b89a7cc2SEnji Cooper 
1710b89a7cc2SEnji Cooper // Macros for indicating success/failure in test code.
1711b89a7cc2SEnji Cooper 
1712da1a9eb0SAlan Somers // Skips test in runtime.
1713da1a9eb0SAlan Somers // Skipping test aborts current function.
1714da1a9eb0SAlan Somers // Skipped tests are neither successful nor failed.
171528f6c2f2SEnji Cooper #define GTEST_SKIP() GTEST_SKIP_("")
1716da1a9eb0SAlan Somers 
1717b89a7cc2SEnji Cooper // ADD_FAILURE unconditionally adds a failure to the current test.
1718b89a7cc2SEnji Cooper // SUCCEED generates a success - it doesn't automatically make the
1719b89a7cc2SEnji Cooper // current test successful, as a test is only successful when it has
1720b89a7cc2SEnji Cooper // no failure.
1721b89a7cc2SEnji Cooper //
1722b89a7cc2SEnji Cooper // EXPECT_* verifies that a certain condition is satisfied.  If not,
1723b89a7cc2SEnji Cooper // it behaves like ADD_FAILURE.  In particular:
1724b89a7cc2SEnji Cooper //
1725b89a7cc2SEnji Cooper //   EXPECT_TRUE  verifies that a Boolean condition is true.
1726b89a7cc2SEnji Cooper //   EXPECT_FALSE verifies that a Boolean condition is false.
1727b89a7cc2SEnji Cooper //
1728b89a7cc2SEnji Cooper // FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except
1729b89a7cc2SEnji Cooper // that they will also abort the current function on failure.  People
1730b89a7cc2SEnji Cooper // usually want the fail-fast behavior of FAIL and ASSERT_*, but those
1731b89a7cc2SEnji Cooper // writing data-driven tests often find themselves using ADD_FAILURE
1732b89a7cc2SEnji Cooper // and EXPECT_* more.
1733b89a7cc2SEnji Cooper 
1734b89a7cc2SEnji Cooper // Generates a nonfatal failure with a generic message.
1735b89a7cc2SEnji Cooper #define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed")
1736b89a7cc2SEnji Cooper 
1737b89a7cc2SEnji Cooper // Generates a nonfatal failure at the given source file location with
1738b89a7cc2SEnji Cooper // a generic message.
1739b89a7cc2SEnji Cooper #define ADD_FAILURE_AT(file, line)        \
1740b89a7cc2SEnji Cooper   GTEST_MESSAGE_AT_(file, line, "Failed", \
1741b89a7cc2SEnji Cooper                     ::testing::TestPartResult::kNonFatalFailure)
1742b89a7cc2SEnji Cooper 
1743b89a7cc2SEnji Cooper // Generates a fatal failure with a generic message.
1744b89a7cc2SEnji Cooper #define GTEST_FAIL() GTEST_FATAL_FAILURE_("Failed")
1745b89a7cc2SEnji Cooper 
174628f6c2f2SEnji Cooper // Like GTEST_FAIL(), but at the given source file location.
174728f6c2f2SEnji Cooper #define GTEST_FAIL_AT(file, line)                \
174828f6c2f2SEnji Cooper   return GTEST_MESSAGE_AT_(file, line, "Failed", \
174928f6c2f2SEnji Cooper                            ::testing::TestPartResult::kFatalFailure)
175028f6c2f2SEnji Cooper 
1751b89a7cc2SEnji Cooper // Define this macro to 1 to omit the definition of FAIL(), which is a
1752b89a7cc2SEnji Cooper // generic name and clashes with some other libraries.
175328f6c2f2SEnji Cooper #if !(defined(GTEST_DONT_DEFINE_FAIL) && GTEST_DONT_DEFINE_FAIL)
1754b89a7cc2SEnji Cooper #define FAIL() GTEST_FAIL()
1755b89a7cc2SEnji Cooper #endif
1756b89a7cc2SEnji Cooper 
1757b89a7cc2SEnji Cooper // Generates a success with a generic message.
1758b89a7cc2SEnji Cooper #define GTEST_SUCCEED() GTEST_SUCCESS_("Succeeded")
1759b89a7cc2SEnji Cooper 
1760b89a7cc2SEnji Cooper // Define this macro to 1 to omit the definition of SUCCEED(), which
1761b89a7cc2SEnji Cooper // is a generic name and clashes with some other libraries.
176228f6c2f2SEnji Cooper #if !(defined(GTEST_DONT_DEFINE_SUCCEED) && GTEST_DONT_DEFINE_SUCCEED)
1763b89a7cc2SEnji Cooper #define SUCCEED() GTEST_SUCCEED()
1764b89a7cc2SEnji Cooper #endif
1765b89a7cc2SEnji Cooper 
1766b89a7cc2SEnji Cooper // Macros for testing exceptions.
1767b89a7cc2SEnji Cooper //
1768b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_THROW(statement, expected_exception):
1769b89a7cc2SEnji Cooper //         Tests that the statement throws the expected exception.
1770b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_NO_THROW(statement):
1771b89a7cc2SEnji Cooper //         Tests that the statement doesn't throw any exception.
1772b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_ANY_THROW(statement):
1773b89a7cc2SEnji Cooper //         Tests that the statement throws an exception.
1774b89a7cc2SEnji Cooper 
1775b89a7cc2SEnji Cooper #define EXPECT_THROW(statement, expected_exception) \
1776b89a7cc2SEnji Cooper   GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_)
1777b89a7cc2SEnji Cooper #define EXPECT_NO_THROW(statement) \
1778b89a7cc2SEnji Cooper   GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_)
1779b89a7cc2SEnji Cooper #define EXPECT_ANY_THROW(statement) \
1780b89a7cc2SEnji Cooper   GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_)
1781b89a7cc2SEnji Cooper #define ASSERT_THROW(statement, expected_exception) \
1782b89a7cc2SEnji Cooper   GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_)
1783b89a7cc2SEnji Cooper #define ASSERT_NO_THROW(statement) \
1784b89a7cc2SEnji Cooper   GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_)
1785b89a7cc2SEnji Cooper #define ASSERT_ANY_THROW(statement) \
1786b89a7cc2SEnji Cooper   GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_)
1787b89a7cc2SEnji Cooper 
1788b89a7cc2SEnji Cooper // Boolean assertions. Condition can be either a Boolean expression or an
1789b89a7cc2SEnji Cooper // AssertionResult. For more information on how to use AssertionResult with
1790b89a7cc2SEnji Cooper // these macros see comments on that class.
179128f6c2f2SEnji Cooper #define GTEST_EXPECT_TRUE(condition)                      \
1792b89a7cc2SEnji Cooper   GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \
1793b89a7cc2SEnji Cooper                       GTEST_NONFATAL_FAILURE_)
179428f6c2f2SEnji Cooper #define GTEST_EXPECT_FALSE(condition)                        \
1795b89a7cc2SEnji Cooper   GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \
1796b89a7cc2SEnji Cooper                       GTEST_NONFATAL_FAILURE_)
179728f6c2f2SEnji Cooper #define GTEST_ASSERT_TRUE(condition) \
179828f6c2f2SEnji Cooper   GTEST_TEST_BOOLEAN_(condition, #condition, false, true, GTEST_FATAL_FAILURE_)
179928f6c2f2SEnji Cooper #define GTEST_ASSERT_FALSE(condition)                        \
1800b89a7cc2SEnji Cooper   GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \
1801b89a7cc2SEnji Cooper                       GTEST_FATAL_FAILURE_)
1802b89a7cc2SEnji Cooper 
180328f6c2f2SEnji Cooper // Define these macros to 1 to omit the definition of the corresponding
180428f6c2f2SEnji Cooper // EXPECT or ASSERT, which clashes with some users' own code.
180528f6c2f2SEnji Cooper 
180628f6c2f2SEnji Cooper #if !(defined(GTEST_DONT_DEFINE_EXPECT_TRUE) && GTEST_DONT_DEFINE_EXPECT_TRUE)
180728f6c2f2SEnji Cooper #define EXPECT_TRUE(condition) GTEST_EXPECT_TRUE(condition)
180828f6c2f2SEnji Cooper #endif
180928f6c2f2SEnji Cooper 
181028f6c2f2SEnji Cooper #if !(defined(GTEST_DONT_DEFINE_EXPECT_FALSE) && GTEST_DONT_DEFINE_EXPECT_FALSE)
181128f6c2f2SEnji Cooper #define EXPECT_FALSE(condition) GTEST_EXPECT_FALSE(condition)
181228f6c2f2SEnji Cooper #endif
181328f6c2f2SEnji Cooper 
181428f6c2f2SEnji Cooper #if !(defined(GTEST_DONT_DEFINE_ASSERT_TRUE) && GTEST_DONT_DEFINE_ASSERT_TRUE)
181528f6c2f2SEnji Cooper #define ASSERT_TRUE(condition) GTEST_ASSERT_TRUE(condition)
181628f6c2f2SEnji Cooper #endif
181728f6c2f2SEnji Cooper 
181828f6c2f2SEnji Cooper #if !(defined(GTEST_DONT_DEFINE_ASSERT_FALSE) && GTEST_DONT_DEFINE_ASSERT_FALSE)
181928f6c2f2SEnji Cooper #define ASSERT_FALSE(condition) GTEST_ASSERT_FALSE(condition)
182028f6c2f2SEnji Cooper #endif
182128f6c2f2SEnji Cooper 
1822b89a7cc2SEnji Cooper // Macros for testing equalities and inequalities.
1823b89a7cc2SEnji Cooper //
1824b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_EQ(v1, v2): Tests that v1 == v2
1825b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_NE(v1, v2): Tests that v1 != v2
1826b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_LT(v1, v2): Tests that v1 < v2
1827b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_LE(v1, v2): Tests that v1 <= v2
1828b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_GT(v1, v2): Tests that v1 > v2
1829b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_GE(v1, v2): Tests that v1 >= v2
1830b89a7cc2SEnji Cooper //
1831b89a7cc2SEnji Cooper // When they are not, Google Test prints both the tested expressions and
1832b89a7cc2SEnji Cooper // their actual values.  The values must be compatible built-in types,
1833b89a7cc2SEnji Cooper // or you will get a compiler error.  By "compatible" we mean that the
1834b89a7cc2SEnji Cooper // values can be compared by the respective operator.
1835b89a7cc2SEnji Cooper //
1836b89a7cc2SEnji Cooper // Note:
1837b89a7cc2SEnji Cooper //
1838b89a7cc2SEnji Cooper //   1. It is possible to make a user-defined type work with
1839b89a7cc2SEnji Cooper //   {ASSERT|EXPECT}_??(), but that requires overloading the
1840b89a7cc2SEnji Cooper //   comparison operators and is thus discouraged by the Google C++
1841b89a7cc2SEnji Cooper //   Usage Guide.  Therefore, you are advised to use the
1842b89a7cc2SEnji Cooper //   {ASSERT|EXPECT}_TRUE() macro to assert that two objects are
1843b89a7cc2SEnji Cooper //   equal.
1844b89a7cc2SEnji Cooper //
1845b89a7cc2SEnji Cooper //   2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on
1846b89a7cc2SEnji Cooper //   pointers (in particular, C strings).  Therefore, if you use it
1847b89a7cc2SEnji Cooper //   with two C strings, you are testing how their locations in memory
1848b89a7cc2SEnji Cooper //   are related, not how their content is related.  To compare two C
1849b89a7cc2SEnji Cooper //   strings by content, use {ASSERT|EXPECT}_STR*().
1850b89a7cc2SEnji Cooper //
1851b89a7cc2SEnji Cooper //   3. {ASSERT|EXPECT}_EQ(v1, v2) is preferred to
1852b89a7cc2SEnji Cooper //   {ASSERT|EXPECT}_TRUE(v1 == v2), as the former tells you
1853b89a7cc2SEnji Cooper //   what the actual value is when it fails, and similarly for the
1854b89a7cc2SEnji Cooper //   other comparisons.
1855b89a7cc2SEnji Cooper //
1856b89a7cc2SEnji Cooper //   4. Do not depend on the order in which {ASSERT|EXPECT}_??()
1857b89a7cc2SEnji Cooper //   evaluate their arguments, which is undefined.
1858b89a7cc2SEnji Cooper //
1859b89a7cc2SEnji Cooper //   5. These macros evaluate their arguments exactly once.
1860b89a7cc2SEnji Cooper //
1861b89a7cc2SEnji Cooper // Examples:
1862b89a7cc2SEnji Cooper //
1863b89a7cc2SEnji Cooper //   EXPECT_NE(Foo(), 5);
1864b89a7cc2SEnji Cooper //   EXPECT_EQ(a_pointer, NULL);
1865b89a7cc2SEnji Cooper //   ASSERT_LT(i, array_size);
1866b89a7cc2SEnji Cooper //   ASSERT_GT(records.size(), 0) << "There is no record left.";
1867b89a7cc2SEnji Cooper 
1868b89a7cc2SEnji Cooper #define EXPECT_EQ(val1, val2) \
186928f6c2f2SEnji Cooper   EXPECT_PRED_FORMAT2(::testing::internal::EqHelper::Compare, val1, val2)
1870b89a7cc2SEnji Cooper #define EXPECT_NE(val1, val2) \
1871b89a7cc2SEnji Cooper   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)
1872b89a7cc2SEnji Cooper #define EXPECT_LE(val1, val2) \
1873b89a7cc2SEnji Cooper   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
1874b89a7cc2SEnji Cooper #define EXPECT_LT(val1, val2) \
1875b89a7cc2SEnji Cooper   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)
1876b89a7cc2SEnji Cooper #define EXPECT_GE(val1, val2) \
1877b89a7cc2SEnji Cooper   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
1878b89a7cc2SEnji Cooper #define EXPECT_GT(val1, val2) \
1879b89a7cc2SEnji Cooper   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
1880b89a7cc2SEnji Cooper 
1881b89a7cc2SEnji Cooper #define GTEST_ASSERT_EQ(val1, val2) \
188228f6c2f2SEnji Cooper   ASSERT_PRED_FORMAT2(::testing::internal::EqHelper::Compare, val1, val2)
1883b89a7cc2SEnji Cooper #define GTEST_ASSERT_NE(val1, val2) \
1884b89a7cc2SEnji Cooper   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)
1885b89a7cc2SEnji Cooper #define GTEST_ASSERT_LE(val1, val2) \
1886b89a7cc2SEnji Cooper   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
1887b89a7cc2SEnji Cooper #define GTEST_ASSERT_LT(val1, val2) \
1888b89a7cc2SEnji Cooper   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)
1889b89a7cc2SEnji Cooper #define GTEST_ASSERT_GE(val1, val2) \
1890b89a7cc2SEnji Cooper   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
1891b89a7cc2SEnji Cooper #define GTEST_ASSERT_GT(val1, val2) \
1892b89a7cc2SEnji Cooper   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
1893b89a7cc2SEnji Cooper 
1894b89a7cc2SEnji Cooper // Define macro GTEST_DONT_DEFINE_ASSERT_XY to 1 to omit the definition of
1895b89a7cc2SEnji Cooper // ASSERT_XY(), which clashes with some users' own code.
1896b89a7cc2SEnji Cooper 
189728f6c2f2SEnji Cooper #if !(defined(GTEST_DONT_DEFINE_ASSERT_EQ) && GTEST_DONT_DEFINE_ASSERT_EQ)
1898b89a7cc2SEnji Cooper #define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2)
1899b89a7cc2SEnji Cooper #endif
1900b89a7cc2SEnji Cooper 
190128f6c2f2SEnji Cooper #if !(defined(GTEST_DONT_DEFINE_ASSERT_NE) && GTEST_DONT_DEFINE_ASSERT_NE)
1902b89a7cc2SEnji Cooper #define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2)
1903b89a7cc2SEnji Cooper #endif
1904b89a7cc2SEnji Cooper 
190528f6c2f2SEnji Cooper #if !(defined(GTEST_DONT_DEFINE_ASSERT_LE) && GTEST_DONT_DEFINE_ASSERT_LE)
1906b89a7cc2SEnji Cooper #define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2)
1907b89a7cc2SEnji Cooper #endif
1908b89a7cc2SEnji Cooper 
190928f6c2f2SEnji Cooper #if !(defined(GTEST_DONT_DEFINE_ASSERT_LT) && GTEST_DONT_DEFINE_ASSERT_LT)
1910b89a7cc2SEnji Cooper #define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2)
1911b89a7cc2SEnji Cooper #endif
1912b89a7cc2SEnji Cooper 
191328f6c2f2SEnji Cooper #if !(defined(GTEST_DONT_DEFINE_ASSERT_GE) && GTEST_DONT_DEFINE_ASSERT_GE)
1914b89a7cc2SEnji Cooper #define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2)
1915b89a7cc2SEnji Cooper #endif
1916b89a7cc2SEnji Cooper 
191728f6c2f2SEnji Cooper #if !(defined(GTEST_DONT_DEFINE_ASSERT_GT) && GTEST_DONT_DEFINE_ASSERT_GT)
1918b89a7cc2SEnji Cooper #define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2)
1919b89a7cc2SEnji Cooper #endif
1920b89a7cc2SEnji Cooper 
1921b89a7cc2SEnji Cooper // C-string Comparisons.  All tests treat NULL and any non-NULL string
1922b89a7cc2SEnji Cooper // as different.  Two NULLs are equal.
1923b89a7cc2SEnji Cooper //
1924b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_STREQ(s1, s2):     Tests that s1 == s2
1925b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_STRNE(s1, s2):     Tests that s1 != s2
1926b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case
1927b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case
1928b89a7cc2SEnji Cooper //
1929b89a7cc2SEnji Cooper // For wide or narrow string objects, you can use the
1930b89a7cc2SEnji Cooper // {ASSERT|EXPECT}_??() macros.
1931b89a7cc2SEnji Cooper //
1932b89a7cc2SEnji Cooper // Don't depend on the order in which the arguments are evaluated,
1933b89a7cc2SEnji Cooper // which is undefined.
1934b89a7cc2SEnji Cooper //
1935b89a7cc2SEnji Cooper // These macros evaluate their arguments exactly once.
1936b89a7cc2SEnji Cooper 
1937b89a7cc2SEnji Cooper #define EXPECT_STREQ(s1, s2) \
1938b89a7cc2SEnji Cooper   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2)
1939b89a7cc2SEnji Cooper #define EXPECT_STRNE(s1, s2) \
1940b89a7cc2SEnji Cooper   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)
1941b89a7cc2SEnji Cooper #define EXPECT_STRCASEEQ(s1, s2) \
1942b89a7cc2SEnji Cooper   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, s1, s2)
1943b89a7cc2SEnji Cooper #define EXPECT_STRCASENE(s1, s2) \
1944b89a7cc2SEnji Cooper   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)
1945b89a7cc2SEnji Cooper 
1946b89a7cc2SEnji Cooper #define ASSERT_STREQ(s1, s2) \
1947b89a7cc2SEnji Cooper   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2)
1948b89a7cc2SEnji Cooper #define ASSERT_STRNE(s1, s2) \
1949b89a7cc2SEnji Cooper   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)
1950b89a7cc2SEnji Cooper #define ASSERT_STRCASEEQ(s1, s2) \
1951b89a7cc2SEnji Cooper   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, s1, s2)
1952b89a7cc2SEnji Cooper #define ASSERT_STRCASENE(s1, s2) \
1953b89a7cc2SEnji Cooper   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)
1954b89a7cc2SEnji Cooper 
1955b89a7cc2SEnji Cooper // Macros for comparing floating-point numbers.
1956b89a7cc2SEnji Cooper //
1957b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_FLOAT_EQ(val1, val2):
1958b89a7cc2SEnji Cooper //         Tests that two float values are almost equal.
1959b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_DOUBLE_EQ(val1, val2):
1960b89a7cc2SEnji Cooper //         Tests that two double values are almost equal.
1961b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error):
1962b89a7cc2SEnji Cooper //         Tests that v1 and v2 are within the given distance to each other.
1963b89a7cc2SEnji Cooper //
1964b89a7cc2SEnji Cooper // Google Test uses ULP-based comparison to automatically pick a default
1965b89a7cc2SEnji Cooper // error bound that is appropriate for the operands.  See the
1966b89a7cc2SEnji Cooper // FloatingPoint template class in gtest-internal.h if you are
1967b89a7cc2SEnji Cooper // interested in the implementation details.
1968b89a7cc2SEnji Cooper 
1969b89a7cc2SEnji Cooper #define EXPECT_FLOAT_EQ(val1, val2)                                         \
1970b89a7cc2SEnji Cooper   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \
1971b89a7cc2SEnji Cooper                       val1, val2)
1972b89a7cc2SEnji Cooper 
1973b89a7cc2SEnji Cooper #define EXPECT_DOUBLE_EQ(val1, val2)                                         \
1974b89a7cc2SEnji Cooper   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \
1975b89a7cc2SEnji Cooper                       val1, val2)
1976b89a7cc2SEnji Cooper 
1977b89a7cc2SEnji Cooper #define ASSERT_FLOAT_EQ(val1, val2)                                         \
1978b89a7cc2SEnji Cooper   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \
1979b89a7cc2SEnji Cooper                       val1, val2)
1980b89a7cc2SEnji Cooper 
1981b89a7cc2SEnji Cooper #define ASSERT_DOUBLE_EQ(val1, val2)                                         \
1982b89a7cc2SEnji Cooper   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \
1983b89a7cc2SEnji Cooper                       val1, val2)
1984b89a7cc2SEnji Cooper 
1985b89a7cc2SEnji Cooper #define EXPECT_NEAR(val1, val2, abs_error)                                   \
198628f6c2f2SEnji Cooper   EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, val1, val2, \
198728f6c2f2SEnji Cooper                       abs_error)
1988b89a7cc2SEnji Cooper 
1989b89a7cc2SEnji Cooper #define ASSERT_NEAR(val1, val2, abs_error)                                   \
199028f6c2f2SEnji Cooper   ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, val1, val2, \
199128f6c2f2SEnji Cooper                       abs_error)
1992b89a7cc2SEnji Cooper 
1993b89a7cc2SEnji Cooper // These predicate format functions work on floating-point values, and
1994b89a7cc2SEnji Cooper // can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g.
1995b89a7cc2SEnji Cooper //
1996b89a7cc2SEnji Cooper //   EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0);
1997b89a7cc2SEnji Cooper 
1998b89a7cc2SEnji Cooper // Asserts that val1 is less than, or almost equal to, val2.  Fails
1999b89a7cc2SEnji Cooper // otherwise.  In particular, it fails if either val1 or val2 is NaN.
2000b89a7cc2SEnji Cooper GTEST_API_ AssertionResult FloatLE(const char* expr1, const char* expr2,
2001b89a7cc2SEnji Cooper                                    float val1, float val2);
2002b89a7cc2SEnji Cooper GTEST_API_ AssertionResult DoubleLE(const char* expr1, const char* expr2,
2003b89a7cc2SEnji Cooper                                     double val1, double val2);
2004b89a7cc2SEnji Cooper 
200528f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
2006b89a7cc2SEnji Cooper 
2007b89a7cc2SEnji Cooper // Macros that test for HRESULT failure and success, these are only useful
2008b89a7cc2SEnji Cooper // on Windows, and rely on Windows SDK macros and APIs to compile.
2009b89a7cc2SEnji Cooper //
2010b89a7cc2SEnji Cooper //    * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr)
2011b89a7cc2SEnji Cooper //
2012b89a7cc2SEnji Cooper // When expr unexpectedly fails or succeeds, Google Test prints the
2013b89a7cc2SEnji Cooper // expected result and the actual result with both a human-readable
2014b89a7cc2SEnji Cooper // string representation of the error, if available, as well as the
2015b89a7cc2SEnji Cooper // hex result code.
2016b89a7cc2SEnji Cooper #define EXPECT_HRESULT_SUCCEEDED(expr) \
2017b89a7cc2SEnji Cooper   EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))
2018b89a7cc2SEnji Cooper 
2019b89a7cc2SEnji Cooper #define ASSERT_HRESULT_SUCCEEDED(expr) \
2020b89a7cc2SEnji Cooper   ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))
2021b89a7cc2SEnji Cooper 
2022b89a7cc2SEnji Cooper #define EXPECT_HRESULT_FAILED(expr) \
2023b89a7cc2SEnji Cooper   EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))
2024b89a7cc2SEnji Cooper 
2025b89a7cc2SEnji Cooper #define ASSERT_HRESULT_FAILED(expr) \
2026b89a7cc2SEnji Cooper   ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))
2027b89a7cc2SEnji Cooper 
2028b89a7cc2SEnji Cooper #endif  // GTEST_OS_WINDOWS
2029b89a7cc2SEnji Cooper 
2030b89a7cc2SEnji Cooper // Macros that execute statement and check that it doesn't generate new fatal
2031b89a7cc2SEnji Cooper // failures in the current thread.
2032b89a7cc2SEnji Cooper //
2033b89a7cc2SEnji Cooper //   * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement);
2034b89a7cc2SEnji Cooper //
2035b89a7cc2SEnji Cooper // Examples:
2036b89a7cc2SEnji Cooper //
2037b89a7cc2SEnji Cooper //   EXPECT_NO_FATAL_FAILURE(Process());
2038b89a7cc2SEnji Cooper //   ASSERT_NO_FATAL_FAILURE(Process()) << "Process() failed";
2039b89a7cc2SEnji Cooper //
2040b89a7cc2SEnji Cooper #define ASSERT_NO_FATAL_FAILURE(statement) \
2041b89a7cc2SEnji Cooper   GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_FATAL_FAILURE_)
2042b89a7cc2SEnji Cooper #define EXPECT_NO_FATAL_FAILURE(statement) \
2043b89a7cc2SEnji Cooper   GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_NONFATAL_FAILURE_)
2044b89a7cc2SEnji Cooper 
2045b89a7cc2SEnji Cooper // Causes a trace (including the given source file path and line number,
2046b89a7cc2SEnji Cooper // and the given message) to be included in every test failure message generated
2047b89a7cc2SEnji Cooper // by code in the scope of the lifetime of an instance of this class. The effect
2048b89a7cc2SEnji Cooper // is undone with the destruction of the instance.
2049b89a7cc2SEnji Cooper //
2050b89a7cc2SEnji Cooper // The message argument can be anything streamable to std::ostream.
2051b89a7cc2SEnji Cooper //
2052b89a7cc2SEnji Cooper // Example:
2053b89a7cc2SEnji Cooper //   testing::ScopedTrace trace("file.cc", 123, "message");
2054b89a7cc2SEnji Cooper //
2055b89a7cc2SEnji Cooper class GTEST_API_ ScopedTrace {
2056b89a7cc2SEnji Cooper  public:
2057b89a7cc2SEnji Cooper   // The c'tor pushes the given source file location and message onto
2058b89a7cc2SEnji Cooper   // a trace stack maintained by Google Test.
2059b89a7cc2SEnji Cooper 
2060b89a7cc2SEnji Cooper   // Template version. Uses Message() to convert the values into strings.
2061b89a7cc2SEnji Cooper   // Slow, but flexible.
2062b89a7cc2SEnji Cooper   template <typename T>
ScopedTrace(const char * file,int line,const T & message)2063b89a7cc2SEnji Cooper   ScopedTrace(const char* file, int line, const T& message) {
2064b89a7cc2SEnji Cooper     PushTrace(file, line, (Message() << message).GetString());
2065b89a7cc2SEnji Cooper   }
2066b89a7cc2SEnji Cooper 
2067b89a7cc2SEnji Cooper   // Optimize for some known types.
ScopedTrace(const char * file,int line,const char * message)2068b89a7cc2SEnji Cooper   ScopedTrace(const char* file, int line, const char* message) {
2069b89a7cc2SEnji Cooper     PushTrace(file, line, message ? message : "(null)");
2070b89a7cc2SEnji Cooper   }
2071b89a7cc2SEnji Cooper 
ScopedTrace(const char * file,int line,const std::string & message)2072b89a7cc2SEnji Cooper   ScopedTrace(const char* file, int line, const std::string& message) {
2073b89a7cc2SEnji Cooper     PushTrace(file, line, message);
2074b89a7cc2SEnji Cooper   }
2075b89a7cc2SEnji Cooper 
2076b89a7cc2SEnji Cooper   // The d'tor pops the info pushed by the c'tor.
2077b89a7cc2SEnji Cooper   //
2078b89a7cc2SEnji Cooper   // Note that the d'tor is not virtual in order to be efficient.
2079b89a7cc2SEnji Cooper   // Don't inherit from ScopedTrace!
2080b89a7cc2SEnji Cooper   ~ScopedTrace();
2081b89a7cc2SEnji Cooper 
2082b89a7cc2SEnji Cooper  private:
2083b89a7cc2SEnji Cooper   void PushTrace(const char* file, int line, std::string message);
2084b89a7cc2SEnji Cooper 
208528f6c2f2SEnji Cooper   ScopedTrace(const ScopedTrace&) = delete;
208628f6c2f2SEnji Cooper   ScopedTrace& operator=(const ScopedTrace&) = delete;
208728f6c2f2SEnji Cooper };
2088b89a7cc2SEnji Cooper 
2089b89a7cc2SEnji Cooper // Causes a trace (including the source file path, the current line
2090b89a7cc2SEnji Cooper // number, and the given message) to be included in every test failure
2091b89a7cc2SEnji Cooper // message generated by code in the current scope.  The effect is
2092b89a7cc2SEnji Cooper // undone when the control leaves the current scope.
2093b89a7cc2SEnji Cooper //
2094b89a7cc2SEnji Cooper // The message argument can be anything streamable to std::ostream.
2095b89a7cc2SEnji Cooper //
2096b89a7cc2SEnji Cooper // In the implementation, we include the current line number as part
2097b89a7cc2SEnji Cooper // of the dummy variable name, thus allowing multiple SCOPED_TRACE()s
2098b89a7cc2SEnji Cooper // to appear in the same block - as long as they are on different
2099b89a7cc2SEnji Cooper // lines.
2100b89a7cc2SEnji Cooper //
2101b89a7cc2SEnji Cooper // Assuming that each thread maintains its own stack of traces.
2102b89a7cc2SEnji Cooper // Therefore, a SCOPED_TRACE() would (correctly) only affect the
2103b89a7cc2SEnji Cooper // assertions in its own thread.
2104b89a7cc2SEnji Cooper #define SCOPED_TRACE(message)                                               \
210528f6c2f2SEnji Cooper   const ::testing::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)( \
2106b89a7cc2SEnji Cooper       __FILE__, __LINE__, (message))
2107b89a7cc2SEnji Cooper 
2108b89a7cc2SEnji Cooper // Compile-time assertion for type equality.
210928f6c2f2SEnji Cooper // StaticAssertTypeEq<type1, type2>() compiles if and only if type1 and type2
211028f6c2f2SEnji Cooper // are the same type.  The value it returns is not interesting.
2111b89a7cc2SEnji Cooper //
2112b89a7cc2SEnji Cooper // Instead of making StaticAssertTypeEq a class template, we make it a
2113b89a7cc2SEnji Cooper // function template that invokes a helper class template.  This
2114b89a7cc2SEnji Cooper // prevents a user from misusing StaticAssertTypeEq<T1, T2> by
2115b89a7cc2SEnji Cooper // defining objects of that type.
2116b89a7cc2SEnji Cooper //
2117b89a7cc2SEnji Cooper // CAVEAT:
2118b89a7cc2SEnji Cooper //
2119b89a7cc2SEnji Cooper // When used inside a method of a class template,
2120b89a7cc2SEnji Cooper // StaticAssertTypeEq<T1, T2>() is effective ONLY IF the method is
2121b89a7cc2SEnji Cooper // instantiated.  For example, given:
2122b89a7cc2SEnji Cooper //
2123b89a7cc2SEnji Cooper //   template <typename T> class Foo {
2124b89a7cc2SEnji Cooper //    public:
2125b89a7cc2SEnji Cooper //     void Bar() { testing::StaticAssertTypeEq<int, T>(); }
2126b89a7cc2SEnji Cooper //   };
2127b89a7cc2SEnji Cooper //
2128b89a7cc2SEnji Cooper // the code:
2129b89a7cc2SEnji Cooper //
2130b89a7cc2SEnji Cooper //   void Test1() { Foo<bool> foo; }
2131b89a7cc2SEnji Cooper //
2132b89a7cc2SEnji Cooper // will NOT generate a compiler error, as Foo<bool>::Bar() is never
2133b89a7cc2SEnji Cooper // actually instantiated.  Instead, you need:
2134b89a7cc2SEnji Cooper //
2135b89a7cc2SEnji Cooper //   void Test2() { Foo<bool> foo; foo.Bar(); }
2136b89a7cc2SEnji Cooper //
2137b89a7cc2SEnji Cooper // to cause a compiler error.
2138b89a7cc2SEnji Cooper template <typename T1, typename T2>
StaticAssertTypeEq()213928f6c2f2SEnji Cooper constexpr bool StaticAssertTypeEq() noexcept {
214028f6c2f2SEnji Cooper   static_assert(std::is_same<T1, T2>::value, "T1 and T2 are not the same type");
2141b89a7cc2SEnji Cooper   return true;
2142b89a7cc2SEnji Cooper }
2143b89a7cc2SEnji Cooper 
2144b89a7cc2SEnji Cooper // Defines a test.
2145b89a7cc2SEnji Cooper //
214628f6c2f2SEnji Cooper // The first parameter is the name of the test suite, and the second
214728f6c2f2SEnji Cooper // parameter is the name of the test within the test suite.
2148b89a7cc2SEnji Cooper //
214928f6c2f2SEnji Cooper // The convention is to end the test suite name with "Test".  For
215028f6c2f2SEnji Cooper // example, a test suite for the Foo class can be named FooTest.
2151b89a7cc2SEnji Cooper //
2152b89a7cc2SEnji Cooper // Test code should appear between braces after an invocation of
2153b89a7cc2SEnji Cooper // this macro.  Example:
2154b89a7cc2SEnji Cooper //
2155b89a7cc2SEnji Cooper //   TEST(FooTest, InitializesCorrectly) {
2156b89a7cc2SEnji Cooper //     Foo foo;
2157b89a7cc2SEnji Cooper //     EXPECT_TRUE(foo.StatusIsOK());
2158b89a7cc2SEnji Cooper //   }
2159b89a7cc2SEnji Cooper 
2160b89a7cc2SEnji Cooper // Note that we call GetTestTypeId() instead of GetTypeId<
2161b89a7cc2SEnji Cooper // ::testing::Test>() here to get the type ID of testing::Test.  This
2162b89a7cc2SEnji Cooper // is to work around a suspected linker bug when using Google Test as
2163b89a7cc2SEnji Cooper // a framework on Mac OS X.  The bug causes GetTypeId<
2164b89a7cc2SEnji Cooper // ::testing::Test>() to return different values depending on whether
2165b89a7cc2SEnji Cooper // the call is from the Google Test framework itself or from user test
2166b89a7cc2SEnji Cooper // code.  GetTestTypeId() is guaranteed to always return the same
2167b89a7cc2SEnji Cooper // value, as it always calls GetTypeId<>() from the Google Test
2168b89a7cc2SEnji Cooper // framework.
216928f6c2f2SEnji Cooper #define GTEST_TEST(test_suite_name, test_name)             \
217028f6c2f2SEnji Cooper   GTEST_TEST_(test_suite_name, test_name, ::testing::Test, \
217128f6c2f2SEnji Cooper               ::testing::internal::GetTestTypeId())
2172b89a7cc2SEnji Cooper 
2173b89a7cc2SEnji Cooper // Define this macro to 1 to omit the definition of TEST(), which
2174b89a7cc2SEnji Cooper // is a generic name and clashes with some other libraries.
217528f6c2f2SEnji Cooper #if !(defined(GTEST_DONT_DEFINE_TEST) && GTEST_DONT_DEFINE_TEST)
217628f6c2f2SEnji Cooper #define TEST(test_suite_name, test_name) GTEST_TEST(test_suite_name, test_name)
2177b89a7cc2SEnji Cooper #endif
2178b89a7cc2SEnji Cooper 
2179b89a7cc2SEnji Cooper // Defines a test that uses a test fixture.
2180b89a7cc2SEnji Cooper //
2181b89a7cc2SEnji Cooper // The first parameter is the name of the test fixture class, which
218228f6c2f2SEnji Cooper // also doubles as the test suite name.  The second parameter is the
218328f6c2f2SEnji Cooper // name of the test within the test suite.
2184b89a7cc2SEnji Cooper //
2185b89a7cc2SEnji Cooper // A test fixture class must be declared earlier.  The user should put
2186b89a7cc2SEnji Cooper // the test code between braces after using this macro.  Example:
2187b89a7cc2SEnji Cooper //
2188b89a7cc2SEnji Cooper //   class FooTest : public testing::Test {
2189b89a7cc2SEnji Cooper //    protected:
219028f6c2f2SEnji Cooper //     void SetUp() override { b_.AddElement(3); }
2191b89a7cc2SEnji Cooper //
2192b89a7cc2SEnji Cooper //     Foo a_;
2193b89a7cc2SEnji Cooper //     Foo b_;
2194b89a7cc2SEnji Cooper //   };
2195b89a7cc2SEnji Cooper //
2196b89a7cc2SEnji Cooper //   TEST_F(FooTest, InitializesCorrectly) {
2197b89a7cc2SEnji Cooper //     EXPECT_TRUE(a_.StatusIsOK());
2198b89a7cc2SEnji Cooper //   }
2199b89a7cc2SEnji Cooper //
2200b89a7cc2SEnji Cooper //   TEST_F(FooTest, ReturnsElementCountCorrectly) {
2201b89a7cc2SEnji Cooper //     EXPECT_EQ(a_.size(), 0);
2202b89a7cc2SEnji Cooper //     EXPECT_EQ(b_.size(), 1);
2203b89a7cc2SEnji Cooper //   }
220428f6c2f2SEnji Cooper #define GTEST_TEST_F(test_fixture, test_name)        \
2205b89a7cc2SEnji Cooper   GTEST_TEST_(test_fixture, test_name, test_fixture, \
2206b89a7cc2SEnji Cooper               ::testing::internal::GetTypeId<test_fixture>())
220728f6c2f2SEnji Cooper #if !(defined(GTEST_DONT_DEFINE_TEST_F) && GTEST_DONT_DEFINE_TEST_F)
220828f6c2f2SEnji Cooper #define TEST_F(test_fixture, test_name) GTEST_TEST_F(test_fixture, test_name)
220928f6c2f2SEnji Cooper #endif
2210b89a7cc2SEnji Cooper 
221128f6c2f2SEnji Cooper // Returns a path to a temporary directory, which should be writable. It is
221228f6c2f2SEnji Cooper // implementation-dependent whether or not the path is terminated by the
221328f6c2f2SEnji Cooper // directory-separator character.
2214b89a7cc2SEnji Cooper GTEST_API_ std::string TempDir();
2215b89a7cc2SEnji Cooper 
221628f6c2f2SEnji Cooper // Returns a path to a directory that contains ancillary data files that might
221728f6c2f2SEnji Cooper // be used by tests. It is implementation dependent whether or not the path is
221828f6c2f2SEnji Cooper // terminated by the directory-separator character. The directory and the files
221928f6c2f2SEnji Cooper // in it should be considered read-only.
222028f6c2f2SEnji Cooper GTEST_API_ std::string SrcDir();
222128f6c2f2SEnji Cooper 
GTEST_DISABLE_MSC_WARNINGS_POP_()222228f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_()  // 4805 4100
222328f6c2f2SEnji Cooper 
222428f6c2f2SEnji Cooper // Dynamically registers a test with the framework.
222528f6c2f2SEnji Cooper //
222628f6c2f2SEnji Cooper // This is an advanced API only to be used when the `TEST` macros are
222728f6c2f2SEnji Cooper // insufficient. The macros should be preferred when possible, as they avoid
222828f6c2f2SEnji Cooper // most of the complexity of calling this function.
222928f6c2f2SEnji Cooper //
223028f6c2f2SEnji Cooper // The `factory` argument is a factory callable (move-constructible) object or
223128f6c2f2SEnji Cooper // function pointer that creates a new instance of the Test object. It
223228f6c2f2SEnji Cooper // handles ownership to the caller. The signature of the callable is
223328f6c2f2SEnji Cooper // `Fixture*()`, where `Fixture` is the test fixture class for the test. All
223428f6c2f2SEnji Cooper // tests registered with the same `test_suite_name` must return the same
223528f6c2f2SEnji Cooper // fixture type. This is checked at runtime.
223628f6c2f2SEnji Cooper //
223728f6c2f2SEnji Cooper // The framework will infer the fixture class from the factory and will call
223828f6c2f2SEnji Cooper // the `SetUpTestSuite` and `TearDownTestSuite` for it.
223928f6c2f2SEnji Cooper //
224028f6c2f2SEnji Cooper // Must be called before `RUN_ALL_TESTS()` is invoked, otherwise behavior is
224128f6c2f2SEnji Cooper // undefined.
224228f6c2f2SEnji Cooper //
224328f6c2f2SEnji Cooper // Use case example:
224428f6c2f2SEnji Cooper //
224528f6c2f2SEnji Cooper // class MyFixture : public ::testing::Test {
224628f6c2f2SEnji Cooper //  public:
224728f6c2f2SEnji Cooper //   // All of these optional, just like in regular macro usage.
224828f6c2f2SEnji Cooper //   static void SetUpTestSuite() { ... }
224928f6c2f2SEnji Cooper //   static void TearDownTestSuite() { ... }
225028f6c2f2SEnji Cooper //   void SetUp() override { ... }
225128f6c2f2SEnji Cooper //   void TearDown() override { ... }
225228f6c2f2SEnji Cooper // };
225328f6c2f2SEnji Cooper //
225428f6c2f2SEnji Cooper // class MyTest : public MyFixture {
225528f6c2f2SEnji Cooper //  public:
225628f6c2f2SEnji Cooper //   explicit MyTest(int data) : data_(data) {}
225728f6c2f2SEnji Cooper //   void TestBody() override { ... }
225828f6c2f2SEnji Cooper //
225928f6c2f2SEnji Cooper //  private:
226028f6c2f2SEnji Cooper //   int data_;
226128f6c2f2SEnji Cooper // };
226228f6c2f2SEnji Cooper //
226328f6c2f2SEnji Cooper // void RegisterMyTests(const std::vector<int>& values) {
226428f6c2f2SEnji Cooper //   for (int v : values) {
226528f6c2f2SEnji Cooper //     ::testing::RegisterTest(
226628f6c2f2SEnji Cooper //         "MyFixture", ("Test" + std::to_string(v)).c_str(), nullptr,
226728f6c2f2SEnji Cooper //         std::to_string(v).c_str(),
226828f6c2f2SEnji Cooper //         __FILE__, __LINE__,
226928f6c2f2SEnji Cooper //         // Important to use the fixture type as the return type here.
227028f6c2f2SEnji Cooper //         [=]() -> MyFixture* { return new MyTest(v); });
227128f6c2f2SEnji Cooper //   }
227228f6c2f2SEnji Cooper // }
227328f6c2f2SEnji Cooper // ...
227428f6c2f2SEnji Cooper // int main(int argc, char** argv) {
227528f6c2f2SEnji Cooper //   ::testing::InitGoogleTest(&argc, argv);
227628f6c2f2SEnji Cooper //   std::vector<int> values_to_test = LoadValuesFromConfig();
227728f6c2f2SEnji Cooper //   RegisterMyTests(values_to_test);
227828f6c2f2SEnji Cooper //   ...
227928f6c2f2SEnji Cooper //   return RUN_ALL_TESTS();
228028f6c2f2SEnji Cooper // }
228128f6c2f2SEnji Cooper //
228228f6c2f2SEnji Cooper template <int&... ExplicitParameterBarrier, typename Factory>
228328f6c2f2SEnji Cooper TestInfo* RegisterTest(const char* test_suite_name, const char* test_name,
228428f6c2f2SEnji Cooper                        const char* type_param, const char* value_param,
228528f6c2f2SEnji Cooper                        const char* file, int line, Factory factory) {
228628f6c2f2SEnji Cooper   using TestT = typename std::remove_pointer<decltype(factory())>::type;
228728f6c2f2SEnji Cooper 
228828f6c2f2SEnji Cooper   class FactoryImpl : public internal::TestFactoryBase {
228928f6c2f2SEnji Cooper    public:
229028f6c2f2SEnji Cooper     explicit FactoryImpl(Factory f) : factory_(std::move(f)) {}
229128f6c2f2SEnji Cooper     Test* CreateTest() override { return factory_(); }
229228f6c2f2SEnji Cooper 
229328f6c2f2SEnji Cooper    private:
229428f6c2f2SEnji Cooper     Factory factory_;
229528f6c2f2SEnji Cooper   };
229628f6c2f2SEnji Cooper 
229728f6c2f2SEnji Cooper   return internal::MakeAndRegisterTestInfo(
229828f6c2f2SEnji Cooper       test_suite_name, test_name, type_param, value_param,
229928f6c2f2SEnji Cooper       internal::CodeLocation(file, line), internal::GetTypeId<TestT>(),
230028f6c2f2SEnji Cooper       internal::SuiteApiResolver<TestT>::GetSetUpCaseOrSuite(file, line),
230128f6c2f2SEnji Cooper       internal::SuiteApiResolver<TestT>::GetTearDownCaseOrSuite(file, line),
230228f6c2f2SEnji Cooper       new FactoryImpl{std::move(factory)});
230328f6c2f2SEnji Cooper }
2304b89a7cc2SEnji Cooper 
2305b89a7cc2SEnji Cooper }  // namespace testing
2306b89a7cc2SEnji Cooper 
2307b89a7cc2SEnji Cooper // Use this function in main() to run all tests.  It returns 0 if all
2308b89a7cc2SEnji Cooper // tests are successful, or 1 otherwise.
2309b89a7cc2SEnji Cooper //
2310b89a7cc2SEnji Cooper // RUN_ALL_TESTS() should be invoked after the command line has been
2311b89a7cc2SEnji Cooper // parsed by InitGoogleTest().
2312b89a7cc2SEnji Cooper //
2313b89a7cc2SEnji Cooper // This function was formerly a macro; thus, it is in the global
2314b89a7cc2SEnji Cooper // namespace and has an all-caps name.
2315b89a7cc2SEnji Cooper int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_;
2316b89a7cc2SEnji Cooper 
RUN_ALL_TESTS()231728f6c2f2SEnji Cooper inline int RUN_ALL_TESTS() { return ::testing::UnitTest::GetInstance()->Run(); }
2318b89a7cc2SEnji Cooper 
2319b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4251
2320b89a7cc2SEnji Cooper 
232128f6c2f2SEnji Cooper #endif  // GOOGLETEST_INCLUDE_GTEST_GTEST_H_
2322