blob: ebd3123b043d6ac84576a62de3ac2c2dd8b1af15 [file] [log] [blame]
Misha Brukmanc89aba32008-12-31 17:34:06 +00001// Copyright 2005, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: wan@google.com (Zhanyong Wan)
31//
32// The Google C++ Testing Framework (Google Test)
33//
34// This header file defines the public API for Google Test. It should be
35// included by any test program that uses Google Test.
36//
37// IMPORTANT NOTE: Due to limitation of the C++ language, we have to
38// leave some internal implementation details in this header file.
39// They are clearly marked by comments like this:
40//
41// // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
42//
43// Such code is NOT meant to be used by a user directly, and is subject
44// to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user
45// program!
46//
47// Acknowledgment: Google Test borrowed the idea of automatic test
48// registration from Barthelemy Dagenais' (barthelemy@prologique.com)
49// easyUnit framework.
50
51#ifndef GTEST_INCLUDE_GTEST_GTEST_H_
52#define GTEST_INCLUDE_GTEST_GTEST_H_
53
54// The following platform macros are used throughout Google Test:
55// _WIN32_WCE Windows CE (set in project files)
56//
57// Note that even though _MSC_VER and _WIN32_WCE really indicate a compiler
58// and a Win32 implementation, respectively, we use them to indicate the
59// combination of compiler - Win 32 API - C library, since the code currently
60// only supports:
61// Windows proper with Visual C++ and MS C library (_MSC_VER && !_WIN32_WCE) and
62// Windows Mobile with Visual C++ and no C library (_WIN32_WCE).
63
64#include <limits>
65#include <gtest/internal/gtest-internal.h>
66#include <gtest/internal/gtest-string.h>
67#include <gtest/gtest-death-test.h>
68#include <gtest/gtest-message.h>
69#include <gtest/gtest-param-test.h>
70#include <gtest/gtest_prod.h>
71#include <gtest/gtest-test-part.h>
72#include <gtest/gtest-typed-test.h>
73
74// Depending on the platform, different string classes are available.
75// On Windows, ::std::string compiles only when exceptions are
76// enabled. On Linux, in addition to ::std::string, Google also makes
77// use of class ::string, which has the same interface as
78// ::std::string, but has a different implementation.
79//
80// The user can tell us whether ::std::string is available in his
81// environment by defining the macro GTEST_HAS_STD_STRING to either 1
82// or 0 on the compiler command line. He can also define
83// GTEST_HAS_GLOBAL_STRING to 1 to indicate that ::string is available
84// AND is a distinct type to ::std::string, or define it to 0 to
85// indicate otherwise.
86//
87// If the user's ::std::string and ::string are the same class due to
88// aliasing, he should define GTEST_HAS_STD_STRING to 1 and
89// GTEST_HAS_GLOBAL_STRING to 0.
90//
91// If the user doesn't define GTEST_HAS_STD_STRING and/or
92// GTEST_HAS_GLOBAL_STRING, they are defined heuristically.
93
94namespace testing {
95
96// The upper limit for valid stack trace depths.
97const int kMaxStackTraceDepth = 100;
98
99// This flag specifies the maximum number of stack frames to be
100// printed in a failure message.
101GTEST_DECLARE_int32_(stack_trace_depth);
102
103// This flag controls whether Google Test includes Google Test internal
104// stack frames in failure stack traces.
105GTEST_DECLARE_bool_(show_internal_stack_frames);
106
107namespace internal {
108
109class GTestFlagSaver;
110
111// Converts a streamable value to a String. A NULL pointer is
112// converted to "(null)". When the input value is a ::string,
113// ::std::string, ::wstring, or ::std::wstring object, each NUL
114// character in it is replaced with "\\0".
115// Declared in gtest-internal.h but defined here, so that it has access
116// to the definition of the Message class, required by the ARM
117// compiler.
118template <typename T>
119String StreamableToString(const T& streamable) {
120 return (Message() << streamable).GetString();
121}
122
123} // namespace internal
124
125// A class for indicating whether an assertion was successful. When
126// the assertion wasn't successful, the AssertionResult object
127// remembers a non-empty message that described how it failed.
128//
129// This class is useful for defining predicate-format functions to be
130// used with predicate assertions (ASSERT_PRED_FORMAT*, etc).
131//
132// The constructor of AssertionResult is private. To create an
133// instance of this class, use one of the factory functions
134// (AssertionSuccess() and AssertionFailure()).
135//
136// For example, in order to be able to write:
137//
138// // Verifies that Foo() returns an even number.
139// EXPECT_PRED_FORMAT1(IsEven, Foo());
140//
141// you just need to define:
142//
143// testing::AssertionResult IsEven(const char* expr, int n) {
144// if ((n % 2) == 0) return testing::AssertionSuccess();
145//
146// Message msg;
147// msg << "Expected: " << expr << " is even\n"
148// << " Actual: it's " << n;
149// return testing::AssertionFailure(msg);
150// }
151//
152// If Foo() returns 5, you will see the following message:
153//
154// Expected: Foo() is even
155// Actual: it's 5
156class AssertionResult {
157 public:
158 // Declares factory functions for making successful and failed
159 // assertion results as friends.
160 friend AssertionResult AssertionSuccess();
161 friend AssertionResult AssertionFailure(const Message&);
162
163 // Returns true iff the assertion succeeded.
164 operator bool() const { return failure_message_.c_str() == NULL; } // NOLINT
165
166 // Returns the assertion's failure message.
167 const char* failure_message() const { return failure_message_.c_str(); }
168
169 private:
170 // The default constructor. It is used when the assertion succeeded.
171 AssertionResult() {}
172
173 // The constructor used when the assertion failed.
174 explicit AssertionResult(const internal::String& failure_message);
175
176 // Stores the assertion's failure message.
177 internal::String failure_message_;
178};
179
180// Makes a successful assertion result.
181AssertionResult AssertionSuccess();
182
183// Makes a failed assertion result with the given failure message.
184AssertionResult AssertionFailure(const Message& msg);
185
186// The abstract class that all tests inherit from.
187//
188// In Google Test, a unit test program contains one or many TestCases, and
189// each TestCase contains one or many Tests.
190//
191// When you define a test using the TEST macro, you don't need to
192// explicitly derive from Test - the TEST macro automatically does
193// this for you.
194//
195// The only time you derive from Test is when defining a test fixture
196// to be used a TEST_F. For example:
197//
198// class FooTest : public testing::Test {
199// protected:
200// virtual void SetUp() { ... }
201// virtual void TearDown() { ... }
202// ...
203// };
204//
205// TEST_F(FooTest, Bar) { ... }
206// TEST_F(FooTest, Baz) { ... }
207//
208// Test is not copyable.
209class Test {
210 public:
211 friend class internal::TestInfoImpl;
212
213 // Defines types for pointers to functions that set up and tear down
214 // a test case.
215 typedef internal::SetUpTestCaseFunc SetUpTestCaseFunc;
216 typedef internal::TearDownTestCaseFunc TearDownTestCaseFunc;
217
218 // The d'tor is virtual as we intend to inherit from Test.
219 virtual ~Test();
220
221 // Sets up the stuff shared by all tests in this test case.
222 //
223 // Google Test will call Foo::SetUpTestCase() before running the first
224 // test in test case Foo. Hence a sub-class can define its own
225 // SetUpTestCase() method to shadow the one defined in the super
226 // class.
227 static void SetUpTestCase() {}
228
229 // Tears down the stuff shared by all tests in this test case.
230 //
231 // Google Test will call Foo::TearDownTestCase() after running the last
232 // test in test case Foo. Hence a sub-class can define its own
233 // TearDownTestCase() method to shadow the one defined in the super
234 // class.
235 static void TearDownTestCase() {}
236
237 // Returns true iff the current test has a fatal failure.
238 static bool HasFatalFailure();
239
240 // Logs a property for the current test. Only the last value for a given
241 // key is remembered.
242 // These are public static so they can be called from utility functions
243 // that are not members of the test fixture.
244 // The arguments are const char* instead strings, as Google Test is used
245 // on platforms where string doesn't compile.
246 //
247 // Note that a driving consideration for these RecordProperty methods
248 // was to produce xml output suited to the Greenspan charting utility,
249 // which at present will only chart values that fit in a 32-bit int. It
250 // is the user's responsibility to restrict their values to 32-bit ints
251 // if they intend them to be used with Greenspan.
252 static void RecordProperty(const char* key, const char* value);
253 static void RecordProperty(const char* key, int value);
254
255 protected:
256 // Creates a Test object.
257 Test();
258
259 // Sets up the test fixture.
260 virtual void SetUp();
261
262 // Tears down the test fixture.
263 virtual void TearDown();
264
265 private:
266 // Returns true iff the current test has the same fixture class as
267 // the first test in the current test case.
268 static bool HasSameFixtureClass();
269
270 // Runs the test after the test fixture has been set up.
271 //
272 // A sub-class must implement this to define the test logic.
273 //
274 // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM.
275 // Instead, use the TEST or TEST_F macro.
276 virtual void TestBody() = 0;
277
278 // Sets up, executes, and tears down the test.
279 void Run();
280
281 // Uses a GTestFlagSaver to save and restore all Google Test flags.
282 const internal::GTestFlagSaver* const gtest_flag_saver_;
283
284 // Often a user mis-spells SetUp() as Setup() and spends a long time
285 // wondering why it is never called by Google Test. The declaration of
286 // the following method is solely for catching such an error at
287 // compile time:
288 //
289 // - The return type is deliberately chosen to be not void, so it
290 // will be a conflict if a user declares void Setup() in his test
291 // fixture.
292 //
293 // - This method is private, so it will be another compiler error
294 // if a user calls it from his test fixture.
295 //
296 // DO NOT OVERRIDE THIS FUNCTION.
297 //
298 // If you see an error about overriding the following function or
299 // about it being private, you have mis-spelled SetUp() as Setup().
300 struct Setup_should_be_spelled_SetUp {};
301 virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; }
302
303 // We disallow copying Tests.
304 GTEST_DISALLOW_COPY_AND_ASSIGN_(Test);
305};
306
307
308// A TestInfo object stores the following information about a test:
309//
310// Test case name
311// Test name
312// Whether the test should be run
313// A function pointer that creates the test object when invoked
314// Test result
315//
316// The constructor of TestInfo registers itself with the UnitTest
317// singleton such that the RUN_ALL_TESTS() macro knows which tests to
318// run.
319class TestInfo {
320 public:
321 // Destructs a TestInfo object. This function is not virtual, so
322 // don't inherit from TestInfo.
323 ~TestInfo();
324
325 // Returns the test case name.
326 const char* test_case_name() const;
327
328 // Returns the test name.
329 const char* name() const;
330
331 // Returns the test case comment.
332 const char* test_case_comment() const;
333
334 // Returns the test comment.
335 const char* comment() const;
336
337 // Returns true if this test should run.
338 //
339 // Google Test allows the user to filter the tests by their full names.
340 // The full name of a test Bar in test case Foo is defined as
341 // "Foo.Bar". Only the tests that match the filter will run.
342 //
343 // A filter is a colon-separated list of glob (not regex) patterns,
344 // optionally followed by a '-' and a colon-separated list of
345 // negative patterns (tests to exclude). A test is run if it
346 // matches one of the positive patterns and does not match any of
347 // the negative patterns.
348 //
349 // For example, *A*:Foo.* is a filter that matches any string that
350 // contains the character 'A' or starts with "Foo.".
351 bool should_run() const;
352
353 // Returns the result of the test.
354 const internal::TestResult* result() const;
355 private:
356#ifdef GTEST_HAS_DEATH_TEST
357 friend class internal::DefaultDeathTestFactory;
358#endif // GTEST_HAS_DEATH_TEST
359 friend class internal::TestInfoImpl;
360 friend class internal::UnitTestImpl;
361 friend class Test;
362 friend class TestCase;
363 friend TestInfo* internal::MakeAndRegisterTestInfo(
364 const char* test_case_name, const char* name,
365 const char* test_case_comment, const char* comment,
366 internal::TypeId fixture_class_id,
367 Test::SetUpTestCaseFunc set_up_tc,
368 Test::TearDownTestCaseFunc tear_down_tc,
369 internal::TestFactoryBase* factory);
370
371 // Increments the number of death tests encountered in this test so
372 // far.
373 int increment_death_test_count();
374
375 // Accessors for the implementation object.
376 internal::TestInfoImpl* impl() { return impl_; }
377 const internal::TestInfoImpl* impl() const { return impl_; }
378
379 // Constructs a TestInfo object. The newly constructed instance assumes
380 // ownership of the factory object.
381 TestInfo(const char* test_case_name, const char* name,
382 const char* test_case_comment, const char* comment,
383 internal::TypeId fixture_class_id,
384 internal::TestFactoryBase* factory);
385
386 // An opaque implementation object.
387 internal::TestInfoImpl* impl_;
388
389 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfo);
390};
391
392// An Environment object is capable of setting up and tearing down an
393// environment. The user should subclass this to define his own
394// environment(s).
395//
396// An Environment object does the set-up and tear-down in virtual
397// methods SetUp() and TearDown() instead of the constructor and the
398// destructor, as:
399//
400// 1. You cannot safely throw from a destructor. This is a problem
401// as in some cases Google Test is used where exceptions are enabled, and
402// we may want to implement ASSERT_* using exceptions where they are
403// available.
404// 2. You cannot use ASSERT_* directly in a constructor or
405// destructor.
406class Environment {
407 public:
408 // The d'tor is virtual as we need to subclass Environment.
409 virtual ~Environment() {}
410
411 // Override this to define how to set up the environment.
412 virtual void SetUp() {}
413
414 // Override this to define how to tear down the environment.
415 virtual void TearDown() {}
416 private:
417 // If you see an error about overriding the following function or
418 // about it being private, you have mis-spelled SetUp() as Setup().
419 struct Setup_should_be_spelled_SetUp {};
420 virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; }
421};
422
423// A UnitTest consists of a list of TestCases.
424//
425// This is a singleton class. The only instance of UnitTest is
426// created when UnitTest::GetInstance() is first called. This
427// instance is never deleted.
428//
429// UnitTest is not copyable.
430//
431// This class is thread-safe as long as the methods are called
432// according to their specification.
433class UnitTest {
434 public:
435 // Gets the singleton UnitTest object. The first time this method
436 // is called, a UnitTest object is constructed and returned.
437 // Consecutive calls will return the same object.
438 static UnitTest* GetInstance();
439
440 // Registers and returns a global test environment. When a test
441 // program is run, all global test environments will be set-up in
442 // the order they were registered. After all tests in the program
443 // have finished, all global test environments will be torn-down in
444 // the *reverse* order they were registered.
445 //
446 // The UnitTest object takes ownership of the given environment.
447 //
448 // This method can only be called from the main thread.
449 Environment* AddEnvironment(Environment* env);
450
451 // Adds a TestPartResult to the current TestResult object. All
452 // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc)
453 // eventually call this to report their results. The user code
454 // should use the assertion macros instead of calling this directly.
455 //
456 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
457 void AddTestPartResult(TestPartResultType result_type,
458 const char* file_name,
459 int line_number,
460 const internal::String& message,
461 const internal::String& os_stack_trace);
462
463 // Adds a TestProperty to the current TestResult object. If the result already
464 // contains a property with the same key, the value will be updated.
465 void RecordPropertyForCurrentTest(const char* key, const char* value);
466
467 // Runs all tests in this UnitTest object and prints the result.
468 // Returns 0 if successful, or 1 otherwise.
469 //
470 // This method can only be called from the main thread.
471 //
472 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
473 int Run() GTEST_MUST_USE_RESULT_;
474
475 // Returns the working directory when the first TEST() or TEST_F()
476 // was executed. The UnitTest object owns the string.
477 const char* original_working_dir() const;
478
479 // Returns the TestCase object for the test that's currently running,
480 // or NULL if no test is running.
481 const TestCase* current_test_case() const;
482
483 // Returns the TestInfo object for the test that's currently running,
484 // or NULL if no test is running.
485 const TestInfo* current_test_info() const;
486
487#ifdef GTEST_HAS_PARAM_TEST
488 // Returns the ParameterizedTestCaseRegistry object used to keep track of
489 // value-parameterized tests and instantiate and register them.
490 internal::ParameterizedTestCaseRegistry& parameterized_test_registry();
491#endif // GTEST_HAS_PARAM_TEST
492
493 // Accessors for the implementation object.
494 internal::UnitTestImpl* impl() { return impl_; }
495 const internal::UnitTestImpl* impl() const { return impl_; }
496 private:
497 // ScopedTrace is a friend as it needs to modify the per-thread
498 // trace stack, which is a private member of UnitTest.
499 friend class internal::ScopedTrace;
500
501 // Creates an empty UnitTest.
502 UnitTest();
503
504 // D'tor
505 virtual ~UnitTest();
506
507 // Pushes a trace defined by SCOPED_TRACE() on to the per-thread
508 // Google Test trace stack.
509 void PushGTestTrace(const internal::TraceInfo& trace);
510
511 // Pops a trace from the per-thread Google Test trace stack.
512 void PopGTestTrace();
513
514 // Protects mutable state in *impl_. This is mutable as some const
515 // methods need to lock it too.
516 mutable internal::Mutex mutex_;
517
518 // Opaque implementation object. This field is never changed once
519 // the object is constructed. We don't mark it as const here, as
520 // doing so will cause a warning in the constructor of UnitTest.
521 // Mutable state in *impl_ is protected by mutex_.
522 internal::UnitTestImpl* impl_;
523
524 // We disallow copying UnitTest.
525 GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTest);
526};
527
528// A convenient wrapper for adding an environment for the test
529// program.
530//
531// You should call this before RUN_ALL_TESTS() is called, probably in
532// main(). If you use gtest_main, you need to call this before main()
533// starts for it to take effect. For example, you can define a global
534// variable like this:
535//
536// testing::Environment* const foo_env =
537// testing::AddGlobalTestEnvironment(new FooEnvironment);
538//
539// However, we strongly recommend you to write your own main() and
540// call AddGlobalTestEnvironment() there, as relying on initialization
541// of global variables makes the code harder to read and may cause
542// problems when you register multiple environments from different
543// translation units and the environments have dependencies among them
544// (remember that the compiler doesn't guarantee the order in which
545// global variables from different translation units are initialized).
546inline Environment* AddGlobalTestEnvironment(Environment* env) {
547 return UnitTest::GetInstance()->AddEnvironment(env);
548}
549
550// Initializes Google Test. This must be called before calling
551// RUN_ALL_TESTS(). In particular, it parses a command line for the
552// flags that Google Test recognizes. Whenever a Google Test flag is
553// seen, it is removed from argv, and *argc is decremented.
554//
555// No value is returned. Instead, the Google Test flag variables are
556// updated.
557//
558// Calling the function for the second time has no user-visible effect.
559void InitGoogleTest(int* argc, char** argv);
560
561// This overloaded version can be used in Windows programs compiled in
562// UNICODE mode.
563void InitGoogleTest(int* argc, wchar_t** argv);
564
565namespace internal {
566
567// These overloaded versions handle ::std::string and ::std::wstring.
568#if GTEST_HAS_STD_STRING
569inline String FormatForFailureMessage(const ::std::string& str) {
570 return (Message() << '"' << str << '"').GetString();
571}
572#endif // GTEST_HAS_STD_STRING
573
574#if GTEST_HAS_STD_WSTRING
575inline String FormatForFailureMessage(const ::std::wstring& wstr) {
576 return (Message() << "L\"" << wstr << '"').GetString();
577}
578#endif // GTEST_HAS_STD_WSTRING
579
580// These overloaded versions handle ::string and ::wstring.
581#if GTEST_HAS_GLOBAL_STRING
582inline String FormatForFailureMessage(const ::string& str) {
583 return (Message() << '"' << str << '"').GetString();
584}
585#endif // GTEST_HAS_GLOBAL_STRING
586
587#if GTEST_HAS_GLOBAL_WSTRING
588inline String FormatForFailureMessage(const ::wstring& wstr) {
589 return (Message() << "L\"" << wstr << '"').GetString();
590}
591#endif // GTEST_HAS_GLOBAL_WSTRING
592
593// Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
594// operand to be used in a failure message. The type (but not value)
595// of the other operand may affect the format. This allows us to
596// print a char* as a raw pointer when it is compared against another
597// char*, and print it as a C string when it is compared against an
598// std::string object, for example.
599//
600// The default implementation ignores the type of the other operand.
601// Some specialized versions are used to handle formatting wide or
602// narrow C strings.
603//
604// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
605template <typename T1, typename T2>
606String FormatForComparisonFailureMessage(const T1& value,
607 const T2& /* other_operand */) {
608 return FormatForFailureMessage(value);
609}
610
611// The helper function for {ASSERT|EXPECT}_EQ.
612template <typename T1, typename T2>
613AssertionResult CmpHelperEQ(const char* expected_expression,
614 const char* actual_expression,
615 const T1& expected,
616 const T2& actual) {
617 if (expected == actual) {
618 return AssertionSuccess();
619 }
620
621 return EqFailure(expected_expression,
622 actual_expression,
623 FormatForComparisonFailureMessage(expected, actual),
624 FormatForComparisonFailureMessage(actual, expected),
625 false);
626}
627
628// With this overloaded version, we allow anonymous enums to be used
629// in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous enums
630// can be implicitly cast to BiggestInt.
631AssertionResult CmpHelperEQ(const char* expected_expression,
632 const char* actual_expression,
633 BiggestInt expected,
634 BiggestInt actual);
635
636// The helper class for {ASSERT|EXPECT}_EQ. The template argument
637// lhs_is_null_literal is true iff the first argument to ASSERT_EQ()
638// is a null pointer literal. The following default implementation is
639// for lhs_is_null_literal being false.
640template <bool lhs_is_null_literal>
641class EqHelper {
642 public:
643 // This templatized version is for the general case.
644 template <typename T1, typename T2>
645 static AssertionResult Compare(const char* expected_expression,
646 const char* actual_expression,
647 const T1& expected,
648 const T2& actual) {
649 return CmpHelperEQ(expected_expression, actual_expression, expected,
650 actual);
651 }
652
653 // With this overloaded version, we allow anonymous enums to be used
654 // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous
655 // enums can be implicitly cast to BiggestInt.
656 //
657 // Even though its body looks the same as the above version, we
658 // cannot merge the two, as it will make anonymous enums unhappy.
659 static AssertionResult Compare(const char* expected_expression,
660 const char* actual_expression,
661 BiggestInt expected,
662 BiggestInt actual) {
663 return CmpHelperEQ(expected_expression, actual_expression, expected,
664 actual);
665 }
666};
667
668// This specialization is used when the first argument to ASSERT_EQ()
669// is a null pointer literal.
670template <>
671class EqHelper<true> {
672 public:
673 // We define two overloaded versions of Compare(). The first
674 // version will be picked when the second argument to ASSERT_EQ() is
675 // NOT a pointer, e.g. ASSERT_EQ(0, AnIntFunction()) or
676 // EXPECT_EQ(false, a_bool).
677 template <typename T1, typename T2>
678 static AssertionResult Compare(const char* expected_expression,
679 const char* actual_expression,
680 const T1& expected,
681 const T2& actual) {
682 return CmpHelperEQ(expected_expression, actual_expression, expected,
683 actual);
684 }
685
686 // This version will be picked when the second argument to
687 // ASSERT_EQ() is a pointer, e.g. ASSERT_EQ(NULL, a_pointer).
688 template <typename T1, typename T2>
689 static AssertionResult Compare(const char* expected_expression,
690 const char* actual_expression,
691 const T1& expected,
692 T2* actual) {
693 // We already know that 'expected' is a null pointer.
694 return CmpHelperEQ(expected_expression, actual_expression,
695 static_cast<T2*>(NULL), actual);
696 }
697};
698
699// A macro for implementing the helper functions needed to implement
700// ASSERT_?? and EXPECT_??. It is here just to avoid copy-and-paste
701// of similar code.
702//
703// For each templatized helper function, we also define an overloaded
704// version for BiggestInt in order to reduce code bloat and allow
705// anonymous enums to be used with {ASSERT|EXPECT}_?? when compiled
706// with gcc 4.
707//
708// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
709#define GTEST_IMPL_CMP_HELPER_(op_name, op)\
710template <typename T1, typename T2>\
711AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
712 const T1& val1, const T2& val2) {\
713 if (val1 op val2) {\
714 return AssertionSuccess();\
715 } else {\
716 Message msg;\
717 msg << "Expected: (" << expr1 << ") " #op " (" << expr2\
718 << "), actual: " << FormatForComparisonFailureMessage(val1, val2)\
719 << " vs " << FormatForComparisonFailureMessage(val2, val1);\
720 return AssertionFailure(msg);\
721 }\
722}\
723AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
724 BiggestInt val1, BiggestInt val2);
725
726// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
727
728// Implements the helper function for {ASSERT|EXPECT}_NE
729GTEST_IMPL_CMP_HELPER_(NE, !=)
730// Implements the helper function for {ASSERT|EXPECT}_LE
731GTEST_IMPL_CMP_HELPER_(LE, <=)
732// Implements the helper function for {ASSERT|EXPECT}_LT
733GTEST_IMPL_CMP_HELPER_(LT, < )
734// Implements the helper function for {ASSERT|EXPECT}_GE
735GTEST_IMPL_CMP_HELPER_(GE, >=)
736// Implements the helper function for {ASSERT|EXPECT}_GT
737GTEST_IMPL_CMP_HELPER_(GT, > )
738
739#undef GTEST_IMPL_CMP_HELPER_
740
741// The helper function for {ASSERT|EXPECT}_STREQ.
742//
743// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
744AssertionResult CmpHelperSTREQ(const char* expected_expression,
745 const char* actual_expression,
746 const char* expected,
747 const char* actual);
748
749// The helper function for {ASSERT|EXPECT}_STRCASEEQ.
750//
751// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
752AssertionResult CmpHelperSTRCASEEQ(const char* expected_expression,
753 const char* actual_expression,
754 const char* expected,
755 const char* actual);
756
757// The helper function for {ASSERT|EXPECT}_STRNE.
758//
759// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
760AssertionResult CmpHelperSTRNE(const char* s1_expression,
761 const char* s2_expression,
762 const char* s1,
763 const char* s2);
764
765// The helper function for {ASSERT|EXPECT}_STRCASENE.
766//
767// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
768AssertionResult CmpHelperSTRCASENE(const char* s1_expression,
769 const char* s2_expression,
770 const char* s1,
771 const char* s2);
772
773
774// Helper function for *_STREQ on wide strings.
775//
776// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
777AssertionResult CmpHelperSTREQ(const char* expected_expression,
778 const char* actual_expression,
779 const wchar_t* expected,
780 const wchar_t* actual);
781
782// Helper function for *_STRNE on wide strings.
783//
784// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
785AssertionResult CmpHelperSTRNE(const char* s1_expression,
786 const char* s2_expression,
787 const wchar_t* s1,
788 const wchar_t* s2);
789
790} // namespace internal
791
792// IsSubstring() and IsNotSubstring() are intended to be used as the
793// first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by
794// themselves. They check whether needle is a substring of haystack
795// (NULL is considered a substring of itself only), and return an
796// appropriate error message when they fail.
797//
798// The {needle,haystack}_expr arguments are the stringified
799// expressions that generated the two real arguments.
800AssertionResult IsSubstring(
801 const char* needle_expr, const char* haystack_expr,
802 const char* needle, const char* haystack);
803AssertionResult IsSubstring(
804 const char* needle_expr, const char* haystack_expr,
805 const wchar_t* needle, const wchar_t* haystack);
806AssertionResult IsNotSubstring(
807 const char* needle_expr, const char* haystack_expr,
808 const char* needle, const char* haystack);
809AssertionResult IsNotSubstring(
810 const char* needle_expr, const char* haystack_expr,
811 const wchar_t* needle, const wchar_t* haystack);
812#if GTEST_HAS_STD_STRING
813AssertionResult IsSubstring(
814 const char* needle_expr, const char* haystack_expr,
815 const ::std::string& needle, const ::std::string& haystack);
816AssertionResult IsNotSubstring(
817 const char* needle_expr, const char* haystack_expr,
818 const ::std::string& needle, const ::std::string& haystack);
819#endif // GTEST_HAS_STD_STRING
820
821#if GTEST_HAS_STD_WSTRING
822AssertionResult IsSubstring(
823 const char* needle_expr, const char* haystack_expr,
824 const ::std::wstring& needle, const ::std::wstring& haystack);
825AssertionResult IsNotSubstring(
826 const char* needle_expr, const char* haystack_expr,
827 const ::std::wstring& needle, const ::std::wstring& haystack);
828#endif // GTEST_HAS_STD_WSTRING
829
830namespace internal {
831
832// Helper template function for comparing floating-points.
833//
834// Template parameter:
835//
836// RawType: the raw floating-point type (either float or double)
837//
838// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
839template <typename RawType>
840AssertionResult CmpHelperFloatingPointEQ(const char* expected_expression,
841 const char* actual_expression,
842 RawType expected,
843 RawType actual) {
844 const FloatingPoint<RawType> lhs(expected), rhs(actual);
845
846 if (lhs.AlmostEquals(rhs)) {
847 return AssertionSuccess();
848 }
849
850 StrStream expected_ss;
851 expected_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
852 << expected;
853
854 StrStream actual_ss;
855 actual_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
856 << actual;
857
858 return EqFailure(expected_expression,
859 actual_expression,
860 StrStreamToString(&expected_ss),
861 StrStreamToString(&actual_ss),
862 false);
863}
864
865// Helper function for implementing ASSERT_NEAR.
866//
867// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
868AssertionResult DoubleNearPredFormat(const char* expr1,
869 const char* expr2,
870 const char* abs_error_expr,
871 double val1,
872 double val2,
873 double abs_error);
874
875// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
876// A class that enables one to stream messages to assertion macros
877class AssertHelper {
878 public:
879 // Constructor.
880 AssertHelper(TestPartResultType type, const char* file, int line,
881 const char* message);
882 // Message assignment is a semantic trick to enable assertion
883 // streaming; see the GTEST_MESSAGE_ macro below.
884 void operator=(const Message& message) const;
885 private:
886 TestPartResultType const type_;
887 const char* const file_;
888 int const line_;
889 String const message_;
890
891 GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelper);
892};
893
894} // namespace internal
895
896#ifdef GTEST_HAS_PARAM_TEST
897// The abstract base class that all value-parameterized tests inherit from.
898//
899// This class adds support for accessing the test parameter value via
900// the GetParam() method.
901//
902// Use it with one of the parameter generator defining functions, like Range(),
903// Values(), ValuesIn(), Bool(), and Combine().
904//
905// class FooTest : public ::testing::TestWithParam<int> {
906// protected:
907// FooTest() {
908// // Can use GetParam() here.
909// }
910// virtual ~FooTest() {
911// // Can use GetParam() here.
912// }
913// virtual void SetUp() {
914// // Can use GetParam() here.
915// }
916// virtual void TearDown {
917// // Can use GetParam() here.
918// }
919// };
920// TEST_P(FooTest, DoesBar) {
921// // Can use GetParam() method here.
922// Foo foo;
923// ASSERT_TRUE(foo.DoesBar(GetParam()));
924// }
925// INSTANTIATE_TEST_CASE_P(OneToTenRange, FooTest, ::testing::Range(1, 10));
926
927template <typename T>
928class TestWithParam : public Test {
929 public:
930 typedef T ParamType;
931
932 // The current parameter value. Is also available in the test fixture's
933 // constructor.
934 const ParamType& GetParam() const { return *parameter_; }
935
936 private:
937 // Sets parameter value. The caller is responsible for making sure the value
938 // remains alive and unchanged throughout the current test.
939 static void SetParam(const ParamType* parameter) {
940 parameter_ = parameter;
941 }
942
943 // Static value used for accessing parameter during a test lifetime.
944 static const ParamType* parameter_;
945
946 // TestClass must be a subclass of TestWithParam<T>.
947 template <class TestClass> friend class internal::ParameterizedTestFactory;
948};
949
950template <typename T>
951const T* TestWithParam<T>::parameter_ = NULL;
952
953#endif // GTEST_HAS_PARAM_TEST
954
955// Macros for indicating success/failure in test code.
956
957// ADD_FAILURE unconditionally adds a failure to the current test.
958// SUCCEED generates a success - it doesn't automatically make the
959// current test successful, as a test is only successful when it has
960// no failure.
961//
962// EXPECT_* verifies that a certain condition is satisfied. If not,
963// it behaves like ADD_FAILURE. In particular:
964//
965// EXPECT_TRUE verifies that a Boolean condition is true.
966// EXPECT_FALSE verifies that a Boolean condition is false.
967//
968// FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except
969// that they will also abort the current function on failure. People
970// usually want the fail-fast behavior of FAIL and ASSERT_*, but those
971// writing data-driven tests often find themselves using ADD_FAILURE
972// and EXPECT_* more.
973//
974// Examples:
975//
976// EXPECT_TRUE(server.StatusIsOK());
977// ASSERT_FALSE(server.HasPendingRequest(port))
978// << "There are still pending requests " << "on port " << port;
979
980// Generates a nonfatal failure with a generic message.
981#define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed")
982
983// Generates a fatal failure with a generic message.
984#define FAIL() GTEST_FATAL_FAILURE_("Failed")
985
986// Generates a success with a generic message.
987#define SUCCEED() GTEST_SUCCESS_("Succeeded")
988
989// Macros for testing exceptions.
990//
991// * {ASSERT|EXPECT}_THROW(statement, expected_exception):
992// Tests that the statement throws the expected exception.
993// * {ASSERT|EXPECT}_NO_THROW(statement):
994// Tests that the statement doesn't throw any exception.
995// * {ASSERT|EXPECT}_ANY_THROW(statement):
996// Tests that the statement throws an exception.
997
998#define EXPECT_THROW(statement, expected_exception) \
999 GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_)
1000#define EXPECT_NO_THROW(statement) \
1001 GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_)
1002#define EXPECT_ANY_THROW(statement) \
1003 GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_)
1004#define ASSERT_THROW(statement, expected_exception) \
1005 GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_)
1006#define ASSERT_NO_THROW(statement) \
1007 GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_)
1008#define ASSERT_ANY_THROW(statement) \
1009 GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_)
1010
1011// Boolean assertions.
1012#define EXPECT_TRUE(condition) \
1013 GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \
1014 GTEST_NONFATAL_FAILURE_)
1015#define EXPECT_FALSE(condition) \
1016 GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \
1017 GTEST_NONFATAL_FAILURE_)
1018#define ASSERT_TRUE(condition) \
1019 GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \
1020 GTEST_FATAL_FAILURE_)
1021#define ASSERT_FALSE(condition) \
1022 GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \
1023 GTEST_FATAL_FAILURE_)
1024
1025// Includes the auto-generated header that implements a family of
1026// generic predicate assertion macros.
1027#include <gtest/gtest_pred_impl.h>
1028
1029// Macros for testing equalities and inequalities.
1030//
1031// * {ASSERT|EXPECT}_EQ(expected, actual): Tests that expected == actual
1032// * {ASSERT|EXPECT}_NE(v1, v2): Tests that v1 != v2
1033// * {ASSERT|EXPECT}_LT(v1, v2): Tests that v1 < v2
1034// * {ASSERT|EXPECT}_LE(v1, v2): Tests that v1 <= v2
1035// * {ASSERT|EXPECT}_GT(v1, v2): Tests that v1 > v2
1036// * {ASSERT|EXPECT}_GE(v1, v2): Tests that v1 >= v2
1037//
1038// When they are not, Google Test prints both the tested expressions and
1039// their actual values. The values must be compatible built-in types,
1040// or you will get a compiler error. By "compatible" we mean that the
1041// values can be compared by the respective operator.
1042//
1043// Note:
1044//
1045// 1. It is possible to make a user-defined type work with
1046// {ASSERT|EXPECT}_??(), but that requires overloading the
1047// comparison operators and is thus discouraged by the Google C++
1048// Usage Guide. Therefore, you are advised to use the
1049// {ASSERT|EXPECT}_TRUE() macro to assert that two objects are
1050// equal.
1051//
1052// 2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on
1053// pointers (in particular, C strings). Therefore, if you use it
1054// with two C strings, you are testing how their locations in memory
1055// are related, not how their content is related. To compare two C
1056// strings by content, use {ASSERT|EXPECT}_STR*().
1057//
1058// 3. {ASSERT|EXPECT}_EQ(expected, actual) is preferred to
1059// {ASSERT|EXPECT}_TRUE(expected == actual), as the former tells you
1060// what the actual value is when it fails, and similarly for the
1061// other comparisons.
1062//
1063// 4. Do not depend on the order in which {ASSERT|EXPECT}_??()
1064// evaluate their arguments, which is undefined.
1065//
1066// 5. These macros evaluate their arguments exactly once.
1067//
1068// Examples:
1069//
1070// EXPECT_NE(5, Foo());
1071// EXPECT_EQ(NULL, a_pointer);
1072// ASSERT_LT(i, array_size);
1073// ASSERT_GT(records.size(), 0) << "There is no record left.";
1074
1075#define EXPECT_EQ(expected, actual) \
1076 EXPECT_PRED_FORMAT2(::testing::internal:: \
1077 EqHelper<GTEST_IS_NULL_LITERAL_(expected)>::Compare, \
1078 expected, actual)
1079#define EXPECT_NE(expected, actual) \
1080 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, expected, actual)
1081#define EXPECT_LE(val1, val2) \
1082 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
1083#define EXPECT_LT(val1, val2) \
1084 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)
1085#define EXPECT_GE(val1, val2) \
1086 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
1087#define EXPECT_GT(val1, val2) \
1088 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
1089
1090#define ASSERT_EQ(expected, actual) \
1091 ASSERT_PRED_FORMAT2(::testing::internal:: \
1092 EqHelper<GTEST_IS_NULL_LITERAL_(expected)>::Compare, \
1093 expected, actual)
1094#define ASSERT_NE(val1, val2) \
1095 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)
1096#define ASSERT_LE(val1, val2) \
1097 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
1098#define ASSERT_LT(val1, val2) \
1099 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)
1100#define ASSERT_GE(val1, val2) \
1101 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
1102#define ASSERT_GT(val1, val2) \
1103 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
1104
1105// C String Comparisons. All tests treat NULL and any non-NULL string
1106// as different. Two NULLs are equal.
1107//
1108// * {ASSERT|EXPECT}_STREQ(s1, s2): Tests that s1 == s2
1109// * {ASSERT|EXPECT}_STRNE(s1, s2): Tests that s1 != s2
1110// * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case
1111// * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case
1112//
1113// For wide or narrow string objects, you can use the
1114// {ASSERT|EXPECT}_??() macros.
1115//
1116// Don't depend on the order in which the arguments are evaluated,
1117// which is undefined.
1118//
1119// These macros evaluate their arguments exactly once.
1120
1121#define EXPECT_STREQ(expected, actual) \
1122 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual)
1123#define EXPECT_STRNE(s1, s2) \
1124 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)
1125#define EXPECT_STRCASEEQ(expected, actual) \
1126 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual)
1127#define EXPECT_STRCASENE(s1, s2)\
1128 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)
1129
1130#define ASSERT_STREQ(expected, actual) \
1131 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual)
1132#define ASSERT_STRNE(s1, s2) \
1133 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)
1134#define ASSERT_STRCASEEQ(expected, actual) \
1135 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual)
1136#define ASSERT_STRCASENE(s1, s2)\
1137 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)
1138
1139// Macros for comparing floating-point numbers.
1140//
1141// * {ASSERT|EXPECT}_FLOAT_EQ(expected, actual):
1142// Tests that two float values are almost equal.
1143// * {ASSERT|EXPECT}_DOUBLE_EQ(expected, actual):
1144// Tests that two double values are almost equal.
1145// * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error):
1146// Tests that v1 and v2 are within the given distance to each other.
1147//
1148// Google Test uses ULP-based comparison to automatically pick a default
1149// error bound that is appropriate for the operands. See the
1150// FloatingPoint template class in gtest-internal.h if you are
1151// interested in the implementation details.
1152
1153#define EXPECT_FLOAT_EQ(expected, actual)\
1154 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \
1155 expected, actual)
1156
1157#define EXPECT_DOUBLE_EQ(expected, actual)\
1158 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \
1159 expected, actual)
1160
1161#define ASSERT_FLOAT_EQ(expected, actual)\
1162 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \
1163 expected, actual)
1164
1165#define ASSERT_DOUBLE_EQ(expected, actual)\
1166 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \
1167 expected, actual)
1168
1169#define EXPECT_NEAR(val1, val2, abs_error)\
1170 EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \
1171 val1, val2, abs_error)
1172
1173#define ASSERT_NEAR(val1, val2, abs_error)\
1174 ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \
1175 val1, val2, abs_error)
1176
1177// These predicate format functions work on floating-point values, and
1178// can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g.
1179//
1180// EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0);
1181
1182// Asserts that val1 is less than, or almost equal to, val2. Fails
1183// otherwise. In particular, it fails if either val1 or val2 is NaN.
1184AssertionResult FloatLE(const char* expr1, const char* expr2,
1185 float val1, float val2);
1186AssertionResult DoubleLE(const char* expr1, const char* expr2,
1187 double val1, double val2);
1188
1189
1190#ifdef GTEST_OS_WINDOWS
1191
1192// Macros that test for HRESULT failure and success, these are only useful
1193// on Windows, and rely on Windows SDK macros and APIs to compile.
1194//
1195// * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr)
1196//
1197// When expr unexpectedly fails or succeeds, Google Test prints the
1198// expected result and the actual result with both a human-readable
1199// string representation of the error, if available, as well as the
1200// hex result code.
1201#define EXPECT_HRESULT_SUCCEEDED(expr) \
1202 EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))
1203
1204#define ASSERT_HRESULT_SUCCEEDED(expr) \
1205 ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))
1206
1207#define EXPECT_HRESULT_FAILED(expr) \
1208 EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))
1209
1210#define ASSERT_HRESULT_FAILED(expr) \
1211 ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))
1212
1213#endif // GTEST_OS_WINDOWS
1214
1215// Macros that execute statement and check that it doesn't generate new fatal
1216// failures in the current thread.
1217//
1218// * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement);
1219//
1220// Examples:
1221//
1222// EXPECT_NO_FATAL_FAILURE(Process());
1223// ASSERT_NO_FATAL_FAILURE(Process()) << "Process() failed";
1224//
1225#define ASSERT_NO_FATAL_FAILURE(statement) \
1226 GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_FATAL_FAILURE_)
1227#define EXPECT_NO_FATAL_FAILURE(statement) \
1228 GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_NONFATAL_FAILURE_)
1229
1230// Causes a trace (including the source file path, the current line
1231// number, and the given message) to be included in every test failure
1232// message generated by code in the current scope. The effect is
1233// undone when the control leaves the current scope.
1234//
1235// The message argument can be anything streamable to std::ostream.
1236//
1237// In the implementation, we include the current line number as part
1238// of the dummy variable name, thus allowing multiple SCOPED_TRACE()s
1239// to appear in the same block - as long as they are on different
1240// lines.
1241#define SCOPED_TRACE(message) \
1242 ::testing::internal::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)(\
1243 __FILE__, __LINE__, ::testing::Message() << (message))
1244
1245
1246// Defines a test.
1247//
1248// The first parameter is the name of the test case, and the second
1249// parameter is the name of the test within the test case.
1250//
1251// The convention is to end the test case name with "Test". For
1252// example, a test case for the Foo class can be named FooTest.
1253//
1254// The user should put his test code between braces after using this
1255// macro. Example:
1256//
1257// TEST(FooTest, InitializesCorrectly) {
1258// Foo foo;
1259// EXPECT_TRUE(foo.StatusIsOK());
1260// }
1261
1262// Note that we call GetTestTypeId() instead of GetTypeId<
1263// ::testing::Test>() here to get the type ID of testing::Test. This
1264// is to work around a suspected linker bug when using Google Test as
1265// a framework on Mac OS X. The bug causes GetTypeId<
1266// ::testing::Test>() to return different values depending on whether
1267// the call is from the Google Test framework itself or from user test
1268// code. GetTestTypeId() is guaranteed to always return the same
1269// value, as it always calls GetTypeId<>() from the Google Test
1270// framework.
1271#define TEST(test_case_name, test_name)\
1272 GTEST_TEST_(test_case_name, test_name,\
1273 ::testing::Test, ::testing::internal::GetTestTypeId())
1274
1275
1276// Defines a test that uses a test fixture.
1277//
1278// The first parameter is the name of the test fixture class, which
1279// also doubles as the test case name. The second parameter is the
1280// name of the test within the test case.
1281//
1282// A test fixture class must be declared earlier. The user should put
1283// his test code between braces after using this macro. Example:
1284//
1285// class FooTest : public testing::Test {
1286// protected:
1287// virtual void SetUp() { b_.AddElement(3); }
1288//
1289// Foo a_;
1290// Foo b_;
1291// };
1292//
1293// TEST_F(FooTest, InitializesCorrectly) {
1294// EXPECT_TRUE(a_.StatusIsOK());
1295// }
1296//
1297// TEST_F(FooTest, ReturnsElementCountCorrectly) {
1298// EXPECT_EQ(0, a_.size());
1299// EXPECT_EQ(1, b_.size());
1300// }
1301
1302#define TEST_F(test_fixture, test_name)\
1303 GTEST_TEST_(test_fixture, test_name, test_fixture,\
1304 ::testing::internal::GetTypeId<test_fixture>())
1305
1306// Use this macro in main() to run all tests. It returns 0 if all
1307// tests are successful, or 1 otherwise.
1308//
1309// RUN_ALL_TESTS() should be invoked after the command line has been
1310// parsed by InitGoogleTest().
1311
1312#define RUN_ALL_TESTS()\
1313 (::testing::UnitTest::GetInstance()->Run())
1314
1315} // namespace testing
1316
1317#endif // GTEST_INCLUDE_GTEST_GTEST_H_